@codyn06/cboard 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +68 -2
- package/THIRD_PARTY_NOTICES +29 -0
- package/bin/cboard-context.js +15 -0
- package/bin/cboard.js +5 -0
- package/dist/adapters/bash.d.ts +2 -0
- package/dist/adapters/bash.js +14 -0
- package/dist/adapters/c-family.d.ts +2 -0
- package/dist/adapters/c-family.js +81 -0
- package/dist/adapters/csharp.d.ts +2 -0
- package/dist/adapters/csharp.js +47 -0
- package/dist/adapters/extra-languages.d.ts +3 -0
- package/dist/adapters/extra-languages.js +15 -0
- package/dist/adapters/go.d.ts +2 -0
- package/dist/adapters/go.js +42 -0
- package/dist/adapters/grammar.d.ts +21 -0
- package/dist/adapters/grammar.js +54 -0
- package/dist/adapters/index.d.ts +20 -0
- package/dist/adapters/index.js +62 -0
- package/dist/adapters/java.d.ts +2 -0
- package/dist/adapters/java.js +36 -0
- package/dist/adapters/kotlin.d.ts +2 -0
- package/dist/adapters/kotlin.js +65 -0
- package/dist/adapters/lua-family.d.ts +2 -0
- package/dist/adapters/lua-family.js +186 -0
- package/dist/adapters/php.d.ts +2 -0
- package/dist/adapters/php.js +28 -0
- package/dist/adapters/python.d.ts +2 -0
- package/dist/adapters/python.js +68 -0
- package/dist/adapters/ruby.d.ts +2 -0
- package/dist/adapters/ruby.js +46 -0
- package/dist/adapters/rust.d.ts +2 -0
- package/dist/adapters/rust.js +85 -0
- package/dist/adapters/swift.d.ts +2 -0
- package/dist/adapters/swift.js +38 -0
- package/dist/adapters/types.d.ts +61 -0
- package/dist/adapters/types.js +1 -0
- package/dist/adapters/typescript.d.ts +2 -0
- package/dist/adapters/typescript.js +52 -0
- package/dist/api.d.ts +50 -0
- package/dist/api.js +421 -0
- package/dist/cboard.d.ts +2 -0
- package/dist/cboard.js +97 -0
- package/dist/cli.d.ts +27 -0
- package/dist/cli.js +180 -0
- package/dist/docs-parse.d.ts +64 -0
- package/dist/docs-parse.js +762 -0
- package/dist/extract.d.ts +36 -0
- package/dist/extract.js +458 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/languages.d.ts +9 -0
- package/dist/languages.js +128 -0
- package/dist/model.d.ts +85 -0
- package/dist/model.js +1 -0
- package/grammars/LICENSES/README.md +3 -0
- package/grammars/LICENSES/c.LICENSE +21 -0
- package/grammars/LICENSES/kotlin.LICENSE +21 -0
- package/grammars/LICENSES/lua.LICENSE +21 -0
- package/grammars/LICENSES/luau.LICENSE +21 -0
- package/grammars/LICENSES/swift.LICENSE +21 -0
- package/grammars/manifest.json +39 -0
- package/grammars/tree-sitter-c.wasm +0 -0
- package/grammars/tree-sitter-kotlin.wasm +0 -0
- package/grammars/tree-sitter-lua.wasm +0 -0
- package/grammars/tree-sitter-luau.wasm +0 -0
- package/grammars/tree-sitter-swift.wasm +0 -0
- package/kit/cboard-connect.js +490 -0
- package/kit/cboard-kit/brief.js +386 -0
- package/kit/cboard-kit/config.js +215 -0
- package/kit/cboard-kit/connect-check.js +52 -0
- package/kit/cboard-kit/headers.js +25 -0
- package/kit/cboard-kit/mcp.js +62 -0
- package/kit/cboard-kit/skill/SKILL.md +154 -0
- package/package.json +53 -4
- package/queries/bash/imports.scm +11 -0
- package/queries/bash/symbols.scm +1 -0
- package/queries/c/imports.scm +2 -0
- package/queries/c/symbols.scm +10 -0
- package/queries/cpp/imports.scm +2 -0
- package/queries/cpp/symbols.scm +11 -0
- package/queries/csharp/imports.scm +2 -0
- package/queries/csharp/symbols.scm +10 -0
- package/queries/go/imports.scm +2 -0
- package/queries/go/symbols.scm +8 -0
- package/queries/java/imports.scm +2 -0
- package/queries/java/symbols.scm +9 -0
- package/queries/javascript/imports.scm +3 -0
- package/queries/javascript/symbols.scm +16 -0
- package/queries/kotlin/imports.scm +2 -0
- package/queries/kotlin/symbols.scm +4 -0
- package/queries/lua/imports.scm +5 -0
- package/queries/lua/symbols.scm +6 -0
- package/queries/luau/imports.scm +5 -0
- package/queries/luau/symbols.scm +9 -0
- package/queries/php/imports.scm +6 -0
- package/queries/php/symbols.scm +7 -0
- package/queries/python/imports.scm +3 -0
- package/queries/python/symbols.scm +8 -0
- package/queries/ruby/imports.scm +2 -0
- package/queries/ruby/symbols.scm +4 -0
- package/queries/rust/imports.scm +5 -0
- package/queries/rust/symbols.scm +11 -0
- package/queries/swift/imports.scm +2 -0
- package/queries/swift/symbols.scm +5 -0
- package/queries/typescript/imports.scm +3 -0
- package/queries/typescript/symbols.scm +20 -0
package/dist/api.js
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
export class GraphHttpError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
constructor(status) {
|
|
5
|
+
super(`Graph HTTP ${String(status)}`);
|
|
6
|
+
this.status = status;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class GraphToolError extends Error {
|
|
10
|
+
tool;
|
|
11
|
+
code;
|
|
12
|
+
constructor(tool, code) {
|
|
13
|
+
super(`Graph MCP ${tool}: ${code}`);
|
|
14
|
+
this.tool = tool;
|
|
15
|
+
this.code = code;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class ContextApi {
|
|
19
|
+
project;
|
|
20
|
+
token;
|
|
21
|
+
base;
|
|
22
|
+
mcp;
|
|
23
|
+
constructor(server, project, token) {
|
|
24
|
+
this.project = project;
|
|
25
|
+
this.token = token;
|
|
26
|
+
const url = new URL(server);
|
|
27
|
+
if (url.username ||
|
|
28
|
+
url.password ||
|
|
29
|
+
url.search ||
|
|
30
|
+
url.hash ||
|
|
31
|
+
!["http:", "https:"].includes(url.protocol))
|
|
32
|
+
throw new Error("Invalid server URL");
|
|
33
|
+
if (url.protocol === "http:" && !["127.0.0.1", "localhost", "[::1]"].includes(url.hostname))
|
|
34
|
+
throw new Error("Remote servers require HTTPS");
|
|
35
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(project))
|
|
36
|
+
throw new Error("Project must be a UUID");
|
|
37
|
+
const root = server.replace(/\/$/, "");
|
|
38
|
+
this.base = `${root}/v1/graph/projects/${project}`;
|
|
39
|
+
this.mcp = `${root}/mcp`;
|
|
40
|
+
}
|
|
41
|
+
async request(path) {
|
|
42
|
+
const response = await fetch(this.base + path, {
|
|
43
|
+
redirect: "error",
|
|
44
|
+
headers: { authorization: `Bearer ${this.token}` },
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok)
|
|
47
|
+
throw new Error(`Graph HTTP ${String(response.status)}`);
|
|
48
|
+
return (await response.json());
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Writes go through the MCP submit_contribution tool: the CDN firewall in front of the hosted
|
|
52
|
+
* service blocks code-sync payloads on the REST contribution route (Cloudflare 403 "Blocked").
|
|
53
|
+
*/
|
|
54
|
+
async callTool(name, args) {
|
|
55
|
+
const response = await fetch(this.mcp, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
redirect: "error",
|
|
58
|
+
headers: {
|
|
59
|
+
authorization: `Bearer ${this.token}`,
|
|
60
|
+
"content-type": "application/json",
|
|
61
|
+
accept: "application/json, text/event-stream",
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
jsonrpc: "2.0",
|
|
65
|
+
id: 1,
|
|
66
|
+
method: "tools/call",
|
|
67
|
+
params: { name, arguments: args },
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw new GraphHttpError(response.status);
|
|
72
|
+
const reply = (await response.json());
|
|
73
|
+
const text = reply.result?.content?.[0]?.text;
|
|
74
|
+
if (reply.error || typeof text !== "string")
|
|
75
|
+
throw new Error(`Graph MCP ${name}: ${reply.error?.message ?? "malformed response"}`);
|
|
76
|
+
const payload = JSON.parse(text);
|
|
77
|
+
if (reply.result?.isError)
|
|
78
|
+
throw new GraphToolError(name, payload.code ?? "error");
|
|
79
|
+
if (payload.truncated || payload.data === undefined)
|
|
80
|
+
throw new Error(`Graph MCP ${name}: response truncated`);
|
|
81
|
+
return payload.data;
|
|
82
|
+
}
|
|
83
|
+
/** True when the server has recorded this contribution (it was applied or deliberately returned). */
|
|
84
|
+
async contributionRecorded(id) {
|
|
85
|
+
try {
|
|
86
|
+
await this.callTool("get_contribution_status", {
|
|
87
|
+
project_id: this.project,
|
|
88
|
+
contribution_id: id,
|
|
89
|
+
});
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
if (error instanceof GraphToolError && error.code === "NOT_FOUND")
|
|
94
|
+
return false;
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async query(filters) {
|
|
99
|
+
return this.request(`/query?${new URLSearchParams(filters)}`);
|
|
100
|
+
}
|
|
101
|
+
async all(filters) {
|
|
102
|
+
const nodes = [];
|
|
103
|
+
const edges = new Map();
|
|
104
|
+
let cursor;
|
|
105
|
+
let revision;
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
do {
|
|
108
|
+
const page = await this.query({ ...filters, limit: "300", ...(cursor ? { cursor } : {}) });
|
|
109
|
+
nodes.push(...page.nodes);
|
|
110
|
+
page.edges.forEach((e) => edges.set(e.id, e));
|
|
111
|
+
revision = page.revision;
|
|
112
|
+
cursor = page.nextCursor;
|
|
113
|
+
if (page.truncated && !cursor)
|
|
114
|
+
throw new Error("Graph response truncated without continuation");
|
|
115
|
+
if (cursor && seen.has(cursor))
|
|
116
|
+
throw new Error("Repeated graph cursor");
|
|
117
|
+
if (cursor)
|
|
118
|
+
seen.add(cursor);
|
|
119
|
+
} while (cursor);
|
|
120
|
+
return { nodes, edges: [...edges.values()], revision, truncated: false };
|
|
121
|
+
}
|
|
122
|
+
async submit(body) {
|
|
123
|
+
const result = await this.callTool("submit_contribution", {
|
|
124
|
+
project_id: this.project,
|
|
125
|
+
// Encoded so the hosted CDN firewall cannot match source excerpts as attacks (#65).
|
|
126
|
+
contribution_b64: Buffer.from(JSON.stringify(body)).toString("base64"),
|
|
127
|
+
});
|
|
128
|
+
if (result.status !== "applied")
|
|
129
|
+
throw new Error(`Contribution returned; batch applied nothing: ${JSON.stringify(result.conflicts)}`);
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
async nodeEdges(id) {
|
|
133
|
+
const edges = [];
|
|
134
|
+
let cursor;
|
|
135
|
+
do {
|
|
136
|
+
const page = await this.request(`/nodes/${id}?include_inactive=true${cursor ? `&edge_cursor=${cursor}` : ""}`);
|
|
137
|
+
edges.push(...page.edges);
|
|
138
|
+
if (page.truncated && !page.nextEdgeCursor)
|
|
139
|
+
throw new Error("Node edges truncated without continuation");
|
|
140
|
+
cursor = page.nextEdgeCursor;
|
|
141
|
+
} while (cursor);
|
|
142
|
+
return edges;
|
|
143
|
+
}
|
|
144
|
+
async allEdges(namespace) {
|
|
145
|
+
const edges = [];
|
|
146
|
+
let cursor;
|
|
147
|
+
const seen = new Set();
|
|
148
|
+
do {
|
|
149
|
+
const page = await this.request(`/edges?${new URLSearchParams({ namespace, limit: "300", include_inactive: "true", ...(cursor ? { cursor } : {}) })}`);
|
|
150
|
+
edges.push(...page.edges);
|
|
151
|
+
cursor = page.nextCursor;
|
|
152
|
+
if (page.truncated && !cursor)
|
|
153
|
+
throw new Error("Edge list truncated without continuation");
|
|
154
|
+
if (cursor && seen.has(cursor))
|
|
155
|
+
throw new Error("Repeated edge cursor");
|
|
156
|
+
if (cursor)
|
|
157
|
+
seen.add(cursor);
|
|
158
|
+
} while (cursor);
|
|
159
|
+
return edges;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Batch sizing: the server accepts 200 items and 200,000 bytes per contribution. The item cap
|
|
164
|
+
* stays at 50 (below) because server cost per contribution is roughly quadratic in items; raising
|
|
165
|
+
* it risks the statement timeout for little gain. Write requests scale as nodes/50, reads as pages.
|
|
166
|
+
* The server allows 200,000 bytes, measured after the MCP tool schema fills defaults (aliases,
|
|
167
|
+
* namespace, properties…). Batches leave 20,000 bytes for those.
|
|
168
|
+
*/
|
|
169
|
+
const BATCH_BYTES = 180_000;
|
|
170
|
+
/**
|
|
171
|
+
* Each contribution is one SERIALIZABLE transaction with pairwise similarity() queries, so cost
|
|
172
|
+
* grows roughly quadratically with batch size; 50 stays under the 5 s statement timeout (#43).
|
|
173
|
+
*/
|
|
174
|
+
export const BATCH_ITEMS = 50;
|
|
175
|
+
export function chunks(items, make) {
|
|
176
|
+
const result = [];
|
|
177
|
+
let current = [];
|
|
178
|
+
for (const item of items) {
|
|
179
|
+
const candidate = [...current, item];
|
|
180
|
+
if (candidate.length > BATCH_ITEMS ||
|
|
181
|
+
Buffer.byteLength(JSON.stringify(make(candidate))) > BATCH_BYTES) {
|
|
182
|
+
if (!current.length)
|
|
183
|
+
throw new Error("Single contribution item exceeds 200KB");
|
|
184
|
+
result.push(current);
|
|
185
|
+
current = [item];
|
|
186
|
+
if (Buffer.byteLength(JSON.stringify(make(current))) > BATCH_BYTES)
|
|
187
|
+
throw new Error("Single contribution item exceeds 200KB");
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
current = candidate;
|
|
191
|
+
}
|
|
192
|
+
if (current.length)
|
|
193
|
+
result.push(current);
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* A failure that suggests the transaction did not commit. A PostgreSQL statement timeout reaches
|
|
198
|
+
* the client through the MCP tool wrapper as HTTP 200 with isError and the code "INTERNAL" (the raw
|
|
199
|
+
* 57014/"timeout" text is never exposed), so that exact code is matched. HTTP 5xx only comes from a
|
|
200
|
+
* gateway or proxy in front of the server.
|
|
201
|
+
* Known race: after a 502/504 the server may still commit after get_contribution_status returned
|
|
202
|
+
* NOT_FOUND; the halves then hit conflicts and the run stops. That is safe but not self-healing.
|
|
203
|
+
*/
|
|
204
|
+
function isRolledBack(error) {
|
|
205
|
+
if (error instanceof GraphHttpError)
|
|
206
|
+
return error.status >= 500;
|
|
207
|
+
return error instanceof GraphToolError && error.code === "INTERNAL";
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Submits one batch. On a server error or statement timeout the batch is retried once as two
|
|
211
|
+
* halves, but only after the server confirms it never recorded the original contribution. A
|
|
212
|
+
* failure of either half propagates and stops the run. Returns one receipt per submitted piece.
|
|
213
|
+
*/
|
|
214
|
+
export async function submitBatch(api, group, make) {
|
|
215
|
+
const whole = make(group);
|
|
216
|
+
try {
|
|
217
|
+
return [{ items: group, receipt: await api.submit(whole) }];
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
if (group.length < 2 || !isRolledBack(error))
|
|
221
|
+
throw error;
|
|
222
|
+
if (await api.contributionRecorded(whole.contributionId))
|
|
223
|
+
throw error;
|
|
224
|
+
const mid = Math.ceil(group.length / 2);
|
|
225
|
+
const out = [];
|
|
226
|
+
for (const half of [group.slice(0, mid), group.slice(mid)])
|
|
227
|
+
out.push({ items: half, receipt: await api.submit(make(half)) });
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function stable(value) {
|
|
232
|
+
const sort = (v) => Array.isArray(v)
|
|
233
|
+
? v.map(sort)
|
|
234
|
+
: v && typeof v === "object"
|
|
235
|
+
? Object.fromEntries(Object.entries(v)
|
|
236
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
237
|
+
.map(([k, x]) => [k, sort(x)]))
|
|
238
|
+
: v;
|
|
239
|
+
return JSON.stringify(sort(value));
|
|
240
|
+
}
|
|
241
|
+
const comparable = (n) => stable([
|
|
242
|
+
n.title,
|
|
243
|
+
n.body,
|
|
244
|
+
n.status ?? null,
|
|
245
|
+
n.properties,
|
|
246
|
+
n.derivation,
|
|
247
|
+
n.provenance.map((p) => [p.repo, p.commit, p.path, p.startLine, p.endLine]),
|
|
248
|
+
]);
|
|
249
|
+
const edgeKey = (e) => stable([
|
|
250
|
+
e.type,
|
|
251
|
+
e.from,
|
|
252
|
+
e.to,
|
|
253
|
+
e.properties.contextNamespace ?? null,
|
|
254
|
+
e.type === "amends" ? (e.properties.scope ?? null) : null,
|
|
255
|
+
]);
|
|
256
|
+
/** Server state is authoritative; local state is never used to skip reconciliation. */
|
|
257
|
+
export async function reconcile(api, snapshot, namespace, supersede = true) {
|
|
258
|
+
const existing = await api.all({ namespace, include_inactive: "true" });
|
|
259
|
+
const byKey = new Map(existing.nodes.map((n) => [n.clientKey, n]));
|
|
260
|
+
// Entities imported before namespace adoption carry the same client key outside the namespace.
|
|
261
|
+
// One project-wide scan of the snapshot's node types (pages of 300) replaces one exact
|
|
262
|
+
// client_key request per absent node, so a first sync of N nodes costs pages, not N requests.
|
|
263
|
+
const absent = new Set(snapshot.nodes.filter((n) => !byKey.has(n.clientKey)).map((n) => n.clientKey));
|
|
264
|
+
if (absent.size) {
|
|
265
|
+
const types = [...new Set(snapshot.nodes.map((n) => n.type))].join(",");
|
|
266
|
+
const found = new Map();
|
|
267
|
+
for (const n of (await api.all({ types, include_inactive: "true" })).nodes)
|
|
268
|
+
if (absent.has(n.clientKey)) {
|
|
269
|
+
if (found.has(n.clientKey))
|
|
270
|
+
throw new Error(`Ambiguous client key ${n.clientKey}`);
|
|
271
|
+
found.set(n.clientKey, n);
|
|
272
|
+
}
|
|
273
|
+
for (const [key, n] of found)
|
|
274
|
+
byKey.set(key, n);
|
|
275
|
+
}
|
|
276
|
+
const ids = new Map([...byKey].map(([key, n]) => [key, n.id]));
|
|
277
|
+
const desired = new Set(snapshot.nodes.map((n) => n.clientKey));
|
|
278
|
+
const revisions = [];
|
|
279
|
+
const fresh = snapshot.nodes.filter((n) => !byKey.has(n.clientKey));
|
|
280
|
+
for (const n of snapshot.nodes) {
|
|
281
|
+
const old = byKey.get(n.clientKey);
|
|
282
|
+
if (old && (comparable(n) !== comparable(old) || old.lifecycle !== "active"))
|
|
283
|
+
revisions.push({
|
|
284
|
+
nodeId: old.id,
|
|
285
|
+
expectedRevision: old.revision,
|
|
286
|
+
title: n.title,
|
|
287
|
+
body: n.body,
|
|
288
|
+
...(n.status ? { status: n.status } : {}),
|
|
289
|
+
properties: n.properties,
|
|
290
|
+
provenance: n.provenance,
|
|
291
|
+
derivation: n.derivation,
|
|
292
|
+
lifecycle: "active",
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
if (supersede)
|
|
296
|
+
for (const old of existing.nodes)
|
|
297
|
+
if (!desired.has(old.clientKey) && old.lifecycle === "active")
|
|
298
|
+
revisions.push({
|
|
299
|
+
nodeId: old.id,
|
|
300
|
+
expectedRevision: old.revision,
|
|
301
|
+
lifecycle: "superseded",
|
|
302
|
+
reason: "Absent from repository snapshot",
|
|
303
|
+
provenance: old.provenance,
|
|
304
|
+
});
|
|
305
|
+
let contributions = 0;
|
|
306
|
+
const empty = () => ({
|
|
307
|
+
contributionId: randomUUID(),
|
|
308
|
+
summary: "Deterministic repository context sync",
|
|
309
|
+
nodes: [],
|
|
310
|
+
edges: [],
|
|
311
|
+
revisions: [],
|
|
312
|
+
edgeRevisions: [],
|
|
313
|
+
});
|
|
314
|
+
for (const batch of chunks(fresh, (nodes) => ({ ...empty(), nodes })))
|
|
315
|
+
for (const { items: group, receipt } of await submitBatch(api, batch, (nodes) => ({
|
|
316
|
+
...empty(),
|
|
317
|
+
nodes,
|
|
318
|
+
}))) {
|
|
319
|
+
contributions++;
|
|
320
|
+
if (receipt.nodeIds.length !== group.length)
|
|
321
|
+
throw new Error("Receipt node mapping incomplete");
|
|
322
|
+
group.forEach((n, i) => {
|
|
323
|
+
const id = receipt.nodeIds[i];
|
|
324
|
+
if (!id)
|
|
325
|
+
throw new Error("Missing node receipt id");
|
|
326
|
+
ids.set(n.clientKey, id);
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
for (const batch of chunks(revisions, (revisions) => ({ ...empty(), revisions })))
|
|
330
|
+
contributions += (await submitBatch(api, batch, (revisions) => ({ ...empty(), revisions })))
|
|
331
|
+
.length;
|
|
332
|
+
const currentEdges = new Map((await api.allEdges(namespace)).map((e) => [e.id, e]));
|
|
333
|
+
const desiredEdges = snapshot.edges.map((e) => {
|
|
334
|
+
const from = ids.get(e.from), to = ids.get(e.to);
|
|
335
|
+
if (!from || !to)
|
|
336
|
+
throw new Error("Unresolved edge endpoint");
|
|
337
|
+
return { ...e, from, to, properties: { ...e.properties, contextNamespace: namespace } };
|
|
338
|
+
});
|
|
339
|
+
const grouped = new Map();
|
|
340
|
+
for (const edge of desiredEdges) {
|
|
341
|
+
const key = edgeKey(edge);
|
|
342
|
+
const old = grouped.get(key);
|
|
343
|
+
if (old) {
|
|
344
|
+
if (stable(old.properties) !== stable(edge.properties) || old.derivation !== edge.derivation)
|
|
345
|
+
throw new Error("Ambiguous duplicate relationship metadata");
|
|
346
|
+
old.provenance.push(...edge.provenance);
|
|
347
|
+
}
|
|
348
|
+
else
|
|
349
|
+
grouped.set(key, { ...edge, provenance: [...edge.provenance] });
|
|
350
|
+
}
|
|
351
|
+
const edges = [...grouped.values()].map((edge) => {
|
|
352
|
+
const sources = new Map();
|
|
353
|
+
for (const locator of edge.provenance) {
|
|
354
|
+
const key = stable([locator.repo, locator.commit, locator.path]);
|
|
355
|
+
const old = sources.get(key);
|
|
356
|
+
sources.set(key, old
|
|
357
|
+
? {
|
|
358
|
+
...locator,
|
|
359
|
+
startLine: Math.min(old.startLine, locator.startLine),
|
|
360
|
+
endLine: Math.max(old.endLine, locator.endLine),
|
|
361
|
+
}
|
|
362
|
+
: locator);
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
...edge,
|
|
366
|
+
provenance: [...sources.values()].sort((a, b) => stable(a).localeCompare(stable(b))),
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
const active = new Set([...currentEdges.values()].filter((e) => e.lifecycle === "active").map(edgeKey));
|
|
370
|
+
const wanted = new Set(edges.map(edgeKey));
|
|
371
|
+
const missing = edges.filter((e) => !active.has(edgeKey(e)));
|
|
372
|
+
for (const batch of chunks(missing, (edges) => ({ ...empty(), edges })))
|
|
373
|
+
contributions += (await submitBatch(api, batch, (edges) => ({ ...empty(), edges }))).length;
|
|
374
|
+
const activeByKey = new Map();
|
|
375
|
+
const duplicateIds = new Set();
|
|
376
|
+
for (const edge of [...currentEdges.values()]
|
|
377
|
+
.filter((e) => e.lifecycle === "active" && e.properties.contextNamespace === namespace)
|
|
378
|
+
.sort((a, b) => a.id.localeCompare(b.id))) {
|
|
379
|
+
const key = edgeKey(edge);
|
|
380
|
+
if (activeByKey.has(key))
|
|
381
|
+
duplicateIds.add(edge.id);
|
|
382
|
+
else
|
|
383
|
+
activeByKey.set(key, edge);
|
|
384
|
+
}
|
|
385
|
+
const updated = [];
|
|
386
|
+
for (const edge of edges) {
|
|
387
|
+
const old = activeByKey.get(edgeKey(edge));
|
|
388
|
+
if (old &&
|
|
389
|
+
stable([old.provenance, old.derivation, old.properties]) !==
|
|
390
|
+
stable([edge.provenance, edge.derivation, edge.properties]))
|
|
391
|
+
updated.push({
|
|
392
|
+
edgeId: old.id,
|
|
393
|
+
expectedRevision: old.revision,
|
|
394
|
+
provenance: edge.provenance,
|
|
395
|
+
derivation: edge.derivation,
|
|
396
|
+
properties: edge.properties,
|
|
397
|
+
lifecycle: "active",
|
|
398
|
+
reason: "Repository snapshot metadata changed",
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const edgeRevisions = [...currentEdges.values()]
|
|
402
|
+
.filter((e) => e.lifecycle === "active" &&
|
|
403
|
+
e.properties.contextNamespace === namespace &&
|
|
404
|
+
(!wanted.has(edgeKey(e)) || duplicateIds.has(e.id)))
|
|
405
|
+
.map((e) => ({
|
|
406
|
+
edgeId: e.id,
|
|
407
|
+
expectedRevision: e.revision,
|
|
408
|
+
provenance: e.provenance,
|
|
409
|
+
lifecycle: "superseded",
|
|
410
|
+
reason: duplicateIds.has(e.id)
|
|
411
|
+
? "Duplicate relationship consolidated with retained provenance"
|
|
412
|
+
: "Absent from repository snapshot",
|
|
413
|
+
}))
|
|
414
|
+
.concat(updated);
|
|
415
|
+
for (const batch of chunks(edgeRevisions, (edgeRevisions) => ({ ...empty(), edgeRevisions })))
|
|
416
|
+
contributions += (await submitBatch(api, batch, (edgeRevisions) => ({ ...empty(), edgeRevisions }))).length;
|
|
417
|
+
return {
|
|
418
|
+
contributions,
|
|
419
|
+
digest: createHash("sha256").update(JSON.stringify(snapshot)).digest("hex"),
|
|
420
|
+
};
|
|
421
|
+
}
|
package/dist/cboard.d.ts
ADDED
package/dist/cboard.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { sync } from "./cli.js";
|
|
5
|
+
const HELP = `cboard — client for cboard project graphs
|
|
6
|
+
|
|
7
|
+
Usage: cboard <command> [flags]
|
|
8
|
+
|
|
9
|
+
Commands:
|
|
10
|
+
connect Connect this repository to a cboard project (writes .cboard.json, .cboard/, .mcp.json, hooks)
|
|
11
|
+
sync Sync this repository's code layer to the cboard graph
|
|
12
|
+
check Check that this repository can reach its cboard graph
|
|
13
|
+
|
|
14
|
+
Run "cboard <command> --help" for a command's flags.
|
|
15
|
+
Options:
|
|
16
|
+
--help, -h Show this help
|
|
17
|
+
--version, -v Show the version
|
|
18
|
+
`;
|
|
19
|
+
const CONNECT_HELP = `Usage: cboard connect --project <uuid> [--server <https url>] [--repo <path>]
|
|
20
|
+
[--no-hooks] [--no-agents] [--no-skill] [--strict] [--codex]
|
|
21
|
+
|
|
22
|
+
Writes .cboard.json, the committed kit in .cboard/ (hooks and the MCP header helper run it, never npx),
|
|
23
|
+
.mcp.json, the SessionStart hooks, an AGENTS.md block, the cboard-graph skill and a .gitattributes
|
|
24
|
+
block. Rerun to update the kit. --codex also prints the Codex MCP config snippet.
|
|
25
|
+
`;
|
|
26
|
+
const SYNC_HELP = `Usage: cboard sync [--repo PATH] [--project UUID] [--server URL] [--token-env NAME] [--state PATH]
|
|
27
|
+
|
|
28
|
+
Project, server and token come from .cboard.json and the repo token file when the flags are omitted.
|
|
29
|
+
`;
|
|
30
|
+
const CHECK_HELP = `Usage: cboard check
|
|
31
|
+
|
|
32
|
+
Reports whether the token in this repository is accepted and its configured project is visible.
|
|
33
|
+
Exits 1 when it is not connected.
|
|
34
|
+
`;
|
|
35
|
+
const packageFile = new URL("../package.json", import.meta.url);
|
|
36
|
+
function version() {
|
|
37
|
+
return JSON.parse(readFileSync(packageFile, "utf8")).version;
|
|
38
|
+
}
|
|
39
|
+
/** The kit lives at ../kit in the installed package (copied by scripts/build-kit.js) and under scripts/ in a checkout. */
|
|
40
|
+
function kitFile(name) {
|
|
41
|
+
for (const base of ["../kit/", "../../../scripts/"]) {
|
|
42
|
+
const file = fileURLToPath(new URL(base + name, import.meta.url));
|
|
43
|
+
if (existsSync(file))
|
|
44
|
+
return file;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`the connect kit is missing (${name}); reinstall @codyn06/cboard`);
|
|
47
|
+
}
|
|
48
|
+
function run(script, args) {
|
|
49
|
+
const result = spawnSync(process.execPath, [script, ...args], { stdio: "inherit" });
|
|
50
|
+
return result.status ?? 1;
|
|
51
|
+
}
|
|
52
|
+
/** @returns the process exit code */
|
|
53
|
+
export async function main(argv) {
|
|
54
|
+
const [command, ...rest] = argv;
|
|
55
|
+
const wantsHelp = rest.includes("--help") || rest.includes("-h");
|
|
56
|
+
switch (command) {
|
|
57
|
+
case "--version":
|
|
58
|
+
case "-v":
|
|
59
|
+
console.log(version());
|
|
60
|
+
return 0;
|
|
61
|
+
case "--help":
|
|
62
|
+
case "-h":
|
|
63
|
+
case "help":
|
|
64
|
+
console.log(HELP);
|
|
65
|
+
return 0;
|
|
66
|
+
case "connect":
|
|
67
|
+
if (wantsHelp) {
|
|
68
|
+
console.log(CONNECT_HELP);
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
return run(kitFile("cboard-connect.js"), rest);
|
|
72
|
+
case "check":
|
|
73
|
+
if (wantsHelp) {
|
|
74
|
+
console.log(CHECK_HELP);
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
return run(kitFile("cboard-kit/connect-check.js"), rest);
|
|
78
|
+
case "sync":
|
|
79
|
+
if (wantsHelp) {
|
|
80
|
+
console.log(SYNC_HELP);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
await sync(rest);
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
console.error(error instanceof Error ? error.message : "Context sync failed");
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
default:
|
|
92
|
+
if (command)
|
|
93
|
+
console.error(`cboard: unknown command "${command}"\n`);
|
|
94
|
+
console.error(HELP);
|
|
95
|
+
return 2;
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
export declare function options(argv: string[]): Record<string, string>;
|
|
3
|
+
interface RepoConfig {
|
|
4
|
+
server?: string;
|
|
5
|
+
projectId?: string;
|
|
6
|
+
tokenFile?: string;
|
|
7
|
+
tokenEnv?: string;
|
|
8
|
+
}
|
|
9
|
+
/** The repository's .cboard.json (written by cboard-connect), or {} when absent. */
|
|
10
|
+
export declare function repoConfig(root: string): RepoConfig;
|
|
11
|
+
export declare const DEFAULT_SERVER = "https://cboard-fij7.onrender.com";
|
|
12
|
+
export declare const DEFAULT_TOKEN_ENV = "CBOARD_PAT";
|
|
13
|
+
/**
|
|
14
|
+
* Which server, project and token a sync uses. Explicit flags win over the environment, which
|
|
15
|
+
* wins over .cboard.json. .cboard.json is tracked, so a cloned repository must not be able to
|
|
16
|
+
* steer a token elsewhere: these rules mirror resolveConfig in scripts/cboard-kit/config.js
|
|
17
|
+
* (keep them in step): tokenEnv must be CBOARD_*, tokenFile must stay inside the repository, a
|
|
18
|
+
* non-default server named only by the file needs CBOARD_TRUSTED_SERVERS, non-local servers
|
|
19
|
+
* must be https, and a local server never receives the hosted token file. The token is never logged.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveSync(args: Record<string, string>, root: string): {
|
|
22
|
+
project: string;
|
|
23
|
+
token: string;
|
|
24
|
+
server: string;
|
|
25
|
+
};
|
|
26
|
+
export declare function sync(argv: string[]): Promise<void>;
|
|
27
|
+
export {};
|