GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): don't bill a replayed Codex first request twice by fixfon · Pull Request #22392 · pingdotgg/t3code

fix(server): don't bill a replayed Codex first request twice #22392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
maria-rcks merged 6 commits into pingdotgg:main from fixfon:fix/22376-codex-counter-reset
Oct 30, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(server): don't bill a replayed Codex first request twice #22392

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
190760e
fix(usage): ignore a replayed Codex first request instead of billing …
fixfon Oct 21, 2026
5366382
fix(usage): a Codex context marker that zeroes the counters forgets t…
fixfon Oct 21, 2026
ce0ea7d
fix(usage): forget the Codex sequence start whenever the cumulative b…
fixfon Oct 21, 2026
6c6a74a
Merge remote-tracking branch 'origin/main' into fix/22376-codex-count…
fixfon Oct 22, 2026
4cc0bab
Merge remote-tracking branch 'github/main' into takeover-22392
maria-rcks Oct 26, 2026
d3117a3
fix(server): distinguish Codex replays from equal counter resets
maria-rcks Oct 26, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

129 changes: 129 additions & 0 deletions 129 apps/server/src/provider/Drivers/codexUsage.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "@effect/vitest";
import { totalTokens } from "@t3tools/provider-core/server/usage";

import { codexUsageFormat, initialCodexScanState, parseCodexLine } from "./codexUsage.ts";

Expand Down Expand Up @@ -373,6 +374,106 @@ describe("parseCodexLine", () => {
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
});

it("ignores a replay of the current sequence's first request without rewinding the baseline", () => {
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const request = counters(100, 20);
const processed = [
counted(request, request),
counted(request, counters(200, 40)),
counted(request, request),
counted(request, counters(300, 60)),
].map((line) => {
const record = parseCodexLine(line, state);
return record === null ? 0 : totalTokens(record.totals);
});

expect({
processed,
skipped: codexUsageFormat.readMalformedRecords?.(state),
}).toEqual({ processed: [120, 120, 0, 120], skipped: 0 });
});

it("bills a genuine restart with the same counters at a new timestamp", () => {
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const request = counters(100, 20);
const lines = [
counted(request, request),
counted(request, counters(200, 40)),
counted(request, request).replace("05:17:49.919Z", "05:18:49.919Z"),
counted(counters(30, 5), counters(130, 25)),
];
const processed = lines.map((line) => {
const record = parseCodexLine(line, state);
return record === null ? 0 : totalTokens(record.totals);
});
expect({ processed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({
processed: [120, 120, 120, 35],
skipped: 0,
});
});

it.each([
{
reset: true,
markerTotal: counters(0, 0),
nextTotal: counters(130, 25),
processed: [120, 120, 0, 120, 35],
},
{
reset: false,
markerTotal: counters(200, 40),
nextTotal: counters(230, 45),
processed: [120, 120, 0, 0, 35],
},
])(
"bills a request equal to the old sequence start only after a context marker reset the counters (reset: $reset)",
({ markerTotal, nextTotal, processed }) => {
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const request = counters(100, 20);
const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, markerTotal, {
model_context_window: 475000,
});
const billed = [
counted(request, request),
counted(request, counters(200, 40)),
marker,
counted(request, request),
counted(counters(30, 5), nextTotal),
].map((line) => {
const record = parseCodexLine(line, state);
return record === null ? 0 : totalTokens(record.totals);
});

expect({
processed: billed,
skipped: codexUsageFormat.readMalformedRecords?.(state),
}).toEqual({ processed, skipped: 0 });
},
);

it("forgets the old sequence start when a malformed pair zeroes the cumulative baseline", () => {
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const request = counters(100, 20);
const processed = [
counted(request, request),
counted(counters(30, 5), counters(0, 0)),
counted(request, request),
counted(counters(30, 5), counters(130, 25)),
].map((line) => {
const record = parseCodexLine(line, state);
return record === null ? 0 : totalTokens(record.totals);
});

expect({
processed,
skipped: codexUsageFormat.readMalformedRecords?.(state),
}).toEqual({ processed: [120, 0, 120, 35], skipped: 1 });
});

it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => {
const meta = (id: string, model_provider?: string) =>
JSON.stringify({ type: "session_meta", payload: { id, model_provider } });
Expand Down Expand Up @@ -535,5 +636,33 @@ describe("parseCodexLine", () => {
);
expect(record).not.toBeNull();
});

it("forgets the old sequence start when a copied zero baseline rebases the counters", () => {
const state = initialCodexScanState();
const forkInstant = "2026-08-01T05:00:00.000Z";
const request = counters(100, 20);
const billed = (line: string) => {
const record = parseCodexLine(line, state);
return record === null ? 0 : totalTokens(record.totals);
};
parseCodexLine(stamped("2026-08-01T04:59:00.000Z", turnContext), state);
const first = billed(stamped("2026-08-01T04:59:01.000Z", counted(request, request)));
parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
const copied = billed(
stamped(
"2026-08-01T05:00:00.001Z",
counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0)),
),
);
const afterBurst = billed(stamped("2026-08-01T05:00:06.000Z", counted(request, request)));
const next = billed(
stamped("2026-08-01T05:00:06.100Z", counted(counters(30, 5), counters(130, 25))),
);

expect({
processed: [first, copied, afterBurst, next],
skipped: codexUsageFormat.readMalformedRecords?.(state),
}).toEqual({ processed: [120, 0, 120, 35], skipped: 0 });
});
});
});
19 changes: 17 additions & 2 deletions 19 apps/server/src/provider/Drivers/codexUsage.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ function readCodexTokenUsage(value: unknown): CodexTokenUsage | null {
return isCodexTokenUsage(usage) ? usage : null;
}

