Skip to content

fix(server): harden ACP dead-session recovery, fail-closed resume refs, and late session/load rejections (CLO-1094) - #224

Open
nullStack65 wants to merge 1 commit into
release/native-0.1.10from
fix/clo-1094-acp-dead-session-resume-cursor
Open

nullStack65 wants to merge 1 commit into
release/native-0.1.10from
fix/clo-1094-acp-dead-session-resume-cursor

Conversation

@nullStack65

Copy link
Copy Markdown
Owner

Summary

Implements CLO-1094 (audit A-P2-08) against release/native-0.1.10 (08d850cf79).

Re-verification on origin/release/native-0.1.10 (HEAD 08d850cf79)

The original audit targeted 669a4f17 (V1 orchestration lineage). On release/native-0.1.10, the server runs orchestration-v2 with several upstream changes already landed; re-verifying each of the 3 sub-items showed:

  1. Dead Cursor/Grok ACP sessions & ConnectionTerminated handling — partially fixed upstream, 3 gaps still reproduced:

    • Cursor migrated off ACP onto @cursor/sdk (packages/provider-cursor/src/server/adapter.ts) and no longer uses AcpSessionRuntime.
    • Grok & ACP (packages/provider-acp/src/server/adapter.ts) already wired onTermination -> runtimeRestartRequired = true so startTurn/resumeThread restart the child.
    • Still reproduced:
      1. wireAcpRuntimeHandlers ignored ConnectionTerminated events on runtime.getEvents().
      2. handleRuntimeTerminationAtGeneration gated held deferred-background-work turn finalization on flavor.toolOutlivesPrompt !== undefined, which is undefined for Grok (deferFinalizeForBackgroundWork: true). When a Grok child exited after prompt settlement while waiting on a background task/subagent, the held turn never finalized and permanently wedged subsequent turns on previous_turn_active.
      3. ProviderSessionManager.resumeThread short-circuited on its loadedProviderThreadKeyByThread cache without checking transport health, skipping runtime.resumeThread (and ProviderTurnStartService's explicit "Recovered context" fallback when reload fails/is unsupported), and AcpAdapterV2.ensureThread did not restart a terminated runtime when forceNewNativeThread was unset.
  2. Unrecognized saved resume cursor silently starting a fresh session across 5 adapters — architecture moved to typed nativeThreadRef, fail-closed gaps still reproduced:

    • V1's untyped resumeCursor: unknown was replaced by typed providerThread.nativeThreadRef and explicit ProviderTurnStartService recovery handoffs (preserving strict fail-closed handling for ProviderAdapterThreadInUseError on Codex).
    • Still reproduced:
      1. ClaudeAdapterV2.resumeThread did not call getNativeThreadId at all and silently succeeded when nativeThreadRef was null, blank, or belonged to another driver.
      2. CodexAdapterV2, CursorAdapterV2, AcpAdapterV2 (Grok), and OpenCode (v1 & v2) did not reject foreign-driver nativeThreadRef values; Codex, Cursor, and OpenCode also accepted blank whitespace nativeId values.
      3. CursorAdapterV2 and OpenCode v1 nativeThreadId used raw throw inside Effect.fn generators, causing unhandled Effect defects (Die) instead of typed ProviderAdapterProtocolError -> ProviderAdapterResumeThreadError failures.
      4. AcpAdapterV2.openSession accepted blank initialNativeThreadId, and ProviderTurnStartService forwarded blank/mismatched nativeThreadRef into initialNativeThreadId.
  3. Cursor/Grok session/load replay-idle race — fixed upstream for default ACP and Cursor, late-RPC-error gap still reproduced for Grok:

    • Cursor is no longer ACP (@cursor/sdk) and does not call session/load.
    • Default ACP (AcpSessionRuntime.ts) was already fixed in 834eb51301 (fix(acp): wait for session/load to reply before prompting (#37139)) to wait for the native session/load RPC response by default (sessionLoadReplayIdleReadiness defaults to false).
    • Grok (packages/provider-grok/src/server/acpSupport.ts:145) intentionally retains opt-in sessionLoadReplayIdleReadiness: true because real grok agent stdio can leave session/load unanswered after replay finishes (covered by upstream late-adoption tests from 63c2b61991).
    • Still reproduced: In AcpSessionRuntime.followLateSessionLoad, if a sessionLoadReplayIdleReadiness: true agent later replied to session/load with an AcpRequestError (explicit RPC rejection), the error was logged and swallowed via Effect.ignore, leaving the provisional session routable.

Changes

  • packages/provider-acp/src/server/adapter.ts, packages/provider-core/src/server/ProviderAdapter.ts, apps/server/src/orchestration-v2/ProviderSessionManager.ts:
    • Handle ConnectionTerminated in wireAcpRuntimeHandlers via idempotent handleRuntimeTermination.
    • Finalize held deferred-background-work turns whenever defersFinalizeForBackgroundWork is active (including Grok), clearing process-owned background state and failing open tools/subagents on child exit.
    • Restart terminated runtimes in AcpAdapterV2.ensureThread and expose isTransportHealthy on ProviderAdapterV2SessionRuntime so ProviderSessionManager.resumeThread never skips reload/recovery after child termination.
  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts, CodexAdapterV2.ts, packages/provider-cursor/src/server/adapter.ts, packages/provider-acp/src/server/adapter.ts, packages/provider-opencode/src/server/adapter.ts, packages/provider-opencode/src/server/v2/adapter.ts, apps/server/src/orchestration-v2/ProviderTurnStartService.ts:
    • Fail closed with typed ProviderAdapterProtocolError when nativeThreadRef is missing, blank, or references a different driver across all 5 adapters (and convert Cursor / OpenCode v1 nativeThreadId from throwing defects to returning Effect.fail).
    • Call getNativeThreadId in ClaudeAdapterV2.resumeThread.
    • Reject blank initialNativeThreadId in AcpAdapterV2.openSession and guard ProviderTurnStartService from forwarding blank/mismatched initialNativeThreadId.
  • packages/provider-acp/src/server/AcpSessionRuntime.ts:
    • Terminate the provisional runtime (recordTermination) in followLateSessionLoad when a late session/load RPC response fails with AcpRequestError, while keeping non-answering Grok timeouts non-fatal.
  • Focused unit tests added across AcpJsonRpcConnection.test.ts, ProviderSessionManager.test.ts, AcpAdapterV2.test.ts, ClaudeAdapterV2.test.ts, CodexAdapterV2.test.ts, OpenCode2AdapterV2.test.ts, packages/provider-cursor/src/server/adapter.test.ts, and packages/provider-opencode/src/server/adapter.test.ts.

Verification

  • vp run typecheck: 41/41 workspace packages passed.
  • vp lint & vp fmt --check on all 19 changed files: 0 warnings, 0 errors.
  • Targeted test suites (Node v26.13.1):
    • apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts: 45/45 passed
    • apps/server/src/orchestration-v2/ProviderSessionManager.test.ts: 75/75 passed
    • apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts: 86/86 passed
    • apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts: 151/151 passed
    • apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts: 210/210 passed
    • apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts: 181/181 passed
    • packages/provider-cursor/src/server/adapter.test.ts: 30/30 passed
    • packages/provider-opencode/src/server/adapter.test.ts: 113/113 passed
    • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts -t "ClaudeAdapterV2 resumeThread": 1/1 passed (note: 28 unrelated pre-existing failures in ClaudeAdapterV2 background wake turns exist on base release/native-0.1.10 from commit 32e8e6e990)

Deploy: fleet
Model: gemini-4-neon
Task: fix
Agent: antigravity
Lane: CLO-1094

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@nullStack65

Copy link
Copy Markdown
Owner Author

System-1 auto-rebase of pull request #224 (e42d90c3c2abe7c2c9c57b94e58b7ce6f1427c7b) onto origin/release/native-0.1.10 (caf9ebeb0cae23bee584f7b0acf3856fb6f6cada) failed closed and did not push. Label needs-human-rebase has been applied for System-2/human escalation.

Why it escalated

  1. Git merge conflicts on resumeThread (ClaudeAdapterV2):
    • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts: origin/release/native-0.1.10 added importedNativeSession?: boolean to resumeThread while guarding title sync on nativeThreadId !== undefined && nativeThreadId !== null; e42d90c3c2 replaced that guard with const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);.
    • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts: describe("ClaudeAdapterV2 resumeThread") conflicted between the upstream missingImport assertion and e42d90c3c2's missingRefError, blankIdError, and foreignDriverError assertions.
  2. Semantic conflict & post-rebase typecheck/test failure in packages/provider-cursor:
    • origin/release/native-0.1.10 introduced CursorAdapterV2.rollbackThread (packages/provider-cursor/src/server/adapter.ts:3604), which calls agentId: nativeThreadId(rollbackInput.providerThread) at line 3641 expecting a string.
    • e42d90c3c2 changed nativeThreadId to return Effect.Effect<string, ProviderAdapter.ProviderAdapterProtocolError>. Because rollbackThread was in a disjoint hunk, git rebase auto-merged without conflict, leaving line 3641 without yield*.
    • vp run --filter @t3tools/provider-cursor typecheck failed with:
      src/server/adapter.ts(3641,19): error TS2322: Type 'Effect<string, ProviderAdapterProtocolError, never>' is not assignable to type 'string'.
    • packages/provider-cursor/src/server/adapter.test.ts failed 1/38 tests:
      CursorAdapterV2 > sends T3 instructions to each Cursor conversation that lacks them (AssertionError: the rewound conversation may have dropped instructions a later turn carried: expected { orchestration: false, …(2) } to deeply equal { orchestration: true, …(2) }).

Other targeted test results on the rebased tree

  • packages/provider-opencode/src/server/adapter.test.ts: 116/116 passed
  • apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts: 165/165 passed
  • apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts: 235/235 passed
  • apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts: 198/198 passed
  • apps/server/src/orchestration-v2/ProviderSessionManager.test.ts: 82/82 passed
  • apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts: 106/106 passed
  • apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts: 47/47 passed
  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts -t "ClaudeAdapterV2 resumeThread": 1 passed (502 skipped)

@nullStack65

Copy link
Copy Markdown
Owner Author

Auto-rebase onto origin/release/native-0.1.10 (caf9ebeb0cae23bee584f7b0acf3856fb6f6cada) failed verification and was not pushed (head remains e42d90c3c2abe7c2c9c57b94e58b7ce6f1427c7b). Escalating for System-2 / human rebase.

Conflicts & Post-Rebase Failures

  1. Semantic / cross-file type & test regression in packages/provider-cursor/src/server/adapter.ts (auto-merged without textual conflict):

    • Pull request fix(server): harden ACP dead-session recovery, fail-closed resume refs, and late session/load rejections (CLO-1094) #224 changed nativeThreadId(providerThread) from returning string synchronously (throwing on invalid refs) to returning Effect.Effect<string, ProviderAdapter.ProviderAdapterProtocolError>.
    • origin/release/native-0.1.10 landed CursorAdapterV2.rollbackThread (packages/provider-cursor/src/server/adapter.ts:3641), which calls agentId: nativeThreadId(rollbackInput.providerThread) synchronously without yield*.
    • vp run --filter @t3tools/provider-cursor typecheck failed with:
      src/server/adapter.ts(3641,19): error TS2322: Type 'Effect<string, ProviderAdapterProtocolError, never>' is not assignable to type 'string'.
    • vp test run packages/provider-cursor/src/server/adapter.test.ts failed in CursorAdapterV2 > sends T3 instructions to each Cursor conversation that lacks them at packages/provider-cursor/src/server/adapter.test.ts:1230 (AssertionError: the rewound conversation may have dropped instructions a later turn carried: expected { orchestration: false, devices: true, runtime: true } to deeply equal { orchestration: true, devices: true, runtime: true }).
  2. Textual & signature conflicts in apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts and ClaudeAdapterV2.test.ts:

…s, and late session/load rejections (CLO-1094)

Squash-rebase of PR #224 onto current release/native-0.1.10; no behavior change from the original
commits. Original authoring trailers: Model gemini-4-neon / Task fix / Agent antigravity

Model: claude-opus-5-5
Task: fix
Agent: Builder drain parked-PR lane (Claude Code worker F3)
Lane: magpie
Harness: t3/claude-code
Model-family: Claude
Model-generation: Opus 5.5
Skills-SHA: unknown (source unavailable)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nullStack65
nullStack65 force-pushed the fix/clo-1094-acp-dead-session-resume-cursor branch from dc078ac to 45e4e61 Compare November 28, 2026 05:47

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant