Skip to content

refactor: share the codex stream's line, usage and tool call helpers - #61

Merged
bketelsen merged 2 commits into
mainfrom
groom/inference-stream
Nov 11, 2026
Merged

bketelsen merged 2 commits into
mainfrom
groom/inference-stream

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The codex provider's stream translation (internal/inference/responses.go) and the usage tap (internal/inference/usage.go) repeat small pieces of logic. This PR moves each into 1 helper:

  • sseJSON: the parse of an SSE data: line that holds a JSON object, which events and usageTap.sseLine each did on their own.
  • chatUsage: the chat completion usage object (prompt_tokens, completion_tokens, total_tokens), built in chatCompletion and in the chunker's last usage chunk.
  • chatToolCall: the chat completion tool call, built in chatCompletion and in the chunker's response.output_item.done path (the chunker adds its index).
  • answerParts: a finished response's text and function calls, with the fallback to response.output. chatCompletion and the chunker's "no deltas came" path both use it, so that path no longer builds a whole chat completion and digs the message out of it with type assertions.

No change in behavior: the same chat completion, chunks, [DONE] line, error chunk and recorded usage for the same provider stream. Every function name, signature and JSON field that other code uses stays the same.

Left out on purpose: internal/inference/{config,server,codex,copilot}.go (risk paths) and cmpString, which codex.go uses.

Risk tier

Risk tier: 1 — a refactor with no change in behavior. internal/inference/responses.go and internal/inference/usage.go are not in .github/risk-paths.json; no listed path is touched.

Verification

  • Commit 1 (7d33cca) adds internal/inference/stream_test.go: 22 characterization tests of events, the usage tap, chatCompletion and the chunker (text deltas, function calls, an answer with no deltas from the items or from response.output, the usage chunk, a completed event with no response, failures, [DONE]). Chunks are compared by value, because encoding/json/v2 does not sort map keys. On the base code: go test -count=1 ./internal/inference/ → ok (0.552s).
  • Commit 2 (1a936fd) is the refactor; it does not change the new test file or any other test.
  • make check green (host): on the base 74a31e4, exit 0; on the head 1a936fd, exit 0.

Cross-review

  • Reviewer openai/gpt-6.1-sol; primary coder deepseek/deepseek-flash, with a small edit by anthropic/claude-opus-5-5.
  • Base 74a31e4856b5e44da4567c53307cb83cf1354bf1, head 1a936fd75c2dae165f88d598306f87f86d07cf69; 1 pass, all 3 changed files, with codex.go read for context.
  • Question: "Does any line of this diff change observable behavior?" Answer: no; provider requests, chunks, chat completions, error chunks, [DONE] and recorded tokens stay the same.
  • Result: Ready, Blocking: none, 0 findings.
  • Revert test: exempt (a pure refactor; the proof is the same tests green on the base and the head).

@github-actions github-actions Bot added the level:L3 Skim label Nov 11, 2026
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 11, 2026
@bketelsen
bketelsen merged commit 7fa7082 into main Nov 11, 2026
11 checks passed
@bketelsen
bketelsen deleted the groom/inference-stream branch November 11, 2026 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants