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

Usage: count Claude Code and Codex use outside PortOS (#13016)

[._.]/bae2dbf3667f
diff --git a/server/index.js b/server/index.js
index 98042f5e45..b644c71009 100644
--- a/server/index.js
+++ b/server/index.js
@@ -605,7 +605,7 @@ if (!isSmokeBoot()) {
// token history current even when the usage page is never opened.
import('./services/claudeCodeTranscriptUsage.js').then(({ startTranscriptUsageRefresh }) => startTranscriptUsageRefresh())
.catch((err) => console.error(`❌ Claude Code transcript usage refresh could not start: ${err.message}`));
- // Same for interactive Grok / Antigravity sessions (local files, zero provider calls).
+ // Same for interactive Claude Code / Codex / Grok / Antigravity sessions (local files, zero provider calls).
import('./services/usageInteractiveScan.js').then(({ startInteractiveUsageRefresh }) => startInteractiveUsageRefresh())
.catch((err) => console.error(`❌ Interactive usage scan could not start: ${err.message}`));
}
diff --git a/server/services/peerUsage.js b/server/services/peerUsage.js
index 6f8c1a5745..166435c979 100644
--- a/server/services/peerUsage.js
+++ b/server/services/peerUsage.js
@@ -525,8 +525,11 @@ const addInto = (target, source, scale = 1) => {
* days, this machine's history after 400), so a month bucket's cost is spread
* evenly over that month's days that no longer have a day bucket. The month's
* total is exact; its shape within the month is not.
+ *
+ * Claude Code use outside PortOS also comes from the instance's transcript
+ * history (see `withTranscriptClaude`).
*/
-function instanceDailyCosts({ daily = {}, monthly = {} }, span, providers, labels) {
+function instanceDailyCosts({ daily = {}, monthly = {}, transcript = {} }, span, providers, labels) {
const missingDays = new Map();
for (const day of span) {
const month = day.slice(0, 7);
@@ -545,15 +548,33 @@ function instanceDailyCosts({ daily = {}, monthly = {} }, span, providers, label
monthShare.set(month, share);
}
return span.map((day) => {
- if (daily[day]) return costByGroup(buildUsageReport({ [day]: daily[day] }, { from: day, to: day, providers }), labels);
- return monthShare.get(day.slice(0, 7)) || {};
+ const costs = daily[day]
+ ? costByGroup(buildUsageReport({ [day]: daily[day] }, { from: day, to: day, providers }), labels)
+ : { ...(monthShare.get(day.slice(0, 7)) || {}) };
+ return withTranscriptClaude(costs, transcript?.[day] && summarizeTranscriptDays({ [day]: transcript[day] }).totals.estimatedCost, labels);
});
}
+/**
+ * The Claude Code CLI's own transcripts count every session on the machine —
+ * the terminal, the desktop app's Code tab, AND the runs PortOS spawned. The
+ * ledger holds PortOS runs plus the terminal sessions the interactive scan
+ * billed (usageInteractiveScan.js, which reaches back only ~35 days from its
+ * first run), so each is a partial view of the same spend: the day's Claude
+ * cost is the larger of the two, never their sum.
+ */
+function withTranscriptClaude(costs, transcriptCost, labels) {
+ if ((transcriptCost || 0) > (costs.claude || 0)) {
+ if (!labels.has('claude')) labels.set('claude', familyLabel('claude'));
+ costs.claude = transcriptCost;
+ }
+ return costs;
+}
+
function earliestDay(sources) {
let earliest = null;
- for (const { daily, monthly } of sources) {
- for (const day of Object.keys(daily || {})) if (!earliest || day < earliest) earliest = day;
+ for (const { daily, monthly, transcript } of sources) {
+ for (const day of [...Object.keys(daily || {}), ...Object.keys(transcript || {})]) if (!earliest || day < earliest) earliest = day;
for (const month of Object.keys(monthly || {})) if (!earliest || `${month}-01` < earliest) earliest = `${month}-01`;
}
return earliest;
@@ -571,11 +592,25 @@ let rollingMemo = null;
export async function getFleetRollingCost({ providers = [], range = '3m', now = new Date() } = {}) {
const { self, peers } = await entriesWithSelf();
const local = getUsage();
+ // An unreadable transcript store charts PortOS runs only rather than failing.
+ const localTranscript = await readLocalTranscriptUsage().catch(() => null);
// This machine always reads its live maps; with no minted identity yet it
// still has its own history to chart.
const sources = [
- { id: self?.instanceId ?? 'local', stamp: local?.lastUpdated, daily: local?.dailyActivity, monthly: local?.monthlyActivity },
- ...peers.map((e) => ({ id: e.instanceId, stamp: e.capturedAt, daily: e.usage.dailyActivity, monthly: e.usage.monthlyActivity })),
+ {
+ id: self?.instanceId ?? 'local',
+ stamp: [local?.lastUpdated, localTranscript?.updatedAt],
+ daily: local?.dailyActivity,
+ monthly: local?.monthlyActivity,
+ transcript: localTranscript?.days,
+ },
+ ...peers.map((e) => ({
+ id: e.instanceId,
+ stamp: e.capturedAt,
+ daily: e.usage.dailyActivity,
+ monthly: e.usage.monthlyActivity,
+ transcript: e.claudeCode?.days,
+ })),
];
const todayMs = dayMs(utcDay(now.getTime()));
diff --git a/server/services/peerUsage.test.js b/server/services/peerUsage.test.js
index 912dd4c78a..bfcf60546e 100644
--- a/server/services/peerUsage.test.js
+++ b/server/services/peerUsage.test.js
@@ -541,6 +541,32 @@ describe('fleet rolling 30-day cost', () => {
expect(series.points.at(-1).values).toEqual([series.points.at(-1).cost]);
});
+ // Transcripts hold every Claude Code session on the machine, PortOS's own
+ // runs included, and the ledger holds a subset of them: each day takes the
+ // larger, never the sum.
+ it('counts Claude Code from transcripts where they exceed the ledger, never on top', async () => {
+ const { atomicWrite } = await import('../lib/fileUtils.js');
+ const { summarizeTranscriptDays } = await import('./claudeCodeModelReport.js');
+ localUsage = usageFixture();
+ const bucket = localUsage.dailyActivity['2026-08-30'];
+ bucket.byProvider = { 'claude-code': bucket.byProvider.anthropic };
+ const ledger = dayCost(localUsage, '2026-08-30');
+ const counts = (output) => ({ messages: 4, input: 2_000_000, output, cacheRead: 0, cacheWrite: 0 });
+ const transcript = { '2026-08-30': { 'claude-opus-5': counts(3_000_000) }, '2026-08-29': { 'claude-opus-5': counts(1_000_000) } };
+ await atomicWrite(TRANSCRIPT_USAGE_FILE, { days: transcript, updatedAt: '2026-08-31T00:00:00.000Z' });
+ const dayTranscript = (day) => summarizeTranscriptDays({ [day]: transcript[day] }).totals.estimatedCost;
+ expect(dayTranscript('2026-08-30')).toBeGreaterThan(ledger);
+
+ const series = await getFleetRollingCost({ providers: [], now: new Date('2026-09-01T00:00:00Z') });
+ expect(series.series).toEqual([{ key: 'claude', label: 'Claude Code' }]);
+ expect(series.points.at(-1).cost).toBeCloseTo(dayTranscript('2026-08-30') + dayTranscript('2026-08-29'), 1);
+
+ // A day where the ledger already holds more keeps the ledger's figure.
+ await atomicWrite(TRANSCRIPT_USAGE_FILE, { days: { '2026-08-30': { 'claude-opus-5': { messages: 1, input: 1, output: 1, cacheRead: 0, cacheWrite: 0 } } }, updatedAt: '2026-08-31T01:00:00.000Z' });
+ const lower = await getFleetRollingCost({ providers: [], now: new Date('2026-09-01T00:00:00Z') });
+ expect(lower.points.at(-1).cost).toBeCloseTo(ledger, 2);
+ });
+
it('charts this machine alone before any peer has synced', async () => {
const series = await getFleetRollingCost({ providers: [], now: new Date('2026-09-01T00:00:00Z') });
expect(series.instanceCount).toBe(1);
diff --git a/server/services/usageInteractiveScan.js b/server/services/usageInteractiveScan.js
index 4f2d21bdcf..008999cc8c 100644
--- a/server/services/usageInteractiveScan.js
+++ b/server/services/usageInteractiveScan.js
@@ -1,12 +1,14 @@
-import { stat } from 'fs/promises';
+import { readdir, stat } from 'fs/promises';
import { homedir } from 'os';
-import { join } from 'path';
+import { basename, join } from 'path';
import { atomicWrite, PATHS, readJSONFile, tryReadFile } from '../lib/fileUtils.js';
import { createMutex } from '../lib/asyncMutex.js';
import {
UNKNOWN_MODEL,
decodeGrokSessionDir,
parseAgyTranscript,
+ parseClaudeTranscript,
+ parseCodexRollout,
parseGrokTurns,
parseJsonLines,
totalTranscriptTokens
@@ -23,11 +25,13 @@ import {
import { applyHistoricalUsageCorrections, forgetSiblingReconciledUsageRuns } from './usage.js';
/**
- * Attribute Grok and Antigravity sessions that PortOS never launched.
+ * Attribute Claude Code, Codex, Grok and Antigravity sessions that PortOS never
+ * launched.
*
* `usageReconciler.js` only reads a CLI's session store inside a PortOS run's
* own workspace and time window, so everything the user does in a terminal —
- * the interactive `grok` / `agy` sessions that actually burn the weekly quota —
+ * the interactive `claude` / `codex` / `grok` / `agy` sessions (and the desktop
+ * apps that write the same session stores) that actually burn the weekly quota —
* never reached the cost report, and both plans read as far cheaper than their
* subscription. This scan walks every session on the machine and bills what the
* run path did NOT.
@@ -49,7 +53,7 @@ import { applyHistoricalUsageCorrections, forgetSiblingReconciledUsageRuns } fro
export const INTERACTIVE_SCAN_FILE = join(PATHS.data, 'usage-interactive-scan.json');
-const FAMILIES = ['grok', 'agy'];
+const FAMILIES = ['claude', 'codex', 'grok', 'agy'];
const DAY_MS = 86_400_000;
// A CLI flushes an event slightly after it stamps it; leaving the newest few
// minutes for the next scan keeps a still-being-written turn from being cut.
@@ -118,7 +122,21 @@ const windowsFor = (byCwd, sessionCwd) => {
return out;
};
-/** Session files written since `from`: `{ cwd, path, mtimeMs, birthMs }`. */
+// The CLI's own override applies only to the real home; a fixture home wins.
+const cliHome = (home, envVar, dirName) => (home === homedir() && process.env[envVar]) || join(home, dirName);
+
+/** Every `.jsonl` under `root`, at any depth (Claude sidechains, Codex date dirs). */
+async function listJsonl(root) {
+ const entries = await readdir(root, { recursive: true, withFileTypes: true }).catch(() => []);
+ return entries
+ .filter((entry) => entry.isFile() && entry.name.endsWith('.jsonl'))
+ .map((entry) => join(entry.parentPath ?? entry.path, entry.name));
+}
+
+/**
+ * Session files written since `from`: `{ cwd, path, mtimeMs, birthMs }`. A null
+ * `cwd` on a Claude or Codex file is read from its own content in `parseSession`.
+ */
async function listSessions(family, home, from) {
const sessions = [];
const add = async (cwd, path) => {
@@ -126,7 +144,15 @@ async function listSessions(family, home, from) {
if (!info || info.mtimeMs < from) return;
sessions.push({ cwd, path, mtimeMs: info.mtimeMs, birthMs: info.birthtimeMs || info.mtimeMs });
};
- if (family === 'grok') {
+ if (family === 'claude') {
+ const root = join(cliHome(home, 'CLAUDE_CONFIG_DIR', '.claude'), 'projects');
+ for (const path of await listJsonl(root)) await add(null, path);
+ } else if (family === 'codex') {
+ const root = join(cliHome(home, 'CODEX_HOME', '.codex'), 'sessions');
+ for (const path of await listJsonl(root)) {
+ if (basename(path).startsWith('rollout-')) await add(null, path);
+ }
+ } else if (family === 'grok') {
const root = join(home, '.grok', 'sessions');
for (const dirName of await listSubdirs(root)) {
const cwd = decodeGrokSessionDir(dirName);
@@ -149,29 +175,50 @@ const addBuckets = (target, buckets) => {
for (const field of BUCKET_FIELDS) target[field] = (target[field] || 0) + (buckets[field] || 0);
};
+/** The working directory a Claude transcript or Codex rollout records about itself. */
+function cwdFromEntries(family, entries) {
+ for (const entry of entries) {
+ if (family === 'codex' && entry.type === 'session_meta' && typeof entry.payload?.cwd === 'string') return entry.payload.cwd;
+ if (family === 'claude' && typeof entry.cwd === 'string' && entry.cwd) return entry.cwd;
+ }
+ return null;
+}
+
+/**
+ * One slice's billed buckets per model. `seen` is the scan-wide set of Claude
+ * message keys already counted: a resumed or forked session replays earlier
+ * messages into a NEW file under their original ids and timestamps.
+ */
+function parseSlice(family, entries, slice, seen) {
+ if (family === 'claude') {
+ const parsed = parseClaudeTranscript(entries, { from: slice.from, to: slice.to, exclude: seen });
+ for (const key of parsed.countedKeys) seen.add(key);
+ return parsed.byModel;
+ }
+ // A rollout's totals are cumulative; a windowed read is the slice's delta.
+ if (family === 'codex') return parseCodexRollout(entries, { from: slice.from, to: slice.to }).byModel;
+ if (family === 'grok') return parseGrokTurns(entries, { from: slice.from, to: slice.to }).byModel;
+ return { [UNKNOWN_MODEL]: agyEstimatedBuckets(parseAgyTranscript(entries, { from: slice.from, to: slice.to })) };
+}
+
/** One session file's billed buckets per `{ day, model }` inside `[from, to]`, minus run windows. */
-async function parseSession(family, session, from, to, byCwd) {
+async function parseSession(family, session, from, to, byCwd, seen) {
const text = await tryReadFile(session.path);
if (!text) return [];
// Parsed once and windowed per day slice: re-splitting a multi-MB file per
// slice would cost ~35 parses of it on a first scan.
const entries = parseJsonLines(text);
+ const cwd = session.cwd ?? cwdFromEntries(family, entries);
// A file cannot hold anything before it existed or after it was last written.
// A birth time AFTER the last write means the file was copied or restored, so
// it says nothing about when its content began — read from the window start.
const lo = session.birthMs <= session.mtimeMs ? Math.max(from, session.birthMs - DAY_MS) : from;
const hi = Math.min(to, session.mtimeMs + WINDOW_SLACK_MS);
const results = [];
- for (const [a, b] of subtractWindows(lo, hi, windowsFor(byCwd, session.cwd))) {
+ for (const [a, b] of subtractWindows(lo, hi, windowsFor(byCwd, cwd))) {
for (const slice of splitByUtcDay(a, b)) {
- if (family === 'grok') {
- const parsed = parseGrokTurns(entries, { from: slice.from, to: slice.to });
- for (const [model, buckets] of Object.entries(parsed.byModel)) {
- if (totalTranscriptTokens(buckets) > 0) results.push({ day: slice.day, model, buckets });
- }
- } else {
- const buckets = agyEstimatedBuckets(parseAgyTranscript(entries, { from: slice.from, to: slice.to }));
- if (totalTranscriptTokens(buckets) > 0) results.push({ day: slice.day, model: UNKNOWN_MODEL, buckets });
+ for (const [model, buckets] of Object.entries(parseSlice(family, entries, slice, seen))) {
+ if (totalTranscriptTokens(buckets) > 0) results.push({ day: slice.day, model, buckets });
}
}
}
@@ -179,7 +226,8 @@ async function parseSession(family, session, from, to, byCwd) {
}
/**
- * Bill every un-launched Grok / Antigravity session since the last scan.
+ * Bill every un-launched Claude Code / Codex / Grok / Antigravity session since
+ * the last scan.
*
* Resolves with `{ families: { [family]: { days, tokens } } }`. A family with no
* configured provider is skipped WITHOUT advancing its watermark, so enabling
@@ -204,8 +252,9 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runs
if (to < from) continue;
const perDay = new Map();
+ const seen = new Set();
for (const session of await listSessions(family, home, from)) {
- for (const { day, model, buckets } of await parseSession(family, session, from, to, byCwd)) {
+ for (const { day, model, buckets } of await parseSession(family, session, from, to, byCwd, seen)) {
if (!perDay.has(day)) perDay.set(day, { byModel: {} });
const dayTotals = perDay.get(day);
addBuckets(dayTotals, buckets);
@@ -219,7 +268,8 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runs
const corrections = [];
let tokens = 0;
for (const [day, totals] of perDay) {
- const measured = { ...totals, source: family === 'grok' ? 'measured' : 'estimate', model: null };
+ // Antigravity writes no token counts; every other family's are the CLI's own.
+ const measured = { ...totals, source: family === 'agy' ? 'estimate' : 'measured', model: null };
const provider = resolveFamilyProvider(providerList, family, measured);
if (!provider) continue;
tokens += totalTranscriptTokens(totals);
diff --git a/server/services/usageInteractiveScan.test.js b/server/services/usageInteractiveScan.test.js
index 91c1da07e1..f8f7ec9488 100644
--- a/server/services/usageInteractiveScan.test.js
+++ b/server/services/usageInteractiveScan.test.js
@@ -17,6 +17,8 @@ const { refreshInteractiveUsage, INTERACTIVE_SCAN_FILE } = await import('./usage
const GROK = { id: 'grok-cli', type: 'cli', command: 'grok', enabled: true, defaultModel: 'example-grok-model' };
const AGY = { id: 'antigravity-cli', type: 'cli', command: 'agy', enabled: true, defaultModel: 'example-agy-model' };
+const CODEX = { id: 'codex-cli', type: 'cli', command: 'codex', enabled: true, defaultModel: 'example-codex-model' };
+const CLAUDE = { id: 'claude-code', type: 'cli', command: 'claude', enabled: true, defaultModel: 'example-claude-model' };
// Real time: a file's birth time can't be faked, and the scan prunes on it.
const NOW = Date.now();
const HOUR = 3_600_000;
@@ -125,4 +127,50 @@ describe('refreshInteractiveUsage', () => {
const record = applied.find((c) => c.providerId === 'antigravity-cli').siblings[0];
expect(record).toMatchObject({ source: 'estimate', tokensIn: 100, tokensOut: 50 });
});
+
+ // A rollout's token totals are cumulative, so the run-owned tail must not
+ // leak into the terminal slice and the terminal head must not be lost.
+ it('bills a terminal Codex rollout up to a PortOS run window, not past it', async () => {
+ const runDir = join(runsDir, 'run-1');
+ await mkdir(runDir, { recursive: true });
+ await writeFile(join(runDir, 'metadata.json'), JSON.stringify({
+ workspacePath: CWD, startTime: new Date(P3 - 60_000).toISOString(), endTime: new Date(P3 + 60_000).toISOString()
+ }));
+ const dir = join(home, '.codex', 'sessions', '2026', '10', '22');
+ await mkdir(dir, { recursive: true });
+ const snapshot = (ms, input, cached, output) => JSON.stringify({
+ timestamp: new Date(ms).toISOString(),
+ type: 'event_msg',
+ payload: { type: 'token_count', info: { total_token_usage: { input_tokens: input, cached_input_tokens: cached, output_tokens: output } } }
+ });
+ await writeFile(join(dir, 'rollout-1.jsonl'), [
+ JSON.stringify({ timestamp: new Date(P2 - 60_000).toISOString(), type: 'session_meta', payload: { id: 'r1', cwd: CWD, model: 'example-codex-model' } }),
+ snapshot(P2, 1000, 400, 100),
+ snapshot(P3, 3000, 1000, 500)
+ ].join('\n'));
+
+ const summary = await refreshInteractiveUsage({ home, runsDir, providers: [CODEX], now: NOW });
+ expect(summary.families.codex.tokens).toBe(600 + 400 + 100);
+ const records = applied.filter((c) => c.providerId === 'codex-cli').map((c) => c.siblings[0]);
+ expect(records.every((r) => r.source === 'measured')).toBe(true);
+ expect(records.reduce((sum, r) => sum + r.tokensOut, 0)).toBe(100);
+ });
+
+ // A resumed Claude Code session replays earlier messages into a new file
+ // under their original ids; they are billed once.
+ it('bills terminal Claude Code sessions once across resumed transcripts', async () => {
+ const dir = join(home, '.claude', 'projects', '-example-workspace');
+ await mkdir(join(dir, 'subagents'), { recursive: true });
+ const line = (id, ms, output) => JSON.stringify({
+ type: 'assistant', timestamp: new Date(ms).toISOString(), cwd: CWD, requestId: `req-${id}`,
+ message: { id, model: 'example-claude-model', usage: { input_tokens: 10, output_tokens: output, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }
+ });
+ await writeFile(join(dir, 'a.jsonl'), line('m1', P2, 100));
+ await writeFile(join(dir, 'subagents', 'b.jsonl'), [line('m1', P2, 100), line('m2', P2 + 60_000, 50)].join('\n'));
+
+ await refreshInteractiveUsage({ home, runsDir, providers: [CLAUDE], now: NOW });
+ const records = applied.filter((c) => c.providerId === 'claude-code').map((c) => c.siblings[0]);
+ expect(records.reduce((sum, r) => sum + r.tokensOut, 0)).toBe(150);
+ expect(records.reduce((sum, r) => sum + r.tokensIn, 0)).toBe(20);
+ });
});