Refactor token usage record/metrics construction into shared token-persistence helpers#3566
Conversation
Claude Engine Smoke Test Results✅ GitHub API — 2 PR entries confirmed **Result: PASS
|
There was a problem hiding this comment.
Pull request overview
Centralizes token usage JSONL record construction and token metric increments into shared helpers within the API proxy, so both HTTP and WebSocket token trackers produce a consistent schema and reduce duplicated logic.
Changes:
- Added
buildTokenUsageRecord()andincrementTokenMetrics()helpers intoken-persistence.js. - Updated HTTP and WebSocket token trackers to delegate record/metrics construction to the shared helpers.
- Extended schema tests to assert helper output is schema-compatible and metrics helper is safe when no metrics sink is provided.
Show a summary per file
| File | Description |
|---|---|
| containers/api-proxy/token-persistence.js | Adds shared helpers for canonical token usage record construction and metric increments. |
| containers/api-proxy/token-tracker-http.js | Replaces inline record/metric logic with shared helper calls while preserving HTTP-specific enrichments. |
| containers/api-proxy/token-tracker-ws.js | Replaces inline record/metric logic with shared helper calls while preserving WS-specific values (101/streaming/byte accounting). |
| containers/api-proxy/token-tracker.schema.test.js | Adds helper-level assertions for schema compatibility and no-op behavior without metrics. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 0
🔬 Smoke Test Results
Overall: PARTIAL — MCP connectivity confirmed; pre-computed step outputs were not injected into the workflow prompt. PR author:
|
🔥 Smoke Test: Copilot BYOK (Offline) Mode
Running in BYOK offline mode ( Overall: PASS (core BYOK path functional) PR author:
|
This comment has been minimized.
This comment has been minimized.
Chroot Smoke Test Results
Overall: ❌ Not all tests passed — Python and Node.js versions differ between host and chroot.
|
🏗️ Build Test Suite Results
Overall: 8/8 ecosystems passed — ✅ PASS
|
Smoke Test Results — FAIL
Overall: FAIL
|
Gemini Engine Smoke Test Results
Overall Status: FAIL Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "localhost"See Network Configuration for more information.
|
Smoke Test: API Proxy OpenTelemetry Tracing
All 5 scenarios pass. OTEL tracing integration is working correctly.
|
|
Smoke Codex Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "registry.npmjs.org"See Network Configuration for more information.
|
HTTP and WebSocket token trackers were independently constructing the same token usage metrics + JSONL record shape, creating duplicate maintenance points for schema or field updates. This PR centralizes that logic so both paths use one implementation.
Shared token usage helpers
buildTokenUsageRecord(normalized, opts)incontainers/api-proxy/token-persistence.jsto produce the canonical token-usage record shape (_schema, timing, model/path/status, token fields,response_bytes).incrementTokenMetrics(metricsRef, provider, normalized)to centralizeinput_tokens_total/output_tokens_totalincrements.HTTP tracker now delegates shared logic
containers/api-proxy/token-tracker-http.jsnow calls:incrementTokenMetrics(...)instead of inline metric increments.buildTokenUsageRecord(...)instead of inline record assembly.x_initiator,billing) remain applied after shared record construction.WebSocket tracker now delegates shared logic
containers/api-proxy/token-tracker-ws.jsnow uses the same shared helpers for metrics and record construction, while preserving WS-specific values (status: 101,streaming: true,responseBytes: totalBytes - headerBytes).Schema-focused test coverage
containers/api-proxy/token-tracker.schema.test.jswith helper-level assertions: