harnery 0.42.0 → 0.43.0

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.
Files changed (77) hide show
  1. package/dist/commander.d.ts +16 -0
  2. package/dist/commander.d.ts.map +1 -1
  3. package/dist/commander.js +18 -0
  4. package/dist/commands/agents.d.ts.map +1 -1
  5. package/dist/commands/agents.js +54 -4
  6. package/dist/commands/tunnel.d.ts +65 -0
  7. package/dist/commands/tunnel.d.ts.map +1 -1
  8. package/dist/commands/tunnel.js +290 -101
  9. package/dist/commands/web-fetch.d.ts +3 -2
  10. package/dist/commands/web-fetch.d.ts.map +1 -1
  11. package/dist/commands/web-fetch.js +25 -19
  12. package/dist/commands/web.d.ts +7 -1
  13. package/dist/commands/web.d.ts.map +1 -1
  14. package/dist/commands/web.js +9 -3
  15. package/dist/core/agents/render/prompt-context.d.ts.map +1 -1
  16. package/dist/core/agents/render/prompt-context.js +11 -5
  17. package/dist/core/agents/render/session-context.d.ts.map +1 -1
  18. package/dist/core/agents/render/session-context.js +57 -13
  19. package/dist/core/agents/state/names.d.ts +43 -3
  20. package/dist/core/agents/state/names.d.ts.map +1 -1
  21. package/dist/core/agents/state/names.js +87 -3
  22. package/dist/core/events/v3/support-storage/pack-reader.d.ts.map +1 -1
  23. package/dist/core/events/v3/support-storage/pack-reader.js +42 -16
  24. package/dist/core/hooks/cli.js +87 -18
  25. package/dist/core/hooks/fork-lineage.d.ts +40 -0
  26. package/dist/core/hooks/fork-lineage.d.ts.map +1 -0
  27. package/dist/core/hooks/fork-lineage.js +62 -0
  28. package/dist/core/hooks/resolve/transcript.d.ts +16 -0
  29. package/dist/core/hooks/resolve/transcript.d.ts.map +1 -1
  30. package/dist/core/hooks/resolve/transcript.js +84 -0
  31. package/dist/core/hooks/shell-rm.d.ts +2 -0
  32. package/dist/core/hooks/shell-rm.d.ts.map +1 -0
  33. package/dist/core/hooks/shell-rm.js +105 -0
  34. package/dist/core/hooks/shell-tokens.d.ts +12 -0
  35. package/dist/core/hooks/shell-tokens.d.ts.map +1 -0
  36. package/dist/core/hooks/shell-tokens.js +97 -0
  37. package/dist/core/hooks/shell-waiter.d.ts.map +1 -1
  38. package/dist/core/hooks/shell-waiter.js +3 -93
  39. package/dist/core/semantic/contract.d.ts +5 -5
  40. package/dist/core/storage/process-log.d.ts +12 -0
  41. package/dist/core/storage/process-log.d.ts.map +1 -1
  42. package/dist/core/storage/process-log.js +10 -2
  43. package/dist/lib/tunnel/allowlist.d.ts +44 -0
  44. package/dist/lib/tunnel/allowlist.d.ts.map +1 -0
  45. package/dist/lib/tunnel/allowlist.js +204 -0
  46. package/dist/lib/tunnel/current-address.d.ts +41 -0
  47. package/dist/lib/tunnel/current-address.d.ts.map +1 -0
  48. package/dist/lib/tunnel/current-address.js +123 -0
  49. package/dist/lib/tunnel/gate.js +13 -6
  50. package/dist/lib/tunnel/processes.d.ts +39 -0
  51. package/dist/lib/tunnel/processes.d.ts.map +1 -0
  52. package/dist/lib/tunnel/processes.js +121 -0
  53. package/dist/lib/tunnel/state.d.ts +28 -3
  54. package/dist/lib/tunnel/state.d.ts.map +1 -1
  55. package/dist/lib/tunnel/state.js +98 -27
  56. package/package.json +2 -1
  57. package/src/commander.ts +32 -0
  58. package/src/commands/agents.ts +97 -11
  59. package/src/commands/tunnel.ts +394 -116
  60. package/src/commands/web-fetch.ts +30 -19
  61. package/src/commands/web.ts +9 -3
  62. package/src/core/agents/render/prompt-context.ts +25 -8
  63. package/src/core/agents/render/session-context.ts +83 -16
  64. package/src/core/agents/state/names.ts +117 -6
  65. package/src/core/events/v3/support-storage/pack-reader.ts +45 -19
  66. package/src/core/hooks/cli.ts +106 -17
  67. package/src/core/hooks/fork-lineage.ts +82 -0
  68. package/src/core/hooks/resolve/transcript.ts +88 -0
  69. package/src/core/hooks/shell-rm.ts +103 -0
  70. package/src/core/hooks/shell-tokens.ts +96 -0
  71. package/src/core/hooks/shell-waiter.ts +4 -91
  72. package/src/core/storage/process-log.ts +17 -2
  73. package/src/lib/tunnel/allowlist.ts +208 -0
  74. package/src/lib/tunnel/current-address.ts +158 -0
  75. package/src/lib/tunnel/gate.ts +16 -8
  76. package/src/lib/tunnel/processes.ts +149 -0
  77. package/src/lib/tunnel/state.ts +123 -27
