Skip to content

agentHost: harden Claude cancellation and lifecycle recovery - #345929

Merged
TylerLeonhardt merged 3 commits into
mainfrom
tyleonhardt/claude-reliability-baseline
Nov 4, 2026
Merged

TylerLeonhardt merged 3 commits into
mainfrom
tyleonhardt/claude-reliability-baseline

Conversation

@TylerLeonhardt

@TylerLeonhardt TylerLeonhardt commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Interrupt paused Claude turns before closing the SDK transport, await shutdown before recovery, and ignore results from the cancelled query.
  • Preserve cancellation during an in-flight rebuild: reject the late startup result even before interrupt acknowledgement, while leaving old-runtime teardown with the pending abort.
  • Keep a replacement cancellable while it waits for previous-process exit. Report interrupt errors to the abort caller without blocking recovery after successful shutdown; genuine shutdown failures still block overlapping recovery.
  • Add stateful SDK-boundary coverage for initial startup cancellation/disposal, failed-launch retry, cold-resume retry, rebuild cancellation, and old-query isolation. Coverage includes both rebuild/interrupt completion orders and replacements waiting for prior-process exit.
  • Strengthen paused-input/file-approval E2E assertions and restore Claude truncation coverage with assertions against the actual continuation request history. Truncation is coverage restoration, not a new production fix.

This is a bounded reliability chunk for human review, not the planned launch/runtime architecture refactor. The SDK remains at 0.3.304.

Validation

Current head: 5ca3fdfa09e92aa6c60d0b9a1a2437ca45081043.

Command Result
pnpm run build-fast --client-only Passed; incremental client transpilation, not a full type-check.
.\scripts\test.bat --run src\vs\platform\agentHost\test\node\claudeSdkPipeline.test.ts --run src\vs\platform\agentHost\test\node\claudeAgent.test.ts --run src\vs\platform\agentHost\test\node\claudeMapSessionEvents.test.ts --run src\vs\platform\agentHost\test\node\claudeSdkMessageRouter.test.ts --reporter dot 403 passed on Windows.
.\scripts\test-integration.bat --run src\vs\platform\agentHost\test\node\e2e\providers\claudeAgentHostE2E.integrationTest.ts --grep 'cancelling a turn paused|truncating a materialized chat' --reporter spec 3 passed in each of two executions on Windows, strict replay, real bundled SDK.
pnpm run eslint src\vs\platform\agentHost\node\claude\claudeSdkPipeline.ts src\vs\platform\agentHost\test\node\claudeSdkPipeline.test.ts Passed.
node .\build\hygiene.ts src\vs\platform\agentHost\node\claude\claudeSdkPipeline.ts src\vs\platform\agentHost\test\node\claudeSdkPipeline.test.ts src\vs\platform\agentHost\test\node\e2e\README.md Passed (2 TypeScript files checked).
git diff --check Passed before committing.

For both replay executions, AGENT_HOST_TEST_PROFILE=full; AGENT_HOST_REPLAY_RECORD, AGENT_HOST_UPDATE_SNAPSHOTS, AGENT_HOST_UPDATE_AHP_SNAPSHOTS, and AGENT_HOST_RUN_KNOWN_ISSUES were explicitly set to 0. No captures were changed for the final correction.

The permanent regression selector .\scripts\test.bat --run src\vs\platform\agentHost\test\node\claudeSdkPipeline.test.ts --grep 'cancellation discards a late runtime' --reporter spec produced 1 failure / 1 pass before the fix. Both cases pass in the final 403-test run. An earlier isolated probe also passed against pre-change 29aacc7f and failed against b52e4b40.

The follow-up selector .\scripts\test.bat --run src\vs\platform\agentHost\test\node\claudeSdkPipeline.test.ts --grep 'replacement waiting for previous shutdown' --reporter spec produced 2 failures / 1 pass before the feedback fixes. All three now pass in the 403-test run: cancelling a waiting replacement, successful recovery despite an interrupt failure, and preserving shutdown failure. The interrupt-order test also checks that the old prompt stream remains open until interruption is acknowledged.

