@bom0792/herdgate 0.9.37 → 0.9.39
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 +65 -0
- package/DESIGN.md +28 -0
- package/README.md +70 -1
- package/package.json +2 -1
- package/plugin/herdr-plugin.toml +18 -1
- package/plugin/server/attachments.mjs +184 -0
- package/plugin/server/git-safe.mjs +126 -0
- package/plugin/server/index.mjs +332 -9
- package/plugin/server/mobile-tui-core.mjs +141 -45
- package/plugin/server/mobile-tui-stream.mjs +258 -1
- package/plugin/server/pi-receipt-gc.mjs +183 -0
- package/plugin/server/pi-receipt-search.mjs +217 -0
- package/plugin/server/pi-receipts.mjs +121 -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.mjs +16 -3
- package/plugin/server/port-attr.mjs +172 -0
- package/plugin/server/publish-tui.mjs +7 -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/host/main.ts +14 -1
- package/src/web/app.css +356 -0
- package/src/web/app.js +1395 -14
- package/src/web/index.html +1 -0
- package/windows/npm-update.mjs +261 -11
- package/windows/update-policy.mjs +98 -0
package/plugin/server/index.mjs
CHANGED
|
@@ -6,21 +6,33 @@
|
|
|
6
6
|
* permissions are checked before dispatching any interactive bridge commands.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { spawn } from "node:child_process";
|
|
9
|
+
import { execFile as execFileCb, spawn } from "node:child_process";
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { promisify } from "node:util";
|
|
11
12
|
import { existsSync, readFileSync, writeFileSync, mkdirSync, rmSync } from "node:fs";
|
|
12
13
|
import { join, dirname } from "node:path";
|
|
13
14
|
import { hostname, homedir } from "node:os";
|
|
14
15
|
import { fileURLToPath } from "node:url";
|
|
15
16
|
import { dirs, readConfig, writeConfig, readState, writeState, herdrSnapshot, summarize, herdrPanes, herdrRead } from "./common.mjs";
|
|
17
|
+
import { gitRead, diffRepoDir } from "./git-safe.mjs";
|
|
16
18
|
import { createSshBridge } from "./ssh.mjs";
|
|
17
19
|
import { findBody, findReceipt, journalBase as receiptJournalBase, publicReceipt, watchReceipts } from "./pi-receipts.mjs";
|
|
20
|
+
import { listAttachments, saveAttachment } from "./attachments.mjs";
|
|
21
|
+
import { searchReceipts, sessionRoots as piSessionRoots, findSessionFile } from "./pi-receipt-search.mjs";
|
|
22
|
+
import { usageRollup } from "./pi-usage.mjs";
|
|
23
|
+
import { readAllSpawns } from "./pi-wake.mjs";
|
|
24
|
+
import { sysSnapshot } from "./sysmon.mjs";
|
|
25
|
+
import { attributePorts } from "./port-attr.mjs";
|
|
26
|
+
import { parsePiTranscript } from "./pi-transcript.mjs";
|
|
18
27
|
import { openWatchStore } from "./pi-receipt-watch.mjs";
|
|
19
28
|
import { JOB_ID, MAX_PENDING, MAX_PROMPT, WORKSPACE_NAME, appendJob, jobsFile, publicJob, queueDepth, readHeartbeat, readJobs, workerBase } from "./pi-worker.mjs";
|
|
20
29
|
import { registerPublisher } from "./publisher-tui-lifecycle.mjs";
|
|
21
30
|
import { HostChannel, b64d, b64e, fingerprint, generateKeyPair } from "./e2e.mjs";
|
|
22
31
|
|
|
23
32
|
const D = dirs();
|
|
33
|
+
const execFile = promisify(execFileCb);
|
|
34
|
+
/** diff.get ships at most 128 KiB of `git show` output, marked truncated beyond. */
|
|
35
|
+
const MAX_DIFF_BYTES = 128 * 1024;
|
|
24
36
|
const NAME = process.env.HERDGATE_HOST_NAME || hostname();
|
|
25
37
|
const log = (line) => console.error(`herdgate: ${line}`);
|
|
26
38
|
/** The panel's settings file (app home herdgate dir) — the owner's switches live
|
|
@@ -116,6 +128,84 @@ function runningPid() {
|
|
|
116
128
|
}
|
|
117
129
|
}
|
|
118
130
|
|
|
131
|
+
/** --- Wave 2 observer wiring: sysmon / port-attr / pi-transcript behind ops --- */
|
|
132
|
+
|
|
133
|
+
/** Raw `herdr pane list` rows, every field kept. common.mjs's herdrPanes is the
|
|
134
|
+
* PWA picker view and drops fields on purpose; the observers need `pid`, which
|
|
135
|
+
* a Herdr build may or may not expose — "no pid column" must stay
|
|
136
|
+
* distinguishable from "no live children". Returns null when the CLI fails. */
|
|
137
|
+
async function paneRowsWithPids() {
|
|
138
|
+
try {
|
|
139
|
+
const { stdout } = await execFile(D.herdrBin, ["pane", "list"], { timeout: 8_000, maxBuffer: 8 << 20 });
|
|
140
|
+
const parsed = JSON.parse(stdout);
|
|
141
|
+
const panes = parsed?.result?.panes ?? parsed?.panes ?? [];
|
|
142
|
+
return Array.isArray(panes) ? panes.filter((p) => p && typeof p.pane_id === "string") : [];
|
|
143
|
+
} catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Every spawn-registry record across scopes; unreadable registry → [] (the
|
|
149
|
+
* observers fail closed, they never take the host down). */
|
|
150
|
+
function spawnRecords() {
|
|
151
|
+
try {
|
|
152
|
+
return readAllSpawns(receiptJournalBase());
|
|
153
|
+
} catch {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Live spawn children joined with their pane rows: one row per live child,
|
|
159
|
+
* newest record per id. The registry stores pane ids, never pids — a pid
|
|
160
|
+
* exists only when this Herdr's pane list carries one. */
|
|
161
|
+
function liveSpawnChildren(panes) {
|
|
162
|
+
const byId = new Map();
|
|
163
|
+
for (const record of spawnRecords()) {
|
|
164
|
+
const prior = byId.get(record.id);
|
|
165
|
+
if (!prior || (record.ts ?? 0) >= (prior.ts ?? 0)) byId.set(record.id, record);
|
|
166
|
+
}
|
|
167
|
+
const byPane = new Map(panes.map((p) => [p.pane_id, p]));
|
|
168
|
+
const rows = [];
|
|
169
|
+
for (const record of byId.values()) {
|
|
170
|
+
const pane = record.paneId ? byPane.get(record.paneId) : undefined;
|
|
171
|
+
if (!pane) continue; // pane gone → child not live
|
|
172
|
+
const pid = Number(pane.pid);
|
|
173
|
+
rows.push({ key: record.name, paneId: record.paneId, pid: Number.isInteger(pid) && pid > 0 ? pid : null });
|
|
174
|
+
}
|
|
175
|
+
return rows;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The transcript behind one receipt: the spawn record's own sessionFile when
|
|
179
|
+
* it exists (records may predate the field), else Pi's standard session roots.
|
|
180
|
+
* Returns null when nothing is found — the caller answers a clear error op. */
|
|
181
|
+
function transcriptFor(entry) {
|
|
182
|
+
const record = spawnRecords()
|
|
183
|
+
.filter((s) => s.sessionId === entry.sessionId && typeof s.sessionFile === "string")
|
|
184
|
+
.sort((a, b) => (b.ts ?? 0) - (a.ts ?? 0))[0];
|
|
185
|
+
if (record && existsSync(record.sessionFile)) return record.sessionFile;
|
|
186
|
+
return findSessionFile(piSessionRoots(), entry.sessionId);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Which Herdr argv focuses a pane — probed once from --help (pane focus, else the
|
|
190
|
+
* tab equivalent), null when this Herdr supports neither. Never throws. */
|
|
191
|
+
let paneFocusArgv;
|
|
192
|
+
async function paneFocusArgvProbe() {
|
|
193
|
+
if (paneFocusArgv !== undefined) return paneFocusArgv;
|
|
194
|
+
const help = async (...args) => {
|
|
195
|
+
try {
|
|
196
|
+
const { stdout } = await execFile(D.herdrBin, args, { timeout: 5_000 });
|
|
197
|
+
return stdout;
|
|
198
|
+
} catch {
|
|
199
|
+
return "";
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
if (/\bfocus\b/.test(await help("pane", "--help"))) return (paneFocusArgv = ["pane", "focus"]);
|
|
203
|
+
const tabHelp = await help("tab", "--help");
|
|
204
|
+
if (/\bfocus\b/.test(tabHelp)) return (paneFocusArgv = ["tab", "focus"]);
|
|
205
|
+
if (/\bselect\b/.test(tabHelp)) return (paneFocusArgv = ["tab", "select"]);
|
|
206
|
+
return (paneFocusArgv = null);
|
|
207
|
+
}
|
|
208
|
+
|
|
119
209
|
/** --- serve: the startup hook. Start the publisher once, then exit (Herdr's hook is one-shot). --- */
|
|
120
210
|
async function serve() {
|
|
121
211
|
if (process.env.HERDGATE_PUBLISHER_CHILD === "1") return publisherLoop(); // the child
|
|
@@ -172,6 +262,15 @@ async function publisherLoop() {
|
|
|
172
262
|
let lastSig = "";
|
|
173
263
|
let devices = [];
|
|
174
264
|
let timer = null;
|
|
265
|
+
// Push cadence and view-request freshness. The 30s default is gentle on a
|
|
266
|
+
// battery-powered host; a plugged-in one sets HERDGATE_PUBLISH_MS=5000 so a
|
|
267
|
+
// phone sees a settle within seconds instead of half a minute. A `view` op
|
|
268
|
+
// inside the freshness window replays the last snapshot instead of spawning
|
|
269
|
+
// `herdr api snapshot` again (bimodal view latency: 4ms cached vs 150ms cold).
|
|
270
|
+
const PUBLISH_MS = Math.max(2_000, Number.parseInt(process.env.HERDGATE_PUBLISH_MS ?? "30000", 10) || 30000);
|
|
271
|
+
const SNAPSHOT_FRESH_MS = Math.max(0, Number.parseInt(process.env.HERDGATE_SNAPSHOT_FRESH_MS ?? "2000", 10) || 0);
|
|
272
|
+
let snapshotInflight = null;
|
|
273
|
+
let snapshotCached = null; // { payload, at }
|
|
175
274
|
|
|
176
275
|
const snapshotPayload = async () => {
|
|
177
276
|
let snap;
|
|
@@ -190,7 +289,18 @@ async function publisherLoop() {
|
|
|
190
289
|
writeState(D.statePath, { lastPublish: { at: new Date().toISOString(), summary } });
|
|
191
290
|
}
|
|
192
291
|
if (changed) wakeAway(true);
|
|
193
|
-
|
|
292
|
+
const payload = { op: "view", view: { v: 1, source: "herdr", takenAt: new Date().toISOString(), summary } };
|
|
293
|
+
snapshotCached = { payload, at: Date.now() };
|
|
294
|
+
return payload;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
/** Single-flight: concurrent demands (interval tick + a device's view op)
|
|
298
|
+
* share one `herdr api snapshot` spawn instead of stampeding. */
|
|
299
|
+
const snapshotOnce = () => {
|
|
300
|
+
if (!snapshotInflight) {
|
|
301
|
+
snapshotInflight = snapshotPayload().finally(() => { snapshotInflight = null; });
|
|
302
|
+
}
|
|
303
|
+
return snapshotInflight;
|
|
194
304
|
};
|
|
195
305
|
|
|
196
306
|
let wsRef = null;
|
|
@@ -290,7 +400,7 @@ async function publisherLoop() {
|
|
|
290
400
|
});
|
|
291
401
|
const publish = async (ws) => {
|
|
292
402
|
if (ws.readyState !== 1 || devices.length === 0) return;
|
|
293
|
-
const payload = await
|
|
403
|
+
const payload = await snapshotOnce();
|
|
294
404
|
if (!payload) return;
|
|
295
405
|
for (const d of devices) {
|
|
296
406
|
await sendPayload(ws, d.id, payload);
|
|
@@ -379,7 +489,7 @@ async function publisherLoop() {
|
|
|
379
489
|
// Advertise honestly: `act` only when the owner's switch allows it.
|
|
380
490
|
let allowAct = false;
|
|
381
491
|
try { allowAct = (JSON.parse(readFileSync(SETTINGS, "utf8")) ?? {}).allowAct === true; } catch { /* no settings yet */ }
|
|
382
|
-
ws.send(JSON.stringify({ v: 1, t: "hello", roomToken: token, info: { name: NAME, version: "0.
|
|
492
|
+
ws.send(JSON.stringify({ v: 1, t: "hello", roomToken: token, info: { name: NAME, version: "0.9.38", capabilities: ["dev.herdgate.host", allowAct ? "act" : "read-only"] } }));
|
|
383
493
|
};
|
|
384
494
|
ws.onmessage = (ev) => {
|
|
385
495
|
void (async () => {
|
|
@@ -395,15 +505,23 @@ async function publisherLoop() {
|
|
|
395
505
|
backoff = 1_000;
|
|
396
506
|
devices = m.devices ?? [];
|
|
397
507
|
lifecycle?.setConnected(true);
|
|
398
|
-
log(`connected to ${url}${loopbackPort ? ` via loopback 127.0.0.1:${loopbackPort}` : ""} (room ${m.roomId}, ${devices.length} device(s))`);
|
|
508
|
+
log(`connected to ${url}${loopbackPort ? ` via loopback 127.0.0.1:${loopbackPort}` : ""} (room ${m.roomId}, ${devices.length} device(s), publish ${PUBLISH_MS}ms)`);
|
|
399
509
|
if (process.env.HERDGATE_PI_RECEIPTS === "1" && !receiptWatch) {
|
|
400
|
-
|
|
510
|
+
// Settles push to subscribed devices only. Parent wakes no longer ride
|
|
511
|
+
// this watcher — the child-cooperative design (arm/marker/observer)
|
|
512
|
+
// missed three times live. Herdr now reports through the [[events]]
|
|
513
|
+
// hooks (server/wake-event.mjs): wake survives a child that never
|
|
514
|
+
// arms, and a pane that dies before settling still reports.
|
|
515
|
+
receiptWatch = watchReceipts(receiptJournalBase(), (entry) => {
|
|
516
|
+
pushReceipt(entry);
|
|
517
|
+
});
|
|
401
518
|
log(`receipts: journal push enabled (${receiptJournalBase()})`);
|
|
519
|
+
log(`wake: herdr event hooks (pane.agent_status_changed/exited/closed)`);
|
|
402
520
|
}
|
|
403
521
|
for (const d of devices) flushWatches(d.id);
|
|
404
522
|
void publish(ws);
|
|
405
523
|
if (timer) clearInterval(timer);
|
|
406
|
-
timer = setInterval(() => void publish(ws),
|
|
524
|
+
timer = setInterval(() => void publish(ws), PUBLISH_MS);
|
|
407
525
|
timer.unref?.();
|
|
408
526
|
return;
|
|
409
527
|
}
|
|
@@ -448,6 +566,118 @@ async function publisherLoop() {
|
|
|
448
566
|
return void replyTo({ op: "receipt.result", id: id || null,
|
|
449
567
|
...publicReceipt(entry), ...(enabled ? {} : { reason: "pi_receipts_disabled" }) });
|
|
450
568
|
}
|
|
569
|
+
if (op === "receipt.search") {
|
|
570
|
+
// Experimental (opt-in HERDGATE_PI_RECEIPTS=1): BM25 keyword search over
|
|
571
|
+
// the journal; bodies and transcripts score host-side only. Hits are
|
|
572
|
+
// publicReceipt-shaped (id, score, status, reason, usage) — no snippet,
|
|
573
|
+
// no sessionId, no session file, no timestamps. Search finds ids; the
|
|
574
|
+
// gated receipt.body op serves any text.
|
|
575
|
+
const enabled = process.env.HERDGATE_PI_RECEIPTS === "1";
|
|
576
|
+
const query = typeof payload.query === "string" ? payload.query.slice(0, 200) : "";
|
|
577
|
+
const limit = Number(payload.limit);
|
|
578
|
+
const hits = enabled && query.trim()
|
|
579
|
+
? searchReceipts({ base: receiptJournalBase(), sessionsRoot: piSessionRoots(), query,
|
|
580
|
+
limit: Number.isFinite(limit) ? limit : 10, all: payload.all === true })
|
|
581
|
+
: [];
|
|
582
|
+
log(`receipts: ${m.device} search ${JSON.stringify(query)} -> ${hits.length} hit(s)`);
|
|
583
|
+
return void replyTo({ op: "receipt.search.result",
|
|
584
|
+
...(enabled ? { hits } : { hits: [], reason: "pi_receipts_disabled" }) });
|
|
585
|
+
}
|
|
586
|
+
if (op === "usage.rollup") {
|
|
587
|
+
// The PWA's Timeline/Usage views: the receipt journal rolled into daily
|
|
588
|
+
// buckets and per-principal spend (the raw usageRollup shape). Aggregates
|
|
589
|
+
// only and usageRollup fails closed to a zeroed result, so read scope is
|
|
590
|
+
// enough and a missing journal answers zeros, never an error.
|
|
591
|
+
const n = Number(payload.days);
|
|
592
|
+
const days = Number.isFinite(n) ? Math.min(31, Math.max(1, Math.floor(n))) : 7;
|
|
593
|
+
const rollup = usageRollup({ base: receiptJournalBase(), days });
|
|
594
|
+
log(`usage: ${m.device} roll-up ${days}d -> ${rollup.total.requests} request(s), ${rollup.total.tokens} token(s)`);
|
|
595
|
+
return void replyTo({ op: "usage.rollup.result", ...rollup });
|
|
596
|
+
}
|
|
597
|
+
if (op === "sysmon.snapshot") {
|
|
598
|
+
// READ-ONLY machine observation (sysmon.mjs): CPU/RAM per live spawn
|
|
599
|
+
// child. Roots are the registry's live panes joined with pane rows —
|
|
600
|
+
// the registry stores no pids, so roots exist only when herdr's pane
|
|
601
|
+
// list carries a pid; without pids the answer is [] plus a note, never
|
|
602
|
+
// a guess. The sampler itself fails closed to []. Subprocess-backed
|
|
603
|
+
// (one pane list), so throttled like term.*.
|
|
604
|
+
const now = Date.now();
|
|
605
|
+
const last = termThrottle.get(`sysmon:${m.device}`) ?? 0;
|
|
606
|
+
if (now - last < 1_000) return void replyTo({ op: "error", code: "busy", message: "sysmon.snapshot is throttled (1/s)" });
|
|
607
|
+
termThrottle.set(`sysmon:${m.device}`, now);
|
|
608
|
+
const panes = await paneRowsWithPids();
|
|
609
|
+
let note = null;
|
|
610
|
+
let roots = [];
|
|
611
|
+
if (panes === null) {
|
|
612
|
+
note = "herdr pane list failed — no live child could be resolved";
|
|
613
|
+
} else {
|
|
614
|
+
const withPid = liveSpawnChildren(panes).filter((row) => row.pid);
|
|
615
|
+
if (withPid.length === 0) {
|
|
616
|
+
note = panes.some((p) => Number.isInteger(Number(p.pid)) && Number(p.pid) > 0)
|
|
617
|
+
? "no live spawn child exposes a pid — nothing to sample"
|
|
618
|
+
: "herdr pane list carries no pid field — CPU/RAM roots unavailable";
|
|
619
|
+
} else {
|
|
620
|
+
roots = withPid.map((row) => ({ pid: row.pid, key: row.key }));
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const agents = sysSnapshot(roots);
|
|
624
|
+
log(`sysmon: ${m.device} snapshot -> ${agents.length} agent row(s)${note ? ` (${note})` : ""}`);
|
|
625
|
+
return void replyTo({ op: "sysmon.snapshot.result", at: new Date().toISOString(), agents, ...(note ? { note } : {}) });
|
|
626
|
+
}
|
|
627
|
+
if (op === "ports.list") {
|
|
628
|
+
// READ-ONLY listener attribution (port-attr.mjs): every LISTEN socket,
|
|
629
|
+
// attributed to the live spawn child it descends from. Without shell
|
|
630
|
+
// pids nothing can be attributed — the module answers the unattributed
|
|
631
|
+
// list and a note says why; an owner is never invented. Throttled 1/s.
|
|
632
|
+
const now = Date.now();
|
|
633
|
+
const last = termThrottle.get(`ports:${m.device}`) ?? 0;
|
|
634
|
+
if (now - last < 1_000) return void replyTo({ op: "error", code: "busy", message: "ports.list is throttled (1/s)" });
|
|
635
|
+
termThrottle.set(`ports:${m.device}`, now);
|
|
636
|
+
const panes = await paneRowsWithPids();
|
|
637
|
+
let shellPids = [];
|
|
638
|
+
let note = null;
|
|
639
|
+
if (panes === null) {
|
|
640
|
+
note = "herdr pane list failed — every listener stays unattributed";
|
|
641
|
+
} else {
|
|
642
|
+
shellPids = liveSpawnChildren(panes).filter((row) => row.pid).map((row) => ({ pid: row.pid, key: row.key }));
|
|
643
|
+
if (shellPids.length === 0) {
|
|
644
|
+
note = panes.some((p) => Number.isInteger(Number(p.pid)) && Number(p.pid) > 0)
|
|
645
|
+
? "no live spawn child exposes a pid — every listener stays unattributed"
|
|
646
|
+
: "herdr pane list carries no pid field — every listener stays unattributed";
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
const { attributed, unattributed } = attributePorts(shellPids);
|
|
650
|
+
log(`ports: ${m.device} list -> ${attributed.length} attributed, ${unattributed.length} unattributed${note ? ` (${note})` : ""}`);
|
|
651
|
+
return void replyTo({ op: "ports.list.result", attributed, unattributed, ...(note ? { note } : {}) });
|
|
652
|
+
}
|
|
653
|
+
if (op === "receipt.enrich") {
|
|
654
|
+
// Experimental (opt-in HERDGATE_PI_RECEIPTS=1): transcript facts for one
|
|
655
|
+
// receipt — model, context size, turns, usage totals, last activity —
|
|
656
|
+
// parsed from the session transcript of the spawn registry record
|
|
657
|
+
// matching the receipt's sessionId (Pi's session roots as fallback).
|
|
658
|
+
// Metadata only; a missing or unreadable transcript is a clear error
|
|
659
|
+
// op, never a throw and never a guessed number.
|
|
660
|
+
const id = typeof payload.id === "string" && JOB_ID.test(payload.id) ? payload.id : "";
|
|
661
|
+
if (!receiptsEnabled()) {
|
|
662
|
+
return void replyTo({ op: "receipt.enrich.result", id: id || null, reason: "pi_receipts_disabled" });
|
|
663
|
+
}
|
|
664
|
+
if (!id) return void replyTo({ op: "error", code: "receipt.enrich", message: "receipt.enrich needs a request id" });
|
|
665
|
+
const entry = findReceipt(receiptJournalBase(), id);
|
|
666
|
+
if (!entry) {
|
|
667
|
+
log(`receipts: ${m.device} enrich ${id} -> no receipt`);
|
|
668
|
+
return void replyTo({ op: "error", code: "receipt.enrich", message: `no receipt ${id} in the journal` });
|
|
669
|
+
}
|
|
670
|
+
const file = transcriptFor(entry);
|
|
671
|
+
const parsed = file ? parsePiTranscript(file) : null;
|
|
672
|
+
if (!parsed || parsed.error) {
|
|
673
|
+
log(`receipts: ${m.device} enrich ${id} -> no transcript (session ${entry.sessionId})`);
|
|
674
|
+
return void replyTo({ op: "error", code: "receipt.enrich", message: `no readable transcript for session ${entry.sessionId}` });
|
|
675
|
+
}
|
|
676
|
+
log(`receipts: ${m.device} enrich ${id} -> ${parsed.model ?? "?"}, ${parsed.turns} turn(s), ${parsed.usageTotal.totalTokens} token(s)`);
|
|
677
|
+
return void replyTo({ op: "receipt.enrich.result", id, sessionId: entry.sessionId,
|
|
678
|
+
model: parsed.model ?? null, contextTokens: parsed.contextTokens ?? null,
|
|
679
|
+
turns: parsed.turns, usageTotal: parsed.usageTotal, lastTs: parsed.lastTs });
|
|
680
|
+
}
|
|
451
681
|
if (op === "receipt.watch" || op === "receipt.ack") {
|
|
452
682
|
// Experimental (opt-in HERDGATE_PI_RECEIPTS=1): durable per-request push
|
|
453
683
|
// subscriptions. Registering targets receipt.push at this device only;
|
|
@@ -554,7 +784,7 @@ async function publisherLoop() {
|
|
|
554
784
|
return void replyTo({ op: "error", code: "queue-full", message: `worker queue is full (${MAX_PENDING} pending)` });
|
|
555
785
|
}
|
|
556
786
|
const jobId = id || randomUUID();
|
|
557
|
-
try { appendJob(file, { v: 1, id: jobId, prompt, ts: Date.now(), state: "queued",
|
|
787
|
+
try { appendJob(file, { v: 1, id: jobId, prompt, ts: Date.now(), state: "queued", principal: `device:${m.device}`,
|
|
558
788
|
...(workspaceName !== undefined ? { workspace: workspaceName } : {}) }); }
|
|
559
789
|
catch (e) {
|
|
560
790
|
return void replyTo({ op: "error", code: "worker-queue", message: `queue append failed: ${e instanceof Error ? e.message : String(e)}` });
|
|
@@ -562,8 +792,55 @@ async function publisherLoop() {
|
|
|
562
792
|
log(`worker: ${m.device} submitted ${jobId} (${prompt.length} chars${workspaceName ? `, ws ${workspaceName}` : ""})`);
|
|
563
793
|
return void replyTo({ op: "worker.submitted", id: jobId, state: "queued", duplicate: false });
|
|
564
794
|
}
|
|
795
|
+
if (op === "attachment.upload" || op === "attachment.list") {
|
|
796
|
+
// Wave C3 — photo/file attachments from the PWA composer (the herdcats
|
|
797
|
+
// lesson: the upload lands ON THE AGENT MACHINE under the plugin's
|
|
798
|
+
// private state dir; the reply carries a path hint, and the Pi agent
|
|
799
|
+
// opens the file with its own read tool — image bytes never ride the
|
|
800
|
+
// prompt). Client names are never trusted: saveAttachment stores
|
|
801
|
+
// <uuid>.<ext> with the ext sniffed from magic bytes, inside
|
|
802
|
+
// stateDir/attachments/<yyyy-mm>/ (0700 per level, file 0600).
|
|
803
|
+
if (!D.stateDir) {
|
|
804
|
+
return void replyTo({ op: "error", code: "attachment", message: "no plugin state dir — attachments unavailable" });
|
|
805
|
+
}
|
|
806
|
+
if (op === "attachment.list") {
|
|
807
|
+
const attachments = listAttachments(D.stateDir);
|
|
808
|
+
log(`attachment: ${m.device} list -> ${attachments.length} file(s)`);
|
|
809
|
+
return void replyTo({ op: "attachment.list.result", attachments });
|
|
810
|
+
}
|
|
811
|
+
const name = typeof payload.name === "string" ? payload.name.trim() : "";
|
|
812
|
+
const dataBase64 = typeof payload.dataBase64 === "string" ? payload.dataBase64 : "";
|
|
813
|
+
if (!name || name.length > 255 || !dataBase64) {
|
|
814
|
+
return void replyTo({ op: "error", code: "attachment", message: "attachment.upload needs a name and base64 data" });
|
|
815
|
+
}
|
|
816
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(dataBase64) || dataBase64.length % 4 !== 0) {
|
|
817
|
+
log(`attachment: ${m.device} upload refused: bad base64`);
|
|
818
|
+
return void replyTo({ op: "error", code: "attachment", message: "attachment.upload data is not valid base64" });
|
|
819
|
+
}
|
|
820
|
+
let bytes;
|
|
821
|
+
try { bytes = Buffer.from(dataBase64, "base64"); }
|
|
822
|
+
catch (e) {
|
|
823
|
+
return void replyTo({ op: "error", code: "attachment", message: `attachment.upload data is not valid base64 (${e.code ?? "decode"})` });
|
|
824
|
+
}
|
|
825
|
+
try {
|
|
826
|
+
const saved = saveAttachment(D.stateDir, { name, bytes });
|
|
827
|
+
log(`attachment: ${m.device} uploaded ${name} (${bytes.length}B) -> ${saved.id}`);
|
|
828
|
+
return void replyTo({ op: "attachment.uploaded", id: saved.id, path: saved.path,
|
|
829
|
+
hint: `Please open and inspect this image: ${saved.path}` });
|
|
830
|
+
} catch (e) {
|
|
831
|
+
const why = e instanceof Error ? e.message : String(e);
|
|
832
|
+
log(`attachment: ${m.device} upload refused: ${why}`);
|
|
833
|
+
return void replyTo({ op: "error", code: "attachment", message: `attachment.upload failed: ${why}` });
|
|
834
|
+
}
|
|
835
|
+
}
|
|
565
836
|
if (op === "view") {
|
|
566
|
-
// Answer this device now
|
|
837
|
+
// Answer this device now. Inside the freshness window replay the last
|
|
838
|
+
// snapshot (no spawn, no lastSig reset — so the next full tick cannot
|
|
839
|
+
// re-detect a "change" and wake away phones for nothing).
|
|
840
|
+
if (snapshotCached && Date.now() - snapshotCached.at < SNAPSHOT_FRESH_MS) {
|
|
841
|
+
for (const d of devices) await sendPayload(ws, d.id, snapshotCached.payload);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
567
844
|
lastSig = "";
|
|
568
845
|
return void publish(ws);
|
|
569
846
|
}
|
|
@@ -599,6 +876,52 @@ async function publisherLoop() {
|
|
|
599
876
|
return void replyTo({ op: "error", code: "herdr", message: `read failed: ${e instanceof Error ? e.message : String(e)}` });
|
|
600
877
|
}
|
|
601
878
|
}
|
|
879
|
+
if (op === "diff.get") {
|
|
880
|
+
// READ-ONLY code archeology: one `git show --stat --patch` of a host-local
|
|
881
|
+
// ref (hash/branch/tag) in the plugin's repo dir (HERDGATE_DIFF_REPO_DIR
|
|
882
|
+
// lets a test point elsewhere). The ref is whitelisted hard and the git
|
|
883
|
+
// call itself goes through gitRead — scrubbed env, no ext-diff/textconv,
|
|
884
|
+
// fsmonitor/hooks neutralized, no payload-chosen cwd — so hostile repo
|
|
885
|
+
// config can never execute through this op.
|
|
886
|
+
const ref = typeof payload.ref === "string" ? payload.ref : "";
|
|
887
|
+
if (!/^[A-Za-z0-9._/~^-]{1,64}$/.test(ref)) {
|
|
888
|
+
return void replyTo({ op: "error", code: "bad-ref", message: "diff.get needs a git ref of [A-Za-z0-9._/-], 1..64 chars" });
|
|
889
|
+
}
|
|
890
|
+
const repoDir = diffRepoDir();
|
|
891
|
+
try {
|
|
892
|
+
const { stdout } = await gitRead(["show", "--no-color", "--stat", "--patch", ref], repoDir, 10_000);
|
|
893
|
+
const bytes = Buffer.byteLength(stdout, "utf8");
|
|
894
|
+
const truncated = bytes > MAX_DIFF_BYTES;
|
|
895
|
+
const text = truncated ? Buffer.from(stdout, "utf8").subarray(0, MAX_DIFF_BYTES).toString("utf8") : stdout;
|
|
896
|
+
log(`diff: ${m.device} show ${ref} -> ${bytes}B${truncated ? " (capped at 128KiB)" : ""}`);
|
|
897
|
+
return void replyTo({ op: "diff.result", ref, text, bytes, truncated });
|
|
898
|
+
} catch (e) {
|
|
899
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
900
|
+
const stderr = String(e?.stderr ?? "");
|
|
901
|
+
const noRepo = /not a git repository/i.test(`${err.message}\n${stderr}`);
|
|
902
|
+
log(`diff: ${m.device} show ${ref} refused: ${err.message}`);
|
|
903
|
+
return void replyTo({ op: "error", code: noRepo ? "diff.repo" : "diff.ref",
|
|
904
|
+
message: noRepo ? "the plugin directory is not a git repository"
|
|
905
|
+
: stderr.split("\n")[0] || err.message });
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
if (op === "pane.focus") {
|
|
909
|
+
// Focus a pane on the host's Herdr. Fire-and-forget spawn, never awaited:
|
|
910
|
+
// a phone must not hang on a TUI command. argv comes from the --help probe.
|
|
911
|
+
const paneId = typeof payload.paneId === "string" ? payload.paneId : "";
|
|
912
|
+
if (!/^w\d+:p\d+$/.test(paneId)) {
|
|
913
|
+
return void replyTo({ op: "error", code: "bad-pane", message: "pane.focus needs a pane id like w0:p1" });
|
|
914
|
+
}
|
|
915
|
+
const argv = await paneFocusArgvProbe();
|
|
916
|
+
if (!argv) {
|
|
917
|
+
return void replyTo({ op: "error", code: "pane.focus", message: "this Herdr has no pane focus command" });
|
|
918
|
+
}
|
|
919
|
+
const child = spawn(D.herdrBin, [...argv, paneId], { stdio: "ignore", detached: true });
|
|
920
|
+
child.on("error", (e) => log(`pane: focus spawn failed: ${e.message}`));
|
|
921
|
+
child.unref();
|
|
922
|
+
log(`pane: ${m.device} focus ${paneId} via \`${argv.join(" ")}\``);
|
|
923
|
+
return void replyTo({ op: "pane.focused", paneId, via: argv.join(" ") });
|
|
924
|
+
}
|
|
602
925
|
return void replyTo({ op: "error", code: "read-only", message: `dev.herdgate.host has no op ${String(op)}` });
|
|
603
926
|
}
|
|
604
927
|
if (m.t === "pair.request") {
|