@@ -0,0 +1,149 @@
1
+ // Process and port discovery for the tunnel commands, per platform.
2
+ //
3
+ // Linux and macOS have `pgrep`/`ps`; Linux has `ss`; both have `lsof`. Windows
4
+ // has none of these, so it uses PowerShell for command lines and `netstat` for
5
+ // listening sockets. Every probe is best effort: a missing tool yields "no
6
+ // answer" (null or an empty list), never a thrown error, and the callers fall
7
+ // back to checks that need no tool (a TCP connect probe, a PID check).
8
+
9
+ import { spawnSync } from "node:child_process";
10
+ import { connect } from "node:net";
11
+
12
+ type Run = (
13
+ command: string,
14
+ args: string[],
15
+ ) => { status: number | null; stdout: string; missing: boolean };
16
+
17
+ const runTool: Run = (command, args) => {
18
+ const r = spawnSync(command, args, { encoding: "utf-8", windowsHide: true, timeout: 15_000 });
19
+ const missing = (r.error as NodeJS.ErrnoException | undefined)?.code === "ENOENT";
20
+ return { status: r.status, stdout: typeof r.stdout === "string" ? r.stdout : "", missing };
21
+ };
22
+
23
+ export interface ProcessRow {
24
+ pid: number;
25
+ command: string;
26
+ }
27
+
28
+ /** Parse `ps -axo pid=,command=` output (also the shape the PowerShell probe emits). */
29
+ export function parseProcessRows(output: string): ProcessRow[] {
30
+ const rows: ProcessRow[] = [];
31
+ for (const line of output.split(/\r?\n/)) {
32
+ const m = line.match(/^\s*(\d+)\s+(.*\S)\s*$/);
33
+ if (m) rows.push({ pid: Number(m[1]), command: m[2] as string });
34
+ }
35
+ return rows;
36
+ }
37
+
38
+ const POWERSHELL_PROCESS_LIST =
39
+ "Get-CimInstance Win32_Process | Where-Object { $_.CommandLine } | " +
40
+ 'ForEach-Object { "$($_.ProcessId) $($_.CommandLine)" }';
41
+
42
+ /** Every process with a command line, or null when the platform gives no way to list them. */
43
+ export function listProcesses(
44
+ platform: NodeJS.Platform = process.platform,
45
+ run: Run = runTool,
46
+ ): ProcessRow[] | null {
47
+ if (platform === "win32") {
48
+ for (const shell of ["powershell.exe", "pwsh.exe", "pwsh"]) {
49
+ const r = run(shell, ["-NoProfile", "-NonInteractive", "-Command", POWERSHELL_PROCESS_LIST]);
50
+ if (r.missing) continue;
51
+ if (r.status === 0) return parseProcessRows(r.stdout);
52
+ }
53
+ return null;
54
+ }
55
+ const ps = run("ps", ["-axo", "pid=,command="]);
56
+ return ps.status === 0 ? parseProcessRows(ps.stdout) : null;
57
+ }
58
+
59
+ /**
60
+ * PIDs whose command line matches `pattern`, an extended regular expression
61
+ * (the intersection of `pgrep -f` and JavaScript syntax). Returns [] when
62
+ * nothing matches or no listing tool works.
63
+ */
64
+ export function findPidsByCommandLine(
65
+ pattern: string,
66
+ platform: NodeJS.Platform = process.platform,
67
+ run: Run = runTool,
68
+ ): number[] {
69
+ if (platform !== "win32") {
70
+ // `--` ends options: the provider pattern starts with `--url`, which pgrep
71
+ // would otherwise reject as an unknown flag and report as "no match".
72
+ const r = run("pgrep", ["-f", "--", pattern]);
73
+ if (!r.missing) {
74
+ if (r.status !== 0) return [];
75
+ return r.stdout
76
+ .split("\n")
77
+ .map((line) => Number(line.trim()))
78
+ .filter((pid) => Number.isInteger(pid) && pid > 0);
79
+ }
80
+ }
81
+ const rows = listProcesses(platform, run);
82
+ if (!rows) return [];
83
+ const re = new RegExp(pattern);
84
+ return rows.filter((row) => re.test(row.command)).map((row) => row.pid);
85
+ }
86
+
87
+ /** Ports from `ss -tlnH`: `LISTEN 0 4096 127.0.0.1:9001 0.0.0.0:*`. */
88
+ export function parseSsPorts(output: string): number[] {
89
+ return [...output.matchAll(/:(\d+)\s/g)].map((m) => Number(m[1]));
90
+ }
91
+
92
+ /** Ports from `lsof -nP -iTCP -sTCP:LISTEN` rows ending `127.0.0.1:58055 (LISTEN)`. */
93
+ export function parseLsofPorts(output: string): number[] {
94
+ return [...output.matchAll(/:(\d+) \(LISTEN\)\s*$/gm)].map((m) => Number(m[1]));
95
+ }
96
+
97
+ /** Ports from `netstat -ano -p TCP` rows: `TCP 0.0.0.0:9001 0.0.0.0:0 LISTENING 4`. */
98
+ export function parseNetstatPorts(output: string): number[] {
99
+ return [...output.matchAll(/^\s*TCP\s+\S*:(\d+)\s+\S+\s+LISTENING\b/gim)].map((m) =>
100
+ Number(m[1]),
101
+ );
102
+ }
103
+
104
+ /**
105
+ * Ports with a LISTEN socket, or null when no discovery tool answered.
106
+ *
107
+ * Null is not "no ports": reporting every port as free would let a caller hand
108
+ * out an occupied port, so callers that need a real answer use `portListening`,
109
+ * which falls back to connecting.
110
+ */
111
+ export function listeningPorts(
112
+ platform: NodeJS.Platform = process.platform,
113
+ run: Run = runTool,
114
+ ): Set<number> | null {
115
+ if (platform === "win32") {
116
+ const r = run("netstat", ["-ano", "-p", "TCP"]);
117
+ return !r.missing && r.status === 0 ? new Set(parseNetstatPorts(r.stdout)) : null;
118
+ }
119
+ if (platform === "linux") {
120
+ const ss = run("ss", ["-tlnH"]);
121
+ if (!ss.missing && ss.status === 0) return new Set(parseSsPorts(ss.stdout));
122
+ }
123
+ // macOS and BSD, or Linux without iproute2. lsof reports a failure when any
124
+ // single process is unreadable while stdout still holds usable rows, so
125
+ // judge it on the output.
126
+ const lsof = run("lsof", ["-nP", "-iTCP", "-sTCP:LISTEN"]);
127
+ if (!lsof.missing && lsof.stdout.trim()) return new Set(parseLsofPorts(lsof.stdout));
128
+ return null;
129
+ }
130
+
131
+ /** True when something accepts a TCP connection on 127.0.0.1:<port>. */
132
+ export function connectProbe(port: number, timeoutMs = 400): Promise<boolean> {
133
+ return new Promise((resolve) => {
134
+ const socket = connect({ port, host: "127.0.0.1" });
135
+ const done = (result: boolean) => {
136
+ socket.destroy();
137
+ resolve(result);
138
+ };
139
+ socket.setTimeout(timeoutMs, () => done(false));
140
+ socket.once("connect", () => done(true));
141
+ socket.once("error", () => done(false));
142
+ });
143
+ }
144
+
145
+ /** Whether `port` has a listener, by the platform tool when there is one, else by connecting. */
146
+ export async function portListening(port: number): Promise<boolean> {
147
+ const ports = listeningPorts();
148
+ return ports ? ports.has(port) : connectProbe(port);
149
+ }
@@ -1,22 +1,43 @@
1
1
  // Tunnel config + state persistence + provider helpers. Commands default to
