AI Agents SDE Task Viewer
      • Context
      • Plan
      • Prd
  1. Home
  2. AgentSDE
  3. agent-core
  4. gh-495
  5. plan
  6. plan.md
plan.md(2.1 KB)· Apr 14, 2026· 2 min read
  • Summary
  • Files
  • Steps
  • Verification
  • Risks

Plan: Fix Token Usage Recording in InvocationResultListener#

Summary#

Token usage recording was lost during the fire-and-forget refactor (e392b51). InvocationResultListener processes phase results but never calls TokenUsageService.record(), leaving all token usage metrics endpoints dark. Fix by injecting TokenUsageService into the listener and calling record() on each phase result with a non-null usage field.

Files#

FileActionDescription
src/invoke/invoke.module.tsmodifyImport TokenUsageModule so TokenUsageService is available for injection
src/invoke/invocation-result.listener.tsmodifyInject TokenUsageService via ModuleRef, call record(taskId, phase, usage) after signal processing
src/invoke/invocation-result.listener.spec.tscreateUnit test asserting record() is called with correct args when usage is present, and skipped when null

Steps#

  1. S1 — Wire TokenUsageModule into InvokeModule: Add TokenUsageModule to the imports array in src/invoke/invoke.module.ts.
  2. S2 — Inject and call TokenUsageService in InvocationResultListener: Add TokenUsageService to the lazy-loaded dependencies in onModuleInit(). In the process() method, after signal parsing completes, call this.tokenUsage.record(taskId, phase, result.usage) guarded by if (result.usage).
  3. S3 — Add unit test for token usage recording: Create invocation-result.listener.spec.ts covering: (a) record() called with correct taskId, phase, and usage when usage is present, (b) record() not called when usage is null/missing.

Verification#

  • npm run test passes with new and existing tests
  • npm run lint passes with zero warnings
  • After deployment, /api/cc/metrics/token-usage/* endpoints return non-zero values following a successful phase completion

Risks#

  • Circular dependency: InvocationResultListener already uses ModuleRef.get() with { strict: false } for lazy loading — adding TokenUsageService follows the same proven pattern, minimal risk.
ContextPrd