@ngockhoale/ukit 3.5.3 → 3.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/manifests/documentation.yaml +12 -1
  3. package/manifests/platform.full.yaml +12 -0
  4. package/package.json +29 -2
  5. package/src/cli/commands/doctor.js +41 -0
  6. package/src/cli/commands/indexTools.js +10 -4
  7. package/src/cli/commands/memory.js +88 -6
  8. package/src/core/memory/episodes.js +106 -7
  9. package/src/core/memory/memoryCheck.js +241 -0
  10. package/src/core/memory/memoryCheckRun.js +39 -0
  11. package/src/core/memory/memoryHit.js +31 -15
  12. package/src/core/memory/mutateMemory.js +37 -1
  13. package/src/core/memory/observations.js +192 -0
  14. package/src/core/memory/recallEngine.js +159 -10
  15. package/src/core/memory/recallLanes.js +182 -0
  16. package/src/core/memory/records.js +23 -0
  17. package/src/core/memory/retrieval.js +13 -1
  18. package/src/core/memory/topicCommand.js +75 -0
  19. package/src/core/memory/topicPages.js +545 -0
  20. package/src/core/memory/writeGate.js +137 -0
  21. package/src/core/runtimeConfig.js +56 -1
  22. package/src/decision/registry.js +17 -0
  23. package/src/index/buildIndex.js +312 -5
  24. package/src/index/languageTools.js +100 -0
  25. package/src/index/paths.js +4 -1
  26. package/src/index/queryIndex.js +356 -14
  27. package/src/index/resolveContext.js +1 -1
  28. package/template_project/.claude/hooks/edit-hint.mjs +64 -17
  29. package/template_project/.claude/hooks/session-episode-start.mjs +35 -0
  30. package/template_project/.claude/hooks/session-episode.sh +9 -4
  31. package/template_project/.claude/settings.json +2 -2
  32. package/template_project/.claude/ukit/index/lib/index-core.mjs +769 -18
  33. package/template_project/.claude/ukit/index/query-index.mjs +14 -3
  34. package/template_project/.claude/ukit/index/refresh-index.mjs +11 -0
  35. package/template_project/.claude/ukit/index/route-task.mjs +4 -1
  36. package/template_project/.claude/ukit/runtime/memory-check.mjs +241 -0
  37. package/template_project/.claude/ukit/runtime/memory-lanes.mjs +9 -2
  38. package/template_project/.claude/ukit/runtime/memory-recall.mjs +159 -10
  39. package/template_project/.claude/ukit/runtime/memory-topics.mjs +545 -0
  40. package/template_project/.claude/ukit/runtime/memory-validity.mjs +28 -1
  41. package/template_project/.claude/ukit/runtime/self-improve-trigger.mjs +212 -5
  42. package/template_project/.omp/hooks/pre/ukit-bridge.js +2 -1
  43. package/template_project/ukit/storage/config.json +21 -2
  44. package/manifests/instructionRules.yaml +0 -442
  45. package/src/core/agentRuntime/completionGate.js +0 -179
  46. package/src/core/agentRuntime/context.js +0 -571
  47. package/src/core/agentRuntime/qualityComparison.js +0 -112
  48. package/src/core/agentRuntime/resourcePolicy.js +0 -78
  49. package/src/core/agentRuntime/runtimeSupport.js +0 -255
  50. package/src/core/codeintel/manifest.js +0 -176
  51. package/src/core/experiments/deliberation.js +0 -327
  52. package/src/core/experiments/dynamicWorkflow.js +0 -507
  53. package/src/core/handoffDocValidator.js +0 -154
  54. package/src/core/memory/deltaOverlays.js +0 -493
  55. package/src/core/memory/learningCandidates.js +0 -347
  56. package/src/core/observability/emit/sessionBoot.js +0 -407
  57. package/src/core/observability/evaluation/replay.js +0 -149
  58. package/src/core/observability/evaluation/scorecard.js +0 -465
  59. package/src/core/observability/schema/compatibility.js +0 -180
  60. package/src/core/observability/support/renderer.js +0 -130
  61. package/src/core/reviewPanelAggregate.js +0 -204
  62. package/src/core/taskBudgetValidator.js +0 -251
  63. package/src/core/taskProgressGuard.js +0 -227
  64. package/src/decision/reviewVerdict.js +0 -339
  65. package/src/decision/runtimeFilter.js +0 -150
  66. package/src/decision/runtimeScheduler.js +0 -239
  67. package/src/index/contextPack.js +0 -285
  68. package/src/index/impactContext.js +0 -360
  69. package/src/index/reviewPolicy.js +0 -368
  70. package/src/index/sensitivePath.js +0 -21
@@ -522,6 +522,10 @@ export function buildDefaultRuntimeConfig(overrides = {}) {
522
522
  canaryProjects: [],
523
523
  killSwitch: false,
524
524
  },
525
+ // Index query (3.5.5): opt-in RRF fusion of the hashed-embedding lane for weak natural
526
+ // queries. Off by default: it lowered natural-en top-3 and mirror-over-original on the
527
+ // gold bench. Read by queryCodeIndex from .ukit/storage/config.json -> index.fusion.
528
+ index: { fusion: false },
525
529
  memory: {
526
530
  enabled: true,
527
531
  progressiveRetrieval: true,
@@ -531,7 +535,22 @@ export function buildDefaultRuntimeConfig(overrides = {}) {
531
535
  maxArchivedSessions: 50,
532
536
  maxPatternCandidates: 50,
533
537
  redactSecrets: true,
534
- sources: ['docs/MEMORY.md'],
538
+ // Capped core + every docs/memory/*.md detail file (3.5.4; one directory
539
+ // level, expanded by recallEngine.expandMemorySources). A missing directory
540
+ // contributes nothing, so a project with only docs/MEMORY.md behaves as before.
541
+ sources: ['docs/MEMORY.md', 'docs/memory/*.md'],
542
+ // Record write gate (3.5.4): 'strict' (default) = size cap, secrets, source
543
+ // evidence and near-duplicate checks on every non-generated record write;
544
+ // 'off' = no gate. scope user needs no file evidence. See writeGate.js.
545
+ writeGate: 'strict',
546
+ // Topic pages (3.5.5, docs/memory/topics/*.md, `ukit memory topic`): recall = pages
547
+ // are recall documents (inert until a page is built); hint = the edit hint names
548
+ // the module page (a path only, never the page text).
549
+ topics: { recall: true, hint: true },
550
+ // Optional CLI-only recall lanes (3.5.5), both off: vector = embedding
551
+ // cosine fused with BM25 by RRF; rerank = unic-decision confirms the top
552
+ // candidates (stage off|shadow|canary|default; active at canary/default).
553
+ recall: { vector: { enabled: false }, rerank: { stage: 'off' } },
535
554
  editHints: { enabled: true, mirrors: [], ignoreMirrors: [], coChange: false },
536
555
  },
537
556
  validation: {
@@ -1021,6 +1040,14 @@ export function validateRuntimeConfig(config) {
1021
1040
  }
1022
1041
  }
1023
1042
 
1043
+ if (config.index !== undefined) {
1044
+ if (!isPlainObject(config.index)) {
1045
+ errors.push('index must be an object.');
1046
+ } else if (config.index.fusion !== undefined) {
1047
+ pushBooleanError(errors, config.index.fusion, 'index.fusion');
1048
+ }
1049
+ }
1050
+
1024
1051
  if (!isPlainObject(config.codeIntel)) {
1025
1052
  errors.push('codeIntel must be an object.');
1026
1053
  } else {
@@ -1195,6 +1222,34 @@ export function validateRuntimeConfig(config) {
1195
1222
  || config.memory.sources.some((entry) => typeof entry !== 'string'))) {
1196
1223
  errors.push('memory.sources must be an array of relative .md paths.');
1197
1224
  }
1225
+ if (config.memory.writeGate !== undefined && !['strict', 'off', false].includes(config.memory.writeGate)) {
1226
+ errors.push('memory.writeGate must be "strict" or "off".');
1227
+ }
1228
+ if (config.memory.topics !== undefined) {
1229
+ if (!isPlainObject(config.memory.topics)) {
1230
+ errors.push('memory.topics must be an object.');
1231
+ } else {
1232
+ for (const key of ['recall', 'hint']) {
1233
+ if (config.memory.topics[key] !== undefined) pushBooleanError(errors, config.memory.topics[key], `memory.topics.${key}`);
1234
+ }
1235
+ }
1236
+ }
1237
+ // Optional recall lanes (3.5.5): absent = both off.
1238
+ const lanes = config.memory.recall;
1239
+ if (lanes !== undefined) {
1240
+ if (!isPlainObject(lanes)) {
1241
+ errors.push('memory.recall must be an object.');
1242
+ } else {
1243
+ if (lanes.vector !== undefined) {
1244
+ if (!isPlainObject(lanes.vector)) errors.push('memory.recall.vector must be an object.');
1245
+ else pushBooleanError(errors, lanes.vector.enabled, 'memory.recall.vector.enabled');
1246
+ }
1247
+ if (lanes.rerank !== undefined) {
1248
+ if (!isPlainObject(lanes.rerank)) errors.push('memory.recall.rerank must be an object.');
1249
+ else pushStageError(errors, lanes.rerank, 'memory.recall.rerank');
1250
+ }
1251
+ }
1252
+ }
1198
1253
  }
1199
1254
 
1200
1255
  if (!isPlainObject(config.validation)) {
@@ -23,6 +23,7 @@ export const DECISION_FAMILIES = new Set([
23
23
  'review',
24
24
  'resume',
25
25
  'learn',
26
+ 'memory',
26
27
  'runtime',
27
28
  ]);
28
29
 
@@ -432,6 +433,22 @@ export const DECISION_REGISTRY = Object.freeze([
432
433
  cacheSensitivity: 'none',
433
434
  leasePolicy: 'none',
434
435
  },
436
+ {
437
+ decisionKey: 'memory.relevance.v1',
438
+ schemaVersion: 1,
439
+ family: 'memory',
440
+ owner: 'memoryRecall',
441
+ kind: 'noul',
442
+ description: 'Whether one recalled note answers the query (rerank of the top BM25 candidates; asked once per candidate, wire name carries `#c<n>`). Gated by memory.recall.rerank.stage, CLI path only.',
443
+ candidatePolicy: 'bm25-top-candidates',
444
+ hardConstraints: ['no-new-candidates', 'abstention-unchanged'],
445
+ probabilityPolicy: 'raw-label',
446
+ fallbackPolicy: 'bm25-order',
447
+ telemetryClass: 'decision',
448
+ rolloutStage: 'off',
449
+ cacheSensitivity: 'none',
450
+ leasePolicy: 'none',
451
+ },
435
452
  ]);
436
453
 
437
454
  // Deep-freeze the shipped catalog — entries are shared metadata consumed by
@@ -150,11 +150,11 @@ const EXCLUDED_DIR_NAMES = new Set([
150
150
  '.venv',
151
151
  'venv',
152
152
  ]);
153
- const CODE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.vue', '.sql']);
153
+ const CODE_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte', '.py', '.go', '.java', '.cs', '.php', '.rb', '.rs', '.sql']);
154
154
  const STYLE_EXTENSIONS = new Set(['.css', '.scss', '.sass', '.less']);
155
155
  // `.sh` is tracked but never parsed, like `.md`/`.json`/`.yaml` — it is matched
156
156
  // by path only. Without it every shipped hook script was invisible to the index.
157
- const TRACKED_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.vue', '.sql', '.json', '.yaml', '.yml', '.md', '.sh']);
157
+ const TRACKED_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte', '.py', '.go', '.java', '.cs', '.php', '.rb', '.rs', '.sql', '.json', '.yaml', '.yml', '.md', '.sh']);
158
158
  const DISCOVERED_EXTENSIONS = new Set([...TRACKED_EXTENSIONS, ...STYLE_EXTENSIONS]);
159
159
  export const DEFAULT_INDEX_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000;
160
160
  // Bumped only when the snapshot shape itself changes; consumers must reject
@@ -1586,7 +1586,7 @@ function findScriptClose(content, startIndex) {
1586
1586
  }
1587
1587
 
1588
1588
  export function extractScriptContent(filePath, content) {
1589
- if (!filePath.endsWith('.vue')) {
1589
+ if (!/\.(?:vue|svelte)$/.test(filePath)) {
1590
1590
  return content;
1591
1591
  }
1592
1592
 
@@ -1682,6 +1682,280 @@ function computeSignature(content, lineStartIndex) {
1682
1682
  return `${collapsed.slice(0, 199)}…`;
1683
1683
  }
1684
1684
 
1685
+ // ── Non-JS languages (3.5.5): line/indent/brace based, no parser. Rows are { name, type, index, parent? }.
1686
+ // Kinds follow the JS lane: exported/public top-level -> function|class, private -> local-*, members -> method,
1687
+ // interface/enum/struct/trait/type aliases -> type.
1688
+ const POLYGLOT_EXT = /\.(?:py|go|java|cs|php|rb|rs)$/i;
1689
+ const POLY_MEMBER_SKIP = new Set(['if', 'for', 'while', 'switch', 'catch', 'return', 'new', 'else', 'throw', 'using', 'lock', 'foreach', 'fixed', 'match', 'await', 'yield', 'do', 'try', 'sizeof', 'typeof', 'nameof', 'default', 'when', 'base', 'this']);
1690
+ const POLY_MODS = '(?:(?:public|protected|private|internal|static|final|abstract|sealed|partial|readonly|virtual|override|async|extern|unsafe|new|synchronized|native|strictfp|default|open)\\s+)*';
1691
+ const POLY_JAVA_TYPE_RE = new RegExp(`^[ \\t]*(?:@\\w+(?:\\([^)]*\\))?\\s+)*${POLY_MODS}(class|interface|enum|struct|record(?:\\s+(?:class|struct))?)\\s+([A-Za-z_]\\w*)`);
1692
+ const POLY_JAVA_METHOD_RE = new RegExp(`^[ \\t]*(?:@\\w+(?:\\([^)]*\\))?\\s+)*(?:<[^>]+>\\s+)?(?:[\\w.<>\\[\\],?]+\\s+)*?([A-Za-z_]\\w*)[ \\t]*(?:<[^>()]*>[ \\t]*)?\\(`);
1693
+ const POLY_PHP_TYPE_RE = /^[ \t]*(?:(?:abstract|final|readonly)\s+)*(class|interface|trait|enum)\s+([A-Za-z_]\w*)/;
1694
+ const POLY_PHP_FUNC_RE = /^[ \t]*(?:(?:public|protected|private|static|abstract|final)\s+)*function[ \t]+(?:&[ \t]*)?([A-Za-z_]\w*)/;
1695
+ const POLY_RS_PUB = '(?:pub(?:\\([^)]*\\))?\\s+)?';
1696
+ const POLY_RS_FN_RE = new RegExp(`^[ \\t]*(${POLY_RS_PUB})(?:(?:default|const|async|unsafe)\\s+)*(?:extern\\s+(?:"[^"]*"\\s+)?)?fn\\s+([A-Za-z_]\\w*)`);
1697
+ const POLY_RS_TYPE_RE = new RegExp(`^[ \\t]*${POLY_RS_PUB}(struct|enum|union|type)\\s+([A-Za-z_]\\w*)`);
1698
+ const POLY_RS_TRAIT_RE = new RegExp(`^[ \\t]*${POLY_RS_PUB}(?:unsafe\\s+)?trait\\s+([A-Za-z_]\\w*)`);
1699
+ // Bounds against pathological input (regex stalls): files above 512 KB (the content-lane per-file cap) are not
1700
+ // extracted, and any line over 2000 chars is blanked in place (offsets preserved) before the per-line patterns run.
1701
+ const POLY_MAX_FILE_CHARS = 512 * 1024;
1702
+ const POLY_MAX_LINE_CHARS = 2000;
1703
+ // impl header -> implemented type name, without a backtracking pattern: skip a balanced <generics>, then the
1704
+ // type after the first `for` (trait impl) or the first identifier (inherent impl).
1705
+ function polyRsImplName(line) {
1706
+ const head = /^[ \t]*(?:unsafe[ \t]+)?impl\b/.exec(line);
1707
+ if (!head) return null;
1708
+ let rest = line.slice(head[0].length).replace(/^[ \t]+/, '');
1709
+ if (rest[0] === '<') {
1710
+ let depth = 0;
1711
+ let k = 0;
1712
+ for (; k < rest.length; k += 1) {
1713
+ if (rest[k] === '<') depth += 1;
1714
+ else if (rest[k] === '>' && (depth -= 1) === 0) break;
1715
+ }
1716
+ rest = rest.slice(k + 1);
1717
+ }
1718
+ const cut = rest.search(/[{]|[ \t]where\b/);
1719
+ const header = cut < 0 ? rest : rest.slice(0, cut);
1720
+ const forAt = header.search(/(?:^|[ \t])for[ \t]/);
1721
+ const target = forAt < 0 ? header : header.slice(forAt).replace(/^[ \t]*for[ \t]+/, '');
1722
+ return /^[ \t]*([A-Za-z_]\w*)/.exec(target)?.[1] ?? null;
1723
+ }
1724
+ const POLY_RS_MOD_RE = new RegExp(`^[ \\t]*${POLY_RS_PUB}mod\\s+([A-Za-z_]\\w*)`);
1725
+
1726
+ // Same-length copy with comments (and, per mode, string bodies) blanked, so offsets/lines stay valid.
1727
+ // mode 0 = comments only, 1 = + "strings", 2 = + "strings" and 'chars'.
1728
+ function maskCLike(content, mode) {
1729
+ const out = content.split('');
1730
+ const n = content.length;
1731
+ const blank = (from, to) => {
1732
+ for (let k = from; k < to; k += 1) {
1733
+ if (out[k] !== '\n') out[k] = ' ';
1734
+ }
1735
+ };
1736
+ let i = 0;
1737
+ while (i < n) {
1738
+ const c = content[i];
1739
+ const d = content[i + 1];
1740
+ if (c === '/' && d === '/') {
1741
+ let end = content.indexOf('\n', i);
1742
+ if (end < 0) end = n;
1743
+ blank(i, end);
1744
+ i = end;
1745
+ } else if (c === '/' && d === '*') {
1746
+ let end = content.indexOf('*/', i + 2);
1747
+ end = end < 0 ? n : end + 2;
1748
+ blank(i, end);
1749
+ i = end;
1750
+ } else if ((mode >= 1 && c === '"') || (mode >= 2 && c === "'")) {
1751
+ let k = i + 1;
1752
+ while (k < n && content[k] !== c && content[k] !== '\n') {
1753
+ if (content[k] === '\\') k += 1;
1754
+ k += 1;
1755
+ }
1756
+ blank(i + 1, Math.min(k, n));
1757
+ i = k + 1;
1758
+ } else {
1759
+ i += 1;
1760
+ }
1761
+ }
1762
+ return out.join('');
1763
+ }
1764
+
1765
+ function extractIndentLanguageSymbols(filePath, content) {
1766
+ const isPy = /\.py$/i.test(filePath);
1767
+ const scopeRe = isPy ? /^([ \t]*)class[ \t]+([A-Za-z_]\w*)/ : /^([ \t]*)(?:module|class)[ \t]+([A-Z][\w:]*)/;
1768
+ const defRe = isPy ? /^([ \t]*)(?:async[ \t]+)?def[ \t]+([A-Za-z_]\w*)/ : /^([ \t]*)def[ \t]+(?:[A-Za-z_]\w*\.)?([A-Za-z_]\w*[?!=]?)/;
1769
+ const rows = [];
1770
+ const stack = [];
1771
+ let docQuote = null;
1772
+ let offset = 0;
1773
+ for (const line of content.split('\n')) {
1774
+ const lineStart = offset;
1775
+ offset += line.length + 1;
1776
+ const startedInDoc = docQuote !== null;
1777
+ if (isPy) {
1778
+ for (const quote of ['"""', "'''"]) {
1779
+ const count = line.split(quote).length - 1;
1780
+ if (count % 2 === 1) docQuote = docQuote === quote ? null : (docQuote ?? quote);
1781
+ }
1782
+ if (startedInDoc) continue;
1783
+ }
1784
+ const scope = scopeRe.exec(line);
1785
+ const def = scope ? null : defRe.exec(line);
1786
+ const m = scope ?? def;
1787
+ if (!m) continue;
1788
+ const indent = m[1].length;
1789
+ while (stack.length > 0 && stack[stack.length - 1].indent >= indent) stack.pop();
1790
+ const top = stack[stack.length - 1];
1791
+ const name = m[2];
1792
+ const index = lineStart + indent;
1793
+ if (scope) {
1794
+ if (!top || top.kind === 'scope') {
1795
+ rows.push({ name, type: isPy && name.startsWith('_') ? 'local-class' : 'class', index, parent: top?.name });
1796
+ }
1797
+ stack.push({ indent, kind: 'scope', name });
1798
+ } else {
1799
+ if (!top) {
1800
+ rows.push({ name, type: isPy && name.startsWith('_') ? 'local-function' : 'function', index });
1801
+ } else if (top.kind === 'scope') {
1802
+ rows.push({ name, type: 'method', index, parent: top.name });
1803
+ }
1804
+ stack.push({ indent, kind: 'def', name });
1805
+ }
1806
+ }
1807
+ return rows;
1808
+ }
1809
+
1810
+ function extractGoSymbols(content) {
1811
+ const text = maskCLike(content, 0);
1812
+ const rows = [];
1813
+ for (const m of text.matchAll(/^type[ \t]+([A-Za-z_]\w*)/gm)) {
1814
+ rows.push({ name: m[1], type: 'type', index: m.index });
1815
+ }
1816
+ for (const m of text.matchAll(/^func[ \t]+([A-Za-z_]\w*)[ \t]*[[(]/gm)) {
1817
+ rows.push({ name: m[1], type: /^[A-Z]/.test(m[1]) ? 'function' : 'local-function', index: m.index });
1818
+ }
1819
+ for (const m of text.matchAll(/^func[ \t]*\([ \t]*(?:[A-Za-z_]\w*[ \t]+)?(?:\*[ \t]*)?([A-Za-z_]\w*)(?:\[[^\]\n]*\])?[ \t]*\)[ \t]*([A-Za-z_]\w*)/gm)) {
1820
+ rows.push({ name: m[2], type: 'method', index: m.index, parent: m[1] });
1821
+ }
1822
+ return rows;
1823
+ }
1824
+
1825
+ // Java, C#, PHP and Rust: depth-tracked on a comment/string-masked copy. A container
1826
+ // (class, trait, impl, mod ...) owns the lines exactly one brace level below its declaration.
1827
+ function extractBraceLanguageSymbols(filePath, content) {
1828
+ const ext = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase();
1829
+ const text = maskCLike(content, ext === 'rs' ? 1 : 2);
1830
+ const rows = [];
1831
+ const stack = [];
1832
+ let depth = 0;
1833
+ let offset = 0;
1834
+ for (const line of text.split('\n')) {
1835
+ const lineStart = offset;
1836
+ offset += line.length + 1;
1837
+ while (stack.length > 0 && stack[stack.length - 1].opened && depth <= stack[stack.length - 1].depth) stack.pop();
1838
+ const top = stack[stack.length - 1];
1839
+ const atMember = top ? depth === top.depth + 1 : false;
1840
+ const index = lineStart + Math.max(0, line.search(/\S/));
1841
+ const pushContainer = (kind, name) => {
1842
+ if (line.includes(';') && !line.includes('{')) return;
1843
+ stack.push({ kind, name, depth, opened: false });
1844
+ };
1845
+
1846
+ if (ext === 'java' || ext === 'cs') {
1847
+ const typeMatch = POLY_JAVA_TYPE_RE.exec(line);
1848
+ if (typeMatch) {
1849
+ const kind = typeMatch[1].split(/\s/)[0];
1850
+ rows.push({ name: typeMatch[2], type: kind === 'class' ? 'class' : 'type', index, parent: top?.name });
1851
+ pushContainer(kind, typeMatch[2]);
1852
+ } else if (atMember) {
1853
+ const m = POLY_JAVA_METHOD_RE.exec(line);
1854
+ if (m && !POLY_MEMBER_SKIP.has(m[1]) && !(top.kind === 'enum' && /^[A-Z0-9_]+$/.test(m[1]))) {
1855
+ rows.push({ name: m[1], type: 'method', index, parent: top.name });
1856
+ }
1857
+ }
1858
+ } else if (ext === 'php') {
1859
+ const typeMatch = POLY_PHP_TYPE_RE.exec(line);
1860
+ const fn = typeMatch ? null : POLY_PHP_FUNC_RE.exec(line);
1861
+ if (typeMatch) {
1862
+ rows.push({ name: typeMatch[2], type: typeMatch[1] === 'class' ? 'class' : 'type', index, parent: top?.name });
1863
+ pushContainer(typeMatch[1], typeMatch[2]);
1864
+ } else if (fn && !top) {
1865
+ rows.push({ name: fn[1], type: 'function', index });
1866
+ } else if (fn && atMember) {
1867
+ rows.push({ name: fn[1], type: 'method', index, parent: top.name });
1868
+ }
1869
+ } else {
1870
+ const fn = POLY_RS_FN_RE.exec(line);
1871
+ const trait = fn ? null : POLY_RS_TRAIT_RE.exec(line);
1872
+ const impl = fn || trait ? null : polyRsImplName(line);
1873
+ const mod = fn || trait || impl ? null : POLY_RS_MOD_RE.exec(line);
1874
+ const typeMatch = fn || trait || impl || mod ? null : POLY_RS_TYPE_RE.exec(line);
1875
+ const atModuleLevel = top ? top.kind === 'mod' && atMember : depth === 0;
1876
+ if (fn) {
1877
+ const container = top && (top.kind === 'impl' || top.kind === 'trait');
1878
+ if (container && atMember) {
1879
+ rows.push({ name: fn[2], type: 'method', index, parent: top.name });
1880
+ } else if (atModuleLevel) {
1881
+ rows.push({ name: fn[2], type: fn[1].trim() ? 'function' : 'local-function', index });
1882
+ }
1883
+ } else if (trait) {
1884
+ if (atModuleLevel) rows.push({ name: trait[1], type: 'type', index });
1885
+ pushContainer('trait', trait[1]);
1886
+ } else if (impl) {
1887
+ pushContainer('impl', impl);
1888
+ } else if (mod) {
1889
+ pushContainer('mod', mod[1]);
1890
+ } else if (typeMatch && atModuleLevel) {
1891
+ rows.push({ name: typeMatch[2], type: 'type', index });
1892
+ }
1893
+ }
1894
+
1895
+ let maxDepth = depth;
1896
+ for (let k = 0; k < line.length; k += 1) {
1897
+ if (line[k] === '{') {
1898
+ depth += 1;
1899
+ if (depth > maxDepth) maxDepth = depth;
1900
+ } else if (line[k] === '}' && depth > 0) {
1901
+ depth -= 1;
1902
+ }
1903
+ }
1904
+ for (const entry of stack) {
1905
+ if (!entry.opened && maxDepth > entry.depth) entry.opened = true;
1906
+ }
1907
+ }
1908
+ return rows;
1909
+ }
1910
+
1911
+ function extractPolyglotSymbols(filePath, rawContent) {
1912
+ if (rawContent.length > POLY_MAX_FILE_CHARS) return [];
1913
+ const parts = rawContent.split('\n');
1914
+ const content = parts.some((l) => l.length > POLY_MAX_LINE_CHARS)
1915
+ ? parts.map((l) => (l.length > POLY_MAX_LINE_CHARS ? ' '.repeat(l.length) : l)).join('\n')
1916
+ : rawContent;
1917
+ const ext = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase();
1918
+ let rows;
1919
+ if (ext === 'py' || ext === 'rb') rows = extractIndentLanguageSymbols(filePath, content);
1920
+ else if (ext === 'go') rows = extractGoSymbols(content);
1921
+ else rows = extractBraceLanguageSymbols(filePath, content);
1922
+ const seen = new Set();
1923
+ return rows.filter((row) => {
1924
+ const key = `${row.type}:${row.name}:${row.parent ?? ''}`;
1925
+ if (seen.has(key)) return false;
1926
+ seen.add(key);
1927
+ return true;
1928
+ });
1929
+ }
1930
+
1931
+ // `const x = (` also opens plain parenthesised expressions (`const n = (a ?? 0)`), so only a
1932
+ // parameter list whose closing paren is followed by `=>` (optionally after a return type) is a function.
1933
+ function findParenArrowDeclarations(content) {
1934
+ const found = [];
1935
+ for (const match of content.matchAll(/(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*=\s*(?:async\s*)?\(/g)) {
1936
+ const open = match.index + match[0].length - 1;
1937
+ const limit = Math.min(content.length, open + 2000);
1938
+ let depth = 0;
1939
+ let close = -1;
1940
+ for (let i = open; i < limit; i += 1) {
1941
+ if (content[i] === '(') depth += 1;
1942
+ else if (content[i] === ')') {
1943
+ depth -= 1;
1944
+ if (depth === 0) {
1945
+ close = i;
1946
+ break;
1947
+ }
1948
+ }
1949
+ }
1950
+ if (close >= 0 && /^\s*(?::\s*[^;\n{}=]*?(?:=>\s*[^;\n{}=]*?)?)?=>/.test(content.slice(close + 1, close + 201))) {
1951
+ found.push({ name: match[1], index: match.index });
1952
+ }
1953
+ }
1954
+ return found;
1955
+ }
1956
+
1957
+ const CLASS_METHOD_SKIP = new Set(['if', 'for', 'while', 'switch', 'catch', 'function', 'return', 'with', 'constructor', 'super']);
1958
+
1685
1959
  export function extractSymbols(filePath, content) {
1686
1960
  const symbols = [];
1687
1961
  const lineStarts = computeLineStarts(content);
@@ -1707,6 +1981,13 @@ export function extractSymbols(filePath, content) {
1707
1981
  }
1708
1982
  };
1709
1983
 
1984
+ if (POLYGLOT_EXT.test(filePath)) {
1985
+ for (const row of extractPolyglotSymbols(filePath, content)) {
1986
+ addSymbol(row.name, row.type, row.index, row.parent ? { parent: row.parent } : null);
1987
+ }
1988
+ return symbols;
1989
+ }
1990
+
1710
1991
  if (filePath.endsWith('.sql')) {
1711
1992
  // Capture the object name, skipping an optional schema qualifier (qas.fn_rpt_billing → fn_rpt_billing).
1712
1993
  const sqlIdent = '(?:(?:["`]?[A-Za-z_][A-Za-z0-9_$]*["`]?)\\s*\\.\\s*)?["`]?([A-Za-z_][A-Za-z0-9_$]*)["`]?';
@@ -1741,8 +2022,10 @@ export function extractSymbols(filePath, content) {
1741
2022
 
1742
2023
  addMatches(/export\s+(?:async\s+)?function\s+([A-Za-z_$][A-Za-z0-9_$]*)/g, 'function');
1743
2024
  addMatches(/export\s+class\s+([A-Za-z_$][A-Za-z0-9_$]*)/g, 'class');
1744
- addMatches(/(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*=\s*(?:async\s*)?\(/g, 'callable');
1745
- addMatches(/(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*=\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][A-Za-z0-9_$]*)\s*=>/g, 'callable');
2025
+ for (const decl of findParenArrowDeclarations(content)) {
2026
+ addSymbol(decl.name, 'callable', decl.index);
2027
+ }
2028
+ addMatches(/(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*=\s*(?:async\s+)?[A-Za-z_$][A-Za-z0-9_$]*\s*=>/g, 'callable');
1746
2029
  addMatches(/export\s+default\s+(?:async\s+)?function\s+([A-Za-z_$][A-Za-z0-9_$]*)/g, 'default-function');
1747
2030
  addMatches(/export\s+default\s+class\s+([A-Za-z_$][A-Za-z0-9_$]*)/g, 'default-class');
1748
2031
  addMatches(/export\s+default\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:;|\n|$)/g, 'default-reference');
@@ -1750,6 +2033,30 @@ export function extractSymbols(filePath, content) {
1750
2033
  addMatches(/defineComponent\(\s*\{[\s\S]*?name\s*:\s*['"]([^'"]+)['"]/g, 'component-name');
1751
2034
  addMatches(/export\s+default\s*\{[\s\S]*?name\s*:\s*['"]([^'"]+)['"]/g, 'component-name');
1752
2035
 
2036
+ // Unexported declarations (3.5.4). Line-anchored, so `export function` / `export class`
2037
+ // (indexed above as exported) never double-count. Local types rank below exported ones.
2038
+ addMatches(/^[ \t]*(?:async\s+)?function\s*\*?\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*[(<]/gm, 'local-function');
2039
+ addMatches(/^[ \t]*(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*=\s*(?:async\s+)?function\b/gm, 'callable');
2040
+ addMatches(/^[ \t]*(?:abstract\s+)?class\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, 'local-class');
2041
+ if (/\.(?:ts|tsx|mts|cts|vue|svelte)$/.test(filePath)) {
2042
+ addMatches(/^[ \t]*(?:export\s+)?(?:declare\s+)?(?:const\s+)?(?:interface|enum)\s+([A-Za-z_$][A-Za-z0-9_$]*)/gm, 'type');
2043
+ addMatches(/^[ \t]*(?:export\s+)?(?:declare\s+)?type\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^=\n]*>)?\s*=/gm, 'type');
2044
+ }
2045
+ // Class methods: indented `name(args) {` lines inside a balanced class body.
2046
+ for (const classMatch of content.matchAll(/(?:^|[^\w$.])class\s+[A-Za-z_$][A-Za-z0-9_$]*(?:\s+extends\s+[\w$.]+(?:\([^)]*\))?)?\s*\{/g)) {
2047
+ const bodyStart = classMatch.index + classMatch[0].length;
2048
+ const classBody = sliceBalancedBlock(content, bodyStart);
2049
+ if (!classBody) {
2050
+ continue;
2051
+ }
2052
+ for (const methodMatch of classBody.matchAll(/^[ \t]+(?:(?:static|async|get|set)\s+)*\*?\s*(#?[A-Za-z_$][A-Za-z0-9_$]*)\s*\([^()]*\)\s*\{/gm)) {
2053
+ if (CLASS_METHOD_SKIP.has(methodMatch[1])) {
2054
+ continue;
2055
+ }
2056
+ addSymbol(methodMatch[1], 'method', bodyStart + methodMatch.index + methodMatch[0].indexOf(methodMatch[1]));
2057
+ }
2058
+ }
2059
+
1753
2060
  // Global-registered frontend functions: reachable from anywhere without an
1754
2061
  // import edge, so impact analysis must treat them as verified dependencies.
1755
2062
  addMatches(/\b(?:window|globalThis|global)\s*\.\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*=/g, 'global');
@@ -19,6 +19,46 @@ const VIETNAMESE_PHRASE_ALIASES = [
19
19
  { regex: /\bthanh phan\b/gu, expansions: ['component'] },
20
20
  { regex: /\bkiem thu\b/gu, expansions: ['test', 'testing'] },
21
21
  { regex: /\bbo cuc\b/gu, expansions: ['layout'] },
22
+ // General programming vocabulary (diacritics folded; matches with or without accents).
23
+ { regex: /\bnhay cam\b/gu, expansions: ['sensitive'] },
24
+ { regex: /\bbi mat\b/gu, expansions: ['secret'] },
25
+ { regex: /\bgia tri\b/gu, expansions: ['value'] },
26
+ { regex: /\bvan ban\b/gu, expansions: ['text'] },
27
+ { regex: /\bsao luu\b/gu, expansions: ['backup'] },
28
+ { regex: /\bkhoi phuc\b/gu, expansions: ['restore', 'recover'] },
29
+ { regex: /\bdinh danh\b/gu, expansions: ['identity', 'id'] },
30
+ { regex: /\bdu an\b/gu, expansions: ['project'] },
31
+ { regex: /\bthu muc\b/gu, expansions: ['directory', 'dir', 'folder'] },
32
+ { regex: /\bphan loai\b/gu, expansions: ['classify', 'category'] },
33
+ { regex: /\buoc tinh\b/gu, expansions: ['estimate'] },
34
+ { regex: /\bcong chan\b/gu, expansions: ['gate', 'guard'] },
35
+ { regex: /\bcap nhat\b/gu, expansions: ['update'] },
36
+ { regex: /\bgo cai dat\b/gu, expansions: ['uninstall'] },
37
+ { regex: /\bcai dat\b/gu, expansions: ['install', 'setup'] },
38
+ { regex: /\bcau hinh\b/gu, expansions: ['config', 'configuration'] },
39
+ { regex: /\bbao cao\b/gu, expansions: ['report'] },
40
+ { regex: /\blich su\b/gu, expansions: ['history'] },
41
+ { regex: /\bnhat ky\b/gu, expansions: ['log'] },
42
+ { regex: /\bchi muc\b/gu, expansions: ['index'] },
43
+ { regex: /\bxu ly\b/gu, expansions: ['handle', 'process'] },
44
+ { regex: /\bphan tich\b/gu, expansions: ['parse', 'analyze'] },
45
+ { regex: /\bhet han\b/gu, expansions: ['expire', 'expiry'] },
46
+ { regex: /\bchup anh\b/gu, expansions: ['snapshot'] },
47
+ { regex: /\bhoan tac\b/gu, expansions: ['rollback', 'revert'] },
48
+ { regex: /\bnguoi dung\b/gu, expansions: ['user'] },
49
+ { regex: /\bthong bao\b/gu, expansions: ['notify', 'message'] },
50
+ { regex: /\bsap xep\b/gu, expansions: ['sort'] },
51
+ { regex: /\bdang ky\b/gu, expansions: ['register'] },
52
+ { regex: /\btham so\b/gu, expansions: ['argument', 'param', 'option'] },
53
+ { regex: /\bket qua\b/gu, expansions: ['result'] },
54
+ { regex: /\bmac dinh\b/gu, expansions: ['default'] },
55
+ { regex: /\bdau vao\b/gu, expansions: ['input'] },
56
+ { regex: /\bdau ra\b/gu, expansions: ['output'] },
57
+ { regex: /\bhang doi\b/gu, expansions: ['queue'] },
58
+ { regex: /\bma hoa\b/gu, expansions: ['encode', 'encrypt'] },
59
+ { regex: /\bgiai ma\b/gu, expansions: ['decode', 'decrypt'] },
60
+ { regex: /\bbo nho\b/gu, expansions: ['memory'] },
61
+ { regex: /\bcanh bao\b/gu, expansions: ['warning', 'warn'] },
22
62
  ];
23
63
 
24
64
  const VIETNAMESE_TOKEN_ALIASES = new Map([
@@ -40,8 +80,58 @@ const VIETNAMESE_TOKEN_ALIASES = new Map([
40
80
  ['soat', ['review', 'audit']],
41
81
  ['trien', ['implement', 'build', 'ship']],
42
82
  ['khai', ['implement', 'build', 'ship']],
83
+ ['kho', ['store', 'storage']],
84
+ ['ghi', ['write', 'record']],
85
+ ['doc', ['read']],
86
+ ['luu', ['save', 'store', 'persist']],
87
+ ['tim', ['find', 'search', 'resolve']],
88
+ ['hong', ['broken', 'repair']],
89
+ ['phut', ['minute', 'minutes']],
90
+ ['ham', ['function']],
91
+ ['bien', ['variable']],
92
+ ['lop', ['class']],
93
+ ['lenh', ['command']],
94
+ ['chay', ['run', 'execute']],
95
+ ['phien', ['session']],
96
+ ['loc', ['filter']],
97
+ ['gui', ['send', 'dispatch']],
98
+ ['khoa', ['lock']],
99
+ ['huy', ['cancel', 'abort']],
100
+ ['quyet', ['decision']],
43
101
  ]);
44
102
 
103
+ // Query-time light stemming: adds the singular/base form of an English query word so
104
+ // 'files' reaches `file`, 'checks' reaches `check`, 'written' reaches `write`. Added next
105
+ // to the original token, never in place of it, and never applied to indexed text.
106
+ const IRREGULAR_STEMS = new Map([
107
+ ['written', 'write'], ['wrote', 'write'], ['built', 'build'], ['ran', 'run'], ['saved', 'save'],
108
+ ['found', 'find'], ['sent', 'send'], ['made', 'make'], ['read', 'read'], ['began', 'begin'],
109
+ ]);
110
+
111
+ const STEM_MIN_QUERY_TOKENS = 5;
112
+
113
+ function stemToken(token) {
114
+ if (!/^[a-z]+$/.test(token) || token.length < 4) {
115
+ return null;
116
+ }
117
+ if (IRREGULAR_STEMS.has(token)) {
118
+ return IRREGULAR_STEMS.get(token);
119
+ }
120
+ let stem = null;
121
+ if (token.endsWith('ies') && token.length > 4) {
122
+ stem = `${token.slice(0, -3)}y`;
123
+ } else if (/(?:ches|shes|xes|sses)$/.test(token)) {
124
+ stem = token.slice(0, -2);
125
+ } else if (token.endsWith('s') && !/(?:ss|us|is|as|os)$/.test(token)) {
126
+ stem = token.slice(0, -1);
127
+ } else if (token.endsWith('ing') && token.length > 5) {
128
+ stem = token.slice(0, -3);
129
+ } else if (token.endsWith('ed') && token.length > 4) {
130
+ stem = token.endsWith('ied') ? `${token.slice(0, -3)}y` : token.slice(0, -2);
131
+ }
132
+ return stem && stem.length >= 3 && stem !== token ? stem : null;
133
+ }
134
+
45
135
  export function buildRouteSignalText(...values) {
46
136
  const baseText = values
47
137
  .map((value) => String(value || '').trim())
@@ -73,6 +163,16 @@ export function buildSearchDescriptor(value, { expandVietnameseAliases = false }
73
163
  for (const token of collectVietnameseAliasTokens(foldedLower, tokens)) {
74
164
  tokens.add(token);
75
165
  }
166
+ // Stems only for question-like queries: identifier-style queries (findRelatedItemIds,
167
+ // collectVietnameseAliasTokens) must keep matching exact tokens.
168
+ if (tokens.size >= STEM_MIN_QUERY_TOKENS) {
169
+ for (const token of [...tokens]) {
170
+ const stem = stemToken(token);
171
+ if (stem && !GENERIC_STOPWORDS.has(stem)) {
172
+ tokens.add(stem);
173
+ }
174
+ }
175
+ }
76
176
  }
77
177
 
78
178
  return {
@@ -19,7 +19,9 @@ export const INDEX_ARTIFACTS = {
19
19
  // SQL 'column' rows with parent/schema, and calls.json gains per-object SQL
20
20
  // reference records — a v8 cached artifact must not be reused for unchanged
21
21
  // files or the new extractions silently vanish.
22
- export const INDEX_SCHEMA_VERSION = 9;
22
+ // v10: symbols.json gains unexported function/class ('local-function', 'local-class'),
23
+ // function-expression consts, class 'method' rows and TS interface/enum/type ('type') rows.
24
+ export const INDEX_SCHEMA_VERSION = 11;
23
25
 
24
26
  export function getIndexDir(rootDir) {
25
27
  return path.join(rootDir, '.cache', 'index');
@@ -60,5 +62,6 @@ export function isLikelyTestFilePath(filePath) {
60
62
  || lower.endsWith('.spec.jsx')
61
63
  || lower.endsWith('.spec.mjs')
62
64
  || lower.endsWith('.spec.cjs')
65
+ || /(?:_test\.(?:go|py|rb|rs)|_spec\.rb|(?:^|\/)test_[^/]+\.py|[a-z0-9]Tests?\.(?:java|cs))$/.test(filePath)
63
66
  );
64
67
  }