claude-flow 3.77.2 → 3.78.1

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.
Files changed (38) hide show
  1. package/.claude/helpers/learning-loop.cjs +21 -5
  2. package/README.md +1 -1
  3. package/node_modules/@claude-flow/mcp/dist/transport/http.d.ts +7 -0
  4. package/node_modules/@claude-flow/mcp/dist/transport/http.d.ts.map +1 -1
  5. package/node_modules/@claude-flow/mcp/dist/transport/http.js +33 -29
  6. package/node_modules/@claude-flow/mcp/dist/transport/http.js.map +1 -1
  7. package/node_modules/@claude-flow/mcp/package.json +1 -1
  8. package/node_modules/@claude-flow/plugin-agent-federation/dist/transport/midstream-aware-loader.d.ts +4 -0
  9. package/node_modules/@claude-flow/plugin-agent-federation/dist/transport/midstream-aware-loader.d.ts.map +1 -1
  10. package/node_modules/@claude-flow/plugin-agent-federation/dist/transport/midstream-aware-loader.js +5 -1
  11. package/node_modules/@claude-flow/plugin-agent-federation/dist/transport/midstream-aware-loader.js.map +1 -1
  12. package/node_modules/@claude-flow/security/dist/input-validator.d.ts +2 -2
  13. package/node_modules/@claude-flow/security/dist/input-validator.d.ts.map +1 -1
  14. package/node_modules/@claude-flow/security/dist/input-validator.js +4 -2
  15. package/node_modules/@claude-flow/security/dist/input-validator.js.map +1 -1
  16. package/node_modules/@claude-flow/security/package.json +1 -1
  17. package/package.json +7 -7
  18. package/v3/@claude-flow/cli/README.md +202 -81
  19. package/v3/@claude-flow/cli/catalog-manifest.json +6 -6
  20. package/v3/@claude-flow/cli/dist/src/init/executor.js +10 -4
  21. package/v3/@claude-flow/cli/dist/src/init/helper-companions.d.ts +1 -1
  22. package/v3/@claude-flow/cli/dist/src/init/helper-companions.js +25 -3
  23. package/v3/@claude-flow/cli/dist/src/init/helper-refresh.js +7 -0
  24. package/v3/@claude-flow/cli/dist/src/mcp-tools/agentbbs-federation.d.ts +5 -0
  25. package/v3/@claude-flow/cli/dist/src/mcp-tools/agentbbs-federation.js +36 -5
  26. package/v3/@claude-flow/cli/dist/src/mcp-tools/capability-brain.js +9 -2
  27. package/v3/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +37 -1
  28. package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.d.ts +4 -2
  29. package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.js +239 -26
  30. package/v3/@claude-flow/cli/dist/src/memory/verified-outcomes.d.ts +10 -1
  31. package/v3/@claude-flow/cli/dist/src/memory/verified-outcomes.js +37 -3
  32. package/v3/@claude-flow/cli/dist/src/missions/schemas.d.ts +105 -561
  33. package/v3/@claude-flow/cli/dist/src/mods/command-registry/schema.d.ts +168 -729
  34. package/v3/@claude-flow/cli/dist/src/mods/command-registry/schema.js +1 -1
  35. package/v3/@claude-flow/cli/package.json +9 -9
  36. package/v3/@claude-flow/shared/dist/mcp/transport/http.d.ts +10 -0
  37. package/v3/@claude-flow/shared/dist/mcp/transport/http.js +44 -6
  38. package/v3/@claude-flow/shared/package.json +1 -1
@@ -648,7 +648,14 @@ export function buildCapabilityBrain(registeredTools, now = new Date()) {
648
648
  };
649
649
  });
650
650
  const uniqueToolCount = seen.size;
