Skip to content

feat(sdk): offline spawn/tool e2e on Node and Bun — M3 close-out (#1111) - #1122

Merged
samuv merged 4 commits into
sdk/39-parityfrom
sdk/40-e2e
Sep 7, 2026
Merged

samuv merged 4 commits into
sdk/39-parityfrom
sdk/40-e2e

Conversation

@samuv

@samuv samuv commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes the M3 milestone of the TypeScript SDK (parent #821) with the offline
end-to-end proof that the product surface — spawn(), query(), client.tool()
and the loopback MCP host — works against a same-checkout mecated on both
Node and Bun, plus the milestone's documentation close-out.

The suites drive the public API, not M1's hand-rolled daemon harness. That is
the point: everything below Scenario 10 is proven against an injected Transport,
so this is the first place the SDK's own launcher, ready-file barrier, lifetime
descriptor, disposal order and hand-written MCP subset meet the real Go
implementation.

  • New sdk/typescript/e2e/spawn.e2e.test.ts and e2e/tool.e2e.test.ts, driven by
    --mock-script fixtures under e2e/fixtures/.
  • Bun leg runs for real. e2e/fixtures/runtime-helper.mjs imports the built
    package, so Bun exercises the published ESM shape rather than Vitest's transform.
  • M3 close-out: plan status flipped to landed, docs/architecture.md and
    docs/design/IMPLEMENTATION-NOTES.md completed, and a short spawn()/tool()
    section added to user-docs/.

Closes #1111
Related: #821 — do not close; M4 (browser/BFF, examples, platform matrix) remains.

Design: ADR 0292.
Plan: docs/acceptance/sdk-typescript-local.md (Scenario 10).

This is the top of the stack and targets sdk/39-parity (#1099). No scenario
stacks on top of it.

Acceptance criteria

AC Proof
AC10.1 a scripted turn calls an sdk tool and receives its result
AC10.2 the tool round trip passes on bun
AC10.3 the spawned daemon opens no tcp port
AC10.4 close stops the daemon and removes its runtime directory
AC10.5 a killed parent stops the daemon through the lifetime pipe (Node and Bun)
AC10.6 retainSession survives on the wire and the default does not
AC10.7 a real startup failure is typed with a redacted tail and no orphan
AC10.8 a throwing handler is generic on the wire and the run continues
AC10.9 read-only and mutating tools both reach the model
AC10.10 a mutating tool asks and a responder-less query denies it
AC10.11 the real go mcp client completes the handshake against the sdk host
AC10.12 inspection — CI job composition, see below
AC10.13 a callback tool is denied by the default capability-set evaluator

task ac-trace-strict: docs/acceptance/sdk-typescript-local.md [landed] — 104 ACs · 0 failure(s).

AC10.12 — CI job composition

The sdk job runs the new suites on Node and the Bun leg genuinely runs; it is
not recorded as deferred. .github/workflows/ci.yml pins oven-sh/setup-bun
(SHA-pinned, bun-version: '1.4.1'), and task sdk:e2e preflights
test -x "$BUN_BIN" || command -v bun || exit 1. There is no skipIf and no
try/catch swallow anywhere in the suites, so a missing or broken Bun turns the
step red rather than silently reducing coverage.

The two Bun deferrals the plan carried are now resolved and recorded as such:
Bun inherits the fourth stdio entry, and the lifetime descriptor works without the
mkfifo(1) dance because the server-enabler pre-PR widened checkLifetimePipeFD
to admit a connected stream socket. lifetimePipe: false was not needed.

Known limitation — callback tools are denied under the default authority evaluator

Found while writing these tests, on the real wire:

tool.result  content='tool "mcp__sdk__lookup" denied by authority: tool is absent
             from the capability set'  isError=true

mintRootAuthority (internal/app/root_authority.go) mints the root capability set
from the process-wide assets.rootCatalog, and Service.RootAuthority is a
func(session.SessionKind) session.Authority that never sees a session's
mcp_servers additions. A client MCP tool therefore mounts into the per-session
catalog but is absent from the capability set, and the default
--authority-evaluator local denies the call after the permission ask has already
been allowed. spawn() does not pass the flag, so every default SDK deployment
hits this
: M3's callback tools are wire-complete but not usable end-to-end yet.

Fixing it means widening RootAuthority to carry the session's client MCP tool
names — production internal/adapter/server/ work, outside this plan's edit surface.
Rather than hide it:

  • the other fixtures pass --authority-evaluator noop, so the suite proves the SDK
    half (host handshake, dispatch, schema validation, refusals, ask routing) instead
    of silently re-proving the denial;
  • AC10.13 pins the default-posture denial, so the eventual fix has a failing test
    to flip rather than a silent behaviour change;
  • it is recorded in the plan's Deferred decisions and known risks, in
    docs/architecture.md, in IMPLEMENTATION-NOTES.md, and in the user-docs/
    callback-tool section so a reader is not led into it.

Panel review

Ran a four-axis panel (Spec / Standards / Test adequacy / Domain: security,
architecture, DevOps, library-reuse, duplication) over the assembled stack
sdk/31..sdk/40 vs main. Spec reported no blockers and verified all 96 vitest:
proof tokens decode to titles present byte-for-byte. One blocker and several
important findings were fixed here — they belong to earlier scenarios, but the stack
merges as a unit and rebasing nine dependent branches to place each fix in its
originating PR would cost more than it buys. Called out so reviewers of those PRs
know to look here:

  • Blocker — a rejected spawn() hung Node for the whole readiness window.
    Promise.race does not cancel its loser, so after a child exited early the
    ready-file poll kept real 20 ms timers alive. A CLI that caught the failure sat
    there for 30 s before the process could exit. The scheduler seam now carries an
    AbortSignal (as the watch.ts sibling already did) and the race aborts it in a
    finally. Measured 5019 ms before, 6 ms after; regression test mutation-checked
    (1888 → 5 polls).
  • The same leak was on the shutdown path. stopProcess races the child's exit
    against a SIGTERM grace timer; a child that exited promptly still left a 3 s
    non-unref'd timer armed. Measured against a real daemon: close() resolved at
    +6 ms while the process could not exit until +3007 ms — an SDK-backed CLI paid
    that tail on every clean shutdown. Both stop bounds now abort their sleep;
    mutation-checked.
  • Security — the SDK-owned listener flags were bypassable by spelling. mecated
    parses with Go's flag package, where -flag, --flag and -flag=v are
    equivalent and the last occurrence wins, so
    spawn({ args: ["-http-addr", "0.0.0.0:8080"] }) replaced an SDK-owned value and
    opened the unauthenticated HTTP surface. The guard now normalises the spelling;
    tested as a table over all five flags × four spellings.
  • Security — PATH resolution mapped empty and relative entries onto the working
    directory
    (CWE-426), so a co-located mecated could be launched with the caller's
    inherited credentials. Absolute entries only, matching Go's removal of the same
    behaviour from os/exec in 1.19.
  • The e2e/ tree was outside tsconfig include and had already rotted. Seven
    call sites still passed the workspace option ADR 0291 removed from
    CreateSessionOptions; the property was dropped at runtime while the tests read as
    though they bound a workspace. The tree is now type-checked — which matters
    precisely because this PR makes the e2e suite a CI gate and triples its size.
  • The . entrypoint purity guard did not guard. It was a regex denylist naming
    four specifiers and missing dynamic imports, so node:crypto/node:net would have
    passed. Now an allowlist over statement-anchored specifiers; verified by planting
    import { randomBytes } from "node:crypto" in src/client.ts and watching it fail.
  • AC10.9 was confounded. Its read-only half ran --posture auto, which allows
    every tool, so expect(asks).toEqual([]) held regardless of the readOnly flag —
    it would have passed with readOnly: false. Both halves now run the same default
    posture and the test asserts what readOnly actually controls: two calls
    overlapping in the concurrent read batch. Mutation-checked. The AC text was
    corrected too — readOnlyHint steers dispatch, not permissions.

Type of change

  • Other (describe): offline end-to-end test coverage + milestone documentation
    close-out, with panel-review fixes to the stack below it

Test plan

Required baseline

  • Linting (task lint)
  • Offline test suite (task test)
  • Offline demo (go run ./cmd/mecademo)

Conditional checks

  • Markdown changed: documentation generation and checks (task docs) — 317 documents, 3053 references, 0 broken
  • User docs or user-facing behavior changed: user-docs site build (task site:build)
  • Guarded engine API surface affected: compatibility check (task api:check)
  • Intentional guarded engine API change: baselines regenerated — n/a, no engine/ change
  • Manual testing (describe below)

SDK gates: task sdk:install sdk:lint sdk:typecheck sdk:test sdk:build sdk:pack sdk:api:check
— 33 files, 183 tests. task sdk:e2e (with Bun 1.4.1) — 9 files, 26 tests.
task ac-trace-strict — 0 failures.

Manual: reproduced the readiness-poll hang before the fix (process exited after
5019 ms) and confirmed 6 ms after; reproduced the authority denial on the real wire
with the default evaluator.

Does this introduce a user-facing change?

Yes — user-docs/building/deployment/grpc-http.md documents spawn(), query() and
client.tool() for Node and Bun, including the callback-tool authority limitation
above. The two src/spawn.ts security fixes also tighten spawn(): an SDK-owned
listener flag is now refused in every spelling, and PATH lookup ignores empty and
relative entries.

Special notes for reviewers

Follow-ups deliberately not taken here, with the panel's reasoning:

  • Widen RootAuthority to carry a session's client MCP tool names. The real fix
    for the limitation above; needs production internal/adapter/server/ changes.
  • http: true opens an unauthenticated loopback listener. Any local user can
    reach it; the SDK never passes --auth-token. Documented, not yet mitigated —
    minting a token needs the SDK's own client to send it.
  • The spawnInternal test seam is hand-faked in six suites and the diagnostics
    observer guard is written four times. Both are real duplication; deferred as a
    test-side refactor rather than widening this PR's blast radius.
  • sessions.fork() silently drops the callback tool set (the server passes nil
    client MCP for a successor). Worth a loud refusal.
  • Smaller reuse items: AbortSignal.prototype.throwIfAborted, events.once for the
    listen/error race, for await for the bounded body read, lazy tool-host start for
    zero-tool clients.

@samuv samuv changed the title sdk/40 e2e feat(sdk): offline spawn/tool e2e on Node and Bun — M3 close-out (#1111) Sep 4, 2026
@samuv samuv self-assigned this Sep 4, 2026
Exercise the product spawn() against a same-checkout mecated driven by
--mock-script fixtures, on both Node and Bun, and close out M3.

The suites cover the tool round trip, a throwing handler, read-only and
mutating dispatch, a responder-less query denial, the real Go MCP client
handshake, the absence of any TCP listener, runtime-directory removal,
lifetime-pipe shutdown after an abrupt parent death, retainSession, and a
real mecated startup failure.

AC10.13 pins a known limitation: the root capability set is minted from
the process-wide catalog, so a per-session client MCP tool is absent from
it and the default --authority-evaluator local denies the call after the
permission ask has been allowed. The remaining fixtures select the no-op
evaluator so the suite proves the SDK half rather than re-proving that
denial. Fixing it needs RootAuthority to carry the session's client MCP
tool names, which is production server work outside this plan.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
Four-axis panel review of sdk/31..sdk/40 against main. The findings below
were raised against earlier scenarios but are fixed here because the stack
merges as a unit and rebasing nine dependent branches to place each fix in
its originating PR would cost more than it buys.

Blocker — a rejected spawn() held the event loop for the whole readiness
window. Promise.race does not cancel its loser, so after a child exited
early the ready-file poll kept real 20ms timers alive: a CLI that caught the
failure sat there for 30 seconds before Node could exit. The scheduler seam
now carries an AbortSignal (as the watch.ts sibling already did) and the
race aborts it in a finally. Measured 5019ms before, 6ms after.

Security — the SDK-owned listener flags were guarded only in their
double-dash spelling, but mecated parses with Go's flag package, where
-flag, --flag and -flag=v are equivalent and the last occurrence wins.
spawn({args:["-http-addr","0.0.0.0:8080"]}) therefore replaced an SDK-owned
value and opened the unauthenticated HTTP surface. The guard now normalises
the spelling before matching.

Security — PATH resolution mapped empty and relative entries onto the
working directory, so a co-located mecated could be launched with the
caller's inherited credentials. Absolute entries only, matching Go's own
removal of that behaviour from os/exec in 1.19.

The e2e tree was outside tsconfig's include, so tsc never saw it. Seven call
sites still passed the `workspace` option that ADR 0291 removed from
CreateSessionOptions; the property was silently dropped at runtime while the
tests read as though they bound a workspace. The tree is now type-checked
and those arguments are gone.

The `.` entrypoint purity guard was a regex denylist naming four specifiers
and missing dynamic imports, so node:crypto and node:net would have passed.
It is now an allowlist over statement-anchored specifiers.

AC10.9 was confounded: its read-only half ran --posture auto, which allows
every tool, so the assertion held regardless of the readOnly flag. Both
halves now run the same default posture and the test asserts what readOnly
actually controls — two calls overlapping in the concurrent read batch —
while recording that readOnlyHint steers dispatch, not permissions.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
The readiness-poll fix left the same leak on the shutdown path. stopProcess
races the child's exit against a SIGTERM grace timer, and Promise.race does
not cancel its loser, so a child that exits promptly still left a
non-unref'd 3s timer holding the event loop.

Measured against a real daemon: close() resolved at +6ms while the process
could not exit until +3007ms. An SDK-backed CLI paid that tail on every
clean shutdown. Both stop bounds now abort their scheduler sleep in a
finally, and the regression test is mutation-checked.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
@samuv
samuv merged commit 991d80d into main Sep 7, 2026
24 checks passed
@samuv
samuv deleted the sdk/40-e2e branch September 7, 2026 08:10
@samuv samuv mentioned this pull request Sep 7, 2026
6 of 13 tasks
kantord added a commit that referenced this pull request Sep 16, 2026
sdk/typescript/e2e/tool.e2e.test.ts had a test deliberately asserting
the OLD (buggy) denial as correct, with a comment explicitly saying
"so the eventual RootAuthority widening has a failing test to flip"
(introduced in #1111/#1122, M3 close-out). The previous two commits
are exactly that widening, so CI's offline SDK e2e caught it: this
test still expected the call denied.

Updated it to assert the tool is now actually invoked and its real
result reaches the model, using the same fixture/assertions as the
file's existing happy-path test, still under the real "local"
evaluator (not noop) so it keeps exercising the authorization path,
not just mount+dispatch. Verified locally against a same-checkout
mecated build.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
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.

sdk/40: offline spawn/tool e2e on Node and Bun

1 participant