flow-comet 1.7.0 → 1.8.0

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 (78) hide show
  1. package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -1
  2. package/.flow-comet/skills/flow-comet/SKILL.md +16 -14
  3. package/.flow-comet/skills/flow-comet/reference/commit-discipline.md +1 -1
  4. package/.flow-comet/skills/flow-comet/reference/decision-points.md +5 -5
  5. package/.flow-comet/skills/flow-comet/reference/dirty-worktree.md +1 -1
  6. package/.flow-comet/skills/flow-comet/reference/entry-detail.md +9 -9
  7. package/.flow-comet/skills/flow-comet/reference/fix-loop.md +4 -4
  8. package/.flow-comet/skills/flow-comet/reference/init-detection.md +1 -1
  9. package/.flow-comet/skills/flow-comet/reference/platform-facts.md +2 -2
  10. package/.flow-comet/skills/flow-comet/reference/post-archive-handoff.md +264 -0
  11. package/.flow-comet/skills/flow-comet/reference/recovery.md +1 -1
  12. package/.flow-comet/skills/flow-comet/reference/replan.md +1 -1
  13. package/.flow-comet/skills/flow-comet/scripts/comet-check.mjs +1 -0
  14. package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +270 -30
  15. package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +25 -8
  16. package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +41 -38
  17. package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +11 -9
  18. package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +2 -2
  19. package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +1 -1
  20. package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +112 -53
  21. package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +357 -3
  22. package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +24 -1
  23. package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +2 -2
  24. package/.flow-comet/skills/flow-comet/scripts/items/n.mjs +9 -10
  25. package/.flow-comet/skills/flow-comet/scripts/lib/ac-coverage.mjs +8 -8
  26. package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +4 -15
  27. package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +79 -107
  28. package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +183 -232
  29. package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +91 -14
  30. package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +20 -32
  31. package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +10 -19
  32. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +27 -17
  33. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +240 -87
  34. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +15 -22
  35. package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +11 -4
  36. package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +192 -24
  37. package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +55 -44
  38. package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +2 -4
  39. package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +0 -3
  40. package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +191 -17
  41. package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +3 -0
  42. package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +23 -5
  43. package/.flow-comet/skills/flow-comet/scripts/lib/wave-projection.mjs +10 -11
  44. package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +40 -3
  45. package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +12 -5
  46. package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +193 -111
  47. package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +314 -57
  48. package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +311 -216
  49. package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +33 -62
  50. package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +188 -91
  51. package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +94 -37
  52. package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +36 -27
  53. package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +285 -9
  54. package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +2 -6
  55. package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +5 -15
  56. package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +6 -5
  57. package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +40 -36
  58. package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +2 -2
  59. package/.flow-comet/skills/flow-comet/scripts/scenarios/n.mjs +346 -25
  60. package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +0 -2
  61. package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +41 -54
  62. package/.flow-comet/skills/flow-comet-archive/SKILL.md +33 -11
  63. package/.flow-comet/skills/flow-comet-change/SKILL.md +1 -1
  64. package/.flow-comet/skills/flow-comet-design/SKILL.md +5 -3
  65. package/.flow-comet/skills/flow-comet-dev/SKILL.md +2 -2
  66. package/.flow-comet/skills/flow-comet-execute/SKILL.md +11 -11
  67. package/.flow-comet/skills/flow-comet-integration/SKILL.md +10 -4
  68. package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +1 -1
  69. package/.flow-comet/skills/flow-comet-plan/SKILL.md +8 -8
  70. package/.flow-comet/skills/flow-comet-review/SKILL.md +8 -8
  71. package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +1 -1
  72. package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +14 -14
  73. package/.flow-comet/skills/flow-comet-task/SKILL.md +3 -2
  74. package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +2 -2
  75. package/.flow-comet/skills/flow-comet-verify/SKILL.md +1 -1
  76. package/README.md +2 -2
  77. package/package.json +1 -1
  78. package/scripts/dsh-bridge.mjs +238 -21
@@ -3,6 +3,7 @@
3
3
  import path from 'path';
4
4
  import { fileURLToPath } from 'url';
