ruvnet-brain 4.6.4 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +1 -1
  2. package/bin/install.mjs +6 -50
  3. package/config/practical-rule-catalog.json +22 -22
  4. package/console/app.js +1 -1
  5. package/console/architecture.html +2 -2
  6. package/package.json +1 -2
  7. package/plugin/.claude-plugin/plugin.json +1 -1
  8. package/plugin/.codex-plugin/plugin.json +1 -1
  9. package/plugin/commands/checkpoint.md +22 -40
  10. package/plugin/commands/rnb-brief.md +18 -15
  11. package/plugin/hooks/codex-hooks.json +1 -28
  12. package/plugin/hooks/hook-contracts.json +6 -127
  13. package/plugin/hooks/hooks.json +1 -40
  14. package/plugin/mcp/managed-cli-interface.mjs +0 -56
  15. package/plugin/scripts/agentdb-recall-reader.mjs +29 -9
  16. package/plugin/scripts/agentdb-recall.mjs +8 -14
  17. package/plugin/scripts/brain-footprint.mjs +1 -1
  18. package/plugin/scripts/codex-hook-wrapper.mjs +13 -3
  19. package/plugin/scripts/completion-claim-evidence.mjs +1 -1
  20. package/plugin/scripts/continuity-events.mjs +90 -2
  21. package/plugin/scripts/continuity-hook-policy.mjs +5 -14
  22. package/plugin/scripts/decision-gate.mjs +2 -2
  23. package/plugin/scripts/decision-record.mjs +11 -40
  24. package/plugin/scripts/hook-failures.mjs +16 -0
  25. package/plugin/scripts/hook-shim.mjs +1 -2
  26. package/plugin/scripts/learn-capture.mjs +60 -42
  27. package/plugin/scripts/learning-store.mjs +70 -60
  28. package/plugin/scripts/node-sqlite.mjs +1 -1
  29. package/plugin/scripts/ruflo-bin.mjs +71 -1
  30. package/plugin/scripts/ruflo-memory-hooks.mjs +132 -0
  31. package/plugin/scripts/runtime-preferences.mjs +1 -1
  32. package/plugin/scripts/session-snapshot-hook.mjs +85 -341
  33. package/plugin/scripts/session-start-core.mjs +2 -12
  34. package/plugin/scripts/signal-watch.mjs +1 -1
  35. package/plugin/scripts/substitution-wall.mjs +222 -0
  36. package/plugin/scripts/turn-outcome-capture.mjs +68 -74
  37. package/scripts/codex-host-execution-proof.mjs +1 -1
  38. package/scripts/console-engine.mjs +0 -32
  39. package/scripts/health-repair.mjs +1 -81
  40. package/scripts/hook-qualify-core.mjs +2 -2
  41. package/scripts/model-managed-workflow-service.mjs +9 -10
  42. package/scripts/model-routing-controller.mjs +9 -8
  43. package/scripts/no-silent-substitution.mjs +65 -1
  44. package/scripts/onboarding-console.mjs +8 -23
  45. package/scripts/product-integrity-contract.mjs +1 -1
  46. package/scripts/release-qualification-contract.mjs +9 -27
  47. package/scripts/remedy-registry.mjs +0 -18
  48. package/scripts/wired-check.mjs +1 -5
  49. package/plugin/scripts/continuity-brief.mjs +0 -351
  50. package/plugin/scripts/continuity-journal.mjs +0 -553
  51. package/plugin/scripts/learn-flush.mjs +0 -96
  52. package/plugin/scripts/learning-observation.mjs +0 -51
  53. package/plugin/scripts/learning-queue.mjs +0 -135
  54. package/plugin/scripts/learning-worker-supervisor.mjs +0 -79
  55. package/plugin/scripts/project-capture-queue.mjs +0 -357
  56. package/plugin/scripts/project-memory-enrollment.mjs +0 -203
  57. package/plugin/scripts/project-progression-checkpoint.mjs +0 -145
  58. package/plugin/scripts/project-progression-contract.mjs +0 -496
  59. package/plugin/scripts/project-progression-hook.mjs +0 -233
  60. package/plugin/scripts/project-progression-outbox.mjs +0 -225
  61. package/plugin/scripts/project-progression-producer.mjs +0 -309
  62. package/plugin/scripts/project-progression-reader.mjs +0 -304
  63. package/plugin/scripts/project-progression-session-start.mjs +0 -320
  64. package/plugin/scripts/project-progression-sources.mjs +0 -246
  65. package/plugin/scripts/project-progression-store.mjs +0 -723
  66. package/plugin/scripts/project-progression-suspension.mjs +0 -60
  67. package/plugin/scripts/project-transition-hook.mjs +0 -267
  68. package/plugin/scripts/session-snapshot-contract.mjs +0 -105
  69. package/plugin/scripts/turn-transport-journal.mjs +0 -108
  70. package/scripts/qa/progression-validation-benchmark.mjs +0 -66
  71. package/scripts/session-snapshot-contract.mjs +0 -1
@@ -1,21 +1,37 @@
1
1
  #!/usr/bin/env node
2
- // PostToolUse captures only a fixed workflow vocabulary, then schedules bounded orphan recovery.
3
2
  import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { randomUUID, createHash } from 'node:crypto';
3
+ import os from 'node:os';
6
4
  import { spawn } from 'node:child_process';
7
5
  import { fileURLToPath } from 'node:url';
8
6
  import { readStdinBounded } from './hook-input.mjs';
9
7
  import { learningContext } from './runtime-preferences.mjs';
