Skip to content

fix(hosting): keep persistent-terminal handoff failures best-effort, as upstream does - #900

Merged
Blind-Striker merged 5 commits into
masterfrom
fix/871-best-effort-handoff
Nov 15, 2026
Merged

Blind-Striker merged 5 commits into
masterfrom
fix/871-best-effort-handoff

Conversation

@Blind-Striker

Copy link
Copy Markdown
Collaborator

StopAsync and the EnsureAsync version-mismatch replacement now treat a failed persistent-terminal handoff or clear as best-effort, whatever the exception type, as the pinned client does. Before, a failure other than an OpenCodeServerException ended StopAsync before it terminated the old service, and ended EnsureAsync at once. Only the caller's cancellation still ends them early.

Closes #871

Upstream at the pin (v2.3.0)

  • stop (packages/client/src/promise/service.ts:103-112, and its Effect twin in packages/client/src/effect/service.ts) catches any failure of the handoff or the clear, warns, and then terminates.
  • ensure wraps the whole replacement stop in .catch(() => undefined) (promise/service.ts:84), terminate included, and polls again.

What changes

  • ServicePtyHandoffStep.RunAsync, the step StopAsync and the replacement share:
    • The catch was OpenCodeServerException or ArgumentException { ParamName: "endpoint" }.
    • It is now catch (Exception failure) when (failure is not OperationCanceledException || !cancellationToken.IsCancellationRequested).
    • So every failure is swallowed, including the step's own request-timeout cancellation. Only the caller's cancellation propagates.
  • ServiceElection.ReplaceAsync: the same filter, in place of catch (OpenCodeServerException).
    • The kept failure (_lastReplaceFailure) widens to Exception.
    • It still surfaces only as the inner exception of the election's timeout OpenCodeServerException, whose type and message do not change.
  • Both catches receive the caller's own token. Nothing on the path from StopAsync or EnsureAsync links or derives it. The only derived deadline tokens live inside ServicePtyHandoff, which already turns a deadline cancellation into a swallow.
  • Suppressions: none are new or widened in count. The step's existing SW003 reason now states what the code does: every failure is caught; the SDK has no warning channel here, so the failure is dropped, where upstream warns; only the caller's cancellation propagates. ReplaceAsync needed no suppression. The suppression ledger is unchanged.
  • CHANGELOG: one line under 🔧 Changes.
  • No canon or guide sentence changes. client-runtime.md and the connection-modes guide already describe the step as best-effort.

An existing test, reversed on purpose

EnsureAsync_Should_Propagate_An_Unrelated_Handoff_Argument_Error_Without_Signalling pinned the narrow catch, which this change reverses by the maintainer's decision on #871. It becomes EnsureAsync_Should_Replace_The_Old_Service_When_Handoff_Preparation_Fails:

  • rows: ArgumentException with ParamName "options", ArgumentException with a null ParamName, and IOException;
  • oracle: the failure does not escape, the old service is told to terminate, the successor is registered, and one contender is spawned.

Tests

  • ServiceStopperTests: StopAsync_Should_Terminate_When_Handoff_Preparation_Fails goes from two rows to four. The rows are:

    • a service failure;
    • an endpoint refusal;
    • an IOException;
    • an OperationCanceledException that is not the caller's.

    A clear-path IOException row is added as well. The terminate oracle sits in a finally, so a regression fails at that assertion. StopAsync_Should_Propagate_Caller_Cancellation_During_Handoff is unchanged.

  • ServiceEnsurerTests:

    • the reversed test above;
    • a terminate IOException that the election carries into its timeout as the inner exception;
    • a caller cancellation during the replacement that still escapes. This last row is an outcome check: it also passes on the base and against the filter mutants (see below).
  • ServicePtyHandoffStepTests (new): the step's own contract. The caller's cancellation propagates with its token. IOException, OpenCodeServerException, and a cancellation that is not the caller's are swallowed on both the handoff branch and the clear branch.

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

  • M1, the step's filter back to the narrow set: killed at the terminate assertions and at the direct step tests.
  • M2, the step with no filter: killed by the direct caller-cancellation test. Through StopAsync it cannot be told apart, because the terminator then throws the caller's cancellation before any signal. That is why the step has its own test file.
  • M3, ReplaceAsync back to OpenCodeServerException: killed by the terminate-IOException timeout test.
  • M4, ReplaceAsync with no filter: survives, and is equivalent at the public boundary. A swallowed caller cancellation surfaces again, with the caller's token, at the election's next delay or loop check. Nothing that touches the outside world runs in between: no spawn and no signal. A direct proof would need a new seam, and this change does not justify one.

Follow-up

The same narrow catch exists one level down, in ServicePtyHandoff.PrepareAsync: the ticket request, the shutdown fallback and the sidecar publish. Upstream catches everything there too. That decision is recorded as #895, and this PR leaves it alone.

Verification

  • The local check on the rebased head 4b840811 (master c99a58a8), 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.
    • OpenCode.Sdk.Tools.Tests except NativeAot: 2,819.
    • OpenCode.Sdk.Tests: 4,261 on each target framework.
    • The ServiceElection pass: 9 on each.
    • Total 11,359 passed, 0 failed.
  • The full Windows local check on the pre-rebase head 38b6d7e8 (master 0bb25bc4): GATE PASS.
    • Static: slopwatch, the Release build, both format passes and both clone checks.
    • Tests:
      • OpenCode.Sdk.Tests 4,249 / 4,249 / 4,241 on net10.0 / net11.0 / net472;
      • Tools 2,819;
      • HQ 84;
      • Extensions 21 / 21 / 20;
      • the service-election pass 27 of 27.
    • The NativeAot pass is out of scope: no packaging, trimming or publishing change.
  • The rebase: master gained the stop option's rename (refactor: align the stop option's Pty name and mode type with upstream #897, PersistentPtys → Pty) and the persistent-terminal shutdown (feat(hosting): discover the background service and shut its persistent terminals down in one call (#848) #898).
  • CI on all three operating systems: this PR's run. It covers net472 on the rebased head.

In 1.1.0 an OpenCodeServerException already let StopAsync terminate; the change is every other exception type. EnsureAsync keeps polling and carries the last replacement failure into its timeout. The caller's cancellation still ends both.
@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 422c45a Nov 15, 2026
17 checks passed
@Blind-Striker
Blind-Striker deleted the fix/871-best-effort-handoff branch November 15, 2026 14:10
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 step: best-effort catch narrower than upstream's

1 participant