Skip to content

refactor: split token-tracker.js into four focused modules#3343

Merged
lpcox merged 3 commits into
mainfrom
copilot/refactor-token-tracker-module
May 18, 2026
Merged

refactor: split token-tracker.js into four focused modules#3343
lpcox merged 3 commits into
mainfrom
copilot/refactor-token-tracker-module

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 18, 2026

containers/api-proxy/token-tracker.js had grown to 916 lines with four distinct concerns mixed together, making the two ~210-line tracker functions (trackTokenUsage, trackWebSocketTokenUsage) hard to review and modify in isolation.

Split

New file Contents
token-persistence.js Log stream lifecycle, validateTokenUsageRecord, writeTokenUsage, closeLogStream, diag
token-parsers.js Pure functions: SSE/JSON parsing, decompression helpers, normalizeUsage — no I/O
token-tracker-http.js trackTokenUsage (HTTP response stream)
token-tracker-ws.js parseWebSocketFrames + trackWebSocketTokenUsage (WebSocket path)
token-tracker.js Re-export facade — identical module.exports as before

Callsites unchanged

server.js, proxy-request.js, and websocket-proxy.js all continue to require('./token-tracker') with no modifications needed.

Dockerfile

Added the four new files to the COPY list so they are included in the container image.

Copilot AI changed the title [WIP] Refactor token-tracker into focused modules refactor: split token-tracker.js into four focused modules May 18, 2026
Copilot finished work on behalf of lpcox May 18, 2026 17:49
Copilot AI requested a review from lpcox May 18, 2026 17:49
@lpcox lpcox marked this pull request as ready for review May 18, 2026 19:32
Copilot AI review requested due to automatic review settings May 18, 2026 19:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the 916-line containers/api-proxy/token-tracker.js into four focused modules (persistence, pure parsers, HTTP tracker, WebSocket tracker), keeping token-tracker.js as a re-export facade so all existing callers and tests continue to work unchanged. The Dockerfile COPY list is updated to include the new files.

Changes:

  • Extracted log-stream lifecycle, record validation, schema constant, and diag into token-persistence.js.
  • Extracted pure SSE/JSON parsing, decompression helpers, and normalizeUsage into token-parsers.js; split tracker bodies into token-tracker-http.js and token-tracker-ws.js.
  • Reduced token-tracker.js to a facade with identical module.exports, and updated the api-proxy Dockerfile to copy the new files.
Show a summary per file
File Description
containers/api-proxy/token-tracker.js Converted to a re-export facade preserving the original public API.
containers/api-proxy/token-persistence.js New module: log stream, schema constant, validateTokenUsageRecord, writeTokenUsage, closeLogStream, diag.
containers/api-proxy/token-parsers.js New module: pure SSE/JSON parsing, decompression helpers, normalizeUsage.
containers/api-proxy/token-tracker-http.js New module: trackTokenUsage for HTTP responses.
containers/api-proxy/token-tracker-ws.js New module: parseWebSocketFrames and trackWebSocketTokenUsage.
containers/api-proxy/Dockerfile Adds the four new JS files to the COPY list.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 6/6 changed files
  • Comments generated: 0

Comment thread containers/api-proxy/token-persistence.js Fixed
Comment thread containers/api-proxy/token-tracker-http.js Fixed
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions github-actions Bot mentioned this pull request May 18, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 18, 2026

@copilot address review feedback

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 18, 2026

@copilot address review feedback

Addressed in a90b00d.

  • Removed diagnostic payload persistence in token-persistence.js so debug logs only write sanitized event names.
  • Implemented non-streaming buffer cap enforcement in token-tracker-http.js, which also resolves the unused MAX_BUFFER_SIZE finding.
  • Re-ran token-tracker tests, lint/build, secret scan, and CodeQL (0 alerts).

Copilot finished work on behalf of lpcox May 18, 2026 19:44
@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results

✅ GitHub API test (recent-prs.json)
✅ Playwright test (github.com title)
✅ File verify test (smoke-test artifact)

PASS — Claude engine validation complete

💥 [THE END] — Illustrated by Smoke Claude

@github-actions
Copy link
Copy Markdown
Contributor

🔬 Smoke Test Results

Test Status
GitHub MCP connectivity
GitHub.com HTTP ⚠️ template vars unexpanded
File write/read ⚠️ template vars unexpanded

Overall: PARTIAL — MCP connectivity confirmed. Pre-step template variables (${{ steps.smoke-data.outputs.* }}) were not expanded at runtime, so HTTP and file tests could not be verified.

PR: refactor: split token-tracker.js into four focused modules — author @Copilot, assignees @lpcox @Copilot

📰 BREAKING: Report filed by Smoke Copilot

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test: FAIL
PRs: chore: recompile all workflow lock files; Refactor COPILOT_MODEL resolution to remove duplicated env precedence logic
✅ GitHub PR review
❌ SafeInputs GH CLI missing
✅ Playwright title check
❌ Tavily returned no available search tool
✅ File write/read and build
✅ Discussion number valid/comment attempted
Overall status: FAIL

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • registry.npmjs.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "registry.npmjs.org"

See Network Configuration for more information.

🔮 The oracle has spoken through Smoke Codex

@github-actions
Copy link
Copy Markdown
Contributor

🏗️ Build Test Suite Results

Ecosystem Project Build/Install Tests Status
Bun elysia 1/1 passed ✅ PASS
Bun hono 1/1 passed ✅ PASS
C++ fmt N/A ✅ PASS
C++ json N/A ✅ PASS
Deno oak N/A 1/1 passed ✅ PASS
Deno std N/A 1/1 passed ✅ PASS
.NET hello-world N/A ✅ PASS
.NET json-parse N/A ✅ PASS
Go color 1/1 passed ✅ PASS
Go env 1/1 passed ✅ PASS
Go uuid 1/1 passed ✅ PASS
Java gson 1/1 passed ✅ PASS
Java caffeine 1/1 passed ✅ PASS
Node.js clsx All passed ✅ PASS
Node.js execa All passed ✅ PASS
Node.js p-limit All passed ✅ PASS
Rust fd 1/1 passed ✅ PASS
Rust zoxide 1/1 passed ✅ PASS

Overall: 8/8 ecosystems passed — ✅ PASS

Generated by Build Test Suite for issue #3343 · ● 3.5M ·

@github-actions
Copy link
Copy Markdown
Contributor

Chroot Version Comparison Results

Runtime Host Version Chroot Version Match?
Python Python 3.12.13 Python 3.12.3 ❌ NO
Node.js v24.15.0 v20.20.2 ❌ NO
Go go1.22.12 go1.22.12 ✅ YES

Overall: ❌ Not all tests passed — Python and Node.js versions differ between host and chroot environments.

Tested by Smoke Chroot

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test: GitHub Actions Services Connectivity

Check Result
Redis PING (host.docker.internal:6379) ❌ No response (timeout)
PostgreSQL pg_isready (host.docker.internal:5432) ❌ No response
PostgreSQL SELECT 1 ❌ No response

Overall: FAIL

host.docker.internal resolves to 172.17.0.1 but no service is reachable on ports 6379 or 5432. Service containers may not be running or are not accessible from this environment.

🔌 Service connectivity validated by Smoke Services

@github-actions
Copy link
Copy Markdown
Contributor

Smoke test results: 1. MCP: FAIL, 2. Connectivity: FAIL, 3. File: PASS, 4. Bash: PASS. Overall: FAIL

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • localhost

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "localhost"

See Network Configuration for more information.

💎 Faceted by Smoke Gemini

@github-actions
Copy link
Copy Markdown
Contributor

🔬 Smoke Test: Copilot BYOK (Offline) Mode

Test Result
GitHub MCP connectivity ✅ PR listed successfully
GitHub.com connectivity ⚠️ Pre-step data not expanded (template vars unresolved)
File write/read ⚠️ Pre-step data not expanded (template vars unresolved)
BYOK inference (agent → api-proxy → api.githubcopilot.com) ✅ Responding in BYOK offline mode

Running in BYOK offline mode (COPILOT_OFFLINE=true) via api-proxy → api.githubcopilot.com.

PR: refactor: split token-tracker.js into four focused modules — author @Copilot, assignees @lpcox @Copilot.

Overall: PARTIAL (BYOK inference ✅, pre-step outputs not injected)

🔑 BYOK report filed by Smoke Copilot BYOK

@lpcox lpcox merged commit b17432f into main May 18, 2026
64 of 68 checks passed
@lpcox lpcox deleted the copilot/refactor-token-tracker-module branch May 18, 2026 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactoring] Split containers/api-proxy/token-tracker.js into focused modules

4 participants