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): cap the Codex watermark read at the run's own window end

[._.]/ Adam Eivye7582e7b643b
diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.js
index 98a0434763..a648f8af98 100644
--- a/server/services/usageReconciler.js
+++ b/server/services/usageReconciler.js
@@ -310,17 +310,22 @@ 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.
- // 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);
+ // Read the rollout's cumulative position AS OF THIS RUN'S WINDOW END —
+ // absolute (no lower bound) so the watermark arithmetic below is a plain
+ // subtraction, but capped at `to` so a run only ever bills through its
+ // own end. Both halves are load-bearing:
+ // - Absolute (no `from`): a windowed parse already nets out the earlier
+ // snapshot as its baseline, so subtracting the watermark from it would
+ // double-subtract (measured: a later run billed 50, not 150). It also
+ // keeps the math immune to several snapshots sharing one epoch ms,
+ // which is why the timestamp boundary this replaced was unsound.
+ // - Capped at `to`: without it, an EARLY run reading the file after it
+ // grew would bill growth generated after its own window and advance
+ // the watermark past it, leaving the run that actually produced those
+ // tokens with nothing (measured: early run billed 250, late run 0).
+ const absolute = parseCodexRollout(text, { to });
if (!cwdMatches(absolute.cwd, workspacePath)) continue;
- // Still require the run's own window to overlap this rollout at all, so a
+ // 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;
diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.js
index 325187a3ac..f92a0c190b 100644
--- a/server/services/usageReconciler.test.js
+++ b/server/services/usageReconciler.test.js
@@ -761,3 +761,65 @@ describe('Codex watermark is in absolute rollout units', () => {
expect(await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:10:00.000Z')).toBeNull();
});
});
+
+describe('Codex attribution across every window ordering', () => {
+ // One rollout, two snapshots: cumulative out 100 at 10:05, 250 at 10:20.
+ const GROWN = [
+ ['2026-07-01T10:05:00.000Z', 1000, 0, 100],
+ ['2026-07-01T10:20:00.000Z', 3000, 0, 250]
+ ];
+ 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 write = (snaps) => writeCodexRollout(['2026', '07', '01'], 'rollout-1.jsonl', rollout(snaps));
+ const read = (startTime, endTime) => readMeasuredUsage({
+ workspacePath: WORKSPACE, startTime, endTime, family: 'codex', home
+ });
+ const EARLY = ['2026-07-01T10:00:00.000Z', '2026-07-01T10:10:00.000Z'];
+ const LATE = ['2026-07-01T10:15:00.000Z', '2026-07-01T10:25:00.000Z'];
+
+ // Regression: reading the file UNWINDOWED let an early run bill growth
+ // generated after its own window and advance the watermark past it, so the run
+ // that actually produced those tokens got nothing (measured: 250 / 0).
+ it('does not let an early run claim growth from after its window', async () => {
+ await write(GROWN); // already grown before the early run reads it
+ const early = await read(...EARLY);
+ const late = await read(...LATE);
+
+ expect(early?.tokensOut).toBe(100);
+ expect(late?.tokensOut).toBe(150);
+ expect((early?.tokensOut || 0) + (late?.tokensOut || 0)).toBe(250);
+ });
+
+ it('bills the whole rollout once when the later run reads first', async () => {
+ await write(GROWN);
+ const late = await read(...LATE);
+ const early = await read(...EARLY);
+
+ // The late run's window end covers both snapshots, so it takes all 250;
+ // the early run then finds nothing unbilled. Total is still exactly 250.
+ expect(late?.tokensOut).toBe(250);
+ expect(early).toBeNull();
+ });
+
+ it('never double-bills two overlapping windows', async () => {
+ await write(GROWN);
+ const a = await read('2026-07-01T10:00:00.000Z', '2026-07-01T10:22:00.000Z');
+ const b = await read('2026-07-01T10:10:00.000Z', '2026-07-01T10:25:00.000Z');
+ expect((a?.tokensOut || 0) + (b?.tokensOut || 0)).toBe(250);
+ });
+
+ it('splits correctly when the rollout grows between the two reads', async () => {
+ await write([GROWN[0]]);
+ const early = await read(...EARLY);
+ await write(GROWN);
+ const late = await read(...LATE);
+
+ expect(early?.tokensOut).toBe(100);
+ expect(late?.tokensOut).toBe(150);
+ });
+});