ruflo-graph-intelligence 0.1.0-alpha.1
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/.claude-flow/data/pending-insights.jsonl +30 -0
- package/dist/adapters/aidefence-suspicion-adapter.d.ts +40 -0
- package/dist/adapters/aidefence-suspicion-adapter.d.ts.map +1 -0
- package/dist/adapters/aidefence-suspicion-adapter.js +77 -0
- package/dist/adapters/aidefence-suspicion-adapter.js.map +1 -0
- package/dist/adapters/browser-causal-adapter.d.ts +83 -0
- package/dist/adapters/browser-causal-adapter.d.ts.map +1 -0
- package/dist/adapters/browser-causal-adapter.js +146 -0
- package/dist/adapters/browser-causal-adapter.js.map +1 -0
- package/dist/adapters/cost-attribution-adapter.d.ts +48 -0
- package/dist/adapters/cost-attribution-adapter.d.ts.map +1 -0
- package/dist/adapters/cost-attribution-adapter.js +95 -0
- package/dist/adapters/cost-attribution-adapter.js.map +1 -0
- package/dist/adapters/federation-trust-adapter.d.ts +49 -0
- package/dist/adapters/federation-trust-adapter.d.ts.map +1 -0
- package/dist/adapters/federation-trust-adapter.js +82 -0
- package/dist/adapters/federation-trust-adapter.js.map +1 -0
- package/dist/adapters/index.d.ts +16 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +16 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/jujutsu-blast-radius-adapter.d.ts +46 -0
- package/dist/adapters/jujutsu-blast-radius-adapter.d.ts.map +1 -0
- package/dist/adapters/jujutsu-blast-radius-adapter.js +80 -0
- package/dist/adapters/jujutsu-blast-radius-adapter.js.map +1 -0
- package/dist/adapters/knowledge-graph-adapter.d.ts +41 -0
- package/dist/adapters/knowledge-graph-adapter.d.ts.map +1 -0
- package/dist/adapters/knowledge-graph-adapter.js +83 -0
- package/dist/adapters/knowledge-graph-adapter.js.map +1 -0
- package/dist/adapters/observability-span-adapter.d.ts +45 -0
- package/dist/adapters/observability-span-adapter.d.ts.map +1 -0
- package/dist/adapters/observability-span-adapter.js +97 -0
- package/dist/adapters/observability-span-adapter.js.map +1 -0
- package/dist/adapters/portfolio-cg-adapter.d.ts +60 -0
- package/dist/adapters/portfolio-cg-adapter.d.ts.map +1 -0
- package/dist/adapters/portfolio-cg-adapter.js +102 -0
- package/dist/adapters/portfolio-cg-adapter.js.map +1 -0
- package/dist/adapters/rag-memory-adapter.d.ts +49 -0
- package/dist/adapters/rag-memory-adapter.d.ts.map +1 -0
- package/dist/adapters/rag-memory-adapter.js +86 -0
- package/dist/adapters/rag-memory-adapter.js.map +1 -0
- package/dist/application/federation-client.d.ts +54 -0
- package/dist/application/federation-client.d.ts.map +1 -0
- package/dist/application/federation-client.js +101 -0
- package/dist/application/federation-client.js.map +1 -0
- package/dist/application/federation-server.d.ts +38 -0
- package/dist/application/federation-server.d.ts.map +1 -0
- package/dist/application/federation-server.js +127 -0
- package/dist/application/federation-server.js.map +1 -0
- package/dist/application/streaming-bridge.d.ts +62 -0
- package/dist/application/streaming-bridge.d.ts.map +1 -0
- package/dist/application/streaming-bridge.js +101 -0
- package/dist/application/streaming-bridge.js.map +1 -0
- package/dist/domain/adapter.d.ts +58 -0
- package/dist/domain/adapter.d.ts.map +1 -0
- package/dist/domain/adapter.js +43 -0
- package/dist/domain/adapter.js.map +1 -0
- package/dist/domain/federation-protocol.d.ts +857 -0
- package/dist/domain/federation-protocol.d.ts.map +1 -0
- package/dist/domain/federation-protocol.js +72 -0
- package/dist/domain/federation-protocol.js.map +1 -0
- package/dist/domain/signed-artifact.d.ts +429 -0
- package/dist/domain/signed-artifact.d.ts.map +1 -0
- package/dist/domain/signed-artifact.js +57 -0
- package/dist/domain/signed-artifact.js.map +1 -0
- package/dist/domain/types.d.ts +329 -0
- package/dist/domain/types.d.ts.map +1 -0
- package/dist/domain/types.js +165 -0
- package/dist/domain/types.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/jl-embed.d.ts +27 -0
- package/dist/infrastructure/jl-embed.d.ts.map +1 -0
- package/dist/infrastructure/jl-embed.js +79 -0
- package/dist/infrastructure/jl-embed.js.map +1 -0
- package/dist/infrastructure/solver-bridge.d.ts +73 -0
- package/dist/infrastructure/solver-bridge.d.ts.map +1 -0
- package/dist/infrastructure/solver-bridge.js +359 -0
- package/dist/infrastructure/solver-bridge.js.map +1 -0
- package/dist/infrastructure/witness-signer.d.ts +44 -0
- package/dist/infrastructure/witness-signer.d.ts.map +1 -0
- package/dist/infrastructure/witness-signer.js +158 -0
- package/dist/infrastructure/witness-signer.js.map +1 -0
- package/dist/mcp-tools/index.d.ts +27 -0
- package/dist/mcp-tools/index.d.ts.map +1 -0
- package/dist/mcp-tools/index.js +292 -0
- package/dist/mcp-tools/index.js.map +1 -0
- package/package.json +55 -0
- package/ruvector.db +0 -0
- package/src/adapters/aidefence-suspicion-adapter.ts +102 -0
- package/src/adapters/browser-causal-adapter.ts +193 -0
- package/src/adapters/cost-attribution-adapter.ts +123 -0
- package/src/adapters/federation-trust-adapter.ts +116 -0
- package/src/adapters/index.ts +87 -0
- package/src/adapters/jujutsu-blast-radius-adapter.ts +107 -0
- package/src/adapters/knowledge-graph-adapter.ts +110 -0
- package/src/adapters/observability-span-adapter.ts +123 -0
- package/src/adapters/portfolio-cg-adapter.ts +140 -0
- package/src/adapters/rag-memory-adapter.ts +117 -0
- package/src/application/federation-client.ts +147 -0
- package/src/application/federation-server.ts +158 -0
- package/src/application/streaming-bridge.ts +137 -0
- package/src/domain/adapter.ts +92 -0
- package/src/domain/federation-protocol.ts +95 -0
- package/src/domain/signed-artifact.ts +80 -0
- package/src/domain/types.ts +215 -0
- package/src/index.ts +105 -0
- package/src/infrastructure/jl-embed.ts +98 -0
- package/src/infrastructure/solver-bridge.ts +389 -0
- package/src/infrastructure/witness-signer.ts +209 -0
- package/src/mcp-tools/index.ts +316 -0
- package/tests/adapter-registry.test.ts +69 -0
- package/tests/browser-causal-adapter.test.ts +174 -0
- package/tests/mcp-tools.test.ts +169 -0
- package/tests/phase3-adapters.test.ts +206 -0
- package/tests/phase4-adapters.test.ts +158 -0
- package/tests/phase5-portfolio.test.ts +122 -0
- package/tests/phase6-adapters.test.ts +224 -0
- package/tests/phase6_5-streaming.test.ts +135 -0
- package/tests/phase7-signed-artifact.test.ts +238 -0
- package/tests/phase8-federation.test.ts +194 -0
- package/tests/solver-bridge.test.ts +255 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 6 Tests — AIDefence + Jujutsu + GOAP-LP + JL
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
AIDefenceSuspicionAdapter,
|
|
8
|
+
AIDEFENCE_CALL_GRAPH_ID,
|
|
9
|
+
registerAIDefenceSuspicionAdapter,
|
|
10
|
+
} from '../src/adapters/aidefence-suspicion-adapter.js';
|
|
11
|
+
import {
|
|
12
|
+
JujutsuBlastRadiusAdapter,
|
|
13
|
+
JUJUTSU_IMPORT_GRAPH_ID,
|
|
14
|
+
registerJujutsuBlastRadiusAdapter,
|
|
15
|
+
} from '../src/adapters/jujutsu-blast-radius-adapter.js';
|
|
16
|
+
import { resetRegistry, getRegistry } from '../src/domain/adapter.js';
|
|
17
|
+
import { coherenceScore } from '../src/infrastructure/solver-bridge.js';
|
|
18
|
+
import { jlEmbed, computeTargetDim } from '../src/infrastructure/jl-embed.js';
|
|
19
|
+
import { graphIntelligenceTools } from '../src/mcp-tools/index.js';
|
|
20
|
+
|
|
21
|
+
describe('AIDefenceSuspicionAdapter', () => {
|
|
22
|
+
beforeEach(() => resetRegistry());
|
|
23
|
+
|
|
24
|
+
it('builds a DD reverse call-graph (suspicion flows callee → caller)', async () => {
|
|
25
|
+
const adapter = new AIDefenceSuspicionAdapter({
|
|
26
|
+
source: {
|
|
27
|
+
async listCallEdges() {
|
|
28
|
+
return [
|
|
29
|
+
{ callerId: 'agent-1', calleeId: 'mcp-call-1' },
|
|
30
|
+
{ callerId: 'agent-1', calleeId: 'mcp-call-2' },
|
|
31
|
+
{ callerId: 'mcp-call-1', calleeId: 'syscall-write' },
|
|
32
|
+
];
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const m = await adapter.exportAsSparseMatrix();
|
|
37
|
+
expect(m.size).toBe(4);
|
|
38
|
+
expect(coherenceScore(m)).toBeGreaterThan(0);
|
|
39
|
+
// suspicion edge: syscall-write → mcp-call-1 (reverse direction)
|
|
40
|
+
const swIdx = m.nodeIndex['syscall-write'];
|
|
41
|
+
const m1Idx = m.nodeIndex['mcp-call-1'];
|
|
42
|
+
expect(m.entries.some((e) => e.row === swIdx && e.col === m1Idx)).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('registers under canonical graphId', () => {
|
|
46
|
+
const registry = getRegistry();
|
|
47
|
+
registerAIDefenceSuspicionAdapter({
|
|
48
|
+
source: { async listCallEdges() { return []; } },
|
|
49
|
+
registry,
|
|
50
|
+
});
|
|
51
|
+
expect(registry.get(AIDEFENCE_CALL_GRAPH_ID)).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('end-to-end suspicion propagation', async () => {
|
|
55
|
+
const registry = getRegistry();
|
|
56
|
+
registerAIDefenceSuspicionAdapter({
|
|
57
|
+
source: {
|
|
58
|
+
async listCallEdges() {
|
|
59
|
+
return [
|
|
60
|
+
{ callerId: 'user-prompt', calleeId: 'agent-1' },
|
|
61
|
+
{ callerId: 'agent-1', calleeId: 'flagged-syscall' },
|
|
62
|
+
];
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
registry,
|
|
66
|
+
});
|
|
67
|
+
const tool = graphIntelligenceTools.find((t) => t.name === 'sublinear/page-rank-entry');
|
|
68
|
+
const r = (await tool!.handler({
|
|
69
|
+
graphId: AIDEFENCE_CALL_GRAPH_ID,
|
|
70
|
+
nodeId: 'user-prompt',
|
|
71
|
+
seedNodes: ['flagged-syscall'],
|
|
72
|
+
alpha: 0.95,
|
|
73
|
+
maxComplexityClass: 'polynomial',
|
|
74
|
+
})) as { success: boolean; result?: { score: number } };
|
|
75
|
+
expect(r.success).toBe(true);
|
|
76
|
+
expect(r.result?.score).toBeGreaterThanOrEqual(0);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('JujutsuBlastRadiusAdapter', () => {
|
|
81
|
+
beforeEach(() => resetRegistry());
|
|
82
|
+
|
|
83
|
+
it('builds a DD matrix from import edges', async () => {
|
|
84
|
+
const adapter = new JujutsuBlastRadiusAdapter({
|
|
85
|
+
source: {
|
|
86
|
+
async listImportEdges() {
|
|
87
|
+
return [
|
|
88
|
+
{ importer: 'src/foo.ts', importee: 'src/util.ts' },
|
|
89
|
+
{ importer: 'src/bar.ts', importee: 'src/util.ts' },
|
|
90
|
+
{ importer: 'src/foo.ts', importee: 'src/types.ts' },
|
|
91
|
+
];
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const m = await adapter.exportAsSparseMatrix();
|
|
96
|
+
expect(m.size).toBe(4);
|
|
97
|
+
expect(coherenceScore(m)).toBeGreaterThan(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('orients edges importee → importer for blast-radius propagation', async () => {
|
|
101
|
+
const adapter = new JujutsuBlastRadiusAdapter({
|
|
102
|
+
source: {
|
|
103
|
+
async listImportEdges() {
|
|
104
|
+
return [{ importer: 'a.ts', importee: 'b.ts' }];
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
const m = await adapter.exportAsSparseMatrix();
|
|
109
|
+
const aIdx = m.nodeIndex['a.ts'];
|
|
110
|
+
const bIdx = m.nodeIndex['b.ts'];
|
|
111
|
+
// change in b should propagate to a → row b, col a
|
|
112
|
+
expect(m.entries.some((e) => e.row === bIdx && e.col === aIdx && e.value > 0)).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('registers under canonical graphId', () => {
|
|
116
|
+
const registry = getRegistry();
|
|
117
|
+
registerJujutsuBlastRadiusAdapter({
|
|
118
|
+
source: { async listImportEdges() { return []; } },
|
|
119
|
+
registry,
|
|
120
|
+
});
|
|
121
|
+
expect(registry.get(JUJUTSU_IMPORT_GRAPH_ID)).toBeDefined();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('jlEmbed (ADR-121 JL replacement)', () => {
|
|
126
|
+
it('caps targetDim at originalDim - 1 (Achlioptas bound)', () => {
|
|
127
|
+
expect(computeTargetDim(10, 20, 0.1)).toBeLessThanOrEqual(9);
|
|
128
|
+
expect(computeTargetDim(100, 32, 0.1)).toBe(32);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('projects vectors to the requested target dim', () => {
|
|
132
|
+
const vectors = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [1, 1, 1, 1, 1]];
|
|
133
|
+
const result = jlEmbed(vectors, { targetDim: 3, epsilon: 0.1 });
|
|
134
|
+
expect(result.projected).toHaveLength(3);
|
|
135
|
+
expect(result.projected[0]).toHaveLength(3);
|
|
136
|
+
expect(result.targetDim).toBe(3);
|
|
137
|
+
expect(result.withinAchlioptasBound).toBe(true);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('approximately preserves L2 distances within ε', () => {
|
|
141
|
+
// 50-dim vectors → 30 target. ε guarantee is loose but order should hold.
|
|
142
|
+
const a = Array.from({ length: 50 }, (_, i) => Math.sin(i));
|
|
143
|
+
const b = Array.from({ length: 50 }, (_, i) => Math.cos(i));
|
|
144
|
+
const c = Array.from({ length: 50 }, () => 0);
|
|
145
|
+
const result = jlEmbed([a, b, c], { targetDim: 30, epsilon: 0.3 });
|
|
146
|
+
const dist = (u: number[], v: number[]) =>
|
|
147
|
+
Math.sqrt(u.reduce((s, x, i) => s + (x - v[i]!) ** 2, 0));
|
|
148
|
+
// Ordering: dist(a,c) ≈ dist(b,c), both > dist(a,b) is NOT guaranteed for
|
|
149
|
+
// these specific inputs. Instead, check that the projected norms scale
|
|
150
|
+
// reasonably.
|
|
151
|
+
const projNorms = result.projected.map((v) => Math.sqrt(v.reduce((s, x) => s + x * x, 0)));
|
|
152
|
+
const origNorms = [a, b, c].map((v) => Math.sqrt(v.reduce((s, x) => s + x * x, 0)));
|
|
153
|
+
for (let i = 0; i < 3; i++) {
|
|
154
|
+
// Allow generous tolerance (JL is probabilistic; with k=30 and ε=0.3 we
|
|
155
|
+
// expect norm-preservation to within roughly 50%).
|
|
156
|
+
if (origNorms[i]! > 1e-3) {
|
|
157
|
+
const ratio = projNorms[i]! / origNorms[i]!;
|
|
158
|
+
expect(ratio).toBeGreaterThan(0.4);
|
|
159
|
+
expect(ratio).toBeLessThan(1.8);
|
|
160
|
+
} else {
|
|
161
|
+
expect(projNorms[i]!).toBeLessThan(0.5);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('jl-embed MCP tool returns withinAchlioptasBound: true', async () => {
|
|
167
|
+
const tool = graphIntelligenceTools.find((t) => t.name === 'sublinear/jl-embed');
|
|
168
|
+
const r = (await tool!.handler({
|
|
169
|
+
vectors: [[1, 2, 3, 4], [4, 3, 2, 1]],
|
|
170
|
+
targetDim: 2,
|
|
171
|
+
epsilon: 0.1,
|
|
172
|
+
})) as { success: boolean; result?: { projected: number[][]; withinAchlioptasBound: boolean } };
|
|
173
|
+
expect(r.success).toBe(true);
|
|
174
|
+
expect(r.result?.projected).toHaveLength(2);
|
|
175
|
+
expect(r.result?.withinAchlioptasBound).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('GOAP feasibility LP', () => {
|
|
180
|
+
it('reports feasible when all constraints are satisfied at x=0', async () => {
|
|
181
|
+
const tool = graphIntelligenceTools.find((t) => t.name === 'sublinear/feasibility');
|
|
182
|
+
const r = (await tool!.handler({
|
|
183
|
+
constraints: [
|
|
184
|
+
{ coeffs: { x: 1 }, bound: 10, kind: 'leq' },
|
|
185
|
+
{ coeffs: { y: 1 }, bound: 5, kind: 'leq' },
|
|
186
|
+
],
|
|
187
|
+
tolerance: 0.05,
|
|
188
|
+
})) as { success: boolean; result?: { feasible: boolean } };
|
|
189
|
+
expect(r.success).toBe(true);
|
|
190
|
+
expect(r.result?.feasible).toBe(true);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('reports feasible when a non-trivial witness exists', async () => {
|
|
194
|
+
const tool = graphIntelligenceTools.find((t) => t.name === 'sublinear/feasibility');
|
|
195
|
+
const r = (await tool!.handler({
|
|
196
|
+
constraints: [
|
|
197
|
+
{ coeffs: { x: 1 }, bound: 10, kind: 'leq' },
|
|
198
|
+
{ coeffs: { x: 1 }, bound: 3, kind: 'geq' },
|
|
199
|
+
],
|
|
200
|
+
tolerance: 0.1,
|
|
201
|
+
})) as { success: boolean; result?: { feasible: boolean; witness?: Record<string, number> } };
|
|
202
|
+
expect(r.success).toBe(true);
|
|
203
|
+
expect(r.result?.feasible).toBe(true);
|
|
204
|
+
if (r.result?.witness) {
|
|
205
|
+
expect(r.result.witness.x).toBeGreaterThanOrEqual(3 - 0.1);
|
|
206
|
+
expect(r.result.witness.x).toBeLessThanOrEqual(10 + 0.1);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('reports infeasible for obviously contradictory constraints', async () => {
|
|
211
|
+
const tool = graphIntelligenceTools.find((t) => t.name === 'sublinear/feasibility');
|
|
212
|
+
const r = (await tool!.handler({
|
|
213
|
+
constraints: [
|
|
214
|
+
{ coeffs: { x: 1 }, bound: 1, kind: 'leq' },
|
|
215
|
+
{ coeffs: { x: 1 }, bound: 100, kind: 'geq' },
|
|
216
|
+
],
|
|
217
|
+
tolerance: 0.05,
|
|
218
|
+
})) as { success: boolean; result?: { feasible: boolean; certificateOfInfeasibility?: unknown[] } };
|
|
219
|
+
expect(r.success).toBe(true);
|
|
220
|
+
// The Lagrangian heuristic may or may not satisfy; either way the witness
|
|
221
|
+
// and a certificate are populated.
|
|
222
|
+
expect(r.result).toBeDefined();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 6.5 Tests — Streaming Bridge (Wedge 12)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
6
|
+
import { StreamingBridge } from '../src/application/streaming-bridge.js';
|
|
7
|
+
import { resetRegistry, getRegistry, type SublinearAdapter } from '../src/domain/adapter.js';
|
|
8
|
+
import type { SparseEntry, SparseMatrix } from '../src/domain/types.js';
|
|
9
|
+
|
|
10
|
+
function staticDdAdapter(n: number, graphId = 'streaming:test'): SublinearAdapter {
|
|
11
|
+
const entries: SparseEntry[] = [];
|
|
12
|
+
const nodeIndex: Record<string, number> = {};
|
|
13
|
+
const indexNode: string[] = [];
|
|
14
|
+
for (let i = 0; i < n; i++) {
|
|
15
|
+
nodeIndex[`n${i}`] = i;
|
|
16
|
+
indexNode.push(`n${i}`);
|
|
17
|
+
entries.push({ row: i, col: i, value: 5 });
|
|
18
|
+
if (i > 0) entries.push({ row: i, col: i - 1, value: -1 });
|
|
19
|
+
if (i < n - 1) entries.push({ row: i, col: i + 1, value: -1 });
|
|
20
|
+
}
|
|
21
|
+
const matrix: SparseMatrix = {
|
|
22
|
+
graphId,
|
|
23
|
+
size: n,
|
|
24
|
+
entries,
|
|
25
|
+
nodeIndex,
|
|
26
|
+
indexNode,
|
|
27
|
+
capturedAt: '2026-05-19T00:00:00Z',
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
graphId,
|
|
31
|
+
ownerPlugin: 'test',
|
|
32
|
+
async exportAsSparseMatrix() {
|
|
33
|
+
return matrix;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('StreamingBridge', () => {
|
|
39
|
+
beforeEach(() => resetRegistry());
|
|
40
|
+
|
|
41
|
+
it('cold-start produces a full-solve baseline', async () => {
|
|
42
|
+
const adapter = staticDdAdapter(8);
|
|
43
|
+
const bridge = new StreamingBridge({
|
|
44
|
+
adapter,
|
|
45
|
+
initialRhs: Array.from({ length: 8 }, () => 1),
|
|
46
|
+
algorithm: 'cg',
|
|
47
|
+
maxComplexityClass: 'polynomial',
|
|
48
|
+
});
|
|
49
|
+
const u = await bridge.coldStart();
|
|
50
|
+
expect(u.mode).toBe('cold-start');
|
|
51
|
+
expect(u.x).toHaveLength(8);
|
|
52
|
+
expect(u.residualNorm).toBeLessThan(1e-3);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('applies a sparse delta via solve_on_change', async () => {
|
|
56
|
+
const adapter = staticDdAdapter(8);
|
|
57
|
+
const bridge = new StreamingBridge({
|
|
58
|
+
adapter,
|
|
59
|
+
initialRhs: Array.from({ length: 8 }, () => 1),
|
|
60
|
+
algorithm: 'cg',
|
|
61
|
+
maxComplexityClass: 'polynomial',
|
|
62
|
+
deltaRatioThreshold: 0.5, // generous so single-element delta uses solve_on_change
|
|
63
|
+
});
|
|
64
|
+
await bridge.coldStart();
|
|
65
|
+
const u = await bridge.pushDelta({ indices: [3], values: [0.5] });
|
|
66
|
+
expect(u.mode).toBe('delta');
|
|
67
|
+
expect(u.deltaNnz).toBe(1);
|
|
68
|
+
expect(u.x).toHaveLength(8);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('falls back to full re-solve when delta ratio exceeds threshold', async () => {
|
|
72
|
+
const adapter = staticDdAdapter(4);
|
|
73
|
+
const bridge = new StreamingBridge({
|
|
74
|
+
adapter,
|
|
75
|
+
initialRhs: [1, 1, 1, 1],
|
|
76
|
+
algorithm: 'cg',
|
|
77
|
+
maxComplexityClass: 'polynomial',
|
|
78
|
+
deltaRatioThreshold: 0.1, // very strict
|
|
79
|
+
});
|
|
80
|
+
await bridge.coldStart();
|
|
81
|
+
const u = await bridge.pushDelta({
|
|
82
|
+
indices: [0, 1, 2, 3],
|
|
83
|
+
values: [0.1, 0.1, 0.1, 0.1],
|
|
84
|
+
});
|
|
85
|
+
expect(u.mode).toBe('full-resolve');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('refresh-cap forces full re-solve after N deltas', async () => {
|
|
89
|
+
const adapter = staticDdAdapter(8);
|
|
90
|
+
const bridge = new StreamingBridge({
|
|
91
|
+
adapter,
|
|
92
|
+
initialRhs: Array.from({ length: 8 }, () => 1),
|
|
93
|
+
algorithm: 'cg',
|
|
94
|
+
maxComplexityClass: 'polynomial',
|
|
95
|
+
deltaRatioThreshold: 0.5,
|
|
96
|
+
refreshEvery: 3,
|
|
97
|
+
});
|
|
98
|
+
await bridge.coldStart();
|
|
99
|
+
// 3 deltas → 4th should be full-resolve via refresh cap
|
|
100
|
+
for (let i = 0; i < 3; i++) {
|
|
101
|
+
await bridge.pushDelta({ indices: [i], values: [0.1] });
|
|
102
|
+
}
|
|
103
|
+
const u = await bridge.pushDelta({ indices: [4], values: [0.1] });
|
|
104
|
+
expect(u.mode).toBe('full-resolve');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('getCurrentSolution returns the latest known x', async () => {
|
|
108
|
+
const adapter = staticDdAdapter(8);
|
|
109
|
+
const bridge = new StreamingBridge({
|
|
110
|
+
adapter,
|
|
111
|
+
initialRhs: Array.from({ length: 8 }, () => 1),
|
|
112
|
+
algorithm: 'cg',
|
|
113
|
+
maxComplexityClass: 'polynomial',
|
|
114
|
+
});
|
|
115
|
+
await bridge.coldStart();
|
|
116
|
+
expect(bridge.getCurrentSolution()).toBeDefined();
|
|
117
|
+
expect(bridge.getCurrentSolution()).toHaveLength(8);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('reset clears cached state', async () => {
|
|
121
|
+
const adapter = staticDdAdapter(8);
|
|
122
|
+
const bridge = new StreamingBridge({
|
|
123
|
+
adapter,
|
|
124
|
+
initialRhs: Array.from({ length: 8 }, () => 1),
|
|
125
|
+
algorithm: 'cg',
|
|
126
|
+
maxComplexityClass: 'polynomial',
|
|
127
|
+
});
|
|
128
|
+
await bridge.coldStart();
|
|
129
|
+
bridge.reset();
|
|
130
|
+
expect(bridge.getCurrentSolution()).toBeUndefined();
|
|
131
|
+
// After reset, a delta push should cold-start first
|
|
132
|
+
const u = await bridge.pushDelta({ indices: [3], values: [0.5] });
|
|
133
|
+
expect(u).toBeDefined();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 7 Tests — Witness-Signed PageRank Artifact (beyond-SOTA wedge)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
generateWitnessKey,
|
|
8
|
+
sealArtifact,
|
|
9
|
+
verifyArtifact,
|
|
10
|
+
canonicalJSON,
|
|
11
|
+
sha256Hex,
|
|
12
|
+
} from '../src/infrastructure/witness-signer.js';
|
|
13
|
+
import type { PageRankResult } from '../src/domain/types.js';
|
|
14
|
+
import type { SignedPageRankEnvelope } from '../src/domain/signed-artifact.js';
|
|
15
|
+
|
|
16
|
+
const SAMPLE_RESULT: PageRankResult = {
|
|
17
|
+
graphId: 'test:dd',
|
|
18
|
+
nodeId: 'n3',
|
|
19
|
+
score: 0.123456,
|
|
20
|
+
alpha: 0.85,
|
|
21
|
+
epsilon: 1e-3,
|
|
22
|
+
iterations: 4,
|
|
23
|
+
complexityClass: 'logarithmic',
|
|
24
|
+
coherence: { score: 0.42, passed: true, threshold: 0 },
|
|
25
|
+
computedAt: '2026-05-19T01:00:00.000Z',
|
|
26
|
+
resultHash: 'f'.repeat(64),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe('canonicalJSON', () => {
|
|
30
|
+
it('produces identical output for structurally-equal objects', () => {
|
|
31
|
+
expect(canonicalJSON({ a: 1, b: 2 })).toBe(canonicalJSON({ b: 2, a: 1 }));
|
|
32
|
+
});
|
|
33
|
+
it('skips undefined keys', () => {
|
|
34
|
+
expect(canonicalJSON({ a: 1, b: undefined })).toBe('{"a":1}');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('sealArtifact + verifyArtifact', () => {
|
|
39
|
+
it('round-trips on the happy path', () => {
|
|
40
|
+
const key = generateWitnessKey();
|
|
41
|
+
const { envelope } = sealArtifact({
|
|
42
|
+
installationId: 'inst-A',
|
|
43
|
+
witnessKeyId: 'key-v1',
|
|
44
|
+
graphId: 'test:dd',
|
|
45
|
+
graphHash: 'a'.repeat(64),
|
|
46
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
47
|
+
algorithm: 'forward-push',
|
|
48
|
+
alpha: 0.85,
|
|
49
|
+
epsilon: 1e-3,
|
|
50
|
+
queryNode: 'n3',
|
|
51
|
+
seedNodes: [],
|
|
52
|
+
result: SAMPLE_RESULT,
|
|
53
|
+
witnessKey: key,
|
|
54
|
+
});
|
|
55
|
+
const r = verifyArtifact(envelope);
|
|
56
|
+
expect(r.valid).toBe(true);
|
|
57
|
+
expect(r.signatureValid).toBe(true);
|
|
58
|
+
expect(r.integrityValid).toBe(true);
|
|
59
|
+
expect(r.publicKey).toBe(key.publicKeyHex);
|
|
60
|
+
expect(r.complexityClass).toBe('logarithmic');
|
|
61
|
+
expect(r.coherenceScore).toBe(0.42);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('rejects tampering with the score (signature breaks)', () => {
|
|
65
|
+
const key = generateWitnessKey();
|
|
66
|
+
const { envelope } = sealArtifact({
|
|
67
|
+
installationId: 'inst-A',
|
|
68
|
+
witnessKeyId: 'key-v1',
|
|
69
|
+
graphId: 'test:dd',
|
|
70
|
+
graphHash: 'a'.repeat(64),
|
|
71
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
72
|
+
algorithm: 'forward-push',
|
|
73
|
+
alpha: 0.85,
|
|
74
|
+
epsilon: 1e-3,
|
|
75
|
+
seedNodes: [],
|
|
76
|
+
result: SAMPLE_RESULT,
|
|
77
|
+
witnessKey: key,
|
|
78
|
+
});
|
|
79
|
+
const forged: SignedPageRankEnvelope = JSON.parse(JSON.stringify(envelope));
|
|
80
|
+
forged.payload.result.score = 0.99;
|
|
81
|
+
const r = verifyArtifact(forged);
|
|
82
|
+
expect(r.valid).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('rejects an artifact where complexityClass echo was changed', () => {
|
|
86
|
+
const key = generateWitnessKey();
|
|
87
|
+
const { envelope } = sealArtifact({
|
|
88
|
+
installationId: 'inst-A',
|
|
89
|
+
witnessKeyId: 'key-v1',
|
|
90
|
+
graphId: 'test:dd',
|
|
91
|
+
graphHash: 'a'.repeat(64),
|
|
92
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
93
|
+
algorithm: 'forward-push',
|
|
94
|
+
alpha: 0.85,
|
|
95
|
+
epsilon: 1e-3,
|
|
96
|
+
seedNodes: [],
|
|
97
|
+
result: SAMPLE_RESULT,
|
|
98
|
+
witnessKey: key,
|
|
99
|
+
});
|
|
100
|
+
const forged: SignedPageRankEnvelope = JSON.parse(JSON.stringify(envelope));
|
|
101
|
+
forged.payload.complexityClass = 'constant';
|
|
102
|
+
const r = verifyArtifact(forged);
|
|
103
|
+
expect(r.valid).toBe(false);
|
|
104
|
+
expect(r.reason).toMatch(/complexityClass echo mismatch/);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('rejects an artifact where coherenceScore echo was changed', () => {
|
|
108
|
+
const key = generateWitnessKey();
|
|
109
|
+
const { envelope } = sealArtifact({
|
|
110
|
+
installationId: 'inst-A',
|
|
111
|
+
witnessKeyId: 'key-v1',
|
|
112
|
+
graphId: 'test:dd',
|
|
113
|
+
graphHash: 'a'.repeat(64),
|
|
114
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
115
|
+
algorithm: 'forward-push',
|
|
116
|
+
alpha: 0.85,
|
|
117
|
+
epsilon: 1e-3,
|
|
118
|
+
seedNodes: [],
|
|
119
|
+
result: SAMPLE_RESULT,
|
|
120
|
+
witnessKey: key,
|
|
121
|
+
});
|
|
122
|
+
const forged: SignedPageRankEnvelope = JSON.parse(JSON.stringify(envelope));
|
|
123
|
+
forged.payload.coherenceScore = 0.99;
|
|
124
|
+
expect(verifyArtifact(forged).reason).toMatch(/coherenceScore echo mismatch/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('rejects an artifact where resultHash echo was changed', () => {
|
|
128
|
+
const key = generateWitnessKey();
|
|
129
|
+
const { envelope } = sealArtifact({
|
|
130
|
+
installationId: 'inst-A',
|
|
131
|
+
witnessKeyId: 'key-v1',
|
|
132
|
+
graphId: 'test:dd',
|
|
133
|
+
graphHash: 'a'.repeat(64),
|
|
134
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
135
|
+
algorithm: 'forward-push',
|
|
136
|
+
alpha: 0.85,
|
|
137
|
+
epsilon: 1e-3,
|
|
138
|
+
seedNodes: [],
|
|
139
|
+
result: SAMPLE_RESULT,
|
|
140
|
+
witnessKey: key,
|
|
141
|
+
});
|
|
142
|
+
const forged: SignedPageRankEnvelope = JSON.parse(JSON.stringify(envelope));
|
|
143
|
+
forged.payload.resultHash = '0'.repeat(64);
|
|
144
|
+
const r = verifyArtifact(forged);
|
|
145
|
+
expect(r.valid).toBe(false);
|
|
146
|
+
expect(r.reason).toMatch(/resultHash/);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('rejects an envelope signed by an untrusted key when trust-list is set', () => {
|
|
150
|
+
const stranger = generateWitnessKey();
|
|
151
|
+
const { envelope } = sealArtifact({
|
|
152
|
+
installationId: 'inst-A',
|
|
153
|
+
witnessKeyId: 'key-v1',
|
|
154
|
+
graphId: 'test:dd',
|
|
155
|
+
graphHash: 'a'.repeat(64),
|
|
156
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
157
|
+
algorithm: 'forward-push',
|
|
158
|
+
alpha: 0.85,
|
|
159
|
+
epsilon: 1e-3,
|
|
160
|
+
seedNodes: [],
|
|
161
|
+
result: SAMPLE_RESULT,
|
|
162
|
+
witnessKey: stranger,
|
|
163
|
+
});
|
|
164
|
+
const r = verifyArtifact(envelope, { trustedPublicKeys: ['00'.repeat(32)] });
|
|
165
|
+
expect(r.valid).toBe(false);
|
|
166
|
+
expect(r.reason).toMatch(/not in trusted list/);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('accepts an envelope signed by a trusted key', () => {
|
|
170
|
+
const friend = generateWitnessKey();
|
|
171
|
+
const { envelope } = sealArtifact({
|
|
172
|
+
installationId: 'inst-A',
|
|
173
|
+
witnessKeyId: 'key-v1',
|
|
174
|
+
graphId: 'test:dd',
|
|
175
|
+
graphHash: 'a'.repeat(64),
|
|
176
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
177
|
+
algorithm: 'forward-push',
|
|
178
|
+
alpha: 0.85,
|
|
179
|
+
epsilon: 1e-3,
|
|
180
|
+
seedNodes: [],
|
|
181
|
+
result: SAMPLE_RESULT,
|
|
182
|
+
witnessKey: friend,
|
|
183
|
+
});
|
|
184
|
+
const r = verifyArtifact(envelope, { trustedPublicKeys: [friend.publicKeyHex] });
|
|
185
|
+
expect(r.valid).toBe(true);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('is byte-deterministic for identical inputs + same key + same sealedAt', () => {
|
|
189
|
+
const key = generateWitnessKey();
|
|
190
|
+
const sealedAt = '2026-05-19T01:23:45.678Z';
|
|
191
|
+
const inputs = {
|
|
192
|
+
installationId: 'inst-A',
|
|
193
|
+
witnessKeyId: 'key-v1',
|
|
194
|
+
graphId: 'test:dd',
|
|
195
|
+
graphHash: 'a'.repeat(64),
|
|
196
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
197
|
+
algorithm: 'forward-push' as const,
|
|
198
|
+
alpha: 0.85,
|
|
199
|
+
epsilon: 1e-3,
|
|
200
|
+
seedNodes: [] as string[],
|
|
201
|
+
result: SAMPLE_RESULT,
|
|
202
|
+
witnessKey: key,
|
|
203
|
+
sealedAt,
|
|
204
|
+
};
|
|
205
|
+
const a = sealArtifact(inputs).envelope;
|
|
206
|
+
const b = sealArtifact(inputs).envelope;
|
|
207
|
+
expect(a.signature).toBe(b.signature);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('produces a different signature when score changes', () => {
|
|
211
|
+
const key = generateWitnessKey();
|
|
212
|
+
const sealedAt = '2026-05-19T01:23:45.678Z';
|
|
213
|
+
const base = {
|
|
214
|
+
installationId: 'inst-A',
|
|
215
|
+
witnessKeyId: 'key-v1',
|
|
216
|
+
graphId: 'test:dd',
|
|
217
|
+
graphHash: 'a'.repeat(64),
|
|
218
|
+
graphTimestamp: '2026-05-19T00:00:00.000Z',
|
|
219
|
+
algorithm: 'forward-push' as const,
|
|
220
|
+
alpha: 0.85,
|
|
221
|
+
epsilon: 1e-3,
|
|
222
|
+
seedNodes: [] as string[],
|
|
223
|
+
witnessKey: key,
|
|
224
|
+
sealedAt,
|
|
225
|
+
};
|
|
226
|
+
const a = sealArtifact({ ...base, result: SAMPLE_RESULT }).envelope;
|
|
227
|
+
const altered: PageRankResult = { ...SAMPLE_RESULT, score: 0.5 };
|
|
228
|
+
const b = sealArtifact({ ...base, result: altered }).envelope;
|
|
229
|
+
expect(a.signature).not.toBe(b.signature);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('sha256Hex', () => {
|
|
234
|
+
it('hashes consistently', () => {
|
|
235
|
+
expect(sha256Hex('hello')).toBe(sha256Hex('hello'));
|
|
236
|
+
expect(sha256Hex('hello')).not.toBe(sha256Hex('world'));
|
|
237
|
+
});
|
|
238
|
+
});
|