fix(bash): memory leak - release parsed syntax trees (#24861)

Co-authored-by: jiwenshang <jiwenshang@xiaohongshu.com>
This commit is contained in:
Luke Parker 2026-04-29 09:10:48 +10:00 committed by GitHub
parent ae8904c4ff
commit d4bf70be06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -252,7 +252,7 @@ function tail(text: string, maxLines: number, maxBytes: number) {
const parse = Effect.fn("BashTool.parse")(function* (command: string, ps: boolean) {
const tree = yield* Effect.promise(() => parser().then((p) => (ps ? p.ps : p.bash).parse(command)))
if (!tree) throw new Error("Failed to parse command")
return tree.rootNode
return tree
})
const ask = Effect.fn("BashTool.ask")(function* (ctx: Tool.Context, scan: Scan) {
@ -596,10 +596,17 @@ export const BashTool = Tool.define(
}
const timeout = params.timeout ?? DEFAULT_TIMEOUT
const ps = Shell.ps(shell)
const root = yield* parse(params.command, ps)
const scan = yield* collect(root, cwd, ps, shell)
if (!Instance.containsPath(cwd)) scan.dirs.add(cwd)
yield* ask(ctx, scan)
yield* Effect.scoped(
Effect.gen(function* () {
const tree = yield* Effect.acquireRelease(
parse(params.command, ps),
(tree) => Effect.sync(() => tree.delete()),
)
const scan = yield* collect(tree.rootNode, cwd, ps, shell)
if (!Instance.containsPath(cwd)) scan.dirs.add(cwd)
yield* ask(ctx, scan)
}),
)
return yield* run(
{