Files
accounted/lib/events
Jakob Wennberg 10bbfb9d79 fix(mcp): make agent failures diagnosable from the log, not just from the source (#2087)
* fix(mcp): make agent failures diagnosable from the log, not just from the source

Mining event_log for mcp.tool_called: VALIDATION_ERROR ran at about one a day
until 2026-08-25, then jumped to a hundred a day and stayed there. One
integration's gnubok_get_kpi_report has been refused 604 times over seven days
and is still failing.

The cause is the unknown-parameter guard from #1856, which is correct and
stays. The caller is even told precisely what is wrong: getStructuredError puts
"Unknown parameter "x" for <tool>. Valid parameters: ..." into message_en,
which the agent receives.

What was broken is what we recorded about it. Two things, both cheap:

- Telemetry logged only message_sv, which for VALIDATION_ERROR is the registry
  default "Förfrågan innehåller ogiltiga uppgifter." and names neither the
  parameter nor the tool. A seven-day outage was indistinguishable from a typo,
  and the cause was only findable by reading the dispatcher source. errorDetail
  now carries message_en, stored only when it differs from errorMessage, so the
  many domain failures whose message_sv is already the specific text cost
  nothing extra.
- errorKind said 'company_access_denied' for all 604, because the arg guard
  throws inside the company-routing try. That is an active misdirection: it
  sends triage looking for a tenancy bug that does not exist. Exactly two
  things in that block raise VALIDATION_ERROR, the unknown-parameter guard and
  a malformed company_id, and neither is a permissions failure, so they now log
  as 'invalid_arguments'.

Tests reproduce the production call shape and were verified to fail when either
fix is reverted.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(mcp): derive the telemetry payload type from the event contract

Two review findings, both valid.

The local ToolCalledPayload interface was a hand-maintained duplicate that
omitted sessionId and half the errorKind union. That is not cosmetic: it is why
errorDetail could be added to the emitter and to lib/events/types.ts while the
test file type-checked against a stale shape. Deriving it from
EventPayload<'mcp.tool_called'> removes the drift.

The null-detail assertion was also conditional, so it would have passed on a
wrong-but-present value. Replaced with two determinate cases: a scope denial
carries both languages without duplicating either, and the unknown-tool exit,
which supplies no diagnostic, stores null.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(mcp): pin the scope name in the diagnostic assertion

'A different string' passes on any placeholder. The reason errorDetail is worth
storing is that it names what the caller lacks, so assert that.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 22:24:28 +02:00
..
2026-02-19 09:48:02 +01:00