@bom0792/herdgate 0.9.36 → 0.9.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/herdr/SKILL.md +100 -0
- package/DESIGN.md +28 -0
- package/README.md +55 -1
- package/package.json +2 -1
- package/plugin/herdr-plugin.toml +17 -0
- package/plugin/server/attachments.mjs +184 -0
- package/plugin/server/e2e.mjs +27 -0
- package/plugin/server/git-safe.mjs +126 -0
- package/plugin/server/index.mjs +317 -5
- package/plugin/server/mobile-tui-core.mjs +701 -41
- package/plugin/server/mobile-tui-stream.mjs +262 -3
- package/plugin/server/pi-receipt-gc.mjs +183 -0
- package/plugin/server/pi-receipt-search.mjs +217 -0
- package/plugin/server/pi-receipts.mjs +125 -13
- package/plugin/server/pi-transcript.mjs +217 -0
- package/plugin/server/pi-usage.mjs +84 -0
- package/plugin/server/pi-wake.mjs +319 -0
- package/plugin/server/pi-worker.d.mts +7 -0
- package/plugin/server/pi-worker.mjs +45 -4
- package/plugin/server/port-attr.mjs +172 -0
- package/plugin/server/sysmon.mjs +212 -0
- package/plugin/server/wake-event.mjs +255 -0
- package/src/agent/index.ts +4 -2
- package/src/agent/qr.ts +70 -0
- package/src/cloud/gateway.ts +5 -1
- package/src/cloud/sshd.ts +5 -8
- package/src/host/main.ts +14 -1
- package/src/web/app.css +379 -0
- package/src/web/app.js +1557 -5
- package/src/web/e2e.js +29 -2
- package/src/web/index.html +1 -0
- package/windows/npm-update.mjs +261 -11
- package/windows/update-policy.mjs +98 -0
|
@@ -25,9 +25,83 @@ export function scopeFor(cwd) {
|
|
|
25
25
|
// journal scope. Reads already scan every scope, so this only converges writes.
|
|
26
26
|
let real = cwd;
|
|
27
27
|
try { real = realpathSync(cwd); } catch { /* deleted cwd: keep the raw path */ }
|
|
28
|
+
// Windows realpath preserves the caller's letter casing (D:\Bom vs d:\bom are
|
|
29
|
+
// the same dir on NTFS), so converge scopes case-insensitively there — verified
|
|
30
|
+
// live against the installed release on Windows (docs/herdr-skills validation).
|
|
31
|
+
if (process.platform === 'win32') real = real.toLowerCase();
|
|
28
32
|
return createHash('sha256').update(real).digest('hex').slice(0, 16);
|
|
29
33
|
}
|
|
30
34
|
|
|
35
|
+
// The actor behind a request, on every event trail it touches (herdr-sched §3.1):
|
|
36
|
+
// schedule:<id> | job:<id> | device:<id> | human:<free-tag> — absent means a
|
|
37
|
+
// human typed it locally in a pane and nothing programmatic claims the request.
|
|
38
|
+
export const PRINCIPAL = /^[a-z][a-z0-9-]{0,15}:[A-Za-z0-9._-]{1,64}$/;
|
|
39
|
+
|
|
40
|
+
// A receipt may carry an optional one-line title derived from the prompt
|
|
41
|
+
// (deriveTitle). It is prompt-derived content, so it lives under the strictest
|
|
42
|
+
// leak policy: stored in the host-local journal, never in publicReceipt unless
|
|
43
|
+
// the caller explicitly opts in with withTitle.
|
|
44
|
+
export const TITLE_MAX = 96;
|
|
45
|
+
const TITLE_CTRL = /[\u0000-\u001f\u007f]/; // no control characters (\n, \r, \t included)
|
|
46
|
+
|
|
47
|
+
function validTitle(value) {
|
|
48
|
+
return typeof value === 'string' && value.length >= 1 && value.length <= TITLE_MAX
|
|
49
|
+
&& !TITLE_CTRL.test(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* First meaningful line of a prompt: drop the first line when it is the request
|
|
54
|
+
* marker (`[herdr-request:…]`) or a scheduler wake frame (`[herdgate-schedule …]`),
|
|
55
|
+
* take the first non-empty line, drop control characters, trim, cap at
|
|
56
|
+
* TITLE_MAX. Returns '' when the prompt says nothing usable — title stays optional.
|
|
57
|
+
*/
|
|
58
|
+
export function deriveTitle(promptText) {
|
|
59
|
+
const lines = String(promptText ?? '').split(/\r?\n/);
|
|
60
|
+
const start = /^\[(herdr-request|herdgate-schedule)[^\]]*\]$/.test(lines[0].trim()) ? 1 : 0;
|
|
61
|
+
for (let i = start; i < lines.length; i++) {
|
|
62
|
+
const line = lines[i].replace(TITLE_CTRL, ' ').replace(/\s+/g, ' ').trim();
|
|
63
|
+
if (line) return line.slice(0, TITLE_MAX);
|
|
64
|
+
}
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Send-path delivery (chat.md useDelivery.ts:120-220 — the transport-vs-app
|
|
69
|
+
// flag, seen from the host side): `delivered` = the host acknowledged the
|
|
70
|
+
// prompt, `stalled` = the transport reported a stall after the host saw it,
|
|
71
|
+
// `unverified` = sent with no acknowledgement. Only the sending transport can
|
|
72
|
+
// observe a stall; the journal writer records what IT observed.
|
|
73
|
+
export const DELIVERY = new Set(['delivered', 'stalled', 'unverified']);
|
|
74
|
+
// Failure phase (mobrel coordinator.go:884 + socket_api.go:157 — "how many
|
|
75
|
+
// bytes reached the socket" as the boundary): `failed_before_dispatch` = the
|
|
76
|
+
// fail happened before the request reached the provider (retry-safe
|
|
77
|
+
// knowledge), `dispatch` = dispatched with the outcome still unknown,
|
|
78
|
+
// `settled` = a final outcome (replied, failed, canceled).
|
|
79
|
+
export const PHASE = new Set(['dispatch', 'settled', 'failed_before_dispatch']);
|
|
80
|
+
// A machine-readable error is bounded like mobrel's ApiError (protocol.go:324):
|
|
81
|
+
// code ≤64 chars, ≤8 args, each string ≤256, total JSON ≤1024. Free-text
|
|
82
|
+
// diagnostics never qualify — without a mappable code there is no error field.
|
|
83
|
+
const ERROR_CODE = /^[a-z][a-z0-9_]{0,63}$/;
|
|
84
|
+
const ERROR_ARGS_MAX = 8;
|
|
85
|
+
const ERROR_STRING_MAX = 256;
|
|
86
|
+
const ERROR_JSON_MAX = 1024;
|
|
87
|
+
|
|
88
|
+
export function validError(error) {
|
|
89
|
+
if (!error || typeof error !== 'object' || Array.isArray(error)) return false;
|
|
90
|
+
if (Object.keys(error).some(key => key !== 'code' && key !== 'args')) return false;
|
|
91
|
+
if (typeof error.code !== 'string' || !ERROR_CODE.test(error.code)) return false;
|
|
92
|
+
if (error.args !== undefined) {
|
|
93
|
+
if (typeof error.args !== 'object' || error.args === null || Array.isArray(error.args)) return false;
|
|
94
|
+
const keys = Object.keys(error.args);
|
|
95
|
+
if (keys.length > ERROR_ARGS_MAX) return false;
|
|
96
|
+
if (!keys.every(key => {
|
|
97
|
+
const value = error.args[key];
|
|
98
|
+
return typeof value === 'string' ? value.length <= ERROR_STRING_MAX
|
|
99
|
+
: typeof value === 'number' ? Number.isFinite(value) : typeof value === 'boolean';
|
|
100
|
+
})) return false;
|
|
101
|
+
}
|
|
102
|
+
return JSON.stringify(error).length <= ERROR_JSON_MAX;
|
|
103
|
+
}
|
|
104
|
+
|
|
31
105
|
export function validEntry(entry) {
|
|
32
106
|
return validEntryShape(entry) ? entry : null;
|
|
33
107
|
}
|
|
@@ -40,7 +114,14 @@ function validEntryShape(entry) {
|
|
|
40
114
|
&& (entry.usage === undefined || (typeof entry.usage === 'object' && entry.usage !== null
|
|
41
115
|
&& Object.keys(entry.usage).length > 0
|
|
42
116
|
&& Object.keys(entry.usage).every(key => ['totalTokens', 'costUsd', 'durationMs'].includes(key))
|
|
43
|
-
&& Object.values(entry.usage).every(value => Number.isFinite(value)))
|
|
117
|
+
&& Object.values(entry.usage).every(value => Number.isFinite(value)))
|
|
118
|
+
&& (entry.principal === undefined || PRINCIPAL.test(entry.principal)))
|
|
119
|
+
&& (entry.delivery === undefined || DELIVERY.has(entry.delivery))
|
|
120
|
+
&& (entry.phase === undefined || PHASE.has(entry.phase))
|
|
121
|
+
&& (entry.error === undefined || validError(entry.error))
|
|
122
|
+
&& (entry.title === undefined || validTitle(entry.title))
|
|
123
|
+
&& (entry.turns === undefined || (typeof entry.turns === 'number' && Number.isInteger(entry.turns) && entry.turns >= 0))
|
|
124
|
+
&& (entry.lastActivityAt === undefined || (typeof entry.lastActivityAt === 'number' && Number.isFinite(entry.lastActivityAt))));
|
|
44
125
|
}
|
|
45
126
|
|
|
46
127
|
/** Parse a journal file; tolerate a torn tail and skip corrupt lines. */
|
|
@@ -81,11 +162,30 @@ export function findReceipt(base, id) {
|
|
|
81
162
|
}
|
|
82
163
|
}
|
|
83
164
|
|
|
84
|
-
/** The public answer for a device: journal internals stay on the host.
|
|
85
|
-
|
|
165
|
+
/** The public answer for a device: journal internals stay on the host.
|
|
166
|
+
* Title is prompt-derived text, so it is withheld by default (fail-safe) and
|
|
167
|
+
* only included when the caller explicitly passes withTitle: true. Measured
|
|
168
|
+
* activity counters (`turns`, `lastActivityAt`) are a count and a timestamp —
|
|
169
|
+
* no conversation content — so they pass through whenever the entry has them. */
|
|
170
|
+
export function publicReceipt(entry, { withTitle = false } = {}) {
|
|
86
171
|
if (!entry) return { status: 'unknown', reason: 'not_retained_in_runtime' };
|
|
87
|
-
const { v, ts, sessionId, paneId, ...rest } = entry;
|
|
88
|
-
|
|
172
|
+
const { v, ts, sessionId, paneId, title, error, ...rest } = entry;
|
|
173
|
+
// `error` is host-side diagnostics and never leaves the host; devices get
|
|
174
|
+
// the delivery/phase metadata instead. Legacy journals predate both fields:
|
|
175
|
+
// a responseEntryId is itself a delivery proof, so it maps to `delivered` —
|
|
176
|
+
// anything else stays absent rather than guessed.
|
|
177
|
+
const shown = withTitle && title !== undefined ? { ...rest, title } : { ...rest };
|
|
178
|
+
if (shown.delivery === undefined && entry.responseEntryId !== undefined) shown.delivery = 'delivered';
|
|
179
|
+
return shown;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** True when the receipt has not seen activity for longer than staleAfterMs.
|
|
183
|
+
* Only an observed `lastActivityAt` can say so: an entry without the field
|
|
184
|
+
* predates the counter (or was written without transcript access), and the
|
|
185
|
+
* difference is UNKNOWN, not stale — no guessing, fail closed to false. */
|
|
186
|
+
export function isStale(entry, now = Date.now(), staleAfterMs = 30 * 60e3) {
|
|
187
|
+
if (!entry || typeof entry.lastActivityAt !== 'number' || !Number.isFinite(entry.lastActivityAt)) return false;
|
|
188
|
+
return now - entry.lastActivityAt > staleAfterMs;
|
|
89
189
|
}
|
|
90
190
|
|
|
91
191
|
/**
|
|
@@ -128,8 +228,10 @@ export function findBody(base, id) {
|
|
|
128
228
|
|
|
129
229
|
/**
|
|
130
230
|
* Watch every workspace journal under base and hand each NEW settle to onEntry
|
|
131
|
-
* (unseen id, or a newer ts for a known id).
|
|
132
|
-
*
|
|
231
|
+
* (unseen id, or a newer ts for a known id). Two copies of one id inside a
|
|
232
|
+
* single sweep collapse to its newest copy — one emit, never two. The first
|
|
233
|
+
* scan is a baseline and emits nothing, so a restarting publisher does not
|
|
234
|
+
* replay old receipts.
|
|
133
235
|
* fs.watch (recursive where supported) gives low latency; a slow interval sweep
|
|
134
236
|
* covers missed events and platforms without recursive watch. A missing base
|
|
135
237
|
* directory just means the next sweep retries. Never throws to the caller.
|
|
@@ -145,15 +247,25 @@ export function watchReceipts(base, onEntry, { intervalMs = 4000 } = {}) {
|
|
|
145
247
|
let scopes = [];
|
|
146
248
|
try { scopes = readdirSync(base).filter(name => /^[0-9a-f]{16}$/.test(name)); }
|
|
147
249
|
catch { /* base not created yet */ }
|
|
250
|
+
// Mailbox newer-of-two (usage.md refresh.rs:326-344 provider_snapshot_for_display):
|
|
251
|
+
// one sweep can observe two copies of the same id — an event writer and a
|
|
252
|
+
// poll writer racing on one mailbox. Gather everything from every scope,
|
|
253
|
+
// order by (id, ts), and keep only each id's newest copy, so a single sweep
|
|
254
|
+
// emits an id at most once and always the newest copy, never both.
|
|
255
|
+
const rows = [];
|
|
148
256
|
for (const name of scopes) {
|
|
149
257
|
let entries;
|
|
150
258
|
try { entries = readJournal(join(base, name, RECEIPTS)); } catch { continue; }
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
259
|
+
rows.push(...entries);
|
|
260
|
+
}
|
|
261
|
+
rows.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : a.ts - b.ts));
|
|
262
|
+
const newest = rows.filter((entry, i) => i + 1 === rows.length || rows[i + 1].id !== entry.id);
|
|
263
|
+
newest.sort((a, b) => a.ts - b.ts || (a.id < b.id ? -1 : 1));
|
|
264
|
+
for (const entry of newest) {
|
|
265
|
+
const prior = seen.get(entry.id);
|
|
266
|
+
if (prior !== undefined && prior >= entry.ts) continue;
|
|
267
|
+
seen.set(entry.id, entry.ts);
|
|
268
|
+
if (emit) { try { onEntry(entry); } catch { /* listener errors must not kill the sweep */ } }
|
|
157
269
|
}
|
|
158
270
|
};
|
|
159
271
|
const ensureWatcher = () => {
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// Pi session transcript parser (see docs/research/2026-11-21-marketplace/deep/
|
|
2
|
+
// usage.md — ported from herdr-agent-usage src/pi.rs, MIT): tail-window read of
|
|
3
|
+
// at most `maxBytes` (default 8MB), opened on exact line boundaries — the line
|
|
4
|
+
// cut by the window start is dropped, not parsed; the window is measured from
|
|
5
|
+
// the length at read time so a line appended mid-read waits for the next pass.
|
|
6
|
+
// A file larger than the window also keeps a 64KB header read so the `session`
|
|
7
|
+
// header (id + version) is always available.
|
|
8
|
+
// The active branch is walked newest→root by id/parentId (a windowed
|
|
9
|
+
// transcript accepts a parent that lives before the window). Model comes from
|
|
10
|
+
// the last `model_change` on the branch (assistant message model as fallback),
|
|
11
|
+
// contextTokens from the newest assistant usage AFTER the last `compaction`
|
|
12
|
+
// (contextTokens || totalTokens || Σ of the four counters), and usageTotal
|
|
13
|
+
// sums usage of assistant/toolResult messages plus `branch_summary` and
|
|
14
|
+
// `compaction` entries — Pi's own session-stat aggregation. Entry schema is
|
|
15
|
+
// exactly what the herdgate receipt observer reads (experiments/pi-receipt/
|
|
16
|
+
// observer.js) and what Pi 1.5.2 writes. A broken or absent transcript yields
|
|
17
|
+
// {error: 'unreadable', file} — never a throw, never a guess.
|
|
18
|
+
|
|
19
|
+
import { closeSync, constants, fstatSync, openSync, readSync } from 'node:fs';
|
|
20
|
+
import { basename } from 'node:path';
|
|
21
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
22
|
+
|
|
23
|
+
const DEFAULT_MAX_BYTES = 8 * 1024 * 1024; // MAX_SESSION_BYTES
|
|
24
|
+
const HEADER_BYTES = 64 * 1024;
|
|
25
|
+
const MAX_BRANCH = 65_536; // branch walk cap; the seen-set guards cycles
|
|
26
|
+
|
|
27
|
+
/** stem == sessionId or `*_<sessionId>` (pi.rs filename_matches_session_id). */
|
|
28
|
+
export function filenameMatchesSessionId(name, sessionId) {
|
|
29
|
+
const stem = String(name).replace(/\.jsonl$/, '');
|
|
30
|
+
return stem === sessionId || stem.endsWith(`_${sessionId}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function readRange(fd, start, end, decoder) {
|
|
34
|
+
const chunks = [];
|
|
35
|
+
let pos = start;
|
|
36
|
+
while (pos < end) {
|
|
37
|
+
const buf = Buffer.alloc(Math.min(64 * 1024, end - pos));
|
|
38
|
+
const n = readSync(fd, buf, 0, buf.length, pos);
|
|
39
|
+
if (!n) break; // file shrank mid-read — take what is there
|
|
40
|
+
chunks.push(decoder.write(buf.subarray(0, n)));
|
|
41
|
+
pos += n;
|
|
42
|
+
}
|
|
43
|
+
chunks.push(decoder.end());
|
|
44
|
+
return chunks.join('');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Read the header window (first 64KB) and the newest `maxBytes` of the file on
|
|
49
|
+
* line boundaries. Whole file when it fits; otherwise the header line cut at
|
|
50
|
+
* 64KB and the tail line cut by the window start are both dropped.
|
|
51
|
+
*/
|
|
52
|
+
function readWindows(filePath, maxBytes) {
|
|
53
|
+
const fd = openSync(filePath, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
|
|
54
|
+
try {
|
|
55
|
+
const size = fstatSync(fd).size;
|
|
56
|
+
if (size <= maxBytes) {
|
|
57
|
+
const lines = readRange(fd, 0, size, new StringDecoder('utf8')).split('\n');
|
|
58
|
+
if (lines.length && lines[lines.length - 1] === '') lines.pop(); // trailing newline
|
|
59
|
+
return { header: lines, tail: lines, windowed: false };
|
|
60
|
+
}
|
|
61
|
+
const header = readRange(fd, 0, Math.min(HEADER_BYTES, size), new StringDecoder('utf8')).split('\n');
|
|
62
|
+
header.pop(); // the line the header window cuts is not parsed
|
|
63
|
+
const tail = readRange(fd, size - maxBytes, size, new StringDecoder('utf8')).split('\n');
|
|
64
|
+
tail.shift(); // the line the window start cuts is dropped, not parsed
|
|
65
|
+
if (tail.length && tail[tail.length - 1] === '') tail.pop();
|
|
66
|
+
return { header, tail, windowed: true };
|
|
67
|
+
} finally {
|
|
68
|
+
try { closeSync(fd); } catch { /* already closed */ }
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function parseJsonObjects(lines) {
|
|
73
|
+
const entries = [];
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
if (!line.trim()) continue;
|
|
76
|
+
try {
|
|
77
|
+
const entry = JSON.parse(line);
|
|
78
|
+
if (entry && typeof entry === 'object' && !Array.isArray(entry)) entries.push(entry);
|
|
79
|
+
} catch {
|
|
80
|
+
continue; // corrupt or torn line — skipped, never fatal
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return entries;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const finite = value => (Number.isFinite(value) ? value : 0);
|
|
87
|
+
|
|
88
|
+
/** contextTokens || totalTokens || Σ of the four counters (saturating). */
|
|
89
|
+
function contextTokensOf(usage) {
|
|
90
|
+
if (!usage) return null;
|
|
91
|
+
if (Number.isFinite(usage.contextTokens)) return usage.contextTokens;
|
|
92
|
+
if (Number.isFinite(usage.totalTokens)) return usage.totalTokens;
|
|
93
|
+
const sum = finite(usage.input) + finite(usage.output) + finite(usage.cacheRead) + finite(usage.cacheWrite);
|
|
94
|
+
return sum;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Parse a Pi session JSONL transcript.
|
|
99
|
+
* Returns {sessionId, model, modelProvider, contextTokens, usageTotal:
|
|
100
|
+
* {totalTokens, costUsd}, usageCoversSession, turns, lastTs} — `model` from
|
|
101
|
+
* the last model_change on the active branch, `contextTokens` after the last
|
|
102
|
+
* compaction, `turns` counts assistant messages on the branch, `lastTs` is
|
|
103
|
+
* the newest message timestamp (message ms first, entry ISO as fallback,
|
|
104
|
+
* session header as last resort, null when nothing carries one).
|
|
105
|
+
* usageCoversSession is false when the tail window dropped older lines (a
|
|
106
|
+
* windowed sum is a lower bound, not the session total).
|
|
107
|
+
* Unreadable/absent/invalid → {error: 'unreadable', file}. Never throws.
|
|
108
|
+
*/
|
|
109
|
+
export function parsePiTranscript(filePath, maxBytes = DEFAULT_MAX_BYTES) {
|
|
110
|
+
let windows;
|
|
111
|
+
try {
|
|
112
|
+
windows = readWindows(filePath, Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : DEFAULT_MAX_BYTES);
|
|
113
|
+
} catch {
|
|
114
|
+
return { error: 'unreadable', file: filePath };
|
|
115
|
+
}
|
|
116
|
+
const session = parseJsonObjects(windows.header).find(entry => entry.type === 'session');
|
|
117
|
+
const sessionId = session?.id;
|
|
118
|
+
if (!session || Number(session.version) !== 3 || typeof sessionId !== 'string'
|
|
119
|
+
|| sessionId.length === 0 || !filenameMatchesSessionId(basename(String(filePath)), sessionId)) {
|
|
120
|
+
return { error: 'unreadable', file: filePath };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const entries = parseJsonObjects(windows.tail).filter(entry => entry.type !== 'session' && typeof entry.id === 'string');
|
|
124
|
+
const byId = new Map(entries.map(entry => [entry.id, entry]));
|
|
125
|
+
|
|
126
|
+
// Active branch: newest entry → root by parentId. A parent that lives before
|
|
127
|
+
// the window is simply not in byId — the walk stops there (accepted).
|
|
128
|
+
const branch = [];
|
|
129
|
+
const seen = new Set();
|
|
130
|
+
let cursor = entries.length ? entries[entries.length - 1] : undefined;
|
|
131
|
+
while (cursor && !seen.has(cursor.id) && branch.length < MAX_BRANCH) {
|
|
132
|
+
seen.add(cursor.id);
|
|
133
|
+
branch.push(cursor);
|
|
134
|
+
const parentId = cursor.parentId;
|
|
135
|
+
cursor = typeof parentId === 'string' ? byId.get(parentId) : undefined;
|
|
136
|
+
}
|
|
137
|
+
branch.reverse(); // root → leaf
|
|
138
|
+
|
|
139
|
+
let model = null;
|
|
140
|
+
let modelProvider = null;
|
|
141
|
+
let lastCompaction = -1;
|
|
142
|
+
let turns = 0;
|
|
143
|
+
let lastTs = null;
|
|
144
|
+
let newestAssistant = null; // whole branch
|
|
145
|
+
for (let i = 0; i < branch.length; i++) {
|
|
146
|
+
const entry = branch[i];
|
|
147
|
+
if (entry.type === 'model_change') {
|
|
148
|
+
if (typeof entry.modelId === 'string') model = entry.modelId;
|
|
149
|
+
if (typeof entry.provider === 'string') modelProvider = entry.provider;
|
|
150
|
+
}
|
|
151
|
+
if (entry.type === 'compaction') lastCompaction = i;
|
|
152
|
+
if (entry.type === 'message' && entry.message) {
|
|
153
|
+
const message = entry.message;
|
|
154
|
+
if (message.role === 'assistant') {
|
|
155
|
+
turns++;
|
|
156
|
+
newestAssistant = message; // branch runs root→leaf, so the last wins
|
|
157
|
+
}
|
|
158
|
+
const ms = Number(message.timestamp);
|
|
159
|
+
if (Number.isFinite(ms) && ms > lastTs) lastTs = ms;
|
|
160
|
+
else {
|
|
161
|
+
const iso = Date.parse(entry.timestamp);
|
|
162
|
+
if (Number.isFinite(iso) && iso > lastTs) lastTs = iso;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (model === null && typeof newestAssistant?.model === 'string') model = newestAssistant.model;
|
|
167
|
+
if (modelProvider === null && typeof newestAssistant?.provider === 'string') modelProvider = newestAssistant.provider;
|
|
168
|
+
|
|
169
|
+
// Context is measured after the last compaction — the newest assistant
|
|
170
|
+
// message from there on; with no compaction the newest assistant overall.
|
|
171
|
+
let contextUsage = null;
|
|
172
|
+
if (lastCompaction >= 0) {
|
|
173
|
+
for (let i = branch.length - 1; i > lastCompaction; i--) {
|
|
174
|
+
const message = branch[i].message;
|
|
175
|
+
if (branch[i].type === 'message' && message?.role === 'assistant' && message.usage) {
|
|
176
|
+
contextUsage = message.usage;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} else if (newestAssistant?.usage) contextUsage = newestAssistant.usage;
|
|
181
|
+
const contextTokens = contextUsage ? contextTokensOf(contextUsage) : null;
|
|
182
|
+
|
|
183
|
+
// Session totals over the window: assistant and toolResult message usage,
|
|
184
|
+
// plus `usage`, `branch_summary` and `compaction` entry usage (Pi's own
|
|
185
|
+
// getSessionStats aggregation). A windowed sum is a lower bound.
|
|
186
|
+
let totalTokens = 0;
|
|
187
|
+
let costUsd = 0;
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
let usage = null;
|
|
190
|
+
if (entry.type === 'usage') usage = entry.usage;
|
|
191
|
+
else if (entry.type === 'branch_summary' || entry.type === 'compaction') usage = entry.usage;
|
|
192
|
+
else if (entry.type === 'message') {
|
|
193
|
+
const role = entry.message?.role;
|
|
194
|
+
if (role === 'assistant' || role === 'toolResult') usage = entry.message.usage;
|
|
195
|
+
}
|
|
196
|
+
if (!usage) continue;
|
|
197
|
+
totalTokens += Number.isFinite(usage.totalTokens)
|
|
198
|
+
? usage.totalTokens
|
|
199
|
+
: finite(usage.input) + finite(usage.output) + finite(usage.cacheRead) + finite(usage.cacheWrite);
|
|
200
|
+
costUsd += finite(usage.cost?.total);
|
|
201
|
+
}
|
|
202
|
+
if (lastTs === null) {
|
|
203
|
+
const headerTs = Date.parse(session.timestamp);
|
|
204
|
+
if (Number.isFinite(headerTs)) lastTs = headerTs;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
sessionId,
|
|
209
|
+
...(model !== null ? { model } : {}),
|
|
210
|
+
...(modelProvider !== null ? { modelProvider } : {}),
|
|
211
|
+
...(contextTokens !== null ? { contextTokens } : {}),
|
|
212
|
+
usageTotal: { totalTokens, costUsd },
|
|
213
|
+
usageCoversSession: !windows.windowed,
|
|
214
|
+
turns,
|
|
215
|
+
lastTs,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Usage roll-up over the experimental Pi receipt journal (see experiments/pi-receipt).
|
|
2
|
+
// Aggregates every workspace scope into daily buckets and per-principal spend —
|
|
3
|
+
// the host-local twin of receipt search: no bodies, no session files, just counts.
|
|
4
|
+
// Fail-closed like the other journal readers: an unreadable base yields a zeroed
|
|
5
|
+
// roll-up, never a throw; corrupt entries are already filtered by readJournal.
|
|
6
|
+
|
|
7
|
+
import { readdirSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { readJournal } from './pi-receipts.mjs';
|
|
10
|
+
|
|
11
|
+
const SCOPE = /^[0-9a-f]{16}$/; // one hashed dir per Pi workspace
|
|
12
|
+
const RECEIPTS = 'receipts.jsonl';
|
|
13
|
+
const HUMAN = '(human)'; // principal absent → a human typed it in a pane
|
|
14
|
+
|
|
15
|
+
const newTotal = () => ({ requests: 0, replied: 0, failed: 0, canceled: 0, unknown: 0, tokens: 0, costUsd: 0 });
|
|
16
|
+
const newBucket = extra => ({ ...extra, requests: 0, replied: 0, failed: 0, tokens: 0, costUsd: 0 });
|
|
17
|
+
|
|
18
|
+
/** Fold one receipt into a bucket; status keys exist only where the shape has them. */
|
|
19
|
+
function fold(bucket, entry) {
|
|
20
|
+
bucket.requests += 1;
|
|
21
|
+
if (bucket[entry.status] !== undefined) bucket[entry.status] += 1;
|
|
22
|
+
bucket.tokens += entry.usage?.totalTokens ?? 0; // missing usage counts as zero
|
|
23
|
+
bucket.costUsd += entry.usage?.costUsd ?? 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const pad2 = n => String(n).padStart(2, '0');
|
|
27
|
+
const localDate = ts => {
|
|
28
|
+
const d = new Date(ts);
|
|
29
|
+
return `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Roll receipts from every workspace scope under base into one summary: totals,
|
|
34
|
+
* `days` daily buckets (newest first, local dates) and per-principal totals
|
|
35
|
+
* sorted by spend. Duplicate receipt ids collapse to the newest ts — compaction
|
|
36
|
+
* already dedupes within one scope, but two scopes may have seen the same
|
|
37
|
+
* request. Never throws; an unreadable base returns the zeroed shape.
|
|
38
|
+
*/
|
|
39
|
+
export function usageRollup({ base, days = 7, now = Date.now() } = {}) {
|
|
40
|
+
const at = Number.isFinite(now) ? now : Date.now();
|
|
41
|
+
const span = Number.isFinite(days) ? Math.max(0, Math.floor(days)) : 7;
|
|
42
|
+
|
|
43
|
+
// Local-day keys, newest first; stepping through the Date constructor so a
|
|
44
|
+
// DST shift inside the window cannot split or duplicate a calendar day.
|
|
45
|
+
const anchor = new Date(at);
|
|
46
|
+
const dayBuckets = new Map();
|
|
47
|
+
for (let back = 0; back < span; back++) {
|
|
48
|
+
const key = localDate(new Date(anchor.getFullYear(), anchor.getMonth(), anchor.getDate() - back).getTime());
|
|
49
|
+
dayBuckets.set(key, newBucket({ date: key }));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const latest = new Map(); // id -> entry; newest ts wins, ties keep the last seen
|
|
53
|
+
try {
|
|
54
|
+
for (const name of readdirSync(base)) {
|
|
55
|
+
if (!SCOPE.test(name)) continue;
|
|
56
|
+
let entries;
|
|
57
|
+
try { entries = readJournal(join(base, name, RECEIPTS)); } catch { continue; }
|
|
58
|
+
for (const entry of entries) {
|
|
59
|
+
const prior = latest.get(entry.id);
|
|
60
|
+
if (prior === undefined || prior.ts <= entry.ts) latest.set(entry.id, entry);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch { /* unreadable base: fall through to the zeroed shape */ }
|
|
64
|
+
|
|
65
|
+
const total = newTotal();
|
|
66
|
+
const principals = new Map();
|
|
67
|
+
for (const entry of latest.values()) {
|
|
68
|
+
fold(total, entry);
|
|
69
|
+
const day = dayBuckets.get(localDate(entry.ts)); // outside the window: totals only
|
|
70
|
+
if (day) fold(day, entry);
|
|
71
|
+
const key = entry.principal ?? HUMAN;
|
|
72
|
+
let bucket = principals.get(key);
|
|
73
|
+
if (!bucket) { bucket = newBucket({ principal: key, lastTs: entry.ts }); principals.set(key, bucket); }
|
|
74
|
+
fold(bucket, entry);
|
|
75
|
+
if (entry.ts > bucket.lastTs) bucket.lastTs = entry.ts;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
generatedAt: at,
|
|
80
|
+
total,
|
|
81
|
+
days: [...dayBuckets.values()], // insertion order == newest first
|
|
82
|
+
byPrincipal: [...principals.values()].sort((a, b) => b.costUsd - a.costUsd),
|
|
83
|
+
};
|
|
84
|
+
}
|