knodin 0.14.1 → 0.14.2

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.
@@ -0,0 +1,278 @@
1
+ import crypto from "node:crypto";
2
+ export function evaluateCallerDeadlinePacket(packet) {
3
+ const base = {
4
+ packetId: packet.packetId,
5
+ producerCompletionMs: packet.producerCompletionMs,
6
+ callerDeliveryMs: packet.callerDeliveryMs,
7
+ callerDeadlineMs: packet.callerDeadlineMs,
8
+ };
9
+ if (packet.termination === "timeout") {
10
+ return {
11
+ ...base,
12
+ outcome: "timeout",
13
+ countsAsDeliveredSuccess: false,
14
+ countsAsNegativeSearchResult: false,
15
+ };
16
+ }
17
+ if (packet.termination === "cancelled") {
18
+ return {
19
+ ...base,
20
+ outcome: "cancellation",
21
+ countsAsDeliveredSuccess: false,
22
+ countsAsNegativeSearchResult: false,
23
+ };
24
+ }
25
+ if (packet.freshnessState !== "fresh" || packet.sourceRevision !== packet.indexRevision) {
26
+ return {
27
+ ...base,
28
+ outcome: "stale-after-edit",
29
+ countsAsDeliveredSuccess: false,
30
+ countsAsNegativeSearchResult: false,
31
+ };
32
+ }
33
+ const withinDeadline = packet.callerDeliveryMs !== null && packet.callerDeliveryMs <= packet.callerDeadlineMs;
34
+ if (packet.expectedNoMatch === true) {
35
+ if (packet.deliveredFacts.length === 0 && withinDeadline) {
36
+ return {
37
+ ...base,
38
+ outcome: "genuine-no-match",
39
+ countsAsDeliveredSuccess: true,
40
+ countsAsNegativeSearchResult: true,
41
+ };
42
+ }
43
+ return {
44
+ ...base,
45
+ outcome: packet.deliveredFacts.length === 0 ? "correct-but-late" : "incorrect",
46
+ countsAsDeliveredSuccess: false,
47
+ countsAsNegativeSearchResult: false,
48
+ };
49
+ }
50
+ const deliveredSet = new Set(packet.deliveredFacts);
51
+ const allFactsPresent = packet.requiredFacts.length > 0 && packet.requiredFacts.every((fact) => deliveredSet.has(fact));
52
+ if (!allFactsPresent) {
53
+ return {
54
+ ...base,
55
+ outcome: "incorrect",
56
+ countsAsDeliveredSuccess: false,
57
+ countsAsNegativeSearchResult: false,
58
+ };
59
+ }
60
+ if (withinDeadline) {
61
+ return {
62
+ ...base,
63
+ outcome: "correct-and-delivered",
64
+ countsAsDeliveredSuccess: true,
65
+ countsAsNegativeSearchResult: false,
66
+ };
67
+ }
68
+ return {
69
+ ...base,
70
+ outcome: "correct-but-late",
71
+ countsAsDeliveredSuccess: false,
72
+ countsAsNegativeSearchResult: false,
73
+ };
74
+ }
75
+ export function summarizeCallerDeadlineCohort(packets) {
76
+ const evaluations = packets.map(evaluateCallerDeadlinePacket);
77
+ const deliveredNumerator = evaluations.filter((e) => e.countsAsDeliveredSuccess).length;
78
+ const deliveredDenominator = evaluations.length;
79
+ const countsByOutcome = {
80
+ "correct-and-delivered": 0,
81
+ "correct-but-late": 0,
82
+ "stale-after-edit": 0,
83
+ timeout: 0,
84
+ cancellation: 0,
85
+ "genuine-no-match": 0,
86
+ incorrect: 0,
87
+ };
88
+ for (const evaluation of evaluations) {
89
+ countsByOutcome[evaluation.outcome] += 1;
90
+ }
91
+ return {
92
+ evaluations,
93
+ deliveredNumerator,
94
+ deliveredDenominator,
95
+ deliveryRate: deliveredDenominator > 0 ? deliveredNumerator / deliveredDenominator : 0,
96
+ countsByOutcome,
97
+ };
98
+ }
99
+ export function evaluateBehaviorPreservingRefactor(candidate) {
100
+ if (!candidate.observableBaselineEstablished) {
101
+ return {
102
+ accepted: false,
103
+ status: "stopped-missing-baseline",
104
+ reason: "Pre-edit observable behavior baseline is missing or inadequate; characterize behavior before structural edits.",
105
+ };
106
+ }
107
+ if (!candidate.dynamicSeamsChecked) {
108
+ return {
109
+ accepted: false,
110
+ status: "stopped-unchecked-dynamic-seams",
111
+ reason: "Dynamic entry points, callback registrations, and public seams must be checked before classifying code as unused.",
112
+ };
113
+ }
114
+ if (!candidate.singleBoundedStep) {
115
+ return {
116
+ accepted: false,
117
+ status: "rejected-multi-step-bundle",
118
+ reason: "Transformations must execute one bounded step per baseline verification.",
119
+ };
120
+ }
121
+ if (!candidate.observableBehaviorPreserved || candidate.deletedDynamicSeam === true) {
122
+ return {
123
+ accepted: false,
124
+ status: "rejected-behavior-regression",
125
+ reason: "Observable behavior changed or a dynamic seam was removed; duplication/dead-code/complexity score improvements never override behavioral preservation.",
126
+ };
127
+ }
128
+ return {
129
+ accepted: true,
130
+ status: "accepted",
131
+ reason: "Observable behavior preserved against pinned baseline.",
132
+ };
133
+ }
134
+ export function evaluateObservedSideEffectCoverage(baseline, candidate) {
135
+ if (baseline.scenarioId !== candidate.scenarioId ||
136
+ baseline.inputHash !== candidate.inputHash ||
137
+ baseline.startingStateHash !== candidate.startingStateHash) {
138
+ return {
139
+ verdict: "invalid-comparison",
140
+ mismatches: ["scenarioId, inputHash, or startingStateHash differed between runs"],
141
+ unknownReasons: [],
142
+ };
143
+ }
144
+ const mismatches = [];
145
+ const unknownReasons = [];
146
+ if (baseline.stdout !== candidate.stdout)
147
+ mismatches.push("stdout mismatch");
148
+ if (baseline.stderr !== candidate.stderr)
149
+ mismatches.push("stderr mismatch");
150
+ if (baseline.exitCode !== candidate.exitCode)
151
+ mismatches.push("exitCode mismatch");
152
+ const allPaths = new Set([
153
+ ...Object.keys(baseline.observedPersistentPaths),
154
+ ...Object.keys(candidate.observedPersistentPaths),
155
+ ]);
156
+ for (const p of allPaths) {
157
+ const bHash = baseline.observedPersistentPaths[p];
158
+ const cHash = candidate.observedPersistentPaths[p];
159
+ if (bHash === undefined || cHash === undefined || bHash === null || cHash === null) {
160
+ unknownReasons.push(`unobserved or missing hash for persistent path: ${p}`);
161
+ }
162
+ else if (bHash !== cHash) {
163
+ mismatches.push(`persistent side-effect mismatch at ${p}`);
164
+ }
165
+ }
166
+ for (const unobserved of [
167
+ ...(baseline.unobservedPaths ?? []),
168
+ ...(candidate.unobservedPaths ?? []),
169
+ ]) {
170
+ unknownReasons.push(`unobserved path: ${unobserved}`);
171
+ }
172
+ if (baseline.transientEffectsPossible || candidate.transientEffectsPossible) {
173
+ unknownReasons.push("transient effects possible outside snapshot boundary");
174
+ }
175
+ if (mismatches.length > 0) {
176
+ return { verdict: "fail", mismatches, unknownReasons };
177
+ }
178
+ if (unknownReasons.length > 0) {
179
+ return { verdict: "unknown", mismatches: [], unknownReasons };
180
+ }
181
+ return { verdict: "pass-within-observed-scope", mismatches: [], unknownReasons: [] };
182
+ }
183
+ export function computeSymbolJourneyDigest(symbol) {
184
+ const signatureDigest = crypto
185
+ .createHash("sha256")
186
+ .update(JSON.stringify({
187
+ name: symbol.name,
188
+ filePath: symbol.filePath,
189
+ signature: symbol.signature.trim(),
190
+ exported: symbol.exported ?? true,
191
+ }))
192
+ .digest("hex")
193
+ .slice(0, 16);
194
+ const bodyDigest = crypto
195
+ .createHash("sha256")
196
+ .update(symbol.body.replace(/\s+/g, " ").trim())
197
+ .digest("hex")
198
+ .slice(0, 16);
199
+ return { signatureDigest, bodyDigest };
200
+ }
201
+ export function evaluateJourneyStepFreshness(step, currentSymbol) {
202
+ if (!currentSymbol ||
203
+ currentSymbol.filePath !== step.linkedFile ||
204
+ currentSymbol.name !== step.linkedSymbol ||
205
+ step.linkedLine < currentSymbol.startLine ||
206
+ step.linkedLine > currentSymbol.endLine) {
207
+ return {
208
+ stepId: step.stepId,
209
+ status: "unverifiable-source-link",
210
+ proseFresh: false,
211
+ signatureUnchanged: false,
212
+ relationshipLabel: step.relationshipLabel,
213
+ };
214
+ }
215
+ const signatureUnchanged = currentSymbol.signatureDigest === step.recordedSignatureDigest;
216
+ const bodyUnchanged = currentSymbol.bodyDigest === step.recordedBodyDigest;
217
+ if (!signatureUnchanged) {
218
+ return {
219
+ stepId: step.stepId,
220
+ status: "stale-signature-change",
221
+ proseFresh: false,
222
+ signatureUnchanged: false,
223
+ relationshipLabel: step.relationshipLabel,
224
+ };
225
+ }
226
+ if (!bodyUnchanged) {
227
+ return {
228
+ stepId: step.stepId,
229
+ status: "stale-body-only-change",
230
+ proseFresh: false,
231
+ signatureUnchanged: true,
232
+ relationshipLabel: step.relationshipLabel,
233
+ };
234
+ }
235
+ return {
236
+ stepId: step.stepId,
237
+ status: "fresh",
238
+ proseFresh: true,
239
+ signatureUnchanged: true,
240
+ relationshipLabel: step.relationshipLabel,
241
+ };
242
+ }
243
+ export function evaluateLearningPathCandidate(input) {
244
+ if (input.sourceEgressCount > 0) {
245
+ return {
246
+ disposition: "retain-incumbent",
247
+ reason: "Candidate introduced source egress.",
248
+ };
249
+ }
250
+ if (input.memoryPeakMb >= 1024) {
251
+ return {
252
+ disposition: "retain-incumbent",
253
+ reason: "Candidate exceeded 1024 MB incremental memory ceiling.",
254
+ };
255
+ }
256
+ if (input.unverifiableLinks > 0) {
257
+ return {
258
+ disposition: "retain-incumbent",
259
+ reason: "Candidate contained unverifiable source links.",
260
+ };
261
+ }
262
+ if (input.staleBodyClaimsAllowed) {
263
+ return {
264
+ disposition: "retain-incumbent",
265
+ reason: "Candidate allowed stale behavioral prose after a body-only edit.",
266
+ };
267
+ }
268
+ if (input.candidateCorrectEdges <= input.baselineCorrectEdges) {
269
+ return {
270
+ disposition: "retain-incumbent",
271
+ reason: "Candidate showed no comprehension/dependency gain over incumbent context/wiki/visualize.",
272
+ };
273
+ }
274
+ return {
275
+ disposition: "qualifies-for-review",
276
+ reason: "Candidate improved dependency comprehension within budget and freshness bounds.",
277
+ };
278
+ }