opencode-agent-orchestration-kit 1.0.31 → 1.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -3
- package/SECURITY.md +7 -0
- package/docker/open-design/data/app.sqlite +0 -0
- package/docker/open-design/data/app.sqlite-shm +0 -0
- package/docker/open-design/data/app.sqlite-wal +0 -0
- package/docker/open-design/opencode-auth/log/2026-05-02T105802.log +17 -0
- package/docker/open-design/opencode-auth/log/2026-05-04T102127.log +16 -0
- package/docker/open-design/opencode-auth/opencode.db +0 -0
- package/docker/open-design/opencode-auth/opencode.db-shm +0 -0
- package/docker/open-design/opencode-auth/opencode.db-wal +0 -0
- package/docs/cli.md +72 -0
- package/docs/installation.md +25 -5
- package/docs/quickstart.md +1 -1
- package/docs/releases/v1.0.32.md +68 -0
- package/docs/releases/v1.0.33.md +72 -0
- package/docs/security.md +4 -0
- package/docs/supply-chain.md +9 -5
- package/docs/threat-model.md +244 -0
- package/docs/use-cases/README.md +31 -0
- package/docs/use-cases/direct-label-change/README.md +76 -0
- package/docs/use-cases/direct-label-change/before/package.json +8 -0
- package/docs/use-cases/direct-label-change/before/src/settings-label.mjs +1 -0
- package/docs/use-cases/direct-label-change/before/test/settings-label.test.mjs +8 -0
- package/docs/use-cases/direct-label-change/case.json +118 -0
- package/docs/use-cases/direct-label-change/expected/package.json +8 -0
- package/docs/use-cases/direct-label-change/expected/src/settings-label.mjs +1 -0
- package/docs/use-cases/direct-label-change/expected/test/settings-label.test.mjs +8 -0
- package/docs/use-cases/feature-tag-normalizer/README.md +78 -0
- package/docs/use-cases/feature-tag-normalizer/before/package.json +8 -0
- package/docs/use-cases/feature-tag-normalizer/before/src/normalize-tags.mjs +3 -0
- package/docs/use-cases/feature-tag-normalizer/before/test/normalize-tags.test.mjs +13 -0
- package/docs/use-cases/feature-tag-normalizer/case.json +119 -0
- package/docs/use-cases/feature-tag-normalizer/expected/package.json +8 -0
- package/docs/use-cases/feature-tag-normalizer/expected/src/normalize-tags.mjs +14 -0
- package/docs/use-cases/feature-tag-normalizer/expected/test/normalize-tags.test.mjs +18 -0
- package/docs/workflows.md +6 -0
- package/opencode/AGENTS.md +6 -0
- package/opencode/agents/review_api.md +3 -1
- package/opencode/agents/review_coordinator.md +3 -1
- package/opencode/agents/review_quality.md +3 -1
- package/opencode/agents/review_security.md +3 -1
- package/opencode/agents/review_tests.md +3 -1
- package/opencode/agents/reviewer.md +2 -2
- package/opencode/commands/loop.md +53 -14
- package/opencode/docs/ai/evolution/README.md +28 -0
- package/opencode/docs/ai/evolution/benchmarks/adversarial-scenarios.jsonl +11 -0
- package/opencode/docs/ai/harness/README.md +2 -0
- package/opencode/docs/ai/harness/agents.md +30 -4
- package/opencode/docs/ai/harness/checks.md +27 -0
- package/opencode/docs/ai/harness/commands.md +21 -4
- package/opencode/docs/ai/harness/orchestrated-review.md +4 -0
- package/opencode/docs/ai/harness/orchestration-contracts.json +891 -0
- package/opencode/package-lock.json +14 -8
- package/opencode/package.json +1 -0
- package/opencode/scripts/adversarial-harness.test.mjs +296 -0
- package/opencode/scripts/check-harness.mjs +826 -11
- package/opencode/scripts/check-harness.test.mjs +572 -1
- package/opencode/scripts/loop-state.mjs +1149 -0
- package/opencode/scripts/loop-state.test.mjs +577 -0
- package/opencode/scripts/review-orchestrated-prepare.mjs +33 -9
- package/package.json +4 -1
- package/scripts/check-supply-chain.mjs +11 -1
- package/scripts/check-supply-chain.test.mjs +47 -0
- package/scripts/check-threat-model.mjs +421 -0
- package/scripts/check-threat-model.test.mjs +304 -0
- package/scripts/check-use-cases.mjs +501 -0
- package/scripts/check-use-cases.test.mjs +225 -0
- package/scripts/check.sh +51 -1
- package/scripts/install-smoke.sh +24 -1
- package/scripts/manage-installation.mjs +567 -25
- package/scripts/manage-installation.test.mjs +418 -0
- package/scripts/oak.mjs +200 -0
- package/scripts/oak.test.mjs +277 -0
- package/scripts/package-smoke.mjs +57 -0
- package/scripts/package-smoke.test.mjs +75 -2
- package/scripts/version.mjs +1 -0
- package/scripts/version.test.mjs +3 -2
- package/supply-chain.json +1 -0
|
@@ -24,7 +24,7 @@ const EXACT_KEYS = {
|
|
|
24
24
|
commit: ["commit"],
|
|
25
25
|
image: ["tag", "digest"],
|
|
26
26
|
version: ["version"],
|
|
27
|
-
overrides: ["@babel/core", "uuid"],
|
|
27
|
+
overrides: ["@babel/core", "brace-expansion", "uuid"],
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
const COMMIT = /^[0-9a-f]{40}$/;
|
|
@@ -115,6 +115,10 @@ export function validateSupplyChainData(data) {
|
|
|
115
115
|
assertStableVersion(data.external_refs.pnpm.version, "external_refs.pnpm.version");
|
|
116
116
|
assertStableVersion(data.external_refs.opencode_ai.version, "external_refs.opencode_ai.version");
|
|
117
117
|
assertStableVersion(data.npm_overrides["@babel/core"], "npm_overrides.@babel/core");
|
|
118
|
+
assertStableVersion(
|
|
119
|
+
data.npm_overrides["brace-expansion"],
|
|
120
|
+
"npm_overrides.brace-expansion",
|
|
121
|
+
);
|
|
118
122
|
assertStableVersion(data.npm_overrides.uuid, "npm_overrides.uuid");
|
|
119
123
|
return data;
|
|
120
124
|
}
|
|
@@ -187,6 +191,10 @@ function validatePackages(root, data, fsOps) {
|
|
|
187
191
|
if (JSON.stringify(rootPackage.files) !== JSON.stringify(PACKAGE_FILES)) {
|
|
188
192
|
throw invalid(`package.json files must be exactly: ${PACKAGE_FILES.join(", ")}`);
|
|
189
193
|
}
|
|
194
|
+
assertExactKeys(rootPackage.bin, ["oak"], "package.json bin");
|
|
195
|
+
if (rootPackage.bin.oak !== "scripts/oak.mjs") {
|
|
196
|
+
throw invalid("package.json bin.oak must be scripts/oak.mjs");
|
|
197
|
+
}
|
|
190
198
|
for (const [script, command] of [
|
|
191
199
|
["dependency-audit", "npm --prefix opencode audit --omit=dev --audit-level=low"],
|
|
192
200
|
["dependency-signature-audit", "npm --prefix opencode audit signatures"],
|
|
@@ -507,6 +515,7 @@ function canonicalDocumentationPinBlock(data) {
|
|
|
507
515
|
| pnpm | ${data.external_refs.pnpm.version} | exact version |
|
|
508
516
|
| opencode-ai | ${data.external_refs.opencode_ai.version} | exact version |
|
|
509
517
|
| @babel/core override | ${data.npm_overrides["@babel/core"]} | exact version |
|
|
518
|
+
| brace-expansion override | ${data.npm_overrides["brace-expansion"]} | exact version |
|
|
510
519
|
| uuid override | ${data.npm_overrides.uuid} | exact version |
|
|
511
520
|
<!-- supply-chain-pins:end -->`;
|
|
512
521
|
}
|
|
@@ -534,6 +543,7 @@ function validateDocumentation(root, data, fsOps) {
|
|
|
534
543
|
"| pnpm |",
|
|
535
544
|
"| opencode-ai |",
|
|
536
545
|
"| @babel/core override |",
|
|
546
|
+
"| brace-expansion override |",
|
|
537
547
|
"| uuid override |",
|
|
538
548
|
].map((row) => actualBlock.indexOf(row));
|
|
539
549
|
const rowsAreOrdered = rowPositions.every(
|
|
@@ -46,6 +46,7 @@ const VALID = {
|
|
|
46
46
|
},
|
|
47
47
|
npm_overrides: {
|
|
48
48
|
"@babel/core": "7.29.7",
|
|
49
|
+
"brace-expansion": "5.0.8",
|
|
49
50
|
uuid: "14.0.0",
|
|
50
51
|
},
|
|
51
52
|
};
|
|
@@ -91,6 +92,7 @@ function canonicalPolicy() {
|
|
|
91
92
|
| pnpm | ${VALID.external_refs.pnpm.version} | exact version |
|
|
92
93
|
| opencode-ai | ${VALID.external_refs.opencode_ai.version} | exact version |
|
|
93
94
|
| @babel/core override | ${VALID.npm_overrides["@babel/core"]} | exact version |
|
|
95
|
+
| brace-expansion override | ${VALID.npm_overrides["brace-expansion"]} | exact version |
|
|
94
96
|
| uuid override | ${VALID.npm_overrides.uuid} | exact version |
|
|
95
97
|
<!-- supply-chain-pins:end -->
|
|
96
98
|
|
|
@@ -156,6 +158,7 @@ function withSurfaceFixture(callback) {
|
|
|
156
158
|
fs.writeFileSync(path.join(root, "supply-chain.json"), `${JSON.stringify(VALID)}\n`);
|
|
157
159
|
fs.writeFileSync(path.join(root, "package.json"), `${JSON.stringify({
|
|
158
160
|
files: PACKAGE_FILES,
|
|
161
|
+
bin: { oak: "scripts/oak.mjs" },
|
|
159
162
|
scripts: {
|
|
160
163
|
"dependency-audit": "npm --prefix opencode audit --omit=dev --audit-level=low",
|
|
161
164
|
"dependency-signature-audit": "npm --prefix opencode audit signatures",
|
|
@@ -177,6 +180,11 @@ function withSurfaceFixture(callback) {
|
|
|
177
180
|
resolved: "https://registry.npmjs.org/@babel/core/-/core-7.29.7.tgz",
|
|
178
181
|
integrity: "sha512-babel",
|
|
179
182
|
},
|
|
183
|
+
"node_modules/brace-expansion": {
|
|
184
|
+
version: VALID.npm_overrides["brace-expansion"],
|
|
185
|
+
resolved: "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.8.tgz",
|
|
186
|
+
integrity: "sha512-braceexpansion",
|
|
187
|
+
},
|
|
180
188
|
"node_modules/uuid": {
|
|
181
189
|
version: VALID.npm_overrides.uuid,
|
|
182
190
|
resolved: "https://registry.npmjs.org/uuid/-/uuid-14.0.0.tgz",
|
|
@@ -211,6 +219,7 @@ function withCliFixture(inventory, callback) {
|
|
|
211
219
|
writeValidDocumentation(root);
|
|
212
220
|
fs.writeFileSync(path.join(root, "package.json"), `${JSON.stringify({
|
|
213
221
|
files: PACKAGE_FILES,
|
|
222
|
+
bin: { oak: "scripts/oak.mjs" },
|
|
214
223
|
scripts: {
|
|
215
224
|
"dependency-audit": "npm --prefix opencode audit --omit=dev --audit-level=low",
|
|
216
225
|
"dependency-signature-audit": "npm --prefix opencode audit signatures",
|
|
@@ -307,6 +316,10 @@ test("requires exact canonical versions without ranges", () => {
|
|
|
307
316
|
(data) => { data.npm_overrides["@babel/core"] = ">=7.29.7"; },
|
|
308
317
|
"npm_overrides.@babel/core",
|
|
309
318
|
);
|
|
319
|
+
expectInvalid(
|
|
320
|
+
(data) => { data.npm_overrides["brace-expansion"] = "^5.0.8"; },
|
|
321
|
+
"npm_overrides.brace-expansion",
|
|
322
|
+
);
|
|
310
323
|
expectInvalid(
|
|
311
324
|
(data) => { data.npm_overrides.uuid = "14.0"; },
|
|
312
325
|
"npm_overrides.uuid",
|
|
@@ -325,6 +338,23 @@ test("surface validation accepts exact overrides, regenerated lock entries, and
|
|
|
325
338
|
});
|
|
326
339
|
});
|
|
327
340
|
|
|
341
|
+
test("surface validation requires exactly the canonical oak bin", () => {
|
|
342
|
+
for (const [bin, expected] of [
|
|
343
|
+
[undefined, /package\.json bin/i],
|
|
344
|
+
[{ oak: "scripts/other.mjs" }, /bin\.oak/i],
|
|
345
|
+
[{ oak: "scripts/oak.mjs", other: "scripts/other.mjs" }, /keys must be exactly/i],
|
|
346
|
+
]) {
|
|
347
|
+
withSurfaceFixture((root) => {
|
|
348
|
+
const packagePath = path.join(root, "package.json");
|
|
349
|
+
const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
|
|
350
|
+
if (bin === undefined) delete packageJson.bin;
|
|
351
|
+
else packageJson.bin = bin;
|
|
352
|
+
fs.writeFileSync(packagePath, `${JSON.stringify(packageJson)}\n`);
|
|
353
|
+
assert.throws(() => checkSupplyChain(root), expected);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
|
|
328
358
|
test("surface validation requires the exact Superpowers commit", () => {
|
|
329
359
|
withSurfaceFixture((root) => {
|
|
330
360
|
const relative = "opencode/opencode.json";
|
|
@@ -549,6 +579,7 @@ for (const [label, current, replacement] of [
|
|
|
549
579
|
["pnpm", `| pnpm | ${VALID.external_refs.pnpm.version} |`, "| pnpm | DRIFTED |"],
|
|
550
580
|
["opencode-ai", `| opencode-ai | ${VALID.external_refs.opencode_ai.version} |`, "| opencode-ai | DRIFTED |"],
|
|
551
581
|
["Babel override", `| @babel/core override | ${VALID.npm_overrides["@babel/core"]} |`, "| @babel/core override | DRIFTED |"],
|
|
582
|
+
["brace-expansion override", `| brace-expansion override | ${VALID.npm_overrides["brace-expansion"]} |`, "| brace-expansion override | DRIFTED |"],
|
|
552
583
|
["uuid override", `| uuid override | ${VALID.npm_overrides.uuid} |`, "| uuid override | DRIFTED |"],
|
|
553
584
|
]) {
|
|
554
585
|
test(`documentation validation rejects a drifted ${label} pin`, () => {
|
|
@@ -646,6 +677,9 @@ for (const [dependency, mutation] of [
|
|
|
646
677
|
["@babel/core", (packageData) => { delete packageData.overrides["@babel/core"]; }],
|
|
647
678
|
["@babel/core range", (packageData) => { packageData.overrides["@babel/core"] = "^7.29.7"; }],
|
|
648
679
|
["@babel/core wrong version", (packageData) => { packageData.overrides["@babel/core"] = "7.29.6"; }],
|
|
680
|
+
["brace-expansion", (packageData) => { delete packageData.overrides["brace-expansion"]; }],
|
|
681
|
+
["brace-expansion range", (packageData) => { packageData.overrides["brace-expansion"] = "^5.0.8"; }],
|
|
682
|
+
["brace-expansion wrong version", (packageData) => { packageData.overrides["brace-expansion"] = "5.0.7"; }],
|
|
649
683
|
["uuid", (packageData) => { delete packageData.overrides.uuid; }],
|
|
650
684
|
["uuid range", (packageData) => { packageData.overrides.uuid = "^14.0.0"; }],
|
|
651
685
|
["uuid wrong version", (packageData) => { packageData.overrides.uuid = "13.0.0"; }],
|
|
@@ -671,6 +705,19 @@ test("surface validation rejects a lockfile not regenerated to the Babel overrid
|
|
|
671
705
|
});
|
|
672
706
|
});
|
|
673
707
|
|
|
708
|
+
test("surface validation rejects a lockfile not regenerated to the brace-expansion override", () => {
|
|
709
|
+
withSurfaceFixture((root) => {
|
|
710
|
+
const relative = "opencode/package-lock.json";
|
|
711
|
+
const lock = JSON.parse(fs.readFileSync(path.join(root, relative), "utf8"));
|
|
712
|
+
lock.packages["node_modules/brace-expansion"].version = "5.0.7";
|
|
713
|
+
fs.writeFileSync(path.join(root, relative), `${JSON.stringify(lock)}\n`);
|
|
714
|
+
assert.throws(
|
|
715
|
+
() => checkSupplyChain(root),
|
|
716
|
+
/package-lock\.json brace-expansion must resolve to 5\.0\.8/,
|
|
717
|
+
);
|
|
718
|
+
});
|
|
719
|
+
});
|
|
720
|
+
|
|
674
721
|
for (const [label, mutate, message] of [
|
|
675
722
|
["missing resolved", (entry) => { delete entry.resolved; }, /node_modules\/example.*canonical npm registry resolved URL/i],
|
|
676
723
|
["non-registry resolved", (entry) => { entry.resolved = "https://example.invalid/example.tgz"; }, /node_modules\/example.*canonical npm registry resolved URL/i],
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const SCRIPT_PATH = fileURLToPath(import.meta.url);
|
|
8
|
+
const ROOT = path.dirname(path.dirname(SCRIPT_PATH));
|
|
9
|
+
const MODEL_PATH = path.join(ROOT, "docs/threat-model.md");
|
|
10
|
+
const AGENTS_PATH = path.join(ROOT, "opencode/agents");
|
|
11
|
+
const SCENARIOS_PATH = path.join(
|
|
12
|
+
ROOT,
|
|
13
|
+
"opencode/docs/ai/evolution/benchmarks/adversarial-scenarios.jsonl",
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const REQUIRED_SECTIONS = [
|
|
17
|
+
"Scope and security objectives",
|
|
18
|
+
"Actors",
|
|
19
|
+
"Assets",
|
|
20
|
+
"Trust boundaries",
|
|
21
|
+
"Agent capabilities",
|
|
22
|
+
"External surfaces",
|
|
23
|
+
"Persistence surfaces",
|
|
24
|
+
"Data flows",
|
|
25
|
+
"Risk register",
|
|
26
|
+
"Adversarial traceability",
|
|
27
|
+
"Residual risks and assumptions",
|
|
28
|
+
"Private and public boundary",
|
|
29
|
+
];
|
|
30
|
+
const SEVERITIES = new Set(["high", "medium", "low"]);
|
|
31
|
+
const STATUSES = new Set([
|
|
32
|
+
"mitigated",
|
|
33
|
+
"partially-mitigated",
|
|
34
|
+
"accepted",
|
|
35
|
+
"out-of-scope",
|
|
36
|
+
]);
|
|
37
|
+
const EVIDENCE_PREFIXES = ["test:", "check:", "limitation:"];
|
|
38
|
+
const PRIVATE_MARKERS = [
|
|
39
|
+
["", "Users", ""].join("/"),
|
|
40
|
+
[".config", "opencode"].join("/"),
|
|
41
|
+
["auth", "json"].join("."),
|
|
42
|
+
["OPENAI", "API", "KEY"].join("_"),
|
|
43
|
+
["synology", "me"].join("."),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const TABLES = {
|
|
47
|
+
Actors: ["ID", "Actor", "Trust", "Capabilities"],
|
|
48
|
+
Assets: ["ID", "Asset", "Protection"],
|
|
49
|
+
"Trust boundaries": ["ID", "Boundary", "Flow"],
|
|
50
|
+
"Agent capabilities": [
|
|
51
|
+
"Agent",
|
|
52
|
+
"Edit",
|
|
53
|
+
"Shell",
|
|
54
|
+
"Network",
|
|
55
|
+
"External directory",
|
|
56
|
+
"Delegation",
|
|
57
|
+
"Role",
|
|
58
|
+
"Source",
|
|
59
|
+
],
|
|
60
|
+
"External surfaces": ["Surface", "Boundary", "Controls or limitations"],
|
|
61
|
+
"Persistence surfaces": [
|
|
62
|
+
"Surface",
|
|
63
|
+
"Owner",
|
|
64
|
+
"Sensitivity",
|
|
65
|
+
"Lifecycle",
|
|
66
|
+
"Controls or limitations",
|
|
67
|
+
],
|
|
68
|
+
"Risk register": [
|
|
69
|
+
"ID",
|
|
70
|
+
"Title",
|
|
71
|
+
"Actor or input",
|
|
72
|
+
"Assets",
|
|
73
|
+
"Boundaries",
|
|
74
|
+
"Abuse",
|
|
75
|
+
"Severity",
|
|
76
|
+
"Controls",
|
|
77
|
+
"Evidence",
|
|
78
|
+
"Status",
|
|
79
|
+
"Residual",
|
|
80
|
+
"Authority",
|
|
81
|
+
"Review trigger",
|
|
82
|
+
],
|
|
83
|
+
"Adversarial traceability": ["Scenario ID", "Risk ID", "Evidence"],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function modelError(message) {
|
|
87
|
+
const error = new Error(message);
|
|
88
|
+
error.code = "INVALID_THREAT_MODEL";
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function cleanCell(value) {
|
|
93
|
+
const trimmed = value.trim();
|
|
94
|
+
const match = trimmed.match(/^`([^`]+)`$/);
|
|
95
|
+
return match ? match[1] : trimmed;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function parseTableLine(line) {
|
|
99
|
+
const trimmed = line.trim();
|
|
100
|
+
if (!trimmed.startsWith("|") || !trimmed.endsWith("|")) {
|
|
101
|
+
throw modelError("Markdown table rows must start and end with |");
|
|
102
|
+
}
|
|
103
|
+
return trimmed
|
|
104
|
+
.slice(1, -1)
|
|
105
|
+
.split("|")
|
|
106
|
+
.map(cleanCell);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function parseMarkdownTable(text, heading, expectedHeaders) {
|
|
110
|
+
if (typeof text !== "string") throw modelError("threat model must be text");
|
|
111
|
+
const lines = text.split(/\r?\n/);
|
|
112
|
+
const headingLine = `## ${heading}`;
|
|
113
|
+
const headingIndex = lines.findIndex((line) => line.trim() === headingLine);
|
|
114
|
+
if (headingIndex === -1) throw modelError(`missing section: ${heading}`);
|
|
115
|
+
|
|
116
|
+
let tableIndex = -1;
|
|
117
|
+
for (let index = headingIndex + 1; index < lines.length; index += 1) {
|
|
118
|
+
const line = lines[index].trim();
|
|
119
|
+
if (line.startsWith("## ")) break;
|
|
120
|
+
if (line.startsWith("|")) {
|
|
121
|
+
tableIndex = index;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (tableIndex === -1) throw modelError(`${heading}: missing Markdown table`);
|
|
126
|
+
|
|
127
|
+
const headers = parseTableLine(lines[tableIndex]);
|
|
128
|
+
if (JSON.stringify(headers) !== JSON.stringify(expectedHeaders)) {
|
|
129
|
+
throw modelError(
|
|
130
|
+
`${heading}: expected headers ${expectedHeaders.join(", ")}`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const separator = parseTableLine(lines[tableIndex + 1] ?? "");
|
|
134
|
+
if (
|
|
135
|
+
separator.length !== headers.length
|
|
136
|
+
|| separator.some((cell) => !/^:?-{3,}:?$/.test(cell))
|
|
137
|
+
) {
|
|
138
|
+
throw modelError(`${heading}: invalid Markdown table separator`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const rows = [];
|
|
142
|
+
for (let index = tableIndex + 2; index < lines.length; index += 1) {
|
|
143
|
+
if (!lines[index].trim().startsWith("|")) break;
|
|
144
|
+
const cells = parseTableLine(lines[index]);
|
|
145
|
+
if (cells.length !== headers.length) {
|
|
146
|
+
throw modelError(`${heading}: row ${rows.length + 1} has wrong column count`);
|
|
147
|
+
}
|
|
148
|
+
rows.push(Object.fromEntries(headers.map((header, cell) => [
|
|
149
|
+
header,
|
|
150
|
+
cells[cell],
|
|
151
|
+
])));
|
|
152
|
+
}
|
|
153
|
+
if (rows.length === 0) throw modelError(`${heading}: table must not be empty`);
|
|
154
|
+
return rows;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function splitCell(value, label) {
|
|
158
|
+
const values = value.split("<br>").map(cleanCell);
|
|
159
|
+
if (values.some((item) => item === "")) {
|
|
160
|
+
throw modelError(`${label}: empty list item`);
|
|
161
|
+
}
|
|
162
|
+
return values;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function requireNonEmpty(row, fields, id) {
|
|
166
|
+
for (const field of fields) {
|
|
167
|
+
if (typeof row[field] !== "string" || row[field].trim() === "") {
|
|
168
|
+
throw modelError(`${id}: ${field} must not be empty`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function requireExactSet(actual, expected, label) {
|
|
174
|
+
const duplicates = actual.filter((value, index) => actual.indexOf(value) !== index);
|
|
175
|
+
if (duplicates.length > 0) {
|
|
176
|
+
throw modelError(`${label}: duplicate ${duplicates[0]}`);
|
|
177
|
+
}
|
|
178
|
+
const actualSet = new Set(actual);
|
|
179
|
+
const expectedSet = new Set(expected);
|
|
180
|
+
const missing = [...expectedSet].filter((value) => !actualSet.has(value));
|
|
181
|
+
const extra = [...actualSet].filter((value) => !expectedSet.has(value));
|
|
182
|
+
if (missing.length > 0 || extra.length > 0) {
|
|
183
|
+
throw modelError(
|
|
184
|
+
`${label}: inventory mismatch; missing=${missing.join(",") || "none"}; extra=${extra.join(",") || "none"}`,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function requiredRange(prefix, count) {
|
|
190
|
+
return Array.from(
|
|
191
|
+
{ length: count },
|
|
192
|
+
(_, index) => `${prefix}-${String(index + 1).padStart(3, "0")}`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function requireIds(rows, pattern, required, label) {
|
|
197
|
+
const ids = rows.map((row) => row.ID);
|
|
198
|
+
for (const id of ids) {
|
|
199
|
+
if (!pattern.test(id)) throw modelError(`${label}: invalid ID ${id}`);
|
|
200
|
+
}
|
|
201
|
+
const duplicates = ids.filter((id, index) => ids.indexOf(id) !== index);
|
|
202
|
+
if (duplicates.length > 0) {
|
|
203
|
+
throw modelError(`${label}: duplicate ${duplicates[0]}`);
|
|
204
|
+
}
|
|
205
|
+
const missing = required.filter((id) => !ids.includes(id));
|
|
206
|
+
if (missing.length > 0) {
|
|
207
|
+
throw modelError(`${label}: missing required IDs ${missing.join(", ")}`);
|
|
208
|
+
}
|
|
209
|
+
return new Set(ids);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function requireKnownReferences(value, known, label) {
|
|
213
|
+
for (const reference of splitCell(value, label)) {
|
|
214
|
+
if (!known.has(reference)) {
|
|
215
|
+
throw modelError(`${label}: unknown reference ${reference}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function requireEvidence(value, label) {
|
|
221
|
+
for (const evidence of splitCell(value, label)) {
|
|
222
|
+
if (!EVIDENCE_PREFIXES.some((prefix) => evidence.startsWith(prefix))) {
|
|
223
|
+
throw modelError(
|
|
224
|
+
`${label}: evidence must start with test:, check:, or limitation:`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function validateThreatModel(text, { agentIds, scenarioIds }) {
|
|
231
|
+
if (!Array.isArray(agentIds) || !Array.isArray(scenarioIds)) {
|
|
232
|
+
throw modelError("agentIds and scenarioIds must be arrays");
|
|
233
|
+
}
|
|
234
|
+
for (const section of REQUIRED_SECTIONS) {
|
|
235
|
+
const matches = text
|
|
236
|
+
.split(/\r?\n/)
|
|
237
|
+
.filter((line) => line.trim() === `## ${section}`);
|
|
238
|
+
if (matches.length !== 1) {
|
|
239
|
+
throw modelError(
|
|
240
|
+
`${section}: required exactly once, found ${matches.length}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
for (const marker of PRIVATE_MARKERS) {
|
|
245
|
+
if (text.includes(marker)) {
|
|
246
|
+
throw modelError(`private marker is forbidden in threat model: ${marker}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const tables = Object.fromEntries(
|
|
251
|
+
Object.entries(TABLES).map(([heading, headers]) => [
|
|
252
|
+
heading,
|
|
253
|
+
parseMarkdownTable(text, heading, headers),
|
|
254
|
+
]),
|
|
255
|
+
);
|
|
256
|
+
const actorIds = requireIds(
|
|
257
|
+
tables.Actors,
|
|
258
|
+
/^AC-\d{3}$/,
|
|
259
|
+
requiredRange("AC", 6),
|
|
260
|
+
"actors",
|
|
261
|
+
);
|
|
262
|
+
const assetIds = requireIds(
|
|
263
|
+
tables.Assets,
|
|
264
|
+
/^A-\d{3}$/,
|
|
265
|
+
requiredRange("A", 10),
|
|
266
|
+
"assets",
|
|
267
|
+
);
|
|
268
|
+
const boundaryIds = requireIds(
|
|
269
|
+
tables["Trust boundaries"],
|
|
270
|
+
/^TB-\d{3}$/,
|
|
271
|
+
requiredRange("TB", 9),
|
|
272
|
+
"trust boundaries",
|
|
273
|
+
);
|
|
274
|
+
const riskIds = requireIds(
|
|
275
|
+
tables["Risk register"],
|
|
276
|
+
/^TM-\d{3}$/,
|
|
277
|
+
requiredRange("TM", 20),
|
|
278
|
+
"risks",
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
for (const row of tables.Actors) {
|
|
282
|
+
requireNonEmpty(row, TABLES.Actors, row.ID);
|
|
283
|
+
}
|
|
284
|
+
for (const row of tables.Assets) {
|
|
285
|
+
requireNonEmpty(row, TABLES.Assets, row.ID);
|
|
286
|
+
}
|
|
287
|
+
for (const row of tables["Trust boundaries"]) {
|
|
288
|
+
requireNonEmpty(row, TABLES["Trust boundaries"], row.ID);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const documentedAgents = tables["Agent capabilities"].map((row) => row.Agent);
|
|
292
|
+
requireExactSet(documentedAgents, agentIds, "agents");
|
|
293
|
+
for (const row of tables["Agent capabilities"]) {
|
|
294
|
+
requireNonEmpty(row, TABLES["Agent capabilities"], row.Agent);
|
|
295
|
+
if (row.Source !== `opencode/agents/${row.Agent}.md`) {
|
|
296
|
+
throw modelError(`${row.Agent}: source must point to its public agent file`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (const row of tables["External surfaces"]) {
|
|
301
|
+
requireNonEmpty(row, TABLES["External surfaces"], row.Surface);
|
|
302
|
+
requireKnownReferences(
|
|
303
|
+
row.Boundary,
|
|
304
|
+
boundaryIds,
|
|
305
|
+
`${row.Surface} boundary`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
for (const row of tables["Persistence surfaces"]) {
|
|
309
|
+
requireNonEmpty(row, TABLES["Persistence surfaces"], row.Surface);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
for (const row of tables["Risk register"]) {
|
|
313
|
+
requireNonEmpty(row, TABLES["Risk register"], row.ID);
|
|
314
|
+
requireKnownReferences(
|
|
315
|
+
row["Actor or input"],
|
|
316
|
+
actorIds,
|
|
317
|
+
`${row.ID} actor or input`,
|
|
318
|
+
);
|
|
319
|
+
requireKnownReferences(row.Assets, assetIds, `${row.ID} assets`);
|
|
320
|
+
requireKnownReferences(row.Boundaries, boundaryIds, `${row.ID} boundaries`);
|
|
321
|
+
if (!SEVERITIES.has(row.Severity)) {
|
|
322
|
+
throw modelError(`${row.ID}: invalid severity ${row.Severity}`);
|
|
323
|
+
}
|
|
324
|
+
if (!STATUSES.has(row.Status)) {
|
|
325
|
+
throw modelError(`${row.ID}: invalid status ${row.Status}`);
|
|
326
|
+
}
|
|
327
|
+
requireEvidence(row.Evidence, `${row.ID} evidence`);
|
|
328
|
+
if (
|
|
329
|
+
row.Status === "accepted"
|
|
330
|
+
&& (
|
|
331
|
+
row.Authority !== "maintainer"
|
|
332
|
+
|| row["Review trigger"].toLowerCase() === "none"
|
|
333
|
+
)
|
|
334
|
+
) {
|
|
335
|
+
throw modelError(
|
|
336
|
+
`${row.ID}: accepted risk requires maintainer authority and a review trigger`,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const traceRows = tables["Adversarial traceability"];
|
|
342
|
+
const documentedScenarios = traceRows.map((row) => row["Scenario ID"]);
|
|
343
|
+
requireExactSet(documentedScenarios, scenarioIds, "scenarios");
|
|
344
|
+
for (const row of traceRows) {
|
|
345
|
+
requireNonEmpty(row, TABLES["Adversarial traceability"], row["Scenario ID"]);
|
|
346
|
+
if (!riskIds.has(row["Risk ID"])) {
|
|
347
|
+
throw modelError(
|
|
348
|
+
`${row["Scenario ID"]}: unknown risk ${row["Risk ID"]}`,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
requireEvidence(row.Evidence, `${row["Scenario ID"]} evidence`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
actors: tables.Actors.length,
|
|
356
|
+
assets: tables.Assets.length,
|
|
357
|
+
boundaries: tables["Trust boundaries"].length,
|
|
358
|
+
agents: documentedAgents.length,
|
|
359
|
+
risks: tables["Risk register"].length,
|
|
360
|
+
scenarios: documentedScenarios.length,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function readRegularFile(file, label) {
|
|
365
|
+
let stat;
|
|
366
|
+
try {
|
|
367
|
+
stat = fs.lstatSync(file);
|
|
368
|
+
} catch {
|
|
369
|
+
throw modelError(`${label} is missing`);
|
|
370
|
+
}
|
|
371
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
372
|
+
throw modelError(`${label} must be a regular non-symlink file`);
|
|
373
|
+
}
|
|
374
|
+
return fs.readFileSync(file, "utf8");
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function loadScenarioIds() {
|
|
378
|
+
const text = readRegularFile(SCENARIOS_PATH, "adversarial scenario corpus");
|
|
379
|
+
if (text.trim() === "") throw modelError("adversarial scenario corpus is empty");
|
|
380
|
+
return text.trim().split("\n").map((line, index) => {
|
|
381
|
+
let scenario;
|
|
382
|
+
try {
|
|
383
|
+
scenario = JSON.parse(line);
|
|
384
|
+
} catch {
|
|
385
|
+
throw modelError(`adversarial scenario line ${index + 1} is invalid JSON`);
|
|
386
|
+
}
|
|
387
|
+
if (typeof scenario.id !== "string" || scenario.id === "") {
|
|
388
|
+
throw modelError(`adversarial scenario line ${index + 1} is missing id`);
|
|
389
|
+
}
|
|
390
|
+
return scenario.id;
|
|
391
|
+
}).sort();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function loadAgentIds() {
|
|
395
|
+
return fs.readdirSync(AGENTS_PATH, { withFileTypes: true })
|
|
396
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
|
|
397
|
+
.map((entry) => path.basename(entry.name, ".md"))
|
|
398
|
+
.sort();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function runCli() {
|
|
402
|
+
const summary = validateThreatModel(
|
|
403
|
+
readRegularFile(MODEL_PATH, "canonical threat model"),
|
|
404
|
+
{
|
|
405
|
+
agentIds: loadAgentIds(),
|
|
406
|
+
scenarioIds: loadScenarioIds(),
|
|
407
|
+
},
|
|
408
|
+
);
|
|
409
|
+
console.log(
|
|
410
|
+
`Threat model check passed: ${summary.actors} actors, ${summary.assets} assets, ${summary.boundaries} boundaries, ${summary.agents} agents, ${summary.risks} risks, ${summary.scenarios} adversarial scenarios.`,
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === SCRIPT_PATH) {
|
|
415
|
+
try {
|
|
416
|
+
runCli();
|
|
417
|
+
} catch (error) {
|
|
418
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
419
|
+
process.exitCode = 1;
|
|
420
|
+
}
|
|
421
|
+
}
|