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 (self): exclude timestamp-less Codex token_count snapshots from a bounded window

[._.]/ Adam Eivy708c39ab90b8
diff --git a/server/lib/providerTranscriptUsage.js b/server/lib/providerTranscriptUsage.js
index 42da770f90..b98a66ccad 100644
--- a/server/lib/providerTranscriptUsage.js
+++ b/server/lib/providerTranscriptUsage.js
@@ -321,11 +321,23 @@ export function parseCodexRollout(jsonlText, { from = null, to = null } = {}) {
if (!total || typeof total !== 'object') continue;
const ts = toEpoch(entry.timestamp);
- if (from != null && ts != null && ts < from) {
+ // A token_count line with no readable timestamp can't be placed relative
+ // to the window — exclude it whenever a bound is supplied, the same way
+ // `inWindow` excludes a timestamp-less message elsewhere in this file.
+ // Falling through to `latest = total` here (this snapshot's old bug) would
+ // fold a snapshot from outside the run's window into it regardless of
+ // `from`/`to`, inflating (or double-billing, on an overlapping run) that
+ // run's cumulative-usage delta.
+ if (ts == null) {
+ if (from != null || to != null) continue;
+ latest = total;
+ continue;
+ }
+ if (from != null && ts < from) {
baseline = total; // pre-window state — subtract it below
continue;
}
- if (to != null && ts != null && ts > to) continue;
+ if (to != null && ts > to) continue;
latest = total;
if (typeof entry.timestamp === 'string' && entry.timestamp) {
diff --git a/server/lib/providerTranscriptUsage.test.js b/server/lib/providerTranscriptUsage.test.js
index 4f00237926..93247b65b5 100644
--- a/server/lib/providerTranscriptUsage.test.js
+++ b/server/lib/providerTranscriptUsage.test.js
@@ -330,6 +330,25 @@ describe('windowing excludes un-placeable messages', () => {
expect(result.messages).toBe(1);
expect(result.tokensOut).toBe(150);
});
+
+ it('excludes a timestamp-less Codex token_count snapshot when a window is supplied', () => {
+ const text = [
+ codexMeta(),
+ // In-window snapshot this run should bill.
+ codexTokenCount({ timestamp: '2026-07-01T12:00:01.000Z', input: 1000, cached: 800, output: 100 }),
+ // A later snapshot with NO timestamp — outside this run's window in
+ // reality, but unplaceable. Falling through to `latest` (the bug) would
+ // fold its bigger cumulative total into this run instead of the run
+ // that actually produced it.
+ codexTokenCount({ timestamp: undefined, input: 9000, cached: 8000, output: 900 })
+ ].join('\n');
+
+ const result = parseCodexRollout(text, {
+ from: Date.parse('2026-07-01T11:00:00.000Z'),
+ to: Date.parse('2026-07-01T13:00:00.000Z')
+ });
+ expect(result.tokensOut).toBe(100);
+ });
});
describe('per-model token buckets', () => {