@opengsd/gsd-core 1.5.0 → 1.6.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/agents/gsd-plan-checker.md +34 -0
- package/agents/gsd-planner.md +2 -0
- package/agents/gsd-roadmapper.md +6 -0
- package/bin/install.js +199 -365
- package/commands/gsd/capture.md +5 -1
- package/gemini-extension.json +1 -1
- package/gsd-core/bin/gsd-tools.cjs +695 -5
- package/gsd-core/bin/lib/adr-parser.cjs +45 -23
- package/gsd-core/bin/lib/audit.cjs +2 -2
- package/gsd-core/bin/lib/capability-consent.cjs +763 -0
- package/gsd-core/bin/lib/capability-ledger.cjs +831 -0
- package/gsd-core/bin/lib/capability-lifecycle.cjs +1551 -0
- package/gsd-core/bin/lib/capability-loader.cjs +764 -0
- package/gsd-core/bin/lib/capability-lock.cjs +553 -0
- package/gsd-core/bin/lib/capability-registry.cjs +198 -4
- package/gsd-core/bin/lib/capability-source.cjs +1242 -0
- package/gsd-core/bin/lib/capability-state.cjs +9 -6
- package/gsd-core/bin/lib/capability-trust.cjs +550 -0
- package/gsd-core/bin/lib/capability-validator.cjs +2066 -0
- package/gsd-core/bin/lib/capability-writer.cjs +14 -5
- package/gsd-core/bin/lib/check-command-router.cjs +69 -18
- package/gsd-core/bin/lib/command-aliases.cjs +8 -0
- package/gsd-core/bin/lib/commands.cjs +247 -0
- package/gsd-core/bin/lib/config-loader.cjs +98 -84
- package/gsd-core/bin/lib/config-schema.cjs +26 -7
- package/gsd-core/bin/lib/config.cjs +7 -1
- package/gsd-core/bin/lib/decisions.cjs +149 -60
- package/gsd-core/bin/lib/frontmatter.cjs +7 -3
- package/gsd-core/bin/lib/gap-checker.cjs +126 -11
- package/gsd-core/bin/lib/init.cjs +91 -22
- package/gsd-core/bin/lib/legacy-cleanup.cjs +96 -0
- package/gsd-core/bin/lib/loop-resolver.cjs +26 -2
- package/gsd-core/bin/lib/markdown-sectionizer.cjs +471 -0
- package/gsd-core/bin/lib/milestone.cjs +41 -2
- package/gsd-core/bin/lib/phase-command-router.cjs +5 -0
- package/gsd-core/bin/lib/phase-id.cjs +25 -11
- package/gsd-core/bin/lib/phase-lifecycle.cjs +14 -5
- package/gsd-core/bin/lib/phase.cjs +33 -4
- package/gsd-core/bin/lib/probe-core.cjs +7 -0
- package/gsd-core/bin/lib/prohibition-enforcement.cjs +59 -26
- package/gsd-core/bin/lib/project-root.cjs +89 -2
- package/gsd-core/bin/lib/resolution.cjs +26 -0
- package/gsd-core/bin/lib/roadmap-command-router.cjs +16 -3
- package/gsd-core/bin/lib/roadmap-parser.cjs +73 -106
- package/gsd-core/bin/lib/roadmap-upgrade.cjs +47 -17
- package/gsd-core/bin/lib/roadmap.cjs +5 -2
- package/gsd-core/bin/lib/runtime-artifact-conversion.cjs +423 -3
- package/gsd-core/bin/lib/runtime-artifact-install-plan.cjs +77 -0
- package/gsd-core/bin/lib/runtime-artifact-layout.cjs +1 -28
- package/gsd-core/bin/lib/runtime-homes.cjs +53 -1
- package/gsd-core/bin/lib/runtime-name-policy.cjs +44 -0
- package/gsd-core/bin/lib/semver-compare.cjs +127 -0
- package/gsd-core/bin/lib/shell-command-projection.cjs +55 -1
- package/gsd-core/bin/lib/state-document.cjs +4 -2
- package/gsd-core/bin/lib/state.cjs +317 -161
- package/gsd-core/bin/lib/surface.cjs +12 -19
- package/gsd-core/bin/lib/uat-predicate.cjs +7 -47
- package/gsd-core/bin/lib/uat.cjs +39 -26
- package/gsd-core/bin/lib/validate.cjs +5 -2
- package/gsd-core/bin/lib/verify.cjs +40 -15
- package/gsd-core/bin/lib/worktree-safety.cjs +202 -0
- package/gsd-core/bin/shared/config-defaults.manifest.json +6 -1
- package/gsd-core/bin/shared/config-schema.manifest.json +5 -1
- package/gsd-core/references/context-budget.md +8 -8
- package/gsd-core/references/execute-phase-between-wave-reset.md +43 -0
- package/gsd-core/references/execute-phase-context-guard.md +16 -0
- package/gsd-core/references/execute-phase-wave-guard.md +33 -0
- package/gsd-core/references/planner-antipatterns.md +48 -0
- package/gsd-core/references/planning-config.md +4 -0
- package/gsd-core/references/prohibition-probe.md +15 -9
- package/gsd-core/references/scout-codebase.md +2 -2
- package/gsd-core/workflows/autonomous.md +33 -33
- package/gsd-core/workflows/diagnose-issues.md +6 -1
- package/gsd-core/workflows/discuss-phase/templates/context.md +1 -1
- package/gsd-core/workflows/discuss-phase.md +1 -2
- package/gsd-core/workflows/execute-phase.md +12 -12
- package/gsd-core/workflows/help/modes/full.md +10 -0
- package/gsd-core/workflows/list-seeds.md +63 -0
- package/gsd-core/workflows/manager.md +37 -37
- package/gsd-core/workflows/pr-branch.md +156 -0
- package/gsd-core/workflows/quick.md +6 -1
- package/gsd-core/workflows/review.md +10 -2
- package/gsd-core/workflows/spec-phase.md +8 -3
- package/gsd-core/workflows/verify-phase.md +2 -2
- package/package.json +6 -3
- package/scripts/gen-capability-matrix.cjs +284 -0
- package/scripts/gen-capability-registry.cjs +96 -1853
- package/scripts/lint-regression-test-names.allowlist.json +1 -0
- package/scripts/lint-resolution-provenance.allowlist.json +1 -0
- package/scripts/lint-resolution-provenance.cjs +192 -0
- package/scripts/lint-test-file-count.allowlist.json +9 -0
- package/scripts/prompt-injection-scan.sh +1 -0
- package/scripts/run-tests.cjs +14 -0
- package/scripts/sync-manifest-versions.cjs +77 -5
|
@@ -0,0 +1,2066 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* capability-validator.cjs — shared, runtime-callable capability validator.
|
|
5
|
+
*
|
|
6
|
+
* Extracted from scripts/gen-capability-registry.cjs per ADR-1244 D2 so that
|
|
7
|
+
* both the build-time generator and the runtime overlay loader can require the
|
|
8
|
+
* validator WITHOUT pulling in the generator's build-time-only machinery
|
|
9
|
+
* (ExitError, config-schema.manifest.json, install-profiles.cjs, clusters.cjs,
|
|
10
|
+
* gen-loop-host-contract.cjs, etc.).
|
|
11
|
+
*
|
|
12
|
+
* This is a COMMITTED plain .cjs (not built from .cts) so it is available on a
|
|
13
|
+
* fresh worktree before `npm run build:lib` has run.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
|
|
18
|
+
const { LOOP_HOST_CONTRACT } = require('./loop-host-contract.cjs');
|
|
19
|
+
|
|
20
|
+
// ─── Shared schema-version constant ──────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
const SCHEMA_VERSION = '1';
|
|
23
|
+
|
|
24
|
+
// ─── Loop Host Contract ───────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
// Canonical point order — explicit constant (do NOT rely on Set insertion order).
|
|
27
|
+
// Used for point-ordering semantics in consumes-satisfiability validation and topo-sort.
|
|
28
|
+
const POINT_ORDER = [
|
|
29
|
+
'discuss:pre',
|
|
30
|
+
'discuss:post',
|
|
31
|
+
'plan:pre',
|
|
32
|
+
'plan:post',
|
|
33
|
+
'execute:pre',
|
|
34
|
+
'execute:wave:pre',
|
|
35
|
+
'execute:wave:post',
|
|
36
|
+
'execute:post',
|
|
37
|
+
'verify:pre',
|
|
38
|
+
'verify:post',
|
|
39
|
+
'ship:pre',
|
|
40
|
+
'ship:post',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
// C1: Artifact availability — host-produced artifacts become available at their step's :post
|
|
44
|
+
// point. Build a map: artifact → earliest POINT_ORDER index at which it is available.
|
|
45
|
+
// (discuss produces CONTEXT.md → discuss:post = index 1;
|
|
46
|
+
// plan produces PLAN.md → plan:post = index 3;
|
|
47
|
+
// execute produces SUMMARY.md → execute:post = index 7;
|
|
48
|
+
// verify produces UAT.md → verify:post = index 9)
|
|
49
|
+
//
|
|
50
|
+
// NOTE: this map covers ONLY host artifacts. Hook-produced artifacts are handled per-run
|
|
51
|
+
// during consumes-satisfiability validation (C2 global pass).
|
|
52
|
+
const HOST_ARTIFACT_EARLIEST_POINT_IDX = (() => {
|
|
53
|
+
const m = Object.create(null);
|
|
54
|
+
for (const entry of LOOP_HOST_CONTRACT) {
|
|
55
|
+
// The :post point is the last point in each step's points array.
|
|
56
|
+
const postPoint = entry.points[entry.points.length - 1];
|
|
57
|
+
const postIdx = POINT_ORDER.indexOf(postPoint);
|
|
58
|
+
for (const artifact of entry.coreArtifacts.produces) {
|
|
59
|
+
// Only record the earliest (should be unique, but take min to be safe).
|
|
60
|
+
if (m[artifact] === undefined || postIdx < m[artifact]) {
|
|
61
|
+
m[artifact] = postIdx;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return m;
|
|
66
|
+
})();
|
|
67
|
+
|
|
68
|
+
// Flatten all valid loop points into a Set for O(1) validation
|
|
69
|
+
const VALID_LOOP_POINTS = new Set(POINT_ORDER);
|
|
70
|
+
|
|
71
|
+
// Map point → step contract (agentRoles + coreArtifacts)
|
|
72
|
+
const POINT_TO_CONTRACT = new Map();
|
|
73
|
+
for (const entry of LOOP_HOST_CONTRACT) {
|
|
74
|
+
for (const point of entry.points) {
|
|
75
|
+
POINT_TO_CONTRACT.set(point, entry);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ─── Config-slice validation ──────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
const VALID_CONFIG_SLICE_TYPES = new Set(['boolean', 'string', 'number', 'enum']);
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Validate a single config-slice entry (one key's { type, default, description }).
|
|
85
|
+
* Returns an array of error strings. Empty = valid.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} capId Capability id (for error messages)
|
|
88
|
+
* @param {string} key Config key (for error messages)
|
|
89
|
+
* @param {object} slice The slice object from cap.config[key]
|
|
90
|
+
* @returns {string[]}
|
|
91
|
+
*/
|
|
92
|
+
function validateConfigSliceEntry(capId, key, slice) {
|
|
93
|
+
const errors = [];
|
|
94
|
+
|
|
95
|
+
if (typeof slice !== 'object' || slice === null || Array.isArray(slice)) {
|
|
96
|
+
errors.push('capability "' + capId + '" config["' + key + '"]: slice must be a non-null object');
|
|
97
|
+
return errors;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// type must be one of the allowed set
|
|
101
|
+
if (!VALID_CONFIG_SLICE_TYPES.has(slice.type)) {
|
|
102
|
+
errors.push(
|
|
103
|
+
'capability "' + capId + '" config["' + key + '"]: type must be one of ' +
|
|
104
|
+
[...VALID_CONFIG_SLICE_TYPES].join(', ') + ' (got: ' + JSON.stringify(slice.type) + ')',
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// default must be present
|
|
109
|
+
if (!Object.prototype.hasOwnProperty.call(slice, 'default')) {
|
|
110
|
+
errors.push(
|
|
111
|
+
'capability "' + capId + '" config["' + key + '"]: default is required',
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
// type-consistency check
|
|
115
|
+
const def = slice.default;
|
|
116
|
+
if (slice.type === 'boolean') {
|
|
117
|
+
if (typeof def !== 'boolean') {
|
|
118
|
+
errors.push(
|
|
119
|
+
'capability "' + capId + '" config["' + key + '"]: default must be a boolean for type:"boolean" (got: ' + typeof def + ')',
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
} else if (slice.type === 'string') {
|
|
123
|
+
if (typeof def !== 'string') {
|
|
124
|
+
errors.push(
|
|
125
|
+
'capability "' + capId + '" config["' + key + '"]: default must be a string for type:"string" (got: ' + typeof def + ')',
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
} else if (slice.type === 'number') {
|
|
129
|
+
if (typeof def !== 'number') {
|
|
130
|
+
errors.push(
|
|
131
|
+
'capability "' + capId + '" config["' + key + '"]: default must be a number for type:"number" (got: ' + typeof def + ')',
|
|
132
|
+
);
|
|
133
|
+
} else if (!Number.isFinite(def)) {
|
|
134
|
+
// FIX 6a: Reject NaN and non-finite number defaults
|
|
135
|
+
errors.push(
|
|
136
|
+
'capability "' + capId + '" config["' + key + '"]: default for type:"number" must be a finite number (got: ' + String(def) + ')',
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
} else if (slice.type === 'enum') {
|
|
140
|
+
// FIX 5a: enum REQUIRES a non-empty values array (all strings), and default must be in it
|
|
141
|
+
if (!Array.isArray(slice.values) || slice.values.length === 0) {
|
|
142
|
+
errors.push(
|
|
143
|
+
'capability "' + capId + '" config["' + key + '"]: type:"enum" requires a non-empty "values" array of strings',
|
|
144
|
+
);
|
|
145
|
+
} else if (!slice.values.every((v) => typeof v === 'string')) {
|
|
146
|
+
errors.push(
|
|
147
|
+
'capability "' + capId + '" config["' + key + '"]: type:"enum" values array must contain only strings',
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (typeof def !== 'string') {
|
|
151
|
+
errors.push(
|
|
152
|
+
'capability "' + capId + '" config["' + key + '"]: default must be a string for type:"enum" (got: ' + typeof def + ')',
|
|
153
|
+
);
|
|
154
|
+
} else if (Array.isArray(slice.values) && slice.values.length > 0 && !slice.values.includes(def)) {
|
|
155
|
+
errors.push(
|
|
156
|
+
'capability "' + capId + '" config["' + key + '"]: default "' + def +
|
|
157
|
+
'" is not one of the declared enum values [' + slice.values.join(', ') + ']',
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// description must be a non-empty string
|
|
164
|
+
if (typeof slice.description !== 'string' || slice.description.length === 0) {
|
|
165
|
+
errors.push(
|
|
166
|
+
'capability "' + capId + '" config["' + key + '"]: description must be a non-empty string (got: ' + JSON.stringify(slice.description) + ')',
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return errors;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ─── Per-capability validation ────────────────────────────────────────────────
|
|
174
|
+
|
|
175
|
+
const KEBAB_RE = /^[a-z][a-z0-9-]*$/;
|
|
176
|
+
const VALID_ROLES = new Set(['feature', 'runtime']);
|
|
177
|
+
const VALID_TIERS = new Set(['core', 'standard', 'full']);
|
|
178
|
+
const VALID_ON_ERROR = new Set(['skip', 'halt']);
|
|
179
|
+
const RUNTIME_COMPAT_WILDCARD = '*';
|
|
180
|
+
|
|
181
|
+
// ── ADR-1244 D1: versioned-manifest envelope ─────────────────────────────────
|
|
182
|
+
// Official strict SemVer 2.0.0 grammar (https://semver.org). Rejects partials
|
|
183
|
+
// ("1.0"), prefixes ("v1.0.0"), leading-zero segments ("01.2.3"), numeric
|
|
184
|
+
// prerelease identifiers with leading zeros ("1.2.3-01"), empty identifiers
|
|
185
|
+
// ("1.2.3-..") and — critically — prerelease/build identifiers containing
|
|
186
|
+
// anything outside [0-9A-Za-z-] (so a version can never smuggle shell
|
|
187
|
+
// metacharacters, spaces or unicode into a downstream `git tag v<version>` or
|
|
188
|
+
// path). Accepts "1.2.3-dev.0", "1.2.3-rc.1", "1.2.3+build.5".
|
|
189
|
+
const SEMVER_RE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
190
|
+
// Permissive *shape* check for a semver range (engines.gsd / compatVersions
|
|
191
|
+
// values). Range SATISFACTION is enforced by the runtime overlay (ADR-1244 D2);
|
|
192
|
+
// here we only reject empty/garbage and shell metacharacters.
|
|
193
|
+
const SEMVER_RANGE_RE = /^[0-9A-Za-z.\-+ |<>=~^*()]+$/;
|
|
194
|
+
// Subresource-integrity hash: "sha512-" + base64 of a 64-byte digest (86 base64
|
|
195
|
+
// chars + "==" padding). Exact length so malformed pins ("sha512-abc") fail.
|
|
196
|
+
const SHA512_INTEGRITY_RE = /^sha512-[A-Za-z0-9+/]{86}==$/;
|
|
197
|
+
|
|
198
|
+
// #1460 (R) HIGH — shell-safe hook-script allowlist. A hook `script` is resolved to an
|
|
199
|
+
// ABSOLUTE path and written verbatim as the hook `command` STRING in settings.json, which a
|
|
200
|
+
// host runtime consumes through a shell (first-party hooks emit `node "${...}/hooks/x.js"`).
|
|
201
|
+
// A manifest-controlled name like `run.sh; touch /tmp/pwn` (filenames may legally contain
|
|
202
|
+
// `;`, spaces, `$`, backtick, `|`, newline on POSIX) would inject a second command — even
|
|
203
|
+
// though the file genuinely exists inside the bundle and so passes path-confinement. We
|
|
204
|
+
// therefore restrict the relative script path to a CONSERVATIVE allowlist: only
|
|
205
|
+
// [A-Za-z0-9._/-], with no leading `-` on any segment (option-injection), no `..` segment,
|
|
206
|
+
// and not absolute. Anything else (whitespace, any shell metacharacter, control/NUL) is a
|
|
207
|
+
// hard validation error — fail closed so the capability install/load is rejected loudly.
|
|
208
|
+
const SAFE_HOOK_SCRIPT_RE = /^[A-Za-z0-9._/-]+$/;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* #1460 (R): true when a relative hook-script path is shell-safe (see SAFE_HOOK_SCRIPT_RE).
|
|
212
|
+
* Rejects absolute paths, `..` segments, a leading `-` on any path segment, and any char
|
|
213
|
+
* outside the allowlist (whitespace / shell metacharacters / control / NUL).
|
|
214
|
+
*/
|
|
215
|
+
function isSafeHookScriptPath(script) {
|
|
216
|
+
if (typeof script !== 'string' || script.length === 0) return false;
|
|
217
|
+
if (!SAFE_HOOK_SCRIPT_RE.test(script)) return false;
|
|
218
|
+
if (path.isAbsolute(script)) return false;
|
|
219
|
+
const segments = script.split(/[/\\]/);
|
|
220
|
+
if (segments.includes('..')) return false;
|
|
221
|
+
// A leading '-' on any segment would be parsed as an option by the shell/`node`.
|
|
222
|
+
for (const seg of segments) {
|
|
223
|
+
if (seg.startsWith('-')) return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A syntactically plausible semver range (shape-only — see SEMVER_RANGE_RE).
|
|
229
|
+
// Requires a digit or a bare wildcard so pure-alpha garbage ("abcx", "()x") is
|
|
230
|
+
// rejected; full range satisfaction is the runtime overlay's job (ADR-1244 D2).
|
|
231
|
+
function isPlausibleRange(s) {
|
|
232
|
+
if (typeof s !== 'string') return false;
|
|
233
|
+
const t = s.trim();
|
|
234
|
+
if (t.length === 0 || !SEMVER_RANGE_RE.test(s)) return false;
|
|
235
|
+
return /\d/.test(t) || t === '*' || t === 'x' || t === 'X';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* ADR-1244 D1: validate the versioned-manifest envelope.
|
|
240
|
+
* - version REQUIRED semver string (the registry rejects a manifest
|
|
241
|
+
* without one).
|
|
242
|
+
* - engines optional object; engines.gsd optional semver-range string.
|
|
243
|
+
* - compatVersions optional object mapping a capability version (semver) to a
|
|
244
|
+
* gsd version range.
|
|
245
|
+
* - integrity optional "sha512-<base64>" string.
|
|
246
|
+
* - provenance optional { sourceRepo, commit } strings.
|
|
247
|
+
*
|
|
248
|
+
* Shape only — range satisfaction and integrity verification are enforced by
|
|
249
|
+
* the source resolver / runtime overlay (ADR-1244 D2/D3).
|
|
250
|
+
*
|
|
251
|
+
* @param {object} cap The parsed JSON object.
|
|
252
|
+
* @returns {string[]} Array of error strings; empty = valid.
|
|
253
|
+
*/
|
|
254
|
+
function validateVersionEnvelope(cap) {
|
|
255
|
+
const errors = [];
|
|
256
|
+
|
|
257
|
+
if (typeof cap.version !== 'string' || !SEMVER_RE.test(cap.version)) {
|
|
258
|
+
errors.push('version must be a semver string (e.g. "1.2.3"); got: ' + JSON.stringify(cap.version));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (cap.engines !== undefined) {
|
|
262
|
+
if (typeof cap.engines !== 'object' || cap.engines === null || Array.isArray(cap.engines)) {
|
|
263
|
+
errors.push('engines must be an object (e.g. { "gsd": ">=1.6.0" })');
|
|
264
|
+
} else if (cap.engines.gsd !== undefined && !isPlausibleRange(cap.engines.gsd)) {
|
|
265
|
+
errors.push('engines.gsd must be a semver range string; got: ' + JSON.stringify(cap.engines.gsd));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (cap.compatVersions !== undefined) {
|
|
270
|
+
if (typeof cap.compatVersions !== 'object' || cap.compatVersions === null || Array.isArray(cap.compatVersions)) {
|
|
271
|
+
errors.push('compatVersions must be an object mapping capability versions to gsd version ranges');
|
|
272
|
+
} else {
|
|
273
|
+
for (const [k, v] of Object.entries(cap.compatVersions)) {
|
|
274
|
+
if (!SEMVER_RE.test(k)) errors.push('compatVersions key "' + k + '" must be a semver string');
|
|
275
|
+
if (!isPlausibleRange(v)) errors.push('compatVersions["' + k + '"] must be a semver range string');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (cap.integrity !== undefined && (typeof cap.integrity !== 'string' || !SHA512_INTEGRITY_RE.test(cap.integrity))) {
|
|
281
|
+
errors.push('integrity must be a "sha512-<base64>" string');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (cap.provenance !== undefined) {
|
|
285
|
+
const p = cap.provenance;
|
|
286
|
+
if (typeof p !== 'object' || p === null || Array.isArray(p)) {
|
|
287
|
+
errors.push('provenance must be an object { sourceRepo, commit }');
|
|
288
|
+
} else {
|
|
289
|
+
if (typeof p.sourceRepo !== 'string' || p.sourceRepo.length === 0) {
|
|
290
|
+
errors.push('provenance.sourceRepo must be a non-empty string');
|
|
291
|
+
}
|
|
292
|
+
if (typeof p.commit !== 'string' || p.commit.length === 0) {
|
|
293
|
+
errors.push('provenance.commit must be a non-empty string');
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return errors;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Validate a single capability declaration.
|
|
303
|
+
*
|
|
304
|
+
* @param {object} cap The parsed JSON object.
|
|
305
|
+
* @param {string} folderId The folder name (must equal cap.id).
|
|
306
|
+
* @returns {string[]} Array of error strings; empty = valid.
|
|
307
|
+
*/
|
|
308
|
+
function validateCapability(cap, folderId) {
|
|
309
|
+
const errors = [];
|
|
310
|
+
|
|
311
|
+
if (typeof cap !== 'object' || cap === null || Array.isArray(cap)) {
|
|
312
|
+
return ['capability must be a JSON object'];
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ── Common envelope ────────────────────────────────────────────────────────
|
|
316
|
+
|
|
317
|
+
if (typeof cap.id !== 'string' || !KEBAB_RE.test(cap.id)) {
|
|
318
|
+
errors.push('id must be a kebab-case string');
|
|
319
|
+
} else if (cap.id !== folderId) {
|
|
320
|
+
errors.push('id "' + cap.id + '" must equal the folder name "' + folderId + '"');
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (!VALID_ROLES.has(cap.role)) {
|
|
324
|
+
errors.push('role must be one of: feature, runtime (got: ' + cap.role + ')');
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (typeof cap.title !== 'string' || cap.title.length === 0) {
|
|
328
|
+
errors.push('title must be a non-empty string');
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// C4: description is required
|
|
332
|
+
if (typeof cap.description !== 'string' || cap.description.length === 0) {
|
|
333
|
+
errors.push('description must be a non-empty string');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!VALID_TIERS.has(cap.tier)) {
|
|
337
|
+
errors.push('tier must be one of: core, standard, full (got: ' + cap.tier + ')');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (!Array.isArray(cap.requires)) {
|
|
341
|
+
errors.push('requires must be an array of capability ids');
|
|
342
|
+
} else {
|
|
343
|
+
for (const req of cap.requires) {
|
|
344
|
+
if (typeof req !== 'string') {
|
|
345
|
+
errors.push('requires entries must be strings (got: ' + JSON.stringify(req) + ')');
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ── Versioned-manifest envelope (ADR-1244 D1) ──────────────────────────────
|
|
351
|
+
errors.push(...validateVersionEnvelope(cap));
|
|
352
|
+
|
|
353
|
+
// ── Role-specific body ────────────────────────────────────────────────────
|
|
354
|
+
|
|
355
|
+
if (cap.role === 'feature') {
|
|
356
|
+
errors.push(...validateFeatureBody(cap));
|
|
357
|
+
} else if (cap.role === 'runtime') {
|
|
358
|
+
errors.push(...validateRuntimeBody(cap));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return errors;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* ADR-959: Validate a single commands[] entry on a feature-role capability.
|
|
366
|
+
* { family: string, module: string, router: string, subcommands?: string[] }
|
|
367
|
+
*
|
|
368
|
+
* - family: non-empty string, no reserved names
|
|
369
|
+
* - module: non-empty string, no path traversal, no absolute paths, no "/"
|
|
370
|
+
* segments other than a bare basename (expected form: "foo.cjs")
|
|
371
|
+
* - router: non-empty string
|
|
372
|
+
* - subcommands: optional array of strings (doc/introspection only)
|
|
373
|
+
*
|
|
374
|
+
* @param {string} capId Capability id (for error messages)
|
|
375
|
+
* @param {*} entry The entry to validate
|
|
376
|
+
* @param {string} prefix Path prefix (e.g. "commands[0]")
|
|
377
|
+
* @returns {string[]} Array of error strings; empty = valid.
|
|
378
|
+
*/
|
|
379
|
+
function validateCommandEntry(capId, entry, prefix) {
|
|
380
|
+
const errors = [];
|
|
381
|
+
const ctx = 'capability "' + capId + '" ' + prefix;
|
|
382
|
+
|
|
383
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
|
|
384
|
+
errors.push(ctx + ' must be an object with family, module, and router');
|
|
385
|
+
return errors;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// family: non-empty string, no reserved names
|
|
389
|
+
if (typeof entry.family !== 'string' || entry.family.length === 0) {
|
|
390
|
+
errors.push(ctx + '.family must be a non-empty string');
|
|
391
|
+
} else if (entry.family === '__proto__' || entry.family === 'constructor' || entry.family === 'prototype') {
|
|
392
|
+
// S2a: inline literal reserved-name guard (CodeQL barrier)
|
|
393
|
+
errors.push(ctx + '.family "' + entry.family + '" is a reserved name');
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// module: must be a safe bare basename matching /^[A-Za-z0-9._-]+\.cjs$/ —
|
|
397
|
+
// no path separators, no "..", no NUL bytes, no absolute paths, ends in .cjs.
|
|
398
|
+
// This conservative pattern subsumes all earlier traversal/absolute/separator checks.
|
|
399
|
+
if (typeof entry.module !== 'string' || entry.module.length === 0) {
|
|
400
|
+
errors.push(ctx + '.module must be a non-empty string');
|
|
401
|
+
} else {
|
|
402
|
+
const mod = entry.module;
|
|
403
|
+
const SAFE_BASENAME = /^[A-Za-z0-9._-]+\.cjs$/;
|
|
404
|
+
if (!SAFE_BASENAME.test(mod)) {
|
|
405
|
+
errors.push(
|
|
406
|
+
ctx + '.module must be a safe bare basename (pattern: /^[A-Za-z0-9._-]+\\.cjs$/, no path separators, no "..", no NUL bytes, must end in ".cjs"); got: ' +
|
|
407
|
+
JSON.stringify(mod),
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// router: non-empty string
|
|
413
|
+
if (typeof entry.router !== 'string' || entry.router.length === 0) {
|
|
414
|
+
errors.push(ctx + '.router must be a non-empty string');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// subcommands: optional array of non-empty strings (doc/introspection only)
|
|
418
|
+
if (entry.subcommands !== undefined) {
|
|
419
|
+
if (!Array.isArray(entry.subcommands)) {
|
|
420
|
+
errors.push(ctx + '.subcommands must be an array of strings if present');
|
|
421
|
+
} else {
|
|
422
|
+
for (let i = 0; i < entry.subcommands.length; i++) {
|
|
423
|
+
if (typeof entry.subcommands[i] !== 'string') {
|
|
424
|
+
errors.push(ctx + '.subcommands[' + i + '] must be a string');
|
|
425
|
+
} else if (entry.subcommands[i].length === 0) {
|
|
426
|
+
errors.push(ctx + '.subcommands[' + i + '] must be a non-empty string');
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return errors;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function validateRuntimeCompat(capId, runtimeCompat) {
|
|
436
|
+
const errors = [];
|
|
437
|
+
const ctx = 'capability "' + capId + '" runtimeCompat';
|
|
438
|
+
|
|
439
|
+
if (typeof runtimeCompat !== 'object' || runtimeCompat === null || Array.isArray(runtimeCompat)) {
|
|
440
|
+
errors.push(ctx + ' must be an object with supported and unsupported arrays');
|
|
441
|
+
return errors;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const validateRuntimeArray = (field, { allowWildcard }) => {
|
|
445
|
+
const value = runtimeCompat[field];
|
|
446
|
+
if (!Array.isArray(value)) {
|
|
447
|
+
errors.push(ctx + '.' + field + ' must be an array of runtime ids' + (allowWildcard ? ' or ["*"]' : ''));
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (field === 'supported' && value.length === 0) {
|
|
451
|
+
errors.push(ctx + '.supported must be a non-empty array');
|
|
452
|
+
}
|
|
453
|
+
let hasWildcard = false;
|
|
454
|
+
for (let i = 0; i < value.length; i++) {
|
|
455
|
+
const entry = value[i];
|
|
456
|
+
if (typeof entry !== 'string' || entry.length === 0) {
|
|
457
|
+
errors.push(ctx + '.' + field + '[' + i + '] must be a non-empty string');
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (entry === '__proto__' || entry === 'constructor' || entry === 'prototype') {
|
|
461
|
+
errors.push(ctx + '.' + field + '[' + i + '] "' + entry + '" is a reserved name');
|
|
462
|
+
}
|
|
463
|
+
if (entry === RUNTIME_COMPAT_WILDCARD) {
|
|
464
|
+
if (!allowWildcard) {
|
|
465
|
+
errors.push(ctx + '.' + field + ' must not include wildcard "*"');
|
|
466
|
+
}
|
|
467
|
+
hasWildcard = true;
|
|
468
|
+
} else if (!KEBAB_RE.test(entry)) {
|
|
469
|
+
errors.push(ctx + '.' + field + '[' + i + '] must be a kebab-case runtime id or "*"');
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (hasWildcard && value.length > 1) {
|
|
473
|
+
errors.push(ctx + '.' + field + ' wildcard "*" cannot be mixed with runtime ids');
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
validateRuntimeArray('supported', { allowWildcard: true });
|
|
478
|
+
validateRuntimeArray('unsupported', { allowWildcard: false });
|
|
479
|
+
|
|
480
|
+
if (runtimeCompat.notes !== undefined) {
|
|
481
|
+
if (typeof runtimeCompat.notes !== 'object' || runtimeCompat.notes === null || Array.isArray(runtimeCompat.notes)) {
|
|
482
|
+
errors.push(ctx + '.notes must be an object of runtime id to string if present');
|
|
483
|
+
} else {
|
|
484
|
+
for (const [key, value] of Object.entries(runtimeCompat.notes)) {
|
|
485
|
+
if (key !== RUNTIME_COMPAT_WILDCARD && !KEBAB_RE.test(key)) {
|
|
486
|
+
errors.push(ctx + '.notes key "' + key + '" must be a kebab-case runtime id or "*"');
|
|
487
|
+
}
|
|
488
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
489
|
+
errors.push(ctx + '.notes["' + key + '"] must be a non-empty string');
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return errors;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function validateFeatureBody(cap) {
|
|
499
|
+
const errors = [];
|
|
500
|
+
|
|
501
|
+
errors.push(...validateRuntimeCompat(cap.id || '(unknown)', cap.runtimeCompat));
|
|
502
|
+
|
|
503
|
+
if (!Array.isArray(cap.skills)) {
|
|
504
|
+
errors.push('skills must be an array of strings');
|
|
505
|
+
} else {
|
|
506
|
+
for (const s of cap.skills) {
|
|
507
|
+
if (typeof s !== 'string') {
|
|
508
|
+
errors.push('skills entries must be strings');
|
|
509
|
+
} else if (s === '__proto__' || s === 'constructor' || s === 'prototype') {
|
|
510
|
+
// S2a: inline literal reserved-name guard (CodeQL barrier)
|
|
511
|
+
errors.push('skills entry "' + s + '" is a reserved name');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ADR-959: optional commands array
|
|
517
|
+
if (cap.commands !== undefined) {
|
|
518
|
+
if (!Array.isArray(cap.commands)) {
|
|
519
|
+
errors.push('commands must be an array of {family, module, router} objects');
|
|
520
|
+
} else {
|
|
521
|
+
for (let i = 0; i < cap.commands.length; i++) {
|
|
522
|
+
errors.push(...validateCommandEntry(cap.id || cap.role, cap.commands[i], 'commands[' + i + ']'));
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (!Array.isArray(cap.agents)) {
|
|
528
|
+
errors.push('agents must be an array of strings');
|
|
529
|
+
} else {
|
|
530
|
+
for (const a of cap.agents) {
|
|
531
|
+
if (typeof a !== 'string') {
|
|
532
|
+
errors.push('agents entries must be strings');
|
|
533
|
+
} else if (a === '__proto__' || a === 'constructor' || a === 'prototype') {
|
|
534
|
+
// S2a: inline literal reserved-name guard (CodeQL barrier)
|
|
535
|
+
errors.push('agents entry "' + a + '" is a reserved name');
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (typeof cap.config !== 'object' || cap.config === null || Array.isArray(cap.config)) {
|
|
541
|
+
errors.push('config must be an object');
|
|
542
|
+
} else {
|
|
543
|
+
// C5: validate config key names and value shapes
|
|
544
|
+
for (const key of Object.keys(cap.config)) {
|
|
545
|
+
if (key === '' ) {
|
|
546
|
+
errors.push('config keys must be non-empty strings');
|
|
547
|
+
} else if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
548
|
+
// S2a: inline literal reserved-name guard (CodeQL barrier)
|
|
549
|
+
errors.push('config key "' + key + '" is a reserved name');
|
|
550
|
+
}
|
|
551
|
+
const val = cap.config[key];
|
|
552
|
+
if (val === null || typeof val !== 'object' || Array.isArray(val)) {
|
|
553
|
+
errors.push('config["' + key + '"] must be an object (got: ' + (val === null ? 'null' : typeof val) + ')');
|
|
554
|
+
} else if (typeof val.type !== 'string' || val.type.length === 0) {
|
|
555
|
+
errors.push('config["' + key + '"] must have a string "type" field (e.g. "boolean", "string", "number", "enum")');
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// C4: hooks, when present, must be an array of {event: string, script: string}
|
|
561
|
+
if (cap.hooks !== undefined) {
|
|
562
|
+
if (!Array.isArray(cap.hooks)) {
|
|
563
|
+
errors.push('hooks must be an array of {event, script} objects');
|
|
564
|
+
} else {
|
|
565
|
+
for (let i = 0; i < cap.hooks.length; i++) {
|
|
566
|
+
const h = cap.hooks[i];
|
|
567
|
+
if (typeof h !== 'object' || h === null || Array.isArray(h)) {
|
|
568
|
+
errors.push('hooks[' + i + '] must be an object with event and script keys');
|
|
569
|
+
} else {
|
|
570
|
+
if (typeof h.event !== 'string' || h.event.length === 0) {
|
|
571
|
+
errors.push('hooks[' + i + '].event must be a non-empty string');
|
|
572
|
+
}
|
|
573
|
+
if (typeof h.script !== 'string' || h.script.length === 0) {
|
|
574
|
+
errors.push('hooks[' + i + '].script must be a non-empty string');
|
|
575
|
+
} else if (!isSafeHookScriptPath(h.script)) {
|
|
576
|
+
// #1460 (R) HIGH: the script becomes an absolute hook `command` consumed by a shell;
|
|
577
|
+
// reject any unsafe character (shell metacharacters/whitespace/control), a leading `-`,
|
|
578
|
+
// an absolute path, or a `..` segment so a manifest can never inject a second command.
|
|
579
|
+
errors.push(
|
|
580
|
+
'hooks[' + i + '].script must be a relative path containing only [A-Za-z0-9._/-] ' +
|
|
581
|
+
'(no whitespace, shell metacharacters (e.g. ; | & $ ` ( ) < > * ? newline), leading "-", ' +
|
|
582
|
+
'absolute path, or ".." segment) — it contains unsafe characters: ' + JSON.stringify(h.script),
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Build the declared skill/agent sets for ref membership checks (used in validateStep).
|
|
591
|
+
// Only build these if the arrays are valid (already validated above).
|
|
592
|
+
const declaredSkills = Array.isArray(cap.skills) ? new Set(cap.skills.filter((s) => typeof s === 'string')) : null;
|
|
593
|
+
const declaredAgents = Array.isArray(cap.agents) ? new Set(cap.agents.filter((a) => typeof a === 'string')) : null;
|
|
594
|
+
|
|
595
|
+
if (!Array.isArray(cap.steps)) {
|
|
596
|
+
errors.push('steps must be an array');
|
|
597
|
+
} else {
|
|
598
|
+
for (let i = 0; i < cap.steps.length; i++) {
|
|
599
|
+
errors.push(...validateStep(cap.steps[i], 'steps[' + i + ']', declaredSkills, declaredAgents));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (!Array.isArray(cap.contributions)) {
|
|
604
|
+
errors.push('contributions must be an array');
|
|
605
|
+
} else {
|
|
606
|
+
for (let i = 0; i < cap.contributions.length; i++) {
|
|
607
|
+
errors.push(...validateContribution(cap.contributions[i], 'contributions[' + i + ']'));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (!Array.isArray(cap.gates)) {
|
|
612
|
+
errors.push('gates must be an array');
|
|
613
|
+
} else {
|
|
614
|
+
for (let i = 0; i < cap.gates.length; i++) {
|
|
615
|
+
errors.push(...validateGate(cap.gates[i], 'gates[' + i + ']'));
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// activationKey: optional string naming the dotted config key that gates this capability.
|
|
620
|
+
// If present: must be a non-empty string that is declared in this capability's own config slice.
|
|
621
|
+
if (cap.activationKey !== undefined) {
|
|
622
|
+
if (typeof cap.activationKey !== 'string' || cap.activationKey.length === 0) {
|
|
623
|
+
errors.push(
|
|
624
|
+
'capability "' + (cap.id || '(unknown)') + '" activationKey must be a non-empty string (got: ' +
|
|
625
|
+
JSON.stringify(cap.activationKey) + ')',
|
|
626
|
+
);
|
|
627
|
+
} else if (cap.activationKey === '__proto__' || cap.activationKey === 'constructor' || cap.activationKey === 'prototype') {
|
|
628
|
+
// Prototype-pollution guard (inline literal, CodeQL barrier)
|
|
629
|
+
errors.push(
|
|
630
|
+
'capability "' + (cap.id || '(unknown)') + '" activationKey "' + cap.activationKey +
|
|
631
|
+
'" is a reserved JavaScript property name and cannot be used as an activationKey',
|
|
632
|
+
);
|
|
633
|
+
} else if (
|
|
634
|
+
typeof cap.config !== 'object' ||
|
|
635
|
+
cap.config === null ||
|
|
636
|
+
!Object.prototype.hasOwnProperty.call(cap.config, cap.activationKey)
|
|
637
|
+
) {
|
|
638
|
+
errors.push(
|
|
639
|
+
'capability "' + (cap.id || '(unknown)') + '" activationKey "' + cap.activationKey +
|
|
640
|
+
'" is not declared in this capability\'s config slice — add it to the "config" object or use a key that is declared there',
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
return errors;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// ADR-857 phase 5e: Closed ConverterName enum — complete set used across 16 runtime descriptors,
|
|
649
|
+
// all exported by bin/install.js (commands/skills) and src/runtime-artifact-conversion.cts (agents).
|
|
650
|
+
// Any ArtifactKind with a non-null converter must use one of these.
|
|
651
|
+
const VALID_CONVERTER_NAMES = new Set([
|
|
652
|
+
// commands / skills converters (pre-existing)
|
|
653
|
+
'convertClaudeCommandToAntigravitySkill',
|
|
654
|
+
'convertClaudeCommandToAugmentSkill',
|
|
655
|
+
'convertClaudeCommandToClineSkill',
|
|
656
|
+
'convertClaudeCommandToClaudeSkill',
|
|
657
|
+
'convertClaudeCommandToCodebuddyCommand',
|
|
658
|
+
'convertClaudeCommandToCodebuddySkill',
|
|
659
|
+
'convertClaudeCommandToCodexSkill',
|
|
660
|
+
'convertClaudeCommandToCopilotSkill',
|
|
661
|
+
'convertClaudeCommandToCursorCommand',
|
|
662
|
+
'convertClaudeCommandToCursorSkill',
|
|
663
|
+
'convertClaudeCommandToKiloSkill',
|
|
664
|
+
'convertClaudeCommandToKimiSkill',
|
|
665
|
+
'convertClaudeCommandToOpencodeSkill',
|
|
666
|
+
'convertClaudeCommandToTraeSkill',
|
|
667
|
+
'convertClaudeCommandToWindsurfSkill',
|
|
668
|
+
// agent converters (#1173 — descriptor-driven agent conversion wiring)
|
|
669
|
+
'convertClaudeAgentToCopilotAgent',
|
|
670
|
+
'convertClaudeAgentToAntigravityAgent',
|
|
671
|
+
'convertClaudeAgentToCursorAgent',
|
|
672
|
+
'convertClaudeAgentToWindsurfAgent',
|
|
673
|
+
'convertClaudeAgentToAugmentAgent',
|
|
674
|
+
'convertClaudeAgentToTraeAgent',
|
|
675
|
+
'convertClaudeAgentToCodebuddyAgent',
|
|
676
|
+
'convertClaudeAgentToClineAgent',
|
|
677
|
+
'convertClaudeAgentToCodexAgent',
|
|
678
|
+
]);
|
|
679
|
+
|
|
680
|
+
// C3: Validate role:runtime body
|
|
681
|
+
const VALID_CONFIG_FORMATS = new Set(['settings-json', 'toml', 'markdown', 'markdown-dir', 'none']);
|
|
682
|
+
const VALID_CONFIG_HOME_KINDS = new Set(['dot-home', 'dot-home-nested', 'xdg', 'generic-agents-root']);
|
|
683
|
+
const VALID_COMMAND_STYLES = new Set(['slash-hyphen', 'shell-var']);
|
|
684
|
+
const VALID_HOOKS_SURFACES = new Set(['settings-json', 'codex-hooks-json', 'cursor-hooks-json', 'copilot-inline', 'cline-rules', 'none']);
|
|
685
|
+
const VALID_HOOK_EVENTS = new Set(['claude', 'gemini', 'opencode-subset']);
|
|
686
|
+
const VALID_SANDBOX_TIERS = new Set(['none', 'codex-agent-sandbox']);
|
|
687
|
+
const VALID_ARTIFACT_KIND_NAMES = new Set(['commands', 'agents', 'skills', 'kimi-agents']);
|
|
688
|
+
const VALID_ARTIFACT_NESTINGS = new Set(['flat', 'nested']);
|
|
689
|
+
const FEATURE_FIELDS_FORBIDDEN_ON_RUNTIME = ['skills', 'agents', 'steps', 'contributions', 'gates', 'hooks', 'activationKey'];
|
|
690
|
+
const VALID_INSTALL_SURFACES = new Set(['settings-json', 'codex-toml', 'copilot-instructions', 'cline-rules', 'cursor-hooks-json', 'profile-marker-only']);
|
|
691
|
+
const VALID_PERMISSION_WRITERS = new Set(['opencode', 'kilo']);
|
|
692
|
+
const VALID_EXTENDED_HOOK_EVENTS = new Set(['SubagentStop', 'Stop', 'PreCompact', 'FileChanged', 'BeforeAgent', 'AfterAgent', 'BeforeModel']);
|
|
693
|
+
|
|
694
|
+
// GATE A: installSurface → allowed hooksSurface values (DEFECT.GENERATIVE-FIX: parity invariant)
|
|
695
|
+
// Derived from the actual pairings in the 16 real runtime descriptors.
|
|
696
|
+
const INSTALL_SURFACE_TO_ALLOWED_HOOKS_SURFACES = new Map([
|
|
697
|
+
['settings-json', new Set(['settings-json', 'none'])],
|
|
698
|
+
['codex-toml', new Set(['codex-hooks-json'])],
|
|
699
|
+
['copilot-instructions', new Set(['copilot-inline'])],
|
|
700
|
+
['cline-rules', new Set(['cline-rules'])],
|
|
701
|
+
['cursor-hooks-json', new Set(['cursor-hooks-json'])],
|
|
702
|
+
['profile-marker-only', new Set(['none'])],
|
|
703
|
+
]);
|
|
704
|
+
|
|
705
|
+
// GATE B: extended hook event families → required hookEvents value
|
|
706
|
+
// Gemini agent-events require hookEvents='gemini'; Claude-family events require hookEvents='claude'.
|
|
707
|
+
const GEMINI_AGENT_EVENTS = new Set(['BeforeAgent', 'AfterAgent', 'BeforeModel']);
|
|
708
|
+
const CLAUDE_FAMILY_EVENTS = new Set(['SubagentStop', 'Stop', 'PreCompact', 'FileChanged']);
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Validate a runtime.configHome object per ADR-1016 Decision 1.
|
|
712
|
+
* Returns an array of error strings.
|
|
713
|
+
*
|
|
714
|
+
* @param {string} capId Capability id (for error messages)
|
|
715
|
+
* @param {*} ch The configHome value
|
|
716
|
+
* @returns {string[]}
|
|
717
|
+
*/
|
|
718
|
+
function validateConfigHome(capId, ch) {
|
|
719
|
+
const errors = [];
|
|
720
|
+
const ctx = 'capability "' + capId + '" runtime.configHome';
|
|
721
|
+
|
|
722
|
+
if (typeof ch !== 'object' || ch === null || Array.isArray(ch)) {
|
|
723
|
+
errors.push(ctx + ' must be an object (got: ' + (ch === null ? 'null' : typeof ch) + ')');
|
|
724
|
+
return errors;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// kind — must be in closed vocab; inline literal guard (CodeQL barrier)
|
|
728
|
+
if (ch.kind === '__proto__' || ch.kind === 'constructor' || ch.kind === 'prototype') {
|
|
729
|
+
errors.push(ctx + '.kind "' + ch.kind + '" is a reserved name');
|
|
730
|
+
} else if (!VALID_CONFIG_HOME_KINDS.has(ch.kind)) {
|
|
731
|
+
errors.push(
|
|
732
|
+
ctx + '.kind must be one of: ' + [...VALID_CONFIG_HOME_KINDS].join(', ') +
|
|
733
|
+
' (got: ' + JSON.stringify(ch.kind) + ')',
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// name — required string
|
|
738
|
+
if (typeof ch.name !== 'string' || ch.name.length === 0) {
|
|
739
|
+
errors.push(ctx + '.name must be a non-empty string');
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// parent — required when kind == dot-home-nested
|
|
743
|
+
if (ch.kind === 'dot-home-nested') {
|
|
744
|
+
if (typeof ch.parent !== 'string' || ch.parent.length === 0) {
|
|
745
|
+
errors.push(ctx + '.parent must be a non-empty string when kind is "dot-home-nested"');
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// env — required; must be an array of strings (every runtime has ≥0 env overrides)
|
|
750
|
+
if (!Array.isArray(ch.env)) {
|
|
751
|
+
errors.push(ctx + '.env is required and must be an array of strings (got: ' + JSON.stringify(ch.env) + ')');
|
|
752
|
+
} else {
|
|
753
|
+
for (let i = 0; i < ch.env.length; i++) {
|
|
754
|
+
if (typeof ch.env[i] !== 'string') {
|
|
755
|
+
errors.push(ctx + '.env[' + i + '] must be a string');
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// probe — optional; if present must be an array of strings
|
|
761
|
+
if (ch.probe !== undefined) {
|
|
762
|
+
if (!Array.isArray(ch.probe)) {
|
|
763
|
+
errors.push(ctx + '.probe must be an array of strings if present');
|
|
764
|
+
} else {
|
|
765
|
+
for (let i = 0; i < ch.probe.length; i++) {
|
|
766
|
+
if (typeof ch.probe[i] !== 'string') {
|
|
767
|
+
errors.push(ctx + '.probe[' + i + '] must be a string');
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// probeExists — optional; if present must be a non-empty string (sub-path existence check for probe)
|
|
774
|
+
if (ch.probeExists !== undefined) {
|
|
775
|
+
if (typeof ch.probeExists !== 'string' || ch.probeExists.length === 0) {
|
|
776
|
+
errors.push(ctx + '.probeExists must be a non-empty string if present (got: ' + JSON.stringify(ch.probeExists) + ')');
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// skillsHome — optional; if present must be a full valid configHome object (recursive validation)
|
|
781
|
+
if (ch.skillsHome !== undefined) {
|
|
782
|
+
// Recursive call: validate skillsHome as a nested configHome.
|
|
783
|
+
// Use a synthetic capId to surface the sub-path in error messages.
|
|
784
|
+
const skillsHomeErrors = validateConfigHome(capId + '.skillsHome', ch.skillsHome);
|
|
785
|
+
// Rewrite the inner ctx prefix so errors read as "...runtime.configHome.skillsHome..."
|
|
786
|
+
for (const e of skillsHomeErrors) {
|
|
787
|
+
errors.push(e.replace(
|
|
788
|
+
'capability "' + capId + '.skillsHome" runtime.configHome',
|
|
789
|
+
ctx + '.skillsHome',
|
|
790
|
+
));
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
return errors;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Validate a single ArtifactKind entry per ADR-1016 Decision 3.
|
|
799
|
+
* Returns an array of error strings.
|
|
800
|
+
*
|
|
801
|
+
* @param {string} capId Capability id (for error messages)
|
|
802
|
+
* @param {*} entry The ArtifactKind object
|
|
803
|
+
* @param {string} prefix Path prefix for error messages (e.g. "artifactLayout.global[0]")
|
|
804
|
+
* @returns {string[]}
|
|
805
|
+
*/
|
|
806
|
+
function validateArtifactKindEntry(capId, entry, prefix) {
|
|
807
|
+
const errors = [];
|
|
808
|
+
const ctx = 'capability "' + capId + '" runtime.' + prefix;
|
|
809
|
+
|
|
810
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
|
|
811
|
+
errors.push(ctx + ' must be an object');
|
|
812
|
+
return errors;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// kind — must be in closed vocab; inline literal guard (CodeQL barrier)
|
|
816
|
+
if (entry.kind === '__proto__' || entry.kind === 'constructor' || entry.kind === 'prototype') {
|
|
817
|
+
errors.push(ctx + '.kind "' + entry.kind + '" is a reserved name');
|
|
818
|
+
} else if (!VALID_ARTIFACT_KIND_NAMES.has(entry.kind)) {
|
|
819
|
+
errors.push(
|
|
820
|
+
ctx + '.kind must be one of: ' + [...VALID_ARTIFACT_KIND_NAMES].join(', ') +
|
|
821
|
+
' (got: ' + JSON.stringify(entry.kind) + ')',
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// destSubpath — required non-empty string
|
|
826
|
+
if (typeof entry.destSubpath !== 'string' || entry.destSubpath.length === 0) {
|
|
827
|
+
errors.push(ctx + '.destSubpath must be a non-empty string');
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// nesting — required; must be in closed vocab (ADR-857 §5d: now drives install)
|
|
831
|
+
if (entry.nesting === undefined || entry.nesting === null) {
|
|
832
|
+
errors.push(ctx + '.nesting is required and must be one of: ' + [...VALID_ARTIFACT_NESTINGS].join(', '));
|
|
833
|
+
} else if (!VALID_ARTIFACT_NESTINGS.has(entry.nesting)) {
|
|
834
|
+
errors.push(
|
|
835
|
+
ctx + '.nesting must be one of: ' + [...VALID_ARTIFACT_NESTINGS].join(', ') +
|
|
836
|
+
' (got: ' + JSON.stringify(entry.nesting) + ')',
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// prefix — required; must be a string (may be empty string '')
|
|
841
|
+
if (entry.prefix === undefined || entry.prefix === null) {
|
|
842
|
+
errors.push(ctx + '.prefix is required (must be a string, may be empty)');
|
|
843
|
+
} else if (typeof entry.prefix !== 'string') {
|
|
844
|
+
errors.push(ctx + '.prefix must be a string (got: ' + typeof entry.prefix + ')');
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// recursive — optional; if present must be a boolean
|
|
848
|
+
if (entry.recursive !== undefined) {
|
|
849
|
+
if (typeof entry.recursive !== 'boolean') {
|
|
850
|
+
errors.push(ctx + '.recursive must be a boolean if present (got: ' + typeof entry.recursive + ')');
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// converter — required; must be a string or null (closed ConverterName enum — now enforced in phase 5e)
|
|
855
|
+
if (!Object.prototype.hasOwnProperty.call(entry, 'converter')) {
|
|
856
|
+
errors.push(ctx + '.converter is required (must be a string or null)');
|
|
857
|
+
} else if (entry.converter !== null && typeof entry.converter !== 'string') {
|
|
858
|
+
errors.push(ctx + '.converter must be a string or null (got: ' + typeof entry.converter + ')');
|
|
859
|
+
} else if (entry.converter !== null && typeof entry.converter === 'string' &&
|
|
860
|
+
!VALID_CONVERTER_NAMES.has(entry.converter)) {
|
|
861
|
+
// Closed ConverterName enum (ADR-857 phase 5e): reject unknown converter names
|
|
862
|
+
errors.push(ctx + '.converter "' + entry.converter + '" is not a known ConverterName');
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
return errors;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/**
|
|
869
|
+
* Validate runtime.artifactLayout per ADR-1016 Decision 3.
|
|
870
|
+
* Accepts the structured { global, local } shape.
|
|
871
|
+
* Returns an array of error strings.
|
|
872
|
+
*
|
|
873
|
+
* @param {string} capId Capability id (for error messages)
|
|
874
|
+
* @param {*} layout The artifactLayout value
|
|
875
|
+
* @returns {string[]}
|
|
876
|
+
*/
|
|
877
|
+
function validateArtifactLayout(capId, layout) {
|
|
878
|
+
const errors = [];
|
|
879
|
+
const ctx = 'capability "' + capId + '" runtime.artifactLayout';
|
|
880
|
+
|
|
881
|
+
if (typeof layout !== 'object' || layout === null || Array.isArray(layout)) {
|
|
882
|
+
errors.push(ctx + ' must be an object with "global" and "local" arrays');
|
|
883
|
+
return errors;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
for (const scope of ['global', 'local']) {
|
|
887
|
+
const arr = layout[scope];
|
|
888
|
+
if (!Array.isArray(arr)) {
|
|
889
|
+
errors.push(ctx + '.' + scope + ' must be an array');
|
|
890
|
+
} else {
|
|
891
|
+
for (let i = 0; i < arr.length; i++) {
|
|
892
|
+
errors.push(...validateArtifactKindEntry(capId, arr[i], 'artifactLayout.' + scope + '[' + i + ']'));
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
return errors;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function validateRuntimeBody(cap) {
|
|
901
|
+
const errors = [];
|
|
902
|
+
|
|
903
|
+
// C3: feature-only fields must NOT appear on a runtime cap
|
|
904
|
+
for (const field of FEATURE_FIELDS_FORBIDDEN_ON_RUNTIME) {
|
|
905
|
+
if (cap[field] !== undefined) {
|
|
906
|
+
errors.push('role:runtime capability must not have "' + field + '" (feature-only field)');
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// C3: require a runtime object
|
|
911
|
+
if (typeof cap.runtime !== 'object' || cap.runtime === null || Array.isArray(cap.runtime)) {
|
|
912
|
+
errors.push('role:runtime capability must have a "runtime" object');
|
|
913
|
+
return errors; // can't validate further without the object
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
const r = cap.runtime;
|
|
917
|
+
|
|
918
|
+
// configHome — must be a structured object (ADR-1016 Decision 1)
|
|
919
|
+
errors.push(...validateConfigHome(cap.id || '(unknown)', r.configHome));
|
|
920
|
+
|
|
921
|
+
// configFormat — closed 5-enum (unchanged)
|
|
922
|
+
if (!VALID_CONFIG_FORMATS.has(r.configFormat)) {
|
|
923
|
+
errors.push('runtime.configFormat must be one of: ' + [...VALID_CONFIG_FORMATS].join(', ') + ' (got: ' + r.configFormat + ')');
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// artifactLayout — structured { global, local } per ADR-1016 Decision 3
|
|
927
|
+
errors.push(...validateArtifactLayout(cap.id || '(unknown)', r.artifactLayout));
|
|
928
|
+
|
|
929
|
+
// commandStyle — closed 2-enum (ADR-1016 Decision 4); inline literal guard (CodeQL barrier)
|
|
930
|
+
if (r.commandStyle === '__proto__' || r.commandStyle === 'constructor' || r.commandStyle === 'prototype') {
|
|
931
|
+
errors.push('runtime.commandStyle "' + r.commandStyle + '" is a reserved name');
|
|
932
|
+
} else if (!VALID_COMMAND_STYLES.has(r.commandStyle)) {
|
|
933
|
+
errors.push(
|
|
934
|
+
'runtime.commandStyle must be one of: ' + [...VALID_COMMAND_STYLES].join(', ') +
|
|
935
|
+
' (got: ' + JSON.stringify(r.commandStyle) + ')',
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// hooksSurface — closed 6-enum (ADR-1016 Decision 5); inline literal guard (CodeQL barrier)
|
|
940
|
+
if (r.hooksSurface === '__proto__' || r.hooksSurface === 'constructor' || r.hooksSurface === 'prototype') {
|
|
941
|
+
errors.push('runtime.hooksSurface "' + r.hooksSurface + '" is a reserved name');
|
|
942
|
+
} else if (!VALID_HOOKS_SURFACES.has(r.hooksSurface)) {
|
|
943
|
+
errors.push(
|
|
944
|
+
'runtime.hooksSurface must be one of: ' + [...VALID_HOOKS_SURFACES].join(', ') +
|
|
945
|
+
' (got: ' + JSON.stringify(r.hooksSurface) + ')',
|
|
946
|
+
);
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// hookEvents — optional; if present must be in closed 3-enum (ADR-1016 Decision 5)
|
|
950
|
+
if (r.hookEvents !== undefined) {
|
|
951
|
+
if (r.hookEvents === '__proto__' || r.hookEvents === 'constructor' || r.hookEvents === 'prototype') {
|
|
952
|
+
errors.push('runtime.hookEvents "' + r.hookEvents + '" is a reserved name');
|
|
953
|
+
} else if (!VALID_HOOK_EVENTS.has(r.hookEvents)) {
|
|
954
|
+
errors.push(
|
|
955
|
+
'runtime.hookEvents must be one of: ' + [...VALID_HOOK_EVENTS].join(', ') +
|
|
956
|
+
' (got: ' + JSON.stringify(r.hookEvents) + ')',
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
// sandboxTier — closed 2-enum (ADR-1016 Decision 6); inline literal guard (CodeQL barrier)
|
|
962
|
+
if (r.sandboxTier === '__proto__' || r.sandboxTier === 'constructor' || r.sandboxTier === 'prototype') {
|
|
963
|
+
errors.push('runtime.sandboxTier "' + r.sandboxTier + '" is a reserved name');
|
|
964
|
+
} else if (!VALID_SANDBOX_TIERS.has(r.sandboxTier)) {
|
|
965
|
+
errors.push(
|
|
966
|
+
'runtime.sandboxTier must be one of: ' + [...VALID_SANDBOX_TIERS].join(', ') +
|
|
967
|
+
' (got: ' + JSON.stringify(r.sandboxTier) + ')',
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
// supportTier — 1 or 2 (unchanged)
|
|
972
|
+
if (r.supportTier !== 1 && r.supportTier !== 2) {
|
|
973
|
+
errors.push('runtime.supportTier must be 1 or 2 (got: ' + r.supportTier + ')');
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
// installSurface — required string in closed enum
|
|
977
|
+
if (!VALID_INSTALL_SURFACES.has(r.installSurface)) {
|
|
978
|
+
errors.push(
|
|
979
|
+
'runtime.installSurface must be one of: ' + [...VALID_INSTALL_SURFACES].join(', ') +
|
|
980
|
+
' (got: ' + JSON.stringify(r.installSurface) + ')',
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// writesSharedSettings — required boolean
|
|
985
|
+
if (typeof r.writesSharedSettings !== 'boolean') {
|
|
986
|
+
errors.push(
|
|
987
|
+
'runtime.writesSharedSettings must be a boolean (got: ' + JSON.stringify(r.writesSharedSettings) + ')',
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// permissionWriter — required key; value must be null or a string in VALID_PERMISSION_WRITERS
|
|
992
|
+
if (!Object.prototype.hasOwnProperty.call(r, 'permissionWriter')) {
|
|
993
|
+
errors.push('runtime.permissionWriter is required (must be null or one of: ' + [...VALID_PERMISSION_WRITERS].join(', ') + ')');
|
|
994
|
+
} else if (r.permissionWriter !== null && !VALID_PERMISSION_WRITERS.has(r.permissionWriter)) {
|
|
995
|
+
errors.push(
|
|
996
|
+
'runtime.permissionWriter must be null or one of: ' + [...VALID_PERMISSION_WRITERS].join(', ') +
|
|
997
|
+
' (got: ' + JSON.stringify(r.permissionWriter) + ')',
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// extendedHookEvents — required array; every element must be in closed enum
|
|
1002
|
+
if (!Array.isArray(r.extendedHookEvents)) {
|
|
1003
|
+
errors.push(
|
|
1004
|
+
'runtime.extendedHookEvents must be an array (got: ' + JSON.stringify(r.extendedHookEvents) + ')',
|
|
1005
|
+
);
|
|
1006
|
+
} else {
|
|
1007
|
+
for (let i = 0; i < r.extendedHookEvents.length; i++) {
|
|
1008
|
+
const ev = r.extendedHookEvents[i];
|
|
1009
|
+
if (typeof ev !== 'string' || !VALID_EXTENDED_HOOK_EVENTS.has(ev)) {
|
|
1010
|
+
errors.push(
|
|
1011
|
+
'runtime.extendedHookEvents[' + i + '] must be one of: ' + [...VALID_EXTENDED_HOOK_EVENTS].join(', ') +
|
|
1012
|
+
' (got: ' + JSON.stringify(ev) + ')',
|
|
1013
|
+
);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
// GATE A: installSurface ↔ hooksSurface consistency (DEFECT.GENERATIVE-FIX)
|
|
1019
|
+
// Only check if both fields are valid strings (individual field validators above report type errors).
|
|
1020
|
+
if (typeof r.installSurface === 'string' && typeof r.hooksSurface === 'string') {
|
|
1021
|
+
const allowedHooksSurfaces = INSTALL_SURFACE_TO_ALLOWED_HOOKS_SURFACES.get(r.installSurface);
|
|
1022
|
+
if (allowedHooksSurfaces !== undefined && !allowedHooksSurfaces.has(r.hooksSurface)) {
|
|
1023
|
+
errors.push(
|
|
1024
|
+
'runtime.hooksSurface "' + r.hooksSurface + '" is not valid for installSurface "' + r.installSurface + '"' +
|
|
1025
|
+
' — allowed: ' + [...allowedHooksSurfaces].join(', ') +
|
|
1026
|
+
' (src: INSTALL_SURFACE_TO_ALLOWED_HOOKS_SURFACES in scripts/gen-capability-registry.cjs)',
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// GATE B: extendedHookEvents ↔ hookEvents consistency (DEFECT.GENERATIVE-FIX)
|
|
1032
|
+
// If extendedHookEvents contains Gemini agent-events, hookEvents must be 'gemini'.
|
|
1033
|
+
// If it contains Claude-family events, hookEvents must be 'claude'.
|
|
1034
|
+
// Empty extendedHookEvents imposes no constraint.
|
|
1035
|
+
if (Array.isArray(r.extendedHookEvents) && r.extendedHookEvents.length > 0) {
|
|
1036
|
+
const hasGeminiEvents = r.extendedHookEvents.some((ev) => GEMINI_AGENT_EVENTS.has(ev));
|
|
1037
|
+
const hasClaudeEvents = r.extendedHookEvents.some((ev) => CLAUDE_FAMILY_EVENTS.has(ev));
|
|
1038
|
+
if (hasGeminiEvents && r.hookEvents !== 'gemini') {
|
|
1039
|
+
errors.push(
|
|
1040
|
+
'runtime.extendedHookEvents contains Gemini agent-events (' +
|
|
1041
|
+
r.extendedHookEvents.filter((ev) => GEMINI_AGENT_EVENTS.has(ev)).join(', ') +
|
|
1042
|
+
') but runtime.hookEvents is "' + r.hookEvents + '" — must be "gemini"',
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
if (hasClaudeEvents && r.hookEvents !== 'claude') {
|
|
1046
|
+
errors.push(
|
|
1047
|
+
'runtime.extendedHookEvents contains Claude-family events (' +
|
|
1048
|
+
r.extendedHookEvents.filter((ev) => CLAUDE_FAMILY_EVENTS.has(ev)).join(', ') +
|
|
1049
|
+
') but runtime.hookEvents is "' + r.hookEvents + '" — must be "claude"',
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
return errors;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// #1459 CONVERGENCE finding 1(b) — GENEROUS DoS backstop on a (possibly project-plantable) hook
|
|
1058
|
+
// fragment file. A real fragment is a few KiB of markdown; 8 MiB is wildly more than any legitimate
|
|
1059
|
+
// fragment. The bounded reader refuses a non-regular (FIFO/device/symlink-to-nonregular) or oversized
|
|
1060
|
+
// fragment WITHOUT a raw blocking read, so a forged in-bundle FIFO/oversized fragment.path becomes an
|
|
1061
|
+
// un-materializable fragment (a validation error / skip) instead of hanging or OOM-ing the loop.
|
|
1062
|
+
const FRAGMENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
1063
|
+
|
|
1064
|
+
function materializeHookFragments(cap, capDir) {
|
|
1065
|
+
const errors = [];
|
|
1066
|
+
const hookGroups = [
|
|
1067
|
+
['steps', Array.isArray(cap.steps) ? cap.steps : []],
|
|
1068
|
+
['contributions', Array.isArray(cap.contributions) ? cap.contributions : []],
|
|
1069
|
+
];
|
|
1070
|
+
|
|
1071
|
+
// #1459 CONVERGENCE finding 1(b): the fragment body is read via the SHARED bounded fd reader (open →
|
|
1072
|
+
// fstat → require regular file → size cap → read exactly size), NOT a raw fs.readFileSync(abs,'utf8')
|
|
1073
|
+
// which BLOCKS forever on a forged in-bundle FIFO and reads an oversized fragment unbounded into memory.
|
|
1074
|
+
// Required lazily so the committed plain-.cjs validator does not hard-depend on the built ledger artifact
|
|
1075
|
+
// at module-load time (materialize is a runtime path, reached only after build:lib). A bounded-reader
|
|
1076
|
+
// throw (non-regular/oversized/IO) → an un-materializable-fragment validation error, not a hang.
|
|
1077
|
+
let readSmallRegularFile;
|
|
1078
|
+
try {
|
|
1079
|
+
({ readSmallRegularFile } = require('./capability-ledger.cjs'));
|
|
1080
|
+
} catch {
|
|
1081
|
+
// Defensive: if the bounded reader is unavailable, fall back to a fail-CLOSED stub so we never
|
|
1082
|
+
// silently revert to an unbounded raw read. A null-returning stub turns every path fragment into an
|
|
1083
|
+
// "could not be read" error rather than a hang (declarative-only fragments use `inline` and skip this).
|
|
1084
|
+
readSmallRegularFile = () => null;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
for (const [groupName, hooks] of hookGroups) {
|
|
1088
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
1089
|
+
const hook = hooks[i];
|
|
1090
|
+
if (!hook || typeof hook !== 'object' || Array.isArray(hook)) continue;
|
|
1091
|
+
const fragment = hook.fragment;
|
|
1092
|
+
if (!fragment || typeof fragment !== 'object' || Array.isArray(fragment)) continue;
|
|
1093
|
+
if (typeof fragment.inline === 'string') continue;
|
|
1094
|
+
if (typeof fragment.path !== 'string') continue;
|
|
1095
|
+
|
|
1096
|
+
const abs = path.resolve(capDir, fragment.path);
|
|
1097
|
+
const capRoot = path.resolve(capDir);
|
|
1098
|
+
if (abs !== capRoot && !abs.startsWith(capRoot + path.sep)) {
|
|
1099
|
+
errors.push(
|
|
1100
|
+
cap.id + '/' + groupName + '[' + i + '].fragment.path escapes capability directory: ' +
|
|
1101
|
+
fragment.path,
|
|
1102
|
+
);
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
try {
|
|
1107
|
+
const body = readSmallRegularFile(abs, FRAGMENT_MAX_BYTES);
|
|
1108
|
+
if (body === null) {
|
|
1109
|
+
// null = genuinely missing (ENOENT) OR refused as non-regular/oversized via the stub fallback.
|
|
1110
|
+
errors.push(
|
|
1111
|
+
cap.id + '/' + groupName + '[' + i + '].fragment.path could not be read (missing, non-regular ' +
|
|
1112
|
+
'(FIFO/device), or exceeds the size cap): ' + fragment.path,
|
|
1113
|
+
);
|
|
1114
|
+
continue;
|
|
1115
|
+
}
|
|
1116
|
+
fragment.inline = body;
|
|
1117
|
+
} catch (err) {
|
|
1118
|
+
// Bounded-reader fail-closed throw (non-regular/oversized/IO) — an un-materializable fragment.
|
|
1119
|
+
errors.push(
|
|
1120
|
+
cap.id + '/' + groupName + '[' + i + '].fragment.path could not be read: ' +
|
|
1121
|
+
fragment.path + ' (' + err.message + ')',
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
return errors;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function validateFragment(fragment, prefix) {
|
|
1131
|
+
const errors = [];
|
|
1132
|
+
|
|
1133
|
+
if (typeof fragment !== 'object' || fragment === null || Array.isArray(fragment)) {
|
|
1134
|
+
errors.push(prefix + ' must be an object with path or inline key');
|
|
1135
|
+
return errors;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const hasPath = Object.prototype.hasOwnProperty.call(fragment, 'path');
|
|
1139
|
+
const hasInline = Object.prototype.hasOwnProperty.call(fragment, 'inline');
|
|
1140
|
+
if (!hasPath && !hasInline) {
|
|
1141
|
+
errors.push(prefix + ' must have a "path" or "inline" key');
|
|
1142
|
+
}
|
|
1143
|
+
if (hasInline) {
|
|
1144
|
+
const inline = fragment.inline;
|
|
1145
|
+
if (typeof inline !== 'string') {
|
|
1146
|
+
errors.push(prefix + '.inline must be a string');
|
|
1147
|
+
} else if (inline === '') {
|
|
1148
|
+
errors.push(prefix + '.inline must be a non-empty string');
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
// S1: fragment.path traversal guard — must be a relative path with no ".." segments
|
|
1152
|
+
if (hasPath) {
|
|
1153
|
+
const p = fragment.path;
|
|
1154
|
+
if (typeof p !== 'string' || p === '' || path.isAbsolute(p) || p.split(/[\\/]/).includes('..')) {
|
|
1155
|
+
errors.push(prefix + '.path must be a relative path with no ".." segments');
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
return errors;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/**
|
|
1163
|
+
* Validate a single step entry.
|
|
1164
|
+
*
|
|
1165
|
+
* @param {object} step The step to validate.
|
|
1166
|
+
* @param {string} prefix Path prefix for error messages (e.g. "steps[0]").
|
|
1167
|
+
* @param {Set|null} declaredSkills Set of skill stems declared in this capability's skills array,
|
|
1168
|
+
* or null if the skills array was not valid (skip membership check).
|
|
1169
|
+
* @param {Set|null} declaredAgents Set of agent names declared in this capability's agents array,
|
|
1170
|
+
* or null if the agents array was not valid (skip membership check).
|
|
1171
|
+
* @returns {string[]}
|
|
1172
|
+
*/
|
|
1173
|
+
function validateStep(step, prefix, declaredSkills, declaredAgents) {
|
|
1174
|
+
const errors = [];
|
|
1175
|
+
|
|
1176
|
+
if (!VALID_LOOP_POINTS.has(step.point)) {
|
|
1177
|
+
errors.push(prefix + '.point "' + step.point + '" is not a valid loop point');
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
if (typeof step.ref !== 'object' || step.ref === null) {
|
|
1181
|
+
errors.push(prefix + '.ref must be an object with skill, agent, or command key');
|
|
1182
|
+
} else {
|
|
1183
|
+
const hasSkill = Object.prototype.hasOwnProperty.call(step.ref, 'skill');
|
|
1184
|
+
const hasAgent = Object.prototype.hasOwnProperty.call(step.ref, 'agent');
|
|
1185
|
+
const hasCommand = Object.prototype.hasOwnProperty.call(step.ref, 'command');
|
|
1186
|
+
const dispatchCount = [hasSkill, hasAgent, hasCommand].filter(Boolean).length;
|
|
1187
|
+
if (dispatchCount === 0) {
|
|
1188
|
+
errors.push(prefix + '.ref must have a "skill", "agent", or "command" key');
|
|
1189
|
+
} else if (dispatchCount > 1) {
|
|
1190
|
+
// ref must be exclusive: skill XOR agent XOR command
|
|
1191
|
+
errors.push(prefix + '.ref must have exactly one of "skill", "agent", or "command", not multiple');
|
|
1192
|
+
}
|
|
1193
|
+
if (hasSkill && typeof step.ref.skill !== 'string') {
|
|
1194
|
+
errors.push(prefix + '.ref.skill must be a string');
|
|
1195
|
+
} else if (hasSkill && typeof step.ref.skill === 'string' && step.ref.skill.startsWith('gsd-')) {
|
|
1196
|
+
// Double-prefix guard: ref.skill is an unprefixed stem (e.g. "ui-review").
|
|
1197
|
+
// Workflow dispatch prepends "gsd-" at runtime → "gsd-ui-review".
|
|
1198
|
+
// A stem that already starts with "gsd-" would produce "gsd-gsd-..." at dispatch.
|
|
1199
|
+
errors.push(
|
|
1200
|
+
prefix + '.ref.skill "' + step.ref.skill + '" must not start with "gsd-" ' +
|
|
1201
|
+
'(it is an unprefixed stem; the workflow prepends "gsd-" at dispatch — ' +
|
|
1202
|
+
'starting with "gsd-" would produce "gsd-' + step.ref.skill + '")',
|
|
1203
|
+
);
|
|
1204
|
+
} else if (hasSkill && typeof step.ref.skill === 'string' && declaredSkills !== null && !declaredSkills.has(step.ref.skill)) {
|
|
1205
|
+
// Membership check: ref.skill must be declared in this capability's skills array.
|
|
1206
|
+
// This catches typos and ensures every dispatched skill is owned by this capability.
|
|
1207
|
+
errors.push(
|
|
1208
|
+
prefix + '.ref.skill "' + step.ref.skill + '" is not declared in this capability\'s skills: [' +
|
|
1209
|
+
[...declaredSkills].join(', ') + ']',
|
|
1210
|
+
);
|
|
1211
|
+
}
|
|
1212
|
+
if (hasAgent && typeof step.ref.agent !== 'string') {
|
|
1213
|
+
errors.push(prefix + '.ref.agent must be a string');
|
|
1214
|
+
} else if (hasAgent && typeof step.ref.agent === 'string' && declaredAgents !== null && !declaredAgents.has(step.ref.agent)) {
|
|
1215
|
+
// Membership check: ref.agent must be declared in this capability's agents array.
|
|
1216
|
+
errors.push(
|
|
1217
|
+
prefix + '.ref.agent "' + step.ref.agent + '" is not declared in this capability\'s agents: [' +
|
|
1218
|
+
[...declaredAgents].join(', ') + ']',
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
if (hasCommand && typeof step.ref.command !== 'string') {
|
|
1222
|
+
errors.push(prefix + '.ref.command must be a string');
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
if (!Array.isArray(step.produces)) {
|
|
1227
|
+
errors.push(prefix + '.produces must be an array');
|
|
1228
|
+
} else {
|
|
1229
|
+
for (const p of step.produces) {
|
|
1230
|
+
if (typeof p !== 'string') errors.push(prefix + '.produces entries must be strings');
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
if (!Array.isArray(step.consumes)) {
|
|
1235
|
+
errors.push(prefix + '.consumes must be an array');
|
|
1236
|
+
} else {
|
|
1237
|
+
for (const c of step.consumes) {
|
|
1238
|
+
if (typeof c !== 'string') errors.push(prefix + '.consumes entries must be strings');
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
if (step.when !== undefined && typeof step.when !== 'string') {
|
|
1243
|
+
errors.push(prefix + '.when must be a string if present');
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
if (step.fragment !== undefined) {
|
|
1247
|
+
errors.push(...validateFragment(step.fragment, prefix + '.fragment'));
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
if (!VALID_ON_ERROR.has(step.onError)) {
|
|
1251
|
+
errors.push(prefix + '.onError must be "skip" or "halt" (got: ' + step.onError + ')');
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
return errors;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function validateContribution(contrib, prefix) {
|
|
1258
|
+
const errors = [];
|
|
1259
|
+
|
|
1260
|
+
if (!VALID_LOOP_POINTS.has(contrib.point)) {
|
|
1261
|
+
errors.push(prefix + '.point "' + contrib.point + '" is not a valid loop point');
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
if (typeof contrib.into !== 'string') {
|
|
1265
|
+
errors.push(prefix + '.into must be a string (agent role name)');
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
if (!Array.isArray(contrib.produces)) {
|
|
1269
|
+
errors.push(prefix + '.produces must be an array');
|
|
1270
|
+
} else {
|
|
1271
|
+
for (const p of contrib.produces) {
|
|
1272
|
+
if (typeof p !== 'string') errors.push(prefix + '.produces entries must be strings');
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
if (!Array.isArray(contrib.consumes)) {
|
|
1277
|
+
errors.push(prefix + '.consumes must be an array');
|
|
1278
|
+
} else {
|
|
1279
|
+
for (const c of contrib.consumes) {
|
|
1280
|
+
if (typeof c !== 'string') errors.push(prefix + '.consumes entries must be strings');
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
errors.push(...validateFragment(contrib.fragment, prefix + '.fragment'));
|
|
1285
|
+
|
|
1286
|
+
if (contrib.when !== undefined && typeof contrib.when !== 'string') {
|
|
1287
|
+
errors.push(prefix + '.when must be a string if present');
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
if (contrib.onError !== undefined && !VALID_ON_ERROR.has(contrib.onError)) {
|
|
1291
|
+
errors.push(prefix + '.onError must be "skip" or "halt" if present');
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
return errors;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function validateGate(gate, prefix) {
|
|
1298
|
+
const errors = [];
|
|
1299
|
+
|
|
1300
|
+
if (!VALID_LOOP_POINTS.has(gate.point)) {
|
|
1301
|
+
errors.push(prefix + '.point "' + gate.point + '" is not a valid loop point');
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
if (typeof gate.check !== 'object' || gate.check === null) {
|
|
1305
|
+
errors.push(prefix + '.check must be an object');
|
|
1306
|
+
} else {
|
|
1307
|
+
const hasQuery = Object.prototype.hasOwnProperty.call(gate.check, 'query');
|
|
1308
|
+
const hasPredicate = Object.prototype.hasOwnProperty.call(gate.check, 'predicate');
|
|
1309
|
+
const hasAgentVerdict = Object.prototype.hasOwnProperty.call(gate.check, 'agentVerdict');
|
|
1310
|
+
const count = [hasQuery, hasPredicate, hasAgentVerdict].filter(Boolean).length;
|
|
1311
|
+
if (count !== 1) {
|
|
1312
|
+
errors.push(prefix + '.check must have exactly one of: query, predicate, agentVerdict');
|
|
1313
|
+
}
|
|
1314
|
+
// agentVerdict forces blocking: false (advisory only)
|
|
1315
|
+
if (hasAgentVerdict && gate.blocking === true) {
|
|
1316
|
+
errors.push(
|
|
1317
|
+
prefix + '.check.agentVerdict forces blocking: false (non-deterministic checks may not halt the loop)',
|
|
1318
|
+
);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
if (gate.when !== undefined && typeof gate.when !== 'string') {
|
|
1323
|
+
errors.push(prefix + '.when must be a string if present');
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
if (typeof gate.blocking !== 'boolean') {
|
|
1327
|
+
errors.push(prefix + '.blocking must be a boolean');
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
if (!VALID_ON_ERROR.has(gate.onError)) {
|
|
1331
|
+
errors.push(prefix + '.onError must be "skip" or "halt" (got: ' + gate.onError + ')');
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
return errors;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
// ─── Contract validation ──────────────────────────────────────────────────────
|
|
1338
|
+
|
|
1339
|
+
/**
|
|
1340
|
+
* Validate per-capability contract constraints against the Loop Host Contract.
|
|
1341
|
+
* This covers:
|
|
1342
|
+
* - contribution.into ∈ step's agentRoles
|
|
1343
|
+
* - when references a config key in cap.config
|
|
1344
|
+
*
|
|
1345
|
+
* NOTE: step.consumes satisfiability is NOT checked here — it requires the full
|
|
1346
|
+
* set of validated capabilities (cross-capability produces). It runs in
|
|
1347
|
+
* validateConsumesGlobal() after loadAndValidate builds capMap.
|
|
1348
|
+
*
|
|
1349
|
+
* @param {object} cap Validated capability object
|
|
1350
|
+
* @param {string} capId Capability id (for error messages)
|
|
1351
|
+
*/
|
|
1352
|
+
function validateAgainstContract(cap, capId) {
|
|
1353
|
+
if (cap.role !== 'feature') return [];
|
|
1354
|
+
const errors = [];
|
|
1355
|
+
const prefix = 'capability "' + capId + '"';
|
|
1356
|
+
|
|
1357
|
+
// contribution.into must be in the step's agentRoles
|
|
1358
|
+
for (const contrib of cap.contributions) {
|
|
1359
|
+
if (!VALID_LOOP_POINTS.has(contrib.point)) continue; // already reported
|
|
1360
|
+
const contract = POINT_TO_CONTRACT.get(contrib.point);
|
|
1361
|
+
if (contract && !contract.agentRoles.includes(contrib.into)) {
|
|
1362
|
+
errors.push(
|
|
1363
|
+
prefix + ' contribution.into "' + contrib.into + '" at point "' + contrib.point +
|
|
1364
|
+
'" is not in the step\'s agentRoles [' + contract.agentRoles.join(', ') + ']',
|
|
1365
|
+
);
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// when references a plausibly-valid config key (string — we require it's in cap.config)
|
|
1370
|
+
for (const step of cap.steps) {
|
|
1371
|
+
if (step.when !== undefined) {
|
|
1372
|
+
if (typeof step.when !== 'string') continue; // already reported above
|
|
1373
|
+
if (
|
|
1374
|
+
typeof cap.config === 'object' &&
|
|
1375
|
+
cap.config !== null &&
|
|
1376
|
+
!Object.prototype.hasOwnProperty.call(cap.config, step.when)
|
|
1377
|
+
) {
|
|
1378
|
+
errors.push(
|
|
1379
|
+
prefix + ' step.when "' + step.when + '" is not defined in capability config keys',
|
|
1380
|
+
);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
for (const contrib of cap.contributions) {
|
|
1386
|
+
if (contrib.when !== undefined) {
|
|
1387
|
+
if (typeof contrib.when !== 'string') continue;
|
|
1388
|
+
if (
|
|
1389
|
+
typeof cap.config === 'object' &&
|
|
1390
|
+
cap.config !== null &&
|
|
1391
|
+
!Object.prototype.hasOwnProperty.call(cap.config, contrib.when)
|
|
1392
|
+
) {
|
|
1393
|
+
errors.push(
|
|
1394
|
+
prefix + ' contribution.when "' + contrib.when + '" is not defined in capability config keys',
|
|
1395
|
+
);
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
for (const gate of cap.gates) {
|
|
1401
|
+
if (gate.when !== undefined) {
|
|
1402
|
+
if (typeof gate.when !== 'string') continue;
|
|
1403
|
+
if (
|
|
1404
|
+
typeof cap.config === 'object' &&
|
|
1405
|
+
cap.config !== null &&
|
|
1406
|
+
!Object.prototype.hasOwnProperty.call(cap.config, gate.when)
|
|
1407
|
+
) {
|
|
1408
|
+
errors.push(
|
|
1409
|
+
prefix + ' gate.when "' + gate.when + '" is not defined in capability config keys',
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
return errors;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* C1+C2: Global consumes-satisfiability validation.
|
|
1420
|
+
*
|
|
1421
|
+
* A hook at point P consuming artifact A is satisfiable iff:
|
|
1422
|
+
* - A is a host-produced artifact available from its step's :post point (C1), and
|
|
1423
|
+
* that :post point's POINT_ORDER index ≤ P's index; OR
|
|
1424
|
+
* - A is produced by any capability hook step at a point whose POINT_ORDER index ≤ P's index
|
|
1425
|
+
* (same-point is OK — topoSortSteps enforces intra-point order); OR
|
|
1426
|
+
* - A is never produced anywhere → rejected.
|
|
1427
|
+
*
|
|
1428
|
+
* Runs after capMap is fully built so cross-capability produces are visible.
|
|
1429
|
+
*
|
|
1430
|
+
* @param {Map<string, object>} capMap Fully-validated capability map.
|
|
1431
|
+
* @returns {string[]} Array of error strings.
|
|
1432
|
+
*/
|
|
1433
|
+
function validateConsumesGlobal(capMap) {
|
|
1434
|
+
const errors = [];
|
|
1435
|
+
|
|
1436
|
+
// Build producedAtPoint: artifact → earliest POINT_ORDER index at which it is produced.
|
|
1437
|
+
// Seed with host artifacts (C1: available from their step's :post point).
|
|
1438
|
+
// Host-artifact entries are tagged {pointIdx, isHost:true} so they are never excluded by
|
|
1439
|
+
// the self-consume check.
|
|
1440
|
+
const producedAtPoint = Object.create(null);
|
|
1441
|
+
for (const [artifact, postIdx] of Object.entries(HOST_ARTIFACT_EARLIEST_POINT_IDX)) {
|
|
1442
|
+
if (artifact === '__proto__' || artifact === 'constructor' || artifact === 'prototype') continue;
|
|
1443
|
+
producedAtPoint[artifact] = postIdx;
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
// Build a richer per-artifact producer list for the self-consume check.
|
|
1447
|
+
// Each entry: { pointIdx, capId, stepIdx } — identifies which cap+step produced the artifact.
|
|
1448
|
+
// Host artifacts are seeded separately (no capId) and always satisfy the consume check.
|
|
1449
|
+
// capHookProducers[artifact] = [{pointIdx, capId, stepIdx}, ...]
|
|
1450
|
+
const capHookProducers = Object.create(null);
|
|
1451
|
+
|
|
1452
|
+
// Add hook-produced artifacts from all capabilities.
|
|
1453
|
+
for (const [capId, cap] of capMap) {
|
|
1454
|
+
if (cap.role !== 'feature') continue;
|
|
1455
|
+
for (let si = 0; si < (cap.steps || []).length; si++) {
|
|
1456
|
+
const step = cap.steps[si];
|
|
1457
|
+
if (!VALID_LOOP_POINTS.has(step.point)) continue;
|
|
1458
|
+
const pointIdx = POINT_ORDER.indexOf(step.point);
|
|
1459
|
+
for (const artifact of (step.produces || [])) {
|
|
1460
|
+
if (typeof artifact !== 'string') continue;
|
|
1461
|
+
if (artifact === '__proto__' || artifact === 'constructor' || artifact === 'prototype') continue;
|
|
1462
|
+
if (producedAtPoint[artifact] === undefined || pointIdx < producedAtPoint[artifact]) {
|
|
1463
|
+
producedAtPoint[artifact] = pointIdx;
|
|
1464
|
+
}
|
|
1465
|
+
if (!capHookProducers[artifact]) capHookProducers[artifact] = [];
|
|
1466
|
+
capHookProducers[artifact].push({ pointIdx, capId, stepIdx: si });
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
// Duplicate-producer invariant: two capability steps may not produce the same artifact
|
|
1472
|
+
// at the same Loop Extension Point. Same-point dual production makes data-flow resolution
|
|
1473
|
+
// ambiguous and is rejected at gen time (Decision #6).
|
|
1474
|
+
for (const artifact of Object.keys(capHookProducers)) {
|
|
1475
|
+
if (artifact === '__proto__' || artifact === 'constructor' || artifact === 'prototype') continue;
|
|
1476
|
+
const producers = capHookProducers[artifact];
|
|
1477
|
+
// Group by pointIdx
|
|
1478
|
+
const byPoint = Object.create(null);
|
|
1479
|
+
for (const entry of producers) {
|
|
1480
|
+
if (!byPoint[entry.pointIdx]) byPoint[entry.pointIdx] = [];
|
|
1481
|
+
byPoint[entry.pointIdx].push(entry);
|
|
1482
|
+
}
|
|
1483
|
+
for (const pointIdxStr of Object.keys(byPoint)) {
|
|
1484
|
+
const group = byPoint[pointIdxStr];
|
|
1485
|
+
// Count distinct (capId, stepIdx) producer steps — a single step listing the same
|
|
1486
|
+
// artifact twice in its produces array pushes duplicate entries but represents only
|
|
1487
|
+
// ONE producer step and must not false-positive the cross-step gate.
|
|
1488
|
+
const distinctProducers = new Set(group.map((e) => e.capId + ' ' + e.stepIdx));
|
|
1489
|
+
if (distinctProducers.size >= 2) {
|
|
1490
|
+
const pointIdx = Number(pointIdxStr);
|
|
1491
|
+
const pointName = POINT_ORDER[pointIdx];
|
|
1492
|
+
const capIds = [...new Set(group.map((e) => e.capId))].sort().join(', ');
|
|
1493
|
+
throw new Error(
|
|
1494
|
+
'duplicate-producer invariant violated: artifact "' + artifact + '" is produced by ' +
|
|
1495
|
+
'two or more capability steps at the same Loop Extension Point "' + pointName + '" ' +
|
|
1496
|
+
'(capabilities: ' + capIds + '). ' +
|
|
1497
|
+
'Two capability steps producing the same artifact at the same Loop Extension Point ' +
|
|
1498
|
+
'makes data-flow resolution ambiguous and is rejected at gen time.',
|
|
1499
|
+
);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
// Now check every hook step's consumes.
|
|
1505
|
+
// Self-consume rule: a step H cannot satisfy its own consumes[A] from its own produces[A].
|
|
1506
|
+
// A is satisfiable for H iff:
|
|
1507
|
+
// (a) A is a host artifact with pointIdx <= stepPointIdx, OR
|
|
1508
|
+
// (b) A is produced by a DIFFERENT cap/step at pointIdx <= stepPointIdx.
|
|
1509
|
+
// "Different" means capId != H.capId OR stepIdx != H.stepIdx.
|
|
1510
|
+
for (const [capId, cap] of capMap) {
|
|
1511
|
+
if (cap.role !== 'feature') continue;
|
|
1512
|
+
const prefix = 'capability "' + capId + '"';
|
|
1513
|
+
for (let si = 0; si < (cap.steps || []).length; si++) {
|
|
1514
|
+
const step = cap.steps[si];
|
|
1515
|
+
if (!VALID_LOOP_POINTS.has(step.point)) continue;
|
|
1516
|
+
const stepPointIdx = POINT_ORDER.indexOf(step.point);
|
|
1517
|
+
for (const artifact of (step.consumes || [])) {
|
|
1518
|
+
if (typeof artifact !== 'string') continue;
|
|
1519
|
+
|
|
1520
|
+
// Check host-artifact satisfaction first (never excluded by self-consume).
|
|
1521
|
+
const hostIdx = HOST_ARTIFACT_EARLIEST_POINT_IDX[artifact];
|
|
1522
|
+
const hostSatisfied = hostIdx !== undefined && hostIdx <= stepPointIdx;
|
|
1523
|
+
if (hostSatisfied) continue; // fast-path: host artifact is available
|
|
1524
|
+
|
|
1525
|
+
// Check cap-hook producers, excluding this step itself.
|
|
1526
|
+
const producers = capHookProducers[artifact];
|
|
1527
|
+
if (!producers || producers.length === 0) {
|
|
1528
|
+
// Not a host artifact and never produced by any hook.
|
|
1529
|
+
errors.push(
|
|
1530
|
+
prefix + ' step at point "' + step.point + '" consumes "' + artifact +
|
|
1531
|
+
'" which is never produced by any host artifact or capability hook',
|
|
1532
|
+
);
|
|
1533
|
+
continue;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
// Find any non-self producer at pointIdx <= stepPointIdx.
|
|
1537
|
+
const otherEarliestIdx = producers.reduce((best, p) => {
|
|
1538
|
+
const isSelf = p.capId === capId && p.stepIdx === si;
|
|
1539
|
+
if (isSelf) return best;
|
|
1540
|
+
return (best === undefined || p.pointIdx < best) ? p.pointIdx : best;
|
|
1541
|
+
}, undefined);
|
|
1542
|
+
|
|
1543
|
+
if (otherEarliestIdx === undefined) {
|
|
1544
|
+
// Only producer is this step itself — self-consume violation.
|
|
1545
|
+
errors.push(
|
|
1546
|
+
prefix + ' step at point "' + step.point + '" consumes "' + artifact +
|
|
1547
|
+
'" which is only produced by this step itself (a step cannot consume its own output)',
|
|
1548
|
+
);
|
|
1549
|
+
} else if (otherEarliestIdx > stepPointIdx) {
|
|
1550
|
+
errors.push(
|
|
1551
|
+
prefix + ' step at point "' + step.point + '" consumes "' + artifact +
|
|
1552
|
+
'" which is only produced after this point (earliest available at POINT_ORDER index ' +
|
|
1553
|
+
otherEarliestIdx + ' = "' + POINT_ORDER[otherEarliestIdx] + '")',
|
|
1554
|
+
);
|
|
1555
|
+
}
|
|
1556
|
+
// else: satisfied by another cap/step at an earlier-or-same point — OK.
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
return errors;
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
// ─── Cross-capability invariants ──────────────────────────────────────────────
|
|
1565
|
+
|
|
1566
|
+
const TIER_RANK = { core: 0, standard: 1, full: 2 };
|
|
1567
|
+
|
|
1568
|
+
/**
|
|
1569
|
+
* Enforce cross-capability invariants.
|
|
1570
|
+
*
|
|
1571
|
+
* @param {Map<string, object>} capMap id → validated capability object
|
|
1572
|
+
* @param {Set<string>} centralKeys Set of keys in the central config-schema
|
|
1573
|
+
* @returns {string[]} Array of error strings; empty = all pass.
|
|
1574
|
+
*/
|
|
1575
|
+
function validateCrossCapability(capMap, centralKeys) {
|
|
1576
|
+
const errors = [];
|
|
1577
|
+
|
|
1578
|
+
// Ownership: one owner per skill stem + agent name
|
|
1579
|
+
const skillOwner = new Map(); // skill → capId
|
|
1580
|
+
const agentOwner = new Map(); // agent → capId
|
|
1581
|
+
const familyOwner = new Map(); // command family → capId (ADR-959)
|
|
1582
|
+
for (const [capId, cap] of capMap) {
|
|
1583
|
+
if (cap.role !== 'feature') continue;
|
|
1584
|
+
for (const skill of cap.skills) {
|
|
1585
|
+
if (skillOwner.has(skill)) {
|
|
1586
|
+
errors.push(
|
|
1587
|
+
'skill "' + skill + '" is owned by both "' + skillOwner.get(skill) + '" and "' + capId + '"',
|
|
1588
|
+
);
|
|
1589
|
+
} else {
|
|
1590
|
+
skillOwner.set(skill, capId);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
for (const agent of cap.agents) {
|
|
1594
|
+
if (agentOwner.has(agent)) {
|
|
1595
|
+
errors.push(
|
|
1596
|
+
'agent "' + agent + '" is owned by both "' + agentOwner.get(agent) + '" and "' + capId + '"',
|
|
1597
|
+
);
|
|
1598
|
+
} else {
|
|
1599
|
+
agentOwner.set(agent, capId);
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
// ADR-959: single family ownership across the whole registry
|
|
1603
|
+
if (Array.isArray(cap.commands)) {
|
|
1604
|
+
for (const cmd of cap.commands) {
|
|
1605
|
+
if (typeof cmd.family !== 'string' || cmd.family.length === 0) continue; // already reported
|
|
1606
|
+
if (cmd.family === '__proto__' || cmd.family === 'constructor' || cmd.family === 'prototype') continue;
|
|
1607
|
+
if (familyOwner.has(cmd.family)) {
|
|
1608
|
+
errors.push(
|
|
1609
|
+
'command family "' + cmd.family + '" is owned by both "' + familyOwner.get(cmd.family) + '" and "' + capId + '"',
|
|
1610
|
+
);
|
|
1611
|
+
} else {
|
|
1612
|
+
familyOwner.set(cmd.family, capId);
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
// Config key ownership: exclusive AND absent from central schema
|
|
1619
|
+
const configKeyOwner = new Map(); // key → capId
|
|
1620
|
+
for (const [capId, cap] of capMap) {
|
|
1621
|
+
if (cap.role !== 'feature' || typeof cap.config !== 'object' || cap.config === null) continue;
|
|
1622
|
+
for (const key of Object.keys(cap.config)) {
|
|
1623
|
+
if (configKeyOwner.has(key)) {
|
|
1624
|
+
errors.push(
|
|
1625
|
+
'config key "' + key + '" is owned by both "' + configKeyOwner.get(key) + '" and "' + capId + '"',
|
|
1626
|
+
);
|
|
1627
|
+
} else {
|
|
1628
|
+
configKeyOwner.set(key, capId);
|
|
1629
|
+
}
|
|
1630
|
+
if (centralKeys.has(key)) {
|
|
1631
|
+
errors.push(
|
|
1632
|
+
'config key "' + key + '" is declared in capability "' + capId +
|
|
1633
|
+
'" AND exists in the central config-schema — migration mid-flight: ' +
|
|
1634
|
+
'remove from central config-schema before adding to the capability',
|
|
1635
|
+
);
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
// requires: all ids exist
|
|
1641
|
+
for (const [capId, cap] of capMap) {
|
|
1642
|
+
if (!Array.isArray(cap.requires)) continue;
|
|
1643
|
+
for (const req of cap.requires) {
|
|
1644
|
+
if (!capMap.has(req)) {
|
|
1645
|
+
errors.push(
|
|
1646
|
+
'capability "' + capId + '" requires "' + req + '" which does not exist',
|
|
1647
|
+
);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
// runtimeCompat: explicit runtime ids must reference runtime capabilities.
|
|
1653
|
+
// The wildcard "*" means descriptor-backed runtimes are supported by default.
|
|
1654
|
+
const runtimeIds = new Set();
|
|
1655
|
+
for (const [id, cap] of capMap) {
|
|
1656
|
+
if (cap.role === 'runtime') runtimeIds.add(id);
|
|
1657
|
+
}
|
|
1658
|
+
for (const [capId, cap] of capMap) {
|
|
1659
|
+
if (cap.role !== 'feature' || typeof cap.runtimeCompat !== 'object' || cap.runtimeCompat === null) continue;
|
|
1660
|
+
for (const field of ['supported', 'unsupported']) {
|
|
1661
|
+
const entries = Array.isArray(cap.runtimeCompat[field]) ? cap.runtimeCompat[field] : [];
|
|
1662
|
+
for (const runtimeId of entries) {
|
|
1663
|
+
if (runtimeId === RUNTIME_COMPAT_WILDCARD) continue;
|
|
1664
|
+
if (typeof runtimeId !== 'string' || runtimeId.length === 0) continue;
|
|
1665
|
+
if (!runtimeIds.has(runtimeId)) {
|
|
1666
|
+
errors.push(
|
|
1667
|
+
'capability "' + capId + '" runtimeCompat.' + field +
|
|
1668
|
+
' references unknown runtime "' + runtimeId + '"',
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
if (cap.runtimeCompat.notes && typeof cap.runtimeCompat.notes === 'object') {
|
|
1674
|
+
for (const runtimeId of Object.keys(cap.runtimeCompat.notes)) {
|
|
1675
|
+
if (runtimeId === RUNTIME_COMPAT_WILDCARD) continue;
|
|
1676
|
+
if (!runtimeIds.has(runtimeId)) {
|
|
1677
|
+
errors.push(
|
|
1678
|
+
'capability "' + capId + '" runtimeCompat.notes references unknown runtime "' + runtimeId + '"',
|
|
1679
|
+
);
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
// requires: acyclic
|
|
1686
|
+
const cycleErrors = detectRequiresCycles(capMap);
|
|
1687
|
+
errors.push(...cycleErrors);
|
|
1688
|
+
|
|
1689
|
+
// requires: tier-monotone (core may not require standard/full; standard may not require full)
|
|
1690
|
+
for (const [capId, cap] of capMap) {
|
|
1691
|
+
if (!Array.isArray(cap.requires) || !VALID_TIERS.has(cap.tier)) continue;
|
|
1692
|
+
const myRank = TIER_RANK[cap.tier];
|
|
1693
|
+
for (const req of cap.requires) {
|
|
1694
|
+
const reqCap = capMap.get(req);
|
|
1695
|
+
if (!reqCap || !VALID_TIERS.has(reqCap.tier)) continue;
|
|
1696
|
+
const reqRank = TIER_RANK[reqCap.tier];
|
|
1697
|
+
if (reqRank > myRank) {
|
|
1698
|
+
errors.push(
|
|
1699
|
+
'tier-monotone violation: capability "' + capId + '" (tier: ' + cap.tier +
|
|
1700
|
+
') requires "' + req + '" (tier: ' + reqCap.tier +
|
|
1701
|
+
') — a capability may not require a higher-tier capability',
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
return errors;
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Detect cycles in the requires graph using DFS.
|
|
1712
|
+
*/
|
|
1713
|
+
function detectRequiresCycles(capMap) {
|
|
1714
|
+
const errors = [];
|
|
1715
|
+
const WHITE = 0, GRAY = 1, BLACK = 2;
|
|
1716
|
+
const color = new Map([...capMap.keys()].map((k) => [k, WHITE]));
|
|
1717
|
+
|
|
1718
|
+
function dfs(id, stack) {
|
|
1719
|
+
if (color.get(id) === GRAY) {
|
|
1720
|
+
const cycleStr = [...stack, id].join(' → ');
|
|
1721
|
+
errors.push('requires cycle detected: ' + cycleStr);
|
|
1722
|
+
return;
|
|
1723
|
+
}
|
|
1724
|
+
if (color.get(id) === BLACK) return;
|
|
1725
|
+
color.set(id, GRAY);
|
|
1726
|
+
stack.push(id);
|
|
1727
|
+
const cap = capMap.get(id);
|
|
1728
|
+
if (cap && Array.isArray(cap.requires)) {
|
|
1729
|
+
for (const req of cap.requires) {
|
|
1730
|
+
if (capMap.has(req)) dfs(req, stack);
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
stack.pop();
|
|
1734
|
+
color.set(id, BLACK);
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
for (const id of capMap.keys()) {
|
|
1738
|
+
if (color.get(id) === WHITE) dfs(id, []);
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
return errors;
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
// ─── requiresClosure ─────────────────────────────────────────────────────────
|
|
1745
|
+
|
|
1746
|
+
/**
|
|
1747
|
+
* Compute the transitive requires closure for a capability id.
|
|
1748
|
+
* Returns a Set<string> of all transitively required capability ids.
|
|
1749
|
+
*
|
|
1750
|
+
* @param {string} id
|
|
1751
|
+
* @param {Map<string, object>} capMap
|
|
1752
|
+
*/
|
|
1753
|
+
function computeRequiresClosure(id, capMap) {
|
|
1754
|
+
const visited = new Set();
|
|
1755
|
+
const queue = [id];
|
|
1756
|
+
while (queue.length > 0) {
|
|
1757
|
+
const current = queue.shift();
|
|
1758
|
+
const cap = capMap.get(current);
|
|
1759
|
+
if (!cap || !Array.isArray(cap.requires)) continue;
|
|
1760
|
+
for (const req of cap.requires) {
|
|
1761
|
+
if (!visited.has(req)) {
|
|
1762
|
+
visited.add(req);
|
|
1763
|
+
queue.push(req);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
return visited;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
// ─── Topological ordering ─────────────────────────────────────────────────────
|
|
1771
|
+
|
|
1772
|
+
function topoSortHookEntries(entries, hookKey, hookKind) {
|
|
1773
|
+
if (entries.length <= 1) return entries;
|
|
1774
|
+
|
|
1775
|
+
// Build adjacency: entry A must come before entry B if B consumes something A produces
|
|
1776
|
+
const n = entries.length;
|
|
1777
|
+
const inDegree = new Array(n).fill(0);
|
|
1778
|
+
const adj = Array.from({ length: n }, () => []);
|
|
1779
|
+
|
|
1780
|
+
for (let i = 0; i < n; i++) {
|
|
1781
|
+
const producesI = new Set(entries[i][hookKey].produces || []);
|
|
1782
|
+
for (let j = 0; j < n; j++) {
|
|
1783
|
+
if (i === j) continue;
|
|
1784
|
+
const consumesJ = entries[j][hookKey].consumes || [];
|
|
1785
|
+
for (const artifact of consumesJ) {
|
|
1786
|
+
if (producesI.has(artifact)) {
|
|
1787
|
+
adj[i].push(j);
|
|
1788
|
+
inDegree[j]++;
|
|
1789
|
+
break;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
// Kahn's algorithm with stable tiebreak on capId
|
|
1796
|
+
const queue = [];
|
|
1797
|
+
for (let i = 0; i < n; i++) {
|
|
1798
|
+
if (inDegree[i] === 0) queue.push(i);
|
|
1799
|
+
}
|
|
1800
|
+
// Sort queue by capId for determinism
|
|
1801
|
+
queue.sort((a, b) => entries[a].capId.localeCompare(entries[b].capId));
|
|
1802
|
+
|
|
1803
|
+
const result = [];
|
|
1804
|
+
while (queue.length > 0) {
|
|
1805
|
+
// Take the first (sorted) ready node
|
|
1806
|
+
const idx = queue.shift();
|
|
1807
|
+
result.push(entries[idx]);
|
|
1808
|
+
const newReady = [];
|
|
1809
|
+
for (const neighbor of adj[idx]) {
|
|
1810
|
+
inDegree[neighbor]--;
|
|
1811
|
+
if (inDegree[neighbor] === 0) newReady.push(neighbor);
|
|
1812
|
+
}
|
|
1813
|
+
newReady.sort((a, b) => entries[a].capId.localeCompare(entries[b].capId));
|
|
1814
|
+
queue.push(...newReady);
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
// Fix #2: if result.length < n, Kahn's could not complete — there is a produces/consumes
|
|
1818
|
+
// cycle. Do NOT silently fall back to declaration order; throw a clear error.
|
|
1819
|
+
if (result.length < n) {
|
|
1820
|
+
const sortedIds = entries.map((e) => e.capId).join(', ');
|
|
1821
|
+
throw new Error(
|
|
1822
|
+
'produces/consumes cycle detected in ' + hookKind + ' at point "' +
|
|
1823
|
+
(entries[0] && entries[0][hookKey] ? entries[0][hookKey].point : '?') +
|
|
1824
|
+
'" among capabilities [' + sortedIds + ']: ' +
|
|
1825
|
+
'a cycle in hook produces/consumes prevents deterministic ordering',
|
|
1826
|
+
);
|
|
1827
|
+
}
|
|
1828
|
+
return result;
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
/**
|
|
1832
|
+
* Topologically sort steps at a given point by produces/consumes.
|
|
1833
|
+
* Capability-id tiebreak for determinism.
|
|
1834
|
+
*
|
|
1835
|
+
* @param {{ capId: string, step: object }[]} entries
|
|
1836
|
+
* @returns {{ capId: string, step: object }[]}
|
|
1837
|
+
*/
|
|
1838
|
+
function topoSortSteps(entries) {
|
|
1839
|
+
return topoSortHookEntries(entries, 'step', 'steps');
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
function topoSortContributions(entries) {
|
|
1843
|
+
return topoSortHookEntries(entries, 'contrib', 'contributions');
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
// ─── Gen-time wired guard ─────────────────────────────────────────────────────
|
|
1847
|
+
|
|
1848
|
+
/**
|
|
1849
|
+
* Validate that every hook point declared by a capability has a corresponding
|
|
1850
|
+
* `loop render-hooks <point>` call site in one of the host-loop workflow files.
|
|
1851
|
+
*
|
|
1852
|
+
* Only valid loop points (in VALID_LOOP_POINTS) are checked here. Invalid points
|
|
1853
|
+
* are already caught by validateStep/validateContribution/validateGate — do not
|
|
1854
|
+
* double-report.
|
|
1855
|
+
*
|
|
1856
|
+
* @param {object} cap Validated capability object.
|
|
1857
|
+
* @param {Set<string>} wiredSet Set of points that have call sites in host workflows.
|
|
1858
|
+
* @returns {string[]} Array of error strings; empty means all points are wired.
|
|
1859
|
+
*/
|
|
1860
|
+
function validateHooksWired(cap, wiredSet) {
|
|
1861
|
+
const errors = [];
|
|
1862
|
+
const capId = cap.id || '(unknown)';
|
|
1863
|
+
|
|
1864
|
+
function checkPoint(point, groupName, idx) {
|
|
1865
|
+
// Only flag valid points that are unwired — invalid points are schema-validator's job.
|
|
1866
|
+
if (!VALID_LOOP_POINTS.has(point)) return;
|
|
1867
|
+
if (!wiredSet.has(point)) {
|
|
1868
|
+
errors.push(
|
|
1869
|
+
'capability "' + capId + '" ' + groupName + '[' + idx + '].point "' + point +
|
|
1870
|
+
'" is declared but not wired in any host-loop workflow ' +
|
|
1871
|
+
'(no `loop render-hooks ' + point + '` call site). ' +
|
|
1872
|
+
'Wire the call site in the host workflow ' +
|
|
1873
|
+
'(see scripts/gen-loop-host-contract.cjs STEP_WORKFLOWS) or remove the hook.',
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
for (let i = 0; i < (cap.steps || []).length; i++) {
|
|
1879
|
+
const hook = cap.steps[i];
|
|
1880
|
+
if (hook.point !== undefined) checkPoint(hook.point, 'steps', i);
|
|
1881
|
+
}
|
|
1882
|
+
for (let i = 0; i < (cap.contributions || []).length; i++) {
|
|
1883
|
+
const hook = cap.contributions[i];
|
|
1884
|
+
if (hook.point !== undefined) checkPoint(hook.point, 'contributions', i);
|
|
1885
|
+
}
|
|
1886
|
+
for (let i = 0; i < (cap.gates || []).length; i++) {
|
|
1887
|
+
const hook = cap.gates[i];
|
|
1888
|
+
if (hook.point !== undefined) checkPoint(hook.point, 'gates', i);
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
return errors;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
// ─── classifyCrossErrors ──────────────────────────────────────────────────────
|
|
1895
|
+
|
|
1896
|
+
/**
|
|
1897
|
+
* Fix #3: Emit pending-migration WARNINGs for config keys that collide with the central
|
|
1898
|
+
* config-schema. Per ADR-894 staged cutover, a collision during the registry-only phase is
|
|
1899
|
+
* NOT a hard error — the capability pipeline is being established before the atomic cutover
|
|
1900
|
+
* PR for each feature. The registry still generates; the warning tells the maintainer which
|
|
1901
|
+
* keys need to be moved out of the central schema at cutover time.
|
|
1902
|
+
*
|
|
1903
|
+
* A NEW unexpected collision (a key that shouldn't be in both) is also surfaced — the
|
|
1904
|
+
* maintainer sees it in build output rather than it being silently swallowed.
|
|
1905
|
+
*
|
|
1906
|
+
* Reference: ADR-894 §4 "config-key ownership exclusive AND complete — presence in both =
|
|
1907
|
+
* collision = a mid-flight migration; finish the move."
|
|
1908
|
+
*
|
|
1909
|
+
* @param {string[]} crossErrors Errors from validateCrossCapability (may include collision msgs)
|
|
1910
|
+
* @returns {{ hardErrors: string[], pendingMigrationWarnings: string[] }}
|
|
1911
|
+
*/
|
|
1912
|
+
function classifyCrossErrors(crossErrors) {
|
|
1913
|
+
const hardErrors = [];
|
|
1914
|
+
const pendingMigrationWarnings = [];
|
|
1915
|
+
const collisionRe = /config key "([^"]+)" is declared in capability "([^"]+)" AND exists in the central config-schema/;
|
|
1916
|
+
|
|
1917
|
+
for (const e of crossErrors) {
|
|
1918
|
+
const m = collisionRe.exec(e);
|
|
1919
|
+
if (m) {
|
|
1920
|
+
// Collision = pending-migration warning, not a hard error during 3a-impl staged cutover
|
|
1921
|
+
pendingMigrationWarnings.push(
|
|
1922
|
+
'⚠ pending-migration: capability \'' + m[2] + '\' declares config key \'' + m[1] +
|
|
1923
|
+
'\' still present in central config-schema; finish the move at cutover',
|
|
1924
|
+
);
|
|
1925
|
+
} else {
|
|
1926
|
+
hardErrors.push(e);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
return { hardErrors, pendingMigrationWarnings };
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
// ─── ADR-857 phase 5e: configFormat ↔ installSurface parity gate ─────────────
|
|
1933
|
+
|
|
1934
|
+
// Map: installSurface → expected configFormat
|
|
1935
|
+
// Derived from the pairing of capability.json descriptors (installSurface)
|
|
1936
|
+
// and capability.json descriptors (configFormat). DEFECT.GENERATIVE-FIX: this map
|
|
1937
|
+
// is the single parity contract between the two generated surfaces.
|
|
1938
|
+
// NOTE: both values come from the descriptor bodies in capMap — no dependency on
|
|
1939
|
+
// runtime-config-adapter-registry.cjs, which now requires capability-registry.cjs
|
|
1940
|
+
// (the file this gen-script produces), and thus must not be required here.
|
|
1941
|
+
const INSTALL_SURFACE_TO_CONFIG_FORMAT = new Map([
|
|
1942
|
+
['settings-json', 'settings-json'],
|
|
1943
|
+
['codex-toml', 'toml'],
|
|
1944
|
+
['copilot-instructions', 'markdown'],
|
|
1945
|
+
['cline-rules', 'markdown-dir'],
|
|
1946
|
+
['cursor-hooks-json', 'none'],
|
|
1947
|
+
['profile-marker-only', 'none'],
|
|
1948
|
+
]);
|
|
1949
|
+
|
|
1950
|
+
/**
|
|
1951
|
+
* ADR-857 phase 5e: configFormat ↔ installSurface parity gate.
|
|
1952
|
+
*
|
|
1953
|
+
* For each runtime capability that has an installSurface in its descriptor,
|
|
1954
|
+
* assert that its configFormat matches the expected value derived from its
|
|
1955
|
+
* installSurface. Both values are read directly from the capMap descriptor
|
|
1956
|
+
* bodies — no dependency on runtime-config-adapter-registry.cjs.
|
|
1957
|
+
*
|
|
1958
|
+
* HARD gate — throws on mismatch (DEFECT.GENERATIVE-FIX: this invariant is
|
|
1959
|
+
* derived from two parallel generated surfaces and must fail loudly).
|
|
1960
|
+
*
|
|
1961
|
+
* @param {Map<string, object>} capMap Fully-validated capability map.
|
|
1962
|
+
* @returns {void} Throws on mismatch; returns normally on success.
|
|
1963
|
+
*/
|
|
1964
|
+
function runConfigFormatParityGate(capMap) {
|
|
1965
|
+
// Read installSurface directly from the descriptor bodies already loaded into
|
|
1966
|
+
// capMap — eliminates the require cycle introduced when adapter-registry was
|
|
1967
|
+
// changed to require capability-registry.cjs (ADR-857 phase 5g drive 2).
|
|
1968
|
+
for (const [capId, cap] of capMap) {
|
|
1969
|
+
if (cap.role !== 'runtime') continue;
|
|
1970
|
+
|
|
1971
|
+
const r = cap.runtime;
|
|
1972
|
+
if (!r || typeof r.configFormat !== 'string') continue; // already validated above
|
|
1973
|
+
|
|
1974
|
+
// Only check runtimes that have an installSurface (i.e. are config-adapter runtimes)
|
|
1975
|
+
if (typeof r.installSurface !== 'string') continue; // grok etc. excluded — no installSurface
|
|
1976
|
+
|
|
1977
|
+
const installSurface = r.installSurface;
|
|
1978
|
+
const expectedConfigFormat = INSTALL_SURFACE_TO_CONFIG_FORMAT.get(installSurface);
|
|
1979
|
+
|
|
1980
|
+
if (expectedConfigFormat === undefined) {
|
|
1981
|
+
// Unknown installSurface — the mapping needs to be updated
|
|
1982
|
+
throw new Error(
|
|
1983
|
+
'configFormat parity gate: runtime "' + capId + '" has installSurface "' + installSurface +
|
|
1984
|
+
'" which is not in the INSTALL_SURFACE_TO_CONFIG_FORMAT mapping — ' +
|
|
1985
|
+
'update the mapping in scripts/gen-capability-registry.cjs',
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
if (r.configFormat !== expectedConfigFormat) {
|
|
1990
|
+
throw new Error(
|
|
1991
|
+
'configFormat parity gate FAILED for runtime "' + capId + '":\n' +
|
|
1992
|
+
' installSurface: ' + installSurface + '\n' +
|
|
1993
|
+
' expected configFormat: ' + expectedConfigFormat + '\n' +
|
|
1994
|
+
' actual configFormat: ' + r.configFormat + '\n' +
|
|
1995
|
+
'The capability.json configFormat must match the value derived from installSurface ' +
|
|
1996
|
+
'(src: scripts/gen-capability-registry.cjs INSTALL_SURFACE_TO_CONFIG_FORMAT)',
|
|
1997
|
+
);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
// ─── Exports ──────────────────────────────────────────────────────────────────
|
|
2003
|
+
|
|
2004
|
+
module.exports = {
|
|
2005
|
+
// Constants
|
|
2006
|
+
SCHEMA_VERSION,
|
|
2007
|
+
POINT_ORDER,
|
|
2008
|
+
HOST_ARTIFACT_EARLIEST_POINT_IDX,
|
|
2009
|
+
VALID_LOOP_POINTS,
|
|
2010
|
+
POINT_TO_CONTRACT,
|
|
2011
|
+
VALID_CONFIG_SLICE_TYPES,
|
|
2012
|
+
KEBAB_RE,
|
|
2013
|
+
VALID_ROLES,
|
|
2014
|
+
VALID_TIERS,
|
|
2015
|
+
VALID_ON_ERROR,
|
|
2016
|
+
RUNTIME_COMPAT_WILDCARD,
|
|
2017
|
+
SEMVER_RE,
|
|
2018
|
+
SEMVER_RANGE_RE,
|
|
2019
|
+
SHA512_INTEGRITY_RE,
|
|
2020
|
+
VALID_CONVERTER_NAMES,
|
|
2021
|
+
VALID_CONFIG_FORMATS,
|
|
2022
|
+
VALID_CONFIG_HOME_KINDS,
|
|
2023
|
+
VALID_COMMAND_STYLES,
|
|
2024
|
+
VALID_HOOKS_SURFACES,
|
|
2025
|
+
VALID_HOOK_EVENTS,
|
|
2026
|
+
VALID_SANDBOX_TIERS,
|
|
2027
|
+
VALID_ARTIFACT_KIND_NAMES,
|
|
2028
|
+
VALID_ARTIFACT_NESTINGS,
|
|
2029
|
+
FEATURE_FIELDS_FORBIDDEN_ON_RUNTIME,
|
|
2030
|
+
VALID_INSTALL_SURFACES,
|
|
2031
|
+
VALID_PERMISSION_WRITERS,
|
|
2032
|
+
VALID_EXTENDED_HOOK_EVENTS,
|
|
2033
|
+
INSTALL_SURFACE_TO_ALLOWED_HOOKS_SURFACES,
|
|
2034
|
+
GEMINI_AGENT_EVENTS,
|
|
2035
|
+
CLAUDE_FAMILY_EVENTS,
|
|
2036
|
+
TIER_RANK,
|
|
2037
|
+
INSTALL_SURFACE_TO_CONFIG_FORMAT,
|
|
2038
|
+
// Functions
|
|
2039
|
+
isPlausibleRange,
|
|
2040
|
+
validateVersionEnvelope,
|
|
2041
|
+
validateCapability,
|
|
2042
|
+
validateCommandEntry,
|
|
2043
|
+
validateRuntimeCompat,
|
|
2044
|
+
validateFeatureBody,
|
|
2045
|
+
validateConfigHome,
|
|
2046
|
+
validateArtifactKindEntry,
|
|
2047
|
+
validateArtifactLayout,
|
|
2048
|
+
validateRuntimeBody,
|
|
2049
|
+
materializeHookFragments,
|
|
2050
|
+
validateFragment,
|
|
2051
|
+
validateStep,
|
|
2052
|
+
validateContribution,
|
|
2053
|
+
validateGate,
|
|
2054
|
+
validateAgainstContract,
|
|
2055
|
+
validateConsumesGlobal,
|
|
2056
|
+
validateCrossCapability,
|
|
2057
|
+
detectRequiresCycles,
|
|
2058
|
+
computeRequiresClosure,
|
|
2059
|
+
topoSortHookEntries,
|
|
2060
|
+
topoSortSteps,
|
|
2061
|
+
topoSortContributions,
|
|
2062
|
+
validateHooksWired,
|
|
2063
|
+
validateConfigSliceEntry,
|
|
2064
|
+
classifyCrossErrors,
|
|
2065
|
+
runConfigFormatParityGate,
|
|
2066
|
+
};
|