mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
F.9 starts the TypeScript service only when discovery finds none, and never replaces or stops a service it did not start.
4 KiB
4 KiB
F.9: service lifecycle and spawn policy
opencode-service starts, waits for, and stops the TypeScript server. It ports
packages/client/src/effect/service.ts (ensure, stop), service-contender.ts, pty-handoff.ts,
and packages/cli/src/services/standalone.ts, with the policy below. User decisions Q2 and Q3 and
orchestrator decisions O-21 and O-22 apply.
Policy
- Only when nothing is running. The client starts a service only when discovery (F.6, all registrations in O-21 order) finds no ready, compatible service.
- Never replace, never stop a service it did not start. No version requirement is ever passed
to
decide, soReplacecannot happen. A registered service that does not answer three probes in a row is left running, andensurefails with a message that names it. The one exception is a registration whose pid is one of thisensurecall's own contenders: that process was started by this client, so it is evicted as TS evicts any unresponsive service.stopis only for the explicitservice stop,restart, andsetcommands (C.8). - What it runs.
ts_command:OPENCODE_TS_BIN(one executable path), elseopencodeonPATH. The service is<ts> serve --service, the standalone server<ts> serve --stdio --port 0. The environment adds the service config'senv(withoutXDG_STATE_HOME) andOPENCODE_PTY_HANDOFFfrom the sidecar, as TS does. - Where it looks. The TS binary picks its registration file from its own build channel, which
the Rust client cannot know (the installed
opencodehere registersservice-oc-opentui-next.json).ensureprobes the client channel's file with the full TS semantics (wait, fail, timeouts). Once it has spawned a contender, each round where that file has no service also runs discovery over every registration and takes the first ready one. A started TS binary that finds its own incumbent exits 0, so this also covers a service that was still starting when the client looked. - Contenders. As TS: detached (own process group), stdin and stdout closed, an 8 KiB stderr
tail; at most two alive; a startup failure is kept and reported once none is left; clean exits
double the spawn delay up to the maximum; a registration that has not answered gets one spawn
delay before a contender competes with it. A contender counts as finished when its stderr closes
and it has exited (Node's
close). The pool takes the clock as an argument so its tests need no processes or sleeps. - Timing.
Timing::default()isdefaultEnsureTiming: poll 25 ms, probe 2 s, spawn delay 5 s up to 30 s, deadline 120 s, stop poll 50 ms × 100. - Standalone. A private child with a random password in
OPENCODE_PASSWORD, stdin kept open as its lease, stdout drained after the{"url":…}ready line, stderr inherited only underOPENCODE_PRINT_LOGS=1. Dropping the handle closes stdin and sends SIGTERM;shutdownalso sends SIGKILL after 3 s. - Connect.
connect::resolveuses standalone for--standaloneordisabled: true; else discovery; elseensure(or standalone whenstandalone_fallback, therunmode withautostart: false). Anon_startcallback lets C.13 print "Starting background server...".
Deviations
- S03: a version-mismatched or unresponsive service is never replaced
(
parity/deviations/S03-never-replace.md). - A missing TS binary fails at once with the spawn error and the
OPENCODE_TS_BINhint; TS reports "Failed to start server" after the first poll.
Proof
- The cases of
packages/client/test/service.test.tsthat apply, run against the same fake service (packages/client/test/fixture/service.tsunderbun), with accelerated timing; replacement cases become "reuses" or "leaves running" cases. - The cases of
service-contender-pool.test.tson fake attempts. - Interop: in a temporary
HOME,XDG_*,OPENCODE_CONFIG_DIR, and port, Rustensurestarts the TS CLI from source, and the TS CLI'sservice statusin the same directories prints its URL.