Repository navigation
feat(agent-email): auto-reap sidecar on parent exit/crash (no manual wiring) - #1841
Conversation
…tion/unhandledRejection) Extend the "Running in production" shutdown guidance to cover a hard crash. The prior snippet only wired SIGINT/SIGTERM; an uncaught throw exits the parent but leaves the detached sidecar holding its port. Documents the reap() funnel pattern and explains why direct process.exit() bypasses async shutdown.
…pt-out via autoCleanup:false) Install process handlers (exit, uncaughtException, unhandledRejection, SIGINT, SIGTERM, SIGHUP) on first spawnSidecar call so the detached email-agent binary is always reaped when the parent goes away. No consumer wiring required. Pass autoCleanup:false to own the lifecycle yourself. shutdown() remains the graceful path; the auto-reaper is a SIGKILL backstop on top of it.
|
Verdict: Approve with suggestions — no blocking issues. This makes the email-agent npm sidecar reap itself automatically when the host process exits, crashes, or is interrupted, instead of leaking the frozen binary (and holding port 8131) whenever a consumer forgot to call One thing worth a conscious decision before merge: by default, importing and spawning now installs process-global 🔍 Technical details🟡 Important (worth confirming, not blocking)Default-on global crash handlers reap on unrelated errors ( 🟢 MinorPossible truncated stderr on the sole-listener crash path ( console.error(err);
process.exit(1);When stderr is a pipe/file (not a TTY), writes are async and an immediate Unit tests cover install/opt-out but not the reap itself ( Cross-platform signal registration ( Strengths
|
|
Thanks — addressed the suggestions in e2ff10b:
|
|
🟡 The new SIGKILL assertion in the reap test will fail on Windows — 🔍 Technical details
expect(killSpy).toHaveBeenCalledWith(-12345, "SIGKILL"); // Windows: never called
if (process.platform === "win32") {
const { spawnSync } = vi.mocked(await import("node:child_process"));
expect(spawnSync).toHaveBeenCalledWith(
"taskkill", ["/PID", "12345", "/T", "/F"], { stdio: "ignore" },
);
} else {
expect(killSpy).toHaveBeenCalledWith(-12345, "SIGKILL");
} |
|
Nice — the auto-reap is the right default, and the README "Running in production" rewrite reads well. One consistency thing worth folding into this PR before it ships in agent-email 0.2.1: the same lifecycle claim this PR corrects in the README also lives in The spots to sync to the new auto-cleanup behavior: 🔍 Exact lines
Note: "the package does not restart a crashed sidecar" stays accurate — this adds auto-reap, not auto-restart. Happy to hand you the exact diffs for SPEC.md/SKILL.md if that's easier than re-deriving them — just say the word. |
|
Fixed the Windows test gap in e001abf — the reap test now platform-guards its assertion: on |
The auto-reap default corrected in the README also lived in SPEC.md and SKILL.md (always-shutdown-or-orphaned); since the release publishes all three docs together they would have shipped contradicting each other.
|
Good catch — synced in d2a3d91. |
…PEC/SKILL/CHANGELOG) (amd#1842) ## Why this matters A functional change to an agent (behavior, API, contract, defaults, lifecycle, error codes) almost always has the same claim repeated across several **bundled** docs. Updating only one ships a package whose own docs contradict each other — and the contradiction goes live the moment that version publishes. This adds a rule under **Documentation Requirements** so that doesn't happen. The rule enumerates the hub-agent doc surfaces that must stay in sync — `README.md`, `SPEC.md`, `SKILL.md`, `CHANGELOG.md`, and any runtime/`openapi` spec — and says to grep the old claim/symbol/status-code across all of them before calling the change done. Concrete trigger: **amd#1841** (agent-email auto-reap) updated `README.md` to "cleanup is automatic" but left `SPEC.md` and `SKILL.md` still saying "always call `shutdown` or the child is orphaned" — both due to publish in the same 0.2.1 release. ## Test plan - [ ] Docs-only change to `CLAUDE.md`; read the new subsection under **### Documentation Requirements** for accuracy and tone.
…t 2.4 (amd#2102) A Node integrator (and the amd#1653 dogfood) couldn't reach the sidecar's streaming agent loop through `@amd-gaia/agent-email` — the 2.4 endpoints landed Python-side in amd#2061 but the package's answer was "hand-roll `fetch` + SSE parsing". Now `client.query()` streams the seven typed canonical events (frozen amd#2057 contract) as an async iterator and `client.cancelQuery(runId)` stops a run mid-flight, with bearer auth and host-minted `run_id` semantics handled for you. The client speaks contract 2.4 (`SCHEMA_VERSION` 2.3 → 2.4, additive — every 2.3 shape unchanged), and streams fail loud: a malformed event, a non-SSE response, or a stream that dies without the mandated terminal `final`/`error` throws, while an unknown event type is surfaced as a visible placeholder, never dropped. Closes amd#2097 ## Test plan - [ ] `cd hub/agents/npm/agent-email && npm ci && npm run build && npm test` — 101 tests: SSE parser (chunk splits at every boundary, multi-line data, CRLF, multibyte flush), client call-shape + event-stream unit tests, and the existing suite. - [ ] Integration (needs a venv with the repo installed): `GAIA_EMAIL_TEST_PYTHON=<repo-venv>/bin/python npm test` — `startSidecar` spawns the real Python sidecar in dev mode (scripted fake-agent seam, the same one `test_query_route.py` swaps) and asserts the 2.4 version handshake, the `status→tool_call→tool_result→final` sequence, mid-run cancel via `cancelQuery`, the 401 bearer gate, and the 404 cancel. Skips visibly on npm CI (no capable interpreter there). - [ ] Docs updated together per amd#1841 — `grep -rn 'no wrapper yet' hub/agents/npm/agent-email/{README,SPEC,SKILL}.md` no longer matches the `/query` rows.
CLAUDE.md requires README, SPEC.md, SKILL.md and CHANGELOG.md to change together for a hub package (#1841 shipped a SPEC and SKILL contradicting the README). The checklist only compared the README with the code.
Closes #1840
Before, the package spawned the email-agent sidecar detached and only reaped it if the consumer explicitly called
shutdown()— a crash or Ctrl+C orphaned the frozen binary, which kept holding port 8131 until the nextEADDRINUSE. After, the package auto-reaps its own sidecar by default on exit / crash / SIGINT / SIGTERM / SIGHUP, so consumers get no leak with zero wiring. Opt out withautoCleanup: falseto manage the lifecycle yourself;shutdown()remains the graceful, awaited path.Proof (branch build + real frozen binary, darwin-arm64)
Built from this branch; verified
autoCleanuppresent indist/lifecycle.js(grep -c autoCleanup node_modules/@amd-gaia/agent-email/dist/lifecycle.js→ 1). No consumer signal handlers in any script.Test 1 — crash-default (autoCleanup: true, default)
→ REAPED — auto-cleanup caught the uncaughtException, port 8131 free.
Test 2 — sigint-default (autoCleanup: true, default)
→ REAPED — auto-cleanup caught SIGINT, port 8131 free.
Test 3 — optout-leak (autoCleanup: false)
→ LEAK — opt-out confirmed: sidecar survived the crash, proving the default auto-reaper is what does the work.
Handlers needed:
uncaughtException+unhandledRejectionin addition to"exit"and signals — on Node v20,"exit"alone does not fire for an uncaught exception before the process terminates.Tests
tscclean, no errorsautoCleanup: falseleaks (opt-out verified)Test plan
cd hub/agents/npm/agent-email && npm install && npm run build && npm test— must pass (35 tests, tsc clean)node crash-default.mjsexits with uncaught error;lsof -nP -iTCP:8131 -sTCP:LISTENreturns nothing (port free)node optout-leak.mjs→ port still held after crash (opt-out behavior verified)