651
- const classifiedToolCount = uniqueToolCount;
651
+ // `fallbackClassifiedTools` pushes once per loop iteration (capability-brain.ts above),
652
+ // including re-occurrences of a duplicate name; dedupe before subtracting from the
653
+ // unique-name count, or a duplicate-named fallback tool drives classifiedToolCount
654
+ // negative (found by adversarial critique, 2026-11-16).
655
+ const classifiedToolCount = uniqueToolCount - new Set(fallbackClassifiedTools).size;
656
+ const coveragePercent = uniqueToolCount === 0
657
+ ? 100
658
+ : Number(((classifiedToolCount / uniqueToolCount) * 100).toFixed(2));
652
659
  return {
653
660
  schemaVersion: 'ruflo.capability-brain/v1',
654
661
  generatedAt: now.toISOString(),
@@ -669,7 +676,7 @@ export function buildCapabilityBrain(registeredTools, now = new Date()) {
669
676
  coverage: {
670
677
  registeredToolCount: uniqueToolCount,
671
678
  classifiedToolCount,
672
- coveragePercent: uniqueToolCount === 0 ? 100 : 100,
679
+ coveragePercent,
673
680
  fallbackClassifiedTools: fallbackClassifiedTools.sort(),
674
681
  duplicateToolNames: [...duplicates].sort(),
675
682
  },
@@ -14,6 +14,7 @@ import { validateIdentifier, validateText, validatePath } from './validate-input
14
14
  import { realpathNearest, isInsideRealRoot } from './realpath-containment.js';
15
15
  import { commandRecordingDisabled, redactCommandForStorage } from '../services/command-recording.js';
16
16
  import { checkCommandLoop, recordCommandOutcome } from './tool-loop-guardrail.js';
17
+ import { scanSettingsForRisk, formatRiskFindingsAsWarnings } from '../init/settings-risk-scanner.js';
17
18
  import { buildLearnedRoutingPatterns, } from '../services/learned-routing.js';
18
19
  import { applyTypesafeRouting, getTypesafeRouter } from '../ruvector/typesafe-router.js';
19
20
  import { DEFAULT_ROUTER_EMBEDDER, embedForRouter, resolveRouterEmbedder, routerEmbedderModel, } from '../ruvector/router-embedder.js';
@@ -2794,6 +2795,41 @@ export const hooksSessionRestore = {
2794
2795
  // Count task and agent entries
2795
2796
  const taskEntries = Object.keys(store.entries).filter(k => k.includes('task')).length;
2796
2797
  const agentEntries = Object.keys(store.entries).filter(k => k.includes('agent')).length;
2798
+ const warnings = [];
2799
+ if (restoreTasks && taskEntries > 0) {
2800
+ warnings.push(`${Math.min(taskEntries, 2)} tasks were in progress and may need review`);
2801
+ }
2802
+ // Re-run the same advisory settings.json risk scan `ruflo init`/`--upgrade`
2803
+ // already runs (settings-risk-scanner.ts) for an explicit `ruflo hooks
2804
+ // session-restore` call or an MCP client calling `hooks_session-restore`
2805
+ // directly — previously only `init`/`--upgrade` ever called it. The
2806
+ // automatic SessionStart path (`hook-handler.cjs session-restore`) has its
2807
+ // own CJS port of this scan (#4053). Advisory only: no enforcement, no
2808
+ // change to which hooks actually run.
2809
+ let settingsRaw;
2810
+ try {
2811
+ const settingsPath = join(getProjectCwd(), '.claude', 'settings.json');
2812
+ if (existsSync(settingsPath))
2813
+ settingsRaw = readFileSync(settingsPath, 'utf-8');
2814
+ }
2815
+ catch {
2816
+ // Unreadable settings.json is surfaced elsewhere (e.g. Claude Code's
2817
+ // own settings load path); this advisory scan stays best-effort.
2818
+ }
2819
+ if (settingsRaw !== undefined) {
2820
+ try {
2821
+ const settings = JSON.parse(settingsRaw);
2822
+ if (settings !== null && typeof settings === 'object' && !Array.isArray(settings)) {
2823
+ warnings.push(...formatRiskFindingsAsWarnings(scanSettingsForRisk({ hooks: settings.hooks, permissions: settings.permissions })));
2824
+ }
2825
+ // else: valid JSON but not a settings object (null/array/scalar) — not this scan's concern.
2826
+ }
2827
+ catch {
2828
+ // Malformed JSON or a scanner fault: this scan is advisory, so it must
2829
+ // never abort the session restore itself (same rule #4053 applied to
2830
+ // the automatic SessionStart path in hook-handler.cjs).
2831
+ }
2832
+ }
2797
2833
  return {
2798
2834
  sessionId: newSessionId,
2799
2835
  originalSessionId,
@@ -2802,7 +2838,7 @@ export const hooksSessionRestore = {
2802
2838
  agentsRestored: restoreAgents ? Math.min(agentEntries, 5) : 0,
2803
2839
  memoryRestored: memoryEntryCount,
2804
2840
  },
2805
- warnings: restoreTasks && taskEntries > 0 ? [`${Math.min(taskEntries, 2)} tasks were in progress and may need review`] : undefined,
2841
+ warnings: warnings.length > 0 ? warnings : undefined,
2806
2842
  dataSource: 'memory-store',
2807
2843
  };
2808
2844
  },
@@ -589,8 +589,10 @@ export declare function deleteEntry(options: {
589
589
  error?: string;
590
590
  }>;
591
591
  /**
592
- * Advisory O_EXCL lock scoped to a single memory.db file (`<dbPath>.lock`),
593
- * same stale-takeover pattern as services/global-ai-budget.ts.
592
+ * Advisory exclusive lock scoped to a single memory.db file (`<dbPath>.lock`),
593
+ * renewed while held and reclaimed after a scoped owner exits, or after five
594
+ * minutes without renewal when its identity cannot be verified. Hard links
595
+ * publish complete metadata atomically; O_EXCL is used where unavailable.
594
596
  *
595
597
  * #2878: sql.js persists by rewriting the whole database image, so every
596
598
  * `load → mutate → export → write` sequence is a read-modify-write that a
@@ -7,7 +7,9 @@ import { encodeEmbeddingQ8, MAX_LIST_EMBEDDINGS } from './embedding-q8.js';
7
7
  import * as fs from 'fs';
8
8
  import * as path from 'path';
9
9
  import { AsyncLocalStorage } from 'node:async_hooks';
10
+ import { randomUUID } from 'node:crypto';
10
11
  import { createRequire } from 'node:module';
12
+ import { hostname } from 'node:os';
11
13
  import { readFileMaybeEncrypted, writeDbImagePreservingFormat, writeFileAtomic, writeFileRestricted } from '../fs-secure.js';
12
14
  import { _resetEmbeddingConfigCache, DEFAULT_EMBEDDING_DIMENSION, resolveEffectiveEmbeddingConfig, resolveEmbeddingConfig, } from './embedding-config.js';
13
15
  import { restoreMemoryDbFromBackup } from '../services/memory-backup.js';
@@ -3497,7 +3499,188 @@ export async function deleteEntry(options) {
3497
3499
  // bounds the race to "another purge/delete running at the same instant",
3498
3500
  // which is the concrete case this feature needs to be safe against.
3499
3501
  const MEMORY_DB_LOCK_STALE_MS = 10_000;
3502
+ const MEMORY_DB_LOCK_UNVERIFIABLE_STALE_MS = 5 * 60_000;
3500
3503
  const MEMORY_DB_LOCK_ACQUIRE_TIMEOUT_MS = 15_000;
3504
+ const MEMORY_DB_LOCK_IDENTITY = (() => {
3505
+ let pidns;
3506
+ let boot;
3507
+ if (process.platform === 'linux') {
3508
+ try {
3509
+ pidns = fs.readlinkSync('/proc/self/ns/pid');
3510
+ boot = fs.readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim();
3511
+ }
3512
+ catch { /* missing Linux identity requires the longer stale lease */ }
3513
+ }
3514
+ return { host: hostname(), platform: process.platform, pidns, boot };
3515
+ })();
3516
+ function readMemoryDbLock(lockFile) {
3517
+ let fd;
3518
+ try {
3519
+ if (!fs.lstatSync(lockFile).isFile())
3520
+ return undefined;
3521
+ fd = fs.openSync(lockFile, fs.constants.O_RDONLY
3522
+ | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0));
3523
+ const stat = fs.fstatSync(fd);
3524
+ if (!stat.isFile() || stat.size > 1024)
3525
+ return undefined;
3526
+ const buffer = Buffer.alloc(1025);
3527
+ const size = fs.readSync(fd, buffer, 0, buffer.length, 0);
3528
+ if (size > 1024)
3529
+ return undefined;
3530
+ return { content: buffer.toString('utf8', 0, size), mtimeMs: stat.mtimeMs, dev: stat.dev, ino: stat.ino };
3531
+ }
3532
+ catch {
3533
+ return undefined;
3534
+ }
3535
+ finally {
3536
+ if (fd !== undefined)
3537
+ fs.closeSync(fd);
3538
+ }
3539
+ }
3540
+ function memoryDbLockOwnerState(content) {
3541
+ try {
3542
+ const owner = JSON.parse(content);
3543
+ // A PID alone (including the legacy format) cannot identify an owner
3544
+ // across hosts, kernel boots or container PID namespaces.
3545
+ if (!owner || owner.host !== MEMORY_DB_LOCK_IDENTITY.host
3546
+ || owner.platform !== MEMORY_DB_LOCK_IDENTITY.platform
3547
+ || owner.pidns !== MEMORY_DB_LOCK_IDENTITY.pidns
3548
+ || owner.boot !== MEMORY_DB_LOCK_IDENTITY.boot)
3549
+ return 'unknown';
3550
+ if (process.platform === 'linux'
3551
+ && (!MEMORY_DB_LOCK_IDENTITY.pidns || !MEMORY_DB_LOCK_IDENTITY.boot))
3552
+ return 'unknown';
3553
+ const pid = owner.pid;
3554
+ if (!Number.isInteger(pid) || pid <= 0 || pid > 0x7fffffff)
3555
+ return 'unknown';
3556
+ try {
3557
+ process.kill(pid, 0);
3558
+ return 'live';
3559
+ }
3560
+ catch (error) {
3561
+ // EPERM and unexpected probe failures are not evidence of a dead owner.
3562
+ const code = error.code;
3563
+ if (code === 'EPERM')
3564
+ return 'live';
3565
+ return code === 'ESRCH' ? 'dead' : 'unknown';
3566
+ }
3567
+ }
3568
+ catch {
3569
+ return 'unknown';
3570
+ }
3571
+ }
3572
+ function memoryDbLockCanBeReclaimed(snapshot) {
3573
+ const age = Date.now() - snapshot.mtimeMs;
3574
+ if (age <= MEMORY_DB_LOCK_STALE_MS)
3575
+ return false;
3576
+ // A live owner renews every few seconds, so five minutes without renewal
3577
+ // means the owner is gone even when its PID still answers: the PID may have
3578
+ // been reused by an unrelated process, or (on macOS/Windows, which have no
3579
+ // boot id) the lock may predate a reboot. The same lease covers owners whose
3580
+ // PID cannot be scoped to this host/boot/namespace.
3581
+ if (age > MEMORY_DB_LOCK_UNVERIFIABLE_STALE_MS)
3582
+ return true;
3583
+ // Inside the lease, only a verifiably dead owner may be taken over. A live
3584
+ // owner whose event loop is briefly blocked keeps its lock.
3585
+ return memoryDbLockOwnerState(snapshot.content) === 'dead';
3586
+ }
3587
+ function sameMemoryDbLock(a, b) {
3588
+ return a?.dev === b.dev && a.ino === b.ino && a.content === b.content && a.mtimeMs === b.mtimeMs;
3589
+ }
3590
+ function removeExpiredMemoryDbLockFile(file) {
3591
+ const snapshot = readMemoryDbLock(file);
3592
+ // Same rule as the main lock: a provably dead owner's guard or candidate is
3593
+ // removable once the short stale window passes, not only after the lease.
3594
+ if (!snapshot || !memoryDbLockCanBeReclaimed(snapshot))
3595
+ return;
3596
+ try {
3597
+ if (sameMemoryDbLock(readMemoryDbLock(file), snapshot))
3598
+ fs.unlinkSync(file);
3599
+ }
3600
+ catch { /* already removed or replaced */ }
3601
+ }
3602
+ function cleanupMemoryDbLockCandidates(lockFile) {
3603
+ const prefix = `${path.basename(lockFile)}.`;
3604
+ try {
3605
+ for (const name of fs.readdirSync(path.dirname(lockFile))) {
3606
+ if (name.startsWith(prefix)
3607
+ && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp$/i.test(name.slice(prefix.length))) {
3608
+ removeExpiredMemoryDbLockFile(path.join(path.dirname(lockFile), name));
3609
+ }
3610
+ }
3611
+ }
3612
+ catch { /* candidate cleanup must not prevent acquiring the lock */ }
3613
+ }
3614
+ /** Returns a separate descriptor only when hard-link publication is unavailable. */
3615
+ function publishMemoryDbLock(ownerFile, target, ownership) {
3616
+ try {
3617
+ fs.linkSync(ownerFile, target);
3618
+ return undefined;
3619
+ }
3620
+ catch (error) {
3621
+ if (!['ENOTSUP', 'EOPNOTSUPP', 'ENOSYS', 'EPERM', 'EXDEV'].includes(error.code ?? ''))
3622
+ throw error;
3623
+ }
3624
+ // Preserve the original O_EXCL protocol on filesystems without hard links.
3625
+ // A crash before writing ownership leaves an empty file recoverable by lease.
3626
+ const fd = fs.openSync(target, 'wx', 0o600);
3627
+ try {
3628
+ fs.writeFileSync(fd, ownership);
3629
+ return fd;
3630
+ }
3631
+ catch (error) {
3632
+ const owned = fs.fstatSync(fd);
3633
+ fs.closeSync(fd);
3634
+ try {
3635
+ const current = fs.lstatSync(target);
3636
+ if (current.dev === owned.dev && current.ino === owned.ino)
3637
+ fs.unlinkSync(target);
3638
+ }
3639
+ catch { /* already removed or replaced */ }
3640
+ throw error;
3641
+ }
3642
+ }
3643
+ function reclaimStaleMemoryDbLock(lockFile, ownerFile, ownership) {
3644
+ const previous = readMemoryDbLock(lockFile);
3645
+ if (!previous || !memoryDbLockCanBeReclaimed(previous))
3646
+ return false;
3647
+ // Only one reclaimer may inspect and unlink an orphan at a time. Without
3648
+ // this guard, a second reclaimer could unlink the first successor's lock.
3649
+ const guard = `${lockFile}.reclaim`;
3650
+ let guardFd;
3651
+ try {
3652
+ guardFd = publishMemoryDbLock(ownerFile, guard, ownership);
3653
+ }
3654
+ catch (error) {
3655
+ if (error.code === 'EEXIST')
3656
+ removeExpiredMemoryDbLockFile(guard);
3657
+ return false;
3658
+ }
3659
+ try {
3660
+ // Another reclaimer may have installed a successor while we waited. Read
3661
+ // again under our guard and preserve renewed or replaced lock inodes.
3662
+ const current = readMemoryDbLock(lockFile);
3663
+ if (!current || !sameMemoryDbLock(current, previous) || !memoryDbLockCanBeReclaimed(current))
3664
+ return false;
3665
+ if (readMemoryDbLock(guard)?.content !== ownership
3666
+ || !sameMemoryDbLock(readMemoryDbLock(lockFile), current))
3667
+ return false;
3668
+ fs.unlinkSync(lockFile);
3669
+ return true;
3670
+ }
3671
+ catch {
3672
+ return false;
3673
+ }
3674
+ finally {
3675
+ if (guardFd !== undefined)
3676
+ fs.closeSync(guardFd);
3677
+ try {
3678
+ if (readMemoryDbLock(guard)?.content === ownership)
3679
+ fs.unlinkSync(guard);
3680
+ }
3681
+ catch { /* preserve a replacement */ }
3682
+ }
3683
+ }
3501
3684
  function delayMs(ms) {
3502
3685
  return new Promise((r) => setTimeout(r, ms));
3503
3686
  }
