@polycode-projects/the-mechanical-code-talker 0.2.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/src/ask.mjs ADDED
@@ -0,0 +1,2403 @@
1
+ // ask.mjs — a mechanical (zero-model-call) natural-language query engine over the
2
+ // tmct graph. PLAN_MECHANICAL_CHAT.md (P0): a small, closed English grammar
3
+ // compiles a free-text question into a graph traversal over the SAME classified
4
+ // relation groups codegraph.mjs's other render functions read, then renders a
5
+ // templated, citation-faithful answer. No embeddings, no generative model calls —
6
+ // a miss is a stated blank, never a guess (the extraction pipeline's "no wrong
7
+ // edge" ethos, held at the query layer too). Term/keyword matching is TIERED
8
+ // (2026-07-02, two-level fuzzy): exact curated match always wins; a Node-only
9
+ // wink-nlp LEMMA/POS tier and a bounded Damerau-Levenshtein FUZZY tier fire only
10
+ // on a miss, a unique fuzzy hit is announced in the answer ("assuming you
11
+ // meant …"), and any tie surfaces as ambiguity — never a silently-broken guess.
12
+ //
13
+ // Four pure, independently-testable stages, orchestrated by ask():
14
+ // parseQuery (grammar) -> resolveObject (mechanical term resolution) ->
15
+ // traverse (graph lookup) -> render (templates).
16
+ //
17
+ // §3.5/3.6 (2026-07-02, ELIZA/PARRY-style breadth): parseQuery normalizes the
18
+ // raw text (contractions, g-drop, filler-strip), rewrites recognized negative-
19
+ // rhetorical constructions to their affirmative form, then runs TWO INDEPENDENT
20
+ // parsing STRATEGIES against the same normalized text — the original anchored-
21
+ // template matcher (precise, fast, unweakened) and a keyword-spotting/
22
+ // decomposition matcher (ELIZA's own mechanism: find the keyword, decompose
23
+ // around it, tolerate reordering/casual phrasing) — and MERGES their results:
24
+ // one strategy hit -> use it; both hit and agree -> use it (high confidence);
25
+ // both hit and DISAGREE -> a genuine parse-level ambiguity, surfaced honestly;
26
+ // neither hits -> the honest grammar miss. STRATEGIES is a plain array so a
27
+ // third strategy could join the same way, not a hardcoded two-branch special
28
+ // case.
29
+ //
30
+ // Where a parsed intent is temporal/churn-shaped (touched/since/cochange as a
31
+ // FILTER over commits, not a structural edge), this engine does NOT re-implement
32
+ // that — see PLAN_MECHANICAL_CHAT.md §2: matchQuery/nlToQuery (temporal.mjs) already
33
+ // own that surface for the Chronograph browser; ask.mjs's own `touches`/`cochange`
34
+ // verbs here answer "which modules touch/co-change with X" as ONE-HOP structural
35
+ // edges (mgx:touchedByCommit / mgx:changeCoupledWith), which is a different (and
36
+ // simpler) question than the browser's time-scrubbing view.
37
+
38
+ import { relationKind, impactClosure } from "./codegraph.mjs";
39
+ import {
40
+ VERB_TO_KIND, ENTITY_TO_TYPE, MODIFIER_TO_KIND,
41
+ CONTRACTIONS, MISSPELLINGS, WRONG_WORDS, G_DROP, FILLER_WORDS,
42
+ CONTEXT_PRONOUNS, NEGATION_FRAMES, COMMIT_CONTENT_FRAMES, META_MEANING_VERBS,
43
+ WHERE_MARKERS, MENTION_MARKERS,
44
+ RELATIVE_PRONOUNS, PLACEHOLDER_NOUNS, BOOLEAN_CONNECTIVES, QUALIFIERS,
45
+ AGGREGATE_TRIGGERS, LIST_TRIGGERS, SUPERLATIVE_EXTREMES, EDGE_NOUN_TO_METRIC, ANAPHORA_TRIGGERS,
46
+ MEMBERSHIP_KINDS, CASCADE_NOISE, CASCADE_SYNONYMS, HELP_TRIGGERS,
47
+ } from "./ask-vocab.mjs";
48
+ import { lookupByProseTokens } from "./prose.mjs";
49
+ // The OPTIONAL Node-only wink-nlp adapter (lemma/POS tier). BOUNDARY: the inlined
50
+ // viewer bundle (viz.mjs askSource) strips this import line and never inlines
51
+ // ask-nlp.mjs, so in the browser `nlpAdapter` is simply an undeclared identifier —
52
+ // defaultNlp() below reads it through `typeof`, the one operator that touches an
53
+ // undeclared name without throwing, and the portable single-file HTML degrades to
54
+ // adapter-less parsing (curated tables + bounded fuzzy still on) instead of
55
+ // shipping a ~1MB language model inside the page.
56
+ import { nlpAdapter } from "./ask-nlp.mjs";
57
+
58
+ /** All edges of a classified relation kind, flattened across relation groups —
59
+ * a local copy of codegraph.mjs's private edgesOfKind (kept local rather than
60
+ * exported+imported to avoid coupling this file's commit boundary to concurrent
61
+ * in-flight edits elsewhere in codegraph.mjs; both read the same relationKind
62
+ * classification, so they cannot drift in meaning). */
63
+ function edgesOfKind(graph, kind) {
64
+ const out = [];
65
+ for (const g of graph.relations) if (relationKind(g) === kind) out.push(...g.edges);
66
+ return out;
67
+ }
68
+
69
+ // ---- §3 vocabulary — single-sourced in ./ask-vocab.mjs; the grammar, the
70
+ // rephrase-hint text, and the renderer's noun forms all derive from those
71
+ // three tables, so they cannot drift. ----
72
+
73
+ // Predicate kinds carrying a finer, symbol-grain sibling (module-coarse -> fn/method-precise).
74
+ // "which functions call X" should read off callsSymbol (fn->fn), not the module-coarse "calls".
75
+ const SYMBOL_GRAIN_SIBLING = { calls: "callsSymbol", touches: "touchesSymbol" };
76
+ const FINE_ENTITY_TYPES = new Set(["Function", "Method", "Class", "Attribute", "GlobalVariable"]);
77
+
78
+ // Query-side UNION families (2026-07-02 query families): a parsed kind that is not
79
+ // itself a stored predicate but a curated union of stored kinds — "what uses X"
80
+ // honestly means the import graph AND the call graph together. Everything else
81
+ // maps to itself; grain selection (asked entity type) then narrows the union's
82
+ // subjects the same way it narrows a single kind's.
83
+ const KIND_UNIONS = { uses: ["imports", "calls", "callsSymbol"] };
84
+ const kindsFor = (kind) => KIND_UNIONS[kind] || [kind];
85
+
86
+ const OVERFLOW_CAP = 12;
87
+
88
+ const PLURAL_FORMS = {
89
+ Function: ["function", "functions"], Method: ["method", "methods"],
90
+ Class: ["class", "classes"], Module: ["module", "modules"],
91
+ Attribute: ["attribute", "attributes"], GlobalVariable: ["variable", "variables"],
92
+ Commit: ["commit", "commits"],
93
+ // "Change" is ask-vocab.mjs's pseudo-type (a wildcard over the touch traversal's
94
+ // results, never a node class) — it still needs noun forms for zero-hit templates.
95
+ Change: ["change", "changes"],
96
+ };
97
+ function nounFor(entityType, n) {
98
+ const [s, p] = PLURAL_FORMS[entityType] || ["result", "results"];
99
+ return n === 1 ? s : p;
100
+ }
101
+
102
+ // Every relation KIND token is already the correct 3rd-person-singular verb form
103
+ // ("X imports Y", "X calls Y", "X touches Y") EXCEPT "cochange", the one kind whose
104
+ // name is a bare noun/verb stem ("X cochange Y" is wrong; "X cochanges Y" is right) —
105
+ // so the reverse-shape zero-hit template below reads off this table instead of
106
+ // unconditionally appending "s" (which used to double-pluralize every other kind:
107
+ // "callss", "importss", "touchess").
108
+ const REVERSE_MISS_VERB = { cochange: "cochanges" };
109
+ function verbFor(kind) {
110
+ return REVERSE_MISS_VERB[kind] || kind;
111
+ }
112
+
113
+ function escapeRegex(s) {
114
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
115
+ }
116
+
117
+ // ---- §3.5 normalization — runs before EITHER parsing strategy sees the text ----
118
+
119
+ /** contraction/informal-spelling table -> word-boundary regex, longest phrase
120
+ * first (so "there's" doesn't get shadowed by a shorter overlapping entry). */
121
+ const tableRe = (table) => new RegExp(
122
+ "\\b(" + Object.keys(table).sort((a, b) => b.length - a.length).map(escapeRegex).join("|") + ")\\b",
123
+ "gi",
124
+ );
125
+ const CONTRACTION_RE = tableRe(CONTRACTIONS);
126
+ // misspelling/wrong-word CORRECTIONS (ask-vocab.mjs) — same mechanism, applied
127
+ // after contractions: restore the intended spelling first, then map misused
128
+ // words to their canonical schema term. Deterministic and curated, so they run
129
+ // BEFORE either parse strategy and ahead of the bounded edit-distance fallback.
130
+ // The trailing lookahead refuses to rewrite a word glued to a dotted extension:
131
+ // WRONG_WORDS entries are real English words that plausibly NAME modules
132
+ // ("revision.mjs", "property.py"), and a correction that corrupts an object
133
+ // term would be a guess — the exact thing these tables exist to avoid.
134
+ const correctionRe = (table) => new RegExp(
135
+ "\\b(" + Object.keys(table).sort((a, b) => b.length - a.length).map(escapeRegex).join("|") + ")\\b(?!\\.[a-z0-9])",
136
+ "gi",
137
+ );
138
+ const MISSPELLING_RE = correctionRe(MISSPELLINGS);
139
+ const WRONG_WORD_RE = correctionRe(WRONG_WORDS);
140
+
141
+ /** Free-text -> normalized free-text: contractions expanded, g-dropped words
142
+ * restored, filler/politeness words stripped. Idempotent and pure — the same
143
+ * input always normalizes the same way, so both parsing strategies see
144
+ * identical text and their outputs are directly comparable. Deliberately
145
+ * does NOT force lowercase: object/subject terms (module names like
146
+ * "myFile", class names like "Base") are meaningfully cased, and every
147
+ * substitution below already matches case-insensitively (`i`/`gi` flags) —
148
+ * forcing the whole string to lowercase would silently corrupt every parsed
149
+ * term's case instead. */
150
+ export function normalizeQuery(text) {
151
+ let q = String(text || "");
152
+ q = q.replace(CONTRACTION_RE, (m) => CONTRACTIONS[m.toLowerCase()]);
153
+ q = q.replace(MISSPELLING_RE, (m) => MISSPELLINGS[m.toLowerCase()]);
154
+ q = q.replace(WRONG_WORD_RE, (m) => WRONG_WORDS[m.toLowerCase()]);
155
+ q = q.replace(G_DROP, "$1ing");
156
+ if (FILLER_WORDS.length) {
157
+ const fillerRe = new RegExp(
158
+ "\\b(" + [...FILLER_WORDS].sort((a, b) => b.length - a.length).map(escapeRegex).join("|") + ")\\b",
159
+ "gi",
160
+ );
161
+ q = q.replace(fillerRe, " ");
162
+ }
163
+ return q.replace(/\s+/g, " ").trim();
164
+ }
165
+
166
+ /** Recognized rhetorical/idiomatic constructions rewritten to the canonical form
167
+ * of the SAME question before either parse strategy sees the text — a small
168
+ * closed pattern set, not a general rewriter. Two families, tried in order:
169
+ * COMMIT_CONTENT_FRAMES first ("what was in commit <sha>" -> "what did <sha>
170
+ * touch"; sha-anchored, so it can't swallow a containment question), then the
171
+ * §3.6 negative-rhetorical NEGATION_FRAMES. First matching frame across both wins
172
+ * and rewriting stops; unmatched text passes through unchanged. */
173
+ export function applyNegationFrames(text) {
174
+ for (const frame of [...COMMIT_CONTENT_FRAMES, ...NEGATION_FRAMES]) {
175
+ const m = text.match(frame.re);
176
+ if (m) return frame.to(m).replace(/\s+/g, " ").trim();
177
+ }
178
+ return text;
179
+ }
180
+
181
+ // ---- strategy 1: anchored templates — fixed precedence order; first fit wins,
182
+ // never ambiguous at the template level (a question matching two shapes is a
183
+ // design smell we test against). Unweakened from the original P0 grammar. ----
184
+
185
+ const VERB_ALT = Object.keys(VERB_TO_KIND).sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
186
+ const ENTITY_ALT = Object.keys(ENTITY_TO_TYPE).sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
187
+ const MODIFIER_ALT = Object.keys(MODIFIER_TO_KIND).sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
188
+ const META_ALT = META_MEANING_VERBS.slice().sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
189
+
190
+ const TEMPLATES = [
191
+ // T1 ASK: "does X import Y" / "is X a subclass of Y" -> Yes/No. Tried FIRST: it starts with
192
+ // does/is/do/did, which the reverse/forward templates below never match (those start with
193
+ // which/what), so precedence between T1 and the rest is structural, not a tie-break guess.
194
+ // "did" joins does/do for the past-tense commit forms ("did commit <sha> touch X").
195
+ {
196
+ name: "ask",
197
+ re: new RegExp(`^(?:does|do|did)\\s+(.+?)\\s+(${VERB_ALT})\\s+(.+?)\\??$`, "i"),
198
+ build: (m) => ({
199
+ shape: "ask", entityType: null, modifier: "direct",
200
+ kind: VERB_TO_KIND[m[2].toLowerCase()], subject: m[1].trim(), object: m[3].trim(),
201
+ }),
202
+ },
203
+ // T2 reverse: "which <entity> [<modifier>] <verb> <object>" — the operator's own example shape.
204
+ {
205
+ name: "reverse",
206
+ re: new RegExp(`^which\\s+(${ENTITY_ALT})\\s+(?:(${MODIFIER_ALT})\\s+)?(${VERB_ALT})\\s+(.+?)\\??$`, "i"),
207
+ build: (m) => ({
208
+ shape: "reverse",
209
+ entityType: ENTITY_TO_TYPE[m[1].toLowerCase()],
210
+ modifier: m[2] ? MODIFIER_TO_KIND[m[2].toLowerCase()] : "direct",
211
+ kind: VERB_TO_KIND[m[3].toLowerCase()],
212
+ object: m[4].trim(),
213
+ }),
214
+ },
215
+ // T3 forward: "what does <object> <verb>" — X is given, list its R-related things.
216
+ // "did" joins does/do for the past-tense commit forms ("what did commit <sha> touch").
217
+ {
218
+ name: "forward",
219
+ re: new RegExp(`^what\\s+(?:does|do|did)\\s+(.+?)\\s+(${VERB_ALT})\\??$`, "i"),
220
+ build: (m) => ({
221
+ shape: "forward", entityType: null, modifier: "direct",
222
+ kind: VERB_TO_KIND[m[2].toLowerCase()], object: m[1].trim(),
223
+ }),
224
+ },
225
+ // T4 meta: "what does <term> mean" — a question about the GRAPH'S OWN VOCABULARY
226
+ // (a SchemaClass/SchemaPredicate label, e.g. "cochange", or a raw prop token, e.g.
227
+ // "mgx:callsSymbol"), not a graph traversal over code edges. Tried after T3: T3 also
228
+ // starts "what does/do", but T3 only fires when the tail is a relation VERB_ALT
229
+ // phrase ("import"/"calls"/…), which "mean"/"means"/etc never are (disjoint tables —
230
+ // ask-vocab.mjs's file comment explains why they're kept separate), so the two never
231
+ // actually compete for the same input.
232
+ {
233
+ name: "meta-mean",
234
+ re: new RegExp(`^what\\s+(?:does|do|is|are)\\s+(.+?)\\s+(?:${META_ALT})\\??$`, "i"),
235
+ build: (m) => ({ shape: "meta", entityType: null, modifier: "direct", kind: "meta", object: m[1].trim() }),
236
+ },
237
+ // T5 meta: "what is a/an <term>" — the OTHER worked phrasing ("what is a Commit").
238
+ // The indefinite article is REQUIRED (not optional): a bare "what is <anything>"
239
+ // would also swallow "what is the meaning of this codebase" (an existing, deliberately
240
+ // honest grammar-miss regression case — ask.test.mjs/ask-dual-strategy.test.mjs both
241
+ // assert it stays null), which never mentions "a"/"an" before its tail. Requiring the
242
+ // article keeps this template's reach to the one worked shape without reopening that.
243
+ {
244
+ name: "meta-whatis",
245
+ re: new RegExp(`^what\\s+(?:is|are)\\s+(?:an?)\\s+(.+?)\\??$`, "i"),
246
+ build: (m) => ({ shape: "meta", entityType: null, modifier: "direct", kind: "meta", object: m[1].trim() }),
247
+ },
248
+ // T6 mention: "where is <term> mentioned/referenced" — the prose/mentions surface
249
+ // (2026-07-02 query families). Tried BEFORE T7: T7's trailing marker is optional,
250
+ // so without this ordering it would swallow the mention question and lose the
251
+ // marker that distinguishes "locate the definition" from "list the prose mentions".
252
+ {
253
+ name: "mention",
254
+ re: new RegExp(`^where\\s+(?:is|are|was|were)\\s+(.+?)\\s+(?:${MENTION_MARKERS.map(escapeRegex).join("|")})\\??$`, "i"),
255
+ build: (m) => ({ shape: "mentions", entityType: null, modifier: "direct", kind: "mentions", object: m[1].trim() }),
256
+ },
257
+ // T7 where: "where is <term> [defined|declared|located|implemented]" — definition
258
+ // location off the site attribute / defining module. "where" starts no other
259
+ // template, so precedence against T1-T5 is structural.
260
+ {
261
+ name: "where",
262
+ re: new RegExp(`^where\\s+(?:is|are|was|were)\\s+(.+?)(?:\\s+(?:${WHERE_MARKERS.map(escapeRegex).join("|")}))?\\??$`, "i"),
263
+ build: (m) => ({ shape: "where", entityType: null, modifier: "direct", kind: "where", object: m[1].trim() }),
264
+ },
265
+ // T8 when: "when did <term> [last] change/touched/updated…" — temporal shape over
266
+ // the touches edges + commit date attributes. The verb slot reuses VERB_ALT, but
267
+ // only the touches family carries dates to answer with, so build() rejects any
268
+ // other kind (returning null falls through — parseAnchored tolerates it) rather
269
+ // than pretending "when did X import Y" has a temporal answer.
270
+ {
271
+ name: "when",
272
+ re: new RegExp(`^when\\s+(?:did|does|do|was|were|is)\\s+(.+?)\\s+(?:last\\s+)?(${VERB_ALT})\\??$`, "i"),
273
+ build: (m) => (VERB_TO_KIND[m[2].toLowerCase()] === "touches"
274
+ ? { shape: "when", entityType: null, modifier: "direct", kind: "touches", object: m[1].trim() }
275
+ : null),
276
+ },
277
+ ];
278
+
279
+ /** Strategy 1: the original P0 anchored grammar — the whole (normalized) string
280
+ * must match one of TEMPLATES start-to-end. A build() may return null to reject
281
+ * a structural match on curated grounds (T8's non-temporal verbs); the scan then
282
+ * simply continues, exactly as if the regex had not matched. Pure. */
283
+ function parseAnchored(text) {
284
+ for (const t of TEMPLATES) {
285
+ const m = text.match(t.re);
286
+ if (m) {
287
+ const parsed = t.build(m);
288
+ if (parsed) return parsed;
289
+ }
290
+ }
291
+ return null;
292
+ }
293
+
294
+ // ---- strategy 2: keyword-spotting/decomposition — ELIZA's own mechanism: find
295
+ // the keyword(s) anywhere in the text, decompose around them, tolerate reordering
296
+ // and casual phrasing. Position-independent (no `^...$` anchor), so it tolerates
297
+ // "what calls this" / "who invokes this" / "something executes this, where from"
298
+ // — real phrasings the anchored grammar's fixed shapes don't cover. ----
299
+
300
+ const STOPWORDS = new Set([
301
+ "what", "who", "which", "where", "when", "why", "how",
302
+ "does", "do", "did", "is", "are", "was", "were", "the", "a", "an", "of", "to", "from", "at", "in", "on",
303
+ "there", "something", "anything", "nothing", "one", "any",
304
+ // temporal filler in when-questions ("when was X last touched") — a symbol
305
+ // literally named "last" would be the accepted residual cost, same trade as
306
+ // every other stopword.
307
+ "last",
308
+ ]);
309
+
310
+ /** Find the longest phrase from `table`'s keys that appears as a contiguous
311
+ * run of `words` (case already lowercased by the caller). Longest-match-first
312
+ * (multi-word phrases before single words) so "co-changes with" isn't
313
+ * shadowed by a shorter unrelated word. A span overlapping `consumed` indices
314
+ * is skipped: the verb and entity tables now share a surface form ("change"
315
+ * is both a touches verb and the Change entity noun), and a word already
316
+ * claimed by the verb pass must not double as the entity keyword. Returns
317
+ * {kind, start, end} (end exclusive) or null. */
318
+ function findPhrase(lcWords, table, consumed = null) {
319
+ const phrases = Object.keys(table).sort((a, b) => b.split(" ").length - a.split(" ").length);
320
+ for (const p of phrases) {
321
+ const pWords = p.split(" ");
322
+ for (let i = 0; i <= lcWords.length - pWords.length; i += 1) {
323
+ if (consumed && pWords.some((_, j) => consumed.has(i + j))) continue;
324
+ if (pWords.every((w, j) => lcWords[i + j] === w)) return { kind: table[p], start: i, end: i + pWords.length };
325
+ }
326
+ }
327
+ return null;
328
+ }
329
+
330
+ // ---- bounded edit distance (two-level fuzzy, 2026-07-02) — hand-rolled
331
+ // Damerau-Levenshtein (optimal string alignment: substitution/insertion/deletion
332
+ // + adjacent transposition), bounded with an early row-minimum exit. Used by the
333
+ // keyword-spot FUZZY tier below and resolveObject's tier 5 — both fire only after
334
+ // every exact/curated tier missed, and a distance TIE is refused (keyword) or
335
+ // surfaced as ambiguity (object), never broken by a guess. Pure JS, no deps, so
336
+ // the inlined viewer bundle gets fuzzy matching for free. ----
337
+
338
+ /** Distance between a and b, or max+1 as soon as it provably exceeds `max`. */
339
+ function editDistance(a, b, max) {
340
+ if (a === b) return 0;
341
+ if (Math.abs(a.length - b.length) > max) return max + 1;
342
+ let prev2 = null;
343
+ let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
344
+ for (let i = 1; i <= a.length; i += 1) {
345
+ const cur = [i];
346
+ let rowMin = i;
347
+ for (let j = 1; j <= b.length; j += 1) {
348
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
349
+ let v = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost);
350
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) v = Math.min(v, prev2[j - 2] + cost);
351
+ cur[j] = v;
352
+ if (v < rowMin) rowMin = v;
353
+ }
354
+ if (rowMin > max) return max + 1;
355
+ prev2 = prev;
356
+ prev = cur;
357
+ }
358
+ return prev[b.length];
359
+ }
360
+
361
+ /** The curated distance budget: 1 edit for short tokens, 2 for longer ones. */
362
+ const fuzzyBound = (s) => (s.length <= 5 ? 1 : 2);
363
+
364
+ /** Every single word appearing in the three parse tables — the "is this word
365
+ * already vocabulary?" gate for the lemma/fuzzy canonicalization passes (an
366
+ * exact vocab word is NEVER rewritten: exact curated match always wins). */
367
+ const VOCAB_WORDS = new Set(
368
+ [...Object.keys(VERB_TO_KIND), ...Object.keys(ENTITY_TO_TYPE), ...Object.keys(MODIFIER_TO_KIND)]
369
+ .flatMap((p) => p.split(" ")),
370
+ );
371
+
372
+ /** Fuzzy-correction TARGETS: verb-phrase and modifier constituents only, length ≥4.
373
+ * Entity nouns are deliberately excluded — real identifiers collide with them at
374
+ * distance ≤2 far too easily ("myfile" is 2 edits from "file", "caller" 2 from
375
+ * "calls"-family words), and entity-noun typos are already owned by the curated
376
+ * MISSPELLINGS table where such calls are made deliberately. Short constituents
377
+ * ("of", "to", "in", "on") are excluded for the same reason: at bound 1 half of
378
+ * English is adjacent to them. */
379
+ const FUZZY_TARGET_WORDS = [...new Set(
380
+ [...Object.keys(VERB_TO_KIND), ...Object.keys(MODIFIER_TO_KIND)]
381
+ .flatMap((p) => p.split(" "))
382
+ .filter((w) => w.length >= 4),
383
+ )];
384
+
385
+ /** A query word may be canonicalized only if it is plain alphabetic, not a
386
+ * stopword, and not already vocabulary. Dotted/digit terms (file names, shas)
387
+ * are never touched. */
388
+ function eligibleForCanon(w) {
389
+ return /^[a-z]+$/.test(w) && !STOPWORDS.has(w) && !VOCAB_WORDS.has(w);
390
+ }
391
+
392
+ /** UNIQUE within-bound fuzzy vocab keyword for `w`, or null — a tie between two
393
+ * distinct target words at the same distance is refused outright (the honest-miss
394
+ * discipline at the vocabulary level; cf. MISSPELLINGS' curated "calss" decision). */
395
+ function fuzzyVocabWord(w) {
396
+ const bound = fuzzyBound(w);
397
+ let best = bound + 1;
398
+ let hit = null;
399
+ let tied = false;
400
+ for (const target of FUZZY_TARGET_WORDS) {
401
+ const d = editDistance(w, target, Math.min(best, bound));
402
+ if (d < best) { best = d; hit = target; tied = false; }
403
+ else if (d === best && d <= bound && target !== hit) tied = true;
404
+ }
405
+ return best <= bound && !tied ? hit : null;
406
+ }
407
+
408
+ /** Strategy 2: scan (already-normalized) text for a verb keyword anywhere,
409
+ * plus optional entity/modifier keywords anywhere, then split whatever's
410
+ * left (after removing the matched spans + stopwords) into the words BEFORE
411
+ * and AFTER the verb. Which side(s) are non-empty decides the shape —
412
+ * mirrors the three anchored shapes but by decomposition instead of a fixed
413
+ * template, so it tolerates reordering/casual phrasing the anchored regexes
414
+ * don't: text on BOTH sides ("does X import Y") -> ask{subject:before,
415
+ * object:after}; only AFTER the verb ("what calls this") -> reverse{object:
416
+ * after}; only BEFORE it ("what does X import") -> forward{object:before}.
417
+ * A lone context pronoun ("this"/"it"/"that"/"here") ending up as a resolved
418
+ * term is left as plain text — resolveTermOrContext (traverse-time)
419
+ * recognizes it against an optional contextId, so no separate flag is
420
+ * needed here. A misparse here costs nothing beyond an honest object-miss
421
+ * downstream (resolveObject never guesses).
422
+ *
423
+ * Keyword matching is TIERED (two-level fuzzy work, 2026-07-02) — each lower
424
+ * tier fires ONLY when every tier above found no verb phrase at all, so an
425
+ * exact curated match can never be displaced:
426
+ * 1. exact — the words as typed (post-normalization, which already applied
427
+ * the curated CONTRACTIONS/MISSPELLINGS/WRONG_WORDS corrections);
428
+ * 2. lemma (only with the optional Node-side `nlp` adapter) — each eligible
429
+ * word is replaced by its wink lemma IF that lemma is itself a vocab word
430
+ * ("imported"/"importing" -> "import"), so inflections hit the curated
431
+ * phrases without enumerating them. Every verb family already stores its
432
+ * lemma form ("import", "call", "touch", "use", …), so a direct
433
+ * lemma-in-vocab check is the whole lookup — no reverse index needed;
434
+ * 3. fuzzy (adapter-free; works in the inlined viewer too) — a word ≥4 chars
435
+ * matching nothing exactly may rewrite to a UNIQUE verb/modifier
436
+ * constituent within the bounded edit distance (see fuzzyVocabWord; ties
437
+ * are refused, entity nouns are never fuzzy targets).
438
+ * The canonicalized words drive PHRASE FINDING only — sideText always reads the
439
+ * ORIGINAL words, so a correction can never corrupt an object/subject term. */
440
+ function parseKeywordSpot(text, nlp = null) {
441
+ // Strip a trailing "?" (mirrors the anchored templates' own `\??$`) and turn commas into
442
+ // pauses/spaces — but NEVER strip a mid-word ".": object terms are routinely dotted file/module
443
+ // names ("a.py", "utils.mjs"), and the anchored strategy captures those raw, so keyword-spot
444
+ // must too or the two strategies would "disagree" over a period that was never part of the intent.
445
+ const words = text.replace(/\?+\s*$/, "").replace(/,/g, " ").split(/\s+/).filter(Boolean);
446
+ const lcWords = words.map((w) => w.toLowerCase());
447
+ // where/mentions shapes (2026-07-02 query families): "where is X [defined]" and
448
+ // "where is X mentioned" carry NO relation verb, so the verb-driven decomposition
449
+ // below can never reach them. Routed here by the "where" question word + marker —
450
+ // but ONLY when no relation verb exists anywhere in the sentence: "something
451
+ // executes this, where from" (an existing worked phrasing) has a verb, and its
452
+ // "where" is decorative, not a location question.
453
+ if (lcWords.includes("where") && !findPhrase(lcWords, VERB_TO_KIND)) {
454
+ const mention = lcWords.some((w) => MENTION_MARKERS.includes(w));
455
+ const markers = new Set([...WHERE_MARKERS, ...MENTION_MARKERS]);
456
+ const objText = words.filter((w, i) => !STOPWORDS.has(lcWords[i]) && !markers.has(lcWords[i])).join(" ").trim();
457
+ if (objText) {
458
+ const kind = mention ? "mentions" : "where";
459
+ return { shape: kind, entityType: null, modifier: "direct", kind, object: objText };
460
+ }
461
+ }
462
+ let canonWords = lcWords;
463
+ let verbHit = findPhrase(lcWords, VERB_TO_KIND);
464
+ if (!verbHit && nlp) {
465
+ // tier 2: lemma (see the tier doc above) — replace only when the lemma is
466
+ // itself vocabulary, so unknown words ("myfile") pass through untouched.
467
+ const lemmaWords = lcWords.map((w) => {
468
+ if (!eligibleForCanon(w)) return w;
469
+ const l = nlp.lemma(w);
470
+ return VOCAB_WORDS.has(l) ? l : w;
471
+ });
472
+ verbHit = findPhrase(lemmaWords, VERB_TO_KIND);
473
+ if (verbHit) canonWords = lemmaWords;
474
+ }
475
+ if (!verbHit) {
476
+ // tier 3: bounded-edit-distance rewrite toward verb/modifier keywords only
477
+ // ("impotr" -> "import"); ≥4-char words only — below that the bound covers
478
+ // half of English (and "and" is 1 edit from the "land in" constituent).
479
+ const fuzzyWords = lcWords.map((w) => (w.length >= 4 && eligibleForCanon(w) ? fuzzyVocabWord(w) || w : w));
480
+ verbHit = findPhrase(fuzzyWords, VERB_TO_KIND);
481
+ if (verbHit) canonWords = fuzzyWords;
482
+ }
483
+ if (!verbHit) return null;
484
+ // POS consumer (wink adapter, Node-side only): rescue the ONE decomposition this
485
+ // strategy provably mis-parses — a relation word used as a NOUN in a "the
486
+ // <imports> of <term>" nominal ("show the imports of walk.mjs" otherwise
487
+ // decomposes to ask{subject:"show"}; bare "the imports of walk.mjs" to the
488
+ // reverse shape, both wrong). The wink probe showed "import" is tagged NOUN even
489
+ // in genuine verb use ("which modules import walk.mjs"), so the POS signal is
490
+ // deliberately NOT a general verb veto — it only fires inside this exact
491
+ // det+NOUN+"of" frame, where the nominal reading is grammatically forced.
492
+ if (nlp && verbHit.end - verbHit.start === 1) {
493
+ const i = verbHit.start;
494
+ const det = lcWords[i - 1];
495
+ if ((det === "the" || det === "these" || det === "those") && lcWords[i + 1] === "of") {
496
+ const tags = nlp.posTags(words);
497
+ if (tags[i] === "NOUN") {
498
+ const objText = words.slice(i + 2).filter((w, j) => !STOPWORDS.has(lcWords[i + 2 + j])).join(" ").trim();
499
+ if (objText) return { shape: "forward", entityType: null, modifier: "direct", kind: verbHit.kind, object: objText };
500
+ }
501
+ }
502
+ }
503
+ const consumed = new Set();
504
+ const mark = (hit) => { if (hit) for (let i = hit.start; i < hit.end; i += 1) consumed.add(i); };
505
+ mark(verbHit);
506
+ const entityHit = findPhrase(canonWords, ENTITY_TO_TYPE, consumed);
507
+ mark(entityHit);
508
+ const modifierHit = findPhrase(canonWords, MODIFIER_TO_KIND, consumed);
509
+ mark(modifierHit);
510
+ const sideText = (from, to) => words
511
+ .slice(from, to)
512
+ .filter((_, j) => !consumed.has(from + j) && !STOPWORDS.has(lcWords[from + j]))
513
+ .join(" ")
514
+ .trim();
515
+ const beforeText = sideText(0, verbHit.start);
516
+ const afterText = sideText(verbHit.end, words.length);
517
+ const kind = verbHit.kind;
518
+ // slices read canonWords, not lcWords: the entity/modifier spans were matched
519
+ // against the canonicalized array, whose word IS the table key.
520
+ const entityType = entityHit ? ENTITY_TO_TYPE[canonWords.slice(entityHit.start, entityHit.end).join(" ")] : null;
521
+ const modifier = modifierHit ? MODIFIER_TO_KIND[canonWords.slice(modifierHit.start, modifierHit.end).join(" ")] : "direct";
522
+
523
+ // when shape (2026-07-02 query families): "when did X change" / "when was X last
524
+ // touched" — the "when" question word turns a touches decomposition temporal.
525
+ // Only touches carries commit dates to answer with; a "when" next to any other
526
+ // relation verb falls through to the ordinary shapes (and their honest answers).
527
+ if (kind === "touches" && lcWords.includes("when")) {
528
+ const objText = beforeText || afterText;
529
+ if (objText) return { shape: "when", entityType: null, modifier: "direct", kind: "touches", object: objText };
530
+ }
531
+
532
+ if (beforeText && afterText) return { shape: "ask", entityType: null, modifier: "direct", kind, subject: beforeText, object: afterText };
533
+ if (afterText) return { shape: "reverse", entityType, modifier, kind, object: afterText };
534
+ // forward keeps the spotted entityType ("which modules did commit <sha> touch" is a
535
+ // forward decomposition — subject before the verb — whose asked grain would otherwise
536
+ // be lost); traverse() only consults it for the commit-as-subject grain selection,
537
+ // so plain forwards behave exactly as before. Modifier stays hardcoded: no forward
538
+ // closure traversal exists (see modifierIsWired).
539
+ if (beforeText) return { shape: "forward", entityType, modifier: "direct", kind, object: beforeText };
540
+ return null;
541
+ }
542
+
543
+ // ---- strategy merge — run both, agree/disagree/single/neither (§ above). A
544
+ // plain array + a merge step, so a third strategy plugs in the same way. ----
545
+
546
+ const STRATEGIES = [
547
+ { name: "anchored", parse: parseAnchored },
548
+ { name: "keyword-spot", parse: parseKeywordSpot },
549
+ ];
550
+
551
+ /** The default lemma/POS adapter: wink-nlp when this is a Node process with the
552
+ * optional deps installed, null otherwise. BOUNDARY (see the import comment):
553
+ * the inlined viewer bundle strips the ask-nlp.mjs import, so `nlpAdapter` is
554
+ * an UNDECLARED identifier there — `typeof` reads it without throwing and the
555
+ * browser path degrades to no adapter, same parse pipeline otherwise. */
556
+ function defaultNlp() {
557
+ return typeof nlpAdapter === "function" ? nlpAdapter() : null;
558
+ }
559
+
560
+ // "commit abc1234" and bare "abc1234" are the SAME term once resolveObject's
561
+ // commit-sha tier strips the noun — the anchored strategy captures the noun inside
562
+ // its object span while keyword-spot consumes it as the entity keyword, so without
563
+ // this the two strategies would "disagree" over a word that names no different thing.
564
+ const cmpTerm = (s) => String(s || "").trim().toLowerCase().replace(/\s+/g, " ").replace(/^commit\s+(?=[0-9a-f]{7,40}$)/, "");
565
+
566
+ /** Do two independently-produced parses mean the same graph query? Same
567
+ * shape, same relation kind, and matching term(s) (both subject and object
568
+ * for "ask"; just object otherwise) — anything less is a genuine
569
+ * disagreement, not a near-miss to paper over. */
570
+ function sameParse(p, q) {
571
+ if (p.shape !== q.shape || p.kind !== q.kind) return false;
572
+ if (p.shape === "ask") return cmpTerm(p.subject) === cmpTerm(q.subject) && cmpTerm(p.object) === cmpTerm(q.object);
573
+ return cmpTerm(p.object) === cmpTerm(q.object);
574
+ }
575
+
576
+ /** Compile a free-text question into {shape, kind, entityType, modifier,
577
+ * object[, subject]}, or null if NEITHER strategy fits — an honest grammar
578
+ * miss (§6.3), never a best-effort guess. When both strategies parse and
579
+ * AGREE, returns that parse unchanged (no fallback ordering — either
580
+ * strategy's own result is equally valid once they agree, per §above: "use
581
+ * either"). When both parse but DISAGREE (different shape/kind/term),
582
+ * returns {ambiguousParse: true, candidates: [...]} — a genuine "this could
583
+ * mean more than one thing" case, distinct from resolveObject's later
584
+ * object-resolution ambiguity. `opts.nlp` overrides the lemma/POS adapter
585
+ * (pass null to force the adapter-less browser behavior in a Node test);
586
+ * leaving it undefined picks the deterministic default (defaultNlp). Pure
587
+ * given (query, adapter) — the adapter itself is a fixed model, no sampling. */
588
+ export function parseQuery(query, { nlp = undefined } = {}) {
589
+ const adapter = nlp === undefined ? defaultNlp() : nlp;
590
+ const raw = String(query || "").trim().replace(/\s+/g, " ");
591
+ if (!raw) return null;
592
+ const text = applyNegationFrames(normalizeQuery(raw));
593
+ if (!text) return null;
594
+ // COMPOSITIONAL PARSE PATH (PLAN §5.16 P3) — the new PRIMARY layer: a recursive
595
+ // descent over CLAUSES for the compositional shapes (nested/relative, boolean,
596
+ // qualifiers, aggregates, superlatives, anaphora). It fires ONLY when a
597
+ // compositional MARKER is present and returns null otherwise, so every plain
598
+ // clause falls straight through to the unchanged two-strategy merge below — the
599
+ // whole existing grammar is preserved bit-for-bit. When a marker IS present but
600
+ // the phrase cannot be compiled, it returns an honest {node:"miss"} rather than
601
+ // letting keyword-spot guess at a composition it never expressed.
602
+ const composite = parseComposite(text, adapter);
603
+ if (composite) return composite;
604
+ const hits = STRATEGIES.map((s) => ({ name: s.name, parsed: s.parse(text, adapter) })).filter((r) => r.parsed);
605
+ if (hits.length === 0) return null;
606
+ if (hits.length === 1) return hits[0].parsed;
607
+ const [a, b] = hits;
608
+ if (sameParse(a.parsed, b.parsed)) return a.parsed;
609
+ return { ambiguousParse: true, candidates: hits.map((h) => h.parsed) };
610
+ }
611
+
612
+ // ============================================================================
613
+ // §compositional grammar (PLAN §5.16 P3) — the step up from ELIZA keyword-
614
+ // spotting to a real recursive-descent grammar. Tokenize -> recursive-descent
615
+ // parse to an AST of nodes -> compile to graph traversal. The AST node shapes
616
+ // (all carry a `node` tag so traverse()/render() can branch without touching the
617
+ // simple-clause path):
618
+ // {node:"clause", clause} — a wrapped simple parse (the leaf)
619
+ // {node:"allOfClass", entityType} — every individual of a class
620
+ // {node:"reverseSet"|"forwardSet", kind, entityType, inner} — nested/relative:
621
+ // the OBJECT (reverse) / SUBJECT (forward) of the outer edge is the id-set
622
+ // produced by evaluating `inner` (another AST) — two-stage traversal.
623
+ // {node:"membership", entityType, term} — "<entity> of/in <term>"
624
+ // {node:"qualifier", filters:[word…], inner} — adjective post-filters on a set
625
+ // {node:"boolean", entityType, atoms:[{op,kind,ast|filters}…]} — set algebra
626
+ // over the SAME subject (and/or/but-not); op ∈ seed/intersection/union/difference
627
+ // {node:"count", entityType, base} — aggregate: |eval(base)|
628
+ // {node:"list", entityType, base, scoped} — list the individuals of eval(base),
629
+ // capped at OVERFLOW_CAP; `scoped` suppresses the "narrow with …" hint when the
630
+ // list was already restricted (a module scope or predicate tail)
631
+ // {node:"superlative", entityType, metric, metricNoun, extreme} — rank by degree
632
+ // {node:"anaphora", mode, filter} — over ask()'s `prev` id array
633
+ // {node:"miss", reason} — a compositional marker was seen
634
+ // but could not compile: an honest stated miss, never a guess.
635
+ // The grammar COMPOSES the closed vocabulary (ask-vocab.mjs); it never opens it —
636
+ // every leaf still resolves through the existing curated clause parser + tiered
637
+ // resolveObject, so a term it can't resolve is still an honest object-miss.
638
+ // ============================================================================
639
+
640
+ // Depth cap on nesting (PLAN P3: "depth ≥2 nesting; guard against runaway with a
641
+ // sane hop cap and an honest 'too deep to resolve' if exceeded").
642
+ const MAX_COMPOSE_DEPTH = 4;
643
+ // A resolvable-later placeholder object term for the OUTER clause of a nested
644
+ // parse: the outer clause is parsed normally (so its verb/shape/grain classify),
645
+ // then its `object` is discarded and replaced at eval time by the inner set. Chosen
646
+ // to be plainly alphabetic (not a stopword, not vocabulary) so the clause parser
647
+ // treats it as an ordinary object term rather than dropping it.
648
+ const NEST_SENTINEL = "zzinnerset";
649
+ // Filler words dropped at the front of a relative predicate / anaphora filter.
650
+ const PRED_LEAD_SKIP = new Set(["that", "which", "who", "are", "is", "was", "were", "do", "does", "also", "still", "both", "and"]);
651
+ const FRAME_WORDS = new Set(["which", "what", "who", "list", "show", "find", "give", "me", "us", "all"]);
652
+
653
+ const splitWords = (text) => String(text).replace(/\?+\s*$/, "").replace(/,/g, " ").split(/\s+/).filter(Boolean);
654
+ const entityNoun = (w) => (ENTITY_TO_TYPE[w] ? { entityType: ENTITY_TO_TYPE[w], placeholder: false }
655
+ : (PLACEHOLDER_NOUNS.includes(w) ? { entityType: null, placeholder: true } : null));
656
+ const isGerundVerb = (w) => !!VERB_TO_KIND[w] && w.endsWith("ing");
657
+
658
+ /** Run the two existing strategies on a FRAGMENT and return a single simple clause
659
+ * (or null). Deterministic tie-break: on strategy disagreement the anchored parse
660
+ * wins (STRATEGIES[0]) — a fragment fed from the composer is already shape-
661
+ * constrained, so the merge's "surface an ambiguity" behavior isn't wanted here. */
662
+ function parseSimpleClause(text, nlp) {
663
+ const hits = STRATEGIES.map((s) => s.parse(text, nlp)).filter(Boolean);
664
+ if (!hits.length) return null;
665
+ if (hits.length === 1) return hits[0];
666
+ return sameParse(hits[0], hits[1]) ? hits[0] : hits[0];
667
+ }
668
+
669
+ /** Top compositional dispatcher — first marker-matching production wins; a
670
+ * production returns null (not this shape → fall through) or an AST node (which
671
+ * may itself be {node:"miss"} when the marker was present but uncompilable). */
672
+ function parseComposite(text, nlp) {
673
+ const w = splitWords(text);
674
+ const lc = w.map((x) => x.toLowerCase());
675
+ return parseAnaphora(w, lc, nlp)
676
+ || parseAggregate(w, lc, nlp)
677
+ || parseSuperlative(w, lc, nlp)
678
+ || parseList(w, lc, nlp, 0)
679
+ || parseNested(w, lc, nlp, 0)
680
+ || parseRelationalOrQualified(w, lc, nlp, 0);
681
+ }
682
+
683
+ /** A set-producing sub-expression (used for nested inner clauses, boolean branches,
684
+ * and count restrictors): nested first, then the relational/qualifier/boolean
685
+ * parser, then a bare simple clause. Carries `depth` for the nesting cap. */
686
+ function parseSetPhrase(text, nlp, depth) {
687
+ if (depth > MAX_COMPOSE_DEPTH) return { node: "miss", reason: "too deep to resolve" };
688
+ const w = splitWords(text);
689
+ const lc = w.map((x) => x.toLowerCase());
690
+ const nested = parseNested(w, lc, nlp, depth);
691
+ if (nested) return nested;
692
+ const rel = parseRelationalOrQualified(w, lc, nlp, depth);
693
+ if (rel) return rel;
694
+ const clause = parseSimpleClause(text, nlp);
695
+ if (clause) return { node: "clause", clause };
696
+ return null;
697
+ }
698
+
699
+ /** NESTED / RELATIVE (object-position relative clause): "<outer verb> <placeholder
700
+ * |entity> that <inner>" — the noun before "that" is the OBJECT of the outer edge,
701
+ * constrained by the inner clause. Distinguished from a subject-relative ("functions
702
+ * that call X", handled by parseRelationalOrQualified) by requiring a VERB before the
703
+ * relative noun (i.e. the noun is not the leading subject). Returns a reverse/forward
704
+ * Set node, an honest miss (marker present, uncompilable), or null (no object-relative
705
+ * marker → let another production try). */
706
+ function parseNested(w, lc, nlp, depth) {
707
+ for (let r = 1; r < lc.length; r += 1) {
708
+ if (!RELATIVE_PRONOUNS.includes(lc[r])) continue;
709
+ if (r + 1 >= lc.length) continue; // nothing after "that"
710
+ const noun = entityNoun(lc[r - 1]);
711
+ if (!noun) continue; // "that" not preceded by a noun
712
+ const head = w.slice(0, r - 1); // outer clause words, minus the placeholder noun
713
+ if (!head.length) continue; // noun is the leading subject → subject-relative, not this shape
714
+ const outer = parseSimpleClause([...head, NEST_SENTINEL].join(" "), nlp);
715
+ if (!outer || (outer.shape !== "reverse" && outer.shape !== "forward")) continue;
716
+ if (outer.modifier && outer.modifier !== "direct") continue; // no transitive-over-set closure primitive
717
+ // build the inner sub-query: "which <placeholder-noun> <inner-text>" — recurses,
718
+ // so the inner may itself be nested/boolean/qualified (depth ≥2).
719
+ const innerText = `which ${lc[r - 1]} ${w.slice(r + 1).join(" ")}`;
720
+ const inner = parseSetPhrase(innerText, nlp, depth + 1);
721
+ if (!inner || inner.node === "miss") return inner ? { node: "miss", reason: inner.reason || "inner clause didn't parse" } : { node: "miss", reason: "inner clause didn't parse" };
722
+ return { node: outer.shape === "reverse" ? "reverseSet" : "forwardSet", kind: outer.kind, entityType: outer.entityType, inner };
723
+ }
724
+ return null;
725
+ }
726
+
727
+ /** ANAPHORA over the previous result set: "which of those/them <filter>", "how many
728
+ * of those <filter>". Requires "of <pronoun>" (so a bare "those" in a term never
729
+ * fires). Returns a {node:"anaphora"} (mode count|list), a miss (filter present but
730
+ * uncompilable), or null. */
731
+ function parseAnaphora(w, lc, nlp) {
732
+ let p = -1;
733
+ for (let i = 1; i < lc.length; i += 1) {
734
+ if (ANAPHORA_TRIGGERS.includes(lc[i]) && lc[i - 1] === "of") { p = i; break; }
735
+ }
736
+ if (p < 0) return null;
737
+ const head = lc.slice(0, p - 1).join(" ");
738
+ const mode = /^(how many|how much|count)\b/.test(head) ? "count" : "list";
739
+ const filter = parsePredicateFilter(w.slice(p + 1), nlp);
740
+ if (filter === undefined) return { node: "miss", reason: "the follow-up filter didn't parse" };
741
+ return { node: "anaphora", mode, filter };
742
+ }
743
+
744
+ /** Parse a trailing filter (for anaphora, and any "of those that …" tail) into
745
+ * {type:"all"} | {type:"qual", filters} | {type:"clause", clause}. Returns
746
+ * undefined when a non-empty filter cannot be compiled (an honest miss upstream). */
747
+ function parsePredicateFilter(words, nlp) {
748
+ let i = 0;
749
+ const lc = words.map((x) => x.toLowerCase());
750
+ while (i < lc.length && PRED_LEAD_SKIP.has(lc[i])) i += 1;
751
+ const rest = words.slice(i);
752
+ const restLc = lc.slice(i);
753
+ if (!rest.length) return { type: "all" };
754
+ if (restLc.every((x) => QUALIFIERS[x])) return { type: "qual", filters: restLc };
755
+ const clause = parseSimpleClause(`what ${rest.join(" ")}`, nlp);
756
+ if (clause && (clause.shape === "reverse" || clause.shape === "forward") && clause.object) {
757
+ return { type: "clause", clause };
758
+ }
759
+ return undefined;
760
+ }
761
+
762
+ /** Trailing "and that's the whole question" filler an aggregate/list tail can carry
763
+ * ("how many classes are there", "list functions in total", "which classes exist in
764
+ * the index") — a count/list over a bare kind is frequently phrased with such a tail,
765
+ * and it must NOT be mistaken for a restrictor (that's the exact bug behind "how many
766
+ * classes are there" → the count-restrictor miss). Combined with STOPWORDS (which
767
+ * already carries are/there/is/in/the/…) at the call site, so only the non-stopword
768
+ * extras live here. A tail with ANY word outside this ∪ STOPWORDS is a real restrictor. */
769
+ const AGG_TAIL_FILLER = new Set([
770
+ "total", "altogether", "overall", "exist", "exists", "existing", "present",
771
+ "here", "now", "currently", "graph", "index", "codebase", "repo", "repository",
772
+ ]);
773
+
774
+ /** AGGREGATE / COUNT: "how many <entity> [<restrictor>]", "count <entity>",
775
+ * "number of <entity> that …". A bare "how many classes" counts the class of
776
+ * individuals; a restrictor tail counts a clause's result set; a purely-filler tail
777
+ * ("… are there", "… in total") is treated as no restrictor (a bare count). */
778
+ function parseAggregate(w, lc, nlp) {
779
+ const trig = AGGREGATE_TRIGGERS.find((t) => lc.slice(0, t.split(" ").length).join(" ") === t);
780
+ if (!trig) return null;
781
+ let i = trig.split(" ").length;
782
+ while (i < lc.length && (lc[i] === "the" || lc[i] === "a" || lc[i] === "all")) i += 1;
783
+ const quals = [];
784
+ while (i < lc.length && QUALIFIERS[lc[i]]) { quals.push(lc[i]); i += 1; }
785
+ const noun = i < lc.length ? entityNoun(lc[i]) : null;
786
+ if (!noun) return { node: "miss", reason: "count needs a known entity kind (functions, classes, modules, …)" };
787
+ const entWord = lc[i];
788
+ i += 1;
789
+ const tail = w.slice(i);
790
+ const tailMeaningful = lc.slice(i).some((t) => !STOPWORDS.has(t) && !AGG_TAIL_FILLER.has(t));
791
+ let base;
792
+ if (tailMeaningful) {
793
+ const setAst = parseSetPhrase(`which ${entWord} ${tail.join(" ")}`, nlp, 1);
794
+ if (!setAst || setAst.node === "miss") return { node: "miss", reason: "the count restrictor didn't parse" };
795
+ base = setAst;
796
+ } else {
797
+ base = { node: "allOfClass", entityType: noun.entityType };
798
+ }
799
+ if (quals.length) base = { node: "qualifier", filters: quals, inner: base };
800
+ return { node: "count", entityType: noun.entityType, base };
801
+ }
802
+
803
+ // Determiners/objects skipped after a LIST trigger verb ("show me THE classes") — a
804
+ // superset of the aggregate skip so "give me all the modules" reaches the kind noun.
805
+ const LIST_SKIP = new Set(["the", "a", "an", "all", "me", "us"]);
806
+ const LIST_TRIGGERS_SORTED = [...LIST_TRIGGERS].sort((a, b) => b.split(" ").length - a.split(" ").length);
807
+ // The listable node classes, named in the honest miss and the empty-index message.
808
+ const LISTABLE_KINDS = "functions, classes, methods, modules, attributes, variables, or commits";
809
+
810
+ /** LIST: "list <kind>", "show me the <kind>s", "what are the <kind>", "list <kind> in
811
+ * <module>". A sibling of the count node — it enumerates the individuals of a class
812
+ * (rendered under OVERFLOW_CAP) instead of counting them. Fires on a LIST_TRIGGERS
813
+ * verb, OR the bare interrogative "what/which <kind>" — but the interrogative form is
814
+ * gated to a filler-only tail so an ordinary reverse query ("which functions call X")
815
+ * is NOT hijacked into a list (it must stay a simple clause; the compat tests pin it).
816
+ * A scope/predicate tail ("in walk.mjs", "that call X") is delegated to parseSetPhrase
817
+ * (reusing membership/relational/boolean), and its `scoped` flag suppresses the
818
+ * "narrow with …" hint. An unknown kind after a clear imperative trigger ("list
819
+ * bananas") is an honest miss naming the listable kinds; anything less certain falls
820
+ * through (null) to the existing parser/cascade rather than guessing. */
821
+ function parseList(w, lc, nlp, depth) {
822
+ let i = 0;
823
+ let interrogative = false;
824
+ let matched = null;
825
+ for (const t of LIST_TRIGGERS_SORTED) {
826
+ const tw = t.split(" ");
827
+ if (lc.slice(0, tw.length).join(" ") === t) { matched = t; i = tw.length; break; }
828
+ }
829
+ if (!matched) {
830
+ if (lc[0] === "what" || lc[0] === "which") { interrogative = true; i = 1; }
831
+ else return null;
832
+ }
833
+ while (i < lc.length && LIST_SKIP.has(lc[i])) i += 1;
834
+ const quals = [];
835
+ while (i < lc.length && QUALIFIERS[lc[i]]) { quals.push(lc[i]); i += 1; }
836
+ const noun = i < lc.length ? entityNoun(lc[i]) : null;
837
+ // "Change" is ask-vocab.mjs's pseudo-type (no node is ever class "Change"), so it is
838
+ // not a listable class — fall through rather than render a false empty.
839
+ if (!noun || noun.placeholder || noun.entityType === "Change") {
840
+ // A clear imperative "list <one unknown plain word>" is an honest miss that NAMES
841
+ // the kinds; a verb-led or multi-word tail, or the interrogative form, is too
842
+ // uncertain to claim as a list — fall through to the existing parser/cascade.
843
+ if (!interrogative && i < lc.length && i === lc.length - 1
844
+ && /^[a-z]+$/.test(lc[i]) && !VERB_TO_KIND[lc[i]] && !PLACEHOLDER_NOUNS.includes(lc[i])) {
845
+ return { node: "miss", reason: `"${lc[i]}" isn't a listable kind — try ${LISTABLE_KINDS}` };
846
+ }
847
+ return null;
848
+ }
849
+ const entityType = noun.entityType;
850
+ const entWord = lc[i];
851
+ i += 1;
852
+ const tail = w.slice(i);
853
+ const tailMeaningful = lc.slice(i).some((t) => !STOPWORDS.has(t) && !AGG_TAIL_FILLER.has(t));
854
+ // The bare interrogative "what/which <kind>" is a list ONLY with an explicit
855
+ // list-confirming filler tail ("… are there", "… that exist"): a real predicate
856
+ // ("which functions call X") is a reverse query, and a *bare* "which methods" is left
857
+ // alone deliberately — otherwise the relaxation cascade could drop an unknown
858
+ // qualifier ("which shiny methods" → "which methods") and silently list everything,
859
+ // erasing the honest "unknown qualifier" miss. Imperative triggers ("list methods")
860
+ // carry their own list intent, so they need no such tail.
861
+ if (interrogative && (tailMeaningful || tail.length === 0)) return null;
862
+ let base;
863
+ let scoped = false;
864
+ if (tailMeaningful) {
865
+ const setAst = parseSetPhrase(`which ${[...quals, entWord, ...tail].join(" ")}`, nlp, (depth || 0) + 1);
866
+ if (!setAst || setAst.node === "miss") return { node: "miss", reason: (setAst && setAst.reason) || "the list filter didn't parse" };
867
+ base = setAst;
868
+ scoped = true;
869
+ } else {
870
+ base = { node: "allOfClass", entityType };
871
+ if (quals.length) base = { node: "qualifier", filters: quals, inner: base };
872
+ }
873
+ return { node: "list", entityType, base, scoped };
874
+ }
875
+
876
+ /** SUPERLATIVE: "which <entity> has the most/fewest <edge-noun>", "the most-connected
877
+ * <entity>", "the largest <entity>". Ranks individuals of <entity> by a degree
878
+ * metric over the classified edge groups. An unrecognized edge noun is an honest
879
+ * miss naming the supported ones. */
880
+ function parseSuperlative(w, lc, nlp) {
881
+ // extreme (single word, or "most connected" two-word)
882
+ let ext = null; let extIdx = -1;
883
+ for (let i = 0; i < lc.length; i += 1) {
884
+ const two = lc.slice(i, i + 2).join(" ");
885
+ if (SUPERLATIVE_EXTREMES[two]) { ext = SUPERLATIVE_EXTREMES[two]; extIdx = i; break; }
886
+ if (SUPERLATIVE_EXTREMES[lc[i]]) { ext = SUPERLATIVE_EXTREMES[lc[i]]; extIdx = i; break; }
887
+ }
888
+ if (!ext) return null;
889
+ // entity noun anywhere (first match, deterministic)
890
+ let entityType; let entWord = null;
891
+ for (const x of lc) { const n = entityNoun(x); if (n && !n.placeholder) { entityType = n.entityType; entWord = x; break; } }
892
+ if (!entWord) return { node: "miss", reason: "a superlative needs an entity kind (module, class, function, …)" };
893
+ // edge noun after the extreme (imports/callers/methods/…)
894
+ let metric = null; let metricNoun = null;
895
+ for (let i = extIdx; i < lc.length; i += 1) {
896
+ if (EDGE_NOUN_TO_METRIC[lc[i]]) { metric = EDGE_NOUN_TO_METRIC[lc[i]]; metricNoun = lc[i]; break; }
897
+ }
898
+ const connectivity = lc.includes("connected") || lc.slice(extIdx, extIdx + 2).join(" ") === "most connected"
899
+ || ["largest", "biggest", "smallest"].includes(lc[extIdx]);
900
+ if (!metric) {
901
+ if (connectivity) { metric = EDGE_NOUN_TO_METRIC.connections; metricNoun = "connections"; }
902
+ else return { node: "miss", reason: "name what to rank by (imports, callers, methods, tests, or connections)" };
903
+ }
904
+ return { node: "superlative", entityType, metric, metricNoun, extreme: ext };
905
+ }
906
+
907
+ /** RELATIONAL / BOOLEAN / QUALIFIER (subject-first): "[which] [<qualifier>…] <entity>
908
+ * [that] <predicate>", where <predicate> is one or more relation clauses joined by
909
+ * and/or/but-not over the SAME subject, a "<of|in> <term>" membership, or empty (a
910
+ * bare qualified class). Fires ONLY on a compositional marker — a leading qualifier,
911
+ * a relative pronoun, a gerund-led predicate, or a membership "of/in" — so a plain
912
+ * reverse query ("which functions call helper") and the bare-template ambiguous case
913
+ * ("which classes extends Base and couples to logging", no marker) both fall through
914
+ * to the existing strategies untouched. Returns an AST node, a miss, or null. */
915
+ function parseRelationalOrQualified(w, lc, nlp, depth) {
916
+ let i = 0;
917
+ while (i < lc.length && FRAME_WORDS.has(lc[i])) i += 1;
918
+ const framed = i > 0;
919
+ const quals = [];
920
+ while (i < lc.length && QUALIFIERS[lc[i]]) { quals.push(lc[i]); i += 1; }
921
+ const noun = i < lc.length ? entityNoun(lc[i]) : null;
922
+ if (!noun) {
923
+ // an unknown adjective sitting in the qualifier slot, right before a known
924
+ // entity noun ("which shiny methods", "static frobnicated functions") — an
925
+ // honest miss that NAMES the supported qualifiers, never a guess (PLAN P3).
926
+ // STOPWORDS are excluded so a normal question auxiliary in that position ("what
927
+ // DID commit X touch", "what WAS in <sha>") is left for the existing parser, not
928
+ // mistaken for an unknown qualifier.
929
+ if ((framed || quals.length) && i + 1 < lc.length && /^[a-z]+$/.test(lc[i])
930
+ && !VERB_TO_KIND[lc[i]] && !STOPWORDS.has(lc[i]) && entityNoun(lc[i + 1])) {
931
+ return { node: "miss", reason: `unknown qualifier "${lc[i]}" — supported: ${Object.keys(QUALIFIERS).join(", ")}` };
932
+ }
933
+ return null; // no subject entity → not this shape
934
+ }
935
+ const entityType = noun.entityType;
936
+ const entWord = lc[i];
937
+ i += 1;
938
+ let predLc = lc.slice(i);
939
+ let predWords = w.slice(i);
940
+ let relFlag = false;
941
+ if (predLc.length && RELATIVE_PRONOUNS.includes(predLc[0])) { relFlag = true; predLc = predLc.slice(1); predWords = predWords.slice(1); }
942
+ const membershipLed = predLc[0] === "of" || predLc[0] === "in";
943
+ const gerundLed = predLc.length > 0 && isGerundVerb(predLc[0]);
944
+ // marker gate — the crux of backward-compat: without one of these, this is not a
945
+ // compositional query and we must NOT hijack it from the existing parser.
946
+ if (!(quals.length || relFlag || membershipLed || gerundLed)) return null;
947
+
948
+ // empty predicate → a bare qualified class ("public methods")
949
+ if (!predWords.length) {
950
+ let base = { node: "allOfClass", entityType };
951
+ if (!quals.length) return { node: "miss", reason: "nothing to filter or traverse" };
952
+ return { node: "qualifier", filters: quals, inner: base };
953
+ }
954
+
955
+ const subjPrefix = noun.placeholder ? "what" : `which ${entWord}`;
956
+ const { branches, ops } = splitBoolean(predLc, predWords);
957
+ // build one atom per branch, borrowing a leading verb phrase across bare branches
958
+ // ("importing X or Y" → the second branch inherits "importing").
959
+ let prevVerb = null;
960
+ const atoms = [];
961
+ for (let b = 0; b < branches.length; b += 1) {
962
+ const bw = branches[b];
963
+ const blc = bw.map((x) => x.toLowerCase());
964
+ const op = b === 0 ? "seed" : ops[b - 1];
965
+ if (bw.length && blc.every((x) => QUALIFIERS[x])) { atoms.push({ op, kind: "qual", filters: blc }); continue; }
966
+ if (blc[0] === "of" || blc[0] === "in") {
967
+ atoms.push({ op, kind: "set", ast: { node: "membership", entityType, term: bw.slice(1).join(" ") } });
968
+ continue;
969
+ }
970
+ let phrase = bw;
971
+ const vh = findPhrase(blc, VERB_TO_KIND);
972
+ if (vh) prevVerb = bw.slice(vh.start, vh.end);
973
+ else if (prevVerb) phrase = [...prevVerb, ...bw];
974
+ // a branch is a single predicate (top-level booleans are already split out), so
975
+ // parse it as nested-or-simple — NOT back through parseSetPhrase, which would
976
+ // re-detect the branch's own gerund/relative lead and recurse on identical text.
977
+ const ast = parseBranchAst(`${subjPrefix} ${phrase.join(" ")}`, nlp, depth + 1);
978
+ if (!ast || ast.node === "miss") return { node: "miss", reason: (ast && ast.reason) || "a clause in the combination didn't parse" };
979
+ atoms.push({ op, kind: "set", ast });
980
+ }
981
+ // the first atom must be a base set, not a bare qualifier (a qualifier needs
982
+ // something to filter). "public methods" already took the empty-predicate path above.
983
+ if (atoms[0].kind !== "set") return { node: "miss", reason: "start with a clause, then combine with and/or/but-not" };
984
+
985
+ let result;
986
+ if (atoms.length === 1) {
987
+ result = atoms[0].ast;
988
+ } else {
989
+ result = { node: "boolean", entityType, atoms };
990
+ }
991
+ if (quals.length) result = { node: "qualifier", filters: quals, inner: result };
992
+ return result;
993
+ }
994
+
995
+ /** Parse a single boolean branch (one predicate over the subject) into a set-AST:
996
+ * nested (the branch has its own object-relative "that") or a plain simple clause.
997
+ * Deliberately does NOT re-enter parseRelationalOrQualified — the branch has no
998
+ * top-level boolean of its own (it was just split off one), so descending there
999
+ * would only re-detect its gerund/relative lead and recurse on the same text. */
1000
+ function parseBranchAst(text, nlp, depth) {
1001
+ if (depth > MAX_COMPOSE_DEPTH) return { node: "miss", reason: "too deep to resolve" };
1002
+ const w = splitWords(text);
1003
+ const lc = w.map((x) => x.toLowerCase());
1004
+ const nested = parseNested(w, lc, nlp, depth);
1005
+ if (nested) return nested;
1006
+ const clause = parseSimpleClause(text, nlp);
1007
+ return clause ? { node: "clause", clause } : null;
1008
+ }
1009
+
1010
+ /** Split a predicate word array on boolean connectives (longest key first, so
1011
+ * "but not" beats a bare "not"). Returns {branches:[[word…]…], ops:[op…]} with
1012
+ * branches.length === ops.length + 1. */
1013
+ function splitBoolean(predLc, predWords) {
1014
+ const conns = Object.keys(BOOLEAN_CONNECTIVES).sort((a, z) => z.split(" ").length - a.split(" ").length);
1015
+ const branches = []; const ops = [];
1016
+ let start = 0; let i = 0;
1017
+ while (i < predLc.length) {
1018
+ let hit = null;
1019
+ for (const c of conns) {
1020
+ const cw = c.split(" ");
1021
+ if (predLc.slice(i, i + cw.length).join(" ") === c) { hit = { c, len: cw.length }; break; }
1022
+ }
1023
+ if (hit && i > start) { // a connective, and not at a branch start (avoid leading "and")
1024
+ branches.push(predWords.slice(start, i));
1025
+ ops.push(BOOLEAN_CONNECTIVES[hit.c]);
1026
+ i += hit.len; start = i;
1027
+ } else if (hit) { i += hit.len; start = i; } // connective at branch start — skip it
1028
+ else i += 1;
1029
+ }
1030
+ branches.push(predWords.slice(start));
1031
+ return { branches, ops };
1032
+ }
1033
+
1034
+ // ---- compositional EVALUATION — compile an AST to a graph traversal, reusing the
1035
+ // same primitives (edgesOfKind, resolveObject, refineToEntities, traverse) the
1036
+ // simple path uses. Pure given (graph, ast, opts). ----
1037
+
1038
+ /** Reverse traversal over a SET of object ids (the nested "callers of {X…}" step) —
1039
+ * mirrors traverse()'s reverse general case (symbol-grain sibling + defines-refine),
1040
+ * but membership-tests e.object against a set instead of a single id. */
1041
+ function reverseOverSet(graph, kind, entityType, objectIds) {
1042
+ const symbolKind = SYMBOL_GRAIN_SIBLING[kind];
1043
+ if (symbolKind && FINE_ENTITY_TYPES.has(entityType)) {
1044
+ const edges = edgesOfKind(graph, symbolKind).filter((e) => objectIds.has(e.object));
1045
+ return uniqueById(edges.map((e) => graph.byId.get(e.subject)).filter((s) => s && s.class === entityType));
1046
+ }
1047
+ const edges = kindsFor(kind).flatMap((k) => edgesOfKind(graph, k)).filter((e) => objectIds.has(e.object));
1048
+ const subjects = uniqueById(edges.map((e) => graph.byId.get(e.subject)).filter(Boolean));
1049
+ if (!entityType || entityType === "Change") return subjects;
1050
+ const direct = subjects.filter((s) => s.class === entityType);
1051
+ if (direct.length) return direct;
1052
+ if (entityType !== "Module" && subjects.some((s) => s.class === "Module")) {
1053
+ return refineToEntities(graph, new Set(subjects.filter((s) => s.class === "Module").map((s) => s.id)), entityType);
1054
+ }
1055
+ return [];
1056
+ }
1057
+
1058
+ /** Forward traversal over a SET of subject ids (the "things {X…} call/define" step). */
1059
+ function forwardOverSet(graph, kind, subjectIds) {
1060
+ const edges = kindsFor(kind).flatMap((k) => edgesOfKind(graph, k)).filter((e) => subjectIds.has(e.subject));
1061
+ return uniqueById(edges.map((e) => graph.byId.get(e.object)).filter(Boolean));
1062
+ }
1063
+
1064
+ function uniqueById(inds) {
1065
+ const seen = new Set(); const out = [];
1066
+ for (const x of inds) if (x && !seen.has(x.id)) { seen.add(x.id); out.push(x); }
1067
+ return out;
1068
+ }
1069
+
1070
+ // Per-graph memo for the qualifier attribute/edge sets (exported symbols, tested
1071
+ // modules, symbol→module map) — computed once, so a qualifier filter over a large
1072
+ // result set stays cheap and deterministic.
1073
+ const qualCache = new WeakMap();
1074
+ function qualSets(graph) {
1075
+ let c = qualCache.get(graph);
1076
+ if (c) return c;
1077
+ const exported = new Set();
1078
+ for (const e of edgesOfKind(graph, "reexports")) {
1079
+ exported.add(String(e.object).toLowerCase());
1080
+ const ind = graph.byId.get(e.object);
1081
+ if (ind) exported.add(String(ind.label).toLowerCase());
1082
+ }
1083
+ const testedModules = new Set(edgesOfKind(graph, "tests").map((e) => e.object));
1084
+ const moduleOfSymbol = new Map();
1085
+ for (const e of edgesOfKind(graph, "defines")) moduleOfSymbol.set(e.object, e.subject);
1086
+ c = { exported, testedModules, moduleOfSymbol };
1087
+ qualCache.set(graph, c);
1088
+ return c;
1089
+ }
1090
+ function moduleIdOf(graph, ind) {
1091
+ if (!ind) return null;
1092
+ if (ind.class === "Module") return ind.id;
1093
+ return qualSets(graph).moduleOfSymbol.get(ind.id) || null;
1094
+ }
1095
+
1096
+ /** Does an individual satisfy one qualifier (spec from QUALIFIERS)? Reads only
1097
+ * attributes/edges the graph already carries — an unpopulated attribute (e.g.
1098
+ * isAbstract) simply yields false, an honest empty rather than an error. */
1099
+ function qualHolds(graph, ind, spec) {
1100
+ if (!spec) return false;
1101
+ switch (spec.via) {
1102
+ case "visibility": {
1103
+ const v = String((ind.attributes || []).find((a) => a.key === "visibility")?.value || "public").toLowerCase();
1104
+ return v === spec.value;
1105
+ }
1106
+ case "attr":
1107
+ return !!(ind.attributes || []).find((a) => a.key === spec.attr)?.value;
1108
+ case "exported": {
1109
+ const ex = qualSets(graph).exported;
1110
+ return ex.has(String(ind.label).toLowerCase()) || ex.has(String(ind.id).toLowerCase());
1111
+ }
1112
+ case "tested": {
1113
+ const mid = moduleIdOf(graph, ind);
1114
+ return (!!mid && qualSets(graph).testedModules.has(mid)) === spec.value;
1115
+ }
1116
+ default: return false;
1117
+ }
1118
+ }
1119
+
1120
+ /** Compile a set-producing AST into an array of individuals. */
1121
+ function evalSet(graph, ast, opts) {
1122
+ switch (ast.node) {
1123
+ case "clause": return traverse(graph, ast.clause, opts).matches || [];
1124
+ case "allOfClass": return graph.individuals.filter((i) => i.class === ast.entityType);
1125
+ case "reverseSet": {
1126
+ const ids = new Set(evalSet(graph, ast.inner, opts).map((i) => i.id));
1127
+ return reverseOverSet(graph, ast.kind, ast.entityType, ids);
1128
+ }
1129
+ case "forwardSet": {
1130
+ const ids = new Set(evalSet(graph, ast.inner, opts).map((i) => i.id));
1131
+ return forwardOverSet(graph, ast.kind, ids);
1132
+ }
1133
+ case "membership": {
1134
+ const r = resolveObject(graph, ast.term);
1135
+ if (!r.match) return [];
1136
+ const ids = new Set([r.match.id]);
1137
+ const objs = uniqueById(MEMBERSHIP_KINDS.flatMap((k) => forwardOverSet(graph, k, ids)));
1138
+ return ast.entityType ? objs.filter((o) => o.class === ast.entityType) : objs;
1139
+ }
1140
+ case "qualifier": {
1141
+ const base = evalSet(graph, ast.inner, opts);
1142
+ return base.filter((ind) => ast.filters.every((f) => qualHolds(graph, ind, QUALIFIERS[f])));
1143
+ }
1144
+ case "boolean": return evalBoolean(graph, ast, opts);
1145
+ case "anaphora": return evalAnaphora(graph, ast, opts).matches;
1146
+ default: return [];
1147
+ }
1148
+ }
1149
+
1150
+ /** Fold a boolean AST left-to-right into a result set. A qualifier atom acts as a
1151
+ * set filter on the accumulator (intersection keeps satisfiers, difference removes
1152
+ * them); a set atom contributes its own id-set for the op. */
1153
+ function evalBoolean(graph, ast, opts) {
1154
+ let acc = [];
1155
+ for (const atom of ast.atoms) {
1156
+ if (atom.op === "seed") { acc = evalSet(graph, atom.ast, opts); continue; }
1157
+ if (atom.kind === "qual") {
1158
+ const holds = (ind) => atom.filters.every((f) => qualHolds(graph, ind, QUALIFIERS[f]));
1159
+ acc = atom.op === "difference" ? acc.filter((i) => !holds(i)) : acc.filter((i) => holds(i));
1160
+ continue;
1161
+ }
1162
+ const oids = new Set(evalSet(graph, atom.ast, opts).map((i) => i.id));
1163
+ if (atom.op === "intersection") acc = acc.filter((i) => oids.has(i.id));
1164
+ else if (atom.op === "difference") acc = acc.filter((i) => !oids.has(i.id));
1165
+ else if (atom.op === "union") {
1166
+ const seen = new Set(acc.map((i) => i.id));
1167
+ for (const other of evalSet(graph, atom.ast, opts)) if (!seen.has(other.id)) { seen.add(other.id); acc.push(other); }
1168
+ }
1169
+ }
1170
+ return acc;
1171
+ }
1172
+
1173
+ /** Anaphora over ask()'s `prev` id array — filter/count the previous answer's ids.
1174
+ * No prev supplied → honest miss (never a guess), like an unresolved pronoun. */
1175
+ function evalAnaphora(graph, ast, opts) {
1176
+ const prev = opts && opts.prev;
1177
+ if (!Array.isArray(prev) || !prev.length) return { compositeMiss: true, reason: "no-prev", matches: [] };
1178
+ let items = prev.map((id) => graph.byId.get(id)).filter(Boolean);
1179
+ const f = ast.filter;
1180
+ if (f && f.type === "qual") {
1181
+ items = items.filter((ind) => f.filters.every((q) => qualHolds(graph, ind, QUALIFIERS[q])));
1182
+ } else if (f && f.type === "clause") {
1183
+ const r = resolveObject(graph, f.clause.object);
1184
+ if (!r.match) items = [];
1185
+ else {
1186
+ // include the symbol-grain sibling so a fn->fn "call" filter tests callsSymbol,
1187
+ // not just the module-coarse "calls" edge (mirrors traverse()'s reverse path).
1188
+ const sib = SYMBOL_GRAIN_SIBLING[f.clause.kind];
1189
+ const kinds = [...kindsFor(f.clause.kind), ...(sib ? [sib] : [])];
1190
+ const ok = new Set(kinds.flatMap((k) => edgesOfKind(graph, k)).filter((e) => e.object === r.match.id).map((e) => e.subject));
1191
+ items = items.filter((ind) => ok.has(ind.id));
1192
+ }
1193
+ }
1194
+ // a count over a prior set names the entity kind when the survivors share a class.
1195
+ const common = items.length && items.every((x) => x.class === items[0].class) ? items[0].class : null;
1196
+ if (ast.mode === "count") return { compositeKind: "count", count: items.length, entityType: common, matches: [] };
1197
+ return { compositeKind: "set", matches: items, entityType: common };
1198
+ }
1199
+
1200
+ // Structural kinds counted for "most-connected" (total degree). Symbol-grain and
1201
+ // commit-history kinds are excluded so "connections" reads as the code-structure
1202
+ // degree a developer means, not every recorded touch.
1203
+ const DEGREE_KINDS = ["imports", "calls", "callsSymbol", "inherits", "contains", "tests"];
1204
+ /** Degree of an individual under a superlative metric ({kind, dir, sibling?, filter?}). */
1205
+ function degreeMetric(graph, ind, metric) {
1206
+ const kinds = metric.kind === "*" ? DEGREE_KINDS : [metric.kind, ...(metric.sibling ? [metric.sibling] : [])];
1207
+ let n = 0;
1208
+ for (const k of kinds) for (const e of edgesOfKind(graph, k)) {
1209
+ const out = e.subject === ind.id; const inc = e.object === ind.id;
1210
+ if (metric.dir === "out" && out) {
1211
+ if (metric.filter) { const o = graph.byId.get(e.object); if (!o || o.class !== metric.filter) continue; }
1212
+ n += 1;
1213
+ } else if (metric.dir === "in" && inc) n += 1;
1214
+ else if (metric.dir === "both" && (out || inc)) n += 1;
1215
+ }
1216
+ return n;
1217
+ }
1218
+ function evalSuperlative(graph, ast) {
1219
+ const pool = graph.individuals.filter((i) => i.class === ast.entityType);
1220
+ const scored = pool.map((ind) => ({ ind, score: degreeMetric(graph, ind, ast.metric) }))
1221
+ .sort((a, z) => (ast.extreme === "most" ? z.score - a.score : a.score - z.score));
1222
+ if (!scored.length) return { compositeKind: "superlative", entityType: ast.entityType, matches: [] };
1223
+ const best = scored[0].score;
1224
+ const winners = scored.filter((s) => s.score === best).map((s) => s.ind);
1225
+ return { compositeKind: "superlative", entityType: ast.entityType, metricNoun: ast.metricNoun, extreme: ast.extreme, score: best, matches: winners };
1226
+ }
1227
+
1228
+ /** Compile any compositional AST to a result object traverse() returns for the
1229
+ * simple path — {matches, …} plus compositeKind/compositeMiss flags render() reads. */
1230
+ export function evalComposite(graph, ast, opts = {}) {
1231
+ if (ast.node === "miss") return { compositeMiss: true, reason: ast.reason || null, matches: [] };
1232
+ if (ast.node === "count") return { compositeKind: "count", count: evalSet(graph, ast.base, opts).length, entityType: ast.entityType, matches: [] };
1233
+ if (ast.node === "list") return { compositeKind: "list", matches: evalSet(graph, ast.base, opts), entityType: ast.entityType, scoped: ast.scoped };
1234
+ if (ast.node === "superlative") return evalSuperlative(graph, ast);
1235
+ if (ast.node === "anaphora") return evalAnaphora(graph, ast, opts);
1236
+ return { compositeKind: "set", matches: evalSet(graph, ast, opts), entityType: ast.entityType || null };
1237
+ }
1238
+
1239
+ // ---- compositional RENDER — templated, same "honest miss vs cited hit" discipline
1240
+ // as renderCore. ----
1241
+
1242
+ const compositeList = (matches) => listJoin(matches.slice(0, OVERFLOW_CAP)
1243
+ .map((m) => (["Function", "Method"].includes(m.class) ? `${m.label}()` : m.label)))
1244
+ + (matches.length > OVERFLOW_CAP ? `, …and ${matches.length - OVERFLOW_CAP} more` : "");
1245
+
1246
+ /** A compositional worked example for the rephrase hint (§honest miss now shows a
1247
+ * compositional phrasing too). */
1248
+ export function compositionalHint() {
1249
+ return 'compositional queries also work: "which functions call X and call Y", "what calls something that imports X", "public methods of X", "list functions" / "show me the classes", "how many classes", "which module has the most imports", or (after a listing) "which of those are tested"';
1250
+ }
1251
+
1252
+ function renderComposite(parsed, result) {
1253
+ if (result.compositeMiss) {
1254
+ if (result.reason === "no-prev") {
1255
+ return { content: `"those"/"them" needs a previous answer to refer to — ask a listing question first, then follow up.`, miss: true, ambiguous: false };
1256
+ }
1257
+ return { content: `couldn't compile this compositional question${result.reason ? ` (${result.reason})` : ""}. ${compositionalHint()}.`, miss: true, ambiguous: false };
1258
+ }
1259
+ if (result.compositeKind === "count") {
1260
+ const noun = result.entityType ? nounFor(result.entityType, result.count) : (result.count === 1 ? "result" : "results");
1261
+ return { content: `${result.count} ${noun}.`, miss: false, ambiguous: false, matches: [] };
1262
+ }
1263
+ if (result.compositeKind === "list") {
1264
+ if (!result.matches.length) {
1265
+ return { content: `no ${nounFor(result.entityType, 2)} in this index.`, miss: true, ambiguous: false, matches: [] };
1266
+ }
1267
+ // an unscoped list that overflowed the cap gets a light hint to narrow by module —
1268
+ // but only for kinds that live IN a module (a "modules in <module>" or "commits in
1269
+ // <module>" scope is meaningless); the scoped forms are already narrow, no hint.
1270
+ const scopeable = !["Module", "Commit"].includes(result.entityType);
1271
+ const hint = (!result.scoped && scopeable && result.matches.length > OVERFLOW_CAP)
1272
+ ? ` — narrow with "${nounFor(result.entityType, 2)} in <module>"`
1273
+ : "";
1274
+ return { content: `${compositeList(result.matches)}${hint}.`, miss: false, ambiguous: false, matches: result.matches };
1275
+ }
1276
+ if (result.compositeKind === "superlative") {
1277
+ if (!result.matches.length) return { content: `no ${nounFor(result.entityType, 2)} to rank in this index.`, miss: true, ambiguous: false };
1278
+ const lead = result.extreme === "most" ? "the most" : "the fewest";
1279
+ const tie = result.matches.length > 1 ? ` (${result.matches.length}-way tie)` : "";
1280
+ return {
1281
+ content: `${compositeList(result.matches)} — ${lead} ${result.metricNoun} (${result.score})${tie}.`,
1282
+ miss: false, ambiguous: false, matches: result.matches,
1283
+ };
1284
+ }
1285
+ // set-producing
1286
+ if (!result.matches.length) {
1287
+ return { content: `nothing in the index matches that${result.entityType ? ` (${nounFor(result.entityType, 2)})` : ""}.`, miss: true, ambiguous: false, matches: [] };
1288
+ }
1289
+ return { content: `${compositeList(result.matches)}.`, miss: false, ambiguous: false, matches: result.matches };
1290
+ }
1291
+
1292
+ /** The rephrase hint shown on a grammar miss — generated from the SAME tables the parser
1293
+ * uses, so it can never suggest a phrasing the grammar doesn't actually support (§6.3). */
1294
+ export function rephraseHint() {
1295
+ return '"which <functions|classes|modules> <imports|calls|uses|inherits from|tests|touched> <name>" or "what does <name> <import|call|export>" or "what uses <name>" or "where is <name> defined" / "where is <name> mentioned" or "when did <name> change" or "which changes touch commit <sha>"/"what did commit <sha> touch" (a commit\'s own changes) or plainly "what calls this" (about a selected node) or "what does <term> mean"/"what is a <ClassName>" (about the graph\'s own vocabulary). '
1296
+ + compositionalHint();
1297
+ }
1298
+
1299
+ // ---- §4 object-term resolution — mechanical, no embeddings, tiered, stop at first hit ----
1300
+
1301
+ function componentSet(s) {
1302
+ return new Set(String(s).toLowerCase().split(/[^a-z0-9]+/).filter(Boolean));
1303
+ }
1304
+
1305
+ /** Resolve a free-text object/subject term against the graph's individuals, in priority
1306
+ * order (§4, generalized beyond the module-coupling worked example to cover every verb
1307
+ * family's object grain — `inherits`/`calls` resolve against Class/Function names, not
1308
+ * just modules): a sha-shaped term ("[commit ]<hex≥7>") first resolves against Commit
1309
+ * individuals by unique id/label prefix (see the inline comment), then (1) exact
1310
+ * label/id match, (2) an `ext:` unresolved-target match (today
1311
+ * ext: targets are edge-endpoint STRINGS with no individual of their own — e.g. an
1312
+ * unimported inherits base, or any import target — so this tier returns a synthetic
1313
+ * {id:"ext:<name>", label:<name>, class:null} match rather than an `individuals` lookup;
1314
+ * see PLAN_MECHANICAL_CHAT.md §10 on why external `imports` targets land here rather
1315
+ * than tier 1/3 today), (3) boundary-aware substring/component match, (4) a prose-index
1316
+ * fallback (PLAN_PROSE_INDEX.md §6): the term, tokenized the same way as a docstring, is
1317
+ * looked up against `graph.proseIndex` via `lookupByProseTokens` — an exact WORD-level
1318
+ * overlap match against a symbol's decomposed-identifier or doc-comment tokens, never a
1319
+ * substring/fuzzy guess (the same "no wrong edge" standard as tiers 1-3, just over a
1320
+ * different token source: prose rather than the literal identifier). Only ever consulted
1321
+ * once every literal-identifier tier above has failed, and the result is tagged
1322
+ * `matchedVia: "prose"` so a caller can tell the match came from prose content rather
1323
+ * than the symbol's own name — the render layer does not currently read this (it treats
1324
+ * a resolved match as a resolved match, same "honest miss vs genuine hit" binary tiers
1325
+ * 1-3 already use), but the field is there for any caller that wants to surface it. (5)
1326
+ * a bounded Damerau-Levenshtein pass against labels AND label components (two-level
1327
+ * fuzzy, 2026-07-02): a UNIQUE within-bound match resolves, tagged `matchedVia:
1328
+ * "fuzzy"` so render() can say "assuming you meant <label>" out loud; multiple
1329
+ * matches at the same best distance are an honest ambiguity listing the candidates.
1330
+ * Never applied to sha-shaped terms (the commit namespace is exact-or-ambiguous
1331
+ * only) nor to terms under 4 chars (the bound would cover half of everything).
1332
+ * (6) no match at all — an honest miss. Returns {match, candidates, tier, ambiguous
1333
+ * [, matchedVia]} — ambiguous on a true tier-3 score tie, a tier-4 overlap-count
1334
+ * tie, or a tier-5 distance tie. */
1335
+ export function resolveObject(graph, term) {
1336
+ const t = String(term || "").trim();
1337
+ if (!t) return { match: null, candidates: [], tier: null, ambiguous: false };
1338
+ const tLc = t.toLowerCase();
1339
+ const pool = graph.individuals;
1340
+
1341
+ // commit-sha tier (checked first, only for sha-shaped terms): "ef74e44e25c8",
1342
+ // "commit ef74e44e25c8", "commit:ef74e44", or a full 40-char sha resolve against
1343
+ // Commit individuals by id/label prefix (ids are commit:<full-sha>, labels the
1344
+ // 12-char short sha), case-insensitive. A UNIQUE prefix is exact-grade over the
1345
+ // closed commit namespace (tier 1); a prefix shared by more than one commit is an
1346
+ // honest ambiguity listing the candidates — never "the first one"; a hex-looking
1347
+ // word matching NO commit falls through to the ordinary tiers unchanged (it may
1348
+ // be a real code identifier).
1349
+ const shaTerm = tLc.match(/^(commit[:\s])?([0-9a-f]{7,40})$/);
1350
+ if (shaTerm) {
1351
+ const sha = shaTerm[2];
1352
+ const hits = pool.filter((i) => i.class === "Commit"
1353
+ && (String(i.id).toLowerCase().startsWith(`commit:${sha}`) || String(i.label).toLowerCase().startsWith(sha)));
1354
+ if (hits.length === 1) return { match: hits[0], candidates: [], tier: 1, ambiguous: false };
1355
+ if (hits.length > 1) return { match: hits[0], candidates: hits.slice(1, 5), tier: 1, ambiguous: true };
1356
+ // the explicit "commit" noun declares intent — with no matching commit, falling
1357
+ // through would let the WORD "commit" component-match the Commit schema node (or
1358
+ // any identifier containing it): a guess, not a resolution. Bare hex still falls
1359
+ // through (it may be a real code identifier).
1360
+ if (shaTerm[1]) return { match: null, candidates: [], tier: null, ambiguous: false };
1361
+ }
1362
+
1363
+ const exact = pool.find((i) => String(i.label).toLowerCase() === tLc || String(i.id).toLowerCase() === tLc);
1364
+ if (exact) return { match: exact, candidates: [], tier: 1, ambiguous: false };
1365
+
1366
+ // ext: targets never have their own individual (they're a raw edge-endpoint id) — find
1367
+ // the actual (case-preserved) id off a real edge rather than reconstructing it, so a
1368
+ // typo'd term can't silently "resolve" to an ext: id nothing in the graph references.
1369
+ const extLc = `ext:${tLc}`;
1370
+ let extId = null;
1371
+ outer: for (const g of graph.relations) {
1372
+ for (const e of g.edges) {
1373
+ if (String(e.object).toLowerCase() === extLc) { extId = e.object; break outer; }
1374
+ }
1375
+ }
1376
+ if (extId) return { match: { id: extId, label: t, class: null }, candidates: [], tier: 2, ambiguous: false };
1377
+
1378
+ // tier 3 — two disjoint regimes (dotted-symbol fix, 2026-07-02, advisor-verified
1379
+ // bug): a DOTTED term with no slash ("res.json", "Widget.render", "walk.mjs") is
1380
+ // symbol-shaped (object.member / Class.method / a bare file name), and the old
1381
+ // any-substring-of-any-label pass let it land on a module whose PATH merely
1382
+ // contains the text ("res.json" -> test/res.json.js — the wrong grain presented
1383
+ // as if the term were that file). Such terms now match only (a) symbol labels
1384
+ // (whole-term containment, or the ".member" suffix when the owner alias differs:
1385
+ // "res.json" -> Response.json), and (b) module labels by EXACT basename equality
1386
+ // ("walk.mjs" -> src/walk.mjs — extension-stripped basename equality is
1387
+ // deliberately NOT used; that is precisely the phantom-path vector). Symbol
1388
+ // matches outrank module matches. Undotted/slashed terms keep the original pass.
1389
+ const scored = [];
1390
+ const dotted = !tLc.includes("/") && /^[\w$]+(\.[\w$]+)+$/.test(tLc);
1391
+ if (dotted) {
1392
+ const lastSeg = tLc.split(".").pop();
1393
+ for (const m of pool) {
1394
+ const label = String(m.label || "").toLowerCase();
1395
+ if (m.class === "Module") {
1396
+ if (label.split("/").pop() === tLc) scored.push({ ind: m, score: 1000 - Math.abs(label.length - tLc.length) });
1397
+ } else if (label.includes(tLc)) {
1398
+ scored.push({ ind: m, score: 2000 - Math.abs(label.length - tLc.length) });
1399
+ } else if (label.endsWith(`.${lastSeg}`)) {
1400
+ scored.push({ ind: m, score: 1500 - Math.abs(label.length - tLc.length) });
1401
+ }
1402
+ }
1403
+ } else {
1404
+ const termComps = componentSet(t);
1405
+ for (const m of pool) {
1406
+ const label = String(m.label || "").toLowerCase();
1407
+ if (label.includes(tLc)) {
1408
+ scored.push({ ind: m, score: 1000 - Math.abs(label.length - tLc.length) });
1409
+ continue;
1410
+ }
1411
+ const overlap = [...termComps].filter((c) => componentSet(m.label).has(c)).length;
1412
+ if (overlap > 0) scored.push({ ind: m, score: overlap * 10 });
1413
+ }
1414
+ }
1415
+ scored.sort((a, b) => b.score - a.score);
1416
+ if (scored.length) {
1417
+ const [best, ...rest] = scored;
1418
+ const tied = rest.filter((x) => x.score === best.score);
1419
+ return {
1420
+ match: best.ind,
1421
+ candidates: rest.slice(0, 4).map((x) => x.ind),
1422
+ tier: 3,
1423
+ ambiguous: tied.length > 0,
1424
+ };
1425
+ }
1426
+
1427
+ // tier 4: prose-index fallback (PLAN_PROSE_INDEX.md §6) — see the function doc above.
1428
+ // The typeof guard is the same viewer-bundle boundary as defaultNlp(): viz.mjs's
1429
+ // askSource strips the prose.mjs import but does not inline prose.mjs, so in the
1430
+ // browser `lookupByProseTokens` is an undeclared identifier — without the guard,
1431
+ // ANY term reaching this tier threw a ReferenceError in the page instead of
1432
+ // rendering the honest miss (a real, previously-untested viewer bug).
1433
+ // DOTTED terms never consult prose: "res.json" word-matches test/res.json.js's
1434
+ // own path tokens, which is the tier-3 phantom-path bug reappearing through a
1435
+ // side door — a dotted term names an identifier, and identifiers resolve by
1436
+ // label (tiers above) or the bounded fuzzy pass below, or they honestly miss.
1437
+ let proseResult = null;
1438
+ const proseHits = !dotted && typeof lookupByProseTokens === "function" ? lookupByProseTokens(graph.proseIndex, t) : [];
1439
+ if (proseHits.length) {
1440
+ const [best, ...rest] = proseHits;
1441
+ const bestInd = graph.byId.get(best.id);
1442
+ if (bestInd) {
1443
+ const tied = rest.filter((h) => h.score === best.score);
1444
+ proseResult = {
1445
+ match: bestInd,
1446
+ candidates: rest.slice(0, 4).map((h) => graph.byId.get(h.id)).filter(Boolean),
1447
+ tier: 4,
1448
+ ambiguous: tied.length > 0,
1449
+ matchedVia: "prose",
1450
+ };
1451
+ // A UNIQUE SEMANTIC prose hit stands — fuzzy is never consulted (lower
1452
+ // tiers fire only on a miss). Two prose outcomes yield to tier 5 instead:
1453
+ // (a) an AMBIGUOUS tie — a typo'd identifier ("bulidContextBundle") often
1454
+ // word-overlaps several symbols' prose tokens at the same score while its
1455
+ // 1-edit NAME match is unique; (b) a hit that only exists because the
1456
+ // prose SPELL layer corrected the query token (via:"spell") — the same
1457
+ // typo tier 5 resolves with stronger (name) evidence and announces out
1458
+ // loud, exactly the division of labour prose.mjs's own spell-layer
1459
+ // comment specifies. If tier 5 cannot resolve uniquely either, the
1460
+ // stashed prose result is surfaced as-is.
1461
+ if (!proseResult.ambiguous && best.via !== "spell") return proseResult;
1462
+ }
1463
+ }
1464
+
1465
+ // tier 5: bounded fuzzy (see the function doc) — every exact tier above missed
1466
+ // (or tier 4 tied), so a typo'd identifier gets one honest chance against labels
1467
+ // and their components. sha-shaped terms never reach here (guard above);
1468
+ // sub-4-char terms are excluded because a 1-edit budget on 3 chars matches far
1469
+ // too much to ever be a unique intent.
1470
+ if (!shaTerm && tLc.length >= 4) {
1471
+ const bound = fuzzyBound(tLc);
1472
+ let best = bound + 1;
1473
+ let hits = [];
1474
+ for (const m of pool) {
1475
+ let d = editDistance(String(m.label || "").toLowerCase(), tLc, bound);
1476
+ if (d > 0) {
1477
+ for (const comp of componentSet(m.label)) {
1478
+ if (d <= 0) break;
1479
+ d = Math.min(d, editDistance(comp, tLc, bound));
1480
+ }
1481
+ }
1482
+ if (d < best) { best = d; hits = [m]; }
1483
+ else if (d === best && d <= bound) hits.push(m);
1484
+ }
1485
+ if (best <= bound && hits.length === 1) {
1486
+ return { match: hits[0], candidates: [], tier: 5, ambiguous: false, matchedVia: "fuzzy" };
1487
+ }
1488
+ if (best <= bound && hits.length > 1 && !proseResult) {
1489
+ // equidistant fuzzy tie with no prose evidence either — honest ambiguity.
1490
+ const [bestInd, ...rest] = hits;
1491
+ return { match: bestInd, candidates: rest.slice(0, 4), tier: 5, ambiguous: true, matchedVia: "fuzzy" };
1492
+ }
1493
+ }
1494
+ // fuzzy couldn't resolve uniquely: surface the prose tie (when there was one)
1495
+ // exactly as before, else the honest miss.
1496
+ return proseResult || { match: null, candidates: [], tier: null, ambiguous: false };
1497
+ }
1498
+
1499
+ /** Resolve a term that may be a context pronoun ("this"/"it"/"that"/"here") —
1500
+ * when `contextId` is given, resolve straight to that graph entity (a real
1501
+ * click/focus in the caller's UI, not a guess); with no contextId, an honest
1502
+ * miss explaining exactly what's missing, distinct from "no such name in the
1503
+ * index". A non-pronoun term always falls through to the ordinary
1504
+ * resolveObject tiers. */
1505
+ function resolveTermOrContext(graph, term, contextId) {
1506
+ if (CONTEXT_PRONOUNS.includes(String(term || "").trim().toLowerCase())) {
1507
+ if (!contextId) return { match: null, candidates: [], tier: null, ambiguous: false, unresolvedPronoun: true };
1508
+ const ind = graph.byId.get(contextId);
1509
+ return ind
1510
+ ? { match: ind, candidates: [], tier: 1, ambiguous: false }
1511
+ : { match: null, candidates: [], tier: null, ambiguous: false, unresolvedPronoun: true };
1512
+ }
1513
+ return resolveObject(graph, term);
1514
+ }
1515
+
1516
+ // ---- traversal — orchestrates codegraph.mjs's edgesOfKind; grain-refines a module-coarse
1517
+ // edge (e.g. imports: Module->Module) down to a finer requested entityType via `defines`
1518
+ // (Module -> top-level Function/Class/Method/Attribute), never re-implementing edge scans. ----
1519
+
1520
+ function refineToEntities(graph, moduleIds, entityType) {
1521
+ const out = [];
1522
+ for (const e of edgesOfKind(graph, "defines")) {
1523
+ if (!moduleIds.has(e.subject)) continue;
1524
+ const ind = graph.byId.get(e.object);
1525
+ if (ind && ind.class === entityType) out.push(ind);
1526
+ }
1527
+ return out;
1528
+ }
1529
+
1530
+ /** Everything a commit touched, across BOTH stored grains — touches (Commit->Module)
1531
+ * and touchesSymbol (Commit->fn/method/class) — narrowed by the asked entity type:
1532
+ * "modules"/"files" keeps the coarse grain only, a fine type keeps its own symbol
1533
+ * class only, and null/"Change" ("which changes touch commit X") keeps the union.
1534
+ * The result carries `commitSubject` so render() cites the commit and groups the
1535
+ * touched entities by class instead of pretending the commit was a search target. */
1536
+ function commitTouches(graph, commit, entityType, extra = {}) {
1537
+ // "Commit" counts as wildcard here too: in "what was touched by commit X" the
1538
+ // keyword-spotter consumes "commit" as the entity keyword though it belongs to
1539
+ // the object noun phrase — and no commit ever touches another commit (no
1540
+ // Commit->Commit edges exist), so honoring it as a class filter could only
1541
+ // ever manufacture a false blank.
1542
+ const wildcard = !entityType || entityType === "Change" || entityType === "Commit";
1543
+ const wantCoarse = wildcard || entityType === "Module";
1544
+ const wantFine = wildcard || FINE_ENTITY_TYPES.has(entityType);
1545
+ const kinds = [...(wantCoarse ? ["touches"] : []), ...(wantFine ? ["touchesSymbol"] : [])];
1546
+ let matches = kinds
1547
+ .flatMap((k) => edgesOfKind(graph, k))
1548
+ .filter((e) => e.subject === commit.id)
1549
+ .map((e) => graph.byId.get(e.object))
1550
+ .filter(Boolean);
1551
+ if (entityType && FINE_ENTITY_TYPES.has(entityType)) matches = matches.filter((m) => m.class === entityType);
1552
+ return {
1553
+ matches, objMatch: commit, commitSubject: true, ambiguous: false, candidates: [],
1554
+ traversal: `${kinds.join("+")} edges where subject = commit ${commit.label}`,
1555
+ ...extra,
1556
+ };
1557
+ }
1558
+
1559
+ /** Safety net (paired with the render-branch fix earlier in this file's history): a
1560
+ * {shape, kind, entityType} combination must be explicitly listed here to receive
1561
+ * real non-"direct" modifier behavior. Anything parsing to a non-"direct" modifier
1562
+ * that ISN'T listed gets an honest "not supported yet" response from render() —
1563
+ * never a silent fallback to direct-only behavior. This means a future
1564
+ * MODIFIER_TO_KIND addition that forgets to wire traverse()/render() for it fails
1565
+ * loud here, by construction, rather than quietly behaving as if the modifier had
1566
+ * never been given (the exact bug class this file's own render-routing fix, above,
1567
+ * just caught). Today's only non-"direct" value is "transitive" (PLAN_MECHANICAL_
1568
+ * CHAT.md P1), wired below for reverse-shape imports/calls closures over
1569
+ * impactClosure (codegraph.mjs) — module-coarse only; the fine-grained
1570
+ * callsSymbol/touchesSymbol siblings and every other predicate kind have no
1571
+ * closure primitive yet, and forward-shape currently never parses a non-"direct"
1572
+ * modifier at all (both parsing strategies hardcode modifier:"direct" for it). */
1573
+ function modifierIsWired(shape, kind, entityType) {
1574
+ return shape === "reverse" && (kind === "imports" || kind === "calls") && (!entityType || entityType === "Module");
1575
+ }
1576
+ // Matches renderImpact's own default (codegraph.mjs) — impactClosure is reused as-is,
1577
+ // not reimplemented, so its own depth convention is the honest one to inherit.
1578
+ const TRANSITIVE_MAX_DEPTH = 8;
1579
+
1580
+ /** Compile a parsed query into a graph lookup. Pure given (graph, parsed, opts).
1581
+ * `opts.contextId` resolves a context pronoun ("this"/"it"/…) when the parse
1582
+ * needed one. Returns {matches, objMatch, candidates, traversal, ambiguous,
1583
+ * answer?, unresolvedPronoun?} — `answer` only set for the "ask" shape.
1584
+ * `matches` is always an array of individuals (or edge records for "ask"). */
1585
+ export function traverse(graph, parsed, { contextId = null, prev = null } = {}) {
1586
+ if (!parsed) return { matches: [], objMatch: null, candidates: [], traversal: null, ambiguous: false };
1587
+ // compositional AST (PLAN §5.16 P3) — the new grammar's nodes carry a `node` tag;
1588
+ // everything else (simple clauses, ambiguousParse) flows through the original path
1589
+ // below completely unchanged.
1590
+ if (parsed.node) return evalComposite(graph, parsed, { contextId, prev });
1591
+ if (parsed.ambiguousParse) return { matches: [], objMatch: null, candidates: [], traversal: null, ambiguous: false };
1592
+ const { shape, kind, entityType } = parsed;
1593
+
1594
+ // meta: a question about the graph's OWN vocabulary ("what does cochange mean", "what
1595
+ // is a Commit") — looked up against the SchemaClass/SchemaPredicate individuals
1596
+ // schema-docs.mjs's ingestSchemaDocs merged into the graph, not a code-edge traversal.
1597
+ // Matched by exact (case-insensitive) label ("cochange", "Commit") OR the raw `token`
1598
+ // attribute a SchemaPredicate also carries ("mgx:callsSymbol") — never substring/fuzzy,
1599
+ // same discipline as resolveObject's own tiers: a real term match or an honest miss.
1600
+ if (shape === "meta") {
1601
+ const term = String(parsed.object || "").trim();
1602
+ const termLc = term.toLowerCase();
1603
+ const match = (graph.individuals || []).find((i) => {
1604
+ if (i.class !== "SchemaClass" && i.class !== "SchemaPredicate") return false;
1605
+ if (String(i.label).toLowerCase() === termLc) return true;
1606
+ const token = (i.attributes || []).find((a) => a.key === "token")?.value;
1607
+ return token && String(token).toLowerCase() === termLc;
1608
+ });
1609
+ if (!match) return { matches: [], objMatch: null, candidates: [], traversal: `schema lookup for "${term}"`, ambiguous: false };
1610
+ return {
1611
+ matches: [match], objMatch: match, candidates: [],
1612
+ traversal: `schema lookup for "${term}"`, ambiguous: false,
1613
+ };
1614
+ }
1615
+
1616
+ // mentions: "where is X mentioned" (2026-07-02 query families) — the prose
1617
+ // surface, not an edge traversal: list the individuals whose decomposed
1618
+ // identifier / doc-comment tokens contain the term's words (the same index
1619
+ // resolveObject's tier 4 consults, surfaced directly). The term itself is NOT
1620
+ // resolved to an entity first — the question is about mentions of the words,
1621
+ // which is exactly what the prose index stores. typeof guard: same viewer-
1622
+ // bundle boundary as tier 4 (prose.mjs is never inlined).
1623
+ if (shape === "mentions") {
1624
+ const term = String(parsed.object || "").trim();
1625
+ const hits = typeof lookupByProseTokens === "function" ? lookupByProseTokens(graph.proseIndex, term) : [];
1626
+ const matches = hits.map((h) => graph.byId.get(h.id)).filter(Boolean);
1627
+ return {
1628
+ matches, objMatch: null, candidates: [], ambiguous: false, mentionsShape: true,
1629
+ traversal: `proseIndex word lookup for "${term}"`,
1630
+ };
1631
+ }
1632
+
1633
+ // §modifier support gate (safety net, see modifierIsWired's own doc above) — checked
1634
+ // BEFORE object resolution, so an unsupported modifier+kind combination gets its own
1635
+ // honest capability-gap message rather than masquerading as an object-miss, or worse,
1636
+ // silently behaving as if "transitively"/"indirectly" had never been said.
1637
+ if (parsed.modifier && parsed.modifier !== "direct" && !modifierIsWired(shape, kind, entityType)) {
1638
+ return {
1639
+ matches: [], objMatch: null, candidates: [], ambiguous: false,
1640
+ unsupportedModifier: true,
1641
+ traversal: `modifier "${parsed.modifier}" requested for a "${kind}" query — no closure traversal exists for this combination yet`,
1642
+ };
1643
+ }
1644
+
1645
+ if (shape === "ask") {
1646
+ const subj = resolveTermOrContext(graph, parsed.subject, contextId);
1647
+ const obj = resolveTermOrContext(graph, parsed.object, contextId);
1648
+ if (!subj.match || !obj.match) {
1649
+ return {
1650
+ matches: [], objMatch: obj.match, candidates: obj.candidates, traversal: null, ambiguous: false, answer: null,
1651
+ unresolvedPronoun: !!(subj.unresolvedPronoun || obj.unresolvedPronoun),
1652
+ };
1653
+ }
1654
+ // touches edges are stored commit -> entity, so when the question names the
1655
+ // commit on the OBJECT side ("was walk.mjs touched by commit X"), orient the
1656
+ // edge test by where the commit actually is instead of failing on direction;
1657
+ // a commit subject is also checked at the symbol grain ("does commit X touch
1658
+ // <function>" lives on touchesSymbol, not the module-coarse kind).
1659
+ let [from, to] = [subj.match, obj.match];
1660
+ let kinds = kindsFor(kind); // "uses" checks the whole union ("does X use Y")
1661
+ if (kind === "touches") {
1662
+ if (to.class === "Commit" && from.class !== "Commit") [from, to] = [to, from];
1663
+ if (from.class === "Commit") kinds = ["touches", "touchesSymbol"];
1664
+ }
1665
+ const edges = kinds.flatMap((k) => edgesOfKind(graph, k)).filter((e) => e.subject === from.id && e.object === to.id);
1666
+ return {
1667
+ matches: edges, answer: edges.length > 0, objMatch: obj.match, subjMatch: subj.match,
1668
+ candidates: [], traversal: `${kinds.join("+")} edge from ${from.label} to ${to.label}`, ambiguous: false,
1669
+ };
1670
+ }
1671
+
1672
+ // reverse and forward both resolve one named term ("object" in the parsed shape — for
1673
+ // forward it is the query's grammatical subject, e.g. "what does X import" -> parsed.object = X).
1674
+ const { match: objMatch, candidates, ambiguous, unresolvedPronoun, matchedVia } = resolveTermOrContext(graph, parsed.object, contextId);
1675
+ if (!objMatch) return { matches: [], objMatch: null, candidates, traversal: null, ambiguous: false, unresolvedPronoun };
1676
+
1677
+ // where: "where is X [defined]" (2026-07-02 query families) — the resolved
1678
+ // entity IS the answer; render() reads its class + site attribute ("path:
1679
+ // start[-end]", seon:startsAt) for the module/line citation.
1680
+ if (shape === "where") {
1681
+ const site = (objMatch.attributes || []).find((a) => a.key === "site")?.value || null;
1682
+ return {
1683
+ matches: [objMatch], objMatch, candidates, ambiguous, matchedVia, whereShape: true, site,
1684
+ traversal: site ? `site attribute of ${objMatch.label}` : `class + defining module of ${objMatch.label}`,
1685
+ };
1686
+ }
1687
+
1688
+ // when: "when did X change" / "when was X last touched" (2026-07-02 query
1689
+ // families) — the commits whose touch edges reach X, newest commit date first
1690
+ // (mgx:commitDate, ISO-8601, so a lexical sort IS the date sort; undated
1691
+ // commits sort last and render() says so honestly). Checked BEFORE the
1692
+ // commit-as-subject flip: "when did <sha> change" asks for the commit's own
1693
+ // date, not its touched files, so a Commit object answers with itself.
1694
+ if (shape === "when") {
1695
+ const dateOf = (c) => String((c.attributes || []).find((a) => a.key === "date")?.value || "");
1696
+ let commits;
1697
+ if (objMatch.class === "Commit") {
1698
+ commits = [objMatch];
1699
+ } else {
1700
+ const edges = ["touches", "touchesSymbol"].flatMap((k) => edgesOfKind(graph, k)).filter((e) => e.object === objMatch.id);
1701
+ const seen = new Set();
1702
+ commits = [];
1703
+ for (const e of edges) {
1704
+ if (seen.has(e.subject)) continue;
1705
+ seen.add(e.subject);
1706
+ const c = graph.byId.get(e.subject);
1707
+ if (c && c.class === "Commit") commits.push(c);
1708
+ }
1709
+ commits.sort((a, b) => dateOf(b).localeCompare(dateOf(a)));
1710
+ }
1711
+ return {
1712
+ matches: commits, objMatch, candidates, ambiguous, matchedVia, whenShape: true,
1713
+ traversal: `touches+touchesSymbol edges where object = ${objMatch.label}, newest commit date first`,
1714
+ };
1715
+ }
1716
+
1717
+ // commit-as-subject flip: touches edges are stored commit -> entity, so when the
1718
+ // RESOLVED term of a touches question is itself a Commit — "which changes touch
1719
+ // commit ef74e44e25c8" (reverse), "what did commit abc1234 touch" (forward),
1720
+ // "what changed in abc1234" (casual reverse) — the honest reading is "what did
1721
+ // that commit touch": read the edges FROM the commit, grain-selected by the asked
1722
+ // entity type, instead of scanning for edges INTO it (a commit is never a touch
1723
+ // target, so the un-flipped scan would render a misleading blank).
1724
+ if (kind === "touches" && objMatch.class === "Commit") {
1725
+ return commitTouches(graph, objMatch, entityType, { candidates, ambiguous, matchedVia });
1726
+ }
1727
+
1728
+ if (shape === "forward") {
1729
+ const edges = kindsFor(kind).flatMap((k) => edgesOfKind(graph, k)).filter((e) => e.subject === objMatch.id);
1730
+ const matches = edges.map((e) => graph.byId.get(e.object)).filter(Boolean);
1731
+ return { matches, objMatch, candidates, traversal: `${kindsFor(kind).join("+")} edges where subject = ${objMatch.label}`, ambiguous, matchedVia };
1732
+ }
1733
+
1734
+ // reverse + transitive (PLAN_MECHANICAL_CHAT.md P1): the gate above guarantees kind is
1735
+ // "imports" or "calls" and entityType is null/"Module" here. Reuses impactClosure
1736
+ // (codegraph.mjs) AS-IS rather than reimplementing a closure — impactClosure's own
1737
+ // dependents map is a REVERSE closure over imports+calls edges TOGETHER (renderImpact's
1738
+ // "what would break" framing), not a strict single-predicate chain, so a query for
1739
+ // "transitively imports" and one for "transitively calls" both resolve to the SAME
1740
+ // mixed reverse-dependency closure. That's a real, deliberate scope decision (matching
1741
+ // the plan's own instruction to wire onto "renderImpact's existing closure traversal"
1742
+ // rather than build a new predicate-pure one) — the traversal receipt below says so
1743
+ // honestly rather than implying a narrower single-predicate result than what was
1744
+ // actually computed.
1745
+ if (parsed.modifier === "transitive") {
1746
+ const levels = impactClosure(graph, objMatch, { maxDepth: TRANSITIVE_MAX_DEPTH });
1747
+ const matches = levels.flat().map((d) => graph.byId.get(d.id)).filter(Boolean);
1748
+ return {
1749
+ matches, objMatch, candidates, ambiguous, matchedVia,
1750
+ traversal: `reverse dependency closure over imports+calls edges from ${objMatch.label} (impactClosure, maxDepth=${TRANSITIVE_MAX_DEPTH})`,
1751
+ };
1752
+ }
1753
+
1754
+ // reverse: "which <entityType> R <objMatch>"
1755
+ const symbolKind = SYMBOL_GRAIN_SIBLING[kind];
1756
+ if (symbolKind && FINE_ENTITY_TYPES.has(entityType)) {
1757
+ const edges = edgesOfKind(graph, symbolKind).filter((e) => e.object === objMatch.id);
1758
+ const matches = edges.map((e) => graph.byId.get(e.subject)).filter((i) => i && i.class === entityType);
1759
+ return { matches, objMatch, candidates, traversal: `${symbolKind} edges where object = ${objMatch.label}`, ambiguous, matchedVia };
1760
+ }
1761
+
1762
+ // General case: some predicates are already fine-grained (inherits: Class->Class, contains:
1763
+ // Class->Member) and some are module-coarse (imports/calls/tests/cochange: Module->Module).
1764
+ // Rather than assume one or the other, check what the edge's actual subjects ARE: if they
1765
+ // already match the requested entityType, use them directly (inherits); only when they're
1766
+ // Module individuals and a FINER entityType was asked for do we refine via `defines`
1767
+ // (imports) — never blindly treat an edge's subject id as if it were always a module id.
1768
+ let edges = kindsFor(kind).flatMap((k) => edgesOfKind(graph, k)).filter((e) => e.object === objMatch.id);
1769
+ let extNote = "";
1770
+ if (!edges.length && objMatch.class) {
1771
+ // Unresolved ext:<Name> endpoints with the SAME name as the resolved entity:
1772
+ // the extractor declined to assert identity (e.g. commander's every "class X
1773
+ // extends Command" edge points at ext:Command, never the Class node), so a
1774
+ // strict id match renders a FALSE blank. Count them by NAME instead and say
1775
+ // so in the receipt — name-grade evidence, labeled as such, same standard as
1776
+ // resolveObject's own ext: tier.
1777
+ const extId = `ext:${String(objMatch.label).toLowerCase()}`;
1778
+ edges = kindsFor(kind).flatMap((k) => edgesOfKind(graph, k)).filter((e) => String(e.object).toLowerCase() === extId);
1779
+ if (edges.length) extNote = ` (by name, via unresolved ${extId} references)`;
1780
+ }
1781
+ // dedupe by id: a union kind ("uses") can reach the same subject through two
1782
+ // legs (a module that both imports AND calls X), and one answer must list it once.
1783
+ const subjects = [];
1784
+ const seenSubjects = new Set();
1785
+ for (const e of edges) {
1786
+ const s = graph.byId.get(e.subject);
1787
+ if (s && !seenSubjects.has(s.id)) { seenSubjects.add(s.id); subjects.push(s); }
1788
+ }
1789
+ let matches, grainNote = "";
1790
+ // "Change" (ask-vocab.mjs's pseudo-type) is a wildcard here: "which changes touch
1791
+ // walk.mjs" means the touch edges' own subjects — the commits — not a node class
1792
+ // to filter by (no individual is ever class "Change", so filtering would always
1793
+ // produce a false blank).
1794
+ if (!entityType || entityType === "Change") {
1795
+ matches = subjects;
1796
+ } else {
1797
+ const direct = subjects.filter((s) => s.class === entityType);
1798
+ if (direct.length) {
1799
+ matches = direct;
1800
+ } else if (entityType !== "Module" && subjects.some((s) => s.class === "Module")) {
1801
+ const moduleIds = new Set(subjects.filter((s) => s.class === "Module").map((s) => s.id));
1802
+ matches = refineToEntities(graph, moduleIds, entityType);
1803
+ grainNote = `, then ${entityType} defined in the matched module(s)`;
1804
+ } else {
1805
+ matches = [];
1806
+ }
1807
+ }
1808
+ return { matches, objMatch, candidates, traversal: `${kindsFor(kind).join("+")} edges where object = ${objMatch.label}${extNote}${grainNote}`, ambiguous, matchedVia };
1809
+ }
1810
+
1811
+ // ---- §5 templated renderer — string interpolation + grouping/pluralization/overflow rules,
1812
+ // never generation; every sentence is read off a matched edge/individual. ----
1813
+
1814
+ function moduleLabelOf(ind) {
1815
+ if (ind.class === "Module") return ind.label;
1816
+ const site = (ind.attributes || []).find((a) => a.key === "site")?.value;
1817
+ if (site) return String(site).split(":")[0];
1818
+ const m = String(ind.id || "").match(/^fn:(.+)#/);
1819
+ return m ? m[1] : "(unknown module)";
1820
+ }
1821
+
1822
+ function symbolLabelOf(ind) {
1823
+ const label = String(ind.label || ind.id || "");
1824
+ return ["Function", "Method"].includes(ind.class) ? `function ${label}()` : label;
1825
+ }
1826
+
1827
+ function listJoin(syms) {
1828
+ return syms.length > 1 ? `${syms.slice(0, -1).join(", ")} and ${syms[syms.length - 1]}` : syms[0];
1829
+ }
1830
+
1831
+ /** One-line, honest rephrasing of a candidate parse — used to describe a
1832
+ * parse-level disagreement between strategies without pretending to pick
1833
+ * a winner. Template only, reads straight off the parsed fields. */
1834
+ function describeParse(p) {
1835
+ const obj = p.object ?? p.subject ?? "?";
1836
+ const ent = p.entityType ? nounFor(p.entityType, 2) + " that " : "";
1837
+ return `${ent}${p.kind} "${obj}"`;
1838
+ }
1839
+
1840
+ /** Render a compiled query result into {content, miss, ambiguous, matches?, candidates?}.
1841
+ * Every branch is a template, not generation — §5's grouping/pluralization/overflow rules.
1842
+ * A tier-5 fuzzy object resolution is ANNOUNCED, not silent: the answer is prefixed
1843
+ * "assuming you meant <label>:" so the correction is on the record next to the result
1844
+ * (an unannounced fuzzy hit would be indistinguishable from an exact one — a guess). */
1845
+ export function render(parsed, result) {
1846
+ const r = renderCore(parsed, result);
1847
+ if (result && result.matchedVia === "fuzzy" && result.objMatch && !r.ambiguous) {
1848
+ r.content = `assuming you meant ${result.objMatch.label}: ${r.content}`;
1849
+ }
1850
+ return r;
1851
+ }
1852
+
1853
+ function renderCore(parsed, result) {
1854
+ if (!parsed) {
1855
+ return { content: `couldn't parse this as a graph question. Try: ${rephraseHint()}`, miss: true, ambiguous: false };
1856
+ }
1857
+ if (parsed.node) return renderComposite(parsed, result);
1858
+ if (parsed.ambiguousParse) {
1859
+ const options = parsed.candidates.map((p, i) => `${i + 1}) ${describeParse(p)}`).join(" or ");
1860
+ return {
1861
+ content: `this could mean more than one thing: ${options} — try rephrasing more specifically.`,
1862
+ miss: false, ambiguous: true, candidates: parsed.candidates.map(describeParse),
1863
+ };
1864
+ }
1865
+ if (result.unresolvedPronoun) {
1866
+ return {
1867
+ content: `"${parsed.object ?? parsed.subject}" needs a selected node to refer to — click a node first, or name it directly.`,
1868
+ miss: true, ambiguous: false,
1869
+ };
1870
+ }
1871
+ if (result.unsupportedModifier) {
1872
+ return {
1873
+ content: `the "${parsed.modifier}" modifier isn't supported for "${parsed.kind}" queries yet — only imports/calls (module-level) have a transitive closure today.`,
1874
+ miss: true, ambiguous: false,
1875
+ };
1876
+ }
1877
+ if (parsed.shape === "meta") {
1878
+ if (!result.objMatch) {
1879
+ return {
1880
+ content: `"${parsed.object}" isn't a term in this graph's own vocabulary (no matching class or predicate).`,
1881
+ miss: true, ambiguous: false,
1882
+ };
1883
+ }
1884
+ const doc = (result.objMatch.attributes || []).find((a) => a.key === "doc")?.value || "";
1885
+ const kindWord = result.objMatch.class === "SchemaClass" ? "a class in the graph's schema" : "a predicate (relation) in the graph's schema";
1886
+ return { content: `${result.objMatch.label} is ${kindWord}: ${doc}`, miss: false, ambiguous: false, matches: result.matches };
1887
+ }
1888
+ // mentions: the prose surface — checked before the generic objMatch-null miss
1889
+ // below, because a mentions result deliberately carries no resolved object
1890
+ // (the question is about the term's words, not a graph entity).
1891
+ if (result.mentionsShape) {
1892
+ if (!result.matches.length) {
1893
+ return {
1894
+ content: `"${parsed.object}" is not mentioned in any indexed identifier or doc-comment prose. (traversal: ${result.traversal})`,
1895
+ miss: true, ambiguous: false,
1896
+ };
1897
+ }
1898
+ const shown = result.matches.slice(0, OVERFLOW_CAP).map((m) => `${m.label} (${nounFor(m.class, 1)})`);
1899
+ const extra = result.matches.length > OVERFLOW_CAP ? `, …and ${result.matches.length - OVERFLOW_CAP} more` : "";
1900
+ return {
1901
+ content: `"${parsed.object}" is mentioned in the prose tokens of ${listJoin(shown)}${extra}.`,
1902
+ miss: false, ambiguous: false, matches: result.matches,
1903
+ };
1904
+ }
1905
+ if (!result.objMatch && (!result.candidates || result.candidates.length === 0) && parsed.shape !== "ask") {
1906
+ // name what kind of thing was looked for: a sha-shaped term was checked against
1907
+ // the commit namespace, a dotted slash-free term against symbol labels — a
1908
+ // generic "no module matching" would misreport both.
1909
+ const objText = String(parsed.object || "").trim();
1910
+ const what = /^(?:commit[:\s])?[0-9a-f]{7,40}$/i.test(objText) ? "commit"
1911
+ : (!objText.includes("/") && /^[\w$]+(\.[\w$]+)+$/.test(objText) ? "symbol" : "module");
1912
+ return {
1913
+ content: `no ${what} matching "${parsed.object}" found in the index.`,
1914
+ miss: true, ambiguous: false, candidates: [],
1915
+ };
1916
+ }
1917
+ if (result.ambiguous) {
1918
+ // the candidates say what KIND of thing is ambiguous — a shared commit-sha
1919
+ // prefix must read "more than one commit", not "module".
1920
+ const pool = [result.objMatch, ...(result.candidates || [])].filter(Boolean);
1921
+ const noun = pool.length && pool.every((i) => i.class === "Commit") ? "commit" : "module";
1922
+ return {
1923
+ content: `"${parsed.object}" matches more than one ${noun} ambiguously — please narrow the term.`,
1924
+ miss: false, ambiguous: true, candidates: pool.map((i) => i.label),
1925
+ };
1926
+ }
1927
+ // where: the resolved entity's own location, cited off the site attribute.
1928
+ if (result.whereShape) {
1929
+ const ind = result.objMatch;
1930
+ if (ind.class === "Module") {
1931
+ return { content: `${ind.label} is a module — the label is its repo path.`, miss: false, ambiguous: false, matches: result.matches };
1932
+ }
1933
+ if (ind.class === "Commit") {
1934
+ return { content: `${ind.label} is a commit, not a code location — try "what did commit ${ind.label} touch".`, miss: true, ambiguous: false };
1935
+ }
1936
+ const m = String(result.site || "").match(/^(.*):(\d+)(?:-(\d+))?$/);
1937
+ if (m) {
1938
+ const lines = m[3] && m[3] !== m[2] ? `lines ${m[2]}-${m[3]}` : `line ${m[2]}`;
1939
+ return { content: `${symbolLabelOf(ind)} is defined in ${m[1]} at ${lines}.`, miss: false, ambiguous: false, matches: result.matches };
1940
+ }
1941
+ return {
1942
+ content: `${symbolLabelOf(ind)} is defined in ${moduleLabelOf(ind)} (no line span recorded in this index).`,
1943
+ miss: false, ambiguous: false, matches: result.matches,
1944
+ };
1945
+ }
1946
+ // when: newest touching commit + its date; undated commits are said out loud
1947
+ // (honest miss with the precise re-index hint), never silently skipped.
1948
+ if (result.whenShape) {
1949
+ const subject = result.objMatch.label;
1950
+ if (!result.matches.length) {
1951
+ return { content: `no recorded commit touches ${subject} in this index. (traversal: ${result.traversal})`, miss: true, ambiguous: false };
1952
+ }
1953
+ const newest = result.matches[0];
1954
+ const date = (newest.attributes || []).find((a) => a.key === "date")?.value || "";
1955
+ if (!date) {
1956
+ return {
1957
+ content: `commit ${newest.label} touched ${subject}, but this index records no commit dates — regenerate the graph to attach mgx:commitDate.`,
1958
+ miss: true, ambiguous: false,
1959
+ };
1960
+ }
1961
+ const msg = (newest.attributes || []).find((a) => a.key === "message")?.value || "";
1962
+ const day = String(date).slice(0, 10);
1963
+ if (newest.id === result.objMatch.id) {
1964
+ return { content: `commit ${newest.label} is dated ${day}${msg ? ` ("${msg}")` : ""}.`, miss: false, ambiguous: false, matches: result.matches };
1965
+ }
1966
+ const more = result.matches.length - 1;
1967
+ return {
1968
+ content: `${subject} was last touched by commit ${newest.label} on ${day}${msg ? ` ("${msg}")` : ""}${more ? `; ${more} earlier commit${more === 1 ? "" : "s"} recorded` : ""}.`,
1969
+ miss: false, ambiguous: false, matches: result.matches,
1970
+ };
1971
+ }
1972
+ // commit-as-subject answers ("which changes touch commit X", "what did commit X
1973
+ // touch"): cite the commit, group the touched entities by CLASS — modules and
1974
+ // symbols are different grains of the same answer, and flattening them into one
1975
+ // undifferentiated list would hide which is which. Same OVERFLOW_CAP as the
1976
+ // other list templates; zero hits is the standard honest blank, commit cited.
1977
+ if (result.commitSubject) {
1978
+ const cite = `commit ${result.objMatch.label}`;
1979
+ if (!result.matches.length) {
1980
+ return {
1981
+ content: `${cite} touched nothing recorded in the index. (traversal: ${result.traversal})`,
1982
+ miss: true, ambiguous: false,
1983
+ };
1984
+ }
1985
+ const byClass = new Map();
1986
+ for (const m of result.matches.slice(0, OVERFLOW_CAP)) {
1987
+ const cls = m.class || "Module";
1988
+ if (!byClass.has(cls)) byClass.set(cls, []);
1989
+ byClass.get(cls).push(["Function", "Method"].includes(cls) ? `${m.label}()` : m.label);
1990
+ }
1991
+ const clauses = [...byClass.entries()].map(([cls, labels]) => `${nounFor(cls, labels.length)} ${listJoin(labels)}`);
1992
+ const extra = result.matches.length > OVERFLOW_CAP ? `; …and ${result.matches.length - OVERFLOW_CAP} more` : "";
1993
+ return { content: `${cite} touched ${clauses.join("; ")}${extra}.`, miss: false, ambiguous: false, matches: result.matches };
1994
+ }
1995
+ if (parsed.shape === "ask") {
1996
+ if (!result.objMatch || !result.subjMatch) {
1997
+ return { content: `couldn't resolve one of the terms in this question.`, miss: true, ambiguous: false };
1998
+ }
1999
+ return {
2000
+ content: result.answer ? `Yes. (${result.traversal})` : `No — no ${parsed.kind} edge found from ${result.subjMatch.label} to ${result.objMatch.label}.`,
2001
+ miss: !result.answer, ambiguous: false,
2002
+ };
2003
+ }
2004
+ if (!result.matches.length) {
2005
+ // forward: parsed.object is the GIVEN subject ("what does X import" -> X), not a
2006
+ // search target — "No modules found that X." reads as broken grammar (and X's own
2007
+ // relation edges are simply absent, not "not found"), so this shape gets its own,
2008
+ // subject-first phrasing rather than reusing reverse's "found ... that OBJECT" template.
2009
+ if (parsed.shape === "forward") {
2010
+ return {
2011
+ content: `${result.objMatch.label} has no ${parsed.kind} edges in the index. (traversal: ${result.traversal || "no traversal resolved"})`,
2012
+ miss: true, ambiguous: false,
2013
+ };
2014
+ }
2015
+ const entityWord = nounFor(parsed.entityType || "Module", 2);
2016
+ return {
2017
+ content: `No ${entityWord} found whose module directly ${verbFor(parsed.kind)} ${parsed.object}. (traversal: ${result.traversal || "no traversal resolved"})`,
2018
+ miss: true, ambiguous: false,
2019
+ };
2020
+ }
2021
+ // Route by the MATCHED entities' actual class, not just the parsed hint — a reverse
2022
+ // query phrased without an explicit entity keyword ("what imports X", entityType null)
2023
+ // still resolves to Module individuals for a module-level relation like "imports", and
2024
+ // grouping those by-module (module label as its own "symbol" label) reads as nonsense
2025
+ // ("in a.mjs there is a.mjs"). The fine-grained per-symbol grouping below is only
2026
+ // meaningful when the matches are sub-module entities (functions/classes/etc) — a
2027
+ // Commit list ("which commits touched X") has no containing module to group by, so
2028
+ // anything that is not a fine entity takes the flat join.
2029
+ if (parsed.shape === "forward" || parsed.entityType === "Module" || result.matches.every((m) => !FINE_ENTITY_TYPES.has(m.class))) {
2030
+ const shown = result.matches.slice(0, OVERFLOW_CAP).map((m) => m.label);
2031
+ const extra = result.matches.length > OVERFLOW_CAP ? `, …and ${result.matches.length - OVERFLOW_CAP} more` : "";
2032
+ return { content: shown.join(" and ") + extra + ".", miss: false, ambiguous: false, matches: result.matches };
2033
+ }
2034
+ // reverse, fine-grained entity: group by module, one clause per module (§5 grouping rule) —
2035
+ // the FIRST module states "in {module} there is …"; each SUBSEQUENT module states
2036
+ // "there is … in {module}" (module trails, not leads), matching the plan's worked example.
2037
+ const byModule = new Map();
2038
+ for (const m of result.matches.slice(0, OVERFLOW_CAP)) {
2039
+ const mod = moduleLabelOf(m);
2040
+ if (!byModule.has(mod)) byModule.set(mod, []);
2041
+ byModule.get(mod).push(symbolLabelOf(m));
2042
+ }
2043
+ const clauses = [...byModule.entries()].map(([mod, syms], i) => {
2044
+ const list = listJoin(syms);
2045
+ return i === 0 ? `in ${mod} there is ${list}` : `there is ${list} in ${mod}`;
2046
+ });
2047
+ const extra = result.matches.length > OVERFLOW_CAP ? ` …and ${result.matches.length - OVERFLOW_CAP} more` : "";
2048
+ return { content: clauses.join(" and ") + extra + ".", miss: false, ambiguous: false, matches: result.matches };
2049
+ }
2050
+
2051
+ // ============================================================================
2052
+ // §progressive-relaxation cascade (SHRDLU in a code graph, with a Zork parser's
2053
+ // forgiveness) — a controlled loop that wraps the WHOLE existing parse and runs
2054
+ // ONLY when the direct parse of the normalized query would MISS. A clean direct hit
2055
+ // never enters the cascade (it stays instant and exact); the cascade only ever DROPS
2056
+ // noise/unmatched words or NORMALISES a near-canonical word to the closed vocabulary,
2057
+ // re-attempting the full parse (compositional + templates + keyword-spot) after each
2058
+ // transform, and bottoms out in the SAME honest miss + rephrase hint the engine
2059
+ // already returned — never inventing a term or guessing an entity. Deterministic:
2060
+ // same input → same cascade path. All of it is plain JS over the already-imported
2061
+ // tables + resolveObject/parseQuery, so it survives the viewer bundle's import strip.
2062
+ // ============================================================================
2063
+
2064
+ const wordsOf = (arr) => arr.flatMap((p) => String(p).toLowerCase().split(" "));
2065
+
2066
+ /** Every token the CLOSED grammar gives QUERY MEANING to — relation verbs, entity
2067
+ * nouns, modifiers, qualifiers, aggregate/superlative triggers, edge-degree nouns,
2068
+ * boolean connectives, placeholder nouns, anaphora/meta/where/mention markers,
2069
+ * relative pronouns, and the small synonym keys. The noise-strip pass will NEVER
2070
+ * remove one of these, and the drop-unmatched pass always keeps them: they carry the
2071
+ * intent, only the packaging around them is negotiable. */
2072
+ const CONTENT_VOCAB = new Set([
2073
+ ...wordsOf(Object.keys(VERB_TO_KIND)), ...wordsOf(Object.keys(ENTITY_TO_TYPE)),
2074
+ ...wordsOf(Object.keys(MODIFIER_TO_KIND)), ...wordsOf(Object.keys(QUALIFIERS)),
2075
+ ...wordsOf(AGGREGATE_TRIGGERS), ...wordsOf(Object.keys(SUPERLATIVE_EXTREMES)),
2076
+ ...wordsOf(Object.keys(EDGE_NOUN_TO_METRIC)), ...wordsOf(Object.keys(BOOLEAN_CONNECTIVES)),
2077
+ ...wordsOf(PLACEHOLDER_NOUNS), ...wordsOf(ANAPHORA_TRIGGERS), ...wordsOf(META_MEANING_VERBS),
2078
+ ...wordsOf(WHERE_MARKERS), ...wordsOf(MENTION_MARKERS), ...wordsOf(RELATIVE_PRONOUNS),
2079
+ ...wordsOf(Object.keys(CASCADE_SYNONYMS)),
2080
+ ]);
2081
+
2082
+ /** Structural scaffolding words — question words, articles-in-questions, frame verbs,
2083
+ * and context pronouns. Not "content", but they hold a sentence together, so the
2084
+ * drop-unmatched pass keeps them (dropping "what"/"of" would corrupt the grammar);
2085
+ * the noise-strip pass may still remove the few of these that are ALSO curated noise
2086
+ * ("the"/"a"/"show"/"me") — the two sets overlap on purpose. */
2087
+ const STRUCTURAL_WORDS = new Set([...STOPWORDS, ...FRAME_WORDS, ...CONTEXT_PRONOUNS]);
2088
+ const CASCADE_NOISE_SET = new Set(wordsOf(CASCADE_NOISE));
2089
+ /** Every token that carries NO graph meaning of its own — curated noise (articles,
2090
+ * politeness, vocatives, presentation frames) PLUS the structural scaffolding
2091
+ * (question words, context pronouns). The bare-kind-noun terminal rule (relaxParse's
2092
+ * Layer 4) treats a query as "just a kind noun wrapped in packaging" only when every
2093
+ * non-kind token is one of these — so an unknown qualifier ("shiny") or a relation
2094
+ * verb, being neither, still blocks the default and preserves the honest miss. */
2095
+ const NOISE_OR_SCAFFOLD = new Set([...CASCADE_NOISE_SET, ...STRUCTURAL_WORDS]);
2096
+
2097
+ /** The aggregate/list TRIGGER words the cascade's drop-unmatched pass will fuzzy-correct
2098
+ * a typo toward (Gap 2, trigger-typo work). Curated (not derived from LIST_TRIGGERS'
2099
+ * multi-word phrases) so the target set stays clean single verbs — "many", "count",
2100
+ * "list", "show", … — and never drags in a stray "down"/"off"/"out" from a phrasal
2101
+ * trigger that would mis-correct an unrelated token. */
2102
+ const TRIGGER_FUZZY_WORDS = [
2103
+ "many", "count", "number", "quantity", "total", "tally",
2104
+ "list", "show", "display", "print", "dump", "enumerate", "name",
2105
+ ];
2106
+ /** Closed-vocab words a plain unknown may be fuzzy-corrected TOWARD before the cascade
2107
+ * discards it: relation verbs, entity kind nouns, and the aggregate/list triggers. A
2108
+ * correction fires only on a token already bound for the drop pile (grammar doesn't own
2109
+ * it, no entity resolves) and only for a UNIQUE within-bound target, so it strictly
2110
+ * beats dropping — a typo of a trigger keeps its intent instead of being lost. Excludes
2111
+ * STOPWORDS/structural words (a random unknown must never bend into "what"/"the") and
2112
+ * <4-char words (at the small bound they match half of English). */
2113
+ const CASCADE_FUZZY_TARGETS = [...new Set([
2114
+ ...wordsOf(Object.keys(VERB_TO_KIND)),
2115
+ ...Object.keys(ENTITY_TO_TYPE),
2116
+ ...TRIGGER_FUZZY_WORDS,
2117
+ ])].filter((wd) => /^[a-z]+$/.test(wd) && wd.length >= 4 && !STOPWORDS.has(wd));
2118
+
2119
+ /** UNIQUE within-bound fuzzy correction of `w` toward CASCADE_FUZZY_TARGETS, or null —
2120
+ * a distance tie between two distinct targets is refused (honest-miss discipline at the
2121
+ * vocabulary level, cf. fuzzyVocabWord). */
2122
+ function fuzzyCascadeWord(w) {
2123
+ const bound = fuzzyBound(w);
2124
+ let best = bound + 1; let hit = null; let tied = false;
2125
+ for (const target of CASCADE_FUZZY_TARGETS) {
2126
+ const d = editDistance(w, target, Math.min(best, bound));
2127
+ if (d < best) { best = d; hit = target; tied = false; }
2128
+ else if (d === best && d <= bound && target !== hit) tied = true;
2129
+ }
2130
+ return best <= bound && !tied ? hit : null;
2131
+ }
2132
+
2133
+ /** Is `parsed` a genuinely ANSWERABLE query — one that both parsed AND (for the simple
2134
+ * clauses) resolves its named term(s) to a graph entity? A composite non-miss node,
2135
+ * an ambiguous parse, and a meta/mentions surface all count; an unresolved-context
2136
+ * pronoun is its OWN specific honest miss (kept, not relaxed). Returns:
2137
+ * true — a real, executable answer (even if it later renders an empty set / "No")
2138
+ * "ambiguous"/"pronoun" — a specific outcome to keep, distinct from relaxable
2139
+ * false — no parse at all, a compositional {node:"miss"}, or an unresolved term
2140
+ * ask() starts the cascade ONLY on `false`, and accepts a relaxed attempt ONLY on the
2141
+ * strict `true` (so the cascade can never "rescue" a query into another kind of miss). */
2142
+ function answerable(graph, parsed, contextId) {
2143
+ if (!parsed) return false;
2144
+ if (parsed.ambiguousParse) return "ambiguous";
2145
+ if (parsed.node) return parsed.node !== "miss";
2146
+ if (parsed.shape === "meta" || parsed.shape === "mentions") return true;
2147
+ const o = resolveTermOrContext(graph, parsed.object, contextId);
2148
+ if (o.unresolvedPronoun) return "pronoun";
2149
+ if (!o.match) return false;
2150
+ if (parsed.shape === "ask") {
2151
+ const s = resolveTermOrContext(graph, parsed.subject, contextId);
2152
+ if (s.unresolvedPronoun) return "pronoun";
2153
+ return s.match ? true : false;
2154
+ }
2155
+ return true;
2156
+ }
2157
+
2158
+ /** Whole-query help/orientation request → show the hint directly (never the relaxation
2159
+ * loop, never a pretend answer). Matches only when the ENTIRE normalized query is a
2160
+ * curated HELP_TRIGGER, so a symbol named "help" in a real question is untouched. */
2161
+ function isHelpRequest(query) {
2162
+ const q = String(query || "").trim().toLowerCase().replace(/[?.!\s]+$/, "");
2163
+ return HELP_TRIGGERS.includes(q);
2164
+ }
2165
+
2166
+ /** The relaxation cascade. Given a query whose direct parse MISSED, walk three
2167
+ * increasingly-permissive layers, re-attempting the full parse after each transform,
2168
+ * and return the FIRST attempt that yields an answerable parse (with a trace of what
2169
+ * it did) — or null if none does (caller then falls back to the original honest miss):
2170
+ * 1. NOISE-STRIP — remove one curated noise token (leftmost) at a time, never one
2171
+ * that is content vocab or resolves to an entity, re-parsing each
2172
+ * time, until a parse answers or no noise tokens remain.
2173
+ * 2. DROP-UNMATCHED — drop the plain-lowercase words that are NEITHER grammar NOR a
2174
+ * resolvable entity (an unknown "frobnicate"); identifier-shaped
2175
+ * tokens (dotted files, shas, CamelCase) are never dropped —
2176
+ * they are content that may honestly fail to resolve.
2177
+ * 3. SYNONYM — rewrite surviving near-canonical words to the closed vocab.
2178
+ * Bounded (one token removed per noise iteration; hard guard) and deterministic. */
2179
+ export function relaxParse(graph, query, { nlp = undefined, contextId = null, prev = null } = {}) {
2180
+ const from = applyNegationFrames(normalizeQuery(String(query || "")));
2181
+ let tokens = splitWords(from);
2182
+ if (!tokens.length) return null;
2183
+ const dropped = [];
2184
+ const steps = [];
2185
+
2186
+ // Two literal-resolution guards (never the fuzzy/prose tiers, whose loose near-
2187
+ // matches would let a noise word masquerade as an entity):
2188
+ // · resolvesExact — EXACT label/id or ext: match only (tier ≤ 2). Used to protect a
2189
+ // curated NOISE word from being stripped: only a token that literally NAMES an
2190
+ // entity ("a module called `the`") is safe-listed. A mere substring coincidence
2191
+ // ("me" ⊂ "Method", tier 3) must NOT block stripping a genuine filler word.
2192
+ // · resolvesLiteral — exact/ext/substring/component (tier ≤ 3). Used to KEEP an
2193
+ // identifier-shaped content token ("logging" ⊂ "src/logging.mjs") through the
2194
+ // drop-unmatched pass, and to hold a synonym rewrite off a real entity name.
2195
+ const resolvesExact = (t) => {
2196
+ const r = resolveObject(graph, t);
2197
+ return !!r.match && r.tier != null && r.tier <= 2;
2198
+ };
2199
+ const resolvesLiteral = (t) => {
2200
+ const r = resolveObject(graph, t);
2201
+ return !!r.match && r.tier != null && r.tier <= 3;
2202
+ };
2203
+ // Does a term string carry at least one REAL word — one the grammar doesn't already
2204
+ // own as vocabulary/scaffolding? Guards against a relaxation that drops the actual
2205
+ // asked term and lets a bare marker slide into its place ("where is [X] defined" →
2206
+ // "where is defined", "defined" is a WHERE_MARKER, never the thing being located).
2207
+ const hasRealTerm = (s) => splitWords(String(s || "")).some((w) => {
2208
+ const lc = w.toLowerCase();
2209
+ return !CONTENT_VOCAB.has(lc) && !STRUCTURAL_WORDS.has(lc);
2210
+ });
2211
+ // Accept a relaxed attempt ONLY if it is a genuinely answerable parse (terms resolve)
2212
+ // AND it renders a REAL positive answer — never another empty/miss (relaxation earns a
2213
+ // win only by turning a miss into an answer, never a differently-worded miss) — and
2214
+ // never by promoting a bare marker to the asked term.
2215
+ const TERM_SHAPES = new Set(["reverse", "forward", "where", "when", "ask"]);
2216
+ const attempt = (toks) => {
2217
+ const text = toks.join(" ");
2218
+ const p = parseQuery(text, { nlp });
2219
+ if (answerable(graph, p, contextId) !== true) return null;
2220
+ if (p && !p.node && TERM_SHAPES.has(p.shape)) {
2221
+ if (p.object != null && !hasRealTerm(p.object)) return null;
2222
+ if (p.shape === "ask" && p.subject != null && !hasRealTerm(p.subject)) return null;
2223
+ }
2224
+ const rendered = render(p, traverse(graph, p, { contextId, prev }));
2225
+ return rendered.miss ? null : { parsed: p, text };
2226
+ };
2227
+ const done = (hit) => ({ parsed: hit.parsed, from, to: hit.text, dropped: [...dropped], steps });
2228
+
2229
+ // Layer 1 — NOISE-STRIP (one lowest-value token at a time)
2230
+ let guard = 0;
2231
+ const hardCap = Math.max(tokens.length, 1) + 12;
2232
+ for (; guard < hardCap; guard += 1) {
2233
+ let idx = -1;
2234
+ for (let i = 0; i < tokens.length; i += 1) {
2235
+ const lc = tokens[i].toLowerCase();
2236
+ if (CASCADE_NOISE_SET.has(lc) && !CONTENT_VOCAB.has(lc) && !resolvesExact(tokens[i])) { idx = i; break; }
2237
+ }
2238
+ if (idx < 0) break;
2239
+ const removed = tokens[idx];
2240
+ tokens = tokens.filter((_, i) => i !== idx);
2241
+ dropped.push(removed);
2242
+ steps.push(`strip noise "${removed}" → "${tokens.join(" ")}"`);
2243
+ const hit = attempt(tokens);
2244
+ if (hit) return done(hit);
2245
+ }
2246
+
2247
+ // Layer 2 — DROP-UNMATCHED (plain-lowercase unknowns beside the real terms)
2248
+ const survivors = [];
2249
+ const nowDropped = [];
2250
+ const corrected = [];
2251
+ for (const t of tokens) {
2252
+ const lc = t.toLowerCase();
2253
+ const plain = /^[a-z]+$/.test(lc);
2254
+ if (!plain || CONTENT_VOCAB.has(lc) || STRUCTURAL_WORDS.has(lc) || resolvesLiteral(t)) {
2255
+ survivors.push(t);
2256
+ continue;
2257
+ }
2258
+ // Gap 2 — before dropping an unmatched plain token, try a bounded fuzzy-correct to a
2259
+ // UNIQUE closed-vocab word (verbs, entity kinds, aggregate/list triggers): a typo of
2260
+ // a TRIGGER ("manyn"→"many", "coutn"→"count", "liist"→"list") is restored, not
2261
+ // discarded, so the count/list intent survives. Only a unique within-bound hit; else
2262
+ // the token is genuinely unrecoverable and drops exactly as before.
2263
+ const fix = fuzzyCascadeWord(lc);
2264
+ if (fix && fix !== lc) { survivors.push(fix); corrected.push(`${t}→${fix}`); continue; }
2265
+ nowDropped.push(t);
2266
+ }
2267
+ if ((corrected.length || nowDropped.length) && survivors.length) {
2268
+ tokens = survivors;
2269
+ dropped.push(...nowDropped);
2270
+ if (corrected.length) steps.push(`fuzzy-correct ${JSON.stringify(corrected)} → "${tokens.join(" ")}"`);
2271
+ if (nowDropped.length) steps.push(`drop unmatched ${JSON.stringify(nowDropped)} → "${tokens.join(" ")}"`);
2272
+ const hit = attempt(tokens);
2273
+ if (hit) return done(hit);
2274
+ }
2275
+
2276
+ // Layer 3 — SYNONYM-NORMALISE the survivors onto the canonical vocabulary
2277
+ let changed = false;
2278
+ const normed = tokens.map((t) => {
2279
+ const lc = t.toLowerCase();
2280
+ if (CASCADE_SYNONYMS[lc] && !resolvesLiteral(t)) { changed = true; return CASCADE_SYNONYMS[lc]; }
2281
+ return t;
2282
+ });
2283
+ if (changed) {
2284
+ steps.push(`normalise synonyms → "${normed.join(" ")}"`);
2285
+ const hit = attempt(normed);
2286
+ if (hit) return done(hit);
2287
+ }
2288
+
2289
+ // Layer 4 (terminal) — BARE KIND NOUN → a bounded DEFAULT ACTION. When noise-strip,
2290
+ // drop-unmatched and synonym-normalise have all failed to yield an answerable parse,
2291
+ // give the operator's "vague enough to land" case a sensible answer instead of an
2292
+ // honest miss: a query that is ONLY a kind noun (class/classes, function/functions,
2293
+ // module, method, attribute, variable, commit, …) wrapped in articles/noise/question
2294
+ // words DEFAULTS TO A COUNT of that kind ("the classes" / "classes" / "tell me the
2295
+ // classes" → "20 classes."). Count, not list: a bare unscoped list of 647 functions is
2296
+ // noise, whereas the count is the cheap useful answer the asker can then drill into
2297
+ // ("list them"). Deterministic — count for every kind, no cardinality cap.
2298
+ //
2299
+ // We classify the ORIGINAL normalized tokens (`from`), NOT the layer-mutated `tokens`:
2300
+ // drop-unmatched has by now EATEN any unknown qualifier, so "the shiny classes" would
2301
+ // otherwise look identical to a bare "classes". Reading the whole phrase keeps the
2302
+ // discipline exact — the rule fires ONLY when every non-kind token is pure packaging
2303
+ // (NOISE_OR_SCAFFOLD). A dangling unknown qualifier ("the shiny classes"), a relation
2304
+ // verb, a marker, or a real term is neither noise nor a kind noun, so it lands in
2305
+ // `others`, blocks the default, and the honest miss (or the real compositional query,
2306
+ // if a lower layer already rescued it) stands.
2307
+ const bareLc = splitWords(from).map((t) => t.toLowerCase());
2308
+ const kindWords = [];
2309
+ const others = [];
2310
+ for (const t of bareLc) {
2311
+ if (NOISE_OR_SCAFFOLD.has(t)) continue;
2312
+ // real entity kinds only — "change"/"changes" is ask-vocab's pseudo-type (never a
2313
+ // node class), so it is not a countable kind; it falls into `others`.
2314
+ const et = ENTITY_TO_TYPE[t];
2315
+ if (et && et !== "Change") kindWords.push(t);
2316
+ else others.push(t);
2317
+ }
2318
+ if (kindWords.length === 1 && others.length === 0) {
2319
+ // reuse the whole aggregate pipeline (parseAggregate → count node → renderer): a
2320
+ // synthesized "count <kind>" is the exact query the cascade's other count paths land.
2321
+ const hit = attempt(["count", kindWords[0]]);
2322
+ if (hit) { steps.push(`bare kind "${kindWords[0]}" → count`); return done(hit); }
2323
+ }
2324
+ // A LONE unknown noun wrapped only in packaging ("the bananas") is left to the generic
2325
+ // honest miss (the rephrase hint already NAMES the kinds): a crisper "isn't a listable
2326
+ // kind" miss here would fire on every one-word non-query the same way ("tell me a joke"),
2327
+ // which chat.mjs's own surface deliberately answers with the general hint — so the
2328
+ // bare-noun default is a COUNT of a KNOWN kind only, never a re-worded miss.
2329
+
2330
+ return null; // exhausted — the honest bottom of the cascade (caller keeps the original miss)
2331
+ }
2332
+
2333
+ // ---- orchestration — the tmct_ask entry point (§6.3: parse -> resolve -> traverse -> render) ----
2334
+
2335
+ /** Answer a free-text question over the graph, mechanically. `opts.contextId`
2336
+ * resolves a context pronoun ("this"/"it"/…) — wired from a UI's currently-
2337
+ * selected node when one exists; omit it in the bare CLI surface, where
2338
+ * a pronoun then produces an honest miss rather than a guess. `opts.nlp`
2339
+ * overrides the lemma/POS adapter (see parseQuery) — leave it undefined and
2340
+ * a Node process picks up wink automatically while the inlined viewer stays
2341
+ * adapter-less by construction. `opts.prev` is the id array of the LAST answer's
2342
+ * matches — thread it from a chat loop so a follow-up anaphora question ("which of
2343
+ * those are tested", "how many of them call X") filters/counts the prior result
2344
+ * set; omit it and anaphora questions produce an honest "needs a previous answer"
2345
+ * miss. Returns the full {content, tmct_ask:
2346
+ * {mechanical,parsed,matches,traversal,miss,ambiguous,candidates?}} envelope
2347
+ * §6.2 specifies. Zero generative model calls. */
2348
+ export function ask(graph, query, { contextId = null, nlp = undefined, prev = null } = {}) {
2349
+ // Explicit help/orientation request → the rephrase hint directly (the honest bottom
2350
+ // of the cascade, reached on demand), never a pretend answer or a relaxation attempt.
2351
+ if (isHelpRequest(query)) {
2352
+ return {
2353
+ content: rephraseHint(),
2354
+ tmct_ask: {
2355
+ mechanical: true, parsed: null, matches: [], traversal: null,
2356
+ miss: true, ambiguous: false, matchedVia: null, help: true, relaxed: null,
2357
+ },
2358
+ };
2359
+ }
2360
+ const direct = parseQuery(query, { nlp });
2361
+ // The relaxation cascade fires ONLY when the DIRECT parse would miss (no parse, a
2362
+ // compositional {node:"miss"}, or an unresolved named term) — a clean hit, an
2363
+ // ambiguous parse, an unresolved-pronoun miss, and a real-but-empty answer all keep
2364
+ // the direct parse untouched (a hit stays instant and exact).
2365
+ let parsed = direct;
2366
+ let relaxed = null;
2367
+ if (answerable(graph, direct, contextId) === false) {
2368
+ const r = relaxParse(graph, query, { nlp, contextId, prev });
2369
+ if (r) { parsed = r.parsed; relaxed = { from: r.from, to: r.to, dropped: r.dropped, steps: r.steps }; }
2370
+ }
2371
+ const result = traverse(graph, parsed, { contextId, prev });
2372
+ const rendered = render(parsed, result);
2373
+ // If relaxation materially rewrote the query and produced a real answer, note it
2374
+ // lightly (terse, honest) so the reader knows how the question was read.
2375
+ const content = (relaxed && !rendered.miss && relaxed.to !== relaxed.from)
2376
+ ? `read as "${relaxed.to}" — ${rendered.content}`
2377
+ : rendered.content;
2378
+ return {
2379
+ content,
2380
+ tmct_ask: {
2381
+ mechanical: true,
2382
+ parsed: (parsed && !parsed.ambiguousParse) ? parsed : null,
2383
+ matches: (result.matches || []).map((m) => ({
2384
+ id: m.id, label: m.label, type: m.class, module: m.class ? moduleLabelOf(m) : undefined,
2385
+ })),
2386
+ traversal: result.traversal || null,
2387
+ miss: !!rendered.miss,
2388
+ ambiguous: !!rendered.ambiguous,
2389
+ // The relaxation trace: null when the direct parse was used as-is (a clean hit or
2390
+ // an honest miss the cascade couldn't/shouldn't rescue), else what the cascade
2391
+ // dropped/normalised to reach an answer. A caller can assert relaxed===null to
2392
+ // prove the cascade never touched a direct hit.
2393
+ relaxed,
2394
+ // Confidence provenance: "prose" when resolveObject fell through to the tier-4
2395
+ // prose-index fallback (PLAN_PROSE_INDEX.md §6 — matched what the symbol talks
2396
+ // about, not its name); "fuzzy" when the tier-5 bounded-edit-distance pass
2397
+ // resolved a typo'd term (the rendered content also announces it: "assuming you
2398
+ // meant <label>"); null for every literal-identifier tier.
2399
+ matchedVia: result.matchedVia || null,
2400
+ ...(rendered.ambiguous ? { candidates: rendered.candidates } : {}),
2401
+ },
2402
+ };
2403
+ }