moflo 4.9.5 → 4.9.6

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.
@@ -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.9.5';
5
+ export const VERSION = '4.9.6';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moflo",
3
- "version": "4.9.5",
3
+ "version": "4.9.6",
4
4
  "description": "MoFlo — AI agent orchestration for Claude Code. A standalone, opinionated toolkit with semantic memory, learned routing, gates, spells, and the /flo issue-execution skill.",
5
5
  "main": "dist/src/cli/index.js",
6
6
  "type": "module",
@@ -40,14 +40,15 @@
40
40
  "README.md",
41
41
  "LICENSE",
42
42
  "scripts/prune-native-binaries.mjs",
43
- "scripts/post-install-notice.mjs"
43
+ "scripts/post-install-notice.mjs",
44
+ "scripts/post-install-bootstrap.mjs"
44
45
  ],
45
46
  "scripts": {
46
47
  "dev": "tsx watch src/cli/index.ts",
47
48
  "prebuild": "node scripts/sync-version.mjs && node scripts/clean-dist.mjs",
48
49
  "build": "tsc",
49
50
  "prepublishOnly": "npm run build",
50
- "postinstall": "node scripts/prune-native-binaries.mjs && node scripts/post-install-notice.mjs",
51
+ "postinstall": "node scripts/prune-native-binaries.mjs && node scripts/post-install-notice.mjs && node scripts/post-install-bootstrap.mjs",
51
52
  "test": "node scripts/test-runner.mjs",
52
53
  "test:ui": "vitest --ui",
53
54
  "test:smoke": "node harness/consumer-smoke/run.mjs",
@@ -80,7 +81,7 @@
80
81
  "@typescript-eslint/eslint-plugin": "^7.18.0",
81
82
  "@typescript-eslint/parser": "^7.18.0",
82
83
  "eslint": "^8.0.0",
83
- "moflo": "^4.9.4",
84
+ "moflo": "^4.9.5",
84
85
  "tsx": "^4.21.0",
85
86
  "typescript": "^5.9.3",
86
87
  "vitest": "^4.0.0"
@@ -0,0 +1,291 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Postinstall self-update bootstrap (#857).
4
+ *
5
+ * Problem this solves:
6
+ * The launcher in <consumer>/.claude/scripts/session-start-launcher.mjs
7
+ * is responsible for copying itself + helpers from node_modules/moflo/
8
+ * on every upgrade. Pre-#854 launchers wrap each copyFileSync in a bare
9
+ * `catch { /* non-fatal *\/ }` and can't reliably replace themselves on
10
+ * Windows under file-lock contention (EBUSY/EPERM/EACCES from concurrent
11
+ * helper invocation, AV real-time scan, npm verification handles).
12
+ *
13
+ * The fix for that lives in the new launcher (#854/#855), but the old
14
+ * launcher has to work to deploy the new one. It doesn't, and consumers
15
+ * stay stuck across 8+ version bumps until manually unstuck.
16
+ *
17
+ * Fix:
18
+ * This script runs at npm postinstall — driven by npm, not by the broken
19
+ * launcher — and copies bin/ scripts + helpers DIRECTLY into the
20
+ * consumer's .claude/scripts/ and .claude/helpers/. After the bootstrap
21
+ * runs, the next session-start launches the NEW launcher, which then
22
+ * handles the rest of the upgrade work (guidance sync, manifest, version
23
+ * stamp, daemon recycle, HNSW rebuild).
24
+ *
25
+ * The bootstrap only has to do enough to break the deadlock.
26
+ *
27
+ * The lists below MUST stay aligned with bin/session-start-launcher.mjs
28
+ * section 3 (the launcher's own sync). A unit test asserts list parity
29
+ * (mcp-tools-drift-guard pattern). See SCRIPT_FILES / BIN_HELPER_FILES /
30
+ * SOURCE_HELPER_FILES exports.
31
+ *
32
+ * Failure posture:
33
+ * - Surface per-file failures on stderr with `flo doctor --fix` advice
34
+ * - Skip silently if <consumer>/.claude doesn't exist (consumer hasn't
35
+ * run `flo init` yet — bootstrap is a no-op for first-time installs)
36
+ * - Never exit non-zero (postinstall failures block npm install)
37
+ */
38
+
39
+ import {
40
+ copyFileSync,
41
+ existsSync,
42
+ mkdirSync,
43
+ readdirSync,
44
+ statSync,
45
+ } from 'node:fs';
46
+ import { dirname, join, resolve } from 'node:path';
47
+ import { fileURLToPath, pathToFileURL } from 'node:url';
48
+
49
+ const SCRIPT_PATH = fileURLToPath(import.meta.url);
50
+ const MOFLO_ROOT = resolve(dirname(SCRIPT_PATH), '..');
51
+
52
+ // ── Sync lists — keep in lockstep with bin/session-start-launcher.mjs §3 ─────
53
+ //
54
+ // Drift guard: tests/unit/post-install-bootstrap-drift.test.ts asserts these
55
+ // arrays match the launcher's section-3 sync lists by parsing both files.
56
+
57
+ export const SCRIPT_FILES = [
58
+ 'hooks.mjs',
59
+ 'session-start-launcher.mjs',
60
+ 'index-guidance.mjs',
61
+ 'build-embeddings.mjs',
62
+ 'generate-code-map.mjs',
63
+ 'semantic-search.mjs',
64
+ 'index-tests.mjs',
65
+ 'index-patterns.mjs',
66
+ 'index-all.mjs',
67
+ 'setup-project.mjs',
68
+ 'run-migrations.mjs',
69
+ ];
70
+
71
+ export const BIN_HELPER_FILES = [
72
+ 'gate.cjs',
73
+ 'gate-hook.mjs',
74
+ 'prompt-hook.mjs',
75
+ 'hook-handler.cjs',
76
+ ];
77
+
78
+ export const SOURCE_HELPER_FILES = [
79
+ 'auto-memory-hook.mjs',
80
+ 'statusline.cjs',
81
+ 'intelligence.cjs',
82
+ 'subagent-start.cjs',
83
+ 'subagent-bootstrap.json',
84
+ 'pre-commit',
85
+ 'post-commit',
86
+ ];
87
+
88
+ // ── Retry + circuit breaker (#854 contract) ──────────────────────────────────
89
+ //
90
+ // Mirrors the launcher's syncWithRetry. Backoff [50,200,800]ms covers Windows
91
+ // EBUSY windows from concurrent helper invocation + AV real-time scan. The
92
+ // breaker opens after 5 distinct files exhaust retries so a sick host
93
+ // (AV mid-scan over node_modules) doesn't compound wall-clock cost.
94
+
95
+ const TRANSIENT_CODES = new Set(['EBUSY', 'EPERM', 'EACCES']);
96
+ const RETRY_BACKOFF_MS = [50, 200, 800];
97
+ const CIRCUIT_BREAK_THRESHOLD = 5;
98
+
99
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
100
+
101
+ function makeSyncer() {
102
+ let circuitOpen = false;
103
+ const failures = [];
104
+
105
+ async function syncWithRetry(operation) {
106
+ const maxAttempts = circuitOpen ? 1 : RETRY_BACKOFF_MS.length + 1;
107
+ let lastErr = null;
108
+ let lastCode = null;
109
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
110
+ if (attempt > 0) await sleep(RETRY_BACKOFF_MS[attempt - 1]);
111
+ try {
112
+ operation();
113
+ return { ok: true };
114
+ } catch (err) {
115
+ lastErr = err;
116
+ lastCode = err && err.code ? err.code : null;
117
+ if (!TRANSIENT_CODES.has(lastCode)) break;
118
+ }
119
+ }
120
+ if (!circuitOpen && failures.length + 1 >= CIRCUIT_BREAK_THRESHOLD) {
121
+ circuitOpen = true;
122
+ }
123
+ return { ok: false, err: lastErr, code: lastCode };
124
+ }
125
+
126
+ async function syncFile(src, dest, manifestKey) {
127
+ if (!existsSync(src)) return { skipped: true };
128
+ try {
129
+ mkdirSync(dirname(dest), { recursive: true });
130
+ } catch (err) {
131
+ failures.push({ key: manifestKey, message: errMessage(err) });
132
+ return { ok: false };
133
+ }
134
+ const result = await syncWithRetry(() => copyFileSync(src, dest));
135
+ if (result.ok) return { ok: true };
136
+ const tail = TRANSIENT_CODES.has(result.code)
137
+ ? ` (retried ${RETRY_BACKOFF_MS.length}× after ${result.code}${circuitOpen ? '; circuit open' : ''})`
138
+ : '';
139
+ failures.push({ key: manifestKey, message: `${errMessage(result.err)}${tail}` });
140
+ return { ok: false };
141
+ }
142
+
143
+ return { syncFile, failures };
144
+ }
145
+
146
+ function errMessage(err) {
147
+ if (!err) return 'unknown error';
148
+ return err.code ? `${err.code} ${err.message || ''}`.trim() : (err.message || String(err));
149
+ }
150
+
151
+ // ── Project root discovery ──────────────────────────────────────────────────
152
+ //
153
+ // npm sets INIT_CWD to the directory where the user originally ran
154
+ // `npm install`. That's the consumer's project root regardless of which
155
+ // package's postinstall is running. Falls back to cwd for direct execution.
156
+
157
+ function consumerProjectRoot() {
158
+ return process.env.INIT_CWD || process.cwd();
159
+ }
160
+
161
+ // ── Main bootstrap ──────────────────────────────────────────────────────────
162
+
163
+ export async function runBootstrap({
164
+ projectRoot = consumerProjectRoot(),
165
+ mofloRoot = MOFLO_ROOT,
166
+ log = (msg) => process.stderr.write(`${msg}\n`),
167
+ } = {}) {
168
+ const claudeDir = resolve(projectRoot, '.claude');
169
+ if (!existsSync(claudeDir)) {
170
+ return { ran: false, reason: 'no-claude-dir' };
171
+ }
172
+
173
+ // moflo's own dogfood install: don't bootstrap into the source repo.
174
+ // The source repo's .claude/ IS the truth source — we'd be copying
175
+ // the very files we just built ON TOP of themselves, which on Windows
176
+ // hits the same file-lock issues we're trying to avoid.
177
+ if (resolve(projectRoot) === resolve(mofloRoot)) {
178
+ return { ran: false, reason: 'moflo-self-install' };
179
+ }
180
+
181
+ const binDir = resolve(mofloRoot, 'bin');
182
+ if (!existsSync(binDir)) {
183
+ return { ran: false, reason: 'no-bin-dir' };
184
+ }
185
+
186
+ const { syncFile, failures } = makeSyncer();
187
+ let synced = 0;
188
+
189
+ // 1. Top-level scripts → .claude/scripts/
190
+ const scriptsDir = resolve(claudeDir, 'scripts');
191
+ if (!existsSync(scriptsDir)) mkdirSync(scriptsDir, { recursive: true });
192
+ for (const file of SCRIPT_FILES) {
193
+ const result = await syncFile(
194
+ resolve(binDir, file),
195
+ resolve(scriptsDir, file),
196
+ `.claude/scripts/${file}`,
197
+ );
198
+ if (result.ok) synced++;
199
+ }
200
+
201
+ // 2. bin/lib/ → .claude/scripts/lib/ (read entire dir)
202
+ const libSrcDir = resolve(binDir, 'lib');
203
+ const libDestDir = resolve(scriptsDir, 'lib');
204
+ if (existsSync(libSrcDir)) {
205
+ if (!existsSync(libDestDir)) mkdirSync(libDestDir, { recursive: true });
206
+ let libEntries;
207
+ try {
208
+ libEntries = readdirSync(libSrcDir);
209
+ } catch (err) {
210
+ log(`bootstrap: lib readdir failed (${errMessage(err)})`);
211
+ libEntries = [];
212
+ }
213
+ for (const file of libEntries) {
214
+ const src = resolve(libSrcDir, file);
215
+ try {
216
+ if (!statSync(src).isFile()) continue;
217
+ } catch { continue; }
218
+ const result = await syncFile(src, resolve(libDestDir, file), `.claude/scripts/lib/${file}`);
219
+ if (result.ok) synced++;
220
+ }
221
+ }
222
+
223
+ // 3. bin/migrations/ → .claude/scripts/migrations/ (recursive)
224
+ const migrationsSrcDir = resolve(binDir, 'migrations');
225
+ const migrationsDestDir = resolve(scriptsDir, 'migrations');
226
+ if (existsSync(migrationsSrcDir)) {
227
+ if (!existsSync(migrationsDestDir)) mkdirSync(migrationsDestDir, { recursive: true });
228
+ let migEntries;
229
+ try {
230
+ migEntries = readdirSync(migrationsSrcDir, { recursive: true, withFileTypes: true });
231
+ } catch (err) {
232
+ log(`bootstrap: migrations readdir failed (${errMessage(err)})`);
233
+ migEntries = [];
234
+ }
235
+ for (const entry of migEntries) {
236
+ if (!entry.isFile()) continue;
237
+ const parent = entry.parentPath || entry.path || migrationsSrcDir;
238
+ const absSrc = resolve(parent, entry.name);
239
+ const rel = absSrc.slice(migrationsSrcDir.length + 1).split(/[\\/]/).join('/');
240
+ const result = await syncFile(absSrc, resolve(migrationsDestDir, rel), `.claude/scripts/migrations/${rel}`);
241
+ if (result.ok) synced++;
242
+ }
243
+ }
244
+
245
+ // 4. bin/ helpers → .claude/helpers/
246
+ const helpersDir = resolve(claudeDir, 'helpers');
247
+ if (!existsSync(helpersDir)) mkdirSync(helpersDir, { recursive: true });
248
+ for (const file of BIN_HELPER_FILES) {
249
+ const result = await syncFile(
250
+ resolve(binDir, file),
251
+ resolve(helpersDir, file),
252
+ `.claude/helpers/${file}`,
253
+ );
254
+ if (result.ok) synced++;
255
+ }
256
+
257
+ // 5. moflo's own .claude/helpers/ → consumer .claude/helpers/
258
+ // (these never lived in bin/ — they're shipped via .claude/helpers/** in files[])
259
+ const sourceHelpersDir = resolve(mofloRoot, '.claude/helpers');
260
+ if (existsSync(sourceHelpersDir)) {
261
+ for (const file of SOURCE_HELPER_FILES) {
262
+ const src = resolve(sourceHelpersDir, file);
263
+ if (!existsSync(src)) continue;
264
+ const result = await syncFile(src, resolve(helpersDir, file), `.claude/helpers/${file}`);
265
+ if (result.ok) synced++;
266
+ }
267
+ }
268
+
269
+ // Surface failures so npm log + Claude relay catches them, with the same
270
+ // healer advice the launcher uses.
271
+ if (failures.length > 0) {
272
+ const sample = failures.slice(0, 5).map((f) => ` - ${f.key}: ${f.message}`).join('\n');
273
+ const more = failures.length > 5 ? `\n …and ${failures.length - 5} more` : '';
274
+ log(
275
+ `moflo: postinstall bootstrap left ${failures.length} file(s) unsynced — run 'flo doctor --fix' to repair:\n${sample}${more}`,
276
+ );
277
+ }
278
+
279
+ return { ran: true, synced, failed: failures.length, failures };
280
+ }
281
+
282
+ // ── Entry point ─────────────────────────────────────────────────────────────
283
+
284
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
285
+ runBootstrap()
286
+ .catch((err) => {
287
+ // Never block install. Log and exit 0.
288
+ process.stderr.write(`moflo: bootstrap failed (${errMessage(err)})\n`);
289
+ })
290
+ .finally(() => process.exit(0));
291
+ }