@nodii/dev 0.6.72 → 0.6.74
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/README.md +150 -7
- package/dist/cli.js +84 -16
- package/dist/core.js +2 -2
- package/dist/design.js +46 -2
- package/dist/doctor.js +17 -7
- package/dist/figma.js +44 -0
- package/dist/flags.js +5 -1
- package/dist/freeze.js +12 -0
- package/dist/gather.js +51 -23
- package/dist/harness.js +8 -4
- package/dist/hooks.js +1 -25
- package/dist/lanes.js +74 -23
- package/dist/managed-hooks.js +60 -9
- package/dist/nudges.js +166 -2
- package/dist/receipt.js +365 -0
- package/dist/review.js +76 -4
- package/dist/safety.js +8 -1
- package/dist/session-kind.js +243 -0
- package/dist/setup.js +8 -6
- package/dist/surface.js +847 -0
- package/dist/vendor/render-templates.mjs +740 -0
- package/package.json +1 -1
package/dist/managed-hooks.js
CHANGED
|
@@ -14,7 +14,8 @@ export const managedHooksDir = (home = homedir()) => join(home, ".config", "nodi
|
|
|
14
14
|
export const MANAGED_DISPATCH_MIN_VERSION = "0.6.27";
|
|
15
15
|
|
|
16
16
|
// Claude reads one machine-wide managed settings file for every profile
|
|
17
|
-
// (CLAUDE_CONFIG_DIR never moves it), so it carries the lane-gated dispatch
|
|
17
|
+
// (CLAUDE_CONFIG_DIR never moves it), so it carries the lane-gated dispatch
|
|
18
|
+
// (and, outside lanes, only the MW8-17 Surface-generation nudge).
|
|
18
19
|
export const CLAUDE_HOOK_EVENTS = [
|
|
19
20
|
"SessionStart", "UserPromptSubmit", "PreToolUse", "PermissionRequest", "PostToolUse",
|
|
20
21
|
"PreCompact", "PostCompact", "SubagentStart", "SubagentStop", "Stop", "Notification", "SessionEnd",
|
|
@@ -53,6 +54,16 @@ export function validateManagedHooks(hooks = {}, { events = CODEX_HOOK_EVENTS, r
|
|
|
53
54
|
const tomlString = (value) => JSON.stringify(String(value));
|
|
54
55
|
const shellString = (value) => `'${String(value).replaceAll("'", `'\\''`)}'`;
|
|
55
56
|
const laneConfig = (env) => /^lane-[a-z0-9_-]+\.json$/i.test(basename(env.NODII_DEV_CONFIG_PATH ?? ""));
|
|
57
|
+
|
|
58
|
+
// A session that is not a lane still learns its kind (MW1-92), and a steward is
|
|
59
|
+
// nudged to cite before it steers (MW1-95): these handlers, on these events,
|
|
60
|
+
// run for every session. Everything else stays lane-only.
|
|
61
|
+
export const SESSION_SCOPED_EVENTS = ["SessionStart", "UserPromptSubmit", "PreToolUse"];
|
|
62
|
+
export const SESSION_SCOPED_COMMANDS = ["nodii-dev hook session-start", "nodii-dev hook session-kind", "nodii-dev hook nudge-pre"];
|
|
63
|
+
// The shell's cheap screen for a non-lane PreToolUse: only a call that could
|
|
64
|
+
// be a steering act (MW1-95) starts the CLI. The CLI decides exactly.
|
|
65
|
+
const STEER_MARKERS = ['*\'"SendMessage"\'*', "*'lane amend'*", "*'gh pr '*", "*'nodii.repo.yaml'*", "*'lefthook.yml'*", "*'.github/workflows/'*"];
|
|
66
|
+
const sessionScoped = (event, handler) => SESSION_SCOPED_EVENTS.includes(event) && SESSION_SCOPED_COMMANDS.includes(String(handler?.command ?? "").trim());
|
|
56
67
|
const emptyResponse = (event) => ["Stop", "SubagentStop"].includes(event) ? "{}\n" : "";
|
|
57
68
|
|
|
58
69
|
/** Stable managed policy: all later hook changes live in user-owned hooks.json. */
|
|
@@ -91,12 +102,21 @@ export function managedHookBundle(hooksText, { home, cliPath, cliVersion = VERSI
|
|
|
91
102
|
return { dir, hooksText: `${JSON.stringify({ hooks: parsed.hooks ?? {} }, null, 2)}\n`, dispatch, metadata, requirements: managedRequirements(dir) };
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
// The stable shell entry point both runtimes call.
|
|
95
|
-
//
|
|
96
|
-
|
|
105
|
+
// The stable shell entry point both runtimes call. A lane config gets every
|
|
106
|
+
// event; any other session only the session-scoped ones (SessionStart,
|
|
107
|
+
// UserPromptSubmit, and a PreToolUse that could be a steering act), plus the
|
|
108
|
+
// MW8-17 Surface-generation nudge: an edit whose payload names a product page
|
|
109
|
+
// goes to `hook surface-gen-pre`, which reads only the edit. Every failure
|
|
110
|
+
// ends in the runtime's empty answer (fail open).
|
|
111
|
+
const SURFACE_GEN = `case "$managed_input" in *product-pages/*) case "$managed_input" in *'"Edit"'*|*'"Write"'*|*'"MultiEdit"'*|*'"apply_patch"'*) if [ -x "$managed_cli" ]; then managed_output=$(printf '%s' "$managed_input" | "$managed_cli" hook surface-gen-pre 2>/dev/null) && [ -n "$managed_output" ] && { printf '%s\\n' "$managed_output"; exit 0; }; fi ;; esac ;; esac; `;
|
|
112
|
+
|
|
113
|
+
export function dispatchScript(cliPath, action, stopJson) {
|
|
97
114
|
const command = shellString(cliPath);
|
|
98
115
|
const empty = stopJson ? ` case "$1" in Stop|SubagentStop) printf '{}\\n' ;; esac\n` : " :\n";
|
|
99
|
-
|
|
116
|
+
// Only a non-lane PreToolUse reads its payload in the shell, to screen it;
|
|
117
|
+
// every other call passes stdin straight through, as before.
|
|
118
|
+
const session = ` SessionStart|UserPromptSubmit) ;;\n PreToolUse) managed_input=$(cat); managed_screened=1; ${SURFACE_GEN}case "$managed_input" in ${STEER_MARKERS.join("|")}) ;; *) empty_response "$1"; exit 0 ;; esac ;;\n *) empty_response "$1"; exit 0 ;;\n`;
|
|
119
|
+
return `#!/bin/sh\nmanaged_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)\nmanaged_cli=${command}\nempty_response() {\n${empty}}\nmanaged_screened=\ncase "\${NODII_DEV_CONFIG_PATH##*/}" in\n lane-*.json) ;;\n *) case "$1" in\n${session} esac ;;\nesac\nif [ ! -x "$managed_cli" ]; then empty_response "$1"; exit 0; fi\nexport NODII_DEV_MANAGED_HOOKS_DIR="$managed_dir"\nexport NODII_DEV_MANAGED_CLI_PATH="$managed_cli"\nif [ -n "$managed_screened" ]; then managed_output=$(printf '%s' "$managed_input" | "$managed_cli" hook ${action} "$1" 2>/dev/null); else managed_output=$("$managed_cli" hook ${action} "$1" 2>/dev/null); fi\nmanaged_status=$?\nif [ "$managed_status" -ne 0 ]; then empty_response "$1"; exit 0; fi\nif [ -n "$managed_output" ]; then printf '%s\\n' "$managed_output"; else empty_response "$1"; fi\n`;
|
|
100
120
|
}
|
|
101
121
|
|
|
102
122
|
/** Claude's managed settings: every event goes to the stable dispatcher; later hook changes live in hooks.json. */
|
|
@@ -166,6 +186,35 @@ export async function writeManagedClaudeHookBundle(bundle) {
|
|
|
166
186
|
await writeAtomic(join(bundle.dir, "managed-settings.json"), bundle.managedSettings, 0o644);
|
|
167
187
|
}
|
|
168
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Refresh the staged dispatchers in place, for `harness update` without
|
|
191
|
+
* --system: each keeps the CLI path its metadata already names, so a plain
|
|
192
|
+
* update never repoints a machine's hooks at another nodii-dev. Only a
|
|
193
|
+
* dispatcher that exists, is staged by nodii-dev and differs is rewritten;
|
|
194
|
+
* the root-owned managed settings never change (they only call the dispatcher).
|
|
195
|
+
* A rewrite stamps metadata.json with `dispatchVersion`, the nodii-dev that
|
|
196
|
+
* generated the dispatcher now on disk (MW5-6); `cliVersion` keeps naming the
|
|
197
|
+
* CLI at `cliPath`, which doctor's version check reads.
|
|
198
|
+
* NODII_DEV_MANAGED_HOME is for throwaway-home proofs and tests only.
|
|
199
|
+
*/
|
|
200
|
+
export async function refreshManagedDispatchers({ home = process.env.NODII_DEV_MANAGED_HOME || homedir(), read = readFile } = {}) {
|
|
201
|
+
const refreshed = [];
|
|
202
|
+
for (const [dir, action, stopJson] of [[managedClaudeHooksDir(home), "claude-managed-dispatch", false], [managedHooksDir(home), "managed-dispatch", true]]) {
|
|
203
|
+
try {
|
|
204
|
+
const metadata = JSON.parse(await read(join(dir, "metadata.json"), "utf8"));
|
|
205
|
+
const { cliPath } = metadata;
|
|
206
|
+
if (!isAbsolute(String(cliPath ?? ""))) continue;
|
|
207
|
+
const current = await read(join(dir, "dispatch"), "utf8");
|
|
208
|
+
const next = dispatchScript(cliPath, action, stopJson);
|
|
209
|
+
if (current === next) continue;
|
|
210
|
+
await writeAtomic(join(dir, "dispatch"), next, 0o700);
|
|
211
|
+
await writeAtomic(join(dir, "metadata.json"), `${JSON.stringify({ ...metadata, dispatchVersion: VERSION }, null, 2)}\n`, 0o600);
|
|
212
|
+
refreshed.push(dir);
|
|
213
|
+
} catch {}
|
|
214
|
+
}
|
|
215
|
+
return refreshed;
|
|
216
|
+
}
|
|
217
|
+
|
|
169
218
|
export async function writeManagedHookBundle(bundle) {
|
|
170
219
|
await mkdir(bundle.dir, { recursive: true, mode: 0o700 });
|
|
171
220
|
await writeFile(join(bundle.dir, "hooks.json"), bundle.hooksText, { mode: 0o600 });
|
|
@@ -210,12 +259,13 @@ export async function dispatchManagedHook(event, payloadText, deps = {}) {
|
|
|
210
259
|
try {
|
|
211
260
|
if (!CODEX_HOOK_EVENTS.includes(event)) return emptyResponse(event);
|
|
212
261
|
const env = deps.env ?? process.env;
|
|
213
|
-
|
|
262
|
+
const lane = laneConfig(env);
|
|
263
|
+
if (!lane && !SESSION_SCOPED_EVENTS.includes(event)) return emptyResponse(event);
|
|
214
264
|
const dir = deps.dir ?? env.NODII_DEV_MANAGED_HOOKS_DIR ?? managedHooksDir();
|
|
215
265
|
const config = JSON.parse(await (deps.read ?? readFile)(join(dir, "hooks.json"), "utf8"));
|
|
216
266
|
validateManagedHooks(config.hooks ?? {});
|
|
217
267
|
const payload = JSON.parse(payloadText);
|
|
218
|
-
const handlers = matchingHandlers(config, event, payload);
|
|
268
|
+
const handlers = matchingHandlers(config, event, payload).filter((handler) => lane || sessionScoped(event, handler));
|
|
219
269
|
const results = await Promise.all(handlers.map((handler) => (deps.run ?? runHandler)(nodiiCommandArgs(handler.command), payloadText, deps)));
|
|
220
270
|
// A failed handler is unavailable policy, not a denial. Ignore both its
|
|
221
271
|
// output and stderr; doctor is where hook health remains visible.
|
|
@@ -263,7 +313,8 @@ export async function dispatchManagedClaudeHook(event, payloadText, deps = {}) {
|
|
|
263
313
|
try {
|
|
264
314
|
if (!CLAUDE_HOOK_EVENTS.includes(event)) return "";
|
|
265
315
|
const env = deps.env ?? process.env;
|
|
266
|
-
|
|
316
|
+
const lane = laneConfig(env);
|
|
317
|
+
if (!lane && !SESSION_SCOPED_EVENTS.includes(event)) return "";
|
|
267
318
|
const read = deps.read ?? readFile;
|
|
268
319
|
const dir = deps.dir ?? env.NODII_DEV_MANAGED_HOOKS_DIR ?? managedClaudeHooksDir();
|
|
269
320
|
const config = JSON.parse(await read(join(dir, "hooks.json"), "utf8"));
|
|
@@ -272,7 +323,7 @@ export async function dispatchManagedClaudeHook(event, payloadText, deps = {}) {
|
|
|
272
323
|
// hooks.json is Dev's Claude settings, so its matchers follow Claude's rules.
|
|
273
324
|
// Every matching handler runs: no user settings file can switch managed
|
|
274
325
|
// policy off. Where a repository also carries the same direct hook, both run.
|
|
275
|
-
const handlers = claudeHandlers(config, event, payload);
|
|
326
|
+
const handlers = claudeHandlers(config, event, payload).filter((handler) => lane || sessionScoped(event, handler));
|
|
276
327
|
const results = await Promise.all(handlers.map((handler) => (deps.run ?? runHandler)(nodiiCommandArgs(handler.command), payloadText, deps)));
|
|
277
328
|
const blocked = results.find((result) => result.status === 2 && result.stderr.trim());
|
|
278
329
|
const successful = results.filter((result) => result.status === 0);
|
package/dist/nudges.js
CHANGED
|
@@ -309,6 +309,100 @@ export async function migrationNudges({ path, rel, existed, after }, knowledge)
|
|
|
309
309
|
return notes.map((note) => `nodii-dev nudge: ${note}.`);
|
|
310
310
|
}
|
|
311
311
|
|
|
312
|
+
// ─── Surface generation (MW8-17): every design-system session ─────────────
|
|
313
|
+
|
|
314
|
+
// A product page's source, not its tests, stories, fixtures or catalog data.
|
|
315
|
+
const SURFACE_PAGE = /(?:^|\/)packages\/blocks\/src\/product-pages\/(?:.*\/)?[^/]+\.tsx$/;
|
|
316
|
+
const NOT_SURFACE_SOURCE = /\.(?:test|spec|stories|story|catalog|fixtures?|guidelines)\.tsx$|-(?:fixtures?|catalog|data|mocks?)\.tsx$/;
|
|
317
|
+
export const surfacePageSource = (path) => SURFACE_PAGE.test(cleanPath(path)) && !NOT_SURFACE_SOURCE.test(cleanPath(path));
|
|
318
|
+
|
|
319
|
+
// A JSX opening tag: `<` not glued to an identifier (so `useState<T>` and `Array<T>` are not tags).
|
|
320
|
+
const JSX_TAG = /(?<![\w$.)\]])<([A-Za-z][\w.:-]*)(?=[\s/>])/g;
|
|
321
|
+
const LOOK_ATTRS = new Set(["className", "style"]);
|
|
322
|
+
|
|
323
|
+
/** The end of a `{…}` starting at `at`, by brace depth alone (as jsxOpenTags does). */
|
|
324
|
+
function braceEnd(source, at) {
|
|
325
|
+
let depth = 0;
|
|
326
|
+
for (let i = at; i < source.length; i += 1) {
|
|
327
|
+
if (source[i] === "{") depth += 1;
|
|
328
|
+
else if (source[i] === "}" && --depth === 0) return i + 1;
|
|
329
|
+
}
|
|
330
|
+
return source.length;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Every JSX element in order, as its tag and its look (className and style
|
|
335
|
+
* values), with the source range of its opening tag. Data props and callbacks
|
|
336
|
+
* are left out on purpose: changing them is not a visual change (DS-BLK-1).
|
|
337
|
+
*/
|
|
338
|
+
export function jsxLooks(text) {
|
|
339
|
+
const source = String(text);
|
|
340
|
+
const out = [];
|
|
341
|
+
for (const match of source.matchAll(JSX_TAG)) {
|
|
342
|
+
let at = match.index + match[0].length;
|
|
343
|
+
const look = [];
|
|
344
|
+
while (at < source.length) {
|
|
345
|
+
while (/\s/.test(source[at] ?? "")) at += 1;
|
|
346
|
+
const char = source[at];
|
|
347
|
+
if (char === undefined || char === ">") { at += 1; break; }
|
|
348
|
+
if (char === "/" && source[at + 1] === ">") { at += 2; break; }
|
|
349
|
+
if (char === "{") { at = braceEnd(source, at); continue; }
|
|
350
|
+
const name = /^[\w:-]+/.exec(source.slice(at))?.[0];
|
|
351
|
+
if (!name) { at += 1; continue; }
|
|
352
|
+
at += name.length;
|
|
353
|
+
while (/\s/.test(source[at] ?? "")) at += 1;
|
|
354
|
+
if (source[at] !== "=") continue;
|
|
355
|
+
at += 1;
|
|
356
|
+
while (/\s/.test(source[at] ?? "")) at += 1;
|
|
357
|
+
const start = at;
|
|
358
|
+
if (source[at] === '"' || source[at] === "'") { const close = source.indexOf(source[at], at + 1); at = close === -1 ? source.length : close + 1; }
|
|
359
|
+
else if (source[at] === "{") at = braceEnd(source, at);
|
|
360
|
+
if (LOOK_ATTRS.has(name)) look.push(`${name}=${source.slice(start, at).replace(/\s+/g, " ")}`);
|
|
361
|
+
}
|
|
362
|
+
out.push({ tag: match[1], look: look.join(" "), start: match.index, end: at });
|
|
363
|
+
}
|
|
364
|
+
return out;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Styling code outside an opening tag: a class list or style object built in a variable.
|
|
368
|
+
const STYLE_CODE = /\bclassName\b|\bstyle\s*[:=]|\b(?:cn|clsx|cva|twMerge|classNames)\s*\(/;
|
|
369
|
+
const outsideTags = (text, looks) => looks.reduceRight((out, { start, end }) => out.slice(0, start) + " " + out.slice(end), String(text));
|
|
370
|
+
|
|
371
|
+
/** Whether an edit changes JSX elements, classNames or styles, as opposed to data and wiring alone. */
|
|
372
|
+
export function visualEdit(before, after) {
|
|
373
|
+
const was = jsxLooks(before);
|
|
374
|
+
const now = jsxLooks(after);
|
|
375
|
+
const shape = (looks) => looks.map(({ tag, look }) => `${tag} ${look}`).join("\n");
|
|
376
|
+
if (shape(was) !== shape(now)) return true;
|
|
377
|
+
const a = outsideTags(before, was);
|
|
378
|
+
const b = outsideTags(after, now);
|
|
379
|
+
return [...changedLines(a, b), ...changedLines(b, a)].some((line) => STYLE_CODE.test(line));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export const SURFACE_GEN_NUDGE = "nodii-dev nudge: You're editing a Surface's visual code. Product pages are generated from their Surfaces through Code Connect (MW8-17). Editing visuals here by hand probably breaks that rule, and the surface-gen-equal gate will refuse it. Change the Surface in Figma, the component's Code Connect template, or the generator instead. Data props and callbacks (DS-BLK-1) are fine to edit.";
|
|
383
|
+
|
|
384
|
+
/** A product page's visual code edited by hand (MW8-17). Quiet for data-only and prop-wiring edits. */
|
|
385
|
+
export function surfaceGenNudges({ path, rel, before, after }) {
|
|
386
|
+
if (!surfacePageSource(rel ?? path ?? "")) return [];
|
|
387
|
+
return visualEdit(before, after) ? [SURFACE_GEN_NUDGE] : [];
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The lane-free PreToolUse entry point behind the managed dispatcher: it runs
|
|
392
|
+
* in every session, so it reads only the edit itself (no git, no Dev, no lane)
|
|
393
|
+
* and fails open to no nudge.
|
|
394
|
+
*/
|
|
395
|
+
export async function surfaceGenPre(payload) {
|
|
396
|
+
try {
|
|
397
|
+
if (!EDIT_TOOLS.has(payload?.tool_name ?? "")) return [];
|
|
398
|
+
if (!editedPaths(payload).some(surfacePageSource)) return [];
|
|
399
|
+
const files = await editedFiles(payload, payload.cwd ?? process.cwd());
|
|
400
|
+
return files.some((file) => surfaceGenNudges(file).length) ? [SURFACE_GEN_NUDGE] : [];
|
|
401
|
+
} catch {
|
|
402
|
+
return [];
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
312
406
|
const NODE_ID_TOOLS = /(?:get_design_context|get_metadata|get_screenshot|get_variable_defs|get_motion_context)$/;
|
|
313
407
|
|
|
314
408
|
/** A code lane reading Figma by node id (MW8-20). */
|
|
@@ -460,13 +554,83 @@ export async function nudgePre(payload, context, { knowledge = laneKnowledge, fi
|
|
|
460
554
|
if (!EDIT_TOOLS.has(tool)) return [];
|
|
461
555
|
const known = await knowledge(cwd);
|
|
462
556
|
const out = [];
|
|
463
|
-
|
|
557
|
+
const files = await editedFiles(payload, cwd);
|
|
558
|
+
for (const file of files) {
|
|
464
559
|
const edit = { ...file, rel: cleanPath(relative(root, file.path)) };
|
|
465
560
|
out.push(...guidelineNudges(edit, known), ...restyleNudges(edit), ...selfPiiNudges(edit), ...(await migrationNudges(edit, known)));
|
|
466
561
|
}
|
|
562
|
+
// Said once per call, however many pages the call edits.
|
|
563
|
+
if (files.some((file) => surfaceGenNudges(file).length)) out.push(SURFACE_GEN_NUDGE);
|
|
467
564
|
return out;
|
|
468
565
|
}
|
|
469
566
|
|
|
567
|
+
// ─── Cite before you steer (MW1-95) ─────────────────────────────────────────
|
|
568
|
+
|
|
569
|
+
// What a steward does to steer: an amendment, a message to a lane or the
|
|
570
|
+
// coordinator, a merge or a merge hold, or a new gate in a repository's gate files.
|
|
571
|
+
const STEER_SHELL = [
|
|
572
|
+
[/\b(?:nodii-dev|@nodii\/dev(?:@\S+)?)\s+lane\s+amend\b/, "lane amendment"],
|
|
573
|
+
[/\bgh\s+pr\s+merge\b/, "merge"],
|
|
574
|
+
[/\bgh\s+pr\s+(?:comment|review|edit)\b[^\n]*(?:\bhold\b|\bblock|\bdo(?: not|n't) merge\b|--request-changes\b)/i, "merge hold"],
|
|
575
|
+
];
|
|
576
|
+
const GATE_FILE = /(?:^|\/)(?:nodii\.repo\.yaml|lefthook\.yml|\.github\/workflows\/[^/]+\.ya?ml)$/;
|
|
577
|
+
const DEC_ID = /\bDEC-\d{3,5}\b/;
|
|
578
|
+
const RULE_ID = /\b([A-Z][A-Z0-9]*(?:-[A-Z][A-Z0-9]*)*)-\d+(?:\.\d+)*\b/g;
|
|
579
|
+
const BODY_FILE = /--(?:body|comment|reason|message)-file[=\s]+("[^"]+"|'[^']+'|\S+)/g;
|
|
580
|
+
|
|
581
|
+
/** Which steering act this call is, or null. Pure, so most calls cost nothing. */
|
|
582
|
+
export function steerAct(payload) {
|
|
583
|
+
const tool = payload?.tool_name ?? "";
|
|
584
|
+
if (tool === "SendMessage") return "message to a lane or the coordinator";
|
|
585
|
+
if (SHELL_TOOLS.has(tool)) {
|
|
586
|
+
const command = String(payload.tool_input?.command ?? "");
|
|
587
|
+
return STEER_SHELL.find(([pattern]) => pattern.test(command))?.[1] ?? null;
|
|
588
|
+
}
|
|
589
|
+
if (EDIT_TOOLS.has(tool)) {
|
|
590
|
+
const path = String(payload.tool_input?.file_path ?? payload.tool_input?.notebook_path ?? "");
|
|
591
|
+
return GATE_FILE.test(path) ? "new gate" : null;
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** The document codes Dev holds, so an issue key such as UAT-PROD-13 is never taken for a clause. */
|
|
597
|
+
let documentCodes = null;
|
|
598
|
+
export async function ruleDocumentCodes(request = devRequest) {
|
|
599
|
+
if (!documentCodes)
|
|
600
|
+
documentCodes = request("GET", "/api/v1/documents?limit=500").then((page) => new Set((page.data ?? []).map((document) => document.code))).catch(() => { documentCodes = null; return null; });
|
|
601
|
+
return documentCodes;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** Whether the text names a DEC id or a clause of a document Dev holds (any clause shape when Dev cannot say). */
|
|
605
|
+
export function citesRule(text, codes) {
|
|
606
|
+
const value = String(text ?? "");
|
|
607
|
+
if (DEC_ID.test(value)) return true;
|
|
608
|
+
for (const [, code] of value.matchAll(RULE_ID)) if (!codes || codes.has(code)) return true;
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
async function steerText(payload, cwd) {
|
|
613
|
+
const input = payload.tool_input ?? {};
|
|
614
|
+
if (payload.tool_name === "SendMessage") return [input.message, input.summary, input.content].filter((part) => typeof part === "string").join("\n");
|
|
615
|
+
if (EDIT_TOOLS.has(payload.tool_name)) return [input.new_string, input.content, ...(input.edits ?? []).map((edit) => edit?.new_string)].filter((part) => typeof part === "string").join("\n");
|
|
616
|
+
const command = String(input.command ?? "");
|
|
617
|
+
const files = await Promise.all([...command.matchAll(BODY_FILE)].map(([, path]) => optional(resolve(cwd, path.replace(/^["']|["']$/g, "")))));
|
|
618
|
+
return [command, ...files.filter(Boolean)].join("\n");
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* PreToolUse for a steward session only: a steering act that names no clause
|
|
623
|
+
* or DEC id is nudged before it goes out. It never blocks (AGENT-HARNESS-21).
|
|
624
|
+
*/
|
|
625
|
+
export async function stewardSteerNudges(payload, { steward, cwd = payload?.cwd ?? process.cwd(), codes = ruleDocumentCodes } = {}) {
|
|
626
|
+
if (!steward) return [];
|
|
627
|
+
const act = steerAct(payload);
|
|
628
|
+
if (!act) return [];
|
|
629
|
+
const known = await (typeof codes === "function" ? codes() : codes);
|
|
630
|
+
if (citesRule(await steerText(payload, cwd), known)) return [];
|
|
631
|
+
return [`nodii-dev nudge: cite before you steer (MW1-95). This ${act} names no clause id (for example MW8-17) or DEC id. Name the clause or decision it rests on. With none, don't send it: the missing rule is an operator question (MW4-27), added to open.md. Impose no landing condition the rulebook lacks.`];
|
|
632
|
+
}
|
|
633
|
+
|
|
470
634
|
// ─── Stop ──────────────────────────────────────────────────────────────────
|
|
471
635
|
|
|
472
636
|
// A claim that the work is finished, judged a sentence at a time so "the docs are not done" cannot hide "it is done".
|
|
@@ -558,7 +722,7 @@ export async function nudgeStop(payload, context, { lastText = lastAssistantText
|
|
|
558
722
|
|
|
559
723
|
export const nudgeSettingsSnippet = () => ({
|
|
560
724
|
hooks: {
|
|
561
|
-
PreToolUse: [{ matcher: "^(AskUserQuestion|mcp__(?:figma|claude_ai_Figma|figma-dev-mode)__.*|Bash|Edit|Write|MultiEdit|NotebookEdit)$", hooks: [{ type: "command", command: "nodii-dev hook nudge-pre" }] }],
|
|
725
|
+
PreToolUse: [{ matcher: "^(AskUserQuestion|SendMessage|mcp__(?:figma|claude_ai_Figma|figma-dev-mode)__.*|Bash|Edit|Write|MultiEdit|NotebookEdit)$", hooks: [{ type: "command", command: "nodii-dev hook nudge-pre" }] }],
|
|
562
726
|
Stop: [{ hooks: [{ type: "command", command: "nodii-dev hook nudge-stop" }] }],
|
|
563
727
|
},
|
|
564
728
|
});
|