@nimiplatform/nimi-coding 0.2.8 → 0.3.1

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.
Files changed (206) hide show
  1. package/CHANGELOG.md +10 -90
  2. package/CONTRIBUTING.md +5 -34
  3. package/README.md +47 -350
  4. package/README.zh-CN.md +33 -287
  5. package/cli/commands/start.mjs +48 -688
  6. package/cli/commands/validate-spec-audit.mjs +1 -5
  7. package/cli/commands/validate-spec-governance.mjs +20 -0
  8. package/cli/constants.mjs +9 -417
  9. package/cli/help.mjs +28 -145
  10. package/cli/index.mjs +5 -33
  11. package/cli/lib/blueprint-audit.mjs +5 -12
  12. package/cli/lib/bootstrap.mjs +0 -9
  13. package/cli/lib/contracts.mjs +0 -163
  14. package/cli/lib/doctor.mjs +155 -10
  15. package/cli/lib/entrypoints.mjs +63 -167
  16. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  17. package/cli/lib/internal/contracts-parse.mjs +84 -520
  18. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  19. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  20. package/cli/lib/internal/validators-shared.mjs +0 -16
  21. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  22. package/cli/lib/internal/validators-spec.mjs +103 -342
  23. package/cli/lib/shared.mjs +1 -42
  24. package/cli/lib/validators.mjs +0 -48
  25. package/cli/seeds/seed-policy.yaml +0 -3
  26. package/config/bootstrap.yaml +4 -4
  27. package/config/spec-generation-inputs.yaml +6 -12
  28. package/contracts/domain-admission.schema.yaml +3 -3
  29. package/contracts/migration-inventory.schema.yaml +12 -75
  30. package/contracts/negative-fixtures.yaml +19 -99
  31. package/contracts/placement-contract.schema.yaml +49 -78
  32. package/contracts/projection-edge.schema.yaml +37 -121
  33. package/contracts/shared-enums.yaml +4 -8
  34. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  35. package/contracts/spec-layout.schema.yaml +32 -0
  36. package/contracts/surface-taxonomy.schema.yaml +43 -94
  37. package/contracts/table-family.schema.yaml +1 -1
  38. package/contracts/tracked-output-admission.schema.yaml +13 -66
  39. package/methodology/core.yaml +33 -25
  40. package/methodology/four-closure-policy.yaml +18 -26
  41. package/methodology/role-separation-policy.yaml +21 -27
  42. package/methodology/spec-reconstruction.yaml +17 -34
  43. package/package.json +2 -4
  44. package/spec/_meta/spec-tree-model.yaml +5 -101
  45. package/spec/product-scope.yaml +25 -57
  46. package/adapters/README.md +0 -25
  47. package/adapters/claude/README.md +0 -89
  48. package/adapters/claude/profile.yaml +0 -70
  49. package/adapters/codex/README.md +0 -53
  50. package/adapters/codex/profile.yaml +0 -78
  51. package/adapters/oh-my-codex/README.md +0 -184
  52. package/adapters/oh-my-codex/profile.yaml +0 -46
  53. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  54. package/cli/commands/audit-sweep.mjs +0 -364
  55. package/cli/commands/closeout.mjs +0 -186
  56. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  57. package/cli/commands/handoff.mjs +0 -123
  58. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  59. package/cli/commands/review-high-risk-execution.mjs +0 -95
  60. package/cli/commands/sweep-design.mjs +0 -295
  61. package/cli/commands/sweep.mjs +0 -22
  62. package/cli/commands/topic-formatters.mjs +0 -382
  63. package/cli/commands/topic-goal.mjs +0 -33
  64. package/cli/commands/topic-options-shared.mjs +0 -27
  65. package/cli/commands/topic-options-workflow.mjs +0 -767
  66. package/cli/commands/topic-options.mjs +0 -626
  67. package/cli/commands/topic-runner.mjs +0 -169
  68. package/cli/commands/topic.mjs +0 -795
  69. package/cli/commands/validate-acceptance.mjs +0 -5
  70. package/cli/commands/validate-execution-packet.mjs +0 -5
  71. package/cli/commands/validate-orchestration-state.mjs +0 -5
  72. package/cli/commands/validate-prompt.mjs +0 -5
  73. package/cli/commands/validate-worker-output.mjs +0 -5
  74. package/cli/lib/adapter-profiles.mjs +0 -403
  75. package/cli/lib/audit-execution.mjs +0 -52
  76. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  77. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  78. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  79. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -658
  80. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  81. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  82. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -527
  83. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  84. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  85. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  86. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  87. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  88. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  89. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  90. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  91. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  92. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  93. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  94. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  95. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  96. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  97. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  98. package/cli/lib/audit-sweep.mjs +0 -19
  99. package/cli/lib/authority-convergence.mjs +0 -720
  100. package/cli/lib/closeout.mjs +0 -732
  101. package/cli/lib/codex-sdk-runner.mjs +0 -76
  102. package/cli/lib/external-execution.mjs +0 -101
  103. package/cli/lib/handoff.mjs +0 -812
  104. package/cli/lib/high-risk-admission.mjs +0 -512
  105. package/cli/lib/high-risk-decision.mjs +0 -353
  106. package/cli/lib/high-risk-ingest.mjs +0 -321
  107. package/cli/lib/high-risk-review.mjs +0 -267
  108. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  109. package/cli/lib/internal/contracts-validators.mjs +0 -399
  110. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  111. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  112. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  113. package/cli/lib/internal/doctor-format.mjs +0 -286
  114. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  115. package/cli/lib/internal/doctor-state.mjs +0 -205
  116. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  117. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  118. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  119. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  120. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  121. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  122. package/cli/lib/sweep-design.mjs +0 -8
  123. package/cli/lib/topic-artifacts.mjs +0 -186
  124. package/cli/lib/topic-authority-coverage.mjs +0 -73
  125. package/cli/lib/topic-closeout.mjs +0 -560
  126. package/cli/lib/topic-common.mjs +0 -404
  127. package/cli/lib/topic-decisions.mjs +0 -332
  128. package/cli/lib/topic-draft-packets.mjs +0 -167
  129. package/cli/lib/topic-execution.mjs +0 -533
  130. package/cli/lib/topic-goal.mjs +0 -440
  131. package/cli/lib/topic-ledger.mjs +0 -281
  132. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  133. package/cli/lib/topic-root-validation.mjs +0 -288
  134. package/cli/lib/topic-runner-commands.mjs +0 -174
  135. package/cli/lib/topic-runner-deferral.mjs +0 -532
  136. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  137. package/cli/lib/topic-runner-validation.mjs +0 -138
  138. package/cli/lib/topic-runner.mjs +0 -727
  139. package/cli/lib/topic-scaffold.mjs +0 -252
  140. package/cli/lib/topic-waves.mjs +0 -403
  141. package/cli/lib/topic.mjs +0 -81
  142. package/config/audit-execution-artifacts.yaml +0 -20
  143. package/config/external-execution-artifacts.yaml +0 -16
  144. package/config/host-adapter.yaml +0 -30
  145. package/config/host-profile.yaml +0 -29
  146. package/config/installer-evidence.yaml +0 -31
  147. package/config/skill-installer.yaml +0 -23
  148. package/config/skill-manifest.yaml +0 -48
  149. package/config/skills.yaml +0 -30
  150. package/contracts/acceptance.schema.yaml +0 -16
  151. package/contracts/admission-checklist.schema.yaml +0 -15
  152. package/contracts/audit-chunk.schema.yaml +0 -123
  153. package/contracts/audit-closeout.schema.yaml +0 -51
  154. package/contracts/audit-finding.schema.yaml +0 -61
  155. package/contracts/audit-ledger.schema.yaml +0 -138
  156. package/contracts/audit-plan.schema.yaml +0 -138
  157. package/contracts/audit-remediation-map.schema.yaml +0 -52
  158. package/contracts/audit-rerun.schema.yaml +0 -31
  159. package/contracts/audit-sweep-result.yaml +0 -53
  160. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  161. package/contracts/closeout.schema.yaml +0 -25
  162. package/contracts/decision-review.schema.yaml +0 -16
  163. package/contracts/doc-spec-audit-result.yaml +0 -19
  164. package/contracts/execution-packet.schema.yaml +0 -49
  165. package/contracts/external-host-compatibility.yaml +0 -22
  166. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  167. package/contracts/high-risk-admission.schema.yaml +0 -23
  168. package/contracts/high-risk-execution-result.yaml +0 -20
  169. package/contracts/orchestration-state.schema.yaml +0 -41
  170. package/contracts/overflow-continuation.schema.yaml +0 -12
  171. package/contracts/packet.schema.yaml +0 -30
  172. package/contracts/pending-note.schema.yaml +0 -17
  173. package/contracts/prompt.schema.yaml +0 -12
  174. package/contracts/remediation.schema.yaml +0 -16
  175. package/contracts/result.schema.yaml +0 -24
  176. package/contracts/spec-reconstruction-result.yaml +0 -41
  177. package/contracts/sweep-design-result.yaml +0 -349
  178. package/contracts/topic-goal.schema.yaml +0 -87
  179. package/contracts/topic-run-ledger.schema.yaml +0 -72
  180. package/contracts/topic-step-decision.schema.yaml +0 -45
  181. package/contracts/topic.schema.yaml +0 -65
  182. package/contracts/true-close.schema.yaml +0 -15
  183. package/contracts/wave.schema.yaml +0 -29
  184. package/contracts/worker-output.schema.yaml +0 -15
  185. package/contracts/workflow-consumer.schema.yaml +0 -110
  186. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  187. package/methodology/authority-convergence-policy.yaml +0 -42
  188. package/methodology/overflow-continuation-policy.yaml +0 -14
  189. package/methodology/skill-exchange-projection.yaml +0 -114
  190. package/methodology/skill-handoff.yaml +0 -34
  191. package/methodology/skill-installer-result.yaml +0 -27
  192. package/methodology/skill-installer-summary-projection.yaml +0 -181
  193. package/methodology/skill-runtime.yaml +0 -23
  194. package/methodology/spec-target-truth-profile.yaml +0 -46
  195. package/methodology/topic-lifecycle-report.yaml +0 -144
  196. package/methodology/topic-lifecycle.yaml +0 -37
  197. package/methodology/topic-naming-ontology.yaml +0 -21
  198. package/methodology/topic-ontology.yaml +0 -38
  199. package/methodology/topic-validation-policy.yaml +0 -9
  200. package/methodology/wave-dag-policy.yaml +0 -14
  201. package/spec/_meta/command-gating-matrix.yaml +0 -143
  202. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  203. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  204. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  205. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  206. package/spec/bootstrap-state.yaml +0 -99
