@rubytech/create-realagent 1.0.678 → 1.0.681

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.
Files changed (60) hide show
  1. package/dist/index.js +232 -39
  2. package/package.json +1 -1
  3. package/payload/platform/lib/graph-mcp/dist/__tests__/cypher-validate.test.d.ts +2 -0
  4. package/payload/platform/lib/graph-mcp/dist/__tests__/cypher-validate.test.d.ts.map +1 -0
  5. package/payload/platform/lib/graph-mcp/dist/__tests__/cypher-validate.test.js +112 -0
  6. package/payload/platform/lib/graph-mcp/dist/__tests__/cypher-validate.test.js.map +1 -0
  7. package/payload/platform/lib/graph-mcp/dist/__tests__/schema-cache.test.d.ts +2 -0
  8. package/payload/platform/lib/graph-mcp/dist/__tests__/schema-cache.test.d.ts.map +1 -0
  9. package/payload/platform/lib/graph-mcp/dist/__tests__/schema-cache.test.js +163 -0
  10. package/payload/platform/lib/graph-mcp/dist/__tests__/schema-cache.test.js.map +1 -0
  11. package/payload/platform/lib/graph-mcp/dist/cypher-validate.d.ts +38 -0
  12. package/payload/platform/lib/graph-mcp/dist/cypher-validate.d.ts.map +1 -0
  13. package/payload/platform/lib/graph-mcp/dist/cypher-validate.js +130 -0
  14. package/payload/platform/lib/graph-mcp/dist/cypher-validate.js.map +1 -0
  15. package/payload/platform/lib/graph-mcp/dist/index.js +201 -45
  16. package/payload/platform/lib/graph-mcp/dist/index.js.map +1 -1
  17. package/payload/platform/lib/graph-mcp/dist/schema-cache.d.ts +78 -0
  18. package/payload/platform/lib/graph-mcp/dist/schema-cache.d.ts.map +1 -0
  19. package/payload/platform/lib/graph-mcp/dist/schema-cache.js +194 -0
  20. package/payload/platform/lib/graph-mcp/dist/schema-cache.js.map +1 -0
  21. package/payload/platform/lib/graph-mcp/src/__tests__/cypher-validate.test.ts +141 -0
  22. package/payload/platform/lib/graph-mcp/src/__tests__/schema-cache.test.ts +169 -0
  23. package/payload/platform/lib/graph-mcp/src/cypher-validate.ts +157 -0
  24. package/payload/platform/lib/graph-mcp/src/index.ts +247 -47
  25. package/payload/platform/lib/graph-mcp/src/schema-cache.ts +212 -0
  26. package/payload/platform/lib/graph-trash/dist/index.d.ts +8 -0
  27. package/payload/platform/lib/graph-trash/dist/index.d.ts.map +1 -1
  28. package/payload/platform/lib/graph-trash/dist/index.js +109 -14
  29. package/payload/platform/lib/graph-trash/dist/index.js.map +1 -1
  30. package/payload/platform/lib/graph-trash/src/index.ts +136 -21
  31. package/payload/platform/plugins/docs/references/deployment.md +4 -2
  32. package/payload/platform/plugins/docs/references/memory-guide.md +5 -1
  33. package/payload/platform/plugins/docs/references/platform.md +1 -1
  34. package/payload/platform/plugins/docs/references/troubleshooting.md +20 -0
  35. package/payload/platform/plugins/memory/PLUGIN.md +1 -0
  36. package/payload/platform/plugins/memory/mcp/dist/index.js +54 -6
  37. package/payload/platform/plugins/memory/mcp/dist/index.js.map +1 -1
  38. package/payload/platform/plugins/memory/mcp/dist/lib/filter-token.d.ts +36 -0
  39. package/payload/platform/plugins/memory/mcp/dist/lib/filter-token.d.ts.map +1 -0
  40. package/payload/platform/plugins/memory/mcp/dist/lib/filter-token.js +86 -0
  41. package/payload/platform/plugins/memory/mcp/dist/lib/filter-token.js.map +1 -0
  42. package/payload/platform/plugins/memory/mcp/dist/tools/memory-delete.d.ts +23 -0
  43. package/payload/platform/plugins/memory/mcp/dist/tools/memory-delete.d.ts.map +1 -1
  44. package/payload/platform/plugins/memory/mcp/dist/tools/memory-delete.js +47 -1
  45. package/payload/platform/plugins/memory/mcp/dist/tools/memory-delete.js.map +1 -1
  46. package/payload/platform/plugins/memory/mcp/dist/tools/memory-find-candidates.d.ts +58 -0
  47. package/payload/platform/plugins/memory/mcp/dist/tools/memory-find-candidates.d.ts.map +1 -0
  48. package/payload/platform/plugins/memory/mcp/dist/tools/memory-find-candidates.js +125 -0
  49. package/payload/platform/plugins/memory/mcp/dist/tools/memory-find-candidates.js.map +1 -0
  50. package/payload/platform/scripts/vnc.sh +12 -409
  51. package/payload/platform/templates/agents/admin/IDENTITY.md +16 -0
  52. package/payload/platform/templates/dotfiles/.tmux.conf +1 -0
  53. package/payload/platform/templates/systemd/maxy-ttyd.service +25 -0
  54. package/payload/server/chunk-3RBKKDHC.js +783 -0
  55. package/payload/server/maxy-edge.js +377 -8
  56. package/payload/server/public/assets/admin-CIkyOur7.js +362 -0
  57. package/payload/server/public/assets/admin-kHJ-D0s7.css +1 -0
  58. package/payload/server/public/index.html +2 -1
  59. package/payload/server/server.js +391 -412
  60. package/payload/server/public/assets/admin-BBL1no_g.js +0 -352
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Cypher schema validation for the graph-mcp proxy (Task 654).
3
+ *
4
+ * Tokenises a cypher string for labels (`:Label`) and relationship types
5
+ * (`[:REL_TYPE]`, including `[r:TYPE]`, `[:TYPE*1..5]`, `[:A|B]`) and checks
6
+ * each against a schema snapshot. Unknown tokens are returned with the
7
+ * Levenshtein-nearest known tokens of the same kind as suggestions.
8
+ *
9
+ * Design posture:
10
+ * - Defence layer, not a cypher correctness gate. Pass-through on any
11
+ * unparseable pattern — Neo4j remains the syntax authority.
12
+ * - Empty schema snapshot (cache not ready, or Neo4j unreachable at boot)
13
+ * fails OPEN: ok=true, no rejections. The boot-race treatment is a
14
+ * deliberate choice — refusing all cypher would wedge the admin agent
15
+ * harder than the typo class this layer prevents. The caller emits
16
+ * `validated=false` on the existing [graph-query] line so operators
17
+ * still see the bypass.
18
+ * - String literals are stripped before tokenisation to avoid matching
19
+ * `:Foo` inside quoted content (e.g. `WHERE n.note CONTAINS ':Foo'`).
20
+ */
21
+
22
+ export interface SchemaSnapshot {
23
+ readonly labels: ReadonlySet<string>;
24
+ readonly relationshipTypes: ReadonlySet<string>;
25
+ }
26
+
27
+ export interface UnknownToken {
28
+ token: string;
29
+ kind: "label" | "relationship";
30
+ nearest: string[];
31
+ hint: string;
32
+ }
33
+
34
+ export interface ValidationResult {
35
+ ok: boolean;
36
+ unknown: UnknownToken[];
37
+ labelTokens: string[];
38
+ edgeTokens: string[];
39
+ }
40
+
41
+ // Bracket content containing a type reference. Examples this matches:
42
+ // [:PART_OF]
43
+ // [r:PART_OF]
44
+ // [:PART_OF*1..5]
45
+ // [r:PART_OF*]
46
+ // [:A|B]
47
+ // The captured group is the TYPE(|TYPE)* alternation; the consumer splits it.
48
+ const EDGE_PATTERN = /\[[^\]]*?:([A-Z_][A-Za-z0-9_]*(?:\|[A-Z_][A-Za-z0-9_]*)*)[^\]]*?\]/g;
49
+
50
+ // Label reference in a node pattern. Applied to a remainder string that has
51
+ // already had edge-bracket substrings stripped, so there is no overlap with
52
+ // the edge pattern. The [A-Z] anchor excludes lowercase map keys.
53
+ const LABEL_PATTERN = /:([A-Z][A-Za-z0-9_]*)/g;
54
+
55
+ function stripStringLiterals(cypher: string): string {
56
+ // Replace single- and double-quoted literals with empty quotes. Preserves
57
+ // positional structure without retaining content that could match the
58
+ // label regex (e.g. ':SomeLabel' inside a string).
59
+ return cypher.replace(/'[^']*'|"[^"]*"/g, '""');
60
+ }
61
+
62
+ function extractTokens(cypher: string): { labels: Set<string>; edges: Set<string> } {
63
+ const cleaned = stripStringLiterals(cypher);
64
+ const edges = new Set<string>();
65
+ let match: RegExpExecArray | null;
66
+ const edgePattern = new RegExp(EDGE_PATTERN.source, EDGE_PATTERN.flags);
67
+ while ((match = edgePattern.exec(cleaned)) !== null) {
68
+ for (const type of match[1].split("|")) {
69
+ const clean = type.trim();
70
+ if (clean) edges.add(clean);
71
+ }
72
+ }
73
+ const remainder = cleaned.replace(edgePattern, "");
74
+ const labels = new Set<string>();
75
+ const labelPattern = new RegExp(LABEL_PATTERN.source, LABEL_PATTERN.flags);
76
+ while ((match = labelPattern.exec(remainder)) !== null) {
77
+ labels.add(match[1]);
78
+ }
79
+ return { labels, edges };
80
+ }
81
+
82
+ function levenshtein(a: string, b: string): number {
83
+ if (a === b) return 0;
84
+ if (a.length === 0) return b.length;
85
+ if (b.length === 0) return a.length;
86
+ let prev = new Array<number>(b.length + 1);
87
+ let curr = new Array<number>(b.length + 1);
88
+ for (let j = 0; j <= b.length; j++) prev[j] = j;
89
+ for (let i = 1; i <= a.length; i++) {
90
+ curr[0] = i;
91
+ for (let j = 1; j <= b.length; j++) {
92
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
93
+ curr[j] = Math.min(curr[j - 1] + 1, prev[j] + 1, prev[j - 1] + cost);
94
+ }
95
+ [prev, curr] = [curr, prev];
96
+ }
97
+ return prev[b.length];
98
+ }
99
+
100
+ function nearestMatches(
101
+ token: string,
102
+ known: ReadonlySet<string>,
103
+ limit = 3,
104
+ ): string[] {
105
+ if (known.size === 0) return [];
106
+ const scored: Array<[string, number]> = [];
107
+ for (const k of known) scored.push([k, levenshtein(token, k)]);
108
+ scored.sort((a, b) => a[1] - b[1] || a[0].localeCompare(b[0]));
109
+ return scored.slice(0, limit).map(([k]) => k);
110
+ }
111
+
112
+ function hintFor(
113
+ token: string,
114
+ kind: "label" | "relationship",
115
+ nearest: string[],
116
+ ): string {
117
+ const didYouMean = nearest.length > 0 ? `Did you mean :${nearest[0]}? ` : "";
118
+ return `${didYouMean}Unknown ${kind} '${token}' — not in the current Neo4j schema. See .docs/neo4j.md for the canonical taxonomy.`;
119
+ }
120
+
121
+ export function validate(
122
+ cypher: string,
123
+ snapshot: SchemaSnapshot,
124
+ ): ValidationResult {
125
+ const { labels, edges } = extractTokens(cypher);
126
+ // Fail-open when the snapshot is empty. An empty snapshot means "schema
127
+ // cache not loaded" (boot race, Neo4j unreachable). Rejecting every token
128
+ // would wedge the admin agent; letting it through preserves the observable
129
+ // `validated=false` signal on the existing [graph-query] line.
130
+ if (snapshot.labels.size === 0 && snapshot.relationshipTypes.size === 0) {
131
+ return {
132
+ ok: true,
133
+ unknown: [],
134
+ labelTokens: [...labels],
135
+ edgeTokens: [...edges],
136
+ };
137
+ }
138
+ const unknown: UnknownToken[] = [];
139
+ for (const token of labels) {
140
+ if (!snapshot.labels.has(token)) {
141
+ const nearest = nearestMatches(token, snapshot.labels);
142
+ unknown.push({ token, kind: "label", nearest, hint: hintFor(token, "label", nearest) });
143
+ }
144
+ }
145
+ for (const token of edges) {
146
+ if (!snapshot.relationshipTypes.has(token)) {
147
+ const nearest = nearestMatches(token, snapshot.relationshipTypes);
148
+ unknown.push({ token, kind: "relationship", nearest, hint: hintFor(token, "relationship", nearest) });
149
+ }
150
+ }
151
+ return {
152
+ ok: unknown.length === 0,
153
+ unknown,
154
+ labelTokens: [...labels],
155
+ edgeTokens: [...edges],
156
+ };
157
+ }
@@ -28,6 +28,8 @@ import { accessSync, appendFileSync, constants, mkdirSync, readFileSync, statSyn
28
28
  import { resolve } from "node:path";
29
29
  import { StringDecoder } from "node:string_decoder";
30
30
  import { initStderrTee } from "../../mcp-stderr-tee/dist/index.js";
31
+ import { validate as validateCypher, type UnknownToken } from "./cypher-validate.js";
32
+ import { SchemaCache, neo4jSchemaFetcher } from "./schema-cache.js";
31
33
 
32
34
  const SERVER_NAME = "graph";
33
35
  const UPSTREAM_PACKAGE = "mcp-neo4j-cypher@0.6.0";
@@ -179,6 +181,38 @@ console.error(
179
181
  `namespace=${namespace} readOnly=${readOnly} tokenLimit=${responseTokenLimit}`,
180
182
  );
181
183
 
184
+ // Task 654 — async schema cache. The validator fails OPEN until the first
185
+ // refresh resolves; all cypher calls during that window are forwarded
186
+ // unvalidated and the existing [graph-query] line gains `validated=false`
187
+ // so operators see the bypass. One Neo4j driver is shared across refreshes;
188
+ // loaded lazily so test harnesses can exercise SchemaCache without pulling
189
+ // neo4j-driver into the module graph.
190
+ // neo4jUri is narrowed to `string` by the guard throw above, but the narrow
191
+ // doesn't flow into the closure below. Pin it to a local const whose type
192
+ // is `string` by construction.
193
+ const resolvedNeo4jUri: string = neo4jUri;
194
+ let schemaFetcherReady: Promise<Awaited<ReturnType<typeof neo4jSchemaFetcher>>> | null = null;
195
+ function getSchemaFetcher(): Promise<Awaited<ReturnType<typeof neo4jSchemaFetcher>>> {
196
+ if (!schemaFetcherReady) {
197
+ schemaFetcherReady = neo4jSchemaFetcher(resolvedNeo4jUri, neo4jUser, neo4jPassword);
198
+ }
199
+ return schemaFetcherReady;
200
+ }
201
+ const schemaCache = new SchemaCache({
202
+ async labels() {
203
+ const f = await getSchemaFetcher();
204
+ return f.labels();
205
+ },
206
+ async relationshipTypes() {
207
+ const f = await getSchemaFetcher();
208
+ return f.relationshipTypes();
209
+ },
210
+ });
211
+ void schemaCache.start().catch((err) => {
212
+ const msg = err instanceof Error ? err.message : String(err);
213
+ console.error(`[schema-cache] start failed error="${msg.replace(/"/g, "'")}"`);
214
+ });
215
+
182
216
  const uvx = resolveUvxPath();
183
217
  if (!uvx.path) {
184
218
  syncEmit(
@@ -216,12 +250,18 @@ child.stderr.on("data", (chunk: Buffer) => {
216
250
  process.stderr.write(chunk);
217
251
  });
218
252
 
219
- // --- JSON-RPC call correlation ---
220
- // tools/call is the only method we time; initialize and tools/list are noise.
253
+ // --- JSON-RPC call correlation + validation (Task 654) ---
254
+ // tools/call is the only method we time or validate. For read/write cypher
255
+ // calls, the line is validated against the schema cache before forwarding.
256
+ // Write-path rejection: synthesised MCP tool-error response on stdout, NOT
257
+ // forwarded. Read-path rejection: forwarded, with warnings appendix prepended
258
+ // to response.content[0].text.
221
259
  interface PendingCall {
222
260
  method: string;
223
261
  cypherPrefix: string | null;
224
262
  startMs: number;
263
+ validated: boolean;
264
+ readWarnings: UnknownToken[];
225
265
  }
226
266
  const pending = new Map<string | number, PendingCall>();
227
267
 
@@ -235,18 +275,29 @@ function stripNamespace(toolName: string | undefined): string {
235
275
  return toolName.startsWith(prefix) ? toolName.slice(prefix.length) : toolName;
236
276
  }
237
277
 
278
+ const READ_CYPHER_TOOL = "read_neo4j_cypher";
279
+ const WRITE_CYPHER_TOOL = "write_neo4j_cypher";
280
+
238
281
  type JsonRpcMessage = {
282
+ jsonrpc?: string;
239
283
  id?: string | number;
240
284
  method?: string;
241
285
  params?: { name?: string; arguments?: Record<string, unknown> };
242
- result?: { content?: Array<{ text?: string; type?: string }> };
286
+ result?: {
287
+ content?: Array<{ text?: string; type?: string }>;
288
+ isError?: boolean;
289
+ };
243
290
  error?: { message?: string; code?: number };
244
291
  };
245
292
 
246
- function extractCypher(args: Record<string, unknown> | undefined): string | null {
293
+ function extractCypherFull(args: Record<string, unknown> | undefined): string | null {
247
294
  if (!args) return null;
248
295
  const q = args["query"] ?? args["cypher"];
249
- return typeof q === "string" ? truncate(q.replace(/\s+/g, " ").trim(), 80) : null;
296
+ return typeof q === "string" ? q : null;
297
+ }
298
+
299
+ function truncateForLog(cypher: string): string {
300
+ return truncate(cypher.replace(/\s+/g, " ").trim(), 80);
250
301
  }
251
302
 
252
303
  function countRows(result: JsonRpcMessage["result"]): string {
@@ -257,72 +308,221 @@ function countRows(result: JsonRpcMessage["result"]): string {
257
308
  return String(result.content.length);
258
309
  }
259
310
 
260
- function onRequestLine(line: string): void {
311
+ /**
312
+ * Render the admin-facing rejection or warnings text. Plain prose so the
313
+ * agent's Tool Failure Discipline reads it the same way it reads any tool
314
+ * error — no special-case structured JSON parser required.
315
+ */
316
+ function renderUnknownTokens(
317
+ unknown: UnknownToken[],
318
+ mode: "rejected" | "warning",
319
+ ): string {
320
+ const heading = mode === "rejected"
321
+ ? "schema-validation rejected — cypher NOT executed"
322
+ : "schema-validation warning — cypher executed but referenced unknown tokens";
323
+ const lines = unknown.map((u) => {
324
+ const nearest = u.nearest.length > 0 ? ` nearest=[${u.nearest.join(", ")}]` : "";
325
+ return ` - ${u.kind} '${u.token}':${nearest} — ${u.hint}`;
326
+ });
327
+ return `${heading}\n${lines.join("\n")}`;
328
+ }
329
+
330
+ function synthesiseRejection(id: string | number, unknown: UnknownToken[]): string {
331
+ const text = `${renderUnknownTokens(unknown, "rejected")}\n\nFix the token names, or if the schema has just changed, invoke ${namespace}_get_neo4j_schema to refresh your view.`;
332
+ const envelope = {
333
+ jsonrpc: "2.0",
334
+ id,
335
+ result: {
336
+ content: [{ type: "text", text }],
337
+ isError: true,
338
+ },
339
+ };
340
+ return JSON.stringify(envelope);
341
+ }
342
+
343
+ function wrapReadWarnings(msg: JsonRpcMessage, warnings: UnknownToken[]): string {
344
+ const warningText = `${renderUnknownTokens(warnings, "warning")}\n\n--- results below (executed despite unknown tokens) ---\n`;
345
+ const original = msg.result?.content ?? [];
346
+ const wrapped: JsonRpcMessage = {
347
+ ...msg,
348
+ result: {
349
+ ...(msg.result ?? {}),
350
+ content: [{ type: "text", text: warningText }, ...original],
351
+ },
352
+ };
353
+ return JSON.stringify(wrapped);
354
+ }
355
+
356
+ type RequestDecision = "forward" | "intercepted";
357
+
358
+ function handleRequestLine(line: string): RequestDecision {
359
+ let msg: JsonRpcMessage;
261
360
  try {
262
- const msg = JSON.parse(line) as JsonRpcMessage;
263
- if (msg.method === "tools/call" && msg.id !== undefined) {
264
- pending.set(msg.id, {
265
- method: stripNamespace(msg.params?.name),
266
- cypherPrefix: extractCypher(msg.params?.arguments),
267
- startMs: Date.now(),
268
- });
269
- }
361
+ msg = JSON.parse(line) as JsonRpcMessage;
270
362
  } catch {
271
- // Non-JSON / partial line — forward untouched, don't log.
363
+ return "forward";
364
+ }
365
+ if (msg.method !== "tools/call" || msg.id === undefined) return "forward";
366
+
367
+ const methodName = stripNamespace(msg.params?.name);
368
+ const cypherFull = extractCypherFull(msg.params?.arguments);
369
+ const cypherPrefix = cypherFull ? truncateForLog(cypherFull) : null;
370
+ const isCypherCall =
371
+ methodName === READ_CYPHER_TOOL || methodName === WRITE_CYPHER_TOOL;
372
+
373
+ const entry: PendingCall = {
374
+ method: methodName,
375
+ cypherPrefix,
376
+ startMs: Date.now(),
377
+ validated: false,
378
+ readWarnings: [],
379
+ };
380
+
381
+ if (!isCypherCall || !cypherFull) {
382
+ pending.set(msg.id, entry);
383
+ return "forward";
272
384
  }
385
+
386
+ const isWrite = methodName === WRITE_CYPHER_TOOL;
387
+ const snapshot = schemaCache.snapshot();
388
+ const cacheReady = schemaCache.ready();
389
+
390
+ if (!cacheReady) {
391
+ console.error(
392
+ `[cypher-validate] tool=${isWrite ? "write" : "read"} outcome=skipped reason=cache-not-ready cypher="${(cypherPrefix ?? "").replace(/"/g, "'")}"`,
393
+ );
394
+ pending.set(msg.id, entry);
395
+ return "forward";
396
+ }
397
+
398
+ const result = validateCypher(cypherFull, snapshot);
399
+ entry.validated = true;
400
+
401
+ if (result.ok) {
402
+ console.error(
403
+ `[cypher-validate] tool=${isWrite ? "write" : "read"} outcome=accepted labels=${result.labelTokens.length} relationships=${result.edgeTokens.length}`,
404
+ );
405
+ pending.set(msg.id, entry);
406
+ return "forward";
407
+ }
408
+
409
+ void schemaCache.maybeRebuildOnStaleMiss(result.unknown);
410
+ const tokenSummary = result.unknown
411
+ .map((u) => `${u.kind}:${u.token}`)
412
+ .join(",");
413
+
414
+ if (isWrite) {
415
+ console.error(
416
+ `[cypher-validate] tool=write outcome=rejected unknown=${tokenSummary} cypher="${(cypherPrefix ?? "").replace(/"/g, "'")}"`,
417
+ );
418
+ const response = synthesiseRejection(msg.id, result.unknown);
419
+ process.stdout.write(`${response}\n`);
420
+ console.error(
421
+ `[graph-query] op=${methodName} brand=${brand} port=${neo4jPort} cypher="${(cypherPrefix ?? "").replace(/"/g, "'")}" rejected=true validated=true ms=${Date.now() - entry.startMs}`,
422
+ );
423
+ return "intercepted";
424
+ }
425
+
426
+ entry.readWarnings = result.unknown;
427
+ console.error(
428
+ `[cypher-validate] tool=read outcome=warned unknown=${tokenSummary} cypher="${(cypherPrefix ?? "").replace(/"/g, "'")}"`,
429
+ );
430
+ pending.set(msg.id, entry);
431
+ return "forward";
273
432
  }
274
433
 
275
- function onResponseLine(line: string): void {
434
+ function handleResponseLine(line: string): string | null {
435
+ let msg: JsonRpcMessage;
276
436
  try {
277
- const msg = JSON.parse(line) as JsonRpcMessage;
278
- if (msg.id === undefined || !pending.has(msg.id)) return;
279
- const p = pending.get(msg.id)!;
280
- pending.delete(msg.id);
281
- const elapsed = Date.now() - p.startMs;
282
- const cypherField = p.cypherPrefix ? `cypher="${p.cypherPrefix.replace(/"/g, "'")}"` : "";
283
- if (msg.error) {
284
- const errText = (msg.error.message ?? JSON.stringify(msg.error)).replace(/"/g, "'");
285
- console.error(
286
- `[graph-query] op=${p.method} brand=${brand} port=${neo4jPort} ${cypherField} error="${errText}" ms=${elapsed}`,
287
- );
288
- } else {
289
- const rows = countRows(msg.result);
437
+ msg = JSON.parse(line) as JsonRpcMessage;
438
+ } catch {
439
+ return null;
440
+ }
441
+ if (msg.id === undefined || !pending.has(msg.id)) return null;
442
+ const p = pending.get(msg.id)!;
443
+ pending.delete(msg.id);
444
+ const elapsed = Date.now() - p.startMs;
445
+ const cypherField = p.cypherPrefix ? `cypher="${p.cypherPrefix.replace(/"/g, "'")}"` : "";
446
+ const validatedField = `validated=${p.validated}`;
447
+ if (msg.error) {
448
+ const errText = (msg.error.message ?? JSON.stringify(msg.error)).replace(/"/g, "'");
449
+ console.error(
450
+ `[graph-query] op=${p.method} brand=${brand} port=${neo4jPort} ${cypherField} error="${errText}" ${validatedField} ms=${elapsed}`,
451
+ );
452
+ return null;
453
+ }
454
+ const rows = countRows(msg.result);
455
+ const warnedField = p.readWarnings.length > 0 ? ` warned=${p.readWarnings.length}` : "";
456
+ console.error(
457
+ `[graph-query] op=${p.method} brand=${brand} port=${neo4jPort} ${cypherField} rows=${rows} ${validatedField}${warnedField} ms=${elapsed}`,
458
+ );
459
+ if (p.readWarnings.length > 0) {
460
+ try {
461
+ return wrapReadWarnings(msg, p.readWarnings);
462
+ } catch (err) {
463
+ const errMsg = err instanceof Error ? err.message : String(err);
290
464
  console.error(
291
- `[graph-query] op=${p.method} brand=${brand} port=${neo4jPort} ${cypherField} rows=${rows} ms=${elapsed}`,
465
+ `[cypher-validate] warning-wrap failed op=${p.method} error="${errMsg.replace(/"/g, "'")}" forwarding response unwrapped`,
292
466
  );
467
+ return null;
293
468
  }
294
- } catch {
295
- // Non-JSON — forward untouched.
296
469
  }
470
+ return null;
297
471
  }
298
472
 
299
- function makeLineSplitter(onLine: (line: string) => void): (chunk: Buffer) => void {
473
+ /**
474
+ * Per-stream buffering splitter. Yields complete lines (without trailing \n)
475
+ * as they accumulate; leaves any partial tail in the buffer until more bytes
476
+ * arrive. Unlike the pre-Task-652 splitter, this one lets the caller decide
477
+ * per-line whether to forward or intercept — the original splitter was
478
+ * fire-and-forget and the raw chunk was forwarded unconditionally alongside,
479
+ * which ruled out interception.
480
+ */
481
+ function makeLineBuffer(): { push: (chunk: Buffer) => string[] } {
300
482
  const decoder = new StringDecoder("utf8");
301
483
  let buf = "";
302
- return (chunk: Buffer) => {
303
- buf += decoder.write(chunk);
304
- let idx: number;
305
- while ((idx = buf.indexOf("\n")) !== -1) {
306
- const line = buf.slice(0, idx);
307
- buf = buf.slice(idx + 1);
308
- if (line.length > 0) onLine(line);
309
- }
484
+ return {
485
+ push(chunk: Buffer): string[] {
486
+ buf += decoder.write(chunk);
487
+ const out: string[] = [];
488
+ let idx: number;
489
+ while ((idx = buf.indexOf("\n")) !== -1) {
490
+ out.push(buf.slice(0, idx));
491
+ buf = buf.slice(idx + 1);
492
+ }
493
+ return out;
494
+ },
310
495
  };
311
496
  }
312
497
 
313
- const splitRequest = makeLineSplitter(onRequestLine);
498
+ const requestBuffer = makeLineBuffer();
314
499
  process.stdin.on("data", (chunk: Buffer) => {
315
- splitRequest(chunk);
316
- child.stdin.write(chunk);
500
+ for (const line of requestBuffer.push(chunk)) {
501
+ if (line.length === 0) {
502
+ child.stdin.write("\n");
503
+ continue;
504
+ }
505
+ const decision = handleRequestLine(line);
506
+ if (decision === "forward") {
507
+ child.stdin.write(`${line}\n`);
508
+ }
509
+ // "intercepted" — synthesised response already written to stdout.
510
+ }
317
511
  });
318
512
  process.stdin.on("end", () => {
319
513
  child.stdin.end();
320
514
  });
321
515
 
322
- const splitResponse = makeLineSplitter(onResponseLine);
516
+ const responseBuffer = makeLineBuffer();
323
517
  child.stdout.on("data", (chunk: Buffer) => {
324
- splitResponse(chunk);
325
- process.stdout.write(chunk);
518
+ for (const line of responseBuffer.push(chunk)) {
519
+ if (line.length === 0) {
520
+ process.stdout.write("\n");
521
+ continue;
522
+ }
523
+ const rewritten = handleResponseLine(line);
524
+ process.stdout.write(`${rewritten ?? line}\n`);
525
+ }
326
526
  });
327
527
  child.stdout.on("end", () => {
328
528
  process.stdout.end();