Skip to content

feat(hosting): discover the background service and shut its persistent terminals down in one call (#848) - #898

Merged
Blind-Striker merged 2 commits into
masterfrom
feat/848-persistent-pty-shutdown
Nov 15, 2026
Merged

Blind-Striker merged 2 commits into
masterfrom
feat/848-persistent-pty-shutdown

Conversation

@Blind-Striker

Copy link
Copy Markdown
Collaborator

Closes #848

What

OpenCodeServer.ShutdownPersistentPtyAsync is upstream's shutdownPersistentPty (packages/cli/src/services/server-connection.ts:80-87), which the CLI's uninstall runs before it stops the service. It discovers the background service exactly as DiscoverAsync does, with the same options. When discovery answers no service, it sends nothing and completes. Otherwise it sends one POST /api/experimental/persistent-pty/shutdown to the registered endpoint with the registration's credential, no location and no request bound of its own, as upstream's call has none, so the client's default per-read network window applies. The service's persistent-terminal daemon then stops with every terminal it owns; a daemon that is not running answers 204. Failures propagate as OpenCodeApiException or OpenCodeTransportException, and nothing is retried. The call stops no service and touches no handoff sidecar. It carries OPENCODE002, because it calls an experimental route.

The maintainer's decisions (2026-11-15): the name ShutdownPersistentPtyAsync, upstream's spelling; OpenCodeServerDiscoverOptions? as the options (O1); Task as the result (R1).

  • OpenCodeServer.BackgroundService.cs: the public door; an internal test hook, ShutdownPersistentPtyWithSeamsAsync, that takes the discovery and the shutdown; and one private SystemDiscoveryProbe() factory that DiscoverAsync and the new door share. Discover's existing TimeProvider.System choice and its RS0030 pragma moved into that factory unchanged; the file still has exactly three RS0030 pairs, and DiscoverAsync's behaviour is unchanged.
  • The shutdown seam's comments name both callers (the handoff's prepare fallback and the new door).
  • No new type, so ReservedNamePolicy, GeneratedSourceCompiler and the suppression ledger are unchanged.

Divergence (O1, recorded in the canon)

Upstream's shutdownPersistentPty removes the version requirement before it discovers (server-connection.ts:83). The SDK takes discovery's own options, whose ExpectedVersion is null by default, which is the same any-version discovery; a caller who sets it reaches only a service at that version. It is a superset that reuses discovery's options type instead of a near-copy without the member. client-runtime.md's list "Where the SDK deliberately differs" records it with its reason and reversal trigger.

Tests

  • OpenCodeServerPersistentPtyShutdownTests (seven tests on MockFileSystem, substitute probe and shutdown): a missing registration sends nothing; a waiting, failed or incompatible service sends nothing; a ready service gets exactly one shutdown with its registration, and the caller's token reaches both the probe and the shutdown; a shutdown OpenCodeApiException propagates as the same instance; ExpectedVersion narrows the discovery; contradictory options throw before any call; a cancellation thrown during discovery propagates unchanged with no shutdown.
  • OpenCodeServerDiscoveryLiveTests: one live test over the pinned managed service, through the patient discovery path (never the two-second probe bound). Both arms first assert a ready, compatible, untimed discovery verdict and exactly one shutdown call. Daemon arm: the created terminal is listed before the call and gone after it. Daemon-absent arm: create answers 503 naming opencode-pty, the shutdown answers 204, and the list is empty.
  • CancellationIdentityTests: a shutdown-persistent-pty row runs the public door end to end and asserts the caller's token, the request order, the Basic header, no x-opencode-directory header, the registration kept, and no sidecar.
  • The public API snapshot gains exactly the door's three lines.

Mutants, each run once against the committed code and failing at an assertion on net10.0 and net11.0: a door that throws on no service; discovery's readiness check removed; no shutdown; None to the shutdown; None to discovery; a swallowed shutdown failure; ExpectedVersion cleared; ExpectedVersion refused; a swallowed ArgumentException; a swallowed discovery cancellation; and, for the live test, a door that skips the shutdown (daemon arm: "Expected to be 1 but found 0").

Live evidence (macOS arm64, daemon arm):

ppty-shutdown-live: arm=daemon create=200 before-status=200 before-ids=pty_persistent_5723206075474231 after-status=200 after-ids=

Version impact (ADR-0034)

Additive: one new public static method. No existing signature changes.

Local evidence (macOS arm64, head 47a831d7 on c734f4b6)

