-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubgraphImporter.ts
More file actions
207 lines (176 loc) · 6.46 KB
/
SubgraphImporter.ts
File metadata and controls
207 lines (176 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ---------------------------------------------------------------------------
// SubgraphImporter — safely integrate received graph fragments (P2-G3)
// ---------------------------------------------------------------------------
//
// Verifies schema and (optionally) signatures on incoming graph fragments,
// merges eligible nodes and edges into the local store, and strips sender
// identity metadata so peer identity is not exposed to local queries.
// ---------------------------------------------------------------------------
import type { Edge, Hash, MetadataStore, Page, VectorStore } from "../core/types";
import type { GraphFragment, SubgraphSlice } from "./types";
// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------
export interface ImportOptions {
metadataStore: MetadataStore;
vectorStore: VectorStore;
/**
* When true, nodes whose pageId does not match SHA-256(content) are
* rejected. Defaults to false for test environments — enable in production.
*/
verifyContentHashes?: boolean;
}
export interface ImportResult {
nodesImported: number;
edgesImported: number;
rejected: Hash[];
}
// ---------------------------------------------------------------------------
// Schema validation helpers
// ---------------------------------------------------------------------------
function isValidPage(p: unknown): p is Page {
if (typeof p !== "object" || p === null) return false;
const page = p as Partial<Page>;
return (
typeof page.pageId === "string" && page.pageId.length > 0 &&
typeof page.content === "string" &&
typeof page.embeddingOffset === "number" &&
// This check is a validation guard, not a hardcoded model numeric.
typeof page.embeddingDim === "number" && page.embeddingDim > 0 // model-derived-ok
);
}
function isValidEdge(e: unknown): e is Edge {
if (typeof e !== "object" || e === null) return false;
const edge = e as Partial<Edge>;
return (
typeof edge.fromPageId === "string" && edge.fromPageId.length > 0 &&
typeof edge.toPageId === "string" && edge.toPageId.length > 0 &&
typeof edge.weight === "number" && edge.weight >= 0
);
}
// ---------------------------------------------------------------------------
// Import logic
// ---------------------------------------------------------------------------
async function computeContentHash(content: string): Promise<string> {
if (!("crypto" in globalThis) || !globalThis.crypto?.subtle) {
throw new Error("SubtleCrypto not available for content hash verification");
}
const encoder = new TextEncoder();
const data = encoder.encode(content);
const digest = await globalThis.crypto.subtle.digest("SHA-256", data);
const bytes = new Uint8Array(digest);
let hex = "";
for (const b of bytes) {
hex += b.toString(16).padStart(2, "0");
}
return hex;
}
async function importNodes(
nodes: Page[],
vectorStore: VectorStore,
metadataStore: MetadataStore,
verifyContentHashes: boolean,
): Promise<{ imported: Hash[]; rejected: Hash[] }> {
const imported: Hash[] = [];
const rejected: Hash[] = [];
for (const raw of nodes) {
if (!isValidPage(raw)) {
if (typeof (raw as Partial<Page>).pageId === "string") {
rejected.push((raw as Page).pageId);
}
continue;
}
// Strip sender identity and discard sender-provided embedding metadata.
// Remote embeddingOffset/embeddingDim refer to the sender's VectorStore and
// are not valid byte offsets in the local store, so we must not persist them.
const page: Page = {
...raw,
creatorPubKey: "",
signature: "",
// Mark as "no local embedding yet"; downstream code can choose to re-embed.
embeddingOffset: 0,
embeddingDim: 0, // model-derived-ok
};
// Optionally verify that pageId matches SHA-256(content)
if (verifyContentHashes) {
let computedId: string;
try {
computedId = await computeContentHash(page.content);
} catch {
// If we cannot verify hashes, reject the page rather than
// silently accepting unverified content.
rejected.push(page.pageId);
continue;
}
if (computedId !== page.pageId) {
rejected.push(page.pageId);
continue;
}
}
// Persist page without trusting remote embedding offsets.
await metadataStore.putPage(page);
imported.push(page.pageId);
}
return { imported, rejected };
}
async function importEdges(
edges: Edge[],
importedNodeIds: Set<Hash>,
metadataStore: MetadataStore,
): Promise<number> {
const validEdges = edges.filter(
(e) =>
isValidEdge(e) &&
importedNodeIds.has(e.fromPageId) &&
importedNodeIds.has(e.toPageId),
);
if (validEdges.length > 0) {
await metadataStore.putEdges(validEdges);
}
return validEdges.length;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Import a GraphFragment received in response to a CuriosityProbe.
*
* Validates schema, strips sender identity metadata, and persists approved
* nodes and edges into the local store. Rejected nodes are returned for
* auditability.
*/
export async function importFragment(
fragment: GraphFragment,
options: ImportOptions,
): Promise<ImportResult> {
const { metadataStore, vectorStore, verifyContentHashes = false } = options;
const { imported, rejected } = await importNodes(
fragment.nodes,
vectorStore,
metadataStore,
verifyContentHashes,
);
const importedSet = new Set<Hash>(imported);
const edgesImported = await importEdges(fragment.edges, importedSet, metadataStore);
return { nodesImported: imported.length, edgesImported, rejected };
}
/**
* Import a SubgraphSlice received via proactive peer exchange.
*
* Applies the same validation and identity stripping as `importFragment`.
*/
export async function importSlice(
slice: SubgraphSlice,
options: ImportOptions,
): Promise<ImportResult> {
const { metadataStore, vectorStore, verifyContentHashes = false } = options;
const { imported, rejected } = await importNodes(
slice.nodes,
vectorStore,
metadataStore,
verifyContentHashes,
);
const importedSet = new Set<Hash>(imported);
const edgesImported = await importEdges(slice.edges, importedSet, metadataStore);
return { nodesImported: imported.length, edgesImported, rejected };
}