@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
@@ -1,6 +1,9 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import { execFile } from 'node:child_process';
4
+ import { createHash } from 'node:crypto';
5
+
6
+ import { withFileLock } from '../core/fileOps.js';
4
7
 
5
8
  import { getArtifactPath, getIndexDir, INDEX_ARTIFACTS, INDEX_SCHEMA_VERSION, isLikelyTestFilePath } from './paths.js';
6
9
  import { importsNeedAliasContext, loadImportAliasContext, resolveImportSpecifier } from './importResolution.js';
@@ -53,7 +56,8 @@ function safeSchemaArtifact(artifact) {
53
56
  return normalized.schemaVersion === INDEX_SCHEMA_VERSION ? normalized : { items: [] };
54
57
  }
55
58
 
56
- export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5 } = {}) {
59
+ // contentLane=false skips the error-string content search (context-resolution paths never pay for it).
60
+ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5, contentLane = true } = {}) {
57
61
  const normalizedQuery = String(query ?? '').trim();
58
62
  if (!normalizedQuery) {
59
63
  return [];
@@ -73,6 +77,7 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
73
77
  rootDir: absoluteRoot,
74
78
  query: normalizedQuery,
75
79
  limit,
80
+ contentLane,
76
81
  });
77
82
  if (QUERY_RESULT_CACHE.has(queryCacheKey)) {
78
83
  return QUERY_RESULT_CACHE.get(queryCacheKey);
@@ -107,8 +112,51 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
107
112
  }
108
113
  }
109
114
 
115
+ // Content lane: a message-like query with no confident symbol/path hit also searches
116
+ // file contents through the existing bounded fallback search. It only adds a bounded
117
+ // bonus, so it can never displace a confident hit (those skip the lane entirely).
118
+ let bestDirect = 0;
119
+ let contentLaneRan = false;
120
+ let contentCutShort = [];
121
+ for (const entry of directScores.values()) {
122
+ bestDirect = Math.max(bestDirect, entry.score);
123
+ }
124
+ if (contentLane && bestDirect < CONTENT_LANE_CONFIDENT_SCORE && queryDescriptor.tokens.size >= CONTENT_LANE_MIN_TOKENS && looksLikeLiteralMessage(normalizedQuery)) {
125
+ contentLaneRan = true;
126
+ // A literal message sits on one source line: every query token must share the matched
127
+ // line, otherwise this is a scattered token hit, not the message. The scan order and the
128
+ // count/byte caps are functions of the index and file contents only (never of load or of
129
+ // ripgrep being installed); wall-clock is a safety cap that reports 'time-budget'.
130
+ const messageTokens = [...new Set(normalizeIdentifierTokens(queryDescriptor.rawLower))];
131
+ let lineHits = [];
132
+ try {
133
+ const ordered = rankContentLaneFiles({
134
+ files, searchDescriptorsByFile, tokenWeights, queryTokens: queryDescriptor.tokens, includeLikelyTestFiles,
135
+ });
136
+ const scan = await scanContentLane({ absoluteRoot, ordered, tokens: messageTokens, limit: CONTENT_LANE_LIMIT });
137
+ lineHits = scan.hits;
138
+ contentCutShort = scan.truncated;
139
+ } catch {
140
+ contentCutShort = ['error'];
141
+ }
142
+ lineHits.forEach((row, index) => {
143
+ const existing = directScores.get(row.filePath);
144
+ const reason = `content_match:${row.filePath}${row.line ? `:${row.line}` : ''}`;
145
+ if (existing) {
146
+ existing.score += CONTENT_LANE_BONUS;
147
+ existing.reasons = dedupe([...(existing.reasons ?? []), reason]);
148
+ } else {
149
+ directScores.set(row.filePath, {
150
+ filePath: row.filePath,
151
+ score: CONTENT_LANE_ONLY_SCORE - index * 0.1,
152
+ reasons: [reason],
153
+ });
154
+ }
155
+ });
156
+ }
157
+
110
158
  if (directScores.size === 0) {
111
- return Object.freeze([]);
159
+ return withContentLaneStatus(Object.freeze([]), contentLaneRan, contentCutShort);
112
160
  }
113
161
 
114
162
  const {
@@ -151,12 +199,13 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
151
199
  }),
152
200
  });
153
201
 
154
- return freezeQueryResults(
155
- [...finalScores.values()]
156
- .map((entry) => ({ ...entry, score: finalizeScore(entry) }))
157
- .sort((a, b) => b.score - a.score || a.filePath.localeCompare(b.filePath))
158
- .slice(0, limit),
159
- );
202
+ const ranked = [...finalScores.values()]
203
+ .map((entry) => ({ ...entry, score: finalizeScore(entry) }))
204
+ .sort((a, b) => b.score - a.score || a.filePath.localeCompare(b.filePath));
205
+ const fused = contentLane && ranked.length > 0 && ranked[0].score < FUSION_WEAK_SCORE && queryDescriptor.tokens.size >= FUSION_MIN_TOKENS
206
+ ? await fuseWeakQuery({ absoluteRoot, query: normalizedQuery, ranked, searchDescriptorsByFile, includeLikelyTestFiles })
207
+ : null;
208
+ return withContentLaneStatus(freezeQueryResults((fused ?? ranked).slice(0, limit)), contentLaneRan, contentCutShort);
160
209
  })().catch((error) => {
161
210
  QUERY_RESULT_CACHE.delete(queryCacheKey);
162
211
  throw error;
@@ -166,6 +215,129 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
166
215
  return queryPromise;
167
216
  }
168
217
 
218
+ // Opt-in lane fusion (default OFF): a weak natural-language query (best score below
219
+ // FUSION_WEAK_SCORE, >= FUSION_MIN_TOKENS tokens) is re-ranked by RRF over the scored list
220
+ // and a hashed-embedding lane (the retriever.js vector lane). Enabled by
221
+ // `.ukit/storage/config.json` -> index.fusion === true or UKIT_INDEX_FUSION=1. The flag is
222
+ // read only after the weak gate passes, and contentLane:false callers (hooks/resolveContext)
223
+ // never reach it. Never throws; async fs only.
224
+ const FUSION_WEAK_SCORE = 6;
225
+ const FUSION_MIN_TOKENS = 4;
226
+ const FUSION_RRF_K = 5;
227
+ const FUSION_VECTOR_WEIGHT = 1;
228
+ const FUSION_LANE_LIMIT = 100;
229
+ const FUSION_DOC_VECTORS = new WeakMap();
230
+
231
+ // Inlined copy of src/core/codeintel/vectorProvider.js hashVectorize/cosine (256 dims, FNV-1a
232
+ // word + trigram features, L2-normalized) so the shipped index-core.mjs stays dependency-free.
233
+ function fusionFnv1a(str) {
234
+ let h = 0x811c9dc5;
235
+ for (let i = 0; i < str.length; i += 1) {
236
+ h ^= str.charCodeAt(i);
237
+ h = Math.imul(h, 0x01000193) >>> 0;
238
+ }
239
+ return h >>> 0;
240
+ }
241
+
242
+ function hashVectorize(text) {
243
+ const dims = 256;
244
+ const vec = new Array(dims).fill(0);
245
+ for (const token of String(text ?? '').split(/[^A-Za-z0-9_$]+/).map((t) => t.toLowerCase()).filter(Boolean)) {
246
+ vec[fusionFnv1a(`w:${token}`) % dims] += 1;
247
+ const padded = `#${token}#`;
248
+ for (let i = 0; i + 3 <= padded.length; i += 1) vec[fusionFnv1a(`t:${padded.slice(i, i + 3)}`) % dims] += 0.1;
249
+ }
250
+ const norm = Math.sqrt(vec.reduce((sum, v) => sum + v * v, 0));
251
+ if (norm > 0) for (let i = 0; i < dims; i += 1) vec[i] /= norm;
252
+ return vec;
253
+ }
254
+
255
+ function cosineSimilarity(a, b) {
256
+ let dot = 0;
257
+ for (let i = 0; i < a.length; i += 1) dot += a[i] * b[i];
258
+ return dot;
259
+ }
260
+
261
+ async function isFusionEnabled(absoluteRoot) {
262
+ if (process.env.UKIT_INDEX_FUSION === '1') return true;
263
+ if (process.env.UKIT_INDEX_FUSION === '0') return false;
264
+ try {
265
+ const config = JSON.parse(await fs.readFile(path.join(absoluteRoot, '.ukit', 'storage', 'config.json'), 'utf8'));
266
+ return config?.index?.fusion === true;
267
+ } catch {
268
+ return false;
269
+ }
270
+ }
271
+
272
+ async function fuseWeakQuery({ absoluteRoot, query, ranked, searchDescriptorsByFile, includeLikelyTestFiles }) {
273
+ try {
274
+ if (!(await isFusionEnabled(absoluteRoot))) return null;
275
+ let docVectors = FUSION_DOC_VECTORS.get(searchDescriptorsByFile);
276
+ if (!docVectors) {
277
+ docVectors = new Map();
278
+ for (const [filePath, descriptor] of searchDescriptorsByFile) {
279
+ docVectors.set(filePath, hashVectorize(`${filePath} ${(descriptor.symbols ?? []).map((s) => s.name).join(' ')}`));
280
+ }
281
+ FUSION_DOC_VECTORS.set(searchDescriptorsByFile, docVectors);
282
+ }
283
+ const queryVector = hashVectorize(query);
284
+ const vectorLane = [];
285
+ for (const [filePath, vector] of docVectors) {
286
+ if (isLikelyTestFilePath(filePath) && !includeLikelyTestFiles) continue;
287
+ const similarity = cosineSimilarity(queryVector, vector);
288
+ if (similarity > 0) vectorLane.push([filePath, similarity]);
289
+ }
290
+ vectorLane.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
291
+ const vectorPaths = vectorLane.slice(0, FUSION_LANE_LIMIT).map(([filePath]) => filePath);
292
+ const indexPaths = ranked.slice(0, FUSION_LANE_LIMIT).map((entry) => entry.filePath);
293
+ const rrf = new Map();
294
+ indexPaths.forEach((filePath, i) => rrf.set(filePath, (rrf.get(filePath) ?? 0) + 1 / (FUSION_RRF_K + i + 1)));
295
+ vectorPaths.forEach((filePath, i) => rrf.set(filePath, (rrf.get(filePath) ?? 0) + FUSION_VECTOR_WEIGHT / (FUSION_RRF_K + i + 1)));
296
+ const byPath = new Map(ranked.map((entry) => [entry.filePath, entry]));
297
+ const order = [...rrf.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([filePath]) => filePath);
298
+ const floor = ranked[ranked.length - 1].score;
299
+ const merged = order.map((filePath) => byPath.get(filePath)
300
+ ?? { filePath, score: Math.min(0.01, floor), reasons: ['fusion:vector'], tests: [] });
301
+ await emitFusionTelemetry(absoluteRoot, { query, indexPaths, vectorPaths, mergedPaths: merged.map((e) => e.filePath) });
302
+ return merged;
303
+ } catch {
304
+ return null;
305
+ }
306
+ }
307
+
308
+ // Same event shape and file as retriever.js emitRetrieverLaneTelemetry (query stored as sha256).
309
+ async function emitFusionTelemetry(absoluteRoot, { query, indexPaths, vectorPaths, mergedPaths }) {
310
+ try {
311
+ const event = {
312
+ ts: Date.now(),
313
+ query: createHash('sha256').update(String(query ?? '')).digest('hex'),
314
+ mode: 'index-query',
315
+ lanes: {
316
+ index: { hits: indexPaths.length, weightUsed: 1, paths: indexPaths },
317
+ vector: { hits: vectorPaths.length, weightUsed: FUSION_VECTOR_WEIGHT, paths: vectorPaths },
318
+ },
319
+ mergedPaths: mergedPaths.slice(0, 100),
320
+ merge: 'rrf',
321
+ };
322
+ const filePath = path.join(absoluteRoot, '.ukit', 'storage', 'cache', 'retriever-lanes.jsonl');
323
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
324
+ await withFileLock(filePath, async () => {
325
+ try {
326
+ const st = await fs.stat(filePath);
327
+ if (st.size > 256 * 1024) {
328
+ const kept = (await fs.readFile(filePath, 'utf8')).split('\n').filter(Boolean).slice(-2000);
329
+ await fs.writeFile(filePath, kept.length ? `${kept.join('\n')}\n` : '', 'utf8');
330
+ }
331
+ } catch {
332
+ // Missing file is fine; append below recreates it.
333
+ }
334
+ await fs.appendFile(filePath, `${JSON.stringify(event)}\n`, 'utf8');
335
+ });
336
+ } catch {
337
+ // Telemetry is advisory.
338
+ }
339
+ }
340
+
169
341
  // The `callable` symbol regex matches any `const x = (`, so it also captures plain
170
342
  // parenthesised expressions such as `const rows = (items ?? [])`. Harmless for fuzzy
171
343
  // ranking, misleading in an outline that is read as a map of the file's API. Those rows
@@ -185,7 +357,11 @@ function isFunctionLikeSignature(signature) {
185
357
  // Reads symbols.json through the existing readArtifact cache (no new file reads, no new
186
358
  // cache). Never throws: an absent file, a missing artifact, or a stale schema all resolve
187
359
  // to [] so callers can render an outline block unconditionally.
188
- export async function getFileOutline({ rootDir = process.cwd(), filePath, limit = 12 } = {}) {
360
+ const OUTLINE_WINDOW = 12; // matches OUTLINE_SYMBOLS_LIMIT in the CLI printers
361
+
362
+ // `focus`: symbol names the query matched (see queryMatchedSymbolNames); they are pinned into
363
+ // the first OUTLINE_WINDOW rows even when unexported, so the printed outline shows the hit.
364
+ export async function getFileOutline({ rootDir = process.cwd(), filePath, limit = 12, focus = null } = {}) {
189
365
  if (!filePath) {
190
366
  return Object.freeze([]);
191
367
  }
@@ -211,9 +387,26 @@ export async function getFileOutline({ rootDir = process.cwd(), filePath, limit
211
387
  seenNames.add(item.name);
212
388
  deduped.push(item);
213
389
  }
390
+ // The first OUTLINE_WINDOW rows (what the CLI prints) hold exported/top-level symbols
391
+ // first; unexported functions and class methods only take the remaining slots. Each
392
+ // part stays in line order, and rows beyond the window follow in line order.
393
+ const window = Number.isFinite(limit) ? limit : OUTLINE_WINDOW;
394
+ let kept = deduped;
395
+ if (deduped.length > window) {
396
+ const pinned = new Set(focus ?? []);
397
+ const isPinned = (item) => pinned.has(item.name);
398
+ const ranked = [
399
+ ...deduped.filter(isPinned),
400
+ ...deduped.filter((item) => !isPinned(item) && !isLocalOutlineRow(item)),
401
+ ...deduped.filter((item) => !isPinned(item) && isLocalOutlineRow(item)),
402
+ ];
403
+ const head = ranked.slice(0, window).sort((a, b) => a.line - b.line);
404
+ const inHead = new Set(head);
405
+ kept = [...head, ...deduped.filter((item) => !inHead.has(item))];
406
+ }
214
407
 
215
408
  return Object.freeze(
216
- deduped.slice(0, limit).map((item) => Object.freeze({
409
+ kept.slice(0, limit).map((item) => Object.freeze({
217
410
  line: item.line,
218
411
  name: item.name,
219
412
  type: item.type,
@@ -343,10 +536,22 @@ const PATH_EXACT_SCORE = 60;
343
536
  const PATH_SUFFIX_SCORE = 48;
344
537
  const PATH_BASENAME_SCORE = 40;
345
538
  const SYMBOL_EXACT_SCORE = 40;
539
+ const LOCAL_SYMBOL_FACTOR = 0.85; // unexported symbols (local function/class, method, unexported type) rank below an exported symbol of the same name
346
540
  const SYMBOL_DECAY = 0.35; // k-th best symbol of a file adds score * SYMBOL_DECAY^k
347
541
  const SYMBOL_REASON_LIMIT = 3;
348
542
  const MIN_SUBSTRING_TOKEN_LENGTH = 3;
349
543
  const BOOST_LIFT_RATIO = 0.2; // import/hotspot/archetype boosts may add at most this share of a direct score (+1)
544
+ const CONTENT_LANE_CONFIDENT_SCORE = 30; // a best direct score at/above this (exact symbol or path hit) skips the content lane
545
+ const CONTENT_LANE_MIN_TOKENS = 3; // shorter queries are names, not messages
546
+ const CONTENT_LANE_LIMIT = 40; // candidates only share tokens somewhere in the file; the same-line check below keeps the real ones
547
+ const CONTENT_LANE_CODE_EXT = /\.(?:m?js|cjs|jsx?|tsx?|vue|svelte|sql|sh|py|go|java|cs|php|rb|rs)$/i; // docs/data files quote queries verbatim (baselines, logs); only source carries error strings
548
+ const CONTENT_LANE_BONUS = 14; // added to a file that already has a direct score
549
+ const CONTENT_LANE_ONLY_SCORE = 13; // base for a file found only by content; below CONTENT_LANE_CONFIDENT_SCORE by design
550
+ const CONTENT_LANE_MAX_FILES = 1500; // files read per query, in priority order (primary bound)
551
+ const CONTENT_LANE_MAX_BYTES = 24 * 1024 * 1024; // characters read per query (primary bound)
552
+ const CONTENT_LANE_MAX_FILE_BYTES = 512 * 1024; // larger files are generated/data, never scanned
553
+ const CONTENT_LANE_BATCH = 24;
554
+ const CONTENT_LANE_SAFETY_MS = 5000; // safety cap only: hitting it reports 'time-budget', normal runs never do
350
555
  const BOOST_ONLY_RATIO = 0.3; // files with no direct match stay below this share of the best direct score
351
556
 
352
557
  // Rarity weight per token in (0,1]: a token present in nearly every file is worth
@@ -377,10 +582,10 @@ function buildTokenWeights(searchDescriptorsByFile) {
377
582
  // hiding the mirror: root installed copies < template_project < everything else.
378
583
  function mirrorPrior(filePath) {
379
584
  if (/^\.(?:claude|codex|omp)\//.test(filePath)) {
380
- return 0.88;
585
+ return 0.8;
381
586
  }
382
587
  if (filePath.startsWith('template_project/')) {
383
- return 0.94;
588
+ return 0.85;
384
589
  }
385
590
  return 1;
386
591
  }
@@ -448,7 +653,7 @@ function scoreDirectFileMatch({ queryDescriptor, rawQuery = '', filePath, search
448
653
  queryDescriptor.compact.length >= 4
449
654
  && symbolDescriptor.descriptor.compact === queryDescriptor.compact
450
655
  ) {
451
- symbolHits.push({ score: SYMBOL_EXACT_SCORE, reasons: [`symbol_exact:${symbolDescriptor.name}`] });
656
+ symbolHits.push({ score: SYMBOL_EXACT_SCORE * (symbolDescriptor.local ? LOCAL_SYMBOL_FACTOR : 1), reasons: [`symbol_exact:${symbolDescriptor.name}`] });
452
657
  continue;
453
658
  }
454
659
 
@@ -488,7 +693,7 @@ function scoreDirectFileMatch({ queryDescriptor, rawQuery = '', filePath, search
488
693
  }
489
694
 
490
695
  if (symbolScore > 0) {
491
- symbolHits.push({ score: symbolScore, reasons: symbolReasons });
696
+ symbolHits.push({ score: symbolScore * (symbolDescriptor.local ? LOCAL_SYMBOL_FACTOR : 1), reasons: symbolReasons });
492
697
  }
493
698
  }
494
699
 
@@ -953,11 +1158,124 @@ function buildFileSearchDescriptors(files, symbolMap) {
953
1158
  return descriptors;
954
1159
  }
955
1160
 
1161
+ // Rows whose declaration is not exported. 'callable' keeps its pre-3.5.4 weight.
1162
+ // Outline-only: a 'callable' const without `export` is also unexported.
1163
+ function isLocalOutlineRow(item) {
1164
+ return isLocalSymbolRow(item) || (item?.type === 'callable' && !/^\s*export\b/.test(item?.signature ?? ''));
1165
+ }
1166
+
1167
+ function isLocalSymbolRow(symbol) {
1168
+ const type = symbol?.type;
1169
+ if (type === 'local-function' || type === 'local-class' || type === 'method') {
1170
+ return true;
1171
+ }
1172
+ return type === 'type' && !/^\s*export\b/.test(symbol?.signature ?? '');
1173
+ }
1174
+
1175
+ // Single identifiers, questions and non-ASCII (Vietnamese) prose are descriptions of behaviour, not strings
1176
+ // that appear in source, so they never pay for a content search.
1177
+ function looksLikeLiteralMessage(query) {
1178
+ return /^[\x20-\x7e]+$/.test(query)
1179
+ && query.split(/\s+/).length >= CONTENT_LANE_MIN_TOKENS // identifiers and short phrases are symbol queries, never a message
1180
+ && !/^(?:how|where|what|why|which|when|who|does|do|is|are|can|find|show|list)\b/i.test(query);
1181
+ }
1182
+
1183
+ // Stable verify order: index evidence first (rarity-weighted overlap of the query tokens with
1184
+ // each file's path and symbol tokens), originals before mirrors, then path order.
1185
+ function rankContentLaneFiles({ files, searchDescriptorsByFile, tokenWeights, queryTokens, includeLikelyTestFiles }) {
1186
+ const rows = [];
1187
+ for (const file of safeItems(files)) {
1188
+ const filePath = file?.filePath;
1189
+ if (typeof filePath !== 'string' || !CONTENT_LANE_CODE_EXT.test(filePath)) {
1190
+ continue;
1191
+ }
1192
+ if (isLikelyTestFilePath(filePath) && !includeLikelyTestFiles) {
1193
+ continue;
1194
+ }
1195
+ let overlap = 0;
1196
+ const descriptor = searchDescriptorsByFile.get(filePath);
1197
+ if (descriptor) {
1198
+ const seen = new Set(descriptor.path.tokens);
1199
+ for (const symbol of descriptor.symbols) {
1200
+ for (const token of symbol.descriptor.tokens) {
1201
+ seen.add(token);
1202
+ }
1203
+ }
1204
+ for (const token of queryTokens) {
1205
+ if (seen.has(token)) {
1206
+ overlap += tokenWeights?.get(token) ?? 1;
1207
+ }
1208
+ }
1209
+ }
1210
+ rows.push({ filePath, overlap, mirror: mirrorPrior(filePath) < 1 ? 1 : 0, size: Number.isFinite(Number(file.size)) && Number(file.size) >= 0 ? Number(file.size) : null });
1211
+ }
1212
+ rows.sort((a, b) => (b.overlap - a.overlap) || (a.mirror - b.mirror) || (a.filePath < b.filePath ? -1 : a.filePath > b.filePath ? 1 : 0));
1213
+ return rows;
1214
+ }
1215
+
1216
+ // Reads files in priority order (batched for speed, consumed in order) until the hit limit or a
1217
+ // count/byte cap. Caps are checked at batch boundaries on counts and byte totals only, so the
1218
+ // same tree yields the same hits regardless of machine load.
1219
+ async function scanContentLane({ absoluteRoot, ordered, tokens, limit }) {
1220
+ const hits = [];
1221
+ const truncated = new Set();
1222
+ if (tokens.length === 0) {
1223
+ return { hits, truncated: [] };
1224
+ }
1225
+ let scanned = 0;
1226
+ let bytes = 0;
1227
+ const deadline = Date.now() + CONTENT_LANE_SAFETY_MS;
1228
+ for (let start = 0; start < ordered.length && hits.length < limit; start += CONTENT_LANE_BATCH) {
1229
+ if (scanned >= CONTENT_LANE_MAX_FILES) { truncated.add('max-files'); break; }
1230
+ if (bytes >= CONTENT_LANE_MAX_BYTES) { truncated.add('max-bytes'); break; }
1231
+ if (Date.now() > deadline) { truncated.add('time-budget'); break; }
1232
+ const batch = ordered.slice(start, start + Math.min(CONTENT_LANE_BATCH, CONTENT_LANE_MAX_FILES - scanned));
1233
+ // Size comes from the index entry, else a stat; an oversized file is skipped before any read.
1234
+ const texts = await Promise.all(batch.map(async (row) => {
1235
+ try {
1236
+ const absolute = path.join(absoluteRoot, row.filePath);
1237
+ const size = row.size ?? (await fs.stat(absolute)).size;
1238
+ if (size > CONTENT_LANE_MAX_FILE_BYTES) {
1239
+ return { text: '', size: 0 };
1240
+ }
1241
+ const text = await fs.readFile(absolute, 'utf8');
1242
+ return { text, size: Buffer.byteLength(text, 'utf8') };
1243
+ } catch {
1244
+ return { text: '', size: 0 };
1245
+ }
1246
+ }));
1247
+ for (const [index, { text, size }] of texts.entries()) {
1248
+ scanned += 1;
1249
+ bytes += size;
1250
+ if (text.length === 0 || size > CONTENT_LANE_MAX_FILE_BYTES) {
1251
+ continue;
1252
+ }
1253
+ const lower = text.toLowerCase();
1254
+ if (!tokens.every((token) => lower.includes(token))) {
1255
+ continue;
1256
+ }
1257
+ const lines = lower.split('\n');
1258
+ const lineIndex = lines.findIndex((line) => tokens.every((token) => line.includes(token)));
1259
+ if (lineIndex >= 0) {
1260
+ hits.push({ filePath: batch[index].filePath, line: lineIndex + 1 });
1261
+ if (hits.length >= limit) {
1262
+ break;
1263
+ }
1264
+ }
1265
+ }
1266
+ }
1267
+ if (scanned >= CONTENT_LANE_MAX_FILES && scanned < ordered.length && hits.length < limit) {
1268
+ truncated.add('max-files');
1269
+ }
1270
+ return { hits, truncated: [...truncated] };
1271
+ }
1272
+
956
1273
  function createFileSearchDescriptor({ filePath, symbols }) {
957
1274
  return {
958
1275
  path: buildTextSearchDescriptor(filePath),
959
1276
  symbols: symbols.map((symbol) => ({
960
1277
  name: symbol.name,
1278
+ local: isLocalSymbolRow(symbol),
961
1279
  descriptor: buildTextSearchDescriptor(symbol.name),
962
1280
  })),
963
1281
  };
@@ -1227,6 +1545,30 @@ export function clearIndexArtifactCache(rootDir = null) {
1227
1545
  }
1228
1546
  }
1229
1547
 
1548
+ // Names of the symbols a query result matched, parsed from its `symbol_exact:` /
1549
+ // `symbol_token:` reasons; feed to getFileOutline({ focus }).
1550
+ export function queryMatchedSymbolNames(result) {
1551
+ const names = [];
1552
+ for (const reason of result?.reasons ?? []) {
1553
+ const match = /^symbol_(?:exact|compact|contains):([^:]+)$/.exec(reason) ?? /^symbol_token:([^:]+):/.exec(reason);
1554
+ if (match) {
1555
+ names.push(match[1]);
1556
+ }
1557
+ }
1558
+ return names;
1559
+ }
1560
+
1561
+ // Non-enumerable (array equality and JSON stay unchanged), like boundedSourceSearch rows.truncated:
1562
+ // `contentLane` is true when the content search ran; `contentLaneTruncated` lists why it was cut
1563
+ // short ('verify-cap', 'time-budget', 'max-files', ...) so a partial scan never reads as "no match".
1564
+ function withContentLaneStatus(results, ran, truncated) {
1565
+ // results arrive frozen, so copy before attaching the status and freeze again.
1566
+ const out = [...results];
1567
+ Object.defineProperty(out, 'contentLane', { value: ran, enumerable: false });
1568
+ Object.defineProperty(out, 'contentLaneTruncated', { value: Object.freeze([...truncated]), enumerable: false });
1569
+ return Object.freeze(out);
1570
+ }
1571
+
1230
1572
  function freezeQueryResults(results) {
1231
1573
  return Object.freeze(
1232
1574
  results.map((item) => Object.freeze({
@@ -83,7 +83,7 @@ export async function resolveContext({
83
83
  }
84
84
 
85
85
  if (queryWasUsed) {
86
- const queryResults = await queryCodeIndex({ rootDir, query: intent, limit: budget.maxFiles });
86
+ const queryResults = await queryCodeIndex({ rootDir, query: intent, limit: budget.maxFiles, contentLane: false });
87
87
  for (const qr of queryResults) {
88
88
  addExplainedFile(result, 'primaryTargets', qr.filePath, buildQueryReason(qr));
89
89
  }
@@ -37,6 +37,34 @@ async function prune(dir, keep) {
37
37
  } catch { /* retention is best-effort */ }
38
38
  }
39
39
 
40
+ function emit(text, p, env) {
41
+ if (!text) return NONE;
42
+ const omp = p.ukit_harness === 'omp' || env.UKIT_HARNESS === 'omp';
43
+ const stdout = omp
44
+ ? `${text}\n`
45
+ : `${JSON.stringify({ hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: text } })}\n`;
46
+ return { code: 0, stdout, stderr: '' };
47
+ }
48
+
49
+ // null: not a memory source (fall through to the graph hint); '' clean; else one advisory line.
50
+ async function memoryAdvisory(projectRoot, rel, config, signal) {
51
+ try {
52
+ const recall = await import('../ukit/runtime/memory-recall.mjs');
53
+ const entries = recall.normalizeMemorySources(config ?? {});
54
+ // Sync pre-check: no directory listing for edits that are not a memory source.
55
+ if (!recall.matchesMemorySource(entries, rel)) return null;
56
+ const sources = await recall.expandMemorySources(projectRoot, entries);
57
+ if (!sources.includes(rel)) return null;
58
+ const check = await import('../ukit/runtime/memory-check.mjs');
59
+ const res = await check.checkMemorySources(projectRoot, {
60
+ sources, engine: { tokenize: recall.tokenize, parseMarkdownBullets: recall.parseMarkdownBullets }, files: [rel],
61
+ });
62
+ return signal.aborted ? '' : check.formatCheckLine(res);
63
+ } catch {
64
+ return '';
65
+ }
66
+ }
67
+
40
68
  async function hint(ctx, signal, commit) {
41
69
  const { payload: p = {}, projectRoot, env = {} } = ctx;
42
70
  if (env.UKIT_EDIT_HINTS === '0' || process.env.UKIT_EDIT_HINTS === '0') return NONE;
@@ -55,9 +83,42 @@ async function hint(ctx, signal, commit) {
55
83
  const task = typeof route?.taskInstanceId === 'string' && route.taskInstanceId ? route.taskInstanceId : session;
56
84
  const stateFile = path.join(projectRoot, '.ukit/storage/cache/edit-hints', `${session}.${safe(p.agent_id, 'main')}.json`);
57
85
  const prev = await readJson(stateFile);
86
+ const strs = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === 'string') : []);
58
87
  const st = prev && prev.task === task && Array.isArray(prev.seen)
59
- ? { task, seen: prev.seen.filter((x) => typeof x === 'string'), shownMem: Array.isArray(prev.shownMem) ? prev.shownMem.filter((x) => typeof x === 'string') : [] }
60
- : { task, seen: [], shownMem: [] };
88
+ ? { task, seen: strs(prev.seen), shownMem: strs(prev.shownMem), advised: strs(prev.advised) }
89
+ : { task, seen: [], shownMem: [], advised: [] };
90
+ const persist = async () => {
91
+ st.seen = st.seen.slice(-MAX_SEEN);
92
+ st.shownMem = st.shownMem.slice(-MAX_SEEN);
93
+ st.advised = st.advised.slice(-MAX_SEEN);
94
+ let body = JSON.stringify(st);
95
+ while (body.length > MAX_BYTES && st.seen.length > 1) {
96
+ st.seen.shift();
97
+ st.shownMem.shift();
98
+ st.advised.shift();
99
+ body = JSON.stringify(st);
100
+ }
101
+ try {
102
+ await fsp.mkdir(path.dirname(stateFile), { recursive: true });
103
+ const tmp = `${stateFile}.${process.pid}.tmp`;
104
+ await fsp.writeFile(tmp, body);
105
+ await fsp.rename(tmp, stateFile);
106
+ } catch { /* state is best-effort */ }
107
+ void prune(path.dirname(stateFile), path.basename(stateFile));
108
+ };
109
+ // 3.5.4: an edited memory source gets the checker's advisory line instead of a graph hint,
110
+ // once per task per finding (the line names file:line and the code), so a project that
111
+ // stays over the cap is not nagged on every edit.
112
+ if (/^docs\/.+\.md$/i.test(rel)) {
113
+ const advisory = await memoryAdvisory(projectRoot, rel, config, signal);
114
+ if (advisory !== null) {
115
+ if (!advisory || st.advised.includes(advisory) || signal.aborted) return NONE;
116
+ commit.on = true;
117
+ st.advised.push(advisory);
118
+ await persist();
119
+ return emit(advisory, p, env);
120
+ }
121
+ }
61
122
  if (st.seen.includes(rel)) return NONE;
62
123
 
63
124
  const lanes = await import('../ukit/runtime/memory-lanes.mjs');
@@ -74,21 +135,7 @@ async function hint(ctx, signal, commit) {
74
135
  commit.on = true;
75
136
  st.seen.push(rel);
76
137
  st.shownMem.push(...(out?.memoryIds || []));
77
- st.seen = st.seen.slice(-MAX_SEEN);
78
- st.shownMem = st.shownMem.slice(-MAX_SEEN);
79
- let body = JSON.stringify(st);
80
- while (body.length > MAX_BYTES && st.seen.length > 1) {
81
- st.seen.shift();
82
- st.shownMem.shift();
83
- body = JSON.stringify(st);
84
- }
85
- try {
86
- await fsp.mkdir(path.dirname(stateFile), { recursive: true });
87
- const tmp = `${stateFile}.${process.pid}.tmp`;
88
- await fsp.writeFile(tmp, body);
89
- await fsp.rename(tmp, stateFile);
90
- } catch { /* state is best-effort */ }
91
- void prune(path.dirname(stateFile), path.basename(stateFile));
138
+ await persist();
92
139
 
93
140
  const text = typeof out?.text === 'string' ? out.text.trim() : '';
94
141
  if (!text) return NONE;
@@ -0,0 +1,35 @@
1
+ // session-episode-start.mjs - SessionStart in-proc chain step (3.5.4).
2
+ //
3
+ // Episodes must not depend on SessionEnd alone: a long-lived session (or a crash) never
4
+ // reaches it. On every SessionStart source (startup, resume, clear, compact) this step
5
+ // launches ONE detached, throttled worker that records the finished tasks of earlier
6
+ // ledgers and of the still-open session (`ukit memory episode --start`) - same
7
+ // writeEpisode/backfill code and dedupe keys as SessionEnd, so a later SessionEnd is a
8
+ // no-op for what is already written.
9
+ //
10
+ // Advisory end-to-end: always { code: 0 }, no output, async fs only, bounded by the chain
11
+ // deadline. The CLI resolution (and the stale-CLI refusal) happens inside the detached
12
+ // worker, never here.
13
+
14
+ import { fileURLToPath, pathToFileURL } from 'node:url';
15
+ import path from 'node:path';
16
+
17
+ const OK = { code: 0, stdout: '', stderr: '' };
18
+
19
+ export async function runHook(ctx = {}) {
20
+ try {
21
+ const payload = ctx && typeof ctx.payload === 'object' && ctx.payload ? ctx.payload : {};
22
+ const root = typeof ctx.projectRoot === 'string' && ctx.projectRoot
23
+ ? ctx.projectRoot
24
+ : (typeof payload.cwd === 'string' && payload.cwd ? payload.cwd : process.env.CLAUDE_PROJECT_DIR);
25
+ if (!root) return OK;
26
+ const here = path.dirname(fileURLToPath(import.meta.url));
27
+ const trigger = await import(pathToFileURL(path.join(here, '..', 'ukit', 'runtime', 'self-improve-trigger.mjs')).href);
28
+ const budget = Number(ctx.deadlineMs) > 0 ? Number(ctx.deadlineMs) : 2000;
29
+ await Promise.race([
30
+ trigger.triggerEpisodeStart({ projectRoot: root }),
31
+ new Promise((resolve) => { setTimeout(resolve, Math.max(100, budget - 200)).unref?.(); }),
32
+ ]);
33
+ } catch { /* advisory */ }
34
+ return OK;
35
+ }
@@ -161,16 +161,21 @@ function runBounded(cmd, args, { timeout, cwd, env, shell } = {}) {
161
161
  try {
162
162
  trigger = await import(require("url").pathToFileURL(process.argv[4]).href);
163
163
  } catch {}
164
- const located = trigger?.resolveCliLocator?.(projectRoot) ?? null;
165
- const cli = located
166
- ? { cmd: located.node, pre: [located.bin], shell: false }
164
+ // 3.5.4: one shared resolver. cli.json wins; a PATH `ukit` older than the project
165
+ // runtime is refused (it writes the wrong record shape) and the reason is left in
166
+ // .ukit/storage/learning/episode-cli.json for `ukit doctor`. The PATH version probe is
167
+ // cached by binary path + mtime and async, inside this armed step.
168
+ const cli = trigger?.resolveEpisodeCli
169
+ ? await trigger.resolveEpisodeCli(projectRoot)
167
170
  : { cmd: "ukit", pre: [], shell: true };
171
+ if (cli.reason) return;
168
172
 
169
- const probe = await runBounded(cli.cmd, [...cli.pre, "--version"], { timeout: 2000 });
173
+ const probe = await runBounded(cli.cmd, [...cli.pre, "--version"], { timeout: 2000, shell: cli.shell });
170
174
  if (!probe.ok) return;
171
175
 
172
176
  await runBounded(cli.cmd, [...cli.pre, "memory", "episode"], {
173
177
  timeout: 4000,
178
+ shell: cli.shell,
174
179
  cwd: projectRoot,
175
180
  env: sessionId
176
181
  ? { ...process.env, UKIT_SESSION_ID: sessionId }
@@ -234,8 +234,8 @@
234
234
  "hooks": [
235
235
  {
236
236
  "type": "command",
237
- "command": "node \"$CLAUDE_PROJECT_DIR/.claude/ukit/runtime/hook-chain-runner.mjs\" --emit-verdict - \"$CLAUDE_PROJECT_DIR/.claude/hooks/project-important.sh\":8 \"$CLAUDE_PROJECT_DIR/.claude/hooks/auto-prune-bash.sh\":12 \"$CLAUDE_PROJECT_DIR/.claude/hooks/reset-compact-pressure.sh\":12 \"$CLAUDE_PROJECT_DIR/.claude/hooks/handoff-resume.sh\":8 \"$CLAUDE_PROJECT_DIR/.claude/hooks/observability-emit.mjs\":8",
238
- "timeout": 60
237
+ "command": "node \"$CLAUDE_PROJECT_DIR/.claude/ukit/runtime/hook-chain-runner.mjs\" --emit-verdict - \"$CLAUDE_PROJECT_DIR/.claude/hooks/project-important.sh\":8 \"$CLAUDE_PROJECT_DIR/.claude/hooks/auto-prune-bash.sh\":12 \"$CLAUDE_PROJECT_DIR/.claude/hooks/reset-compact-pressure.sh\":12 \"$CLAUDE_PROJECT_DIR/.claude/hooks/handoff-resume.sh\":8 \"$CLAUDE_PROJECT_DIR/.claude/hooks/observability-emit.mjs\":8 \"$CLAUDE_PROJECT_DIR/.claude/hooks/session-episode-start.mjs\":8",
238
+ "timeout": 70
239
239
  }
240
240
  ]
241
241
  }