@letsrebase/verify 0.0.0-stage → 0.1.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/.claude/commands/rebase-verify.md +118 -0
- package/LICENSE +21 -0
- package/PRIVACY.md +113 -0
- package/README.md +209 -2
- package/SECURITY.md +33 -0
- package/bin/rebase-verify.mjs +699 -0
- package/package.json +38 -4
- package/share/practices.json +224 -0
- package/share/tech-labels.json +57 -0
- package/share/tech-lexicon.json +62 -0
- package/src/adapters/claude-code.mjs +225 -0
- package/src/adapters/codex.mjs +476 -0
- package/src/adapters/cursor.mjs +584 -0
- package/src/adapters/kimi.mjs +594 -0
- package/src/adapters/omp.mjs +59 -0
- package/src/adapters/opencode.mjs +439 -0
- package/src/adapters/pi.mjs +414 -0
- package/src/adapters/tool-vocab.mjs +15 -0
- package/src/agent-run.mjs +10 -0
- package/src/agentic-literacy.mjs +211 -0
- package/src/agents/acp.mjs +364 -0
- package/src/agents/json-answer.mjs +72 -0
- package/src/agents/registry.mjs +370 -0
- package/src/agents/run.mjs +131 -0
- package/src/ai-relationship.mjs +95 -0
- package/src/consistency.mjs +357 -0
- package/src/days.mjs +45 -0
- package/src/digest.mjs +501 -0
- package/src/distribution.mjs +54 -0
- package/src/enrich.mjs +118 -0
- package/src/fingerprint.mjs +134 -0
- package/src/forensics.mjs +178 -0
- package/src/groundedness.mjs +347 -0
- package/src/hub.mjs +136 -0
- package/src/intensity.mjs +132 -0
- package/src/live-status.mjs +79 -0
- package/src/practices-llm.mjs +90 -0
- package/src/practices-rules.mjs +295 -0
- package/src/practices-sample.mjs +111 -0
- package/src/practices.mjs +272 -0
- package/src/profile-llm.mjs +249 -0
- package/src/profile.mjs +497 -0
- package/src/redact.mjs +97 -0
- package/src/sources.mjs +91 -0
- package/src/submit.mjs +65 -0
- package/src/system-text.mjs +3 -0
- package/src/tech-usage.mjs +88 -0
- package/src/trajectory.mjs +264 -0
- package/src/wizard.mjs +411 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// The coding agents rebase-verify can drive, how to detect them (without
|
|
2
|
+
// running them), how to start them over ACP, and how to remove the transcript
|
|
3
|
+
// a run leaves behind.
|
|
4
|
+
import { closeSync, existsSync, lstatSync, openSync, readSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { basename, join } from "node:path";
|
|
7
|
+
import { AGENT_RUN_MARKER } from "../agent-run.mjs";
|
|
8
|
+
import { MODEL } from "../profile-llm.mjs";
|
|
9
|
+
|
|
10
|
+
const homeOf = (ctx = {}) => ctx.home ?? ctx.env?.HOME ?? ctx.env?.USERPROFILE ?? process.env.HOME ?? process.env.USERPROFILE ?? homedir();
|
|
11
|
+
const envOf = (ctx = {}) => ctx.env ?? process.env;
|
|
12
|
+
|
|
13
|
+
// An empty value counts as unset.
|
|
14
|
+
function envGet(env, name) {
|
|
15
|
+
let v = env[name];
|
|
16
|
+
if (v === undefined) {
|
|
17
|
+
const k = Object.keys(env).find((x) => x.toUpperCase() === name.toUpperCase());
|
|
18
|
+
v = k === undefined ? undefined : env[k];
|
|
19
|
+
}
|
|
20
|
+
return v === undefined || v === "" ? undefined : v;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** True when `name` is an executable file on PATH. Never runs it. */
|
|
24
|
+
export function onPath(name, env = process.env, platform = process.platform) {
|
|
25
|
+
const win = platform === "win32";
|
|
26
|
+
const dirs = (envGet(env, "PATH") ?? "")
|
|
27
|
+
.split(win ? ";" : ":")
|
|
28
|
+
.map((d) => (win ? d.replace(/^"(.*)"$/, "$1") : d))
|
|
29
|
+
.filter(Boolean);
|
|
30
|
+
const exts = win ? ["", ...(envGet(env, "PATHEXT") ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
31
|
+
for (const d of dirs) {
|
|
32
|
+
for (const e of exts) {
|
|
33
|
+
try {
|
|
34
|
+
const st = statSync(join(d, name + e));
|
|
35
|
+
if (st.isFile() && (win || (st.mode & 0o111) !== 0)) return true;
|
|
36
|
+
} catch {}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function detector({ bins, homeDirs = [], homeFiles = [], envDirs = [], envFiles = [] }) {
|
|
43
|
+
return (ctx = {}) => {
|
|
44
|
+
const env = envOf(ctx);
|
|
45
|
+
const home = homeOf(ctx);
|
|
46
|
+
for (const b of bins) if (onPath(b, env)) return { installed: true, how: `${b} on PATH` };
|
|
47
|
+
for (const v of envDirs) {
|
|
48
|
+
const d = envGet(env, v);
|
|
49
|
+
if (d && existsSync(d)) return { installed: true, how: `${v} exists` };
|
|
50
|
+
}
|
|
51
|
+
for (const [v, f] of envFiles) {
|
|
52
|
+
const d = envGet(env, v);
|
|
53
|
+
if (d && existsSync(join(d, f))) return { installed: true, how: `${v}/${f} exists` };
|
|
54
|
+
}
|
|
55
|
+
for (const d of homeDirs) if (existsSync(join(home, d))) return { installed: true, how: `~/${d} exists` };
|
|
56
|
+
for (const f of homeFiles) if (existsSync(join(home, f))) return { installed: true, how: `~/${f} exists` };
|
|
57
|
+
return { installed: false, how: "not found" };
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---- cleanup helpers: never follow symlinks, match on the run dir basename only
|
|
62
|
+
function runName(runDir) {
|
|
63
|
+
const n = basename(String(runDir ?? "").replace(/[\\/]+$/, ""));
|
|
64
|
+
return n.startsWith(AGENT_RUN_MARKER) && n.length > AGENT_RUN_MARKER.length ? n : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// the whole run name as one token: not glued to more name characters
|
|
68
|
+
function hasToken(text, name) {
|
|
69
|
+
let i = text.indexOf(name);
|
|
70
|
+
while (i >= 0) {
|
|
71
|
+
const before = text[i - 1];
|
|
72
|
+
const after = text[i + name.length];
|
|
73
|
+
if (!(before && /[A-Za-z0-9]/.test(before)) && !(after && /[A-Za-z0-9]/.test(after))) return true;
|
|
74
|
+
i = text.indexOf(name, i + 1);
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function removeOmp(runDir, ctx) {
|
|
80
|
+
const name = runName(runDir);
|
|
81
|
+
if (!name) return [];
|
|
82
|
+
const root = join(envGet(envOf(ctx), "PI_CODING_AGENT_DIR") ?? join(homeOf(ctx), ".omp", "agent"), "sessions");
|
|
83
|
+
let entries;
|
|
84
|
+
try {
|
|
85
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
86
|
+
} catch {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const removed = [];
|
|
90
|
+
for (const e of entries) {
|
|
91
|
+
if (!hasToken(e.name, name)) continue;
|
|
92
|
+
const p = join(root, e.name);
|
|
93
|
+
try {
|
|
94
|
+
const st = lstatSync(p);
|
|
95
|
+
if (st.isSymbolicLink()) rmSync(p, { force: true }); // the link only, never its target
|
|
96
|
+
else if (st.isDirectory()) rmSync(p, { recursive: true, force: true });
|
|
97
|
+
else continue;
|
|
98
|
+
removed.push(p);
|
|
99
|
+
} catch {}
|
|
100
|
+
}
|
|
101
|
+
return removed;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const FIRST_LINE_CAP = 1024 * 1024;
|
|
105
|
+
|
|
106
|
+
// First line of a file, read in chunks up to 1 MiB. `complete` is false when
|
|
107
|
+
// no newline showed up within the cap.
|
|
108
|
+
function firstLine(file) {
|
|
109
|
+
let fd;
|
|
110
|
+
try {
|
|
111
|
+
fd = openSync(file, "r");
|
|
112
|
+
const chunk = Buffer.alloc(65536);
|
|
113
|
+
const parts = [];
|
|
114
|
+
let pos = 0;
|
|
115
|
+
while (pos < FIRST_LINE_CAP) {
|
|
116
|
+
const n = readSync(fd, chunk, 0, chunk.length, pos);
|
|
117
|
+
if (n <= 0) break;
|
|
118
|
+
const i = chunk.subarray(0, n).indexOf(10);
|
|
119
|
+
if (i >= 0) {
|
|
120
|
+
parts.push(Buffer.from(chunk.subarray(0, i)));
|
|
121
|
+
return { text: Buffer.concat(parts).toString("utf8"), complete: true };
|
|
122
|
+
}
|
|
123
|
+
parts.push(Buffer.from(chunk.subarray(0, n)));
|
|
124
|
+
pos += n;
|
|
125
|
+
}
|
|
126
|
+
return { text: Buffer.concat(parts).toString("utf8"), complete: pos < FIRST_LINE_CAP };
|
|
127
|
+
} catch {
|
|
128
|
+
return { text: "", complete: false };
|
|
129
|
+
} finally {
|
|
130
|
+
if (fd !== undefined) try { closeSync(fd); } catch {}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function recordedCwd({ text, complete }) {
|
|
135
|
+
if (complete) {
|
|
136
|
+
try {
|
|
137
|
+
const j = JSON.parse(text);
|
|
138
|
+
const c = j?.payload?.cwd ?? j?.cwd;
|
|
139
|
+
if (typeof c === "string") return c;
|
|
140
|
+
} catch {}
|
|
141
|
+
}
|
|
142
|
+
const m = /"cwd"\s*:\s*("(?:[^"\\]|\\.)*")/.exec(text);
|
|
143
|
+
if (!m) return undefined;
|
|
144
|
+
try {
|
|
145
|
+
return JSON.parse(m[1]);
|
|
146
|
+
} catch {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function* walkFiles(dir) {
|
|
152
|
+
let entries;
|
|
153
|
+
try {
|
|
154
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
155
|
+
} catch {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
for (const e of entries) {
|
|
159
|
+
const p = join(dir, e.name);
|
|
160
|
+
if (e.isDirectory()) yield* walkFiles(p); // Dirent is lstat-based: symlinked dirs are not entered
|
|
161
|
+
else if (e.isFile()) yield p;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function removeCodex(runDir, ctx) {
|
|
166
|
+
const name = runName(runDir);
|
|
167
|
+
if (!name) return [];
|
|
168
|
+
const env = envOf(ctx);
|
|
169
|
+
const codexHome = envGet(env, "CODEX_HOME") ?? join(homeOf(ctx), ".codex");
|
|
170
|
+
const removed = [];
|
|
171
|
+
for (const f of walkFiles(join(codexHome, "sessions"))) {
|
|
172
|
+
if (!/^rollout-.*\.jsonl$/.test(basename(f))) continue;
|
|
173
|
+
const cwd = recordedCwd(firstLine(f));
|
|
174
|
+
if (typeof cwd !== "string" || !hasToken(cwd, name)) continue;
|
|
175
|
+
try {
|
|
176
|
+
rmSync(f, { force: true });
|
|
177
|
+
removed.push(f);
|
|
178
|
+
} catch {}
|
|
179
|
+
}
|
|
180
|
+
return removed;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Claude Code leaves an empty project dir for the run's cwd (only an empty memory/) even with
|
|
184
|
+
// persistSession off. Removed when its name carries the run name as a whole token and no
|
|
185
|
+
// transcript (.jsonl) is inside.
|
|
186
|
+
function removeClaude(runDir, ctx) {
|
|
187
|
+
const name = runName(runDir);
|
|
188
|
+
if (!name) return [];
|
|
189
|
+
const base = envGet(envOf(ctx), "CLAUDE_CONFIG_DIR") ?? join(homeOf(ctx), ".claude");
|
|
190
|
+
const root = join(base, "projects");
|
|
191
|
+
let entries;
|
|
192
|
+
try {
|
|
193
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
194
|
+
} catch {
|
|
195
|
+
return [];
|
|
196
|
+
}
|
|
197
|
+
const removed = [];
|
|
198
|
+
for (const e of entries) {
|
|
199
|
+
if (!e.isDirectory() || !hasToken(e.name, name)) continue;
|
|
200
|
+
const p = join(root, e.name);
|
|
201
|
+
try {
|
|
202
|
+
if (lstatSync(p).isSymbolicLink()) continue;
|
|
203
|
+
let transcript = false;
|
|
204
|
+
for (const f of walkFiles(p)) if (f.endsWith(".jsonl")) transcript = true;
|
|
205
|
+
if (transcript) continue;
|
|
206
|
+
rmSync(p, { recursive: true, force: true });
|
|
207
|
+
removed.push(p);
|
|
208
|
+
} catch {}
|
|
209
|
+
}
|
|
210
|
+
return removed;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const nothing = () => [];
|
|
214
|
+
|
|
215
|
+
// The npm packages npx downloads for an agent: one source for the spec and for what the person is told.
|
|
216
|
+
const CLAUDE_ADAPTER = "@agentclientprotocol/claude-agent-acp@0.108.1";
|
|
217
|
+
const CODEX_ADAPTER = "@agentclientprotocol/codex-acp@2.7.0";
|
|
218
|
+
|
|
219
|
+
function acpEntry({ id, label, bins, homeDirs, command, args, source = null, loginHint, installHint }) {
|
|
220
|
+
return {
|
|
221
|
+
id,
|
|
222
|
+
label,
|
|
223
|
+
kind: "acp",
|
|
224
|
+
tested: false,
|
|
225
|
+
source,
|
|
226
|
+
detect: detector({ bins, homeDirs }),
|
|
227
|
+
spec: ({ system } = {}) => ({ command, args, systemPromptVia: "inline", system }),
|
|
228
|
+
loginHint,
|
|
229
|
+
installHint,
|
|
230
|
+
cleanup: nothing,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export const AGENTS = [
|
|
235
|
+
{
|
|
236
|
+
id: "omp",
|
|
237
|
+
label: "oh-my-pi",
|
|
238
|
+
kind: "acp",
|
|
239
|
+
tested: true,
|
|
240
|
+
source: "omp",
|
|
241
|
+
detect: detector({ bins: ["omp"] }),
|
|
242
|
+
spec: ({ system } = {}) => ({
|
|
243
|
+
command: "omp",
|
|
244
|
+
args: ["--no-tools", "--no-rules", "--no-skills", "--no-extensions", "acp"],
|
|
245
|
+
env: ["PI_CODING_AGENT_DIR"],
|
|
246
|
+
systemPromptVia: "inline",
|
|
247
|
+
system,
|
|
248
|
+
}),
|
|
249
|
+
loginHint: "omp login",
|
|
250
|
+
installHint: "install omp from https://github.com/can1357/oh-my-pi",
|
|
251
|
+
cleanup: removeOmp,
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
id: "claude-code",
|
|
255
|
+
label: "Claude Code",
|
|
256
|
+
kind: "acp",
|
|
257
|
+
tested: true,
|
|
258
|
+
download: CLAUDE_ADAPTER,
|
|
259
|
+
source: "claude-code",
|
|
260
|
+
detect: detector({ bins: ["claude"], homeDirs: [".claude"], envDirs: ["CLAUDE_CONFIG_DIR"] }),
|
|
261
|
+
spec: ({ system } = {}) => ({
|
|
262
|
+
command: "npx",
|
|
263
|
+
args: ["-y", CLAUDE_ADAPTER],
|
|
264
|
+
env: ["CLAUDE_CONFIG_DIR", "CLAUDE_CODE_EXECUTABLE"],
|
|
265
|
+
sessionMeta: { claudeCode: { options: { tools: [], settingSources: [], persistSession: false } } },
|
|
266
|
+
systemPromptVia: "meta",
|
|
267
|
+
system,
|
|
268
|
+
}),
|
|
269
|
+
loginHint: "run `claude`, then /login",
|
|
270
|
+
installHint: "install Claude Code from https://claude.com/claude-code",
|
|
271
|
+
cleanup: removeClaude,
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
id: "codex",
|
|
275
|
+
label: "Codex",
|
|
276
|
+
kind: "acp",
|
|
277
|
+
tested: true,
|
|
278
|
+
download: CODEX_ADAPTER,
|
|
279
|
+
source: "codex",
|
|
280
|
+
detect: detector({ bins: ["codex"], homeFiles: [".codex/auth.json"], envFiles: [["CODEX_HOME", "auth.json"]] }),
|
|
281
|
+
spec: ({ system } = {}) => ({
|
|
282
|
+
command: "npx",
|
|
283
|
+
args: ["-y", CODEX_ADAPTER],
|
|
284
|
+
env: ["CODEX_HOME", "CODEX_PATH"],
|
|
285
|
+
mode: "read-only",
|
|
286
|
+
systemPromptVia: "param",
|
|
287
|
+
system,
|
|
288
|
+
}),
|
|
289
|
+
loginHint: "codex login",
|
|
290
|
+
installHint: "install Codex from https://github.com/openai/codex",
|
|
291
|
+
cleanup: removeCodex,
|
|
292
|
+
},
|
|
293
|
+
acpEntry({ id: "opencode", label: "opencode", bins: ["opencode"], command: "opencode", args: ["acp"], source: "opencode", loginHint: "opencode auth login", installHint: "install opencode from https://opencode.ai" }),
|
|
294
|
+
acpEntry({ id: "gemini", label: "Gemini CLI", bins: ["gemini"], command: "gemini", args: ["--acp"], loginHint: "run `gemini` to sign in", installHint: "install the Gemini CLI from https://github.com/google-gemini/gemini-cli" }),
|
|
295
|
+
acpEntry({ id: "cursor", label: "Cursor", bins: ["cursor-agent"], command: "cursor-agent", args: ["acp"], source: "cursor", loginHint: "agent login", installHint: "install the Cursor CLI from https://cursor.com/cli" }),
|
|
296
|
+
acpEntry({ id: "copilot", label: "GitHub Copilot", bins: ["copilot"], command: "copilot", args: ["--acp"], loginHint: "run `copilot`, then /login", installHint: "install the Copilot CLI from https://github.com/github/copilot-cli" }),
|
|
297
|
+
acpEntry({ id: "kimi", label: "Kimi", bins: ["kimi"], command: "kimi", args: ["acp"], source: "kimi", loginHint: "kimi login", installHint: "install Kimi CLI from https://github.com/MoonshotAI/kimi-cli" }),
|
|
298
|
+
acpEntry({ id: "qwen", label: "Qwen Code", bins: ["qwen"], command: "qwen", args: ["--acp"], loginHint: "run `qwen` to sign in", installHint: "install Qwen Code from https://github.com/QwenLM/qwen-code" }),
|
|
299
|
+
{
|
|
300
|
+
id: "anthropic-api",
|
|
301
|
+
label: "Anthropic API",
|
|
302
|
+
kind: "api",
|
|
303
|
+
tested: false,
|
|
304
|
+
source: null,
|
|
305
|
+
models: [MODEL],
|
|
306
|
+
detect: (ctx = {}) => (envGet(envOf(ctx), "ANTHROPIC_API_KEY") ? { installed: true, how: "ANTHROPIC_API_KEY is set" } : { installed: false, how: "ANTHROPIC_API_KEY is not set" }),
|
|
307
|
+
spec: () => null,
|
|
308
|
+
loginHint: "set ANTHROPIC_API_KEY",
|
|
309
|
+
installHint: "set ANTHROPIC_API_KEY",
|
|
310
|
+
cleanup: nothing,
|
|
311
|
+
},
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
// Test-only: REBASE_VERIFY_TEST_AGENT=<path to an ACP agent script> adds an
|
|
315
|
+
// entry `test-agent` that runs it with node, handing it the FAKE_* variables.
|
|
316
|
+
// It is how the end-to-end tests drive `generate --agent` without a real agent.
|
|
317
|
+
if (process.env.REBASE_VERIFY_TEST_AGENT) {
|
|
318
|
+
AGENTS.push({
|
|
319
|
+
id: "test-agent",
|
|
320
|
+
label: "Test agent",
|
|
321
|
+
kind: "acp",
|
|
322
|
+
tested: false,
|
|
323
|
+
source: null,
|
|
324
|
+
detect: () => ({ installed: true, how: "REBASE_VERIFY_TEST_AGENT is set" }),
|
|
325
|
+
spec: ({ system } = {}) => ({
|
|
326
|
+
command: process.execPath,
|
|
327
|
+
args: [process.env.REBASE_VERIFY_TEST_AGENT],
|
|
328
|
+
env: Object.keys(process.env).filter((k) => k.startsWith("FAKE_")),
|
|
329
|
+
systemPromptVia: "inline",
|
|
330
|
+
system,
|
|
331
|
+
}),
|
|
332
|
+
loginHint: "log in to the test agent",
|
|
333
|
+
installHint: "the test agent needs no install",
|
|
334
|
+
cleanup: nothing,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Cursor ships its binary as `agent`: only trust that name when ~/.cursor exists.
|
|
339
|
+
const cursor = AGENTS.find((a) => a.id === "cursor");
|
|
340
|
+
const cursorBase = cursor.detect;
|
|
341
|
+
cursor.detect = (ctx = {}) => {
|
|
342
|
+
const r = cursorBase(ctx);
|
|
343
|
+
if (r.installed) return r;
|
|
344
|
+
if (existsSync(join(homeOf(ctx), ".cursor")) && onPath("agent", envOf(ctx))) return { installed: true, how: "agent on PATH and ~/.cursor exists" };
|
|
345
|
+
return r;
|
|
346
|
+
};
|
|
347
|
+
// Started as `cursor-agent acp` when it was found as cursor-agent, `agent acp` when it was found as agent.
|
|
348
|
+
cursor.spec = ({ system, ctx = {} } = {}) => ({
|
|
349
|
+
command: cursorBase(ctx).installed ? "cursor-agent" : "agent",
|
|
350
|
+
args: ["acp"],
|
|
351
|
+
systemPromptVia: "inline",
|
|
352
|
+
system,
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
/** Installed entries with their session counts, in the order the wizard shows them. */
|
|
356
|
+
export function availableAgents({ sessionsBySource = {}, env = process.env, home } = {}) {
|
|
357
|
+
const ctx = { env, home };
|
|
358
|
+
const rows = [];
|
|
359
|
+
for (const a of AGENTS) {
|
|
360
|
+
if (!a.detect(ctx).installed) continue;
|
|
361
|
+
rows.push({ ...a, sessions: a.source ? (sessionsBySource[a.source] ?? 0) : 0 });
|
|
362
|
+
}
|
|
363
|
+
const rank = (a) => (a.id === "anthropic-api" ? 2 : a.tested ? 0 : 1);
|
|
364
|
+
return rows
|
|
365
|
+
.map((r, i) => [r, i])
|
|
366
|
+
.sort(([a, i], [b, j]) => rank(a) - rank(b) || b.sessions - a.sessions || i - j)
|
|
367
|
+
.map(([r]) => r);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export const defaultAgent = (list) => list[0] ?? null;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// The narrative and the model practices through the person's chosen agent:
|
|
2
|
+
// one session for both steps (narrative turn first, then the practices
|
|
3
|
+
// batches), every answer read as the first balanced JSON object and checked
|
|
4
|
+
// before anything is used. The agent's transcript is removed afterwards.
|
|
5
|
+
import { appendFileSync } from "node:fs";
|
|
6
|
+
import { PRACTICES_WARN_WHAT, classifyPractices } from "../practices-llm.mjs";
|
|
7
|
+
import { MODEL, NARRATIVE_SYSTEM, anthropicCall, validateNarrative, warnApiKey } from "../profile-llm.mjs";
|
|
8
|
+
import { AgentAuthError, AgentStartError, ModelNotOfferedError, ToolUseError, startAgent } from "./acp.mjs";
|
|
9
|
+
import { firstJsonObject } from "./json-answer.mjs";
|
|
10
|
+
|
|
11
|
+
// Fixed at session start for agents that take a system prompt there, and put in front of
|
|
12
|
+
// every turn for the ones that do not; the turn's own instructions travel in the message.
|
|
13
|
+
export const RUN_SYSTEM = "Do not use any tool. Answer only from the text in this message, and reply with the JSON object it asks for, nothing else.";
|
|
14
|
+
|
|
15
|
+
const MAX_MODEL = 120;
|
|
16
|
+
|
|
17
|
+
// The hub takes 1 to 120 printable characters or null: cut and clean what the agent reports.
|
|
18
|
+
function boundModel(m) {
|
|
19
|
+
if (typeof m !== "string") return null;
|
|
20
|
+
const clean = [...m.replace(/[\p{C}\p{Z}]/gu, (c) => (c === " " ? " " : ""))].slice(0, MAX_MODEL).join("").trim();
|
|
21
|
+
return clean || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const typed = (e) =>
|
|
25
|
+
e instanceof ToolUseError || e instanceof AgentAuthError || e instanceof AgentStartError || e instanceof ModelNotOfferedError || e?.name === "AbortError";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Run the narrative turn, then the practices batches, with `agent` (a registry entry).
|
|
29
|
+
*
|
|
30
|
+
* @returns {Promise<{ narrative: object, practicesAnswers: object | null, used: { agent: string, model: string | null } }>}
|
|
31
|
+
* `practicesAnswers` is null when there is no `practicesInput`. Typed failures
|
|
32
|
+
* (ToolUseError, AgentAuthError, AgentStartError, ModelNotOfferedError with
|
|
33
|
+
* `offered`, AbortError) are thrown as they are; any other failure names the agent.
|
|
34
|
+
* Every turn after the first runs in a new session on the same agent process, so
|
|
35
|
+
* no answer sees an earlier one. `start` replaces startAgent (tests). `session` (and the
|
|
36
|
+
* `runDir` its "started" event reported) hands in an agent the caller already started, for
|
|
37
|
+
* instance to read its models first; the run closes it like one it started itself.
|
|
38
|
+
* `warn` replaces the API path's stderr notice (the wizard shows it in its consent note).
|
|
39
|
+
*/
|
|
40
|
+
export async function runWithAgent({ agent, model, narrativeInput, practicesInput, onStatus = () => {}, logFile, signal, start = startAgent, session: given = null, runDir: givenDir = null, warn = warnApiKey }) {
|
|
41
|
+
const label = agent.label ?? agent.id;
|
|
42
|
+
const fail = (e) => {
|
|
43
|
+
if (signal?.aborted) return e?.name === "AbortError" ? e : Object.assign(new Error("aborted"), { name: "AbortError" });
|
|
44
|
+
return typed(e) ? e : new Error(`${label}: ${e.message}`);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
async function steps(ask, before = () => {}) {
|
|
48
|
+
before("narrative");
|
|
49
|
+
// like a practices batch: one more try, in a new session, when the answer holds no JSON or does not fit
|
|
50
|
+
let narrative;
|
|
51
|
+
for (let attempt = 0; !narrative; attempt++) {
|
|
52
|
+
onStatus({ step: "narrative", retry: attempt > 0 });
|
|
53
|
+
const text = await ask(NARRATIVE_SYSTEM, JSON.stringify(narrativeInput));
|
|
54
|
+
try {
|
|
55
|
+
narrative = validateNarrative(firstJsonObject(text), agent.id);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (attempt >= 1) throw new Error(`the narrative answer cannot be used: ${e.message}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let practicesAnswers = null;
|
|
61
|
+
if (practicesInput) {
|
|
62
|
+
before("practices");
|
|
63
|
+
practicesAnswers = await classifyPractices(practicesInput, {
|
|
64
|
+
retries: 1,
|
|
65
|
+
onBatch: (b) => onStatus({ step: "practices", ...b }),
|
|
66
|
+
call: ({ system, user }) => ask(system, user),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return { narrative, practicesAnswers };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (agent.kind === "api") {
|
|
73
|
+
if (model && model !== MODEL) throw Object.assign(new ModelNotOfferedError(model), { offered: [MODEL] });
|
|
74
|
+
const call = anthropicCall(process.env.ANTHROPIC_API_KEY, { signal });
|
|
75
|
+
const out = await steps(
|
|
76
|
+
(system, user) => {
|
|
77
|
+
if (signal?.aborted) throw fail(new Error("aborted"));
|
|
78
|
+
return call({ system, user });
|
|
79
|
+
},
|
|
80
|
+
(step) => (step === "narrative" ? warn() : warn(PRACTICES_WARN_WHAT)),
|
|
81
|
+
);
|
|
82
|
+
return { ...out, used: { agent: agent.id, model: MODEL } };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const log = (extra) => {
|
|
86
|
+
if (logFile) appendFileSync(logFile, JSON.stringify({ t: Date.now(), ...extra }) + "\n");
|
|
87
|
+
};
|
|
88
|
+
let runDir = givenDir;
|
|
89
|
+
let session = given;
|
|
90
|
+
try {
|
|
91
|
+
session ??= await start(agent.spec({ system: RUN_SYSTEM }), {
|
|
92
|
+
logFile,
|
|
93
|
+
signal,
|
|
94
|
+
onEvent: (e) => {
|
|
95
|
+
if (e.kind === "started") runDir = e.cwd;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
if (model) {
|
|
99
|
+
try {
|
|
100
|
+
await session.setModel(model);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
if (e instanceof ModelNotOfferedError) e.offered = session.models.map((m) => m.id);
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const used = { agent: agent.id, model: boundModel(model ?? session.currentModel) };
|
|
107
|
+
let turns = 0;
|
|
108
|
+
const out = await steps(async (system, user) => {
|
|
109
|
+
if (turns++ > 0) await session.renew();
|
|
110
|
+
return (await session.prompt(`${system}\n\n${user}`, { system: RUN_SYSTEM })).text;
|
|
111
|
+
});
|
|
112
|
+
return { ...out, used };
|
|
113
|
+
} catch (e) {
|
|
114
|
+
throw fail(e);
|
|
115
|
+
} finally {
|
|
116
|
+
try {
|
|
117
|
+
await session?.close();
|
|
118
|
+
} catch {
|
|
119
|
+
// the transcript cleanup below still runs, and the run's own error stays
|
|
120
|
+
}
|
|
121
|
+
if (runDir) {
|
|
122
|
+
let removed = [];
|
|
123
|
+
try {
|
|
124
|
+
removed = agent.cleanup(runDir) ?? [];
|
|
125
|
+
} catch {
|
|
126
|
+
// a cleanup that cannot run must not hide the run's own result
|
|
127
|
+
}
|
|
128
|
+
log({ dir: "cleanup", removed: removed.length });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// AI relationship: the style with which the candidate works with the model.
|
|
2
|
+
//
|
|
3
|
+
// One continuous axis with two poles:
|
|
4
|
+
// - "directing": you treat the model like a careful junior. Prompts are
|
|
5
|
+
// long, structured, with file paths, numbered steps, acceptance criteria.
|
|
6
|
+
// - "co-thinking": you think out loud with the model. Prompts are short,
|
|
7
|
+
// conversational, full of open questions ("what if", "cosa pensi"),
|
|
8
|
+
// letting the model push back and shape the problem.
|
|
9
|
+
//
|
|
10
|
+
// Few people sit at one pole. Most are a balanced mix and switch mode by
|
|
11
|
+
// context (e.g. directing on security and data, co-thinking on UI and ideas).
|
|
12
|
+
// Co-construction — using the model to define the problem rather than just
|
|
13
|
+
// execute it — lives naturally in the middle of the axis, no third bucket.
|
|
14
|
+
//
|
|
15
|
+
// Anti-leak: the same system-injected prompts (compaction, task-notifications)
|
|
16
|
+
// excluded elsewhere are excluded here too.
|
|
17
|
+
|
|
18
|
+
import { SYSTEM_PROMPT_RE } from "./system-text.mjs";
|
|
19
|
+
|
|
20
|
+
const DIRECTING_PATTERNS = [
|
|
21
|
+
/(^|\n)\s*\d+\.\s/, // numbered list item
|
|
22
|
+
/(^|\n)\s*-\s+\[.\]/, // checkbox
|
|
23
|
+
/\b(step|fase)\s*\d/i,
|
|
24
|
+
/\/Users|file_path|\.tsx?|\.mjs|\.sql\b|\.md\b/i, // file references
|
|
25
|
+
/\b(acceptance|criteria|requirements|spec|verify|verifica|must|should|deve|devi)\b/i,
|
|
26
|
+
/\b(do not|don't|never|exclude|only|exactly|esattamente)\b/i, // hard constraints
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const CO_THINKING_PATTERNS = [
|
|
30
|
+
/\?/,
|
|
31
|
+
/\b(why|how come|what if|secondo te|cosa pensi|cosa ne pensi|thoughts|you think|insieme|pensiamo|capiamo|capire|esplorare|secondo me|ti sembra|ti torna|che ne dici|mi serve capire|chiaro\?)\b/i,
|
|
32
|
+
/\b(vai|fai|dimmi|continua|prosegui|ok\b)\b/i, // short delegations of trust
|
|
33
|
+
/^[a-zàèéìòù]/, // starts lowercase (informal)
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
function scorePrompt(text) {
|
|
37
|
+
if (!text) return null;
|
|
38
|
+
const t = text.trim();
|
|
39
|
+
if (!t || SYSTEM_PROMPT_RE.test(t)) return null;
|
|
40
|
+
const words = t.split(/\s+/).length;
|
|
41
|
+
let direct = 0;
|
|
42
|
+
let coThink = 0;
|
|
43
|
+
for (const r of DIRECTING_PATTERNS) if (r.test(t)) direct++;
|
|
44
|
+
for (const r of CO_THINKING_PATTERNS) if (r.test(t)) coThink++;
|
|
45
|
+
// Length signal — long prompts skew directing, very short skew co-thinking.
|
|
46
|
+
if (words >= 80) direct += 2;
|
|
47
|
+
else if (words >= 30) direct += 1;
|
|
48
|
+
if (words <= 8) coThink += 2;
|
|
49
|
+
else if (words <= 16) coThink += 1;
|
|
50
|
+
return { direct, coThink, words, text: t };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function computeAiRelationship(parsed) {
|
|
54
|
+
let directTotal = 0;
|
|
55
|
+
let coThinkTotal = 0;
|
|
56
|
+
let sampledPrompts = 0;
|
|
57
|
+
const directingExamples = [];
|
|
58
|
+
const coThinkingExamples = [];
|
|
59
|
+
|
|
60
|
+
for (const s of parsed.sessions ?? []) {
|
|
61
|
+
for (const m of s.messages ?? []) {
|
|
62
|
+
if (m.role !== "user") continue;
|
|
63
|
+
const r = scorePrompt(m.textRedacted);
|
|
64
|
+
if (!r) continue;
|
|
65
|
+
sampledPrompts++;
|
|
66
|
+
directTotal += r.direct;
|
|
67
|
+
coThinkTotal += r.coThink;
|
|
68
|
+
// Cherry-pick a few clear examples for each pole (locally only —
|
|
69
|
+
// sent to the LLM as evidence, not embedded in the candidate.json).
|
|
70
|
+
if (r.direct >= 3 && r.direct > r.coThink && directingExamples.length < 4) {
|
|
71
|
+
directingExamples.push(r.text.slice(0, 240));
|
|
72
|
+
}
|
|
73
|
+
if (r.coThink >= 2 && r.coThink > r.direct && r.words <= 30 && coThinkingExamples.length < 4) {
|
|
74
|
+
coThinkingExamples.push(r.text.slice(0, 160));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (sampledPrompts === 0) return null;
|
|
80
|
+
|
|
81
|
+
const tot = directTotal + coThinkTotal;
|
|
82
|
+
const directing = tot ? Math.round((directTotal / tot) * 100) : 50;
|
|
83
|
+
const coThinking = 100 - directing;
|
|
84
|
+
let mode = "balanced";
|
|
85
|
+
if (directing >= 65) mode = "directing-leaning";
|
|
86
|
+
else if (coThinking >= 65) mode = "co-thinking-leaning";
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
mode,
|
|
90
|
+
directing,
|
|
91
|
+
coThinking,
|
|
92
|
+
sampledPrompts,
|
|
93
|
+
examples: { directing: directingExamples, coThinking: coThinkingExamples },
|
|
94
|
+
};
|
|
95
|
+
}
|