claude-flow 3.25.0 → 3.25.2

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.
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import initSqlJs from 'sql.js';
9
- import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync } from 'fs';
9
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync, openSync, writeSync, fsyncSync, closeSync, renameSync, rmSync } from 'fs';
10
10
  import { dirname, join, basename } from 'path';
11
11
  import { fileURLToPath } from 'url';
12
12
  import { execSync } from 'child_process';
@@ -138,7 +138,22 @@ async function initDatabase() {
138
138
  function persist() {
139
139
  const data = db.export();
140
140
  const buffer = Buffer.from(data);
141
- writeFileSync(DB_PATH, buffer);
141
+ // Atomic write (issue #2584): temp → fsync → rename so a kill/OOM mid-flush
142
+ // or a concurrent writer can't leave a torn, malformed metrics.db image.
143
+ const tmp = `${DB_PATH}.tmp-${process.pid}-${Date.now().toString(36)}`;
144
+ let fd;
145
+ try {
146
+ fd = openSync(tmp, 'wx');
147
+ if (buffer.length > 0) writeSync(fd, buffer, 0, buffer.length, 0);
148
+ fsyncSync(fd);
149
+ closeSync(fd);
150
+ fd = undefined;
151
+ renameSync(tmp, DB_PATH);
152
+ } catch (e) {
153
+ if (fd !== undefined) { try { closeSync(fd); } catch { /* */ } }
154
+ try { rmSync(tmp, { force: true }); } catch { /* */ }
155
+ throw e;
156
+ }
142
157
  }
143
158
 
144
159
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-flow",
3
- "version": "3.25.0",
3
+ "version": "3.25.2",
4
4
  "description": "Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -16,6 +16,25 @@
16
16
  * sniff so legacy plaintext files keep working unchanged during the
17
17
  * incremental migration.
18
18
  */
19
+ /**
20
+ * Crash-safe atomic file write (issue #2584).
21
+ *
22
+ * A plain `writeFileSync(dbPath, db.export())` of a large sql.js image is NOT
23
+ * crash-safe: a kill/OOM mid-write — or a second process rewriting the same
24
+ * path concurrently — leaves a half-written image, and reopening it yields
25
+ * `database disk image is malformed`. The corruption window scales with file
26
+ * size, so a 185 MB monolithic flush is especially exposed.
27
+ *
28
+ * This writes to a unique temp sibling, fsyncs the bytes to disk, then
29
+ * `rename()`s over the target. rename() is atomic on POSIX within one
30
+ * filesystem, so a reader/reopen sees either the old complete image or the new
31
+ * complete image — never a torn one. The directory entry is best-effort fsynced
32
+ * so the rename itself survives a crash. On any failure the temp file is
33
+ * removed and the original is left untouched.
34
+ */
35
+ export declare function writeFileAtomic(path: string, data: Buffer, opts?: {
36
+ mode?: number;
37
+ }): void;
19
38
  /**
20
39
  * Create a directory tree with mode 0700 (owner-only). No-op if exists.
21
40
  * Uses recursive: true so missing parents are created with the same mode.
@@ -16,8 +16,76 @@
16
16
  * sniff so legacy plaintext files keep working unchanged during the
17
17
  * incremental migration.
18
18
  */
19
- import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
19
+ import { chmodSync, closeSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeSync, } from 'node:fs';
20
+ import { basename, dirname, join } from 'node:path';
20
21
  import { decryptBuffer, encryptBuffer, getKey, isEncryptedBlob, isEncryptionEnabled, } from './encryption/vault.js';
22
+ /**
23
+ * Crash-safe atomic file write (issue #2584).
24
+ *
25
+ * A plain `writeFileSync(dbPath, db.export())` of a large sql.js image is NOT
26
+ * crash-safe: a kill/OOM mid-write — or a second process rewriting the same
27
+ * path concurrently — leaves a half-written image, and reopening it yields
28
+ * `database disk image is malformed`. The corruption window scales with file
29
+ * size, so a 185 MB monolithic flush is especially exposed.
30
+ *
31
+ * This writes to a unique temp sibling, fsyncs the bytes to disk, then
32
+ * `rename()`s over the target. rename() is atomic on POSIX within one
33
+ * filesystem, so a reader/reopen sees either the old complete image or the new
34
+ * complete image — never a torn one. The directory entry is best-effort fsynced
35
+ * so the rename itself survives a crash. On any failure the temp file is
36
+ * removed and the original is left untouched.
37
+ */
38
+ export function writeFileAtomic(path, data, opts = {}) {
39
+ const mode = opts.mode ?? 0o600;
40
+ const dir = dirname(path);
41
+ const tmp = join(dir, `.${basename(path)}.tmp-${process.pid}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`);
42
+ let fd;
43
+ try {
44
+ fd = openSync(tmp, 'wx', mode);
45
+ if (data.length > 0)
46
+ writeSync(fd, data, 0, data.length, 0);
47
+ fsyncSync(fd); // durability: force bytes to disk BEFORE the rename
48
+ closeSync(fd);
49
+ fd = undefined;
50
+ renameSync(tmp, path); // atomic swap — readers never see a torn image
51
+ try {
52
+ chmodSync(path, mode);
53
+ }
54
+ catch {
55
+ // Windows / FS without POSIX modes — silently skip.
56
+ }
57
+ // Best-effort: fsync the directory so the rename survives a power loss.
58
+ try {
59
+ const dfd = openSync(dir, 'r');
60
+ try {
61
+ fsyncSync(dfd);
62
+ }
63
+ finally {
64
+ closeSync(dfd);
65
+ }
66
+ }
67
+ catch {
68
+ // Directory fsync unsupported (e.g. Windows) — the rename is still atomic.
69
+ }
70
+ }
71
+ catch (e) {
72
+ if (fd !== undefined) {
73
+ try {
74
+ closeSync(fd);
75
+ }
76
+ catch {
77
+ /* already closed */
78
+ }
79
+ }
80
+ try {
81
+ unlinkSync(tmp);
82
+ }
83
+ catch {
84
+ /* temp never created / already gone */
85
+ }
86
+ throw e;
87
+ }
88
+ }
21
89
  /**
22
90
  * Create a directory tree with mode 0700 (owner-only). No-op if exists.
23
91
  * Uses recursive: true so missing parents are created with the same mode.
@@ -45,20 +113,11 @@ export function writeFileRestricted(path, data, optsOrEncoding = 'utf-8') {
45
113
  const plaintext = Buffer.isBuffer(data) ? data : Buffer.from(data, encoding);
46
114
  payload = encryptBuffer(plaintext, getKey());
47
115
  }
48
- // For encrypted payloads we always have a Buffer pass through without an
49
- // encoding so writeFileSync doesn't try to text-decode it.
50
- if (Buffer.isBuffer(payload)) {
51
- writeFileSync(path, payload);
52
- }
53
- else {
54
- writeFileSync(path, payload, encoding);
55
- }
56
- try {
57
- chmodSync(path, 0o600);
58
- }
59
- catch {
60
- // Windows / FS without POSIX modes — silently skip.
61
- }
116
+ // Atomic write (temp fsync rename) so a torn/concurrent flush can never
117
+ // leave a half-written file the failure mode behind issue #2584. Buffers go
118
+ // straight through; strings are encoded first (default utf-8).
119
+ const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload, encoding);
120
+ writeFileAtomic(path, buf, { mode: 0o600 });
62
121
  }
63
122
  export function readFileMaybeEncrypted(path, encoding = 'utf-8') {
64
123
  const raw = readFileSync(path);
@@ -89,26 +89,26 @@ function ensureEmbeddings() {
89
89
  return embeddingsPromise;
90
90
  embeddingsPromise = (async () => {
91
91
  try {
92
- // Tier -1 (PRIMARY): Lattice WASM — real multi-model semantic embeddings
93
- // (miniLM / bge / paraphrase-miniLM / GPU qwen3-0.6b) ahead of everything.
94
- // Optional + FAIL-CLOSED: absent/init-fail ⇒ fall straight through to the
95
- // existing ruvector-ONNX → hash tiers with zero regression.
92
+ // Tier -1 (PRIMARY): optional WASM embedder a pluggable real embedder
93
+ // ahead of everything. OPT-IN (RUFLO_EMBED_WASM_PKG) + FAIL-CLOSED: unset
94
+ // or absent/init-fail ⇒ fall straight through to the existing
95
+ // ruvector-ONNX → hash tiers with zero regression.
96
96
  try {
97
- const lat = await import('../ruvector/lattice-wasm.js').catch(() => null);
98
- if (lat && await lat.latticeAvailable()) {
99
- const model = lat.DEFAULT_LATTICE_MODEL;
97
+ const we = await import('../ruvector/wasm-embedder.js').catch(() => null);
98
+ if (we && await we.wasmEmbedderAvailable()) {
99
+ const model = we.DEFAULT_EMBED_MODEL;
100
100
  realEmbeddings = {
101
101
  embed: async (text) => {
102
- const v = await lat.latticeEmbed(text, model);
102
+ const v = await we.wasmEmbed(text, model);
103
103
  if (!v || !v.length)
104
- throw new Error('lattice embed failed'); // → generateEmbedding falls to next tier
104
+ throw new Error('wasm embed failed'); // → generateEmbedding falls to next tier
105
105
  return v;
106
106
  },
107
107
  };
108
- embeddingServiceName = `lattice-wasm/${model} (${lat.latticeModels().length} model${lat.latticeModels().length === 1 ? '' : 's'})`;
108
+ embeddingServiceName = `wasm-embedder/${model} (${we.wasmEmbedderModels().length} model${we.wasmEmbedderModels().length === 1 ? '' : 's'})`;
109
109
  }
110
110
  }
111
- catch { /* lattice absent — fall through */ }
111
+ catch { /* not configured — fall through */ }
112
112
  // Tier 0: ruvector@0.2.27 — bundled all-MiniLM-L6-v2 + parallel worker pool.
