@principal-ai/subsystems-core 0.52.0 → 0.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +30 -2
  2. package/dist/audit/boundary.d.ts +104 -0
  3. package/dist/audit/boundary.d.ts.map +1 -0
  4. package/dist/audit/boundary.js +363 -0
  5. package/dist/audit/boundary.js.map +1 -0
  6. package/dist/audit/c4.d.ts +41 -0
  7. package/dist/audit/c4.d.ts.map +1 -0
  8. package/dist/audit/c4.js +41 -0
  9. package/dist/audit/c4.js.map +1 -0
  10. package/dist/audit/engine.d.ts +135 -0
  11. package/dist/audit/engine.d.ts.map +1 -0
  12. package/dist/audit/engine.js +865 -0
  13. package/dist/audit/engine.js.map +1 -0
  14. package/dist/audit/fixes.d.ts +80 -0
  15. package/dist/audit/fixes.d.ts.map +1 -0
  16. package/dist/audit/fixes.js +162 -0
  17. package/dist/audit/fixes.js.map +1 -0
  18. package/dist/audit/graphify-provider.d.ts +51 -0
  19. package/dist/audit/graphify-provider.d.ts.map +1 -0
  20. package/dist/audit/graphify-provider.js +36 -0
  21. package/dist/audit/graphify-provider.js.map +1 -0
  22. package/dist/audit/hash.d.ts +25 -0
  23. package/dist/audit/hash.d.ts.map +1 -0
  24. package/dist/audit/hash.js +53 -0
  25. package/dist/audit/hash.js.map +1 -0
  26. package/dist/audit/index.d.ts +12 -0
  27. package/dist/audit/index.d.ts.map +1 -0
  28. package/dist/audit/index.js +40 -0
  29. package/dist/audit/index.js.map +1 -0
  30. package/dist/audit/local-graphify.d.ts +75 -0
  31. package/dist/audit/local-graphify.d.ts.map +1 -0
  32. package/dist/audit/local-graphify.js +283 -0
  33. package/dist/audit/local-graphify.js.map +1 -0
  34. package/dist/audit/paths.d.ts +16 -0
  35. package/dist/audit/paths.d.ts.map +1 -0
  36. package/dist/audit/paths.js +31 -0
  37. package/dist/audit/paths.js.map +1 -0
  38. package/dist/audit/pure.d.ts +18 -0
  39. package/dist/audit/pure.d.ts.map +1 -0
  40. package/dist/audit/pure.js +50 -0
  41. package/dist/audit/pure.js.map +1 -0
  42. package/dist/audit/report-logic.d.ts +17 -0
  43. package/dist/audit/report-logic.d.ts.map +1 -0
  44. package/dist/audit/report-logic.js +206 -0
  45. package/dist/audit/report-logic.js.map +1 -0
  46. package/dist/audit/report.d.ts +140 -0
  47. package/dist/audit/report.d.ts.map +1 -0
  48. package/dist/audit/report.js +3 -0
  49. package/dist/audit/report.js.map +1 -0
  50. package/dist/audit/trail.d.ts +29 -0
  51. package/dist/audit/trail.d.ts.map +1 -0
  52. package/dist/audit/trail.js +59 -0
  53. package/dist/audit/trail.js.map +1 -0
  54. package/dist/audit/verification.d.ts +98 -0
  55. package/dist/audit/verification.d.ts.map +1 -0
  56. package/dist/audit/verification.js +11 -0
  57. package/dist/audit/verification.js.map +1 -0
  58. package/dist/audit/verify/component.d.ts +68 -0
  59. package/dist/audit/verify/component.d.ts.map +1 -0
  60. package/dist/audit/verify/component.js +500 -0
  61. package/dist/audit/verify/component.js.map +1 -0
  62. package/dist/audit/verify/declarations.d.ts +58 -0
  63. package/dist/audit/verify/declarations.d.ts.map +1 -0
  64. package/dist/audit/verify/declarations.js +227 -0
  65. package/dist/audit/verify/declarations.js.map +1 -0
  66. package/dist/audit/verify/grammar.d.ts +12 -0
  67. package/dist/audit/verify/grammar.d.ts.map +1 -0
  68. package/dist/audit/verify/grammar.js +57 -0
  69. package/dist/audit/verify/grammar.js.map +1 -0
  70. package/dist/audit/verify/index.d.ts +12 -0
  71. package/dist/audit/verify/index.d.ts.map +1 -0
  72. package/dist/audit/verify/index.js +31 -0
  73. package/dist/audit/verify/index.js.map +1 -0
  74. package/dist/audit/verify/parser.d.ts +19 -0
  75. package/dist/audit/verify/parser.d.ts.map +1 -0
  76. package/dist/audit/verify/parser.js +122 -0
  77. package/dist/audit/verify/parser.js.map +1 -0
  78. package/dist/audit/verify/span.d.ts +49 -0
  79. package/dist/audit/verify/span.d.ts.map +1 -0
  80. package/dist/audit/verify/span.js +377 -0
  81. package/dist/audit/verify/span.js.map +1 -0
  82. package/dist/graphify/anchor.d.ts +45 -0
  83. package/dist/graphify/anchor.d.ts.map +1 -0
  84. package/dist/graphify/anchor.js +179 -0
  85. package/dist/graphify/anchor.js.map +1 -0
  86. package/dist/graphify/consolidated.d.ts +259 -0
  87. package/dist/graphify/consolidated.d.ts.map +1 -0
  88. package/dist/graphify/consolidated.js +12 -0
  89. package/dist/graphify/consolidated.js.map +1 -0
  90. package/dist/graphify/construct.d.ts +41 -0
  91. package/dist/graphify/construct.d.ts.map +1 -0
  92. package/dist/graphify/construct.js +106 -0
  93. package/dist/graphify/construct.js.map +1 -0
  94. package/dist/graphify/ids.d.ts +19 -0
  95. package/dist/graphify/ids.d.ts.map +1 -0
  96. package/dist/graphify/ids.js +53 -0
  97. package/dist/graphify/ids.js.map +1 -0
  98. package/dist/graphify/index.d.ts +16 -0
  99. package/dist/graphify/index.d.ts.map +1 -0
  100. package/dist/graphify/index.js +32 -0
  101. package/dist/graphify/index.js.map +1 -0
  102. package/dist/graphify/resolve.d.ts +48 -0
  103. package/dist/graphify/resolve.d.ts.map +1 -0
  104. package/dist/graphify/resolve.js +89 -0
  105. package/dist/graphify/resolve.js.map +1 -0
  106. package/dist/graphify/signature.d.ts +87 -0
  107. package/dist/graphify/signature.d.ts.map +1 -0
  108. package/dist/graphify/signature.js +282 -0
  109. package/dist/graphify/signature.js.map +1 -0
  110. package/dist/graphify/types.d.ts +141 -0
  111. package/dist/graphify/types.d.ts.map +1 -0
  112. package/dist/graphify/types.js +9 -0
  113. package/dist/graphify/types.js.map +1 -0
  114. package/dist/index.d.ts +4 -0
  115. package/dist/index.d.ts.map +1 -1
  116. package/dist/index.js +9 -1
  117. package/dist/index.js.map +1 -1
  118. package/dist/infra/c4.d.ts +59 -0
  119. package/dist/infra/c4.d.ts.map +1 -0
  120. package/dist/infra/c4.js +106 -0
  121. package/dist/infra/c4.js.map +1 -0
  122. package/dist/infra/index.d.ts +13 -0
  123. package/dist/infra/index.d.ts.map +1 -0
  124. package/dist/infra/index.js +22 -0
  125. package/dist/infra/index.js.map +1 -0
  126. package/dist/infra/jsonc.d.ts +10 -0
  127. package/dist/infra/jsonc.d.ts.map +1 -0
  128. package/dist/infra/jsonc.js +77 -0
  129. package/dist/infra/jsonc.js.map +1 -0
  130. package/dist/infra/terraform.d.ts +41 -0
  131. package/dist/infra/terraform.d.ts.map +1 -0
  132. package/dist/infra/terraform.js +208 -0
  133. package/dist/infra/terraform.js.map +1 -0
  134. package/dist/infra/types.d.ts +107 -0
  135. package/dist/infra/types.d.ts.map +1 -0
  136. package/dist/infra/types.js +47 -0
  137. package/dist/infra/types.js.map +1 -0
  138. package/dist/infra/wrangler.d.ts +32 -0
  139. package/dist/infra/wrangler.d.ts.map +1 -0
  140. package/dist/infra/wrangler.js +177 -0
  141. package/dist/infra/wrangler.js.map +1 -0
  142. package/dist/node.d.ts +3 -0
  143. package/dist/node.d.ts.map +1 -1
  144. package/dist/node.js +4 -1
  145. package/dist/node.js.map +1 -1
  146. package/dist/storage/topic-reference-links.d.ts +3 -3
  147. package/dist/storage/topic-reference-links.d.ts.map +1 -1
  148. package/dist/storage/topic-reference-links.js +23 -8
  149. package/dist/storage/topic-reference-links.js.map +1 -1
  150. package/dist/storage/topic-share.d.ts +69 -0
  151. package/dist/storage/topic-share.d.ts.map +1 -0
  152. package/dist/storage/topic-share.js +67 -0
  153. package/dist/storage/topic-share.js.map +1 -0
  154. package/dist/storage/topic-types.d.ts +5 -1
  155. package/dist/storage/topic-types.d.ts.map +1 -1
  156. package/dist/types/subsystem-model.d.ts +59 -0
  157. package/dist/types/subsystem-model.d.ts.map +1 -1
  158. package/dist/types/subsystem-model.js +50 -1
  159. package/dist/types/subsystem-model.js.map +1 -1
  160. package/dist/validation.d.ts.map +1 -1
  161. package/dist/validation.js +25 -2
  162. package/dist/validation.js.map +1 -1
  163. package/package.json +22 -2
  164. package/schemas/subsystem-model.schema.json +35 -19
  165. package/src/audit/boundary.ts +447 -0
  166. package/src/audit/c4.ts +70 -0
  167. package/src/audit/engine.ts +1099 -0
  168. package/src/audit/fixes.ts +192 -0
  169. package/src/audit/graphify-provider.test.ts +58 -0
  170. package/src/audit/graphify-provider.ts +77 -0
  171. package/src/audit/hash.ts +58 -0
  172. package/src/audit/index.ts +34 -0
  173. package/src/audit/local-graphify.test.ts +163 -0
  174. package/src/audit/local-graphify.ts +354 -0
  175. package/src/audit/paths.ts +25 -0
  176. package/src/audit/pure.ts +91 -0
  177. package/src/audit/report-logic.test.ts +145 -0
  178. package/src/audit/report-logic.ts +245 -0
  179. package/src/audit/report.ts +207 -0
  180. package/src/audit/trail.ts +77 -0
  181. package/src/audit/verification.ts +104 -0
  182. package/src/audit/verify/component.ts +760 -0
  183. package/src/audit/verify/declarations.ts +284 -0
  184. package/src/audit/verify/grammar.ts +57 -0
  185. package/src/audit/verify/index.ts +41 -0
  186. package/src/audit/verify/parser.ts +124 -0
  187. package/src/audit/verify/span.ts +478 -0
  188. package/src/graphify/anchor.ts +206 -0
  189. package/src/graphify/consolidated.ts +298 -0
  190. package/src/graphify/construct.ts +153 -0
  191. package/src/graphify/ids.ts +48 -0
  192. package/src/graphify/index.ts +16 -0
  193. package/src/graphify/resolve.ts +115 -0
  194. package/src/graphify/signature.ts +354 -0
  195. package/src/graphify/types.ts +164 -0
  196. package/src/index.ts +10 -0
  197. package/src/infra/c4.ts +146 -0
  198. package/src/infra/index.ts +21 -0
  199. package/src/infra/infra.test.ts +360 -0
  200. package/src/infra/jsonc.ts +70 -0
  201. package/src/infra/terraform.ts +251 -0
  202. package/src/infra/types.ts +136 -0
  203. package/src/infra/wrangler.ts +203 -0
  204. package/src/node.ts +3 -0
  205. package/src/storage/topic-reference-links.test.ts +7 -1
  206. package/src/storage/topic-reference-links.ts +24 -8
  207. package/src/storage/topic-share.test.ts +91 -0
  208. package/src/storage/topic-share.ts +118 -0
  209. package/src/storage/topic-types.ts +3 -1
  210. package/src/storage/topicStore.test.ts +1 -0
  211. package/src/types/subsystem-model.test.ts +52 -2
  212. package/src/types/subsystem-model.ts +104 -0
  213. package/src/validation.test.ts +71 -7
  214. package/src/validation.ts +27 -2
@@ -0,0 +1,760 @@
1
+ /**
2
+ * Deterministic per-component verification shared by every audit host.
3
+ *
4
+ * Filesystem and tree-sitter access are local; the model store, Graphify
5
+ * transport, accepted augmentations, and span promotion are injected by the
6
+ * host. Studio and the CLI therefore run the same verification policy.
7
+ */
8
+
9
+ import { existsSync, promises as fs } from 'node:fs'
10
+ import { join } from 'node:path'
11
+ import {
12
+ findUniqueDefinitionBySymbol,
13
+ resolveComponentAnchor,
14
+ type ComponentAnchorResult,
15
+ } from '../../graphify/anchor'
16
+ import { normalizeSourcePath } from '../../graphify/ids'
17
+ import {
18
+ inferConstructFromGraphify,
19
+ resolveConstructMatch,
20
+ } from '../../graphify/construct'
21
+ import {
22
+ compareSignatures,
23
+ extractGraphifySignature,
24
+ } from '../../graphify/signature'
25
+ import type { GraphifyEdge, GraphifyNode } from '../../graphify/types'
26
+ import type { SubsystemComponent } from '../../types/subsystem-model'
27
+ import type { GraphifyAuditProvider } from '../graphify-provider'
28
+ import { storeTypeUndeclared } from '../fixes'
29
+ import {
30
+ buildSourceSpan,
31
+ hashContentRange,
32
+ parseSourceLocation,
33
+ } from '../hash'
34
+ import { purlRepoKey } from '../paths'
35
+ import type { AuditComponentVerification } from '../verification'
36
+ import { deriveSpan, type SpanTreeCache } from './span'
37
+
38
+ export interface VerifyComponentSpanClaim {
39
+ startLine: number
40
+ endLine: number
41
+ contentHash: string
42
+ }
43
+
44
+ export interface VerifyComponentSignatureClaim {
45
+ parameters: Array<{ type: string; optional?: boolean }>
46
+ returnType?: string
47
+ }
48
+
49
+ export interface VerifyComponentAugmentation {
50
+ claims: {
51
+ span?: VerifyComponentSpanClaim
52
+ construct?: string
53
+ signature?: VerifyComponentSignatureClaim
54
+ }
55
+ evidence?: string[]
56
+ }
57
+
58
+ /** Host-owned state the verifier reads or writes. */
59
+ export interface VerifyComponentDeps {
60
+ /** Load the stored model, or null when the id is unknown. */
61
+ loadModel(
62
+ graphId: string,
63
+ ): Promise<{ id: string; components: SubsystemComponent[] } | null>
64
+
65
+ /** Persist promoted components (non-dry-run span promotion only). */
66
+ updateComponents?(
67
+ graphId: string,
68
+ components: SubsystemComponent[],
69
+ ): Promise<void>
70
+
71
+ findAcceptedSpanAugmentation(input: {
72
+ purl: string
73
+ file: string
74
+ symbol: string
75
+ }): Promise<VerifyComponentAugmentation | null>
76
+
77
+ findAcceptedConstructAugmentation(input: {
78
+ purl: string
79
+ file: string
80
+ symbol: string
81
+ }): Promise<VerifyComponentAugmentation | null>
82
+
83
+ findAcceptedSignatureAugmentation(input: {
84
+ purl: string
85
+ file: string
86
+ symbol: string
87
+ }): Promise<VerifyComponentAugmentation | null>
88
+ }
89
+
90
+ export interface VerifyComponentOptions {
91
+ dryRun?: boolean
92
+ graphifyProvider: GraphifyAuditProvider
93
+ treeCache?: SpanTreeCache
94
+ }
95
+
96
+ function mapAnchor(
97
+ anchor: ComponentAnchorResult,
98
+ ): NonNullable<AuditComponentVerification['anchor']> {
99
+ return {
100
+ resolution: anchor.resolution,
101
+ nodeId: anchor.node ? String(anchor.node.id) : undefined,
102
+ label: anchor.node ? String(anchor.node.label ?? '') : undefined,
103
+ source_file:
104
+ anchor.node && typeof anchor.node.source_file === 'string'
105
+ ? anchor.node.source_file
106
+ : undefined,
107
+ source_location:
108
+ anchor.node && typeof anchor.node.source_location === 'string'
109
+ ? anchor.node.source_location
110
+ : undefined,
111
+ candidates: anchor.candidates.map((n) => ({
112
+ nodeId: String(n.id),
113
+ label: String(n.label ?? ''),
114
+ source_file:
115
+ typeof n.source_file === 'string' ? n.source_file : undefined,
116
+ source_location:
117
+ typeof n.source_location === 'string' ? n.source_location : undefined,
118
+ })),
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Freshness of a span against live content.
124
+ *
125
+ * `full` when the live range hashes to the span's `contentHash` and Graphify
126
+ * still points at the span's start. `stale` when the content changed or the
127
+ * declaration moved (positional drift). The hash is the invariant: a construct
128
+ * that merely moved can be re-pinned by re-hashing the shifted range.
129
+ */
130
+ function spanFreshness(
131
+ current: { contentHash: string; startLine: number },
132
+ liveHash: string,
133
+ graphifyStart: number | null,
134
+ ): NonNullable<AuditComponentVerification['declaration']>['freshness'] {
135
+ if (liveHash !== current.contentHash) return 'stale'
136
+ if (graphifyStart != null && graphifyStart !== current.startLine)
137
+ return 'stale'
138
+ return 'full'
139
+ }
140
+
141
+ async function captureDeclaration(
142
+ graphId: string,
143
+ components: SubsystemComponent[],
144
+ component: SubsystemComponent,
145
+ anchor: ComponentAnchorResult,
146
+ fileContent: string | null,
147
+ opts: VerifyComponentOptions,
148
+ deps: VerifyComponentDeps,
149
+ ): Promise<
150
+ NonNullable<AuditComponentVerification['declaration']> | undefined
151
+ > {
152
+ const stored = component.declarationSpan
153
+
154
+ // The capture lane: an accepted span augmentation is the only source of a
155
+ // full span (Graphify gives the start line, never the end).
156
+ let captured: VerifyComponentSpanClaim | undefined
157
+ if (component.purl && component.file && component.symbol) {
158
+ const aug = await deps.findAcceptedSpanAugmentation({
159
+ purl: component.purl,
160
+ file: component.file,
161
+ symbol: component.symbol,
162
+ })
163
+ const claim = aug?.claims.span
164
+ if (claim) {
165
+ captured = buildSourceSpan({
166
+ startLine: claim.startLine,
167
+ endLine: claim.endLine,
168
+ contentHash: claim.contentHash,
169
+ })
170
+ }
171
+ }
172
+
173
+ const current = captured ?? stored
174
+
175
+ // Tree-sitter derivation: graphify start + a parse of the file gives the
176
+ // declaration's end line for *any* grounded claim, not only ones with a
177
+ // stored span. This is what makes an unanchored construct capturable
178
+ // without an agent when every structural gate passes.
179
+ let derivation:
180
+ | NonNullable<AuditComponentVerification['declaration']>['derivation']
181
+ if (fileContent && component.file) {
182
+ const d = await deriveSpan({
183
+ content: fileContent,
184
+ file: component.file,
185
+ symbol: component.symbol,
186
+ construct: component.construct,
187
+ anchor,
188
+ storedSpan: stored,
189
+ treeCache: opts.treeCache,
190
+ })
191
+ let span: ReturnType<typeof buildSourceSpan> | undefined
192
+ if (
193
+ d.class === 'trivial' &&
194
+ d.graphifyStart != null &&
195
+ d.derivedEnd != null
196
+ ) {
197
+ const hash = hashContentRange(
198
+ fileContent,
199
+ d.graphifyStart,
200
+ d.derivedEnd,
201
+ )
202
+ if (hash) {
203
+ span = buildSourceSpan({
204
+ startLine: d.graphifyStart,
205
+ endLine: d.derivedEnd,
206
+ contentHash: hash,
207
+ })
208
+ }
209
+ }
210
+ derivation = {
211
+ class: d.class,
212
+ endLine: d.derivedEnd ?? undefined,
213
+ span,
214
+ failedGates: d.failedGates,
215
+ evidence: d.evidence,
216
+ }
217
+ }
218
+
219
+ if (!current) return { freshness: 'unanchored', derivation }
220
+ if (!fileContent) return { freshness: 'unchecked', span: current, derivation }
221
+
222
+ const liveHash = hashContentRange(
223
+ fileContent,
224
+ current.startLine,
225
+ current.endLine,
226
+ )
227
+ if (liveHash == null)
228
+ return { freshness: 'missing', span: current, derivation }
229
+
230
+ const graphifyStart =
231
+ anchor.resolution === 'exact' && anchor.node
232
+ ? parseSourceLocation(
233
+ typeof anchor.node.source_location === 'string'
234
+ ? anchor.node.source_location
235
+ : undefined,
236
+ )
237
+ : null
238
+
239
+ // A content-preserving move: shift the stored span to Graphify's start,
240
+ // keep its length, and re-hash. Offered only when the hash is unchanged —
241
+ // then the construct moved intact and the shift is a safe re-pin.
242
+ let repin: ReturnType<typeof buildSourceSpan> | undefined
243
+ if (
244
+ stored &&
245
+ graphifyStart != null &&
246
+ graphifyStart !== current.startLine &&
247
+ graphifyStart !== stored.startLine
248
+ ) {
249
+ const len = stored.endLine - stored.startLine
250
+ const shiftedHash = hashContentRange(
251
+ fileContent,
252
+ graphifyStart,
253
+ graphifyStart + len,
254
+ )
255
+ if (shiftedHash === stored.contentHash) {
256
+ repin = buildSourceSpan({
257
+ startLine: graphifyStart,
258
+ endLine: graphifyStart + len,
259
+ contentHash: shiftedHash,
260
+ })
261
+ }
262
+ }
263
+
264
+ const freshness = spanFreshness(current, liveHash, graphifyStart)
265
+
266
+ // Promote an accepted span augmentation into the model (dry-run reports
267
+ // without writing).
268
+ if (
269
+ !opts.dryRun &&
270
+ deps.updateComponents &&
271
+ captured &&
272
+ (!stored ||
273
+ stored.startLine !== captured.startLine ||
274
+ stored.endLine !== captured.endLine ||
275
+ stored.contentHash !== captured.contentHash)
276
+ ) {
277
+ await deps.updateComponents(
278
+ graphId,
279
+ components.map((c) =>
280
+ c.alias === component.alias
281
+ ? { ...c, declarationSpan: captured }
282
+ : c,
283
+ ),
284
+ )
285
+ }
286
+
287
+ return {
288
+ freshness,
289
+ span: current,
290
+ liveContentHash: liveHash,
291
+ repin,
292
+ derivation,
293
+ }
294
+ }
295
+
296
+ async function finalizeResult(
297
+ graphId: string,
298
+ components: SubsystemComponent[],
299
+ component: SubsystemComponent,
300
+ anchor: ComponentAnchorResult | undefined,
301
+ fileContent: string | null,
302
+ result: AuditComponentVerification,
303
+ opts: VerifyComponentOptions,
304
+ deps: VerifyComponentDeps,
305
+ ): Promise<AuditComponentVerification> {
306
+ if (!anchor) return result
307
+ const declaration = await captureDeclaration(
308
+ graphId,
309
+ components,
310
+ component,
311
+ anchor,
312
+ fileContent,
313
+ opts,
314
+ deps,
315
+ )
316
+ return declaration ? { ...result, declaration } : result
317
+ }
318
+
319
+ export async function verifySubsystemComponent(
320
+ graphId: string,
321
+ componentAlias: string,
322
+ opts: VerifyComponentOptions,
323
+ deps: VerifyComponentDeps,
324
+ ): Promise<AuditComponentVerification> {
325
+ const graphifyProvider = opts.graphifyProvider
326
+ const graph = await deps.loadModel(graphId)
327
+ if (!graph) {
328
+ return { ok: false, error: `unknown graph: ${graphId}` }
329
+ }
330
+ const component = graph.components.find((c) => c.alias === componentAlias)
331
+ if (!component) {
332
+ return { ok: false, error: `unknown component: ${componentAlias}`, componentAlias }
333
+ }
334
+
335
+ const purlKey = purlRepoKey(component.purl)
336
+ const repoRoot = purlKey ? graphifyProvider.resolveRepoRoot(purlKey) : null
337
+
338
+ const fileResult: NonNullable<AuditComponentVerification['file']> = {
339
+ exists: false,
340
+ symbolDeclared: null,
341
+ repoRoot: repoRoot ?? undefined,
342
+ }
343
+ let fileContent: string | null = null
344
+ if (repoRoot && component.file) {
345
+ const abs = join(repoRoot, component.file)
346
+ try {
347
+ await fs.access(abs)
348
+ fileResult.exists = true
349
+ fileContent = await fs.readFile(abs, 'utf8')
350
+ // Symbol presence is graphify's job (exact anchor). No text-regex check.
351
+ } catch {
352
+ fileResult.exists = false
353
+ }
354
+ }
355
+
356
+ if (!purlKey) {
357
+ return {
358
+ ok: true,
359
+ componentAlias,
360
+ file: fileResult,
361
+ cache: { status: 'unavailable', purl: component.purl || '' },
362
+ }
363
+ }
364
+
365
+ const readiness = await graphifyProvider.readiness({
366
+ components: [{ purl: purlKey }],
367
+ })
368
+ const purlStatus = readiness.purls[0]?.status ?? 'unavailable'
369
+ const cacheStatus =
370
+ purlStatus === 'ready'
371
+ ? 'ready'
372
+ : purlStatus === 'missing' || purlStatus === 'building'
373
+ ? 'missing'
374
+ : 'unavailable'
375
+
376
+ const cache: NonNullable<AuditComponentVerification['cache']> = {
377
+ status: cacheStatus,
378
+ purl: purlKey,
379
+ repoRoot: readiness.purls[0]?.repoRoot ?? repoRoot ?? undefined,
380
+ }
381
+
382
+ if (cacheStatus !== 'ready') {
383
+ return { ok: true, componentAlias, file: fileResult, cache }
384
+ }
385
+
386
+ const bundle = await graphifyProvider.loadGraph(purlKey, {
387
+ repoRoot: cache.repoRoot,
388
+ })
389
+ if (!bundle) {
390
+ return {
391
+ ok: true,
392
+ componentAlias,
393
+ file: fileResult,
394
+ cache: { ...cache, status: 'missing' },
395
+ }
396
+ }
397
+
398
+ const nodes = bundle.nodes as GraphifyNode[]
399
+ const edges = bundle.edges as GraphifyEdge[]
400
+ const anchor = resolveComponentAnchor(nodes, {
401
+ file: component.file,
402
+ symbol: component.symbol,
403
+ purl: purlKey,
404
+ })
405
+
406
+ // Symbol presence comes from graphify anchor, not a text regex.
407
+ const claimedSymbol =
408
+ typeof component.symbol === 'string' && component.symbol.trim().length > 0
409
+ if (claimedSymbol) {
410
+ fileResult.symbolDeclared = anchor.resolution === 'exact'
411
+ }
412
+
413
+ let fileSuggest: AuditComponentVerification['fileSuggest']
414
+ let fileCandidates: AuditComponentVerification['fileCandidates']
415
+ if (
416
+ fileResult.exists === false &&
417
+ claimedSymbol &&
418
+ repoRoot &&
419
+ typeof component.symbol === 'string'
420
+ ) {
421
+ const hit = findUniqueDefinitionBySymbol(nodes, component.symbol)
422
+ if (hit.status === 'unique') {
423
+ const suggested = normalizeSourcePath(
424
+ String(hit.node.source_file ?? ''),
425
+ )
426
+ const claimed = component.file
427
+ ? normalizeSourcePath(component.file)
428
+ : ''
429
+ if (
430
+ suggested &&
431
+ suggested !== claimed &&
432
+ existsSync(join(repoRoot, suggested))
433
+ ) {
434
+ fileSuggest = {
435
+ file: suggested,
436
+ nodeId: String(hit.node.id),
437
+ label: String(hit.node.label ?? ''),
438
+ }
439
+ }
440
+ } else if (hit.status === 'ambiguous') {
441
+ const seen = new Set<string>()
442
+ const list: NonNullable<AuditComponentVerification['fileCandidates']> = []
443
+ for (const node of hit.candidates) {
444
+ const file = normalizeSourcePath(String(node.source_file ?? ''))
445
+ if (!file || seen.has(file)) continue
446
+ if (!existsSync(join(repoRoot, file))) continue
447
+ seen.add(file)
448
+ list.push({
449
+ file,
450
+ nodeId: String(node.id),
451
+ label: String(node.label ?? ''),
452
+ })
453
+ }
454
+ if (list.length > 1) fileCandidates = list
455
+ else if (list.length === 1) {
456
+ // Only one candidate still on disk — treat as unique suggest.
457
+ fileSuggest = list[0]
458
+ }
459
+ }
460
+ }
461
+
462
+ const base: AuditComponentVerification = {
463
+ ok: true,
464
+ componentAlias,
465
+ file: fileResult,
466
+ cache,
467
+ anchor: mapAnchor(anchor),
468
+ fileSuggest,
469
+ fileCandidates,
470
+ }
471
+
472
+ // Kind check only after exact anchor; external claims skip.
473
+ if (anchor.resolution !== 'exact' || !anchor.node) {
474
+ return finalizeResult(
475
+ graphId,
476
+ graph.components,
477
+ component,
478
+ anchor,
479
+ fileContent,
480
+ base,
481
+ opts,
482
+ deps,
483
+ )
484
+ }
485
+ if (component.construct === 'external') {
486
+ return finalizeResult(
487
+ graphId,
488
+ graph.components,
489
+ component,
490
+ anchor,
491
+ fileContent,
492
+ base,
493
+ opts,
494
+ deps,
495
+ )
496
+ }
497
+
498
+ const inferred = inferConstructFromGraphify(anchor.node, edges)
499
+ const claimed = String(component.construct ?? '')
500
+
501
+ // Accepted, agent-confirmed construct for this file+symbol, if any. Graphify's
502
+ // inferred construct is a structural hint, not ground truth — an augmentation
503
+ // can confirm the claim when Graphify is silent (`unknown`) *or* disagrees
504
+ // (its vocabulary has no `store`/`custom_entity`, and a call-style accessor
505
+ // label biases it to `function`).
506
+ const aug =
507
+ claimed && component.file && component.symbol
508
+ ? await deps.findAcceptedConstructAugmentation({
509
+ purl: purlKey,
510
+ file: component.file,
511
+ symbol: component.symbol,
512
+ })
513
+ : null
514
+ const augConstruct = aug?.claims.construct?.trim()
515
+
516
+ const outcome = resolveConstructMatch(
517
+ claimed,
518
+ inferred.construct,
519
+ augConstruct,
520
+ )
521
+ const augmentedEvidence = outcome.augmentedBy
522
+ ? [
523
+ `augmented construct ${outcome.augmentedBy} (agent-confirmed, graphify inferred ${inferred.construct})`,
524
+ ...(aug?.evidence ?? []),
525
+ ]
526
+ : []
527
+ const construct: NonNullable<AuditComponentVerification['construct']> = {
528
+ claimed,
529
+ inferred: inferred.construct,
530
+ match: outcome.match,
531
+ evidence: [...inferred.evidence, ...augmentedEvidence],
532
+ ...(outcome.augmentedBy ? { augmented: outcome.augmentedBy } : {}),
533
+ }
534
+
535
+ if (outcome.match === null) {
536
+ return finalizeResult(
537
+ graphId,
538
+ graph.components,
539
+ component,
540
+ anchor,
541
+ fileContent,
542
+ {
543
+ ...base,
544
+ ok: true,
545
+ code: 'construct_unconfirmed',
546
+ construct,
547
+ },
548
+ opts,
549
+ deps,
550
+ )
551
+ }
552
+ if (outcome.match === false) {
553
+ return finalizeResult(
554
+ graphId,
555
+ graph.components,
556
+ component,
557
+ anchor,
558
+ fileContent,
559
+ {
560
+ ...base,
561
+ ok: false,
562
+ code: 'construct_mismatch',
563
+ error: `construct mismatch: claimed ${claimed}, inferred ${inferred.construct}`,
564
+ construct,
565
+ },
566
+ opts,
567
+ deps,
568
+ )
569
+ }
570
+
571
+ const withKind: AuditComponentVerification = { ...base, construct }
572
+
573
+ // Store completeness — an in-memory store declares the type it holds, the
574
+ // way every other declaration declares its signature. A gap, never a hard
575
+ // failure: the agent reads the declaration and authors `declaration.valueType`.
576
+ const withStoreType: AuditComponentVerification = {
577
+ ...withKind,
578
+ ...(storeTypeUndeclared(component) ? { storeTypeUndeclared: true } : {}),
579
+ }
580
+
581
+ // Signature / params — function & method only, after kind ok.
582
+ if (claimed !== 'function' && claimed !== 'method') {
583
+ return finalizeResult(
584
+ graphId,
585
+ graph.components,
586
+ component,
587
+ anchor,
588
+ fileContent,
589
+ withStoreType,
590
+ opts,
591
+ deps,
592
+ )
593
+ }
594
+
595
+ const nodesById = new Map(nodes.map((n) => [String(n.id), n]))
596
+ const inferredSig = extractGraphifySignature(
597
+ String(anchor.node.id),
598
+ edges,
599
+ nodesById,
600
+ )
601
+ const claimedForCompare =
602
+ component.declaration &&
603
+ (component.declaration.kind === 'function' ||
604
+ component.declaration.kind === 'method')
605
+ ? {
606
+ parameters:
607
+ 'parameters' in component.declaration
608
+ ? (component.declaration.parameters as Array<{
609
+ name?: string
610
+ type: string
611
+ }>)
612
+ : undefined,
613
+ returnType:
614
+ 'returnType' in component.declaration
615
+ ? (component.declaration.returnType as string | undefined)
616
+ : undefined,
617
+ }
618
+ : undefined
619
+
620
+ const sig = compareSignatures(claimedForCompare, inferredSig)
621
+ let signature: NonNullable<AuditComponentVerification['signature']> = {
622
+ match: sig.match,
623
+ skipped: sig.skipped,
624
+ reason: sig.reason,
625
+ skipCode: sig.skipCode,
626
+ claimed: sig.claimed,
627
+ inferred: sig.inferred,
628
+ }
629
+
630
+ // Graphify has no usable signature edges. An accepted, agent-extracted
631
+ // augmentation IS the confirmation — there is nothing deterministic to
632
+ // compare against: Graphify is silent, and comparing to the model's own
633
+ // declaration would be circular. Trust it, and label it as agent-confirmed
634
+ // so it is never mistaken for a Graphify-verified match.
635
+ if (sig.skipped && component.file && component.symbol) {
636
+ const aug = await deps.findAcceptedSignatureAugmentation({
637
+ purl: purlKey,
638
+ file: component.file,
639
+ symbol: component.symbol,
640
+ })
641
+ const augSig = aug?.claims.signature
642
+ if (augSig) {
643
+ signature = {
644
+ match: true,
645
+ skipped: false,
646
+ reason: 'augmented signature (agent-extracted, human-confirmed)',
647
+ claimed: sig.claimed,
648
+ inferred: sig.inferred,
649
+ augmented: augSig,
650
+ }
651
+ }
652
+ }
653
+
654
+ if (!signature.match && !signature.skipped) {
655
+ return finalizeResult(
656
+ graphId,
657
+ graph.components,
658
+ component,
659
+ anchor,
660
+ fileContent,
661
+ {
662
+ ...withKind,
663
+ ok: false,
664
+ code: 'signature_mismatch',
665
+ error: `signature mismatch: ${sig.reason ?? 'types differ'} (claimed params [${sig.claimed.parameterTypes.join(', ')}] vs [${sig.inferred.parameterTypes.join(', ')}]; return [${sig.claimed.returnTypes.join(', ')}] vs [${sig.inferred.returnTypes.join(', ')}])`,
666
+ signature,
667
+ },
668
+ opts,
669
+ deps,
670
+ )
671
+ }
672
+
673
+ return finalizeResult(
674
+ graphId,
675
+ graph.components,
676
+ component,
677
+ anchor,
678
+ fileContent,
679
+ { ...withStoreType, signature },
680
+ opts,
681
+ deps,
682
+ )
683
+ }
684
+
685
+ /** Verdict category for a single component verification result. */
686
+ export function verifyVerdict(r: AuditComponentVerification): {
687
+ category: string
688
+ detail?: string
689
+ } {
690
+ if (!r.ok) {
691
+ if (r.cache && r.cache.status !== 'ready') {
692
+ return { category: `cache_${r.cache.status}`, detail: r.cache.purl }
693
+ }
694
+ switch (r.code) {
695
+ case 'construct_mismatch':
696
+ return {
697
+ category: 'construct_mismatch',
698
+ detail: `claimed ${r.construct?.claimed}, inferred ${r.construct?.inferred}`,
699
+ }
700
+ case 'signature_mismatch':
701
+ return {
702
+ category: 'signature_mismatch',
703
+ detail: r.error,
704
+ }
705
+ default:
706
+ return { category: 'error', detail: r.error }
707
+ }
708
+ }
709
+ if (r.construct?.match === null || r.code === 'construct_unconfirmed') {
710
+ return {
711
+ category: 'construct_unconfirmed',
712
+ detail: `claimed ${r.construct?.claimed}, inferred unknown`,
713
+ }
714
+ }
715
+ if (r.construct?.augmented) {
716
+ return {
717
+ category: 'construct_augmented',
718
+ detail: `claimed ${r.construct.claimed}, graphify inferred ${r.construct.inferred}, agent-confirmed`,
719
+ }
720
+ }
721
+ if (r.storeTypeUndeclared) {
722
+ return { category: 'store_type_undeclared' }
723
+ }
724
+ if (r.cache && r.cache.status !== 'ready') {
725
+ return { category: `cache_${r.cache.status}`, detail: r.cache.purl }
726
+ }
727
+ if (!r.anchor || r.anchor.resolution !== 'exact') {
728
+ return {
729
+ category: `anchor_${r.anchor?.resolution ?? 'missing'}`,
730
+ detail: r.anchor?.nodeId,
731
+ }
732
+ }
733
+ if (r.signature) {
734
+ if (r.signature.skipped) {
735
+ const detail = r.signature.reason
736
+ switch (r.signature.skipCode) {
737
+ case 'no_claimed_types':
738
+ return { category: 'signature_no_claimed_types', detail }
739
+ case 'generic_arg_only':
740
+ return { category: 'signature_generic_arg_only', detail }
741
+ case 'partially_generic_arg':
742
+ return { category: 'signature_partially_generic_arg', detail }
743
+ case 'unresolved_claimed_types':
744
+ return { category: 'signature_unresolved_claimed_types', detail }
745
+ default:
746
+ return { category: 'signature_skipped', detail }
747
+ }
748
+ }
749
+ if (r.signature.augmented) {
750
+ return {
751
+ category: 'signature_augmented',
752
+ detail: 'agent-extracted, human-confirmed',
753
+ }
754
+ }
755
+ return r.signature.match
756
+ ? { category: 'signature_match' }
757
+ : { category: 'signature_mismatch', detail: r.error }
758
+ }
759
+ return { category: 'construct_only' }
760
+ }