@yeison.restrepo.r/code-conductor 1.34.4 → 1.35.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/README.md +6 -0
- package/lib/installer/host-owned.mjs +1 -0
- package/package.json +1 -1
- package/project-template/.claude/hooks/pre-tool-use.mjs +75 -3
- package/project-template/gitignore +1 -0
- package/scripts/snap-build.mjs +21 -5
- package/scripts/snap-contract.mjs +31 -6
- package/scripts/snap-validate.mjs +16 -9
package/README.md
CHANGED
|
@@ -100,6 +100,10 @@ Four checks live in `tools/` as tracked repository infrastructure. Three of them
|
|
|
100
100
|
|
|
101
101
|
- **`[BUG-045]`, open:** the Guard 3 allowlist cannot cover a quoted path, because the boundary sets it interpolates contain no quote character, so an entry `docs/` does not cover `cat "docs/x.md" *.md`. Filed with its ritual priced, untouched pending its own change.
|
|
102
102
|
- **`[BUG-032]`, open:** global memory preferences sit outside the documented lookup chain. Nothing in the chain points at `~/.claude/memory/personal.md`, and the installer never deploys `global/memory/`, so a preference filed there is never read by the agent it was written for.
|
|
103
|
+
- **`[BUG-050]`, open:** the release instruments are silent on sub-shaped ids. `tools/record-parity.mjs` and `tools/id-ceiling.mjs` neither read nor reject an id like `[ARCH-008-S1]`, so a release claiming one would pass record parity having read nothing. No shipped release does; releases claim top-level ids until the item's spec chooses its repair.
|
|
104
|
+
- **`[BUG-051]`, open:** two shipped commands run `scripts/detect-stack.mjs`, but the installer deploys the detector to `.claude/scripts/`. In an installed project, `/cc-stack` fails loud (`MODULE_NOT_FOUND`, "no stack"), and `/cc-resume`'s blank-command auto-fill silently skips. Until it is fixed, `/cc-init`, which already uses the deployed path, records the stack and commands.
|
|
105
|
+
- **`[BUG-052]`, open:** the installer has no self-install guard. Its sweep of the 1.23.2 legacy root `scripts/` matches by exact file list, which the development repository satisfies by identity, so running `--project` inside this repository deletes tracked source. Do not run the installer against a clone of code-conductor itself.
|
|
106
|
+
- **`[BUG-053]`, open:** the pre-commit test gate that the retired `install.sh` appended runs `npm test` with git's hook environment. From a linked worktree, git hands hooks an absolute `GIT_DIR` and `GIT_INDEX_FILE`, so the suite's fixture `git` commands write into the real repository's refs, config and index. Do not run the gate from a linked worktree.
|
|
103
107
|
- **The `P7` false positive above**, still live.
|
|
104
108
|
- **A re-run against an untouched `project.md` prints a recovery hint it cannot prove is needed.** If you install, never write anything into `.claude/memory/project.md`, and install again, you get a line suggesting the file may have been overwritten by a pre-`1.30.0` re-run. It was not; it equals the stub because it was seeded and never edited. The check compares content and **cannot distinguish "seeded and untouched" from "clobbered"**, which is why the wording is hedged to "may have been" rather than "was". This residual is named and accepted in [`BUG-039`'s spec at `:129`](docs/superpowers/specs/2026-09-27-bug039-installer-host-owned-state-design.md), where the alternative (restoring from the host's own git history) was rejected as writing host files out of the host's history with new failure modes. A **fresh** install is silent, which the Quickstart shows.
|
|
105
109
|
- **Three open dossiers**, which are the evidence-collection pipeline working rather than a backlog: a session denial tally, one for interleaved-artifact reports, and an intermittent commit-hook hang in the `snap-build` suite. A dossier holds specimens until a mechanism is characterized by probe; an id is minted only when the written condition is met. `[BUG-047]` is what that pipeline produces when it completes: an out-of-scope note, then a dossier, then four specimens across four sessions, then a mint, then a release.
|
|
@@ -286,6 +290,8 @@ Hit a block you believe is wrong? Re-run the command with `CC_GUARD3_WARN=1` and
|
|
|
286
290
|
|
|
287
291
|
**node_modules guard (Guard 4)** - a `Read` whose path carries `node_modules` as an exact path component is denied, with backslashes and `..` resolved first. Use Glob for existence checks.
|
|
288
292
|
|
|
293
|
+
**Band scope guard (Guard 5)** - a `Write`, `Edit`, `create_file` or `write_file` from a subagent whose `agent_type` names a band role (`spec`, `plan`, `code`, `audit`, `qa`) is checked against the nearest `.claude/memory/band-envelope.json` above its working directory, a SNAP v3 envelope. A malformed envelope, an agent that is not the envelope's role, a role not holding `RW`, or a path outside the envelope's `scope` globs (anchored at the band root, the directory whose `.claude/` holds the envelope) is denied with a named reason. The main session, any other agent, and any project without an envelope are untouched. It assumes cooperative agents: `agent_type` is a name taken on trust, and `Bash`, `NotebookEdit`, MCP write tools and symlinked paths are not covered.
|
|
294
|
+
|
|
289
295
|
Input the hook cannot parse fails closed: it is denied with one stderr line naming `CC_HOOK_ALLOW=1`, which overrides that denial alone and leaves every guard fully active on every payload the hook can read. Set `CC_HOOK_DEBUG=1` to see the diagnostic lines it otherwise swallows.
|
|
290
296
|
|
|
291
297
|
### context-guard *(global + project)* — v1.15.0
|
|
@@ -40,6 +40,7 @@ export const PROJECT_HOST_OWNED = new Map([
|
|
|
40
40
|
['memory/session-snapshot.json', 'skip'],
|
|
41
41
|
['memory/session-snapshot.md', 'skip'],
|
|
42
42
|
['memory/turn-count.txt', 'skip'],
|
|
43
|
+
['memory/band-envelope.json', 'skip'],
|
|
43
44
|
['settings.json', 'merge'],
|
|
44
45
|
['settings.local.json', 'skip'],
|
|
45
46
|
]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yeison.restrepo.r/code-conductor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.35.0",
|
|
4
4
|
"description": "A governance layer for Claude Code sessions: hooks that check an agent's commands before they run, a spec-first workflow, and project memory the next session reads.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentic-development",
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
// hookSpecificOutput.permissionDecision and EVERY path exits 0. Nothing here exits 2:
|
|
5
5
|
// a deliberate denial must never be indistinguishable from a crashed script.
|
|
6
6
|
// Zero dependencies by design; node: builtins only.
|
|
7
|
-
import { readFileSync, statSync } from 'node:fs';
|
|
8
|
-
import { posix, join } from 'node:path';
|
|
7
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
8
|
+
import { posix, join, resolve, dirname, relative, isAbsolute, sep } from 'node:path';
|
|
9
9
|
|
|
10
10
|
const LINE_LIMIT = 150;
|
|
11
11
|
const BLOCKED_COMPONENTS = new Set(['node_modules']);
|
|
@@ -108,6 +108,76 @@ function guard2DuplicateWrite(input) {
|
|
|
108
108
|
);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
// ── Guard 5 constants ─────────────────────────────────────────────────────────
|
|
112
|
+
// Copies of scripts/snap-contract.mjs values (ARCH-010 D7). This file is mirrored to two
|
|
113
|
+
// locations and the contract sits at a different relative path from each, so no import is
|
|
114
|
+
// correct in both; tests/hooks/guard5.test.js pins every copy to the contract instead.
|
|
115
|
+
const BAND_ROLES = ['spec', 'plan', 'code', 'audit', 'qa'];
|
|
116
|
+
const BAND_TOOL_KINDS = ['R', 'RW', 'X'];
|
|
117
|
+
const BAND_WRITE_TOOLS = ['Write', 'Edit', 'create_file', 'write_file'];
|
|
118
|
+
const BAND_ENVELOPE_MAX_BYTES = 10485760;
|
|
119
|
+
const BAND_ENVELOPE_REL = ['.claude', 'memory', 'band-envelope.json'];
|
|
120
|
+
|
|
121
|
+
const isPlainObject = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
|
|
122
|
+
|
|
123
|
+
// The band root is the nearest ancestor of cwd holding the envelope; the scope globs
|
|
124
|
+
// anchor there too, so discovery and anchoring are one rule (owner ruling, Case F).
|
|
125
|
+
function findBandRoot(start) {
|
|
126
|
+
let dir = resolve(start);
|
|
127
|
+
while (!existsSync(join(dir, ...BAND_ENVELOPE_REL))) {
|
|
128
|
+
const parent = dirname(dir);
|
|
129
|
+
if (parent === dir) return null;
|
|
130
|
+
dir = parent;
|
|
131
|
+
}
|
|
132
|
+
return dir;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Only what Guard 5 itself reads is checked. Anything less is Case C, not a guess.
|
|
136
|
+
function readEnvelope(path) {
|
|
137
|
+
let env;
|
|
138
|
+
try {
|
|
139
|
+
if (statSync(path).size > BAND_ENVELOPE_MAX_BYTES) return null;
|
|
140
|
+
env = JSON.parse(readFileSync(path, 'utf8'));
|
|
141
|
+
} catch { return null; }
|
|
142
|
+
if (!isPlainObject(env) || env.v !== 3 || !isPlainObject(env.sys) || !isPlainObject(env.ops)) return null;
|
|
143
|
+
if (!BAND_ROLES.includes(env.sys.role) || !BAND_TOOL_KINDS.includes(env.sys.tk)) return null;
|
|
144
|
+
const scope = env.ops.scope;
|
|
145
|
+
if (env.sys.tk === 'RW' && !(Array.isArray(scope) && scope.every((g) => typeof g === 'string'))) return null;
|
|
146
|
+
return env;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// `**/` spans zero or more directories, a trailing `**` any depth, `*` one segment.
|
|
150
|
+
function globToRegExp(glob) {
|
|
151
|
+
const body = glob.split(/(\*\*\/|\*\*|\*)/).map((part) => {
|
|
152
|
+
if (part === '**/') return '(?:.*/)?';
|
|
153
|
+
if (part === '**') return '.*';
|
|
154
|
+
if (part === '*') return '[^/]*';
|
|
155
|
+
return part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
156
|
+
}).join('');
|
|
157
|
+
return new RegExp(`^${body}$`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Guard 5: a role agent writing outside its band envelope's declared scope (ARCH-010).
|
|
161
|
+
// The main session carries no agent_type and is never subject to it (Case A); with no
|
|
162
|
+
// envelope there is nothing to verify (Case B). Cases C-G are the spec's table.
|
|
163
|
+
function guard5BandScope(input, payload) {
|
|
164
|
+
const role = payload.agent_type;
|
|
165
|
+
if (!BAND_ROLES.includes(role)) return null;
|
|
166
|
+
const cwd = typeof payload.cwd === 'string' && payload.cwd ? payload.cwd : process.cwd();
|
|
167
|
+
const root = findBandRoot(cwd);
|
|
168
|
+
if (!root) return null;
|
|
169
|
+
const file = join(root, ...BAND_ENVELOPE_REL);
|
|
170
|
+
const env = readEnvelope(file);
|
|
171
|
+
if (!env) return deny(`Guard 5: BAND_ENVELOPE_INVALID: ${file} is unreadable or not a valid v3 band envelope.`);
|
|
172
|
+
if (env.sys.role !== role) return deny(`Guard 5: BAND_ROLE_MISMATCH: agent ${role} is not the envelope's role ${env.sys.role}.`);
|
|
173
|
+
if (env.sys.tk !== 'RW') return deny(`Guard 5: BAND_READ_ONLY: role ${role} holds tk ${env.sys.tk}, not RW.`);
|
|
174
|
+
const target = targetPath(input);
|
|
175
|
+
const rel = relative(root, resolve(cwd, target)).split(sep).join('/');
|
|
176
|
+
const inside = target !== '' && rel !== '' && rel !== '..' && !rel.startsWith('../') && !isAbsolute(rel);
|
|
177
|
+
if (inside && env.ops.scope.some((g) => globToRegExp(g).test(rel))) return null;
|
|
178
|
+
return deny(`Guard 5: BAND_SCOPE_VIOLATION: ${target || '<no path>'} is outside the declared scope [${env.ops.scope.join(', ')}]`);
|
|
179
|
+
}
|
|
180
|
+
|
|
111
181
|
// ── Guard 3 constants ─────────────────────────────────────────────────────────
|
|
112
182
|
// POSIX ERE fragments from tests/fixtures/guard3-reference.sh, translated class by
|
|
113
183
|
// class. [[:space:]] in the C locale is EXACTLY [ \t\n\r\f\v]; JavaScript \s also
|
|
@@ -563,6 +633,8 @@ const DISPATCH = {
|
|
|
563
633
|
Edit: [],
|
|
564
634
|
Bash: [guard3BashScan],
|
|
565
635
|
};
|
|
636
|
+
// Guard 5 runs first on every write-family tool: a scope deny outranks Guard 2's ask.
|
|
637
|
+
for (const tool of BAND_WRITE_TOOLS) DISPATCH[tool].unshift(guard5BandScope);
|
|
566
638
|
|
|
567
639
|
// Case B. Unparseable input is not "nothing to verify", it is "the verifier could not
|
|
568
640
|
// run", which is the condition that fails closed. CC_HOOK_ALLOW bypasses THIS denial only;
|
|
@@ -600,7 +672,7 @@ function main() {
|
|
|
600
672
|
const guards = DISPATCH[name];
|
|
601
673
|
if (!guards) { debug(`no guard registered for tool_name "${name}"; allowing`); return; }
|
|
602
674
|
for (const guard of guards) {
|
|
603
|
-
const decision = guard(input);
|
|
675
|
+
const decision = guard(input, payload);
|
|
604
676
|
if (decision) { emit(decision); return; }
|
|
605
677
|
}
|
|
606
678
|
}
|
package/scripts/snap-build.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
|
-
import { CAPS, PRE_PARSE_MAX_BYTES as MAX_SNAP_BYTES, V1_MAX_CHARS } from './snap-contract.mjs';
|
|
2
|
+
import { CAPS, PRE_PARSE_MAX_BYTES as MAX_SNAP_BYTES, V1_MAX_CHARS, V3_CAPS } from './snap-contract.mjs';
|
|
3
3
|
|
|
4
4
|
const die = (msg) => { process.stderr.write(`SNAP_BUILD_ERROR: ${msg}\n`); process.exit(1); };
|
|
5
5
|
const byteLen = (s) => Buffer.byteLength(s, 'utf8');
|
|
@@ -49,7 +49,22 @@ const ops = { n: normArray(obj.n, CAPS['ops.n']), f: normArray(obj.f, CAPS['ops.
|
|
|
49
49
|
const mem = { d: normArray(obj.d, CAPS['mem.d']), x: normArray(obj.x, CAPS['mem.x']) };
|
|
50
50
|
const pr = typeof obj.pr === 'string' ? obj.pr : '';
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
// ---- v3: any band field selects the band envelope (ARCH-010); without one, v1/v2 are untouched ----
|
|
53
|
+
const band = ['role', 'tk', 'scope', 'gate', 'p'].some((k) => obj[k] !== undefined);
|
|
54
|
+
if (band) {
|
|
55
|
+
for (const k of ['role', 'tk', 'gate']) {
|
|
56
|
+
if (typeof obj[k] !== 'string' || obj[k] === '') die(`missing or empty scalar: ${k}`);
|
|
57
|
+
}
|
|
58
|
+
if (obj.scope !== undefined && !Array.isArray(obj.scope)) die('scope must be an array');
|
|
59
|
+
if (obj.p !== undefined && (typeof obj.p !== 'object' || obj.p === null || Array.isArray(obj.p))) die('p must be a plain object');
|
|
60
|
+
// Assignment order is serialization order: BLOCK_FIELDS[3] lists scope before gate.
|
|
61
|
+
Object.assign(sys, { role: obj.role, tk: obj.tk });
|
|
62
|
+
if (obj.scope !== undefined) ops.scope = normArray(obj.scope, V3_CAPS['ops.scope']);
|
|
63
|
+
ops.gate = obj.gate;
|
|
64
|
+
if (obj.p !== undefined) mem.p = obj.p;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (pr === '' && !band) {
|
|
53
68
|
// ---- v1: V1_MAX_CHARS cap, drop oldest of mem.d / ops.f ----
|
|
54
69
|
const snap = { v: 1, sys, ops, mem };
|
|
55
70
|
let line = JSON.stringify(snap);
|
|
@@ -60,8 +75,9 @@ if (pr === '') {
|
|
|
60
75
|
}
|
|
61
76
|
writeOut(line);
|
|
62
77
|
} else {
|
|
63
|
-
// ---- v2: 10 MiB cap, truncate raw pr before serialize ----
|
|
64
|
-
const
|
|
78
|
+
// ---- v2 and v3: 10 MiB cap, truncate raw pr before serialize ----
|
|
79
|
+
const v = band ? 3 : 2;
|
|
80
|
+
const skeletonBytes = byteLen(JSON.stringify({ v, sys, ops, mem, pr: '' }));
|
|
65
81
|
if (skeletonBytes > MAX_SNAP_BYTES) die('skeleton exceeds cap even without prose');
|
|
66
82
|
|
|
67
83
|
// total serialized bytes for a candidate pr value (skeleton already counts the two empty-value quotes)
|
|
@@ -82,6 +98,6 @@ if (pr === '') {
|
|
|
82
98
|
}
|
|
83
99
|
}
|
|
84
100
|
|
|
85
|
-
const snap = { v
|
|
101
|
+
const snap = { v, sys, ops, mem, pr: pr.slice(0, keep) };
|
|
86
102
|
writeOut(JSON.stringify(snap));
|
|
87
103
|
}
|
|
@@ -14,13 +14,13 @@ export const PRE_PARSE_MAX_BYTES = 10485760; // 10 MiB
|
|
|
14
14
|
// constraint, kept under its own name.
|
|
15
15
|
export const V1_MAX_CHARS = 4096;
|
|
16
16
|
|
|
17
|
-
// Post-parse, version-specific caps, applied once snap.v is known.
|
|
18
|
-
//
|
|
19
|
-
export const POST_PARSE_MAX = { 1: V1_MAX_CHARS, 2: PRE_PARSE_MAX_BYTES };
|
|
17
|
+
// Post-parse, version-specific caps, applied once snap.v is known. v2 and v3 share the
|
|
18
|
+
// pre-parse ceiling: neither is a context-budget file.
|
|
19
|
+
export const POST_PARSE_MAX = { 1: V1_MAX_CHARS, 2: PRE_PARSE_MAX_BYTES, 3: PRE_PARSE_MAX_BYTES };
|
|
20
20
|
|
|
21
21
|
// Highest v this contract understands. Raising it here moves the
|
|
22
22
|
// SNAP_UNKNOWN_VERSION boundary with no edit anywhere else.
|
|
23
|
-
export const MAX_VERSION =
|
|
23
|
+
export const MAX_VERSION = 3;
|
|
24
24
|
|
|
25
25
|
// One array-cap table in ONE key scheme: dotted path to [count cap, element cap].
|
|
26
26
|
// Two schemes for one table is how a drift hides from every diff and grep.
|
|
@@ -30,10 +30,35 @@ export const CAPS = {
|
|
|
30
30
|
'mem.d': [10, 300],
|
|
31
31
|
'mem.x': [5, 200],
|
|
32
32
|
};
|
|
33
|
+
// Same scheme, v3 only: ops.scope exists in no earlier version, so it cannot join CAPS.
|
|
34
|
+
export const V3_CAPS = { 'ops.scope': [20, 300] };
|
|
33
35
|
|
|
34
|
-
// Field sets
|
|
36
|
+
// Field sets, both per version. v1 and v2 share one block map, unchanged since v1;
|
|
37
|
+
// v3 adds the band fields reserved by FEAT-010 (ARCH-010).
|
|
35
38
|
export const TOP_FIELDS = {
|
|
36
39
|
1: ['v', 'sys', 'ops', 'mem'],
|
|
37
40
|
2: ['v', 'sys', 'ops', 'mem', 'pr'],
|
|
41
|
+
3: ['v', 'sys', 'ops', 'mem', 'pr'],
|
|
38
42
|
};
|
|
39
|
-
|
|
43
|
+
const V1_BLOCKS = { sys: ['ph', 'c', 's'], ops: ['n', 'f'], mem: ['d', 'x'] };
|
|
44
|
+
export const BLOCK_FIELDS = {
|
|
45
|
+
1: V1_BLOCKS,
|
|
46
|
+
2: V1_BLOCKS,
|
|
47
|
+
3: { sys: ['ph', 'c', 's', 'role', 'tk'], ops: ['n', 'f', 'scope', 'gate'], mem: ['d', 'x', 'p'] },
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// The band contract (ARCH-010). A role names an agent with a mask behind it, so the roster
|
|
51
|
+
// stops at FEAT-012's five and widens only at a new version. A gate names a band's whole
|
|
52
|
+
// exit condition, which the ARCH-009 band table fixes independently of any agent.
|
|
53
|
+
export const ROLES = ['spec', 'plan', 'code', 'audit', 'qa'];
|
|
54
|
+
export const TOOL_KINDS = ['R', 'RW', 'X'];
|
|
55
|
+
export const BANDS = ['boundary', 'define', 'build', 'verify', 'ship'];
|
|
56
|
+
export const GATES = ['boundary_routed', 'define_approved', 'build_executed', 'verify_pass', 'ship_released'];
|
|
57
|
+
export const ROLE_BAND = { spec: 'define', plan: 'define', code: 'build', audit: 'verify', qa: 'verify' };
|
|
58
|
+
// The tools Guard 5 gates. The hook cannot import this module from both of its install
|
|
59
|
+
// locations, so it carries a copy that tests/hooks/guard5.test.js pins to this one (D7).
|
|
60
|
+
export const WRITE_TOOLS = ['Write', 'Edit', 'create_file', 'write_file'];
|
|
61
|
+
|
|
62
|
+
// A receiving role expects the exit gate of the band before its own: GATES[i] is the
|
|
63
|
+
// exit of BANDS[i].
|
|
64
|
+
export const expectedGate = (role) => GATES[BANDS.indexOf(ROLE_BAND[role]) - 1];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs'; import { BLOCK_FIELDS, CAPS, MAX_VERSION, POST_PARSE_MAX, PRE_PARSE_MAX_BYTES, TOP_FIELDS } from './snap-contract.mjs';
|
|
1
|
+
import { readFileSync } from 'node:fs'; import { BLOCK_FIELDS, CAPS, GATES, MAX_VERSION, POST_PARSE_MAX, PRE_PARSE_MAX_BYTES, ROLES, TOOL_KINDS, TOP_FIELDS, V3_CAPS, expectedGate } from './snap-contract.mjs';
|
|
2
2
|
const err = (m) => { process.stderr.write(`SNAP_ERROR: ${m}\n`); process.exit(1); };
|
|
3
|
-
const path = process.argv
|
|
3
|
+
const [path, flag, to] = process.argv.slice(2); if (path === undefined) err('no path provided');
|
|
4
|
+
if (flag !== undefined && (flag !== '--to' || !ROLES.includes(to))) err(`usage: snap-validate.mjs <file> [--to ${ROLES.join('|')}]`);
|
|
4
5
|
let raw; try { raw = readFileSync(path, 'utf8'); } catch (e) { err(e.code === 'ENOENT' ? 'file not found' : e.code); }
|
|
5
6
|
if (raw.includes('�')) err('encoding error');
|
|
6
7
|
const trimmed = raw.trim(); if (trimmed.includes('\n')) err('internal newline in payload');
|
|
@@ -8,18 +9,23 @@ if (trimmed === '') err('empty file');
|
|
|
8
9
|
if (raw.length > PRE_PARSE_MAX_BYTES) err(`payload too large: ${raw.length} > ${PRE_PARSE_MAX_BYTES} (pre-parse ceiling)`);
|
|
9
10
|
let snap; try { snap = JSON.parse(trimmed); } catch { err('malformed JSON'); }
|
|
10
11
|
if (typeof snap !== 'object' || snap === null || Array.isArray(snap)) err('root must be a plain object');
|
|
12
|
+
// D2: v is decided before every check whose meaning depends on it (BUG-038's check-order class, second sighting).
|
|
13
|
+
if (snap.v === undefined) err('missing: v'); if (typeof snap.v !== 'number' || !Number.isInteger(snap.v) || snap.v < 1) err('v must be a positive integer');
|
|
14
|
+
if (snap.v > MAX_VERSION) err('SNAP_UNKNOWN_VERSION');
|
|
11
15
|
for (const b of ['sys', 'ops', 'mem']) if (typeof snap[b] !== 'object' || snap[b] === null || Array.isArray(snap[b])) err(`missing block: ${b}`);
|
|
12
|
-
const
|
|
13
|
-
const missing =
|
|
16
|
+
const band = snap.v >= 3; const at = (k) => snap[k.split('.')[0]][k.split('.')[1]];
|
|
17
|
+
const missing = ['sys.ph', 'sys.c', 'sys.s', 'ops.n', 'ops.f', 'mem.d', 'mem.x', ...(band ? ['sys.role', 'sys.tk', 'ops.gate'] : [])].filter(k => at(k) === undefined);
|
|
14
18
|
if (missing.length) { for (const k of missing) process.stderr.write(`SNAP_ERROR: missing: ${k}\n`); process.exit(1); }
|
|
15
|
-
const
|
|
16
|
-
const topExtra = Object.keys(snap).find(k => !topAllowed.includes(k)); if (topExtra) err(`unexpected key: ${topExtra}`);
|
|
19
|
+
const topExtra = Object.keys(snap).find(k => !TOP_FIELDS[snap.v].includes(k)); if (topExtra) err(`unexpected key: ${topExtra}`);
|
|
17
20
|
if (snap.pr !== undefined && typeof snap.pr !== 'string') err('pr must be a string');
|
|
18
|
-
for (const b of ['sys', 'ops', 'mem']) { const extra = Object.keys(snap[b]).find(k => !BLOCK_FIELDS[b].includes(k)); if (extra) err(`unexpected key: ${b}.${extra}`); }
|
|
19
|
-
if (typeof snap.v !== 'number' || !Number.isInteger(snap.v) || snap.v < 1) err('v must be a positive integer'); if (snap.v > MAX_VERSION) err('SNAP_UNKNOWN_VERSION');
|
|
21
|
+
for (const b of ['sys', 'ops', 'mem']) { const extra = Object.keys(snap[b]).find(k => !BLOCK_FIELDS[snap.v][b].includes(k)); if (extra) err(`unexpected key: ${b}.${extra}`); }
|
|
20
22
|
if (raw.length > POST_PARSE_MAX[snap.v]) err(`payload too large: ${raw.length} > ${POST_PARSE_MAX[snap.v]} (v${snap.v} cap)`);
|
|
21
23
|
if (!['spec', 'plan', 'impl', 'rev'].includes(snap.sys.ph)) err('ph must be spec|plan|impl|rev');
|
|
22
|
-
|
|
24
|
+
if (band && !ROLES.includes(snap.sys.role)) err(`role must be ${ROLES.join('|')}`); if (band && !TOOL_KINDS.includes(snap.sys.tk)) err(`tk must be ${TOOL_KINDS.join('|')}`);
|
|
25
|
+
if (band && !GATES.includes(snap.ops.gate)) err(`gate must be ${GATES.join('|')}`); if (band && snap.sys.tk === 'RW' && snap.ops.scope === undefined) err('missing: ops.scope (required when tk is RW)');
|
|
26
|
+
const p = snap.mem.p; if (p !== undefined && (typeof p !== 'object' || p === null || Array.isArray(p))) err('p must be a plain object');
|
|
27
|
+
const arrayCaps = { ...CAPS, ...(snap.ops.scope === undefined ? {} : V3_CAPS) };
|
|
28
|
+
for (const [key, [cap, elemCap]] of Object.entries(arrayCaps)) {
|
|
23
29
|
const [blk, sub] = key.split('.'); const arr = snap[blk][sub]; if (!Array.isArray(arr)) err(`${key} must be an array`); if (arr.length > cap) err(`${key} exceeds cap`);
|
|
24
30
|
arr.forEach((el, i) => { if (typeof el !== 'string' || el.trim() === '') err(`empty element in ${key}[${i}]`); if (JSON.stringify(el).slice(1, -1).length > elemCap) err(`element too long in ${key}[${i}]`); });
|
|
25
31
|
}
|
|
@@ -29,4 +35,5 @@ snap.ops.f.forEach((el, i) => {
|
|
|
29
35
|
if (!['C', 'M', 'D'].includes(el.slice(idx + 1))) err(`invalid action code in ops.f[${i}]`);
|
|
30
36
|
});
|
|
31
37
|
if (!/^[0-9a-f]{7,64}$/.test(snap.sys.c)) err('invalid sys.c format'); if (!/^[a-zA-Z0-9._-]+$/.test(snap.sys.s)) err('invalid chars in sys.s');
|
|
38
|
+
if (to !== undefined && !band) err('--to requires a v3 envelope'); if (to !== undefined && snap.ops.gate !== expectedGate(to)) err(`SNAP_GATE_MISMATCH: ${to} expects ${expectedGate(to)}, got ${snap.ops.gate}`);
|
|
32
39
|
process.exit(0);
|