@principal-ai/subsystems-core 0.51.3 → 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 -0
- 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/graphify-provider.d.ts +51 -0
- package/dist/audit/graphify-provider.d.ts.map +1 -0
- package/dist/audit/graphify-provider.js +36 -0
- package/dist/audit/graphify-provider.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 +12 -0
- package/dist/audit/index.d.ts.map +1 -0
- package/dist/audit/index.js +40 -0
- package/dist/audit/index.js.map +1 -0
- 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 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1 -0
- package/dist/node.js.map +1 -1
- package/package.json +22 -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/graphify-provider.test.ts +58 -0
- package/src/audit/graphify-provider.ts +77 -0
- package/src/audit/hash.ts +58 -0
- package/src/audit/index.ts +34 -0
- 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 +4 -0
- package/src/node.ts +1 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic audit fixes and small completeness predicates.
|
|
3
|
+
*
|
|
4
|
+
* These are pure projections of a verification result into a one-click fix, or
|
|
5
|
+
* of a component into a completeness gap. They are shared so the engine and the
|
|
6
|
+
* UI offer the same repairs.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
SubsystemComponent,
|
|
11
|
+
SubsystemSourceSpan,
|
|
12
|
+
} from '../types/subsystem-model'
|
|
13
|
+
import type {
|
|
14
|
+
SubsystemModelAuditFinding,
|
|
15
|
+
SubsystemModelAuditFix,
|
|
16
|
+
} from './report'
|
|
17
|
+
import type {
|
|
18
|
+
AuditDeclarationCheck,
|
|
19
|
+
AuditSignatureCheck,
|
|
20
|
+
} from './verification'
|
|
21
|
+
|
|
22
|
+
/** Model has no named types; graphify has bags — safe deterministic fill. */
|
|
23
|
+
export function isAdoptableEmptyClaimedSignature(sig: {
|
|
24
|
+
claimed: { parameterTypes: string[]; returnTypes: string[] }
|
|
25
|
+
inferred: { parameterTypes: string[]; returnTypes: string[] }
|
|
26
|
+
}): boolean {
|
|
27
|
+
const claimedEmpty =
|
|
28
|
+
sig.claimed.parameterTypes.length === 0 &&
|
|
29
|
+
sig.claimed.returnTypes.length === 0
|
|
30
|
+
const inferredHas =
|
|
31
|
+
sig.inferred.parameterTypes.length > 0 || sig.inferred.returnTypes.length > 0
|
|
32
|
+
return claimedEmpty && inferredHas
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function adoptGraphifySignatureFixFromVerify(
|
|
36
|
+
sig: AuditSignatureCheck,
|
|
37
|
+
): Extract<SubsystemModelAuditFix, { id: 'adopt_graphify_signature' }> | undefined {
|
|
38
|
+
if (!isAdoptableEmptyClaimedSignature(sig)) return undefined
|
|
39
|
+
return {
|
|
40
|
+
id: 'adopt_graphify_signature',
|
|
41
|
+
label: 'Adopt graphify signature types',
|
|
42
|
+
parameterTypes: [...sig.inferred.parameterTypes],
|
|
43
|
+
returnTypes: [...sig.inferred.returnTypes],
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function adoptGraphifyFileFixFromVerify(
|
|
48
|
+
component: { file?: string },
|
|
49
|
+
suggest: { file: string },
|
|
50
|
+
): Extract<SubsystemModelAuditFix, { id: 'adopt_graphify_file' }> {
|
|
51
|
+
return {
|
|
52
|
+
id: 'adopt_graphify_file',
|
|
53
|
+
label: `Update file to ${suggest.file}`,
|
|
54
|
+
file: suggest.file,
|
|
55
|
+
previousFile: component.file,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Exact Graphify anchor + a content-preserving move → one-click re-pin.
|
|
61
|
+
*
|
|
62
|
+
* Graphify supplies only the start line, so the candidate span is the stored
|
|
63
|
+
* span shifted to the new start with its length preserved. It is offered only
|
|
64
|
+
* when the shifted range re-hashes to the stored `contentHash` — i.e. the
|
|
65
|
+
* construct moved intact. A changed hash is not a move; it needs the agent lane.
|
|
66
|
+
*/
|
|
67
|
+
export function adoptGraphifyDeclarationRefFixFromVerify(
|
|
68
|
+
component: { declarationSpan?: SubsystemSourceSpan },
|
|
69
|
+
declaration: AuditDeclarationCheck,
|
|
70
|
+
anchor: { resolution: string } | undefined,
|
|
71
|
+
): Extract<
|
|
72
|
+
SubsystemModelAuditFix,
|
|
73
|
+
{ id: 'adopt_graphify_declaration_ref' }
|
|
74
|
+
> | undefined {
|
|
75
|
+
if (declaration.freshness !== 'stale') return undefined
|
|
76
|
+
if (anchor?.resolution !== 'exact') return undefined
|
|
77
|
+
const repin = declaration.repin
|
|
78
|
+
const stored = component.declarationSpan
|
|
79
|
+
if (!repin || !stored) return undefined
|
|
80
|
+
// Only a content-preserving move is safe to re-pin.
|
|
81
|
+
if (repin.contentHash !== stored.contentHash) return undefined
|
|
82
|
+
if (repin.startLine === stored.startLine) return undefined
|
|
83
|
+
return {
|
|
84
|
+
id: 'adopt_graphify_declaration_ref',
|
|
85
|
+
label: `Re-pin declaration to L${repin.startLine} (was L${stored.startLine})`,
|
|
86
|
+
declarationSpan: repin,
|
|
87
|
+
previousStartLine: stored.startLine,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A grounded claim with no stored span whose declaration tree-sitter can pin
|
|
93
|
+
* exactly — every structural gate passed, so the derived `[start, end]` range
|
|
94
|
+
* and its content hash are deterministic.
|
|
95
|
+
*/
|
|
96
|
+
export function adoptTreeSitterSpanFixFromVerify(
|
|
97
|
+
declaration: AuditDeclarationCheck,
|
|
98
|
+
): Extract<SubsystemModelAuditFix, { id: 'adopt_tree_sitter_span' }> | undefined {
|
|
99
|
+
const span = declaration.derivation?.span
|
|
100
|
+
if (declaration.derivation?.class !== 'trivial' || !span) return undefined
|
|
101
|
+
return {
|
|
102
|
+
id: 'adopt_tree_sitter_span',
|
|
103
|
+
label: `Capture declaration span L${span.startLine}–${span.endLine}`,
|
|
104
|
+
declarationSpan: span,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A store's declared value type, normalized. Null when it declares none. */
|
|
109
|
+
function storeValueTypeOf(
|
|
110
|
+
declaration: { valueType?: string } | undefined,
|
|
111
|
+
): string | null {
|
|
112
|
+
const value = declaration?.valueType
|
|
113
|
+
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Store completeness — an in-memory store declares the type it holds.
|
|
118
|
+
*
|
|
119
|
+
* A store is a state declaration, so it says what it declares: either a
|
|
120
|
+
* `valueType` (`Map<string, FeedState>`) or named state members. A memory store
|
|
121
|
+
* with neither has no declared type at all. Never a hard failure.
|
|
122
|
+
*/
|
|
123
|
+
export function storeTypeUndeclared(component: SubsystemComponent): boolean {
|
|
124
|
+
if (component.construct !== 'store') return false
|
|
125
|
+
const declaration = component.declaration
|
|
126
|
+
if (!declaration || declaration.kind !== 'store') return false
|
|
127
|
+
if (declaration.storage !== 'memory') return false
|
|
128
|
+
return (
|
|
129
|
+
!storeValueTypeOf(declaration) && (declaration.properties ?? []).length === 0
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A store whose pinned declaration moved — its declared value type may be out
|
|
135
|
+
* of date. Scoped to stores that declare a value type.
|
|
136
|
+
*/
|
|
137
|
+
export function storeTypeStale(
|
|
138
|
+
component: SubsystemComponent,
|
|
139
|
+
freshness: string | undefined,
|
|
140
|
+
): boolean {
|
|
141
|
+
if (freshness !== 'stale') return false
|
|
142
|
+
const declaration = component.declaration
|
|
143
|
+
if (!declaration || declaration.kind !== 'store') return false
|
|
144
|
+
return storeValueTypeOf(declaration) != null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The npm package a path is installed from, when it sits in an install root. */
|
|
148
|
+
export interface InstalledDependency {
|
|
149
|
+
packageName: string
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The npm package an installed path belongs to — `…/node_modules/@pierre/diffs/dist/x.js`
|
|
154
|
+
* → `@pierre/diffs` — or null when the path is ordinary repo source.
|
|
155
|
+
*/
|
|
156
|
+
export function installedDependencyPackage(
|
|
157
|
+
file: string | undefined,
|
|
158
|
+
): InstalledDependency | null {
|
|
159
|
+
if (!file) return null
|
|
160
|
+
const segments = file.split('/')
|
|
161
|
+
const at = segments.lastIndexOf('node_modules')
|
|
162
|
+
if (at < 0) return null
|
|
163
|
+
const name = segments[at + 1]
|
|
164
|
+
if (!name) return null
|
|
165
|
+
// Scoped names take two segments (`@scope/pkg`); unscoped names end at `.`.
|
|
166
|
+
const scoped = name.startsWith('@') ? segments[at + 2] : undefined
|
|
167
|
+
if (name.startsWith('@') && !scoped) return null
|
|
168
|
+
return { packageName: scoped ? `${name}/${scoped}` : name }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Keep a parser/runtime failure legible in the audit without dumping paths. */
|
|
172
|
+
export function verificationFailureDiagnostic(error: unknown): string {
|
|
173
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
174
|
+
if (/\bcall_indirect\b|_ts_parser_new_wasm/i.test(message)) {
|
|
175
|
+
return 'Tree-sitter WASM parser aborted (out-of-bounds indirect call)'
|
|
176
|
+
}
|
|
177
|
+
const name = error instanceof Error && error.name ? error.name : 'Error'
|
|
178
|
+
return `component verification threw ${name}`
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function verificationUnavailableFinding(
|
|
182
|
+
purl: string,
|
|
183
|
+
error: unknown,
|
|
184
|
+
): SubsystemModelAuditFinding {
|
|
185
|
+
const diagnostic = verificationFailureDiagnostic(error)
|
|
186
|
+
return {
|
|
187
|
+
kind: 'graphify_unavailable',
|
|
188
|
+
severity: 'info',
|
|
189
|
+
purl,
|
|
190
|
+
message: `Source verification aborted for ${purl}: ${diagnostic}. Retry the audit; if it repeats, inspect the host logs.`,
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
cacheGraphifyAuditProvider,
|
|
4
|
+
type GraphifyAuditProvider,
|
|
5
|
+
type GraphifyAuditSnapshot,
|
|
6
|
+
} from './graphify-provider'
|
|
7
|
+
|
|
8
|
+
function provider(loadGraph: GraphifyAuditProvider["loadGraph"]): GraphifyAuditProvider {
|
|
9
|
+
return {
|
|
10
|
+
ensureCurrent: async () => ({ ensured: [], failed: [] }),
|
|
11
|
+
readiness: async () => ({ status: 'possible', purls: [] }),
|
|
12
|
+
resolveRepoRoot: () => null,
|
|
13
|
+
loadGraph,
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('cacheGraphifyAuditProvider', () => {
|
|
18
|
+
test('deduplicates concurrent graph loads by PURL', async () => {
|
|
19
|
+
let loads = 0
|
|
20
|
+
let resolve!: (graph: GraphifyAuditSnapshot) => void
|
|
21
|
+
const source = provider(
|
|
22
|
+
() => {
|
|
23
|
+
loads++
|
|
24
|
+
return new Promise((done) => {
|
|
25
|
+
resolve = done
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
)
|
|
29
|
+
const cached = cacheGraphifyAuditProvider(source)
|
|
30
|
+
const first = cached.loadGraph('pkg:github/acme/app')
|
|
31
|
+
const second = cached.loadGraph('pkg:github/acme/app')
|
|
32
|
+
|
|
33
|
+
expect(loads).toBe(1)
|
|
34
|
+
const graph = { nodes: [], edges: [] }
|
|
35
|
+
resolve(graph)
|
|
36
|
+
expect(await first).toBe(graph)
|
|
37
|
+
expect(await second).toBe(graph)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('evicts a rejected load so a later attempt can retry', async () => {
|
|
41
|
+
let loads = 0
|
|
42
|
+
const source = provider(async () => {
|
|
43
|
+
loads++
|
|
44
|
+
if (loads === 1) throw new Error('temporary read failure')
|
|
45
|
+
return { nodes: [], edges: [] }
|
|
46
|
+
})
|
|
47
|
+
const cached = cacheGraphifyAuditProvider(source)
|
|
48
|
+
|
|
49
|
+
await expect(cached.loadGraph('pkg:github/acme/app')).rejects.toThrow(
|
|
50
|
+
'temporary read failure',
|
|
51
|
+
)
|
|
52
|
+
expect(await cached.loadGraph('pkg:github/acme/app')).toEqual({
|
|
53
|
+
nodes: [],
|
|
54
|
+
edges: [],
|
|
55
|
+
})
|
|
56
|
+
expect(loads).toBe(2)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graphify port used by subsystem-model audits.
|
|
3
|
+
*
|
|
4
|
+
* The audit should depend on graph data and freshness semantics, not on how a
|
|
5
|
+
* graph was produced or served. Implementations may read the local cache, run
|
|
6
|
+
* Graphify on demand, or call a Graphify MCP service.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface GraphifyAuditModel {
|
|
10
|
+
components: Array<{
|
|
11
|
+
purl?: string
|
|
12
|
+
construct?: string
|
|
13
|
+
}>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface GraphifyAuditEnsureResult {
|
|
17
|
+
ensured: string[]
|
|
18
|
+
failed: Array<{ purl: string; error: string }>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GraphifyAuditPurlReadiness {
|
|
22
|
+
purl: string
|
|
23
|
+
status: 'ready' | 'missing' | 'building' | 'unavailable'
|
|
24
|
+
repoRoot?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface GraphifyAuditReadiness {
|
|
28
|
+
status: 'possible' | 'partial' | 'not_ready' | 'running' | 'unavailable'
|
|
29
|
+
purls: GraphifyAuditPurlReadiness[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface GraphifyAuditSnapshot {
|
|
33
|
+
nodes: unknown[]
|
|
34
|
+
edges: unknown[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface GraphifyAuditProvider {
|
|
38
|
+
/** Ensure current-HEAD(+dirty) graphs for the model's local repositories. */
|
|
39
|
+
ensureCurrent(model: GraphifyAuditModel): Promise<GraphifyAuditEnsureResult>
|
|
40
|
+
/** Describe current-HEAD(+dirty) graph availability without building. */
|
|
41
|
+
readiness(
|
|
42
|
+
model: GraphifyAuditModel,
|
|
43
|
+
buildingPurls?: ReadonlySet<string>,
|
|
44
|
+
): Promise<GraphifyAuditReadiness>
|
|
45
|
+
/** Resolve a repo PURL to a local checkout, when one is available. */
|
|
46
|
+
resolveRepoRoot(purl: string): string | null
|
|
47
|
+
/** Load the graph for a PURL after ensure/readiness has been checked. */
|
|
48
|
+
loadGraph(
|
|
49
|
+
purl: string,
|
|
50
|
+
options?: { repoRoot?: string },
|
|
51
|
+
): Promise<GraphifyAuditSnapshot | null>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Keep one Graphify read/parse per repository for a single audit pass.
|
|
56
|
+
* Rejected reads are evicted so a later attempt can recover.
|
|
57
|
+
*/
|
|
58
|
+
export function cacheGraphifyAuditProvider(
|
|
59
|
+
provider: GraphifyAuditProvider,
|
|
60
|
+
): GraphifyAuditProvider {
|
|
61
|
+
const snapshots = new Map<string, Promise<GraphifyAuditSnapshot | null>>()
|
|
62
|
+
return {
|
|
63
|
+
ensureCurrent: (model) => provider.ensureCurrent(model),
|
|
64
|
+
readiness: (model, buildingPurls) => provider.readiness(model, buildingPurls),
|
|
65
|
+
resolveRepoRoot: (purl) => provider.resolveRepoRoot(purl),
|
|
66
|
+
loadGraph(purl, options) {
|
|
67
|
+
const existing = snapshots.get(purl)
|
|
68
|
+
if (existing) return existing
|
|
69
|
+
const pending = provider.loadGraph(purl, options)
|
|
70
|
+
snapshots.set(purl, pending)
|
|
71
|
+
void pending.catch(() => {
|
|
72
|
+
if (snapshots.get(purl) === pending) snapshots.delete(purl)
|
|
73
|
+
})
|
|
74
|
+
return pending
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source-span hashing (Node crypto).
|
|
3
|
+
*
|
|
4
|
+
* The audit compares a stored span's `contentHash` against the live source.
|
|
5
|
+
* The normalization and digest must stay aligned with the viewer's
|
|
6
|
+
* `normalizeDeclarationLine` (algo v1), so this lives in the shared engine and
|
|
7
|
+
* both hosts hash identically.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
import type { SubsystemSourceSpan } from '../types/subsystem-model'
|
|
12
|
+
|
|
13
|
+
/** Normalize one declaration line before hashing (algo v1). */
|
|
14
|
+
export function normalizeDeclarationLine(line: string): string {
|
|
15
|
+
return line.replace(/\r$/, '').trimEnd()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Parse graphify `source_location` (`"L42"`) → 1-based line number. */
|
|
19
|
+
export function parseSourceLocation(
|
|
20
|
+
loc: string | undefined | null,
|
|
21
|
+
): number | null {
|
|
22
|
+
if (!loc?.trim()) return null
|
|
23
|
+
const m = /^L(\d+)$/.exec(loc.trim())
|
|
24
|
+
if (!m) return null
|
|
25
|
+
const n = Number.parseInt(m[1]!, 10)
|
|
26
|
+
return Number.isFinite(n) && n >= 1 ? n : null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Assemble a span from its parts (no I/O). */
|
|
30
|
+
export function buildSourceSpan(input: {
|
|
31
|
+
startLine: number
|
|
32
|
+
endLine: number
|
|
33
|
+
contentHash: string
|
|
34
|
+
}): SubsystemSourceSpan {
|
|
35
|
+
return {
|
|
36
|
+
startLine: input.startLine,
|
|
37
|
+
endLine: input.endLine,
|
|
38
|
+
contentHash: input.contentHash,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Hash a range of lines (1-based, inclusive) — the span's content fingerprint.
|
|
44
|
+
* Returns null if the range is invalid or out of bounds.
|
|
45
|
+
*/
|
|
46
|
+
export function hashContentRange(
|
|
47
|
+
content: string,
|
|
48
|
+
start: number,
|
|
49
|
+
end: number,
|
|
50
|
+
): string | null {
|
|
51
|
+
const lines = content.split(/\r?\n/)
|
|
52
|
+
if (start < 1 || end < start || end > lines.length) return null
|
|
53
|
+
const slice = lines
|
|
54
|
+
.slice(start - 1, end)
|
|
55
|
+
.map(normalizeDeclarationLine)
|
|
56
|
+
.join('\n')
|
|
57
|
+
return createHash('sha256').update(slice, 'utf8').digest('hex').slice(0, 32)
|
|
58
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Full audit surface (Node-only).
|
|
3
|
+
*
|
|
4
|
+
* Pure policy and types are re-exported from `./pure`; the engine and source
|
|
5
|
+
* hashing live here because they touch the filesystem and Node crypto.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './pure'
|
|
9
|
+
export {
|
|
10
|
+
buildAuditFingerprint,
|
|
11
|
+
runSubsystemModelAudit,
|
|
12
|
+
type AuditCallSiteClaim,
|
|
13
|
+
type AuditEngineDeps,
|
|
14
|
+
type AuditEngineResult,
|
|
15
|
+
type AuditGraphifySlot,
|
|
16
|
+
type AuditModel,
|
|
17
|
+
type AuditModelFileCheck,
|
|
18
|
+
} from './engine'
|
|
19
|
+
export {
|
|
20
|
+
buildSourceSpan,
|
|
21
|
+
hashContentRange,
|
|
22
|
+
normalizeDeclarationLine,
|
|
23
|
+
parseSourceLocation,
|
|
24
|
+
} from './hash'
|
|
25
|
+
export * from './verify'
|
|
26
|
+
export {
|
|
27
|
+
cacheSlotKey,
|
|
28
|
+
createLocalGraphifyAuditProvider,
|
|
29
|
+
LocalGraphifyCache,
|
|
30
|
+
localGraphifyStoreRoot,
|
|
31
|
+
sanitizePurlDirName,
|
|
32
|
+
type CurrentLocalGraphifySlot,
|
|
33
|
+
type LocalGraphifyConfig,
|
|
34
|
+
} from './local-graphify'
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression coverage for the shared local Graphify cache reader.
|
|
3
|
+
*
|
|
4
|
+
* Studio and the CLI must agree on the HEAD(+dirty) slot identity, so this
|
|
5
|
+
* pins the on-disk layout, readiness states, and fingerprint inputs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, test } from 'bun:test'
|
|
9
|
+
import { spawnSync } from 'node:child_process'
|
|
10
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { tmpdir } from 'node:os'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import {
|
|
14
|
+
cacheSlotKey,
|
|
15
|
+
createLocalGraphifyAuditProvider,
|
|
16
|
+
sanitizePurlDirName,
|
|
17
|
+
} from './local-graphify'
|
|
18
|
+
|
|
19
|
+
const PURL = 'pkg:github/acme/widget'
|
|
20
|
+
|
|
21
|
+
function git(root: string, args: string[]): string {
|
|
22
|
+
const res = spawnSync('git', ['-C', root, ...args], {
|
|
23
|
+
encoding: 'utf8',
|
|
24
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
25
|
+
})
|
|
26
|
+
if (res.status !== 0) throw new Error(`git ${args.join(' ')} failed`)
|
|
27
|
+
return res.stdout ?? ''
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makeRepo(): { root: string; head: string } {
|
|
31
|
+
const root = mkdtempSync(join(tmpdir(), 'lf-repo-'))
|
|
32
|
+
git(root, ['init', '-q'])
|
|
33
|
+
git(root, ['config', 'user.email', 'test@example.com'])
|
|
34
|
+
git(root, ['config', 'user.name', 'Test'])
|
|
35
|
+
writeFileSync(join(root, 'a.ts'), 'export const a = 1\n')
|
|
36
|
+
git(root, ['add', '.'])
|
|
37
|
+
git(root, ['commit', '-q', '-m', 'init'])
|
|
38
|
+
return { root, head: git(root, ['rev-parse', 'HEAD']).trim() }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function makeStore(): string {
|
|
42
|
+
return mkdtempSync(join(tmpdir(), 'lf-store-'))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function writeSlot(
|
|
46
|
+
storeRoot: string,
|
|
47
|
+
slotKey: string,
|
|
48
|
+
graph: { nodes: unknown[]; links: unknown[] },
|
|
49
|
+
builtAt: string,
|
|
50
|
+
): void {
|
|
51
|
+
const dir = join(
|
|
52
|
+
storeRoot,
|
|
53
|
+
sanitizePurlDirName(PURL),
|
|
54
|
+
slotKey,
|
|
55
|
+
)
|
|
56
|
+
mkdirSync(dir, { recursive: true })
|
|
57
|
+
writeFileSync(join(dir, 'graph.json'), JSON.stringify(graph))
|
|
58
|
+
writeFileSync(join(dir, 'meta.json'), JSON.stringify({ builtAt }))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('local graphify cache', () => {
|
|
62
|
+
test('ready slot loads the graph and yields a stable fingerprint', async () => {
|
|
63
|
+
const { root, head } = makeRepo()
|
|
64
|
+
const store = makeStore()
|
|
65
|
+
try {
|
|
66
|
+
writeSlot(
|
|
67
|
+
store,
|
|
68
|
+
cacheSlotKey(head, null),
|
|
69
|
+
{ nodes: [{ id: 'n1' }], links: [] },
|
|
70
|
+
'2026-01-01T00:00:00.000Z',
|
|
71
|
+
)
|
|
72
|
+
const provider = createLocalGraphifyAuditProvider({
|
|
73
|
+
resolveRepoRoot: () => root,
|
|
74
|
+
storeRoot: store,
|
|
75
|
+
})
|
|
76
|
+
const model = { components: [{ purl: PURL, construct: 'function' }] }
|
|
77
|
+
|
|
78
|
+
const readiness = await provider.readiness(model)
|
|
79
|
+
expect(readiness.status).toBe('possible')
|
|
80
|
+
expect(readiness.purls).toEqual([
|
|
81
|
+
{ purl: PURL, status: 'ready', repoRoot: root },
|
|
82
|
+
])
|
|
83
|
+
|
|
84
|
+
const snapshot = await provider.loadGraph(PURL)
|
|
85
|
+
expect(snapshot?.nodes).toEqual([{ id: 'n1' }])
|
|
86
|
+
expect(snapshot?.edges).toEqual([])
|
|
87
|
+
|
|
88
|
+
const slot = await provider.resolveGraphifySlot(PURL)
|
|
89
|
+
expect(slot).toEqual({
|
|
90
|
+
slotKey: cacheSlotKey(head, null),
|
|
91
|
+
builtAt: '2026-01-01T00:00:00.000Z',
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const ensure = await provider.ensureCurrent(model)
|
|
95
|
+
expect(ensure).toEqual({ ensured: [PURL], failed: [] })
|
|
96
|
+
} finally {
|
|
97
|
+
rmSync(root, { recursive: true, force: true })
|
|
98
|
+
rmSync(store, { recursive: true, force: true })
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('missing current slot is reported explicitly, not fabricated', async () => {
|
|
103
|
+
const { root } = makeRepo()
|
|
104
|
+
const store = makeStore()
|
|
105
|
+
try {
|
|
106
|
+
const provider = createLocalGraphifyAuditProvider({
|
|
107
|
+
resolveRepoRoot: () => root,
|
|
108
|
+
storeRoot: store,
|
|
109
|
+
})
|
|
110
|
+
const model = { components: [{ purl: PURL }] }
|
|
111
|
+
|
|
112
|
+
const readiness = await provider.readiness(model)
|
|
113
|
+
expect(readiness.status).toBe('not_ready')
|
|
114
|
+
expect(readiness.purls[0]?.status).toBe('missing')
|
|
115
|
+
expect(await provider.loadGraph(PURL)).toBeNull()
|
|
116
|
+
|
|
117
|
+
const ensure = await provider.ensureCurrent(model)
|
|
118
|
+
expect(ensure.ensured).toEqual([])
|
|
119
|
+
expect(ensure.failed[0]?.purl).toBe(PURL)
|
|
120
|
+
expect(ensure.failed[0]?.error).toContain('no graphify cache')
|
|
121
|
+
} finally {
|
|
122
|
+
rmSync(root, { recursive: true, force: true })
|
|
123
|
+
rmSync(store, { recursive: true, force: true })
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('unresolved checkout yields unavailable, not ready', async () => {
|
|
128
|
+
const store = makeStore()
|
|
129
|
+
try {
|
|
130
|
+
const provider = createLocalGraphifyAuditProvider({
|
|
131
|
+
resolveRepoRoot: () => null,
|
|
132
|
+
storeRoot: store,
|
|
133
|
+
})
|
|
134
|
+
const readiness = await provider.readiness({
|
|
135
|
+
components: [{ purl: PURL }],
|
|
136
|
+
})
|
|
137
|
+
expect(readiness.purls[0]?.status).toBe('unavailable')
|
|
138
|
+
const ensure = await provider.ensureCurrent({
|
|
139
|
+
components: [{ purl: PURL }],
|
|
140
|
+
})
|
|
141
|
+
expect(ensure.failed[0]?.error).toContain('no local checkout')
|
|
142
|
+
} finally {
|
|
143
|
+
rmSync(store, { recursive: true, force: true })
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('non-repo purls are ignored', async () => {
|
|
148
|
+
const store = makeStore()
|
|
149
|
+
try {
|
|
150
|
+
const provider = createLocalGraphifyAuditProvider({
|
|
151
|
+
resolveRepoRoot: () => null,
|
|
152
|
+
storeRoot: store,
|
|
153
|
+
})
|
|
154
|
+
const readiness = await provider.readiness({
|
|
155
|
+
components: [{ purl: 'external:internal' }, { purl: undefined }],
|
|
156
|
+
})
|
|
157
|
+
expect(readiness.purls).toEqual([])
|
|
158
|
+
expect(readiness.status).toBe('unavailable')
|
|
159
|
+
} finally {
|
|
160
|
+
rmSync(store, { recursive: true, force: true })
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
})
|