@bom0792/herdgate 0.9.29 → 0.9.34
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 +81 -0
- package/.agents/skills/herdr/herdr_pi.py +272 -0
- package/.agents/skills/herdr/profiles.example.toml +18 -0
- package/.agents/skills/herdr-advisor/SKILL.md +12 -0
- package/.agents/skills/herdr-committee/SKILL.md +13 -0
- package/.agents/skills/herdr-handoff/SKILL.md +19 -0
- package/.agents/skills/herdr-help/SKILL.md +14 -0
- package/.agents/skills/herdr-plugin/SKILL.md +21 -0
- package/DEPLOY.md +106 -120
- package/DESIGN.md +9 -4
- package/README.md +55 -2
- package/bin/herdgate.mjs +12 -2
- package/bin/home-tui.mjs +2 -0
- package/bin/pi-skills.mjs +99 -0
- package/dist-readme.md +9 -2
- package/package.json +38 -9
- package/plugin/server/e2e.d.mts +2 -2
- package/plugin/server/e2e.mjs +51 -32
- package/plugin/server/index.mjs +128 -32
- package/plugin/server/pi-receipts.d.mts +28 -0
- package/plugin/server/pi-receipts.mjs +77 -0
- package/plugin/server/pi-worker.d.mts +47 -0
- package/plugin/server/pi-worker.mjs +127 -0
- package/plugin/server/ssh.mjs +12 -4
- package/scripts/bench-mobile-tui.mjs +246 -0
- package/scripts/build-host-bundle.sh +14 -8
- package/scripts/build-release.sh +47 -0
- package/scripts/deploy-vps.sh +5 -78
- package/scripts/install-pi-skills.mjs +4 -0
- package/src/cloud/gateway.ts +163 -134
- package/src/cloud/main.ts +72 -15
- package/src/cloud/pairing.ts +7 -2
- package/src/cloud/push.ts +54 -0
- package/src/cloud/sshd.ts +76 -38
- package/src/cloud/util.ts +5 -5
- package/src/host/mobile-tui.ts +11 -1
- package/src/web/app.js +88 -61
- package/src/web/e2e.js +51 -32
- package/src/web/sw.js +1 -1
- package/templates/cloud/.dockerignore +7 -0
- package/templates/cloud/Caddyfile.example +3 -0
- package/templates/cloud/Dockerfile +10 -0
- package/templates/cloud/README.md +74 -0
- package/templates/cloud/compose.env.example +18 -0
- package/templates/cloud/compose.yml +41 -0
- package/templates/cloud/gitignore.example +4 -0
- package/templates/cloud/herdgate-cloud-edge.service +18 -0
- package/templates/cloud/herdgate-cloud.service +26 -0
- package/tsconfig.cloud.json +11 -0
- package/tsconfig.json +18 -0
- package/windows/install-cli.mjs +15 -5
- package/windows/setup-tui.mjs +1 -0
- package/src/web/herdgate-host.zip +0 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Shared format for the experimental Pi worker queue (see experiments/pi-worker).
|
|
2
|
+
// The publisher appends jobs when HERDGATE_PI_WORKER=1; the separately started
|
|
3
|
+
// experiment worker consumes them and journals receipts via the shared journal.
|
|
4
|
+
// This module only owns the on-disk queue format — no network, no Pi SDK here.
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { dirname, join } from 'node:path';
|
|
9
|
+
|
|
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
|
+
export const MAX_PROMPT = 16 * 1024; // admission bound for one prompt
|
|
12
|
+
export const MAX_PENDING = 100; // queued + running jobs admitted at once
|
|
13
|
+
export const JOB_STATES = new Set(['queued', 'running', 'replied', 'failed', 'canceled', 'unknown']);
|
|
14
|
+
const JOBS = 'jobs.jsonl';
|
|
15
|
+
|
|
16
|
+
/** Oldest Pi release the experiments are verified against. Newer Pi releases run
|
|
17
|
+
* without code changes; only a Pi older than this is refused. */
|
|
18
|
+
export const MIN_PI_VERSION = '1.5.2';
|
|
19
|
+
|
|
20
|
+
/** Loose semver floor: numeric triple comparison, 'v' prefix and pre-release
|
|
21
|
+
* suffixes ignored — except a pre-release of the exact min triple does not pass. */
|
|
22
|
+
export function versionAtLeast(version, min = MIN_PI_VERSION) {
|
|
23
|
+
const parse = value => String(value ?? '').trim().replace(/^v/, '').split('-')[0].split('.').map(Number);
|
|
24
|
+
const actual = parse(version);
|
|
25
|
+
const floor = parse(min);
|
|
26
|
+
for (let i = 0; i < 3; i++) {
|
|
27
|
+
const a = actual[i] ?? 0;
|
|
28
|
+
const b = floor[i] ?? 0;
|
|
29
|
+
if (!Number.isInteger(a) || !Number.isInteger(b)) return false;
|
|
30
|
+
if (a !== b) return a > b;
|
|
31
|
+
}
|
|
32
|
+
if (String(version).includes('-') && !String(min).includes('-')) return false; // 1.5.2-rc1 is not >= 1.5.2
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Directory holding jobs.jsonl and worker state. One worker per host (prototype). */
|
|
37
|
+
export function workerBase(env = process.env) {
|
|
38
|
+
if (env.HERDGATE_PI_WORKER_DIR) return env.HERDGATE_PI_WORKER_DIR;
|
|
39
|
+
return join(env.XDG_STATE_HOME || join(homedir(), '.local', 'state'), 'herdgate', 'pi-worker');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function jobsFile(base) {
|
|
43
|
+
return join(base, JOBS);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function validJob(job) {
|
|
47
|
+
return validJobShape(job) ? job : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function validJobShape(job) {
|
|
51
|
+
return Boolean(job && job.v === 1 && JOB_ID.test(job.id ?? '')
|
|
52
|
+
&& JOB_STATES.has(job.state)
|
|
53
|
+
&& typeof job.prompt === 'string' && job.prompt.length <= MAX_PROMPT
|
|
54
|
+
&& typeof job.ts === 'number'
|
|
55
|
+
&& ['sessionId', 'reason', 'requestEntryId', 'responseEntryId'].every(key =>
|
|
56
|
+
job[key] === undefined || typeof job[key] === 'string'));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Newest record per job id; corrupt lines and a torn tail are skipped. */
|
|
60
|
+
export function readJobs(file) {
|
|
61
|
+
let raw;
|
|
62
|
+
try { raw = readFileSync(file, 'utf8'); }
|
|
63
|
+
catch (error) { if (error.code === 'ENOENT') return new Map(); throw error; }
|
|
64
|
+
const lines = raw.split('\n');
|
|
65
|
+
lines.pop();
|
|
66
|
+
const jobs = new Map();
|
|
67
|
+
for (const line of lines) {
|
|
68
|
+
try {
|
|
69
|
+
const job = JSON.parse(line);
|
|
70
|
+
if (validJob(job)) jobs.set(job.id, job);
|
|
71
|
+
} catch { /* skip */ }
|
|
72
|
+
}
|
|
73
|
+
return jobs;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function appendJob(file, job) {
|
|
77
|
+
if (!validJob(job)) throw new Error('invalid job record');
|
|
78
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
79
|
+
appendFileSync(file, `${JSON.stringify(job)}\n`, { mode: 0o600 });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The public answer for a device: the prompt never leaves the host. */
|
|
83
|
+
export function publicJob(job) {
|
|
84
|
+
if (!job) return { state: 'unknown', reason: 'not_admitted' };
|
|
85
|
+
const { v, prompt, ...rest } = job;
|
|
86
|
+
return rest;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function queueDepth(jobs) {
|
|
90
|
+
let queued = 0;
|
|
91
|
+
let running = 0;
|
|
92
|
+
for (const job of jobs.values()) {
|
|
93
|
+
if (job.state === 'queued') queued += 1;
|
|
94
|
+
else if (job.state === 'running') running += 1;
|
|
95
|
+
}
|
|
96
|
+
return { queued, running };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Deterministic UUID for one scheduled run: same (schedule, bucket) -> same id, so a
|
|
100
|
+
* restarted scheduler re-admits the same job instead of duplicating the run. */
|
|
101
|
+
export function scheduledRunId(scheduleId, bucket) {
|
|
102
|
+
const digest = createHash('sha256').update(`${scheduleId}|${bucket}`).digest('hex');
|
|
103
|
+
const variant = (parseInt(digest[16], 16) & 0x3) | 0x8;
|
|
104
|
+
return [digest.slice(0, 8), digest.slice(8, 12), `4${digest.slice(13, 16)}`,
|
|
105
|
+
`${variant.toString(16)}${digest.slice(17, 20)}`, digest.slice(20, 32)].join('-');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function heartbeatPath(base) {
|
|
109
|
+
return join(base, 'heartbeat.json');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** One heartbeat record per tick; the publisher serves it via worker.health. */
|
|
113
|
+
export function writeHeartbeat(base, entry) {
|
|
114
|
+
if (!entry || entry.v !== 1 || typeof entry.ts !== 'number' || typeof entry.source !== 'string') {
|
|
115
|
+
throw new Error('invalid heartbeat');
|
|
116
|
+
}
|
|
117
|
+
mkdirSync(base, { recursive: true, mode: 0o700 });
|
|
118
|
+
writeFileSync(heartbeatPath(base), JSON.stringify(entry), { mode: 0o600 });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function readHeartbeat(base) {
|
|
122
|
+
try {
|
|
123
|
+
const entry = JSON.parse(readFileSync(heartbeatPath(base), 'utf8'));
|
|
124
|
+
if (entry && entry.v === 1 && typeof entry.ts === 'number' && typeof entry.source === 'string') return entry;
|
|
125
|
+
} catch { /* missing or corrupt -> no heartbeat */ }
|
|
126
|
+
return null;
|
|
127
|
+
}
|
package/plugin/server/ssh.mjs
CHANGED
|
@@ -89,7 +89,11 @@ export function createSshBridge({ send, scopeOf, log = () => {}, resolve = resol
|
|
|
89
89
|
if (c.flushTimer) return;
|
|
90
90
|
c.flushTimer = setTimeout(() => {
|
|
91
91
|
c.flushTimer = null;
|
|
92
|
-
if (
|
|
92
|
+
if (c.closed || !c.outBuf.length) return;
|
|
93
|
+
sendOut(c, false);
|
|
94
|
+
// On a busy interactive channel keep batching subsequent chunks. After
|
|
95
|
+
// an idle period, the next response can leave without the batch delay.
|
|
96
|
+
if (c.lowLatency) scheduleFlush(c);
|
|
93
97
|
}, FLUSH_MS);
|
|
94
98
|
c.flushTimer.unref?.();
|
|
95
99
|
};
|
|
@@ -170,11 +174,15 @@ export function createSshBridge({ send, scopeOf, log = () => {}, resolve = resol
|
|
|
170
174
|
}
|
|
171
175
|
// Only the dedicated local read-only adapter may parse framed read RPC itself.
|
|
172
176
|
const trusted = !tui && scope === "read" && (trustedReadCore === bin || mobile);
|
|
173
|
-
const
|
|
177
|
+
const lowLatency = mobile && scope === "act" && args.includes("connect");
|
|
178
|
+
const c = { device, ch, scope, proc, trustedReadCore: trusted, lowLatency, inBuf: Buffer.alloc(0), outBuf: Buffer.alloc(0), flushTimer: null, malformed: 0, closed: false };
|
|
174
179
|
channels.set(key(device, ch), c);
|
|
175
180
|
const onData = (chunk) => {
|
|
176
|
-
c.outBuf = Buffer.concat([c.outBuf, chunk]);
|
|
177
|
-
if (c.
|
|
181
|
+
c.outBuf = c.outBuf.length ? Buffer.concat([c.outBuf, chunk]) : chunk;
|
|
182
|
+
if (c.lowLatency && !c.flushTimer) {
|
|
183
|
+
sendOut(c, false);
|
|
184
|
+
scheduleFlush(c);
|
|
185
|
+
} else if (c.outBuf.length >= FLUSH_BYTES) sendOut(c, false);
|
|
178
186
|
else scheduleFlush(c);
|
|
179
187
|
};
|
|
180
188
|
proc.stdout?.on("data", onData);
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Runs an isolated Herdr server, Gate, publisher and TCP edge. Never uses live rooms or keys.
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { execFile, spawn } from 'node:child_process';
|
|
5
|
+
import { randomBytes } from 'node:crypto';
|
|
6
|
+
import { once } from 'node:events';
|
|
7
|
+
import { mkdir, mkdtemp, rm } from 'node:fs/promises';
|
|
8
|
+
import { tmpdir } from 'node:os';
|
|
9
|
+
import { dirname, join, resolve } from 'node:path';
|
|
10
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { promisify, stripVTControlCharacters } from 'node:util';
|
|
13
|
+
import { Client } from 'ssh2';
|
|
14
|
+
import { HerdGateAgent } from '../src/agent/index.ts';
|
|
15
|
+
import { startCloud } from '../src/cloud/gateway.ts';
|
|
16
|
+
import { startTcpEdge } from '../src/cloud/ssh-tcp-edge.ts';
|
|
17
|
+
import { State } from '../src/cloud/stores.ts';
|
|
18
|
+
import { createSshBridge } from '../plugin/server/ssh.mjs';
|
|
19
|
+
import { openTerminal } from '../plugin/server/mobile-tui-stream.mjs';
|
|
20
|
+
|
|
21
|
+
const run = promisify(execFile);
|
|
22
|
+
const herdr = process.env.TEST_HERDR_BIN;
|
|
23
|
+
if (!herdr || resolve(herdr) !== herdr) throw new Error('Set TEST_HERDR_BIN to the absolute path to Herdr');
|
|
24
|
+
const rounds = Number(process.argv[2] ?? 24);
|
|
25
|
+
if (!Number.isInteger(rounds) || rounds < 1 || rounds > 100) throw new Error('Pass 1 to 100 rounds');
|
|
26
|
+
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
27
|
+
const adapter = join(root, 'plugin/server/mobile-tui-core.mjs');
|
|
28
|
+
const dir = await mkdtemp(join(tmpdir(), 'herdgate-mobile-bench-'));
|
|
29
|
+
const env = {
|
|
30
|
+
HOME: dir, PATH: process.env.PATH, LANG: process.env.LANG,
|
|
31
|
+
SHELL: '/bin/sh', XDG_CONFIG_HOME: join(dir, 'config'), XDG_STATE_HOME: join(dir, 'state'),
|
|
32
|
+
XDG_DATA_HOME: join(dir, 'data'), HERDR_SOCKET_PATH: join(dir, 'herdr.sock'), HERDR_BIN_PATH: herdr,
|
|
33
|
+
};
|
|
34
|
+
const host = (...args) => run(herdr, args, { env, cwd: dir, timeout: 5000 });
|
|
35
|
+
const stats = (samples) => {
|
|
36
|
+
if (!samples.length) return undefined;
|
|
37
|
+
const s = samples.toSorted((a, b) => a - b);
|
|
38
|
+
const at = (p) => +s[Math.ceil(p * s.length) - 1].toFixed(1);
|
|
39
|
+
return { n: s.length, p50: at(0.50), p95: at(0.95), max: at(1) };
|
|
40
|
+
};
|
|
41
|
+
const deadline = (promise, label) => {
|
|
42
|
+
const ctrl = new AbortController();
|
|
43
|
+
return Promise.race([promise, delay(6500, undefined, { signal: ctrl.signal, ref: false }).then(() => { throw new Error(`Timeout: ${label}`); })]).finally(() => ctrl.abort());
|
|
44
|
+
};
|
|
45
|
+
const elapsed = (since) => performance.now() - since;
|
|
46
|
+
const rpc = (id, method, params) => JSON.stringify({ jsonrpc: '2.0', id, method, params });
|
|
47
|
+
const marker = () => randomBytes(5).toString('hex');
|
|
48
|
+
|
|
49
|
+
async function coreBench(input, output, letters) {
|
|
50
|
+
let buf = Buffer.alloc(0);
|
|
51
|
+
let id = 0;
|
|
52
|
+
const raw = output[Symbol.asyncIterator]();
|
|
53
|
+
const fill = async () => {
|
|
54
|
+
const next = await raw.next();
|
|
55
|
+
assert.ok(!next.done, 'Core stream ended');
|
|
56
|
+
buf = Buffer.concat([buf, next.value]);
|
|
57
|
+
};
|
|
58
|
+
const send = (channel, kind, payload = Buffer.alloc(0)) => {
|
|
59
|
+
const head = Buffer.alloc(9);
|
|
60
|
+
head.writeUInt32LE(channel);
|
|
61
|
+
head[4] = kind;
|
|
62
|
+
head.writeUInt32LE(payload.length, 5);
|
|
63
|
+
input.write(Buffer.concat([head, payload]));
|
|
64
|
+
};
|
|
65
|
+
const read = async () => {
|
|
66
|
+
while (buf.length < 9) await fill();
|
|
67
|
+
const end = 9 + buf.readUInt32LE(5);
|
|
68
|
+
while (buf.length < end) await fill();
|
|
69
|
+
const value = { channel: buf.readUInt32LE(0), kind: buf[4], body: buf.subarray(9, end), time: performance.now() };
|
|
70
|
+
buf = buf.subarray(end);
|
|
71
|
+
if (value.channel !== 0 && value.kind === 0) {
|
|
72
|
+
const credit = Buffer.alloc(4);
|
|
73
|
+
credit.writeUInt32LE(value.body.length);
|
|
74
|
+
send(value.channel, 1, credit);
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
};
|
|
78
|
+
const request = (method, params) => {
|
|
79
|
+
send(0, 0, Buffer.from(rpc(++id, method, params)));
|
|
80
|
+
return id;
|
|
81
|
+
};
|
|
82
|
+
const response = async (want) => {
|
|
83
|
+
for (;;) {
|
|
84
|
+
const f = await read();
|
|
85
|
+
if (f.channel !== 0) continue;
|
|
86
|
+
const reply = JSON.parse(f.body.toString());
|
|
87
|
+
if (reply.id !== want) continue;
|
|
88
|
+
assert.ok(reply.result, JSON.stringify(reply.error));
|
|
89
|
+
return reply.result;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const helloStart = performance.now();
|
|
93
|
+
input.write(rpc(++id, 'hello', { framed: true }) + '\n');
|
|
94
|
+
await deadline((async () => {
|
|
95
|
+
while (!buf.includes(10)) await fill();
|
|
96
|
+
const nl = buf.indexOf(10);
|
|
97
|
+
assert.equal(JSON.parse(buf.subarray(0, nl)).result.framed, true);
|
|
98
|
+
buf = buf.subarray(nl + 1);
|
|
99
|
+
})(), 'Core hello');
|
|
100
|
+
const hello = elapsed(helloStart);
|
|
101
|
+
const { terminals } = await deadline(response(request('terminal.list')), 'list');
|
|
102
|
+
assert.equal(terminals.length, 1, 'benchmark must address only its isolated pane');
|
|
103
|
+
const terminal = terminals[0].address;
|
|
104
|
+
const attachStart = performance.now();
|
|
105
|
+
const { channel } = await deadline(response(request('terminal.attach', { terminal, cols: 120, rows: 28 })), 'attach');
|
|
106
|
+
for (;;) {
|
|
107
|
+
const f = await deadline(read(), 'first paint');
|
|
108
|
+
if (f.channel === channel && f.kind === 0) break;
|
|
109
|
+
}
|
|
110
|
+
const attach = elapsed(attachStart);
|
|
111
|
+
const key = [], sendPaint = [], sendAck = [], keyPaintBytes = [];
|
|
112
|
+
for (let i = 0; i < rounds; i++) {
|
|
113
|
+
const wanted = `k-${marker()}`;
|
|
114
|
+
const start = performance.now();
|
|
115
|
+
send(channel, 0, Buffer.from(wanted));
|
|
116
|
+
let text = '', bytes = 0;
|
|
117
|
+
while (!text.includes(wanted)) {
|
|
118
|
+
const f = await deadline(read(), 'key echo');
|
|
119
|
+
assert.equal(f.channel, channel);
|
|
120
|
+
assert.notEqual(f.kind, 2);
|
|
121
|
+
if (f.kind !== 0) continue;
|
|
122
|
+
text += stripVTControlCharacters(f.body.toString());
|
|
123
|
+
bytes += f.body.length;
|
|
124
|
+
}
|
|
125
|
+
key.push(elapsed(start));
|
|
126
|
+
keyPaintBytes.push(bytes);
|
|
127
|
+
send(channel, 0, Buffer.from([0x15])); // Ctrl+U clears the isolated shell line.
|
|
128
|
+
await delay(125);
|
|
129
|
+
// Alternate every glyph. Herdr can omit unchanged cells from its ANSI deltas.
|
|
130
|
+
const letter = letters[i % 2];
|
|
131
|
+
const suffix = letter.repeat(12);
|
|
132
|
+
const command = `printf '\\033[2J\\033[H'; printf '%s' ${Array(12).fill(letter).join(' ')}; printf '\\n'`;
|
|
133
|
+
const startSend = performance.now();
|
|
134
|
+
const req = request('terminal.send', { terminal, text: command });
|
|
135
|
+
let ack = false, painted = false, seen = '';
|
|
136
|
+
while (!ack || !painted) {
|
|
137
|
+
const f = await deadline(read(), `Send ${i + 1} (ack=${ack}, painted=${painted})`);
|
|
138
|
+
if (f.channel === 0) {
|
|
139
|
+
const msg = JSON.parse(f.body);
|
|
140
|
+
if (msg.id === req) {
|
|
141
|
+
assert.ok(msg.result, JSON.stringify(msg.error));
|
|
142
|
+
sendAck.push(f.time - startSend);
|
|
143
|
+
ack = true;
|
|
144
|
+
}
|
|
145
|
+
} else if (f.channel === channel && f.kind === 0 && !painted) {
|
|
146
|
+
seen += stripVTControlCharacters(f.body.toString());
|
|
147
|
+
if (seen.includes(suffix)) {
|
|
148
|
+
painted = true;
|
|
149
|
+
sendPaint.push(f.time - startSend);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
await delay(125);
|
|
154
|
+
}
|
|
155
|
+
send(channel, 2);
|
|
156
|
+
return { hello, attach, keyMs: stats(key), sendPaintMs: stats(sendPaint), sendAckMs: stats(sendAck), keyPaintBytes: stats(keyPaintBytes) };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let server, cloud, edge, publisher, bridge;
|
|
160
|
+
try {
|
|
161
|
+
await mkdir(join(dir, 'config/herdr'), { recursive: true });
|
|
162
|
+
server = spawn(herdr, ['server'], { env, cwd: dir, stdio: 'ignore' });
|
|
163
|
+
const until = performance.now() + 5000;
|
|
164
|
+
while (!await host('status', 'server', '--json').then(({ stdout }) => JSON.parse(stdout).running, () => false)) {
|
|
165
|
+
if (performance.now() > until) throw new Error('Isolated Herdr server did not start');
|
|
166
|
+
await delay(50);
|
|
167
|
+
}
|
|
168
|
+
await host('workspace', 'create', '--cwd', dir, '--label', 'bench-mobile', '--focus');
|
|
169
|
+
const panes = JSON.parse((await host('pane', 'list')).stdout).result.panes;
|
|
170
|
+
assert.equal(panes.length, 1);
|
|
171
|
+
const pane = panes[0];
|
|
172
|
+
await host('pane', 'run', pane.pane_id, "PS1='bench> '; export PS1");
|
|
173
|
+
// Measure Herdr's native controller alone, before Core framing and SSH.
|
|
174
|
+
const oldSocket = process.env.HERDR_SOCKET_PATH;
|
|
175
|
+
process.env.HERDR_SOCKET_PATH = env.HERDR_SOCKET_PATH;
|
|
176
|
+
const nativeSamples = [];
|
|
177
|
+
let native;
|
|
178
|
+
try {
|
|
179
|
+
native = await openTerminal(herdr, pane.terminal_id, 120, 28);
|
|
180
|
+
for (let i = 0; i < rounds; i++) {
|
|
181
|
+
const wanted = `n-${marker()}`;
|
|
182
|
+
const start = performance.now();
|
|
183
|
+
await native.send({ type: 'terminal.input', bytes: Buffer.from(wanted).toString('base64') });
|
|
184
|
+
let text = '';
|
|
185
|
+
while (!text.includes(wanted)) {
|
|
186
|
+
const paint = await deadline(native.frames.next(), 'native echo');
|
|
187
|
+
assert.equal(paint.done, false);
|
|
188
|
+
text += stripVTControlCharacters(paint.value.bytes.toString());
|
|
189
|
+
}
|
|
190
|
+
nativeSamples.push(elapsed(start));
|
|
191
|
+
await native.send({ type: 'terminal.input', bytes: 'FQ==' });
|
|
192
|
+
await delay(150);
|
|
193
|
+
}
|
|
194
|
+
} finally {
|
|
195
|
+
native?.close();
|
|
196
|
+
if (oldSocket === undefined) delete process.env.HERDR_SOCKET_PATH;
|
|
197
|
+
else process.env.HERDR_SOCKET_PATH = oldSocket;
|
|
198
|
+
}
|
|
199
|
+
console.log(JSON.stringify({ route: 'Herdr native controller', keyMs: stats(nativeSamples) }));
|
|
200
|
+
await delay(250);
|
|
201
|
+
const child = spawn(process.execPath, [adapter, '--interactive', 'connect'], { env, cwd: dir });
|
|
202
|
+
const childExit = once(child, 'close');
|
|
203
|
+
try { console.log(JSON.stringify({ route: 'Core adapter (local pipes)', ...await coreBench(child.stdin, child.stdout, 'AB') })); }
|
|
204
|
+
finally { child.stdin.end(); await deadline(childExit, 'adapter exit').catch(() => child.kill()); }
|
|
205
|
+
await delay(250);
|
|
206
|
+
|
|
207
|
+
const account = { id: 'bench', name: 'bench', token: marker() };
|
|
208
|
+
const statePath = join(dir, 'gate.json');
|
|
209
|
+
const state = new State([account], statePath);
|
|
210
|
+
const room = state.addRoom(account.id, 'bench');
|
|
211
|
+
const { device, secret } = state.addDevice(room.room, 'bench-act', 'act');
|
|
212
|
+
cloud = await startCloud({ accounts: [account], statePath, log: () => {}, ssh: { port: 0, keyPath: join(dir, 'gate-key.json') } });
|
|
213
|
+
edge = await startTcpEdge({ gate: `http://127.0.0.1:${cloud.port}`, port: 0 });
|
|
214
|
+
bridge = createSshBridge({
|
|
215
|
+
tuiOnly: true, mobileTuiAdapter: { device: device.id, scope: 'act', script: adapter },
|
|
216
|
+
scopeOf: (id) => publisher.scopeOf(id), send: (id, body) => publisher.send(id, body),
|
|
217
|
+
spawnImpl: (bin, args, opts) => spawn(bin, args, { ...opts, env, cwd: dir }),
|
|
218
|
+
});
|
|
219
|
+
publisher = new HerdGateAgent({ endpoint: `ws://127.0.0.1:${cloud.port}/v1/agent/ws`, roomToken: room.token, adapter: {
|
|
220
|
+
info: () => ({ name: 'bench', version: 'test' }), onFrame: (id, body) => { bridge.handle(id, body); },
|
|
221
|
+
onDevices: () => {}, onPairAsk: () => false,
|
|
222
|
+
} });
|
|
223
|
+
publisher.start();
|
|
224
|
+
await deadline(publisher.firstHello, 'publisher hello');
|
|
225
|
+
const client = new Client();
|
|
226
|
+
client.on('error', () => {});
|
|
227
|
+
try {
|
|
228
|
+
const start = performance.now();
|
|
229
|
+
const ready = once(client, 'ready');
|
|
230
|
+
client.connect({ host: '127.0.0.1', port: edge.port, username: device.id, password: secret, readyTimeout: 5000 });
|
|
231
|
+
await deadline(ready, 'SSH ready');
|
|
232
|
+
const sshReady = elapsed(start);
|
|
233
|
+
const stream = await deadline(new Promise((res, rej) => client.exec('hc connect', (err, channel) => err ? rej(err) : res(channel))), 'SSH exec');
|
|
234
|
+
stream.stderr.resume();
|
|
235
|
+
console.log(JSON.stringify({ route: 'TCP edge + Gate + publisher (all local)', sshReady, ...await coreBench(stream, stream, 'CD') }));
|
|
236
|
+
stream.end();
|
|
237
|
+
} finally { client.end(); }
|
|
238
|
+
} finally {
|
|
239
|
+
bridge?.closeAll();
|
|
240
|
+
publisher?.stop();
|
|
241
|
+
await edge?.close();
|
|
242
|
+
await cloud?.close();
|
|
243
|
+
await host('server', 'stop').catch(() => {});
|
|
244
|
+
if (server && server.exitCode === null && server.signalCode === null) server.kill();
|
|
245
|
+
await rm(dir, { recursive: true, force: true });
|
|
246
|
+
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
#
|
|
2
|
+
# The default still puts the bundle in the PWA's web root. For staging, pass a ZIP path.
|
|
3
3
|
set -euo pipefail
|
|
4
4
|
DIR=$(cd "$(dirname "$0")/.." && pwd)
|
|
5
|
+
[[ $# -le 1 ]] || { echo "usage: build-host-bundle.sh [output.zip]" >&2; exit 2; }
|
|
6
|
+
DEST=${1:-"$DIR/src/web/herdgate-host.zip"}
|
|
5
7
|
OUT=$(mktemp -d)
|
|
6
8
|
trap 'rm -rf "$OUT"' EXIT
|
|
7
|
-
mkdir -p "$OUT/
|
|
8
|
-
cp
|
|
9
|
-
cp
|
|
10
|
-
cp "$DIR/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
mkdir -p "$OUT/host/mod/herdgate" "$OUT/host/plugin/server" "$OUT/host/windows" "$OUT/host/templates"
|
|
10
|
+
cp "$DIR/app-mod/herdgate/"{main.js,mod.json,herdgate.svg} "$OUT/host/mod/herdgate/"
|
|
11
|
+
cp "$DIR/plugin/herdr-plugin.toml" "$OUT/host/plugin/"
|
|
12
|
+
cp "$DIR/plugin/server/"*.mjs "$OUT/host/plugin/server/"
|
|
13
|
+
cp "$DIR/windows/"*.mjs "$OUT/host/windows/"
|
|
14
|
+
cp "$DIR/templates/herdgate-via.mjs" "$OUT/host/templates/"
|
|
15
|
+
cp "$DIR/dist-readme.md" "$OUT/host/README.md"
|
|
16
|
+
( cd "$OUT/host" && zip -qr "$OUT/host.zip" mod plugin windows templates README.md )
|
|
17
|
+
mkdir -p "$(dirname "$DEST")"
|
|
18
|
+
mv -- "$OUT/host.zip" "$DEST"
|
|
19
|
+
printf '%s\n' "$DEST"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Build a fresh, deployable release set; never use local config, installed dependencies or live state.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
|
5
|
+
cd "$ROOT"
|
|
6
|
+
[[ $# -le 1 ]] || { echo 'usage: build-release.sh [output-directory]' >&2; exit 2; }
|
|
7
|
+
VERSION=$(node -p 'require("./package.json").version')
|
|
8
|
+
DEST=${1:-"$ROOT/dist/$VERSION"}
|
|
9
|
+
[[ "$DEST" = /* ]] || DEST="$ROOT/$DEST"
|
|
10
|
+
if [[ -e "$DEST" || -L "$DEST" ]]; then
|
|
11
|
+
echo "release output already exists: $DEST" >&2
|
|
12
|
+
exit 1
|
|
13
|
+
fi
|
|
14
|
+
mkdir -p "$(dirname "$DEST")"
|
|
15
|
+
STAGE=$(mktemp -d "$(dirname "$DEST")/.herdgate-release.XXXXXX")
|
|
16
|
+
trap 'rm -rf "$STAGE"' EXIT
|
|
17
|
+
mkdir "$STAGE/release"
|
|
18
|
+
OUT="$STAGE/release"
|
|
19
|
+
NAME="herdgate-cloud-$VERSION"
|
|
20
|
+
CLOUD="$STAGE/$NAME"
|
|
21
|
+
mkdir -p "$CLOUD/runtime/web/icons" "$CLOUD/scripts"
|
|
22
|
+
|
|
23
|
+
npm run build:cloud -- --outDir "$CLOUD/runtime"
|
|
24
|
+
cp src/web/{index.html,app.js,app.css,app.webmanifest,sw.js,e2e.js} "$CLOUD/runtime/web/"
|
|
25
|
+
cp src/web/icons/{icon.svg,icon-192.png,icon-512.png} "$CLOUD/runtime/web/icons/"
|
|
26
|
+
cp templates/cloud/{Dockerfile,compose.yml,compose.env.example,.dockerignore,herdgate-cloud.service,herdgate-cloud-edge.service,Caddyfile.example,README.md} "$CLOUD/"
|
|
27
|
+
cp templates/cloud/gitignore.example "$CLOUD/.gitignore"
|
|
28
|
+
cp scripts/patch-ssh2.mjs "$CLOUD/scripts/"
|
|
29
|
+
cp package-lock.json "$CLOUD/"
|
|
30
|
+
# Use the same versions and lock as npm, without the host installer or Pi postinstall.
|
|
31
|
+
node --input-type=module - "$CLOUD/package.json" <<'NODE'
|
|
32
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
33
|
+
const { name, version, type, engines, dependencies } = JSON.parse(readFileSync('package.json', 'utf8'));
|
|
34
|
+
const scripts = { start: 'node runtime/cloud/main.js', 'ssh-tcp-edge': 'node runtime/cloud/ssh-tcp-edge.js' };
|
|
35
|
+
writeFileSync(process.argv[2], JSON.stringify({ name, version, type, engines, private: true, scripts, dependencies }, null, 2) + '\n');
|
|
36
|
+
NODE
|
|
37
|
+
|
|
38
|
+
HOST="herdgate-host-$VERSION.zip"
|
|
39
|
+
bash scripts/build-host-bundle.sh "$OUT/$HOST"
|
|
40
|
+
cp "$OUT/$HOST" "$CLOUD/runtime/web/herdgate-host.zip"
|
|
41
|
+
tar -czf "$OUT/$NAME.tar.gz" -C "$STAGE" "$NAME"
|
|
42
|
+
npm pack --ignore-scripts --pack-destination "$OUT"
|
|
43
|
+
( cd "$OUT" && sha256sum -- *.tar.gz *.tgz *.zip > SHA256SUMS )
|
|
44
|
+
# Move the complete set into place; don't nest or replace an existing release.
|
|
45
|
+
mkdir -- "$DEST"
|
|
46
|
+
mv -- "$OUT/"* "$DEST/"
|
|
47
|
+
printf 'Herdgate %s: %s\n' "$VERSION" "$DEST"
|
package/scripts/deploy-vps.sh
CHANGED
|
@@ -1,79 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
#
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
TARGET="${1:?dùng: deploy-vps.sh root@<vps-ip> <gate-hostname>}"
|
|
8
|
-
GATE_HOST="${2:?dùng: deploy-vps.sh root@<vps-ip> <gate-hostname>}"
|
|
9
|
-
if [[ ! "$GATE_HOST" =~ ^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$ ]] || (( ${#GATE_HOST} > 253 )); then
|
|
10
|
-
echo "gate-hostname phải là tên DNS, không có giao thức hay cổng" >&2
|
|
11
|
-
exit 2
|
|
12
|
-
fi
|
|
13
|
-
KEY="$HOME/.ssh/herdgate_deploy"
|
|
14
|
-
DIR=$(cd "$(dirname "$0")/.." && pwd)
|
|
15
|
-
|
|
16
|
-
echo "==> 1/5 sync source + node_modules + state"
|
|
17
|
-
rsync -az --delete -e "ssh -i $KEY" \
|
|
18
|
-
--exclude .git --exclude test --exclude app-mod --exclude plugin/herdr-plugin.toml \
|
|
19
|
-
"$DIR/src" "$DIR/package.json" "$DIR/tsconfig.json" "$DIR/node_modules" "$TARGET:/opt/herdgate/"
|
|
20
|
-
rsync -az -e "ssh -i $KEY" "$HOME/.local/state/herdgate/" "$TARGET:/opt/herdgate-state/"
|
|
21
|
-
|
|
22
|
-
echo "==> 2/5 node 22 (nvm, per-root) + systemd herdgate"
|
|
23
|
-
ssh -i "$KEY" "$TARGET" 'set -e
|
|
24
|
-
if ! command -v node >/dev/null || [ "$(node -v | cut -c2- | cut -d. -f1)" -lt 22 ]; then
|
|
25
|
-
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash >/dev/null
|
|
26
|
-
. ~/.nvm/nvm.sh
|
|
27
|
-
nvm install 22 --silent
|
|
28
|
-
fi
|
|
29
|
-
NODE=$(command -v node)
|
|
30
|
-
mkdir -p ~/.config/systemd/user 2>/dev/null || true
|
|
31
|
-
cat > /etc/systemd/system/herdgate.service <<EOF2
|
|
32
|
-
[Unit]
|
|
33
|
-
Description=herdgate gateway (cloud relay hub)
|
|
34
|
-
After=network-online.target
|
|
35
|
-
Wants=network-online.target
|
|
36
|
-
|
|
37
|
-
[Service]
|
|
38
|
-
ExecStart=$NODE /opt/herdgate/src/cloud/main.ts
|
|
39
|
-
Environment=HERDGATE_BIND=127.0.0.1
|
|
40
|
-
Environment=HERDGATE_PORT=8833
|
|
41
|
-
Environment=HERDGATE_STATE=/opt/herdgate-state/state.json
|
|
42
|
-
Environment=HERDGATE_SSH_PORT=2299
|
|
43
|
-
Environment=HERDGATE_SSH_BIND=0.0.0.0
|
|
44
|
-
Environment=HERDGATE_ACCOUNT_TOKEN=__TOKEN__
|
|
45
|
-
WorkingDirectory=/opt/herdgate
|
|
46
|
-
Restart=always
|
|
47
|
-
RestartSec=3
|
|
48
|
-
|
|
49
|
-
[Install]
|
|
50
|
-
WantedBy=multi-user.target
|
|
51
|
-
EOF2
|
|
52
|
-
'
|
|
53
|
-
|
|
54
|
-
echo "==> 3/5 chèn account token từ unit nhà (base64 qua đường, khỏi sợ ký tự đặc biệt)"
|
|
55
|
-
TOKEN=$(grep -oP 'HERDGATE_ACCOUNT_TOKEN=\K.*' ~/.config/systemd/user/herdgate.service | tr -d '"')
|
|
56
|
-
TOKEN_B64=$(printf %s "$TOKEN" | base64 -w0)
|
|
57
|
-
ssh -i "$KEY" "$TARGET" "python3 -c \"import base64; tok=base64.b64decode('$TOKEN_B64').decode(); p=open('/etc/systemd/system/herdgate.service').read().replace('__TOKEN__',tok); open('/etc/systemd/system/herdgate.service','w').write(p)\""
|
|
58
|
-
|
|
59
|
-
echo "==> 4/5 caddy (TLS tự động) + firewall"
|
|
60
|
-
ssh -i "$KEY" "$TARGET" "GATE_HOST=$GATE_HOST "'set -e
|
|
61
|
-
if ! command -v caddy >/dev/null; then
|
|
62
|
-
apt-get update -qq && apt-get install -y -qq debian-keyring debian-archive-keyring apt-transport-https curl >/dev/null
|
|
63
|
-
curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/gpg.key | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
|
|
64
|
-
curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt | tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null
|
|
65
|
-
apt-get update -qq && apt-get install -y -qq caddy >/dev/null
|
|
66
|
-
fi
|
|
67
|
-
cat > /etc/caddy/Caddyfile <<EOF2
|
|
68
|
-
$GATE_HOST {
|
|
69
|
-
reverse_proxy 127.0.0.1:8833
|
|
70
|
-
}
|
|
71
|
-
EOF2
|
|
72
|
-
systemctl enable --now caddy >/dev/null 2>&1 || systemctl restart caddy
|
|
73
|
-
command -v ufw >/dev/null && ufw allow 80/tcp && ufw allow 443/tcp && ufw allow 2299/tcp comment herdgate-ssh || true
|
|
74
|
-
'
|
|
75
|
-
|
|
76
|
-
echo "==> 5/5 start herdgate"
|
|
77
|
-
ssh -i "$KEY" "$TARGET" 'systemctl daemon-reload && systemctl enable --now herdgate && sleep 3 && systemctl is-active herdgate'
|
|
78
|
-
|
|
79
|
-
echo "OK — kiểm tra: curl https://$GATE_HOST/healthz (sau khi DNS đã trỏ)"
|
|
2
|
+
# Retired: this used to copy a local Gate's credentials and state to the target.
|
|
3
|
+
set -eu
|
|
4
|
+
printf '%s
|
|
5
|
+
' 'deploy-vps.sh was retired; it does not connect to the target.' 'For a clean deployment, run npm run build:release and follow the Cloud archive README.' 'For migration, back up and restore state and keys explicitly. See DEPLOY.md.' >&2
|
|
6
|
+
exit 2
|