@@ -3512,8 +3695,10 @@ function delayMs(ms) {
3512
3695
  */
3513
3696
  const heldMemoryDbLocks = new AsyncLocalStorage();
3514
3697
  /**
3515
- * Advisory O_EXCL lock scoped to a single memory.db file (`<dbPath>.lock`),
3516
- * same stale-takeover pattern as services/global-ai-budget.ts.
3698
+ * Advisory exclusive lock scoped to a single memory.db file (`<dbPath>.lock`),
3699
+ * renewed while held and reclaimed after a scoped owner exits, or after five
3700
+ * minutes without renewal when its identity cannot be verified. Hard links
3701
+ * publish complete metadata atomically; O_EXCL is used where unavailable.
3517
3702
  *
3518
3703
  * #2878: sql.js persists by rewriting the whole database image, so every
3519
3704
  * `load → mutate → export → write` sequence is a read-modify-write that a
@@ -3534,40 +3719,68 @@ export async function withMemoryDbLock(dbPath, fn) {
3534
3719
  const nested = new Set(held ?? []);
3535
3720
  nested.add(resolved);
3536
3721
  const lockFile = `${resolved}.lock`;
3722
+ cleanupMemoryDbLockCandidates(lockFile);
3723
+ removeExpiredMemoryDbLockFile(`${lockFile}.reclaim`);
3724
+ const token = randomUUID();
3725
+ const ownership = JSON.stringify({ ...MEMORY_DB_LOCK_IDENTITY, pid: process.pid, token });
3726
+ const ownerFile = `${lockFile}.${token}.tmp`;
3727
+ const fd = fs.openSync(ownerFile, 'wx', 0o600);
3537
3728
  const deadline = Date.now() + MEMORY_DB_LOCK_ACQUIRE_TIMEOUT_MS;
3538
- for (;;) {
3539
- let fd;
3540
- try {
3541
- fd = fs.openSync(lockFile, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY, 0o600);
3542
- }
3543
- catch (e) {
3544
- if (e.code !== 'EEXIST')
3545
- throw e;
3729
+ let heartbeat;
3730
+ let acquired = false;
3731
+ let lockFd;
3732
+ try {
3733
+ // Publish fully initialized ownership atomically. A crash while writing
3734
+ // the unique candidate must not leave an empty, unrecoverable lock file.
3735
+ fs.writeFileSync(fd, ownership);
3736
+ for (;;) {
3546
3737
  try {
3547
- const st = fs.lstatSync(lockFile);
3548
- if (Date.now() - st.mtimeMs > MEMORY_DB_LOCK_STALE_MS) {
3549
- fs.unlinkSync(lockFile);
3738
+ lockFd = publishMemoryDbLock(ownerFile, lockFile, ownership);
3739
+ acquired = true;
3740
+ break;
3741
+ }
3742
+ catch (e) {
3743
+ const code = e.code;
3744
+ if (code !== 'EEXIST')
3745
+ throw e;
3746
+ if (reclaimStaleMemoryDbLock(lockFile, ownerFile, ownership))
3550
3747
  continue;
3748
+ if (Date.now() > deadline) {
3749
+ const guard = `${lockFile}.reclaim`;
3750
+ const recovery = fs.existsSync(guard)
3751
+ ? `; inspect the recorded owner of ${guard} and confirm no reclamation is active before manual recovery`
3752
+ : '; inspect the recorded owner before manual recovery';
3753
+ throw new Error(`timed out acquiring memory.db lock: ${lockFile}${recovery}`);
3551
3754
  }
3755
+ await delayMs(25);
3552
3756
  }
3553
- catch { /* raced — retry */ }
3554
- if (Date.now() > deadline) {
3555
- throw new Error(`timed out acquiring memory.db lock: ${lockFile}`);
3556
- }
3557
- await delayMs(25);
3558
- continue;
3559
3757
  }
