π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(memory,hooks): keep the on-disk format on sql.js write-back (#4057); command-recording off switch, redaction and a cleanup that sees expired rows (#4056)
RuFlo18ecd8ae7b35
diff --git a/plugins/ruflo-core/.claude-plugin/plugin.json b/plugins/ruflo-core/.claude-plugin/plugin.jsonindex bcbceff41b..d301be5d83 100644--- a/plugins/ruflo-core/.claude-plugin/plugin.json+++ b/plugins/ruflo-core/.claude-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "ruflo-core","description": "Foundation plugin β registers the ruflo MCP server (300+ tools across memory/agentdb/embeddings/hooks/aidefence/neural/autopilot/browser/agent/swarm), provides 4 generalist agents (coder/researcher/reviewer/witness-curator), 5 skills (discover-plugins/init-project/ruflo-doctor/ruflo-status/witness), and a curated plugin-discovery catalog",- "version": "0.2.7",+ "version": "0.2.8","author": {"name": "ruvnet","url": "https://github.com/ruvnet"diff --git a/plugins/ruflo-core/CHANGELOG.md b/plugins/ruflo-core/CHANGELOG.mdindex 698d7710db..ee693c14c6 100644--- a/plugins/ruflo-core/CHANGELOG.md+++ b/plugins/ruflo-core/CHANGELOG.md@@ -2,6 +2,9 @@Newest first. One `## <version> β <date>` heading per version, then `feat:`, `fix:`, `breaking:` and `chore:` bullets (ADR-478). Built from git history; this is the whole history.+## 0.2.8 β 2026-10-11+- fix: the PostToolUse Bash shim stands down when command recording is switched off (`CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=0` or `CLAUDE_FLOW_HOOKS_ENABLED=false`), so no CLI process is started per Bash call (#4056)+## 0.2.7 β 2026-10-11- fix: researcher agent calls agentdb_semantic-route, pattern-search, context-synthesize, hierarchical-store/recall and causal-edge with their real parameter names (input, topK, maxEntries, key/value/tier, sourceId/targetId/relation); it now follows edges with agentdb_graph-query and agentdb_graph-pathfinder (causal-edge only records)- fix: drop the invalid `--train-neural` flag from `hooks post-task` in the coder and researcher agentsdiff --git a/plugins/ruflo-core/scripts/command-recording-switch.test.cjs b/plugins/ruflo-core/scripts/command-recording-switch.test.cjsnew file mode 100644index 0000000000..ce9017097f--- /dev/null+++ b/plugins/ruflo-core/scripts/command-recording-switch.test.cjs@@ -0,0 +1,31 @@+'use strict';+/**+ * #4056: the PostToolUse Bash shim stands down when command recording is off.+ * Pure-function test of the switch (RUFLO_HOOK_UNIT_TEST keeps main() from+ * running on require).+ */+const test = require('node:test');+const assert = require('node:assert/strict');+process.env.RUFLO_HOOK_UNIT_TEST = '1';+const { commandRecordingOff } = require('./ruflo-hook.cjs');++test('recording is on by default', () => {+ assert.equal(commandRecordingOff({}), false);+});++test('CLAUDE_FLOW_HOOKS_RECORD_COMMANDS 0/false/off/no turns it off', () => {+ for (const v of ['0', 'false', 'off', 'no', 'FALSE', ' Off ']) {+ assert.equal(commandRecordingOff({ CLAUDE_FLOW_HOOKS_RECORD_COMMANDS: v }), true, v);+ }+});++test('CLAUDE_FLOW_HOOKS_ENABLED=false turns it off', () => {+ assert.equal(commandRecordingOff({ CLAUDE_FLOW_HOOKS_ENABLED: 'false' }), true);+ assert.equal(commandRecordingOff({ CLAUDE_FLOW_HOOKS_ENABLED: 'true' }), false);+});++test('other values leave it on', () => {+ for (const v of ['1', 'true', 'on', '']) {+ assert.equal(commandRecordingOff({ CLAUDE_FLOW_HOOKS_RECORD_COMMANDS: v }), false, v);+ }+});diff --git a/plugins/ruflo-core/scripts/ruflo-hook.cjs b/plugins/ruflo-core/scripts/ruflo-hook.cjsindex da07682124..d15545bf90 100644--- a/plugins/ruflo-core/scripts/ruflo-hook.cjs+++ b/plugins/ruflo-core/scripts/ruflo-hook.cjs@@ -403,6 +403,17 @@ function precompactAuto() {process.stdout.write(lines.join('\n') + '\n');}+/**+ * #4056: command history is opt-out. When the person has switched it off the+ * shim does not even start the CLI for a Bash event (the CLI re-checks, and+ * also reads hooks.recordCommands from the config files).+ */+function commandRecordingOff(env = process.env) {+ const off = new Set(['0', 'false', 'off', 'no']);+ return [env.CLAUDE_FLOW_HOOKS_RECORD_COMMANDS, env.CLAUDE_FLOW_HOOKS_ENABLED]+ .some((v) => v !== undefined && off.has(String(v).trim().toLowerCase()));+}+function main() {const [subcommand] = process.argv.slice(2);if (!subcommand) done(); // no subcommand β no-op, same as bash version@@ -437,6 +448,7 @@ function main() {// PostToolUse: derive CLI flags from the hook event JSON (replaces jq).if (subcommand === 'post-command') {+ if (commandRecordingOff()) done();const cmd = event?.tool_input?.command;if (!cmd) done(); // bash version: `[ -z "$CMD" ] && exit 0`const exitCode = event?.tool_response?.exit_code ?? 0;@@ -477,4 +489,4 @@ if (process.env.RUFLO_HOOK_UNIT_TEST !== '1') {main();}-module.exports = { escapeCmdArg, invokeHook, npxPrefix, resolveCommandPath, resolveInvocation, resolveNpmShim };+module.exports = { commandRecordingOff, escapeCmdArg, invokeHook, npxPrefix, resolveCommandPath, resolveInvocation, resolveNpmShim };diff --git a/plugins/ruflo-core/scripts/smoke.sh b/plugins/ruflo-core/scripts/smoke.shindex 7e6065c47c..f406f556f4 100755--- a/plugins/ruflo-core/scripts/smoke.sh+++ b/plugins/ruflo-core/scripts/smoke.sh@@ -1,5 +1,5 @@#!/usr/bin/env bash-# Structural smoke test for ruflo-core v0.2.7 (ADR-0001).+# Structural smoke test for ruflo-core v0.2.8 (ADR-0001).set -uROOT="$(cd "$(dirname "$0")/.." && pwd)"PASS=0@@ -8,10 +8,10 @@ step() { printf "β %s ... " "$1"; }ok() { printf "PASS\n"; PASS=$((PASS+1)); }bad() { printf "FAIL: %s\n" "$1"; FAIL=$((FAIL+1)); }-step "1. plugin.json declares 0.2.7 with new keywords"+step "1. plugin.json declares 0.2.8 with new keywords"v=$(grep -E '"version"' "$ROOT/.claude-plugin/plugin.json" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)-if [[ "$v" != "0.2.7" ]]; then- bad "expected 0.2.7, got '$v'"+if [[ "$v" != "0.2.8" ]]; then+ bad "expected 0.2.8, got '$v'"elsemiss=""for k in foundation mcp-server plugin-catalog discovery; dodiff --git a/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts b/v3/@claude-flow/cli/__tests__/command-recording-4056.test.tsnew file mode 100644index 0000000000..352e382a08--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts@@ -0,0 +1,105 @@+/**+ * #4056 β the PostToolUse Bash hook stored every command in full as+ * `commands/cmd-<ts>`: 27k rows / 285 MB on one project, full command text+ * (which can carry tokens), and no off switch.+ */+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import {+ commandRecordingDisabled,+ redactCommandForStorage,+ MAX_RECORDED_COMMAND_CHARS,+} from '../src/services/command-recording.js';++describe('commandRecordingDisabled', () => {+ let cwd: string;+ beforeEach(() => { cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-rec-4056-')); });+ afterEach(() => { fs.rmSync(cwd, { recursive: true, force: true }); });++ it('is on by default', () => {+ expect(commandRecordingDisabled({}, cwd)).toBe(false);+ });++ it.each(['0', 'false', 'off', 'no', 'FALSE'])('CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=%s turns it off', v => {+ expect(commandRecordingDisabled({ CLAUDE_FLOW_HOOKS_RECORD_COMMANDS: v }, cwd)).toBe(true);+ });++ it.each(['1', 'true', 'on'])('CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=%s leaves it on', v => {+ expect(commandRecordingDisabled({ CLAUDE_FLOW_HOOKS_RECORD_COMMANDS: v }, cwd)).toBe(false);+ });++ it('honours CLAUDE_FLOW_HOOKS_ENABLED=false (written into generated settings but never read before)', () => {+ expect(commandRecordingDisabled({ CLAUDE_FLOW_HOOKS_ENABLED: 'false' }, cwd)).toBe(true);+ expect(commandRecordingDisabled({ CLAUDE_FLOW_HOOKS_ENABLED: 'true' }, cwd)).toBe(false);+ });++ it('honours hooks.recordCommands=false in claude-flow.config.json', () => {+ fs.writeFileSync(path.join(cwd, 'claude-flow.config.json'), JSON.stringify({ hooks: { recordCommands: false } }));+ expect(commandRecordingDisabled({}, cwd)).toBe(true);+ });++ it('honours hooks.recordCommands=false in .claude-flow/config.json', () => {+ fs.mkdirSync(path.join(cwd, '.claude-flow'));+ fs.writeFileSync(path.join(cwd, '.claude-flow', 'config.json'), JSON.stringify({ hooks: { recordCommands: false } }));+ expect(commandRecordingDisabled({}, cwd)).toBe(true);+ });++ it('a malformed config file does not turn recording off or throw', () => {+ fs.writeFileSync(path.join(cwd, 'claude-flow.config.json'), '{ not json');+ expect(commandRecordingDisabled({}, cwd)).toBe(false);+ });+});++describe('redactCommandForStorage', () => {+ const GH = 'ghp_' + 'a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8';+ const OAI = 'sk-' + 'proj-abcdefghijklmnopqrstuvwxyz0123456789';++ it('leaves an ordinary command alone', () => {+ expect(redactCommandForStorage('npm test -- --run src/foo.test.ts')).toBe('npm test -- --run src/foo.test.ts');+ });++ it.each([+ ['curl -H "Authorization: Bearer abc123def456ghi789" https://x.test', 'abc123def456ghi789'],+ ['curl -H "authorization: Basic dXNlcjpwYXNz" https://x.test', 'dXNlcjpwYXNz'],+ ['tool --password=hunter2hunter2 run', 'hunter2hunter2'],+ ['tool --token abcdef0123456789 run', 'abcdef0123456789'],+ ['tool --api-key "sekrit-value-123" run', 'sekrit-value-123'],+ ['NPM_TOKEN=npm_abcdefghijklmnopqrstuvwxyz0123 npm publish', 'npm_abcdefghijklmnopqrstuvwxyz0123'],+ ['AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY aws s3 ls', 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'],+ [`git clone https://${GH}@github.com/o/r.git`, GH],+ [`curl -s https://api.openai.com -d x -H "x-key: ${OAI}"`, OAI],+ ['psql postgres://admin:[email protected]/app', 's3cretpass'],+ ])('redacts the secret in: %s', (cmd, secret) => {+ const out = redactCommandForStorage(cmd);+ expect(out).not.toContain(secret);+ expect(out).toContain('[redacted]');+ });++ it('keeps the command name and flags so history is still useful', () => {+ const out = redactCommandForStorage('curl -H "Authorization: Bearer abc123def456ghi789" https://x.test/api');+ expect(out).toContain('curl');+ expect(out).toContain('https://x.test/api');+ });++ it('truncates a very long command and says so', () => {+ const out = redactCommandForStorage('echo ' + 'a'.repeat(MAX_RECORDED_COMMAND_CHARS * 3));+ expect(out.length).toBeLessThanOrEqual(MAX_RECORDED_COMMAND_CHARS + 32);+ expect(out).toMatch(/truncated/);+ });++ it('is linear on hostile input (no catastrophic backtracking)', () => {+ for (const hostile of [+ 'x'.repeat(200_000),+ '--password '.repeat(20_000),+ 'A=b '.repeat(50_000),+ 'Authorization: '.repeat(20_000),+ '://'.repeat(60_000),+ ]) {+ const t0 = Date.now();+ redactCommandForStorage(hostile);+ expect(Date.now() - t0).toBeLessThan(500);+ }+ });+});diff --git a/v3/@claude-flow/cli/__tests__/memory-cleanup-expired-4056.test.ts b/v3/@claude-flow/cli/__tests__/memory-cleanup-expired-4056.test.tsnew file mode 100644index 0000000000..d48a19e1c5--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/memory-cleanup-expired-4056.test.ts@@ -0,0 +1,89 @@+/**+ * #4056 β `memory cleanup --expired-only` reported `Patterns removed: 0` and+ * left every expired row in place. Cause: `memory_cleanup` finds its+ * candidates through `listEntries`, and `listEntries` hides expired rows+ * (TTL is a read-time visibility rule, `liveMemoryRowSql`), so the cleanup+ * could never see what it was meant to remove.+ */+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import {+ _resetMemoryRootCache,+ initializeMemoryDatabase,+ storeEntry,+ listEntries,+} from '../src/memory/memory-initializer.js';+import { memoryTools } from '../src/mcp-tools/memory-tools.js';++const ENV = ['CLAUDE_FLOW_MEMORY_PATH', 'CLAUDE_FLOW_DISABLE_BRIDGE', 'CLAUDE_FLOW_DB_PATH'];+const SAVED: Record<string, string | undefined> = {};+let tmp: string;+let dbPath: string;++async function expireRows(namespace: string, expiresAt: number): Promise<void> {+ const initSqlJs = (await import('sql.js')).default;+ const SQL = await initSqlJs();+ const db = new SQL.Database(fs.readFileSync(dbPath));+ db.run('UPDATE memory_entries SET expires_at = ? WHERE namespace = ?', [expiresAt, namespace]);+ fs.writeFileSync(dbPath, Buffer.from(db.export()));+ db.close();+}++beforeEach(async () => {+ for (const n of ENV) SAVED[n] = process.env[n];+ tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'cleanup-expired-4056-'));+ dbPath = path.join(tmp, 'memory.db');+ process.env.CLAUDE_FLOW_MEMORY_PATH = tmp;+ process.env.CLAUDE_FLOW_DISABLE_BRIDGE = '1';+ delete process.env.CLAUDE_FLOW_DB_PATH;+ _resetMemoryRootCache();+ expect((await initializeMemoryDatabase({ dbPath, force: true, migrate: false })).success).toBe(true);+ for (let i = 0; i < 5; i++) {+ await storeEntry({ key: `cmd-${i}`, value: `ls ${i}`, namespace: 'commands', dbPath, generateEmbeddingFlag: false });+ }+ await storeEntry({ key: 'keep', value: 'x', namespace: 'other', dbPath, generateEmbeddingFlag: false });+ await expireRows('commands', 1000);+});++afterEach(() => {+ for (const n of ENV) {+ if (SAVED[n] === undefined) delete process.env[n];+ else process.env[n] = SAVED[n];+ }+ _resetMemoryRootCache();+ fs.rmSync(tmp, { recursive: true, force: true });+});++describe('expired rows are visible to cleanup, hidden from normal reads (#4056)', () => {+ it('a normal listing still hides expired rows', async () => {+ const res = await listEntries({ namespace: 'commands', dbPath });+ expect(res.total).toBe(0);+ });++ it('includeExpired lists them and reports expiresAt', async () => {+ const res = await listEntries({ namespace: 'commands', dbPath, includeExpired: true });+ expect(res.total).toBe(5);+ for (const e of res.entries) expect(e.expiresAt).toBe(1000);+ });++ const cleanup = memoryTools.find(t => t.name === 'memory_cleanup')!;++ it('memory_cleanup dry run counts the expired rows', async () => {+ const res = await cleanup.handler({ dryRun: true }) as { candidates: { expired: number } };+ expect(res.candidates.expired).toBe(5);+ });++ it('memory_cleanup removes the expired rows and leaves the rest', async () => {+ const res = await cleanup.handler({ dryRun: false }) as { deleted: { entries: number } };+ expect(res.deleted.entries).toBe(5);++ const initSqlJs = (await import('sql.js')).default;+ const SQL = await initSqlJs();+ const db = new SQL.Database(fs.readFileSync(dbPath));+ const live = db.exec("SELECT namespace, COUNT(*) FROM memory_entries WHERE status = 'active' GROUP BY namespace");+ db.close();+ expect(live[0].values).toEqual([['other', 1]]);+ });+});diff --git a/v3/@claude-flow/cli/__tests__/memory-writeback-preserves-format-4057.test.ts b/v3/@claude-flow/cli/__tests__/memory-writeback-preserves-format-4057.test.tsnew file mode 100644index 0000000000..d7bee59d5c--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/memory-writeback-preserves-format-4057.test.ts@@ -0,0 +1,145 @@+/**+ * #4057 β `memory purge` fell back to the whole-file sql.js path and, with+ * encryption at rest on, rewrote a PLAINTEXT native SQLite store as an+ * RFE1-encrypted blob while reporting `[OK]`. The native bridge and every+ * SQLite reader could no longer open the store.+ *+ * Contract: a whole-file sql.js read-modify-write must write the image back+ * in the format it read (plaintext in -> plaintext out, RFE1 in -> RFE1 out),+ * whatever the environment says.+ */+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import { randomBytes } from 'node:crypto';+import {+ initializeMemoryDatabase,+ storeEntry,+ listEntries,+ purgeNamespace,+ deleteEntry,+ ensureSchemaColumns,+ applyTemporalDecay,+} from '../src/memory/memory-initializer.js';+import { isEncryptedBlob } from '../src/encryption/vault.js';++const SAVED: Record<string, string | undefined> = {};+const ENV = ['CLAUDE_FLOW_ENCRYPT_AT_REST', 'CLAUDE_FLOW_ENCRYPTION_KEY', 'CLAUDE_FLOW_DISABLE_BRIDGE'];++let tmp: string;+let dbPath: string;++const isPlainSqlite = (p: string) =>+ fs.readFileSync(p).subarray(0, 15).toString('latin1') === 'SQLite format 3';++async function seed(): Promise<void> {+ const init = await initializeMemoryDatabase({ dbPath, force: true, migrate: false });+ expect(init.success).toBe(true);+ for (const [ns, key] of [['feedback', 'a'], ['feedback', 'b'], ['keep', 'c']]) {+ await storeEntry({ key, value: `v-${key}`, namespace: ns, dbPath, generateEmbeddingFlag: false });+ }+}++beforeEach(() => {+ for (const n of ENV) SAVED[n] = process.env[n];+ tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'writeback-format-4057-'));+ dbPath = path.join(tmp, 'agentdb-memory.db');+ process.env.CLAUDE_FLOW_DISABLE_BRIDGE = '1';+ delete process.env.CLAUDE_FLOW_ENCRYPT_AT_REST;+ delete process.env.CLAUDE_FLOW_ENCRYPTION_KEY;+});++afterEach(() => {+ for (const n of ENV) {+ if (SAVED[n] === undefined) delete process.env[n];+ else process.env[n] = SAVED[n];+ }+ fs.rmSync(tmp, { recursive: true, force: true });+});++function turnEncryptionOn(): void {+ process.env.CLAUDE_FLOW_ENCRYPT_AT_REST = '1';+ process.env.CLAUDE_FLOW_ENCRYPTION_KEY = randomBytes(32).toString('hex');+}++describe('whole-file sql.js write-back keeps the on-disk format (#4057)', () => {+ it('purgeNamespace on a plaintext store stays plaintext when encryption is switched on', async () => {+ await seed();+ expect(isPlainSqlite(dbPath)).toBe(true);+ turnEncryptionOn();++ const res = await purgeNamespace({ namespace: 'feedback', dbPath });++ expect(res.success).toBe(true);+ expect(res.deletedCount).toBe(2);+ expect(isPlainSqlite(dbPath)).toBe(true);+ expect(isEncryptedBlob(fs.readFileSync(dbPath))).toBe(false);+ });++ it('deleteEntry on a plaintext store stays plaintext when encryption is switched on', async () => {+ await seed();+ turnEncryptionOn();++ const res = await deleteEntry({ key: 'c', namespace: 'keep', dbPath });++ expect(res.success).toBe(true);+ expect(isPlainSqlite(dbPath)).toBe(true);+ });++ it('ensureSchemaColumns on a plaintext store stays plaintext when encryption is switched on', async () => {+ await seed();+ turnEncryptionOn();++ const res = await ensureSchemaColumns(dbPath);++ expect(res.success).toBe(true);+ expect(isPlainSqlite(dbPath)).toBe(true);+ });++ it('an already-encrypted store stays encrypted after a purge (no silent downgrade)', async () => {+ turnEncryptionOn();+ await seed();+ expect(isEncryptedBlob(fs.readFileSync(dbPath))).toBe(true);++ const res = await purgeNamespace({ namespace: 'feedback', dbPath });++ expect(res.success).toBe(true);+ expect(isEncryptedBlob(fs.readFileSync(dbPath))).toBe(true);+ const left = await listEntries({ namespace: 'keep', dbPath });+ expect(left.total).toBe(1);+ });++ it('the store is still readable by a plain SQLite reader after the purge', async () => {+ await seed();+ turnEncryptionOn();+ await purgeNamespace({ namespace: 'feedback', dbPath });++ const initSqlJs = (await import('sql.js')).default;+ const SQL = await initSqlJs();+ const db = new SQL.Database(fs.readFileSync(dbPath));+ const rows = db.exec("SELECT namespace, COUNT(*) FROM memory_entries GROUP BY namespace");+ db.close();+ expect(rows[0].values).toEqual([['keep', 1]]);+ });+ it('applyTemporalDecay keeps an encrypted store encrypted (it used to write the decrypted image back)', async () => {+ turnEncryptionOn();+ await seed();+ expect(isEncryptedBlob(fs.readFileSync(dbPath))).toBe(true);++ const res = await applyTemporalDecay(dbPath);++ expect(res.success).toBe(true);+ expect(isEncryptedBlob(fs.readFileSync(dbPath))).toBe(true);+ });++ it('applyTemporalDecay keeps a plaintext store plaintext when encryption is switched on', async () => {+ await seed();+ turnEncryptionOn();++ const res = await applyTemporalDecay(dbPath);++ expect(res.success).toBe(true);+ expect(isPlainSqlite(dbPath)).toBe(true);+ });+});diff --git a/v3/@claude-flow/cli/src/commands/hooks.ts b/v3/@claude-flow/cli/src/commands/hooks.tsindex b1acf74773..b3ec30cd10 100644--- a/v3/@claude-flow/cli/src/commands/hooks.ts+++ b/v3/@claude-flow/cli/src/commands/hooks.ts@@ -710,9 +710,18 @@ const postCommandCommand: Command = {timestamp: Date.now(),});+ // #4056: recording switched off on purpose is not a failure.+ const recordingOff = (result as { disabled?: boolean }).disabled === true;+if (ctx.flags.format === 'json') {output.printJson(result);- return { success: result.recorded, exitCode: result.recorded ? 0 : 1, data: result };+ const ok = result.recorded || recordingOff;+ return { success: ok, exitCode: ok ? 0 : 1, data: result };+ }++ if (recordingOff) {+ output.printInfo('Command recording is switched off (CLAUDE_FLOW_HOOKS_RECORD_COMMANDS / CLAUDE_FLOW_HOOKS_ENABLED / hooks.recordCommands); nothing was stored.');+ return { success: true, data: result };}if (!result.recorded) {diff --git a/v3/@claude-flow/cli/src/fs-secure.ts b/v3/@claude-flow/cli/src/fs-secure.tsindex 9985a5088f..c62068c38a 100644--- a/v3/@claude-flow/cli/src/fs-secure.ts+++ b/v3/@claude-flow/cli/src/fs-secure.ts@@ -24,6 +24,7 @@ import {mkdirSync,openSync,readFileSync,+ readSync,renameSync,unlinkSync,writeSync,@@ -35,6 +36,7 @@ import {getKey,isEncryptedBlob,isEncryptionEnabled,+ MAGIC,} from './encryption/vault.js';/**@@ -163,6 +165,55 @@ export function writeFileRestricted(writeFileAtomic(path, buf, { mode: 0o600 });}+/**+ * The at-rest format of an existing database file: `true` = RFE1-encrypted,+ * `false` = plaintext, `null` = missing or unreadable (nothing to preserve).+ * Reads four bytes only.+ */+export function existingFileIsEncrypted(path: string): boolean | null {+ let fd: number | undefined;+ try {+ fd = openSync(path, 'r');+ const head = Buffer.alloc(4);+ const n = readSync(fd, head, 0, 4, 0);+ if (n < 4) return null;+ return head.equals(MAGIC);+ } catch {+ return null;+ } finally {+ if (fd !== undefined) {+ try { closeSync(fd); } catch { /* best effort */ }+ }+ }+}++/**+ * Write a whole-file database image back in the format it was read in (#4057).+ *+ * A read-modify-write of a database image (sql.js loads the file, mutates it,+ * exports it) must not change what the file IS. With encryption at rest on, a+ * plain `{ encrypt: true }` write turned a plaintext native SQLite store into+ * an RFE1 blob that the native bridge and every SQLite reader then rejected,+ * while the command reported success. And the reverse, a plain+ * `writeFileAtomic` of a decrypted image, silently downgraded an encrypted+ * store to plaintext.+ *+ * existing file RFE1 -> RFE1 (encrypted)+ * existing file plaintext -> plaintext+ * no existing file -> `encrypt` (default true: the env gate decides)+ * `encrypt: false` -> plaintext, always (explicit caller choice)+ */+export function writeDbImagePreservingFormat(+ path: string,+ data: Uint8Array | Buffer,+ opts: { encrypt?: boolean } = {},+): void {+ const requested = opts.encrypt ?? true;+ const existing = existingFileIsEncrypted(path);+ const encrypt = requested === false ? false : (existing ?? requested);+ writeFileRestricted(path, Buffer.isBuffer(data) ? data : Buffer.from(data), { encrypt });+}+/*** Read a file and transparently decrypt if it carries the RFE1 magic.*diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.tsindex 9aaad24d97..c5e03b27f6 100644--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts@@ -9,6 +9,7 @@ import * as nodeFs from 'fs';import { dirname, join, resolve } from 'path';import { type MCPTool, getProjectCwd } from './types.js';import { validateIdentifier, validateText, validatePath } from './validate-input.js';+import { commandRecordingDisabled, redactCommandForStorage } from '../services/command-recording.js';import { checkCommandLoop, recordCommandOutcome } from './tool-loop-guardrail.js';import {buildLearnedRoutingPatterns,@@ -1203,13 +1204,36 @@ export const hooksPostCommand: MCPTool = {// repeated consecutive failures of the same command.recordCommandOutcome(command, success);+ // #4056: command history is opt-out (CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=0,+ // CLAUDE_FLOW_HOOKS_ENABLED=false, or hooks.recordCommands=false in the+ // config). The circuit breaker above is in-memory only; nothing below+ // runs when recording is off, so no command text reaches the store or+ // the trajectory pipeline.+ if (commandRecordingDisabled(process.env, getProjectCwd())) {+ return {+ recorded: false,+ disabled: true,+ command: undefined,+ exitCode,+ success,+ timestamp: new Date().toISOString(),+ _storedIn: 'none' as const,+ learningPath: 'recorded-only' as const,+ trajectoriesDelta: 0,+ note: 'Command recording is switched off (CLAUDE_FLOW_HOOKS_RECORD_COMMANDS / CLAUDE_FLOW_HOOKS_ENABLED / hooks.recordCommands); nothing was stored.',+ };+ }++ // What is stored is the redacted, length-capped form, never the raw text.+ const storedCommand = redactCommandForStorage(command);+// Persist command outcome via AgentDBlet _storedIn: 'agentdb' | 'json-store' | 'none' = 'none';try {const bridge = await import('../memory/memory-bridge.js');const stored = await bridge.bridgeStoreEntry({key: `cmd-${Date.now()}`,- value: JSON.stringify({ command, exitCode, success }),+ value: JSON.stringify({ command: storedCommand, exitCode, success }),namespace: 'commands',tags: [success ? 'success' : 'error'],ttl,@@ -1229,7 +1253,7 @@ export const hooksPostCommand: MCPTool = {}const key = `cmd-${now}`;store.entries[key] = {- key, value: JSON.stringify({ command, exitCode, success }), namespace: 'commands',+ key, value: JSON.stringify({ command: storedCommand, exitCode, success }), namespace: 'commands',storedAt: new Date(now).toISOString(), lastAccessed: new Date(now).toISOString(), accessCount: 0,expiresAt: new Date(now + ttl * 1000).toISOString(),};@@ -1250,8 +1274,8 @@ export const hooksPostCommand: MCPTool = {await intel.recordTrajectory([{type: 'action',- content: `Command \`${command.slice(0, 200)}\` exited ${exitCode} (${success ? 'success' : 'failure'})`,- metadata: { hook: 'post-command', command: command.slice(0, 500), exitCode, success },+ content: `Command \`${storedCommand.slice(0, 200)}\` exited ${exitCode} (${success ? 'success' : 'failure'})`,+ metadata: { hook: 'post-command', command: storedCommand.slice(0, 500), exitCode, success },timestamp: Date.now(),}],success ? 'success' : 'failure',diff --git a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.tsindex a3518c6cae..8793d48352 100644--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts@@ -1477,7 +1477,8 @@ export const memoryTools: MCPTool[] = [const dryRun = input.dryRun !== false; // default trueconst namespace = input.namespace ? String(input.namespace) : undefined;if (namespace) { const v = validateIdentifier(namespace, 'namespace'); if (!v.valid) throw new Error(v.error); }- const all = await listEntries({ limit: 100000, namespace });+ // #4056: TTL hides expired rows from normal reads, so the cleanup has to ask for them.+ const all = await listEntries({ limit: 100000, namespace, includeExpired: true });const now = Date.now();const expired = all.entries.filter(e => {const exp = (e as { expiresAt?: string | number | null }).expiresAt;diff --git a/v3/@claude-flow/cli/src/memory/live-memory-row.ts b/v3/@claude-flow/cli/src/memory/live-memory-row.tsindex 84c7d6d4dd..028cc78f65 100644--- a/v3/@claude-flow/cli/src/memory/live-memory-row.ts+++ b/v3/@claude-flow/cli/src/memory/live-memory-row.ts@@ -1,4 +1,5 @@/** TTL is a read-time visibility rule, independent of background cleanup. */-export function liveMemoryRowSql(): string {+export function liveMemoryRowSql(opts: { includeExpired?: boolean } = {}): string {+ if (opts.includeExpired) return `(status = 'active' OR status IS NULL)`;return `(status = 'active' OR status IS NULL) AND (expires_at IS NULL OR expires_at > ${Date.now()})`;}diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.tsindex 34f4171ab5..3921ad2476 100644--- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts+++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts@@ -1543,6 +1543,8 @@ export async function bridgeListEntries(options: {provenanceFilter?: string[];/** ADR-472: include each entry's embedding as int8+scale (`embeddingQ8`); at most MAX_LIST_EMBEDDINGS rows. */includeEmbedding?: boolean;+ /** #4056: also return rows whose TTL has passed (with `expiresAt`). For cleanup only. */+ includeExpired?: boolean;}): Promise<{success: boolean;entries: {@@ -1553,6 +1555,8 @@ export async function bridgeListEntries(options: {accessCount: number;createdAt: string;updatedAt: string;+ /** #4056: epoch ms the row expires at; null when it never does. */+ expiresAt?: number | null;hasEmbedding: boolean;/** #2073: Present when `includeContent: true` was requested. */content?: string;@@ -1593,7 +1597,7 @@ export async function bridgeListEntries(options: {// the `status = 'active'` filter matched zero. Treat NULL as// "legacy-active" β the safe default for any entry that predates the// status column.- const statusFilter = liveMemoryRowSql();+ const statusFilter = liveMemoryRowSql({ includeExpired: options.includeExpired });// Countlet total = 0;@@ -1611,7 +1615,7 @@ export async function bridgeListEntries(options: {const entries: any[] = [];try {const stmt = ctx.db.prepare(`- SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, provenance_type+ SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, provenance_type, expires_atFROM memory_entriesWHERE ${statusFilter} ${extraFilter}ORDER BY updated_at DESC@@ -1629,6 +1633,7 @@ export async function bridgeListEntries(options: {accessCount: row.access_count ?? 0,createdAt: row.created_at || new Date().toISOString(),updatedAt: row.updated_at || new Date().toISOString(),+ expiresAt: row.expires_at ?? null,hasEmbedding: !!(row.embedding && String(row.embedding).length > 10),provenanceType: row.provenance_type || 'unknown',};diff --git a/v3/@claude-flow/cli/src/memory/memory-initializer.ts b/v3/@claude-flow/cli/src/memory/memory-initializer.tsindex e105b7814b..d1f0a1bd12 100644--- a/v3/@claude-flow/cli/src/memory/memory-initializer.ts+++ b/v3/@claude-flow/cli/src/memory/memory-initializer.ts@@ -18,7 +18,7 @@ import * as fs from 'fs';import * as path from 'path';import { AsyncLocalStorage } from 'node:async_hooks';import { createRequire } from 'node:module';-import { readFileMaybeEncrypted, writeFileAtomic, writeFileRestricted } from '../fs-secure.js';+import { readFileMaybeEncrypted, writeDbImagePreservingFormat, writeFileAtomic, writeFileRestricted } from '../fs-secure.js';import { restoreMemoryDbFromBackup } from '../services/memory-backup.js';import { validateIdentifier } from '../mcp-tools/validate-input.js';@@ -1425,7 +1425,7 @@ export async function ensureSchemaColumns(dbPath: string, options: { encryptWritif (modified) {// Save updated databaseconst data = db.export();- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });+ writeDbImagePreservingFormat(dbPath, data, { encrypt: options.encryptWrites ?? true });}db.close();@@ -2212,7 +2212,7 @@ export async function applyTemporalDecay(dbPath?: string): Promise<{const initSqlJs = (await import('sql.js')).default;const SQL = await initSqlJs();- const fileBuffer = fs.readFileSync(path_);+ const fileBuffer = readFileMaybeEncrypted(path_, null);const db = new SQL.Database(fileBuffer);// Apply decay: confidence *= exp(-decay_rate * days_since_last_use)@@ -2233,7 +2233,7 @@ export async function applyTemporalDecay(dbPath?: string): Promise<{// Save (atomic β issue #2584: a torn full-image flush corrupts the store)const data = db.export();- writeFileAtomic(path_, Buffer.from(data));+ writeDbImagePreservingFormat(path_, data);db.close();return {@@ -3100,7 +3100,7 @@ export async function storeEntry(options: {// Saveconst data = db.export();- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });+ writeDbImagePreservingFormat(dbPath, data);db.close();});@@ -3457,6 +3457,8 @@ export async function listEntries(options: {provenanceFilter?: string[];/** ADR-472: include each entry's embedding as int8+scale (`embeddingQ8`); at most MAX_LIST_EMBEDDINGS rows. Read-only. */includeEmbedding?: boolean;+ /** #4056: also return rows whose TTL has passed (with `expiresAt`). For cleanup only; normal reads hide them. */+ includeExpired?: boolean;}): Promise<{success: boolean;entries: {@@ -3467,6 +3469,8 @@ export async function listEntries(options: {accessCount: number;createdAt: string;updatedAt: string;+ /** #4056: epoch ms the row expires at; null when it never does. */+ expiresAt?: number | null;hasEmbedding: boolean;/** #2073: Present when `includeContent: true` was requested. */content?: string;@@ -3533,7 +3537,7 @@ export async function listEntries(options: {// that predate the status column may have NULL after migration.// See memory-bridge.ts:bridgeListEntries for full context.// Get total count- const whereClauses = [liveMemoryRowSql()];+ const whereClauses = [liveMemoryRowSql({ includeExpired: options.includeExpired })];const whereParams: string[] = [];if (namespace) {whereClauses.push('namespace = ?');@@ -3559,7 +3563,7 @@ export async function listEntries(options: {const safeOffset = parseInt(String(offset), 10) || 0;// #2120 β same NULL-as-active acceptance as the count above.const listStmt = db.prepare(- `SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, provenance_type+ `SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, provenance_type, expires_atFROM memory_entries WHERE ${whereSql}ORDER BY updated_at DESC LIMIT ? OFFSET ?`);@@ -3578,6 +3582,7 @@ export async function listEntries(options: {accessCount: number;createdAt: string;updatedAt: string;+ expiresAt?: number | null;hasEmbedding: boolean;content?: string;provenanceType?: string;@@ -3586,8 +3591,8 @@ export async function listEntries(options: {if (result[0]?.values) {for (const row of result[0].values) {- const [id, key, ns, content, embedding, accessCount, createdAt, updatedAt, provenanceTypeVal] = row as [- string, string, string, string, string | null, number, string, string, string | null+ const [id, key, ns, content, embedding, accessCount, createdAt, updatedAt, provenanceTypeVal, expiresAtVal] = row as [+ string, string, string, string, string | null, number, string, string, string | null, number | null];const entry: {id: string;@@ -3597,6 +3602,7 @@ export async function listEntries(options: {accessCount: number;createdAt: string;updatedAt: string;+ expiresAt?: number | null;hasEmbedding: boolean;content?: string;provenanceType?: string;@@ -3611,6 +3617,7 @@ export async function listEntries(options: {accessCount: accessCount || 0,createdAt: createdAt || new Date().toISOString(),updatedAt: updatedAt || new Date().toISOString(),+ expiresAt: expiresAtVal ?? null,hasEmbedding: !!embedding && embedding.length > 10,provenanceType: provenanceTypeVal || 'unknown'};@@ -3746,7 +3753,7 @@ export async function getEntry(options: {// Save updated databaseconst data = db.export();- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });+ writeDbImagePreservingFormat(dbPath, data);db.close();@@ -3913,7 +3920,7 @@ export async function deleteEntry(options: {// Save updated databaseconst data = db.export();- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });+ writeDbImagePreservingFormat(dbPath, data, { encrypt: options.encryptWrites ?? true });db.close();@@ -4106,7 +4113,7 @@ export async function purgeNamespace(options: {const remainingEntries = (countResult[0]?.values?.[0]?.[0] as number) || 0;const data = db.export();- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });+ writeDbImagePreservingFormat(dbPath, data, { encrypt: options.encryptWrites ?? true });db.close();if (deletedCount > 0 && hnswIndex?.entries) {diff --git a/v3/@claude-flow/cli/src/services/command-recording.ts b/v3/@claude-flow/cli/src/services/command-recording.tsnew file mode 100644index 0000000000..ecafcfc4ec--- /dev/null+++ b/v3/@claude-flow/cli/src/services/command-recording.ts@@ -0,0 +1,96 @@+/**+ * Policy for what the PostToolUse Bash hook may keep (#4056).+ *+ * `hooks post-command` used to store every command in full as+ * `commands/cmd-<ts>`: 27k rows / 285 MB on one project, full command text+ * that can carry tokens, and no way to switch it off. This module answers two+ * questions for that writer:+ *+ * 1. May a command be recorded at all? (`commandRecordingDisabled`)+ * 2. What form of it may be stored? (`redactCommandForStorage`)+ *+ * Every pattern below is bounded, and the input is capped before any pattern+ * runs, so a hostile command cannot stall the hook (the same lesson as the+ * settings-risk scanner, #4053).+ */+import * as fs from 'node:fs';+import * as path from 'node:path';++/** Longest command text kept per row. */+export const MAX_RECORDED_COMMAND_CHARS = 2000;++/** Only this much of a command is ever scanned; the stored text is shorter still. */+const MAX_SCANNED_CHARS = 8000;++const OFF_VALUES = new Set(['0', 'false', 'off', 'no']);++const isOff = (v: string | undefined): boolean => v !== undefined && OFF_VALUES.has(v.trim().toLowerCase());++function configDisablesRecording(cwd: string): boolean {+ for (const file of [+ path.resolve(cwd, 'claude-flow.config.json'),+ path.resolve(cwd, '.claude-flow', 'config.json'),+ ]) {+ try {+ if (!fs.existsSync(file)) continue;+ const raw = JSON.parse(fs.readFileSync(file, 'utf-8')) as { hooks?: { recordCommands?: unknown } };+ if (raw?.hooks?.recordCommands === false) return true;+ } catch {+ /* malformed config: keep the default (recording on) rather than guess */+ }+ }+ return false;+}++/**+ * True when command recording is switched off. Any one of these turns it off:+ * - `CLAUDE_FLOW_HOOKS_RECORD_COMMANDS` = 0 | false | off | no+ * - `CLAUDE_FLOW_HOOKS_ENABLED` = 0 | false | off | no+ * - `hooks.recordCommands: false` in `claude-flow.config.json` or `.claude-flow/config.json`+ */+export function commandRecordingDisabled(+ env: Record<string, string | undefined> = process.env,+ cwd: string = process.cwd(),+): boolean {+ if (isOff(env.CLAUDE_FLOW_HOOKS_RECORD_COMMANDS)) return true;+ if (isOff(env.CLAUDE_FLOW_HOOKS_ENABLED)) return true;+ return configDisablesRecording(cwd);+}++const QUOTED_OR_BARE = `"[^"]{0,512}"|'[^']{0,512}'|[^\\s"']{1,512}`;++// Order matters little; each rule only ever shortens the text.+const RULES: Array<[RegExp, string]> = [+ // Authorization: Bearer xyz / authorization=Basic xyz+ [/(authorization["']?\s*[:=]\s*)(?:(?:bearer|basic|token)\s+)?[^\s"']{1,512}/gi, '$1[redacted]'],+ // --password x / --token=x / --api-key "x" / -token x+ [new RegExp(`(--?(?:password|passwd|pwd|token|secret|api[-_]?key|access[-_]?token|auth[-_]?token|client[-_]?secret)(?:=|\\s+))(?:${QUOTED_OR_BARE})`, 'gi'), '$1[redacted]'],+ // NPM_TOKEN=x AWS_SECRET_ACCESS_KEY=x DB_PASSWORD=x+ [new RegExp(`\\b([A-Za-z_][A-Za-z0-9_]{0,63}(?:TOKEN|SECRET|PASSWORD|PASSWD|API_?KEY|ACCESS_?KEY|PRIVATE_?KEY|CREDENTIALS?))=(?:${QUOTED_OR_BARE})`, 'gi'), '$1=[redacted]'],+ // user:password@host in a URL+ [/(:\/\/[^\s:@/]{1,128}:)[^\s@/]{1,256}(@)/g, '$1[redacted]$2'],+ // well-known token shapes+ [/\b(?:sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|npm_[A-Za-z0-9]{20,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, '[redacted]'],+];++/**+ * The form of a command that may be written to the store: secrets of the+ * common shapes replaced with `[redacted]`, and the text capped at+ * `MAX_RECORDED_COMMAND_CHARS`. This is best effort, not a guarantee; the off+ * switch is the guarantee.+ */+export function redactCommandForStorage(command: string): string {+ let text = String(command);+ if (text.length > MAX_SCANNED_CHARS) {+ // Cut at a token boundary so a half-token at the cut is not left behind.+ const cut = text.slice(0, MAX_SCANNED_CHARS);+ const lastSpace = cut.search(/\s\S*$/);+ text = lastSpace > 0 ? cut.slice(0, lastSpace) : cut;+ text += ' ';+ }+ for (const [re, replacement] of RULES) text = text.replace(re, replacement);+ if (text.length > MAX_RECORDED_COMMAND_CHARS) {+ text = `${text.slice(0, MAX_RECORDED_COMMAND_CHARS)} β¦[truncated]`;+ }+ return text.trimEnd();+}