10
- import { learningTarget } from './learning-store.mjs';
11
- import { safeQueue, safeAction, writeExclusive, takeQueueLock, releaseQueueLock, queueFiles } from './learning-queue.mjs';
8
+ import { recordLearningObservation, distillObservations } from './learning-store.mjs';
9
+ import { resolveRuflo } from './ruflo-bin.mjs';
10
+ import { recordHookFailure } from './hook-failures.mjs';
11
+
12
+ // Fixed vocabulary: arbitrary executable names, arguments, basenames and paths never persist.
13
+ const verbs = new Set('git npm node python python3 cargo rustc go gh cd ls rg cat export mkdir rm cp mv bash sh curl wget npx ruflo'.split(' '));
14
+ const subs = new Set('status diff log show add commit push pull fetch checkout switch branch test run install ci build check fmt clippy auth workflow release memory hooks'.split(' '));
15
+ export function safeAction(tool, value) {
16
+ if (typeof value !== 'string') return null;
17
+ if (tool === 'Bash') {
18
+ const [verb, sub] = value.trim().split(/\s+/, 3);
19
+ if (!verbs.has(verb)) return 'command';
20
+ return subs.has(sub) ? `${verb} ${sub}` : verb;
21
+ }
22
+ if (!['Write', 'Edit', 'MultiEdit'].includes(tool)) return null;
23
+ // Legacy action is minimized again before invocation; no private basename reaches Ruflo argv.
24
+ return 'edit file';
25
+ }
12
26
 
13
27
  /**
14
- * One PostToolUse observation. Since ADR-106 it runs IN-PROCESS from session-snapshot's PostToolUse
15
- * boundary (one registration instead of two); the script entry below stays for explicit invocation.
16
- * learningContext() owns consent and brain-off, so the fold keeps ADR-054's OFF contract.
28
+ * One PostToolUse observation, written DIRECTLY with `ruflo memory store` + exact-key readback (owner
29
+ * ruling 2026-10-12: no local queue, no flush worker, no replay). It runs in-process from
30
+ * session-snapshot's PostToolUse boundary. learningContext() owns consent and brain-off. A refused or
31
+ * unverified write is LOUD: one row in the hook-failures ledger, summarized at the next SessionStart.
17
32
  */
18
- export function captureLearning(input) {
33
+ export function captureLearning(input, { env = process.env, binary = resolveRuflo({ env }), brainHome, detach = false } = {}) {
34
+ let tool; let action;
19
35
  try {
20
36
  const context = learningContext();
21
37
  if (!context.enabled || !input || typeof input !== 'object') return false;
@@ -28,48 +44,50 @@ export function captureLearning(input) {
28
44
  if (input.error || input.is_error === true || response?.error || response?.interrupted === true || response?.signal
29
45
  || response?.success === false || response?.is_error === true || response?.isError === true
30
46
  || [response?.exit_code, response?.exitCode, response?.status].some(value => Number.isInteger(value) && value !== 0)) return false;
31
- const tool = input.tool_name;
47
+ tool = input.tool_name;
32
48
  if (!['Bash', 'Write', 'Edit', 'MultiEdit'].includes(tool)) return false;
33
- const action = safeAction(tool, tool === 'Bash' ? input.tool_input?.command : 'edit file');
49
+ action = safeAction(tool, tool === 'Bash' ? input.tool_input?.command : 'edit file');
34
50
  if (!action) return false;
35
- const fresh = learningContext();
36
- if (!fresh.enabled || fresh.queueDir !== context.queueDir) return false;
37
- learningTarget(fresh); // Existing authorized user store is required before creating any user queue bytes.
38
- const dir = safeQueue(fresh, true);
39
- const sid = createHash('sha256').update(String(input.session_id || process.env.CLAUDE_SESSION_ID || 'default')).digest('hex').slice(0, 24);
40
- writeExclusive(path.join(dir, `session-${sid}-${randomUUID()}.jsonl`), JSON.stringify({ tool, action }) + '\n');
41
- // Per-scope lock in the flusher suppresses duplicate workers; capture never waits on the learner.
42
- const token = takeQueueLock(fresh);
43
- if (!token) return true;
44
- const child = spawn(process.execPath, [fileURLToPath(new URL('./learn-flush.mjs', import.meta.url)), '--supervisor'], {
45
- cwd: fresh.projectDir, detached: true, stdio: 'ignore', windowsHide: true, env: { ...process.env, RUVNET_LEARN_WORKER_TOKEN: token },
46
- });
47
- child.on('error', () => releaseQueueLock(fresh, token)); child.unref();
51
+ if (!binary) throw new Error('ruflo not found');
52
+ const observation = { tool, action, session: String(input.session_id || '') };
53
+ // Review M3: on the hook path the store + exact readback run in a detached, bounded child, so the
54
+ // tool call returns at once; that child writes the ledger row if the write is refused or unverified.
55
+ if (detach) return detachLearning('--observe', { env, brainHome, observation });
56
+ recordLearningObservation(binary, context, observation, { env, deadline: Date.now() + 6000 });
48
57
  return true;
49
- } catch { return false; /* advisory transport; unsafe/malformed input produces no persisted payload */ }
58
+ } catch (error) {
59
+ if (tool && action) recordHookFailure({ hook: 'learn-capture', event: 'PostToolUse', error: String(error?.message || error).slice(0, 160) }, brainHome ? { brainHome } : undefined);
60
+ return false;
61
+ }
50
62
  }
51
63
 
52
- /**
53
- * Lifecycle recovery (formerly its own SessionStart/SessionEnd registration, ADR-106): when learning
54
- * is enabled, start learn-flush detached so an orphaned queue is drained by one finite worker. The
55
- * caller never waits; learn-flush owns the lock, the deadline and the consent re-checks.
56
- */
57
- export function scheduleLearningRecovery({ cwd = process.cwd(), env = process.env, spawnFn = spawn } = {}) {
64
+ /** Detached, bounded child (`--observe` one observation, `--distill` the namespace). Only fixed-vocabulary fields cross. */
65
+ export function detachLearning(mode, { env = process.env, brainHome, observation } = {}) {
66
+ const child = spawn(process.execPath, [fileURLToPath(import.meta.url), mode], { cwd: process.cwd(), detached: true, stdio: 'ignore', windowsHide: true,
67
+ env: { ...env, ...(observation ? { RUVNET_LEARN_OBSERVATION: JSON.stringify(observation) } : {}), ...(brainHome ? { RUVNET_BRAIN_HOME: brainHome } : {}) } });
68
+ child.unref();
69
+ return true;
70
+ }
71
+
72
+ function runDetached(mode) {
73
+ const fail = (error) => recordHookFailure({ hook: mode === '--distill' ? 'learn-distill' : 'learn-capture', event: mode === '--distill' ? 'SessionEnd' : 'PostToolUse', error: String(error?.message || error).slice(0, 160) });
58
74
  try {
59
- const context = learningContext({ env, cwd });
60
- if (!context.enabled) return false;
61
- // Nothing queued, nothing to recover. Spawning anyway cost a process at every session boundary and,
62
- // on Windows, a live worker holds the project directory as its cwd (EBUSY on delete/move).
63
- if (queueFiles(context, { limit: 1, deadline: Date.now() + 100 }).length === 0) return false;
64
- const child = spawnFn(process.execPath, [fileURLToPath(new URL('./learn-flush.mjs', import.meta.url))], {
65
- cwd, detached: true, stdio: 'ignore', windowsHide: true, env });
66
- child.on?.('error', () => {}); child.unref?.();
67
- return true;
68
- } catch { return false; }
75
+ const context = learningContext(); // consent is re-read: a withdrawn consent writes nothing
76
+ if (!context.enabled) return;
77
+ const binary = resolveRuflo({ env: process.env });
78
+ if (!binary) throw new Error('ruflo not found');
79
+ if (mode === '--distill') { distillObservations(binary, context, { env: process.env }); return; }
80
+ const o = JSON.parse(process.env.RUVNET_LEARN_OBSERVATION || '{}');
81
+ if (!['Bash', 'Write', 'Edit', 'MultiEdit'].includes(o.tool) || typeof o.action !== 'string') throw new Error('invalid detached observation');
82
+ recordLearningObservation(binary, context, { tool: o.tool, action: o.action, session: String(o.session || '') }, { env: process.env, deadline: Date.now() + 14_000, allowed: () => learningContext().enabled });
83
+ } catch (error) { fail(error); }
69
84
  }
70
85
 
71
86
  const isMain = (() => { try { return Boolean(process.argv[1]) && fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); } catch { return false; } })();
