Skip to content

fix(hosting): the handoff preparation's inner steps are best-effort (#895) - #905

Merged
Blind-Striker merged 4 commits into
masterfrom
fix/895-inner-handoff-catches
Nov 15, 2026
Merged

Blind-Striker merged 4 commits into
masterfrom
fix/895-inner-handoff-catches

Conversation

@Blind-Striker

@Blind-Striker Blind-Striker commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

The persistent-terminal handoff preparation now treats a failure of each of its three inner steps as best-effort, whatever the exception type, as the pinned client's prepare does: the ticket request, the terminal-shutdown fallback and the sidecar publish. Only the caller's cancellation still ends the preparation.

Closes #895

Upstream at the pin (v2.3.0)

prepare in packages/client/src/pty-handoff.ts:

  • the ticket request .catch(async (cause) => { ... }) takes any failure, re-reads the sidecar for a concurrent preparation, warns, and otherwise shuts the terminals down and returns null;
  • the shutdown fallback is .catch(() => {});
  • the publish is .catch((cause) => console.warn(...)).

What changes

All in ServicePtyHandoff. Each step's catch becomes catch (Exception failure) when (failure is not OperationCanceledException || !cancellationToken.IsCancellationRequested), the form the outer step took in the earlier best-effort change (#871):

  • PrepareAsync, the ticket request: was OpenCodeException or OperationCanceledException or ArgumentException { ParamName: "endpoint" }. Any failure now goes to the concurrent-preparation check and the shutdown fallback.
  • ShutdownAsync, the shutdown fallback: the same narrow set before. A failed shutdown no longer skips the null-sidecar publish.
  • TryPublishAsync, the publish: was OpenCodeServerException.
  • Each first catch, which maps the caller's cancellation through FailureClassification.CallerCancellation, is unchanged. Every filter receives PrepareAsync's own token; the request and shutdown deadlines are linked tokens that reach only the operations, so a deadline expiry is swallowed, as upstream's AbortSignal.timeout rejection is.

What a consumer sees: StopAsync with Pty = Handoff and the EnsureAsync replacement already continued after a failed preparation (#871). Now, after a ticket request fails with an exception other than an OpenCodeException, the SDK still asks the old service to shut its persistent terminals down and publishes the empty handoff sidecar, as opencode's CLI does.

Suppressions and documentation

  • No suppression is new or widened in count; the suppression ledger is unchanged. Two existing SW003 reasons now state what the code beside them does:
    • ShutdownAsync: upstream drops a failed shutdown with .catch(() => {}), and the SDK drops it silently as upstream does; only the caller's cancellation propagates, and a null sidecar is published next.
    • TryPublishAsync: upstream catches every publish failure and only warns; the SDK has no warning channel here, so the failure is dropped.
    • The dropped ticket-request warning is recorded as a comment in PrepareAsync's catch, where the drop happens. No tool asked for a suppression there.
  • TryPublishAsync's summary now says only the caller's cancellation fails the preparation.
  • CHANGELOG: one line under 🔧 Changes, in Before/After form, next to Handoff step: best-effort catch narrower than upstream's #871's.
  • No canon or guide sentence changes: client-runtime.md already described the preparation as best-effort, with only the caller's cancellation propagating. The code now matches that text.

An existing test, reversed on purpose

PrepareAsync_Should_Propagate_An_Unrelated_Shutdown_Argument_Error pinned the narrow shutdown catch, which this change reverses by the maintainer's decision on #895. It becomes PrepareAsync_Should_Swallow_A_Failed_Shutdown_Fallback:

  • rows: an ArgumentException with ParamName "options", and an IOException;
  • oracle: the preparation completes, the shutdown was called once, and the null sidecar is published.

Tests

  • ServicePtyHandoffTests grew past the repository's file-size limit, so the best-effort proofs moved to a new ServicePtyHandoffBestEffortTests, and the shared helpers became members of ServicePtyHandoffScenario. The moved tests are unchanged apart from the helper references; nothing was dropped.
  • New rows:
    • the ticket request fails with an IOException: the preparation takes the concurrent check, then the shutdown, then the null sidecar;
    • the publish fails with an IOException or an ArgumentException: the preparation completes without a sidecar;
    • the caller cancels during the publish: the cancellation escapes with the caller's token. ServicePtyHandoffCancellationTests already proves this for the ticket and shutdown steps.
  • The ticket-request seam. No existing seam can make the ticket path throw a non-OpenCodeException: the pipeline maps transport failures, the ticket adapter maps JSON failures, and the only ArgumentException the pipeline throws was already admitted. The test therefore makes the injected clock's first timer creation, the request deadline, throw an IOException. An independent reader traced the call order and judged this a fair proof. The test aims at a recording loopback server and asserts it received no request, so it can never reach a real service.

Mutants (each built with the analyzers on, on net10.0, net11.0 and net472):

  • M1, the ticket catch back to the narrow set: killed. The IOException ticket row fails at "Expected to not throw any exception".
  • M2, the shutdown catch back to the narrow set: killed. The reversed test's IOException row fails at "Expected to not throw any exception".
  • M3, the publish catch back to OpenCodeServerException: killed. The publish row's ArgumentException fails at "Expected to not throw any exception".
  • M4, the publish catch with no filter: killed. The publish caller-cancellation row fails at "Expected to throw OperationCanceledException".

Review

An independent reader reviewed the change against prepare step by step, and every finding but two was fixed. Not changed:

  • The second filter in the two-catch sites is redundant with the first catch for a cancellation. It keeps the earlier change's shape. A mutant that replaces it with true is equivalent.
  • The split between the two test files keeps the route-absent fallback in the original file. Its placement does not change coverage.

Verification

  • The full local check on the pre-rebase head bf641755 (master 422c45ad), on Windows, on net10.0, net11.0 and net472: every step passed.
    • Release build 0 warnings, slopwatch 0 issues, both format passes, verify-clones 0 new.
    • The handoff classes 57 on each target framework, the background-service classes 578 on each, and the service-election pass 8 on each.
    • OpenCode.Sdk.Tools.Tests except NativeAot: 2,819.
  • The rebase onto 03238e37 (feat(events)!: one shared event feed per client (2.0.0) #902) was clean. It overlaps this change only in CHANGELOG.md.
  • The local check on the rebased head b1c32308 (master 03238e37), on Linux, net10.0 and net11.0, SDK 11.0.100, Bun 1.4.4 at the pin: every step passed.
    • Slopwatch 0 issues, Release build 0 warnings, both format passes, verify-clones 0 new.
    • --help, and generate --verify: current.
    • OpenCode.Sdk.Tools.Tests except NativeAot: 2,823.
    • OpenCode.Sdk.Tests: 4,369 on each target framework.
    • The ServiceElection pass: 9 on each.
    • Extensions: 21 on each.
    • Total 11,621 passed, 0 failed.
  • CI on all three operating systems: this PR's run. It covers net472 on the rebased head.

@Blind-Striker Blind-Striker added this to the Next milestone Nov 15, 2026
@Blind-Striker
Blind-Striker added this pull request to the merge queue Nov 15, 2026
Merged via the queue into master with commit 8693db6 Nov 15, 2026
17 checks passed
@Blind-Striker
Blind-Striker deleted the fix/895-inner-handoff-catches branch November 15, 2026 18:22
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.

Handoff preparation: inner best-effort catches narrower than upstream's

1 participant