3560
- fs.writeSync(fd, String(process.pid));
3758
+ heartbeat = setInterval(() => {
3759
+ // Refresh the inode we opened, never a replacement at the same path.
3760
+ const now = new Date();
3761
+ try {
3762
+ fs.futimesSync(lockFd ?? fd, now, now);
3763
+ }
3764
+ catch { /* PID probing still protects a live owner */ }
3765
+ }, Math.floor(MEMORY_DB_LOCK_STALE_MS / 3));
3766
+ heartbeat.unref();
3767
+ return await heldMemoryDbLocks.run(nested, fn);
3768
+ }
3769
+ finally {
3770
+ if (heartbeat)
3771
+ clearInterval(heartbeat);
3772
+ if (lockFd !== undefined)
3773
+ fs.closeSync(lockFd);
3561
3774
  fs.closeSync(fd);
3562
3775
  try {
3563
- return await heldMemoryDbLocks.run(nested, fn);
3564
- }
3565
- finally {
3566
- try {
3776
+ if (acquired && readMemoryDbLock(lockFile)?.content === ownership)
3567
3777
  fs.unlinkSync(lockFile);
3568
- }
3569
- catch { /* already gone */ }
3570
3778
  }
3779
+ catch { /* already gone or replaced */ }
3780
+ try {
3781
+ fs.unlinkSync(ownerFile);
3782
+ }
3783
+ catch { /* already gone */ }
3571
3784
  }
