Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): store the Codex watermark in absolute rollout units

[._.]/ Adam Eivy68fa36800a9e
diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.js
index 4d540a6c90..98a0434763 100644
--- a/server/services/usageReconciler.js
+++ b/server/services/usageReconciler.js
@@ -61,11 +61,13 @@ const WINDOW_SLACK_MS = 60_000;
// but a run that already completed is never reconciled again.
const claimedMessages = new Set();
-// Cumulative token totals already charged per Codex rollout path. Codex bills as
-// a cumulative delta, so a per-snapshot claim can't stop a GROWN rollout from
-// re-including an earlier run's tokens. Keyed on TOTALS rather than a timestamp
-// because several `token_count` snapshots can share one epoch millisecond — a
-// timestamp boundary would either re-bill them or drop a later one entirely.
+// The ABSOLUTE cumulative position of each Codex rollout that has already been
+// billed, keyed by file path. Codex reports cumulative totals, so a rollout that
+// grows between two runs would otherwise re-include the earlier run's tokens.
+// Stored in absolute rollout units (not "tokens we billed") so the delta is a
+// plain subtraction: a windowed parse already nets out the earlier snapshot, so
+// mixing the two would double-subtract. Absolute totals are also immune to
+// several snapshots sharing one epoch millisecond.
const codexHighWater = new Map();
/** Test-only: forget every claim so suites start from a clean ledger. */
@@ -308,44 +310,51 @@ export async function readMeasuredUsage({ workspacePath, startTime, endTime, fam
// baseline before both runs) re-includes what the first run already
// billed. Track the highest cumulative boundary billed per file and
// re-parse from there, so each run charges only the genuinely new part.
- // Rollouts are filed by date, not cwd — confirm the cwd before folding.
- const parsed = parseCodexRollout(text, { from, to });
- if (!cwdMatches(parsed.cwd, workspacePath)) continue;
+ // Read the rollout's ABSOLUTE cumulative position (no window), then bill
+ // the difference from what was already charged. The watermark must be in
+ // absolute rollout units, not "tokens billed": a windowed parse already
+ // excludes the earlier snapshot as its baseline, so subtracting the
+ // billed amount from an already-delta'd figure double-subtracts (measured:
+ // a later run billed 50 instead of 150). Absolute totals also make the
+ // arithmetic immune to several snapshots sharing one epoch millisecond,
+ // which is why the timestamp boundary this replaced was unsound.
+ const absolute = parseCodexRollout(text);
+ if (!cwdMatches(absolute.cwd, workspacePath)) continue;
+ // Still require the run's own window to overlap this rollout at all, so a
+ // rollout from an unrelated period isn't attributed to this run.
+ if (totalTranscriptTokens(parseCodexRollout(text, { from, to })) === 0) continue;
- // Subtract whatever was already billed for this rollout. The high-water
- // mark is the CUMULATIVE TOTAL charged so far, not a timestamp: several
- // `token_count` snapshots can share one epoch millisecond, so a
- // timestamp boundary would either re-bill them or (excluding the whole
- // millisecond) silently drop a later snapshot's tokens. Subtracting
- // totals is exact regardless of how the snapshots are stamped.
const billed = codexHighWater.get(path);
- const net = billed
- ? {
- ...parsed,
- messages: Math.max(0, (parsed.messages || 0) - billed.messages),
- tokensIn: Math.max(0, parsed.tokensIn - billed.tokensIn),
- tokensOut: Math.max(0, parsed.tokensOut - billed.tokensOut),
- cacheReadTokens: Math.max(0, parsed.cacheReadTokens - billed.cacheReadTokens),
- cacheWriteTokens: Math.max(0, parsed.cacheWriteTokens - billed.cacheWriteTokens)
- }
- : parsed;
+ const delta = (field) => Math.max(0, (absolute[field] || 0) - (billed?.[field] || 0));
+ const net = {
+ ...absolute,
+ messages: delta('messages'),
+ tokensIn: delta('tokensIn'),
+ tokensOut: delta('tokensOut'),
+ cacheReadTokens: delta('cacheReadTokens'),
+ cacheWriteTokens: delta('cacheWriteTokens')
+ };
if (totalTranscriptTokens(net) === 0) continue;
- // Advance the mark before the next `await`, for the same reason the
- // Claude claim reserves per file: two overlapping runs must not both
- // read the pre-update value.
+ // Advance the mark to the absolute position just read, before the next
+ // `await` — for the same reason the Claude claim reserves per file: two
+ // overlapping runs must not both act on the pre-update value. `Math.max`
+ // guards a rollout that was truncated/rewritten smaller, so the mark
+ // never moves backwards and re-bills what it already charged.
codexHighWater.set(path, {
- messages: (billed?.messages || 0) + (net.messages || 0),
- tokensIn: (billed?.tokensIn || 0) + net.tokensIn,
- tokensOut: (billed?.tokensOut || 0) + net.tokensOut,
- cacheReadTokens: (billed?.cacheReadTokens || 0) + net.cacheReadTokens,
- cacheWriteTokens: (billed?.cacheWriteTokens || 0) + net.cacheWriteTokens
+ messages: Math.max(billed?.messages || 0, absolute.messages || 0),
+ tokensIn: Math.max(billed?.tokensIn || 0, absolute.tokensIn || 0),
+ tokensOut: Math.max(billed?.tokensOut || 0, absolute.tokensOut || 0),
+ cacheReadTokens: Math.max(billed?.cacheReadTokens || 0, absolute.cacheReadTokens || 0),
+ cacheWriteTokens: Math.max(billed?.cacheWriteTokens || 0, absolute.cacheWriteTokens || 0)
});
codexReserved.push([path, billed]);
- // `byModel` must carry the NET tokens too, or the per-model records the
- // caller bills from would re-charge the already-billed portion.
- fold(net.byModel && Object.keys(net.byModel).length === 1
- ? { ...net, byModel: { [Object.keys(net.byModel)[0]]: {
+ // Mirror the net into `byModel` — callers bill from the per-model records,
+ // so leaving them at absolute totals would re-charge the billed portion.
+ // Codex reports one model per rollout, so the whole net is that model's.
+ const modelKey = Object.keys(absolute.byModel || {})[0];
+ fold(modelKey
+ ? { ...net, byModel: { [modelKey]: {
messages: net.messages,
tokensIn: net.tokensIn,
tokensOut: net.tokensOut,
diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.js
index 807a526a0f..325187a3ac 100644
--- a/server/services/usageReconciler.test.js
+++ b/server/services/usageReconciler.test.js
@@ -703,3 +703,61 @@ describe('model-less bucket attribution (deliberate single-bucket choice)', () =
expect(records[0].tokensOut).toBe(500);
});
});
+
+describe('Codex watermark is in absolute rollout units', () => {
+ const rollout = (snaps) => [
+ JSON.stringify({ timestamp: '2026-07-01T10:00:00.000Z', type: 'session_meta', payload: { id: 'rollout-1', cwd: WORKSPACE, model: 'gpt-5.3-codex' } }),
+ ...snaps.map(([timestamp, input, cached, output]) => JSON.stringify({
+ timestamp, type: 'event_msg',
+ payload: { type: 'token_count', info: { total_token_usage: { input_tokens: input, cached_input_tokens: cached, output_tokens: output, total_tokens: input + output } } }
+ }))
+ ].join('\n');
+ const read = (startTime, endTime) => readMeasuredUsage({
+ workspacePath: WORKSPACE, startTime, endTime, family: 'codex', home
+ });
+
+ // Regression: storing "tokens we billed" and subtracting it from a WINDOWED
+ // parse double-subtracts, because the window already excludes the earlier
+ // snapshot as its baseline. Measured: the later run billed 50, not 150.
+ it('bills the full new delta when the later run starts after the billed snapshot', async () => {
+ await writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout([
+ ['2026-07-01T10:05:00.000Z', 1000, 0, 100]
+ ]));
+ const first = await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:06:00.000Z');
+ expect(first.tokensOut).toBe(100);
+
+ // Grows to cumulative 250; the second run's window starts AFTER 10:05, so a
+ // windowed parse alone would already report 150.
+ await writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout([
+ ['2026-07-01T10:05:00.000Z', 1000, 0, 100],
+ ['2026-07-01T10:20:00.000Z', 3000, 0, 250]
+ ]));
+ const second = await read('2026-07-01T10:15:00.000Z', '2026-07-01T10:25:00.000Z');
+
+ expect(second?.tokensOut).toBe(150);
+ expect(first.tokensOut + second.tokensOut).toBe(250);
+ // The per-model bucket must equal the netted aggregate, or records re-charge.
+ expect(second.byModel['gpt-5.3-codex'].tokensOut).toBe(second.tokensOut);
+ });
+
+ it('does not re-bill a rollout that was truncated or rewritten smaller', async () => {
+ await writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout([
+ ['2026-07-01T10:05:00.000Z', 1000, 0, 100]
+ ]));
+ expect((await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:10:00.000Z')).tokensOut).toBe(100);
+
+ // Rewritten with LOWER cumulative totals — the mark must not move backwards.
+ await writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout([
+ ['2026-07-01T10:05:00.000Z', 500, 0, 40]
+ ]));
+ expect(await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:30:00.000Z')).toBeNull();
+ });
+
+ it('ignores a rollout whose activity falls entirely outside the run window', async () => {
+ await writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout([
+ ['2026-07-01T02:00:00.000Z', 1000, 0, 100]
+ ]));
+ // The absolute read sees tokens, but none of them are in this run's window.
+ expect(await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:10:00.000Z')).toBeNull();
+ });
+});