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

Count Claude subagent usage and carry media units across the fleet (#13540)

[._.]/4390c3a69ddb
diff --git a/server/services/peerUsage.js b/server/services/peerUsage.js
index dd034b16cd..0160a0006a 100644
--- a/server/services/peerUsage.js
+++ b/server/services/peerUsage.js
@@ -68,7 +68,14 @@ const MONTH_KEY_RE = /^\d{4}-\d{2}$/;
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
const str = (v, max) => (isNonBlankStr(v) ? v.slice(0, max) : null);
-const COUNT_FIELDS = ['sessions', 'messages', 'tokens', 'tokensIn', 'tokensOut', 'cacheReadTokens', 'cacheWriteTokens'];
+// The media units and long-context subsets are priced at read time (usage.js
+// countFields), so a peer's digest has to carry them or its Grok/Antigravity
+// images, video seconds and 200k-context premium vanish from the fleet report.
+const COUNT_FIELDS = [
+ 'sessions', 'messages', 'tokens', 'tokensIn', 'tokensOut', 'cacheReadTokens', 'cacheWriteTokens',
+ 'longTokensIn', 'longTokensOut', 'longCacheReadTokens', 'longCacheWriteTokens',
+ 'images', 'videoSeconds', 'inputImages'
+];
function takeCounts(source, target) {
for (const field of COUNT_FIELDS) {
diff --git a/server/services/peerUsage.test.js b/server/services/peerUsage.test.js
index a54b21e74d..d71ee5bfbd 100644
--- a/server/services/peerUsage.test.js
+++ b/server/services/peerUsage.test.js
@@ -467,6 +467,19 @@ describe('fleet report', () => {
expect(whole.totals.tokensOut).toBe(7000);
});
+ it('carries a peer\'s media units and long-context subsets through the wire digest', async () => {
+ const usage = usageFixture({ day: '2026-01-15', tokensOut: 100 });
+ const bucket = usage.dailyActivity['2026-01-15'].byProvider.anthropic;
+ Object.assign(bucket, { images: 3, videoSeconds: 12, inputImages: 2, longTokensIn: 40, longTokensOut: 5 });
+ Object.assign(bucket.byModel['claude-opus-5'], { images: 3, videoSeconds: 12, inputImages: 2, longTokensIn: 40, longTokensOut: 5 });
+ await applyUsageRemote({ instances: { 'inst-peer': peerEntry({ usage: buildUsageDigest(usage, { now: new Date('2026-01-20T00:00:00Z') }) }) } });
+
+ const { data } = await getUsageSnapshot();
+ const stored = data.instances['inst-peer'].usage.dailyActivity['2026-01-15'].byProvider.anthropic;
+ expect(stored).toMatchObject({ images: 3, videoSeconds: 12, inputImages: 2, longTokensIn: 40, longTokensOut: 5 });
+ expect(stored.byModel['claude-opus-5']).toMatchObject({ images: 3, videoSeconds: 12, inputImages: 2, longTokensIn: 40 });
+ });
+
it('scopes every instance to the same report window', async () => {
await applyUsageRemote({ instances: { 'inst-peer': peerEntry() } });
const outside = await getFleetUsage({ from: '2026-09-01', to: '2026-09-30', providers: [] });
diff --git a/server/services/usageInteractiveScan.js b/server/services/usageInteractiveScan.js
index 780e8ccdbe..a691fbd0a7 100644
--- a/server/services/usageInteractiveScan.js
+++ b/server/services/usageInteractiveScan.js
@@ -93,7 +93,7 @@ function splitByUtcDay(from, to) {
}
/** Every PortOS run's `[start, end]` (slack applied) by workspace, for runs touching `[from, now]`. */
-async function loadRunWindows(runsDir, from, now) {
+async function loadRunWindows(runsDir, from, now, pick = () => true) {
const byCwd = new Map();
for (const name of await listSubdirs(runsDir)) {
const dir = join(runsDir, name);
@@ -102,7 +102,7 @@ async function loadRunWindows(runsDir, from, now) {
if (!info || info.mtimeMs < from - DAY_MS) continue;
const metadata = await readJSONFile(join(dir, 'metadata.json'), null);
const start = Date.parse(metadata?.startTime || '');
- if (!metadata?.workspacePath || Number.isNaN(start)) continue;
+ if (!metadata?.workspacePath || Number.isNaN(start) || !pick(metadata)) continue;
// No endTime yet: the run is still going, so its window is open-ended.
const end = Date.parse(metadata.endTime || '');
const window = [start - WINDOW_SLACK_MS, (Number.isNaN(end) ? now : end) + WINDOW_SLACK_MS];
@@ -218,7 +218,7 @@ function parseSlice(family, entries, slice, seen) {
}
/** One session file's billed buckets per `{ day, model }` inside `[from, to]`, minus run windows. */
-async function parseSession(family, session, from, to, byCwd, seen) {
+async function parseSession(family, session, from, to, byCwd, seen, regionsOf = (lo, hi, cwd) => subtractWindows(lo, hi, windowsFor(byCwd, cwd))) {
const text = await tryReadFile(session.path);
if (!text) return [];
// Parsed once and windowed per day slice: re-splitting a multi-MB file per
@@ -231,7 +231,7 @@ async function parseSession(family, session, from, to, byCwd, seen) {
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, cwd))) {
+ for (const [a, b] of regionsOf(lo, hi, cwd)) {
for (const slice of splitByUtcDay(a, b)) {
const parsed = parseSlice(family, entries, slice, seen);
let attachedIds = false;
@@ -246,6 +246,61 @@ async function parseSession(family, session, from, to, byCwd, seen) {
return results;
}
+const isSidechainPath = (path) => path.includes('/subagents/');
+
+/**
+ * One-time repair for Claude runs reconciled before the run path read subagent
+ * sidechains (`<session>/subagents/*.jsonl`). Those runs billed their parent
+ * transcript only, and this scan skips every run window, so the sidechain turns
+ * inside them were never billed by anything. Bills them once: sidechain messages
+ * inside the windows of runs reconciled before `cutoff`. Runs reconciled since
+ * (or not yet) read sidechains themselves, so their windows are carved out.
+ */
+async function repairSettledSidechains({ home, runsDir, providerList, now, cutoff, to }) {
+ const from = now - FIRST_SCAN_LOOKBACK_DAYS * DAY_MS;
+ const settledBefore = (metadata) => metadata.usageReconciled === true
+ && Date.parse(metadata.usageReconciledAt || '') < cutoff;
+ const settled = await loadRunWindows(runsDir, from, now, settledBefore);
+ const others = await loadRunWindows(runsDir, from, now, (metadata) => !settledBefore(metadata));
+ const regionsOf = (lo, hi, cwd) => {
+ const outside = subtractWindows(lo, hi, windowsFor(settled, cwd));
+ const inside = outside.reduce((pieces, [a, b]) => pieces.flatMap(([x, y]) => subtractWindows(x, y, [[a, b]])), [[lo, hi]]);
+ return inside.flatMap(([a, b]) => subtractWindows(a, b, windowsFor(others, cwd)));
+ };
+ const perDay = new Map();
+ const seen = new Set();
+ const sessions = (await listSessions('claude', home, from)).filter((session) => isSidechainPath(session.path));
+ for (const session of sessions) {
+ for (const { day, model, buckets } of await parseSession('claude', session, from, to, settled, seen, regionsOf)) {
+ if (!perDay.has(day)) perDay.set(day, { byModel: {} });
+ const dayTotals = perDay.get(day);
+ addBuckets(dayTotals, buckets);
+ dayTotals.byModel[model] ??= {};
+ addBuckets(dayTotals.byModel[model], buckets);
+ }
+ await yieldToEventLoop();
+ }
+ const corrections = [];
+ for (const [day, totals] of perDay) {
+ const measured = { ...totals, source: 'measured', model: null };
+ const provider = resolveFamilyProvider(providerList, 'claude', measured);
+ if (!provider) continue;
+ corrections.push({
+ runId: `sidechain-repair:${day}`,
+ day,
+ providerId: provider.id,
+ siblings: recordsFromMeasured(provider.id, provider.defaultModel ?? null, measured, 'sibling'),
+ siblingScanned: true,
+ repairIds: [],
+ });
+ }
+ if (corrections.length) {
+ await applyHistoricalUsageCorrections(corrections);
+ await forgetSiblingReconciledUsageRuns(corrections.map((c) => c.runId));
+ }
+ return corrections.reduce((sum, c) => sum + c.siblings.reduce((n, r) => n + (r.tokensOut || 0), 0), 0);
+}
+
/**
* Bill every un-launched Claude Code / Codex / Grok / Antigravity session since
* the last scan.
@@ -267,6 +322,16 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runs
const summary = { families: {} };
const byCwd = await loadRunWindows(runsDir, now - FIRST_SCAN_LOOKBACK_DAYS * DAY_MS, now);
+ let sidechainRepairedAt = state?.sidechainRepairedAt ?? null;
+ if (!sidechainRepairedAt && resolveFamilyProvider(providerList, 'claude')) {
+ // Process start, not `now`: a run reconciled after boot already ran the fixed reader.
+ const cutoff = now - process.uptime() * 1000;
+ const outputTokens = await repairSettledSidechains({ home, runsDir, providerList, now, cutoff, to });
+ sidechainRepairedAt = new Date(now).toISOString();
+ summary.sidechainRepair = { tokensOut: outputTokens };
+ if (outputTokens > 0) console.log(`💸 Settled Claude subagent usage billed: ${outputTokens} output tokens`);
+ }
+
for (const family of FAMILIES) {
if (!resolveFamilyProvider(providerList, family)) continue;
const from = watermarks[family] != null ? watermarks[family] + 1 : now - FIRST_SCAN_LOOKBACK_DAYS * DAY_MS;
@@ -319,7 +384,7 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runs
if (tokens > 0) console.log(`💸 Interactive ${family} usage billed: ${tokens} tokens over ${corrections.length} day(s)`);
}
- await atomicWrite(INTERACTIVE_SCAN_FILE, { watermarks, updatedAt: new Date(now).toISOString() });
+ await atomicWrite(INTERACTIVE_SCAN_FILE, { watermarks, sidechainRepairedAt, updatedAt: new Date(now).toISOString() });
return summary;
})
);
diff --git a/server/services/usageInteractiveScan.test.js b/server/services/usageInteractiveScan.test.js
index f8f7ec9488..f6b73d595f 100644
--- a/server/services/usageInteractiveScan.test.js
+++ b/server/services/usageInteractiveScan.test.js
@@ -173,4 +173,31 @@ describe('refreshInteractiveUsage', () => {
expect(records.reduce((sum, r) => sum + r.tokensOut, 0)).toBe(150);
expect(records.reduce((sum, r) => sum + r.tokensIn, 0)).toBe(20);
});
+
+ it('bills subagent turns inside a run reconciled before the sidechain fix, once, and leaves later runs alone', async () => {
+ const dir = join(home, '.claude', 'projects', '-example-workspace', 'sess-1', 'subagents');
+ await mkdir(dir, { 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 } }
+ });
+ const SETTLED = NOW - 10 * HOUR;
+ const LATER = NOW - 2 * HOUR;
+ await writeFile(join(dir, 'agent-1.jsonl'), [line('old', SETTLED, 300), line('new', LATER, 700)].join('\n'));
+ const writeRun = async (name, at, reconciledAt) => {
+ await mkdir(join(runsDir, name), { recursive: true });
+ await writeFile(join(runsDir, name, 'metadata.json'), JSON.stringify({
+ workspacePath: CWD, startTime: new Date(at - 60_000).toISOString(), endTime: new Date(at + 60_000).toISOString(),
+ usageReconciled: true, usageReconciledAt: new Date(reconciledAt).toISOString()
+ }));
+ };
+ await writeRun('settled', SETTLED, NOW - 9 * HOUR);
+ // Reconciled after this process started, so the fixed run path already billed its sidechains.
+ await writeRun('fixed', LATER, NOW + HOUR);
+
+ const first = await refreshInteractiveUsage({ home, runsDir, providers: [CLAUDE], now: NOW });
+ expect(first.sidechainRepair.tokensOut).toBe(300);
+ const second = await refreshInteractiveUsage({ home, runsDir, providers: [CLAUDE], now: NOW + 10 * 60_000 });
+ expect(second.sidechainRepair).toBeUndefined();
+ });
});
diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.js
index 9ec3dccf4c..3c5611fe40 100644
--- a/server/services/usageReconciler.js
+++ b/server/services/usageReconciler.js
@@ -428,9 +428,13 @@ async function readClaudeSessions({ home, workspacePath, from, to, fold, reserve
// The project directory name is the slugified cwd — an exact lookup, with
// no directory scan and no chance of picking up another repo's sessions.
const projectDir = join(home, '.claude', 'projects', claudeProjectSlug(workspacePath));
- for (const file of await listDir(projectDir)) {
- if (!file.endsWith('.jsonl')) continue;
- const path = join(projectDir, file);
+ // Recursive: Claude Code writes each subagent's turns to
+ // `<session>/subagents/agent-*.jsonl`, not into the parent transcript, and
+ // those sidechains carry most of a delegating run's output tokens.
+ const entries = await readdir(projectDir, { recursive: true, withFileTypes: true }).catch(() => []);
+ for (const entry of entries) {
+ if (!entry.isFile() || !entry.name.endsWith('.jsonl')) continue;
+ const path = join(entry.parentPath ?? entry.path, entry.name);
const text = await tryReadFile(path);
if (!text) continue;
const parsed = parseClaudeTranscript(text, { from, to, exclude: excludeFor(path) });
diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.js
index 9ea70d9dca..eceb6a58a0 100644
--- a/server/services/usageReconciler.test.js
+++ b/server/services/usageReconciler.test.js
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtemp, mkdir, writeFile, rm } from 'fs/promises';
import { tmpdir } from 'os';
-import { join } from 'path';
+import { dirname, join } from 'path';
vi.mock('./usage.js', () => ({
markUsageRunReconciled: vi.fn().mockResolvedValue(undefined),
@@ -50,6 +50,7 @@ const claudeAssistant = ({ id, timestamp, input = 10, cacheWrite = 100, cacheRea
const writeClaudeSession = async (file, lines, slug = PROJECT_SLUG) => {
const dir = join(home, '.claude', 'projects', slug);
await mkdir(dir, { recursive: true });
+ await mkdir(dirname(join(dir, file)), { recursive: true });
await writeFile(join(dir, file), lines.join('\n'));
};
@@ -201,6 +202,25 @@ describe('readMeasuredUsage', () => {
});
});
+ it('bills subagent sidechain transcripts nested under the session directory', async () => {
+ await writeClaudeSession('a.jsonl', [
+ claudeAssistant({ id: 'parent', timestamp: '2026-07-01T10:05:00.000Z' })
+ ]);
+ await writeClaudeSession('sess-1/subagents/agent-1.jsonl', [
+ claudeAssistant({ id: 'child', timestamp: '2026-07-01T10:06:00.000Z', output: 400 })
+ ]);
+
+ 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).toMatchObject({ sessions: 2, tokensOut: 450 });
+ });
+
it('excludes sessions outside the run window', async () => {
await writeClaudeSession('a.jsonl', [
claudeAssistant({ id: 'yesterday', timestamp: '2026-06-30T10:00:00.000Z' }),