@leocm/flowcraft 2.1.0 → 2.3.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 (96) hide show
  1. package/README.md +19 -2
  2. package/bin/cli.js +108 -108
  3. package/flowcraft.yaml +30 -30
  4. package/install.sh +24 -24
  5. package/lib/args.js +38 -38
  6. package/lib/config.js +20 -20
  7. package/lib/copy.js +131 -131
  8. package/lib/fix.js +110 -110
  9. package/lib/fsutil.js +104 -104
  10. package/lib/global.js +229 -229
  11. package/lib/init.js +43 -43
  12. package/lib/runtime.js +84 -84
  13. package/lib/version.js +55 -55
  14. package/lib/wizard.js +125 -125
  15. package/package.json +33 -33
  16. package/presets/backend.yaml +20 -20
  17. package/presets/default.yaml +15 -15
  18. package/presets/frontend.yaml +20 -20
  19. package/presets/quick-fix.yaml +15 -15
  20. package/setup/.claude/hooks/check-phase-test.py +152 -152
  21. package/setup/.claude/hooks/check-phase-write.py +235 -235
  22. package/setup/.claude/hooks/dispatch.py +67 -67
  23. package/setup/.claude/hooks/file-suggestion.py +45 -45
  24. package/setup/.claude/hooks/flowcraft_env.py +162 -137
  25. package/setup/.claude/hooks/permission-request.py +132 -125
  26. package/setup/.claude/hooks/post-tool-failure.py +36 -36
  27. package/setup/.claude/hooks/pre-compact.py +66 -66
  28. package/setup/.claude/hooks/review-runner.py +846 -846
  29. package/setup/.claude/hooks/setup-keys.py +282 -282
  30. package/setup/.claude/hooks/statusline.py +63 -63
  31. package/setup/.claude/hooks/stop-check.py +186 -171
  32. package/setup/.claude/hooks/subagent-start.py +50 -50
  33. package/setup/.claude/hooks/subagent-stop.py +63 -63
  34. package/setup/.claude/hooks/task-completed.py +57 -57
  35. package/setup/.claude/hooks/task_manager.py +592 -592
  36. package/setup/.claude/hooks/user-prompt-submit.py +91 -85
  37. package/setup/.claude/settings.json +233 -237
  38. package/setup/.claude/skills/Pstatus/SKILL.md +68 -68
  39. package/setup/.claude/skills/archive/SKILL.md +195 -195
  40. package/setup/.claude/skills/brainstorming/SKILL.md +5 -5
  41. package/setup/.claude/skills/brainstorming/scripts/server.cjs +723 -723
  42. package/setup/.claude/skills/brainstorming/scripts/start-server.py +245 -245
  43. package/setup/.claude/skills/brainstorming/scripts/stop-server.py +178 -178
  44. package/setup/.claude/skills/brainstorming/visual-companion.md +298 -298
  45. package/setup/.claude/skills/chrome-integration/SKILL.md +66 -66
  46. package/setup/.claude/skills/code-review/SKILL.md +264 -264
  47. package/setup/.claude/skills/executing-plans/SKILL.md +1 -1
  48. package/setup/.claude/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
  49. package/setup/.claude/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
  50. package/setup/.claude/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
  51. package/setup/.claude/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
  52. package/setup/.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
  53. package/setup/.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
  54. package/setup/.claude/skills/methodology/SKILL.md +124 -0
  55. package/setup/.claude/skills/phase/SKILL.md +62 -62
  56. package/setup/.claude/skills/playwright/SKILL.md +85 -85
  57. package/setup/.claude/skills/review/SKILL.md +97 -97
  58. package/setup/.claude/skills/task/SKILL.md +26 -26
  59. package/setup/.claude/skills/test-driven-development/SKILL.md +1 -1
  60. package/setup/.claude/skills/ui-ux-pro-max/SKILL.md +1 -1
  61. package/setup/.claude/skills/verification-before-completion/SKILL.md +1 -1
  62. package/setup/.claude/skills/writing-plans/SKILL.md +1 -1
  63. package/setup/.codex/.agents/skills/archive/SKILL.md +195 -195
  64. package/setup/.codex/.agents/skills/brainstorming/SKILL.md +5 -5
  65. package/setup/.codex/.agents/skills/brainstorming/scripts/server.cjs +723 -723
  66. package/setup/.codex/.agents/skills/brainstorming/scripts/start-server.py +245 -245
  67. package/setup/.codex/.agents/skills/brainstorming/scripts/stop-server.py +178 -178
  68. package/setup/.codex/.agents/skills/brainstorming/visual-companion.md +298 -298
  69. package/setup/.codex/.agents/skills/chrome-integration/SKILL.md +66 -66
  70. package/setup/.codex/.agents/skills/code-review/SKILL.md +264 -264
  71. package/setup/.codex/.agents/skills/executing-plans/SKILL.md +1 -1
  72. package/setup/.codex/.agents/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
  73. package/setup/.codex/.agents/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
  74. package/setup/.codex/.agents/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
  75. package/setup/.codex/.agents/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
  76. package/setup/.codex/.agents/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
  77. package/setup/.codex/.agents/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
  78. package/setup/.codex/.agents/skills/methodology/SKILL.md +124 -0
  79. package/setup/.codex/.agents/skills/phase/SKILL.md +62 -62
  80. package/setup/.codex/.agents/skills/playwright/SKILL.md +85 -85
  81. package/setup/.codex/.agents/skills/review/SKILL.md +97 -97
  82. package/setup/.codex/.agents/skills/task/SKILL.md +27 -27
  83. package/setup/.codex/.agents/skills/test-driven-development/SKILL.md +1 -1
  84. package/setup/.codex/.agents/skills/ui-ux-pro-max/SKILL.md +1 -1
  85. package/setup/.codex/.agents/skills/verification-before-completion/SKILL.md +1 -1
  86. package/setup/.codex/.agents/skills/writing-plans/SKILL.md +1 -1
  87. package/setup/.codex/hooks/review-runner.py +851 -851
  88. package/setup/.codex/hooks/setup-keys.py +287 -287
  89. package/setup/AGENTS.md +11 -1
  90. package/setup/CLAUDE.md +11 -1
  91. package/setup/install.sh +24 -24
  92. package/setup/tests/test_decision_timeout.py +241 -241
  93. package/setup/tests/test_hook_guards.py +357 -357
  94. package/setup/tests/test_sdlc_bootstrap.py +167 -167
  95. package/setup/tests/test_sessionlog_switch.py +199 -0
  96. package/setup/tests/test_task_manager.py +148 -148
