@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,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
|
+
/**
|
|
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
|
+
}
|
package/src/audit/index.ts
CHANGED
|
@@ -1,9 +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'
|
|
1
9
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
|
|
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
|
+
})
|