113
113
  // Probe with isOnnxAvailable() and verify an actual embed succeeds (avoids
114
114
  // the type-load-success-but-runtime-fails trap from ADR-086). The probe now
@@ -320,6 +320,7 @@ async function generateEmbedding(text, dims = 384) {
320
320
  // Hash-based deterministic embedding (better than pure random for consistency)
321
321
  // NOTE: No semantic meaning — only useful for consistent deduplication, not similarity search
322
322
  if (text) {
323
+ (await import('../memory/embedding-policy.js')).enforceNoStub('neural-tools.generateEmbedding'); // "no stubs" strict mode
323
324
  if (embeddingServiceName === 'none') {
324
325
  embeddingServiceName = 'hash-fallback';
325
326
  }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Embedding policy — "no more stubs" enforcement.
3
+ *
4
+ * Every embedder in the codebase tries REAL models first (Lattice WASM →
5
+ * ruvector ONNX → AgentDB bridge → transformers.js) and only falls back to a
6
+ * deterministic HASH embedding when none loads. A hash embedding has no semantic
7
+ * meaning — it silently degrades similarity search into noise.
8
+ *
9
+ * This module makes that failure mode a POLICY choice instead of a silent
10
+ * default. With RUFLO_REQUIRE_REAL_EMBEDDINGS truthy, any hash last-resort throws
11
+ * loudly ("no stubs") rather than returning a meaningless vector — fail-closed,
12
+ * so a broken embedding substrate is a hard error, not corrupt retrieval.
13
+ *
14
+ * Default (unset): unchanged behavior (warn + degrade), so nothing breaks for
15
+ * installs where a real embedder genuinely can't load. Set the flag in any
16
+ * environment that must never serve pseudo-embeddings.
17
+ */
18
+ export declare function requireRealEmbeddings(): boolean;
19
+ /** Throw the canonical "no stubs" error if strict mode is on; else no-op. */
20
+ export declare function enforceNoStub(where: string): void;
21
+ //# sourceMappingURL=embedding-policy.d.ts.map
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Embedding policy — "no more stubs" enforcement.
3
+ *
4
+ * Every embedder in the codebase tries REAL models first (Lattice WASM →
5
+ * ruvector ONNX → AgentDB bridge → transformers.js) and only falls back to a
6
+ * deterministic HASH embedding when none loads. A hash embedding has no semantic
7
+ * meaning — it silently degrades similarity search into noise.
8
+ *
9
+ * This module makes that failure mode a POLICY choice instead of a silent
10
+ * default. With RUFLO_REQUIRE_REAL_EMBEDDINGS truthy, any hash last-resort throws
11
+ * loudly ("no stubs") rather than returning a meaningless vector — fail-closed,
12
+ * so a broken embedding substrate is a hard error, not corrupt retrieval.
13
+ *
14
+ * Default (unset): unchanged behavior (warn + degrade), so nothing breaks for
15
+ * installs where a real embedder genuinely can't load. Set the flag in any
16
+ * environment that must never serve pseudo-embeddings.
17
+ */
18
+ export function requireRealEmbeddings() {
19
+ return /^(1|true|yes|on|strict)$/i.test(process.env.RUFLO_REQUIRE_REAL_EMBEDDINGS ?? '');
20
+ }
21
+ /** Throw the canonical "no stubs" error if strict mode is on; else no-op. */
22
+ export function enforceNoStub(where) {
23
+ if (requireRealEmbeddings()) {
24
+ throw new Error(`[no-stub] real embeddings required but only a hash fallback was available at ${where}. ` +
25
+ `RUFLO_REQUIRE_REAL_EMBEDDINGS is set — install a real embedder ` +
26
+ `(ruvector, or @claude-flow/embeddings with "embeddings init --download") ` +
27
+ `or unset the flag to allow degraded hash embeddings.`);
28
+ }
29
+ }
30
+ //# sourceMappingURL=embedding-policy.js.map
@@ -262,6 +262,9 @@ export declare function recoverMemoryDatabase(dbPath: string, opts?: {
262
262
  backupPath?: string;
263
263
  rows?: number;
264
264
  reason?: string;
265
+ restoredFromBackup?: boolean;
266
+ from?: string;
267
+ restoreReason?: string;
265
268
  }>;
266
269
  export declare function repairVectorIndexes(dbPath: string, opts?: {
267
270
  verbose?: boolean;
@@ -11,7 +11,8 @@
11
11
  import * as fs from 'fs';
12
12
  import * as path from 'path';
13
13
  import { createRequire } from 'node:module';
14
- import { readFileMaybeEncrypted, writeFileRestricted } from '../fs-secure.js';
14
+ import { readFileMaybeEncrypted, writeFileAtomic, writeFileRestricted } from '../fs-secure.js';
15
+ import { restoreMemoryDbFromBackup } from '../services/memory-backup.js';
15
16
  /**
16
17
  * #2356 — cached, synchronous capability probe for @ruvector/core. `getHNSWStatus`
17
18
  * is sync and is called by `neural status` in a fresh process that never warms
@@ -1182,6 +1183,16 @@ async function activateControllerRegistry(dbPath, verbose) {
1182
1183
  export async function recoverMemoryDatabase(dbPath, opts = {}) {
1183
1184
  if (!dbPath || !fs.existsSync(dbPath))
1184
1185
  return { recovered: false, reason: 'no-db' };
1186
+ // Fallback for when the in-place rebuild can't produce a verified DB (issue
1187
+ // #2584): rebuild-from-corrupt salvages nothing when the damage is total, so
1188
+ // restore the newest integrity-ok backup instead of leaving the store dead.
1189
+ const restoreFromBackup = async (reason) => {
1190
+ const r = await restoreMemoryDbFromBackup(dbPath, { verbose: opts.verbose });
1191
+ if (r.restored) {
1192
+ return { recovered: true, restoredFromBackup: true, backupPath: r.corruptBackupPath, rows: r.rows, from: r.from };
1193
+ }
1194
+ return { recovered: false, reason, restoreReason: r.skipped };
1195
+ };
1185
1196
  let Database;
1186
1197
  try {
1187
1198
  // Module name behind a variable so TS does not statically resolve the
@@ -1190,7 +1201,7 @@ export async function recoverMemoryDatabase(dbPath, opts = {}) {
1190
1201
  Database = (await import(mod)).default;
1191
1202
  }
1192
1203
  catch {
1193
- return { recovered: false, reason: 'no-native' };
1204
+ return await restoreFromBackup('no-native');
1194
1205
  }
1195
1206
  const ts = Date.now();
1196
1207
  const tmpPath = `${dbPath}.recovering-${ts}`;
@@ -1287,9 +1298,9 @@ export async function recoverMemoryDatabase(dbPath, opts = {}) {
1287
1298
  }
1288
1299
  catch { /* */ }
1289
1300
  if (opts.verbose) {
1290
- console.log(`memory DB auto-recovery aborted (integrity=${integ}, rows ${dstRows}/${srcRows}) — original untouched`);
1301
+ console.log(`memory DB rebuild-in-place failed (integrity=${integ}, rows ${dstRows}/${srcRows}) — trying newest good backup`);
1291
1302
  }
1292
- return { recovered: false, reason: 'verify-failed' };
1303
+ return await restoreFromBackup('verify-failed');
1293
1304
  }
1294
1305
  // Back up the corrupt DB, then atomically swap in the verified rebuild.
1295
1306
  fs.copyFileSync(dbPath, bakPath);
@@ -1323,8 +1334,8 @@ export async function recoverMemoryDatabase(dbPath, opts = {}) {
1323
1334
  }
1324
1335
  catch { /* */ }
1325
1336
  if (opts.verbose)
1326
- console.log(`memory DB auto-recovery error: ${e?.message ?? e}`);
1327
- return { recovered: false, reason: 'error' };
1337
+ console.log(`memory DB rebuild-in-place error (${e?.message ?? e}) — trying newest good backup`);
1338
+ return await restoreFromBackup('error');
1328
1339
  }
1329
1340
  }
1330
1341
  export async function repairVectorIndexes(dbPath, opts = {}) {
@@ -1717,9 +1728,9 @@ export async function applyTemporalDecay(dbPath) {
1717
1728
  `;
1718
1729
  db.run(decayQuery, [now, now, now]);
1719
1730
  const changes = db.getRowsModified();
1720
- // Save
1731
+ // Save (atomic — issue #2584: a torn full-image flush corrupts the store)
1721
1732
  const data = db.export();
1722
- fs.writeFileSync(path_, Buffer.from(data));
1733
+ writeFileAtomic(path_, Buffer.from(data));
1723
1734
  db.close();
1724
1735
  return {
1725
1736
  success: true,
@@ -2011,6 +2022,7 @@ export async function generateLocalEmbedding(text) {
2011
2022
  }
2012
2023
  // Deterministic hash-based fallback (for testing/demo without ONNX).
2013
2024
  // AUDIT #3: backend='mock' — these vectors do NOT carry real semantics.
2025
+ (await import('./embedding-policy.js')).enforceNoStub('memory-initializer.generateLocalEmbedding'); // "no stubs" strict mode
2014
2026
  const embedding = generateHashEmbedding(text, state.dimensions);
2015
2027
  return {
2016
2028
  embedding,
@@ -13,6 +13,7 @@
13
13
  import os from 'node:os';
14
14
  import path from 'node:path';
15
15
  import { randomUUID } from 'node:crypto';
16
+ import { enforceNoStub } from '../memory/embedding-policy.js';
16
17
  // ============================================================================
17
18
  // Fallback Implementation (when ruvector not available)
18
19
  // ============================================================================
@@ -248,6 +249,7 @@ export function generateEmbedding(text, dimensions = 768) {
248
249
  // Fall back to hash-based embedding
249
250
  }
250
251
  }
252
+ enforceNoStub('vector-db.generateEmbedding'); // "no stubs" strict mode → throw instead of hash
251
253
  const embedding = generateHashEmbedding(text, dimensions);
252
254
  // Tag the result so consumers can detect it came from hash fallback
253
255
  Object.defineProperty(embedding, '_warning', {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Package specifier for the optional WASM embedder. EMPTY by default (opt-in).
3
+ * `RUFLO_LATTICE_WASM_PKG` is accepted as a back-compat alias.
4
+ */
5
+ export declare const EMBED_WASM_PKG: string;
6
+ export declare const DEFAULT_EMBED_MODEL: string;
7
+ /** Is an optional WASM embedder configured + installed + initializable? Cached; never throws. */
8
+ export declare function wasmEmbedderAvailable(): Promise<boolean>;
9
+ /** Models the configured WASM embedder reports (empty until available). */
10
+ export declare function wasmEmbedderModels(): string[];
11
+ /** Embed via the optional WASM embedder, or null (caller falls through). Never throws. */
12
+ export declare function wasmEmbed(text: string, model?: string): Promise<number[] | null>;
13
+ //# sourceMappingURL=wasm-embedder.d.ts.map
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Optional WASM embedder tier — a pluggable PRIMARY embedding tier ahead of
3
+ * ruvector-ONNX → hash.
4
+ *
5
+ * OPT-IN by design: there is NO default package (an earlier iteration defaulted
6
+ * to `@ruvector/lattice-wasm`, which does not exist — npm 404). Point
7
+ * `RUFLO_EMBED_WASM_PKG` at any real WASM embedder that follows the ruvnet
8
+ * wasm-bindgen convention (a text→vector embed export). With the env unset, this
9
+ * tier is INERT and everything resolves exactly as before (ruvector ONNX). Fully
10
+ * fail-closed; never throws.
11
+ *
12
+ * The adapter probes the module's API tolerantly and verifies a real embed
13
+ * succeeds before accepting the tier (guards the ADR-086 loads-but-runtime-fails
14
+ * trap), so an incompatible package simply reports unavailable.
15
+ */
16
+ import { createRequire } from 'module';
17
+ import { readFileSync } from 'fs';
18
+ const require_ = createRequire(import.meta.url);
19
+ /**
20
+ * Package specifier for the optional WASM embedder. EMPTY by default (opt-in).
21
+ * `RUFLO_LATTICE_WASM_PKG` is accepted as a back-compat alias.
22
+ */
23
+ export const EMBED_WASM_PKG = process.env.RUFLO_EMBED_WASM_PKG || process.env.RUFLO_LATTICE_WASM_PKG || '';
24
+ export const DEFAULT_EMBED_MODEL = process.env.RUFLO_EMBED_MODEL || 'default';
25
+ /* eslint-disable @typescript-eslint/no-explicit-any */
26
+ let _mod = null;
27
+ let _ready = false;
28
+ let _probed = false;
29
+ let _available = false;
30
+ let _models = [];
31
+ async function loadModule() {
32
+ if (!EMBED_WASM_PKG)
33
+ return null; // opt-in: no package configured ⇒ inert
34
+ if (_mod)
35
+ return _mod;
36
+ _mod = await import(EMBED_WASM_PKG).catch(() => null); // optional — absent ⇒ null ⇒ unavailable
37
+ return _mod;
38
+ }
39
+ async function ensureInit() {
40
+ if (_ready)
41
+ return true;
42
+ const mod = await loadModule();
43
+ if (!mod)
44
+ return false;
45
+ try {
46
+ if (typeof mod.initSync === 'function') {
47
+ let wasmBytes;
48
+ for (const cand of ['index_bg.wasm', 'embedder_bg.wasm', 'wasm_bg.wasm']) {
49
+ try {
50
+ wasmBytes = readFileSync(require_.resolve(`${EMBED_WASM_PKG}/${cand}`));
51
+ break;
52
+ }
53
+ catch { /* try next */ }
54
+ }
55
+ mod.initSync(wasmBytes ? { module: wasmBytes } : undefined);
56
+ }
57
+ else if (typeof mod.default === 'function') {
58
+ await mod.default();
59
+ }
60
+ _ready = true;
61
+ return true;
62
+ }
63
+ catch {
64
+ return false;
65
+ }
66
+ }
67
+ function toVec(r) {
68
+ const v = (r && typeof r === 'object' && 'embedding' in r) ? r.embedding : r;
69
+ if (!v)
70
+ return null;
71
+ if (Array.isArray(v))
72
+ return v;
73
+ if (v.length !== undefined)
74
+ return Array.from(v);
75
+ return null;
76
+ }
77
+ async function embedRaw(text, model) {
78
+ const mod = _mod;
79
+ if (!mod)
80
+ return null;
81
+ const attempts = [
82
+ () => typeof mod.embed === 'function' ? mod.embed(text, model) : undefined,
83
+ () => typeof mod.embed === 'function' ? mod.embed(text) : undefined,
84
+ () => typeof mod.embedText === 'function' ? mod.embedText(text, model) : undefined,
85
+ () => typeof mod.Embedder === 'function' ? new mod.Embedder(model).embed(text) : undefined,
86
+ ];
87
+ for (const a of attempts) {
88
+ try {
89
+ const r = await a();
90
+ const v = toVec(r);
91
+ if (v)
92
+ return v;
93
+ }
94
+ catch { /* try next surface */ }
95
+ }
96
+ return null;
97
+ }
98
+ /** Is an optional WASM embedder configured + installed + initializable? Cached; never throws. */
99
+ export async function wasmEmbedderAvailable() {
100
+ if (_probed)
101
+ return _available;
102
+ _probed = true;
103
+ try {
104
+ if (!EMBED_WASM_PKG) {
105
+ _available = false;
106
+ return false;
107
+ } // opt-in; not configured
108
+ if (!(await ensureInit())) {
109
+ _available = false;
110
+ return false;
111
+ }
112
+ const mod = _mod;
113
+ try {
114
+ const list = typeof mod.listModels === 'function' ? mod.listModels() : (mod.models ?? mod.MODELS);
115
+ if (Array.isArray(list) && list.length)
116
+ _models = list;
117
+ }
118
+ catch { /* leave default */ }
119
+ if (!_models.length)
120
+ _models = [DEFAULT_EMBED_MODEL];
121
+ const probe = await embedRaw('probe', DEFAULT_EMBED_MODEL);
122
+ _available = !!probe && probe.length > 0;
123
+ return _available;
124
+ }
125
+ catch {
126
+ _available = false;
127
+ return false;
128
+ }
129
+ }
130
+ /** Models the configured WASM embedder reports (empty until available). */
131
+ export function wasmEmbedderModels() { return [..._models]; }
132
+ /** Embed via the optional WASM embedder, or null (caller falls through). Never throws. */
133
+ export async function wasmEmbed(text, model = DEFAULT_EMBED_MODEL) {
134
+ try {
135
+ if (!(await wasmEmbedderAvailable()))
136
+ return null;
137
+ return await embedRaw(text, model);
138
+ }
139
+ catch {
140
+ return null;
141
+ }
142
+ }
143
+ //# sourceMappingURL=wasm-embedder.js.map
@@ -21,4 +21,33 @@ export interface BackupResult {
21
21
  }
22
22
  export declare function defaultMemoryDbPath(cwd?: string): string;
23
23
  export declare function backupMemoryDb(opts?: BackupOptions): Promise<BackupResult>;
24
+ export interface RestoreResult {
25
+ restored: boolean;
26
+ /** The backup file that was restored. */
27
+ from?: string;
28
+ /** memory_entries count in the restored DB (-1 if it couldn't be verified). */
29
+ rows?: number;
30
+ /** Where the corrupt live DB was parked before the swap. */
31
+ corruptBackupPath?: string;
32
+ skipped?: string;
33
+ }
34
+ /**
35
+ * Restore the newest integrity-ok backup over a corrupt/malformed memory DB
36
+ * (issue #2584).
37
+ *
38
+ * The in-place rebuild path (`recoverMemoryDatabase`) rebuilds FROM the corrupt
39
+ * image, so when the damage is bad enough that `sqlite3 .recover` salvages
40
+ * nothing, that rebuild also salvages nothing and every `memory_store` keeps
41
+ * erroring. This is the missing fallback: scan `<db dir>/backups/` newest-first,
42
+ * pick the newest snapshot that passes `PRAGMA integrity_check` (and has rows),
43
+ * park the corrupt live DB at `<db>.corrupt-<ts>.bak`, then ATOMICALLY install
44
+ * the good backup (copy → fsync → rename, no full-image buffer in memory). Drops
45
+ * stale -wal/-shm. Non-destructive on failure: the live DB is only replaced once
46
+ * a verified backup is in hand.
47
+ */
48
+ export declare function restoreMemoryDbFromBackup(dbPath: string, opts?: {
49
+ destDir?: string;
50
+ timestamp?: number;
51
+ verbose?: boolean;
52
+ }): Promise<RestoreResult>;
24
53
  //# sourceMappingURL=memory-backup.d.ts.map
@@ -91,4 +91,108 @@ export async function backupMemoryDb(opts = {}) {
91
91
  }
92
92
  return { backedUp: true, path: destPath, sizeBytes, rotatedAway, gcsUri };
93
93
  }
94
+ /**
95
+ * Restore the newest integrity-ok backup over a corrupt/malformed memory DB
96
+ * (issue #2584).
97
+ *
98
+ * The in-place rebuild path (`recoverMemoryDatabase`) rebuilds FROM the corrupt
99
+ * image, so when the damage is bad enough that `sqlite3 .recover` salvages
100
+ * nothing, that rebuild also salvages nothing and every `memory_store` keeps
101
+ * erroring. This is the missing fallback: scan `<db dir>/backups/` newest-first,
102
+ * pick the newest snapshot that passes `PRAGMA integrity_check` (and has rows),
103
+ * park the corrupt live DB at `<db>.corrupt-<ts>.bak`, then ATOMICALLY install
104
+ * the good backup (copy → fsync → rename, no full-image buffer in memory). Drops
105
+ * stale -wal/-shm. Non-destructive on failure: the live DB is only replaced once
106
+ * a verified backup is in hand.
107
+ */
108
+ export async function restoreMemoryDbFromBackup(dbPath, opts = {}) {
109
+ if (!dbPath)
110
+ return { restored: false, skipped: 'no-db-path' };
111
+ const destDir = opts.destDir ?? path.join(path.dirname(dbPath), 'backups');
112
+ let snaps;
113
+ try {
114
+ snaps = fs
115
+ .readdirSync(destDir)
116
+ .filter(f => /^memory-.*\.db$/.test(f))
117
+ .map(f => path.join(destDir, f))
118
+ .sort() // ISO-stamped names sort chronologically
119
+ .reverse(); // newest first
120
+ }
121
+ catch {
122
+ return { restored: false, skipped: 'no-backups-dir' };
123
+ }
124
+ if (!snaps.length)
125
+ return { restored: false, skipped: 'no-backups' };
126
+ // better-sqlite3 verifies a candidate's integrity. Absent (WASM-only host) →
127
+ // accept the newest non-empty snapshot, flagged rows=-1 (unverified).
128
+ let Database = null;
129
+ try {
130
+ const mod = 'better-sqlite3';
131
+ Database = (await import(mod)).default;
132
+ }
133
+ catch {
134
+ /* verifier unavailable — trust newest non-empty */
135
+ }
136
+ let chosen = null;
137
+ for (const file of snaps) {
138
+ try {
139
+ if (Database) {
140
+ const db = new Database(file, { readonly: true });
141
+ const integ = String(db.pragma('integrity_check', { simple: true }) ?? '');
142
+ let rows = 0;
143
+ try {
144
+ rows = db.prepare('SELECT COUNT(*) AS c FROM memory_entries').get()?.c ?? 0;
145
+ }
146
+ catch { /* no entries table — not a usable memory DB */ }
147
+ db.close();
148
+ if (integ.toLowerCase() === 'ok' && rows > 0) {
149
+ chosen = { file, rows };
150
+ break;
151
+ }
152
+ }
153
+ else if (fs.statSync(file).size > 0) {
154
+ chosen = { file, rows: -1 };
155
+ break;
156
+ }
157
+ }
158
+ catch { /* unreadable snapshot — try the next-older one */ }
159
+ }
160
+ if (!chosen)
161
+ return { restored: false, skipped: 'no-integrity-ok-backup' };
162
+ const ts = opts.timestamp ?? Date.now();
163
+ const corruptBackupPath = `${dbPath}.corrupt-${ts}.bak`;
164
+ const tmp = `${dbPath}.restoring-${ts}`;
165
+ try {
166
+ if (fs.existsSync(dbPath))
167
+ fs.copyFileSync(dbPath, corruptBackupPath);
168
+ fs.copyFileSync(chosen.file, tmp); // stream copy — no 185MB buffer
169
+ const fd = fs.openSync(tmp, 'r+');
170
+ try {
171
+ fs.fsyncSync(fd);
172
+ }
173
+ finally {
174
+ fs.closeSync(fd);
175
+ } // durable before swap
176
+ fs.renameSync(tmp, dbPath); // atomic install
177
+ for (const s of ['-wal', '-shm']) {
178
+ try {
179
+ fs.rmSync(`${dbPath}${s}`, { force: true });
180
+ }
181
+ catch { /* */ }
182
+ }
183
+ }
184
+ catch (e) {
185
+ try {
186
+ fs.rmSync(tmp, { force: true });
187
+ }
188
+ catch { /* */ }
189
+ return { restored: false, skipped: `install failed: ${e?.message ?? e}` };
190
+ }
191
+ if (opts.verbose) {
192
+ console.log(`memory DB restored from backup ${path.basename(chosen.file)}` +
193
+ (chosen.rows >= 0 ? ` (${chosen.rows} rows)` : ' (unverified)') +
194
+ `. Corrupt original saved to ${corruptBackupPath}`);
195
+ }
196
+ return { restored: true, from: chosen.file, rows: chosen.rows, corruptBackupPath };
197
+ }
94
198
  //# sourceMappingURL=memory-backup.js.map
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claude-flow/cli",
3
- "version": "3.25.0",
3
+ "version": "3.25.2",
4
4
  "type": "module",
5
5
  "description": "Ruflo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
6
6
  "main": "dist/src/index.js",