Skip to content

refactor(observability): collapse duplicates after beta.3#37

Merged
arapov merged 1 commit into
masterfrom
refactor/observability-cleanup
May 20, 2026
Merged

refactor(observability): collapse duplicates after beta.3#37
arapov merged 1 commit into
masterfrom
refactor/observability-cleanup

Conversation

@arapov
Copy link
Copy Markdown
Collaborator

@arapov arapov commented May 20, 2026

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 emitToolCall helper (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.chain emission into withRequestContext (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 /mcp handler shrinks to a clean three-line wrapper:

await withRequestContext({ clientId }, async () => {
  await server.connect(transport);
  await transport.handleRequest(req, res, req.body);
});

The emission fires on scope exit even when fn throws (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 logEvent becomes a declarative chainHandlers table. Adding a new event type that should feed the chain is now one row.

Test plan

  • npm run typecheck clean
  • npm run lint clean
  • npm run format:check clean
  • npm test460 / 460 (no test count change, refactor)
  • npm run build succeeds — dist/index.js 144.74 KB, dist/http.js 170.83 KB
  • One existing test (tool.chain aggregates the request's tools and capsule calls) updated to drop its now-redundant manual emission — withRequestContext does it automatically
  • Privacy sweep clean

🤖 Generated with Claude Code

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.
@arapov arapov merged commit 56ff1a8 into master May 20, 2026
1 check passed
@arapov arapov deleted the refactor/observability-cleanup branch May 20, 2026 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant