diff --git a/AGENTS.md b/AGENTS.md index 5ec4b9461..7503aadcd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -167,6 +167,7 @@ lib/ pending-requests.ts daemon side: `PendingRequests`, a channel's still-open blocking requests and current session id for the hello daemon-broker.ts daemon side: the loopback forwarder that gives a sharing host's omp processes `OMP_AUTH_BROKER_URL`/`OMP_AUTH_BROKER_TOKEN` (`credentialsEnv`); forwards to the link's server with the link's host token registry.ts `/omp-web/hosts.json`: create/update/revoke, token hashing and lookup; the `local` record (`rotateLocalHostToken`, `customLocalLabel`) + usage-sync.ts main side of usage from linked hosts: `pullAllHostUsage` pulls each linked host's transcript stamps and records (`listUsageTranscripts`, `readUsageRecords`) into `usage.db` on `/api/usage`, 30 s TTL per host (a forced refresh reads every transcript again); a host's sync state follows its link status, its last sync time is kept in `usage.db` session-index.ts sessions and projects across hosts, cached per host; `hostForSessionId`, `sessionSpawnTarget`, `invalidateLocalSessionLists` cluster.ts pure cluster rules: `clusterOf` (= `pools[0]`), `isClusterMember`, `sharesCredentials`, `joinCluster`/`leaveCluster`, `clusterJoinBlock`, `ompSharesCredentials` (also the daemon's spawn check); browser-importable (Settings uses it) cluster-config.ts pure, daemon-safe: what a cluster member mirrors from the master (`MIRRORED`, `LOCAL_SETTINGS`, `CONFIG_LIMITS`), `buildManifest`, `stripLocalSettings`/`mergeLocalSettings`, `isManagedPath`, `ClusterConfigError` @@ -1690,7 +1691,7 @@ during the wait. `invalidateSessionFileListCache()` — never add a session-mutation path that forgets this. Regression test: `session-reader.test.mjs`. - A mutation that main triggers calls `invalidateLocalSessionLists()`: it clears main's per-host entry and sends the daemon `invalidateSessionLists` (the daemon runs the invalidation above). A route that changes this machine's session files and does not call it leaves the sidebar stale for up to 30 s. -- **Still read on main, from this machine's own files:** transcript search (`session-content-search.ts`, `/api/sessions/search`), the session watcher (`session-watcher.ts`), this machine's archive list/restore (linked hosts' go through their daemons), import, usage (`usage-db.ts`), subagent history and local file-reference readers. Their main-side session caches are invalidated separately. Workspace shell-output authorization and bytes now come from the owning daemon; new remote-capable readers must use the host seam. +- **Still read on main, from this machine's own files:** transcript search (`session-content-search.ts`, `/api/sessions/search`), the session watcher (`session-watcher.ts`), this machine's archive list/restore (linked hosts' go through their daemons), import, this machine's usage (`usage-db.ts`; linked hosts' usage comes through `lib/hosts/usage-sync.ts`), subagent history and local file-reference readers. Their main-side session caches are invalidated separately. Workspace shell-output authorization and bytes now come from the owning daemon; new remote-capable readers must use the host seam. ### Chat scroll-follow - `useAgentSession` follows the conversation: the effect depends on both diff --git a/CHANGELOG.md b/CHANGELOG.md index ec584eb19..cd689f8e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,13 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f ## [Unreleased] +### Added + +- **Usage includes linked hosts.** Settings → Usage now counts the tokens and cost of sessions on every linked host, not only this machine. Each host's daemon reads its own transcripts; the web server keeps a copy, so a host that goes offline keeps its last numbers. A new **Host** breakdown shows each machine's share and when an offline host last synced, and project rows name their host. A host that is still syncing fills in on its own. Refresh reads every host's sessions again, with the prices in that host's `models.yml`. A host on an older ompweb is listed as needing an update. + ### Fixed +- **Refreshing Usage applies changed prices, and says when it fails.** Prices edited in `models.yml` now apply to existing sessions when you refresh Settings → Usage. They used to keep the old prices until the web server restarted. A refresh that fails now shows the error above the last report instead of leaving it up without a word. - **Windows updates no longer fail when the update status is read while it is written.** Host update state, the server worker's status and upgrade plans retry their atomic replacement for a moment when a concurrent reader holds the file, as the server's own update state already did. - **GitHub workflow watches no longer regress to stale run states.** Workflow updates retain GitHub's run timestamp and ignore older snapshots for the same or an earlier attempt, so a delayed `queued` response cannot follow a newer `in_progress` delivery. - **Keep sidebar transcript results aligned with expandable hits on every linked host.** Each daemon now searches its own active conversations, ignoring abandoned branches and pre-compaction messages. diff --git a/app/api/usage/route.ts b/app/api/usage/route.ts index 4e353ece4..96517f83b 100644 --- a/app/api/usage/route.ts +++ b/app/api/usage/route.ts @@ -1,5 +1,5 @@ import { apiErrorResponse } from "@/lib/api-utils"; -import { getUsageReport } from "@/lib/usage-service"; +import { getUsageReport } from "@/lib/usage-report"; import type { UsageGranularity, UsageTimeRange } from "@/lib/usage-types"; export async function GET(req: Request) { diff --git a/components/UsageConfig.hosts.test.mjs b/components/UsageConfig.hosts.test.mjs new file mode 100644 index 000000000..42427d5cc --- /dev/null +++ b/components/UsageConfig.hosts.test.mjs @@ -0,0 +1,173 @@ +import "../tests/setup-dom.mjs"; +import assert from "node:assert/strict"; +import test from "node:test"; +import { act, createElement } from "react"; +const { cleanup, fireEvent, render, within } = await import("@testing-library/react/pure.js"); +const { UsageConfig } = await import("./UsageConfig"); + +const settle = () => act(() => new Promise((resolve) => setTimeout(resolve, 20))); + +const host = (hostId, label, state, syncedAt, cost) => ({ hostId, label, state, syncedAt, cost, tokens: cost * 1000, share: 0, sessionsCount: 1 }); +const project = (hostId, hostLabel, name) => ({ + hostId, hostLabel, project: `/work/${name}`, projectName: name, cost: 1, tokens: 1000, share: 50, sessionsCount: 1, +}); + +/** Answer `/api/usage` with `reports` in turn, the last one from then on; `null` answers 503. Returns the URLs asked for. */ +function fakeUsage(t, ...reports) { + const requests = []; + const originalFetch = globalThis.fetch; + globalThis.fetch = async (url) => { + if (!String(url).startsWith("/api/usage")) return Response.json({}); + requests.push(String(url)); + const next = reports[Math.min(requests.length, reports.length) - 1]; + return next ? Response.json(next) : new Response("", { status: 503, statusText: "Service Unavailable" }); + }; + t.after(() => { + cleanup(); + globalThis.fetch = originalFetch; + }); + return requests; +} + +/** Fake `setTimeout`. The returned function advances it by `ms`, then lets pending fetches answer and React render. */ +function mockTimers(t) { + t.mock.timers.enable({ apis: ["setTimeout"] }); + t.after(() => t.mock.timers.reset()); + return (ms = 0) => act(async () => { + t.mock.timers.tick(ms); + for (let i = 0; i < 30; i++) await Promise.resolve(); + }); +} + +function report(extra) { + return { + summary: { + totalCost: 3, totalTokens: 3000, inputTokens: 2000, outputTokens: 1000, reasoningTokens: 0, + cacheReadTokens: 0, cacheWriteTokens: 0, cachePercentage: 0, cacheSavings: 0, tokensPerActiveDay: 3000, + costQuality: { providerReported: 100, modelPriced: 0, unpriced: 0 }, + }, + scanInfo: { transcriptsScanned: 2, transcriptsOutsideWindow: 0, usageRecordsCount: 2, durationSeconds: 0.1 }, + providers: [], modelBreakdown: [], dayBreakdown: [], timeSeries: [], + projectBreakdown: [project("local", "", "web"), project("h1", "laptop", "api")], + ...extra, + }; +} + +test("the HOST breakdown lists every machine with its sync state", async (t) => { + const syncedAt = Date.UTC(2026, 10, 10, 12, 0); + fakeUsage(t, report({ + hostBreakdown: [ + host("local", "", "current", null, 2), + host("h1", "laptop", "offline", syncedAt, 1), + host("h2", "build", "unsupported", null, 0), + host("h3", "tablet", "stale", null, 0), + host("h4", "nas", "syncing", null, 0), + ], + })); + const { container } = render(createElement(UsageConfig)); + await settle(); + fireEvent.click(within(container).getByRole("button", { name: "HOST" })); + const rows = [...container.querySelectorAll("tbody tr")].map((row) => row.cells[0].textContent); + assert.deepEqual(rows, [ + "This machine", + `laptopOffline · last synced ${new Date(syncedAt).toLocaleString()}`, + "buildUpdate ompweb on this host to include its usage", + "tabletNot synced yet", + "nasSyncing…", + ]); +}); + +test("project rows of a linked host name it", async (t) => { + fakeUsage(t, report({ hostBreakdown: [host("local", "", "current", null, 2), host("h1", "laptop", "current", 1, 1)] })); + const { container } = render(createElement(UsageConfig)); + await settle(); + fireEvent.click(within(container).getByRole("button", { name: "PROJECT" })); + const rows = [...container.querySelectorAll("tbody tr")].map((row) => row.cells[0].textContent); + assert.deepEqual(rows, ["web", "api · laptop"]); +}); + +test("a single machine shows no HOST toggle", async (t) => { + fakeUsage(t, report({ hostBreakdown: [host("local", "", "current", null, 3)] })); + const { container } = render(createElement(UsageConfig)); + await settle(); + assert.ok(within(container).getByRole("button", { name: "PROJECT" })); + assert.equal(within(container).queryByRole("button", { name: "HOST" }), null); +}); + +test("a report without hostBreakdown shows no HOST toggle", async (t) => { + fakeUsage(t, report({})); + const { container } = render(createElement(UsageConfig)); + await settle(); + assert.ok(within(container).getByRole("button", { name: "PROJECT" })); + assert.equal(within(container).queryByRole("button", { name: "HOST" }), null); +}); + +test("while a host is syncing the report is fetched again, until the host is done", async (t) => { + const laptop = (state) => report({ hostBreakdown: [host("local", "", "current", null, 2), host("h1", "laptop", state, null, 1)] }); + const requests = fakeUsage(t, laptop("syncing"), laptop("syncing"), laptop("current")); + const advance = mockTimers(t); + const { container } = render(createElement(UsageConfig)); + await advance(); + fireEvent.click(within(container).getByRole("button", { name: "HOST" })); + const laptopRow = () => container.querySelectorAll("tbody tr")[1].cells[0].textContent; + assert.deepEqual([requests.length, laptopRow()], [1, "laptopSyncing…"]); + + await advance(1999); + assert.equal(requests.length, 1); + await advance(1); + assert.deepEqual([requests.length, laptopRow()], [2, "laptopSyncing…"]); + await advance(2000); + assert.deepEqual([requests.length, laptopRow()], [3, "laptop"]); + assert.ok(requests.every((url) => !url.includes("refresh")), "polling never forces a refresh"); + + await advance(60_000); + assert.equal(requests.length, 3, "nothing is syncing any more"); +}); + +test("leaving the page stops fetching for a host that is still syncing", async (t) => { + const requests = fakeUsage(t, report({ hostBreakdown: [host("local", "", "current", null, 2), host("h1", "laptop", "syncing", null, 1)] })); + const advance = mockTimers(t); + const { unmount } = render(createElement(UsageConfig)); + await advance(); + unmount(); + await advance(60_000); + assert.equal(requests.length, 1); +}); + +test("a background update that fails is shown above the report it could not replace, until one arrives", async (t) => { + const laptop = (state) => report({ hostBreakdown: [host("local", "", "current", null, 2), host("h1", "laptop", state, null, 1)] }); + const requests = fakeUsage(t, laptop("syncing"), null, laptop("current")); + const advance = mockTimers(t); + const { container } = render(createElement(UsageConfig)); + await advance(); + assert.equal(within(container).queryByRole("alert"), null); + + await advance(2000); + assert.equal(within(container).getByRole("alert").textContent, "Could not load usage data: Failed to fetch usage: Service Unavailable"); + assert.ok(within(container).getByRole("button", { name: "HOST" }), "the report stays on screen"); + + await advance(2000); + assert.deepEqual([requests.length, within(container).queryByRole("alert")], [3, null]); +}); + +test("a background update still on its way when Refresh is pressed does not replace the refreshed report", async (t) => { + const laptop = (state) => report({ hostBreakdown: [host("local", "", "current", null, 2), host("h1", "laptop", state, null, 1)] }); + const requests = fakeUsage(t, laptop("syncing"), laptop("current"), laptop("syncing")); + const delivered = globalThis.fetch; + const late = Promise.withResolvers(); + let calls = 0; + // The second call, the background update, answers only once the refresh has. + globalThis.fetch = (url, init) => (++calls === 2 ? late.promise.then(() => delivered(url, init)) : delivered(url, init)); + const advance = mockTimers(t); + const { container } = render(createElement(UsageConfig)); + await advance(); + fireEvent.click(within(container).getByRole("button", { name: "HOST" })); + const laptopRow = () => container.querySelectorAll("tbody tr")[1].cells[0].textContent; + + await advance(2000); + fireEvent.click(within(container).getByRole("button", { name: "Refresh usage data" })); + await advance(); + late.resolve(); + await advance(); + assert.deepEqual([requests.length, laptopRow()], [3, "laptop"]); +}); diff --git a/components/UsageConfig.tsx b/components/UsageConfig.tsx index c0c35d340..be01d0a28 100644 --- a/components/UsageConfig.tsx +++ b/components/UsageConfig.tsx @@ -5,6 +5,7 @@ import { AlertCircle, Loader2, RefreshCw } from "lucide-react"; import { useI18n } from "@/lib/i18n"; import { ThemedSelect } from "@/components/ui/primitives"; import type { + HostUsageSummary, UsageBreakdownView, UsageGranularity, UsageMetricView, @@ -35,6 +36,9 @@ function formatCurrency(amount: number): string { return `$${amount.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } +/** How long after a report with a host still syncing the next one is fetched. */ +const HOST_SYNC_POLL_MS = 2000; + export function UsageConfig() { const { t } = useI18n(); @@ -52,47 +56,76 @@ export function UsageConfig() { const [hoverIndex, setHoverIndex] = useState(null); const [hoverPos, setHoverPos] = useState<{ x: number; y: number } | null>(null); const chartSvgRef = useRef(null); + const requestRef = useRef(null); const fetchUsage = useCallback( - async (isRefresh = false, signal?: AbortSignal) => { - if (isRefresh) setRefreshing(true); - else setLoading(true); - setError(null); + // `poll` refetches behind the page: no spinner, and an earlier failure stays up until an answer arrives. + async (mode: "load" | "refresh" | "poll" = "load") => { + requestRef.current?.abort(); + const controller = new AbortController(); + requestRef.current = controller; + const { signal } = controller; + + if (mode === "refresh") setRefreshing(true); + else if (mode === "load") setLoading(true); + if (mode !== "poll") setError(null); try { const params = new URLSearchParams({ range: timeRange, granularity, }); - if (isRefresh) params.set("refresh", "true"); + if (mode === "refresh") params.set("refresh", "true"); const res = await fetch(`/api/usage?${params.toString()}`, { signal }); if (!res.ok) { throw new Error(`Failed to fetch usage: ${res.statusText}`); } const data: UsageReport = await res.json(); + if (signal.aborted) return; setReport(data); + setError(null); } catch (err) { - if (err instanceof DOMException && err.name === "AbortError") { + if (signal.aborted || (err instanceof DOMException && err.name === "AbortError")) { return; } setError(err instanceof Error ? err.message : String(err)); } finally { - setLoading(false); - setRefreshing(false); + if (!signal.aborted && mode !== "poll") { + setLoading(false); + setRefreshing(false); + } } }, [timeRange, granularity], ); useEffect(() => { - const controller = new AbortController(); - fetchUsage(false, controller.signal); + void fetchUsage("load"); return () => { - controller.abort(); + requestRef.current?.abort(); }; }, [fetchUsage]); + // The server answers after 10 s at most and lets slower hosts finish behind the report. + const hostsSyncing = report?.hostBreakdown?.some((host) => host.state === "syncing") ?? false; + useEffect(() => { + if (!hostsSyncing || loading || refreshing) return; + let cancelled = false; + let timer: number | undefined; + const poll = () => { + timer = window.setTimeout(async () => { + await fetchUsage("poll"); + if (!cancelled) poll(); + }, HOST_SYNC_POLL_MS); + }; + poll(); + return () => { + cancelled = true; + window.clearTimeout(timer); + }; + }, [hostsSyncing, loading, refreshing, fetchUsage]); + // Date range subtitle calculation const dateRangeSubtitle = useMemo(() => { if (!report || !report.timeSeries || report.timeSeries.length === 0) { @@ -265,7 +298,7 @@ export function UsageConfig() { {error} + {showHosts && ( + + )} @@ -1009,11 +1082,13 @@ export function UsageConfig() { - {breakdownView === "model" + {view === "model" ? t("usageConfig.model") - : breakdownView === "day" + : view === "day" ? t("usageConfig.day") - : t("usageConfig.project")} + : view === "host" + ? t("usageConfig.host") + : t("usageConfig.project")} {t("usageConfig.cost")} {t("usageConfig.share")} @@ -1021,7 +1096,7 @@ export function UsageConfig() { - {breakdownView === "model" && + {view === "model" && (modelBreakdown.length === 0 ? ( @@ -1052,7 +1127,7 @@ export function UsageConfig() { )) ))} - {breakdownView === "day" && + {view === "day" && (dayBreakdown.length === 0 ? ( @@ -1078,7 +1153,7 @@ export function UsageConfig() { )) ))} - {breakdownView === "project" && + {view === "project" && (projectBreakdown.length === 0 ? ( @@ -1087,9 +1162,12 @@ export function UsageConfig() { ) : ( projectBreakdown.map((p) => ( - + - {p.projectName} + + {p.projectName} + {p.hostId && p.hostId !== "local" ? ` · ${p.hostLabel}` : ""} + {formatCurrency(p.cost)} @@ -1103,6 +1181,30 @@ export function UsageConfig() { )) ))} + + {view === "host" && + hostBreakdown.map((h) => { + const syncLine = hostSyncLine(h); + return ( + + +
{h.label || t("hostsConfig.thisMachine")}
+ {syncLine && ( +
{syncLine}
+ )} + + + {formatCurrency(h.cost)} + + + {h.share.toFixed(1)}% + + + {formatTokens(h.tokens)} + + + ); + })} diff --git a/docs/specs/2026-10-28-remote-hosts-design.md b/docs/specs/2026-10-28-remote-hosts-design.md index 325ef0dca..7acfdb0aa 100644 --- a/docs/specs/2026-10-28-remote-hosts-design.md +++ b/docs/specs/2026-10-28-remote-hosts-design.md @@ -503,6 +503,7 @@ parts of those routes stay on main. | `spawn {ch, cwd, sessionFile, toolNames, advisor}` | `startRpcSession` spawn | | `sessionFileExists {path}` / `invalidateSessionLists {path}` | `AgentSessionWrapper` | | `uploadStart {id, name, size, type, attempt}` / `uploadAbort {id}` / `uploadAttach {id, sessionFile, sessionId}` | new in v6: composer uploads (`docs/specs/2026-11-02-file-uploads-design.md`, U3, U5) | +| `listUsageTranscripts {refresh}` / `readUsageRecords {requests}` | Usage from linked hosts (`lib/hosts/usage-sync.ts`): compatible addition (the link version is unchanged; older daemons answer 501 `unknown_op`, and Usage lists that host as needing an update). The first lists stamps (`path`, `mtimeMs`, `size`) of each session's transcript and artifact transcripts with the host's `parserVersion`; `refresh` drops the host's cached parses and prices. The second parses only paths the host lists itself, priced with the host's `models.yml`, and answers `{files, missing}` with at most 8 MiB of serialized records; a path in neither did not fit this page and main asks again. A transcript with more records than one page holds ends its page with `more` and continues when its request comes back with `skip` (the records received so far); main holds the slices and stores the transcript whole, or fails the pull if its stamp changed in between. Main stores each page as it arrives and drops rows only once the whole pull was read: those of transcripts a read reported `missing` and of those the index left out. So neither op guesses: only a sessions root or a session's artifacts directory with nothing at its path counts as empty, and any other directory or transcript that cannot be read to its end answers 500 `usage_unreadable`. Records are bounded on both sides (`USAGE_RECORD_LIMITS`): the parser skips a line whose numbers no request can have, and main refuses such a record | | `setUploadRetention {days}` | compatible addition (the link version is unchanged; older daemons answer 501 `unknown_op`, which main ignores): days to keep sent uploads, `null` = for ever; sent on every hello and when the web setting changes (`docs/specs/2026-11-02-file-uploads-design.md`, U6) | Op timeout: 30 s of silence (each result byte frame that arrives restarts it), then 504 naming the host; `uploadAttach` gets 10 minutes (a 2 GiB copy across file systems). diff --git a/lib/hosts/daemon.test.mjs b/lib/hosts/daemon.test.mjs index b6734c191..efaf8ed6f 100644 --- a/lib/hosts/daemon.test.mjs +++ b/lib/hosts/daemon.test.mjs @@ -899,6 +899,41 @@ test("an unknown op gets a 501 res, not a refused line", async () => { } }); +test("usage ops are answered from this host's sessions dir", async () => { + const log = captureLog(); + const projectDir = join(process.env.PI_CODING_AGENT_DIR, "sessions", "-usage"); + const session = join(projectDir, "2026-01-01_usage.jsonl"); + const subagent = join(projectDir, "2026-01-01_usage", "Sub.jsonl"); + mkdirSync(dirname(subagent), { recursive: true }); + const request = (id, input) => JSON.stringify({ type: "message", id, timestamp: "2026-01-01T00:00:01.000Z", + message: { role: "assistant", provider: "anthropic", model: "claude-sonnet-4-5", usage: { input, output: 1 } } }); + writeFileSync(session, [JSON.stringify({ type: "session", id: "usage", cwd: "/work/usage", timestamp: "2026-01-01T00:00:00.000Z" }), request("u1", 10)].join("\n") + "\n"); + writeFileSync(subagent, request("s1", 20) + "\n"); + const server = scriptedServer(); + const daemon = start(server); + try { + await waitFor(() => server.hellos.length === 1, "the hello"); + server.send({ type: "req", id: "u1", op: "listUsageTranscripts", args: { refresh: true } }); + await waitFor(() => server.res("u1"), "the usage index"); + const index = server.res("u1").result; + assert.equal(index.ok, true, JSON.stringify(index)); + const listed = index.value.sessions.find((entry) => entry.session === session); + assert.deepEqual(listed?.transcripts.map((stamp) => stamp.path).sort(), [session, subagent].sort()); + + server.send({ type: "req", id: "u2", op: "readUsageRecords", args: { requests: [{ session, paths: [session, subagent, "/etc/hosts"] }] } }); + await waitFor(() => server.res("u2"), "the usage records"); + const page = server.res("u2").result; + assert.equal(page.ok, true, JSON.stringify(page)); + assert.deepEqual(page.value.files.map((file) => [file.path, file.records.map((record) => [record.sessionId, record.input])]), + [[session, [["usage", 10]]], [subagent, [["usage", 20]]]]); + assert.deepEqual(page.value.missing, ["/etc/hosts"]); + } finally { + await daemon.stop(); + log.restore(); + rmSync(projectDir, { recursive: true, force: true }); + } +}); + test("setUploadRetention validates days, persists them under the agent dir and sweeps; the next daemon sweeps with them at start", async () => { const log = captureLog(); const upload = (name, ageDays) => { diff --git a/lib/hosts/hub.test.mjs b/lib/hosts/hub.test.mjs index 7c79371c0..33e78202d 100644 --- a/lib/hosts/hub.test.mjs +++ b/lib/hosts/hub.test.mjs @@ -546,6 +546,52 @@ test("export, session and context results must have the fields the routes read", await link.down.cancel(); }); +test("usage index and record results must have every field usage storage reads", async () => { + const link = await linkedHost("Usage shapes"); + let seq = 0; + const answer = async (call, value) => { + const pending = call(); + const req = await link.down.nextReal(); + await linkPost(link, [{ type: "res", seq: ++seq, id: req.id, ok: true, result: { ok: true, value } }], req.seq); + return pending; + }; + const ops = link.remote.sessionOps; + const badResult = (error) => error.status === 502 && error.code === "host_bad_result"; + const stamp = { path: "/s/a.jsonl", mtimeMs: 1.5, size: 10 }; + const index = { parserVersion: 2, sessions: [{ session: "/s/a.jsonl", transcripts: [stamp] }] }; + const record = { + timestamp: 1, sessionId: "a", sessionCwd: "/w", provider: "p", model: "m", input: 1, output: 2, reasoning: 0, + cacheRead: 0, cacheWrite: 0, totalTokens: 3, cost: 0.1, cacheSavings: 0, costQuality: "model_priced", sessionStarted: 1, + }; + const page = { files: [{ ...stamp, records: [record, { ...record, entryId: "e#Sub", subagentFile: "/s/a/Sub.jsonl" }] }], missing: ["/gone"] }; + try { + for (const malformed of [ + null, { sessions: [] }, { ...index, parserVersion: 2.5 }, { ...index, sessions: [{ session: "", transcripts: [stamp] }] }, + { ...index, sessions: [{ session: "/s/a.jsonl", transcripts: [{ ...stamp, size: 0 }] }] }, + { ...index, sessions: [{ session: "/s/a.jsonl", transcripts: [{ ...stamp, mtimeMs: "1" }] }] }, + ]) await assert.rejects(answer(() => ops.listUsageTranscripts(false), malformed), badResult); + assert.deepEqual((await answer(() => ops.listUsageTranscripts(false), index)).value, index); + + const withFile = (file) => ({ ...page, files: [file] }); + for (const malformed of [ + null, { ...page, missing: [1] }, { ...page, missing: undefined }, { ...page, files: undefined }, + withFile({ ...stamp, records: undefined }), withFile({ ...stamp, path: "", records: [] }), withFile({ ...stamp, records: [], more: "yes" }), + withFile({ ...stamp, records: [{ ...record, costQuality: "guessed" }] }), + withFile({ ...stamp, records: [{ ...record, cost: "0.1" }] }), + withFile({ ...stamp, records: [{ ...record, totalTokens: Number.POSITIVE_INFINITY }] }), + withFile({ ...stamp, records: [{ ...record, totalTokens: 1e10 }] }), + withFile({ ...stamp, records: [{ ...record, input: -1 }] }), + withFile({ ...stamp, records: [{ ...record, output: 0.5 }] }), + withFile({ ...stamp, records: [{ ...record, cost: 1e7 }] }), + withFile({ ...stamp, records: [{ ...record, sessionCwd: null }] }), + withFile({ ...stamp, records: [{ ...record, entryId: 7 }] }), + ]) await assert.rejects(answer(() => ops.readUsageRecords([{ session: "/s/a.jsonl", paths: ["/s/a.jsonl"] }]), malformed), badResult); + assert.deepEqual((await answer(() => ops.readUsageRecords([{ session: "/s/a.jsonl", paths: ["/s/a.jsonl"] }]), page)).value, page); + } finally { + await link.down.cancel(); + } +}); + test("no uplink traffic for offlineMs marks the host offline and notifies", async () => { hub.linkTimings.offlineMs = 40; const changed = new Promise((resolve) => { diff --git a/lib/hosts/hub.ts b/lib/hosts/hub.ts index cfacf465c..d4f8c1588 100644 --- a/lib/hosts/hub.ts +++ b/lib/hosts/hub.ts @@ -12,6 +12,8 @@ import { appendTransportSamples, transportMetricsPath } from "../transport-metri import { isRecord } from "../type-guards"; import type { HostCapacity } from "./capacity"; import type { ArchivedSessionInfo, SessionInfo } from "../types"; +import { hasPlausibleNumbers } from "../usage-service"; +import type { CostQualityTier, UsageRecord } from "../usage-types"; import { isLinkUp, type Host, type HostStatus, type SpawnRequest, type SpawnResult } from "./host"; import { CREATION_ID_RE, @@ -1136,11 +1138,41 @@ function validResult(op: keyof SessionOps, result: unknown, args: unknown[]): bo // An unknown answer must not read as "fenced and stopped": the route drops its claim on `running: false`. case "restoreFence": return isRecord(value) && typeof value.instance === "string" && value.instance !== "" && typeof value.running === "boolean"; + // Usage rows are stored as sent: every field must have its UsageRecord type. + case "listUsageTranscripts": + return isRecord(value) && Number.isSafeInteger(value.parserVersion) && Array.isArray(value.sessions) + && value.sessions.every((entry) => isRecord(entry) && usagePath(entry.session) + && Array.isArray(entry.transcripts) && entry.transcripts.every(usageStamp)); + case "readUsageRecords": + return isRecord(value) && Array.isArray(value.files) + && value.files.every((file) => usageStamp(file) && Array.isArray(file.records) && file.records.every(usageRecord) + && (file.more === undefined || typeof file.more === "boolean")) + && Array.isArray(value.missing) && value.missing.every(usagePath); default: return true; } } +const usagePath = (value: unknown) => typeof value === "string" && value !== ""; +const finiteNumber = (value: unknown) => typeof value === "number" && Number.isFinite(value); +const COST_QUALITY_TIERS: readonly unknown[] = ["provider_reported", "model_priced", "unpriced"] satisfies CostQualityTier[]; + +function usageStamp(value: unknown): boolean { + return isRecord(value) && usagePath(value.path) && finiteNumber(value.mtimeMs) + && typeof value.size === "number" && Number.isSafeInteger(value.size) && value.size > 0; +} + +function usageRecord(value: unknown): boolean { + if (!isRecord(value)) return false; + const { sessionId, sessionCwd, provider, model, costQuality, entryId, subagentFile } = value; + return [sessionId, sessionCwd, provider, model].every((field) => typeof field === "string") + && (["timestamp", "input", "output", "reasoning", "cacheRead", "cacheWrite", "totalTokens", "cost", "cacheSavings", "sessionStarted"] as const) + .every((field) => finiteNumber(value[field])) + // The limits the host's own parser keeps: a report sums these over every stored row. + && hasPlausibleNumbers(value as unknown as UsageRecord) + && COST_QUALITY_TIERS.includes(costQuality) && optionalString(entryId) && optionalString(subagentFile); +} + /** Paths belong to the daemon's OS, which need not be the web server's OS. */ function preparedPath(value: unknown): boolean { if (typeof value !== "string" || value.includes("\0")) return false; diff --git a/lib/hosts/link-protocol.ts b/lib/hosts/link-protocol.ts index 49ec8133b..200926a0b 100644 --- a/lib/hosts/link-protocol.ts +++ b/lib/hosts/link-protocol.ts @@ -109,6 +109,8 @@ export const SESSION_OP_PARAMS: { readonly [K in keyof SessionOps]: readonly str hideProject: ["path"], validateCwd: ["path"], preparePoolProject: ["input"], + listUsageTranscripts: ["refresh"], + readUsageRecords: ["requests"], }; /** Unknown additions are answered with 501; pool preparation needs a v8 peer. */ diff --git a/lib/hosts/session-ops-usage.test.mjs b/lib/hosts/session-ops-usage.test.mjs new file mode 100644 index 000000000..9b555d4cb --- /dev/null +++ b/lib/hosts/session-ops-usage.test.mjs @@ -0,0 +1,325 @@ +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import test from "node:test"; +import { mock } from "bun:test"; + +// Installed before the ops load: chmod cannot make a path unreadable when the tests run as root. +const realFs = { ...await import("fs") }; +const NO_FAULT = { readdir: null, stat: null, open: null, read: null, code: "EACCES" }; +const fault = { ...NO_FAULT }; +const cutShort = new Set(); +mock.module("fs", () => ({ + ...realFs, + readdirSync(directory, ...args) { + if (directory === fault.readdir) throw Object.assign(new Error("directory unreadable"), { code: fault.code }); + return realFs.readdirSync(directory, ...args); + }, + statSync(file, ...args) { + if (file === fault.stat) throw Object.assign(new Error("file unreadable"), { code: fault.code }); + return realFs.statSync(file, ...args); + }, + openSync(file, ...args) { + if (file === fault.open) throw Object.assign(new Error("file cannot be opened"), { code: fault.code }); + const fd = realFs.openSync(file, ...args); + if (file === fault.read) cutShort.add(fd); + return fd; + }, + // `fault.read`: every byte arrives, then the read that would have found the end of the file fails. + readSync(fd, ...args) { + const bytes = realFs.readSync(fd, ...args); + if (bytes === 0 && cutShort.delete(fd)) throw Object.assign(new Error("read failed"), { code: fault.code }); + return bytes; + }, +})); + +const unreadable = (result, what) => assert.deepEqual([result.ok, result.status, result.code], [false, 500, "usage_unreadable"], what); + +const { sessionOps, USAGE_PAGE_LIMITS } = await import("./session-ops.ts"); +const { getSessionsDir } = await import("../omp/paths.ts"); +const { USAGE_PARSER_VERSION } = await import("../usage-service.ts"); + +const STARTED = Date.parse("2026-01-01T00:00:00.000Z"); + +function assistant(id, input, offset, usage = { cost: { total: 0.25 } }) { + return { + type: "message", + id, + timestamp: new Date(STARTED + offset).toISOString(), + message: { role: "assistant", provider: "anthropic", model: "claude-sonnet-4-5", usage: { input, output: 10, ...usage } }, + }; +} + +function writeTranscript(path, lines) { + writeFileSync(path, `${lines.map((line) => JSON.stringify(line)).join("\n")}\n`); +} + +/** + * Agent dir with two sessions: `alpha` has a subagent transcript, an empty + * artifact and an advisor transcript; `beta` has one request. + */ +async function withUsageAgentDir(run) { + const agentDir = mkdtempSync(join(tmpdir(), "omp-web-session-ops-usage-")); + const previous = process.env.PI_CODING_AGENT_DIR; + process.env.PI_CODING_AGENT_DIR = agentDir; + const savedLimits = { ...USAGE_PAGE_LIMITS }; + try { + const projectDir = join(getSessionsDir(), "-work-alpha"); + mkdirSync(projectDir, { recursive: true }); + const alpha = join(projectDir, "2026-01-01_alpha.jsonl"); + writeTranscript(alpha, [ + { type: "session", id: "alpha", cwd: "/work/alpha", timestamp: new Date(STARTED).toISOString() }, + assistant("a1", 100, 1000), + ]); + const artifacts = join(projectDir, "2026-01-01_alpha"); + mkdirSync(join(artifacts, "Sub"), { recursive: true }); + const subagent = join(artifacts, "Sub.jsonl"); + writeTranscript(subagent, [ + { type: "session", id: "sub-own-id", cwd: "/elsewhere", timestamp: new Date(STARTED + 500).toISOString() }, + assistant("s1", 200, 2000), + ]); + const advisor = join(artifacts, "Sub", "__advisor.jsonl"); + writeTranscript(advisor, [assistant("v1", 300, 3000)]); + writeFileSync(join(artifacts, "Empty.jsonl"), ""); + const beta = join(projectDir, "2026-01-02_beta.jsonl"); + writeTranscript(beta, [ + { type: "session", id: "beta", cwd: "/work/alpha", timestamp: new Date(STARTED).toISOString() }, + assistant("b1", 400, 4000), + ]); + await run({ agentDir, alpha, subagent, advisor, beta }); + } finally { + Object.assign(fault, NO_FAULT); + Object.assign(USAGE_PAGE_LIMITS, savedLimits); + if (previous === undefined) delete process.env.PI_CODING_AGENT_DIR; + else process.env.PI_CODING_AGENT_DIR = previous; + rmSync(agentDir, { recursive: true, force: true }); + } +} + +test("the usage index lists each session with its artifact transcripts and skips empty files", () => withUsageAgentDir(async ({ alpha, subagent, advisor, beta }) => { + const result = await sessionOps.listUsageTranscripts(true); + assert.equal(result.ok, true); + assert.equal(result.value.parserVersion, USAGE_PARSER_VERSION); + const listed = Object.fromEntries(result.value.sessions.map(({ session, transcripts }) => [session, transcripts.map((t) => t.path).sort()])); + assert.deepEqual(listed, { [alpha]: [alpha, subagent, advisor].sort(), [beta]: [beta] }); + for (const { transcripts } of result.value.sessions) { + for (const stamp of transcripts) { + assert.ok(stamp.size > 0 && Number.isFinite(stamp.mtimeMs), `${stamp.path} is stamped`); + } + } +})); + +test("a host without a sessions directory has an empty usage index", () => withUsageAgentDir(async () => { + rmSync(getSessionsDir(), { recursive: true }); + assert.deepEqual(await sessionOps.listUsageTranscripts(false), { ok: true, value: { parserVersion: USAGE_PARSER_VERSION, sessions: [] } }); +})); + +test("artifact transcripts are read as usage of the session that owns them", () => withUsageAgentDir(async ({ alpha, subagent, advisor }) => { + const result = await sessionOps.readUsageRecords([{ session: alpha, paths: [alpha, subagent, advisor] }]); + assert.equal(result.ok, true); + assert.deepEqual(result.value.missing, []); + const byPath = new Map(result.value.files.map((file) => [file.path, file])); + assert.deepEqual([...byPath.keys()], [alpha, subagent, advisor]); + for (const [path, input] of [[alpha, 100], [subagent, 200], [advisor, 300]]) { + const [record] = byPath.get(path).records; + assert.equal(record.input, input); + assert.equal(record.sessionId, "alpha"); + assert.equal(record.sessionCwd, "/work/alpha"); + assert.equal(record.sessionStarted, STARTED); + assert.equal(record.cost, 0.25); + } +})); + +test("paths a session does not list come back missing and are never read", () => withUsageAgentDir(async ({ agentDir, alpha, beta }) => { + const outside = join(agentDir, "outside.jsonl"); + writeTranscript(outside, [assistant("x1", 1, 0)]); + const result = await sessionOps.readUsageRecords([ + { session: alpha, paths: [beta, "/etc/hosts", outside, `${alpha}/../2026-01-02_beta.jsonl`] }, + { session: outside, paths: [outside] }, + { session: "/etc/hosts", paths: ["/etc/hosts"] }, + ]); + assert.equal(result.ok, true); + assert.deepEqual(result.value.files, []); + assert.deepEqual(result.value.missing, [beta, "/etc/hosts", outside, `${alpha}/../2026-01-02_beta.jsonl`, outside, "/etc/hosts"]); +})); + +test("a transcript deleted since the index comes back missing", () => withUsageAgentDir(async ({ alpha, subagent }) => { + rmSync(subagent); + const result = await sessionOps.readUsageRecords([{ session: alpha, paths: [alpha, subagent] }]); + assert.deepEqual(result.value.files.map((file) => file.path), [alpha]); + assert.deepEqual(result.value.missing, [subagent]); +})); + +for (const code of ["EACCES", "EIO"]) { + test(`a directory or transcript that fails with ${code} fails the usage ops instead of reading as gone or empty`, () => withUsageAgentDir(async ({ alpha, subagent, advisor }) => { + const requests = [{ session: alpha, paths: [alpha, subagent, advisor] }]; + for (const directory of [getSessionsDir(), dirname(alpha), dirname(subagent), dirname(advisor)]) { + Object.assign(fault, NO_FAULT, { readdir: directory, code }); + unreadable(await sessionOps.listUsageTranscripts(false), `index with ${directory} unreadable`); + unreadable(await sessionOps.readUsageRecords(requests), `read with ${directory} unreadable`); + } + Object.assign(fault, NO_FAULT, { stat: subagent, code }); + unreadable(await sessionOps.listUsageTranscripts(false), "index with a transcript that cannot be examined"); + unreadable(await sessionOps.readUsageRecords(requests), "read of a transcript that cannot be examined"); + Object.assign(fault, NO_FAULT, { open: advisor, code }); + unreadable(await sessionOps.readUsageRecords(requests), "read of a transcript that cannot be opened"); + Object.assign(fault, NO_FAULT, { read: advisor, code }); + unreadable(await sessionOps.readUsageRecords(requests), "read of a transcript that fails before its end"); + Object.assign(fault, NO_FAULT, { open: alpha, code }); + unreadable(await sessionOps.readUsageRecords([{ session: alpha, paths: [advisor] }]), "read of an artifact whose session cannot be opened"); + + Object.assign(fault, NO_FAULT); + const { files } = (await sessionOps.readUsageRecords(requests)).value; + assert.deepEqual(files.map((file) => [file.path, file.records.length]), [[alpha, 1], [subagent, 1], [advisor, 1]], "no failed read was kept"); + })); +} + +test("a listed directory that goes away, or a path that is no directory, fails the usage ops", () => withUsageAgentDir(async ({ alpha, subagent, advisor }) => { + for (const directory of [getSessionsDir(), dirname(alpha), dirname(subagent), dirname(advisor)]) { + Object.assign(fault, NO_FAULT, { readdir: directory, code: "ENOENT" }); + unreadable(await sessionOps.listUsageTranscripts(false), `index with ${directory} gone while it is still there`); + } + for (const directory of [getSessionsDir(), dirname(subagent)]) { + Object.assign(fault, NO_FAULT, { readdir: directory, code: "ENOTDIR" }); + unreadable(await sessionOps.listUsageTranscripts(false), `index with ${directory} or a parent of it no directory`); + } +})); + +test("a symlinked artifacts directory, or a symlinked directory inside it, is not followed", { skip: process.platform === "win32" }, () => withUsageAgentDir(async ({ agentDir, alpha, subagent, advisor, beta }) => { + const outsideDir = join(agentDir, "outside"); + mkdirSync(outsideDir, { recursive: true }); + writeTranscript(join(outsideDir, "Sub.jsonl"), [assistant("leak", 999, 1000)]); + + const artifacts = dirname(subagent); + rmSync(artifacts, { recursive: true }); + symlinkSync(outsideDir, artifacts); + const index = await sessionOps.listUsageTranscripts(false); + assert.equal(index.ok, true); + assert.deepEqual( + Object.fromEntries(index.value.sessions.map(({ session, transcripts }) => [session, transcripts.map((t) => t.path)])), + { [alpha]: [alpha], [beta]: [beta] }, + ); + const direct = await sessionOps.readUsageRecords([{ session: alpha, paths: [alpha, subagent] }]); + assert.equal(direct.ok, true); + assert.deepEqual(direct.value.files.map((file) => file.path), [alpha]); + assert.deepEqual(direct.value.missing, [subagent]); + + // Read paths are listed again on every read, so a link swapped in after the index was taken is not followed either. + rmSync(artifacts, { recursive: true, force: true }); + mkdirSync(artifacts, { recursive: true }); + symlinkSync(outsideDir, join(artifacts, "Sub")); + const escaped = await sessionOps.readUsageRecords([{ session: alpha, paths: [advisor] }]); + assert.equal(escaped.ok, true); + assert.deepEqual(escaped.value.files, []); + assert.deepEqual(escaped.value.missing, [advisor]); +})); + +test("a sessions root that links to nothing fails the usage ops", { skip: process.platform === "win32" }, () => withUsageAgentDir(async ({ agentDir }) => { + const root = getSessionsDir(); + renameSync(root, join(agentDir, "moved")); + symlinkSync(join(agentDir, "unmounted"), root); + unreadable(await sessionOps.listUsageTranscripts(false), "index with a sessions root linking to nothing"); +})); + +test("a transcript deleted between its stamp and its read comes back missing", () => withUsageAgentDir(async ({ alpha, subagent, advisor }) => { + Object.assign(fault, NO_FAULT, { open: subagent, code: "ENOENT" }); + const result = await sessionOps.readUsageRecords([{ session: alpha, paths: [alpha, subagent, advisor] }]); + assert.deepEqual(result.value.files.map((file) => file.path), [alpha, advisor]); + assert.deepEqual(result.value.missing, [subagent]); +})); + +const weigh = (record) => Buffer.byteLength(JSON.stringify(record)); +/** The serialized size of the one record `path` holds. */ +const recordSize = async (session, path) => weigh((await sessionOps.readUsageRecords([{ session, paths: [path] }])).value.files[0].records[0]); + +test("an answer at its byte limit leaves the rest out; asking for the rest again reads all of it", () => withUsageAgentDir(async ({ alpha, subagent, advisor, beta }) => { + // Room for any one of the four records, never for two. + USAGE_PAGE_LIMITS.bytes = Math.max(await recordSize(alpha, alpha), await recordSize(alpha, subagent), await recordSize(alpha, advisor), await recordSize(beta, beta)); + let pending = [{ session: alpha, paths: [alpha, subagent, advisor] }, { session: beta, paths: [beta] }]; + const read = []; + while (pending.length > 0) { + const page = await sessionOps.readUsageRecords(pending); + assert.equal(page.ok, true); + assert.deepEqual(page.value.files.map((file) => [file.records.length, file.more ?? false]), [[1, false]], "each answer still makes progress"); + assert.deepEqual(page.value.missing, []); + read.push(page.value.files[0].path); + pending = pending + .map(({ session, paths }) => ({ session, paths: paths.filter((path) => !read.includes(path)) })) + .filter((request) => request.paths.length > 0); + } + assert.deepEqual(read, [alpha, subagent, advisor, beta]); +})); + +test("a transcript too large for one answer goes out in slices, each the last file of its answer", () => withUsageAgentDir(async ({ alpha, beta }) => { + writeTranscript(beta, [ + { type: "session", id: "beta", cwd: "/work/alpha", timestamp: new Date(STARTED).toISOString() }, + ...[1, 2, 3, 4, 5].map((input) => assistant(`b${input}`, input, 1000 * input)), + ]); + USAGE_PAGE_LIMITS.bytes = 2 * await recordSize(beta, beta); + const answer = async (skip) => { + const page = (await sessionOps.readUsageRecords([{ session: beta, paths: [beta], skip }, { session: alpha, paths: [alpha] }])).value; + return page.files.map((file) => [file.path, file.records.map((record) => record.input), file.more ?? false]); + }; + assert.deepEqual(await answer(undefined), [[beta, [1, 2], true]]); + assert.deepEqual(await answer(2), [[beta, [3, 4], true]]); + assert.deepEqual(await answer(4), [[beta, [5], false]], "the last slice leaves no room for the next transcript"); + assert.deepEqual(await answer(5), [[beta, [], false], [alpha, [100], false]]); + assert.deepEqual(await answer(99), [[beta, [], false], [alpha, [100], false]]); +})); + +test("answers are bounded by what their records weigh serialized; a record no answer could carry is left out", () => withUsageAgentDir(async ({ beta }) => { + const long = (id, input, model) => ({ ...assistant(id, input, 1000 * input), message: { role: "assistant", provider: "anthropic", model, usage: { input, output: 10, cost: { total: 0.25 } } } }); + const model = "m".repeat(5000); + writeTranscript(beta, [ + { type: "session", id: "beta", cwd: "/work/alpha", timestamp: new Date(STARTED).toISOString() }, + long("b1", 1, model), long("b2", 2, model), long("b3", 3, "x".repeat(40_000)), long("b4", 4, model), + ]); + // Two of the long records, and nowhere near the 40 000 characters of the third. + const limit = 2 * await recordSize(beta, beta); + USAGE_PAGE_LIMITS.bytes = limit; + const answers = []; + for (let skip = 0, more = true; more; skip += answers.at(-1).records.length) { + const [file] = (await sessionOps.readUsageRecords([{ session: beta, paths: [beta], skip }])).value.files; + answers.push(file); + more = file.more === true; + } + assert.deepEqual(answers.map((file) => file.records.map((record) => record.input)), [[1, 2], [4]]); + for (const file of answers) assert.ok(weigh(file.records) <= limit + 16, "no answer outgrows the limit"); +})); + +test("a refreshed index drops the prices earlier reads were answered with", () => withUsageAgentDir(async ({ agentDir, beta }) => { + // No provider-reported cost: the price comes from models.yml. + writeTranscript(beta, [ + { type: "session", id: "beta", cwd: "/work/alpha", timestamp: new Date(STARTED).toISOString() }, + assistant("b1", 1_000_000, 4000, { output: 0 }), + ]); + const price = (input) => writeFileSync(join(agentDir, "models.yml"), JSON.stringify({ + providers: { anthropic: { models: [{ id: "claude-sonnet-4-5", cost: { input, output: 0, cacheRead: 0, cacheWrite: 0 } }] } }, + })); + const cost = async () => (await sessionOps.readUsageRecords([{ session: beta, paths: [beta] }])).value.files[0].records[0].cost; + + price(2); + await sessionOps.listUsageTranscripts(true); + assert.equal(await cost(), 2); + price(5); + await sessionOps.listUsageTranscripts(false); + assert.equal(await cost(), 2, "an unchanged transcript keeps its cached records"); + await sessionOps.listUsageTranscripts(true); + assert.equal(await cost(), 5); +})); + +test("malformed usage arguments are 400 invalid_request", () => withUsageAgentDir(async ({ alpha }) => { + for (const refresh of [undefined, "true", 1]) { + const result = await sessionOps.listUsageTranscripts(refresh); + assert.equal(result.ok, false); + assert.equal(result.status, 400); + assert.equal(result.code, "invalid_request"); + } + for (const requests of [undefined, {}, [alpha], [{ session: alpha }], [{ session: 1, paths: [] }], [{ session: alpha, paths: [1] }], [{ session: alpha, paths: [alpha], skip: -1 }], [{ session: alpha, paths: [alpha], skip: 1.5 }]]) { + const result = await sessionOps.readUsageRecords(requests); + assert.equal(result.ok, false); + assert.equal(result.status, 400); + assert.equal(result.code, "invalid_request"); + } +})); diff --git a/lib/hosts/session-ops.ts b/lib/hosts/session-ops.ts index faf72495a..34ff049c6 100644 --- a/lib/hosts/session-ops.ts +++ b/lib/hosts/session-ops.ts @@ -1,6 +1,7 @@ // Session/project operations shared by API routes and the authenticated host daemon. -// Session reads take IDs, never paths; pooled preparation takes registered paths -// derived by server admission, never browser-supplied filesystem authority. +// Session reads take IDs, never paths; usage reads take only paths this host lists itself +// (`inventorySessionFiles` plus each session's artifact transcripts); pooled preparation takes +// registered paths derived by server admission, never browser-supplied filesystem authority. // Live-wrapper work (RPC sends, stopping a child) stays in the routes on main. // No `next/*` or `@/` imports: the daemon loads this through plain jiti. import { randomUUID } from "crypto"; @@ -9,6 +10,7 @@ import { closeSync, mkdirSync, openSync, readFileSync, readdirSync, readSync, re import * as fsRuntime from "fs"; import { homedir, tmpdir } from "os"; import { basename, dirname, isAbsolute, join, resolve } from "path"; +import { setImmediate as nextTurn } from "timers/promises"; import { promisify } from "util"; import { comparableProjectPath } from "../comparable-path"; import { MAX_SESSION_SEARCH_QUERY_LENGTH, searchSessions as searchSessionContent } from "../session-content-search"; @@ -21,6 +23,9 @@ import { listSessionIdsForRestore, deleteSessionFileWithArtifacts, getLeafEntryId, + inventorySessionFiles, + isGone, + listSessionArtifactTranscripts, loadSessionFile, MAX_SESSION_LOAD_BYTES, parseTitleSlotLine, @@ -28,6 +33,8 @@ import { setSessionTitle, writeSessionFileAtomicSync, } from "../omp/session-files"; +import { readModelsConfig } from "../omp/models-config"; +import { getSessionsDir } from "../omp/paths"; import { preparePoolProject, type PreparedPoolProject } from "../pooled-projects"; import { projectIdentityKey, sessionPathKey } from "../paths"; import { @@ -61,6 +68,8 @@ import { extractSubagentHistory, readCompletionArtifact, readSubagentTranscriptP import { SUBAGENT_ID_RE } from "../subagent-types"; import { isRecord } from "../type-guards"; import type { ArchivedSessionInfo, ManagedProject, ProjectLaunchConfig, SessionContext, SessionInfo, SessionTitleSource } from "../types"; +import { invalidateUsageCache, parseSessionUsage, USAGE_PARSER_VERSION } from "../usage-service"; +import type { UsageReadRequest, UsageRecord, UsageRecordsPage, UsageSessionStamps, UsageTranscriptIndex, UsageTranscriptRecords, UsageTranscriptStamp } from "../usage-types"; import { resolveProject } from "../worktree"; export type OpResult = { ok: true; value: T } | { ok: false; status: number; code: string; error: string }; @@ -134,6 +143,11 @@ export interface SessionOps { /** Input origin and cloneRoot must come from the server's registered pool project. */ preparePoolProject(input: Parameters[0]): Promise>; validateCwd(path: string): Promise>; + /** Every transcript usage is counted from (sessions and their artifact transcripts), stamped. `refresh` drops the + * parse cache, so the reads that follow price with the current `models.yml`. */ + listUsageTranscripts(refresh: boolean): Promise>; + /** Usage records of listed transcripts, as many as fit one answer. */ + readUsageRecords(requests: UsageReadRequest[]): Promise>; } const execFileAsync = promisify(execFile); @@ -297,6 +311,51 @@ function normalizeCwd(cwd: string): string { return isAbsolute(cwd) ? cwd : resolve(cwd); } +/** One `readUsageRecords` answer holds at most `bytes` of serialized records and stops growing after `ms`. A transcript + * with more goes out in slices, so no answer comes near the link's 64 MiB result limit. */ +export const USAGE_PAGE_LIMITS = { bytes: 8 * 1024 * 1024, ms: 10_000 }; + +/** Null for a path that is gone, empty or not a file. Any other stat failure throws: unreadable is not gone. */ +function usageStamp(path: string): UsageTranscriptStamp | null { + let stats: Stats; + try { + stats = statSync(path); + } catch (error) { + if (isGone(error)) return null; + throw error; + } + return stats.isFile() && stats.size > 0 ? { path, mtimeMs: stats.mtimeMs, size: stats.size } : null; +} + +/** A transcript's usage, whole, and what each record weighs serialized. Null when it was deleted since it was stamped; + * any other read failure throws. A record no answer has room for is no usage a transcript can hold: it is left out, like + * any malformed line. Prices are read here, with no await before the parse caches them under the transcript's stamp. */ +function usageRecords(path: string, session: string): { records: UsageRecord[]; sizes: number[] } | null { + let records: UsageRecord[]; + try { + records = parseSessionUsage(path, readModelsConfig(), session, true); + } catch (error) { + if (isGone(error)) return null; + throw error; + } + const sizes = records.map((record) => Buffer.byteLength(JSON.stringify(record))); + if (sizes.every((size) => size <= USAGE_PAGE_LIMITS.bytes)) return { records, sizes }; + const fits = (_: unknown, index: number) => sizes[index] <= USAGE_PAGE_LIMITS.bytes; + return { records: records.filter(fits), sizes: sizes.filter(fits) }; +} + +/** Main stores what the usage ops answer as the whole truth: it drops the rows of every transcript they leave out or + * report missing. So a directory or transcript that cannot be read fails the op. */ +function usageUnreadable(error: unknown): OpResult { + return fail(500, "usage_unreadable", `Usage transcripts could not be read: ${error instanceof Error ? error.message : String(error)}`); +} + +function isUsageReadRequest(value: unknown): value is UsageReadRequest { + return isRecord(value) && typeof value.session === "string" + && Array.isArray(value.paths) && value.paths.every((path) => typeof path === "string") + && (value.skip === undefined || (typeof value.skip === "number" && Number.isSafeInteger(value.skip) && value.skip >= 0)); +} + /** * Render a session to self-contained HTML by shelling out to the user's omp * binary: `omp --export ` (the output path is the first @@ -806,4 +865,64 @@ export const sessionOps: SessionOps = { } return preparePoolProject(input); }, + + async listUsageTranscripts(refresh) { + if (typeof refresh !== "boolean") return fail(400, "invalid_request", "refresh must be a boolean"); + if (refresh) invalidateUsageCache(); + try { + const sessions: UsageSessionStamps[] = []; + for (const [index, session] of inventorySessionFiles(getSessionsDir()).entries()) { + if (index > 0 && index % 50 === 0) await nextTurn(); + const transcripts = [session, ...listSessionArtifactTranscripts(session, true)].flatMap((path) => usageStamp(path) ?? []); + if (transcripts.length > 0) sessions.push({ session, transcripts }); + } + return ok({ parserVersion: USAGE_PARSER_VERSION, sessions }); + } catch (error) { + return usageUnreadable(error); + } + }, + + async readUsageRecords(requests) { + if (!Array.isArray(requests) || !requests.every(isUsageReadRequest)) { + return fail(400, "invalid_request", "Usage reads must be a list of { session, paths, skip? }"); + } + const started = Date.now(); + const files: UsageTranscriptRecords[] = []; + const missing: string[] = []; + let bytes = 0; + try { + const sessions = new Set(inventorySessionFiles(getSessionsDir())); + for (const { session, paths, skip = 0 } of requests) { + const transcripts = sessions.has(session) ? new Set([session, ...listSessionArtifactTranscripts(session, true)]) : null; + for (const [at, path] of paths.entries()) { + if (files.length > 0 && Date.now() - started > USAGE_PAGE_LIMITS.ms) return ok({ files, missing }); + // Stamped before parsing: a write that lands during the parse changes the stamp and is read again next time. + const stamp = transcripts?.has(path) ? usageStamp(path) : null; + const usage = stamp && usageRecords(path, session); + if (!stamp || !usage) { + missing.push(path); + continue; + } + const { records, sizes } = usage; + const from = at === 0 ? Math.min(skip, records.length) : 0; + let end = from; + let room = USAGE_PAGE_LIMITS.bytes - bytes; + while (end < records.length && sizes[end] <= room) room -= sizes[end++]; + // Sliced only when it must be: a transcript that does not fit behind the others waits for the next answer. + if (end < records.length && files.length > 0) return ok({ files, missing }); + bytes = USAGE_PAGE_LIMITS.bytes - room; + const sent = from === 0 && end === records.length ? records : records.slice(from, end); + if (end < records.length) { + files.push({ ...stamp, records: sent, more: true }); + return ok({ files, missing }); + } + files.push({ ...stamp, records: sent }); + await nextTurn(); + } + } + } catch (error) { + return usageUnreadable(error); + } + return ok({ files, missing }); + }, }; diff --git a/lib/hosts/usage-sync.test.mjs b/lib/hosts/usage-sync.test.mjs new file mode 100644 index 000000000..43667e96b --- /dev/null +++ b/lib/hosts/usage-sync.test.mjs @@ -0,0 +1,512 @@ +// Main's side of usage from linked hosts: pulls through `listUsageTranscripts`/`readUsageRecords` into usage.db. +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test, { after } from "node:test"; + +const agentDir = mkdtempSync(join(tmpdir(), "omp-web-usage-sync-")); +const previousAgentDir = process.env.PI_CODING_AGENT_DIR; +process.env.PI_CODING_AGENT_DIR = agentDir; + +const { closeUsageDatabase, getUsageDatabase, getUsageReportFromDb } = await import("../usage-db.ts"); +const { pullAllHostUsage, usageSyncTimings } = await import("./usage-sync.ts"); +const { closeHostLink, getHost } = await import("./hub.ts"); +const { HostOpError } = await import("./link-protocol.ts"); +const { createHost, revokeHost } = await import("./registry.ts"); +const { sessionOps } = await import("./session-ops.ts"); +const { linkHost, upgradeSocket } = await import("./link-driver.test-helper.mjs"); + +after(() => { + closeUsageDatabase(); + if (previousAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR; + else process.env.PI_CODING_AGENT_DIR = previousAgentDir; + rmSync(agentDir, { recursive: true, force: true }); +}); + +const PARSER_VERSION = 7; +let dbCount = 0; +/** An empty usage.db per test: hosts of earlier tests stay registered but hold no rows here. */ +const freshDb = () => getUsageDatabase(join(agentDir, `usage-${++dbCount}.db`)); + +/** A transcript on a linked host: one request of `input` tokens. */ +function transcript(session, cwd, input, { mtimeMs = 1, entryId = `${session}-${input}`, timestamp = Date.now() - 1_000 } = {}) { + return { + session, + mtimeMs, + size: 100, + records: [{ + timestamp, sessionId: session, sessionCwd: cwd, provider: "anthropic", model: "claude-opus-5-5", + input, output: 0, reasoning: 0, cacheRead: 0, cacheWrite: 0, totalTokens: input, cost: input / 1000, cacheSavings: 0, + costQuality: "model_priced", entryId, sessionStarted: 1, + }], + }; +} + +/** Serve `link` as a daemon whose transcripts are `files` (path to transcript; edit it to change the host). Each + * `readUsageRecords` answers at most `pageSize` files and leaves the rest out; `readFails(n)` says whether the nth read + * fails. Returns the read requests, in order, and in `refreshes` the `refresh` of each index request. */ +function serveUsage(link, files, { pageSize = Infinity, readFails = () => false, ops = {} } = {}) { + const reads = []; + reads.refreshes = []; + void link.serve({ + listUsageTranscripts: ({ refresh }) => { + reads.refreshes.push(refresh); + const sessions = new Map(); + for (const [path, { session, mtimeMs, size }] of files) { + if (!sessions.has(session)) sessions.set(session, []); + sessions.get(session).push({ path, mtimeMs, size }); + } + return { ok: true, value: { parserVersion: PARSER_VERSION, sessions: [...sessions].map(([session, transcripts]) => ({ session, transcripts })) } }; + }, + readUsageRecords: ({ requests }) => { + reads.push(requests); + if (readFails(reads.length)) return { ok: false, status: 500, code: "usage_unreadable", error: "disk on fire" }; + const page = { files: [], missing: [] }; + for (const path of requests.flatMap((request) => request.paths)) { + const file = files.get(path); + if (!file) page.missing.push(path); + else if (page.files.length < pageSize) page.files.push({ path, mtimeMs: file.mtimeMs, size: file.size, records: file.records }); + } + return { ok: true, value: page }; + }, + ...ops, + }); + return reads; +} + +const hostTokens = (db, hostId) => db.prepare("SELECT COALESCE(SUM(input_tokens), 0) AS t FROM usage_records WHERE host_id = ?").get(hostId).t; +const statusOf = (hosts, hostId) => hosts.find((host) => host.hostId === hostId); +const pathsOf = (requests) => requests.flatMap((request) => request.paths); + +function captureWarnings(t) { + const warnings = []; + const warn = console.warn; + console.warn = (...args) => warnings.push(args.join(" ")); + t.after(() => { console.warn = warn; }); + return warnings; +} + +/** Pull every host as soon as it is asked, not once per TTL. */ +function withoutTtl(t) { + const ttlMs = usageSyncTimings.ttlMs; + usageSyncTimings.ttlMs = 0; + t.after(() => { usageSyncTimings.ttlMs = ttlMs; }); +} + +test("a linked host's usage is counted under its id, with its projects named", async (t) => { + const db = freshDb(); + const link = await linkHost("Laptop"); + t.after(() => link.drop()); + serveUsage(link, new Map([ + ["/home/u/.omp/agent/sessions/a.jsonl", transcript("a", "/home/u/alpha", 100)], + ["/home/u/.omp/agent/sessions/a/sub.jsonl", transcript("a", "/home/u/alpha", 20)], + ["C:\\Users\\u\\.omp\\agent\\sessions\\b.jsonl", transcript("b", "C:\\Users\\u\\beta", 50)], + ])); + + const hosts = await pullAllHostUsage(db, false); + const laptop = statusOf(hosts, link.id); + assert.equal(laptop.label, "Laptop"); + assert.equal(laptop.state, "current"); + assert.equal(typeof laptop.syncedAt, "number"); + + const report = await getUsageReportFromDb({ range: "30d" }, db, hosts); + assert.equal(report.summary.inputTokens, 170); + const row = report.hostBreakdown.find((host) => host.hostId === link.id); + assert.deepEqual([row.tokens, row.sessionsCount, row.share], [170, 2, 100]); + assert.deepEqual([report.hostBreakdown[0].hostId, report.hostBreakdown[0].tokens], ["local", 0], "this machine comes first and has no transcripts"); + assert.deepEqual( + report.projectBreakdown.map(({ hostId, hostLabel, projectName, tokens }) => ({ hostId, hostLabel, projectName, tokens })), + [ + { hostId: link.id, hostLabel: "Laptop", projectName: "alpha", tokens: 120 }, + { hostId: link.id, hostLabel: "Laptop", projectName: "beta", tokens: 50 }, + ], + ); +}); + +test("only new or changed transcripts are read again and ones the host dropped are removed; a refresh reads them all", async (t) => { + const db = freshDb(); + const link = await linkHost("Changes"); + t.after(() => link.drop()); + const files = new Map([ + ["/s/a.jsonl", transcript("a", "/p", 1)], + ["/s/b.jsonl", transcript("b", "/p", 10)], + ["/s/c.jsonl", transcript("c", "/p", 100)], + ]); + const reads = serveUsage(link, files); + await pullAllHostUsage(db, false); + assert.equal(hostTokens(db, link.id), 111); + + files.set("/s/a.jsonl", transcript("a", "/p", 2, { mtimeMs: 2 })); + files.delete("/s/c.jsonl"); + files.set("/s/d.jsonl", transcript("d", "/p", 1000)); + await pullAllHostUsage(db, false); + assert.equal(reads.length, 1, "within the TTL the host is not asked again"); + + withoutTtl(t); + await pullAllHostUsage(db, false); + assert.deepEqual(pathsOf(reads[1]).sort(), ["/s/a.jsonl", "/s/d.jsonl"]); + assert.equal(hostTokens(db, link.id), 1012); + + // The host may price differently now: nothing about a transcript's stamp says so. + await pullAllHostUsage(db, true); + assert.deepEqual(pathsOf(reads[2]).sort(), ["/s/a.jsonl", "/s/b.jsonl", "/s/d.jsonl"]); + assert.equal(hostTokens(db, link.id), 1012); +}); + +test("transcripts that did not fit an answer are asked for again until all are read", async (t) => { + const db = freshDb(); + const link = await linkHost("Paged"); + t.after(() => link.drop()); + const reads = serveUsage(link, new Map([ + ["/s/a.jsonl", transcript("a", "/p", 1)], + ["/s/a/sub.jsonl", transcript("a", "/p", 10)], + ["/s/b.jsonl", transcript("b", "/p", 100)], + ]), { pageSize: 1 }); + + const hosts = await pullAllHostUsage(db, false); + assert.deepEqual(reads.map(pathsOf), [ + ["/s/a.jsonl", "/s/a/sub.jsonl", "/s/b.jsonl"], + ["/s/a/sub.jsonl", "/s/b.jsonl"], + ["/s/b.jsonl"], + ]); + assert.equal(hostTokens(db, link.id), 111); + assert.equal(statusOf(hosts, link.id).state, "current"); +}); + +test("one read asks for at most 50 sessions", async (t) => { + const db = freshDb(); + const link = await linkHost("Many"); + t.after(() => link.drop()); + const files = new Map(Array.from({ length: 120 }, (_, i) => [`/s/${i}.jsonl`, transcript(`s${i}`, "/p", 1)])); + const reads = serveUsage(link, files); + + await pullAllHostUsage(db, false); + assert.deepEqual(reads.map((requests) => requests.length), [50, 50, 20]); + assert.equal(hostTokens(db, link.id), 120); +}); + +test("a host that reads none of what it was asked for fails the pull instead of being asked for ever", async (t) => { + const db = freshDb(); + const link = await linkHost("Mute"); + t.after(() => link.drop()); + const reads = serveUsage(link, new Map([["/s/m.jsonl", transcript("m", "/p", 1)]]), { pageSize: 0 }); + const warnings = captureWarnings(t); + + const hosts = await pullAllHostUsage(db, false); + assert.deepEqual([statusOf(hosts, link.id).state, statusOf(hosts, link.id).syncedAt], ["stale", null]); + assert.equal(reads.length, 1); + assert.deepEqual(warnings, ["[omp-web] could not sync usage from host Mute: Mute read none of the usage transcripts it was asked for"]); +}); + +test("an answer about a transcript that was not asked for changes nothing", async (t) => { + const db = freshDb(); + const link = await linkHost("Chatty"); + t.after(() => link.drop()); + const files = new Map([["/s/a.jsonl", transcript("a", "/p", 1)], ["/s/b.jsonl", transcript("b", "/p", 10)]]); + const unlisted = { path: "/s/unlisted.jsonl", mtimeMs: 1, size: 100, records: transcript("x", "/p", 1000).records }; + serveUsage(link, files, { + ops: { + readUsageRecords: ({ requests }) => { + const asked = pathsOf(requests); + const read = asked.map((path) => ({ path, mtimeMs: files.get(path).mtimeMs, size: 100, records: files.get(path).records })); + return { ok: true, value: { files: [...read, unlisted], missing: ["/s/b.jsonl"].filter((path) => !asked.includes(path)) } }; + }, + }, + }); + await pullAllHostUsage(db, false); + assert.equal(hostTokens(db, link.id), 11); + + files.set("/s/a.jsonl", transcript("a", "/p", 2, { mtimeMs: 2 })); + withoutTtl(t); + await pullAllHostUsage(db, false); + assert.equal(hostTokens(db, link.id), 12, "the unchanged transcript the host called missing is kept"); +}); + +test("a pull that fails keeps the pages it stored and drops nothing; the next one finishes the job", async (t) => { + const db = freshDb(); + const link = await linkHost("Halfway"); + t.after(() => link.drop()); + const files = new Map([ + ["/s/a.jsonl", transcript("a", "/p", 1)], + ["/s/b.jsonl", transcript("b", "/p", 10)], + ["/s/c.jsonl", transcript("c", "/p", 100)], + ]); + let failFrom = Infinity; + const reads = serveUsage(link, files, { pageSize: 1, readFails: (n) => n >= failFrom }); + await pullAllHostUsage(db, false); + assert.equal(hostTokens(db, link.id), 111); + + files.set("/s/a.jsonl", transcript("a", "/p", 2, { mtimeMs: 2 })); + files.set("/s/b.jsonl", transcript("b", "/p", 20, { mtimeMs: 2 })); + files.delete("/s/c.jsonl"); + failFrom = reads.length + 2; + withoutTtl(t); + captureWarnings(t); + const failed = statusOf(await pullAllHostUsage(db, false), link.id); + assert.equal(failed.state, "stale"); + assert.equal(hostTokens(db, link.id), 112, "the first page is stored, the transcript the host deleted is still counted"); + + failFrom = Infinity; + const done = statusOf(await pullAllHostUsage(db, false), link.id); + assert.equal(done.state, "current"); + assert.ok(done.syncedAt >= failed.syncedAt); + assert.equal(hostTokens(db, link.id), 22); + assert.deepEqual(pathsOf(reads.at(-1)), ["/s/b.jsonl"], "only what the failed pull had not stored is read"); +}); + +test("a daemon without the usage ops is unsupported; an offline or failing host keeps its last numbers", async (t) => { + const db = freshDb(); + const old = await linkHost("Old daemon"); + t.after(() => old.drop()); + serveUsage(old, new Map(), { ops: { listUsageTranscripts: () => { throw new HostOpError(501, "unknown_op", "Unknown op"); } } }); + const { host: outdated, token } = createHost("Outdated link"); + upgradeSocket(token).socket.frame(`${JSON.stringify({ type: "hello", v: 3 })}\n`); + assert.equal(getHost(outdated.id).status(), "needs-upgrade"); + + const flaky = await linkHost("Flaky"); + t.after(() => flaky.drop()); + let failing = false; + const flakyFiles = new Map([["/s/f.jsonl", transcript("f", "/p", 5)]]); + serveUsage(flaky, flakyFiles, { + // What a daemon answers when it cannot examine one of its transcripts. + ops: { listUsageTranscripts: () => failing ? { ok: false, status: 500, code: "usage_unreadable", error: "disk on fire" } : { ok: true, value: { parserVersion: PARSER_VERSION, sessions: [{ session: "f", transcripts: [{ path: "/s/f.jsonl", mtimeMs: 1, size: 100 }] }] } } }, + }); + const gone = await linkHost("Going away"); + serveUsage(gone, new Map([["/s/g.jsonl", transcript("g", "/p", 40)]])); + + const first = await pullAllHostUsage(db, true); + assert.equal(statusOf(first, old.id).state, "unsupported"); + assert.equal(statusOf(first, outdated.id).state, "unsupported"); + assert.equal(statusOf(first, gone.id).state, "current"); + const flakySyncedAt = statusOf(first, flaky.id).syncedAt; + const goneSyncedAt = statusOf(first, gone.id).syncedAt; + + closeHostLink(gone.id); + assert.equal(getHost(gone.id).status(), "offline"); + failing = true; + const warnings = captureWarnings(t); + const second = await pullAllHostUsage(db, true); + assert.deepEqual([statusOf(second, gone.id).state, statusOf(second, gone.id).syncedAt], ["offline", goneSyncedAt]); + assert.deepEqual([statusOf(second, flaky.id).state, statusOf(second, flaky.id).syncedAt], ["stale", flakySyncedAt]); + assert.deepEqual(warnings, ["[omp-web] could not sync usage from host Flaky: disk on fire"]); + + const report = await getUsageReportFromDb({ range: "30d" }, db, second); + assert.equal(report.hostBreakdown.find((host) => host.hostId === gone.id).tokens, 40); + assert.equal(report.hostBreakdown.find((host) => host.hostId === flaky.id).tokens, 5); +}); + +test("a host that dropped since its last pull is offline whatever the TTL, and its last sync time outlives a restart", async () => { + const db = freshDb(); + const link = await linkHost("Dropped"); + serveUsage(link, new Map([["/s/d.jsonl", transcript("d", "/p", 8)]])); + const synced = statusOf(await pullAllHostUsage(db, false), link.id); + assert.equal(synced.state, "current"); + + closeHostLink(link.id); + const offline = statusOf(await pullAllHostUsage(db, false), link.id); + assert.deepEqual(offline, { ...synced, state: "offline" }); + + // What a restart leaves behind: the database, and no memory of earlier pulls. + globalThis.__ompHostUsagePulls.clear(); + assert.deepEqual(statusOf(await pullAllHostUsage(db, false), link.id), offline); + assert.equal(hostTokens(db, link.id), 8); +}); + +test("a revoked host's rows are pruned; an unreadable hosts.json fails the pull and prunes nothing", async (t) => { + const db = freshDb(); + const link = await linkHost("Revoked"); + t.after(() => link.drop()); + serveUsage(link, new Map([["/s/r.jsonl", transcript("r", "/p", 9)]])); + await pullAllHostUsage(db, true); + assert.equal(hostTokens(db, link.id), 9); + + const hostsFile = join(agentDir, "omp-web", "hosts.json"); + const registry = readFileSync(hostsFile, "utf8"); + writeFileSync(hostsFile, "{ not json"); + try { + await assert.rejects(pullAllHostUsage(db, true), /hosts\.json/); + } finally { + writeFileSync(hostsFile, registry); + } + assert.equal(hostTokens(db, link.id), 9); + + revokeHost(link.id); + const hosts = await pullAllHostUsage(db, true); + assert.equal(hostTokens(db, link.id), 0); + assert.equal(statusOf(hosts, link.id), undefined); + assert.equal(db.prepare("SELECT COUNT(*) AS c FROM synced_hosts WHERE host_id = ?").get(link.id).c, 0); +}); + +test("the same request in transcripts on two hosts counts once", async (t) => { + const db = freshDb(); + const timestamp = Date.now() - 5_000; + for (const label of ["Copy one", "Copy two"]) { + const link = await linkHost(label); + t.after(() => link.drop()); + serveUsage(link, new Map([[`/s/${label}.jsonl`, transcript("moved", "/p", 300, { entryId: "e1", timestamp })]])); + } + const report = await getUsageReportFromDb({ range: "30d" }, db, await pullAllHostUsage(db, true)); + assert.equal(report.summary.inputTokens, 300); +}); + +test("a report waits for slow hosts only so long; their pull finishes in the background", async (t) => { + const db = freshDb(); + const link = await linkHost("Slow"); + t.after(() => link.drop()); + const gate = Promise.withResolvers(); + const index = { ok: true, value: { parserVersion: PARSER_VERSION, sessions: [{ session: "slow", transcripts: [{ path: "/s/slow.jsonl", mtimeMs: 1, size: 100 }] }] } }; + const reads = serveUsage(link, new Map([["/s/slow.jsonl", transcript("slow", "/p", 3)]]), { + ops: { listUsageTranscripts: async () => { await gate.promise; return index; } }, + }); + const waitMs = usageSyncTimings.waitMs; + usageSyncTimings.waitMs = 20; + t.after(() => { usageSyncTimings.waitMs = waitMs; }); + + const hosts = await pullAllHostUsage(db, true); + assert.deepEqual([statusOf(hosts, link.id).state, statusOf(hosts, link.id).syncedAt], ["syncing", null]); + assert.equal(hostTokens(db, link.id), 0); + + usageSyncTimings.waitMs = waitMs; + gate.resolve(); + assert.equal(statusOf(await pullAllHostUsage(db, false), link.id).state, "current"); + assert.equal(hostTokens(db, link.id), 3); + assert.equal(reads.length, 1, "the report that waited did not start a second pull"); +}); + +test("a refresh prices a host's unchanged transcripts with its current models.yml", async (t) => { + const db = freshDb(); + const project = join(agentDir, "sessions", "-priced"); + mkdirSync(project, { recursive: true }); + t.after(() => rmSync(join(agentDir, "sessions"), { recursive: true, force: true })); + writeFileSync(join(project, "2026-01-01_priced.jsonl"), [ + JSON.stringify({ type: "session", id: "priced", cwd: "/work/priced", timestamp: new Date().toISOString() }), + JSON.stringify({ type: "message", id: "e1", message: { role: "assistant", provider: "anthropic", model: "claude-opus-5-5", usage: { input: 1_000_000, output: 0 } } }), + ].join("\n")); + const price = (input) => writeFileSync(join(agentDir, "models.yml"), JSON.stringify({ + providers: { anthropic: { models: [{ id: "claude-opus-5-5", cost: { input, output: 0, cacheRead: 0, cacheWrite: 0 } }] } }, + })); + t.after(() => rmSync(join(agentDir, "models.yml"), { force: true })); + + // A linked host answering with the real ops, as its daemon does. + const link = await linkHost("Priced"); + t.after(() => link.drop()); + void link.serve({ + listUsageTranscripts: ({ refresh }) => sessionOps.listUsageTranscripts(refresh), + readUsageRecords: ({ requests }) => sessionOps.readUsageRecords(requests), + }); + const cost = () => db.prepare("SELECT COALESCE(SUM(cost), 0) AS c FROM usage_records WHERE host_id = ?").get(link.id).c; + withoutTtl(t); + + price(2); + await pullAllHostUsage(db, true); + assert.equal(cost(), 2); + + price(5); + await pullAllHostUsage(db, false); + assert.equal(cost(), 2, "an unchanged transcript is not read again"); + await pullAllHostUsage(db, true); + assert.equal(cost(), 5); +}); + +test("a refresh that fails partway is finished by the next pull, so no transcript keeps its old price", async (t) => { + const db = freshDb(); + const link = await linkHost("Repricing"); + t.after(() => link.drop()); + const files = new Map([ + ["/s/a.jsonl", transcript("a", "/p", 1)], + ["/s/b.jsonl", transcript("b", "/p", 10)], + ["/s/c.jsonl", transcript("c", "/p", 100)], + ]); + let failFrom = Infinity; + const reads = serveUsage(link, files, { pageSize: 1, readFails: (n) => n >= failFrom }); + await pullAllHostUsage(db, false); + withoutTtl(t); + captureWarnings(t); + + failFrom = reads.length + 2; + assert.equal(statusOf(await pullAllHostUsage(db, true), link.id).state, "stale"); + failFrom = Infinity; + assert.equal(statusOf(await pullAllHostUsage(db, false), link.id).state, "current"); + assert.deepEqual(reads.refreshes, [false, true, true], "the host is still asked to drop its cached prices"); + assert.deepEqual(pathsOf(reads.at(-2)), ["/s/b.jsonl", "/s/c.jsonl"], "what the refresh did not reprice is read again"); + assert.equal(hostTokens(db, link.id), 111); + + const settled = reads.length; + await pullAllHostUsage(db, false); + assert.deepEqual([reads.refreshes.at(-1), reads.length], [false, settled], "once every transcript is repriced, pulls are ordinary again"); +}); + +test("a transcript too large for one answer arrives in slices and is only stored whole", async (t) => { + const db = freshDb(); + const project = join(agentDir, "sessions", "-large"); + const session = join(project, "2026-01-01_large.jsonl"); + const artifact = join(project, "2026-01-01_large", "Sub.jsonl"); + mkdirSync(join(project, "2026-01-01_large"), { recursive: true }); + t.after(() => rmSync(join(agentDir, "sessions"), { recursive: true, force: true })); + const request = (id, input) => JSON.stringify({ type: "message", id, message: { role: "assistant", provider: "anthropic", model: "claude-opus-5-5", usage: { input, output: 0 } } }); + writeFileSync(session, [ + JSON.stringify({ type: "session", id: "large", cwd: "/work/large", timestamp: new Date().toISOString() }), + ...[1, 2, 3, 4, 5].map((input) => request(`e${input}`, input)), + ].join("\n")); + writeFileSync(artifact, request("s1", 100)); + // Room for two of the session's records an answer: its five take three answers. + const { USAGE_PAGE_LIMITS } = await import("./session-ops.ts"); + const bytes = USAGE_PAGE_LIMITS.bytes; + const [record] = (await sessionOps.readUsageRecords([{ session, paths: [session] }])).value.files[0].records; + USAGE_PAGE_LIMITS.bytes = 2 * Buffer.byteLength(JSON.stringify(record)); + t.after(() => { USAGE_PAGE_LIMITS.bytes = bytes; }); + + const link = await linkHost("Large"); + t.after(() => link.drop()); + const reads = []; + let failFrom = 3; + void link.serve({ + listUsageTranscripts: ({ refresh }) => sessionOps.listUsageTranscripts(refresh), + readUsageRecords: ({ requests }) => { + reads.push(requests); + return reads.length >= failFrom ? { ok: false, status: 500, code: "usage_unreadable", error: "disk on fire" } : sessionOps.readUsageRecords(requests); + }, + }); + withoutTtl(t); + captureWarnings(t); + + assert.equal(statusOf(await pullAllHostUsage(db, false), link.id).state, "stale"); + assert.equal(hostTokens(db, link.id), 0, "two slices of three were read: nothing of the transcript is stored"); + + failFrom = Infinity; + reads.length = 0; + assert.equal(statusOf(await pullAllHostUsage(db, false), link.id).state, "current"); + assert.deepEqual(reads.map((requests) => [requests[0].skip ?? 0, pathsOf(requests)]), [ + [0, [session, artifact]], + [2, [session, artifact]], + [4, [session, artifact]], + [0, [artifact]], + ]); + assert.deepEqual(db.prepare("SELECT input_tokens AS t FROM usage_records WHERE host_id = ? ORDER BY input_tokens").all(link.id).map((row) => row.t), [1, 2, 3, 4, 5, 100]); +}); + +for (const [label, mtimeMs, failure] of [ + ["Shifting", (read) => read, "Shifting changed a usage transcript while it was being read"], + ["Endless", () => 1, "Endless sent more usage records than a transcript can hold"], +]) { + test(`slices that ${label === "Shifting" ? "are of a transcript that changed in between" : "never end"} fail the pull and store nothing`, async (t) => { + const db = freshDb(); + const link = await linkHost(label); + t.after(() => link.drop()); + let reads = 0; + serveUsage(link, new Map([["/s/a.jsonl", transcript("a", "/p", 1)]]), { + ops: { + readUsageRecords: () => { + const slice = { path: "/s/a.jsonl", mtimeMs: mtimeMs(++reads), size: 3, records: transcript("a", "/p", reads).records, more: true }; + return { ok: true, value: { files: [slice], missing: [] } }; + }, + }, + }); + const warnings = captureWarnings(t); + + assert.equal(statusOf(await pullAllHostUsage(db, false), link.id).state, "stale"); + assert.equal(hostTokens(db, link.id), 0); + assert.deepEqual(warnings, [`[omp-web] could not sync usage from host ${label}: ${failure}`]); + }); +} diff --git a/lib/hosts/usage-sync.ts b/lib/hosts/usage-sync.ts new file mode 100644 index 000000000..412d17f3b --- /dev/null +++ b/lib/hosts/usage-sync.ts @@ -0,0 +1,203 @@ +// Usage of linked hosts: each host's daemon reads its own transcripts, main keeps a copy in usage.db so a host that +// goes offline keeps its last numbers. Pulled only on /api/usage requests. No `next/*` or `@/` imports. +import type { Database } from "bun:sqlite"; +import { hostSyncTimes, invalidateHostUsageStamps, isUpToDate, markHostSynced, pruneHostUsage, storedStamps, storeHostUsage } from "../usage-db"; +import type { HostUsageStatus, HostUsageSyncState, UsageReadRequest, UsageTranscriptRecords } from "../usage-types"; +import { isLinkUp, type Host } from "./host"; +import { getHost, onHostHello } from "./hub"; +import { HostOpError } from "./link-protocol"; +import { LOCAL_HOST_ID, readHosts } from "./registry"; + +/** `ttlMs`: a host pulled this recently is not asked again; `waitMs`: how long a report waits for its pulls. */ +export const usageSyncTimings = { ttlMs: 30_000, waitMs: 10_000 }; + +/** Most sessions and paths asked for in one `readUsageRecords`. */ +const READ_BATCH = { sessions: 50, paths: 2_000 }; + +interface HostPulls { + running: Promise | null; + /** When the last pull succeeded; 0 pulls again whatever the TTL. */ + pulledAt: number; + /** How the last pull ended; `stale` before the first one. */ + outcome: "current" | "stale" | "offline" | "unsupported"; +} + +declare global { + var __ompHostUsagePulls: Map | undefined; +} + +// globalThis, like the hub: survives a `bun --hot` re-evaluation. +function pullsOf(hostId: string): HostPulls { + if (!globalThis.__ompHostUsagePulls) { + globalThis.__ompHostUsagePulls = new Map(); + // A host that links again may have new transcripts from while it was away. + onHostHello((id) => { + const pulls = globalThis.__ompHostUsagePulls?.get(id); + if (pulls) pulls.pulledAt = 0; + }); + } + let pulls = globalThis.__ompHostUsagePulls.get(hostId); + if (!pulls) { + pulls = { running: null, pulledAt: 0, outcome: "stale" }; + globalThis.__ompHostUsagePulls.set(hostId, pulls); + } + return pulls; +} + +/** Take the next `readUsageRecords` batch off the front of `queue`; a session with too many paths is split. */ +function nextBatch(queue: UsageReadRequest[]): UsageReadRequest[] { + const batch: UsageReadRequest[] = []; + let paths = 0; + while (queue.length > 0 && batch.length < READ_BATCH.sessions && paths < READ_BATCH.paths) { + const request = queue[0]; + const room = READ_BATCH.paths - paths; + if (request.paths.length <= room) { + queue.shift(); + batch.push(request); + paths += request.paths.length; + } else { + batch.push({ session: request.session, paths: request.paths.slice(0, room) }); + queue[0] = { session: request.session, paths: request.paths.slice(room) }; + paths = READ_BATCH.paths; + } + } + return batch; +} + +/** Bring `db`'s copy of `host`'s usage up to date: read what is new or changed, drop what the host no longer has. + * A refresh (`refresh`, or one an earlier pull did not finish) reads every transcript again, so the host's current + * prices apply to all of them. */ +async function syncHostUsage(host: Host, db: Database, refresh: boolean): Promise { + const unlisted = storedStamps(db, host.id); + // A refresh clears the stored parser versions first (`invalidateHostUsageStamps`); any left cleared is still pending. + const repricing = refresh || [...unlisted.values()].some((stamp) => stamp.parserVersion === null); + const index = await host.sessionOps.listUsageTranscripts(repricing); + if (!index.ok) throw new HostOpError(index.status, index.code, index.error); + const { parserVersion, sessions } = index.value; + const queue: UsageReadRequest[] = []; + for (const { session, transcripts } of sessions) { + const paths: string[] = []; + for (const stamp of transcripts) { + const upToDate = isUpToDate(unlisted.get(stamp.path), stamp, parserVersion); + unlisted.delete(stamp.path); + if (!upToDate) paths.push(stamp.path); + } + if (paths.length > 0) queue.push({ session, paths }); + } + // What the pull finds gone is dropped last: a pull that fails on the way takes nothing away. + const gone: string[] = []; + // A transcript too large for one answer comes in slices and is held until the last one: it is only stored whole. + let held = null as UsageTranscriptRecords | null; + while (queue.length > 0) { + const batch = nextBatch(queue); + // Continued only while it is what the batch starts with: the skip is for that path and no other. + if (held && batch[0].paths[0] === held.path) batch[0] = { ...batch[0], skip: held.records.length }; + else held = null; + const page = await host.sessionOps.readUsageRecords(batch); + if (!page.ok) throw new HostOpError(page.status, page.code, page.error); + // Only an answer about a path that was asked for counts. + const open = new Set(batch.flatMap((request) => request.paths)); + const files: UsageTranscriptRecords[] = []; + let sliced = 0; + for (const file of page.value.files) { + if (!open.has(file.path)) continue; + const earlier: UsageTranscriptRecords | null = held?.path === file.path ? held : null; + if (earlier && (earlier.mtimeMs !== file.mtimeMs || earlier.size !== file.size)) { + throw new HostOpError(409, "usage_changed", `${host.label} changed a usage transcript while it was being read`); + } + const whole: UsageTranscriptRecords = earlier ? { ...file, records: earlier.records.concat(file.records) } : file; + // No transcript holds more records than bytes: slices beyond that would be held without end. + if (whole.records.length > file.size) { + throw new HostOpError(502, "host_bad_result", `${host.label} sent more usage records than a transcript can hold`); + } + if (file.more) { + held = whole; + sliced += file.records.length; + } else { + if (earlier) held = null; + open.delete(file.path); + files.push(whole); + } + } + const missing = page.value.missing.filter((path) => open.delete(path)); + if (held && !open.has(held.path)) held = null; + // A host that settles none of a batch and sends no further slice would be asked for it for ever. + if (files.length + missing.length + sliced === 0) { + throw new HostOpError(502, "host_bad_result", `${host.label} read none of the usage transcripts it was asked for`); + } + // Stored page by page, each transcript whole: a pull that fails later leaves the rest for the next one. + if (files.length > 0) storeHostUsage(db, host.id, parserVersion, files, []); + gone.push(...missing); + const rest = batch + .map(({ session, paths }) => ({ session, paths: paths.filter((path) => open.has(path)) })) + .filter((request) => request.paths.length > 0); + queue.unshift(...rest); + } + // The index was the host's whole inventory (it fails otherwise), so a stored transcript it left out is gone too. + gone.push(...unlisted.keys()); + if (gone.length > 0) storeHostUsage(db, host.id, parserVersion, [], gone); + markHostSynced(db, host.id, Date.now()); +} + +/** Pull `host`'s usage into `db` unless it was pulled within the TTL (`refresh` pulls anyway). Never rejects: a + * failed pull keeps the stored rows and shows in the host's sync state. */ +async function pullHostUsage(host: Host, db: Database, refresh: boolean): Promise { + const pulls = pullsOf(host.id); + while (pulls.running) await pulls.running; + // Kept in the database, so a refresh that fails or is cut off by a restart still reprices every transcript later. + if (refresh) invalidateHostUsageStamps(db, host.id); + if (!isLinkUp(host.status())) return; + if (!refresh && Date.now() - pulls.pulledAt < usageSyncTimings.ttlMs) return; + pulls.running = syncHostUsage(host, db, refresh).then(() => { + pulls.outcome = "current"; + pulls.pulledAt = Date.now(); + }, (error: unknown) => { + const status = error instanceof HostOpError ? error.status : 0; + // 501: a daemon from before the usage ops. 503: the socket is gone, which the link's status only shows later. + pulls.outcome = status === 501 ? "unsupported" : status === 503 ? "offline" : "stale"; + pulls.pulledAt = 0; + if (pulls.outcome === "stale") { + console.warn(`[omp-web] could not sync usage from host ${host.label}: ${error instanceof Error ? error.message : String(error)}`); + } + }).finally(() => { + pulls.running = null; + }); + await pulls.running; +} + +/** The link's status as it is now comes first: a pull that succeeded says nothing about a host that has since dropped. */ +function syncState(host: Host, pulls: HostPulls): HostUsageSyncState { + const status = host.status(); + if (status === "needs-upgrade") return "unsupported"; + if (!isLinkUp(status)) return "offline"; + return pulls.running ? "syncing" : pulls.outcome; +} + +/** Pull every linked host's usage, waiting at most `usageSyncTimings.waitMs` (slower pulls finish in the background), + * and drop what is stored for hosts no longer registered. Returns the linked hosts by label. */ +export async function pullAllHostUsage(db: Database, refresh: boolean): Promise { + // An unreadable registry throws before pruning or building a report with missing hosts. + const registered = readHosts() + .filter((host) => host.id !== LOCAL_HOST_ID) + .sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()) || a.id.localeCompare(b.id)); + const registeredIds = registered.map((host) => host.id); + pruneHostUsage(db, registeredIds); + for (const hostId of globalThis.__ompHostUsagePulls?.keys() ?? []) { + if (!registeredIds.includes(hostId)) globalThis.__ompHostUsagePulls!.delete(hostId); + } + + const hosts = registered.flatMap((row) => getHost(row.id) ?? []); + const waited = Promise.withResolvers(); + const timer = setTimeout(waited.resolve, usageSyncTimings.waitMs); + timer.unref(); + await Promise.race([Promise.all(hosts.map((host) => pullHostUsage(host, db, refresh))), waited.promise]); + clearTimeout(timer); + + const syncedAt = hostSyncTimes(db); + return hosts.map((host) => ({ + hostId: host.id, + label: host.label, + state: syncState(host, pullsOf(host.id)), + syncedAt: syncedAt.get(host.id) ?? null, + })); +} diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 76a40d343..747d7a0d6 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -1985,6 +1985,12 @@ "usageConfig.model": "Model", "usageConfig.day": "Day", "usageConfig.project": "Project", + "usageConfig.host": "Host", + "usageConfig.hostSyncing": "Syncing…", + "usageConfig.hostOffline": "Offline · last synced {time}", + "usageConfig.hostSyncFailed": "Sync failed · last synced {time}", + "usageConfig.hostNeverSynced": "Not synced yet", + "usageConfig.hostUnsupported": "Update ompweb on this host to include its usage", "usageConfig.share": "Share", "usageConfig.costQuality": "Cost quality", "usageConfig.providerReported": "Provider reported", diff --git a/lib/i18n/locales/ja.json b/lib/i18n/locales/ja.json index e4467fc41..d88c4d2d2 100644 --- a/lib/i18n/locales/ja.json +++ b/lib/i18n/locales/ja.json @@ -1945,6 +1945,12 @@ "usageConfig.model": "モデル", "usageConfig.day": "日付", "usageConfig.project": "プロジェクト", + "usageConfig.host": "ホスト", + "usageConfig.hostSyncing": "同期中…", + "usageConfig.hostOffline": "オフライン · 最終同期 {time}", + "usageConfig.hostSyncFailed": "同期に失敗 · 最終同期 {time}", + "usageConfig.hostNeverSynced": "未同期", + "usageConfig.hostUnsupported": "使用量を含めるには、このホストの ompweb を更新してください", "usageConfig.share": "割合", "usageConfig.costQuality": "コスト品質", "usageConfig.providerReported": "プロバイダー報告", diff --git a/lib/i18n/locales/zh-CN.json b/lib/i18n/locales/zh-CN.json index b98b9dddc..d9d7f265c 100644 --- a/lib/i18n/locales/zh-CN.json +++ b/lib/i18n/locales/zh-CN.json @@ -1945,6 +1945,12 @@ "usageConfig.model": "模型", "usageConfig.day": "日期", "usageConfig.project": "项目", + "usageConfig.host": "主机", + "usageConfig.hostSyncing": "同步中…", + "usageConfig.hostOffline": "离线 · 上次同步 {time}", + "usageConfig.hostSyncFailed": "同步失败 · 上次同步 {time}", + "usageConfig.hostNeverSynced": "尚未同步", + "usageConfig.hostUnsupported": "请更新此主机上的 ompweb 以计入其用量", "usageConfig.share": "占比", "usageConfig.costQuality": "费用质量", "usageConfig.providerReported": "提供商报告", diff --git a/lib/i18n/locales/zh-TW.json b/lib/i18n/locales/zh-TW.json index ca85ab4e1..91d836f4f 100644 --- a/lib/i18n/locales/zh-TW.json +++ b/lib/i18n/locales/zh-TW.json @@ -1985,6 +1985,12 @@ "usageConfig.model": "模型", "usageConfig.day": "日期", "usageConfig.project": "專案", + "usageConfig.host": "主機", + "usageConfig.hostSyncing": "同步中…", + "usageConfig.hostOffline": "離線 · 上次同步 {time}", + "usageConfig.hostSyncFailed": "同步失敗 · 上次同步 {time}", + "usageConfig.hostNeverSynced": "尚未同步", + "usageConfig.hostUnsupported": "請更新此主機上的 ompweb 以計入其用量", "usageConfig.share": "佔比", "usageConfig.costQuality": "費用計算品質", "usageConfig.providerReported": "供應商回報", diff --git a/lib/omp/session-files.ts b/lib/omp/session-files.ts index 2524beccb..f52db854c 100644 --- a/lib/omp/session-files.ts +++ b/lib/omp/session-files.ts @@ -1179,7 +1179,7 @@ export async function listSessionFiles(sessionsRoot: string): Promise * caches: skipped directories or unreadable/unidentifiable headers cannot prove a session absent on this machine. */ export function listSessionIdsForRestore(sessionsRoot = getSessionsDir()): string[] { const ids: string[] = []; - for (const file of collectSessionFiles(sessionsRoot, true)) { + for (const file of collectSessionFiles(sessionsRoot, "restore")) { const header = readSessionHeaderSync(file, true); if (!header || typeof header.id !== "string" || !header.id) throw new Error(`Cannot identify active session at ${file}`); ids.push(header.id); @@ -1187,7 +1187,32 @@ export function listSessionIdsForRestore(sessionsRoot = getSessionsDir()): strin return ids; } -function collectSessionFiles(sessionsRoot: string, strict = false): string[] { +/** Every session file, uncached. Unlike `listSessionFiles` it throws rather than leave a directory out, so a session + * the inventory omits is gone. Only a sessions root that does not exist is an empty inventory. */ +export function inventorySessionFiles(sessionsRoot: string): string[] { + return collectSessionFiles(sessionsRoot, "complete"); +} + +/** The path does not exist. Any other failure, a parent that is no directory included, says nothing about it. */ +export function isGone(error: unknown): boolean { + return (error as NodeJS.ErrnoException | null)?.code === "ENOENT"; +} + +/** Nothing is at `target`: not even a link to something that is gone, which reads as gone itself. */ +function isAbsent(target: string): boolean { + try { + lstatSync(target); + return false; + } catch (error) { + if (isGone(error)) return true; + throw error; + } +} + +/** `complete` throws for a root or project directory it cannot read. `restore` is the restore preflight, which also + * throws for symlinks and for a root that disappears once seen. */ +function collectSessionFiles(sessionsRoot: string, mode: "lenient" | "complete" | "restore" = "lenient"): string[] { + const strict = mode === "restore"; const files: string[] = []; let projects: Dirent[]; let observedRoot = false; @@ -1201,6 +1226,7 @@ function collectSessionFiles(sessionsRoot: string, strict = false): string[] { } catch (error) { // Only a root absent from the outset is an empty inventory. Once observed, disappearance is uncertainty. if (strict && (observedRoot || !(error && typeof error === "object" && "code" in error && error.code === "ENOENT"))) throw error; + if (mode === "complete" && !(isGone(error) && isAbsent(sessionsRoot))) throw error; return []; } for (const dirent of projects) { @@ -1213,7 +1239,7 @@ function collectSessionFiles(sessionsRoot: string, strict = false): string[] { inner = readDirectorySyncRuntime(dirPath, { withFileTypes: true }); } catch (error) { // This project was enumerated: even ENOENT may mean it was renamed with active sessions inside. Never skip it in preflight. - if (strict) throw error; + if (mode !== "lenient") throw error; continue; } for (const file of inner) { @@ -1435,18 +1461,27 @@ export function deleteSessionFileWithArtifacts(filePath: string): void { * The top-level `local/` folder is skipped: it is the agent's `local://` * scratch space, which omp never writes transcripts to and which can hold * arbitrary trees (cloned repos, dependencies). Returns [] without an - * artifacts directory. + * artifacts directory. With `complete`, any other directory that cannot be + * read throws instead of being left out, and an artifacts directory that is a + * symlink has no transcripts: what it points at lies outside the sessions tree. */ -export function listSessionArtifactTranscripts(sessionFile: string): string[] { +export function listSessionArtifactTranscripts(sessionFile: string, complete = false): string[] { if (!sessionFile.endsWith(".jsonl")) return []; const root = sessionFile.slice(0, -".jsonl".length); + try { + if (!lstatSync(root).isDirectory()) return []; + } catch (error) { + if (complete && !isGone(error)) throw error; + return []; + } const transcripts: string[] = []; const pending = [root]; for (let dir = pending.pop(); dir !== undefined; dir = pending.pop()) { let entries: Dirent[]; try { entries = readDirectorySyncRuntime(dir, { withFileTypes: true }); - } catch { + } catch (error) { + if (complete && !(dir === root && isGone(error) && isAbsent(dir))) throw error; continue; } for (const entry of entries) { diff --git a/lib/usage-db.test.mjs b/lib/usage-db.test.mjs index 5853de548..1ba599fc5 100644 --- a/lib/usage-db.test.mjs +++ b/lib/usage-db.test.mjs @@ -343,3 +343,152 @@ test("files last synced by another omp-web build are re-parsed; files synced by assert.equal(syncSessionFilesToDb([file], { providers: {} }, db).filesUpdated, 0); }); }); + +/** The v3 layout: no `host_id`, `synced_files` keyed by path alone, one leftover row. */ +const V3_LAYOUT = ` + CREATE TABLE synced_files (file_path TEXT PRIMARY KEY, mtime_ms REAL NOT NULL, file_size INTEGER NOT NULL, + records_count INTEGER NOT NULL, synced_at INTEGER NOT NULL, parser_version INTEGER); + CREATE TABLE usage_records (id INTEGER PRIMARY KEY AUTOINCREMENT, file_path TEXT NOT NULL, session_id TEXT NOT NULL, + session_cwd TEXT NOT NULL, timestamp INTEGER NOT NULL, provider TEXT NOT NULL, model TEXT NOT NULL, + input_tokens INTEGER NOT NULL, output_tokens INTEGER NOT NULL, reasoning_tokens INTEGER NOT NULL, + cache_read_tokens INTEGER NOT NULL, cache_write_tokens INTEGER NOT NULL, total_tokens INTEGER NOT NULL, + cost REAL NOT NULL, cache_savings REAL NOT NULL, cost_quality TEXT NOT NULL, entry_id TEXT, subagent_file TEXT, + session_started INTEGER NOT NULL DEFAULT 0); + CREATE INDEX idx_usage_records_file_path ON usage_records(file_path); + INSERT INTO usage_records (file_path, session_id, session_cwd, timestamp, provider, model, input_tokens, output_tokens, + reasoning_tokens, cache_read_tokens, cache_write_tokens, total_tokens, cost, cache_savings, cost_quality) + VALUES ('/gone.jsonl', 's', '/p', 1, 'anthropic', 'm', 999999, 0, 0, 0, 0, 999999, 0, 0, 'model_priced'); + PRAGMA user_version = 3; +`; + +test("a v3 usage.db is rebuilt with host ids and host sync times", async () => { + const { closeUsageDatabase, getUsageDatabase, syncSessionFilesToDb } = await loadSubject(); + + await withTempDbDir(async (dir) => { + closeUsageDatabase(); + const path = join(dir, "v3.db"); + const old = new Database(path); + old.exec(V3_LAYOUT); + old.close(); + + const db = getUsageDatabase(path); + assert.equal(db.prepare("PRAGMA user_version").get().user_version, 5); + assert.equal(inputTokens(db), 0, "the v3 rows are discarded"); + assert.deepEqual(db.prepare("SELECT * FROM synced_hosts").all(), []); + const indexes = db.prepare("SELECT name FROM sqlite_master WHERE type = 'index'").all().map((row) => row.name); + assert.ok(indexes.includes("idx_usage_records_host")); + assert.ok(indexes.includes("idx_usage_records_file_path")); + + syncSessionFilesToDb([writeSession(dir, "a.jsonl", 100)], { providers: {} }, db); + assert.deepEqual(db.prepare("SELECT DISTINCT host_id FROM usage_records").all(), [{ host_id: "local" }]); + }); +}); + +/** One linked-host transcript at `path` with a single request of `input` tokens in project `cwd`. */ +function hostTranscript(path, input, { cwd = "/home/u/remote-proj", session = "remote-session", entryId = `${path}#e1` } = {}) { + return { + path, + mtimeMs: 1, + size: 10, + records: [{ + timestamp: Date.now(), sessionId: session, sessionCwd: cwd, provider: "anthropic", model: "claude-opus-5-5", + input, output: 0, reasoning: 0, cacheRead: 0, cacheWrite: 0, totalTokens: input, cost: input / 1000, cacheSavings: 0, + costQuality: "model_priced", entryId, sessionStarted: 1, + }], + }; +} + +const hostTokens = (db, hostId) => db.prepare("SELECT COALESCE(SUM(input_tokens), 0) AS t FROM usage_records WHERE host_id = ?").get(hostId).t; + +test("a provider named __proto__ from a linked host is charted like any other", async () => { + const { getUsageReportFromDb, storeHostUsage } = await loadSubject(); + await withTempDbDir(async (_dir, db) => { + const transcript = hostTranscript("/s/p.jsonl", 7); + transcript.records[0].provider = "__proto__"; + storeHostUsage(db, "laptop", 1, [transcript], []); + const report = await getUsageReportFromDb({ range: "30d" }, db, [{ hostId: "laptop", label: "Laptop", state: "current", syncedAt: 1 }]); + const charted = report.timeSeries.filter((point) => Object.hasOwn(point.byProvider, "__proto__")); + assert.deepEqual(charted.map((point) => point.byProvider.__proto__.tokens), [7]); + }); +}); + +test("syncing and force-refreshing this machine's usage leave linked hosts' rows alone", async () => { + const { getUsageReportFromDb, storeHostUsage, syncSessionFilesToDb } = await loadSubject(); + + await withTempDbDir(async (dir, db) => { + const local = writeSession(dir, "a.jsonl", 100); + // The same path on another machine is a different transcript. + storeHostUsage(db, "laptop", 1, [hostTranscript(local, 7), hostTranscript("/home/u/.omp/s/b.jsonl", 30)], []); + + syncSessionFilesToDb([local], { providers: {} }, db); + assert.equal(hostTokens(db, "laptop"), 37); + syncSessionFilesToDb([], { providers: {} }, db); + assert.equal(hostTokens(db, "local"), 0, "the local transcript is purged"); + assert.equal(hostTokens(db, "laptop"), 37, "the purge kept the host's rows, the one sharing the path too"); + + const linked = [ + { hostId: "laptop", label: "Laptop", state: "current", syncedAt: 5 }, + { hostId: "idle", label: "Idle", state: "offline", syncedAt: null }, + ]; + const before = Date.now(); + const report = await getUsageReportFromDb({ range: "30d", forceRefresh: true }, db, linked); + assert.equal(hostTokens(db, "laptop"), 37, "forceRefresh rebuilt only this machine's rows"); + assert.equal(report.summary.inputTokens, 137); + assert.deepEqual(report.hostBreakdown.map(({ hostId, label, state, tokens, sessionsCount }) => ({ hostId, label, state, tokens, sessionsCount })), [ + { hostId: "local", label: "", state: "current", tokens: 100, sessionsCount: 1 }, + { hostId: "laptop", label: "Laptop", state: "current", tokens: 37, sessionsCount: 1 }, + { hostId: "idle", label: "Idle", state: "offline", tokens: 0, sessionsCount: 0 }, + ]); + const [ownSyncedAt, ...linkedSyncedAt] = report.hostBreakdown.map((host) => host.syncedAt); + assert.ok(ownSyncedAt >= before, "this machine was synced for this report"); + assert.deepEqual(linkedSyncedAt, [5, null]); + const laptopProject = report.projectBreakdown.find((row) => row.hostId === "laptop"); + assert.deepEqual([laptopProject.project, laptopProject.projectName, laptopProject.hostLabel], ["/home/u/remote-proj", "remote-proj", "Laptop"]); + assert.equal(report.projectBreakdown.find((row) => row.hostId === "local").hostLabel, ""); + }); +}); + +test("a force refresh prices this machine's unchanged transcripts with the current models.yml", async () => { + const { getUsageReportFromDb } = await loadSubject(); + + await withTempDbDir(async (dir, db) => { + writeSession(dir, "a.jsonl", 1_000_000); + const price = (input) => writeFileSync(join(dir, "models.yml"), JSON.stringify({ + providers: { anthropic: { models: [{ id: "claude-opus-5-5", cost: { input, output: 0, cacheRead: 0, cacheWrite: 0 } }] } }, + })); + const cost = async (forceRefresh) => (await getUsageReportFromDb({ range: "30d", forceRefresh }, db)).summary.totalCost; + + price(2); + assert.equal(await cost(true), 2); + price(5); + assert.equal(await cost(false), 2, "an unchanged transcript is not read again"); + assert.equal(await cost(true), 5); + }); +}); + +test("linked hosts' rows are replaced per transcript, dropped when gone, and pruned with their host", async () => { + const { getUsageReportFromDb, hostSyncTimes, markHostSynced, pruneHostUsage, storedStamps, storeHostUsage } = await loadSubject(); + + await withTempDbDir(async (_dir, db) => { + assert.throws(() => storeHostUsage(db, "local", 1, [hostTranscript("/x.jsonl", 1)], [])); + + storeHostUsage(db, "win", 1, [hostTranscript("C:\\s\\a.jsonl", 10, { cwd: "C:\\Users\\u\\proj" }), hostTranscript("C:\\s\\b.jsonl", 20)], []); + storeHostUsage(db, "win", 2, [{ ...hostTranscript("C:\\s\\a.jsonl", 15, { cwd: "C:\\Users\\u\\proj" }), mtimeMs: 9, size: 11 }], ["C:\\s\\b.jsonl"]); + markHostSynced(db, "win", 111); + assert.equal(hostTokens(db, "win"), 15); + assert.deepEqual([...storedStamps(db, "win")], [["C:\\s\\a.jsonl", { mtimeMs: 9, size: 11, parserVersion: 2 }]]); + + const report = await getUsageReportFromDb({ range: "30d" }, db); + const row = report.projectBreakdown.find((project) => project.hostId === "win"); + assert.deepEqual([row.projectName, row.hostLabel], ["proj", "win"], "Windows paths name their folder; an unnamed host goes by its id"); + + storeHostUsage(db, "kept", 1, [hostTranscript("/k.jsonl", 5)], []); + markHostSynced(db, "kept", 222); + markHostSynced(db, "kept", 333); + pruneHostUsage(db, ["kept"]); + assert.equal(hostTokens(db, "win"), 0); + assert.equal(storedStamps(db, "win").size, 0); + assert.equal(hostTokens(db, "kept"), 5); + assert.deepEqual([...hostSyncTimes(db)], [["kept", 333]]); + }); +}); diff --git a/lib/usage-db.ts b/lib/usage-db.ts index 1e7b045ec..cb9474c3e 100644 --- a/lib/usage-db.ts +++ b/lib/usage-db.ts @@ -1,12 +1,14 @@ import { existsSync, mkdirSync, statSync } from "fs"; import { Database } from "bun:sqlite"; -import { basename, dirname, join } from "path"; +import { dirname, join, posix, win32 } from "path"; +import { customLocalLabel, LOCAL_HOST_ID } from "./hosts/registry"; import { readModelsConfig, type ModelsFileConfig } from "./omp/models-config"; import { getAgentDir, getSessionsDir } from "./omp/paths"; -import { listSessionArtifactTranscripts, listSessionFiles } from "./omp/session-files"; +import { invalidateSessionFileListCache, listSessionArtifactTranscripts, listSessionFiles } from "./omp/session-files"; import { formatChartDateLabel, formatFullDateLabel, + invalidateUsageCache, parseSessionUsage, toLocalDateString, toLocalMonthString, @@ -16,14 +18,16 @@ import { import { getProviderColor, getProviderDisplayName } from "./usage-rates"; import type { DayUsageSummary, + HostUsageStatus, + HostUsageSummary, ModelUsageSummary, ProjectUsageSummary, ProviderUsageSummary, TimeSeriesPoint, UsageQueryOptions, - UsageRecord, UsageReport, UsageSummary, + UsageTranscriptRecords, } from "./usage-types"; declare global { @@ -36,9 +40,11 @@ declare global { * older layout is dropped and rebuilt from the transcripts on disk: it is only * a cache. v3 adds `entry_id`, `subagent_file`, `session_started` and `parser_version` (v2 was * written by a pre-release build of this change with the old layout, so it - * must be rebuilt too). + * must be rebuilt too). v5 adds `host_id` (rows from linked hosts, `lib/hosts/usage-sync.ts`), + * keys `synced_files` by (host_id, file_path) and adds `synced_hosts` (v4 was a + * pre-release layout of the same change). */ -const USAGE_DB_SCHEMA_VERSION = 3; +const USAGE_DB_SCHEMA_VERSION = 5; /** Get the path to the usage SQLite database file (~/.omp/agent/usage.db). */ export function getUsageDbPath(): string { @@ -68,18 +74,27 @@ function migrateUsageDatabase(db: Database): void { db.exec(` DROP TABLE IF EXISTS usage_records; DROP TABLE IF EXISTS synced_files; + DROP TABLE IF EXISTS synced_hosts; CREATE TABLE synced_files ( - file_path TEXT PRIMARY KEY, + host_id TEXT NOT NULL DEFAULT 'local', + file_path TEXT NOT NULL, mtime_ms REAL NOT NULL, file_size INTEGER NOT NULL, records_count INTEGER NOT NULL, synced_at INTEGER NOT NULL, - parser_version INTEGER + parser_version INTEGER, + PRIMARY KEY (host_id, file_path) + ); + + CREATE TABLE synced_hosts ( + host_id TEXT PRIMARY KEY, + synced_at INTEGER NOT NULL ); CREATE TABLE usage_records ( id INTEGER PRIMARY KEY AUTOINCREMENT, + host_id TEXT NOT NULL DEFAULT 'local', file_path TEXT NOT NULL, session_id TEXT NOT NULL, session_cwd TEXT NOT NULL, @@ -101,7 +116,8 @@ function migrateUsageDatabase(db: Database): void { ); CREATE INDEX idx_usage_records_timestamp ON usage_records(timestamp); - CREATE INDEX idx_usage_records_file_path ON usage_records(file_path); + CREATE INDEX idx_usage_records_file_path ON usage_records(file_path, host_id); + CREATE INDEX idx_usage_records_host ON usage_records(host_id); CREATE INDEX idx_usage_records_provider ON usage_records(provider); CREATE INDEX idx_usage_records_session_cwd ON usage_records(session_cwd); CREATE INDEX idx_usage_records_entry ON usage_records(entry_id, timestamp); @@ -183,58 +199,104 @@ export interface SyncStats { recordsInserted: number; } +export interface StoredStamp { + mtimeMs: number; + size: number; + parserVersion: number | null; +} + +/** Transcripts stored for `hostId`, by path, as they were stamped when last read. */ +export function storedStamps(db: Database, hostId: string): Map { + const rows = db + .prepare("SELECT file_path, mtime_ms, file_size, parser_version FROM synced_files WHERE host_id = ?") + .all(hostId) as Array<{ file_path: string; mtime_ms: number; file_size: number; parser_version: number | null }>; + return new Map(rows.map((row) => [row.file_path, { mtimeMs: row.mtime_ms, size: row.file_size, parserVersion: row.parser_version }])); +} + +/** Whether `stored` is a copy of the transcript as `stamp` describes it, read by parser rules `parserVersion`. */ +export function isUpToDate(stored: StoredStamp | undefined, stamp: { mtimeMs: number; size: number }, parserVersion: number): boolean { + return stored !== undefined && stored.mtimeMs === stamp.mtimeMs && stored.size === stamp.size && stored.parserVersion === parserVersion; +} + +/** Writes to one host's transcripts. A transcript's rows and its stamp change together. */ +function transcriptWrites(db: Database, hostId: string) { + const insertRecord = db.prepare(` + INSERT INTO usage_records ( + host_id, file_path, session_id, session_cwd, timestamp, provider, model, + input_tokens, output_tokens, reasoning_tokens, cache_read_tokens, + cache_write_tokens, total_tokens, cost, cache_savings, cost_quality, + entry_id, subagent_file, session_started + ) VALUES ( + ?, ?, ?, ?, ?, ?, ?, + ?, ?, ?, ?, + ?, ?, ?, ?, ?, + ?, ?, ? + ) + `); + const deleteRecords = db.prepare("DELETE FROM usage_records WHERE host_id = ? AND file_path = ?"); + const deleteStamp = db.prepare("DELETE FROM synced_files WHERE host_id = ? AND file_path = ?"); + const upsertStamp = db.prepare(` + INSERT OR REPLACE INTO synced_files (host_id, file_path, mtime_ms, file_size, records_count, synced_at, parser_version) + VALUES (?, ?, ?, ?, ?, ?, ?) + `); + return { + replace({ path, mtimeMs, size, records }: UsageTranscriptRecords, parserVersion: number, syncedAt: number): void { + deleteRecords.run(hostId, path); + for (const r of records) { + insertRecord.run( + hostId, + path, + r.sessionId, + r.sessionCwd, + r.timestamp, + r.provider, + r.model, + r.input, + r.output, + r.reasoning, + r.cacheRead, + r.cacheWrite, + r.totalTokens, + r.cost, + r.cacheSavings, + r.costQuality, + r.entryId ?? null, + r.subagentFile ?? null, + r.sessionStarted, + ); + } + upsertStamp.run(hostId, path, mtimeMs, size, records.length, syncedAt, parserVersion); + }, + remove(path: string): void { + deleteRecords.run(hostId, path); + deleteStamp.run(hostId, path); + }, + }; +} + /** - * Incrementally sync session .jsonl files, plus every transcript in each + * Incrementally sync this machine's session .jsonl files, plus every transcript in each * session's artifacts directory (subagents, advisors, `/tan` clones, extension * sub-sessions — each bills its own model usage), into the usage database. * Artifact transcripts are attributed to the session they belong to. Only - * files that are new, changed, or last synced by other parsing rules are parsed. + * files that are new, changed, or last synced by other parsing rules are parsed; + * `refresh` parses all of them again, replacing their rows in the same transaction. + * Rows of linked hosts are left alone. */ export function syncSessionFilesToDb( sessionFiles: string[], customModelsConfig: ModelsFileConfig = readModelsConfig(), customDb?: Database, + refresh = false, ): SyncStats { const db = customDb || getUsageDatabase(); const now = Date.now(); - - // 1. Fetch currently synced files from SQLite - const syncedRows = db.prepare("SELECT file_path, mtime_ms, file_size, parser_version FROM synced_files").all() as Array<{ - file_path: string; - mtime_ms: number; - file_size: number; - parser_version: number | null; - }>; - - const syncedMap = new Map(); - for (const row of syncedRows) { - syncedMap.set(row.file_path, { mtime_ms: row.mtime_ms, file_size: row.file_size, parser_version: row.parser_version }); - } + const stored = storedStamps(db, LOCAL_HOST_ID); + const writes = transcriptWrites(db, LOCAL_HOST_ID); let filesUpdated = 0; let recordsInserted = 0; - const insertRecordStmt = db.prepare(` - INSERT INTO usage_records ( - file_path, session_id, session_cwd, timestamp, provider, model, - input_tokens, output_tokens, reasoning_tokens, cache_read_tokens, - cache_write_tokens, total_tokens, cost, cache_savings, cost_quality, - entry_id, subagent_file, session_started - ) VALUES ( - ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, - ?, ?, ?, ?, ?, - ?, ?, ? - ) - `); - - const deleteRecordsStmt = db.prepare("DELETE FROM usage_records WHERE file_path = ?"); - const deleteSyncedFileStmt = db.prepare("DELETE FROM synced_files WHERE file_path = ?"); - const upsertSyncedFileStmt = db.prepare(` - INSERT OR REPLACE INTO synced_files (file_path, mtime_ms, file_size, records_count, synced_at, parser_version) - VALUES (?, ?, ?, ?, ?, ?) - `); - const currentFilesSet = new Set(); const transcripts = sessionFiles.flatMap((sessionFile) => [sessionFile, ...listSessionArtifactTranscripts(sessionFile)].map((filePath) => ({ filePath, sessionFile })), @@ -250,7 +312,6 @@ export function syncSessionFilesToDb( }; try { - // 2. Incremental sync for new / modified files for (const { filePath, sessionFile } of transcripts) { let stats; try { @@ -260,57 +321,19 @@ export function syncSessionFilesToDb( continue; } currentFilesSet.add(filePath); - const existing = syncedMap.get(filePath); - if ( - existing && - existing.mtime_ms === stats.mtimeMs && - existing.file_size === stats.size && - existing.parser_version === USAGE_PARSER_VERSION - ) { - // File has not changed since it was last synced by these rules - continue; - } - - // Parse records from disk - const records: UsageRecord[] = parseSessionUsage(filePath, customModelsConfig, sessionFile); + if (!refresh && isUpToDate(stored.get(filePath), stats, USAGE_PARSER_VERSION)) continue; + const records = parseSessionUsage(filePath, customModelsConfig, sessionFile); ensureTransaction(); - deleteRecordsStmt.run(filePath); - - for (const r of records) { - insertRecordStmt.run( - filePath, - r.sessionId, - r.sessionCwd, - r.timestamp, - r.provider, - r.model, - r.input, - r.output, - r.reasoning, - r.cacheRead, - r.cacheWrite, - r.totalTokens, - r.cost, - r.cacheSavings, - r.costQuality, - r.entryId ?? null, - r.subagentFile ?? null, - r.sessionStarted, - ); - recordsInserted++; - } - - upsertSyncedFileStmt.run(filePath, stats.mtimeMs, stats.size, records.length, now, USAGE_PARSER_VERSION); + writes.replace({ path: filePath, mtimeMs: stats.mtimeMs, size: stats.size, records }, USAGE_PARSER_VERSION, now); + recordsInserted += records.length; filesUpdated++; } - // 3. Purge deleted session files - for (const filePath of syncedMap.keys()) { + for (const filePath of stored.keys()) { if (!currentFilesSet.has(filePath) || !existsSync(filePath)) { ensureTransaction(); - deleteRecordsStmt.run(filePath); - deleteSyncedFileStmt.run(filePath); + writes.remove(filePath); filesDeleted++; } } @@ -329,6 +352,49 @@ export function syncSessionFilesToDb( }; } +/** Replace linked host `hostId`'s rows of each read transcript and drop those of `gone` paths, in one transaction. */ +export function storeHostUsage( + db: Database, + hostId: string, + parserVersion: number, + files: UsageTranscriptRecords[], + gone: string[], +): void { + if (hostId === LOCAL_HOST_ID) throw new Error("This machine's usage is read from its own transcripts"); + const writes = transcriptWrites(db, hostId); + const now = Date.now(); + db.transaction(() => { + for (const file of files) writes.replace(file, parserVersion, now); + for (const path of gone) writes.remove(path); + })(); +} + +/** Mark every stored transcript of linked host `hostId` as read by no parser, so the next pull reads it again. */ +export function invalidateHostUsageStamps(db: Database, hostId: string): void { + db.prepare("UPDATE synced_files SET parser_version = NULL WHERE host_id = ?").run(hostId); +} + +/** Record that linked host `hostId`'s stored rows matched its transcripts at `syncedAt`. */ +export function markHostSynced(db: Database, hostId: string, syncedAt: number): void { + db.prepare("INSERT OR REPLACE INTO synced_hosts (host_id, synced_at) VALUES (?, ?)").run(hostId, syncedAt); +} + +/** When each linked host was last marked synced, by host id. Kept in the database so it outlives a restart. */ +export function hostSyncTimes(db: Database): Map { + const rows = db.prepare("SELECT host_id, synced_at FROM synced_hosts").all() as Array<{ host_id: string; synced_at: number }>; + return new Map(rows.map((row) => [row.host_id, row.synced_at])); +} + +/** Drop everything stored for linked hosts not in `keepHostIds` (revoked or deleted). */ +export function pruneHostUsage(db: Database, keepHostIds: string[]): void { + const keep = JSON.stringify([LOCAL_HOST_ID, ...keepHostIds]); + db.transaction(() => { + db.prepare("DELETE FROM usage_records WHERE host_id NOT IN (SELECT value FROM json_each(?))").run(keep); + db.prepare("DELETE FROM synced_files WHERE host_id NOT IN (SELECT value FROM json_each(?))").run(keep); + db.prepare("DELETE FROM synced_hosts WHERE host_id NOT IN (SELECT value FROM json_each(?))").run(keep); + })(); +} + /** * Rebuild `temp.uncounted_usage`: ids of stored rows the report must skip. * - Copies. omp copies transcript entries, `task` summaries included, with @@ -337,13 +403,14 @@ export function syncSessionFilesToDb( * files is one request. The row from the earliest-started owning session * counts — the original, since every copy starts later; a `/tan` clone ties * with its parent and loses on file path — and deleting that session hands - * the entry to the next copy. Copies are rare, so they are found with one - * sequential scan of the (entry_id, timestamp) index before any row-by-row - * comparison. + * the entry to the next copy. This holds across hosts: a session copied to + * another machine is still one request. Copies are rare, so they are found + * with one sequential scan of the (entry_id, timestamp) index before any + * row-by-row comparison. * - `task` result summaries whose subagent transcript, next to this summary or - * any copy of it, has usage from before the summary: that transcript is - * counted in full. A later transcript that merely reuses the subagent id - * (possible in a branch, which does not copy artifacts) does not cover it. + * any copy of it on the copy's host, has usage from before the summary: that + * transcript is counted in full. A later transcript that merely reuses the + * subagent id (possible in a branch, which does not copy artifacts) does not cover it. * Computed once per report so the report's queries share it. */ function collectUncountedUsage(db: Database): void { @@ -366,10 +433,10 @@ function collectUncountedUsage(db: Database): void { UNION SELECT s.id FROM usage_records s WHERE s.subagent_file IS NOT NULL AND ( - EXISTS (SELECT 1 FROM usage_records t WHERE t.file_path = s.subagent_file AND t.timestamp <= s.timestamp) + EXISTS (SELECT 1 FROM usage_records t WHERE t.host_id = s.host_id AND t.file_path = s.subagent_file AND t.timestamp <= s.timestamp) OR EXISTS ( SELECT 1 FROM usage_records c - JOIN usage_records t ON t.file_path = c.subagent_file AND t.timestamp <= c.timestamp + JOIN usage_records t ON t.host_id = c.host_id AND t.file_path = c.subagent_file AND t.timestamp <= c.timestamp WHERE c.entry_id = s.entry_id AND c.timestamp = s.timestamp ) ); @@ -381,10 +448,13 @@ const COUNTED_ROWS = "id NOT IN (SELECT id FROM temp.uncounted_usage)"; /** * Execute SQL analytics queries over the SQLite database to generate a full UsageReport. + * This machine's transcripts are synced first. `linkedHosts` names and orders the other machines in + * `hostBreakdown`; their rows were pulled by `lib/hosts/usage-sync.ts`. */ export async function getUsageReportFromDb( options: UsageQueryOptions = {}, customDb?: Database, + linkedHosts: HostUsageStatus[] = [], ): Promise { const startTime = Date.now(); const timeRange = options.range || "30d"; @@ -392,19 +462,22 @@ export async function getUsageReportFromDb( const projectFilter = options.project ? options.project.trim().toLowerCase() : undefined; const db = customDb || getUsageDatabase(); - if (options.forceRefresh) { - try { - db.exec("DELETE FROM synced_files; DELETE FROM usage_records;"); - } catch { - // Ignore - } + const refresh = options.forceRefresh === true; + if (refresh) { + // A cached parse keeps the prices it was read with. + invalidateUsageCache(); + invalidateSessionFileListCache(); } // Sync latest sessions (and the helper transcripts in their artifacts // directories) from disk before querying const sessionsDir = getSessionsDir(); const sessionFiles = existsSync(sessionsDir) ? await listSessionFiles(sessionsDir) : []; - syncSessionFilesToDb(sessionFiles, readModelsConfig(), db); + syncSessionFilesToDb(sessionFiles, readModelsConfig(), db, refresh); + const hosts: HostUsageStatus[] = [ + { hostId: LOCAL_HOST_ID, label: customLocalLabel(), state: "current", syncedAt: Date.now() }, + ...linkedHosts, + ]; collectUncountedUsage(db); const hasExplicitBounds = typeof options.from === "number" && @@ -425,18 +498,19 @@ export async function getUsageReportFromDb( } // 1. Summary Query + // Tokens are added with TOTAL throughout: unlike SUM it cannot overflow, however many rows there are. const summaryRow = db .prepare( ` SELECT COUNT(*) AS usageRecordsCount, COALESCE(SUM(cost), 0) AS totalCost, - COALESCE(SUM(total_tokens), 0) AS totalTokens, - COALESCE(SUM(input_tokens), 0) AS inputTokens, - COALESCE(SUM(output_tokens), 0) AS outputTokens, - COALESCE(SUM(reasoning_tokens), 0) AS reasoningTokens, - COALESCE(SUM(cache_read_tokens), 0) AS cacheReadTokens, - COALESCE(SUM(cache_write_tokens), 0) AS cacheWriteTokens, + TOTAL(total_tokens) AS totalTokens, + TOTAL(input_tokens) AS inputTokens, + TOTAL(output_tokens) AS outputTokens, + TOTAL(reasoning_tokens) AS reasoningTokens, + TOTAL(cache_read_tokens) AS cacheReadTokens, + TOTAL(cache_write_tokens) AS cacheWriteTokens, COALESCE(SUM(cache_savings), 0) AS cacheSavings, COUNT(DISTINCT strftime('%Y-%m-%d', timestamp / 1000, 'unixepoch', 'localtime')) AS activeDays, SUM(CASE WHEN cost_quality = 'provider_reported' THEN 1 ELSE 0 END) AS providerReportedCount, @@ -468,6 +542,9 @@ export async function getUsageReportFromDb( const tokensPerActiveDay = activeDays > 0 ? Math.round(totalTokens / activeDays) : 0; const cachePercentage = cacheReadTokens + inputTokens > 0 ? (cacheReadTokens / (cacheReadTokens + inputTokens)) * 100 : 0; + /** Share of the total cost, or of the total tokens while nothing is priced. */ + const shareOf = (cost: number, tokens: number): number => + totalCost > 0 ? (cost / totalCost) * 100 : totalTokens > 0 ? (tokens / totalTokens) * 100 : 0; const summary: UsageSummary = { totalCost, @@ -491,7 +568,7 @@ export async function getUsageReportFromDb( SELECT provider, COALESCE(SUM(cost), 0) AS cost, - COALESCE(SUM(total_tokens), 0) AS tokens + TOTAL(total_tokens) AS tokens FROM usage_records WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} GROUP BY provider @@ -500,22 +577,14 @@ export async function getUsageReportFromDb( ) .all(...params) as Array<{ provider: string; cost: number; tokens: number }>; - const providers: ProviderUsageSummary[] = providerRows.map((row) => { - const share = - totalCost > 0 - ? (row.cost / totalCost) * 100 - : totalTokens > 0 - ? (row.tokens / totalTokens) * 100 - : 0; - return { - provider: row.provider, - name: getProviderDisplayName(row.provider), - cost: row.cost, - tokens: row.tokens, - share, - color: getProviderColor(row.provider), - }; - }); + const providers: ProviderUsageSummary[] = providerRows.map((row) => ({ + provider: row.provider, + name: getProviderDisplayName(row.provider), + cost: row.cost, + tokens: row.tokens, + share: shareOf(row.cost, row.tokens), + color: getProviderColor(row.provider), + })); // 3. Time Series Query const isMonthly = granularity === "monthly"; @@ -529,7 +598,7 @@ export async function getUsageReportFromDb( provider, MIN(timestamp) AS minTimestamp, COALESCE(SUM(cost), 0) AS cost, - COALESCE(SUM(total_tokens), 0) AS tokens + TOTAL(total_tokens) AS tokens FROM usage_records WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} GROUP BY bucketDate, provider @@ -574,7 +643,7 @@ export async function getUsageReportFromDb( timestamp: cur.getTime(), totalCost: 0, totalTokens: 0, - byProvider: {}, + byProvider: Object.create(null), }); } cur.setMonth(cur.getMonth() + 1); @@ -592,7 +661,7 @@ export async function getUsageReportFromDb( timestamp: cur.getTime(), totalCost: 0, totalTokens: 0, - byProvider: {}, + byProvider: Object.create(null), }); } cur.setDate(cur.getDate() + 1); @@ -610,7 +679,7 @@ export async function getUsageReportFromDb( timestamp: row.minTimestamp, totalCost: 0, totalTokens: 0, - byProvider: {}, + byProvider: Object.create(null), }; timeSeriesMap.set(key, bucket); } @@ -644,12 +713,12 @@ export async function getUsageReportFromDb( model, provider, COALESCE(SUM(cost), 0) AS cost, - COALESCE(SUM(total_tokens), 0) AS tokens, - COALESCE(SUM(input_tokens), 0) AS inputTokens, - COALESCE(SUM(output_tokens), 0) AS outputTokens, - COALESCE(SUM(cache_read_tokens), 0) AS cacheReadTokens, - COALESCE(SUM(cache_write_tokens), 0) AS cacheWriteTokens, - COALESCE(SUM(reasoning_tokens), 0) AS reasoningTokens, + TOTAL(total_tokens) AS tokens, + TOTAL(input_tokens) AS inputTokens, + TOTAL(output_tokens) AS outputTokens, + TOTAL(cache_read_tokens) AS cacheReadTokens, + TOTAL(cache_write_tokens) AS cacheWriteTokens, + TOTAL(reasoning_tokens) AS reasoningTokens, COUNT(*) AS recordsCount FROM usage_records WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} @@ -672,12 +741,7 @@ export async function getUsageReportFromDb( const modelBreakdown: ModelUsageSummary[] = modelRows.map((row) => ({ ...row, - share: - totalCost > 0 - ? (row.cost / totalCost) * 100 - : totalTokens > 0 - ? (row.tokens / totalTokens) * 100 - : 0, + share: shareOf(row.cost, row.tokens), })); // 5. Day Breakdown Query @@ -687,10 +751,10 @@ export async function getUsageReportFromDb( SELECT strftime('%Y-%m-%d', timestamp / 1000, 'unixepoch', 'localtime') AS date, COALESCE(SUM(cost), 0) AS cost, - COALESCE(SUM(total_tokens), 0) AS tokens, - COALESCE(SUM(input_tokens), 0) AS inputTokens, - COALESCE(SUM(output_tokens), 0) AS outputTokens, - COALESCE(SUM(cache_read_tokens), 0) AS cacheReadTokens + TOTAL(total_tokens) AS tokens, + TOTAL(input_tokens) AS inputTokens, + TOTAL(output_tokens) AS outputTokens, + TOTAL(cache_read_tokens) AS cacheReadTokens FROM usage_records WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} GROUP BY date @@ -709,12 +773,7 @@ export async function getUsageReportFromDb( const dayBreakdown: DayUsageSummary[] = dayRows.map((row) => ({ ...row, label: formatFullDateLabel(row.date), - share: - totalCost > 0 - ? (row.cost / totalCost) * 100 - : totalTokens > 0 - ? (row.tokens / totalTokens) * 100 - : 0, + share: shareOf(row.cost, row.tokens), })); // 6. Project Breakdown Query @@ -722,45 +781,79 @@ export async function getUsageReportFromDb( .prepare( ` SELECT + host_id AS hostId, session_cwd AS project, COALESCE(SUM(cost), 0) AS cost, - COALESCE(SUM(total_tokens), 0) AS tokens, + TOTAL(total_tokens) AS tokens, COUNT(DISTINCT session_id) AS sessionsCount FROM usage_records WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} - GROUP BY session_cwd + GROUP BY host_id, session_cwd ORDER BY cost DESC, tokens DESC `, ) .all(...params) as Array<{ + hostId: string; project: string; cost: number; tokens: number; sessionsCount: number; }>; - const projectBreakdown: ProjectUsageSummary[] = projectRows.map((row) => ({ - project: row.project || "Default Project", - projectName: basename(row.project || "Default Project") || row.project, - cost: row.cost, - tokens: row.tokens, - share: - totalCost > 0 - ? (row.cost / totalCost) * 100 - : totalTokens > 0 - ? (row.tokens / totalTokens) * 100 - : 0, - sessionsCount: row.sessionsCount, - })); + const hostLabels = new Map(hosts.map((host) => [host.hostId, host.label])); + const projectBreakdown: ProjectUsageSummary[] = projectRows.map((row) => { + const project = row.project || "Default Project"; + return { + project, + // A linked host's paths follow its own OS, not this server's. + projectName: (project.startsWith("/") ? posix : win32).basename(project) || project, + hostId: row.hostId, + hostLabel: hostLabels.get(row.hostId) ?? row.hostId, + cost: row.cost, + tokens: row.tokens, + share: shareOf(row.cost, row.tokens), + sessionsCount: row.sessionsCount, + }; + }); + + // 7. Host Breakdown Query + const hostRows = db + .prepare( + ` + SELECT + host_id AS hostId, + COALESCE(SUM(cost), 0) AS cost, + TOTAL(total_tokens) AS tokens, + COUNT(DISTINCT session_id) AS sessionsCount + FROM usage_records + WHERE ${COUNTED_ROWS} AND timestamp >= ? AND timestamp <= ? ${whereProject} + GROUP BY host_id + `, + ) + .all(...params) as Array<{ hostId: string; cost: number; tokens: number; sessionsCount: number }>; + + const hostTotals = new Map(hostRows.map((row) => [row.hostId, row])); + const hostBreakdown: HostUsageSummary[] = hosts.map((host) => { + const row = hostTotals.get(host.hostId); + const cost = row?.cost ?? 0; + const tokens = row?.tokens ?? 0; + return { + ...host, + cost, + tokens, + share: shareOf(cost, tokens), + sessionsCount: row?.sessionsCount ?? 0, + }; + }); // Scan info const totalSyncedRow = db.prepare("SELECT COUNT(*) as c FROM synced_files").get() as { c: number }; const inWindowSyncedRow = db .prepare( ` - SELECT COUNT(DISTINCT file_path) as c + SELECT COUNT(*) as c FROM (SELECT DISTINCT host_id, file_path FROM usage_records - WHERE timestamp >= ? AND timestamp <= ? ${whereProject} + WHERE timestamp >= ? AND timestamp <= ? ${whereProject}) `, ) .get(...params) as { c: number }; @@ -779,6 +872,7 @@ export async function getUsageReportFromDb( modelBreakdown, dayBreakdown, projectBreakdown, + hostBreakdown, scanInfo: { transcriptsScanned, transcriptsOutsideWindow, diff --git a/lib/usage-report.ts b/lib/usage-report.ts new file mode 100644 index 000000000..b59dcafca --- /dev/null +++ b/lib/usage-report.ts @@ -0,0 +1,12 @@ +import { pullAllHostUsage } from "./hosts/usage-sync"; +import { getUsageDatabase, getUsageReportFromDb } from "./usage-db"; +import type { UsageQueryOptions, UsageReport } from "./usage-types"; + +/** + * Usage of this machine's sessions plus every linked host's, from the persistent SQLite usage database. + * Kept out of usage-service.ts, which the host daemon loads under Node, where `bun:sqlite` does not exist. + */ +export async function getUsageReport(options: UsageQueryOptions = {}): Promise { + const db = getUsageDatabase(); + return getUsageReportFromDb(options, db, await pullAllHostUsage(db, options.forceRefresh === true)); +} diff --git a/lib/usage-service.test.mjs b/lib/usage-service.test.mjs index c3a1b537e..8856ec6f3 100644 --- a/lib/usage-service.test.mjs +++ b/lib/usage-service.test.mjs @@ -153,8 +153,29 @@ test("parseSessionUsage extracts subagent task usage results", async () => { }); }); +test("lines whose numbers no request can have are skipped like malformed ones", async () => { + const { parseSessionUsage } = await loadSubject(); + + await withTempSessionDir((dir) => { + const sessionFile = join(dir, "session-implausible.jsonl"); + const request = (id, usage) => JSON.stringify({ type: "message", id, message: { role: "assistant", provider: "anthropic", model: "claude-3-7-sonnet", usage } }); + writeFileSync(sessionFile, [ + JSON.stringify({ type: "session", id: "sess-implausible", cwd: "/test/repo", timestamp: new Date().toISOString() }), + request("plausible", { input: 100, output: 10 }), + request("too-many-tokens", { input: 100, output: 10, totalTokens: 1e30 }), + request("negative-tokens", { input: -5, output: 10 }), + request("fractional-tokens", { input: 0.5, output: 10 }), + request("too-costly", { input: 100, output: 10, cost: 1e7 }), + // JSON has no Infinity, but a number too large for a double parses as one. + request("infinite-cost", { input: 100, output: 10, cost: 0 }).replace('"cost":0', '"cost":1e999'), + ].join("\n"), "utf8"); + + assert.deepEqual(parseSessionUsage(sessionFile).map((record) => record.entryId), ["plausible"]); + }); +}); + test("getUsageReport generates complete aggregated report across sessions", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); withTempSessionDir(async (dir) => { const sessionsDir = join(dir, "sessions", "test-project"); @@ -261,7 +282,7 @@ const taskResultEntry = (id, at, results) => JSON.stringify({ }); test("getUsageReport counts every helper transcript once, under the session that owns it", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); await withTempSessionDir(async (dir) => { const project = join(dir, "sessions", "test-project"); @@ -311,7 +332,7 @@ test("getUsageReport counts every helper transcript once, under the session that }); test("getUsageReport counts history copied by /tan, /fork and branches once, and keeps it when the original is deleted", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); await withTempSessionDir(async (dir) => { const project = join(dir, "sessions", "test-project"); @@ -364,7 +385,7 @@ test("getUsageReport counts history copied by /tan, /fork and branches once, and }); test("copied history stays with the session it came from when the copy lives in another project folder", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); await withTempSessionDir(async (dir) => { // `omp --fork` writes the copy into the launch directory's folder, which may sort before the original's. @@ -387,7 +408,7 @@ test("copied history stays with the session it came from when the copy lives in }); test("a later subagent that reuses an id does not hide an inherited task summary", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); await withTempSessionDir(async (dir) => { // A branch keeps the parent's `task` summary for `Reviewer` but not its transcript (the @@ -424,7 +445,7 @@ test("time range bounds compute valid intervals", async () => { }); test("getUsageReport respects explicit from=0 timestamp without falling back to 30d", async () => { - const { getUsageReport } = await loadSubject(); + const { getUsageReport } = await import("./usage-report.ts"); withTempSessionDir(async (dir) => { const sessionsDir = join(dir, "sessions", "test-project"); diff --git a/lib/usage-service.ts b/lib/usage-service.ts index b4fd1e0c1..7d0818399 100644 --- a/lib/usage-service.ts +++ b/lib/usage-service.ts @@ -1,19 +1,17 @@ -import { statSync } from "fs"; +import { closeSync, openSync, statSync } from "fs"; import { basename, join } from "path"; import { readModelsConfig } from "./omp/models-config"; -import { forEachFileLineSync, invalidateSessionFileListCache, readSessionHeaderSync } from "./omp/session-files"; +import { forEachFileLineSync, readSessionHeaderSync } from "./omp/session-files"; import { SUBAGENT_ID_RE } from "./subagent-types"; import { isRecord } from "./type-guards"; import { calculateCacheSavings, calculateUsageCost, + clearRatesCache, resolveModelRates, } from "./usage-rates"; -import { getUsageReportFromDb } from "./usage-db"; import type { - UsageQueryOptions, UsageRecord, - UsageReport, UsageTimeRange, } from "./usage-types"; @@ -27,8 +25,23 @@ import type { * reload that brings in new rules. * v2: artifact transcripts counted; entry ids, task-summary targets and the * owning session's start stored. + * v3: lines whose numbers no request can have are skipped. */ -export const USAGE_PARSER_VERSION = 2; +export const USAGE_PARSER_VERSION = 3; + +/** The most one request can report. A record beyond it, or with a token count that is no whole number, is a malformed + * line. Kept out, the rows left cannot make a report's cost sums go non-finite. */ +export const USAGE_RECORD_LIMITS = { tokens: 1e9, cost: 1e6 }; + +/** Whether every number of `record` is one a request can have. */ +export function hasPlausibleNumbers(record: UsageRecord): boolean { + const count = (value: number) => Number.isInteger(value) && value >= 0 && value <= USAGE_RECORD_LIMITS.tokens; + return count(record.input) && count(record.output) && count(record.reasoning) + && count(record.cacheRead) && count(record.cacheWrite) && count(record.totalTokens) + && record.cost >= 0 && record.cost <= USAGE_RECORD_LIMITS.cost + && Math.abs(record.cacheSavings) <= USAGE_RECORD_LIMITS.cost + && Number.isFinite(record.timestamp) && Number.isFinite(record.sessionStarted); +} interface SessionUsageCacheEntry { parserVersion: number; @@ -77,10 +90,12 @@ function setUsageCacheEntry(filePath: string, entry: SessionUsageCacheEntry): vo } } -/** Clear in-memory usage cache (useful on session mutation or manual refresh). */ +/** Clear the in-memory usage cache and the model rates cached beside it (manual refresh): both keep the prices + * `models.yml` had when they were read. */ export function invalidateUsageCache(): void { globalThis.__ompUsageCache?.clear(); usageCacheApproxBytes = 0; + clearRatesCache(); } /** @@ -182,17 +197,23 @@ export function computeTimeRangeBounds( * that session's artifacts directory (subagent, advisor, `/tan` clone, * extension sub-session) the records carry the owner's id and cwd rather than * the transcript's own header, so they count toward that session and project. + * + * A transcript that cannot be read to its end yields what was read, uncached. + * With `strict` that throws instead, as does an unreadable file or owner: the + * caller stores the result as the whole transcript. */ export function parseSessionUsage( filePath: string, customModelsConfig = readModelsConfig(), sessionFile = filePath, + strict = false, ): UsageRecord[] { let stats; try { stats = statSync(filePath); if (!stats.isFile() || stats.size === 0) return []; - } catch { + } catch (error) { + if (strict) throw error; return []; } @@ -204,6 +225,8 @@ export function parseSessionUsage( const isArtifact = sessionFile !== filePath; const owner = isArtifact ? readSessionHeaderSync(sessionFile) : null; + // An owner that opens but has no header is malformed and its artifacts go by its file name; one that does not open throws. + if (strict && isArtifact && !owner) closeSync(openSync(sessionFile, "r")); let sessionId = owner?.id ?? basename(sessionFile, ".jsonl"); let sessionCwd = owner?.cwd ?? ""; let sessionTimestamp = stats.mtimeMs; @@ -212,6 +235,9 @@ export function parseSessionUsage( let activeProvider = ""; let activeModel = ""; const records: UsageRecord[] = []; + const keep = (record: UsageRecord): void => { + if (hasPlausibleNumbers(record)) records.push(record); + }; try { forEachFileLineSync(filePath, (rawLine) => { @@ -293,7 +319,7 @@ export function parseSessionUsage( const cacheSavings = calculateCacheSavings(rawUsage, rates); if (totalTokens > 0 || cost > 0) { - records.push({ + keep({ timestamp, sessionId, sessionCwd, @@ -372,7 +398,7 @@ export function parseSessionUsage( const cacheSavings = calculateCacheSavings(u, rates); if (totalTokens > 0 || cost > 0) { - records.push({ + keep({ timestamp, sessionId, sessionCwd, @@ -399,8 +425,10 @@ export function parseSessionUsage( } } }); - } catch { - // Return partially collected records on read error + } catch (error) { + if (strict) throw error; + // Not cached: its stamp would vouch for a transcript that was only partly read. + return records; } setUsageCacheEntry(filePath, { @@ -413,15 +441,3 @@ export function parseSessionUsage( return records; } - -/** - * Generate full usage report over all sessions according to query options. - * Backed by the persistent local SQLite usage database. - */ -export async function getUsageReport(options: UsageQueryOptions = {}): Promise { - if (options.forceRefresh) { - invalidateUsageCache(); - invalidateSessionFileListCache(); - } - return getUsageReportFromDb(options); -} diff --git a/lib/usage-types.ts b/lib/usage-types.ts index 33c292f50..ec0447724 100644 --- a/lib/usage-types.ts +++ b/lib/usage-types.ts @@ -1,7 +1,7 @@ export type UsageTimeRange = "today" | "7d" | "30d" | "90d" | "month" | "all"; export type UsageGranularity = "daily" | "monthly" | "projects"; export type UsageMetricView = "cost" | "tokens"; -export type UsageBreakdownView = "model" | "day" | "project"; +export type UsageBreakdownView = "model" | "day" | "project" | "host"; export type CostQualityTier = "provider_reported" | "model_priced" | "unpriced"; export interface ModelRates { @@ -112,6 +112,8 @@ export interface DayUsageSummary { export interface ProjectUsageSummary { project: string; // directory path projectName: string; // display folder name + hostId: string; + hostLabel: string; cost: number; tokens: number; share: number; // 0-100 percentage @@ -135,6 +137,7 @@ export interface UsageReport { modelBreakdown: ModelUsageSummary[]; dayBreakdown: DayUsageSummary[]; projectBreakdown: ProjectUsageSummary[]; + hostBreakdown: HostUsageSummary[]; scanInfo: UsageReportScanInfo; } @@ -146,3 +149,59 @@ export interface UsageQueryOptions { to?: number; forceRefresh?: boolean; } + +/** One transcript as its host last saw it; a changed stamp means it must be read again. */ +export interface UsageTranscriptStamp { + path: string; + mtimeMs: number; + size: number; +} + +export interface UsageSessionStamps { + session: string; + transcripts: UsageTranscriptStamp[]; +} + +/** Every transcript the host counts usage from. The host fails the op rather than leave one out it cannot examine. */ +export interface UsageTranscriptIndex { + parserVersion: number; + sessions: UsageSessionStamps[]; +} + +export interface UsageReadRequest { + session: string; + paths: string[]; + /** Records of `paths[0]` that earlier answers delivered: this one continues after them. */ + skip?: number; +} + +export interface UsageTranscriptRecords extends UsageTranscriptStamp { + records: UsageRecord[]; + /** The transcript has more records than one answer holds: ask for it again, skipping the ones received. */ + more?: boolean; +} + +/** `missing`: gone, or not a transcript this host lists. A path asked for and in neither list was not reached within + * the page limits: ask again. A file with `more` is the last of its page. */ +export interface UsageRecordsPage { + files: UsageTranscriptRecords[]; + missing: string[]; +} + +/** `stale`: the last pull failed. `unsupported`: the host's daemon predates the usage ops. */ +export type HostUsageSyncState = "current" | "syncing" | "offline" | "stale" | "unsupported"; + +export interface HostUsageStatus { + hostId: string; + label: string; + state: HostUsageSyncState; + /** When the stored rows were last brought up to date; null before the first full pull. */ + syncedAt: number | null; +} + +export interface HostUsageSummary extends HostUsageStatus { + cost: number; + tokens: number; + share: number; // 0-100 percentage + sessionsCount: number; +}