refactor(observability): collapse duplicates after beta.3#37
Merged
Conversation
Post-beta.3 cleanup pass on the observability code. No behaviour
change. Three targeted simplifications:
1. Extract `emitToolCall` helper in src/server/register-tool.ts.
Three near-identical `logEvent("tool.call", ...)` blocks
(registerTool success + error, registerToolTask IIFE) collapse
to one-line calls. Single place to change the event shape.
2. Move `tool.chain` emission into `withRequestContext` itself.
The chain lifecycle now lives in the helper that creates the
context — callers can never forget to emit. src/http/app.ts's
`/mcp` handler shrinks to a clean three-line wrapper. The
emission fires on scope exit even when fn throws, so partial
chains stay observable on error paths.
3. Replace the inline if/else in `logEvent` ctx-mutation with a
declarative dispatch table. Adding a new event type that should
feed the chain is now one row.
460 tests still pass. Bundle: dist/index.js 144.74 KB, dist/http.js
170.83 KB. The observability surface (event names, fields, gating)
is unchanged.
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.
Summary
Post-beta.3 cleanup pass on the observability code. No behaviour change — same events, same fields, same gating. Three targeted simplifications that reduce duplication and make the contracts clearer.
Changes
1. Extract
emitToolCallhelper (src/server/register-tool.ts)Three near-identical
logEvent("tool.call", ...)blocks (registerTool success branch, registerTool error branch, registerToolTask IIFE) collapse to one-line calls. Single place to change the event shape.2. Move
tool.chainemission intowithRequestContext(src/log.ts+src/http/app.ts)The chain lifecycle now lives in the helper that creates the context. Callers can never forget to emit.
src/http/app.ts's/mcphandler shrinks to a clean three-line wrapper:The emission fires on scope exit even when
fnthrows (try/finally), so partial chains stay observable on error paths.3. Replace inline if/else with dispatch table (
src/log.ts)The per-event ctx-mutation logic in
logEventbecomes a declarativechainHandlerstable. Adding a new event type that should feed the chain is now one row.Test plan
npm run typecheckcleannpm run lintcleannpm run format:checkcleannpm test— 460 / 460 (no test count change, refactor)npm run buildsucceeds —dist/index.js144.74 KB,dist/http.js170.83 KBtool.chain aggregates the request's tools and capsule calls) updated to drop its now-redundant manual emission —withRequestContextdoes it automatically🤖 Generated with Claude Code