@caveman-ai/cli 1.2.1 → 1.2.4
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/dist/agent-skills.generated.js +2 -2
- package/dist/agents.generated.js +28 -0
- package/dist/binaries.generated.js +1 -1
- package/dist/caveman-pi-extension.mjs +787 -0
- package/dist/index.js +410 -42
- package/dist/native-hook-fast.js +24 -3
- package/dist/portable-command.js +56 -12
- package/dist/portable-process.mjs +17 -7
- package/package.json +4 -4
package/dist/native-hook-fast.js
CHANGED
|
@@ -78,6 +78,11 @@ function profile() {
|
|
|
78
78
|
const mode = policyMode();
|
|
79
79
|
return mode === "record" ? "record-only" : mode === "max" ? "full-max" : "full-safe";
|
|
80
80
|
}
|
|
81
|
+
// Return as soon as the accumulated bytes parse as one complete JSON payload
|
|
82
|
+
// instead of waiting for EOF. The host writes one object and closes, but under
|
|
83
|
+
// the Windows pipe implementation that close can lag arbitrarily (#729/#833),
|
|
84
|
+
// and a hook that blocks on EOF spends the host's whole budget waiting for a
|
|
85
|
+
// close it already has all the data for.
|
|
81
86
|
async function stdin() {
|
|
82
87
|
const chunks = [];
|
|
83
88
|
let bytes = 0;
|
|
@@ -87,6 +92,15 @@ async function stdin() {
|
|
|
87
92
|
if (bytes > 2 * 1024 * 1024)
|
|
88
93
|
throw new Error("hook payload too large");
|
|
89
94
|
chunks.push(value);
|
|
95
|
+
const joined = Buffer.concat(chunks);
|
|
96
|
+
// A partial payload throws and we wait for more bytes.
|
|
97
|
+
try {
|
|
98
|
+
JSON.parse(joined.toString("utf8"));
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
return joined;
|
|
90
104
|
}
|
|
91
105
|
return Buffer.concat(chunks);
|
|
92
106
|
}
|
|
@@ -378,9 +392,16 @@ function fallback(entry) {
|
|
|
378
392
|
}
|
|
379
393
|
catch { /* host remains fail-open */ }
|
|
380
394
|
}
|
|
395
|
+
// Budget for the delegated child. This runs inside a host hook, so an
|
|
396
|
+
// unbounded spawnSync could block the user's turn forever if index.js stalls,
|
|
397
|
+
// and on Windows process spawn is ~10x macOS before antivirus (#819) — the
|
|
398
|
+
// double-spawn alone can exceed the host's cap. Cap it below the host's and
|
|
399
|
+
// fall through to the fail-open path on expiry, the way every other subprocess
|
|
400
|
+
// in this package already does (#799).
|
|
401
|
+
const DELEGATE_TIMEOUT_MS = 3000;
|
|
381
402
|
function delegateToFullCLI(raw, agent) {
|
|
382
403
|
const cli = join(dirname(fileURLToPath(import.meta.url)), "index.js");
|
|
383
|
-
const result = spawnSync(process.execPath, [cli, "native-hook", agent], { input: raw, maxBuffer: 3 * 1024 * 1024, env: process.env });
|
|
404
|
+
const result = spawnSync(process.execPath, [cli, "native-hook", agent], { input: raw, maxBuffer: 3 * 1024 * 1024, env: process.env, timeout: DELEGATE_TIMEOUT_MS });
|
|
384
405
|
if (!result.error && result.status === 0) {
|
|
385
406
|
if (result.stdout?.length)
|
|
386
407
|
process.stdout.write(result.stdout);
|
|
@@ -390,7 +411,7 @@ function delegateToFullCLI(raw, agent) {
|
|
|
390
411
|
}
|
|
391
412
|
async function main() {
|
|
392
413
|
const agentArg = process.argv[2] === "native-hook" ? process.argv[3] : process.argv[2];
|
|
393
|
-
const agent = agentArg === "claude" || agentArg === "codex" || agentArg === "hermes" || agentArg === "gemini" || agentArg === "opencode" ? agentArg : undefined;
|
|
414
|
+
const agent = agentArg === "claude" || agentArg === "codex" || agentArg === "hermes" || agentArg === "gemini" || agentArg === "opencode" || agentArg === "pi" ? agentArg : undefined;
|
|
394
415
|
if (!agent)
|
|
395
416
|
return;
|
|
396
417
|
let raw;
|
|
@@ -449,7 +470,7 @@ async function main() {
|
|
|
449
470
|
else if (mode !== "record" && agent === "claude" && eventName === "PostToolUse" && response?.output_replacement) {
|
|
450
471
|
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: "PostToolUse", updatedToolOutput: response.output_replacement } }));
|
|
451
472
|
}
|
|
452
|
-
else if (mode !== "record" && agent === "opencode" && eventName === "PostToolUse" && response) {
|
|
473
|
+
else if (mode !== "record" && (agent === "opencode" || agent === "pi") && (eventName === "PostToolUse" || eventName === "PostToolUseFailure") && response) {
|
|
453
474
|
process.stdout.write(JSON.stringify(response));
|
|
454
475
|
}
|
|
455
476
|
}
|
package/dist/portable-command.js
CHANGED
|
@@ -1,28 +1,72 @@
|
|
|
1
|
-
import { readFileSync, statSync } from "node:fs";
|
|
2
|
-
import { dirname, resolve } from "node:path";
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, isAbsolute, join, resolve } from "node:path";
|
|
3
3
|
export function parseWindowsNodeShim(source) {
|
|
4
4
|
for (const line of source.split(/\r?\n/)) {
|
|
5
5
|
if (!/(?:\bnode(?:\.exe)?\b|_prog)/i.test(line) || !/%\*/.test(line))
|
|
6
6
|
continue;
|
|
7
|
-
|
|
7
|
+
// Shim-relative target (npm cmd-shim, pnpm/yarn-classic @zkochan forms), or
|
|
8
|
+
// a drive-absolute target (pnpm emits one when the global bin dir and the
|
|
9
|
+
// store sit on different drives — path.relative crosses drives as absolute).
|
|
10
|
+
const match = line.match(/"%(?:dp0%|~dp0)\\([^"\r\n]+\.(?:cjs|mjs|js))"\s+%\*/i)
|
|
11
|
+
?? line.match(/"([A-Za-z]:[\\/][^"\r\n]+\.(?:cjs|mjs|js))"\s+%\*/i);
|
|
8
12
|
if (match)
|
|
9
13
|
return match[1];
|
|
10
14
|
}
|
|
11
15
|
return null;
|
|
12
16
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
function envValue(env, name) {
|
|
18
|
+
const key = Object.keys(env).find((candidate) => candidate.toLowerCase() === name.toLowerCase());
|
|
19
|
+
return key === undefined ? undefined : env[key];
|
|
20
|
+
}
|
|
21
|
+
// Resolve a bare command through PATH/PATHEXT the way Windows does. This used
|
|
22
|
+
// to be the caller's job, and the contract was invisible: hand this function a
|
|
23
|
+
// bare "npx" on win32 and the .cmd test below missed, so it handed the bare
|
|
24
|
+
// name straight to spawn(), which fails with EINVAL rather than launching
|
|
25
|
+
// anything. Every other copy of this helper in the repo resolves internally;
|
|
26
|
+
// so does this one now. Extensionless names resolve only through PATHEXT —
|
|
27
|
+
// npm/pnpm .bin dirs park a non-executable Unix shim under the bare name next
|
|
28
|
+
// to the real .CMD, and probing the bare name first picks the wrong one (#834).
|
|
29
|
+
export function resolveWindowsCommand(command, env) {
|
|
30
|
+
const pathExt = envValue(env, "PATHEXT") ?? ".COM;.EXE;.BAT;.CMD";
|
|
31
|
+
const names = extname(command)
|
|
32
|
+
? [command]
|
|
33
|
+
: pathExt.split(";").map((extension) => `${command}${extension.startsWith(".") ? extension : `.${extension}`}`);
|
|
34
|
+
// A path skips PATH lookup but NOT PATHEXT: handing an extensionless file
|
|
35
|
+
// straight to execFile is the exact EFTYPE this function exists to prevent.
|
|
36
|
+
if (isAbsolute(command) || /[\\/]/.test(command)) {
|
|
37
|
+
for (const name of names)
|
|
38
|
+
if (existsSync(name))
|
|
39
|
+
return name;
|
|
40
|
+
return existsSync(command) ? command : undefined;
|
|
41
|
+
}
|
|
42
|
+
for (const directory of (envValue(env, "PATH") ?? "").split(";")) {
|
|
43
|
+
if (!directory)
|
|
44
|
+
continue;
|
|
45
|
+
for (const name of names) {
|
|
46
|
+
const candidate = join(directory, name);
|
|
47
|
+
if (existsSync(candidate))
|
|
48
|
+
return candidate;
|
|
49
|
+
}
|
|
16
50
|
}
|
|
17
|
-
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
export function portableInvocation(command, args, platform = process.platform, env = process.env) {
|
|
54
|
+
if (platform !== "win32")
|
|
55
|
+
return { command, args: [...args] };
|
|
56
|
+
const executable = resolveWindowsCommand(command, env) ?? command;
|
|
57
|
+
if (!/\.(?:cmd|bat)$/i.test(executable))
|
|
58
|
+
return { command: executable, args: [...args] };
|
|
59
|
+
const stat = statSync(executable);
|
|
18
60
|
if (!stat.isFile() || stat.size > 256 * 1024) {
|
|
19
|
-
throw new Error(`cannot safely launch Windows command shim: ${
|
|
61
|
+
throw new Error(`cannot safely launch Windows command shim: ${executable}`);
|
|
20
62
|
}
|
|
21
|
-
const
|
|
22
|
-
if (!
|
|
23
|
-
throw new Error(`cannot safely launch non-Node Windows command shim: ${
|
|
63
|
+
const shimScript = parseWindowsNodeShim(readFileSync(executable, "utf8"));
|
|
64
|
+
if (!shimScript) {
|
|
65
|
+
throw new Error(`cannot safely launch non-Node Windows command shim: ${executable}; install a native .exe`);
|
|
24
66
|
}
|
|
25
|
-
const script =
|
|
67
|
+
const script = /^[A-Za-z]:[\\/]/.test(shimScript)
|
|
68
|
+
? shimScript
|
|
69
|
+
: resolve(dirname(executable), ...shimScript.split(/[\\/]+/));
|
|
26
70
|
if (!statSync(script).isFile()) {
|
|
27
71
|
throw new Error(`Windows command shim target is missing: ${script}`);
|
|
28
72
|
}
|
|
@@ -26,20 +26,30 @@ function resolveWindowsCommand(command, env) {
|
|
|
26
26
|
return null;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
export function parseWindowsNodeShim(source) {
|
|
30
|
+
for (const line of source.split(/\r?\n/)) {
|
|
31
|
+
if (!/(?:\bnode(?:\.exe)?\b|_prog)/i.test(line) || !/%\*/.test(line)) continue;
|
|
32
|
+
// Shim-relative target (npm cmd-shim, pnpm/yarn-classic @zkochan forms), or
|
|
33
|
+
// a drive-absolute target (pnpm emits one when the global bin dir and the
|
|
34
|
+
// store sit on different drives — path.relative crosses drives as absolute).
|
|
35
|
+
const match = line.match(/"%(?:dp0%|~dp0)\\([^"\r\n]+\.(?:cjs|mjs|js))"\s+%\*/i)
|
|
36
|
+
|| line.match(/"([A-Za-z]:[\\/][^"\r\n]+\.(?:cjs|mjs|js))"\s+%\*/i);
|
|
37
|
+
if (match) return match[1];
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
29
42
|
export function portableInvocation(command, args, platform = process.platform, env = process.env) {
|
|
30
43
|
if (platform !== "win32") return { command, args: [...args] };
|
|
31
44
|
const executable = resolveWindowsCommand(command, env) ?? command;
|
|
32
45
|
if (!/\.(?:cmd|bat)$/i.test(executable)) return { command: executable, args: [...args] };
|
|
33
46
|
const stat = statSync(executable);
|
|
34
47
|
if (!stat.isFile() || stat.size > 256 * 1024) throw new Error(`unsafe Windows command shim: ${executable}`);
|
|
35
|
-
|
|
36
|
-
for (const line of readFileSync(executable, "utf8").split(/\r?\n/)) {
|
|
37
|
-
if (!/(?:\bnode(?:\.exe)?\b|_prog)/i.test(line) || !/%\*/.test(line)) continue;
|
|
38
|
-
const match = line.match(/"%(?:dp0%|~dp0)\\([^"\r\n]+\.(?:cjs|mjs|js))"\s+%\*/i);
|
|
39
|
-
if (match) { relativeScript = match[1]; break; }
|
|
40
|
-
}
|
|
48
|
+
const relativeScript = parseWindowsNodeShim(readFileSync(executable, "utf8"));
|
|
41
49
|
if (!relativeScript) throw new Error(`non-Node Windows command shim: ${executable}`);
|
|
42
|
-
const script =
|
|
50
|
+
const script = /^[A-Za-z]:[\\/]/.test(relativeScript)
|
|
51
|
+
? relativeScript
|
|
52
|
+
: resolve(dirname(executable), ...relativeScript.split(/[\\/]+/));
|
|
43
53
|
if (!statSync(script).isFile()) throw new Error(`Windows command shim target missing: ${script}`);
|
|
44
54
|
return { command: process.execPath, args: [script, ...args] };
|
|
45
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caveman-ai/cli",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Caveman CLI — wrap coding agents with recoverable local context compression and truthful metering (`caveman claude`), then drive Caveman Cloud from terminal. Compression and metering run in companion Go binaries: run `caveman setup` to see install state. Local savings stay inferred until verified.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"LICENSE"
|
|
27
27
|
],
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node scripts/shebang.mjs",
|
|
29
|
+
"build": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node scripts/bundle-pi-extension.mjs && node scripts/shebang.mjs",
|
|
30
30
|
"prepack": "npm run build",
|
|
31
31
|
"lint": "tsc --noEmit",
|
|
32
|
-
"test": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node scripts/shebang.mjs && node --test --test-concurrency=4 tests/*.runtime.mjs",
|
|
32
|
+
"test": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node scripts/bundle-pi-extension.mjs && node scripts/shebang.mjs && node --test --test-force-exit --test-concurrency=4 tests/*.runtime.mjs",
|
|
33
33
|
"bench:native": "node scripts/benchmark-native-hooks.mjs",
|
|
34
|
-
"test:fast": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node --test --test-concurrency=4 --test-reporter=dot tests/agent-native.runtime.mjs tests/billing.runtime.mjs tests/capability-config.runtime.mjs tests/login.runtime.mjs tests/mcp-surface.runtime.mjs tests/porcelain.runtime.mjs tests/practice-registry.runtime.mjs tests/skills.runtime.mjs tests/status.runtime.mjs tests/subscription-recovery.runtime.mjs tests/wrap-gate.runtime.mjs"
|
|
34
|
+
"test:fast": "node scripts/gen-binaries.mjs && node scripts/gen-wedge-installer.mjs && node scripts/compile-registries.mjs && tsc && node scripts/bundle-delegate.mjs && node scripts/bundle-tui.mjs && node scripts/bundle-pi-extension.mjs && node --test --test-force-exit --test-concurrency=4 --test-reporter=dot tests/agent-native.runtime.mjs tests/billing.runtime.mjs tests/capability-config.runtime.mjs tests/login.runtime.mjs tests/mcp-surface.runtime.mjs tests/porcelain.runtime.mjs tests/practice-registry.runtime.mjs tests/skills.runtime.mjs tests/status.runtime.mjs tests/subscription-recovery.runtime.mjs tests/wrap-gate.runtime.mjs"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {},
|
|
37
37
|
"devDependencies": {
|