The original GitHub Actions run 41725486505 failed on nine browser/terminal tool-registration cases in Linux extension-host API tests, not Claude E2E. Attempt 2 passed on the unchanged 34e87ac1 head, without modifying those unrelated tests. This establishes that the failure was transient; its precise cause was not proven. That retry is prior-head evidence, not CI validation of 5ca3fdfa. The subsequent current-head run 41731865422 has now succeeded at 5ca3fdfa, including the previously failing Linux extension-host job and PR Gate. Required Azure validation and the disclosed local/platform gaps remain separate.

Scoped TypeScript check — passed for both edited files

This uses the repository TypeScript project, checks diagnostics for the two files changed by the correction, and emits nothing. It is not a new full-client type-check.

@'
import path from 'node:path';
import ts from 'typescript';
const configPath = path.resolve('src', 'tsconfig.json');
const config = ts.readConfigFile(configPath, ts.sys.readFile);
const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(configPath));
const changedFiles = [
 path.resolve('src', 'vs', 'platform', 'agentHost', 'node', 'claude', 'claudeSdkPipeline.ts'),
 path.resolve('src', 'vs', 'platform', 'agentHost', 'test', 'node', 'claudeSdkPipeline.test.ts'),
];
const program = ts.createProgram(parsed.fileNames, { ...parsed.options, noEmit: true });
const diagnostics = [...(config.error ? [config.error] : []), ...parsed.errors, ...program.getOptionsDiagnostics()];
for (const file of changedFiles) {
 const source = program.getSourceFile(file);
 if (!source) throw new Error('Missing source file in project: ' + file);
 diagnostics.push(...program.getSyntacticDiagnostics(source), ...program.getSemanticDiagnostics(source));
}
if (diagnostics.length) {
 console.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, { getCurrentDirectory: () => process.cwd(), getCanonicalFileName: name => name, getNewLine: () => '\n' }));
 process.exitCode = 1;
} else {
 console.info('PASS: scoped TypeScript diagnostics for both changed files using the repository src tsconfig. No emit.');
}
'@ | node --max-old-space-size=8192 --input-type=module -

Live UI verification

On 5ca3fdfa, the Windows Code OSS Agents window was exercised using the actual Claude Agent provider and Claude Haiku 5.5 with live model traffic, not replay. pnpm run build-fast confirmed the complete client and extension outputs were current. Three scenarios passed:

  • Cancel a genuine pending question, then complete a replacement with its own response and no pending question.
  • Cancel a genuine file-approval request without approving it, then complete an independent replacement. Filesystem checks confirmed the requested file was absent before cancellation, after replacement, and after restart.
  • Restart the local agent host through its UI command, then resume the same conversation. The next prompt omitted the prior answer marker, and a new completed response recalled it correctly; logs confirmed restoration and isResume=true for the original SDK conversation.

This was one Windows live session, not exhaustive platform/model testing or a replacement for deterministic race tests. No policy or source changes were made, and the isolated app/profile/workspace were cleaned up. The earlier full-suite/environment issues and outstanding platform-validation limits below are unchanged.

Review and validation limits

  • Agent pre-PR correctness/code-style review found the rebuild cancellation race; the correction and discriminating regression tests address it. No additional introduced blocker was found on re-review. Human review is still required.
  • Cyclomatic complexity was measured with ESLint's classic rule; cognitive complexity was reviewed manually, not assigned an automated score. After the selected review fixes, cyclomatic scores are abort 5, interrupt/shutdown 2, rebuild 10, and message consumer 27 (unchanged). The feedback fixes replace one cached abort promise with one owned operation containing its cancellation target, shutdown completion, and caller-facing result. No generation counters, test-only production APIs, or helper abstractions were added. Broader simplification is deliberately separate.
  • Azure definition 111 builds 492479 and 492480 passed all six packaged Agent Host E2E tasks on earlier commit b52e4b40, covering Windows x64, Linux x64, and macOS ARM64, not x64. Those runs show repeatability at that revision; they do not validate the final correction. No new Azure runs were queued for this head.
  • The earlier full local six-suite run failed: one Claude restart-readiness timeout of unknown cause, Windows test-home cleanup EPERM failures around FNM junctions, and Copilot discovery affected by three machine-policy hooks. Passing focused retries and CI did not erase or explain those failures. They remain separate investigations; no skips, timeouts, policy controls, or expected-failure assertions were weakened to obtain green output.
  • The prior Claude inventory retained 33 unexecuted variants; the complete suite was not rerun after this correction. Fresh required cross-platform validation, including the macOS x64 coverage decision, remains outstanding before merge.
  • The branch remains based on main@29aacc7f; it has not been rebased onto current main. Any update to the base requires validation of the changed inputs. No tracking issue was supplied.

