moflo 4.12.7 → 4.12.8

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.
@@ -26,9 +26,26 @@ export async function readHookStdin() {
26
26
  let done = false;
27
27
  let timer = null;
28
28
  const parse = (s) => { try { return s ? JSON.parse(s) : {}; } catch { return {}; } };
29
- const finish = () => { if (done) return; done = true; if (timer) clearTimeout(timer); res(parse(data)); };
29
+ const onData = (c) => { if (!done) data += c; };
30
+ // Detach and pause on the way out. Without this the 500ms cap is a lie for
31
+ // any caller that keeps running afterwards: the listeners stay attached and
32
+ // a flowing stdin keeps the event loop alive well past the timeout. It was
33
+ // invisible while every caller exited immediately after reading; #1441 made
34
+ // the session-start launcher the first caller with real work left to do.
35
+ const finish = () => {
36
+ if (done) return;
37
+ done = true;
38
+ if (timer) clearTimeout(timer);
39
+ try {
40
+ process.stdin.off('data', onData);
41
+ process.stdin.off('end', finish);
42
+ process.stdin.off('error', finish);
43
+ process.stdin.pause();
44
+ } catch { /* teardown must never outrank returning the payload */ }
45
+ res(parse(data));
46
+ };
30
47
  process.stdin.setEncoding('utf-8');
31
- process.stdin.on('data', (c) => { if (!done) data += c; });
48
+ process.stdin.on('data', onData);
32
49
  process.stdin.on('end', finish);
33
50
  process.stdin.on('error', finish);
34
51
  timer = setTimeout(finish, 500);
@@ -19,6 +19,7 @@ import { makeSyncer, contentEqual, syncDirRecursive } from './lib/file-sync.mjs'
19
19
  import { INTERNAL_SKILLS } from './lib/internal-skills.mjs';
20
20
  import { parseSkillCategories, computeExcludedSkills } from './lib/skill-categories.mjs';
21
21
  import { loadShippedScripts } from './lib/shipped-scripts.mjs';
22
+ import { readHookStdin } from './lib/hook-io.mjs';
22
23
  import {
23
24
  readContinuityConfig,
24
25
  readGitState,
@@ -651,7 +652,7 @@ function stopDaemon(lockFile) {
651
652
  // error 'process can only be terminated forcefully'. The prior
652
653
  // implementation invoked it anyway, swallowed the error, then polled
653
654
  // alive for 3s before escalating — exactly the time-waste that pushed
654
- // §3's stopDaemon past the 3000ms SessionStart hook timeout. Go
655
+ // §3's stopDaemon past the SessionStart hook timeout. Go
655
656
  // straight to /F /T (tree-kill, in case a worker child outlived its
656
657
  // parent) on Win.
657
658
  if (process.platform === 'win32') {
@@ -753,23 +754,155 @@ function resolveDaemonRecyclerPath() {
753
754
  }
754
755
 
755
756
  // ── 2. Reset workflow state for new session ──────────────────────────────────
756
- const stateDir = resolve(projectRoot, '.claude');
757
- const stateFile = resolve(stateDir, 'workflow-state.json');
758
- try {
759
- if (!existsSync(stateDir)) mkdirSync(stateDir, { recursive: true });
760
- writeFileSync(stateFile, JSON.stringify({
757
+ //
758
+ // #1441 reset on a NEW session only. Claude Code fires SessionStart with a
759
+ // `source` of `startup`, `resume`, `clear` or `compact`, and moflo's settings
760
+ // entry carries no matcher, so this launcher runs for all four. A compaction
761
+ // is the SAME session continuing with a shorter context; a resume is the same
762
+ // session reopened. Resetting there wiped gate state mid-run, two ways:
763
+ //
764
+ // 1. `flMode` and `sddMode` are derived ONLY from the user's prompt text
765
+ // (gate.cjs applyPromptStateReset). Clearing them turned the #952
766
+ // swarm/hive invocation gate and the #1297 SDD gate OFF for the rest of
767
+ // the run — the user's next prompt is ordinary prose, so nothing ever
768
+ // re-armed them. A `/fl -s` run that compacted stopped enforcing
769
+ // swarm_init before Agent spawns, silently.
770
+ // 2. testsRun / simplifyRun / verifyRun / learningsStored and their
771
+ // fingerprints were discarded, so a compaction forced a full re-run of
772
+ // tests, /flo-simplify and /verify before `gh pr create`.
773
+ //
774
+ // Unknown, absent, or unparseable source resets — byte-identical to the old
775
+ // behaviour on a host that doesn't send the field, and the safe direction
776
+ // (gates armed rather than silently off).
777
+ const CONTINUING_SESSION_SOURCES = new Set(['compact', 'resume']);
778
+ const KNOWN_SESSION_SOURCES = new Set(['startup', 'clear', 'compact', 'resume']);
779
+
780
+ // …but a compaction is NOT a pure "keep everything" either. The credits above
781
+ // describe work that still stands — tests ran, /verify passed, the diff is
782
+ // unchanged. The memory-search credit is the one thing compaction genuinely
783
+ // invalidates: `memorySearched` says "this actor has the search results in
784
+ // context", and after a compaction it does not. Preserving it would hand the
785
+ // post-compaction model a satisfied memory gate over a context that no longer
786
+ // holds a single result — the exact "moflo isn't being used" symptom, arrived
787
+ // at from the opposite direction.
788
+ //
789
+ // `memoryRequired` is re-armed for the same reason, and it also closes a second
790
+ // hole reported against #1441: gate.cjs derives `memoryRequired` from the user's
791
+ // prompt TEXT, and `/compact` is an 8-character non-task string, so submitting it
792
+ // set `memoryRequired: false` and the scan/read gates went quiet until some later
793
+ // prompt happened to qualify. SessionStart fires AFTER that UserPromptSubmit, so
794
+ // re-arming here corrects it — and unlike a prompt-text rule it also covers AUTO
795
+ // compaction, where no `/compact` is ever typed.
796
+ //
797
+ // Resume is untouched by this: it reloads the conversation, so a prior search is
798
+ // still in context.
799
+ //
800
+ // Forcing `memoryRequired` on is deliberate even when the pre-compaction prompt
801
+ // was genuinely trivial and scored false on its own merits. One memory search is
802
+ // cheap; a context-blind model exploring files with the gate disarmed is the
803
+ // thing this whole issue is about.
804
+ const MEMORY_CREDIT_KEYS = ['memorySearched', 'memorySearchedBy', 'memoryRequired'];
805
+
806
+ // Full shape, not the 4-field literal this used to write. gate.cjs readState()
807
+ // merges STATE_DEFAULTS over whatever it parses, so the short shape behaved
808
+ // identically THERE — but it left a half-populated file for every other reader
809
+ // of workflow-state.json, and it silently drifted from STATE_DEFAULTS each time
810
+ // a gate field was added. tests/bin/launcher-1441-compact-preserves-state.test.ts
811
+ // pins these keys to gate.cjs's STATE_DEFAULTS so they cannot drift apart again.
812
+ function freshWorkflowState() {
813
+ return {
761
814
  tasksCreated: false,
762
815
  taskCount: 0,
816
+ tasksAcknowledged: false,
763
817
  memorySearched: false,
764
- sessionStart: new Date().toISOString()
765
- }, null, 2));
766
- } catch {
767
- // Non-fatal - workflow gate will use defaults
818
+ memorySearchedBy: {},
819
+ memoryRequired: true,
820
+ learningsStored: false,
821
+ testsRun: false,
822
+ testsFingerprint: null,
823
+ simplifyRun: false,
824
+ simplifySnapshotSha: null,
825
+ simplifyFingerprint: null,
826
+ verifyRun: false,
827
+ verifyOutcome: null,
828
+ verifyFingerprint: null,
829
+ interactionCount: 0,
830
+ sessionStart: new Date().toISOString(),
831
+ lastBlockedAt: null,
832
+ lastNamespaceHint: '',
833
+ lastNamespaceHintEmittedBy: {},
834
+ flMode: null,
835
+ swarmInitialized: false,
836
+ hiveInitialized: false,
837
+ sddMode: false,
838
+ activeSddSlug: null,
839
+ };
840
+ }
841
+
842
+ // Derived from freshWorkflowState(), not a second literal: a re-armed memory
843
+ // gate is by definition the fresh-session value of those keys, and one place to
844
+ // change beats two that agree only by inspection.
845
+ function rearmedMemoryState() {
846
+ const fresh = freshWorkflowState();
847
+ return Object.fromEntries(MEMORY_CREDIT_KEYS.map((key) => [key, fresh[key]]));
848
+ }
849
+
850
+ // Bounded at 500ms by readHookStdin and short-circuited on a TTY, so a withheld
851
+ // stdin cannot eat the 5000ms SessionStart budget (hook-block-hash.ts). Read
852
+ // here rather than at the top of the file so the cost lands next to its only
853
+ // consumer.
854
+ const hookPayload = await readHookStdin();
855
+ const sessionSource = typeof hookPayload?.source === 'string' ? hookPayload.source : '';
856
+ // A source we don't recognise still resets — but say so. Falling through in
857
+ // silence is how a renamed or newly-added "continuing" source would re-open
858
+ // this bug with nothing to notice it by; the reset is only the safe default
859
+ // while the set above is accurate.
860
+ if (sessionSource && !KNOWN_SESSION_SOURCES.has(sessionSource)) {
861
+ emitWarning(
862
+ `unrecognized SessionStart source "${sessionSource}" — resetting workflow state. ` +
863
+ 'If this source continues an existing session, it belongs in §2\'s skip list (#1441).',
864
+ );
865
+ }
866
+ const stateDir = resolve(projectRoot, '.claude');
867
+ const stateFile = resolve(stateDir, 'workflow-state.json');
868
+ if (!CONTINUING_SESSION_SOURCES.has(sessionSource)) {
869
+ try {
870
+ if (!existsSync(stateDir)) mkdirSync(stateDir, { recursive: true });
871
+ writeFileSync(stateFile, JSON.stringify(freshWorkflowState(), null, 2));
872
+ } catch {
873
+ // Non-fatal - workflow gate will use defaults
874
+ }
875
+ } else if (sessionSource === 'compact') {
876
+ // Merge, never rewrite: everything the run has earned stays, only the memory
877
+ // credit is re-armed. Skipped entirely when there is no state file yet — a
878
+ // compaction before any prompt has nothing to re-arm, and writing a partial
879
+ // file here would defeat freshWorkflowState()'s shape guarantee.
880
+ //
881
+ // Read-modify-write, unsynchronised, like gate.cjs's own writeState. Safe
882
+ // here because a compaction quiesces the session: no tool hook is mid-flight
883
+ // to race with, and the next UserPromptSubmit is strictly after this hook.
884
+ // Leaving an unparseable file alone is also correct rather than merely
885
+ // tolerable — gate.cjs readState() falls back to STATE_DEFAULTS on a parse
886
+ // failure, which arms the memory gate. Repairing it here would only convert a
887
+ // fail-safe into an equivalent write.
888
+ try {
889
+ if (existsSync(stateFile)) {
890
+ const parsed = JSON.parse(readFileSync(stateFile, 'utf-8'));
891
+ writeFileSync(
892
+ stateFile,
893
+ JSON.stringify({ ...parsed, ...rearmedMemoryState() }, null, 2),
894
+ );
895
+ }
896
+ } catch (err) {
897
+ // Non-fatal, but not silent (#854): a failure here leaves the memory gate
898
+ // credited over a context that no longer holds the results.
899
+ emitWarning(`could not re-arm the memory gate after compaction (${errMessage(err)})`);
900
+ }
768
901
  }
769
902
 
770
903
  // ── 2a. Recycle daemon when behind installed version (#1054 follow-up) ──────
771
904
  // Promoted from §3a-pre to run BEFORE §3's file-sync work. The launcher has
772
- // a 3000ms SessionStart hook timeout (src/cli/services/hook-block-hash.ts);
905
+ // a 5000ms SessionStart hook timeout (src/cli/services/hook-block-hash.ts);
773
906
  // §0c (DB repair) + §3 (file-sync, manifest, cherry-pick) + stopDaemon's
774
907
  // up-to-4s graceful poll routinely exceeds it on upgrade sessions, killing
775
908
  // the launcher mid-§3. Result: §3a-pre never ran on the very sessions that
@@ -1470,7 +1603,7 @@ try {
1470
1603
 
1471
1604
  // ── 3a-pre. (removed) Daemon-version-skew recycle moved to §2a. ─────────────
1472
1605
  // The previous version of this block ran AFTER §3's heavy file-sync work,
1473
- // which routinely exceeded the 3000ms SessionStart hook timeout and was
1606
+ // which routinely exceeded the then-3000ms SessionStart hook timeout and was
1474
1607
  // killed before reaching this point. §2a now runs early and force-kills the
1475
1608
  // stale daemon before §3 can starve out. Don't restore §3a-pre — keep the
1476
1609
  // recycle in one place so the two paths can't drift.