✨ Replace free-port probing with attached services - #390
Conversation
PR #390: ✨ Replace free-port probing with cooperative services53 files, +3262 / -1918 Scope🔴 PR has 5180 lines changed. Split into focused PRs. 🟡 5180 lines changed. PRs under 400 receive more thorough review. 🟡 53 files changed. Are all changes related? Structural✅ No structural bloat detected. Slop✅ Slop indicators look low. Static Analysis✅ Oxlint found no issues. CorrectnessNo extraneous code patterns detected. |
taras
commented
Aug 7, 2026
The scope warning is understood. This is one cross-package behavioral migration rather than several independent features:
The branch is arranged as three dependency-ordered commits for review: host/runtime API, replay-safe modifiers and integration, then documentation. Every changed file maps to an acceptance criterion in #381, and the aggregate ten-command verifier passes with an unchanged tracked tree. |
Uh oh!
There was an error while loading. Please reload this page.
Why
Free-port probing reserves nothing, so a provider can lose the selected port before its process binds. It also gives durable replay no way to distinguish a live invocation-local endpoint from persisted state.
Closes#381.
Closes#137.
What changes
Before:
Providers selected a port with
findFreePort, launched a process separately, and could persist or replay an endpoint that no longer named a live service. The first implementation also delayed unterminated service stdout until teardown, allowed durable eval to collide with a live service binding, and did not translate observable process teardown failures.After:
A
service=<binding>exec block starts a scoped attached service. The child binds loopback port 0, authenticates one exact handshake record with a per-attachment token, and publishes a frozen endpoint only to the current invocation's live environment.ephemeral evalreruns on partial replay and can consume that endpoint without journaling it.Ordinary stdout is forwarded incrementally, while bounded handshake candidates are suppressed. Durable eval rejects exports that collide with live bindings before live execution, replay restoration, or an eval journal append. Ephemeral eval rejects durable collisions but may update an existing ephemeral live binding. Observable process teardown failures become
ServiceTeardownErrorand compose with active execution failures through invocation teardown aggregation.Completed replay does not start the attached service, and workflow execution explicitly denies inherited host service providers.
How it works
The stdout observer retains only bytes that can still match
XMD_SERVICE_READY:at line start. A mismatch releases those bytes immediately; only a real handshake candidate is buffered, with a finite bound. The durable operation validates live-name collisions before either execution or replay restoration, while consuming a matching replay entry so partial replay remains coherent.Review guide
Start with:
packages/cli/src/service-host.tsandpackages/core/tests/ephemeral-service.test.tsThen review:
packages/core/src/live-env.tsandpackages/core/src/eval-handler.tsfor namespace collision rulespackages/durable-streams/effect.tsfor validation before live execution or replay restorationpackages/cli/tests/service-host.test.tsfor byte-level observation, concurrency, cancellation, and teardown aggregationpackages/cli/tests/service-document.test.tsfor real-process supervision and nested lifetimesspecs/executable-mdx-spec.mdfor the durable/live contract and test matrixLook carefully at:
What must stay true
How to verify it
./dist/xmd test smoke-test/attached-service-ping-pong.test.md --component-dir smoke-test --component-dir packages/core/components --rawruns the real two-attachment ping-pong document through the compiled binary and production host adapter.deno test --allow-all packages/cli/tests/service-document.test.tsproves journal isolation, partial/completed replay, supervision, cleanup, and nested lifetime behavior with real processes.deno test --allow-all packages/cli/tests/service-host.test.tsproves incremental handshake observation, concurrent attachments, cancellation, and teardown errors.deno task verifypasses all ten applicable commands concurrently: lint, check, Deno tests, JSR dry-run, Node 22 typecheck/tests, Bun tests, docs, site check, and site build.ping→pong→pingexchange whose endpoints remain live-only across partial replay, live unterminated stdout, split byte forwarding, split/duplicate/bounded handshake-record suppression, partial-replay collisions, teardown error aggregation, post-handshake cancellation and listener release, unexpected exit during projected content, prompt failure cleanup without restart, and nested teardown ordering.Scope
Included
service, andephemeral evalfindFreePortand the Llamafile-specific provider exampleIntentionally unchanged
daemonremains the fixed-configuration subprocess primitive and does not publish an endpoint.New abstractions
Service,ServiceAttachment, andServiceEndpointdefine provider-neutral scoped attachment.The live environment carries invocation-only bindings without entering durable state.
EphemeralEvalgives middleware a scoped, non-journaled eval boundary.Durable-operation validation runs before live execution or replay restoration so a rejected operation creates no result event.
Each new abstraction has multiple concrete uses or a clear justification.
No speculative functionality is included.
Generated or mechanical changes
site/routes/docs/exec-eval.tsxwas formatted with the site formatter after its documentation update.Risks and limitations
127.0.0.1; non-loopback providers are rejected.Scope confirmation