@bom0792/herdgate 0.9.35 → 0.9.37
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 +44 -5
- package/.agents/skills/herdr-advisor/SKILL.md +1 -1
- package/.agents/skills/herdr-committee/SKILL.md +1 -1
- package/.agents/skills/herdr-handoff/SKILL.md +1 -1
- package/bin/herdgate.mjs +5 -1
- package/package.json +1 -1
- package/plugin/server/e2e.mjs +27 -0
- package/plugin/server/index.mjs +106 -4
- package/plugin/server/mobile-tui-core.mjs +590 -26
- package/plugin/server/mobile-tui-stream.mjs +4 -2
- package/plugin/server/pi-receipt-watch.d.mts +24 -0
- package/plugin/server/pi-receipt-watch.mjs +99 -0
- package/plugin/server/pi-receipts.d.mts +12 -0
- package/plugin/server/pi-receipts.mjs +114 -3
- package/plugin/server/pi-worker.d.mts +7 -0
- package/plugin/server/pi-worker.mjs +29 -1
- package/src/cloud/gateway.ts +8 -8
- package/src/cloud/sshd.ts +14 -11
- package/src/cloud/util.ts +10 -0
- package/src/web/app.css +23 -0
- package/src/web/app.js +175 -4
- package/src/web/e2e.js +29 -2
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
// Native Herdr terminal controller. Never
|
|
1
|
+
// Native Herdr terminal controller. Never interprets a shell command. --takeover makes the
|
|
2
|
+
// latest viewer own the stream: Herdr allows one controller per terminal, and this way a
|
|
3
|
+
// reconnect or a second viewer (phone, desktop) replaces the old one instead of failing.
|
|
2
4
|
import { spawn } from "node:child_process";
|
|
3
5
|
|
|
4
6
|
const maxLine = 1024 * 1024;
|
|
5
7
|
|
|
6
8
|
export async function openTerminal(bin, id, cols, rows) {
|
|
7
|
-
const child = spawn(bin, ["terminal", "session", "control", id, "--cols", String(cols), "--rows", String(rows)], {
|
|
9
|
+
const child = spawn(bin, ["terminal", "session", "control", id, "--takeover", "--cols", String(cols), "--rows", String(rows)], {
|
|
8
10
|
stdio: ["pipe", "pipe", "ignore"], windowsHide: true, shell: false,
|
|
9
11
|
});
|
|
10
12
|
child.on("error", () => child.stdout.destroy());
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const WATCH_TTL_MS: number;
|
|
2
|
+
export const MAX_TTL_MS: number;
|
|
3
|
+
export const MAX_SUBS_PER_DEVICE: number;
|
|
4
|
+
|
|
5
|
+
export interface WatchSubscription {
|
|
6
|
+
device: string;
|
|
7
|
+
requestId: string;
|
|
8
|
+
wantBody: boolean;
|
|
9
|
+
createdAt: number;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
deliveredAt?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface WatchStore {
|
|
15
|
+
register(input: { device: string; requestId: string; wantBody?: boolean; ttlMs?: number }): WatchSubscription;
|
|
16
|
+
markDelivered(device: string, requestId: string): boolean;
|
|
17
|
+
ack(device: string, requestId: string): boolean;
|
|
18
|
+
pendingFor(device: string): WatchSubscription[];
|
|
19
|
+
pendingAll(): WatchSubscription[];
|
|
20
|
+
sweep(now?: number): number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function watchFile(base: string): string;
|
|
24
|
+
export function openWatchStore(base: string): WatchStore;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Durable receipt-push subscriptions (experimental; see experiments/pi-receipt).
|
|
2
|
+
// Lesson taken from Paseo's finish subscriptions: the notification intent is
|
|
3
|
+
// owned by the receiver's request id, persists across publisher restarts and
|
|
4
|
+
// device reconnects, and only an explicit acknowledgment retires it. Delivered
|
|
5
|
+
// but unacknowledged intents are re-offered on reconnect; nothing is resent to
|
|
6
|
+
// devices that did not register. Shared reader/writer for the publisher; no
|
|
7
|
+
// network access here. Same opt-in gate as receipt.get (HERDGATE_PI_RECEIPTS=1).
|
|
8
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { dirname, join } from 'node:path';
|
|
10
|
+
import { RECEIPT_ID } from './pi-receipts.mjs';
|
|
11
|
+
|
|
12
|
+
/** Matches the observer's arm TTL: a watch outlives the request it tracks. */
|
|
13
|
+
export const WATCH_TTL_MS = 10 * 60 * 1000;
|
|
14
|
+
export const MAX_TTL_MS = 24 * 60 * 60 * 1000;
|
|
15
|
+
export const MAX_SUBS_PER_DEVICE = 16;
|
|
16
|
+
|
|
17
|
+
/** Where the durable watch intents live, beside the receipt journals. */
|
|
18
|
+
export function watchFile(base) {
|
|
19
|
+
return join(base, 'watch.json');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function valid(device) {
|
|
23
|
+
return typeof device === 'string' && /^[\w.-]{1,64}$/.test(device);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function load(file) {
|
|
27
|
+
let raw;
|
|
28
|
+
try { raw = JSON.parse(readFileSync(file, 'utf8')); }
|
|
29
|
+
catch { return []; } // missing or unreadable -> empty, fail closed for replay
|
|
30
|
+
const list = Array.isArray(raw?.subscriptions) ? raw.subscriptions : [];
|
|
31
|
+
return list.filter(entry => valid(entry?.device) && RECEIPT_ID.test(entry?.requestId ?? '')
|
|
32
|
+
&& typeof entry.wantBody === 'boolean' && Number.isFinite(entry?.createdAt) && Number.isFinite(entry?.expiresAt));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function save(file, subscriptions) {
|
|
36
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
37
|
+
const tmp = `${file}.tmp`;
|
|
38
|
+
writeFileSync(tmp, JSON.stringify({ v: 1, subscriptions }) + '\n', { mode: 0o600 });
|
|
39
|
+
rmSync(file, { force: true });
|
|
40
|
+
renameSync(tmp, file);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sync, atomic, newest-wins store. Registering the same (device, requestId)
|
|
45
|
+
* supersedes the prior intent; ack removes it; sweep drops expired intents.
|
|
46
|
+
* pendingFor() lists what a reconnecting device must be re-offered.
|
|
47
|
+
*/
|
|
48
|
+
export function openWatchStore(base) {
|
|
49
|
+
const file = watchFile(base);
|
|
50
|
+
const write = (subscriptions) => {
|
|
51
|
+
save(file, subscriptions);
|
|
52
|
+
return subscriptions;
|
|
53
|
+
};
|
|
54
|
+
let subscriptions = load(file);
|
|
55
|
+
return {
|
|
56
|
+
register({ device, requestId, wantBody = false, ttlMs = WATCH_TTL_MS }) {
|
|
57
|
+
if (!valid(device)) throw new Error('invalid_device');
|
|
58
|
+
if (!RECEIPT_ID.test(requestId ?? '')) throw new Error('invalid_request_id');
|
|
59
|
+
if (wantBody !== undefined && typeof wantBody !== 'boolean') throw new Error('invalid_want_body');
|
|
60
|
+
if (!Number.isFinite(ttlMs) || ttlMs <= 0 || ttlMs > MAX_TTL_MS) throw new Error('invalid_ttl');
|
|
61
|
+
const now = Date.now();
|
|
62
|
+
subscriptions = subscriptions.filter(s => !(s.device === device && s.requestId === requestId));
|
|
63
|
+
subscriptions.push({ device, requestId, wantBody: Boolean(wantBody), createdAt: now, expiresAt: now + ttlMs });
|
|
64
|
+
const mine = subscriptions.filter(s => s.device === device);
|
|
65
|
+
if (mine.length > MAX_SUBS_PER_DEVICE) {
|
|
66
|
+
const drop = new Set(mine.slice(0, mine.length - MAX_SUBS_PER_DEVICE));
|
|
67
|
+
subscriptions = subscriptions.filter(s => !drop.has(s));
|
|
68
|
+
}
|
|
69
|
+
write(subscriptions);
|
|
70
|
+
return { ...subscriptions.find(s => s.device === device && s.requestId === requestId) };
|
|
71
|
+
},
|
|
72
|
+
markDelivered(device, requestId) {
|
|
73
|
+
const sub = subscriptions.find(s => s.device === device && s.requestId === requestId);
|
|
74
|
+
if (sub) { sub.deliveredAt = Date.now(); write(subscriptions); }
|
|
75
|
+
return Boolean(sub);
|
|
76
|
+
},
|
|
77
|
+
ack(device, requestId) {
|
|
78
|
+
const before = subscriptions.length;
|
|
79
|
+
subscriptions = subscriptions.filter(s => !(s.device === device && s.requestId === requestId));
|
|
80
|
+
if (subscriptions.length === before) return false;
|
|
81
|
+
write(subscriptions);
|
|
82
|
+
return true;
|
|
83
|
+
},
|
|
84
|
+
pendingFor(device) {
|
|
85
|
+
return subscriptions.filter(s => s.device === device && s.expiresAt > Date.now())
|
|
86
|
+
.map(({ id, ...rest }) => ({ ...rest }));
|
|
87
|
+
},
|
|
88
|
+
pendingAll() {
|
|
89
|
+
return subscriptions.filter(s => s.expiresAt > Date.now())
|
|
90
|
+
.map(({ id, ...rest }) => ({ ...rest }));
|
|
91
|
+
},
|
|
92
|
+
sweep(now = Date.now()) {
|
|
93
|
+
const before = subscriptions.length;
|
|
94
|
+
subscriptions = subscriptions.filter(s => s.expiresAt > now);
|
|
95
|
+
if (subscriptions.length !== before) write(subscriptions);
|
|
96
|
+
return before - subscriptions.length;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -26,3 +26,15 @@ export function validEntry(entry: unknown): JournalEntry | null;
|
|
|
26
26
|
export function readJournal(dir: string): JournalEntry[];
|
|
27
27
|
export function findReceipt(base: string, id: string): JournalEntry | null;
|
|
28
28
|
export function publicReceipt(entry: JournalEntry | null): PublicReceipt;
|
|
29
|
+
export function watchReceipts(
|
|
30
|
+
base: string,
|
|
31
|
+
onEntry: (entry: JournalEntry) => void,
|
|
32
|
+
options?: { intervalMs?: number },
|
|
33
|
+
): () => void;
|
|
34
|
+
export const MAX_BODY_BYTES: number;
|
|
35
|
+
export interface ReceiptBody {
|
|
36
|
+
text: string;
|
|
37
|
+
truncated: boolean;
|
|
38
|
+
bytes: number;
|
|
39
|
+
}
|
|
40
|
+
export function findBody(base: string, id: string): ReceiptBody | null;
|
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
import { createHash } from 'node:crypto';
|
|
5
5
|
import { homedir } from 'node:os';
|
|
6
6
|
import { join } from 'node:path';
|
|
7
|
-
import { readFileSync, readdirSync } from 'node:fs';
|
|
7
|
+
import { closeSync, constants, fstatSync, openSync, readFileSync, readSync, readdirSync, realpathSync, watch } from 'node:fs';
|
|
8
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
9
|
+
|
|
10
|
+
export const MAX_BODY_BYTES = 64 * 1024; // served size cap for one archived reply
|
|
8
11
|
|
|
9
12
|
export const RECEIPT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
10
13
|
const RECEIPTS = 'receipts.jsonl';
|
|
@@ -17,7 +20,16 @@ export function journalBase(env = process.env) {
|
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export function scopeFor(cwd) {
|
|
20
|
-
|
|
23
|
+
// Resolve symlinks before hashing (Paseo's import-matcher lesson): two panes
|
|
24
|
+
// entering the same workspace through different link paths must share one
|
|
25
|
+
// journal scope. Reads already scan every scope, so this only converges writes.
|
|
26
|
+
let real = cwd;
|
|
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();
|
|
32
|
+
return createHash('sha256').update(real).digest('hex').slice(0, 16);
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
export function validEntry(entry) {
|
|
@@ -28,7 +40,11 @@ function validEntryShape(entry) {
|
|
|
28
40
|
return Boolean(entry && entry.v === 1 && RECEIPT_ID.test(entry.id ?? '')
|
|
29
41
|
&& ['replied', 'failed', 'canceled', 'unknown'].includes(entry.status)
|
|
30
42
|
&& typeof entry.sessionId === 'string' && typeof entry.ts === 'number'
|
|
31
|
-
&& ['reason', 'requestEntryId', 'responseEntryId', 'paneId'].every(key => entry[key] === undefined || typeof entry[key] === 'string')
|
|
43
|
+
&& ['reason', 'requestEntryId', 'responseEntryId', 'paneId'].every(key => entry[key] === undefined || typeof entry[key] === 'string')
|
|
44
|
+
&& (entry.usage === undefined || (typeof entry.usage === 'object' && entry.usage !== null
|
|
45
|
+
&& Object.keys(entry.usage).length > 0
|
|
46
|
+
&& Object.keys(entry.usage).every(key => ['totalTokens', 'costUsd', 'durationMs'].includes(key))
|
|
47
|
+
&& Object.values(entry.usage).every(value => Number.isFinite(value)))));
|
|
32
48
|
}
|
|
33
49
|
|
|
34
50
|
/** Parse a journal file; tolerate a torn tail and skip corrupt lines. */
|
|
@@ -75,3 +91,98 @@ export function publicReceipt(entry) {
|
|
|
75
91
|
const { v, ts, sessionId, paneId, ...rest } = entry;
|
|
76
92
|
return rest;
|
|
77
93
|
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Find the archived reply text for a request id across workspace scopes.
|
|
97
|
+
* Writers archive bodies only when configured to retain the reply. The
|
|
98
|
+
* served text is capped (truncated flags the cut). Exact id
|
|
99
|
+
* only, no enumeration; unreadable -> null (fail closed).
|
|
100
|
+
*/
|
|
101
|
+
export function findBody(base, id) {
|
|
102
|
+
if (!RECEIPT_ID.test(id ?? '')) return null;
|
|
103
|
+
try {
|
|
104
|
+
for (const name of readdirSync(base)) {
|
|
105
|
+
if (!/^[0-9a-f]{16}$/.test(name)) continue;
|
|
106
|
+
let fd;
|
|
107
|
+
try {
|
|
108
|
+
fd = openSync(join(base, name, 'responses', `${id}.md`), constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0));
|
|
109
|
+
const stat = fstatSync(fd);
|
|
110
|
+
if (!stat.isFile()) continue;
|
|
111
|
+
const buf = Buffer.alloc(Math.min(stat.size, MAX_BODY_BYTES));
|
|
112
|
+
let read = 0;
|
|
113
|
+
while (read < buf.length) {
|
|
114
|
+
const n = readSync(fd, buf, read, buf.length - read, null);
|
|
115
|
+
if (!n) break;
|
|
116
|
+
read += n;
|
|
117
|
+
}
|
|
118
|
+
if (read !== buf.length) continue; // archive changed while reading
|
|
119
|
+
const truncated = stat.size > read;
|
|
120
|
+
const data = buf.subarray(0, read);
|
|
121
|
+
// For a capped reply, omit a final incomplete code point rather than substituting U+FFFD.
|
|
122
|
+
const text = truncated ? new StringDecoder('utf8').write(data) : data.toString('utf8');
|
|
123
|
+
return { text, truncated, bytes: stat.size };
|
|
124
|
+
} catch { continue; }
|
|
125
|
+
finally { if (fd !== undefined) closeSync(fd); }
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
} catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Watch every workspace journal under base and hand each NEW settle to onEntry
|
|
135
|
+
* (unseen id, or a newer ts for a known id). The first scan is a baseline and
|
|
136
|
+
* emits nothing, so a restarting publisher does not replay old receipts.
|
|
137
|
+
* fs.watch (recursive where supported) gives low latency; a slow interval sweep
|
|
138
|
+
* covers missed events and platforms without recursive watch. A missing base
|
|
139
|
+
* directory just means the next sweep retries. Never throws to the caller.
|
|
140
|
+
*/
|
|
141
|
+
export function watchReceipts(base, onEntry, { intervalMs = 4000 } = {}) {
|
|
142
|
+
let stopped = false;
|
|
143
|
+
const seen = new Map(); // id -> ts
|
|
144
|
+
let watcher = null;
|
|
145
|
+
let timer = null;
|
|
146
|
+
let sweepScheduled = false;
|
|
147
|
+
const sweep = emit => {
|
|
148
|
+
if (stopped) return;
|
|
149
|
+
let scopes = [];
|
|
150
|
+
try { scopes = readdirSync(base).filter(name => /^[0-9a-f]{16}$/.test(name)); }
|
|
151
|
+
catch { /* base not created yet */ }
|
|
152
|
+
for (const name of scopes) {
|
|
153
|
+
let entries;
|
|
154
|
+
try { entries = readJournal(join(base, name, RECEIPTS)); } catch { continue; }
|
|
155
|
+
for (const entry of entries) {
|
|
156
|
+
const prior = seen.get(entry.id);
|
|
157
|
+
if (prior !== undefined && prior >= entry.ts) continue;
|
|
158
|
+
seen.set(entry.id, entry.ts);
|
|
159
|
+
if (emit) { try { onEntry(entry); } catch { /* listener errors must not kill the sweep */ } }
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const ensureWatcher = () => {
|
|
164
|
+
if (watcher || stopped) return;
|
|
165
|
+
try {
|
|
166
|
+
watcher = watch(base, { recursive: true }, () => {
|
|
167
|
+
if (sweepScheduled || stopped) return;
|
|
168
|
+
sweepScheduled = true;
|
|
169
|
+
const late = setTimeout(() => { sweepScheduled = false; sweep(true); }, 250);
|
|
170
|
+
late.unref?.();
|
|
171
|
+
});
|
|
172
|
+
watcher.on('error', () => { try { watcher?.close(); } catch { /* already closed */ } watcher = null; });
|
|
173
|
+
} catch { /* base missing or unsupported here; the interval retries */ }
|
|
174
|
+
};
|
|
175
|
+
sweep(false); // baseline only
|
|
176
|
+
ensureWatcher();
|
|
177
|
+
timer = setInterval(() => {
|
|
178
|
+
if (stopped) return;
|
|
179
|
+
ensureWatcher();
|
|
180
|
+
sweep(true);
|
|
181
|
+
}, intervalMs);
|
|
182
|
+
timer.unref?.();
|
|
183
|
+
return () => {
|
|
184
|
+
stopped = true;
|
|
185
|
+
try { watcher?.close(); } catch { /* already closed */ }
|
|
186
|
+
if (timer) clearInterval(timer);
|
|
187
|
+
};
|
|
188
|
+
}
|
|
@@ -6,6 +6,8 @@ export interface JobRecord {
|
|
|
6
6
|
prompt: string;
|
|
7
7
|
ts: number;
|
|
8
8
|
state: JobState;
|
|
9
|
+
/** Routing key resolved against the host's workspaces.json allowlist. */
|
|
10
|
+
workspace?: string;
|
|
9
11
|
sessionId?: string;
|
|
10
12
|
reason?: string;
|
|
11
13
|
requestEntryId?: string;
|
|
@@ -30,6 +32,11 @@ export const MIN_PI_VERSION: string;
|
|
|
30
32
|
export function versionAtLeast(version: string, min?: string): boolean;
|
|
31
33
|
export function workerBase(env?: Record<string, string | undefined>): string;
|
|
32
34
|
export function jobsFile(base: string): string;
|
|
35
|
+
/** Routing key a device may reference; paths never cross the wire. */
|
|
36
|
+
export const WORKSPACE_NAME: RegExp;
|
|
37
|
+
export function workspacesFile(base: string): string;
|
|
38
|
+
/** Owner-curated name -> absolute workspace path. Missing/corrupt -> empty map. */
|
|
39
|
+
export function readWorkspaces(base: string): Map<string, string>;
|
|
33
40
|
export function validJob(job: unknown): JobRecord | null;
|
|
34
41
|
export function readJobs(file: string): Map<string, JobRecord>;
|
|
35
42
|
export function appendJob(file: string, job: JobRecord): void;
|
|
@@ -5,13 +5,16 @@
|
|
|
5
5
|
import { createHash } from 'node:crypto';
|
|
6
6
|
import { homedir } from 'node:os';
|
|
7
7
|
import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
8
|
-
import { dirname, join } from 'node:path';
|
|
8
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
9
9
|
|
|
10
10
|
export const JOB_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
11
11
|
export const MAX_PROMPT = 16 * 1024; // admission bound for one prompt
|
|
12
12
|
export const MAX_PENDING = 100; // queued + running jobs admitted at once
|
|
13
13
|
export const JOB_STATES = new Set(['queued', 'running', 'replied', 'failed', 'canceled', 'unknown']);
|
|
14
|
+
/** Routing key a device may reference — resolved against the host's allowlist. */
|
|
15
|
+
export const WORKSPACE_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
14
16
|
const JOBS = 'jobs.jsonl';
|
|
17
|
+
const WORKSPACES = 'workspaces.json';
|
|
15
18
|
|
|
16
19
|
/** Oldest Pi release the experiments are verified against. Newer Pi releases run
|
|
17
20
|
* without code changes; only a Pi older than this is refused. */
|
|
@@ -43,6 +46,30 @@ export function jobsFile(base) {
|
|
|
43
46
|
return join(base, JOBS);
|
|
44
47
|
}
|
|
45
48
|
|
|
49
|
+
export function workspacesFile(base) {
|
|
50
|
+
return join(base, WORKSPACES);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Owner-curated routing table: name -> absolute workspace path, 0600, optional.
|
|
54
|
+
* A device references a name; the worker resolves it here. No raw paths cross
|
|
55
|
+
* the wire, and a name missing from this file fails closed (never runs). */
|
|
56
|
+
export function readWorkspaces(base) {
|
|
57
|
+
let raw;
|
|
58
|
+
try { raw = readFileSync(workspacesFile(base), 'utf8'); }
|
|
59
|
+
catch (error) { if (error.code === 'ENOENT') return new Map(); throw error; }
|
|
60
|
+
let parsed;
|
|
61
|
+
try { parsed = JSON.parse(raw); }
|
|
62
|
+
catch { return new Map(); } // corrupt allowlist routes nothing new
|
|
63
|
+
const table = parsed?.v === 1 && parsed.workspaces && typeof parsed.workspaces === 'object'
|
|
64
|
+
? parsed.workspaces : null;
|
|
65
|
+
if (!table) return new Map();
|
|
66
|
+
const out = new Map();
|
|
67
|
+
for (const [name, path] of Object.entries(table)) {
|
|
68
|
+
if (WORKSPACE_NAME.test(name) && typeof path === 'string' && isAbsolute(path)) out.set(name, path);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
46
73
|
export function validJob(job) {
|
|
47
74
|
return validJobShape(job) ? job : null;
|
|
48
75
|
}
|
|
@@ -52,6 +79,7 @@ function validJobShape(job) {
|
|
|
52
79
|
&& JOB_STATES.has(job.state)
|
|
53
80
|
&& typeof job.prompt === 'string' && job.prompt.length <= MAX_PROMPT
|
|
54
81
|
&& typeof job.ts === 'number'
|
|
82
|
+
&& (job.workspace === undefined || WORKSPACE_NAME.test(job.workspace))
|
|
55
83
|
&& ['sessionId', 'reason', 'requestEntryId', 'responseEntryId'].every(key =>
|
|
56
84
|
job[key] === undefined || typeof job[key] === 'string'));
|
|
57
85
|
}
|
package/src/cloud/gateway.ts
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
import { AgentCodes } from "./pairing.ts";
|
|
36
36
|
import { parseAuthorizedKey, sshKeyFingerprint } from "./sshd.ts";
|
|
37
37
|
import { type Account, type DeviceRecord, type RoomRecord, State } from "./stores.ts";
|
|
38
|
-
import { readJsonBody, randomId, sameSecret, sha256Hex } from "./util.ts";
|
|
38
|
+
import { pruneBudget, readJsonBody, randomId, sameSecret, sha256Hex } from "./util.ts";
|
|
39
39
|
import { serveStatic } from "./static.ts";
|
|
40
40
|
import { startSshd, type SshHandle, type SshConfig } from "./sshd.ts";
|
|
41
41
|
import { pushEndpoint, sendWake } from "./push.ts";
|
|
@@ -235,12 +235,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
|
|
|
235
235
|
if (loopbackOf(addr)) return `${addr}#${randomId("k")}`;
|
|
236
236
|
return addr;
|
|
237
237
|
};
|
|
238
|
-
const pruneFails = (): void =>
|
|
239
|
-
if (fails.size <= FAILS_MAX) return;
|
|
240
|
-
const now = Date.now();
|
|
241
|
-
for (const [k, f] of fails) if (now - f.at > 60_000) fails.delete(k);
|
|
242
|
-
if (fails.size > FAILS_MAX) fails.clear();
|
|
243
|
-
};
|
|
238
|
+
const pruneFails = (): void => pruneBudget(fails, FAILS_MAX, 60_000);
|
|
244
239
|
const strike = (key: string): void => {
|
|
245
240
|
pruneFails();
|
|
246
241
|
const f = fails.get(key) ?? { n: 0, until: 0, at: Date.now() };
|
|
@@ -261,6 +256,11 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
|
|
|
261
256
|
const MINT_PER_KEY = 10;
|
|
262
257
|
const MINT_WINDOW_MS = 10 * 60_000;
|
|
263
258
|
const mints = new Map<string, { n: number; until: number; at: number }>();
|
|
259
|
+
// The budget map itself must stay bounded too (audit
|
|
260
|
+
// gateway:mint-budget-map-unbounded): per-connection keys behind a
|
|
261
|
+
// same-host proxy must not accumulate forever.
|
|
262
|
+
const MINTS_MAX = 4_096;
|
|
263
|
+
const pruneMints = (): void => pruneBudget(mints, MINTS_MAX, MINT_WINDOW_MS);
|
|
264
264
|
|
|
265
265
|
const sendText = (ws: WebSocket, text: string | Buffer): void => {
|
|
266
266
|
if (ws.readyState !== WebSocket.OPEN) return;
|
|
@@ -826,10 +826,10 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
|
|
|
826
826
|
// Two budgets so minting cannot be driven without limit (audit
|
|
827
827
|
// pairing:unbounded-code-mint): a small per-client rate and a global
|
|
828
828
|
// pending cap inside AgentCodes.
|
|
829
|
+
pruneMints();
|
|
829
830
|
const m = mints.get(key) ?? { n: 0, until: 0, at: Date.now() };
|
|
830
831
|
const now = Date.now();
|
|
831
832
|
if (m.until > now) {
|
|
832
|
-
mints.set(key, m);
|
|
833
833
|
return json(res, 429, { error: "too many pairing codes; retry shortly" });
|
|
834
834
|
}
|
|
835
835
|
if (now - m.at > MINT_WINDOW_MS) m.n = 0;
|
package/src/cloud/sshd.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { dirname } from "node:path";
|
|
|
23
23
|
import type { Duplex } from "node:stream";
|
|
24
24
|
import ssh2Pkg from "ssh2";
|
|
25
25
|
import { type State } from "./stores.ts";
|
|
26
|
-
import { randomToken } from "./util.ts";
|
|
26
|
+
import { pruneBudget, randomToken } from "./util.ts";
|
|
27
27
|
|
|
28
28
|
const { Server } = ssh2Pkg;
|
|
29
29
|
const utils = (ssh2Pkg as unknown as { utils?: { generateKeyPairSync?(type: string): { private: string; public: string } } }).utils;
|
|
@@ -165,7 +165,11 @@ export async function startSshd(deps: SshDeps): Promise<SshHandle | null> {
|
|
|
165
165
|
|
|
166
166
|
const hostKey = loadOrMintHostKey(deps);
|
|
167
167
|
const channels = new Map<string, Channel>(); // `${deviceId}:${ch}` → channel
|
|
168
|
-
const strikes = new Map<string, { n: number; until: number }>();
|
|
168
|
+
const strikes = new Map<string, { n: number; until: number; at: number }>();
|
|
169
|
+
// Bounded like the gateway's failed-hello map (audit
|
|
170
|
+
// sshd:auth-strike-map-unbounded): WSS tunnels each authenticate under a
|
|
171
|
+
// per-connection key, so failed-auth churn must not accumulate forever.
|
|
172
|
+
const STRIKES_MAX = 4_096;
|
|
169
173
|
|
|
170
174
|
const ipOf = (ctx: { ip?: string }): string => ctx.ip ?? "?";
|
|
171
175
|
const blocked = (ip: string): boolean => {
|
|
@@ -173,8 +177,10 @@ export async function startSshd(deps: SshDeps): Promise<SshHandle | null> {
|
|
|
173
177
|
return Boolean(f && f.until > Date.now());
|
|
174
178
|
};
|
|
175
179
|
const strike = (ip: string): void => {
|
|
176
|
-
|
|
180
|
+
pruneBudget(strikes, STRIKES_MAX, AUTH_BLOCK_MS);
|
|
181
|
+
const f = strikes.get(ip) ?? { n: 0, until: 0, at: Date.now() };
|
|
177
182
|
f.n += 1;
|
|
183
|
+
f.at = Date.now();
|
|
178
184
|
if (f.n >= AUTH_STRIKES) {
|
|
179
185
|
f.until = Date.now() + AUTH_BLOCK_MS;
|
|
180
186
|
f.n = 0;
|
|
@@ -200,7 +206,7 @@ export async function startSshd(deps: SshDeps): Promise<SshHandle | null> {
|
|
|
200
206
|
/** To the HOST, addressed as the device (the bridge on the host consumes these). */
|
|
201
207
|
const toHost = (deviceId: string, payload: Record<string, unknown>): boolean => deps.sendAsDevice(deviceId, payload);
|
|
202
208
|
|
|
203
|
-
const server = new Server({ hostKeys: [hostKey.private] }, (client, info) => {
|
|
209
|
+
const server = new Server({ hostKeys: [hostKey.private], ...(process.env.HERDGATE_SSH_AUTH_DEBUG === "1" ? { debug: (msg: string) => console.error(`ssh2: ${msg}`) } : {}) }, (client, info) => {
|
|
204
210
|
// ssh2's server-side Client stores the socket supplied to injectSocket as
|
|
205
211
|
// _sock (1.17). Do not authorize a connection if that transport is unknown.
|
|
206
212
|
const socket = (client as unknown as { _sock?: Transport })._sock;
|
|
@@ -217,19 +223,16 @@ export async function startSshd(deps: SshDeps): Promise<SshHandle | null> {
|
|
|
217
223
|
const id = ctx.username.trim();
|
|
218
224
|
const rec = /^d_[A-Za-z0-9]+$/.test(id) ? deps.state.deviceById(id) : null;
|
|
219
225
|
if (ctx.method === "publickey") {
|
|
220
|
-
// Non-interactive clients (herdr-core's `machine add` shells out to system ssh,
|
|
221
|
-
// which prefers keys and the agent) log in with a registered public key. ssh2
|
|
222
|
-
// parses the request; verifying the signature (RFC 4252 §7, `ctx.blob` is the
|
|
223
|
-
// exact signed bytes) is ours. A request WITHOUT a signature is the client
|
|
224
|
-
// asking "would you take this key?" — accepting answers PK_OK (ssh2 does that).
|
|
225
226
|
const pk = ctx as unknown as { key?: { algo: string; data: Buffer }; signature?: Buffer; blob?: Buffer; hashAlgo?: string };
|
|
226
227
|
const key = pk.key;
|
|
227
|
-
if (
|
|
228
|
+
if (process.env.HERDGATE_SSH_AUTH_DEBUG === "1") deps.log(`ssh-auth-debug: user=${ctx.username} sig=${Boolean(pk.signature)} algo=${key?.algo} blocked=${blocked(ip)}`);
|
|
229
|
+
if (!rec || !key?.data) { if (process.env.HERDGATE_SSH_AUTH_DEBUG === "1") deps.log(`ssh-auth-debug: reject rec=${Boolean(rec)} key=${Boolean(key?.data)}`); return reject(); }
|
|
228
230
|
const entry = rec.sshKeys?.find((k) => k.algo === key.algo && Buffer.from(k.b64, "base64").equals(key.data));
|
|
229
|
-
if (!entry) return reject(); // agent key not for us: no strike, just no
|
|
231
|
+
if (!entry) { if (process.env.HERDGATE_SSH_AUTH_DEBUG === "1") deps.log(`ssh-auth-debug: reject entry (algos=${rec.sshKeys?.map((k) => k.algo).join(",")})`); return reject(); } // agent key not for us: no strike, just no
|
|
230
232
|
if (!pk.signature) return ctx.accept();
|
|
231
233
|
const keyObj = sshPubKeyToKeyObject(key.algo, key.data);
|
|
232
234
|
if (!keyObj || !cryptoVerify(pk.hashAlgo ?? undefined, pk.blob ?? Buffer.alloc(0), keyObj, pk.signature)) {
|
|
235
|
+
if (process.env.HERDGATE_SSH_AUTH_DEBUG === "1") deps.log(`ssh-auth-debug: reject verify keyObj=${Boolean(keyObj)} hashAlgo=${pk.hashAlgo} sigLen=${pk.signature?.length} blobLen=${pk.blob?.length}`);
|
|
233
236
|
strike(ip);
|
|
234
237
|
deps.log(`ssh: bad key signature for ${rec.id} from ${ip}`);
|
|
235
238
|
return reject();
|
package/src/cloud/util.ts
CHANGED
|
@@ -45,3 +45,13 @@ export async function readJsonBody(req: import("node:http").IncomingMessage, max
|
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
+
|
|
49
|
+
/** Drop idle abuse-budget entries; if the map is still over the hard cap,
|
|
50
|
+
* drop everything rather than growing without bound. Shared by the
|
|
51
|
+
* failed-hello, pairing-mint and SSH-auth strike maps (audits
|
|
52
|
+
* gateway:mint-budget-map-unbounded, sshd:auth-strike-map-unbounded). */
|
|
53
|
+
export function pruneBudget<K, V extends { at: number }>(map: Map<K, V>, max: number, windowMs: number, now = Date.now()): void {
|
|
54
|
+
if (map.size <= max) return;
|
|
55
|
+
for (const [k, v] of map) if (now - v.at > windowMs) map.delete(k);
|
|
56
|
+
if (map.size > max) map.clear();
|
|
57
|
+
}
|
package/src/web/app.css
CHANGED
|
@@ -331,3 +331,26 @@ dialog::backdrop {
|
|
|
331
331
|
word-break: break-word;
|
|
332
332
|
margin: 0;
|
|
333
333
|
}
|
|
334
|
+
|
|
335
|
+
/* Pi worker panel (experimental) — mirrors the SSH leg's disclosure block. */
|
|
336
|
+
#view details.worker {
|
|
337
|
+
border: 1px solid var(--line, #2a2f3a);
|
|
338
|
+
border-radius: 8px;
|
|
339
|
+
padding: 8px 12px;
|
|
340
|
+
margin: 8px 0;
|
|
341
|
+
}
|
|
342
|
+
#view details.worker summary {
|
|
343
|
+
cursor: pointer;
|
|
344
|
+
font-weight: 600;
|
|
345
|
+
}
|
|
346
|
+
#view details.worker textarea,
|
|
347
|
+
#view details.worker input {
|
|
348
|
+
width: 100%;
|
|
349
|
+
box-sizing: border-box;
|
|
350
|
+
margin: 4px 0 8px;
|
|
351
|
+
}
|
|
352
|
+
#view details.worker pre {
|
|
353
|
+
white-space: pre-wrap;
|
|
354
|
+
overflow-wrap: anywhere;
|
|
355
|
+
margin: 4px 0;
|
|
356
|
+
}
|