Repository navigation
Description
Summary
Under CPU contention, the attachment-save path can execute the canonical tool before plugin/agent initialization completes. PermissionV2.configured then resolves the "work" agent from an empty agent map and falls back to a deny-all rule, so AttachmentStore.copyTo fails before its final write and the persisted tool state becomes status: "error". Observed in CI as test/session/prompt.test.ts:1511 "attachment save copies managed bytes after edit authorization" expecting save.state.status "completed" but getting "error" (e.g. run 40957853540, unit linux, ~1000ms body duration).
Verified mechanism (directed diagnosis, read-only)
packages/core/src/plugin/internal.ts:110-129runsPluginInternal.bootviaEffect.forkScoped; location-service construction does not wait for plugin initialization.State.batch(packages/core/src/state.ts:33-40) postpones state reloads until plugin registration finishes.AgentV2therefore starts with an empty agent map (packages/core/src/agent.ts:55-56).- The attachment compatibility wrapper can acquire and execute the canonical tool inside this window.
PermissionV2.configuredcallsagents.resolve("work"), receivesundefined, and selects[{ action: "*", resource: "*", effect: "deny" }](packages/core/src/permission.ts:15,143-150).AttachmentStore.copyTochecks this permission before its final write; the denial becomes a canonical error result, an AI-SDK tool-error, and the persistedsave.state.status === "error".
Filesystem observation in a reproduced failing iteration: the destination file was never created (pre-write failure, not a completed copy later overwritten, not "Tool execution aborted"). The captured failure completed in 158ms; there is no one-second deadline on this path (the ~1s CI body duration is consistent with a fast failure, not a timer).
Reproduction (failing-first, both commits)
- Isolated, unloaded: 21/21 pass at base 0f7d647 AND at head 895923c.
- Full
prompt.test.ts: 136 pass / 1 skip / 0 fail at both commits. - With four external CPU-competitor processes: base 9/10 pass (1 fail), head 7/10 pass (3 fail) - identical assertion at both commits.
- Persisted error recovered from a loaded head run:
Permission blocked by rule: {"action":"*","resource":"*","effect":"deny"}(tool "attachment", 158ms). - Command:
bun test test/session/prompt.test.ts --timeout 30000 --test-name-pattern '^attachment save copies managed bytes after edit authorization$'.
Caveat: the exact CI-side state.error text is inferred (the diagnosing sandbox could not fetch the raw CI log); the mechanism and the identical assertion failure are directly verified locally. Small loaded samples do not establish different failure rates between the two commits - this is a pre-existing race, not a regression from the CLI dedupe work in #697.
Fix direction
Make canonical tool execution wait for plugin/agent readiness (e.g. have the V1↔V2 bridge/compat wrapper await agent-map population before executing, or gate PermissionV2.configured on initialization), so a tool can never run against the deny-all fallback of an uninitialized agent map. Do not paper over this with test timeouts or retries.
Activity
fix(cli): readiness wait for session bootstrap before run (#701)