Skip to content

feat(agent-email/npm): /v1/email/query SSE streaming client + contract 2.4 - #2102

Merged
kovtcharov merged 1 commit into
mainfrom
feat/agent-email-npm-query-client
Jul 15, 2026
Merged

kovtcharov merged 1 commit into
mainfrom
feat/agent-email-npm-query-client

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

A Node integrator (and the #1653 dogfood) couldn't reach the sidecar's streaming agent loop through @amd-gaia/agent-email — the 2.4 endpoints landed Python-side in #2061 but the package's answer was "hand-roll fetch + SSE parsing". Now client.query() streams the seven typed canonical events (frozen #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 #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 feat(agent-email): auto-reap sidecar on parent exit/crash (no manual wiring) #1841 — grep -rn 'no wrapper yet' hub/agents/npm/agent-email/{README,SPEC,SKILL}.md no longer matches the /query rows.

…t 2.4

A Node integrator could not reach the sidecar's streaming agent loop through
the package — the 2.4 endpoints (POST /v1/email/query + /query/{run_id}/cancel,
#2061) existed only as raw-fetch instructions in the docs. Now
EmailClient.query() returns an async iterator of the seven typed canonical SSE
events (frozen #2057 contract) and cancelQuery(runId) stops a run between
steps, with the per-session bearer and host-minted run_id semantics handled by
the client.

- SCHEMA_VERSION 2.3 -> 2.4 (additive; every 2.3 shape unchanged), lifecycle
  expected-version default follows it.
- Fail-loud stream handling: non-SSE response, malformed event, or a stream
  that closes without the mandated terminal final/error throws
  QueryStreamError; an unknown event type is yielded as a visible
  { type: "unknown" } placeholder, never dropped (spec §7).
- Unit tests: incremental SSE parser (arbitrary chunk splits, multi-line data,
  CRLF, multibyte flush at EOF) + client call-shape and event-stream behavior.
- Integration test: startSidecar spawns the real Python sidecar in dev mode
  (scripted fake-agent seam, 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, the 401 bearer gate, and the 404 cancel. Skips
  visibly when no capable Python interpreter is available (e.g. npm CI).
- README/SPEC.md/SKILL.md/CHANGELOG updated together (#1841): the 'no wrapper
  yet' claims for /query are gone; docs show the typed iterator.

Closes #2097
@github-actions github-actions Bot added the agent::email Email agent changes label Jul 15, 2026
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Verified locally on the PR branch: npm ci && npm run build && npm test → 101/101 passed, including the 5 integration tests that spawn a real dev-mode sidecar (observed spawn → tree-kill in the run). Spot-checked the fail-loud contract: unknown event types surface as visible {type:"unknown", eventType, raw} objects (never dropped), a stream that closes without the mandated terminal final/error throws QueryStreamError, malformed payloads throw — the no-silent-fallback rule is honored end-to-end. SSE parser tests cover chunk splits at every frame boundary, CRLF, and multibyte flushes; all four doc surfaces updated together per the #1841 rule. Recommend merge. (Independent of #2100/#2101 — any order.)

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve ✅

This wraps the sidecar's /v1/email/query streaming agent loop in the typed npm client so integrators stop hand-rolling fetch + SSE parsing: query() yields typed events as an async iterator and cancelQuery() stops a run mid-flight, with contract bumped 2.3 → 2.4 (additive). The bottom line: it's clean, fails loud in all the right places (malformed event / non-SSE response / stream that dies without a terminal event all throw; an unknown event type surfaces visibly instead of being dropped), and is unusually well tested.

I found no blocking issues. Two minor things worth a glance before publish:

  • Version bump: the CHANGELOG documents this feature under a 0.6.0 heading, but package.json is still 0.5.0 (the lockfile in this PR only moved 0.4.0 → 0.5.0). Just confirm the bump to 0.6.0 lands before the package is published, or the CHANGELOG will describe a version that was never cut.
  • Type fidelity: I checked the hand-written TS types against the Python source of truth and they match exactly — no action needed, calling it out because that's the thing most likely to silently rot.

Nicely done — the "assert the outgoing call shape, not just a stubbed 200" tests and the real-sidecar integration harness are exactly the pattern the repo asks for.

🔍 Technical details

🟢 Minor

1. CHANGELOG 0.6.0 vs package.json 0.5.0 mismatch (package.json / CHANGELOG.md:8)
The new agent-loop feature is documented under ## 0.6.0, but package.json reads 0.5.0 and the only version change in the diff is package-lock.json catching up to 0.5.0. Likely intentional if the release tooling bumps at tag time — flagging so the publish step doesn't ship 0.5.0 with a 0.6.0 changelog entry.

2. Error paths leave the response body undrained (src/client.ts:379, :387)
The non-text/event-stream and !res.body branches throw without consuming/cancelling res.body. Under Node/undici an unconsumed body relies on GC to release the socket. The non-2xx branch already drains via await res.text(); a res.body?.cancel() before the non-SSE throw would match it. Very low impact (error path only), optional.

Strengths

  • Resource + abort handling is correct. timeoutMs bounds only time-to-headers (clearTimeout at src/client.ts:370), the stream runs unbounded after; the inner finally calls reader.cancel() on consumer-break/terminal/throw, and the outer finally removes the abort listener. The "consumer breaks early → transport cancelled" test (query.test.ts:1423) locks this in.
  • Fail-loud honored end to end. Malformed JSON, missing type, non-object payload, non-SSE response, and a stream closing without a terminal final/error all raise QueryStreamError; a terminal error is yielded (surfaced verbatim) while transport failures throw; an unknown type becomes {type:"unknown",…} rather than being dropped (src/client.ts:417-430). Matches the no-silent-fallback rule.
  • Types are faithful mirrors of the Python contract. Verified EmailQueryRequest / QueryContextItem / QueryCancelResponse and all seven event shapes against query_routes.py and sse_translation.py (message/delta/tool+args/tool+data+render?/run_id+action+summary/answer+usage?/detail+status) — field-for-field match.
  • Test quality is high. Call-shape assertions (path, SSE accept, bearer, exact body) prevent a contract-violating request hiding behind a stubbed 200; the SSE parser is split at every byte boundary and one-byte-at-a-time; multibyte-flush is covered; and query-integration.test.ts drives the real sidecar (2.4 handshake, cancel, 401, 404) via the same fake-agent seam the Python test_query_route.py uses. cancelQuery uses encodeURIComponent(runId) and refuses an empty id loudly.
  • Docs updated together (feat(agent-email): auto-reap sidecar on parent exit/crash (no manual wiring) #1841). README / SPEC / SKILL / CHANGELOG all move off "no wrapper yet" for /query in the same PR; the still-unwrapped POST /v1/email/init row is correctly left as-is.

@kovtcharov
kovtcharov enabled auto-merge July 15, 2026 23:31
@kovtcharov
kovtcharov added this pull request to the merge queue Jul 15, 2026
Merged via the queue into main with commit f14673a Jul 15, 2026
25 checks passed
@kovtcharov
kovtcharov deleted the feat/agent-email-npm-query-client branch July 15, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent::email Email agent changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(agent-email/npm): add /v1/email/query SSE streaming to the client + contract 2.4

2 participants