@@ -1,167 +1,167 @@
1
- """Bootstrap prompt tests for PRD R1-R4.
2
-
3
- Detects non-SDLC projects (no state file) at SessionStart and emits a
4
- one-time init prompt; opt-out marker silences it; enabled projects
5
- keep the normal state-restore context.
6
- """
7
- import json
8
- import os
9
- import shutil
10
- import subprocess
11
- import sys
12
- from pathlib import Path
13
-
14
- import pytest
15
-
16
- HOOKS = Path(__file__).resolve().parents[1] / ".claude" / "hooks"
17
- CODEX_HOOK = Path(__file__).resolve().parents[1] / ".codex" / "hooks" / "sdlc-codex-hook.cjs"
18
- sys.path.insert(0, str(HOOKS))
19
-
20
- OPT_OUT = ".flowcraft.optout"
21
-
22
-
23
- def run_py_hook(name, payload, cwd, hook_dir):
24
- """Run a Python hook from a neutral copy (setup/.claude holds a template state)."""
25
- env = os.environ.copy()
26
- env["CLAUDE_PROJECT_DIR"] = str(cwd)
27
- env.pop("FLOWCRAFT_TASK_ID", None)
28
- env.pop("FLOWCRAFT_SESSION_ID", None)
29
- return subprocess.run(
30
- [sys.executable, str(hook_dir / name)],
31
- input=payload,
32
- capture_output=True,
33
- encoding="utf-8",
34
- errors="replace",
35
- cwd=str(cwd),
36
- env=env,
37
- timeout=60,
38
- )
39
-
40
-
41
- def run_codex_hook(payload, cwd, hook_copy_dir):
42
- """Run the codex hook from a neutral copy so __dirname never resolves to setup."""
43
- return subprocess.run(
44
- ["node", str(hook_copy_dir / "sdlc-codex-hook.cjs"), "session-start"],
45
- input=payload,
46
- capture_output=True,
47
- encoding="utf-8",
48
- errors="replace",
49
- cwd=str(cwd),
50
- timeout=60,
51
- )
52
-
53
-
54
- @pytest.fixture
55
- def py_hook_copy(tmp_path):
56
- """Copy session-start + its imports into a state-free dir."""
57
- d = tmp_path / "pyhookcopy"
58
- d.mkdir()
59
- for name in ("session-start.py", "flowcraft_env.py", "task_manager.py"):
60
- shutil.copy(str(HOOKS / name), str(d / name))
61
- return d
62
-
63
-
64
- @pytest.fixture
65
- def codex_hook_copy(tmp_path):
66
- """Copy the codex hook into a state-free dir (setup/.codex has a template state)."""
67
- d = tmp_path / "hookcopy"
68
- d.mkdir()
69
- shutil.copy(str(CODEX_HOOK), str(d / "sdlc-codex-hook.cjs"))
70
- return d
71
-
72
-
73
- def startup_payload(source="startup"):
74
- return json.dumps({"source": source, "session_id": "s", "hook_event_name": "SessionStart"})
75
-
76
-
77
- def make_claude_state(root, phase="P0"):
78
- (root / ".claude").mkdir(parents=True, exist_ok=True)
79
- (root / ".claude" / "project-state.md").write_text(
80
- f"current_phase: {phase}\ntask_description: \"t\"\n", encoding="utf-8"
81
- )
82
-
83
-
84
- # ---------- R1/R2: Claude session-start prompt ----------
85
-
86
- class TestSessionStartPrompt:
87
- def test_no_state_startup_prompts(self, tmp_path, py_hook_copy):
88
- p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
89
- assert p.returncode == 0
90
- out = json.loads(p.stdout)
91
- ctx = out["hookSpecificOutput"]["additionalContext"]
92
- assert "未启用" in ctx
93
-
94
- def test_optout_silences_prompt(self, tmp_path, py_hook_copy):
95
- (tmp_path / OPT_OUT).write_text("", encoding="utf-8")
96
- p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
97
- assert p.returncode == 0
98
- assert p.stdout.strip() == ""
99
-
100
- def test_resume_source_no_prompt(self, tmp_path, py_hook_copy):
101
- p = run_py_hook("session-start.py", startup_payload("resume"), tmp_path, py_hook_copy)
102
- assert p.returncode == 0
103
- assert p.stdout.strip() == ""
104
-
105
- def test_codex_state_silences_prompt(self, tmp_path, py_hook_copy):
106
- """R1: Codex 单侧管理的项目(已有 .codex 状态)不提示初始化。"""
107
- (tmp_path / ".codex").mkdir()
108
- (tmp_path / ".codex" / "project-state.md").write_text("current_phase: P0\n", encoding="utf-8")
109
- p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
110
- assert p.returncode == 0
111
- assert p.stdout.strip() == ""
112
-
113
- def test_state_present_keeps_normal_context(self, tmp_path, py_hook_copy):
114
- make_claude_state(tmp_path)
115
- p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
116
- out = json.loads(p.stdout)
117
- ctx = out["hookSpecificOutput"]["additionalContext"]
118
- assert "未启用" not in ctx
119
- assert "状态恢复" in ctx
120
-
121
-
122
- # ---------- R1/R2: Codex session-start prompt ----------
123
-
124
- def codex_payload(project_dir, source="startup"):
125
- return json.dumps({"cwd": str(project_dir), "source": source})
126
-
127
-
128
- def make_codex_state(root, phase="P0"):
129
- (root / ".codex").mkdir(parents=True, exist_ok=True)
130
- (root / ".codex" / "project-state.md").write_text(
131
- f"current_phase: {phase}\ntask_description: \"t\"\n", encoding="utf-8"
132
- )
133
-
134
-
135
- class TestCodexSessionStartPrompt:
136
- def test_no_state_prompts(self, tmp_path, codex_hook_copy):
137
- proj = tmp_path / "proj"
138
- proj.mkdir()
139
- p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
140
- assert p.returncode == 0
141
- assert "未启用" in p.stdout
142
-
143
- def test_optout_silences_prompt(self, tmp_path, codex_hook_copy):
144
- proj = tmp_path / "proj"
145
- proj.mkdir()
146
- (proj / OPT_OUT).write_text("", encoding="utf-8")
147
- p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
148
- assert p.returncode == 0
149
- assert p.stdout.strip() == ""
150
-
151
- def test_claude_state_silences_prompt(self, tmp_path, codex_hook_copy):
152
- """R1: Claude 单侧管理的项目(已有 .claude 状态)不提示初始化。"""
153
- proj = tmp_path / "proj"
154
- proj.mkdir()
155
- (proj / ".claude").mkdir()
156
- (proj / ".claude" / "project-state.md").write_text("current_phase: P0\n", encoding="utf-8")
157
- p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
158
- assert p.returncode == 0
159
- assert p.stdout.strip() == ""
160
-
161
- def test_state_present_keeps_normal_context(self, tmp_path, codex_hook_copy):
162
- proj = tmp_path / "proj"
163
- proj.mkdir()
164
- make_codex_state(proj)
165
- p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
166
- assert "未启用" not in p.stdout
167
- assert "状态恢复" in p.stdout
1
+ """Bootstrap prompt tests for PRD R1-R4.
2
+
3
+ Detects non-SDLC projects (no state file) at SessionStart and emits a
4
+ one-time init prompt; opt-out marker silences it; enabled projects
5
+ keep the normal state-restore context.
6
+ """
7
+ import json
8
+ import os
9
+ import shutil
10
+ import subprocess
11
+ import sys
12
+ from pathlib import Path
13
+
14
+ import pytest
15
+
16
+ HOOKS = Path(__file__).resolve().parents[1] / ".claude" / "hooks"
17
+ CODEX_HOOK = Path(__file__).resolve().parents[1] / ".codex" / "hooks" / "sdlc-codex-hook.cjs"
18
+ sys.path.insert(0, str(HOOKS))
19
+
20
+ OPT_OUT = ".flowcraft.optout"
21
+
22
+
23
+ def run_py_hook(name, payload, cwd, hook_dir):
24
+ """Run a Python hook from a neutral copy (setup/.claude holds a template state)."""
25
+ env = os.environ.copy()
26
+ env["CLAUDE_PROJECT_DIR"] = str(cwd)
27
+ env.pop("FLOWCRAFT_TASK_ID", None)
28
+ env.pop("FLOWCRAFT_SESSION_ID", None)
29
+ return subprocess.run(
30
+ [sys.executable, str(hook_dir / name)],
31
+ input=payload,
32
+ capture_output=True,
33
+ encoding="utf-8",
34
+ errors="replace",
35
+ cwd=str(cwd),
36
+ env=env,
37
+ timeout=60,
38
+ )
39
+
40
+
41
+ def run_codex_hook(payload, cwd, hook_copy_dir):
42
+ """Run the codex hook from a neutral copy so __dirname never resolves to setup."""
43
+ return subprocess.run(
44
+ ["node", str(hook_copy_dir / "sdlc-codex-hook.cjs"), "session-start"],
45
+ input=payload,
46
+ capture_output=True,
47
+ encoding="utf-8",
48
+ errors="replace",
49
+ cwd=str(cwd),
50
+ timeout=60,
51
+ )
52
+
53
+
54
+ @pytest.fixture
55
+ def py_hook_copy(tmp_path):
56
+ """Copy session-start + its imports into a state-free dir."""
57
+ d = tmp_path / "pyhookcopy"
58
+ d.mkdir()
59
+ for name in ("session-start.py", "flowcraft_env.py", "task_manager.py"):
60
+ shutil.copy(str(HOOKS / name), str(d / name))
61
+ return d
62
+
63
+
64
+ @pytest.fixture
65
+ def codex_hook_copy(tmp_path):
66
+ """Copy the codex hook into a state-free dir (setup/.codex has a template state)."""
67
+ d = tmp_path / "hookcopy"
68
+ d.mkdir()
69
+ shutil.copy(str(CODEX_HOOK), str(d / "sdlc-codex-hook.cjs"))
70
+ return d
71
+
72
+
73
+ def startup_payload(source="startup"):
74
+ return json.dumps({"source": source, "session_id": "s", "hook_event_name": "SessionStart"})
75
+
76
+
77
+ def make_claude_state(root, phase="P0"):
78
+ (root / ".claude").mkdir(parents=True, exist_ok=True)
79
+ (root / ".claude" / "project-state.md").write_text(
80
+ f"current_phase: {phase}\ntask_description: \"t\"\n", encoding="utf-8"
81
+ )
82
+
83
+
84
+ # ---------- R1/R2: Claude session-start prompt ----------
85
+
86
+ class TestSessionStartPrompt:
87
+ def test_no_state_startup_prompts(self, tmp_path, py_hook_copy):
88
+ p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
89
+ assert p.returncode == 0
90
+ out = json.loads(p.stdout)
91
+ ctx = out["hookSpecificOutput"]["additionalContext"]
92
+ assert "未启用" in ctx
93
+
94
+ def test_optout_silences_prompt(self, tmp_path, py_hook_copy):
95
+ (tmp_path / OPT_OUT).write_text("", encoding="utf-8")
96
+ p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
97
+ assert p.returncode == 0
98
+ assert p.stdout.strip() == ""
99
+
100
+ def test_resume_source_no_prompt(self, tmp_path, py_hook_copy):
101
+ p = run_py_hook("session-start.py", startup_payload("resume"), tmp_path, py_hook_copy)
102
+ assert p.returncode == 0
103
+ assert p.stdout.strip() == ""
104
+
105
+ def test_codex_state_silences_prompt(self, tmp_path, py_hook_copy):
106
+ """R1: Codex 单侧管理的项目(已有 .codex 状态)不提示初始化。"""
107
+ (tmp_path / ".codex").mkdir()
108
+ (tmp_path / ".codex" / "project-state.md").write_text("current_phase: P0\n", encoding="utf-8")
109
+ p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
110
+ assert p.returncode == 0
111
+ assert p.stdout.strip() == ""
112
+
113
+ def test_state_present_keeps_normal_context(self, tmp_path, py_hook_copy):
114
+ make_claude_state(tmp_path)
115
+ p = run_py_hook("session-start.py", startup_payload("startup"), tmp_path, py_hook_copy)
116
+ out = json.loads(p.stdout)
117
+ ctx = out["hookSpecificOutput"]["additionalContext"]
118
+ assert "未启用" not in ctx
119
+ assert "状态恢复" in ctx
120
+
121
+
122
+ # ---------- R1/R2: Codex session-start prompt ----------
123
+
124
+ def codex_payload(project_dir, source="startup"):
125
+ return json.dumps({"cwd": str(project_dir), "source": source})
126
+
127
+
128
+ def make_codex_state(root, phase="P0"):
129
+ (root / ".codex").mkdir(parents=True, exist_ok=True)
130
+ (root / ".codex" / "project-state.md").write_text(
131
+ f"current_phase: {phase}\ntask_description: \"t\"\n", encoding="utf-8"
132
+ )
133
+
134
+
135
+ class TestCodexSessionStartPrompt:
136
+ def test_no_state_prompts(self, tmp_path, codex_hook_copy):
137
+ proj = tmp_path / "proj"
138
+ proj.mkdir()
139
+ p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
140
+ assert p.returncode == 0
141
+ assert "未启用" in p.stdout
142
+
143
+ def test_optout_silences_prompt(self, tmp_path, codex_hook_copy):
144
+ proj = tmp_path / "proj"
145
+ proj.mkdir()
146
+ (proj / OPT_OUT).write_text("", encoding="utf-8")
147
+ p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
148
+ assert p.returncode == 0
149
+ assert p.stdout.strip() == ""
150
+
151
+ def test_claude_state_silences_prompt(self, tmp_path, codex_hook_copy):
152
+ """R1: Claude 单侧管理的项目(已有 .claude 状态)不提示初始化。"""
153
+ proj = tmp_path / "proj"
154
+ proj.mkdir()
155
+ (proj / ".claude").mkdir()
156
+ (proj / ".claude" / "project-state.md").write_text("current_phase: P0\n", encoding="utf-8")
157
+ p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
158
+ assert p.returncode == 0
159
+ assert p.stdout.strip() == ""
160
+
161
+ def test_state_present_keeps_normal_context(self, tmp_path, codex_hook_copy):
162
+ proj = tmp_path / "proj"
163
+ proj.mkdir()
164
+ make_codex_state(proj)
165
+ p = run_codex_hook(codex_payload(proj), proj, codex_hook_copy)
166
+ assert "未启用" not in p.stdout
167
+ assert "状态恢复" in p.stdout
@@ -0,0 +1,199 @@
1
+ """Session-log local switch (PRD R1) + settings template plugin cleanup (PRD R2)
2
+ + permission-request root fallback (PRD R4).
3
+
4
+ R1: flowcraft.yaml `sessionlog.local` gates local session_log.json writes;
5
+ default (key absent / unparseable) keeps the legacy always-write behaviour.
6
+ R2: the Claude settings template must not enable uninstalled plugins.
7
+ R4: permission-request.py resolves the project root via walk-up, so
8
+ subdirectory-launched sessions see the same phase as the rest of the hooks.
9
+ """
10
+ import importlib.util
11
+ import json
12
+ import os
13
+ import subprocess
14
+ import sys
15
+ from pathlib import Path
16
+
17
+ import pytest
18
+
19
+ HOOKS = Path(__file__).resolve().parents[1] / ".claude" / "hooks"
20
+ SETTINGS_TEMPLATE = HOOKS.parent / "settings.json"
21
+ sys.path.insert(0, str(HOOKS))
22
+
23
+ # Stub log_session.py: drop a marker next to .claude/ when invoked, so the
24
+ # tests observe the *call*, not the JSON payload.
25
+ STUB_LOG = (
26
+ "from pathlib import Path\n"
27
+ "Path(__file__).resolve().parent.parent.joinpath('log_called.marker').write_text('1', encoding='utf-8')\n"
28
+ )
29
+
30
+
31
+ def load_hook_module(name, filename):
32
+ """Load a hook script as a module for unit tests."""
33
+ spec = importlib.util.spec_from_file_location(name, HOOKS / filename)
34
+ mod = importlib.util.module_from_spec(spec)
35
+ spec.loader.exec_module(mod)
36
+ return mod
37
+
38
+
39
+ flowcraft_env = load_hook_module("flowcraft_env_switch_ut", "flowcraft_env.py")
40
+
41
+
42
+ def make_project(root, local=None):
43
+ """Create a minimal SDLC project; optionally configure sessionlog.local."""
44
+ (root / ".claude").mkdir(parents=True, exist_ok=True)
45
+ (root / ".claude" / "project-state.md").write_text(
46
+ 'current_phase: P2\ntask_description: "t"\n', encoding="utf-8"
47
+ )
48
+ hooks = root / ".claude" / "hooks"
49
+ hooks.mkdir(exist_ok=True)
50
+ (hooks / "log_session.py").write_text(STUB_LOG, encoding="utf-8")
51
+ if local is not None:
52
+ (root / "flowcraft.yaml").write_text(
53
+ f"sessionlog:\n local: {str(local).lower()}\n", encoding="utf-8"
54
+ )
55
+
56
+
57
+ def run_hook(name, payload, cwd):
58
+ """Run a hook script against a fake project via CLAUDE_PROJECT_DIR."""
59
+ env = os.environ.copy()
60
+ env["CLAUDE_PROJECT_DIR"] = str(cwd)
61
+ env.pop("FLOWCRAFT_TASK_ID", None)
62
+ env.pop("FLOWCRAFT_SESSION_ID", None)
63
+ return subprocess.run(
64
+ [sys.executable, str(HOOKS / name)],
65
+ input=payload,
66
+ capture_output=True,
67
+ encoding="utf-8",
68
+ errors="replace",
69
+ cwd=str(cwd),
70
+ env=env,
71
+ timeout=60,
72
+ )
73
+
74
+
75
+ def marker(root):
76
+ """Path of the stub log marker file."""
77
+ return root / ".claude" / "log_called.marker"
78
+
79
+
80
+ # ---------- R1: flowcraft.yaml config parsing ----------
81
+
82
+ class TestSessionlogLocalConfig:
83
+ def test_no_yaml_defaults_true(self, tmp_path):
84
+ assert flowcraft_env.sessionlog_local_enabled(tmp_path) is True
85
+
86
+ def test_false_disables(self, tmp_path):
87
+ (tmp_path / "flowcraft.yaml").write_text("sessionlog:\n local: false\n", encoding="utf-8")
88
+ assert flowcraft_env.sessionlog_local_enabled(tmp_path) is False
89
+
90
+ def test_true_enables(self, tmp_path):
91
+ (tmp_path / "flowcraft.yaml").write_text("sessionlog:\n local: true\n", encoding="utf-8")
92
+ assert flowcraft_env.sessionlog_local_enabled(tmp_path) is True
93
+
94
+ def test_malformed_yaml_defaults_true(self, tmp_path):
95
+ (tmp_path / "flowcraft.yaml").write_text("sessionlog: [broken\n", encoding="utf-8")
96
+ assert flowcraft_env.sessionlog_local_enabled(tmp_path) is True
97
+
98
+ def test_other_section_local_not_misread(self, tmp_path):
99
+ """审查修复: 其他配置节的缩进 local: 不得被误读为日志开关。"""
100
+ (tmp_path / "flowcraft.yaml").write_text(
101
+ "sessionlog:\n url: http://x\nother:\n local: false\n", encoding="utf-8"
102
+ )
103
+ assert flowcraft_env.sessionlog_local_enabled(tmp_path) is True
104
+
105
+
106
+ # ---------- R1: user-prompt-submit hook gating ----------
107
+
108
+ class TestUserPromptSubmitSwitch:
109
+ def test_default_writes(self, tmp_path):
110
+ make_project(tmp_path)
111
+ run_hook("user-prompt-submit.py", "hi", tmp_path)
112
+ assert marker(tmp_path).exists()
113
+
114
+ def test_local_false_skips(self, tmp_path):
115
+ make_project(tmp_path, local=False)
116
+ p = run_hook("user-prompt-submit.py", "hi", tmp_path)
117
+ assert p.returncode == 0
118
+ assert not marker(tmp_path).exists()
119
+
120
+ def test_local_true_writes(self, tmp_path):
121
+ make_project(tmp_path, local=True)
122
+ run_hook("user-prompt-submit.py", "hi", tmp_path)
123
+ assert marker(tmp_path).exists()
124
+
125
+
126
+ # ---------- R1: stop-check hook gating ----------
127
+
128
+ class TestStopCheckSwitch:
129
+ def test_default_writes(self, tmp_path):
130
+ make_project(tmp_path)
131
+ run_hook("stop-check.py", json.dumps({"last_assistant_message": "hi"}), tmp_path)
132
+ assert marker(tmp_path).exists()
133
+
134
+ def test_local_false_skips(self, tmp_path):
135
+ make_project(tmp_path, local=False)
136
+ p = run_hook("stop-check.py", json.dumps({"last_assistant_message": "hi"}), tmp_path)
137
+ assert p.returncode == 0
138
+ assert not marker(tmp_path).exists()
139
+
140
+ def test_local_true_writes(self, tmp_path):
141
+ make_project(tmp_path, local=True)
142
+ run_hook("stop-check.py", json.dumps({"last_assistant_message": "hi"}), tmp_path)
143
+ assert marker(tmp_path).exists()
144
+
145
+
146
+ # ---------- R2: settings template has no enabled plugins ----------
147
+
148
+ class TestSettingsTemplateNoPlugins:
149
+ def test_no_enabled_plugins(self):
150
+ """PRD R2: 模板不再默认启用未安装插件(消除启动期网络解析)。"""
151
+ text = SETTINGS_TEMPLATE.read_text(encoding="utf-8")
152
+ assert "enabledPlugins" not in text
153
+
154
+
155
+ # ---------- R4: permission-request root fallback ----------
156
+
157
+ def run_permission_request(cwd, payload):
158
+ """Run permission-request.py without CLAUDE_PROJECT_DIR (subdir session)."""
159
+ env = os.environ.copy()
160
+ env.pop("CLAUDE_PROJECT_DIR", None)
161
+ env.pop("FLOWCRAFT_TASK_ID", None)
162
+ env.pop("FLOWCRAFT_SESSION_ID", None)
163
+ return subprocess.run(
164
+ [sys.executable, str(HOOKS / "permission-request.py")],
165
+ input=payload,
166
+ capture_output=True,
167
+ encoding="utf-8",
168
+ errors="replace",
169
+ cwd=str(cwd),
170
+ env=env,
171
+ timeout=60,
172
+ )
173
+
174
+
175
+ def write_payload(path):
176
+ """Build a Write-tool payload for a .py target."""
177
+ return json.dumps({"tool_name": "Write", "tool_input": {"file_path": str(path)}})
178
+
179
+
180
+ class TestPermissionRequestRootFallback:
181
+ def test_subdir_session_resolves_parent_state(self, tmp_path):
182
+ """R4: 子目录启动会话经 walk-up 解析到根状态(P2)→代码写入放行。"""
183
+ root = tmp_path / "proj"
184
+ sub = root / "sub"
185
+ sub.mkdir(parents=True)
186
+ (root / ".claude").mkdir()
187
+ (root / ".claude" / "project-state.md").write_text(
188
+ 'current_phase: P2\ntask_description: "t"\n', encoding="utf-8"
189
+ )
190
+ p = run_permission_request(sub, write_payload(sub / "x.py"))
191
+ out = json.loads(p.stdout)
192
+ assert out["hookSpecificOutput"]["decision"]["behavior"] == "allow"
193
+
194
+ def test_no_state_keeps_p0_semantics(self, tmp_path):
195
+ """R4: 无状态文件项目保持 P0 语义(代码写入拒绝)。"""
196
+ p = run_permission_request(tmp_path, write_payload(tmp_path / "x.py"))
197
+ out = json.loads(p.stdout)
198
+ assert out["hookSpecificOutput"]["decision"]["behavior"] == "deny"
199
+ assert "P0" in out["hookSpecificOutput"]["decision"]["message"]