@bongos/core 1.19.269 → 1.19.270

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/.bongos-core.json CHANGED
@@ -2,22 +2,22 @@
2
2
  "artifact": "bongos-core",
3
3
  "manifest_schema": 1,
4
4
  "generator": "scripts/gds/package-core.js",
5
- "core_version": "1.19.269",
6
- "core_contract": "1.19.269",
7
- "source_commit": "eb9189a55c35d65f7b127a4f825cf15111450f45",
5
+ "core_version": "1.19.270",
6
+ "core_contract": "1.19.270",
7
+ "source_commit": "b552261cf04dbe95edbfa682bdeb797004935248",
8
8
  "source_ref": "HEAD",
9
- "built_at": "2026-08-20T23:06:25.339Z",
9
+ "built_at": "2026-08-20T23:27:09.964Z",
10
10
  "redaction": {
11
11
  "model": "docs-redacted+functional-verbatim",
12
12
  "docs_redacted": 318,
13
13
  "agent_docs_stubbed": 22,
14
- "functional_verbatim": 1368,
14
+ "functional_verbatim": 1369,
15
15
  "rules": 3,
16
16
  "gate_literals": 3,
17
17
  "gate": "passed"
18
18
  },
19
- "file_count": 1708,
20
- "tree_sha256": "1bb1e8f0972473d957a4d18ab8cb07dc0e7320b9c00e1e504bbd939804fc8b3e",
19
+ "file_count": 1709,
20
+ "tree_sha256": "fd8774b261076703d5db5da8703522555606efc1b3463642d9ded0253791b414",
21
21
  "files": [
22
22
  {
23
23
  "path": ".claude/skills/blocker-review/SKILL.md",
@@ -367,7 +367,7 @@
367
367
  {
368
368
  "path": "clients/bongos-client/index.d.ts",
369
369
  "mode": "0000644",
370
- "sha256": "1c790a9dca384a70cba29ba814cff546cdd69eb784f512fb88e06c336b3f75a9"
370
+ "sha256": "96bbd222f3ccbc2d57bce28e0ac89584252b0e3213bc79f5c8aad54ce10cc845"
371
371
  },
372
372
  {
373
373
  "path": "clients/bongos-client/index.mjs",
@@ -1402,7 +1402,7 @@
1402
1402
  {
1403
1403
  "path": "docs/api/openapi.json",
1404
1404
  "mode": "0000644",
1405
- "sha256": "979d2d19c20a8d4eb4fb7f4715ef307c40d64b99038d0d816702a50bd211fcc9"
1405
+ "sha256": "2f14ac090adc4b7de69f20948172cee0cf002e1224262d5a51ddcada95ad9489"
1406
1406
  },
1407
1407
  {
1408
1408
  "path": "docs/architecture.md",
@@ -3907,7 +3907,7 @@
3907
3907
  {
3908
3908
  "path": "modules/memory/routes/search.js",
3909
3909
  "mode": "0000644",
3910
- "sha256": "3f3e2d2e1d51f17c436668c27faa72916434c706fa14a7daaffbd363d10030dd"
3910
+ "sha256": "a79f61f7906393fcb0bffb67f224c47b7129223e3a6806357de091a9339fa42e"
3911
3911
  },
3912
3912
  {
3913
3913
  "path": "modules/memory/search-chunker.js",
@@ -3917,7 +3917,7 @@
3917
3917
  {
3918
3918
  "path": "modules/memory/search.js",
3919
3919
  "mode": "0000644",
3920
- "sha256": "6ca89effa3792d71982c32c2d3d6b69253a8063a32a84a5f78e3fe18b6186b39"
3920
+ "sha256": "a9b900bcbdbb55b27f15dafe3ee45f57cd0e4dfda0b32cb38b29139b24f4ef68"
3921
3921
  },
3922
3922
  {
3923
3923
  "path": "modules/onboarding/CLAUDE.md",
@@ -4472,12 +4472,12 @@
4472
4472
  {
4473
4473
  "path": "package-lock.json",
4474
4474
  "mode": "0000644",
4475
- "sha256": "ddb6a432b5c2aab632e59179fa2429ac1055aa0eaac7ea30e94c7cba0e4cf8e8"
4475
+ "sha256": "3da63688ee1642c534fe9f8c177822c8de4335ed57c30878366e4a59f6a94a31"
4476
4476
  },
4477
4477
  {
4478
4478
  "path": "package.json",
4479
4479
  "mode": "0000644",
4480
- "sha256": "00d26fc1394b365885ef97c011108dfbe1734aa49ed388a8c49a308480fe3b5a"
4480
+ "sha256": "1b3cda6b70ab6ff9c4034e5a459a3ea8b6bdeb39ad6368e0627d0a35015ad8c2"
4481
4481
  },
4482
4482
  {
4483
4483
  "path": "public-docs/index.html",
@@ -5447,7 +5447,7 @@
5447
5447
  {
5448
5448
  "path": "scripts/gds/recall.js",
5449
5449
  "mode": "0000644",
5450
- "sha256": "564996b820fa09ccb95bcef2e5a1b57f22e5b16c4e54b67dfe1a2cd37ff716a4"
5450
+ "sha256": "191b81d68114cbb920d2aed23398d581d2e1d51f6bc92e243ae7541f454d4b1e"
5451
5451
  },
5452
5452
  {
5453
5453
  "path": "scripts/gds/redteam-patrol.js",
@@ -6122,7 +6122,7 @@
6122
6122
  {
6123
6123
  "path": "src/module-api.js",
6124
6124
  "mode": "0000644",
6125
- "sha256": "f87f3562ac47d91ae9031fbc80d64b2200e1a7ca2c5bfebab13d0bf0f2197b2a"
6125
+ "sha256": "a70173a565935a8a93c133dd94bde27e0000836e6ec9b043499ab0d27b4f29c2"
6126
6126
  },
6127
6127
  {
6128
6128
  "path": "src/module-loader/catalog.js",
@@ -7984,6 +7984,11 @@
7984
7984
  "mode": "0000644",
7985
7985
  "sha256": "dedec6f449696a7fefa2cec1aeb290dc91386c4450e8feaf28a49e9b46a5e7ed"
7986
7986
  },
7987
+ {
7988
+ "path": "tests/recall_corpus_health.mjs",
7989
+ "mode": "0000644",
7990
+ "sha256": "ec1ebb9e317221df08c6009d1abd8816da47eda20740ae928cf9f829b8da4951"
7991
+ },
7987
7992
  {
7988
7993
  "path": "tests/recall_hints.mjs",
7989
7994
  "mode": "0000644",
@@ -278,7 +278,7 @@ export interface PostProvisioningInstancesIdTeardownResponse { ok: boolean; queu
278
278
  export interface PostProvisioningInstancesRequest { slug: string; target_ref?: string; hosting_shape?: string; domain?: string; no_address?: boolean; tier?: string; onboard_mode?: string; type?: string }
279
279
  export interface PostSearchMarkRequest { chunk_id?: StringifiableId; useful?: unknown }
280
280
  export interface PostSearchRequest { expand?: unknown; q?: string; source_kind?: string; limit?: unknown }
281
- export interface PostSearchResponse { expand?: unknown; chunk?: unknown; query?: unknown; count?: unknown; total_full_tokens?: unknown; results?: unknown }
281
+ export interface PostSearchResponse { expand?: unknown; chunk?: unknown; query?: unknown; count?: unknown; total_full_tokens?: unknown; results?: unknown; corpus?: unknown }
282
282
  export interface PostSecurityDocsRequest { slug: string; title: string; content_md: string }
283
283
  export interface PostSecurityDocsResponse { ok: boolean; doc: unknown }
284
284
  export interface PostSecurityReportsIdConfirmRequest { drachmae_awarded?: unknown }
@@ -17187,7 +17187,8 @@
17187
17187
  "query": {},
17188
17188
  "count": {},
17189
17189
  "total_full_tokens": {},
17190
- "results": {}
17190
+ "results": {},
17191
+ "corpus": {}
17191
17192
  }
17192
17193
  },
17193
17194
  "PostSecurityDocsRequest": {
@@ -128,7 +128,25 @@ module.exports = function buildSearchRouter() {
128
128
  // total_full_tokens: the cost of expanding EVERY hit — the headline an
129
129
  // agent reads to decide whether to expand selectively (ADR 0095 C1).
130
130
  const totalFullTokens = results.reduce((sum, r) => sum + (r.full_tokens || 0), 0);
131
- return res.json({ query: q, count: results.length, total_full_tokens: totalFullTokens, results });
131
+ // task 1003097: a zero-result search is ambiguous on the wire — a DEAD
132
+ // INDEX and a genuine miss are the same 200-with-an-empty-array, which is
133
+ // how /recall stayed broken for weeks with nothing reporting a fault. On
134
+ // the zero path only (so a healthy search pays nothing), ask the corpus why
135
+ // and say which of the three it was. Best-effort: a diagnostic must never
136
+ // turn a working search into a 500.
137
+ // corpusHealth absorbs its own failures (enrichment must never break a
138
+ // working search), so no try/catch here. `corpus` stays undefined on a
139
+ // hit, and JSON.stringify drops an undefined key — so a successful search
140
+ // is byte-identical on the wire, and the response literal stays STATIC so
141
+ // gen-api-docs can still infer PostSearchResponse from it.
142
+ let corpus;
143
+ if (results.length === 0) {
144
+ const health = await search.corpusHealth({ builderId: req.builder.id, builderRank: req.builder.rank });
145
+ // ONLY the classifier's output — it returns the SAFE subset. Spreading
146
+ // `health` here would put the unscoped existence probe back on the wire.
147
+ corpus = search.classifyEmptyResult(health);
148
+ }
149
+ return res.json({ query: q, count: results.length, total_full_tokens: totalFullTokens, results, corpus });
132
150
  } catch (err) {
133
151
  console.error('[gds] POST /search', { builderId: req.builder.id, err: err && err.message });
134
152
  return res.fail('search_failed', { status: 500, message: 'search could not be completed' });
@@ -304,8 +304,178 @@ async function markChunkUsefulness(
304
304
  };
305
305
  }
306
306
 
307
+ // task 1003097: WHY A ZERO-RESULT SEARCH IS NOT SELF-EXPLANATORY.
308
+ //
309
+ // /recall returned zero matches for EVERY query on live for weeks and nothing
310
+ // reported a fault, because a dead index and a genuine no-match are the same
311
+ // 200-with-an-empty-array on the wire. That is the most expensive possible wrong
312
+ // answer: it reads as "we have never touched this", so sessions re-derive work
313
+ // that already exists. (The cause was task 1002859 — one NUL byte rolled back
314
+ // the whole ingest, silently, on every deploy.)
315
+ //
316
+ // So when a search finds nothing, ask the corpus WHY and say which of three
317
+ // things happened. One cheap aggregate, run ONLY on the zero-result path, so a
318
+ // healthy search pays nothing:
319
+ //
320
+ // index_empty — there are no chunks at all. The ingest has never
321
+ // succeeded, or the table was wiped and never rebuilt.
322
+ // This is an OUTAGE, not an answer.
323
+ // nothing_visible — chunks exist, but none are visible at this caller's rank
324
+ // or ownership. Scoping, not absence — and worth naming,
325
+ // because the scope join is the other candidate cause a
326
+ // diagnosis has to rule out.
327
+ // no_match — the corpus is populated and visible, and the query
328
+ // genuinely matched nothing. The only one of the three that
329
+ // is a legitimate "no results".
330
+ //
331
+ // NOTE for anyone reading task 1003097's brief: it points at "migration 127
332
+ // search_ingest_runs" as an ingest ledger to consult. No such table exists in
333
+ // this repo (the search schema is migration 103, search_chunks alone), so
334
+ // freshness here is derived from MAX(updated_at) on the chunks themselves.
335
+ // Who may be told about content they cannot read. Metic+ is the boundary this
336
+ // repo already uses for the permission/methodology core (ADR 0043), and
337
+ // distinguishing "the index is empty" from "everything is above your rank"
338
+ // necessarily reveals that unreadable content exists.
339
+ const PRIVILEGED_DIAGNOSTIC_RANKS = ['metic', 'archon'];
340
+
341
+ // task 1003097: why a zero-result search is not self-explanatory.
342
+ //
343
+ // /recall returned zero for EVERY query on live for weeks and nothing reported a
344
+ // fault, because a dead index and a genuine no-match are the same
345
+ // 200-with-an-empty-array. That reads as "we have never touched this", so
346
+ // sessions re-derive work that already exists. (Cause: task 1002859 — one NUL
347
+ // byte rolled back the whole ingest, silently, every deploy.)
348
+ //
349
+ // Two panel findings shaped this into its current form, and both are worth
350
+ // keeping in view:
351
+ //
352
+ // SCOPING (ADR 0016). The first cut returned unfiltered total/doc/db counts and
353
+ // a global last_indexed_at to every caller — an oracle for the size,
354
+ // composition and freshness of content the caller may not read. This route
355
+ // family deliberately avoids that (fetchChunkBody answers one 404 for both
356
+ // "missing" and "not yours"). Now: the caller's own view is scoped with the
357
+ // SAME predicate as the search, and the unscoped probe is only consulted for a
358
+ // Metic+ caller.
359
+ //
360
+ // COST. It also ran a full-table COUNT(*) — and during an actual outage EVERY
361
+ // search is a zero-result search, so that scan would run on every request from
362
+ // every builder for the duration. But nothing here needs a COUNT: the states
363
+ // turn on whether ANY row exists, not how many. EXISTS short-circuits at the
364
+ // first matching row, so this is two index-assisted probes rather than a scan,
365
+ // and it stays flat as the corpus grows. Dropping the exact numbers also
366
+ // removes the last of the oracle.
367
+ async function corpusHealth({ builderId, builderRank } = {}, { pool: p = pool, client = null } = {}) {
368
+ const allowedRanks = ranksAtOrBelow(builderRank);
369
+ const ownerId = Number.isInteger(builderId) ? builderId : (Number(builderId) || null);
370
+ const privileged = PRIVILEGED_DIAGNOSTIC_RANKS.includes(String(builderRank || '').toLowerCase());
371
+ const sql = `
372
+ SELECT EXISTS (
373
+ SELECT 1 FROM search_chunks
374
+ WHERE min_rank = ANY($1::text[])
375
+ AND (owner_builder_id IS NULL OR owner_builder_id = $2::bigint)
376
+ ) AS any_visible,
377
+ -- Unscoped existence. Consulted ONLY for a privileged caller (the
378
+ -- route passes it straight to classifyEmptyResult, which strips it
379
+ -- for everyone else), and cheap either way because EXISTS stops at
380
+ -- the first row.
381
+ EXISTS (SELECT 1 FROM search_chunks) AS any_at_all,
382
+ (SELECT MAX(updated_at) FROM search_chunks
383
+ WHERE min_rank = ANY($1::text[])
384
+ AND (owner_builder_id IS NULL OR owner_builder_id = $2::bigint)
385
+ ) AS last_indexed_at
386
+ `;
387
+ const runner = client || p;
388
+ let rows;
389
+ try {
390
+ ({ rows } = await runner.query(sql, [allowedRanks, ownerId]));
391
+ } catch (_) {
392
+ // ENRICHMENT ONLY — it runs to EXPLAIN a zero result, so it must never turn
393
+ // a working search into an error. Nulls fail closed through
394
+ // classifyEmptyResult, which never reports health it cannot prove.
395
+ return { any_visible: null, any_at_all: null, last_indexed_at: null, privileged };
396
+ }
397
+ const r = rows[0] || {};
398
+ return {
399
+ any_visible: r.any_visible === true || r.any_visible === false ? r.any_visible : null,
400
+ any_at_all: r.any_at_all === true || r.any_at_all === false ? r.any_at_all : null,
401
+ last_indexed_at: r.last_indexed_at || null,
402
+ privileged,
403
+ };
404
+ }
405
+
406
+ // PURE. The state, the sentence to show, and only the facts this caller may
407
+ // have. Separated from the query so the truth table AND the scoping are
408
+ // unit-tested without a DB.
409
+ function classifyEmptyResult(health) {
410
+ const anyVisible = health && typeof health.any_visible === 'boolean' ? health.any_visible : null;
411
+ const privileged = !!(health && health.privileged);
412
+ // Never echo the unscoped probe. last_indexed_at is already scoped to the
413
+ // caller's own view by the query.
414
+ const safe = { last_indexed_at: (health && health.last_indexed_at) || null };
415
+
416
+ if (anyVisible === null) {
417
+ return {
418
+ ...safe,
419
+ state: 'unknown',
420
+ healthy: false,
421
+ message: 'The index could not be inspected, so this zero cannot be interpreted — it may be an '
422
+ + 'empty corpus, a scoping wall, or a real miss. Treat it as UNKNOWN rather than as "nothing '
423
+ + 'exists", and check the server log for the search-corpus probe.',
424
+ };
425
+ }
426
+
427
+ if (anyVisible) {
428
+ return {
429
+ ...safe,
430
+ state: 'no_match',
431
+ healthy: true,
432
+ message: 'No match. The index is populated and visible to you, and the query ran, so this is a '
433
+ + 'genuine miss — try fewer or different terms.',
434
+ };
435
+ }
436
+
437
+ // Nothing visible. WHETHER that is an empty corpus or a scoping wall is a fact
438
+ // about content this caller cannot read, so only Metic+ is told which.
439
+ if (!privileged) {
440
+ return {
441
+ ...safe,
442
+ state: 'nothing_visible_to_you',
443
+ healthy: false,
444
+ message: 'NOTHING IS VISIBLE TO YOU HERE — either the index is empty or everything in it is '
445
+ + 'scoped above your rank. This answer does not distinguish them, so do NOT read it as "the '
446
+ + 'project has never touched this subject". Ask a Metic+ to check whether the index needs '
447
+ + 'rebuilding.',
448
+ };
449
+ }
450
+
451
+ if (health.any_at_all === false) {
452
+ return {
453
+ ...safe,
454
+ state: 'index_empty',
455
+ healthy: false,
456
+ message: 'THE SEARCH INDEX IS EMPTY — this is an outage, not an answer. No chunk has ever been '
457
+ + 'written, or the table was wiped and never rebuilt. Nothing can be found until an ingest '
458
+ + 'succeeds: run `node scripts/gds/search-ingest-md.js` (repo docs) and '
459
+ + '`node scripts/gds/search-ingest-db.js` (DB prose), and check their exit codes — they run '
460
+ + 'best-effort from the deploy and a failure there is easy to miss (task 1002859).',
461
+ };
462
+ }
463
+ return {
464
+ ...safe,
465
+ state: 'nothing_visible',
466
+ healthy: false,
467
+ message: 'The index is NOT empty, but nothing in it is visible to you — every chunk is scoped '
468
+ + 'above your rank or owned by another builder. This is a scoping result, not an empty corpus, '
469
+ + 'so a zero here says nothing about whether the subject is documented.',
470
+ };
471
+ }
472
+
307
473
  module.exports = {
308
474
  searchChunks,
475
+ // task 1003097: a zero-result search must say WHICH of three things happened —
476
+ // a dead index, a scoping wall, or a genuine miss.
477
+ corpusHealth,
478
+ classifyEmptyResult,
309
479
  fetchChunkBody,
310
480
  markChunkUsefulness,
311
481
  ranksAtOrBelow,
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bongos/core",
3
- "version": "1.19.269",
3
+ "version": "1.19.270",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@bongos/core",
9
- "version": "1.19.269",
9
+ "version": "1.19.270",
10
10
  "license": "AGPL-3.0-or-later",
11
11
  "dependencies": {
12
12
  "express": "^4.21.2",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bongos/core",
3
- "version": "1.19.269",
3
+ "version": "1.19.270",
4
4
  "description": "Cloud Bongos — the AI-first build platform core (GDS + platform surfaces + module system), installed as a versioned dependency (ADR 0108).",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "main": "src/platform-server.js",
@@ -73,6 +73,41 @@ async function runExpand(args) {
73
73
  console.log(c.body);
74
74
  }
75
75
 
76
+
77
+ // task 1003097: the zero-result message, as a PURE function of (query, corpus)
78
+ // so the three outcomes are unit-tested instead of eyeballed.
79
+ //
80
+ // The old single line hedged — "or it may not be indexed yet" — which reads
81
+ // identically whether the index is fine or completely dead. /recall returned zero
82
+ // for EVERY query for weeks and this sentence was the only thing a builder saw,
83
+ // so it read as "we have never touched this" and sessions re-derived work that
84
+ // already existed. Returns { stream, lines, exitCode }: an OUTAGE goes to stderr
85
+ // with a non-zero exit, because a scheduled routine scripting /recall has to be
86
+ // able to tell a broken index from an honest miss.
87
+ function renderNoMatch({ q, corpus }) {
88
+ if (!corpus || !corpus.message) {
89
+ // Older server with no corpus diagnostic — previous wording, unchanged.
90
+ return {
91
+ stream: 'out',
92
+ exitCode: 0,
93
+ lines: [`No matches for "${q}". The index covers repo docs + (nightly) DB prose; try fewer / different terms, or it may not be indexed yet.`],
94
+ };
95
+ }
96
+ const stamp = corpus.last_indexed_at ? ` Last indexed: ${corpus.last_indexed_at}.` : '';
97
+ if (corpus.healthy) {
98
+ return { stream: 'out', exitCode: 0, lines: [`No matches for "${q}". ${corpus.message}${stamp}`] };
99
+ }
100
+ return {
101
+ stream: 'err',
102
+ exitCode: 1,
103
+ lines: [
104
+ `\n ⚠ RECALL IS NOT ANSWERING — "${q}" returned nothing, and the reason is not your query.\n`,
105
+ ` ${corpus.message}${stamp}\n`,
106
+ ' Do NOT read this as "the project has never touched this subject".\n',
107
+ ],
108
+ };
109
+ }
110
+
76
111
  async function main() {
77
112
  const args = parseArgs(process.argv.slice(2));
78
113
 
@@ -102,9 +137,12 @@ async function main() {
102
137
  return;
103
138
  }
104
139
 
105
- const { count, results, total_full_tokens } = r.data;
140
+ const { count, results, total_full_tokens, corpus } = r.data;
106
141
  if (!count) {
107
- console.log(`No matches for "${args.q}". The index covers repo docs + (nightly) DB prose; try fewer / different terms, or it may not be indexed yet.`);
142
+ const out = renderNoMatch({ q: args.q, corpus });
143
+ const sink = out.stream === 'err' ? console.error : console.log;
144
+ for (const line of out.lines) sink(line);
145
+ if (out.exitCode) process.exitCode = out.exitCode;
108
146
  return;
109
147
  }
110
148
  const totalNote = Number.isFinite(total_full_tokens)
@@ -132,4 +170,4 @@ if (require.main === module) {
132
170
  main().catch((e) => { console.error('recall: error', e && e.message ? e.message : e); process.exit(1); });
133
171
  }
134
172
 
135
- module.exports = { parseArgs, truncate };
173
+ module.exports = { parseArgs, truncate, renderNoMatch };
package/src/module-api.js CHANGED
@@ -965,8 +965,10 @@ const { validateOrRespond, LIMITS, parseId, asyncHandler, corsPublicGet, parsePa
965
965
  // landed since 1.19.267 with no explicit bump. run 32407127474. (task 1002620)
966
966
  // 1.19.269 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
967
967
  // landed since 1.19.268 with no explicit bump. run 32427178123. (task 1002620)
968
+ // 1.19.270 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
969
+ // landed since 1.19.269 with no explicit bump. run 32428659648. (task 1002620)
968
970
  // ---------------------------------------------------------------------------
969
- const CORE_VERSION = '1.19.269'; // CI auto-patch carrier (ADR 0161); see changelog above
971
+ const CORE_VERSION = '1.19.270'; // CI auto-patch carrier (ADR 0161); see changelog above
970
972
 
971
973
  // A namespaced logger so a module's log lines are attributable + consistent.
972
974
  // Usage: const log = api.logger('dev-box'); log.info('mounted');
@@ -0,0 +1,194 @@
1
+ // tests/recall_corpus_health.mjs — task 1003097.
2
+ //
3
+ // THE OUTAGE THIS PREVENTS RECURRING: /recall returned zero matches for EVERY
4
+ // query on live for weeks, and nothing anywhere reported a fault, because a DEAD
5
+ // INDEX and a genuine no-match are the same 200-with-an-empty-array on the wire.
6
+ // The CLI's one line even hedged toward the wrong reading — "or it may not be
7
+ // indexed yet" — so a total outage looked like "the project has never touched
8
+ // this subject", which is the most expensive possible wrong answer: sessions then
9
+ // re-derive work that already exists. (Cause: task 1002859.)
10
+ //
11
+ // The fix is that a zero result now says WHICH of three things happened. What we
12
+ // prove here (pure — no DB, no network):
13
+ // 1. The truth table: no chunks at all -> index_empty (an outage);
14
+ // chunks exist but none visible -> nothing_visible (scoping, and the OTHER
15
+ // candidate cause a diagnosis must rule out); populated + visible -> the one
16
+ // legitimate no_match.
17
+ // 2. Only no_match is marked healthy — so nothing downstream can mistake an
18
+ // outage for an answer.
19
+ // 3. The CLI renders an outage to STDERR with a NON-ZERO EXIT (a scheduled
20
+ // routine scripting /recall must be able to tell a broken index from an
21
+ // honest miss), a genuine miss to stdout with exit 0, and stays
22
+ // backward-compatible with a server that sends no diagnostic at all.
23
+ // 4. The outage text tells the reader what to run, and explicitly contradicts
24
+ // the wrong inference.
25
+ //
26
+ // Run: node tests/recall_corpus_health.mjs
27
+
28
+ import { strict as assert } from 'node:assert';
29
+ import { createRequire } from 'node:module';
30
+ import fs from 'node:fs';
31
+ import path from 'node:path';
32
+ import { fileURLToPath } from 'node:url';
33
+
34
+ const require = createRequire(import.meta.url);
35
+ const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
36
+ const search = require('../modules/memory/search.js');
37
+ const { renderNoMatch } = require('../scripts/gds/recall.js');
38
+ const { classifyEmptyResult } = search;
39
+
40
+ let passed = 0;
41
+ let failed = 0;
42
+ function test(name, fn) {
43
+ try { fn(); passed++; console.log(` ok ${name}`); }
44
+ catch (err) { failed++; console.log(` FAIL ${name}`); console.log(` ${err.message}`); }
45
+ }
46
+
47
+ // ---- 1/2. the truth table, and the scoping ----
48
+
49
+ test('nothing visible + index HAS content: a sub-Metic is not told which', () => {
50
+ // Panel finding (ADR 0016): distinguishing "empty" from "above your rank" is a
51
+ // fact ABOUT CONTENT THE CALLER CANNOT READ. This route family already answers
52
+ // one 404 for both "missing" and "not yours"; the diagnostic must not become
53
+ // the oracle that surface refuses to be.
54
+ const c = classifyEmptyResult({ any_visible: false, any_at_all: true, privileged: false });
55
+ assert.equal(c.state, 'nothing_visible_to_you');
56
+ assert.equal(c.healthy, false);
57
+ assert.match(c.message, /does not distinguish them/, 'says plainly that it is not telling');
58
+ assert.match(c.message, /Ask a Metic\+/, 'and routes the caller to someone who can find out');
59
+ assert.match(c.message, /do NOT read it as "the project has never touched this subject"/i);
60
+ });
61
+
62
+ test('the SAFE subset never carries the unscoped probe, at any rank', () => {
63
+ for (const privileged of [false, true]) {
64
+ for (const any_at_all of [true, false]) {
65
+ const c = classifyEmptyResult({ any_visible: false, any_at_all, privileged });
66
+ assert.ok(!('any_at_all' in c), 'the unscoped existence probe must never reach the caller');
67
+ assert.ok(!('total_chunks' in c), 'and no global count either');
68
+ assert.ok(!('privileged' in c), 'nor the rank decision itself');
69
+ }
70
+ }
71
+ });
72
+
73
+ test('a Metic+ IS told which — empty index vs scoping wall', () => {
74
+ const empty = classifyEmptyResult({ any_visible: false, any_at_all: false, privileged: true });
75
+ assert.equal(empty.state, 'index_empty');
76
+ assert.equal(empty.healthy, false);
77
+ assert.match(empty.message, /outage, not an answer/);
78
+ assert.match(empty.message, /search-ingest-md\.js/, 'names what to run');
79
+ assert.match(empty.message, /search-ingest-db\.js/);
80
+ assert.match(empty.message, /exit codes/, 'and why the failure was missable');
81
+
82
+ const walled = classifyEmptyResult({ any_visible: false, any_at_all: true, privileged: true });
83
+ assert.equal(walled.state, 'nothing_visible');
84
+ assert.equal(walled.healthy, false);
85
+ assert.match(walled.message, /scoping result, not an empty corpus/);
86
+ assert.doesNotMatch(walled.message, /\d+ chunk/, 'no exact count — that was the oracle');
87
+ });
88
+
89
+ test('anything visible → no_match, the ONLY healthy zero', () => {
90
+ for (const privileged of [false, true]) {
91
+ const c = classifyEmptyResult({ any_visible: true, any_at_all: true, privileged });
92
+ assert.equal(c.state, 'no_match');
93
+ assert.equal(c.healthy, true);
94
+ assert.match(c.message, /genuine miss/);
95
+ }
96
+ });
97
+
98
+ test('a probe that could not be READ is "unknown", never reported as empty', () => {
99
+ // corpusHealth absorbs its own errors, so a failed probe yields nulls.
100
+ // Reporting that as "the index is empty, run the ingest" would be a confident
101
+ // wrong answer — the exact failure mode this task exists to remove.
102
+ for (const input of [undefined, null, {}, { any_visible: null, privileged: true }]) {
103
+ const c = classifyEmptyResult(input);
104
+ assert.equal(c.state, 'unknown', `handled ${JSON.stringify(input)}`);
105
+ assert.equal(c.healthy, false, 'never claim health we cannot prove');
106
+ assert.doesNotMatch(c.message, /search-ingest-md/, 'must NOT prescribe a fix for a cause it has not established');
107
+ }
108
+ });
109
+
110
+ // ---- 3/4. what the builder actually sees ----
111
+
112
+ test('an outage goes to STDERR with a non-zero exit', () => {
113
+ const corpus = classifyEmptyResult({ any_visible: false, any_at_all: false, privileged: true });
114
+ const out = renderNoMatch({ q: 'board room ratification', corpus });
115
+ assert.equal(out.stream, 'err', 'an outage is not normal output');
116
+ assert.equal(out.exitCode, 1, 'a scripted caller must be able to detect it');
117
+ const text = out.lines.join('\n');
118
+ assert.match(text, /RECALL IS NOT ANSWERING/);
119
+ assert.match(text, /the reason is not your query/);
120
+ assert.match(text, /Do NOT read this as "the project has never touched this subject"/,
121
+ 'the wrong inference must be contradicted explicitly — it is the whole cost of this bug');
122
+ });
123
+
124
+ test('a genuine miss stays ordinary stdout with exit 0', () => {
125
+ const corpus = { ...classifyEmptyResult({ any_visible: true, any_at_all: true, privileged: false }), last_indexed_at: '2026-08-20T00:00:00.000Z' };
126
+ const out = renderNoMatch({ q: 'nonexistent gibberish', corpus });
127
+ assert.equal(out.stream, 'out');
128
+ assert.equal(out.exitCode, 0);
129
+ const text = out.lines.join('\n');
130
+ assert.doesNotMatch(text, /RECALL IS NOT ANSWERING/, 'never cry outage on an honest miss');
131
+ assert.match(text, /Last indexed: 2026-08-20/, 'freshness is still surfaced');
132
+ });
133
+
134
+ test('a server that sends no diagnostic keeps the previous behaviour', () => {
135
+ // Backward compatibility matters here in particular: the fix ships before the
136
+ // pin moves, so a new CLI will talk to an OLD server for a while.
137
+ const out = renderNoMatch({ q: 'anything', corpus: undefined });
138
+ assert.equal(out.stream, 'out');
139
+ assert.equal(out.exitCode, 0);
140
+ assert.match(out.lines.join('\n'), /No matches for "anything"/);
141
+ });
142
+
143
+ // ---- wiring ----
144
+
145
+ test('the route asks for the diagnostic ONLY on the zero-result path', () => {
146
+ // A healthy search must not pay for an extra aggregate.
147
+ const src = fs.readFileSync(path.join(ROOT, 'modules/memory/routes/search.js'), 'utf8');
148
+ const idx = src.indexOf('search.corpusHealth');
149
+ assert.ok(idx > 0, 'the route consults corpusHealth');
150
+ const before = src.slice(Math.max(0, idx - 300), idx);
151
+ assert.match(before, /if \(results\.length === 0\)/, 'gated on the zero path');
152
+ });
153
+
154
+ test('the response literal stays STATIC so the OpenAPI schema survives', () => {
155
+ // A conditional spread in res.json() defeated gen-api-docs's literal parser
156
+ // and silently DELETED the whole PostSearchResponse schema from the spec.
157
+ // `corpus` is always a key (undefined on a hit — JSON.stringify drops it), so
158
+ // the wire shape of a successful search is unchanged and the schema holds.
159
+ const src = fs.readFileSync(path.join(ROOT, 'modules/memory/routes/search.js'), 'utf8');
160
+ assert.match(src, /res\.json\(\{ query: q, count: results\.length, total_full_tokens: totalFullTokens, results, corpus \}\)/,
161
+ 'the search response must be a flat, static object literal');
162
+ assert.doesNotMatch(src, /\.\.\.\(corpus \?/, 'no conditional spread in the response literal');
163
+ const spec = JSON.parse(fs.readFileSync(path.join(ROOT, 'docs/api/openapi.json'), 'utf8'));
164
+ const schema = spec.components.schemas.PostSearchResponse;
165
+ assert.ok(schema, 'PostSearchResponse must still exist in the spec');
166
+ assert.ok('corpus' in schema.properties, 'and now document the corpus diagnostic');
167
+ });
168
+
169
+ test('corpusHealth probes EXISTENCE, scoped with the same predicate as the search', () => {
170
+ const src = fs.readFileSync(path.join(ROOT, 'modules/memory/search.js'), 'utf8');
171
+ const fn = src.slice(src.indexOf('async function corpusHealth'));
172
+ const body = fn.slice(0, fn.indexOf('\n}'));
173
+ // EXISTS, not COUNT: during an outage EVERY search is a zero-result search, so
174
+ // a full-table scan here would run on every request from every builder for the
175
+ // duration. EXISTS short-circuits at the first row and stays flat as the
176
+ // corpus grows.
177
+ assert.doesNotMatch(body, /COUNT\(\*\)/, 'no full-table count on the zero path');
178
+ assert.match(body, /EXISTS \(/, 'existence probes instead');
179
+ assert.match(body, /min_rank = ANY/, 'the visible probe is rank-scoped');
180
+ assert.match(body, /owner_builder_id IS NULL OR owner_builder_id/, 'with the search\'s own ownership predicate');
181
+ assert.match(body, /MAX\(updated_at\)/, 'freshness, also scoped');
182
+ });
183
+
184
+ test('the route hands the caller ONLY the classifier output', () => {
185
+ // Spreading `health` into the response would put the unscoped probe back on
186
+ // the wire — the regression this ordering exists to prevent.
187
+ const src = fs.readFileSync(path.join(ROOT, 'modules/memory/routes/search.js'), 'utf8');
188
+ assert.match(src, /corpus = search\.classifyEmptyResult\(health\);/);
189
+ assert.doesNotMatch(src, /\.\.\.health/, 'never spread the raw health object into the response');
190
+ });
191
+
192
+ console.log('');
193
+ console.log(`recall_corpus_health: ${passed} passed, ${failed} failed`);
194
+ process.exit(failed === 0 ? 0 : 1);