@inetafrica/open-claudia 2.7.4 → 2.7.5
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/CHANGELOG.md +4 -1
- package/bin/tool.js +110 -11
- package/core/system-prompt.js +2 -1
- package/core/tool-repo.js +35 -6
- package/core/tools.js +107 -8
- package/package.json +1 -1
- package/test-tools.js +76 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## v2.7.
|
|
3
|
+
## v2.7.5
|
|
4
|
+
- **Tool composition with inherited approval — a composing tool cannot self-approve (Phase 4).** Higher-order tools may shell into lower tools (report → kticket + kchat), but the callee's risk gate stays intact: `tool run` now exports the tier the **outermost** invocation's flags acknowledged (`OPEN_CLAUDIA_APPROVED_TIER`) plus the call chain (`OPEN_CLAUDIA_TOOL_STACK`) into the child env, and a nested run gates on that inherited tier while **ignoring its own argv flags** — so a composing tool that hardcodes `--yes-destructive` in its source is refused with a message pointing at the outer re-run. An inherited tier passes through unchanged (depth-safe), a tool already in the chain is refused (cycle guard), and each nested run still stamps its own telemetry. Verified live: fabricated approval blocked at exit 3, legit outer `--yes-destructive` flowing through, self-call cycle caught at depth 1.
|
|
5
|
+
- **`_lib/` shared plumbing with dependent journaling (Phase 4).** Genuinely shared code (central auth, mongo-exec) now lives in `tools/_lib` — tiny modules tools import via a literal `_lib/<name>` path, never tools themselves (the registry, surfacing index, and legacy migration all skip the `_` prefix). New `tool lib list|show|set`: `set` writes the module (secret-blocking, traversal-safe), finds every dependent by a boundary-safe source grep (`central-auth` matches `_lib/central-auth.js` and the `path.join` form, **not** `central-auth-v2`), journals each dependent `unverified since _lib change — re-verified by next real use`, and commits the lot in ONE commit naming the dependents — the PLAN's blast-radius rule, since a lib change can break several tools at once and health is verified by use, not smoke tests.
|
|
6
|
+
- **Umbrella grouping from registry tags (Phase 4).** Tools gain an optional `tags:` header field (`--tags` on `scaffold`/`add`/`set`, sanitised, strong-match in `tool search`); `tool list` renders the first tag as an umbrella group — `kazee/{kchat,kticket}`, `infra/{prod-k8s}` — with untagged tools flat below. Purely presentational: "Kazee" stays a registry grouping, never a binary. The fixed system-prompt tools section gains one composition bullet (approval is inherited, never fabricated; `_lib` marks dependents unverified). Test coverage: tags round-trip, lib dependent grep boundaries, setLib journaling, and the full nested-gate matrix including the fabrication and cycle refusals.
|
|
4
7
|
- **The dream now audits tool usage — raw-op detector + usage KPI (Phase 3/item 9 + enforcement 5c).** The guard log knew about *blocked* commands (denies) and *authorised* one-offs (`keyring exec` bypasses), but operational work done raw that the gate didn't cover — inline heredoc scripts fed to an interpreter, raw DB clients, mutating `kubectl` — left no trace, so the tool-usage KPI had a blind spot exactly where the "repeated heredocs slip through" failure lives. New deterministic detector in `core/tool-guard.js` (`matchRawOp`/`noteRawOp`): high-precision patterns (`heredoc-interpreter`, `db-client`, `kubectl-mutate`), wired into the runner's Bash-stream chokepoint so every command is observed — **observation only, never blocking**; the authorised channels and anything the deny-gate already owns are skipped so one command never yields two guard events. Reads (`kubectl get/logs`, `rollout status`, `cat <<EOF` file writes, mentioning a client in grep) stay unflagged.
|
|
5
8
|
- **Doc/code drift flag completes the dream tool-pass.** Alongside the existing hygiene flags (dangling pack links, cold never-run tools, near-duplicate pairs), the pass now flags tools whose source was edited after TOOL.md was last touched, and verbs that telemetry proves are in real use but the docs never mention (`tools.docDrift()`; `help`/`(default)` ignored). Flag only — the fix is a `tool note`. Zero false flags on the live 12-tool registry at ship time.
|
|
6
9
|
- **Nightly KPI in the dream report, computed from stamps — never prose.** The dream's tool pass now appends `📈 Tool usage since last dream`: % of operational actions via `tool run` (target >90%, denominator = tool runs + escape-hatch bypasses + raw-op sightings; gate denies reported separately since blocked ≠ completed), bypass count **with reasons**, raw-ops by pattern, and failure-at-use counts naming the failing tools. Sources are `state.json` run-history stamps (new `tools.runsSince(sinceTs)`) + `tool-guard.jsonl` (new `readGuardEvents(sinceTs)`) — per the plan, the KPI is never parsed out of conversation prose. Escape-hatch bypasses surface in the dream report only (no immediate chat pings — pings would train us both to ignore them). Test coverage: raw-op matrix incl. no-double-count with denies, event grouping/windowing, `runsSince` window math.
|
package/bin/tool.js
CHANGED
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
// through. Tool-first loop: search → exists: use it → missing a verb: extend →
|
|
3
3
|
// nothing: scaffold, then work through the tool.
|
|
4
4
|
//
|
|
5
|
-
// open-claudia tool list — index (
|
|
5
|
+
// open-claudia tool list — index, grouped by first tag (kazee/, infra/, …)
|
|
6
6
|
// open-claudia tool search <query> — lexical match (find before adding a dup)
|
|
7
7
|
// open-claudia tool show <name> [--source] — TOOL.md, telemetry, history (source with --source)
|
|
8
8
|
// open-claudia tool scaffold <name> — create a new tool skeleton (dir + stub + TOOL.md)
|
|
9
9
|
// [--pack <dir>] [--desc "..."] [--risk read-only|write|destructive]
|
|
10
|
-
// [--requires "k1,k2"] [--usage "..."] [--lang node|bash]
|
|
10
|
+
// [--requires "k1,k2"] [--tags "group"] [--usage "..."] [--lang node|bash]
|
|
11
11
|
// open-claudia tool add <path> [--name n] — register an existing script as a tool
|
|
12
|
-
// [--pack <dir>] [--desc "..."] [--risk tier] [--requires "k1,k2"] [--usage "..."]
|
|
12
|
+
// [--pack <dir>] [--desc "..."] [--risk tier] [--requires "k1,k2"] [--tags "group"] [--usage "..."]
|
|
13
13
|
// open-claudia tool set <name> [--desc ...] — fix a tool's metadata in place
|
|
14
|
-
// [--pack <dir>] [--risk tier] [--requires "k1,k2"] [--usage "..."]
|
|
14
|
+
// [--pack <dir>] [--risk tier] [--requires "k1,k2"] [--tags "group"] [--usage "..."]
|
|
15
15
|
// open-claudia tool note <name> — append to TOOL.md (auto-committed)
|
|
16
16
|
// [--issue "..."] [--state "..."] [--journal "..."]
|
|
17
17
|
// open-claudia tool edit <name> — print the file path to open & edit
|
|
18
18
|
// open-claudia tool run <name> [args...] — run it (keyring pre-loaded, risk-gated)
|
|
19
|
+
// open-claudia tool lib [list|show|set] ... — shared _lib plumbing tools import (see below)
|
|
19
20
|
// open-claudia tool migrate — convert legacy flat-file tools to directories
|
|
20
21
|
// open-claudia tool remove <name> — delete one
|
|
21
22
|
// open-claudia tool path — print the tools directory
|
|
@@ -38,6 +39,13 @@
|
|
|
38
39
|
// it declares via --requires — reference them as $NAME inside the script and
|
|
39
40
|
// declare every one. `open-claudia keyring list` shows what exists; never
|
|
40
41
|
// hardcode secrets in a tool. Undeclared keys simply won't be in the env.
|
|
42
|
+
//
|
|
43
|
+
// Composition (Phase 4): a tool may shell into other tools (report → kticket).
|
|
44
|
+
// Approval is inherited, never fabricated: the runner exports the tier the
|
|
45
|
+
// OUTER flags acknowledged, and a nested `tool run` gates on that inherited
|
|
46
|
+
// tier while ignoring its own argv flags. Shared plumbing lives in _lib/
|
|
47
|
+
// (`tool lib set <name> <src>`); a lib change journals every dependent tool
|
|
48
|
+
// "unverified since _lib change" — re-verified by its next real use.
|
|
41
49
|
|
|
42
50
|
const { spawnSync } = require("child_process");
|
|
43
51
|
const tools = require("../core/tools");
|
|
@@ -124,10 +132,31 @@ function run(args) {
|
|
|
124
132
|
return console.log(`No tools yet (${tools.TOOLS_DIR}).\nSave one with: open-claudia tool add <script-path> --pack <skill>`);
|
|
125
133
|
}
|
|
126
134
|
console.log(`${all.length} reusable tool(s) — run via 'open-claudia tool run <name>':\n`);
|
|
127
|
-
|
|
135
|
+
const bullet = (t, indent = "") => {
|
|
128
136
|
const skill = t.pack ? ` [skill: ${t.pack}]` : "";
|
|
129
137
|
const risk = t.risk === "read-only" ? "" : ` [${t.risk || "write?"}]`;
|
|
130
|
-
console.log(
|
|
138
|
+
console.log(`${indent}• ${t.name} — ${t.description || "(no description)"}${skill}${risk}`);
|
|
139
|
+
};
|
|
140
|
+
// Umbrella grouping from registry tags: first tag is the group
|
|
141
|
+
// (kazee/{kticket,…}); untagged tools render flat. Purely presentational —
|
|
142
|
+
// groups are tags, never binaries.
|
|
143
|
+
const groups = new Map();
|
|
144
|
+
for (const t of all) {
|
|
145
|
+
const g = (t.tags && t.tags[0]) || "";
|
|
146
|
+
if (!groups.has(g)) groups.set(g, []);
|
|
147
|
+
groups.get(g).push(t);
|
|
148
|
+
}
|
|
149
|
+
if (groups.size === 1 && groups.has("")) {
|
|
150
|
+
for (const t of all) bullet(t);
|
|
151
|
+
} else {
|
|
152
|
+
for (const g of [...groups.keys()].filter(Boolean).sort()) {
|
|
153
|
+
console.log(`${g}/`);
|
|
154
|
+
for (const t of groups.get(g)) bullet(t, " ");
|
|
155
|
+
}
|
|
156
|
+
if (groups.has("")) {
|
|
157
|
+
console.log("");
|
|
158
|
+
for (const t of groups.get("")) bullet(t);
|
|
159
|
+
}
|
|
131
160
|
}
|
|
132
161
|
console.log(`\nDocs: open-claudia tool show <name> · Dir: ${tools.TOOLS_DIR}`);
|
|
133
162
|
break;
|
|
@@ -162,6 +191,7 @@ function run(args) {
|
|
|
162
191
|
console.log(`Tool: ${t.name}`);
|
|
163
192
|
if (t.description) console.log(`Description: ${t.description}`);
|
|
164
193
|
if (t.pack) console.log(`Skill pack: ${t.pack} (open-claudia pack show ${t.pack})`);
|
|
194
|
+
if (t.tags && t.tags.length) console.log(`Tags: ${t.tags.join(", ")}`);
|
|
165
195
|
console.log(`Risk: ${t.risk || "(undeclared — treated as write)"}`);
|
|
166
196
|
console.log(`Usage: open-claudia tool run ${t.name}${t.usage ? " (" + t.usage + ")" : ""}`);
|
|
167
197
|
if (t.requires.length) {
|
|
@@ -228,6 +258,7 @@ function run(args) {
|
|
|
228
258
|
description: typeof flags.desc === "string" ? flags.desc : (typeof flags.description === "string" ? flags.description : undefined),
|
|
229
259
|
risk: typeof flags.risk === "string" ? flags.risk : undefined,
|
|
230
260
|
requires: typeof flags.requires === "string" ? flags.requires.split(/[,\s]+/).filter(Boolean) : undefined,
|
|
261
|
+
tags: typeof flags.tags === "string" ? flags.tags.split(/[,\s]+/).filter(Boolean) : undefined,
|
|
231
262
|
usage: typeof flags.usage === "string" ? flags.usage : undefined,
|
|
232
263
|
});
|
|
233
264
|
console.log(`Saved tool "${t.name}"${t.pack ? ` (skill: ${t.pack})` : ""} — risk: ${t.risk}.`);
|
|
@@ -276,9 +307,10 @@ function run(args) {
|
|
|
276
307
|
if (typeof flags.pack === "string") patch.pack = flags.pack;
|
|
277
308
|
if (typeof flags.risk === "string") patch.risk = flags.risk;
|
|
278
309
|
if (typeof flags.requires === "string") patch.requires = flags.requires.split(/[,\s]+/).filter(Boolean);
|
|
310
|
+
if (typeof flags.tags === "string") patch.tags = flags.tags.split(/[,\s]+/).filter(Boolean);
|
|
279
311
|
if (typeof flags.usage === "string") patch.usage = flags.usage;
|
|
280
312
|
if (Object.keys(patch).length === 0) {
|
|
281
|
-
console.error('Nothing to set. Pass at least one of --desc / --pack / --risk / --requires / --usage. Body edits: open-claudia tool edit ' + name);
|
|
313
|
+
console.error('Nothing to set. Pass at least one of --desc / --pack / --risk / --requires / --tags / --usage. Body edits: open-claudia tool edit ' + name);
|
|
282
314
|
process.exitCode = 1; return;
|
|
283
315
|
}
|
|
284
316
|
let updated;
|
|
@@ -290,6 +322,7 @@ function run(args) {
|
|
|
290
322
|
if (updated.pack) console.log(` Skill pack: ${updated.pack}`);
|
|
291
323
|
console.log(` Risk: ${updated.risk || "(undeclared — treated as write)"}`);
|
|
292
324
|
if (updated.requires.length) console.log(` Requires: ${updated.requires.join(", ")}`);
|
|
325
|
+
if (updated.tags && updated.tags.length) console.log(` Tags: ${updated.tags.join(", ")}`);
|
|
293
326
|
if (updated.usage) console.log(` Usage: ${updated.usage}`);
|
|
294
327
|
reportSafety(updated);
|
|
295
328
|
break;
|
|
@@ -317,7 +350,9 @@ function run(args) {
|
|
|
317
350
|
|
|
318
351
|
// Risk-tier gate: write needs --yes, destructive needs --yes-destructive.
|
|
319
352
|
// The flag is passed through to the tool (which may have its own gate).
|
|
320
|
-
|
|
353
|
+
// Nested runs (a tool calling this tool) gate on the tier the OUTERMOST
|
|
354
|
+
// invocation approved instead — argv flags can't fabricate approval.
|
|
355
|
+
const gate = tools.runGate(t, toolArgs, process.env);
|
|
321
356
|
if (!gate.ok) { console.error(gate.message); process.exitCode = 3; return; }
|
|
322
357
|
|
|
323
358
|
const missing = tools.missingRequires(t);
|
|
@@ -325,8 +360,17 @@ function run(args) {
|
|
|
325
360
|
console.error(`Cannot run ${t.name}: missing keyring keys ${missing.join(", ")}. Set them with 'open-claudia keyring set <name> <value>'.`);
|
|
326
361
|
process.exitCode = 1; return;
|
|
327
362
|
}
|
|
363
|
+
// Composition inheritance: export the approved tier + call chain so any
|
|
364
|
+
// tool this tool shells into is gated by what the human actually approved
|
|
365
|
+
// at the top — an already-inherited tier passes through UNCHANGED.
|
|
366
|
+
const env = tools.runEnv(t);
|
|
367
|
+
const parentStack = String(process.env.OPEN_CLAUDIA_TOOL_STACK || "").trim();
|
|
368
|
+
env.OPEN_CLAUDIA_APPROVED_TIER = parentStack
|
|
369
|
+
? (tools.normalizeRisk(process.env.OPEN_CLAUDIA_APPROVED_TIER) || "read-only")
|
|
370
|
+
: tools.approvedTierFromArgv(toolArgs);
|
|
371
|
+
env.OPEN_CLAUDIA_TOOL_STACK = parentStack ? `${parentStack}>${t.name}` : t.name;
|
|
328
372
|
const started = Date.now();
|
|
329
|
-
const r = spawnSync(t.file, toolArgs, { stdio: "inherit", env
|
|
373
|
+
const r = spawnSync(t.file, toolArgs, { stdio: "inherit", env });
|
|
330
374
|
const ms = Date.now() - started;
|
|
331
375
|
if (r.error) {
|
|
332
376
|
tools.recordRunResult(t.name, { args: toolArgs, exit: -1, ms });
|
|
@@ -348,7 +392,7 @@ function run(args) {
|
|
|
348
392
|
const { positional, flags } = parseFlags(rest);
|
|
349
393
|
const name = positional[0];
|
|
350
394
|
if (!name) {
|
|
351
|
-
console.error('Usage: tool scaffold <name> [--pack dir] [--desc "..."] [--risk read-only|write|destructive] [--requires "k1,k2"] [--usage "..."] [--lang node|bash]');
|
|
395
|
+
console.error('Usage: tool scaffold <name> [--pack dir] [--desc "..."] [--risk read-only|write|destructive] [--requires "k1,k2"] [--tags "group"] [--usage "..."] [--lang node|bash]');
|
|
352
396
|
process.exitCode = 1; return;
|
|
353
397
|
}
|
|
354
398
|
try {
|
|
@@ -357,6 +401,7 @@ function run(args) {
|
|
|
357
401
|
description: typeof flags.desc === "string" ? flags.desc : (typeof flags.description === "string" ? flags.description : undefined),
|
|
358
402
|
risk: typeof flags.risk === "string" ? flags.risk : undefined,
|
|
359
403
|
requires: typeof flags.requires === "string" ? flags.requires.split(/[,\s]+/).filter(Boolean) : [],
|
|
404
|
+
tags: typeof flags.tags === "string" ? flags.tags.split(/[,\s]+/).filter(Boolean) : [],
|
|
360
405
|
usage: typeof flags.usage === "string" ? flags.usage : undefined,
|
|
361
406
|
lang: typeof flags.lang === "string" ? flags.lang : "node",
|
|
362
407
|
});
|
|
@@ -410,6 +455,60 @@ function run(args) {
|
|
|
410
455
|
break;
|
|
411
456
|
}
|
|
412
457
|
|
|
458
|
+
case "lib": {
|
|
459
|
+
const sub = (rest[0] || "list").toLowerCase();
|
|
460
|
+
const libArgs = rest.slice(1);
|
|
461
|
+
if (sub === "list" || sub === "ls") {
|
|
462
|
+
const libs = tools.listLib();
|
|
463
|
+
if (!libs.length) {
|
|
464
|
+
console.log(`No shared _lib modules yet (${tools.LIB_DIR}).`);
|
|
465
|
+
console.log('Add one: open-claudia tool lib set <name.js> <src-path> — for plumbing 2+ tools share (central auth, mongo-exec).');
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
console.log(`${libs.length} shared _lib module(s) — imported by tools via a literal "_lib/<name>" path:\n`);
|
|
469
|
+
for (const l of libs) {
|
|
470
|
+
const deps = tools.libDependents(l.name);
|
|
471
|
+
console.log(`• ${l.name} (${l.size} bytes, ${l.updatedAt.slice(0, 10)})${deps.length ? ` — used by: ${deps.join(", ")}` : " — no dependents yet"}`);
|
|
472
|
+
}
|
|
473
|
+
console.log(`\nnode: require(\`\${__dirname}/../_lib/<name>.js\`) · bash: source "$(dirname "$0")/../_lib/<name>.sh"`);
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
if (sub === "show" || sub === "cat") {
|
|
477
|
+
const name = libArgs[0];
|
|
478
|
+
if (!name) { console.error("Usage: tool lib show <name>"); process.exitCode = 1; return; }
|
|
479
|
+
const content = tools.readLib(name);
|
|
480
|
+
if (content === null) { console.error(`No _lib module "${name}". Run: open-claudia tool lib list`); process.exitCode = 1; return; }
|
|
481
|
+
const deps = tools.libDependents(name);
|
|
482
|
+
console.log(`_lib/${name} — used by: ${deps.length ? deps.join(", ") : "(no dependents yet)"}\n`);
|
|
483
|
+
console.log(content);
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
if (sub === "set" || sub === "add") {
|
|
487
|
+
const name = libArgs[0];
|
|
488
|
+
const srcPath = libArgs[1];
|
|
489
|
+
if (!name || !srcPath) { console.error("Usage: tool lib set <name.js> <src-path>"); process.exitCode = 1; return; }
|
|
490
|
+
let content;
|
|
491
|
+
try { content = require("fs").readFileSync(srcPath, "utf-8"); }
|
|
492
|
+
catch (e) { console.error(`Cannot read ${srcPath}: ${e.message}`); process.exitCode = 1; return; }
|
|
493
|
+
try {
|
|
494
|
+
const r = repo.setLib(name, content);
|
|
495
|
+
console.log(`${r.existed ? "Updated" : "Added"} _lib/${r.name}.`);
|
|
496
|
+
if (r.dependents.length) {
|
|
497
|
+
console.log(`Dependents journaled as unverified until their next real use: ${r.dependents.join(", ")}.`);
|
|
498
|
+
console.log("Re-verify each by simply using it — a green run at point of use is the health check.");
|
|
499
|
+
} else {
|
|
500
|
+
console.log("No dependents yet — tools import it via a literal \"_lib/" + r.name.replace(/\.[^.]+$/, "") + "\" path.");
|
|
501
|
+
}
|
|
502
|
+
} catch (e) {
|
|
503
|
+
console.error(`Could not set _lib module: ${e.message}`); process.exitCode = 1;
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
console.error("Usage: tool lib [list | show <name> | set <name.js> <src-path>]");
|
|
508
|
+
process.exitCode = 1;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
|
|
413
512
|
case "migrate": {
|
|
414
513
|
const { migrated, skipped } = repo.migrateLegacyTools();
|
|
415
514
|
if (!migrated.length && !skipped.length) console.log("Nothing to migrate — all tools already use the directory layout.");
|
|
@@ -432,7 +531,7 @@ function run(args) {
|
|
|
432
531
|
break;
|
|
433
532
|
|
|
434
533
|
default:
|
|
435
|
-
console.log('Usage: open-claudia tool [list | search <query> | show <name> [--source] | scaffold <name> [flags] | add <path> [flags] | set <name> [flags] | note <name> [flags] | edit <name> | run <name> [args] | migrate | remove <name> | path]');
|
|
534
|
+
console.log('Usage: open-claudia tool [list | search <query> | show <name> [--source] | scaffold <name> [flags] | add <path> [flags] | set <name> [flags] | note <name> [flags] | edit <name> | run <name> [args] | lib [list|show|set] | migrate | remove <name> | path]');
|
|
436
535
|
}
|
|
437
536
|
}
|
|
438
537
|
|
package/core/system-prompt.js
CHANGED
|
@@ -98,7 +98,8 @@ Raw shell one-liners and heredocs are for probing and diagnosis only — never t
|
|
|
98
98
|
- Tools carry their own memory: TOOL.md (Interface · Known issues · State · Journal) is the manual, state.json records every run (per-verb health), and every change is a git commit. Keep TOOL.md truthful as you work; update the tool in place when behaviour changes.
|
|
99
99
|
- Preauth, least-privilege: keyring creds are injected ONLY into \`tool run\` subprocesses, and only the keys a tool declares via \`--requires\` (\`open-claudia keyring list\` shows names) — your own shell and sub-agents hold NO creds. Reference creds as \`$inet_central_user\` etc. inside tool sources and declare every one; never write a secret into a tool file (saves are refused).
|
|
100
100
|
- Deny-gate: raw shell commands that act on known production surfaces (platform APIs, prod hosts, in-cluster mongo, GitLab host shell) are blocked mid-flight with a redirect to the covering tool — that block is the system working, not an obstacle to route around. Genuine one-off no tool covers yet? \`open-claudia keyring exec --reason "<why>" -- <cmd>\` runs with full creds and logs the bypass for audit; scaffold the tool if you'd ever run it twice.
|
|
101
|
-
-
|
|
101
|
+
- Composition: a tool may shell into other tools (report → kticket + kchat). Approval is inherited, never fabricated — a nested callee runs at most at the tier YOUR outer \`tool run\` flags acknowledged, so pass --yes/--yes-destructive on the outer call (after user approval) when the composition writes. Shared plumbing lives in \`_lib/\` (\`tool lib list|show|set\`); a lib change marks each dependent "unverified since _lib change" until its next real use.
|
|
102
|
+
- Announce in one line when you scaffold or change a tool, same as packs. Full CLI: \`open-claudia tool list|search|show|scaffold|add|set|note|edit|run|lib|migrate|remove\`.
|
|
102
103
|
${listing}`;
|
|
103
104
|
}
|
|
104
105
|
|
package/core/tool-repo.js
CHANGED
|
@@ -64,6 +64,32 @@ function commitTool(name, message) {
|
|
|
64
64
|
return git(["commit", "-q", "-m", message], { ident: true }).ok;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
// Write/update a shared _lib module, then journal every dependent tool as
|
|
68
|
+
// "unverified since _lib change" and commit the lot in ONE commit whose message
|
|
69
|
+
// names the dependents (PLAN §3.5: a _lib change can break several tools at
|
|
70
|
+
// once; each is re-verified by its next real use, not by smoke tests).
|
|
71
|
+
// Commits with target "." — sanitizeName would mangle "_lib", and the commit
|
|
72
|
+
// must carry the dependents' TOOL.md marks anyway.
|
|
73
|
+
function setLib(name, content) {
|
|
74
|
+
const tools = require("./tools");
|
|
75
|
+
ensureRepo();
|
|
76
|
+
const existed = tools.readLib(name) !== null;
|
|
77
|
+
const { name: libName, file } = tools.writeLib(name, content);
|
|
78
|
+
const dependents = tools.libDependents(libName);
|
|
79
|
+
for (const dep of dependents) {
|
|
80
|
+
const t = tools.findTool(dep);
|
|
81
|
+
if (!t || !t.docFile) continue;
|
|
82
|
+
let doc = tools.readToolDoc(dep) || toolMdSkeleton(t);
|
|
83
|
+
const line = `- [${today()}] unverified since _lib change (${libName}) — re-verified by next real use.`;
|
|
84
|
+
const re = /(##\s*Journal[^\n]*\n)/i;
|
|
85
|
+
doc = re.test(doc) ? doc.replace(re, `$1${line}\n`) : doc + `\n## Journal\n${line}\n`;
|
|
86
|
+
try { fs.writeFileSync(t.docFile, doc, { mode: 0o600 }); } catch (e) { /* journal is best-effort */ }
|
|
87
|
+
}
|
|
88
|
+
const depNote = dependents.length ? `dependents: ${dependents.join(", ")} (unverified until next use)` : "no dependents yet";
|
|
89
|
+
commitTool(null, `lib ${libName}: ${existed ? "update" : "add"} — ${depNote}`);
|
|
90
|
+
return { name: libName, file, dependents, existed };
|
|
91
|
+
}
|
|
92
|
+
|
|
67
93
|
// Recent history for one tool — surfaced by `tool show` as the upgrade log.
|
|
68
94
|
function toolLog(name, limit = 5) {
|
|
69
95
|
const n = require("./tools").sanitizeName(name);
|
|
@@ -98,13 +124,14 @@ function toolMdSkeleton({ name, description, usage, risk, requires, pack, stateL
|
|
|
98
124
|
].join("\n");
|
|
99
125
|
}
|
|
100
126
|
|
|
101
|
-
function nodeStub({ name, description, pack, risk, requires, usage }) {
|
|
127
|
+
function nodeStub({ name, description, pack, risk, requires, usage, tags }) {
|
|
102
128
|
const req = requires && requires.length ? `// requires: ${requires.join(", ")}\n` : "";
|
|
129
|
+
const tag = tags && tags.length ? `// tags: ${tags.join(", ")}\n` : "";
|
|
103
130
|
return `#!/usr/bin/env node
|
|
104
131
|
// open-claudia-tool: ${name}
|
|
105
132
|
// description: ${description || ""}
|
|
106
133
|
${pack ? `// pack: ${pack}\n` : ""}// risk: ${risk}
|
|
107
|
-
${req}// usage: ${usage || `${name} <verb> [args]`}
|
|
134
|
+
${req}${tag}// usage: ${usage || `${name} <verb> [args]`}
|
|
108
135
|
//
|
|
109
136
|
// One CLI per system, verbs as subcommands. Add a function per verb below.
|
|
110
137
|
// Confirmation flags (--yes / --yes-destructive) are enforced by the tool
|
|
@@ -127,13 +154,14 @@ Promise.resolve(fn(args.slice(1))).catch((e) => { console.error(e.message || e);
|
|
|
127
154
|
`;
|
|
128
155
|
}
|
|
129
156
|
|
|
130
|
-
function bashStub({ name, description, pack, risk, requires, usage }) {
|
|
157
|
+
function bashStub({ name, description, pack, risk, requires, usage, tags }) {
|
|
131
158
|
const req = requires && requires.length ? `# requires: ${requires.join(", ")}\n` : "";
|
|
159
|
+
const tag = tags && tags.length ? `# tags: ${tags.join(", ")}\n` : "";
|
|
132
160
|
return `#!/usr/bin/env bash
|
|
133
161
|
# open-claudia-tool: ${name}
|
|
134
162
|
# description: ${description || ""}
|
|
135
163
|
${pack ? `# pack: ${pack}\n` : ""}# risk: ${risk}
|
|
136
|
-
${req}# usage: ${usage || `${name} <verb> [args]`}
|
|
164
|
+
${req}${tag}# usage: ${usage || `${name} <verb> [args]`}
|
|
137
165
|
#
|
|
138
166
|
# One CLI per system, verbs as subcommands: add a case per verb below.
|
|
139
167
|
# Confirmation flags (--yes / --yes-destructive) are enforced by the tool
|
|
@@ -163,7 +191,8 @@ function scaffoldTool(name, opts = {}) {
|
|
|
163
191
|
if (tools.findTool(n)) throw new Error(`tool "${n}" already exists — extend it (tool edit ${n}) instead of re-scaffolding`);
|
|
164
192
|
const risk = tools.normalizeRisk(opts.risk) || "write";
|
|
165
193
|
const requires = [].concat(opts.requires || []).filter(Boolean);
|
|
166
|
-
const
|
|
194
|
+
const tags = [].concat(opts.tags || []).map((s) => tools.sanitizeName(s)).filter(Boolean);
|
|
195
|
+
const spec = { name: n, description: opts.description || "", pack: opts.pack || "", risk, requires, tags, usage: opts.usage || "" };
|
|
167
196
|
|
|
168
197
|
ensureRepo();
|
|
169
198
|
const dir = path.join(toolsDir(), n);
|
|
@@ -267,4 +296,4 @@ function hasLegacyTools() {
|
|
|
267
296
|
return false;
|
|
268
297
|
}
|
|
269
298
|
|
|
270
|
-
module.exports = { ensureRepo, commitTool, toolLog, scaffoldTool, migrateLegacyTools, hasLegacyTools, toolMdSkeleton };
|
|
299
|
+
module.exports = { ensureRepo, commitTool, setLib, toolLog, scaffoldTool, migrateLegacyTools, hasLegacyTools, toolMdSkeleton };
|
package/core/tools.js
CHANGED
|
@@ -36,7 +36,7 @@ const TOOLS_DIR = process.env.TOOLS_DIR ? path.resolve(process.env.TOOLS_DIR) :
|
|
|
36
36
|
// parse from the leading comment block. Language-agnostic: we accept "#" (sh,
|
|
37
37
|
// python, ruby) and "//" (js, ts, go) comment prefixes.
|
|
38
38
|
const MARKER = "open-claudia-tool";
|
|
39
|
-
const HEADER_KEYS = ["description", "pack", "requires", "usage", "risk"];
|
|
39
|
+
const HEADER_KEYS = ["description", "pack", "requires", "usage", "risk", "tags"];
|
|
40
40
|
const RISK_TIERS = ["read-only", "write", "destructive"];
|
|
41
41
|
|
|
42
42
|
// Normalise a risk value to one of RISK_TIERS ("" when unrecognised). Blast
|
|
@@ -219,7 +219,7 @@ function matchTools(text, { limit = 5, threshold = null } = {}) {
|
|
|
219
219
|
const min = threshold ?? Number(process.env.TOOL_MATCH_THRESHOLD || 2);
|
|
220
220
|
const out = [];
|
|
221
221
|
for (const t of listTools()) {
|
|
222
|
-
const strong = wordsOf([t.name, t.description, t.usage].filter(Boolean).join(" "));
|
|
222
|
+
const strong = wordsOf([t.name, t.description, t.usage, (t.tags || []).join(" ")].filter(Boolean).join(" "));
|
|
223
223
|
const weak = wordsOf([(t.requires || []).join(" "), t.pack, t.content].filter(Boolean).join(" "));
|
|
224
224
|
let score = 0;
|
|
225
225
|
for (const term of terms) {
|
|
@@ -244,7 +244,7 @@ function uncomment(line) {
|
|
|
244
244
|
// script is only a tool if the block contains an "open-claudia-tool: <name>"
|
|
245
245
|
// line.
|
|
246
246
|
function parseHeader(content) {
|
|
247
|
-
const out = { name: "", description: "", pack: "", requires: [], usage: "", risk: "" };
|
|
247
|
+
const out = { name: "", description: "", pack: "", requires: [], usage: "", risk: "", tags: [] };
|
|
248
248
|
const lines = String(content || "").split("\n");
|
|
249
249
|
let started = false;
|
|
250
250
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -264,6 +264,7 @@ function parseHeader(content) {
|
|
|
264
264
|
if (key === MARKER) out.name = sanitizeName(val);
|
|
265
265
|
else if (key === "requires") out.requires = val.split(/[,\s]+/).map((s) => s.trim()).filter(Boolean);
|
|
266
266
|
else if (key === "risk") out.risk = normalizeRisk(val);
|
|
267
|
+
else if (key === "tags") out.tags = val.split(/[,\s]+/).map((s) => sanitizeName(s)).filter(Boolean);
|
|
267
268
|
else if (HEADER_KEYS.includes(key)) out[key] = val;
|
|
268
269
|
}
|
|
269
270
|
return out.name ? out : null;
|
|
@@ -293,6 +294,7 @@ function readTool(name, usageMap) {
|
|
|
293
294
|
requires: header.requires || [],
|
|
294
295
|
usage: header.usage || "",
|
|
295
296
|
risk: header.risk || "",
|
|
297
|
+
tags: header.tags || [],
|
|
296
298
|
layout: p.layout,
|
|
297
299
|
file: p.exec,
|
|
298
300
|
docFile: p.doc,
|
|
@@ -356,7 +358,7 @@ function listTools() {
|
|
|
356
358
|
const usage = readUsage();
|
|
357
359
|
const tools = [];
|
|
358
360
|
for (const name of entries) {
|
|
359
|
-
if (name.startsWith(".")) continue;
|
|
361
|
+
if (name.startsWith(".") || name.startsWith("_")) continue; // _lib etc. — plumbing, not tools
|
|
360
362
|
const full = path.join(TOOLS_DIR, name);
|
|
361
363
|
let st;
|
|
362
364
|
try { st = fs.statSync(full); } catch (e) { continue; }
|
|
@@ -413,14 +415,75 @@ function docDrift(t) {
|
|
|
413
415
|
return null;
|
|
414
416
|
}
|
|
415
417
|
|
|
418
|
+
// ── _lib shared plumbing ─────────────────────────────────────────────────────
|
|
419
|
+
// Genuinely shared code (central auth, mongo-exec) lives in tools/_lib — tiny
|
|
420
|
+
// modules IMPORTED by tools, never tools themselves (listTools skips the "_"
|
|
421
|
+
// prefix, so _lib never appears in the registry or surfacing index). Import
|
|
422
|
+
// with a literal "_lib/<name>" path so dependents stay grep-detectable:
|
|
423
|
+
// node: require(`${__dirname}/../_lib/<name>.js`)
|
|
424
|
+
// bash: source "$(dirname "$0")/../_lib/<name>.sh"
|
|
425
|
+
// A _lib change can break several tools at once, so every change is committed
|
|
426
|
+
// with its dependents named and each dependent journaled "unverified since
|
|
427
|
+
// _lib change" — re-verified by its next real use (no smoke tests, by design).
|
|
428
|
+
const LIB_DIR = path.join(TOOLS_DIR, "_lib");
|
|
429
|
+
|
|
430
|
+
function libPath(name) {
|
|
431
|
+
const base = path.basename(String(name || "").trim());
|
|
432
|
+
if (!base || base.startsWith(".")) throw new Error(`invalid lib name "${name}"`);
|
|
433
|
+
return path.join(LIB_DIR, base);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function listLib() {
|
|
437
|
+
let entries;
|
|
438
|
+
try { entries = fs.readdirSync(LIB_DIR); } catch (e) { return []; }
|
|
439
|
+
const out = [];
|
|
440
|
+
for (const name of entries) {
|
|
441
|
+
if (name.startsWith(".")) continue;
|
|
442
|
+
try {
|
|
443
|
+
const st = fs.statSync(path.join(LIB_DIR, name));
|
|
444
|
+
if (st.isFile()) out.push({ name, file: path.join(LIB_DIR, name), size: st.size, updatedAt: st.mtime.toISOString() });
|
|
445
|
+
} catch (e) { /* unreadable entry — skip */ }
|
|
446
|
+
}
|
|
447
|
+
out.sort((a, b) => a.name.localeCompare(b.name));
|
|
448
|
+
return out;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function readLib(name) {
|
|
452
|
+
try { return fs.readFileSync(libPath(name), "utf-8"); } catch (e) { return null; }
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function writeLib(name, content) {
|
|
456
|
+
const file = libPath(name);
|
|
457
|
+
assertNoSecrets(content, `_lib/${path.basename(file)}`);
|
|
458
|
+
fs.mkdirSync(LIB_DIR, { recursive: true, mode: 0o700 });
|
|
459
|
+
fs.writeFileSync(file, content, { mode: 0o600 });
|
|
460
|
+
return { name: path.basename(file), file };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Which registered tools import a given lib — a deterministic source grep for
|
|
464
|
+
// the "_lib/<stem>" reference shape (tolerates path.join'd segments), so a lib
|
|
465
|
+
// change can name exactly the tools it may have broken. Extension-agnostic but
|
|
466
|
+
// boundary-safe: "central-auth" matches _lib/central-auth.js, not
|
|
467
|
+
// _lib/central-auth-v2.js.
|
|
468
|
+
function libDependents(name) {
|
|
469
|
+
const base = path.basename(String(name || "").trim());
|
|
470
|
+
if (!base) return [];
|
|
471
|
+
const stem = base.replace(/\.[^.]+$/, "");
|
|
472
|
+
const esc = stem.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
473
|
+
const re = new RegExp(`_lib[/\\\\"'\\s,]{1,5}["']?${esc}(?:\\.[A-Za-z0-9]+)?(?![A-Za-z0-9._-])`);
|
|
474
|
+
return listTools().filter((t) => re.test(t.content || "")).map((t) => t.name);
|
|
475
|
+
}
|
|
476
|
+
|
|
416
477
|
// Build the header comment block for a script that doesn't already declare one.
|
|
417
478
|
// Uses the comment prefix that matches the script's shebang/extension.
|
|
418
|
-
function buildHeader(prefix, { name, description, pack, requires, usage, risk }) {
|
|
479
|
+
function buildHeader(prefix, { name, description, pack, requires, usage, risk, tags }) {
|
|
419
480
|
const lines = [`${prefix} ${MARKER}: ${name}`];
|
|
420
481
|
if (description) lines.push(`${prefix} description: ${description}`);
|
|
421
482
|
if (pack) lines.push(`${prefix} pack: ${pack}`);
|
|
422
483
|
if (risk) lines.push(`${prefix} risk: ${risk}`);
|
|
423
484
|
if (requires && requires.length) lines.push(`${prefix} requires: ${[].concat(requires).join(", ")}`);
|
|
485
|
+
const cleanTags = [].concat(tags || []).map((s) => sanitizeName(s)).filter(Boolean);
|
|
486
|
+
if (cleanTags.length) lines.push(`${prefix} tags: ${cleanTags.join(", ")}`);
|
|
424
487
|
if (usage) lines.push(`${prefix} usage: ${usage}`);
|
|
425
488
|
return lines.join("\n");
|
|
426
489
|
}
|
|
@@ -549,6 +612,7 @@ function addTool(srcPath, opts = {}) {
|
|
|
549
612
|
pack: opts.pack || "",
|
|
550
613
|
risk,
|
|
551
614
|
requires: opts.requires || [],
|
|
615
|
+
tags: opts.tags || [],
|
|
552
616
|
usage: opts.usage || "",
|
|
553
617
|
});
|
|
554
618
|
const hasShebang = content.startsWith("#!");
|
|
@@ -565,6 +629,7 @@ function addTool(srcPath, opts = {}) {
|
|
|
565
629
|
content = content.replace(new RegExp(`((#|//)\\s*${MARKER}:.*\\n)`), `$1${line}\n`);
|
|
566
630
|
};
|
|
567
631
|
if (opts.pack && !header.pack) injectAfterMarker(`${prefix} pack: ${opts.pack}`);
|
|
632
|
+
if (opts.tags && opts.tags.length && !header.tags.length) injectAfterMarker(`${prefix} tags: ${[].concat(opts.tags).map((s) => sanitizeName(s)).filter(Boolean).join(", ")}`);
|
|
568
633
|
if (!header.risk) {
|
|
569
634
|
const risk = optRisk || "write";
|
|
570
635
|
if (!optRisk) defaultedRisk = true;
|
|
@@ -635,6 +700,9 @@ function updateToolHeader(name, opts = {}) {
|
|
|
635
700
|
}
|
|
636
701
|
let requires = opts.requires !== undefined ? opts.requires : t.requires;
|
|
637
702
|
if (!Array.isArray(requires)) requires = String(requires || "").split(/[,\s]+/).filter(Boolean);
|
|
703
|
+
let tags = opts.tags !== undefined ? opts.tags : t.tags;
|
|
704
|
+
if (!Array.isArray(tags)) tags = String(tags || "").split(/[,\s]+/).filter(Boolean);
|
|
705
|
+
tags = tags.map((s) => sanitizeName(s)).filter(Boolean);
|
|
638
706
|
|
|
639
707
|
const lines = t.content.split("\n");
|
|
640
708
|
let headStart = 0;
|
|
@@ -660,6 +728,7 @@ function updateToolHeader(name, opts = {}) {
|
|
|
660
728
|
if (pack) fieldLines.push(`${prefix} pack: ${pack}`);
|
|
661
729
|
if (risk) fieldLines.push(`${prefix} risk: ${risk}`);
|
|
662
730
|
if (requires.length) fieldLines.push(`${prefix} requires: ${requires.join(", ")}`);
|
|
731
|
+
if (tags.length) fieldLines.push(`${prefix} tags: ${tags.join(", ")}`);
|
|
663
732
|
if (usage) fieldLines.push(`${prefix} usage: ${usage}`);
|
|
664
733
|
|
|
665
734
|
let newBlock;
|
|
@@ -680,9 +749,38 @@ function updateToolHeader(name, opts = {}) {
|
|
|
680
749
|
// unlock it) and gets a spelled-out warning. An undeclared tier is treated as
|
|
681
750
|
// write — fail-safe until classified. The flag is the agent's deliberate
|
|
682
751
|
// confirmation step AFTER user approval; it never replaces asking the user.
|
|
683
|
-
|
|
752
|
+
//
|
|
753
|
+
// Composition (Phase 4): tools may shell into other tools. Approval flows ONLY
|
|
754
|
+
// from the outermost, human-approved invocation — the runner exports the tier
|
|
755
|
+
// its flags acknowledged (OPEN_CLAUDIA_APPROVED_TIER) plus the call chain
|
|
756
|
+
// (OPEN_CLAUDIA_TOOL_STACK) into the child env, and a nested run gates on that
|
|
757
|
+
// inherited tier while IGNORING its own argv flags, so a composing tool cannot
|
|
758
|
+
// fabricate approval by hardcoding --yes-destructive in its own source.
|
|
759
|
+
const TIER_RANK = { "read-only": 0, write: 1, destructive: 2 };
|
|
760
|
+
|
|
761
|
+
function approvedTierFromArgv(argv = []) {
|
|
762
|
+
if (argv.includes("--yes-destructive")) return "destructive";
|
|
763
|
+
if (argv.includes("--yes")) return "write";
|
|
764
|
+
return "read-only";
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function runGate(tool, argv = [], env = process.env) {
|
|
684
768
|
const declared = normalizeRisk(tool && tool.risk);
|
|
685
769
|
const tier = declared || "write";
|
|
770
|
+
const stack = String((env && env.OPEN_CLAUDIA_TOOL_STACK) || "").trim();
|
|
771
|
+
if (stack) {
|
|
772
|
+
// Nested run — a tool called this tool.
|
|
773
|
+
if (stack.split(">").includes(tool.name)) {
|
|
774
|
+
return { ok: false, tier, nested: true, message: `⛔ Tool-call cycle: "${tool.name}" is already running in this chain (${stack}).` };
|
|
775
|
+
}
|
|
776
|
+
const approved = normalizeRisk(env.OPEN_CLAUDIA_APPROVED_TIER) || "read-only";
|
|
777
|
+
if (TIER_RANK[tier] <= TIER_RANK[approved]) return { ok: true, tier, nested: true };
|
|
778
|
+
return {
|
|
779
|
+
ok: false, tier, nested: true,
|
|
780
|
+
message: `⛔ "${tool.name}" is ${tier}-tier but was called by "${stack}", approved only for ${approved} actions.\n` +
|
|
781
|
+
`A composing tool cannot self-approve. Get the user's approval, then re-run the OUTER tool with ${tier === "destructive" ? "--yes-destructive" : "--yes"}.`,
|
|
782
|
+
};
|
|
783
|
+
}
|
|
686
784
|
const note = declared ? "" : ` (no risk tier declared — treated as write; classify: open-claudia tool set ${tool.name} --risk read-only|write|destructive)`;
|
|
687
785
|
if (tier === "read-only") return { ok: true, tier };
|
|
688
786
|
if (tier === "destructive") {
|
|
@@ -749,10 +847,11 @@ function toolNameFromPath(filePath) {
|
|
|
749
847
|
}
|
|
750
848
|
|
|
751
849
|
module.exports = {
|
|
752
|
-
TOOLS_DIR, MARKER, USAGE_FILE, RISK_TIERS,
|
|
850
|
+
TOOLS_DIR, MARKER, USAGE_FILE, RISK_TIERS, LIB_DIR,
|
|
753
851
|
parseHeader, readTool, readToolDoc, toolCondition, listTools, findTool, runsSince, docDrift, addTool, removeTool, updateToolHeader,
|
|
754
852
|
runEnv, missingRequires, toolNameFromPath, sanitizeName, ensureDir, normalizeRisk,
|
|
755
|
-
toolPaths, readState, writeState, recordRunResult, runGate,
|
|
853
|
+
toolPaths, readState, writeState, recordRunResult, runGate, approvedTierFromArgv,
|
|
854
|
+
listLib, readLib, writeLib, libDependents,
|
|
756
855
|
matchTools, recordRun, recordCreate, readUsage,
|
|
757
856
|
lintToolSource, checkSyntax, envRefsIn, assertNoSecrets,
|
|
758
857
|
};
|
package/package.json
CHANGED
package/test-tools.js
CHANGED
|
@@ -362,6 +362,82 @@ fs.utimesSync(tools.findTool("condtool").file, future, future);
|
|
|
362
362
|
assert.strictEqual(tools.docDrift(tools.findTool("condtool")), "source newer than TOOL.md", "source edited after docs is drift");
|
|
363
363
|
assert.strictEqual(tools.docDrift(null), null, "no tool → no drift");
|
|
364
364
|
|
|
365
|
+
// ── tags (Phase 4 umbrella grouping): round-trip through the header, settable,
|
|
366
|
+
// sanitised, and part of the STRONG match haystack ──
|
|
367
|
+
const tagSrc = path.join(tmp, "kgadget.sh");
|
|
368
|
+
fs.writeFileSync(tagSrc, "#!/usr/bin/env bash\necho g\n");
|
|
369
|
+
const tagged = tools.addTool(tagSrc, { name: "kgadget", description: "gadget ops", risk: "read-only", tags: ["Kazee"] });
|
|
370
|
+
assert.deepStrictEqual(tagged.tags, ["kazee"], "tags parse back from the header, sanitised to lowercase");
|
|
371
|
+
assert.ok(/# tags: kazee/.test(tagged.content), "tags land as a header line");
|
|
372
|
+
assert.ok(tools.matchTools("kazee").some((m) => m.name === "kgadget"), "a tag term matches strongly");
|
|
373
|
+
const retagged = tools.updateToolHeader("kgadget", { tags: "kazee, infra" });
|
|
374
|
+
assert.deepStrictEqual(retagged.tags, ["kazee", "infra"], "updateToolHeader sets tags from a comma string");
|
|
375
|
+
assert.strictEqual((retagged.content.match(/# tags:/g) || []).length, 1, "tags line survives exactly once");
|
|
376
|
+
const scTagged = repo.scaffoldTool("ktagged", { description: "tagged scaffold", risk: "read-only", tags: ["infra"] });
|
|
377
|
+
assert.deepStrictEqual(scTagged.tags, ["infra"], "scaffold carries tags into the stub header");
|
|
378
|
+
|
|
379
|
+
// ── _lib shared plumbing (Phase 4): invisible to the registry, secret-guarded,
|
|
380
|
+
// dependents found by a boundary-safe source grep ──
|
|
381
|
+
tools.writeLib("central-auth.js", "module.exports = { login() { return process.env.inet_central_user; } };\n");
|
|
382
|
+
assert.ok(tools.readLib("central-auth.js").includes("login()"), "writeLib/readLib round-trip");
|
|
383
|
+
assert.ok(tools.listLib().some((l) => l.name === "central-auth.js"), "listLib sees the module");
|
|
384
|
+
assert.ok(!tools.listTools().some((t) => t.name.startsWith("_") || t.name === "lib"), "_lib never appears in the tool registry");
|
|
385
|
+
assert.strictEqual(tools.readLib("nope.js"), null, "missing lib reads as null");
|
|
386
|
+
assert.throws(() => tools.writeLib("leak.js", 'const k = "sk-ant-abcdefgh12345678";\n'), /secret/i, "a hardcoded secret blocks a lib save");
|
|
387
|
+
const trav = tools.writeLib("../escape.js", "// harmless\n");
|
|
388
|
+
assert.strictEqual(path.dirname(trav.file), tools.LIB_DIR, "path traversal is neutralised — basename only, stays inside _lib");
|
|
389
|
+
assert.throws(() => tools.writeLib(".hidden", "x"), /invalid lib name/i, "dot-prefixed lib names are refused");
|
|
390
|
+
|
|
391
|
+
const depA = path.join(tmp, "authuser.sh");
|
|
392
|
+
fs.writeFileSync(depA, '#!/usr/bin/env bash\nsource "$(dirname "$0")/../_lib/central-auth.js"\n');
|
|
393
|
+
tools.addTool(depA, { name: "authuser", description: "uses central auth", risk: "read-only" });
|
|
394
|
+
const depB = path.join(tmp, "authjoin.js");
|
|
395
|
+
fs.writeFileSync(depB, '#!/usr/bin/env node\nconst lib = require(require("path").join(__dirname, "..", "_lib", "central-auth.js"));\n');
|
|
396
|
+
tools.addTool(depB, { name: "authjoin", description: "joins via central auth", risk: "read-only" });
|
|
397
|
+
const depC = path.join(tmp, "othertool.sh");
|
|
398
|
+
fs.writeFileSync(depC, '#!/usr/bin/env bash\nsource "$(dirname "$0")/../_lib/central-auth-v2.js"\n');
|
|
399
|
+
tools.addTool(depC, { name: "othertool", description: "uses the v2 lib", risk: "read-only" });
|
|
400
|
+
const deps = tools.libDependents("central-auth.js");
|
|
401
|
+
assert.ok(deps.includes("authuser"), "a literal _lib/<name> reference is a dependent");
|
|
402
|
+
assert.ok(deps.includes("authjoin"), "a path.join'd _lib reference is a dependent");
|
|
403
|
+
assert.ok(!deps.includes("othertool"), "central-auth-v2 is NOT a dependent of central-auth (boundary-safe)");
|
|
404
|
+
assert.deepStrictEqual(tools.libDependents("unused-lib.js"), [], "a lib nothing imports has no dependents");
|
|
405
|
+
|
|
406
|
+
// ── setLib: one commit, every dependent journaled "unverified since _lib change" ──
|
|
407
|
+
const libRes = repo.setLib("central-auth.js", "module.exports = { login() { return { user: process.env.inet_central_user }; } };\n");
|
|
408
|
+
assert.strictEqual(libRes.name, "central-auth.js", "setLib resolves the lib name");
|
|
409
|
+
assert.ok(libRes.existed, "setLib reports an update over an existing lib");
|
|
410
|
+
assert.deepStrictEqual(libRes.dependents.sort(), ["authjoin", "authuser"], "setLib names exactly the dependents");
|
|
411
|
+
assert.ok(/unverified since _lib change \(central-auth\.js\)/.test(tools.readToolDoc("authuser")), "dependent journaled as unverified");
|
|
412
|
+
assert.ok(/unverified since _lib change/.test(tools.readToolDoc("authjoin")), "second dependent journaled too");
|
|
413
|
+
assert.ok(!/unverified since _lib change/.test(tools.readToolDoc("othertool")), "a non-dependent is left alone");
|
|
414
|
+
|
|
415
|
+
// ── nested tool-calls-tool gate (Phase 4): approval is inherited from the
|
|
416
|
+
// outermost human-approved invocation; a composing tool cannot fabricate it ──
|
|
417
|
+
assert.strictEqual(tools.approvedTierFromArgv([]), "read-only", "no flags → read-only approval");
|
|
418
|
+
assert.strictEqual(tools.approvedTierFromArgv(["--yes"]), "write", "--yes → write approval");
|
|
419
|
+
assert.strictEqual(tools.approvedTierFromArgv(["--yes", "--yes-destructive"]), "destructive", "--yes-destructive wins");
|
|
420
|
+
const nestedEnv = (approved, stack) => ({ OPEN_CLAUDIA_APPROVED_TIER: approved, OPEN_CLAUDIA_TOOL_STACK: stack });
|
|
421
|
+
assert.ok(tools.runGate({ name: "kticket", risk: "write" }, [], nestedEnv("write", "report")).ok,
|
|
422
|
+
"a write callee runs under an inherited write approval — no argv flag needed");
|
|
423
|
+
assert.ok(tools.runGate({ name: "kticket", risk: "read-only" }, [], nestedEnv("read-only", "report")).ok,
|
|
424
|
+
"a read-only callee always runs nested");
|
|
425
|
+
const fabricated = tools.runGate({ name: "wipe", risk: "destructive" }, ["--yes-destructive"], nestedEnv("write", "report"));
|
|
426
|
+
assert.ok(!fabricated.ok, "a composing tool cannot self-approve destructive with its own hardcoded flag");
|
|
427
|
+
assert.ok(/cannot self-approve/i.test(fabricated.message), "the refusal explains approval must come from the outer run");
|
|
428
|
+
assert.ok(!tools.runGate({ name: "kticket", risk: "write" }, ["--yes"], nestedEnv("read-only", "report")).ok,
|
|
429
|
+
"an outer read-only approval does not unlock a nested write, argv flags ignored");
|
|
430
|
+
assert.ok(tools.runGate({ name: "wipe", risk: "destructive" }, [], nestedEnv("destructive", "report")).ok,
|
|
431
|
+
"an outer --yes-destructive approval flows through to a destructive callee");
|
|
432
|
+
assert.ok(tools.runGate({ name: "wipe", risk: "" }, [], nestedEnv("write", "report")).ok,
|
|
433
|
+
"an undeclared callee tier is treated as write — runs under an inherited write approval");
|
|
434
|
+
assert.ok(!tools.runGate({ name: "wipe", risk: "" }, [], nestedEnv("read-only", "report")).ok,
|
|
435
|
+
"an undeclared callee tier is refused under a read-only approval (fail-safe when nested too)");
|
|
436
|
+
const cycle = tools.runGate({ name: "report", risk: "read-only" }, [], nestedEnv("read-only", "weekly>report"));
|
|
437
|
+
assert.ok(!cycle.ok && /cycle/i.test(cycle.message), "a tool already in the call chain is refused (cycle guard)");
|
|
438
|
+
const topLevel = tools.runGate({ name: "kticket", risk: "write" }, ["--yes"], {});
|
|
439
|
+
assert.ok(topLevel.ok && !topLevel.nested, "no stack env → the classic argv-flag gate applies");
|
|
440
|
+
|
|
365
441
|
// ── remove ──
|
|
366
442
|
assert.ok(tools.removeTool("hello"), "removeTool returns the removed tool");
|
|
367
443
|
assert.strictEqual(tools.findTool("hello"), null, "removed tool is gone");
|