Repository navigation
feat(service): add spawn service for long-lived HTTP services (#409) - #481
Merged
Merged
Conversation
Adds a verb for the shape spawn had no answer to: run a long-lived HTTP
service on an instance and get a working local URL for it. `spawn launch`
assumes a job that ends; `spawn connect -- cmd` gives no way to learn the
port. Neither closes the loop.
The seam is one JSON line on the service's stdout:
{"event":"ready","addr":"127.0.0.1:54321","provenance":{...}}
The service picks its own port (`:0`) and announces what it bound; spawn
forwards to that. Nothing guesses a port, and nothing polls one — "is 8080
open yet?" cannot tell *my service starting* from *someone else's already
there*. spawn does not name any workload and no workload names spawn.
pkg/service is the reusable core (local child, readiness scan, tunnel,
remote-over-SSH ladder); cmd/service.go is the verb. docs/ carries the
public spec so a binary can be made spawnable without reading the code.
Two hazards drove the design, both covered by tests:
- The readiness line is forgeable. `init()` runs before `main`, so a
workload can always print a well-formed line of its choosing first —
"first match wins" is defeated by construction. Each candidate address
is verified before it is trusted.
- Verification is an HTTP request, not a TCP connect, because `ssh -L`
accepts on the local end whether or not anything listens on the far
side. A connect to a forward proves only that ssh is running. Any HTTP
status counts, including 401/404 — the question is whether a server is
there, not whether the caller is authorized.
Lifetime: closing an SSH session does not reliably kill what it started
(no TTY, so no SIGHUP), so the remote command is wrapped to TERM→KILL the
service on stdin EOF. That is a request, not a guarantee — a partition can
strand it. The instance TTL is the guarantee, so the launch path goes
through the same mandatory-TTL zombie guard as every other launch, and
`--dry-run` quotes the bound (rate × ttl) before spending anything.
`--host` attaches to an instance spawn did not launch and promises no
teardown.
Exposure is loopback-only on both ends. The token, when a service supplies
one, goes in the printed URL but never in the loggable address form.
Tested at $0 over a local loopback path — the Substrate emulator is
control-plane only (instances never boot, no SSH), so the emulator could
not keep the first real spawn from being the first test.
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Semgrep's dangerous-exec-command rule is blocking in this repo (#368), and it fires on every `exec.Command` with a non-literal argument — which is all six of them here. Annotated in the repo's existing style rather than widened in .semgrepignore, so each one carries its own reason: - `runQuietly`: the binary is a literal at both call sites ("scp", "ssh") and the args come from scpArgs/SSHExecArgs. No shell is involved, so an argument holding shell metacharacters is one argv element, not a command. - `RunLocal`: running a caller-named binary IS the contract. The path comes from the operator invoking spawn, who already has a local shell. - The four test sites: fixtures. Two of them run a shell deliberately — the forged-readiness-line reproduction needs one, and WrapRemoteCommand produces a shell script by design, so verifying the reap-on-EOF wrapper means running it. Also replaces the test server's `fmt.Fprint(w, body)` with `w.Write`, which is what it should have been: there is nothing to format, and it drops the no-fprintf-to-responsewriter finding rather than suppressing it.
… XSS rule Two CI gates. The Tier 0 gate is doing exactly its job: a new top-level command must be classified as covered or deferred, and `service` was neither. Deferred to Tier 2, because the verb IS a booted instance plus an SSH session plus a port-forward, and Substrate emulates the control plane only — instances never boot and there is no SSH. What the verb exists for is nonetheless fully tested at $0 in pkg/service (real child, real readiness line, real loopback forward, driven through the forward, both forgery cases), with the launch config, cost bound, and exit classification unit-tested in cmd. Semgrep has a second rule for the test server's response write — no-direct-write-to-responsewriter fires on `w.Write` just as no-fprintf-to-responsewriter fired on `fmt.Fprint`, so there is no spelling that satisfies both. Annotated: the body is a literal from the test and the only reader is an in-process HTTP probe, so there is no browser and no HTML to escape.
This was referenced Nov 3, 2026
SQ3 — Where is the spore.host seam? (hypothesis: zero go-notebook changes)
scttfrdmn/go-notebook#101
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #409.
The gap
spawn had no answer for "run a long-lived HTTP service on an instance and give me a working local URL."
spawn launchassumes a job that ends.spawn connect -- cmdruns a command but gives you no way to learn what port it bound. Neither closes the loop.The seam
One JSON line on the service's stdout:
{"event":"ready","addr":"127.0.0.1:54321","provenance":{"commit":"abc1234"}}The service picks its own port (
:0, let the OS assign) and announces what it actually bound; spawn forwards to that. Nothing guesses a port, and nothing polls one — "is 8080 open yet?" cannot distinguish my service is starting from someone else's is already there, and the answer arrives late either way.spawn names no workload, and no workload names spawn.
docs/service-readiness-contract.mdis the public spec, so a binary can be made spawnable without reading spawn's source.Layout
pkg/service/readiness.goreadyevent; verify candidatespkg/service/local.gopkg/service/tunnel.gopkg/service/remote.gocmd/service.goTwo hazards, both tested
The readiness line is forgeable.
init()runs beforemain, so a workload can always print a well-formed line of its own choosing first — "first match wins" is defeated by construction, and a launcher that trusts the first line forwards to whatever port the workload named. Each candidate is verified before it's trusted; the real line wins.Verification is an HTTP request, not a TCP connect.
ssh -Lbinds and accepts on the local end whether or not anything is listening on the far side, so a successful connect to a forward proves only that ssh is running. A forged address passes a TCP dial and fails a request.TestRunRemoteTCPDialWouldAcceptAForgedAddressdemonstrates the difference directly rather than asserting it in a comment.Any HTTP status counts, including 401 and 404: the question is whether a server is there, not whether the caller is authorized. Rejecting non-200s would reject every authenticated service as forged.
Lifetime and cost
Closing an SSH session does not reliably kill what it started — no TTY means no SIGHUP — so a service would keep serving, and keep billing, after the launcher exits. The remote command is wrapped: it backgrounds the service and blocks on stdin, then TERM→KILLs on EOF.
That's a request, not a guarantee; a partition can strand the remote process. The instance TTL is the guarantee, so:
guardZombieInstance— the same mandatory-TTL guard as every other launch (a flag-driven verb loses the spec's TTL guarantee, so it has to)OnComplete: "terminate", notstop— a stopped instance still holds a billable EBS volume--dry-runquotes the rate and therate × ttlbound before anything is spent--hostattaches to an instance spawn did not launch, and promises no teardownExposure is loopback-only on both ends. A supplied token goes into the printed URL (or you can't connect) but never into the loggable address form.
The verb also classifies its own exit against EC2 rather than guessing: a session that ends because the TTL expired (the designed end) and one that ends because the connection dropped (a failure) look identical from inside the process, and a guess would either cry wolf on every expiry or hide a real drop.
Testing
go test ./pkg/service/ ./cmd/— green, including with-race. The whole contract is exercised end to end at $0 over a local loopback path: real child process, real readiness line, real forward, driven through the forward, including both forgery cases. The Substrate emulator is control-plane only (instances never boot, no SSH), so the emulator could not have kept the first real spawn from being the first test.Every guard was mutation-tested to confirm its test is load-bearing —
terminate→stop, re-appending the addr args, dropping the--hostguard, disabling the idle default in the dry run, mutating the caller's args in place,if truein the hold loop, treatingstoppedas still-serving,HTTPProbe→TCPDialer, breaking the stdin gate, removingstdin.Close()fromStop. Each mutation fails at least one test. One of these found a real gap: the exit classification had no coverage at all, which is whyholdServicenow takes injectablesessionDone/instanceGoneandinstanceStateIsGoneis extracted.Note on CI
TestCatalogValidfails on this branch and onmain— the shipped catalog references private ECR images for chimerax/paraview (#392). Unrelated to this change; confirmed pre-existing by stashing.make checkrunsgofmt -w ., which also wants to reformat three unrelated files carrying pre-existing drift (lambda/dashboard-api/auth.go,pkg/launcher/bootstrap_test.go,pkg/scheduler/scheduler.go). Left alone — one topic per PR.Deliberately not here
Migrating
cmd/plugin.go:withSSHTunnelandpkg/plugin/push_ssh.go:pushViaTunnelontoservice.OpenTunnel. Both predate this and duplicate the forward logic; that's a separate topic.