Skip to content

agentHost: log why a Claude session rebuilds before a turn - #346442

Merged
Bryan Chen (bryanchen-d) merged 2 commits into
mainfrom
copilot/vscode/claude-rebuild-reason-log
Nov 6, 2026
Merged

Bryan Chen (bryanchen-d) merged 2 commits into
mainfrom
copilot/vscode/claude-rebuild-reason-log

Conversation

@bryanchen-d

@bryanchen-d Bryan Chen (bryanchen-d) commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Logs which synced state made ClaudeAgentSession.send rebuild the SDK subprocess before a turn. Today it rebuilds silently. The log is the only change. The conditions move into _getRebindReason(), which checks them in the same order and returns at the first one that differs, so evaluation still stops early like the original || chain.

TylerLeonhardt FYI, since this is your area. I'm happy to close this if you'd rather handle it another way.

Why: Agent Host E2E — Claude › model changes between turns retain provider context has failed twice on main recently, both on a busy agent right after a 6–7 s cold Copilot client start:

  • 493113 (macOS arm64). An extra request shifted the replay, and turn 2 stalled for 90 s.
  • 493539 (macOS arm64). The test body passed with MARIGOLD remembered, but the after each replay check failed: model request #2 does not match the recorded request. The recorded request gulp-symdest does not preserve links on electron #2 contains the /model <local-command-caveat> echo between the turns, and the live request doesn't.

In 493539, turn 2's send logged rebuild isResume=true at 07.707, right after turn 1's result. That's the pre-flight rebuild in send (synced-state drift), not the model switch. The rebuild launches the SDK with Options.model already set to the new model, so _replayCurrentConfig has nothing to switch and the SDK never writes the /model echo. Without a rebuild (the recorded path), turn 2 hot-swaps through Query.setModel, which does write it. Both paths keep the provider context. Only the replayed request differs, so the test fails whenever some synced state lands between turn 1's launch and turn 2, which a slow cold start makes likely.

We couldn't tell which state changed, because the rebind doesn't log its reason. With this change, the next hit will log, for example:

[Claude] session <id>: rebinding before turn turn-model-change-second for changed client tools

Overlap: #345986 rewrites this same hunk with _pendingRebuildReasons() and logs rebuilding before turn <id>: <reasons>, which covers this PR. If #345986 lands first, I'll close this one. This PR only matters if the rebuild reason is needed before #345986 lands.

Open question for you: once we know the trigger, should the fixture tolerate a rebuild before turn 2, or should the test wait until synced state settles before turn 1?

Validation: claudeAgent.test.ts and claudeSdkPipeline.test.ts pass (352) on the current head. Earlier, claudeSessionClientToolsModel.test.ts (14) and claudeSessionClientCustomizationsModel.test.ts (11) passed too. eslint is clean, and tsc --noEmit reports no errors in the Claude agent host files.

Authored with Copilot

Copilot AI balanced review requested due to automatic review settings November 6, 2026 03:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the change adding diagnostic logging to pre-turn rebuilds.

No open issues 🎉

What changed in this PR

Adds diagnostic logging to Claude session pre-turn rebuilds, making it easier to identify which synced-state changes triggered an SDK subprocess restart.

Changes:

  • Evaluates all existing rebuild conditions to collect every applicable reason.
  • Logs the session ID, turn ID, and reasons before invoking the existing rebuild path.
File Description
src/​vs/​platform/​agentHost/​node/​claude/​claudeAgentSession.ts Collects and logs pre-turn rebuild reasons while retaining the existing rebuild conditions.

Review effort: Balanced

@bryanchen-d

Copy link
Copy Markdown
Collaborator Author

Shouldn't it be 'rebindReason' as it is rebinding the claude session? also the way you changed it to have a array of each condition check, that will leads to evaluation of every condition unlike previously it may skip the rest of condition checks when one of them is true.

Co-authored with Copilot

@bryanchen-d

Copy link
Copy Markdown
Collaborator Author

Both fixed in ca9339c235:

  • The names now say rebind: rebindReason, _getRebindReason(), and the log line reads rebinding before turn ... for changed <reason>.
  • The checks moved into _getRebindReason(), which runs them in the original order and returns at the first difference. So later conditions are skipped again, like the || chain. The log names only the first reason. That's enough to tell what drifted.

claudeAgent.test.ts and claudeSdkPipeline.test.ts pass (352), and tsc is clean.

Co-authored with Copilot

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the rebuild-reason diagnostic logging change.

No open issues 🎉

Review effort: Balanced

ClaudeAgentSession.send relaunches the SDK subprocess when synced state
changed since launch, but did not say which state. Log the changed state,
so a rebuild seen in E2E logs, such as the one that drops the /model echo in
'model changes between turns retain provider context', can be attributed.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the session-rebuild diagnostic logging change.

No open issues 🎉

Review effort: Balanced

@bryanchen-d
Bryan Chen (bryanchen-d) marked this pull request as ready for review November 6, 2026 22:51
@bryanchen-d
Bryan Chen (bryanchen-d) enabled auto-merge (squash) November 6, 2026 22:51
@bryanchen-d
Bryan Chen (bryanchen-d) merged commit 23b07a5 into main Nov 6, 2026
53 checks passed
@bryanchen-d
Bryan Chen (bryanchen-d) deleted the copilot/vscode/claude-rebuild-reason-log branch November 6, 2026 23:25
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants