Skip to content

test: add background service lifecycle tests - #53406

Merged
kitlangton merged 1 commit into
v2from
service-lifecycle-tests
Oct 5, 2026
Merged

kitlangton merged 1 commit into
v2from
service-lifecycle-tests

Conversation

@kitlangton

@kitlangton kitlangton commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

Every CLI, TUI, and desktop client shares one managed background service (opencode serve --service). Its lifecycle bugs show up as a client stuck on "Starting background server…" or a healthy server killed out from under a client. The client tests cover the lifecycle decisions with fixture servers, but nothing runs real servers through restarts, upgrades, crashes, and races and then checks where the system ends up.

What Changes

Real servers: packages/cli/test/service-lifecycle.test.ts adds 7 cases. They cover only behavior that needs real processes, driven through the real opencode service start|restart commands and the Effect client's Service.ensure. Each case gets an isolated home directory and its own port.

Case Expected outcome
2 CLI starts and 3 client ensure calls at once, from cold One server
2 racing service restart calls during concurrent starts One new server, the old one gone
Older server version Version-agnostic clients keep it; a client requiring the current version replaces it while version-agnostic clients race
Server that returns 404 for /api/info Version-agnostic clients report an incompatible protocol and leave it running; a client requiring its version replaces it
Unrelated process on the port Actionable "already in use" error, then a normal start once it leaves
Corrupted service.json, an immediate start, then a crash (SIGKILL) One replacement server after each
Server that fails to boot "Background service failed to start", with no respawn

Every case ends by requiring the service to settle:

  • exactly one live serve --service process for that home
  • service.json names it, and /api/info answers 200 with that PID and the expected version on the configured port
  • a stop leaves no process, an empty state directory, and a free port

The restart race and the crash case also require that the same server is still registered after its 5 s registration self-check.

Client fixture tests: two cases in both the Effect and Promise client suites, 0.1–0.3 s each:

  • stopping a server that is still starting lets the waiting ensure start a replacement
  • a crash that leaves service.json naming a dead process is replaced

Keeping It Cheap

  • Shared transpiler cache: each isolated home sets XDG_CACHE_HOME, which otherwise makes every spawned server transpile the CLI from scratch. Sharing one Bun transpiler cache cuts time to registration from about 1.3 s to 0.9 s.
  • Per-home processes: each home runs the CLI through its own one-line entry file, so process checks see only that home's servers.
  • In-process setup: setup and teardown use the client directly. The suite spawns CLI commands only in the cases that test them.

The unrelated-port case takes about 24 s, because each startup attempt waits out the server's fixed 15 s bind-recovery window before the error surfaces.

Scope

Tests only, and everything passes on current v2. Cases for gaps these tests surfaced land with their fixes.

Verification

cd packages/cli
bun typecheck
bun test --timeout 30000 test/service-lifecycle.test.ts --rerun-each 3
cd ../client
bun typecheck
bun run test test/service.test.ts test/promise-service.test.ts test/service-reconnect.test.ts test/service-contender-pool.test.ts --rerun-each 3

@kitlangton
kitlangton force-pushed the service-lifecycle-tests branch from 6a702f3 to 8db2e0f Compare October 5, 2026 20:46
@kitlangton kitlangton changed the title test(cli): add background service lifecycle tests test: add background service lifecycle tests Oct 5, 2026
@kitlangton
kitlangton merged commit cdd63fa into v2 Oct 5, 2026
10 of 13 checks passed
@kitlangton
kitlangton deleted the service-lifecycle-tests branch October 5, 2026 21:23
@adtahmid19-collab

Copy link
Copy Markdown

run

Nowaker added a commit to Nowaker/opencode that referenced this pull request Nov 19, 2026
"reports a failed registered service" (Promise and Effect) failed on a
loaded host with "Missing service command": the tests run ensure with
accelerated timing, a 100 ms probe deadline and a 200 ms contender
grace. When the failed-owner fixture's HTTP 500 health answer took
longer than 100 ms, the probe aborted, ensure saw the registration as
unanswered, and once the grace elapsed it recruited a contender, which
fails on the test's empty command. The tests then measured probe
latency, not the classification they are about.

These two tests now use awaitAnswer timing: the probe deadline equals
ensure's own budget and the contender grace is twice that, so only an
answered probe can decide, and a stall past the whole budget fails with
ensure's own timeout error rather than by spawning. The registration
is published only after the fixture's server listens, so the first
probe is never refused. Production timing and ensure are unchanged.

The tests date from anomalyco#53406 (cdd63fa).

Not proven by a test: the race has no deterministic red, because under
the old timing the tests fail only when the answer takes longer than
100 ms. The fix is argued from the code; one diagnostic run at load
54.8 passed both files (45/0).

AI-Tool: opencode 1.19.5-vt-432-006d837c50
AI-Model: anthropic/claude-opus-5-5
AI-Variant: high
AI-Platform: linux
AI-Harness: Vibeterm 5c268353754
AI-Session-ID: ses_e3832b1b0ffe6q98OeUzhgqhjQ
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.

2 participants