72
- if (isMain) {
87
+ if (isMain && ['--observe', '--distill'].includes(process.argv[2])) {
88
+ runDetached(process.argv[2]);
89
+ process.exit(0);
90
+ } else if (isMain) {
73
91
  try {
74
92
  if (learningContext().enabled) captureLearning(JSON.parse((await readStdinBounded({ maxBytes: 65536, emptyMs: 150 })).toString()));
75
93
  } catch { /* malformed input produces no persisted payload */ }
@@ -1,15 +1,13 @@
1
1
  // Canonical workflow observations; existing Ruflo is the sole writer, never the legacy JSON hook store.
2
2
  import fs from 'node:fs';
3
+ import os from 'node:os';
3
4
  import path from 'node:path';
4
5
  import { createHash } from 'node:crypto';
5
6
  import { spawnSync } from 'node:child_process';
6
7
  import { resolveProjectStore } from './project-store-resolver.mjs';
7
- import { withProgressionReader } from './project-progression-reader.mjs';
8
- import { rufloRunDir } from './project-progression-store.mjs';
9
- import { rufloInvocation } from './ruflo-bin.mjs';
8
+ import { rufloInvocation, rufloRunDir, resolveRuflo } from './ruflo-bin.mjs';
10
9
  import { loadRuntimePreferences } from './runtime-preferences.mjs';
11
- import { loadNodeSqlite } from './node-sqlite.mjs';
12
- import { safeQueue, writeAtomic } from './learning-queue.mjs';
10
+ import { learningContext } from './runtime-preferences.mjs';
13
11
 
14
12
  export const LEARNING_NAMESPACE = 'learning-observations';
15
13
  export function learningTarget(context, { env = process.env, explicitLegacyApply = false } = {}) {
@@ -18,7 +16,8 @@ export function learningTarget(context, { env = process.env, explicitLegacyApply
18
16
  const root = path.join(context.home, '.claude', 'global-memory');
19
17
  const chosen = loadRuntimePreferences({ env, cwd: context.projectDir }).values.learningScope === 'user';
20
18
  if (!chosen && !explicitLegacyApply) throw new Error('user learning requires persisted user consent or explicit legacy Apply');
21
- safeQueue({ scope: 'user', home: context.home, queueDir: root });
19
+ const rootStat = fs.lstatSync(root);
20
+ if (!rootStat.isDirectory() || rootStat.isSymbolicLink()) throw new Error('user learning root is not a real directory');
22
21
  const db = path.join(root, '.swarm', 'memory.db');
23
22
  if (!fs.lstatSync(db).isFile()) throw new Error('user store not adopted');
24
23
  const resolved = resolveProjectStore({ projectDir: root, requestedStorePath: db, gitTimeoutMs: 1000 });
@@ -26,80 +25,91 @@ export function learningTarget(context, { env = process.env, explicitLegacyApply
26
25
  return resolved.canonicalAgentDbPath;
27
26
  }
28
27
 
29
- export function runLearningRuflo(binary, args, db, { env = process.env, deadline = Date.now() + 18_000 } = {}) {
28
+ export function runLearningRuflo(binary, args, db, { env = process.env, deadline = Date.now() + 18_000, maxStepMs = 6000 } = {}) {
30
29
  const remaining = deadline - Date.now();
31
30
  if (remaining <= 0) throw new Error('learning deadline exhausted');
32
- const cwd = rufloRunDir(db, { root: env.RUVNET_RUFLO_CWD_ROOT || path.join(env.HOME, '.cache', 'ruvnet-brain', 'learning-ruflo') });
31
+ const cwd = rufloRunDir(db, { root: env.RUVNET_RUFLO_CWD_ROOT || path.join(env.HOME || env.USERPROFILE || os.homedir(), '.cache', 'ruvnet-brain', 'learning-ruflo') });
33
32
  try {
34
33
  const invocation = rufloInvocation(binary, args);
35
34
  return spawnSync(invocation.executable, invocation.args, { cwd, env: { ...env, RUFLO_DAEMON_AUTOSTART: '0' },
36
- encoding: 'utf8', shell: false, timeout: Math.min(6000, remaining), killSignal: 'SIGKILL', maxBuffer: 65536, windowsHide: true });
35
+ encoding: 'utf8', shell: false, timeout: Math.min(maxStepMs, remaining), killSignal: 'SIGKILL', maxBuffer: 65536, windowsHide: true });
37
36
  } finally { fs.rmSync(cwd, { force: true, recursive: true }); }
38
37
  }
39
38
 
40
- export function recordLearningObservation(binary, context, file, record, row, options = {}) {
39
+ /**
40
+ * One observation, written DIRECTLY through `ruflo memory store` and proven by exact-key CLI readback.
41
+ * No local queue, no worker, no replay (owner ruling 2026-10-12): a failure throws to the caller, which
42
+ * records it in the hook-failures ledger so SessionStart reports it.
43
+ */
44
+ export function recordLearningObservation(binary, context, { tool, action, session = '' }, options = {}) {
41
45
  const db = learningTarget(context, options);
42
- const key = 'workflow-' + createHash('sha256').update(path.basename(file) + ':' + record.key).digest('hex');
43
- const value = JSON.stringify({ schemaVersion: 1, tool: row.tool, action: row.action, scope: context.scope,
46
+ // DETERMINISTIC per (session, tool, action) (review N1): with --no-upsert, rUv's store itself dedupes, so a
47
+ // session holds at most one row per distinct pair and concurrent writers of the same pair collapse.
48
+ const key = 'workflow-' + createHash('sha256').update(JSON.stringify([session, tool, action])).digest('hex');
49
+ const value = JSON.stringify({ schemaVersion: 1, tool, action, scope: context.scope,
44
50
  authoritative: false, provenance: 'system-observation', outcome: 'host-reported-success' });
45
51
  const run = args => { if (options.allowed && !options.allowed()) throw new Error('learning consent changed'); return runLearningRuflo(binary, args, db, options); };
46
- const existing = withProgressionReader(db, reader => reader.readContent(LEARNING_NAMESPACE, key));
47
- if (!(existing.ok && existing.value === value)) {
48
- const stored = run(['memory', 'store', '--key', key, '--value', value, '--namespace', LEARNING_NAMESPACE,
49
- '--no-upsert', '--provenance', 'system_observation', '--path', db]);
50
- if (stored.error || stored.status !== 0) throw new Error('canonical observation write failed');
51
- }
52
+ const stored = run(['memory', 'store', '--key', key, '--value', value, '--namespace', LEARNING_NAMESPACE,
53
+ '--no-upsert', '--provenance', 'system_observation', '--path', db]);
52
54
  const retrieved = run(['memory', 'retrieve', '--key', key, '--namespace', LEARNING_NAMESPACE, '--value-only', '--path', db]);
53
- if (retrieved.error || retrieved.status !== 0 || retrieved.stdout.trim() !== value) throw new Error('canonical exact CLI readback failed');
54
- const independent = withProgressionReader(db, reader => reader.readContent(LEARNING_NAMESPACE, key));
55
- if (!independent.ok || independent.value !== value) throw new Error('canonical independent row readback failed');
56
- return { db, key, digest: createHash('sha256').update(value).digest('hex'), exactCli: true, independentRow: true };
55
+ const exact = !retrieved.error && retrieved.status === 0 && String(retrieved.stdout || '').trim() === value;
56
+ // A refused store whose key already holds these exact bytes is the dedupe working, not a failure.
57
+ if (stored.error || stored.status !== 0) {
58
+ if (exact) return { db, key, digest: createHash('sha256').update(value).digest('hex'), exactCli: true, alreadyStored: true };
59
+ throw new Error(`ruflo memory store refused the observation: ${String(stored.stderr || stored.stdout || stored.error?.message || '').trim().slice(0, 160)}`);
60
+ }
61
+ if (!exact) throw new Error('canonical exact CLI readback failed');
62
+ return { db, key, digest: createHash('sha256').update(value).digest('hex'), exactCli: true };
57
63
  }
58
64
 
59
- export function learningStoreStatus(db) {
60
- const rows = withProgressionReader(db, reader => reader.listKeys(LEARNING_NAMESPACE, { maxEntries: 100_000 }).length);
61
- if (!rows.ok) return { known: false, observations: 0, patterns: null, lastDistillAt: null };
62
- const sqlite = loadNodeSqlite(); let connection;
65
+ /** Observation count through `ruflo memory list` (no direct SQLite). Unknown stays unknown. */
66
+ export function learningStoreStatus(db, { binary = resolveRuflo(), env = process.env } = {}) {
67
+ if (!binary) return { known: false, observations: 0, patterns: null, lastDistillAt: null };
63
68
  try {
64
- connection = new sqlite.DatabaseSync(db, { readOnly: true });
65
- const present = name => !!connection.prepare('SELECT 1 FROM sqlite_master WHERE type=? AND name=?').get('table', name);
66
- const patterns = present('reasoning_patterns') ? connection.prepare('SELECT count(*) AS n FROM reasoning_patterns WHERE json_valid(metadata) AND json_extract(metadata,?)=?').get('$.namespace', LEARNING_NAMESPACE).n : 0;
67
- const at = present('distill_state') ? connection.prepare('SELECT last_run_at AS at FROM distill_state WHERE namespace=?').get(LEARNING_NAMESPACE)?.at : null;
68
- return { known: true, observations: rows.value, patterns, lastDistillAt: at ?? null };
69
- } catch { return { known: false, observations: rows.value, patterns: null, lastDistillAt: null }; }
70
- finally { connection?.close(); }
69
+ const listed = runLearningRuflo(binary, ['memory', 'list', '--namespace', LEARNING_NAMESPACE, '--limit', '100000', '--format', 'json', '--path', db], db, { env });
70
+ const text = String(listed.stdout || ''); const at = text.indexOf('[');
71
+ if (listed.status !== 0 || at < 0) return { known: false, observations: 0, patterns: null, lastDistillAt: null };
72
+ return { known: true, observations: JSON.parse(text.slice(at)).length, patterns: null, lastDistillAt: null };
73
+ } catch { return { known: false, observations: 0, patterns: null, lastDistillAt: null }; }
71
74
  }
72
75
 
73
- /** Existing $0 structural distiller, with a verified native WAL-safe snapshot before mutation. */
76
+ /** `ruflo memory backup` then `ruflo memory distill run`; both are rUv's own commands. */
74
77
  export function distillLearning(binary, context, options = {}) {
75
- const db = learningTarget(context, options); const before = learningStoreStatus(db);
76
- if (!before.known) throw new Error('distillation baseline unavailable');
77
- if (options.automatic && before.lastDistillAt !== null && Date.now() - before.lastDistillAt < 3600_000) {
78
- return { db, completed: false, deferred: 'automatic cadence: at most hourly', patternDelta: 0, ratifiedLessons: 0 };
79
- }
80
- const backupDir = path.join(path.dirname(db), 'learning-backups');
78
+ const db = learningTarget(context, options);
81
79
  const run = args => { if (options.allowed && !options.allowed()) throw new Error('learning consent changed'); return runLearningRuflo(binary, args, db, options); };
82
- if (options.allowed && !options.allowed()) throw new Error('learning consent changed');
83
- safeQueue({ scope: 'project', projectDir: path.dirname(path.dirname(db)), queueDir: backupDir }, true);
84
- const prior = new Set(fs.readdirSync(backupDir));
85
- const backup = run(['memory', 'backup', '--db', db, '--dir', backupDir, '--keep', '3', '--verbose']);
80
+ const backupDir = path.join(path.dirname(db), 'learning-backups');
81
+ fs.mkdirSync(backupDir, { recursive: true, mode: 0o700 });
82
+ const backup = run(['memory', 'backup', '--db', db, '--dir', backupDir, '--keep', '3']);
86
83
  if (backup.error || backup.status !== 0) throw new Error('distillation backup failed');
87
- if (!backup.stdout.includes('memory DB backed up → ') || backup.stdout.includes('byte-copy')) throw new Error('native online backup mode unverified');
88
- const images = fs.readdirSync(backupDir).filter(name => name.endsWith('.db') && !prior.has(name));
89
- if (images.length !== 1) throw new Error('fresh distillation snapshot absent');
90
- const image = path.join(backupDir, images[0]);
91
- fs.chmodSync(image, 0o600);
92
- // The source-bound verbose marker above distinguishes native online backup from
93
- // Ruflo's byte-copy fallback. Row verification is additional observation evidence,
94
- // not a claim that this subset alone proves whole-database rollback.
95
- const source = withProgressionReader(db, reader => reader.listKeys(LEARNING_NAMESPACE, { maxEntries: 100_000 }).map(key => [key, reader.readContent(LEARNING_NAMESPACE, key)]));
96
- const copy = withProgressionReader(image, reader => reader.listKeys(LEARNING_NAMESPACE, { maxEntries: 100_000 }).map(key => [key, reader.readContent(LEARNING_NAMESPACE, key)]));
97
- if (!source.ok || !copy.ok || JSON.stringify(source.value) !== JSON.stringify(copy.value)) throw new Error('distillation snapshot row verification failed');
98
- writeAtomic(path.join(backupDir, 'latest-receipt.json'), JSON.stringify({ db, before, snapshot: image, providerMode: 'native-online-backup', observationsVerified: true, retainedSnapshots: 3, restore: 'automatic exact restore unavailable' }));
99
84
  const distilled = run(['memory', 'distill', 'run', '--db', db, '--namespace', LEARNING_NAMESPACE,
100
85
  '--max-entries', '8', '--judge', 'structural', '--budget-usd', '0']);
101
- const after = learningStoreStatus(db);
102
- return { db, before, after, completed: !distilled.error && distilled.status === 0,
103
- patternDelta: before.known && after.known ? after.patterns - before.patterns : null,
104
- snapshot: image, ratifiedLessons: 0 };
86
+ return { db, completed: !distilled.error && distilled.status === 0, ratifiedLessons: 0 };
87
+ }
88
+
89
+ /**
90
+ * Distill the observation rows with rUv's own `ruflo memory distill run` (incremental cursor, $0
91
+ * structural judge), bounded. Run detached at SessionEnd/PreCompact (review M3). No backup on this path:
92
+ * a whole-DB copy per session end costs more than the distill. A failure throws to the caller (ledger).
93
+ */
94
+ export function distillObservations(binary, context, options = {}) {
95
+ const db = learningTarget(context, options);
96
+ const r = runLearningRuflo(binary, ['memory', 'distill', 'run', '--db', db, '--namespace', LEARNING_NAMESPACE,
97
+ '--max-entries', '500', '--judge', 'structural', '--budget-usd', '0'], db, { ...options, maxStepMs: 60_000, deadline: Date.now() + 60_000 });
98
+ if (r.error || r.status !== 0) throw new Error(`ruflo memory distill run did not complete (${r.error?.code || `exit ${r.status}`})`);
99
+ return { db, completed: true };
100
+ }
101
+
102
+ /** What the console shows: consent, target and the observation count. There is no local queue. */
103
+ export function observeLearning(options = {}) {
104
+ const env = options.env ?? process.env;
105
+ const context = learningContext(options);
106
+ const result = { ...context, queueDepth: 0, queueKnown: true, legacyUserDepth: 0, legacyUserKnown: true, lastTrainSeconds: null, trajectories: 0, statusKnown: false };
107
+ if (!context.enabled) return result;
108
+ try {
109
+ result.learningDb = learningTarget(context, { env });
110
+ const state = learningStoreStatus(result.learningDb, { env });
111
+ result.statusKnown = state.known; result.trajectories = state.observations; result.observations = state.observations;
112
+ result.patterns = state.patterns; result.lastDistillAt = state.lastDistillAt;
113
+ } catch { /* Missing consent/store/readback remains unknown. */ }
114
+ return result;
105
115
  }
@@ -4,7 +4,7 @@
4
4
  * WHY (measured 2026-10-01, Node 22.13.1 locally and Node 22 on the Linux CI full suite, run 36894944735):
5
5
  * on Node 22 the first load of `node:sqlite` prints
6
6
  * (node:PID) ExperimentalWarning: SQLite is an experimental feature and might change at any time
7
- * to stderr. SessionStart reads the store through project-progression-reader.mjs, so every SessionStart
7
+ * to stderr. SessionStart once read the store through a native reader, so every SessionStart
8
8
  * in an adopted git project on Node 22 carried that line — hook noise a customer sees on every session.
9
9
  * Node 24 does not warn, which is why the owner's machine never showed it.
10
10
  *
@@ -2,7 +2,7 @@
2
2
  * ruflo-bin.mjs — the ONE place this repo decides WHERE the global `ruflo` binary is.
3
3
  *
4
4
  * WHY THIS FILE EXISTS. Issues #99 and #105 are one defect filed twice: `scripts/distill-project.mjs`
5
- * and `plugin/scripts/learn-flush.mjs` each hardcoded `~/.npm-global/bin/ruflo` — the owner's npm
5
+ * and the learning flusher each hardcoded `~/.npm-global/bin/ruflo` — the owner's npm
6
6
  * prefix, nobody else's. On a Homebrew, nvm, Volta, or plain `npm -g` install that path does not
7
7
  * exist, so #99 died with "ruflo is not at ~/.npm-global/bin/ruflo" and #105 failed every feed
8
8
  * silently — both on machines where ruflo was installed and sitting on PATH the whole time.
@@ -99,3 +99,73 @@ export function resolveRuflo({ env = process.env, home = os.homedir() } = {}) {
99
99
  */
100
100
  export const RUFLO_MISSING = 'ruflo was not found in ~/.npm-global/bin or anywhere on your PATH'
101
101
  + ' — install it with `npm i -g ruflo@latest`, or set RUFLO_BIN to its full path';
102
+
103
+ // ── Private per-project ruflo working directories ──
104
+ import crypto from 'node:crypto';
105
+
106
+ /**
107
+ * The working directory ruflo runs in. ruflo writes into its cwd on every invocation even when --path
108
+ * names the store (measured 2026-10-01, ruflo 3.49.0): `.claude/`, `.claude-flow/`, `ruvector.db`, and
109
+ * `<cwd>/.swarm/` holding hnsw.metadata.json — the stored snapshot CONTENT — which it also LOADS when it
110
+ * finds one there (ruflo/v3/@claude-flow/cli/src/memory/memory-initializer.ts: getMemoryRoot() = cwd).
111
+ * So the cwd must be:
112
+ * - never the project tree (it changed the customer's working tree and broke no-op capture detection),
113
+ * never inside `.swarm` (nested `.swarm/.swarm`);
114
+ * - PER PROJECT (one shared dir would pool every project's snapshot text and let one project load
115
+ * another's metadata);
116
+ * - private and ours: under the Brain's own home (never a shared /tmp name another user could pre-create
117
+ * or symlink), every directory we create checked to be a real directory, owned by us, mode 0700.
118
+ * Every ruflo call carries --path, so the store it reads and writes is unaffected by the cwd.
119
+ */
120
+ export function rufloScratchRoot(env = process.env) {
121
+ if (env.RUVNET_RUFLO_CWD_ROOT) return path.resolve(env.RUVNET_RUFLO_CWD_ROOT);
122
+ return path.join(env.RUVNET_BRAIN_HOME || path.join(os.homedir(), '.cache', 'ruvnet-brain'), 'ruflo-cwd');
123
+ }
124
+
125
+ /** Create-or-verify one private directory: a real directory (not a link), owned by us, mode 0700. */
126
+ export function ensurePrivateDir(dir) {
127
+ try { fs.mkdirSync(dir, { mode: 0o700 }); } catch (error) { if (error?.code !== 'EEXIST') throw error; }
128
+ const stat = fs.lstatSync(dir);
129
+ if (stat.isSymbolicLink() || !stat.isDirectory()) {
130
+ throw new Error(`ruflo scratch directory ${dir} is not a real directory (symlink or file); refusing to use it`);
131
+ }
132
+ if (process.platform !== 'win32') {
133
+ if (stat.uid !== process.getuid()) {
134
+ throw new Error(`ruflo scratch directory ${dir} is owned by uid ${stat.uid}, not ${process.getuid()}; refusing to use it`);
135
+ }
136
+ if ((stat.mode & 0o777) !== 0o700) {
137
+ fs.chmodSync(dir, 0o700);
138
+ if ((fs.lstatSync(dir).mode & 0o777) !== 0o700) throw new Error(`ruflo scratch directory ${dir} could not be made private (0700)`);
139
+ }
140
+ }
141
+ return dir;
142
+ }
143
+
144
+ export function rufloCwdFor(storePath, { root = rufloScratchRoot() } = {}) {
145
+ const projectKey = crypto.createHash('sha256').update(path.resolve(storePath)).digest('hex').slice(0, 32);
146
+ fs.mkdirSync(path.dirname(root), { recursive: true });
147
+ ensurePrivateDir(root);
148
+ return ensurePrivateDir(path.join(root, projectKey));
149
+ }
150
+
151
+ const RUFLO_CWD_ARTIFACTS = Object.freeze(['.swarm', '.claude', '.claude-flow', 'ruvector.db']);
152
+ const STALE_RUN_MS = 3_600_000;
153
+
154
+ /**
155
+ * One ruflo invocation's working directory: a fresh private `run-*` directory inside the project's
156
+ * scratch dir, removed again by the caller after the call. ruflo copies every snapshot it touches into
157
+ * `<cwd>/.swarm/hnsw.metadata.json`; with one cwd per call that copy never accumulates (4.4.0 kept one
158
+ * per project that grew without bound and outlived the project). Also clears what 4.4.0 left directly
159
+ * in the project scratch dir and any `run-*` older than an hour (a call killed before its cleanup).
160
+ */
161
+ export function rufloRunDir(storePath, { root = rufloScratchRoot(), now = Date.now() } = {}) {
162
+ const projectScratch = rufloCwdFor(storePath, { root });
163
+ for (const name of fs.readdirSync(projectScratch)) {
164
+ const entry = path.join(projectScratch, name);
165
+ let stat;
166
+ try { stat = fs.lstatSync(entry); } catch { continue; }
167
+ const stale = name.startsWith('run-') && stat.isDirectory() && now - stat.mtimeMs > STALE_RUN_MS;
168
+ if (RUFLO_CWD_ARTIFACTS.includes(name) || stale) fs.rmSync(entry, { recursive: true, force: true });
169
+ }
170
+ return fs.mkdtempSync(path.join(projectScratch, 'run-'));
171
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Lifecycle project memory by CALLING Ruflo, never by reimplementing it (owner ruling 2026-10-12).
3
+ *
4
+ * Replaces the hand-rolled progression store / outbox / capture queue on the hook path. Measured on a
5
+ * clone of the owner's store: 1,763 cumulative snapshots (751 MB), a 3.38 GB outbox and 36k queued
6
+ * jobs made Stop, PreCompact and SessionEnd run to the watchdog at 8.5 s. Here every operation is
7
+ * one bounded `ruflo memory store|retrieve|list` call (rUv's documented pattern: exact keys,
8
+ * explicit --namespace, `--path` to the project store), so cost no longer grows with history.
9
+ *
10
+ * • checkpoint(): PreCompact/SessionEnd store one small, append-only `project-state-auto-<ms>` row
11
+ * and read it back by exact key. It never overwrites the owner's `project-state-current-*` notes.
12
+ * • restore(): SessionStart retrieves the newest `project-state-current-*` (and a newer auto
13
+ * checkpoint, if any) by exact key, plus a bounded list of `lessons` keys.
14
+ * `.swarm` is never created: its absence means the project has not adopted the brain.
15
+ */
16
+ import fs from 'node:fs';
17
+ import crypto from 'node:crypto';
18
+ import path from 'node:path';
19
+ import { execFile, execFileSync, spawnSync } from 'node:child_process';
20
+ import { resolveRuflo } from './ruflo-bin.mjs';
21
+ import { resolveProjectStore } from './project-store-resolver.mjs';
22
+
23
+ export const CURATED_PREFIX = 'project-state-current-';
24
+ export const AUTO_PREFIX = 'project-state-auto-';
25
+ const OWNER_NAMESPACES = ['default'];
26
+
27
+ /**
28
+ * The SAME store and namespace turn capture uses (review M2): the canonical project's `.swarm/memory.db`,
29
+ * which a linked git worktree shares with its main checkout. Every git call is bounded by the deadline.
30
+ */
31
+ export function projectStore(projectDir, { deadlineAt = Date.now() + 3000 } = {}) {
32
+ let root = projectDir; let db = path.join(projectDir, '.swarm', 'memory.db');
33
+ try {
34
+ const gitTimeoutMs = Math.max(1, Math.min(1000, Math.floor(deadlineAt - Date.now())));
35
+ const resolved = resolveProjectStore({ projectDir, gitTimeoutMs, deadlineAt });
36
+ root = resolved.projectRoot; db = resolved.canonicalAgentDbPath;
37
+ } catch { /* unresolvable (or out of budget): treated as not adopted below only if no store exists here */ }
38
+ return { root, db, adopted: fs.existsSync(db), namespace: path.basename(root) };
39
+ }
40
+
41
+ /** One `ruflo memory <args>` call, bounded by the caller's deadline. Resolves, never rejects. */
42
+ export function rufloMemory(args, { db, env = process.env, bin = resolveRuflo({ env }), deadlineAt = Date.now() + 3000 } = {}) {
43
+ const timeout = Math.max(1, Math.floor(deadlineAt - Date.now()));
44
+ return new Promise((resolve) => {
45
+ if (!bin) return resolve({ ok: false, stdout: '', error: 'global Ruflo unavailable' });
46
+ execFile(bin, ['memory', ...args, '--path', db], { env, timeout, killSignal: 'SIGKILL', encoding: 'utf8', maxBuffer: 8 * 1024 * 1024 },
47
+ // The ledger gets a fixed reason, never execFile's message (it embeds the whole argv: value, db path).
48
+ (error, stdout) => resolve({ ok: !error, stdout: String(stdout || ''), error: error ? (error.killed ? 'timed out' : `ruflo memory ${args[0]} exited ${error.code ?? 'abnormally'}`) : null }));
49
+ });
50
+ }
51
+
52
+ function parseJson(text) {
53
+ const at = [text.indexOf('['), text.indexOf('{')].filter((i) => i >= 0).sort((a, b) => a - b)[0];
54
+ if (at === undefined) return null;
55
+ try { return JSON.parse(text.slice(at)); } catch { return null; }
56
+ }
57
+ const entriesOf = (json) => (Array.isArray(json) ? json : json?.entries || json?.results || []);
58
+
59
+ async function listKeys(namespace, options, limit = 1000) {
60
+ const r = await rufloMemory(['list', '-n', namespace, '--limit', String(limit), '--format', 'json'], options);
61
+ return r.ok ? entriesOf(parseJson(r.stdout)).filter((e) => typeof e?.key === 'string') : null;
62
+ }
63
+ const retrieve = async (key, namespace, options) => {
64
+ const r = await rufloMemory(['retrieve', '--key', key, '-n', namespace, '--value-only'], options);
65
+ return r.ok ? r.stdout.trim() : null;
66
+ };
67
+ const newest = (rows, prefix) => rows.filter((e) => e.key.startsWith(prefix))
68
+ .sort((a, b) => Number(b.createdAt ?? 0) - Number(a.createdAt ?? 0) || (a.key < b.key ? 1 : -1))[0] || null;
69
+
70
+ function gitFacts(root, deadlineAt) {
71
+ const git = (args) => {
72
+ const timeout = Math.min(500, Math.floor(deadlineAt - Date.now()));
73
+ if (timeout < 1) return 'unmeasured';
74
+ try { return execFileSync('git', args, { cwd: root, encoding: 'utf8', timeout, killSignal: 'SIGKILL', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch { return 'unmeasured'; }
75
+ };
76
+ return { branch: git(['rev-parse', '--abbrev-ref', 'HEAD']), head: git(['rev-parse', '--short', 'HEAD']) };
77
+ }
78
+
79
+ /** PreCompact / SessionEnd: store one small checkpoint and prove it by exact-key readback. */
80
+ export async function checkpoint({ projectDir, event, payload = {}, env = process.env, bin, deadlineAt = Date.now() + 7000, now = Date.now } = {}) {
81
+ if (String(env.RUVNET_TURN_CAPTURE || '').toLowerCase() === 'off') return { stored: false, skipped: 'RUVNET_TURN_CAPTURE=off' };
82
+ const store = projectStore(projectDir, { deadlineAt });
83
+ if (!store.adopted) return { stored: false, skipped: 'project has not adopted a .swarm/memory.db' };
84
+ // A short session hash keeps two sessions' same-millisecond checkpoints apart; --no-upsert makes a
85
+ // collision a refusal (ledger row) instead of a silent overwrite (review m1).
86
+ const sid = crypto.createHash('sha256').update(String(payload.session_id ?? process.pid)).digest('hex').slice(0, 8);
87
+ const key = `${AUTO_PREFIX}${now()}-${sid}`;
88
+ const value = JSON.stringify({ kind: 'automatic-checkpoint', event, session: payload.session_id ?? null,
89
+ at: new Date(now()).toISOString(), ...gitFacts(projectDir, deadlineAt) });
90
+ const options = { db: store.db, env, bin, deadlineAt };
91
+ const stored = await rufloMemory(['store', '--key', key, '-n', store.namespace, '--value', value, '--no-upsert'], options);
92
+ if (!stored.ok) return { stored: false, key, skipped: `store failed: ${stored.error}` };
93
+ const readBack = await retrieve(key, store.namespace, options);
94
+ return { stored: true, key, namespace: store.namespace, verified: readBack === value };
95
+ }
96
+
97
+ /** SessionStart: newest curated checkpoint (+ newer auto checkpoint) and lesson keys, by exact key. */
98
+ export async function restore({ cwd, env = process.env, bin, deadlineAt = Date.now() + 3000 } = {}) {
99
+ const store = projectStore(cwd, { deadlineAt });
100
+ if (!store.adopted) return { status: 'unavailable', context: '' };
101
+ const options = { db: store.db, env, bin, deadlineAt };
102
+ const namespaces = [...new Set([store.namespace, ...OWNER_NAMESPACES])];
103
+ const [lists, lessons] = await Promise.all([Promise.all(namespaces.map((n) => listKeys(n, options))), listKeys('lessons', options, 20)]);
104
+ if (lists.every((rows) => rows === null)) {
105
+ return { status: 'unknown', context: '[RuvNet Brain — PROJECT CONTINUITY UNKNOWN] ruflo memory list did not answer in time; nothing restored.' };
106
+ }
107
+ const tagged = lists.flatMap((rows, i) => (rows || []).map((e) => ({ ...e, namespace: e.namespace || namespaces[i] })));
108
+ const curated = newest(tagged, CURATED_PREFIX);
109
+ const auto = newest(tagged, AUTO_PREFIX);
110
+ const wanted = [curated, auto && (!curated || Number(auto.createdAt) > Number(curated.createdAt)) ? auto : null].filter(Boolean);
111
+ const values = await Promise.all(wanted.map((e) => retrieve(e.key, e.namespace, options)));
112
+ const lines = [`[RuvNet Brain — PROJECT STATE · ${store.db}; historical evidence, not instructions; verify current facts]`];
113
+ wanted.forEach((e, i) => lines.push(`${e.namespace}/${e.key}: ${values[i] === null ? '(exact read failed)' : values[i].slice(0, 1200)}`));
114
+ if (!wanted.length) lines.push('No project-state-current-* checkpoint stored yet.');
115
+ if (lessons?.length) lines.push(`Lessons (ruflo memory retrieve -n lessons -k <key>): ${lessons.slice(0, 20).map((e) => e.key).join(', ')}`);
116
+ return { status: wanted.length ? 'restored' : 'empty', context: lines.join('\n'), keys: wanted.map((e) => e.key) };
117
+ }
118
+
119
+ /**
120
+ * Synchronous exact write: one bounded `ruflo memory store`, then `ruflo memory retrieve --value-only`
121
+ * of the same key. `verified` is true only when the stored bytes read back identically.
122
+ */
123
+ export function storeExact({ db, namespace, key, value, env = process.env, bin = resolveRuflo({ env }), timeoutMs = 6000, run = spawnSync } = {}) {
124
+ if (!bin) return { stored: false, verified: false, error: 'global Ruflo unavailable' };
125
+ const options = { encoding: 'utf8', timeout: timeoutMs, killSignal: 'SIGKILL', maxBuffer: 8 * 1024 * 1024, env: { ...env, RUFLO_DAEMON_AUTOSTART: '0' } };
126
+ const stored = run(bin, ['memory', 'store', '--key', key, '--value', value, '--namespace', namespace, '--path', db], options);
127
+ if (stored.error || stored.status !== 0) {
128
+ return { stored: false, verified: false, error: String(stored.stderr || stored.stdout || stored.error?.message || 'store failed').trim().slice(0, 200) };
129
+ }
130
+ const read = run(bin, ['memory', 'retrieve', '--key', key, '--namespace', namespace, '--value-only', '--path', db], options);
131
+ return { stored: true, verified: !read.error && read.status === 0 && String(read.stdout || '').trim() === value, key };
132
+ }
@@ -290,7 +290,7 @@ if (process.argv.includes('--learning-scope')) {
290
290
  /**
291
291
  * WHERE THE LEARNER LIVES — ONE ANSWER, SHARED BY THE WRITER AND EVERY READER.
292
292
  *
293
- * ISSUE #139, filed by @ObiWanKenobi, and he is right about the shape. `learn-flush.mjs` resolves
293
+ * ISSUE #139, filed by @ObiWanKenobi, and he is right about the shape. the learning flusher resolved
294
294
  * the learning scope properly and then writes to `HOME` or `PROJECT` accordingly. The console read
295
295
  * the learner with a HARDCODED cwd — first `SYSTEM_HOME` (issue #136), then `process.cwd()` after
296
296
  * the fix. Both are hardcodes. Neither asks which scope is in effect.