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): give every counted transcript line a claim key

[._.]/ Adam Eivy3923b971ceec
diff --git a/server/lib/providerTranscriptUsage.js b/server/lib/providerTranscriptUsage.js
index d9ffea1b6c..c240c378a2 100644
--- a/server/lib/providerTranscriptUsage.js
+++ b/server/lib/providerTranscriptUsage.js
@@ -142,7 +142,12 @@ export function parseClaudeTranscript(jsonlText, { from = null, to = null, exclu
let firstTs = null;
let lastTs = null;
+ // Position of the current line within the file, so a line carrying no
+ // identifier at all still gets a stable key (see `dedupeKey` below).
+ let lineIndex = -1;
+
for (const entry of parseJsonLines(jsonlText)) {
+ lineIndex += 1;
if (!sessionId && typeof entry.sessionId === 'string') sessionId = entry.sessionId;
if (!cwd && typeof entry.cwd === 'string') cwd = entry.cwd;
@@ -153,14 +158,18 @@ export function parseClaudeTranscript(jsonlText, { from = null, to = null, exclu
if (!inWindow(ts, from, to)) continue;
// One response spans multiple lines with identical usage — count it once.
- const dedupeKey = entry.message?.id || entry.uuid;
- if (dedupeKey) {
- if (seen.has(dedupeKey)) continue;
- // Already billed to another run whose window also covers this message —
- // skip it so overlapping runs can't each claim the same tokens.
- if (exclude?.has(dedupeKey)) continue;
- seen.add(dedupeKey);
- }
+ //
+ // EVERY counted line needs a key, including one carrying neither
+ // `message.id` nor `uuid`: a keyless line is invisible to the cross-run
+ // claim ledger, so two overlapping runs each bill it (measured: 100 billed
+ // for 50 reported). Fall back to the line's position, which is stable for an
+ // append-only transcript — the `@` prefix can't collide with a real id.
+ const dedupeKey = entry.message?.id || entry.uuid || `@line-${lineIndex}`;
+ if (seen.has(dedupeKey)) continue;
+ // Already billed to another run whose window also covers this message —
+ // skip it so overlapping runs can't each claim the same tokens.
+ if (exclude?.has(dedupeKey)) continue;
+ seen.add(dedupeKey);
totals.messages += 1;
totals.tokensIn += num(usage.input_tokens);
diff --git a/server/services/usageReconciler.property.test.js b/server/services/usageReconciler.property.test.js
index 9ea27b49b0..c0e7ae700a 100644
--- a/server/services/usageReconciler.property.test.js
+++ b/server/services/usageReconciler.property.test.js
@@ -117,17 +117,23 @@ describe('property: Claude claim ledger', () => {
for (let i = 0, n = 1 + pick(4); i < n; i++) {
const output = 10 + pick(100);
reportedOut += output;
- lines.push(JSON.stringify({
+ // Vary WHICH identifiers the line carries. A line with neither
+ // `message.id` nor `uuid` was invisible to the claim ledger and got
+ // double-billed — a shape the first version of this generator could not
+ // produce, so it is generated explicitly now.
+ const identifiers = pick(4);
+ const record = {
type: 'assistant',
- uuid: `u${file}-${i}`,
cwd: WORKSPACE,
timestamp: iso(pick(40)),
message: {
- id: `m${file}-${i}`,
model: 'claude-opus-5',
usage: { input_tokens: 1, output_tokens: output, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }
}
- }));
+ };
+ if (identifiers !== 1 && identifiers !== 3) record.message.id = `m${file}-${i}`;
+ if (identifiers !== 2 && identifiers !== 3) record.uuid = `u${file}-${i}`;
+ lines.push(JSON.stringify(record));
}
await writeFile(join(dir, `session-${file}.jsonl`), lines.join('\n'));
}
diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.js
index f92a0c190b..07d37fdeec 100644
--- a/server/services/usageReconciler.test.js
+++ b/server/services/usageReconciler.test.js
@@ -823,3 +823,47 @@ describe('Codex attribution across every window ordering', () => {
expect(late?.tokensOut).toBe(150);
});
});
+
+describe('assistant lines carrying no identifier', () => {
+ // A line with usage but neither `message.id` nor `uuid` had no key, so it was
+ // invisible to the cross-run claim ledger and two overlapping runs each billed
+ // it (measured: 100 billed for 50 reported).
+ const keyless = (timestamp, output) => JSON.stringify({
+ type: 'assistant',
+ cwd: WORKSPACE,
+ timestamp,
+ message: {
+ model: 'claude-opus-5',
+ usage: { input_tokens: 1, output_tokens: output, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }
+ }
+ });
+
+ it('bills an unidentified line exactly once across two overlapping runs', async () => {
+ await writeClaudeSession('a.jsonl', [keyless('2026-07-01T10:05:00.000Z', 50)]);
+
+ const first = await readMeasuredUsage({
+ workspacePath: WORKSPACE, startTime: '2026-07-01T10:00:00.000Z', endTime: '2026-07-01T10:10:00.000Z', family: 'claude', home
+ });
+ const second = await readMeasuredUsage({
+ workspacePath: WORKSPACE, startTime: '2026-07-01T10:02:00.000Z', endTime: '2026-07-01T10:12:00.000Z', family: 'claude', home
+ });
+
+ expect(first?.tokensOut).toBe(50);
+ expect(second).toBeNull();
+ expect((first?.tokensOut || 0) + (second?.tokensOut || 0)).toBe(50);
+ });
+
+ it('still counts several distinct unidentified lines separately', async () => {
+ // Positional keys must not collapse two different keyless lines into one.
+ await writeClaudeSession('a.jsonl', [
+ keyless('2026-07-01T10:05:00.000Z', 50),
+ keyless('2026-07-01T10:06:00.000Z', 25)
+ ]);
+
+ const result = await readMeasuredUsage({
+ workspacePath: WORKSPACE, startTime: '2026-07-01T10:00:00.000Z', endTime: '2026-07-01T10:10:00.000Z', family: 'claude', home
+ });
+ expect(result.tokensOut).toBe(75);
+ expect(result.messages).toBe(2);
+ });
+});