Skip to content

Agent initialization race: attachment save fails with deny-all permission when canonical tool runs before plugin/agent readiness #700

Closed

Description

@agenteinstinct1-max

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)

  1. packages/core/src/plugin/internal.ts:110-129 runs PluginInternal.boot via Effect.forkScoped; location-service construction does not wait for plugin initialization.
  2. State.batch (packages/core/src/state.ts:33-40) postpones state reloads until plugin registration finishes.
  3. AgentV2 therefore starts with an empty agent map (packages/core/src/agent.ts:55-56).
  4. The attachment compatibility wrapper can acquire and execute the canonical tool inside this window.
  5. PermissionV2.configured calls agents.resolve("work"), receives undefined, and selects [{ action: "*", resource: "*", effect: "deny" }] (packages/core/src/permission.ts:15,143-150).
  6. AttachmentStore.copyTo checks this permission before its final write; the denial becomes a canonical error result, an AI-SDK tool-error, and the persisted save.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

  1. added a commit that references this issue on Oct 30, 2026
    3b91864
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions