@principal-ai/subsystems-core 0.51.4 → 0.51.5
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 +21 -6
- package/dist/audit/boundary.d.ts +104 -0
- package/dist/audit/boundary.d.ts.map +1 -0
- package/dist/audit/boundary.js +363 -0
- package/dist/audit/boundary.js.map +1 -0
- package/dist/audit/c4.d.ts +41 -0
- package/dist/audit/c4.d.ts.map +1 -0
- package/dist/audit/c4.js +41 -0
- package/dist/audit/c4.js.map +1 -0
- package/dist/audit/engine.d.ts +135 -0
- package/dist/audit/engine.d.ts.map +1 -0
- package/dist/audit/engine.js +828 -0
- package/dist/audit/engine.js.map +1 -0
- package/dist/audit/fixes.d.ts +80 -0
- package/dist/audit/fixes.d.ts.map +1 -0
- package/dist/audit/fixes.js +162 -0
- package/dist/audit/fixes.js.map +1 -0
- package/dist/audit/hash.d.ts +25 -0
- package/dist/audit/hash.d.ts.map +1 -0
- package/dist/audit/hash.js +53 -0
- package/dist/audit/hash.js.map +1 -0
- package/dist/audit/index.d.ts +11 -1
- package/dist/audit/index.d.ts.map +1 -1
- package/dist/audit/index.js +37 -3
- package/dist/audit/index.js.map +1 -1
- package/dist/audit/local-graphify.d.ts +75 -0
- package/dist/audit/local-graphify.d.ts.map +1 -0
- package/dist/audit/local-graphify.js +283 -0
- package/dist/audit/local-graphify.js.map +1 -0
- package/dist/audit/paths.d.ts +16 -0
- package/dist/audit/paths.d.ts.map +1 -0
- package/dist/audit/paths.js +31 -0
- package/dist/audit/paths.js.map +1 -0
- package/dist/audit/pure.d.ts +18 -0
- package/dist/audit/pure.d.ts.map +1 -0
- package/dist/audit/pure.js +50 -0
- package/dist/audit/pure.js.map +1 -0
- package/dist/audit/report-logic.d.ts +17 -0
- package/dist/audit/report-logic.d.ts.map +1 -0
- package/dist/audit/report-logic.js +206 -0
- package/dist/audit/report-logic.js.map +1 -0
- package/dist/audit/report.d.ts +136 -0
- package/dist/audit/report.d.ts.map +1 -0
- package/dist/audit/report.js +3 -0
- package/dist/audit/report.js.map +1 -0
- package/dist/audit/trail.d.ts +29 -0
- package/dist/audit/trail.d.ts.map +1 -0
- package/dist/audit/trail.js +59 -0
- package/dist/audit/trail.js.map +1 -0
- package/dist/audit/verification.d.ts +98 -0
- package/dist/audit/verification.d.ts.map +1 -0
- package/dist/audit/verification.js +11 -0
- package/dist/audit/verification.js.map +1 -0
- package/dist/audit/verify/component.d.ts +68 -0
- package/dist/audit/verify/component.d.ts.map +1 -0
- package/dist/audit/verify/component.js +500 -0
- package/dist/audit/verify/component.js.map +1 -0
- package/dist/audit/verify/declarations.d.ts +58 -0
- package/dist/audit/verify/declarations.d.ts.map +1 -0
- package/dist/audit/verify/declarations.js +227 -0
- package/dist/audit/verify/declarations.js.map +1 -0
- package/dist/audit/verify/grammar.d.ts +12 -0
- package/dist/audit/verify/grammar.d.ts.map +1 -0
- package/dist/audit/verify/grammar.js +57 -0
- package/dist/audit/verify/grammar.js.map +1 -0
- package/dist/audit/verify/index.d.ts +12 -0
- package/dist/audit/verify/index.d.ts.map +1 -0
- package/dist/audit/verify/index.js +31 -0
- package/dist/audit/verify/index.js.map +1 -0
- package/dist/audit/verify/parser.d.ts +19 -0
- package/dist/audit/verify/parser.d.ts.map +1 -0
- package/dist/audit/verify/parser.js +122 -0
- package/dist/audit/verify/parser.js.map +1 -0
- package/dist/audit/verify/span.d.ts +49 -0
- package/dist/audit/verify/span.d.ts.map +1 -0
- package/dist/audit/verify/span.js +377 -0
- package/dist/audit/verify/span.js.map +1 -0
- package/dist/graphify/anchor.d.ts +45 -0
- package/dist/graphify/anchor.d.ts.map +1 -0
- package/dist/graphify/anchor.js +179 -0
- package/dist/graphify/anchor.js.map +1 -0
- package/dist/graphify/consolidated.d.ts +259 -0
- package/dist/graphify/consolidated.d.ts.map +1 -0
- package/dist/graphify/consolidated.js +12 -0
- package/dist/graphify/consolidated.js.map +1 -0
- package/dist/graphify/construct.d.ts +41 -0
- package/dist/graphify/construct.d.ts.map +1 -0
- package/dist/graphify/construct.js +106 -0
- package/dist/graphify/construct.js.map +1 -0
- package/dist/graphify/ids.d.ts +19 -0
- package/dist/graphify/ids.d.ts.map +1 -0
- package/dist/graphify/ids.js +53 -0
- package/dist/graphify/ids.js.map +1 -0
- package/dist/graphify/index.d.ts +16 -0
- package/dist/graphify/index.d.ts.map +1 -0
- package/dist/graphify/index.js +32 -0
- package/dist/graphify/index.js.map +1 -0
- package/dist/graphify/resolve.d.ts +48 -0
- package/dist/graphify/resolve.d.ts.map +1 -0
- package/dist/graphify/resolve.js +89 -0
- package/dist/graphify/resolve.js.map +1 -0
- package/dist/graphify/signature.d.ts +87 -0
- package/dist/graphify/signature.d.ts.map +1 -0
- package/dist/graphify/signature.js +282 -0
- package/dist/graphify/signature.js.map +1 -0
- package/dist/graphify/types.d.ts +141 -0
- package/dist/graphify/types.d.ts.map +1 -0
- package/dist/graphify/types.js +9 -0
- package/dist/graphify/types.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +16 -2
- package/src/audit/boundary.ts +447 -0
- package/src/audit/c4.ts +70 -0
- package/src/audit/engine.ts +1058 -0
- package/src/audit/fixes.ts +192 -0
- package/src/audit/hash.ts +58 -0
- package/src/audit/index.ts +33 -8
- package/src/audit/local-graphify.test.ts +163 -0
- package/src/audit/local-graphify.ts +354 -0
- package/src/audit/paths.ts +25 -0
- package/src/audit/pure.ts +91 -0
- package/src/audit/report-logic.test.ts +145 -0
- package/src/audit/report-logic.ts +245 -0
- package/src/audit/report.ts +203 -0
- package/src/audit/trail.ts +77 -0
- package/src/audit/verification.ts +104 -0
- package/src/audit/verify/component.ts +760 -0
- package/src/audit/verify/declarations.ts +284 -0
- package/src/audit/verify/grammar.ts +57 -0
- package/src/audit/verify/index.ts +41 -0
- package/src/audit/verify/parser.ts +124 -0
- package/src/audit/verify/span.ts +478 -0
- package/src/graphify/anchor.ts +206 -0
- package/src/graphify/consolidated.ts +298 -0
- package/src/graphify/construct.ts +153 -0
- package/src/graphify/ids.ts +48 -0
- package/src/graphify/index.ts +16 -0
- package/src/graphify/resolve.ts +115 -0
- package/src/graphify/signature.ts +354 -0
- package/src/graphify/types.ts +164 -0
- package/src/index.ts +3 -3
|
@@ -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
|
+
}
|