@claude-flow/cli 3.25.1 → 3.25.3
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.
- package/.claude/helpers/metrics-db.mjs +17 -2
- package/README.md +2 -0
- package/dist/src/benchmarks/gaia-agent.js +3 -3
- package/dist/src/commands/doctor.js +14 -1
- package/dist/src/commands/memory.js +6 -3
- package/dist/src/commands/neural.js +3 -1
- package/dist/src/fs-secure.d.ts +19 -0
- package/dist/src/fs-secure.js +74 -15
- package/dist/src/memory/memory-initializer.d.ts +3 -0
- package/dist/src/memory/memory-initializer.js +24 -13
- package/dist/src/services/memory-backup.d.ts +29 -0
- package/dist/src/services/memory-backup.js +104 -0
- package/package.json +2 -25
|
@@ -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
|
-
|
|
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/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
3
|
[](https://cognitum.one/agentic-engineering)
|
|
4
|
+
[](https://agentics.org/siliconvalley/?UTM=GH-RuFlo-SV)
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
<!-- Try Ruflo — the 4 badges first-time visitors actually act on -->
|
|
6
8
|
[](https://flo.ruv.io/)
|
|
@@ -537,11 +537,11 @@ export function isAnswerCorrect(modelAnswer, expected) {
|
|
|
537
537
|
// Exact match
|
|
538
538
|
if (normModel === normExpected)
|
|
539
539
|
return true;
|
|
540
|
-
// Substring match
|
|
540
|
+
// Substring match: expected contained in model answer (forward only).
|
|
541
|
+
// Reverse (normExpected.includes(normModel)) removed — see #2566 / ADR-169 R1:
|
|
542
|
+
// it scored fragmentary model answers ("a" vs "Paris, France") as correct.
|
|
541
543
|
if (normModel.includes(normExpected))
|
|
542
544
|
return true;
|
|
543
|
-
if (normExpected.includes(normModel))
|
|
544
|
-
return true;
|
|
545
545
|
// Numeric match with tolerance
|
|
546
546
|
const numModel = parseFloat(normModel.replace(/[^0-9.\-]/g, ''));
|
|
547
547
|
const numExpected = parseFloat(normExpected.replace(/[^0-9.\-]/g, ''));
|
|
@@ -269,11 +269,24 @@ async function checkLearningBridge() {
|
|
|
269
269
|
message: `@claude-flow/memory resolvable${version ? ` (v${version})` : ''}`,
|
|
270
270
|
};
|
|
271
271
|
}
|
|
272
|
+
// #2599: Self-heal on plain `doctor` (not just --fix). The project-side resolver
|
|
273
|
+
// fails when the sidecar is stale or missing (e.g. npx cache generation rotated),
|
|
274
|
+
// but the CLI process itself can resolve @claude-flow/memory from its own module
|
|
275
|
+
// context. Write the sidecar automatically instead of hard-failing the check.
|
|
276
|
+
const record = recordMemoryPackagePath(cwd, 'doctor-auto');
|
|
277
|
+
if (record) {
|
|
278
|
+
const version = readMemoryPackageVersion(record.distPath);
|
|
279
|
+
return {
|
|
280
|
+
name: 'Learning Bridge',
|
|
281
|
+
status: 'pass',
|
|
282
|
+
message: `@claude-flow/memory resolvable via auto-recorded sidecar${version ? ` (v${version})` : ''}`,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
272
285
|
return {
|
|
273
286
|
name: 'Learning Bridge',
|
|
274
287
|
status: 'fail',
|
|
275
288
|
message: '@claude-flow/memory NOT resolvable — SessionStart self-learning imports are a silent no-op',
|
|
276
|
-
fix: '
|
|
289
|
+
fix: 'npm i -D @claude-flow/memory (optional dep appears absent — likely --omit=optional install)',
|
|
277
290
|
};
|
|
278
291
|
}
|
|
279
292
|
// Check API keys
|
|
@@ -66,16 +66,19 @@ const storeCommand = {
|
|
|
66
66
|
{
|
|
67
67
|
name: 'upsert',
|
|
68
68
|
short: 'u',
|
|
69
|
-
|
|
69
|
+
// #2594: default true so `store → delete → store` doesn't hit the UNIQUE
|
|
70
|
+
// (namespace, key) constraint on the soft-deleted row. Pass --no-upsert
|
|
71
|
+
// for strict insert semantics.
|
|
72
|
+
description: 'Update if key exists (default; pass --no-upsert for strict insert)',
|
|
70
73
|
type: 'boolean',
|
|
71
|
-
default:
|
|
74
|
+
default: true
|
|
72
75
|
},
|
|
73
76
|
DB_PATH_OPTION
|
|
74
77
|
],
|
|
75
78
|
examples: [
|
|
76
79
|
{ command: 'claude-flow memory store -k "api/auth" -v "JWT implementation"', description: 'Store text' },
|
|
77
80
|
{ command: 'claude-flow memory store -k "pattern/singleton" --vector', description: 'Store vector' },
|
|
78
|
-
{ command: 'claude-flow memory store -k "pattern" -v "
|
|
81
|
+
{ command: 'claude-flow memory store -k "pattern" -v "new" --no-upsert', description: 'Strict insert (fail if key exists)' }
|
|
79
82
|
],
|
|
80
83
|
action: async (ctx) => {
|
|
81
84
|
const key = ctx.flags.key;
|
|
@@ -498,7 +498,9 @@ const statusCommand = {
|
|
|
498
498
|
},
|
|
499
499
|
{
|
|
500
500
|
component: 'ReasoningBank',
|
|
501
|
-
status: stats.
|
|
501
|
+
status: (stats.patternsLearned > 0 || stats.reasoningBankSize > 0)
|
|
502
|
+
? output.success('Active')
|
|
503
|
+
: output.dim('Empty'),
|
|
502
504
|
details: `${stats.patternsLearned} patterns stored`,
|
|
503
505
|
},
|
|
504
506
|
{
|
package/dist/src/fs-secure.d.ts
CHANGED
|
@@ -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.
|
package/dist/src/fs-secure.js
CHANGED
|
@@ -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,
|
|
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
|
-
//
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
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);
|
|
@@ -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
|
|
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
|
|
1301
|
+
console.log(`memory DB rebuild-in-place failed (integrity=${integ}, rows ${dstRows}/${srcRows}) — trying newest good backup`);
|
|
1291
1302
|
}
|
|
1292
|
-
return
|
|
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
|
|
1327
|
-
return
|
|
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
|
-
|
|
1733
|
+
writeFileAtomic(path_, Buffer.from(data));
|
|
1723
1734
|
db.close();
|
|
1724
1735
|
return {
|
|
1725
1736
|
success: true,
|
|
@@ -2232,11 +2243,11 @@ export async function verifyMemoryInit(dbPath, options) {
|
|
|
2232
2243
|
duration: Date.now() - indexStart
|
|
2233
2244
|
});
|
|
2234
2245
|
}
|
|
2235
|
-
//
|
|
2236
|
-
|
|
2237
|
-
//
|
|
2238
|
-
|
|
2239
|
-
|
|
2246
|
+
// Verification is read-only: sql.js holds an in-memory copy; discarding it on
|
|
2247
|
+
// close() leaves the on-disk DB untouched. Writing back here would race the
|
|
2248
|
+
// still-open better-sqlite3 handle (WAL) owned by ControllerRegistry /
|
|
2249
|
+
// repairVectorIndexes — atomic rename fails with EPERM on Windows (#2596)
|
|
2250
|
+
// and risks clobbering concurrent writes on all platforms.
|
|
2240
2251
|
db.close();
|
|
2241
2252
|
const passed = tests.filter(t => t.passed).length;
|
|
2242
2253
|
const failed = tests.filter(t => !t.passed).length;
|
|
@@ -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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claude-flow/cli",
|
|
3
|
-
"version": "3.25.
|
|
3
|
+
"version": "3.25.3",
|
|
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",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"test:plugin-store": "npx tsx src/plugins/tests/standalone-test.ts",
|
|
90
90
|
"test:pattern-store": "npx tsx src/transfer/store/tests/standalone-test.ts",
|
|
91
91
|
"postinstall": "node ./scripts/postinstall.cjs",
|
|
92
|
-
"prepublishOnly": "cp ../../../README.md ./README.md && rm -rf plugins && mkdir -p plugins && cp -r ../../../plugins/ruflo-metaharness plugins/",
|
|
92
|
+
"prepublishOnly": "cp ../../../README.md ./README.md && rm -rf plugins && mkdir -p plugins && cp -r ../../../plugins/ruflo-metaharness plugins/ && node scripts/sign-helpers.mjs && node scripts/verify-helpers.mjs",
|
|
93
93
|
"release": "npm version prerelease --preid=alpha && npm run publish:all",
|
|
94
94
|
"publish:all": "./scripts/publish.sh"
|
|
95
95
|
},
|
|
@@ -108,33 +108,10 @@
|
|
|
108
108
|
"yaml": "^2.8.0"
|
|
109
109
|
},
|
|
110
110
|
"optionalDependencies": {
|
|
111
|
-
"@claude-flow/aidefence": "^3.0.2",
|
|
112
|
-
"@claude-flow/codex": "^3.0.0-alpha.8",
|
|
113
|
-
"@claude-flow/embeddings": "^3.0.0-alpha.18",
|
|
114
|
-
"@claude-flow/guidance": "^3.0.0-alpha.1",
|
|
115
111
|
"@claude-flow/memory": "^3.0.0-alpha.21",
|
|
116
|
-
"@claude-flow/plugin-gastown-bridge": "^0.1.3",
|
|
117
112
|
"@claude-flow/security": "^3.0.0-alpha.10",
|
|
118
|
-
"@metaharness/darwin": "~0.8.0",
|
|
119
|
-
"@metaharness/kernel": "~0.1.2",
|
|
120
|
-
"@metaharness/redblue": "~0.1.4",
|
|
121
|
-
"@metaharness/router": "~0.3.2",
|
|
122
|
-
"@metaharness/weight-eft": "~0.1.1",
|
|
123
|
-
"@ruvector/attention": "^0.1.32",
|
|
124
|
-
"@ruvector/attention-darwin-arm64": "0.1.32",
|
|
125
|
-
"@ruvector/diskann": "^0.1.0",
|
|
126
|
-
"@ruvector/learning-wasm": "^0.1.29",
|
|
127
|
-
"@ruvector/router": "^0.1.30",
|
|
128
|
-
"@ruvector/ruvllm-wasm": "^2.0.2",
|
|
129
|
-
"@ruvector/rvagent-wasm": "^0.1.0",
|
|
130
|
-
"@ruvector/sona": "^0.1.6",
|
|
131
|
-
"@ruvector/tiny-dancer": "^0.1.22",
|
|
132
|
-
"agentbbs": "~0.1.0",
|
|
133
113
|
"agentdb": "^3.0.0-alpha.17",
|
|
134
114
|
"agentic-flow": "^3.0.0-alpha.1",
|
|
135
|
-
"agenticow": "~0.2.4",
|
|
136
|
-
"metaharness": "~0.2.8",
|
|
137
|
-
"page-agent": "~1.11.0",
|
|
138
115
|
"ruvector": "^0.2.27"
|
|
139
116
|
},
|
|
140
117
|
"publishConfig": {
|