Ready for human review of this bounded cancellation/reliability change. Human approval and required checks remain necessary; the validation limits above are not waived. Broader architecture simplification remains a separate PR.

TylerLeonhardt and others added 2 commits November 3, 2026 10:04
Preserve the Claude cancellation/replacement runtime behavior and strengthen
its test baseline: stateful component cases for stale old-runtime results,
late-startup cleanup and truncation anchors, a test-only SDK streaming helper,
stronger model-history assertions, one live-recorded truncation capture, and
related E2E docs.

Co-authored-by: Copilot <[email protected]>
Reject a rebuilt runtime as soon as cancellation begins without closing the previous transport before interruption completes. Cover both startup and interrupt completion orders with deterministic regression tests.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 3, 2026 21:50

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.

🟡 Changes recommended

Recovery has unresolved race conditions that can submit a cancelled replacement prompt or fail a replacement because of an old interrupt error despite successful teardown.

2 open issues
What changed in this PR

This PR hardens Claude Agent Host cancellation and recovery by interrupting turns before closing transport, waiting for shutdown, and ignoring stale query results. It also restores Claude truncation coverage without changing production truncation behavior.

Changes:

  • Coordinate cancellation, rebuild recovery, and cleanup when interruption fails.
  • Add lifecycle regression tests and stronger paused-turn assertions.
  • Enable Claude cancellation and truncation E2E scenarios, refresh captures, and document validation limits.
File Description
src/​vs/​platform/​agentHost/​test/​node/​e2e/​suites/​fileOperationsSuite.ts Verify pending approval, cancellation, and replacement completion.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​suites/​coreSuite.ts Strengthen input-cancellation and continuation-history assertions.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​README.md Document restored coverage and remaining validation requirements.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​providers/​claudeAgentHostE2E.integrationTest.ts Enable Claude cancellation and truncation scenarios.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​KNOWN_ISSUES.md Remove entries for the re-enabled scenarios.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​captures/​copilotcli-cancelling-a-turn-paused-for-file-tool-approval-allows-a-replacement-turn.yaml Align the capture with the approval-requiring filename.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​captures/​claude-truncating-a-materialized-chat-removes-later-context-and-allows-continuation.yaml Add a truncated-history continuation capture.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​captures/​claude-cancelling-a-turn-paused-for-input-allows-a-replacement-turn.yaml Refresh interrupted-input and replacement history.
src/​vs/​platform/​agentHost/​test/​node/​e2e/​captures/​claude-cancelling-a-turn-paused-for-file-tool-approval-allows-a-replacement-turn.yaml Refresh interrupted file-approval history.
src/​vs/​platform/​agentHost/​test/​node/​claudeSdkPipeline.test.ts Cover shutdown ordering, rebuild cancellation, and stale-result isolation.
src/​vs/​platform/​agentHost/​test/​node/​claudeMapSessionEventsTestUtils.ts Add a reusable SDK text-response helper.
src/​vs/​platform/​agentHost/​test/​node/​claudeAgent.test.ts Cover startup cancellation, retries, rebuilds, and teardown failures.
src/​vs/​platform/​agentHost/​node/​claude/​claudeSdkPipeline.ts Coordinate interruption, shutdown, recovery, and result filtering.
src/​vs/​platform/​agentHost/​node/​claude/​claudeAgentSession.ts Await cancellation before releasing pending requests.
src/​vs/​platform/​agentHost/​node/​claude/​claudeAgent.ts Await session cancellation and preserve cleanup on failure.

Review effort: Balanced

Keep pending recovery cancellation distinct from the previous SDK shutdown. Report interrupt failures to abort callers without blocking recovery after successful teardown, and retain shutdown failures. Cover both review cases with gated SDK-boundary tests.

Co-authored-by: Copilot <[email protected]>
@TylerLeonhardt
TylerLeonhardt marked this pull request as ready for review November 4, 2026 23:05
@TylerLeonhardt
TylerLeonhardt enabled auto-merge (squash) November 4, 2026 23:05
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