Skip to content

fix(server): a later Claude wake's approval stays out of a paused Monitor continuation - #37431

Open
Motaghi wants to merge 1 commit into
pingdotgg:mainfrom
Motaghi:behmot/claude-direct-entry-buffered-tool
Open

Motaghi wants to merge 1 commit into
pingdotgg:mainfrom
Motaghi:behmot/claude-direct-entry-buffered-tool

Conversation

@Motaghi

@Motaghi Motaghi commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Problem

When Claude wakes on its own, the adapter buffers the wake and asks for a continuation run. That run takes the SDK message lock, attaches its turn, and replays the buffer. Since #36339, a Monitor-event continuation replays only its own wake and leaves a later wake buffered for a continuation of its own. The replay can suspend on workflow transcript reads, and the SDK calls canUseTool from its own read loop, so Claude can ask for the later wake's tool during that pause. canUseToolEffect took the attached turn at once, so the approval appeared in the Monitor run while the tool itself later appeared in the later wake's run.

Change

When a turn is attached and its Claude session still has a wake buffer, canUseTool waits for the replay (the SDK message lock, raced against the request's withdrawal), then reads the active turn again. If the request's tool_use is still buffered, it belongs to a wake this turn did not replay, and the request is denied as it would be with no active turn. The lock is released before the approval is raised or awaited. With no wake buffer for the session, nothing changes.

Scope

No separate issue: CodeRabbit found this race while reviewing #36740 (#36740 (comment)), and it is on main independently of that PR. The new test reproduces it deterministically. #36740 adds the same two small helpers (awaitAbortSignal, isToolUseBuffered) with identical bodies, so whichever lands second has a trivial conflict. This PR does not depend on #36740.

Verification

  • New ClaudeAdapterV2.test.ts case: a workflow finishes inside a Monitor event wake, and a later wake's Bash tool_use waits behind it. The Monitor continuation's replay is paused through WorkflowAgentTranscriptReadObserver, and Claude asks for the Bash approval during the pause. On main, the approval request is raised in the Monitor run (the test fails at the runtime_request.updated assertion). With the fix, the request is denied, and the Bash item appears only in the later wake's own continuation run.
  • The wake, Monitor, workflow, replay and approval tests in ClaudeAdapterV2.test.ts pass (375).
  • vp run check:changed passes; ClaudeAutomaticDelivery.integration.test.ts, RestartContinuation.test.ts, the Claude replay fixtures and ProviderSwitch.integration.test.ts pass.
  • Not checked in a running app: the race needs a transcript read to pause a replay at the moment Claude asks for a tool, which I could not trigger on demand with live Claude turns.

Written with Claude Opus 5.5 in Claude Code, reviewed by gpt-6.1-sol in Codex; I checked the evidence and the change.

…itor continuation

A continuation attaches its turn under the SDK message lock and then
replays the wake buffer, which can suspend on workflow transcript reads.
Claude can ask for a later wake's tool meanwhile, and canUseTool took the
attached turn at once, so the approval landed in the Monitor run while the
tool stayed buffered for the later wake's own continuation.

When the attached turn's session still has a wake buffer, canUseTool now
waits for the lock (or the request's withdrawal), and denies as before if
the tool's tool_use is still buffered after the replay.
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Nov 16, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 5cec1f4

Macroscope's review found this PR approvable — The adapter now waits for wake replay before assigning tool approvals, keeping later buffered wake requests out of a paused Monitor continuation. Existing approval policies remain unchanged, and unmatched requests use the existing deny path.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The adapter now checks whether a tool request remains buffered during wake-buffer replay before resolving its active turn. The check can end when the callback’s abort signal fires. A replay test covers a buffered Bash request across a Monitor continuation and the next continuation.

Changes: Buffered tool handling

Layer / File(s) Summary
Active turn resolution
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts, apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
The adapter checks the wake buffer under sdkMessageLock and returns no active turn when the requested tool use is still buffered or the callback aborts. The test checks that the Bash request does not produce an approval event or tool item in the Monitor continuation, and that the next continuation receives the tool item.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: juliusmarminge, macroscopeapp, maria-rcks

Merge Risk: Minimal · up to 5cec1

This change stops a tool approval for a later wake from appearing in an earlier Monitor continuation while replay is paused. Requests that stay buffered are denied, and the new test covers the race. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a fix in server code so a later Claude wake's approval request does not leak into a paused Monitor continuation. The PR description and summary confirm that canUseTool…
Description check Passed The description follows the template sections: Problem, Change, Scope, and Verification. The Problem section explains the race and expected behavior. The Change section describes the fix and the lock …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a fix in server code so a later Claude wake's approval request does not leak into a paused Monitor continuation. The PR description and summary confirm that canUseTool now waits for replay and denies buffered tool_use requests. The title is specific and follows a conventional commit format.

Full details: Description check

Explanation

The description follows the template sections: Problem, Change, Scope, and Verification. The Problem section explains the race and expected behavior. The Change section describes the fix and the lock release. The Scope section explains why no issue is linked and cites the related discussion. The Verification section lists a new focused test, the observed failure on main, the passing test counts, and what was not checked in a running app. The opening HTML comment block from the template is absent, and the description does not state a conventional-commit title in that section, but the title itself meets that requirement. The description also names the model and harness used, as the template requests.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6b7027b and 5cec1f4.

Files selected for processing: 2

  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant