@ngockhoale/ukit 3.5.3 → 3.5.4

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 (59) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/manifests/documentation.yaml +12 -1
  3. package/manifests/platform.full.yaml +12 -0
  4. package/package.json +27 -1
  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 +77 -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 +27 -0
  11. package/src/core/memory/mutateMemory.js +37 -1
  12. package/src/core/memory/observations.js +192 -0
  13. package/src/core/memory/recallEngine.js +120 -7
  14. package/src/core/memory/records.js +23 -0
  15. package/src/core/memory/writeGate.js +137 -0
  16. package/src/core/runtimeConfig.js +11 -1
  17. package/src/index/buildIndex.js +26 -0
  18. package/src/index/paths.js +3 -1
  19. package/src/index/queryIndex.js +225 -10
  20. package/src/index/resolveContext.js +1 -1
  21. package/template_project/.claude/hooks/edit-hint.mjs +64 -17
  22. package/template_project/.claude/hooks/session-episode-start.mjs +35 -0
  23. package/template_project/.claude/hooks/session-episode.sh +9 -4
  24. package/template_project/.claude/settings.json +2 -2
  25. package/template_project/.claude/ukit/index/lib/index-core.mjs +255 -12
  26. package/template_project/.claude/ukit/index/query-index.mjs +14 -3
  27. package/template_project/.claude/ukit/index/route-task.mjs +4 -1
  28. package/template_project/.claude/ukit/runtime/memory-check.mjs +241 -0
  29. package/template_project/.claude/ukit/runtime/memory-recall.mjs +120 -7
  30. package/template_project/.claude/ukit/runtime/memory-validity.mjs +28 -1
  31. package/template_project/.claude/ukit/runtime/self-improve-trigger.mjs +212 -5
  32. package/template_project/.omp/hooks/pre/ukit-bridge.js +2 -1
  33. package/template_project/ukit/storage/config.json +4 -2
  34. package/src/core/agentRuntime/completionGate.js +0 -179
  35. package/src/core/agentRuntime/context.js +0 -571
  36. package/src/core/agentRuntime/qualityComparison.js +0 -112
  37. package/src/core/agentRuntime/resourcePolicy.js +0 -78
  38. package/src/core/agentRuntime/runtimeSupport.js +0 -255
  39. package/src/core/codeintel/manifest.js +0 -176
  40. package/src/core/experiments/deliberation.js +0 -327
  41. package/src/core/experiments/dynamicWorkflow.js +0 -507
  42. package/src/core/handoffDocValidator.js +0 -154
  43. package/src/core/memory/deltaOverlays.js +0 -493
  44. package/src/core/memory/learningCandidates.js +0 -347
  45. package/src/core/observability/emit/sessionBoot.js +0 -407
  46. package/src/core/observability/evaluation/replay.js +0 -149
  47. package/src/core/observability/evaluation/scorecard.js +0 -465
  48. package/src/core/observability/schema/compatibility.js +0 -180
  49. package/src/core/observability/support/renderer.js +0 -130
  50. package/src/core/reviewPanelAggregate.js +0 -204
  51. package/src/core/taskBudgetValidator.js +0 -251
  52. package/src/core/taskProgressGuard.js +0 -227
  53. package/src/decision/reviewVerdict.js +0 -339
  54. package/src/decision/runtimeFilter.js +0 -150
  55. package/src/decision/runtimeScheduler.js +0 -239
  56. package/src/index/contextPack.js +0 -285
  57. package/src/index/impactContext.js +0 -360
  58. package/src/index/reviewPolicy.js +0 -368
  59. package/src/index/sensitivePath.js +0 -21
@@ -0,0 +1,241 @@
1
+ // memoryCheck.js — memory rules + deterministic checker for memory source files
2
+ // (3.5.4). ONE file, node: builtins only, shipped byte-identical as
3
+ // template_project/.claude/ukit/runtime/memory-check.mjs (the PostToolUse advisory
4
+ // and Codex-less hosts use the mirror; `ukit memory check` and the record write
5
+ // gate use this copy). The recall engine and the secret scanner are injected, so
6
+ // there is exactly one tokenizer and one scanner. Async fs only; never throws.
7
+ //
8
+ // violation (exit 1) warning (exit 0)
9
+ // core-over-lines / core-over-bytes no-evidence (bullet names no existing
10
+ // line-too-long (core) / bullet-too-long file path or commit)
11
+ // secret-detected / near-duplicate
12
+ // unknown-supersedes / duplicate-id
13
+ //
14
+ // no-evidence is only a warning for markdown: pre-3.5.4 notes without a path are
15
+ // not rejected retroactively. Record writes (writeGate.js) enforce it.
16
+ import fsp from 'node:fs/promises';
17
+ import path from 'node:path';
18
+
19
+ // Caps. Measured on the live corpus before the split: longest hand-written
20
+ // bullet 1315 chars, longest record text 114, episodes sliced to 900.
21
+ export const RECORD_TEXT_MAX = 1200;
22
+ export const CORE_MAX_LINES = 150;
23
+ export const CORE_MAX_BYTES = 12000; // "12 KB" core cap, decimal so 12,000 is a hard number
24
+ export const CORE_LINE_MAX = 400; // one line per item in the core
25
+ export const DETAIL_BULLET_MAX = 1500; // verbatim detail bullets in docs/memory/*
26
+ export const CORE_FILE = 'docs/MEMORY.md';
27
+
28
+ // Lexical near-duplicate: Jaccard over the BM25 tokens. Measured (3.5.4) on
29
+ // docs/MEMORY.md (68 notes) + live records: the highest similarity between two
30
+ // distinct hand-written notes is 0.250 (markdown/markdown), 0.080 (markdown/
31
+ // record); 0.80 flags only near-restatements. Generated "Verification pattern
32
+ // ... failed N x" records reach 0.917 with each other, so generated writers are
33
+ // exempt in writeGate.js (GENERATED_CREATORS) and never rejected.
34
+ export const NEAR_DUP_JACCARD = 0.8;
35
+ export const NEAR_DUP_MIN_TOKENS = 6;
36
+
37
+ // Machine-generated writers: templated text, expected to resemble each other.
38
+ export const GENERATED_CREATORS = Object.freeze(new Set([
39
+ 'episode-hook', 'propose', 'learning-observe', 'learning-candidate',
40
+ 'delta-overlay', 'migration', 'system', 'observe',
41
+ ]));
42
+
43
+ export function jaccard(aSet, bSet) {
44
+ if (aSet.size === 0 || bSet.size === 0) return 0;
45
+ let inter = 0;
46
+ for (const t of aSet) if (bSet.has(t)) inter += 1;
47
+ return inter / (aSet.size + bSet.size - inter);
48
+ }
49
+
50
+ /**
51
+ * findNearDuplicate(text, candidates, tokenize, threshold) -> { id, similarity } | null
52
+ * candidates: [{ id, text }]. Notes under NEAR_DUP_MIN_TOKENS tokens are skipped
53
+ * (a short note shares most of its tokens by chance).
54
+ */
55
+ export function findNearDuplicate(text, candidates, tokenize, threshold = NEAR_DUP_JACCARD) {
56
+ const a = new Set(tokenize(String(text ?? '')));
57
+ if (a.size < NEAR_DUP_MIN_TOKENS) return null;
58
+ let best = null;
59
+ for (const c of candidates) {
60
+ const b = new Set(tokenize(String(c?.text ?? '')));
61
+ if (b.size < NEAR_DUP_MIN_TOKENS) continue;
62
+ const similarity = jaccard(a, b);
63
+ if (similarity >= threshold && (!best || similarity > best.similarity)) best = { id: c.id, similarity };
64
+ }
65
+ return best;
66
+ }
67
+
68
+ // Markdown supersession convention (honoured by recallEngine.parseMarkdownBullets):
69
+ // [id: slug] names a bullet (slug = [a-z0-9][a-z0-9._-]*)
70
+ // [supersedes: slug] the bullet replaces the bullet(s) so named
71
+ const MD_ID_RE = /\[id:\s*([a-z0-9][a-z0-9._-]*)\s*\]/gi;
72
+ // dir/file paths, or a bare file name with a known extension
73
+ const PATH_WORD_RE = /(?<![\w./@-])(?:[\w@.-]+\/)+[\w@.-]+(?::\d+)?|(?<![\w./@-])[\w-]+\.(?:m?js|cjs|json|md|sh|ya?ml|ts|py)\b/g;
74
+ const SHA_RE = /\b[0-9a-f]{7,40}\b/;
75
+
76
+ async function exists(root, rel) {
77
+ const clean = rel.replace(/[:#].*$/, '').replace(/^\.\//, '');
78
+ if (!clean || clean.startsWith('/') || clean.split('/').includes('..')) return false;
79
+ try {
80
+ await fsp.stat(path.join(root, clean));
81
+ return true;
82
+ } catch {
83
+ return false;
84
+ }
85
+ }
86
+
87
+ // A note is "about code" when it names a code token (camelCase, snake_case, call(),
88
+ // --flag, dotted config key). Owner preferences, process rules and release history
89
+ // name none, cannot point at a file, and never warn. The heading skip covers prose
90
+ // sections whose notes happen to mention a flag.
91
+ const CODE_TOKEN_RE = /\b[a-z]+[A-Z]\w*\b|\b[a-z0-9]+_[a-z0-9_]+\b|\b\w+\(\)|(?<![\w-])--[a-z][\w-]+|\b[a-z]+\.[a-z]+\.[a-z]\w*\b|\b[\w-]+\.(?:m?js|cjs|json|sh|ya?ml|ts)\b/;
92
+ const PROSE_HEADING_RE = /protocol|release history|preference|process|owner/i;
93
+ export function isCodeNote(doc) {
94
+ return !PROSE_HEADING_RE.test(doc?.heading ?? '') && CODE_TOKEN_RE.test(doc?.text ?? '');
95
+ }
96
+
97
+ /** A bullet is evidenced when it names an existing project path (backticked) or a commit. */
98
+ export async function bulletNamesExistingFile(root, text) {
99
+ // Parsed bullet text has its backticks stripped, so match path-shaped words.
100
+ let tried = 0;
101
+ for (const m of text.matchAll(PATH_WORD_RE)) {
102
+ if (tried >= 12) break;
103
+ tried += 1;
104
+ if (await exists(root, m[0])) return true;
105
+ }
106
+ return SHA_RE.test(text) && /\b(commit|sha)\b/i.test(text);
107
+ }
108
+
109
+ const DETAIL_RE = /Detail:\s+(\S+\.md)/g;
110
+
111
+ /**
112
+ * missingDetailPointers(root, text) -> [{ line, rel, why }]: core lines whose
113
+ * `Detail: <file>.md` pointer escapes the project root or names a missing file.
114
+ */
115
+ export async function missingDetailPointers(root, text) {
116
+ const out = [];
117
+ const base = path.resolve(root);
118
+ const lines = String(text).split('\n');
119
+ for (let i = 0; i < lines.length; i += 1) {
120
+ for (const m of lines[i].replace(/`/g, '').matchAll(DETAIL_RE)) {
121
+ const abs = path.resolve(base, m[1]);
122
+ if (abs !== base && !abs.startsWith(base + path.sep)) { out.push({ line: i + 1, rel: m[1], why: 'escapes the project root' }); continue; }
123
+ try { await fsp.stat(abs); } catch { out.push({ line: i + 1, rel: m[1], why: 'does not exist' }); }
124
+ }
125
+ }
126
+ return out;
127
+ }
128
+
129
+ /**
130
+ * checkMemorySources(projectRoot, { sources, engine, scan, files }) ->
131
+ * { ok, violations: [{file,line,code,message}], warnings, stats }
132
+ * sources: relative .md paths (normalizeMemorySources). engine: { tokenize,
133
+ * parseMarkdownBullets }. scan: (text) -> { labels: string[] } (optional).
134
+ * files: limit the report to these paths (every source is still read, so
135
+ * cross-file duplicates and supersession slugs resolve).
136
+ */
137
+ export async function checkMemorySources(projectRoot, { sources, engine, scan = null, files = null } = {}) {
138
+ const root = path.resolve(projectRoot);
139
+ const violations = [];
140
+ const warnings = [];
141
+ const stats = { files: 0, bullets: 0, coreLines: 0, coreBytes: 0 };
142
+ const all = [];
143
+ const raw = new Map();
144
+ for (const rel of sources ?? []) {
145
+ let text;
146
+ try { text = await fsp.readFile(path.join(root, rel), 'utf8'); } catch { continue; }
147
+ stats.files += 1;
148
+ raw.set(rel, text);
149
+ for (const doc of engine.parseMarkdownBullets(text, rel)) all.push({ ...doc, rel });
150
+ }
151
+ const focus = (rel) => !files || files.includes(rel);
152
+ const add = (list, rel, line, code, message) => { if (focus(rel)) list.push({ file: rel, line, code, message }); };
153
+
154
+ const core = raw.get(CORE_FILE);
155
+ if (core !== undefined) {
156
+ const lines = core.split('\n');
157
+ const lineCount = core.endsWith('\n') ? lines.length - 1 : lines.length;
158
+ const bytes = Buffer.byteLength(core, 'utf8');
159
+ stats.coreLines = lineCount;
160
+ stats.coreBytes = bytes;
161
+ if (lineCount > CORE_MAX_LINES) {
162
+ add(violations, CORE_FILE, lineCount, 'core-over-lines',
163
+ `core has ${lineCount} lines (max ${CORE_MAX_LINES}); merge items or move detail to docs/memory/`);
164
+ }
165
+ if (bytes > CORE_MAX_BYTES) {
166
+ add(violations, CORE_FILE, 1, 'core-over-bytes',
167
+ `core is ${bytes} bytes (max ${CORE_MAX_BYTES}); merge items or move detail to docs/memory/`);
168
+ }
169
+ for (const p of await missingDetailPointers(root, core)) {
170
+ add(violations, CORE_FILE, p.line, 'missing-detail', `Detail pointer ${p.rel} ${p.why}; point at an existing file under docs/memory/`);
171
+ }
172
+ lines.forEach((line, i) => {
173
+ if (/^[-*]\s/.test(line) && line.length > CORE_LINE_MAX) {
174
+ add(violations, CORE_FILE, i + 1, 'line-too-long',
175
+ `core item is ${line.length} chars (max ${CORE_LINE_MAX}); keep one line, move detail to docs/memory/`);
176
+ }
177
+ });
178
+ }
179
+ for (const [rel, text] of raw) {
180
+ if (rel === CORE_FILE) continue;
181
+ text.split('\n').forEach((line, i) => {
182
+ if (/^[-*]\s/.test(line) && line.length > DETAIL_BULLET_MAX) {
183
+ add(violations, rel, i + 1, 'bullet-too-long', `bullet is ${line.length} chars (max ${DETAIL_BULLET_MAX}); split it`);
184
+ }
185
+ });
186
+ }
187
+
188
+ stats.bullets = all.length;
189
+ const slugs = new Map();
190
+ for (const [rel, text] of raw) {
191
+ text.split('\n').forEach((line, i) => {
192
+ for (const m of line.matchAll(MD_ID_RE)) {
193
+ const slug = m[1].toLowerCase();
194
+ if (slugs.has(slug)) add(violations, rel, i + 1, 'duplicate-id', `id "${slug}" already used at ${slugs.get(slug)}`);
195
+ else slugs.set(slug, `${rel}:${i + 1}`);
196
+ }
197
+ });
198
+ }
199
+ for (const doc of all) {
200
+ for (const slug of doc.supersedesSlugs ?? []) {
201
+ if (!slugs.has(slug)) add(violations, doc.rel, doc.line, 'unknown-supersedes', `supersedes "${slug}" but no bullet has [id: ${slug}]`);
202
+ }
203
+ if (scan && focus(doc.rel)) {
204
+ let labels = [];
205
+ try { labels = scan(doc.text)?.labels ?? []; } catch { labels = []; }
206
+ if (labels.length > 0) add(violations, doc.rel, doc.line, 'secret-detected', `secret-like value (${labels.join(', ')}); remove it`);
207
+ }
208
+ if (focus(doc.rel) && isCodeNote(doc) && !(await bulletNamesExistingFile(root, doc.text))) {
209
+ add(warnings, doc.rel, doc.line, 'no-evidence', 'names no existing file path or commit; add the path this note is about');
210
+ }
211
+ }
212
+ // A superseded bullet may legitimately resemble its replacement.
213
+ const replaced = new Set();
214
+ for (const doc of all) for (const s of doc.supersedesSlugs ?? []) { const t = slugs.get(s); if (t) replaced.add(t); }
215
+ const live = all.filter((d) => !replaced.has(`${d.rel}:${d.line}`));
216
+ const seen = new Set();
217
+ const tokenCache = new Map();
218
+ const memoTokenize = (t) => {
219
+ let v = tokenCache.get(t);
220
+ if (!v) { v = engine.tokenize(t); tokenCache.set(t, v); }
221
+ return v;
222
+ };
223
+ for (let i = 0; i < live.length; i += 1) {
224
+ if (!focus(live[i].rel)) continue;
225
+ const dup = findNearDuplicate(live[i].text, live.slice(0, i).map((d) => ({ id: `${d.rel}:${d.line}`, text: d.text })), memoTokenize);
226
+ if (dup && !seen.has(dup.id)) {
227
+ seen.add(dup.id);
228
+ add(violations, live[i].rel, live[i].line, 'near-duplicate',
229
+ `${Math.round(dup.similarity * 100)}% lexical overlap with ${dup.id}; merge them or tag one [supersedes: <slug>]`);
230
+ }
231
+ }
232
+ return { ok: violations.length === 0, violations, warnings, stats };
233
+ }
234
+
235
+ /** One advisory line for the first violation, '' when clean. */
236
+ export function formatCheckLine(res) {
237
+ const v = res?.violations?.[0];
238
+ if (!v) return '';
239
+ const more = res.violations.length > 1 ? ` (+${res.violations.length - 1} more)` : '';
240
+ return `[UKit memory] ${v.file}:${v.line} ${v.code}: ${v.message}${more}. Run \`ukit memory check\`.`;
241
+ }
@@ -0,0 +1,27 @@
1
+ // memoryCheckRun.js — `ukit memory check [--json] [--file <rel>]...` runner (3.5.4).
2
+ // Binds the shared checker (memoryCheck.js) to the real engine, secret scanner and config.
3
+ import { loadRuntimeConfig } from '../runtimeConfig.js';
4
+ import { loadSensitiveAllowlist, scanText } from '../sensitiveValueScanner.js';
5
+ import { checkMemorySources } from './memoryCheck.js';
6
+ import { expandMemorySources, normalizeMemorySources, parseMarkdownBullets, tokenize } from './recallEngine.js';
7
+
8
+ export async function runMemoryCheck(projectRoot, args = []) {
9
+ const json = args.includes('--json');
10
+ const files = [];
11
+ for (let i = 0; i < args.length; i += 1) if (args[i] === '--file' && args[i + 1]) files.push(args[i + 1]);
12
+ const config = await loadRuntimeConfig(projectRoot).catch(() => ({}));
13
+ const allowlistHashes = loadSensitiveAllowlist(config);
14
+ const res = await checkMemorySources(projectRoot, {
15
+ sources: await expandMemorySources(projectRoot, normalizeMemorySources(config)),
16
+ engine: { tokenize, parseMarkdownBullets },
17
+ scan: (text) => scanText(text, { allowlistHashes }),
18
+ files: files.length > 0 ? files : null,
19
+ });
20
+ if (json) return { ok: res.ok, output: JSON.stringify(res, null, 2) };
21
+ const rows = [
22
+ `[UKit] memory check: ${res.ok ? 'OK' : 'VIOLATIONS'} — ${res.stats.files} file(s), ${res.stats.bullets} note(s), core ${res.stats.coreLines} lines / ${res.stats.coreBytes} bytes`,
23
+ ...res.violations.map((v) => ` violation ${v.file}:${v.line} ${v.code} — ${v.message}`),
24
+ ];
25
+ if (res.warnings.length > 0) rows.push(` ${res.warnings.length} warning(s) (notes naming no existing file); \`--json\` lists them`);
26
+ return { ok: res.ok, output: rows.join('\n') };
27
+ }
@@ -26,6 +26,7 @@ import {
26
26
  import { createRecord, normalizeRecord } from './records.js';
27
27
  import { guardWritePayload, redactWritePayload } from './writeGuard.js';
28
28
  import { classifyWrite, mergeEvidence, REVIEW_QUEUE_MAX } from './writeClassification.js';
29
+ import { checkRecordWrite } from './writeGate.js';
29
30
  import { resolveMemoryStage, appendRolloutReceipt, latencyBandForMs } from './memoryFlags.js';
30
31
  import { resolveProjectIdentity } from './projectIdentity.js';
31
32
  import { captureAnchors } from '../../../template_project/.claude/ukit/runtime/memory-validity.mjs';
@@ -101,7 +102,7 @@ export async function mutateMemory(target, envelope = {}, { classifyAdd, guard,
101
102
  const { recordsPath, paths } = resolveTarget(target);
102
103
  const journalPath = path.join(paths.memoryV2Dir, 'mutation-journal.json');
103
104
  const { op, payload, idempotencyKey, expectedRevision, expectedGeneration,
104
- confirmScope } = envelope;
105
+ confirmScope, actor } = envelope;
105
106
  if (!OPS.has(op)) {
106
107
  return { status: 'rejected', reason: `unknown-op:${String(op)}` };
107
108
  }
@@ -132,6 +133,18 @@ export async function mutateMemory(target, envelope = {}, { classifyAdd, guard,
132
133
  }
133
134
  receiptStage = writerStage === 'shadow' ? 'shadow' : null;
134
135
  }
136
+ // 3.5.4 write gate (writeGate.js), memory.writeGate: 'strict' (default) | 'off'
137
+ // (false is accepted as off). Strict = evidence + near-duplicate + size, applied
138
+ // to agent writes (envelope.actor 'agent', i.e. the CLI) and to EVERY write to
139
+ // the user store, with or without an actor (it is cross-project, so a duplicate
140
+ // there is the costliest). Other library callers (storeV2, tests, tools) get
141
+ // the size cap only. 'off' disables the whole gate. The user store has no
142
+ // project config, so the CLI passes the project's mode in envelope.writeGate.
143
+ const gateMode = envelope.writeGate !== undefined ? envelope.writeGate : config?.memory?.writeGate;
144
+ const gateOn = gateMode !== false && gateMode !== 'off';
145
+ // The user store has no project root, so file evidence is not required there
146
+ // (writeGate.js skips it); size, secrets and near-duplicates still apply.
147
+ const gateStrict = gateOn && (actor === 'agent' || target.kind === 'user');
135
148
  const guardFn = guard ?? ((p) => guardWritePayload(p, { config, projectId }));
136
149
 
137
150
  try {
@@ -228,6 +241,17 @@ export async function mutateMemory(target, envelope = {}, { classifyAdd, guard,
228
241
  appliedRecordId = record.id;
229
242
  return { result: { status: 'ok', record, reason: 'review' }, records: [...records, record], postWrite };
230
243
  }
244
+ if (gateOn && decision?.action !== 'ENRICH') {
245
+ const gate = await checkRecordWrite({
246
+ record,
247
+ records,
248
+ projectRoot: target.kind === 'project' ? target.projectRoot : null,
249
+ op: 'add',
250
+ strict: gateStrict,
251
+ replaces: decision?.targetIds ?? (decision?.targetId ? [decision.targetId] : []),
252
+ });
253
+ if (!gate.ok) return { result: { status: 'rejected', reason: gate.reason, code: gate.code } };
254
+ }
231
255
  if (decision?.action === 'ENRICH' && decision.targetId) {
232
256
  const targetRec = records.find((r) => r.id === decision.targetId);
233
257
  if (targetRec) {
@@ -269,6 +293,18 @@ export async function mutateMemory(target, envelope = {}, { classifyAdd, guard,
269
293
  if (guarded && guarded.ok === false) {
270
294
  return { result: { status: 'rejected', reason: guarded.reason ?? 'secret-detected', labels: guarded.labels } };
271
295
  }
296
+ if (gateOn && op === 'update') {
297
+ const gate = await checkRecordWrite({
298
+ record: { ...existing, ...patch },
299
+ records,
300
+ projectRoot: target.kind === 'project' ? target.projectRoot : null,
301
+ op: 'update',
302
+ strict: gateStrict,
303
+ patch,
304
+ replaces: [existing.id],
305
+ });
306
+ if (!gate.ok) return { result: { status: 'rejected', reason: gate.reason, code: gate.code } };
307
+ }
272
308
  const patched = applyRecordPatch(records, existing.id, {
273
309
  ...patch, revision: (existing.revision ?? 1) + 1,
274
310
  });
@@ -0,0 +1,192 @@
1
+ // observations.js (3.5.4) - observation records: consolidated notes backed by >= 2
2
+ // episodes. UKit's part is deterministic: validate evidence (validateObservation)
3
+ // and propose candidates from content-bearing episodes (proposeObservations,
4
+ // `ukit memory observe`). The running agent decides and writes through the
5
+ // mutateMemory gate; nothing here writes, and no hook calls the proposer.
6
+ import path from 'node:path';
7
+
8
+ import { readJsonIfExists } from '../fileOps.js';
9
+ import { buildRuntimePaths } from '../runtimePaths.js';
10
+ import { readRecordStore } from './recordStore.js';
11
+ import { observationEvidenceErrors } from './records.js';
12
+ import { ARCHIVE_FILE } from './episodeArchive.js';
13
+ import { stripPromptBoilerplate } from './episodes.js';
14
+
15
+ export const DEFAULT_MIN_SUPPORT = 3;
16
+ const MAX_CANDIDATES = 20;
17
+ const MAX_EPISODE_IDS = 8;
18
+ const FOLLOW_WINDOW = 3;
19
+
20
+ /** Ids of every episode in the live store and in the episodes archive. */
21
+ export async function loadEpisodeIdsFor(projectRoot) {
22
+ const paths = buildRuntimePaths(projectRoot);
23
+ const ids = new Set();
24
+ const store = await readRecordStore(paths.memoryV2RecordsPath);
25
+ for (const r of store.records) if (r.type === 'episode') ids.add(r.id);
26
+ const archive = await readJsonIfExists(path.join(paths.memoryV2Dir, ARCHIVE_FILE)).catch(() => null);
27
+ for (const r of Array.isArray(archive?.records) ? archive.records : []) if (r?.id) ids.add(r.id);
28
+ return ids;
29
+ }
30
+
31
+ /**
32
+ * validateObservation(record, { loadEpisodeIds }) -> { ok, errors }
33
+ * loadEpisodeIds: async () => Set|Array of existing episode ids (store + archive).
34
+ * Non-observation records pass. Fails closed when existence cannot be checked.
35
+ */
36
+ export async function validateObservation(record, { loadEpisodeIds } = {}) {
37
+ if (record?.type !== 'observation') return { ok: true, errors: [] };
38
+ const errors = observationEvidenceErrors(record);
39
+ if (errors.length > 0) return { ok: false, errors };
40
+ if (typeof loadEpisodeIds !== 'function') return { ok: false, errors: ['cannot verify evidence: loadEpisodeIds missing'] };
41
+ let known;
42
+ try { known = new Set(await loadEpisodeIds()); } catch { return { ok: false, errors: ['cannot verify evidence: episode lookup failed'] }; }
43
+ const missing = [...new Set(record.meta.evidence.episodes)].filter((id) => !known.has(id));
44
+ if (missing.length > 0) return { ok: false, errors: [`evidence episodes not found in store or archive: ${missing.join(', ')}`] };
45
+ return { ok: true, errors: [] };
46
+ }
47
+
48
+ /**
49
+ * validateObservationEvidence(record, { projectRoot }) -> { ok, reason? }
50
+ * Write-gate entry: validateObservation with the id lookup bound to the store + archive.
51
+ */
52
+ export async function validateObservationEvidence(record, { projectRoot } = {}) {
53
+ const res = await validateObservation(record, { loadEpisodeIds: () => loadEpisodeIdsFor(projectRoot) });
54
+ return res.ok ? { ok: true } : { ok: false, reason: res.errors[0] };
55
+ }
56
+
57
+ const isContent =(r) => r?.type === 'episode' && Array.isArray(r.evidence) && r.meta?.verify
58
+ && r.evidence.some((e) => e?.kind === 'file');
59
+ const filesOf = (r) => [...new Set(r.evidence.filter((e) => e?.kind === 'file').map((e) => e.locator))];
60
+ const stemOf = (p) => path.basename(p).replace(/\.(test|spec)\.[^.]+$/, '').replace(/\.[^.]+$/, '');
61
+ const isTest = (p) => /(^|\/)tests?\//.test(p) || /\.(test|spec)\./.test(p);
62
+ const stripMirror = (p) => p.replace(/^template_project\//, '');
63
+ const isMirror = (src, other) => src !== other && stripMirror(src) === stripMirror(other);
64
+ const isTestOf = (src, other) => !isTest(src) && isTest(other) && stemOf(src) === stemOf(other);
65
+
66
+ // Identity of the TASK behind an episode, so restarts count once. Priority: an explicit handoff
67
+ // task id (TASK-xxx) in the prompt or the written files; else the prompt after boilerplate strip.
68
+ // Older stored episodes may still carry the boilerplate, so the strip is applied here as well.
69
+ const TASK_ID_RE = /\bTASK-[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*\b/;
70
+ export function taskIdentity(ep) {
71
+ const text = String(ep?.text ?? '');
72
+ const prompt = (text.match(/^Task: (.*?)(?:\. Wrote: |$)/s) ?? [])[1] ?? '';
73
+ const id = prompt.match(TASK_ID_RE)?.[0]
74
+ ?? filesOf(ep).map((f) => f.match(/(?:^|\/)(TASK-[^/]*)\.md$/)?.[1]).find(Boolean);
75
+ if (id) return `id:${id}`;
76
+ const norm = stripPromptBoilerplate(prompt).toLowerCase().slice(0, 120);
77
+ return norm ? `prompt:${norm}` : `ep:${ep?.id}`;
78
+ }
79
+
80
+ /** Content-bearing episodes, oldest first. */
81
+ export function contentEpisodes(records) {
82
+ return (Array.isArray(records) ? records : []).filter(isContent)
83
+ .sort((a, b) => (a.created_at ?? 0) - (b.created_at ?? 0));
84
+ }
85
+
86
+ const ids = (eps) => eps.slice(-MAX_EPISODE_IDS).map((e) => e.id);
87
+ const cand = (kind, key, files, text, eps) => {
88
+ const episodes = [...new Set(ids(eps))];
89
+ return { kind, key, files, text, support: eps.length, evidence: { episodes, count: episodes.length } };
90
+ };
91
+
92
+ // The ONE noise rule for co-write pairs: run bookkeeping (anything under docs/AI_HANDOFF/,
93
+ // or INDEX.md / RUN.md / TASK-*.md anywhere) is always written together by the handoff
94
+ // process itself, so a pair where BOTH files are bookkeeping says nothing about the code.
95
+ export const isBookkeeping = (f) => /^docs\/AI_HANDOFF\//.test(f) || /(^|\/)(INDEX\.md|RUN\.md|TASK-[^/]*\.md)$/.test(f);
96
+ // Living-status / log docs are touched by nearly every task; a pair with EITHER of them says
97
+ // nothing about the code. A hub file (in more than HUB_SHARE of all content episodes) co-occurs
98
+ // with everything for the same reason, so pairs involving it are dropped too.
99
+ export const isLivingStatusDoc = (f) => /^(docs\/(STATUS|WORKLOG|TASKS)\.md|CHANGELOG\.md)$/.test(f) || /^docs\/AI_HANDOFF\//.test(f);
100
+ export const HUB_SHARE = 0.3;
101
+ const HUB_MIN_EPISODES = 10; // below this the share is too noisy to call anything a hub
102
+
103
+ /**
104
+ * proposeObservations(records, { minSupport }) -> candidates (pure).
105
+ * kinds: co-write (file pair in >= N episodes), repeated-fail (file mostly in
106
+ * failed-verify episodes), follow-up (mirror/test written later than its source).
107
+ */
108
+ export function proposeObservations(records, { minSupport = DEFAULT_MIN_SUPPORT, existingKeys = new Set() } = {}) {
109
+ const eps = contentEpisodes(records);
110
+ const out = [];
111
+ const byFile = new Map();
112
+ const pairs = new Map();
113
+ for (const ep of eps) {
114
+ const files = filesOf(ep).sort();
115
+ for (const f of files) {
116
+ if (!byFile.has(f)) byFile.set(f, []);
117
+ byFile.get(f).push(ep);
118
+ }
119
+ for (let i = 0; i < files.length; i += 1) {
120
+ for (let j = i + 1; j < files.length; j += 1) {
121
+ const k = `${files[i]}\u0000${files[j]}`;
122
+ if (!pairs.has(k)) pairs.set(k, []);
123
+ pairs.get(k).push(ep);
124
+ }
125
+ }
126
+ }
127
+ for (const [k, list] of pairs) {
128
+ const [a, b] = k.split('\u0000');
129
+ if (isBookkeeping(a) && isBookkeeping(b)) continue;
130
+ if (isLivingStatusDoc(a) || isLivingStatusDoc(b)) continue;
131
+ if (eps.length >= HUB_MIN_EPISODES
132
+ && (byFile.get(a).length / eps.length > HUB_SHARE || byFile.get(b).length / eps.length > HUB_SHARE)) continue;
133
+ const base = Math.min(byFile.get(a).length, byFile.get(b).length);
134
+ if (list.length < minSupport || list.length / base < 0.5) continue;
135
+ out.push(cand('co-write', `co-write:${a}|${b}`, [a, b],
136
+ `Editing ${a} usually also touches ${b} (written together in ${list.length} episodes).`, list));
137
+ }
138
+ for (const [f, list] of byFile) {
139
+ if (isBookkeeping(f) || isLivingStatusDoc(f)) continue;
140
+ // Restarts of one task are ONE failure: count distinct tasks, keep one episode per task.
141
+ const byTask = new Map();
142
+ for (const e of list) {
143
+ const t = taskIdentity(e);
144
+ const slot = byTask.get(t) ?? { failed: null, any: e };
145
+ if (e.meta.verify === 'fail') slot.failed = e;
146
+ byTask.set(t, slot);
147
+ }
148
+ const failed = [...byTask.values()].filter((s) => s.failed).map((s) => s.failed);
149
+ if (failed.length < minSupport || failed.length / byTask.size < 0.5) continue;
150
+ out.push(cand('repeated-fail', `repeated-fail:${f}`, [f],
151
+ `Tasks editing ${f} often end with a failed verify (${failed.length} of ${byTask.size} tasks).`, failed));
152
+ }
153
+ for (const src of byFile.keys()) {
154
+ for (const other of byFile.keys()) {
155
+ const kind = isMirror(src, other) ? 'mirror' : (isTestOf(src, other) ? 'test' : null);
156
+ if (!kind) continue;
157
+ const srcOnly = byFile.get(src).filter((e) => !filesOf(e).includes(other));
158
+ const followed = srcOnly.filter((e) => {
159
+ const at = eps.indexOf(e);
160
+ return eps.slice(at + 1, at + 1 + FOLLOW_WINDOW).some((n) => filesOf(n).includes(other));
161
+ });
162
+ if (followed.length < minSupport) continue;
163
+ out.push(cand('follow-up', `follow-up:${src}|${other}`, [src, other],
164
+ `${other} (${kind} of ${src}) is usually edited in a later task after ${src} (${followed.length} times); update it in the same task.`, followed));
165
+ }
166
+ }
167
+ return out.filter((c) => !existingKeys.has(c.key))
168
+ .sort((x, y) => y.support - x.support || x.key.localeCompare(y.key)).slice(0, MAX_CANDIDATES);
169
+ }
170
+
171
+ /** `ukit memory observe [--json] [--min N]` - proposes only; never writes. */
172
+ export async function runObserve(projectRoot, rest = []) {
173
+ const json = rest.includes('--json');
174
+ const at = rest.indexOf('--min');
175
+ const minSupport = at >= 0 && Number(rest[at + 1]) >= 2 ? Number(rest[at + 1]) : DEFAULT_MIN_SUPPORT;
176
+ const store = await readRecordStore(buildRuntimePaths(projectRoot).memoryV2RecordsPath);
177
+ const existingKeys = new Set(store.records.filter((r) => r.type === 'observation' && r.meta?.key).map((r) => r.meta.key));
178
+ const content = contentEpisodes(store.records);
179
+ const candidates = proposeObservations(store.records, { minSupport, existingKeys });
180
+ if (json) {
181
+ console.log(JSON.stringify({ episodes: content.length, minSupport, candidates }, null, 2));
182
+ return { episodes: content.length, candidates };
183
+ }
184
+ console.log(`[UKit] observe: ${content.length} content-bearing episode(s), ${candidates.length} candidate(s) (min support ${minSupport})`);
185
+ for (const c of candidates) {
186
+ console.log(`- [${c.kind}] ${c.text}\n evidence: ${c.evidence.count} episodes ${c.evidence.episodes.join(' ')}`);
187
+ }
188
+ if (candidates.length > 0) {
189
+ console.log('Proposals only: write one through the memory write gate as type=observation with meta.evidence={episodes,count} and meta.key=<key>.');
190
+ }
191
+ return { episodes: content.length, candidates };
192
+ }