@@ -1,697 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { existsSync, readFileSync } from "node:fs";
3
- import path from "node:path";
4
-
5
- import {
6
- appendRunEvent,
7
- chunkRef,
8
- ensureIsoTimestamp,
9
- inputError,
10
- loadChunk,
11
- loadPlan,
12
- packetRef,
13
- safeSweepId,
14
- withAuditSweepMutationLock,
15
- writeYamlRef,
16
- } from "./common.mjs";
17
- import { buildP0P1RecallProfile, criteriaEnableP0P1Recall } from "./p0p1-profile.mjs";
18
- import { budgetBlockForChunk } from "./risk-budget.mjs";
19
-
20
- const HIGH_RISK_TERMS = new Set([
21
- "auth",
22
- "authn",
23
- "authz",
24
- "jwt",
25
- "token",
26
- "session",
27
- "security",
28
- "permission",
29
- "capability",
30
- "delegation",
31
- "approval",
32
- "boundary",
33
- "bridge",
34
- "ipc",
35
- "runtime",
36
- "sdk",
37
- "hook",
38
- "destructive",
39
- "provider",
40
- "model",
41
- "secret",
42
- "revocation",
43
- "firewall",
44
- ]);
45
-
46
- const GENERATED_TERMS = new Set([
47
- "generated",
48
- "codegen",
49
- "table",
50
- "tables",
51
- "index",
52
- "registry",
53
- "manifest",
54
- ]);
55
-
56
- const STOP_WORDS = new Set([
57
- "the",
58
- "and",
59
- "for",
60
- "with",
61
- "from",
62
- "into",
63
- "contract",
64
- "kernel",
65
- "spec",
66
- "nimi",
67
- "desktop",
68
- "runtime",
69
- "source",
70
- ]);
71
-
72
- export function updatePlanChunk(plan, chunkId, patch) {
73
- return {
74
- ...plan,
75
- chunks: plan.chunks.map((chunk) => chunk.chunk_id === chunkId ? { ...chunk, ...patch } : chunk),
76
- };
77
- }
78
-
79
- function uniqueSorted(values) {
80
- return [...new Set((values ?? []).filter((value) => typeof value === "string" && value.trim()).map((value) => value.replace(/\\/g, "/")))].sort();
81
- }
82
-
83
- function sha256Json(value) {
84
- return createHash("sha256").update(JSON.stringify(value)).digest("hex");
85
- }
86
-
87
- function tokenize(value) {
88
- return String(value ?? "")
89
- .toLowerCase()
90
- .split(/[^a-z0-9]+/u)
91
- .filter((token) => token.length >= 3 && !STOP_WORDS.has(token));
92
- }
93
-
94
- function readProjectFileText(projectRoot, fileRef, byteLimit = 65536) {
95
- if (!projectRoot || typeof fileRef !== "string") {
96
- return "";
97
- }
98
- const absolutePath = path.resolve(projectRoot, fileRef);
99
- if (!absolutePath.startsWith(path.resolve(projectRoot) + path.sep) && absolutePath !== path.resolve(projectRoot)) {
100
- return "";
101
- }
102
- if (!existsSync(absolutePath)) {
103
- return "";
104
- }
105
- try {
106
- return readFileSync(absolutePath, "utf8").slice(0, byteLimit);
107
- } catch {
108
- return "";
109
- }
110
- }
111
-
112
- function adaptiveDepthForChunk(chunk) {
113
- const surface = [
114
- chunk.chunk_id,
115
- chunk.owner_domain,
116
- chunk.spec_surface,
117
- ...(chunk.files ?? []),
118
- ...(chunk.authority_refs ?? []),
119
- ].join(" ").toLowerCase();
120
- const tokens = new Set(tokenize(surface));
121
- const generated = [...GENERATED_TERMS].some((term) => tokens.has(term) || surface.includes(`/${term}`) || surface.includes(`-${term}`));
122
- if (generated) {
123
- return {
124
- level: "shallow",
125
- selected_limit: 16,
126
- reason: "generated_table_index_or_registry_surface",
127
- codex_posture: "audit authority invariants against a compact representative implementation slice; do not expand to the full generated inventory unless a P0/P1 signal requires it",
128
- };
129
- }
130
- const highRisk = [...HIGH_RISK_TERMS].some((term) => tokens.has(term))
131
- || ["runtime", "sdk", "security", "boundary"].includes(chunk.owner_domain);
132
- if (highRisk) {
133
- return {
134
- level: "deep",
135
- selected_limit: 96,
136
- reason: "high_risk_runtime_sdk_security_boundary_or_capability_surface",
137
- codex_posture: "inspect the selected implementation slice deeply for P0/P1 recall before deciding no-finding",
138
- };
139
- }
140
- return {
141
- level: "normal",
142
- selected_limit: 48,
143
- reason: "ordinary_spec_authority_surface",
144
- codex_posture: "inspect the selected implementation slice semantically with normal depth",
145
- };
146
- }
147
-
148
- function relativeImportTargets(sourceRef, sourceText, inventorySet) {
149
- const targets = [];
150
- const imports = [
151
- ...sourceText.matchAll(/\b(?:import|export)\s+(?:[^'"`]*?\s+from\s+)?["'`]([^"'`]+)["'`]/gu),
152
- ...sourceText.matchAll(/\brequire\(["'`]([^"'`]+)["'`]\)/gu),
153
- ].map((match) => match[1]);
154
- const sourceDir = path.posix.dirname(sourceRef);
155
- const extensions = ["", ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".go", ".rs", ".json", ".yaml", ".yml"];
156
- for (const specifier of imports) {
157
- if (typeof specifier !== "string" || !specifier.startsWith(".")) {
158
- continue;
159
- }
160
- const base = path.posix.normalize(path.posix.join(sourceDir, specifier));
161
- const candidates = [
162
- ...extensions.map((extension) => `${base}${extension}`),
163
- ...extensions.filter(Boolean).map((extension) => `${base}/index${extension}`),
164
- ];
165
- const target = candidates.find((candidate) => inventorySet.has(candidate));
166
- if (target) {
167
- targets.push(target);
168
- }
169
- }
170
- return uniqueSorted(targets);
171
- }
172
-
173
- function buildRetrievalPrepass({ projectRoot, chunk, depth }) {
174
- const evidenceInventory = uniqueSorted(chunk.evidence_inventory ?? []);
175
- const authorityRefs = uniqueSorted(chunk.authority_refs ?? chunk.files ?? []);
176
- if (evidenceInventory.length === 0) {
177
- return {
178
- mode: "compact_selected_slice",
179
- selected_implementation_refs: [],
180
- selected_count: 0,
181
- full_inventory_count: 0,
182
- omitted_inventory_count: 0,
183
- omitted_inventory_sha256: sha256Json([]),
184
- omitted_inventory_canonicalization: "sorted_json_array_of_project_relative_refs",
185
- selection_signals: ["empty_evidence_inventory"],
186
- import_graph_edges: [],
187
- };
188
- }
189
-
190
- const authorityText = [
191
- chunk.chunk_id,
192
- chunk.owner_domain,
193
- chunk.spec_surface,
194
- ...authorityRefs,
195
- ...authorityRefs.map((ref) => readProjectFileText(projectRoot, ref)),
196
- ].join("\n");
197
- const authorityTokens = new Set(tokenize(authorityText));
198
- const ownerTokens = new Set(tokenize([chunk.owner_domain, chunk.spec_surface, chunk.chunk_id].join(" ")));
199
- const inventorySet = new Set(evidenceInventory);
200
- const scores = new Map(evidenceInventory.map((ref) => [ref, {
201
- ref,
202
- score: 0,
203
- signals: [],
204
- }]));
205
-
206
- for (const ref of evidenceInventory) {
207
- const entry = scores.get(ref);
208
- const refText = ref.toLowerCase();
209
- const refTokens = new Set(tokenize(ref));
210
- const tokenHits = [...authorityTokens].filter((token) => refTokens.has(token) || refText.includes(token));
211
- if (tokenHits.length > 0) {
212
- entry.score += Math.min(24, tokenHits.length * 4);
213
- entry.signals.push(`authority_keywords:${tokenHits.slice(0, 8).join(",")}`);
214
- }
215
- const ownerHits = [...ownerTokens].filter((token) => refTokens.has(token) || refText.includes(token));
216
- if (ownerHits.length > 0) {
217
- entry.score += Math.min(9, ownerHits.length * 3);
218
- entry.signals.push(`owner_domain:${ownerHits.slice(0, 4).join(",")}`);
219
- }
220
- if (/(^|[/_.-])(test|tests|spec|e2e)([/_.-]|$)/u.test(refText) || /(_test|\.test|\.spec)\.[a-z0-9]+$/u.test(refText)) {
221
- entry.score += 3;
222
- entry.signals.push("test_or_spec_file");
223
- }
224
- if (/(^|[/_.-])(index|registry|table|tables|generated|codegen)([/_.-]|$)/u.test(refText)) {
225
- entry.score += depth.level === "shallow" ? 2 : -2;
226
- entry.signals.push("generated_table_index_surface");
227
- }
228
- }
229
-
230
- const importGraphEdges = [];
231
- const scanLimit = Math.min(evidenceInventory.length, depth.level === "deep" ? 240 : depth.level === "normal" ? 120 : 60);
232
- for (const ref of evidenceInventory.slice(0, scanLimit)) {
233
- const text = readProjectFileText(projectRoot, ref, 32768);
234
- if (!text) {
235
- continue;
236
- }
237
- const targets = relativeImportTargets(ref, text, inventorySet);
238
- for (const target of targets) {
239
- importGraphEdges.push({ from: ref, to: target });
240
- scores.get(ref).score += 2;
241
- scores.get(ref).signals.push("import_graph_source");
242
- scores.get(target).score += 3;
243
- scores.get(target).signals.push("import_graph_target");
244
- }
245
- }
246
-
247
- const ranked = [...scores.values()].sort((left, right) => {
248
- if (right.score !== left.score) {
249
- return right.score - left.score;
250
- }
251
- return left.ref.localeCompare(right.ref);
252
- });
253
- const initialSelected = ranked.slice(0, Math.min(depth.selected_limit, ranked.length)).map((entry) => entry.ref);
254
- const selectedSet = new Set(initialSelected);
255
- for (const selected of initialSelected) {
256
- const selectedDir = path.posix.dirname(selected);
257
- const selectedStem = path.posix.basename(selected).replace(/(\.test|\.spec|_test)?\.[^.]+$/u, "");
258
- for (const ref of evidenceInventory) {
259
- if (selectedSet.size >= depth.selected_limit) {
260
- break;
261
- }
262
- if (selectedSet.has(ref)) {
263
- continue;
264
- }
265
- const sameDir = path.posix.dirname(ref) === selectedDir;
266
- const sameStem = path.posix.basename(ref).includes(selectedStem);
267
- const isNearbyTest = sameDir && (sameStem || /(\.test|\.spec|_test)\./u.test(ref));
268
- if (isNearbyTest) {
269
- selectedSet.add(ref);
270
- scores.get(ref).signals.push("nearby_file_or_test");
271
- }
272
- }
273
- }
274
- const selected = uniqueSorted([...selectedSet]);
275
- const selectedSignals = selected.map((ref) => ({
276
- ref,
277
- score: scores.get(ref)?.score ?? 0,
278
- signals: uniqueSorted(scores.get(ref)?.signals ?? []).slice(0, 8),
279
- }));
280
- return {
281
- mode: "compact_selected_slice",
282
- selected_implementation_refs: selected,
283
- selected_count: selected.length,
284
- full_inventory_count: evidenceInventory.length,
285
- omitted_inventory_count: Math.max(0, evidenceInventory.length - selected.length),
286
- omitted_inventory_sha256: sha256Json(evidenceInventory),
287
- omitted_inventory_canonicalization: "sorted_json_array_of_project_relative_refs",
288
- selection_signals: [
289
- "authority_keywords",
290
- "owner_domain",
291
- "import_graph",
292
- "tests",
293
- "nearby_files",
294
- ],
295
- selected_ref_signals: selectedSignals,
296
- import_graph_edges: importGraphEdges.slice(0, 200),
297
- };
298
- }
299
-
300
- export function buildAuditorPacket(sweepId, chunk, auditor, dispatchedAt, plan, options = {}) {
301
- const specAuthority = chunk.planning_basis === "spec_authority";
302
- const p0p1RecallProfile = criteriaEnableP0P1Recall(chunk.criteria)
303
- ? buildP0P1RecallProfile({ chunk, plan })
304
- : null;
305
- const p0p1RuleCheckRequiredIds = [
306
- "fail_open_or_pseudo_success",
307
- "partial_coverage_misrepresented_as_complete",
308
- "authority_boundary_or_private_import_bypass",
309
- "permission_or_capability_bypass",
310
- "ungated_destructive_action",
311
- "provider_or_model_hardcoding",
312
- "app_local_shadow_truth",
313
- ];
314
- const auditDepth = adaptiveDepthForChunk(chunk);
315
- const retrievalPrepass = buildRetrievalPrepass({ projectRoot: options.projectRoot, chunk, depth: auditDepth });
316
- const selectedImplementationRefs = retrievalPrepass.selected_implementation_refs;
317
- return {
318
- version: 1,
319
- kind: "audit-auditor-packet",
320
- sweep_id: sweepId,
321
- chunk_id: chunk.chunk_id,
322
- auditor,
323
- planning_basis: chunk.planning_basis ?? "file_inventory",
324
- spec_surface: chunk.spec_surface ?? null,
325
- criteria: chunk.criteria,
326
- owner_domain: chunk.owner_domain,
327
- audit_depth: auditDepth,
328
- files: chunk.files,
329
- authority_refs: chunk.authority_refs ?? chunk.files,
330
- host_authority_projection_refs: chunk.host_authority_projection_refs ?? [],
331
- evidence_roots: chunk.evidence_roots ?? [],
332
- admitted_evidence_roots: chunk.admitted_evidence_roots ?? [],
333
- evidence_inventory: selectedImplementationRefs,
334
- selected_implementation_refs: selectedImplementationRefs,
335
- retrieval_prepass: retrievalPrepass,
336
- omitted_evidence_inventory: {
337
- manager_owned: true,
338
- full_inventory_count: retrievalPrepass.full_inventory_count,
339
- selected_count: retrievalPrepass.selected_count,
340
- omitted_count: retrievalPrepass.omitted_inventory_count,
341
- sha256: retrievalPrepass.omitted_inventory_sha256,
342
- canonicalization: retrievalPrepass.omitted_inventory_canonicalization,
343
- source: "chunk.evidence_inventory",
344
- },
345
- evidence_inventory_status: chunk.evidence_inventory_status ?? null,
346
- evidence_inventory_empty_reason: chunk.evidence_inventory_empty_reason ?? null,
347
- coverage_contract: chunk.coverage_contract ?? null,
348
- risk_budget_policy: plan.risk_budget_policy ?? null,
349
- risk_budget_status: plan.risk_budget_status ?? null,
350
- audit_strategy: p0p1RecallProfile ? {
351
- mode: "p0_p1_triage_then_deep",
352
- profile: p0p1RecallProfile,
353
- } : {
354
- mode: specAuthority ? "spec_first_full_audit" : "file_inventory_audit",
355
- },
356
- audit_instructions: specAuthority ? {
357
- posture: "spec_first_full_audit",
358
- authority_source: ".nimi/spec/**",
359
- auditor_goal: "Find all material issues. Missing an issue is worse than a false positive.",
360
- required_categories: [
361
- "security",
362
- "logic-error",
363
- "error-handling",
364
- "code-quality",
365
- "performance",
366
- "consistency",
367
- "type-safety",
368
- "resource-leak",
369
- "race-condition",
370
- "spec-drift",
371
- "boundary",
372
- "contract",
373
- "architecture",
374
- ],
375
- required_flow: [
376
- "read every authority_ref first",
377
- "use selected_implementation_refs/evidence_inventory as the compact implementation slice selected by the manager retrieval prepass",
378
- "do not ask for or reconstruct the omitted full evidence_inventory; audit-codex validates cited refs against the manager-owned full chunk inventory",
379
- "inspect and cite the implementation refs needed for semantic authority and P0/P1 reasoning",
380
- `use audit_depth=${auditDepth.level}: ${auditDepth.codex_posture}`,
381
- "evaluate inspected implementation evidence against the authority_refs",
382
- "if evidence_inventory is empty, treat evidence_inventory_empty_reason as an auditable planning assertion rather than proof of correctness",
383
- "emit auditor.id, auditor.mode, auditor.methodology_ref, and auditor.provenance with kind=semantic_audit, packet_ref, and session_ref or transcript_ref or review_ref",
384
- "do not author coverage.files, coverage.authority_refs, or coverage.evidence_files; audit-codex mechanically populates them from the packet before ingest",
385
- "emit one authority_outcome per authority_ref",
386
- `emit coverage.p0p1_rule_checks with exactly these ids and no aliases: ${p0p1RuleCheckRequiredIds.join(", ")}`,
387
- "each P0/P1 rule check must include id, status, implementation_refs, and chunk-specific negative_reasoning",
388
- "use status=checked with at least one in-scope implementation_ref when implementation evidence was inspected",
389
- "use status=not_applicable only when there is no implementation surface and explain why in negative_reasoning",
390
- "emit coverage.p0p1_evidence_refs for implementation refs actually inspected",
391
- "emit every finding that satisfies the audit-finding contract",
392
- ],
393
- p0p1_recall: p0p1RecallProfile,
394
- } : null,
395
- output_contract: {
396
- format: "json",
397
- required_top_level_fields: ["chunk_id", "auditor", "coverage", "findings"],
398
- auditor_required_shape: {
399
- required_fields: ["id", "mode", "methodology_ref", "provenance"],
400
- provenance_required: {
401
- kind: "semantic_audit",
402
- packet_ref: packetRef(sweepId, chunk.chunk_id),
403
- one_of: ["session_ref", "transcript_ref", "review_ref"],
404
- },
405
- },
406
- raw_coverage_required_fields: [
407
- "authority_outcomes",
408
- "p0p1_evidence_refs",
409
- "p0p1_rule_checks",
410
- ],
411
- manager_owned_coverage_fields: [
412
- "files",
413
- "authority_refs",
414
- "evidence_files",
415
- ],
416
- manager_owned_coverage_population: {
417
- coverage_files_from_chunk_files: chunk.files,
418
- coverage_authority_refs_from_chunk_authority_refs: chunk.authority_refs ?? chunk.files,
419
- coverage_evidence_files_from_chunk_evidence_inventory: specAuthority ? "manager_owned_chunk.evidence_inventory" : null,
420
- codex_cited_implementation_refs_must_belong_to_evidence_inventory: true,
421
- },
422
- spec_authority_coverage_requires_authority_outcomes: specAuthority,
423
- authority_outcome_required_fields: [
424
- "authority_ref",
425
- "status",
426
- "inspected_implementation_refs_or_implementation_evidence_refs_or_implementation_not_applicable_reason",
427
- "negative_reasoning",
428
- ],
429
- authority_outcome_manager_owned_fields: [
430
- "evidence_refs",
431
- ],
432
- authority_outcome_status_semantics: {
433
- audited: "The authority ref and available implementation evidence were inspected. Use this even when findings were discovered.",
434
- blocked: "The auditor could not inspect required evidence and must explain the blocker in reason.",
435
- not_applicable: "No implementation surface applies and the auditor must explain why in reason.",
436
- },
437
- spec_authority_normalized_evidence_requires_evidence_files: specAuthority,
438
- p0p1_negative_reasoning_required_when_no_critical_or_high_findings: Boolean(p0p1RecallProfile),
439
- p0p1_negative_reasoning_field: p0p1RecallProfile ? "coverage.p0p1_negative_reasoning" : null,
440
- p0p1_evidence_refs_field: p0p1RecallProfile ? "coverage.p0p1_evidence_refs" : null,
441
- p0p1_rule_checks_field: p0p1RecallProfile ? "coverage.p0p1_rule_checks" : null,
442
- p0p1_rule_check_required_ids: p0p1RecallProfile ? p0p1RuleCheckRequiredIds : [],
443
- p0p1_rule_check_id_policy: {
444
- exact_ids_required: true,
445
- aliases_rejected_fail_closed: true,
446
- required_ids_source: "output_contract.p0p1_rule_check_required_ids",
447
- },
448
- p0p1_rule_check_required_fields: ["id", "status", "implementation_refs", "negative_reasoning"],
449
- p0p1_rule_check_status_enum: ["checked", "not_applicable"],
450
- p0p1_rule_check_semantics: {
451
- checked_requires_in_scope_implementation_refs: true,
452
- checked_refs_should_come_from_selected_implementation_refs: true,
453
- not_applicable_requires_negative_reasoning: true,
454
- missing_status_or_negative_reasoning_rejected_before_ingest: true,
455
- },
456
- finding_locations_must_belong_to_chunk_files_or_evidence_inventory: true,
457
- authority_only_finding_location_policy: "when no implementation surface exists, findings[].location.file must be the in-scope authority_ref that contains the defect",
458
- finding_contract_ref: ".nimi/contracts/audit-finding.schema.yaml",
459
- ingest_command: `nimicoding sweep audit chunk ingest --sweep-id ${sweepId} --chunk-id ${chunk.chunk_id} --from <audit-output.json> --verified-at <ISO-8601-UTC>`,
460
- },
461
- hard_constraints: [
462
- "do_not_sample_out_files_from_this_chunk",
463
- "for_spec_authority_chunks_audit_the_authority_refs_first_and_use_evidence_roots_for_implementation_evidence",
464
- "for_spec_authority_chunks_emit_one_authority_outcome_per_authority_ref",
465
- "for_spec_authority_chunks_cite_only_inspected_implementation_refs_from_selected_implementation_refs",
466
- "audit_codex_populates_full_coverage_evidence_files_from_manager_owned_chunk_inventory",
467
- "full_evidence_inventory_is_manager_owned_and_not_exposed_to_codex",
468
- "if_no_implementation_surface_exists_mark_the_authority_outcome_not_applicable_with_reason",
469
- "do_not_return_pseudo_success",
470
- "do_not_emit_findings_outside_chunk_files_or_declared_evidence_inventory",
471
- "fail_closed_if_a_file_cannot_be_audited",
472
- ],
473
- created_at: dispatchedAt,
474
- };
475
- }
476
-
477
- export async function dispatchAuditSweepChunk(projectRoot, options) {
478
- const sweepId = safeSweepId(options.sweepId);
479
- if (!sweepId || typeof options.chunkId !== "string") {
480
- return inputError("nimicoding sweep audit refused: --sweep-id and --chunk-id are required.\n");
481
- }
482
-
483
- const timestampError = ensureIsoTimestamp(options.dispatchedAt, "--dispatched-at");
484
- if (timestampError) {
485
- return timestampError;
486
- }
487
-
488
- return withAuditSweepMutationLock(projectRoot, sweepId, "chunk dispatch", async () => {
489
- const planResult = await loadPlan(projectRoot, sweepId);
490
- if (!planResult.ok) {
491
- return inputError(planResult.error);
492
- }
493
-
494
- const chunkResult = await loadChunk(projectRoot, sweepId, options.chunkId);
495
- if (!chunkResult.ok) {
496
- return inputError(chunkResult.error);
497
- }
498
-
499
- if (chunkResult.chunk.state !== "planned") {
500
- return inputError("nimicoding sweep audit refused: chunk dispatch requires planned state.\n");
501
- }
502
- const budgetBlock = budgetBlockForChunk(planResult.plan, chunkResult.chunk);
503
- if (budgetBlock) {
504
- return inputError(`nimicoding sweep audit refused: ${budgetBlock}; build or admit remediation bundles before continuing discovery.\n`);
505
- }
506
-
507
- const updatedChunk = {
508
- ...chunkResult.chunk,
509
- state: "dispatched",
510
- lifecycle: {
511
- ...chunkResult.chunk.lifecycle,
512
- dispatched_at: options.dispatchedAt,
513
- },
514
- dispatch: {
515
- auditor: options.auditor ?? "external_auditor",
516
- criteria: chunkResult.chunk.criteria,
517
- files: chunkResult.chunk.files,
518
- authority_refs: chunkResult.chunk.authority_refs ?? chunkResult.chunk.files,
519
- host_authority_projection_refs: chunkResult.chunk.host_authority_projection_refs ?? [],
520
- evidence_roots: chunkResult.chunk.evidence_roots ?? [],
521
- admitted_evidence_roots: chunkResult.chunk.admitted_evidence_roots ?? [],
522
- evidence_inventory: chunkResult.chunk.evidence_inventory ?? [],
523
- evidence_inventory_status: chunkResult.chunk.evidence_inventory_status ?? null,
524
- evidence_inventory_empty_reason: chunkResult.chunk.evidence_inventory_empty_reason ?? null,
525
- },
526
- updated_at: options.dispatchedAt,
527
- };
528
- const packet = buildAuditorPacket(sweepId, chunkResult.chunk, updatedChunk.dispatch.auditor, options.dispatchedAt, planResult.plan, { projectRoot });
529
- const auditorPacketRef = packetRef(sweepId, options.chunkId);
530
- await writeYamlRef(projectRoot, auditorPacketRef, packet);
531
- await writeYamlRef(projectRoot, chunkResult.chunkRef, updatedChunk);
532
- await writeYamlRef(projectRoot, planResult.planRef, {
533
- ...updatePlanChunk(planResult.plan, options.chunkId, { state: "dispatched" }),
534
- updated_at: options.dispatchedAt,
535
- });
536
- const runRef = await appendRunEvent(projectRoot, sweepId, {
537
- event_type: "chunk_dispatched",
538
- chunk_id: options.chunkId,
539
- chunk_ref: chunkRef(sweepId, options.chunkId),
540
- packet_ref: auditorPacketRef,
541
- auditor: updatedChunk.dispatch.auditor,
542
- });
543
-
544
- return {
545
- ok: true,
546
- exitCode: 0,
547
- sweepId,
548
- chunkId: options.chunkId,
549
- state: "dispatched",
550
- chunkRef: chunkResult.chunkRef,
551
- packetRef: auditorPacketRef,
552
- runLedgerRef: runRef,
553
- };
554
- });
555
- }
556
-
557
- export async function reviewAuditSweepChunk(projectRoot, options) {
558
- const sweepId = safeSweepId(options.sweepId);
559
- if (!sweepId || typeof options.chunkId !== "string") {
560
- return inputError("nimicoding sweep audit refused: --sweep-id and --chunk-id are required.\n");
561
- }
562
-
563
- const timestampError = ensureIsoTimestamp(options.reviewedAt, "--reviewed-at");
564
- if (timestampError) {
565
- return timestampError;
566
- }
567
-
568
- if (!["pass", "fail"].includes(options.verdict)) {
569
- return inputError("nimicoding sweep audit refused: --verdict must be pass or fail.\n");
570
- }
571
-
572
- return withAuditSweepMutationLock(projectRoot, sweepId, "chunk review", async () => {
573
- const planResult = await loadPlan(projectRoot, sweepId);
574
- if (!planResult.ok) {
575
- return inputError(planResult.error);
576
- }
577
-
578
- const chunkResult = await loadChunk(projectRoot, sweepId, options.chunkId);
579
- if (!chunkResult.ok) {
580
- return inputError(chunkResult.error);
581
- }
582
-
583
- if (chunkResult.chunk.state !== "ingested") {
584
- return inputError("nimicoding sweep audit refused: chunk review requires ingested state.\n");
585
- }
586
- if (options.verdict === "pass" && chunkResult.chunk.audit_validity?.posture === "invalid") {
587
- return inputError("nimicoding sweep audit refused: manager review cannot freeze invalid no-finding evidence as pass.\n");
588
- }
589
-
590
- const nextState = options.verdict === "pass" ? "frozen" : "failed";
591
- const updatedChunk = {
592
- ...chunkResult.chunk,
593
- state: nextState,
594
- lifecycle: {
595
- ...chunkResult.chunk.lifecycle,
596
- reviewed_at: options.reviewedAt,
597
- frozen_at: options.verdict === "pass" ? options.reviewedAt : chunkResult.chunk.lifecycle.frozen_at,
598
- failed_at: options.verdict === "fail" ? options.reviewedAt : chunkResult.chunk.lifecycle.failed_at,
599
- },
600
- review: {
601
- verdict: options.verdict,
602
- reviewer: options.reviewer ?? "nimicoding_manager",
603
- summary: options.summary ?? null,
604
- reviewed_at: options.reviewedAt,
605
- },
606
- failure: options.verdict === "fail"
607
- ? { reason: options.summary ?? "manager_review_failed", failed_at: options.reviewedAt }
608
- : chunkResult.chunk.failure,
609
- updated_at: options.reviewedAt,
610
- };
611
- await writeYamlRef(projectRoot, chunkResult.chunkRef, updatedChunk);
612
- await writeYamlRef(projectRoot, planResult.planRef, {
613
- ...updatePlanChunk(planResult.plan, options.chunkId, { state: nextState }),
614
- updated_at: options.reviewedAt,
615
- });
616
- const runRef = await appendRunEvent(projectRoot, sweepId, {
617
- event_type: options.verdict === "pass" ? "chunk_frozen" : "chunk_failed",
618
- chunk_id: options.chunkId,
619
- chunk_ref: chunkResult.chunkRef,
620
- reviewer: updatedChunk.review.reviewer,
621
- summary: updatedChunk.review.summary,
622
- });
623
-
624
- return {
625
- ok: true,
626
- exitCode: 0,
627
- sweepId,
628
- chunkId: options.chunkId,
629
- state: nextState,
630
- chunkRef: chunkResult.chunkRef,
631
- runLedgerRef: runRef,
632
- };
633
- });
634
- }
635
-
636
- export async function skipAuditSweepChunk(projectRoot, options) {
637
- const sweepId = safeSweepId(options.sweepId);
638
- if (!sweepId || typeof options.chunkId !== "string") {
639
- return inputError("nimicoding sweep audit refused: --sweep-id and --chunk-id are required.\n");
640
- }
641
- const timestampError = ensureIsoTimestamp(options.skippedAt, "--skipped-at");
642
- if (timestampError) {
643
- return timestampError;
644
- }
645
- if (typeof options.reason !== "string" || !options.reason.trim()) {
646
- return inputError("nimicoding sweep audit refused: --reason is required when skipping a chunk.\n");
647
- }
648
-
649
- return withAuditSweepMutationLock(projectRoot, sweepId, "chunk skip", async () => {
650
- const planResult = await loadPlan(projectRoot, sweepId);
651
- if (!planResult.ok) {
652
- return inputError(planResult.error);
653
- }
654
- const chunkResult = await loadChunk(projectRoot, sweepId, options.chunkId);
655
- if (!chunkResult.ok) {
656
- return inputError(chunkResult.error);
657
- }
658
- if (chunkResult.chunk.state === "frozen") {
659
- return inputError("nimicoding sweep audit refused: frozen chunks cannot be skipped.\n");
660
- }
661
-
662
- const updatedChunk = {
663
- ...chunkResult.chunk,
664
- state: "skipped",
665
- lifecycle: {
666
- ...chunkResult.chunk.lifecycle,
667
- skipped_at: options.skippedAt,
668
- },
669
- skip: {
670
- reason: options.reason,
671
- skipped_at: options.skippedAt,
672
- },
673
- updated_at: options.skippedAt,
674
- };
675
- await writeYamlRef(projectRoot, chunkResult.chunkRef, updatedChunk);
676
- await writeYamlRef(projectRoot, planResult.planRef, {
677
- ...updatePlanChunk(planResult.plan, options.chunkId, { state: "skipped" }),
678
- updated_at: options.skippedAt,
679
- });
680
- const runRef = await appendRunEvent(projectRoot, sweepId, {
681
- event_type: "chunk_skipped",
682
- chunk_id: options.chunkId,
683
- chunk_ref: chunkResult.chunkRef,
684
- reason: options.reason,
685
- });
686
-
687
- return {
688
- ok: true,
689
- exitCode: 0,
690
- sweepId,
691
- chunkId: options.chunkId,
692
- state: "skipped",
693
- chunkRef: chunkResult.chunkRef,
694
- runLedgerRef: runRef,
695
- };
696
- });
697
- }