@nodii/dev 0.6.176 → 0.6.177
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 +8 -1
- package/dist/core.js +1 -1
- package/dist/handrolled.js +85 -0
- package/dist/safety.js +117 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -596,7 +596,14 @@ blocks, silently when satisfied (AGENT-HARNESS-15):
|
|
|
596
596
|
deploy/apply without a `GO deploy <sha>` grant; shell writes outside the
|
|
597
597
|
worktree (redirects, `tee`, `cp`, `mv`, `rm`, `mkdir`, `touch` …; temp space
|
|
598
598
|
is fine) and mutating `git -C` into another checkout; Edit/Write outside the
|
|
599
|
-
worktree, outside owned paths or on generated files.
|
|
599
|
+
worktree, outside owned paths or on generated files. One exception
|
|
600
|
+
(hub-548): an edit to a generated Surface (`*.surface.generated.tsx`)
|
|
601
|
+
is allowed when it only adds or changes keyed `@handrolled <KEY>: ...` /
|
|
602
|
+
`@end-handrolled` regions (DEC-0488, DEC-0494, MW8-17.11). The file after the
|
|
603
|
+
edit must parse and pass the surface-gen check's own test against the file
|
|
604
|
+
before it: every byte outside the regions unchanged and in order. Edit,
|
|
605
|
+
MultiEdit, Write and Codex `apply_patch` Update hunks are judged this way; a
|
|
606
|
+
delete, a move or any other generated file stays refused.
|
|
600
607
|
- **A coordinator** may use `--no-verify` only with `NODII_DEV_NO_VERIFY_REASON`
|
|
601
608
|
set; the reason is recorded in private Git metadata.
|
|
602
609
|
|
package/dist/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { homedir, platform } from "node:os";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
|
|
7
|
-
export const VERSION = "0.6.
|
|
7
|
+
export const VERSION = "0.6.177";
|
|
8
8
|
export const configPath = () =>
|
|
9
9
|
process.env.NODII_DEV_CONFIG_PATH ??
|
|
10
10
|
join(homedir(), ".config/nodii-dev/config.json");
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* hub-548: the keyed hand-patch marks of DEC-0488, DEC-0494 and
|
|
3
|
+
* MW8-17.11. A copy of design-system's infra/code-connect/handrolled.mjs
|
|
4
|
+
* (origin/main 2fb827cdd), so the edit guard judges a mark edit by the same
|
|
5
|
+
* test the surface-gen check and CI run. Keep the two in step.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Parse keyed, non-nested hand patches. Standalone marker lines add no bytes. */
|
|
9
|
+
export function handrolledRegions(text) {
|
|
10
|
+
const regions = [];
|
|
11
|
+
const tokens = /\/\*\s*@(handrolled|end-handrolled)\b/g;
|
|
12
|
+
let open = null;
|
|
13
|
+
for (const match of text.matchAll(tokens)) {
|
|
14
|
+
const line = text.slice(0, match.index).split("\n").length;
|
|
15
|
+
const end = text.indexOf("*/", match.index);
|
|
16
|
+
const fail = (reason) => {
|
|
17
|
+
throw new Error(`line ${line}: ${reason}`);
|
|
18
|
+
};
|
|
19
|
+
if (end < 0) fail("unclosed handrolled comment");
|
|
20
|
+
const comment = text.slice(match.index, end + 2);
|
|
21
|
+
// JSX comment braces belong to the marker, not to the generated content.
|
|
22
|
+
const before = /\{[ \t]*$/.exec(text.slice(0, match.index));
|
|
23
|
+
const after = /^[ \t]*\}/.exec(text.slice(end + 2));
|
|
24
|
+
const markerStart = before && after ? before.index : match.index;
|
|
25
|
+
const markerEnd = before && after ? end + 2 + after[0].length : end + 2;
|
|
26
|
+
const lineStart = text.lastIndexOf("\n", match.index - 1) + 1;
|
|
27
|
+
const nextLine = text.indexOf("\n", end + 2);
|
|
28
|
+
const lineEnd = nextLine < 0 ? text.length : nextLine;
|
|
29
|
+
const standalone =
|
|
30
|
+
/^\s*$/.test(text.slice(lineStart, markerStart)) &&
|
|
31
|
+
/^\s*$/.test(text.slice(markerEnd, lineEnd));
|
|
32
|
+
const start = standalone ? lineStart : markerStart;
|
|
33
|
+
const stop = standalone
|
|
34
|
+
? nextLine < 0
|
|
35
|
+
? text.length
|
|
36
|
+
: nextLine + 1
|
|
37
|
+
: markerEnd;
|
|
38
|
+
if (match[1] === "handrolled") {
|
|
39
|
+
if (open) fail("nested handrolled region");
|
|
40
|
+
const keyed =
|
|
41
|
+
/^\/\*\s*@handrolled\s+([A-Z][A-Z0-9]*(?:-[A-Z0-9]+)*-\d+):\s*\S[\s\S]*\*\/$/.exec(
|
|
42
|
+
comment
|
|
43
|
+
);
|
|
44
|
+
if (!keyed) fail("handrolled mark needs an issue key and description");
|
|
45
|
+
open = { key: keyed[1], line, start };
|
|
46
|
+
} else {
|
|
47
|
+
if (!/^\/\*\s*@end-handrolled\s*\*\/$/.test(comment))
|
|
48
|
+
fail("invalid end-handrolled mark");
|
|
49
|
+
if (!open) fail("end-handrolled without an opening mark");
|
|
50
|
+
regions.push({ ...open, endLine: line, end: stop });
|
|
51
|
+
open = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (open)
|
|
55
|
+
throw new Error(
|
|
56
|
+
`line ${open.line}: unclosed handrolled region ${open.key}`
|
|
57
|
+
);
|
|
58
|
+
return regions;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Every byte outside the marked spans must occur unchanged, in order. */
|
|
62
|
+
export function compareHandrolled(current, generated) {
|
|
63
|
+
const regions = handrolledRegions(current);
|
|
64
|
+
if (!regions.length) return { equal: current === generated, regions };
|
|
65
|
+
let sourceAt = 0;
|
|
66
|
+
let generatedAt = 0;
|
|
67
|
+
for (let i = 0; i < regions.length; i++) {
|
|
68
|
+
const chunk = current.slice(sourceAt, regions[i].start);
|
|
69
|
+
const at =
|
|
70
|
+
i === 0
|
|
71
|
+
? generated.startsWith(chunk)
|
|
72
|
+
? 0
|
|
73
|
+
: -1
|
|
74
|
+
: generated.indexOf(chunk, generatedAt);
|
|
75
|
+
if (at < 0) return { equal: false, regions };
|
|
76
|
+
generatedAt = at + chunk.length;
|
|
77
|
+
sourceAt = regions[i].end;
|
|
78
|
+
}
|
|
79
|
+
const tail = current.slice(sourceAt);
|
|
80
|
+
return {
|
|
81
|
+
equal:
|
|
82
|
+
generated.endsWith(tail) && generated.length - tail.length >= generatedAt,
|
|
83
|
+
regions,
|
|
84
|
+
};
|
|
85
|
+
}
|
package/dist/safety.js
CHANGED
|
@@ -7,6 +7,7 @@ import { promisify } from "node:util";
|
|
|
7
7
|
import { CliError, LANE_CAP_RAISE_PROMPT, LIVE_LANE_STATUSES, configPath, devRequest, encode, hookLanePath, query, readConfig } from "./core.js";
|
|
8
8
|
import { laneCapReminder } from "./watchdog.js";
|
|
9
9
|
import { heavyViolation } from "./heavy.js";
|
|
10
|
+
import { compareHandrolled } from "./handrolled.js";
|
|
10
11
|
|
|
11
12
|
const execFileAsync = promisify(execFile);
|
|
12
13
|
const ACTIVE = new Set(LIVE_LANE_STATUSES);
|
|
@@ -691,8 +692,120 @@ async function realAncestorPath(path) {
|
|
|
691
692
|
}
|
|
692
693
|
}
|
|
693
694
|
|
|
695
|
+
// hub-548: a generated Surface that DEC-0488, DEC-0494 and MW8-17.11
|
|
696
|
+
// let a lane patch inside keyed @handrolled marks.
|
|
697
|
+
const SURFACE_GENERATED = /(?:^|\/)[^/]+\.surface\.generated\.tsx$/;
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* hub-548: whether an edit to a generated Surface only adds or changes
|
|
701
|
+
* keyed `@handrolled <KEY>: ...` ... `@end-handrolled` regions. The file's text
|
|
702
|
+
* after the edit must parse (every region keyed, none nested or unclosed) and
|
|
703
|
+
* pass the surface-gen check's own test against the text before it: every byte
|
|
704
|
+
* outside the regions unchanged and in order. So the guard is no looser than
|
|
705
|
+
* the check CI runs ("Generated pages match their Surfaces"). Any other
|
|
706
|
+
* generated file, a delete or move, an after-text that can't be computed, or a
|
|
707
|
+
* call with no tool input, is not a mark edit.
|
|
708
|
+
*/
|
|
709
|
+
async function markEditAllowed(absolute, path, payload) {
|
|
710
|
+
if (!payload || !SURFACE_GENERATED.test(cleanPath(absolute))) return false;
|
|
711
|
+
const before = await optional(absolute);
|
|
712
|
+
if (before === null) return false;
|
|
713
|
+
const after = textAfterEdit(payload, path, before);
|
|
714
|
+
if (after === null) return false;
|
|
715
|
+
try {
|
|
716
|
+
const { equal, regions } = compareHandrolled(after, before);
|
|
717
|
+
return equal && regions.length > 0;
|
|
718
|
+
} catch {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
function replaceOnce(text, edit) {
|
|
724
|
+
const from = typeof edit?.old_string === "string" ? edit.old_string : null;
|
|
725
|
+
if (!from || typeof edit.new_string !== "string") return null;
|
|
726
|
+
const count = text.split(from).length - 1;
|
|
727
|
+
if (!count || (count > 1 && edit.replace_all !== true)) return null;
|
|
728
|
+
return edit.replace_all === true ? text.split(from).join(edit.new_string) : text.replace(from, () => edit.new_string);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/** The file's text after this tool call, or null when it can't be known. */
|
|
732
|
+
export function textAfterEdit(payload, path, before) {
|
|
733
|
+
const input = payload?.tool_input ?? {};
|
|
734
|
+
switch (payload?.tool_name) {
|
|
735
|
+
case "Write":
|
|
736
|
+
return typeof input.content === "string" ? input.content : null;
|
|
737
|
+
case "Edit":
|
|
738
|
+
return replaceOnce(before, input);
|
|
739
|
+
case "MultiEdit": {
|
|
740
|
+
if (!Array.isArray(input.edits) || !input.edits.length) return null;
|
|
741
|
+
let text = before;
|
|
742
|
+
for (const edit of input.edits) if ((text = replaceOnce(text, edit)) === null) return null;
|
|
743
|
+
return text;
|
|
744
|
+
}
|
|
745
|
+
case "apply_patch":
|
|
746
|
+
return patchedText(String(input.command ?? ""), path, before);
|
|
747
|
+
default:
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Apply a Codex patch's `*** Update File:` hunks for one path. A path the patch
|
|
754
|
+
* adds, deletes or moves, or a hunk whose old lines are not found, gives null.
|
|
755
|
+
*/
|
|
756
|
+
function patchedText(command, path, before) {
|
|
757
|
+
let section = null;
|
|
758
|
+
let found = false;
|
|
759
|
+
const hunks = [];
|
|
760
|
+
for (const line of command.split(/\r?\n/)) {
|
|
761
|
+
const header = /^\*\*\* (Add|Update|Delete) File: (.+)$/.exec(line);
|
|
762
|
+
if (header) {
|
|
763
|
+
section = header[2].trim() === path ? header[1] : null;
|
|
764
|
+
if (section && section !== "Update") return null;
|
|
765
|
+
if (section) found = true;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
if (section !== "Update") continue;
|
|
769
|
+
if (/^\*\*\* Move to: /.test(line)) return null;
|
|
770
|
+
if (/^\*\*\* (?:End Patch|End of File)/.test(line)) continue;
|
|
771
|
+
if (line.startsWith("@@")) {
|
|
772
|
+
hunks.push({ anchor: line.slice(2).trim(), old: [], next: [] });
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
if (!hunks.length) hunks.push({ anchor: "", old: [], next: [] });
|
|
776
|
+
const hunk = hunks.at(-1);
|
|
777
|
+
const body = line.slice(1);
|
|
778
|
+
if (line === "" || line[0] === " ") { hunk.old.push(body); hunk.next.push(body); }
|
|
779
|
+
else if (line[0] === "-") hunk.old.push(body);
|
|
780
|
+
else if (line[0] === "+") hunk.next.push(body);
|
|
781
|
+
else return null;
|
|
782
|
+
}
|
|
783
|
+
if (!found || !hunks.length) return null;
|
|
784
|
+
const text = before.split("\n");
|
|
785
|
+
let at = 0;
|
|
786
|
+
for (const hunk of hunks) {
|
|
787
|
+
if (!hunk.old.length) return null;
|
|
788
|
+
if (hunk.anchor) {
|
|
789
|
+
const anchor = text.findIndex((line, index) => index >= at && line.trim() === hunk.anchor);
|
|
790
|
+
if (anchor < 0) return null;
|
|
791
|
+
at = anchor + 1;
|
|
792
|
+
}
|
|
793
|
+
const match = (same) => {
|
|
794
|
+
for (let index = at; index + hunk.old.length <= text.length; index++)
|
|
795
|
+
if (hunk.old.every((line, offset) => same(text[index + offset], line))) return index;
|
|
796
|
+
return -1;
|
|
797
|
+
};
|
|
798
|
+
let index = match((a, b) => a === b);
|
|
799
|
+
if (index < 0) index = match((a, b) => a.trimEnd() === b.trimEnd());
|
|
800
|
+
if (index < 0) return null;
|
|
801
|
+
text.splice(index, hunk.old.length, ...hunk.next);
|
|
802
|
+
at = index + hunk.next.length;
|
|
803
|
+
}
|
|
804
|
+
return text.join("\n");
|
|
805
|
+
}
|
|
806
|
+
|
|
694
807
|
/** Edit-tool ownership and generated-file blocks. */
|
|
695
|
-
export async function editViolations(paths, context) {
|
|
808
|
+
export async function editViolations(paths, context, payload = null) {
|
|
696
809
|
const violations = [];
|
|
697
810
|
const root = await realpath(context.root).catch(() => resolve(context.root));
|
|
698
811
|
for (const path of paths) {
|
|
@@ -732,7 +845,7 @@ export async function editViolations(paths, context) {
|
|
|
732
845
|
violations.push({ code: "SAFETY_EDIT_OUTSIDE_SCOPE", message: `Edit blocked outside the lane's owned paths in ${home.root}: ${home.rel}.` });
|
|
733
846
|
continue;
|
|
734
847
|
}
|
|
735
|
-
if (await generatedFile(home.rel, { ...context, root: home.root, generatedPaths: await harnessGenerated(home.root) }))
|
|
848
|
+
if (await generatedFile(home.rel, { ...context, root: home.root, generatedPaths: await harnessGenerated(home.root) }) && !(await markEditAllowed(absolute, path, payload)))
|
|
736
849
|
violations.push({ code: "SAFETY_EDIT_GENERATED", message: `Edit blocked for generated file ${home.rel} in ${home.root}; change its source and regenerate it.` });
|
|
737
850
|
continue;
|
|
738
851
|
}
|
|
@@ -740,7 +853,7 @@ export async function editViolations(paths, context) {
|
|
|
740
853
|
violations.push({ code: "SAFETY_EDIT_OUTSIDE_SCOPE", message: `Edit blocked outside the lane's owned paths: ${rel}.` });
|
|
741
854
|
continue;
|
|
742
855
|
}
|
|
743
|
-
if (await generatedFile(rel, context))
|
|
856
|
+
if ((await generatedFile(rel, context)) && !(await markEditAllowed(absolute, path, payload)))
|
|
744
857
|
violations.push({ code: "SAFETY_EDIT_GENERATED", message: `Edit blocked for generated file ${rel}; change its source and regenerate it.` });
|
|
745
858
|
}
|
|
746
859
|
return violations;
|
|
@@ -943,7 +1056,7 @@ export async function safetyPreHook(payload, contextOrResolver = safetyContext,
|
|
|
943
1056
|
if (provenLane(context)) blocked(violation);
|
|
944
1057
|
return { allowed: true, nudges: [nudgeViolation(violation)] };
|
|
945
1058
|
}
|
|
946
|
-
const violation = (await editViolations(paths, context))[0];
|
|
1059
|
+
const violation = (await editViolations(paths, context, payload))[0];
|
|
947
1060
|
if (violation && provenLane(context)) blocked(violation);
|
|
948
1061
|
return {
|
|
949
1062
|
allowed: true,
|