The full local check passed on the rebased head:

  • slopwatch (0 issues), dotnet build --configuration Release -p:IncludeNet472Tests=true, dotnet build --configuration Release, both format passes and verify-clones;
  • OpenCode.Sdk.Tools.Tests [Category!=NativeAot]: 2819 of 2819;
  • OpenCode.Sdk.Tests [(Category!=ServiceElection)&(Category!=NativeAot)]: 8496 of 8496 on net10.0 and net11.0;
  • OpenCode.Sdk.Tests [Category=ServiceElection] with --max-parallel-test-modules 1: 18 of 18.

The run used the pinned ripgrep 15.1.0 on PATH. The branch was rebased onto #897 (the stop option's Pty rename) without conflicts; this change names neither renamed type.

Reviews

  • A fresh Opus reader on the implementation found no defects: the door matches upstream's call, the DiscoverAsync refactor is behaviour-identical, and the daemon-absent oracle matches what the pinned server answers. Its test gaps and nits were fixed by a fix-up worker in the second commit. One gap stays open as Live tests leave the persistent-terminal daemon running when an assertion fails #896: a live test that fails between the create and the shutdown leaves the detached terminal daemon running; PersistentPtyLiveTests has the same exposure, and one fix will cover both.
  • A fresh Opus reader on the fix-up's hunks found a broken anchor, a third guide sentence that still called discovery "the one door that reads the environment", and four nits; all are fixed in the same commit.
  • PersistentPtyLiveTests.DaemonService and CreateRequest() became internal so the new live test reuses them; moving them into Support/ waits for the persistent-PTY family rename.

@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 c99a58a Nov 15, 2026
17 checks passed
@Blind-Striker
Blind-Striker deleted the feat/848-persistent-pty-shutdown branch November 15, 2026 12:17
@Blind-Striker

Copy link
Copy Markdown
Collaborator Author

net472 mutation proof: pending, on a Windows run. The mutants in this PR's body were proven on net10.0 and net11.0 on macOS, where the net472 test target builds but does not run; the lead replays them on a Windows host and posts the results here.

@Blind-Striker

Blind-Striker commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

net472 mutation proof for this PR, replayed on the head 47a831d7 (Windows, Release, full analyzer build, --framework net472, filter /*/*/OpenCodeServerPersistentPtyShutdownTests/*):

  • Baseline: 10 of 10 green. Final run: 10 of 10 green.
  • Mutants: 9 of 10 fail on net472. One cannot be built under the full analyzer build, so it is recorded as unreachable, not as a kill. Each file restore is byte-identical.

Test names omit the prefix ShutdownPersistentPtyWithSeamsAsync_Should_.

Mutant Build Failing tests First message
1. throw when discovery returns null pass (spelled as a coalesce-throw) Send_No_Shutdown_When_The_Service_Is_Not_Ready_Or_Compatible (3 rows), Apply_ExpectedVersion(2.0.2, 0), Complete_Without_Probing_Or_Shutting_Down_When_The_Registration_Is_Missing OpenCodeServerException: No usable service was discovered.
2. never call the shutdown unreachable: IDE0060 (unused parameter shutdown) not run —
3. CancellationToken.None to the shutdown pass Shut_Down_The_Discovered_Service_With_The_Caller_Token CanBeCanceled: expected True but found False
4. CancellationToken.None to discovery pass Shut_Down_The_Discovered_Service_With_The_Caller_Token, Propagate_Probe_Cancellation_Unchanged no ProbeAsync call with the caller token
5. swallow OpenCodeException from the shutdown pass Propagate_The_Shutdown_Api_Exception_Unchanged Expected to throw OpenCodeApiException
6. clear ExpectedVersion before discovery pass Apply_ExpectedVersion(2.0.2, 0) Expected no matching ShutdownAsync calls; received one
7. reject a set ExpectedVersion pass Apply_ExpectedVersion (2 rows) ArgumentException: ExpectedVersion is not supported.
8. swallow the discovery ArgumentException pass Refuse_Contradictory_Options_Before_Probing_Or_Shutting_Down Expected to throw ArgumentException
9. swallow the discovery cancellation pass Propagate_Probe_Cancellation_Unchanged Expected to throw OperationCanceledException
10. remove the readiness and compatibility check (ServiceDiscovery.cs) pass Send_No_Shutdown_When_The_Service_Is_Not_Ready_Or_Compatible (3 rows) Expected no matching ShutdownAsync calls; received one

On this unit seam, the full analyzer build rejects mutant 2 ("never call the shutdown") as an unused parameter. That behaviour is pinned by the live test's counting wrapper, whose "skip the shutdown" mutant was proven on net10.0 and net11.0, as the description records.

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.

Persistent terminals: discover the service and shut its terminals down in one call

1 participant