2
2
  // <cwd>/.cache/tunnel/; gitignored, so the allowlist is per-machine.
3
3
 
4
- import { execSync } from "node:child_process";
4
+ import { spawnSync } from "node:child_process";
5
5
  import {
6
+ accessSync,
7
+ chmodSync,
8
+ constants,
6
9
  existsSync,
7
10
  mkdirSync,
8
11
  readdirSync,
9
12
  readFileSync,
13
+ statSync,
10
14
  unlinkSync,
11
15
  writeFileSync,
12
16
  } from "node:fs";
13
- import { resolve } from "node:path";
17
+ import { homedir } from "node:os";
18
+ import { delimiter, join, resolve } from "node:path";
14
19
  import { isPathAllowed } from "./path-scope.ts";
15
20
 
16
- // Tunnel state lives under <root>/.cache/tunnel/. Root defaults to cwd for the
17
- // command surface and can be supplied by callers that already resolved a repo.
18
- function cachePath(tool: string, filename: string, root: string = process.cwd()): string {
19
- const dir = resolve(root, ".cache", tool);
21
+ /** Overrides the directory that holds tunnel state and config (a host keeps it out of `.cache/`). */
22
+ export const TUNNEL_DIR_ENV = "HARNERY_TUNNEL_DIR";
23
+ /** Explicit cloudflared binary, for a host that ships a managed copy. */
24
+ export const CLOUDFLARED_ENV = "HARNERY_CLOUDFLARED";
25
+
26
+ /**
27
+ * Where tunnel state lives: `$HARNERY_TUNNEL_DIR` when set, else
28
+ * `<root>/.cache/tunnel/`. Root defaults to cwd for the command surface and can
29
+ * be supplied by callers that already resolved a repo.
30
+ */
31
+ export function tunnelDir(
32
+ root: string = process.cwd(),
33
+ env: NodeJS.ProcessEnv = process.env,
34
+ ): string {
35
+ const override = env[TUNNEL_DIR_ENV]?.trim();
36
+ return override ? resolve(override) : resolve(root, ".cache", "tunnel");
37
+ }
38
+
39
+ function cachePath(filename: string, root: string = process.cwd()): string {
40
+ const dir = tunnelDir(root);
20
41
  if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
21
42
  return resolve(dir, filename);
22
43
  }
@@ -63,7 +84,16 @@ const DEFAULT_CONFIG: TunnelConfig = {
63
84
  };
64
85
 
65
86
  export interface TunnelConfig {
87
+ /** Exact addresses and CIDR ranges the gate admits (Cloudflare provider). */
66
88
  allowed_ips: string[];
89
+ /**
90
+ * Entries `allow add --current` added for this machine's own public
91
+ * addresses. They are a subset of `allowed_ips`; the next refresh replaces
92
+ * exactly these and never touches an entry added by hand.
93
+ */
94
+ auto_allowed?: string[];
95
+ /** Explicit cloudflared binary; `$HARNERY_CLOUDFLARED` wins over this. */
96
+ cloudflared_path?: string;
67
97
  }
68
98
 
69
99
  export type TunnelProvider = "cloudflare" | "tailscale";
@@ -104,27 +134,31 @@ function normalizeState(raw: TunnelState, fallbackName: string): TunnelState {
104
134
  }
105
135
 
106
136
  export function readConfig(): TunnelConfig {
107
- const p = cachePath("tunnel", CONFIG_FILE);
137
+ const p = cachePath(CONFIG_FILE);
108
138
  if (!existsSync(p)) {
109
139
  writeConfig(DEFAULT_CONFIG);
110
140
  return { ...DEFAULT_CONFIG, allowed_ips: [...DEFAULT_CONFIG.allowed_ips] };
111
141
  }
112
142
  try {
113
- return JSON.parse(readFileSync(p, "utf-8")) as TunnelConfig;
143
+ const parsed = JSON.parse(readFileSync(p, "utf-8")) as Partial<TunnelConfig>;
144
+ return {
145
+ ...parsed,
146
+ allowed_ips: Array.isArray(parsed.allowed_ips) ? parsed.allowed_ips : [],
147
+ };
114
148
  } catch {
115
149
  return { ...DEFAULT_CONFIG, allowed_ips: [...DEFAULT_CONFIG.allowed_ips] };
116
150
  }
117
151
  }
118
152
 
119
153
  export function writeConfig(cfg: TunnelConfig): void {
120
- writeFileSync(cachePath("tunnel", CONFIG_FILE), JSON.stringify(cfg, null, 2));
154
+ writeFileSync(cachePath(CONFIG_FILE), JSON.stringify(cfg, null, 2));
121
155
  }
122
156
 
123
157
  export function readState(
124
158
  name: string = DEFAULT_INSTANCE,
125
159
  root: string = process.cwd(),
126
160
  ): TunnelState | null {
127
- const p = cachePath("tunnel", stateFile(name), root);
161
+ const p = cachePath(stateFile(name), root);
128
162
  if (!existsSync(p)) return null;
129
163
  try {
130
164
  return normalizeState(JSON.parse(readFileSync(p, "utf-8")) as TunnelState, name);
@@ -134,11 +168,11 @@ export function readState(
134
168
  }
135
169
 
136
170
  export function writeState(state: TunnelState, root: string = process.cwd()): void {
137
- writeFileSync(cachePath("tunnel", stateFile(state.name), root), JSON.stringify(state, null, 2));
171
+ writeFileSync(cachePath(stateFile(state.name), root), JSON.stringify(state, null, 2));
138
172
  }
139
173
 
140
174
  export function clearState(name: string = DEFAULT_INSTANCE, root: string = process.cwd()): void {
141
- const p = cachePath("tunnel", stateFile(name), root);
175
+ const p = cachePath(stateFile(name), root);
142
176
  if (existsSync(p)) unlinkSync(p);
143
177
  }
144
178
 
@@ -147,7 +181,7 @@ export function clearState(name: string = DEFAULT_INSTANCE, root: string = proce
147
181
  * `state*.json` under `.cache/tunnel/`; tolerates missing/corrupt files.
148
182
  */
149
183
  export function listStates(root: string = process.cwd()): TunnelState[] {
150
- const dir = resolve(root, ".cache", "tunnel");
184
+ const dir = tunnelDir(root);
151
185
  if (!existsSync(dir)) return [];
152
186
  const out: TunnelState[] = [];
153
187
  for (const file of readdirSync(dir)) {
@@ -226,31 +260,93 @@ export function tunnelServesPaths(state: TunnelState, paths: readonly string[]):
226
260
  return paths.every((path) => isPathAllowed(path, state.allow_paths));
227
261
  }
228
262
 
229
- /**
230
- * Ensure cloudflared is on PATH or installed at ~/.local/bin/cloudflared.
231
- * Auto-downloads on Linux; throws on other platforms with brew hint.
232
- */
233
- export function ensureCloudflared(): string {
263
+ function isRunnableFile(path: string): boolean {
234
264
  try {
235
- execSync("command -v cloudflared", { stdio: "ignore" });
236
- return "cloudflared";
265
+ if (!statSync(path).isFile()) return false;
266
+ if (process.platform === "win32") return true;
267
+ accessSync(path, constants.X_OK);
268
+ return true;
237
269
  } catch {
238
- /* not on PATH; fall through */
270
+ return false;
239
271
  }
240
- const local = `${process.env.HOME}/.local/bin/cloudflared`;
241
- if (existsSync(local)) return local;
272
+ }
273
+
274
+ /** First `name` on PATH (honoring `.exe` on Windows), or null. Needs no shell. */
275
+ function findOnPath(name: string, env: NodeJS.ProcessEnv = process.env): string | null {
276
+ const names = process.platform === "win32" ? [`${name}.exe`, name] : [name];
277
+ for (const dir of (env.PATH ?? env.Path ?? "").split(delimiter)) {
278
+ if (!dir) continue;
279
+ for (const candidate of names) {
280
+ const full = join(dir, candidate);
281
+ if (isRunnableFile(full)) return full;
282
+ }
283
+ }
284
+ return null;
285
+ }
286
+
287
+ /**
288
+ * Resolve the cloudflared binary:
289
+ * 1. `$HARNERY_CLOUDFLARED`, then `cloudflared_path` in the tunnel config: an
290
+ * explicit choice, so a missing or non-runnable file is an error, never a
291
+ * silent fall-through to some other copy.
292
+ * 2. `cloudflared` on PATH.
293
+ * 3. `~/.local/bin/cloudflared`.
294
+ * 4. Linux x86-64 and arm64: download the latest release to (3).
295
+ * Elsewhere it throws with install guidance.
296
+ */
297
+ export function ensureCloudflared(
298
+ env: NodeJS.ProcessEnv = process.env,
299
+ configuredPath: string | undefined = readConfig().cloudflared_path,
300
+ ): string {
301
+ const explicit = env[CLOUDFLARED_ENV]?.trim();
302
+ const fromConfig = configuredPath?.trim();
303
+ for (const [source, path] of [
304
+ [CLOUDFLARED_ENV, explicit],
305
+ ["cloudflared_path in the tunnel config", fromConfig],
306
+ ] as const) {
307
+ if (!path) continue;
308
+ if (isRunnableFile(path)) return path;
309
+ throw new Error(
310
+ `cloudflared is set by ${source} to ${path}, but that file is missing or not runnable.`,
311
+ );
312
+ }
313
+
314
+ const onPath = findOnPath("cloudflared", env);
315
+ if (onPath) return onPath;
316
+ const exe = process.platform === "win32" ? "cloudflared.exe" : "cloudflared";
317
+ const local = join(homedir(), ".local", "bin", exe);
318
+ if (isRunnableFile(local)) return local;
242
319
 
243
- if (process.platform !== "linux") {
320
+ const asset =
321
+ process.platform === "linux"
322
+ ? (
323
+ { x64: "cloudflared-linux-amd64", arm64: "cloudflared-linux-arm64" } as Record<
324
+ string,
325
+ string
326
+ >
327
+ )[process.arch]
328
+ : undefined;
329
+ if (!asset) {
244
330
  throw new Error(
245
- "cloudflared not installed. On macOS: `brew install cloudflared`. " +
331
+ "cloudflared not installed. Install it (macOS: `brew install cloudflared`; Windows: " +
332
+ "`winget install Cloudflare.cloudflared`), or point `$HARNERY_CLOUDFLARED` at a binary. " +
246
333
  "On Linux it auto-installs to ~/.local/bin/cloudflared.",
247
334
  );
248
335
  }
249
336
 
250
337
  process.stderr.write("Installing cloudflared to ~/.local/bin/...\n"); // lint-ok-emission: sync setup phase before structured output; pairs with the inherited stdio of the curl below
251
- execSync(
252
- `curl -fsSL https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o ${local} && chmod +x ${local}`,
338
+ mkdirSync(join(homedir(), ".local", "bin"), { recursive: true });
339
+ const download = spawnSync(
340
+ "curl",
341
+ [
342
+ "-fsSL",
343
+ `https://github.com/cloudflare/cloudflared/releases/latest/download/${asset}`,
344
+ "-o",
345
+ local,
346
+ ],
253
347
  { stdio: "inherit" },
254
348
  );
349
+ if (download.status !== 0) throw new Error("Downloading cloudflared failed; install it by hand.");
350
+ chmodSync(local, 0o755);
255
351
  return local;
256
352
  }