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#
| File | Action | Description |
|---|---|---|
src/invoke/invoke.module.ts | modify | Import TokenUsageModule so TokenUsageService is available for injection |
src/invoke/invocation-result.listener.ts | modify | Inject TokenUsageService via ModuleRef, call record(taskId, phase, usage) after signal processing |
src/invoke/invocation-result.listener.spec.ts | create | Unit test asserting record() is called with correct args when usage is present, and skipped when null |
Steps#
- S1 — Wire TokenUsageModule into InvokeModule: Add
TokenUsageModuleto theimportsarray insrc/invoke/invoke.module.ts. - S2 — Inject and call TokenUsageService in InvocationResultListener: Add
TokenUsageServiceto the lazy-loaded dependencies inonModuleInit(). In theprocess()method, after signal parsing completes, callthis.tokenUsage.record(taskId, phase, result.usage)guarded byif (result.usage). - S3 — Add unit test for token usage recording: Create
invocation-result.listener.spec.tscovering: (a)record()called with correcttaskId,phase, andusagewhen usage is present, (b)record()not called when usage is null/missing.
Verification#
npm run testpasses with new and existing testsnpm run lintpasses with zero warnings- After deployment,
/api/cc/metrics/token-usage/*endpoints return non-zero values following a successful phase completion
Risks#
- Circular dependency:
InvocationResultListeneralready usesModuleRef.get()with{ strict: false }for lazy loading — addingTokenUsageServicefollows the same proven pattern, minimal risk.