5
5
  import {
6
+ inspectWorkflowPathSegments,
6
7
  inspectWorkflowProtectedPath,
7
8
  parseProtocolWriteWhitelist,
8
9
  readProtocolFile,
@@ -183,7 +184,18 @@ function codexWriteTargetsFromCommand(command) {
183
184
  const addTarget = (raw) => {
184
185
  let t = raw && raw.trim();
185
186
  if (t) t = t.replace(/^["']|["']$/g, ''); // 剥引号
186
- if (t && t !== 'NUL' && t !== '/dev/null' && !targets.includes(t)) targets.push(t); // 去重
187
+ if (t) {
188
+ // 空设备不是写入目标,但"是设备"取决于执行它的 shell:
189
+ // - `nul` 只在 Windows 是空设备(大小写不敏感);POSIX shell 上 `>nul` 会真的
190
+ // 创建一个名为 nul 的文件,必须照常按写入目标判定;
191
+ // - `/dev/null` 在两类 shell 上都不是落盘目标;
192
+ // - `/dev/stdout` / `/dev/stderr` **不过滤**:它们可能被 `exec 1<>file` 之类的
193
+ // 重定向绑定到真实文件,放行等于给出一条绕过白名单的写路径。
194
+ const lower = t.toLowerCase();
195
+ const isDevice =
196
+ lower === '/dev/null' || (lower === 'nul' && process.platform === 'win32');
197
+ if (!isDevice && !targets.includes(t)) targets.push(t);
198
+ } // 去重
187
199
  };
188
200
  const patterns = [
189
201
  // PowerShell **路径型**写类 cmdlet 的 -Path/-LiteralPath/-FilePath(引号内空格整体捕获并剥引号;
@@ -529,27 +541,46 @@ function blockProtocolFileWrite(target) {
529
541
  );
530
542
  }
531
543
 
544
+ // 写目标解析失败(链接逃逸 / 硬链接共享文件对象 / realpath 报错)的统一出口:
545
+ // 按成因选报文,避免把硬链接说成"符号链接",也避免三处调用各写一份措辞。
546
+ function blockUnsafeWriteTarget(rawTarget, reason, label = '写入') {
547
+ const shared = /硬链接/.test(reason);
548
+ hookBlock(
549
+ shared
550
+ ? 'BLOCKED: ' + label + ' "' + rawTarget + '" 指向被多处共享的文件对象(' + reason + ')——拒绝'
551
+ : 'BLOCKED: ' + label + ' "' + rawTarget + '" 经符号链接/junction 指向非法落点(' + reason + ')',
552
+ shared
553
+ ? '恢复: 该路径与白名单外的文件是同一实体,请改用独立文件;确需共享时移除多余链接后重试'
554
+ : '恢复: 改用项目内的真实路径,或移除指向项目外的链接后再写入'
555
+ );
556
+ }
557
+
532
558
  // 身份在场时的最小保护集执行:候选写入目标逐个判定,命中即以既有报文风格拦截(hookBlock
533
559
  // 不返回);未命中 ⇒ 返回(调用方按子代理语义放行)。判定入口唯一——身份短路与保护集判定都在此收口。
534
560
  // 两个目标各双判据:词法同一性(规范拼写)+ 物理同一性(别名形态)。
535
561
  async function enforceMinimalProtectionSet(hookInput) {
536
562
  const stateFileAbsolute = path.join(runRoot, ...STATE_FILE_REL.split('/'));
537
563
  for (const candidate of protectedCandidateTargets(hookInput)) {
538
- const resolvedTarget = resolveWriteTarget(candidate);
564
+ const resolvedTarget = await resolveWriteTarget(candidate);
539
565
  if (resolvedTarget === null) continue;
566
+ // 身份通道不在此处拒绝链接形态(空闲放行与执行者可写任意路径的既有语义);
567
+ // 链接指向受保护文件时由下方同一性判定拦住。
568
+
540
569
  if (
541
570
  blockedProtocolFileTarget(resolvedTarget.absolute) ||
542
571
  (await isSameProtectedFileObject(resolvedTarget.absolute, protocolPath))
543
572
  ) {
544
573
  blockProtocolFileWrite(resolvedTarget.raw);
545
574
  }
546
- if (resolvedTarget.targetRel !== null) {
547
- if (
548
- blockedStateFileTarget(resolvedTarget.targetRel) ||
549
- (await isSameProtectedFileObject(resolvedTarget.absolute, stateFileAbsolute))
550
- ) {
551
- blockStateFileWrite(resolvedTarget.raw);
552
- }
575
+ // 两条判据并列而非嵌套:路径词法命中(targetRel)与物理同一性(可能经链接、也可能在项目根外——
576
+ // 根外目标的 targetRel 恒为 null,嵌套会把"根外链接指向状态文件"整条放走)。
577
+ if (
578
+ (resolvedTarget.targetRel !== null && blockedStateFileTarget(resolvedTarget.targetRel)) ||
579
+ (await isSameProtectedFileObject(resolvedTarget.absolute, stateFileAbsolute))
580
+ ) {
581
+ blockStateFileWrite(
582
+ typeof resolvedTarget.lexicalRel === 'string' ? resolvedTarget.lexicalRel : resolvedTarget.raw,
583
+ );
553
584
  }
554
585
  }
555
586
  }
@@ -696,19 +727,147 @@ function normalizeDriveRootSpelling(rawTarget) {
696
727
  // insideRunRoot=true → targetRel = 相对 runRoot 的 POSIX 路径(白名单前缀匹配 / 状态文件比较)
697
728
  // insideRunRoot=false → targetRel = null(项目根之外;由「runRoot 外写入关断」与 Bash 分支同判)
698
729
  // 无写入语义(非字符串 / 空串)→ null(该目标不参与任何判定)。
699
- function resolveWriteTarget(rawTarget) {
730
+ // '..' 段必须按文件系统顺序解析,不能交给 path.resolve 词法折叠:折叠发生在段扫描之前,
731
+ // 链接段连同它后面的 '..' 一起被提前吃掉(.specs/j/../x 折成 .specs/x,扫描器看不到 j),
732
+ // 而内核按分量走一遍是 .specs/j → src、再 '..' 回项目根。两结果不一致就是"折叠掩盖了链接"。
733
+ // 只在目标含 '..' 段时才做这次复核——其余路径的折叠与逐段解析等价,不必多付一次遍历。
734
+ async function resolveThroughSegmentsPhysically(rawTarget) {
735
+ const parts = rawTarget.split(/[\\/]+/).filter((segment) => segment !== '' && segment !== '.');
736
+ let cursor;
737
+ let start = 0;
738
+ if (path.isAbsolute(rawTarget)) {
739
+ cursor = path.parse(path.resolve(rawTarget)).root;
740
+ if (/^[A-Za-z]:$/.test(parts[0] ?? '')) start = 1;
741
+ } else {
742
+ cursor = runRoot;
743
+ }
744
+ for (let index = start; index < parts.length; index += 1) {
745
+ const segment = parts[index];
746
+ if (segment === '..') {
747
+ cursor = path.dirname(cursor);
748
+ continue;
749
+ }
750
+ const candidate = path.join(cursor, segment);
751
+ let stat = null;
752
+ try {
753
+ stat = await fs.lstat(candidate);
754
+ } catch {
755
+ stat = null; // 尚不存在(写资格允许新建):按词法形态继续
756
+ }
757
+ if (stat && stat.isSymbolicLink()) {
758
+ try {
759
+ cursor = await fs.realpath(candidate);
760
+ } catch {
761
+ cursor = candidate;
762
+ }
763
+ continue;
764
+ }
765
+ cursor = candidate;
766
+ }
767
+ return cursor;
768
+ }
769
+
770
+ async function resolveWriteTarget(rawTarget) {
700
771
  if (typeof rawTarget !== 'string') return null;
701
772
  const trimmed = rawTarget.trim();
702
773
  if (trimmed === '') return null;
703
- const absolute = path.resolve(runRoot, normalizeDriveRootSpelling(trimmed));
774
+ const normalizedTarget = normalizeDriveRootSpelling(trimmed);
775
+ const absolute = path.resolve(runRoot, normalizedTarget);
776
+ // 含 '..' 段 → 逐段物理复核(见上方注释)。折叠结果与物理解析不一致 ⇒ 拒绝,理由点名该段。
777
+ if (normalizedTarget.split(/[\\/]+/).some((segment) => segment === '..')) {
778
+ const physical = await resolveThroughSegmentsPhysically(normalizedTarget);
779
+ if (path.resolve(physical) !== path.resolve(absolute)) {
780
+ return {
781
+ raw: trimmed,
782
+ absolute,
783
+ insideRunRoot: true,
784
+ targetRel: null,
785
+ physicalRel: null,
786
+ lexicalRel: null,
787
+ exists: false,
788
+ blocked: true,
789
+ blockedReason:
790
+ '路径含 ".." 段且该段位于符号链接之后——词法折叠与逐段解析落点不同(折叠 '
791
+ + path.relative(runRoot, absolute).replaceAll('\\', '/')
792
+ + ',实际 ' + path.relative(runRoot, physical).replaceAll('\\', '/')
793
+ + ');请改用不含 ".."' + ' 的目标路径',
794
+ };
795
+ }
796
+ }
704
797
  const relative = path.relative(runRoot, absolute);
705
798
  const outside =
706
799
  path.isAbsolute(relative) || relative === '..' || relative.startsWith('..' + path.sep);
800
+ const lexicalRel = outside ? null : relative === '' ? '.' : relative.replaceAll('\\', '/');
801
+ if (outside) {
802
+ return {
803
+ raw: trimmed,
804
+ absolute,
805
+ insideRunRoot: false,
806
+ targetRel: null,
807
+ physicalRel: null,
808
+ lexicalRel: null,
809
+ exists: false,
810
+ blocked: false,
811
+ };
812
+ }
813
+ let inspection;
814
+ try {
815
+ inspection = await inspectWorkflowPathSegments(runRoot, absolute, 'write target', 'any', {
816
+ followLinks: true,
817
+ physicalRel: true,
818
+ });
819
+ } catch (error) {
820
+ return {
821
+ raw: trimmed,
822
+ absolute,
823
+ insideRunRoot: true,
824
+ targetRel: null,
825
+ physicalRel: null,
826
+ lexicalRel,
827
+ exists: false,
828
+ blocked: true,
829
+ blockedReason: error && error.message ? error.message : String(error),
830
+ };
831
+ }
832
+ // 硬链接是路径判定的盲区:它是同一文件对象的第二个名字,realpath 解析出的仍是它自己,
833
+ // 白名单前缀判不出来。目标存在且链接数 > 1 ⇒ 该路径与别处共享文件对象,写入会改到白名单之外
834
+ // 的文件——按 fail-closed 拒绝(普通新建/单链接文件 nlink === 1,不受影响)。
835
+ let hardLinkedFile = false;
836
+ let nlink = 1;
837
+ if (inspection.exists) {
838
+ try {
839
+ const targetStat = await fs.stat(absolute, { bigint: true });
840
+ nlink = Number(targetStat.nlink);
841
+ // 只对普通文件生效:POSIX 目录 nlink 恒 ≥2("." 与 ".."),把目录算进来会在
842
+ // Linux / macOS 上把「写目录目标」全判成硬链接(本机 NTFS 目录 nlink=1,看不出来)。
843
+ hardLinkedFile = targetStat.isFile() && nlink > 1;
844
+ } catch (error) {
845
+ // stat 失败时无法确认链接数 ⇒ 按 fail-closed 拒绝:硬链判据的意义就是"看不出来也要拒",
846
+ // 在拿不到 nlink 的情况下放行,等于让瞬时文件系统错误把这条判据整个关掉。
847
+ return {
848
+ raw: trimmed,
849
+ absolute,
850
+ insideRunRoot: true,
851
+ targetRel: null,
852
+ physicalRel: null,
853
+ lexicalRel,
854
+ exists: false,
855
+ blocked: true,
856
+ blockedReason:
857
+ '无法确认目标的链接数(stat 失败:' + (error && error.code ? error.code : String(error)) + ')',
858
+ };
859
+ }
860
+ }
707
861
  return {
708
862
  raw: trimmed,
709
863
  absolute,
710
- insideRunRoot: !outside,
711
- targetRel: outside ? null : relative === '' ? '.' : relative.replaceAll('\\', '/'),
864
+ insideRunRoot: true,
865
+ targetRel: inspection.physicalRel,
866
+ lexicalRel,
867
+ blocked: hardLinkedFile,
868
+ blockedReason: hardLinkedFile
869
+ ? '目标与其它路径共享同一文件对象(硬链接,链接数 ' + nlink + ')'
870
+ : undefined,
712
871
  };
713
872
  }
714
873
 
@@ -722,7 +881,7 @@ function toolFilePathFromHookInput(input) {
722
881
 
723
882
  // file_path 形态(Write / Edit / …):载荷无 file_path ⇒ null(无写入语义,不判定)。
724
883
  // 与命令级 token 共用 resolveWriteTarget——同一路径不因所选工具而异。
725
- function fileWriteTargetFromHookInput(input) {
884
+ async function fileWriteTargetFromHookInput(input) {
726
885
  return resolveWriteTarget(toolFilePathFromHookInput(input));
727
886
  }
728
887
 
@@ -788,12 +947,13 @@ async function main() {
788
947
  }
789
948
  // Phase 写入控制:按运行时状态文件的 currentNode 检查写入目标是否在白名单内
790
949
  // state 文件读取失败时不阻断(state 可能不存在),继续执行后续安全检查
791
- const stateFile = path.join(runRoot, RUNTIME_DIR, RUNTIME_STATE_FILE_NAME);
950
+ // 机器状态文件的绝对路径(读取与保护集共用本表达式——各处不得再拼一遍)
951
+ const stateFileAbsolute = path.join(runRoot, ...STATE_FILE_REL.split('/'));
792
952
  let currentNode = null;
793
953
  let executionMode = 'subagent';
794
954
  let activeChange = null;
795
955
  try {
796
- const stateContent = await fs.readFile(stateFile, 'utf8');
956
+ const stateContent = await fs.readFile(stateFileAbsolute, 'utf8');
797
957
  const state = JSON.parse(stateContent);
798
958
  currentNode = state.currentNode;
799
959
  executionMode = state.executionMode ?? 'subagent';
@@ -824,7 +984,7 @@ async function main() {
824
984
  return;
825
985
  }
826
986
 
827
- const fileTarget = fileWriteTargetFromHookInput(hookInput);
987
+ const fileTarget = await fileWriteTargetFromHookInput(hookInput);
828
988
  const target = fileTarget ? fileTarget.targetRel : null;
829
989
 
830
990
  // R5: Bash 工具写路径适配(CC 与 Codex 通用)——Bash 工具的 command 字符串(无 file_path),
@@ -833,16 +993,36 @@ async function main() {
833
993
  if (hookInput && typeof hookInput === 'object' && hookInput.tool_name === 'Bash') {
834
994
  const command = hookInput.tool_input && typeof hookInput.tool_input === 'object' ? hookInput.tool_input.command : null;
835
995
  const writeTargets = codexWriteTargetsFromCommand(command);
836
- if (writeTargets.length > 0 && currentNode) {
837
- const whitelist = PHASE_WRITE_WHITELIST[currentNode];
838
- // 声明模式未列出节点 → fail-closed(同 file_path 判定);缺省表无此节点 → 协调者默认 .specs/
839
- const effectiveWhitelist = whitelist || (declared ? null : ['.specs/']);
996
+ // 空闲态(无 state / 无 activeChange / completed)按既有语义零拦截,只保留一条例外:
997
+ // **状态文件**恒拦——否则删掉 state 文件就能重建 executionMode=direct 的状态造成持久降级。
998
+ // 协议文件、链接/硬链拒绝、隔离区放行与白名单仍只在活跃流程生效(用户裁决 2026-11-09)。
999
+ if (writeTargets.length > 0) {
840
1000
  for (const t of writeTargets) {
841
- const resolvedTarget = resolveWriteTarget(t);
1001
+ const resolvedTarget = await resolveWriteTarget(t);
842
1002
  const targetRel = resolvedTarget ? resolvedTarget.targetRel : null;
843
- if (targetRel !== null && blockedStateFileTarget(targetRel)) {
1003
+ // 状态文件的两条判据并列(路径 + 物理身份):硬链接是同一文件的第二个名字,路径前缀
1004
+ // 看不见它(与身份通道的保护集判定同一个判据,不另写一份)。
1005
+ if (
1006
+ (targetRel !== null && blockedStateFileTarget(targetRel)) ||
1007
+ (resolvedTarget && (await isSameProtectedFileObject(resolvedTarget.absolute, stateFileAbsolute)))
1008
+ ) {
844
1009
  blockStateFileWrite(t);
845
1010
  }
1011
+ // 空闲(无 state / 无 activeChange / completed):只剩 state 文件一条拦截,其余与
1012
+ // 既有 (no active workflow) 同语义——放行(用户裁决:空闲态零拦截)。
1013
+ if (!currentNode) continue;
1014
+ // 协议文件只在流程运行中拦(空闲态放行);两通道同判——同一个文件用 Bash 写
1015
+ //(printf > / Remove-Item)不得比 file_path 更宽(「同一路径不因所选工具而异」)。
1016
+ if (
1017
+ resolvedTarget &&
1018
+ (blockedProtocolFileTarget(resolvedTarget.absolute) ||
1019
+ (await isSameProtectedFileObject(resolvedTarget.absolute, protocolPath)))
1020
+ ) {
1021
+ blockProtocolFileWrite(t);
1022
+ }
1023
+ if (resolvedTarget && resolvedTarget.blocked) {
1024
+ blockUnsafeWriteTarget(t, resolvedTarget.blockedReason, '命令写入');
1025
+ }
846
1026
  // worktree 隔离区放行(与 file_path 判定共用同一判定——同路径同结论);
847
1027
  // 放行前置 = 物理包含性(词法命中但真实落点在区外 → 拒,不放行)
848
1028
  if (targetRel !== null && isInsideAgentWorktree(targetRel)) {
@@ -852,6 +1032,9 @@ async function main() {
852
1032
  '恢复: 改用隔离区内的真实路径(隔离区放行仅限物理位于 <项目根>/.claude/worktrees/ 之下的写入)'
853
1033
  );
854
1034
  }
1035
+ const whitelist = PHASE_WRITE_WHITELIST[currentNode];
1036
+ // 声明模式未列出节点 → fail-closed(同 file_path 判定);缺省表无此节点 → 协调者默认 .specs/
1037
+ const effectiveWhitelist = whitelist || (declared ? null : ['.specs/']);
855
1038
  const allowed = targetRel !== null && effectiveWhitelist !== null && targetAllowed(targetRel, effectiveWhitelist, activeChange);
856
1039
  if (!allowed) {
857
1040
  hookBlock(
@@ -865,10 +1048,39 @@ async function main() {
865
1048
  }
866
1049
  }
867
1050
 
1051
+ // ── 最小保护集(先于隔离区放行)────────────────────────────────────────────
1052
+ // worktree 的早退只该豁免**白名单**(隔离区里写源码是设计意图),不该豁免保护集:
1053
+ // 实测「.claude/worktrees/ 下指向 state / 协议文件的硬链接」在协调者通道被放行,
1054
+ // 而同一硬链放在 .specs/ 下被拒——同一目标因所在目录而异。三处判定前置到早退之前。
1055
+ // 链接/硬链的拒绝只在活跃流程生效(空闲放行是既有语义);state 文件**恒拦**——否则删掉
1056
+ // state 即可重建 permissive state(用户裁决 2026-11-09:空闲态零拦截,最多只拦 state);
1057
+ // 协议文件只在活跃流程拦。
1058
+ if (currentNode && fileTarget && fileTarget.blocked) {
1059
+ blockUnsafeWriteTarget(fileTarget.raw, fileTarget.blockedReason);
1060
+ }
1061
+ if (fileTarget) {
1062
+ if (
1063
+ (target !== null && blockedStateFileTarget(target)) ||
1064
+ (await isSameProtectedFileObject(fileTarget.absolute, stateFileAbsolute))
1065
+ ) {
1066
+ blockStateFileWrite(
1067
+ typeof fileTarget.lexicalRel === 'string' ? fileTarget.lexicalRel : fileTarget.raw,
1068
+ );
1069
+ }
1070
+ if (
1071
+ currentNode &&
1072
+ (blockedProtocolFileTarget(fileTarget.absolute) ||
1073
+ (await isSameProtectedFileObject(fileTarget.absolute, protocolPath)))
1074
+ ) {
1075
+ blockProtocolFileWrite(fileTarget.raw);
1076
+ }
1077
+ }
1078
+
868
1079
  // ── worktree 隔离区放行(CC Agent isolation:"worktree")────────────────────
869
1080
  // 判定实现见 isInsideAgentWorktree + agentWorktreeWriteStaysIsolated(与上方 Bash 命令写入
870
1081
  // 判定共用——同一路径不因工具而异);词法命中但物理落点在区外 → 拒(fail-closed)。
871
- if (isInsideAgentWorktree(target)) {
1082
+ // 空闲态不进入:隔离区放行与它的穿越拒绝都只在活跃流程生效(空闲放行语义;与 Bash 分支逐条对齐)。
1083
+ if (currentNode && isInsideAgentWorktree(target)) {
872
1084
  if (!(await agentWorktreeWriteStaysIsolated(target))) {
873
1085
  hookBlock(
874
1086
  `BLOCKED: 写入 "${target}" 位于 worktree 隔离区但物理落点在区外(符号链接/junction 穿越)`,
@@ -892,9 +1104,6 @@ async function main() {
892
1104
  }
893
1105
 
894
1106
  if (currentNode && target) {
895
- if (blockedStateFileTarget(target)) {
896
- blockStateFileWrite(target);
897
- }
898
1107
  const whitelist = PHASE_WRITE_WHITELIST[currentNode];
899
1108
  if (whitelist) {
900
1109
  const allowed = targetAllowed(target, whitelist, activeChange);
@@ -951,7 +1160,38 @@ async function main() {
951
1160
  console.log('NODE: ' + current);
952
1161
  }
953
1162
 
954
- main().catch((error) => {
955
- console.error(error.message);
956
- process.exit(1);
1163
+ main().catch(async (error) => {
1164
+ const detail = error && error.message ? error.message : String(error);
1165
+ // 兜底必须是**阻塞**而不是普通错误:宿主只把 exit 2 当拒绝,其余非零退出码会被降级成
1166
+ // 非阻断错误(实测:协议文件缺失时 exit 1 → 之后所有写入都不再被拦 = 整条防线关闭)。
1167
+ // 与既有"解析失败 / 未知状态 fail-closed"同向:无法判定就不放行。拒绝通道复用 hookBlock
1168
+ // (Codex = {"decision":"block"} + exit 0;CC = 文本 + exit 2)——否则 Codex 上仍会静默放行。
1169
+ //
1170
+ // 但**无写入语义的调用必须放行**:守卫在 main 的第一步就要读协议,读不到时连 ls / git status
1171
+ // 都被拒——而报文给出的恢复命令(prepare-env / workflow-state)本身也是 Bash 调用,会被同一条
1172
+ // 兜底再拒一次,恢复路径被自己堵死。判据与既有「未命中写入模式 = 无写入语义,放行」同源:
1173
+ // 拿不到写入目标就没有可判的对象,阻塞换不来安全性,只换来会话不可用。
1174
+ let hasWriteSemantics = false;
1175
+ try {
1176
+ const input = await readHookInput();
1177
+ if (input && typeof input === 'object') {
1178
+ if (input.tool_name === 'Bash') {
1179
+ const command = input.tool_input && typeof input.tool_input === 'object' ? input.tool_input.command : null;
1180
+ hasWriteSemantics = codexWriteTargetsFromCommand(command).length > 0;
1181
+ } else {
1182
+ hasWriteSemantics = toolFilePathFromHookInput(input) !== null;
1183
+ }
1184
+ }
1185
+ } catch {
1186
+ hasWriteSemantics = true; // 读不出来就按有写入语义处理(保守)
1187
+ }
1188
+ if (!hasWriteSemantics) {
1189
+ console.error('DEGRADED: 守卫无法完成判定(' + detail + ')——本次调用没有写入目标,放行;写入仍会被拦,直到协议/状态修好');
1190
+ hookOk('(guard degraded: no write target)');
1191
+ return;
1192
+ }
1193
+ hookBlock(
1194
+ 'BLOCKED: 守卫无法完成判定(' + detail + ')',
1195
+ '恢复: 确认本次运行使用的协议文件(技能包 reference/workflow-protocol.json,或 FLOW_COMET_PROTOCOL 指定的文件)与 .flow-comet/flow-comet-state.json 完好;必要时重跑安装器(prepare-env)修复技能副本,或用 workflow-state.mjs 修复状态。若恢复命令本身也被拒,先在 agent 会话之外的终端执行',
1196
+ );
957
1197
  });
@@ -96,7 +96,7 @@ export const ENTRIES = [
96
96
  },
97
97
 
98
98
  {
99
- name: 'A3 init 同 id 重跑:防护提示不阻断',
99
+ name: 'A3 init 同 id 重跑:防护提示不阻断;另一 change 在途时默认阻断 + --replace 留档可见',
100
100
  run: (dir) => {
101
101
  writeFile(dir, 'package.json', '{"name":"x"}');
102
102
  assertExit(runState(['init', CHANGE_ID], dir), 0);
@@ -104,6 +104,25 @@ export const ENTRIES = [
104
104
  assertExit(res, 0);
105
105
  assertOut(res, 'WARN: change ' + CHANGE_ID + ' 已存在');
106
106
  assertOut(res, '重置节点状态');
107
+ // 另一 change 在途:默认阻断(非零退出 + 点名在途 change + 两条出路),state 字节零改写
108
+ const statePath = path.join(dir, '.flow-comet', 'flow-comet-state.json');
109
+ const before = fs.readFileSync(statePath);
110
+ const blocked = runState(['init', 'next-change'], dir);
111
+ assertExit(blocked, 1);
112
+ assertOut(blocked, 'BLOCKED: 在途 change "' + CHANGE_ID + '" 尚未完成');
113
+ assertOut(blocked, '--replace ' + CHANGE_ID);
114
+ assertOut(blocked, 'workflow-state.mjs" status');
115
+ if (!fs.readFileSync(statePath).equals(before)) throw new Error('默认阻断路径改写了 state 字节');
116
+ // 显式 --replace + --reason:旧状态逐字节留档 + RECOVERY 块可见 + 新 change 正常初始化
117
+ const replaced = runState(['init', 'next-change', '--replace', CHANGE_ID, '--reason', '切换 change'], dir);
118
+ assertExit(replaced, 0);
119
+ assertOut(replaced, 'RECOVERY: 在途 change "' + CHANGE_ID + '" 的状态已留档');
120
+ const snapshotLine = replaced.output.split('\n').find((row) => row.startsWith('RECOVERY: 快照 '));
121
+ if (!snapshotLine) throw new Error('RECOVERY 块缺快照路径:\n' + replaced.output);
122
+ const snapshotPath = path.join(dir, ...snapshotLine.slice('RECOVERY: 快照 '.length).trim().split('/'));
123
+ if (!fs.readFileSync(snapshotPath).equals(before)) throw new Error('留档快照与覆盖前的 state 字节不一致');
124
+ const st = readStateFile(dir);
125
+ if (st.activeChange !== 'next-change') throw new Error('放行后新 change 未初始化: ' + JSON.stringify(st.activeChange));
107
126
  },
108
127
  },
109
128
 
@@ -841,18 +860,16 @@ export const ENTRIES = [
841
860
  assertExit(fixSectionRes, 0);
842
861
  assertNotOut(fixSectionRes, '同文件跨任务且无依赖路径');
843
862
  assertNotOut(fixSectionRes, 'BLOCKED');
844
- // 旧 change 形态(无新 change 标记):同形态仅 WARN 渐进不阻断,恢复指引仍在
863
+ // 同一形态(无任何新旧标记字段):同判 BLOCKED(单轨,无渐进放行侧),恢复指引仍在
845
864
  planTask(C1 + sameFilePairNoDeps + fixPair);
846
865
  const sameFileState = readStateFile(dir);
847
- delete sameFileState.newChange;
848
866
  writeState(dir, sameFileState);
849
867
  const sameFileWarn = exitPlanNoApply();
850
- assertExit(sameFileWarn, 0);
851
- assertOut(sameFileWarn, 'WARN');
852
- assertOut(sameFileWarn, '同文件跨任务且无依赖路径');
868
+ assertExit(sameFileWarn, 1);
869
+ assertOut(sameFileWarn, 'BLOCKED: TASK.md 同文件跨任务且无依赖路径');
853
870
  assertOut(sameFileWarn, 'X1×X2');
854
- assertNotOut(sameFileWarn, 'BLOCKED');
855
- writeState(dir, { ...sameFileState, newChange: true });
871
+ assertOut(sameFileWarn, '恢复');
872
+ writeState(dir, { ...sameFileState });
856
873
  // 恢复:按指引补显式 depends_on,且 **P02 保持写原重叠路径 bak_a.txt**
857
874
  // (不改路径避开重叠)——证明「补 depends_on 解决原重叠」而非「改路径避开重叠」;补依赖后
858
875
  // P02 跨趟(dep 未满足,不在本趟可运行集合)→ 同波次并行对只剩 P01 → 重叠消失 → 同出口通过。