// A zeroed baseline rebases the counters, so the sequence start measured against it is forgotten.
function rebaseCodexUsage(state: CodexScanState, cumulative: CodexTokenUsage) {
state.lastCumulativeUsage = cumulative;
if (cumulative.input_tokens + cumulative.output_tokens === 0) delete state.sequenceStartSignature;
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

function advanceCodexUsage(previous: CodexTokenUsage | null, last: CodexTokenUsage) {
return readCodexTokenUsage(
Object.fromEntries(CODEX_USAGE_FIELDS.map((key) => [key, (previous?.[key] ?? 0) + last[key]])),
Expand All @@ -71,6 +77,8 @@ export const CodexScanState = Schema.Struct({
sessionId: Schema.mutableKey(Schema.String),
lastUsageSignature: Schema.mutableKey(Schema.NullOr(Schema.String)),
lastCumulativeUsage: Schema.mutableKey(Schema.NullOr(CodexTokenUsage)),
// Optional so scan states cached before this field existed stay valid.
sequenceStartSignature: Schema.mutableKey(Schema.optionalKey(Schema.String)),
malformedRecords: Schema.mutableKey(TokenCount),
sawSessionMeta: Schema.mutableKey(Schema.Boolean),
modelProvider: Schema.mutableKey(Schema.String),
Expand Down Expand Up @@ -193,7 +201,7 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
state.forkCopyAnchorMs = timestampMs;
// Copied history is already billed in the parent, but establishes the child's baseline.
const signature = last === null ? null : JSON.stringify(last);
if (cumulative !== null) state.lastCumulativeUsage = cumulative;
if (cumulative !== null) rebaseCodexUsage(state, cumulative);
else if (!hasCumulative && last !== null && signature !== state.lastUsageSignature) {
state.lastCumulativeUsage = advanceCodexUsage(state.lastCumulativeUsage, last);
}
Expand All @@ -206,7 +214,7 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |

const previous = state.lastCumulativeUsage;
// A valid snapshot remains the next baseline even when its paired request is malformed.
if (cumulative !== null) state.lastCumulativeUsage = cumulative;
if (cumulative !== null) rebaseCodexUsage(state, cumulative);
if (last === null || (hasCumulative && cumulative === null)) {
state.malformedRecords += 1;
return null;
Expand Down Expand Up @@ -236,6 +244,12 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
if (cumulative !== null) {
// A session resumed in a new Codex process restarts its cumulative counters at this request.
const restarted = CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
const sequenceStart = JSON.stringify([timestampMs, last]);
// Only the same event is a replay; a later request can restart with identical counters.
if (restarted && sequenceStart === state.sequenceStartSignature) {
state.lastCumulativeUsage = previous;
return null;
}
// Forks and paginated continuations can inherit totals without copying a counter snapshot.
// Only bill the latest request, and require the unseen prefix to be valid usage too.
const reconciled =
Expand All @@ -248,6 +262,7 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
state.malformedRecords += 1;
return null;
}
if (restarted) state.sequenceStartSignature = sequenceStart;
} else {
if (signature === state.lastUsageSignature) return null;
// Advance expected totals for billed legacy requests so a cumulative re-emission is stale.
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.