moflo 4.8.41 → 4.8.43

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.
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Shared background process manager for moflo.
3
+ *
4
+ * All background spawn paths (hooks.mjs, hook-handler.cjs, session-start-launcher.mjs)
5
+ * delegate here so that PID tracking, dedup, and cleanup happen in one place.
6
+ *
7
+ * API:
8
+ * spawn(cmd, args, label) — spawn with label-based dedup + PID tracking
9
+ * killAll() — SIGTERM every tracked process, prune registry
10
+ * getActive() — list currently alive tracked processes
11
+ * prune() — remove dead entries from registry
12
+ *
13
+ * Registry: .claude-flow/background-pids.json
14
+ * Lock: .claude-flow/spawn.lock (30 s TTL — prevents thundering-herd)
15
+ */
16
+
17
+ import { spawn } from 'child_process';
18
+ import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync, unlinkSync, statSync, openSync, closeSync } from 'fs';
19
+ import { resolve, dirname } from 'path';
20
+ import { fileURLToPath } from 'url';
21
+
22
+ const __filename = fileURLToPath(import.meta.url);
23
+ const __dirname = dirname(__filename);
24
+
25
+ const LOCK_TTL_MS = 30_000;
26
+
27
+ /** Resolve the project root (two levels up from bin/lib/). */
28
+ function defaultRoot() {
29
+ return resolve(__dirname, '../..');
30
+ }
31
+
32
+ /** Ensure .claude-flow/ directory exists. */
33
+ function ensureDir(dir) {
34
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
35
+ }
36
+
37
+ /** Check if a PID is alive (cross-platform). */
38
+ function isAlive(pid) {
39
+ try {
40
+ process.kill(pid, 0);
41
+ return true;
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ // ── Registry I/O ────────────────────────────────────────────────────────────
48
+
49
+ function registryPath(root) {
50
+ return resolve(root, '.claude-flow', 'background-pids.json');
51
+ }
52
+
53
+ function lockPath(root) {
54
+ return resolve(root, '.claude-flow', 'spawn.lock');
55
+ }
56
+
57
+ function readRegistry(root) {
58
+ const p = registryPath(root);
59
+ if (!existsSync(p)) return [];
60
+ try {
61
+ const parsed = JSON.parse(readFileSync(p, 'utf-8'));
62
+ return Array.isArray(parsed) ? parsed : [];
63
+ } catch {
64
+ return [];
65
+ }
66
+ }
67
+
68
+ /** Atomic write: write to tmp file then rename to avoid torn reads. */
69
+ function writeRegistry(root, entries) {
70
+ const p = registryPath(root);
71
+ const tmp = p + '.tmp.' + process.pid;
72
+ ensureDir(dirname(p));
73
+ writeFileSync(tmp, JSON.stringify(entries, null, 2));
74
+ renameSync(tmp, p);
75
+ }
76
+
77
+ // ── Lock (30 s TTL) ────────────────────────────────────────────────────────
78
+
79
+ function checkLock(root) {
80
+ const lp = lockPath(root);
81
+ if (!existsSync(lp)) return false;
82
+ try {
83
+ const age = Date.now() - statSync(lp).mtimeMs;
84
+ return age < LOCK_TTL_MS;
85
+ } catch {
86
+ return false;
87
+ }
88
+ }
89
+
90
+ /** Atomic lock acquisition using exclusive-create flag. */
91
+ function writeLock(root) {
92
+ const lp = lockPath(root);
93
+ ensureDir(dirname(lp));
94
+ try {
95
+ writeFileSync(lp, String(Date.now()), { flag: 'wx' });
96
+ } catch {
97
+ // File already exists — overwrite if stale, otherwise skip
98
+ try {
99
+ const age = Date.now() - statSync(lp).mtimeMs;
100
+ if (age >= LOCK_TTL_MS) {
101
+ unlinkSync(lp);
102
+ writeFileSync(lp, String(Date.now()), { flag: 'wx' });
103
+ }
104
+ } catch { /* lost race on stale cleanup — non-fatal */ }
105
+ }
106
+ }
107
+
108
+ function clearLock(root) {
109
+ const lp = lockPath(root);
110
+ try {
111
+ if (existsSync(lp)) unlinkSync(lp);
112
+ } catch { /* non-fatal */ }
113
+ }
114
+
115
+ // ── Public API ──────────────────────────────────────────────────────────────
116
+
117
+ /**
118
+ * Create a ProcessManager bound to a project root.
119
+ * @param {string} [root] — project root (defaults to two levels above bin/lib/)
120
+ */
121
+ export function createProcessManager(root) {
122
+ const projectRoot = root || defaultRoot();
123
+
124
+ return {
125
+ /**
126
+ * Spawn a background process with label-based dedup and PID tracking.
127
+ *
128
+ * If a process with the same `label` is already alive, the spawn is skipped.
129
+ *
130
+ * @param {string} cmd — executable (e.g. 'node')
131
+ * @param {string[]} args — arguments
132
+ * @param {string} label — unique label for dedup (e.g. 'index-guidance')
133
+ * @returns {{ pid: number|null, skipped: boolean }}
134
+ */
135
+ spawn(cmd, args, label) {
136
+ // Dedup: skip if same label is already alive
137
+ const entries = readRegistry(projectRoot);
138
+ const existing = entries.find(e => e.label === label);
139
+ if (existing && isAlive(existing.pid)) {
140
+ return { pid: existing.pid, skipped: true };
141
+ }
142
+
143
+ try {
144
+ // Redirect background process output to log file instead of /dev/null
145
+ // This ensures errors from background indexers/pretrain are captured
146
+ let stdio = 'ignore';
147
+ try {
148
+ const swarmDir = resolve(projectRoot, '.swarm');
149
+ ensureDir(swarmDir);
150
+ const logPath = resolve(swarmDir, 'background.log');
151
+ const fd = openSync(logPath, 'a');
152
+ stdio = ['ignore', fd, fd];
153
+ } catch {
154
+ // Fall back to ignore if log file can't be opened
155
+ }
156
+
157
+ const proc = spawn(cmd, args, {
158
+ cwd: projectRoot,
159
+ stdio,
160
+ detached: true,
161
+ shell: false,
162
+ windowsHide: true,
163
+ });
164
+
165
+ // Swallow async spawn errors (e.g. ENOENT for bad command)
166
+ proc.on('error', () => {});
167
+ proc.unref();
168
+
169
+ if (proc.pid) {
170
+ // Remove any stale entry with the same label, then append new
171
+ const fresh = entries.filter(e => e.label !== label);
172
+ fresh.push({
173
+ pid: proc.pid,
174
+ label,
175
+ cmd: `${cmd} ${args.join(' ')}`.substring(0, 200),
176
+ startedAt: new Date().toISOString(),
177
+ });
178
+ writeRegistry(projectRoot, fresh);
179
+ }
180
+
181
+ return { pid: proc.pid || null, skipped: false };
182
+ } catch {
183
+ return { pid: null, skipped: false };
184
+ }
185
+ },
186
+
187
+ /**
188
+ * Kill all tracked background processes.
189
+ * @returns {{ killed: number, total: number }}
190
+ */
191
+ killAll() {
192
+ const entries = readRegistry(projectRoot);
193
+ let killed = 0;
194
+
195
+ for (const entry of entries) {
196
+ if (!isAlive(entry.pid)) continue;
197
+ try {
198
+ process.kill(entry.pid, 'SIGTERM');
199
+ killed++;
200
+ } catch { /* already gone */ }
201
+ }
202
+
203
+ // Clear registry and lock
204
+ writeRegistry(projectRoot, []);
205
+ clearLock(projectRoot);
206
+
207
+ return { killed, total: entries.length };
208
+ },
209
+
210
+ /**
211
+ * Return list of currently alive tracked processes.
212
+ * @returns {Array<{ pid: number, label: string, cmd: string, startedAt: string }>}
213
+ */
214
+ getActive() {
215
+ const entries = readRegistry(projectRoot);
216
+ return entries.filter(e => isAlive(e.pid));
217
+ },
218
+
219
+ /**
220
+ * Remove dead entries from the registry.
221
+ * @returns {{ pruned: number, remaining: number }}
222
+ */
223
+ prune() {
224
+ const entries = readRegistry(projectRoot);
225
+ const alive = entries.filter(e => isAlive(e.pid));
226
+ writeRegistry(projectRoot, alive);
227
+ return { pruned: entries.length - alive.length, remaining: alive.length };
228
+ },
229
+
230
+ /**
231
+ * Check if the spawn lock is held (another session-restore spawned recently).
232
+ */
233
+ isLocked() {
234
+ return checkLock(projectRoot);
235
+ },
236
+
237
+ /**
238
+ * Acquire the spawn lock (30 s TTL).
239
+ */
240
+ acquireLock() {
241
+ writeLock(projectRoot);
242
+ },
243
+
244
+ /**
245
+ * Release the spawn lock.
246
+ */
247
+ releaseLock() {
248
+ clearLock(projectRoot);
249
+ },
250
+
251
+ /** Expose the project root for callers that need it. */
252
+ get root() {
253
+ return projectRoot;
254
+ },
255
+ };
256
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Synchronous cleanup of the ProcessManager background-pids registry.
3
+ *
4
+ * Safe to call from CJS hooks that run under process.exit() — no async,
5
+ * no ESM imports, pure fs + process.kill.
6
+ *
7
+ * Used by: .claude/helpers/hook-handler.cjs, bin/hook-handler.cjs (session-end)
8
+ */
9
+ 'use strict';
10
+
11
+ var fs = require('fs');
12
+ var path = require('path');
13
+
14
+ /**
15
+ * Kill all tracked background processes and clear the registry.
16
+ * @param {string} projectDir - absolute path to the project root
17
+ * @returns {number} count of processes killed
18
+ */
19
+ function killTrackedSync(projectDir) {
20
+ var pidFile = path.join(projectDir, '.claude-flow', 'background-pids.json');
21
+ var lockFile = path.join(projectDir, '.claude-flow', 'spawn.lock');
22
+ var killed = 0;
23
+
24
+ try {
25
+ if (fs.existsSync(pidFile)) {
26
+ var entries = JSON.parse(fs.readFileSync(pidFile, 'utf-8'));
27
+ if (!Array.isArray(entries)) entries = [];
28
+ for (var i = 0; i < entries.length; i++) {
29
+ try { process.kill(entries[i].pid, 0); } catch (e) { continue; }
30
+ try { process.kill(entries[i].pid, 'SIGTERM'); killed++; } catch (e) { /* ok */ }
31
+ }
32
+ fs.writeFileSync(pidFile, '[]');
33
+ }
34
+ } catch (e) { /* non-fatal */ }
35
+
36
+ try { if (fs.existsSync(lockFile)) fs.unlinkSync(lockFile); } catch (e) { /* ok */ }
37
+
38
+ return killed;
39
+ }
40
+
41
+ module.exports = { killTrackedSync };
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  /**
3
3
  * Semantic search using 384-dim embeddings (Xenova/all-MiniLM-L6-v2 or hash fallback)
4
4
  *
@@ -13,7 +13,22 @@ import { resolve, dirname } from 'path';
13
13
  import { fileURLToPath } from 'url';
14
14
 
15
15
  const __dirname = dirname(fileURLToPath(import.meta.url));
16
- const projectRoot = resolve(__dirname, '../..');
16
+
17
+ // Detect project root by walking up from cwd to find package.json.
18
+ // IMPORTANT: Do NOT use resolve(__dirname, '..') or '../..' — this script lives
19
+ // in bin/ during development but gets synced to .claude/scripts/ in consumer
20
+ // projects, so __dirname-relative paths break. findProjectRoot() works everywhere.
21
+ function findProjectRoot() {
22
+ let dir = process.cwd();
23
+ const root = resolve(dir, '/');
24
+ while (dir !== root) {
25
+ if (existsSync(resolve(dir, 'package.json'))) return dir;
26
+ dir = dirname(dir);
27
+ }
28
+ return process.cwd();
29
+ }
30
+
31
+ const projectRoot = findProjectRoot();
17
32
 
18
33
  // ── 1. Helper: fire-and-forget a background process ─────────────────────────
19
34
  function fireAndForget(cmd, args, label) {
package/bin/index-all.mjs CHANGED
@@ -114,7 +114,7 @@ async function main() {
114
114
  if (isIndexEnabled('guidance')) {
115
115
  const guidanceScript = resolveBin('flo-index', 'index-guidance.mjs');
116
116
  if (guidanceScript) {
117
- runStep('guidance-index', 'node', [guidanceScript]);
117
+ runStep('guidance-index', 'node', [guidanceScript, '--no-embeddings']);
118
118
  } else {
119
119
  log('SKIP guidance-index (script not found)');
120
120
  }
@@ -126,7 +126,7 @@ async function main() {
126
126
  if (isIndexEnabled('code_map')) {
127
127
  const codeMapScript = resolveBin('flo-codemap', 'generate-code-map.mjs');
128
128
  if (codeMapScript) {
129
- runStep('code-map', 'node', [codeMapScript], 180_000);
129
+ runStep('code-map', 'node', [codeMapScript, '--no-embeddings'], 180_000);
130
130
  } else {
131
131
  log('SKIP code-map (script not found)');
132
132
  }
@@ -138,7 +138,7 @@ async function main() {
138
138
  if (isIndexEnabled('tests')) {
139
139
  const testScript = resolveBin('flo-testmap', 'index-tests.mjs');
140
140
  if (testScript) {
141
- runStep('test-index', 'node', [testScript]);
141
+ runStep('test-index', 'node', [testScript, '--no-embeddings']);
142
142
  } else {
143
143
  log('SKIP test-index (script not found)');
144
144
  }
@@ -166,7 +166,17 @@ async function main() {
166
166
  log('SKIP pretrain (CLI not found)');
167
167
  }
168
168
 
169
- // 6. HNSW rebuildMUST run last, after all writes are committed (#81)
169
+ // 6. Build embeddingssingle pass for ALL namespaces, after all indexers finish.
170
+ // Individual indexers are called with --no-embeddings to prevent background
171
+ // embedding spawns that race with this chain (sql.js last-write-wins).
172
+ const embeddingsScript = resolveBin('flo-embeddings', 'build-embeddings.mjs');
173
+ if (embeddingsScript) {
174
+ runStep('build-embeddings', 'node', [embeddingsScript], 300_000);
175
+ } else {
176
+ log('SKIP build-embeddings (script not found)');
177
+ }
178
+
179
+ // 7. HNSW rebuild — MUST run last, after all writes are committed (#81)
170
180
  if (localCli) {
171
181
  runStep('hnsw-rebuild', 'node', [localCli, 'memory', 'rebuild', '--force']);
172
182
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moflo",
3
- "version": "4.8.41",
3
+ "version": "4.8.43",
4
4
  "description": "MoFlo — AI agent orchestration for Claude Code. Forked from ruflo/claude-flow with patches applied to source, plus feature-level orchestration.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -89,7 +89,7 @@
89
89
  "@types/bcrypt": "^5.0.2",
90
90
  "@types/node": "^20.19.37",
91
91
  "eslint": "^8.0.0",
92
- "moflo": "^4.8.39",
92
+ "moflo": "^4.8.41",
93
93
  "tsx": "^4.21.0",
94
94
  "typescript": "^5.9.3",
95
95
  "vitest": "^4.0.0"
@@ -2,5 +2,5 @@
2
2
  * Auto-generated by build. Do not edit manually.
3
3
  * Source of truth: root package.json → scripts/sync-version.mjs
4
4
  */
5
- export const VERSION = '4.8.41';
5
+ export const VERSION = '4.8.43';
6
6
  //# sourceMappingURL=version.js.map
@@ -1,106 +1,106 @@
1
- {
2
- "name": "@moflo/cli",
3
- "version": "4.8.41",
4
- "type": "module",
5
- "description": "MoFlo CLI — AI agent orchestration with specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
6
- "main": "dist/src/index.js",
7
- "types": "dist/src/index.d.ts",
8
- "sideEffects": false,
9
- "bin": {
10
- "cli": "./bin/cli.js",
11
- "claude-flow": "./bin/cli.js",
12
- "claude-flow-mcp": "./bin/mcp-server.js"
13
- },
14
- "homepage": "https://github.com/eric-cielo/moflo#readme",
15
- "bugs": {
16
- "url": "https://github.com/eric-cielo/moflo/issues"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "https://github.com/eric-cielo/moflo.git",
21
- "directory": "v3/@claude-flow/cli"
22
- },
23
- "keywords": [
24
- "claude",
25
- "claude-code",
26
- "anthropic",
27
- "ai-agents",
28
- "multi-agent",
29
- "swarm",
30
- "mcp",
31
- "model-context-protocol",
32
- "llm",
33
- "cli",
34
- "orchestration",
35
- "automation",
36
- "developer-tools",
37
- "coding-assistant",
38
- "vector-database",
39
- "embeddings",
40
- "self-learning",
41
- "enterprise"
42
- ],
43
- "author": {
44
- "name": "Eric Cielo",
45
- "email": "eric@motailz.com",
46
- "url": "https://github.com/eric-cielo"
47
- },
48
- "license": "MIT",
49
- "exports": {
50
- ".": {
51
- "types": "./dist/src/index.d.ts",
52
- "import": "./dist/src/index.js"
53
- },
54
- "./ruvector": {
55
- "types": "./dist/src/ruvector/index.d.ts",
56
- "import": "./dist/src/ruvector/index.js"
57
- },
58
- "./ruvector/*": {
59
- "types": "./dist/src/ruvector/*.d.ts",
60
- "import": "./dist/src/ruvector/*.js"
61
- },
62
- "./mcp-tools": {
63
- "types": "./dist/src/mcp-tools/index.d.ts",
64
- "import": "./dist/src/mcp-tools/index.js"
65
- }
66
- },
67
- "files": [
68
- "dist",
69
- "bin",
70
- ".claude",
71
- "README.md"
72
- ],
73
- "scripts": {
74
- "build": "tsc",
75
- "test": "vitest run",
76
- "test:plugin-store": "npx tsx src/plugins/tests/standalone-test.ts",
77
- "test:pattern-store": "npx tsx src/transfer/store/tests/standalone-test.ts",
78
- "preinstall": "node bin/preinstall.cjs || true",
79
- "prepublishOnly": "cp ../../../README.md ./README.md",
80
- "release": "npm version prerelease --preid=alpha && npm run publish:all",
81
- "publish:all": "./scripts/publish.sh"
82
- },
83
- "devDependencies": {
84
- "typescript": "^5.3.0"
85
- },
86
- "dependencies": {
87
- "@noble/ed25519": "^2.1.0",
88
- "semver": "^7.6.0"
89
- },
90
- "optionalDependencies": {
91
- "@claude-flow/aidefence": "file:../aidefence",
92
- "@claude-flow/embeddings": "file:../embeddings",
93
- "@claude-flow/guidance": "file:../guidance",
94
- "@claude-flow/memory": "file:../memory",
95
- "@claude-flow/plugin-gastown-bridge": "^0.1.3",
96
- "agentic-flow": "^2.0.7",
97
- "@ruvector/attention": "^0.1.4",
98
- "@ruvector/learning-wasm": "^0.1.29",
99
- "@ruvector/router": "^0.1.27",
100
- "@ruvector/sona": "^0.1.5"
101
- },
102
- "publishConfig": {
103
- "access": "public",
104
- "tag": "latest"
105
- }
106
- }
1
+ {
2
+ "name": "@moflo/cli",
3
+ "version": "4.8.43",
4
+ "type": "module",
5
+ "description": "MoFlo CLI — AI agent orchestration with specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
6
+ "main": "dist/src/index.js",
7
+ "types": "dist/src/index.d.ts",
8
+ "sideEffects": false,
9
+ "bin": {
10
+ "cli": "./bin/cli.js",
11
+ "claude-flow": "./bin/cli.js",
12
+ "claude-flow-mcp": "./bin/mcp-server.js"
13
+ },
14
+ "homepage": "https://github.com/eric-cielo/moflo#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/eric-cielo/moflo/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/eric-cielo/moflo.git",
21
+ "directory": "v3/@claude-flow/cli"
22
+ },
23
+ "keywords": [
24
+ "claude",
25
+ "claude-code",
26
+ "anthropic",
27
+ "ai-agents",
28
+ "multi-agent",
29
+ "swarm",
30
+ "mcp",
31
+ "model-context-protocol",
32
+ "llm",
33
+ "cli",
34
+ "orchestration",
35
+ "automation",
36
+ "developer-tools",
37
+ "coding-assistant",
38
+ "vector-database",
39
+ "embeddings",
40
+ "self-learning",
41
+ "enterprise"
42
+ ],
43
+ "author": {
44
+ "name": "Eric Cielo",
45
+ "email": "eric@motailz.com",
46
+ "url": "https://github.com/eric-cielo"
47
+ },
48
+ "license": "MIT",
49
+ "exports": {
50
+ ".": {
51
+ "types": "./dist/src/index.d.ts",
52
+ "import": "./dist/src/index.js"
53
+ },
54
+ "./ruvector": {
55
+ "types": "./dist/src/ruvector/index.d.ts",
56
+ "import": "./dist/src/ruvector/index.js"
57
+ },
58
+ "./ruvector/*": {
59
+ "types": "./dist/src/ruvector/*.d.ts",
60
+ "import": "./dist/src/ruvector/*.js"
61
+ },
62
+ "./mcp-tools": {
63
+ "types": "./dist/src/mcp-tools/index.d.ts",
64
+ "import": "./dist/src/mcp-tools/index.js"
65
+ }
66
+ },
67
+ "files": [
68
+ "dist",
69
+ "bin",
70
+ ".claude",
71
+ "README.md"
72
+ ],
73
+ "scripts": {
74
+ "build": "tsc",
75
+ "test": "vitest run",
76
+ "test:plugin-store": "npx tsx src/plugins/tests/standalone-test.ts",
77
+ "test:pattern-store": "npx tsx src/transfer/store/tests/standalone-test.ts",
78
+ "preinstall": "node bin/preinstall.cjs || true",
79
+ "prepublishOnly": "cp ../../../README.md ./README.md",
80
+ "release": "npm version prerelease --preid=alpha && npm run publish:all",
81
+ "publish:all": "./scripts/publish.sh"
82
+ },
83
+ "devDependencies": {
84
+ "typescript": "^5.3.0"
85
+ },
86
+ "dependencies": {
87
+ "@noble/ed25519": "^2.1.0",
88
+ "semver": "^7.6.0"
89
+ },
90
+ "optionalDependencies": {
91
+ "@claude-flow/aidefence": "file:../aidefence",
92
+ "@claude-flow/embeddings": "file:../embeddings",
93
+ "@claude-flow/guidance": "file:../guidance",
94
+ "@claude-flow/memory": "file:../memory",
95
+ "@claude-flow/plugin-gastown-bridge": "^0.1.3",
96
+ "agentic-flow": "^2.0.7",
97
+ "@ruvector/attention": "^0.1.4",
98
+ "@ruvector/learning-wasm": "^0.1.29",
99
+ "@ruvector/router": "^0.1.27",
100
+ "@ruvector/sona": "^0.1.5"
101
+ },
102
+ "publishConfig": {
103
+ "access": "public",
104
+ "tag": "latest"
105
+ }
106
+ }