@caveman-ai/cli 1.0.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.
- package/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/agent-mcp.js +645 -0
- package/dist/agent-skills.generated.js +113 -0
- package/dist/agents.generated.js +315 -0
- package/dist/binaries.generated.js +4 -0
- package/dist/caveman-delegate-mcp.mjs +178 -0
- package/dist/index.js +14654 -0
- package/dist/learn-tui.js +61 -0
- package/dist/native-hook-fast.js +456 -0
- package/dist/native-pack.generated.js +168 -0
- package/dist/portable-command.js +30 -0
- package/dist/portable-process.mjs +79 -0
- package/dist/practices.generated.js +3030 -0
- package/dist/recipes.generated.js +148 -0
- package/dist/reserved-verbs.generated.js +60 -0
- package/dist/verified-methods.mirror.js +8 -0
- package/package.json +39 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// caveman-delegate-mcp — stdio MCP server exposing ONE tool: caveman_delegate.
|
|
3
|
+
// Runs a bounded subtask in the pi harness (measured ~4.5k-token prefix vs a
|
|
4
|
+
// ~30k-token minimum for a stripped Claude Code child; see
|
|
5
|
+
// docs/research/agent-efficiency/2026-08-07-harness-routing.md) and returns
|
|
6
|
+
// the worker's report plus its provider-reported usage. Dependency-free.
|
|
7
|
+
//
|
|
8
|
+
// Env:
|
|
9
|
+
// CAVE_DELEGATE_PROVIDER pi provider id (default: openai-codex)
|
|
10
|
+
// CAVE_DELEGATE_MODEL model id (default: model= from ~/.codex/config.toml)
|
|
11
|
+
// CAVE_DELEGATE_PI_BIN pi binary (default: pi)
|
|
12
|
+
// CAVE_DELEGATE_PI_DIR PI_CODING_AGENT_DIR override (isolation/testing)
|
|
13
|
+
// CAVE_DELEGATE_TIMEOUT_MS worker timeout (default: 300000)
|
|
14
|
+
import { spawn } from "node:child_process";
|
|
15
|
+
import { mkdtempSync, readdirSync, readFileSync, existsSync, rmSync } from "node:fs";
|
|
16
|
+
import { tmpdir, homedir } from "node:os";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { createInterface } from "node:readline";
|
|
19
|
+
import { delegateSpawnOptions, killProcessTree, portableInvocation } from "./portable-process.mjs";
|
|
20
|
+
|
|
21
|
+
const PROTOCOL_FALLBACK = "2025-06-18";
|
|
22
|
+
const DEFAULT_TIMEOUT_MS = 300_000;
|
|
23
|
+
|
|
24
|
+
function delegateTimeoutMs() {
|
|
25
|
+
const parsed = Number(process.env.CAVE_DELEGATE_TIMEOUT_MS ?? DEFAULT_TIMEOUT_MS);
|
|
26
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : DEFAULT_TIMEOUT_MS;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function defaultModel() {
|
|
30
|
+
if (process.env.CAVE_DELEGATE_MODEL) return process.env.CAVE_DELEGATE_MODEL;
|
|
31
|
+
try {
|
|
32
|
+
const toml = readFileSync(join(homedir(), ".codex", "config.toml"), "utf8");
|
|
33
|
+
const m = toml.match(/^\s*model\s*=\s*"([^"]+)"/m);
|
|
34
|
+
if (m) return m[1];
|
|
35
|
+
} catch {}
|
|
36
|
+
return "gpt-5.2-codex";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function collectUsage(sessionDir) {
|
|
40
|
+
const sum = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 };
|
|
41
|
+
let saw = false;
|
|
42
|
+
try {
|
|
43
|
+
for (const f of readdirSync(sessionDir)) {
|
|
44
|
+
if (!f.endsWith(".jsonl")) continue;
|
|
45
|
+
for (const line of readFileSync(join(sessionDir, f), "utf8").split("\n")) {
|
|
46
|
+
if (!line.trim()) continue;
|
|
47
|
+
let obj;
|
|
48
|
+
try { obj = JSON.parse(line); } catch { continue; }
|
|
49
|
+
const u = obj?.message?.usage ?? obj?.usage;
|
|
50
|
+
if (!u || typeof u.input !== "number") continue;
|
|
51
|
+
saw = true;
|
|
52
|
+
sum.input += u.input || 0;
|
|
53
|
+
sum.output += u.output || 0;
|
|
54
|
+
sum.cacheRead += u.cacheRead || 0;
|
|
55
|
+
sum.cacheWrite += u.cacheWrite || 0;
|
|
56
|
+
sum.cost += u.cost?.total || 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch {}
|
|
60
|
+
return saw ? sum : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function runWorker(task, cwd) {
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
const sessionDir = mkdtempSync(join(tmpdir(), "cave-delegate-"));
|
|
66
|
+
const provider = process.env.CAVE_DELEGATE_PROVIDER || "openai-codex";
|
|
67
|
+
const args = [
|
|
68
|
+
"--provider", provider,
|
|
69
|
+
"--model", defaultModel(),
|
|
70
|
+
"--session-dir", sessionDir,
|
|
71
|
+
"-p", task,
|
|
72
|
+
];
|
|
73
|
+
const env = { ...process.env, CAVEMAN_DELEGATE_CHILD: "1" };
|
|
74
|
+
if (process.env.CAVE_DELEGATE_PI_DIR) env.PI_CODING_AGENT_DIR = process.env.CAVE_DELEGATE_PI_DIR;
|
|
75
|
+
let invocation;
|
|
76
|
+
try {
|
|
77
|
+
invocation = portableInvocation(process.env.CAVE_DELEGATE_PI_BIN || "pi", args, process.platform, env);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
try { rmSync(sessionDir, { recursive: true, force: true }); } catch {}
|
|
80
|
+
resolve({ code: -1, out: "", err: String(error), usage: null });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const child = spawn(invocation.command, invocation.args, {
|
|
84
|
+
cwd: cwd && existsSync(cwd) ? cwd : process.cwd(),
|
|
85
|
+
env,
|
|
86
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
87
|
+
...delegateSpawnOptions(),
|
|
88
|
+
});
|
|
89
|
+
let out = "", err = "";
|
|
90
|
+
child.stdout.on("data", (d) => (out += d));
|
|
91
|
+
child.stderr.on("data", (d) => (err += d));
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
void killProcessTree(child).catch(() => {
|
|
94
|
+
try { child.kill("SIGKILL"); } catch {}
|
|
95
|
+
});
|
|
96
|
+
}, delegateTimeoutMs());
|
|
97
|
+
let settled = false;
|
|
98
|
+
const finish = (code, error, includeUsage) => {
|
|
99
|
+
if (settled) return;
|
|
100
|
+
settled = true;
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
const usage = includeUsage ? collectUsage(sessionDir) : null;
|
|
103
|
+
try { rmSync(sessionDir, { recursive: true, force: true }); } catch {}
|
|
104
|
+
resolve({ code, out: out.trim(), err: error, usage });
|
|
105
|
+
};
|
|
106
|
+
child.on("close", (code) => {
|
|
107
|
+
finish(code ?? -1, err.trim(), true);
|
|
108
|
+
});
|
|
109
|
+
child.on("error", (e) => {
|
|
110
|
+
finish(-1, String(e), false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const TOOL = {
|
|
116
|
+
name: "caveman_delegate",
|
|
117
|
+
description:
|
|
118
|
+
"Run a bounded, mechanical subtask (search, test run, log triage, bulk edit) in a token-efficient worker agent and return its report. Prefer this over spawning a subagent for bounded mechanical work.",
|
|
119
|
+
inputSchema: {
|
|
120
|
+
type: "object",
|
|
121
|
+
properties: {
|
|
122
|
+
task: { type: "string", description: "Complete, self-contained task instructions for the worker" },
|
|
123
|
+
cwd: { type: "string", description: "Working directory (default: current)" },
|
|
124
|
+
},
|
|
125
|
+
required: ["task"],
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
async function handle(msg) {
|
|
130
|
+
const { id, method, params } = msg;
|
|
131
|
+
if (method === "initialize") {
|
|
132
|
+
return {
|
|
133
|
+
protocolVersion: params?.protocolVersion || PROTOCOL_FALLBACK,
|
|
134
|
+
capabilities: { tools: { listChanged: false } },
|
|
135
|
+
serverInfo: { name: "caveman-delegate", version: "0.1.0" },
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (method === "tools/list") return { tools: [TOOL] };
|
|
139
|
+
if (method === "ping") return {};
|
|
140
|
+
if (method === "tools/call") {
|
|
141
|
+
if (params?.name !== TOOL.name) throw new Error(`unknown tool: ${params?.name}`);
|
|
142
|
+
if (process.env.CAVEMAN_DELEGATE_CHILD === "1") throw new Error("cave_delegate_depth_exceeded");
|
|
143
|
+
const task = params?.arguments?.task;
|
|
144
|
+
if (!task || typeof task !== "string") throw new Error("cave_delegate_missing_task");
|
|
145
|
+
const r = await runWorker(task, params?.arguments?.cwd);
|
|
146
|
+
const lines = [r.out || "(worker produced no output)"];
|
|
147
|
+
if (r.usage) {
|
|
148
|
+
const u = r.usage;
|
|
149
|
+
let usageLine = `delegate usage (measured, provider-reported): input ${u.input + u.cacheRead + u.cacheWrite} (cacheRead ${u.cacheRead}, cacheWrite ${u.cacheWrite}), output ${u.output}`;
|
|
150
|
+
if (u.cost > 0) usageLine += `, cost $${u.cost.toFixed(4)}`;
|
|
151
|
+
lines.push("---", usageLine);
|
|
152
|
+
}
|
|
153
|
+
if (r.code !== 0) {
|
|
154
|
+
lines.push("---", `worker exit ${r.code}: ${r.err.slice(-500)}`);
|
|
155
|
+
return { content: [{ type: "text", text: lines.join("\n") }], isError: true };
|
|
156
|
+
}
|
|
157
|
+
return { content: [{ type: "text", text: lines.join("\n") }] };
|
|
158
|
+
}
|
|
159
|
+
if (method?.startsWith("notifications/")) return undefined;
|
|
160
|
+
throw Object.assign(new Error(`method not found: ${method}`), { code: -32601 });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const rl = createInterface({ input: process.stdin, terminal: false });
|
|
164
|
+
rl.on("line", async (line) => {
|
|
165
|
+
if (!line.trim()) return;
|
|
166
|
+
let msg;
|
|
167
|
+
try { msg = JSON.parse(line); } catch { return; }
|
|
168
|
+
if (msg.id === undefined) { try { await handle(msg); } catch {} return; }
|
|
169
|
+
try {
|
|
170
|
+
const result = await handle(msg);
|
|
171
|
+
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: msg.id, result }) + "\n");
|
|
172
|
+
} catch (e) {
|
|
173
|
+
process.stdout.write(JSON.stringify({
|
|
174
|
+
jsonrpc: "2.0", id: msg.id,
|
|
175
|
+
error: { code: e.code || -32000, message: e.message || String(e) },
|
|
176
|
+
}) + "\n");
|
|
177
|
+
}
|
|
178
|
+
});
|