3572
3785
  }
3573
3786
  export async function purgeNamespace(options) {
@@ -18,7 +18,10 @@
18
18
  * It then writes `.claude-flow/data/learned-lessons.json`: patterns that
19
19
  * verified outcomes produced, those at or above the SONA routing gate as
20
20
  * lessons, and repeated verified failures as cautions. The mod reads that
21
- * file and adds at most three cited lessons to a prompt.
21
+ * file and adds at most three cited lessons to a prompt. The file carries
22
+ * this machine's origin id (`~/.claude-flow/learning-loop-origin`, #4747),
23
+ * and readers show no file without it, so a lessons file committed to a
24
+ * repository is never presented as the project's own history.
22
25
  *
23
26
  * ADR-503 hardens it against gamed signals (verified-guards.ts,
24
27
  * verified-reverify.ts): a tainted success does not train unless the project
@@ -33,6 +36,8 @@ export { buildLessons, CAUTION_MIN_FAILURES, LESSON_GATE, lessonFile, lessonId,
33
36
  export declare const VERIFIED_PATH: string;
34
37
  export declare const LEDGER_PATH: string;
35
38
  export declare const LESSONS_PATH: string;
39
+ /** Under the home directory, never a project, so a cloned repository cannot carry it (#4747). */
40
+ export declare const ORIGIN_PATH: string;
36
41
  /** Ingested ids remembered; the mod keeps at most 500 lines, so this always covers the whole file. */
37
42
  export declare const MAX_LEDGER_IDS = 2000;
38
43
  /** A file past this many bytes is not read (the mod caps its own at far less). */
@@ -157,6 +162,8 @@ export declare function parseRecord(line: string): VerifiedRecord | null;
157
162
  export declare function emptyLedger(): IngestLedger;
158
163
  /** A ledger file read back; a corrupt or hostile one starts over (re-ingest is bounded by the mod's 500-line cap). */
159
164
  export declare function readLedger(raw: string | null): IngestLedger;
165
+ /** This machine's lessons origin id, created on first use; null when the home directory cannot hold it. */
166
+ export declare function lessonsOrigin(home?: string): string | null;
160
167
  export interface IngestOptions {
161
168
  /** Project root; defaults to process.cwd(), where SONA keeps `.swarm/`. */
162
169
  root?: string;
@@ -176,6 +183,8 @@ export interface IngestOptions {
176
183
  allowReverify?: boolean;
177
184
  /** Re-verify runner; defaults to the detached-git-worktree runner. Tests inject one. */
178
185
  reverifier?: ReverifyRunner;
186
+ /** Where the origin id lives (#4747); defaults to os.homedir(). */
187
+ home?: string;
179
188
  }
180
189
  export declare function ingestVerifiedOutcomes(options?: IngestOptions): Promise<IngestResult>;
181
190
  /**
@@ -18,7 +18,10 @@
18
18
  * It then writes `.claude-flow/data/learned-lessons.json`: patterns that
19
19
  * verified outcomes produced, those at or above the SONA routing gate as
20
20
  * lessons, and repeated verified failures as cautions. The mod reads that
21
- * file and adds at most three cited lessons to a prompt.
21
+ * file and adds at most three cited lessons to a prompt. The file carries
22
+ * this machine's origin id (`~/.claude-flow/learning-loop-origin`, #4747),
23
+ * and readers show no file without it, so a lessons file committed to a
24
+ * repository is never presented as the project's own history.
22
25
  *
23
26
  * ADR-503 hardens it against gamed signals (verified-guards.ts,
24
27
  * verified-reverify.ts): a tainted success does not train unless the project
@@ -26,7 +29,9 @@
26
29
  * it trains, and an anomaly gate holds suspicious patterns back from the
27
30
  * prompt. Every one of these can only downgrade.
28
31
  */
32
+ import { randomBytes } from 'node:crypto';
29
33
  import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
34
+ import { homedir } from 'node:os';
30
35
  import { dirname, join } from 'node:path';
31
36
  import { addOutcome, blockedTaints, emptyStats, parseStats, parseTaint, readLoopConfig } from './verified-guards.js';
32
37
  import { gitWorktreeRunner, ReverifySession } from './verified-reverify.js';
@@ -35,6 +40,8 @@ export { buildLessons, CAUTION_MIN_FAILURES, LESSON_GATE, lessonFile, lessonId,
35
40
  export const VERIFIED_PATH = join('.claude-flow', 'data', 'verified-trajectories.jsonl');
36
41
  export const LEDGER_PATH = join('.claude-flow', 'data', 'verified-ingest.json');
37
42
  export const LESSONS_PATH = join('.claude-flow', 'data', 'learned-lessons.json');
43
+ /** Under the home directory, never a project, so a cloned repository cannot carry it (#4747). */
44
+ export const ORIGIN_PATH = join('.claude-flow', 'learning-loop-origin');
38
45
  /** Ingested ids remembered; the mod keeps at most 500 lines, so this always covers the whole file. */
39
46
  export const MAX_LEDGER_IDS = 2000;
40
47
  /** A file past this many bytes is not read (the mod caps its own at far less). */
@@ -144,6 +151,31 @@ function writeAtomic(path, body) {
144
151
  writeFileSync(tmp, body);
145
152
  renameSync(tmp, path);
146
153
  }
154
+ const ORIGIN = /^[a-f0-9]{32}$/;
155
+ /** This machine's lessons origin id, created on first use; null when the home directory cannot hold it. */
156
+ export function lessonsOrigin(home = homedir()) {
157
+ const path = join(home, ORIGIN_PATH);
158
+ const read = () => {
159
+ try {
160
+ const id = readFileSync(path, 'utf8').trim();
161
+ return ORIGIN.test(id) ? id : null;
162
+ }
163
+ catch {
164
+ return null;
165
+ }
166
+ };
167
+ const known = read();
168
+ if (known)
169
+ return known;
170
+ try {
171
+ mkdirSync(dirname(path), { recursive: true });
172
+ writeFileSync(path, randomBytes(16).toString('hex'), { mode: 0o600, flag: existsSync(path) ? 'w' : 'wx' });
173
+ }
174
+ catch {
175
+ // another ingest created it first, or the home directory is read-only
176
+ }
177
+ return read();
178
+ }
147
179
  /**
148
180
  * Ingests every not-yet-ingested record. Verified records train SONA; unknown
149
181
  * ones are counted. Safe to call often: an unchanged file is one stat.
@@ -302,8 +334,10 @@ export async function ingestVerifiedOutcomes(options = {}) {
302
334
  delete ledger.patterns[k];
303
335
  writeAtomic(ledgerPath, JSON.stringify(ledger));
304
336
  const lessons = sona ? buildLessons(patterns, ledger.patterns, now) : undefined;
305
- if (lessons)
306
- writeAtomic(join(root, LESSONS_PATH), JSON.stringify(lessons, null, 2));
337
+ if (lessons) {
338
+ const origin = lessonsOrigin(options.home);
339
+ writeAtomic(join(root, LESSONS_PATH), JSON.stringify(origin ? { ...lessons, origin } : lessons, null, 2));
340
+ }
307
341
  return done(null, !!sona, lessons);
308
342
  }
309
343
  /**