Skip to content

feat(assistant): record cost and outcome of discovery runs and Matter activations - #6321

Merged
liugddx merged 2 commits into
mainfrom
feat/discovery-cost-observability-6309
Oct 27, 2026
Merged

liugddx merged 2 commits into
mainfrom
feat/discovery-cost-observability-6309

Conversation

@liugddx

@liugddx liugddx commented Oct 26, 2026

Copy link
Copy Markdown
Member

Summary

Decision 4 on #6247 is to add cost visibility before any limit. This records what each discovery run and each Matter activation cost and how it ended, and limits nothing.

  • Every heartbeat discovery, quick discovery and Matter activation appends one row to a local run-cost ledger: kind, start, end, outcome, and the model usage its Sessions accrued in that window (calls; input, cache read/write and output tokens; model time).
  • Cost is not a new metering path. Usage is read from the canonical Usage ledger by Session id and time window, the technique hosted-execution settlement already uses. Discovery workers are dedicated background Sessions, so a user's Session cost never includes them (feat(usage): separate auxiliary model calls from a Session's cost and cache hit rate #5691).
  • A run that finishes and hands over nothing is recorded as no_report, so reported and silent runs can be told apart (RFC(assistant): tool boundaries, delegation ownership and heartbeat cost of the discovery session #6247 item 6).
  • An unreadable or partial Usage window is recorded as incomplete instead of as zero.
  • The ledger is written after the run, swallows its own failures, and nothing reads it back. No run is limited, delayed or rerouted.
  • A summary (daily totals per kind, activations per Matter, recent runs) is exposed in the assistant status.

Fixes #6309
Refs #6247

Verification

Node 24.21.0, on origin/main@da614305c:

Check Result
tsc build of core, storage, mcp, runtime, runtime-host pass
npm run typecheck in packages/runtime-host pass
biome check on the 12 changed files pass
npm run check:asf-headers pass
npm run test:assistant in packages/runtime-host 268 of 268 pass (93 + 89 + 86), including the new run-cost tests
usage and execution-composition dist tests 108 of 109 pass

The one failure, assistant Host fallback registers a task workspace usable by ordinary Session creation and binding, fails the same way on origin/main@da614305c without this change, so it is not introduced here. I did not investigate its cause. The full workspace test suite and the desktop build were not run.

New tests cover: a run that reports is recorded with both workers' cost and kept apart from the conversation; a quick run that reports nothing is recorded as no_report with its model cost; an unreadable Usage ledger is recorded as unknown cost, not zero cost and not a stalled run.

Review focus

  • Where the hooks sit: finally blocks in the Matter controller and the two discovery controllers. A recording failure must not change a run's outcome; that is what the swallow-and-continue in the ledger guarantees.
  • readSessionUsageWindow in canonical-usage-reader.ts marks a window incomplete when the Usage ledger has partial, missing, unreadable or unrepaired rows.

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code (Claude Opus 5.5) drafted the implementation and tests from the #6309 acceptance list. I reviewed the diff and ran the checks above. The commit carries a Generated-by trailer.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

🤖 Generated with Claude Code

… activations

Every discovery run (broad heartbeat, quick discovery) and every Matter
activation now appends one row to a local run-cost ledger: kind, start,
end, outcome, and the model usage its Sessions accrued in that window.
Usage is read from the canonical Usage ledger by Session and time window,
the technique hosted-execution settlement already uses, so no new metering
path is added and discovery cost stays apart from the user's Session cost.

A run that finishes and hands over nothing is recorded as no_report, and an
unreadable or partial Usage window is recorded as incomplete rather than
as zero. The ledger is written after the run, swallows its own failures,
and nothing reads it back to limit, delay or reroute a run. A summary
(daily totals per kind, activations per Matter, recent runs) is exposed in
the assistant status.

Refs #6309, #6247

Generated-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@liugddx liugddx self-assigned this Oct 26, 2026
@github-actions github-actions Bot added the effort/L Under 1000 readable lines label Oct 26, 2026

@me2seeks me2seeks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found one reproducible outcome-accounting bug. The 32 focused cost/quick tests pass on Node 24.18.1, but adding a retryable network failure to the existing Matter cost fixture records interrupted instead of retried.

…rrupted

A retryable terminal failure makes completion() call retryExecution(), which
clears the activation on purpose and sets the outcome to retried. Execution
then fell through to the classification that reads a missing activation as the
user interrupting the run, and overwrote retried with interrupted. Every
automatic retry was therefore counted as a user interruption.

Skip that classification once a retry has decided the outcome. Add tests for a
retryable (network) failure and a non-retryable one, so the fix cannot
over-correct.

Refs #6309

Generated-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@liugddx

liugddx commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

Thanks @me2seeks, reproduced and fixed in b03705e.

Root cause as you described: completion() returns after retryExecution() cleared the activation, and the classification after it read the missing activation as interrupted. It now skips that classification once a retry has decided the outcome.

I added your scenario as a test: a retryable network failure is recorded as retried. It fails without the fix (expected retried, got interrupted). A sibling test checks a non-retryable failure is still failed, so the fix does not over-correct. test:assistant passes (93 + 89 + 88) on Node 24.21.0, plus typecheck and biome.

@me2seeks me2seeks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the updated head. The retry outcome is now preserved and covered by retryable and non-retryable regression cases. Verified the previous finding is fixed; local assistant tests pass (93 + 89 + 88), hosted CI is green, and the review thread is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(assistant): record cost and outcome of discovery runs and Matter activations

2 participants