@leocm/flowcraft 0.0.0-stage → 2.1.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.
- package/README.md +855 -3
- package/bin/cli.js +108 -0
- package/flowcraft.yaml +30 -0
- package/install.sh +24 -0
- package/lib/args.js +38 -0
- package/lib/config.js +20 -0
- package/lib/copy.js +131 -0
- package/lib/fix.js +110 -0
- package/lib/fsutil.js +104 -0
- package/lib/global.js +229 -0
- package/lib/init.js +43 -0
- package/lib/runtime.js +84 -0
- package/lib/version.js +55 -0
- package/lib/wizard.js +125 -0
- package/package.json +31 -4
- package/presets/backend.yaml +20 -0
- package/presets/default.yaml +15 -0
- package/presets/frontend.yaml +20 -0
- package/presets/quick-fix.yaml +15 -0
- package/setup/.claude/commands/P0.5.md +1 -0
- package/setup/.claude/commands/P0.md +1 -0
- package/setup/.claude/commands/P1.md +1 -0
- package/setup/.claude/commands/P2.md +1 -0
- package/setup/.claude/commands/P3.md +1 -0
- package/setup/.claude/commands/P4.md +1 -0
- package/setup/.claude/commands/P5.md +1 -0
- package/setup/.claude/commands/code-analysis-init.md +1 -0
- package/setup/.claude/commands/code-analysis-rebuild.md +1 -0
- package/setup/.claude/commands/code-analysis-show-memory.md +1 -0
- package/setup/.claude/commands/code-analysis-update.md +1 -0
- package/setup/.claude/hooks/check-phase-advancement.py +180 -0
- package/setup/.claude/hooks/check-phase-test.py +152 -0
- package/setup/.claude/hooks/check-phase-write.py +235 -0
- package/setup/.claude/hooks/dispatch.py +67 -0
- package/setup/.claude/hooks/file-suggestion.py +45 -0
- package/setup/.claude/hooks/flowcraft_env.py +137 -0
- package/setup/.claude/hooks/log_session.py +93 -0
- package/setup/.claude/hooks/permission-request.py +125 -0
- package/setup/.claude/hooks/post-tool-failure.py +36 -0
- package/setup/.claude/hooks/pre-compact.py +66 -0
- package/setup/.claude/hooks/review-runner.py +846 -0
- package/setup/.claude/hooks/session-end.py +184 -0
- package/setup/.claude/hooks/session-start.py +188 -0
- package/setup/.claude/hooks/settings.json +0 -0
- package/setup/.claude/hooks/setup-keys.py +282 -0
- package/setup/.claude/hooks/statusline.py +63 -0
- package/setup/.claude/hooks/stop-check.py +171 -0
- package/setup/.claude/hooks/subagent-start.py +50 -0
- package/setup/.claude/hooks/subagent-stop.py +63 -0
- package/setup/.claude/hooks/task-completed.py +57 -0
- package/setup/.claude/hooks/task_manager.py +592 -0
- package/setup/.claude/hooks/user-prompt-submit.py +85 -0
- package/setup/.claude/project-state.md +109 -0
- package/setup/.claude/settings.json +237 -0
- package/setup/.claude/settings.local.json +13 -0
- package/setup/.claude/skills/P5report/SKILL.md +77 -0
- package/setup/.claude/skills/Pstatus/SKILL.md +68 -0
- package/setup/.claude/skills/archive/SKILL.md +196 -0
- package/setup/.claude/skills/brainstorming/SKILL.md +155 -0
- package/setup/.claude/skills/brainstorming/scripts/frame-template.html +213 -0
- package/setup/.claude/skills/brainstorming/scripts/helper.js +167 -0
- package/setup/.claude/skills/brainstorming/scripts/server.cjs +723 -0
- package/setup/.claude/skills/brainstorming/scripts/start-server.py +245 -0
- package/setup/.claude/skills/brainstorming/scripts/stop-server.py +178 -0
- package/setup/.claude/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/setup/.claude/skills/brainstorming/visual-companion.md +298 -0
- package/setup/.claude/skills/checkpoint/SKILL.md +42 -0
- package/setup/.claude/skills/chrome-integration/SKILL.md +67 -0
- package/setup/.claude/skills/code-analysis/SKILL.md +1243 -0
- package/setup/.claude/skills/code-review/SKILL.md +265 -0
- package/setup/.claude/skills/cross-review/SKILL.md +41 -0
- package/setup/.claude/skills/executing-plans/SKILL.md +64 -0
- package/setup/.claude/skills/git-prep/SKILL.md +50 -0
- package/setup/.claude/skills/gitnexus/gitnexus-cli/SKILL.md +67 -0
- package/setup/.claude/skills/gitnexus/gitnexus-debugging/SKILL.md +89 -0
- package/setup/.claude/skills/gitnexus/gitnexus-exploring/SKILL.md +78 -0
- package/setup/.claude/skills/gitnexus/gitnexus-guide/SKILL.md +66 -0
- package/setup/.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +97 -0
- package/setup/.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md +121 -0
- package/setup/.claude/skills/graphify/SKILL.md +49 -0
- package/setup/.claude/skills/openspec-apply-change/SKILL.md +185 -0
- package/setup/.claude/skills/openspec-archive-change/SKILL.md +182 -0
- package/setup/.claude/skills/openspec-bulk-archive-change/SKILL.md +339 -0
- package/setup/.claude/skills/openspec-continue-change/SKILL.md +118 -0
- package/setup/.claude/skills/openspec-explore/SKILL.md +311 -0
- package/setup/.claude/skills/openspec-ff-change/SKILL.md +113 -0
- package/setup/.claude/skills/openspec-new-change/SKILL.md +77 -0
- package/setup/.claude/skills/openspec-onboard/SKILL.md +573 -0
- package/setup/.claude/skills/openspec-propose/SKILL.md +149 -0
- package/setup/.claude/skills/openspec-sync-specs/SKILL.md +262 -0
- package/setup/.claude/skills/openspec-update-change/SKILL.md +91 -0
- package/setup/.claude/skills/openspec-verify-change/SKILL.md +175 -0
- package/setup/.claude/skills/oss-version-check/SKILL.md +71 -0
- package/setup/.claude/skills/oss-version-check/check_oss_version.py +205 -0
- package/setup/.claude/skills/phase/SKILL.md +63 -0
- package/setup/.claude/skills/playwright/SKILL.md +86 -0
- package/setup/.claude/skills/playwright-cli/SKILL.md +420 -0
- package/setup/.claude/skills/playwright-cli/references/element-attributes.md +23 -0
- package/setup/.claude/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/setup/.claude/skills/playwright-cli/references/references/element-attributes.md +23 -0
- package/setup/.claude/skills/playwright-cli/references/references/playwright-tests.md +39 -0
- package/setup/.claude/skills/playwright-cli/references/references/request-mocking.md +87 -0
- package/setup/.claude/skills/playwright-cli/references/references/running-code.md +241 -0
- package/setup/.claude/skills/playwright-cli/references/references/session-management.md +225 -0
- package/setup/.claude/skills/playwright-cli/references/references/storage-state.md +275 -0
- package/setup/.claude/skills/playwright-cli/references/references/test-generation.md +433 -0
- package/setup/.claude/skills/playwright-cli/references/references/tracing.md +139 -0
- package/setup/.claude/skills/playwright-cli/references/references/video-recording.md +143 -0
- package/setup/.claude/skills/playwright-cli/references/request-mocking.md +87 -0
- package/setup/.claude/skills/playwright-cli/references/running-code.md +241 -0
- package/setup/.claude/skills/playwright-cli/references/session-management.md +225 -0
- package/setup/.claude/skills/playwright-cli/references/storage-state.md +275 -0
- package/setup/.claude/skills/playwright-cli/references/test-generation.md +433 -0
- package/setup/.claude/skills/playwright-cli/references/tracing.md +139 -0
- package/setup/.claude/skills/playwright-cli/references/video-recording.md +143 -0
- package/setup/.claude/skills/review/SKILL.md +98 -0
- package/setup/.claude/skills/review-tools/SKILL.md +76 -0
- package/setup/.claude/skills/subagent-driven-development/SKILL.md +94 -0
- package/setup/.claude/skills/task/SKILL.md +26 -0
- package/setup/.claude/skills/test-driven-development/SKILL.md +320 -0
- package/setup/.claude/skills/test-driven-development/writing-good-tests.md +198 -0
- package/setup/.claude/skills/ui-ux-pro-max/SKILL.md +377 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/charts.csv +26 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/colors.csv +97 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/icons.csv +101 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/landing.csv +31 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/products.csv +97 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/astro.csv +54 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/styles.csv +68 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/typography.csv +58 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/setup/.claude/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
- package/setup/.claude/skills/ui-ux-pro-max/scripts/core.py +253 -0
- package/setup/.claude/skills/ui-ux-pro-max/scripts/design_system.py +1067 -0
- package/setup/.claude/skills/ui-ux-pro-max/scripts/search.py +114 -0
- package/setup/.claude/skills/ui-ux-standards/SKILL.md +154 -0
- package/setup/.claude/skills/verification-before-completion/SKILL.md +120 -0
- package/setup/.claude/skills/wiki-read/SKILL.md +190 -0
- package/setup/.claude/skills/writing-plans/SKILL.md +168 -0
- package/setup/.claude/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/setup/.claude/tasks/.last-session +1 -0
- package/setup/.codex/.agents/skills/P5report/SKILL.md +77 -0
- package/setup/.codex/.agents/skills/Pstatus/SKILL.md +30 -0
- package/setup/.codex/.agents/skills/archive/SKILL.md +196 -0
- package/setup/.codex/.agents/skills/brainstorming/SKILL.md +155 -0
- package/setup/.codex/.agents/skills/brainstorming/scripts/frame-template.html +213 -0
- package/setup/.codex/.agents/skills/brainstorming/scripts/helper.js +167 -0
- package/setup/.codex/.agents/skills/brainstorming/scripts/server.cjs +723 -0
- package/setup/.codex/.agents/skills/brainstorming/scripts/start-server.py +245 -0
- package/setup/.codex/.agents/skills/brainstorming/scripts/stop-server.py +178 -0
- package/setup/.codex/.agents/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/setup/.codex/.agents/skills/brainstorming/visual-companion.md +298 -0
- package/setup/.codex/.agents/skills/checkpoint/SKILL.md +42 -0
- package/setup/.codex/.agents/skills/chrome-integration/SKILL.md +67 -0
- package/setup/.codex/.agents/skills/code-analysis/SKILL.md +1243 -0
- package/setup/.codex/.agents/skills/code-review/SKILL.md +265 -0
- package/setup/.codex/.agents/skills/cross-review/SKILL.md +42 -0
- package/setup/.codex/.agents/skills/executing-plans/SKILL.md +64 -0
- package/setup/.codex/.agents/skills/git-prep/SKILL.md +50 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-cli/SKILL.md +67 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-debugging/SKILL.md +89 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-exploring/SKILL.md +78 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-guide/SKILL.md +66 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +97 -0
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-refactoring/SKILL.md +121 -0
- package/setup/.codex/.agents/skills/graphify/SKILL.md +49 -0
- package/setup/.codex/.agents/skills/openspec-apply-change/SKILL.md +185 -0
- package/setup/.codex/.agents/skills/openspec-archive-change/SKILL.md +182 -0
- package/setup/.codex/.agents/skills/openspec-bulk-archive-change/SKILL.md +339 -0
- package/setup/.codex/.agents/skills/openspec-continue-change/SKILL.md +118 -0
- package/setup/.codex/.agents/skills/openspec-explore/SKILL.md +311 -0
- package/setup/.codex/.agents/skills/openspec-ff-change/SKILL.md +113 -0
- package/setup/.codex/.agents/skills/openspec-new-change/SKILL.md +77 -0
- package/setup/.codex/.agents/skills/openspec-onboard/SKILL.md +573 -0
- package/setup/.codex/.agents/skills/openspec-propose/SKILL.md +149 -0
- package/setup/.codex/.agents/skills/openspec-sync-specs/SKILL.md +262 -0
- package/setup/.codex/.agents/skills/openspec-update-change/SKILL.md +91 -0
- package/setup/.codex/.agents/skills/openspec-verify-change/SKILL.md +175 -0
- package/setup/.codex/.agents/skills/oss-version-check/SKILL.md +71 -0
- package/setup/.codex/.agents/skills/oss-version-check/check_oss_version.py +205 -0
- package/setup/.codex/.agents/skills/phase/SKILL.md +63 -0
- package/setup/.codex/.agents/skills/playwright/SKILL.md +86 -0
- package/setup/.codex/.agents/skills/playwright-cli/SKILL.md +420 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/element-attributes.md +23 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/element-attributes.md +23 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/playwright-tests.md +39 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/request-mocking.md +87 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/running-code.md +241 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/session-management.md +225 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/storage-state.md +275 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/test-generation.md +433 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/tracing.md +139 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/references/video-recording.md +143 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/request-mocking.md +87 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/running-code.md +241 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/session-management.md +225 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/storage-state.md +275 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/test-generation.md +433 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/tracing.md +139 -0
- package/setup/.codex/.agents/skills/playwright-cli/references/video-recording.md +143 -0
- package/setup/.codex/.agents/skills/review/SKILL.md +98 -0
- package/setup/.codex/.agents/skills/review-tools/SKILL.md +76 -0
- package/setup/.codex/.agents/skills/subagent-driven-development/SKILL.md +94 -0
- package/setup/.codex/.agents/skills/task/SKILL.md +27 -0
- package/setup/.codex/.agents/skills/test-driven-development/SKILL.md +320 -0
- package/setup/.codex/.agents/skills/test-driven-development/writing-good-tests.md +198 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/SKILL.md +377 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/charts.csv +26 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/colors.csv +97 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/icons.csv +101 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/landing.csv +31 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/products.csv +97 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/astro.csv +54 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/styles.csv +68 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/typography.csv +58 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/core.py +253 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/design_system.py +1067 -0
- package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/search.py +114 -0
- package/setup/.codex/.agents/skills/ui-ux-standards/SKILL.md +154 -0
- package/setup/.codex/.agents/skills/verification-before-completion/SKILL.md +120 -0
- package/setup/.codex/.agents/skills/wiki-read/SKILL.md +190 -0
- package/setup/.codex/.agents/skills/writing-plans/SKILL.md +168 -0
- package/setup/.codex/.agents/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/setup/.codex/config.toml +10 -0
- package/setup/.codex/hooks/log_session.py +92 -0
- package/setup/.codex/hooks/review-runner.py +851 -0
- package/setup/.codex/hooks/sdlc-codex-hook.cjs +1039 -0
- package/setup/.codex/hooks/setup-keys.py +287 -0
- package/setup/.codex/hooks.json +121 -0
- package/setup/.codex/model-catalog.json +154 -0
- package/setup/.codex/project-state.md +103 -0
- package/setup/AGENTS.md +722 -0
- package/setup/CLAUDE.md +717 -0
- package/setup/install.sh +24 -0
- package/setup/tests/test_decision_timeout.py +241 -0
- package/setup/tests/test_hook_guards.py +357 -0
- package/setup/tests/test_sdlc_bootstrap.py +167 -0
- package/setup/tests/test_task_manager.py +148 -0
|
@@ -0,0 +1,1039 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* SDLC Codex Hook(Node.js 版)
|
|
4
|
+
*
|
|
5
|
+
* 适配 Codex hooks:从 stdin 读取 Codex 事件 JSON({cwd, tool_name, tool_input, hook_event_name}),
|
|
6
|
+
* 输出 Codex 支持的决策 JSON({"decision":{...}})或上下文 JSON({"hookSpecificOutput":{...}})。
|
|
7
|
+
*
|
|
8
|
+
* 设计原则:
|
|
9
|
+
* 1. Codex 状态独立读写 .codex/project-state.md,日志复用 .codex/hooks/log_session.py
|
|
10
|
+
* 2. 所有决策通过 stdout JSON 表达,脚本始终以 0 退出,避免退出码语义在 Codex 中造成误拦截
|
|
11
|
+
* 3. hook 失败时静默放行,保证不阻断正常开发
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const { spawnSync } = require('child_process');
|
|
18
|
+
|
|
19
|
+
const EVENT = process.argv[2] || '';
|
|
20
|
+
|
|
21
|
+
/** 阶段守卫涉及的代码文件扩展名 */
|
|
22
|
+
const CODE_EXTENSIONS = new Set([
|
|
23
|
+
'java', 'js', 'ts', 'jsx', 'tsx', 'py', 'go', 'rs', 'c', 'cpp', 'h', 'cs',
|
|
24
|
+
'rb', 'php', 'swift', 'kt', 'dart', 'vue', 'svelte', 'html', 'css',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/** 文档扩展名(PermissionRequest 中允许任意阶段修改) */
|
|
28
|
+
const DOC_EXTENSIONS = new Set([
|
|
29
|
+
'md', 'txt', 'json', 'yaml', 'yml', 'toml', 'ini', 'cfg', 'conf', 'csv', 'xml', 'svg', 'log',
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
/** .claude 目录保护白名单(除白名单外禁止 Codex 修改,确保与 Claude 隔离) */
|
|
33
|
+
const CLAUDE_ALLOWED_FRAGMENTS = [
|
|
34
|
+
'/project-state.md',
|
|
35
|
+
'/session_log.json',
|
|
36
|
+
'/session_log.md',
|
|
37
|
+
'/prd.md',
|
|
38
|
+
'/reports/',
|
|
39
|
+
'/hooks/',
|
|
40
|
+
'/skills/',
|
|
41
|
+
'/memory/',
|
|
42
|
+
'/docs/',
|
|
43
|
+
'/plans/',
|
|
44
|
+
'/plan/',
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/** Git 提交/推送等仅在 P5 放行的命令前缀 */
|
|
48
|
+
const GIT_COMMIT_PATTERNS = [/^git\s+(commit|push|tag|merge)\b/i];
|
|
49
|
+
|
|
50
|
+
/** 测试命令前缀 */
|
|
51
|
+
const TEST_PATTERNS = [
|
|
52
|
+
/^npm\s+test/i, /^npx\s+(jest|vitest|mocha)\b/i, /^yarn\s+test/i,
|
|
53
|
+
/^pnpm\s+test/i, /^pytest/i, /^go\s+test/i, /^cargo\s+test/i,
|
|
54
|
+
/^mvn\s+test/i, /^mvnd\s+test/i, /^jest\s/i, /^vitest\s/i, /^mocha\s/i,
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/** Lint/构建命令(任意阶段放行) */
|
|
58
|
+
const BUILD_PATTERNS = [
|
|
59
|
+
/^npx\s+eslint/i, /^npx\s+tsc/i, /^npm\s+run\s+lint/i, /^npm\s+run\s+build/i,
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
/** 危险命令(交给 Codex 自身的权限确认,不自动放行也不自动拦截) */
|
|
63
|
+
const DANGEROUS_PATTERNS = [/\brm\s+-rf\b/, /--force/, /\breset\s+--hard\b/, /\bbranch\s+-D\b/, /push\s+--force/];
|
|
64
|
+
|
|
65
|
+
/** 危险操作关键词(R2):命中则决策点不启用超时 */
|
|
66
|
+
const DANGER_KEYWORDS = ['删除', '推送', '合并', 'rebase', 'reset', 'force', '回退'];
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 读取 stdin 内容并尝试解析为 JSON
|
|
70
|
+
*
|
|
71
|
+
* @returns {{raw: string, parsed?: object}} 原始文本与解析结果
|
|
72
|
+
*/
|
|
73
|
+
function readStdin() {
|
|
74
|
+
try {
|
|
75
|
+
const data = fs.readFileSync(0, 'utf-8');
|
|
76
|
+
if (!data || !data.trim()) {
|
|
77
|
+
return { raw: '' };
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
return { raw: data.trim(), parsed: JSON.parse(data) };
|
|
81
|
+
} catch {
|
|
82
|
+
return { raw: data.trim() };
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
return { raw: '' };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 定位项目根目录(从输入 cwd 向上查找 .codex/project-state.md)
|
|
91
|
+
*
|
|
92
|
+
* @param {object} input Codex hook 输入
|
|
93
|
+
* @returns {string} 项目根目录绝对路径
|
|
94
|
+
*/
|
|
95
|
+
function findProjectRoot(input) {
|
|
96
|
+
// 优先从 hook 脚本自身位置推断项目根:
|
|
97
|
+
// 脚本位于 <项目根>/.codex/hooks/ 下,不依赖 hook 执行时的 cwd。
|
|
98
|
+
const candidates = [__dirname, (input && input.cwd) || '', process.cwd()].filter(Boolean);
|
|
99
|
+
const visited = new Set();
|
|
100
|
+
const homeDir = path.resolve(os.homedir());
|
|
101
|
+
|
|
102
|
+
for (const base of candidates) {
|
|
103
|
+
let dir = path.resolve(base);
|
|
104
|
+
for (let i = 0; i < 8; i++) {
|
|
105
|
+
if (visited.has(dir)) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
visited.add(dir);
|
|
109
|
+
if (fs.existsSync(path.join(dir, '.codex', 'project-state.md'))) {
|
|
110
|
+
// R3: 用户主目录不作为项目根(除非 cwd 就在主目录)——防止全局
|
|
111
|
+
// 安装的 hooks 把 ~/.codex 状态套到所有项目上
|
|
112
|
+
const isHome = path.resolve(dir) === homeDir;
|
|
113
|
+
const isActiveCwd =
|
|
114
|
+
path.resolve(dir) === path.resolve((input && input.cwd) || '') ||
|
|
115
|
+
path.resolve(dir) === path.resolve(process.cwd());
|
|
116
|
+
if (!isHome || isActiveCwd) {
|
|
117
|
+
return dir;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const parent = path.dirname(dir);
|
|
121
|
+
if (parent === dir) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
dir = parent;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return path.resolve((input && input.cwd) || process.cwd());
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 读取项目状态文件内容
|
|
132
|
+
*
|
|
133
|
+
* @param {string} root 项目根目录
|
|
134
|
+
* @returns {string} 状态文件文本,不存在返回空字符串
|
|
135
|
+
*/
|
|
136
|
+
function readStateFile(root) {
|
|
137
|
+
const stateFile = path.join(root, '.codex', 'project-state.md');
|
|
138
|
+
try {
|
|
139
|
+
return fs.readFileSync(stateFile, 'utf-8');
|
|
140
|
+
} catch {
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 提取当前阶段编号(如 P2 -> "2")
|
|
147
|
+
*
|
|
148
|
+
* @param {string} stateText 状态文件文本
|
|
149
|
+
* @returns {string} 阶段编号
|
|
150
|
+
*/
|
|
151
|
+
function getCurrentPhase(stateText) {
|
|
152
|
+
const match = stateText.match(/^current_phase:\s*P?([0-9.]+)/m);
|
|
153
|
+
return match ? match[1] : '0';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 提取状态文件中的简单字段值(去除引号与行尾注释)
|
|
158
|
+
*
|
|
159
|
+
* @param {string} stateText 状态文件文本
|
|
160
|
+
* @param {string} field 字段名,如 task_description
|
|
161
|
+
* @returns {string} 字段值
|
|
162
|
+
*/
|
|
163
|
+
function getStateField(stateText, field) {
|
|
164
|
+
const match = stateText.match(new RegExp(`^${field}:\\s*"?(.+?)"?\\s*$`, 'm'));
|
|
165
|
+
if (!match) {
|
|
166
|
+
return '';
|
|
167
|
+
}
|
|
168
|
+
return match[1].replace(/\s*#.*$/, '').replace(/^"+|"+$/g, '').trim();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 统计 modified_files 列表中的文件数量
|
|
173
|
+
*
|
|
174
|
+
* @param {string} stateText 状态文件文本
|
|
175
|
+
* @returns {number} 文件数量
|
|
176
|
+
*/
|
|
177
|
+
function countModifiedFiles(stateText) {
|
|
178
|
+
const lines = stateText.split('\n');
|
|
179
|
+
let inFiles = false;
|
|
180
|
+
let count = 0;
|
|
181
|
+
for (const line of lines) {
|
|
182
|
+
if (/^modified_files:/.test(line)) {
|
|
183
|
+
inFiles = true;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (inFiles) {
|
|
187
|
+
if (line && !/^\s/.test(line) && line.includes(':')) {
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
if (/^\s*-\s+/.test(line)) {
|
|
191
|
+
count += 1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return count;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 读取 flowcraft.yaml 的 decision_timeout(R3),缺失默认 120 秒
|
|
200
|
+
*
|
|
201
|
+
* @param {string} root 项目根目录
|
|
202
|
+
* @returns {number} 超时秒数
|
|
203
|
+
*/
|
|
204
|
+
function readDecisionTimeout(root) {
|
|
205
|
+
try {
|
|
206
|
+
const cfg = fs.readFileSync(path.join(root, 'flowcraft.yaml'), 'utf-8');
|
|
207
|
+
const m = cfg.match(/^decision_timeout:\s*(\d+)/m);
|
|
208
|
+
if (m) {
|
|
209
|
+
return parseInt(m[1], 10);
|
|
210
|
+
}
|
|
211
|
+
} catch {
|
|
212
|
+
// 缺失或不可读 → 默认 120
|
|
213
|
+
}
|
|
214
|
+
return 120;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 从 AI 回复中解析决策卡标题与推荐项(R4)
|
|
219
|
+
*
|
|
220
|
+
* @param {string} text AI 回复文本
|
|
221
|
+
* @returns {{point: string, recommended: string}} 决策点名称与推荐字母,无卡或解析不出推荐项时为空
|
|
222
|
+
*/
|
|
223
|
+
function parseDecisionCard(text) {
|
|
224
|
+
if (!text || !text.includes('🧭') || !text.includes('请选择')) {
|
|
225
|
+
return { point: '', recommended: '' };
|
|
226
|
+
}
|
|
227
|
+
let point = '决策点';
|
|
228
|
+
const pm = text.match(/🧭\s*决策点[^\n||]*[||]\s*\*{0,2}([^*\n]+)\*{0,2}/);
|
|
229
|
+
if (pm) {
|
|
230
|
+
point = pm[1].trim();
|
|
231
|
+
}
|
|
232
|
+
const rm = text.match(/✅[^\n]*?·\s*([A-Z])\s*[..、]/);
|
|
233
|
+
return { point, recommended: rm ? rm[1] : '' };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 更新待决决策标记(R4/R5):决策卡回合写入,非决策卡回合清除
|
|
238
|
+
*
|
|
239
|
+
* @param {string} root 项目根目录
|
|
240
|
+
* @param {string} aiMessage AI 回复文本
|
|
241
|
+
*/
|
|
242
|
+
function updateDecisionMarker(root, aiMessage) {
|
|
243
|
+
const markerPath = path.join(root, '.codex', 'decision-pending.json');
|
|
244
|
+
const card = parseDecisionCard(aiMessage);
|
|
245
|
+
if (card.point && card.recommended && !DANGER_KEYWORDS.some((k) => card.point.includes(k))) {
|
|
246
|
+
const timeout = readDecisionTimeout(root);
|
|
247
|
+
try {
|
|
248
|
+
fs.writeFileSync(
|
|
249
|
+
markerPath,
|
|
250
|
+
JSON.stringify({
|
|
251
|
+
point: card.point,
|
|
252
|
+
recommended: card.recommended,
|
|
253
|
+
deadline: Math.floor(Date.now() / 1000) + timeout,
|
|
254
|
+
timeout,
|
|
255
|
+
session_id: '',
|
|
256
|
+
}),
|
|
257
|
+
'utf-8'
|
|
258
|
+
);
|
|
259
|
+
} catch {
|
|
260
|
+
// 写失败静默,不影响主流程
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
try {
|
|
264
|
+
if (fs.existsSync(markerPath)) {
|
|
265
|
+
fs.unlinkSync(markerPath);
|
|
266
|
+
}
|
|
267
|
+
} catch {
|
|
268
|
+
// 忽略
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 清除待决决策标记(R7:任意用户输入即视为已响应)
|
|
275
|
+
*
|
|
276
|
+
* @param {string} root 项目根目录
|
|
277
|
+
*/
|
|
278
|
+
function clearDecisionMarker(root) {
|
|
279
|
+
const markerPath = path.join(root, '.codex', 'decision-pending.json');
|
|
280
|
+
try {
|
|
281
|
+
if (fs.existsSync(markerPath)) {
|
|
282
|
+
fs.unlinkSync(markerPath);
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
// 忽略
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 过期待决决策的懒检查上下文(R5 兜底通道)
|
|
291
|
+
*
|
|
292
|
+
* @param {string} root 项目根目录
|
|
293
|
+
* @returns {string} 过期注入文本,未过期或无标记返回空
|
|
294
|
+
*/
|
|
295
|
+
function expiredDecisionContext(root) {
|
|
296
|
+
try {
|
|
297
|
+
const pending = JSON.parse(fs.readFileSync(path.join(root, '.codex', 'decision-pending.json'), 'utf-8'));
|
|
298
|
+
if (pending.deadline && Date.now() / 1000 > Number(pending.deadline)) {
|
|
299
|
+
return (
|
|
300
|
+
` ⏱ 决策点「${pending.point || ''}」已超时 ${pending.timeout || 120}s 且无用户选择:` +
|
|
301
|
+
`请先按推荐项 ${pending.recommended || ''} 自动执行并标注可回退,再处理新输入。`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
} catch {
|
|
305
|
+
// 无标记或损坏 → 跳过
|
|
306
|
+
}
|
|
307
|
+
return '';
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 输出 Codex 决策 JSON(拒绝时携带提示消息)
|
|
312
|
+
*
|
|
313
|
+
* @param {string} behavior allow/deny
|
|
314
|
+
* @param {string} message 提示消息
|
|
315
|
+
*/
|
|
316
|
+
function emitDecision(behavior, message) {
|
|
317
|
+
process.stdout.write(JSON.stringify({ decision: { behavior, message } }));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 输出 Codex 上下文注入 JSON
|
|
322
|
+
*
|
|
323
|
+
* @param {string} eventName hook 事件名
|
|
324
|
+
* @param {string} text 注入给模型的上下文
|
|
325
|
+
*/
|
|
326
|
+
function emitContext(eventName, text) {
|
|
327
|
+
process.stdout.write(
|
|
328
|
+
JSON.stringify({
|
|
329
|
+
hookSpecificOutput: { hookEventName: eventName, additionalContext: text },
|
|
330
|
+
})
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* 调用 .codex/hooks/log_session.py 记录会话日志(失败静默)
|
|
336
|
+
*
|
|
337
|
+
* @param {string} root 项目根目录
|
|
338
|
+
* @param {string} role user/assistant
|
|
339
|
+
* @param {string} content 日志内容
|
|
340
|
+
*/
|
|
341
|
+
/** 追加日志文件(失败静默,不影响主流程) */
|
|
342
|
+
function appendHookLog(logPath, message) {
|
|
343
|
+
try {
|
|
344
|
+
fs.appendFileSync(logPath, `[${new Date().toISOString()}] ${message}\n`);
|
|
345
|
+
} catch {
|
|
346
|
+
// 忽略
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** 写 hook 诊断日志(失败静默) */
|
|
351
|
+
function logDiagnostic(root, eventName, message) {
|
|
352
|
+
appendHookLog(path.join(root, '.codex', 'hooks', 'hook-errors.log'), `${eventName} ${message}`);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** 从多个候选字段中取第一个非空字符串 */
|
|
356
|
+
function pickString(input, fieldNames) {
|
|
357
|
+
if (!input || typeof input !== 'object') {
|
|
358
|
+
return '';
|
|
359
|
+
}
|
|
360
|
+
for (const name of fieldNames) {
|
|
361
|
+
const v = input[name];
|
|
362
|
+
if (typeof v === 'string' && v.trim()) {
|
|
363
|
+
return v;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return '';
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** 从事件输入中提取用户问题(兼容桌面端/CLI 多种字段) */
|
|
370
|
+
function extractUserPrompt(input) {
|
|
371
|
+
if (!input || typeof input !== 'object') {
|
|
372
|
+
return '';
|
|
373
|
+
}
|
|
374
|
+
const direct = pickString(input, ['prompt', 'message', 'input', 'content', 'text']);
|
|
375
|
+
if (direct) {
|
|
376
|
+
return direct;
|
|
377
|
+
}
|
|
378
|
+
// raw 仅在非 JSON 形态时视为 prompt(readStdin 解析失败时 raw 为整段原始 JSON,须排除)
|
|
379
|
+
const raw = input.raw;
|
|
380
|
+
if (typeof raw === 'string' && raw.trim() && !/^[\[\{]/.test(raw.trim())) {
|
|
381
|
+
return raw;
|
|
382
|
+
}
|
|
383
|
+
const ti = input.tool_input;
|
|
384
|
+
if (ti && typeof ti === 'object') {
|
|
385
|
+
return pickString(ti, ['prompt', 'message', 'input', 'content', 'text']);
|
|
386
|
+
}
|
|
387
|
+
if (typeof ti === 'string' && ti.trim()) {
|
|
388
|
+
return ti;
|
|
389
|
+
}
|
|
390
|
+
return '';
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** 从事件输入中提取 AI 回复(兼容多种字段) */
|
|
394
|
+
function extractAssistantMessage(input) {
|
|
395
|
+
return pickString(input, ['last_assistant_message', 'assistant_message', 'message', 'content', 'text']);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function logSession(root, role, content) {
|
|
399
|
+
const logScript = path.join(root, '.codex', 'hooks', 'log_session.py');
|
|
400
|
+
if (!fs.existsSync(logScript) || !content) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
let ok = false;
|
|
404
|
+
try {
|
|
405
|
+
const tempFile = path.join(
|
|
406
|
+
os.tmpdir(),
|
|
407
|
+
`sdlc-hook-${Date.now()}-${Math.random().toString(16).slice(2)}.txt`
|
|
408
|
+
);
|
|
409
|
+
fs.writeFileSync(tempFile, content, 'utf-8');
|
|
410
|
+
|
|
411
|
+
let gitUser = 'unknown';
|
|
412
|
+
try {
|
|
413
|
+
gitUser = spawnSync('git', ['config', 'user.name'], { encoding: 'utf-8', timeout: 3000 })
|
|
414
|
+
.stdout.trim() || 'unknown';
|
|
415
|
+
} catch {
|
|
416
|
+
gitUser = 'unknown';
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const env = {
|
|
420
|
+
...process.env,
|
|
421
|
+
SESSION_LOG: path.join(root, '.codex', 'session_log.json'),
|
|
422
|
+
GIT_USER: gitUser,
|
|
423
|
+
TEMP_INPUT: tempFile,
|
|
424
|
+
};
|
|
425
|
+
// 跨平台探测 Python 命令:Linux/macOS 常用 python3,Windows 常用 python
|
|
426
|
+
let pyResult = null;
|
|
427
|
+
for (const py of ['python3', 'python']) {
|
|
428
|
+
try {
|
|
429
|
+
pyResult = spawnSync(py, [logScript, role], { encoding: 'utf-8', timeout: 5000, env });
|
|
430
|
+
// Windows 上 python3 可能不存在:spawnSync 不报 error,但退出码为 9009,
|
|
431
|
+
// 此时必须继续尝试 python,不能误判为成功。
|
|
432
|
+
if (!pyResult.error && pyResult.status === 0) {
|
|
433
|
+
ok = true;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
} catch {
|
|
437
|
+
// 当前命令不可用,尝试下一个
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
fs.unlinkSync(tempFile);
|
|
442
|
+
} catch {
|
|
443
|
+
// 临时文件清理失败不影响流程
|
|
444
|
+
}
|
|
445
|
+
} catch (err) {
|
|
446
|
+
logDiagnostic(root, 'logSession', `记录失败 role=${role}: ${(err && err.message) || err}`);
|
|
447
|
+
}
|
|
448
|
+
if (!ok) {
|
|
449
|
+
logDiagnostic(root, 'logSession', `python 脚本未成功执行 role=${role}(python3/python 均失败或退出码非 0)`);
|
|
450
|
+
}
|
|
451
|
+
return ok;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* 从 Codex apply_patch 的 tool_input 中解析目标文件路径
|
|
456
|
+
*
|
|
457
|
+
* @param {object} input Codex hook 输入
|
|
458
|
+
* @returns {string[]} 目标文件路径列表
|
|
459
|
+
*/
|
|
460
|
+
function extractPatchFiles(input) {
|
|
461
|
+
const toolInput = (input && input.tool_input) || {};
|
|
462
|
+
const patchText =
|
|
463
|
+
typeof toolInput === 'string'
|
|
464
|
+
? toolInput
|
|
465
|
+
: toolInput.patch || toolInput.input || toolInput.content || '';
|
|
466
|
+
if (!patchText) {
|
|
467
|
+
return [];
|
|
468
|
+
}
|
|
469
|
+
const files = [];
|
|
470
|
+
const regex = /^\*\*\* (?:Update File|Add File|Delete File|Move to):\s*(.+)$/gm;
|
|
471
|
+
let match = regex.exec(patchText);
|
|
472
|
+
while (match) {
|
|
473
|
+
files.push(match[1].trim().replace(/\\/g, '/'));
|
|
474
|
+
match = regex.exec(patchText);
|
|
475
|
+
}
|
|
476
|
+
return files;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* 从 shell_command 的 tool_input 中提取命令文本
|
|
481
|
+
*
|
|
482
|
+
* @param {object} input Codex hook 输入
|
|
483
|
+
* @returns {string} 命令文本
|
|
484
|
+
*/
|
|
485
|
+
function extractCommand(input) {
|
|
486
|
+
const toolInput = (input && input.tool_input) || {};
|
|
487
|
+
if (typeof toolInput === 'string') {
|
|
488
|
+
return toolInput;
|
|
489
|
+
}
|
|
490
|
+
return toolInput.command || toolInput.shell || toolInput.cmd || '';
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* 规范化文件路径(正斜杠、去掉 ./ 前缀、转小写用于匹配)
|
|
495
|
+
*
|
|
496
|
+
* @param {string} filePath 原始路径
|
|
497
|
+
* @returns {string} 规范化路径
|
|
498
|
+
*/
|
|
499
|
+
function normalizePath(filePath) {
|
|
500
|
+
return String(filePath || '').replace(/\\/g, '/').replace(/^\.\//, '').replace(/^\/+/, '');
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* 判断文件扩展名
|
|
505
|
+
*
|
|
506
|
+
* @param {string} filePath 文件路径
|
|
507
|
+
* @returns {string} 小写扩展名,无扩展名返回空字符串
|
|
508
|
+
*/
|
|
509
|
+
function getExtension(filePath) {
|
|
510
|
+
const match = String(filePath || '').match(/\.([^./\\]+)$/);
|
|
511
|
+
return match ? match[1].toLowerCase() : '';
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* 判断目标路径是否位于 root 之下(R1: 守卫只管项目内文件)
|
|
516
|
+
*
|
|
517
|
+
* @param {string} filePath 规范化路径
|
|
518
|
+
* @param {string} root 项目根目录
|
|
519
|
+
* @returns {boolean} 是否位于 root 之下
|
|
520
|
+
*/
|
|
521
|
+
function isUnderRoot(filePath, root) {
|
|
522
|
+
const target = path.resolve(filePath).toLowerCase().replace(/\\/g, '/');
|
|
523
|
+
const base = path.resolve(root).toLowerCase().replace(/\\/g, '/');
|
|
524
|
+
return target === base || target.startsWith(base + '/');
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* 判断是否为受保护的 .claude 文件(非白名单路径;仅项目内 .claude 受管)
|
|
529
|
+
*
|
|
530
|
+
* @param {string} normalized 规范化路径
|
|
531
|
+
* @param {string} root 项目根目录
|
|
532
|
+
* @returns {boolean} 是否为受保护框架文件
|
|
533
|
+
*/
|
|
534
|
+
function isProtectedClaudeFile(normalized, root) {
|
|
535
|
+
if (!normalized.includes('/.claude/') && !normalized.startsWith('.claude/')) {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
// R1: 用户级 ~/.claude(如 Claude Code plans 草稿)不归守卫管
|
|
539
|
+
if (root && !isUnderRoot(normalized, root)) {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
return !CLAUDE_ALLOWED_FRAGMENTS.some((fragment) => normalized.includes(fragment));
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* check-phase-write:apply_patch 阶段守卫
|
|
547
|
+
*
|
|
548
|
+
* @param {object} input Codex hook 输入
|
|
549
|
+
* @param {string} root 项目根目录
|
|
550
|
+
* @param {string} stateText 状态文件文本
|
|
551
|
+
*/
|
|
552
|
+
function checkPhaseWrite(input, root, stateText) {
|
|
553
|
+
// 非受管项目(无状态文件)不设守卫,避免全局 hooks 误拦截(与 Claude 侧行为一致)
|
|
554
|
+
if (!stateText || !stateText.trim()) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const phase = getCurrentPhase(stateText);
|
|
558
|
+
const files = extractPatchFiles(input);
|
|
559
|
+
if (files.length === 0) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
for (const rawFile of files) {
|
|
564
|
+
const normalized = normalizePath(rawFile);
|
|
565
|
+
if (normalized.includes('flowcraft/')) {
|
|
566
|
+
continue; // 框架自身文件允许修改
|
|
567
|
+
}
|
|
568
|
+
if (isProtectedClaudeFile(normalized, root)) {
|
|
569
|
+
emitDecision('deny', `【SDLC 守卫】禁止修改 SDLC 框架文件: ${rawFile},如需修改请手动编辑。`);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const ext = getExtension(normalized);
|
|
574
|
+
const isCode = CODE_EXTENSIONS.has(ext);
|
|
575
|
+
const isPlanFile = normalized.includes('/plan/') || normalized.startsWith('plan/');
|
|
576
|
+
const isPrdFile = normalized.includes('/.codex/prd.md') || normalized === '.codex/prd.md';
|
|
577
|
+
|
|
578
|
+
if ((phase === '0' || phase === '0.5') && isCode) {
|
|
579
|
+
emitDecision('deny', `【SDLC 守卫】${phase === '0' ? 'P0' : 'P0.5'} 阶段禁止修改代码文件: ${rawFile}`);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (phase === '1' && isCode && !isPlanFile && !isPrdFile) {
|
|
583
|
+
emitDecision('deny', `【SDLC 守卫】P1 阶段禁止修改代码文件: ${rawFile},只允许 PRD 和 plan 文件。`);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (phase === '5' && isCode) {
|
|
587
|
+
emitDecision('deny', `【SDLC 守卫】P5 交付阶段,代码已冻结: ${rawFile}`);
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* 检查指定阶段 P{n} 的 deliverable_checklist 产出物是否齐全
|
|
595
|
+
*
|
|
596
|
+
* @param {string} stateText 状态文件文本
|
|
597
|
+
* @param {string} phase 阶段编号
|
|
598
|
+
* @param {string} root 项目根目录
|
|
599
|
+
* @returns {string[]} 缺失文件列表
|
|
600
|
+
*/
|
|
601
|
+
function checkDeliverables(stateText, phase, root) {
|
|
602
|
+
const sectionMatch = stateText.match(new RegExp(`P${phase}:\\s*\\n((?:\\s+-\\s+"[^"]+"\\n?)*)`));
|
|
603
|
+
if (!sectionMatch || !sectionMatch[1].trim()) {
|
|
604
|
+
return [];
|
|
605
|
+
}
|
|
606
|
+
const missing = [];
|
|
607
|
+
const lineRegex = /^\s*-\s+"([^"]+)"/gm;
|
|
608
|
+
let match = lineRegex.exec(sectionMatch[1]);
|
|
609
|
+
while (match) {
|
|
610
|
+
const target = path.join(root, match[1]);
|
|
611
|
+
if (!fs.existsSync(target)) {
|
|
612
|
+
missing.push(match[1]);
|
|
613
|
+
}
|
|
614
|
+
match = lineRegex.exec(sectionMatch[1]);
|
|
615
|
+
}
|
|
616
|
+
return missing;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* check-phase-advancement:阶段推进顺序与产出物校验
|
|
621
|
+
*
|
|
622
|
+
* @param {object} input Codex hook 输入
|
|
623
|
+
* @param {string} root 项目根目录
|
|
624
|
+
* @param {string} stateText 状态文件文本
|
|
625
|
+
*/
|
|
626
|
+
function checkPhaseAdvancement(input, root, stateText) {
|
|
627
|
+
const files = extractPatchFiles(input);
|
|
628
|
+
const touchesState = files.some((file) => normalizePath(file).endsWith('.codex/project-state.md'));
|
|
629
|
+
if (!touchesState) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const patchText =
|
|
634
|
+
typeof input.tool_input === 'string'
|
|
635
|
+
? input.tool_input
|
|
636
|
+
: (input.tool_input && (input.tool_input.patch || input.tool_input.input || '')) || '';
|
|
637
|
+
const newPhaseMatch = patchText.match(/^\+current_phase:[ \t]*P?([0-9.]+)/m);
|
|
638
|
+
if (!newPhaseMatch) {
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const oldPhase = getCurrentPhase(stateText);
|
|
643
|
+
const newPhase = newPhaseMatch[1];
|
|
644
|
+
if (oldPhase === newPhase) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (oldPhase === '2' && newPhase !== '3') {
|
|
649
|
+
emitDecision('deny', '【SDLC 违规】P2(编码阶段)完成后必须推进到 P3(测试阶段),不能跳过。');
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
if (oldPhase === '3' && newPhase !== '4') {
|
|
653
|
+
emitDecision('deny', '【SDLC 违规】P3(测试阶段)完成后必须推进到 P4(审查阶段),不能跳过。');
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if (oldPhase === '4' && newPhase !== '5') {
|
|
657
|
+
emitDecision('deny', '【SDLC 违规】P4(审查阶段)完成后必须推进到 P5(交付阶段),不能跳过。');
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (oldPhase === '1' && newPhase === '2') {
|
|
662
|
+
const methodOk = /requirement_method_selected:\s*true/.test(stateText);
|
|
663
|
+
if (!methodOk) {
|
|
664
|
+
emitDecision('deny', '【SDLC 违规】P1→P2 前必须先完成「需求澄清方式选择」(A brainstorming / B openspec)并记录 checkpoint_status.requirement_method_selected = true。');
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
const missing = checkDeliverables(stateText, oldPhase, root);
|
|
670
|
+
if (missing.length > 0) {
|
|
671
|
+
emitDecision('deny', `【SDLC 违规】P${oldPhase}→P${newPhase} 产出物检查失败,缺少: ${missing.join('、')},请先补充完整。`);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* check-phase-test:shell_command 阶段守卫
|
|
677
|
+
*
|
|
678
|
+
* @param {object} input Codex hook 输入
|
|
679
|
+
* @param {string} root 项目根目录
|
|
680
|
+
* @param {string} stateText 状态文件文本
|
|
681
|
+
*/
|
|
682
|
+
function checkPhaseTest(input, root, stateText) {
|
|
683
|
+
// 无阶段限制,测试/构建命令放行;危险命令交由 Codex 自身权限确认
|
|
684
|
+
const command = extractCommand(input);
|
|
685
|
+
if (DANGEROUS_PATTERNS.some((pattern) => pattern.test(command))) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* permission-request:权限请求阶段的统一安全决策
|
|
692
|
+
*
|
|
693
|
+
* @param {object} input Codex hook 输入
|
|
694
|
+
* @param {string} root 项目根目录
|
|
695
|
+
* @param {string} stateText 状态文件文本
|
|
696
|
+
*/
|
|
697
|
+
function permissionRequest(input, root, stateText) {
|
|
698
|
+
// 非受管项目(无状态文件)交给 Codex 自身权限确认
|
|
699
|
+
if (!stateText || !stateText.trim()) {
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const phaseNum = Number(getCurrentPhase(stateText) || '0');
|
|
703
|
+
const toolName = (input && input.tool_name) || '';
|
|
704
|
+
const toolInput = (input && input.tool_input) || {};
|
|
705
|
+
const filePath = typeof toolInput === 'string' ? '' : toolInput.file_path || '';
|
|
706
|
+
const command = extractCommand(input);
|
|
707
|
+
|
|
708
|
+
if (toolName === 'apply_patch') {
|
|
709
|
+
const files = filePath ? [filePath] : extractPatchFiles(input);
|
|
710
|
+
for (const rawFile of files) {
|
|
711
|
+
const ext = getExtension(rawFile);
|
|
712
|
+
if (DOC_EXTENSIONS.has(ext)) {
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
if ((ext === 'html' || ext === 'css') && phaseNum >= 1) {
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
if (CODE_EXTENSIONS.has(ext)) {
|
|
719
|
+
if (phaseNum >= 2) {
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
emitDecision('deny', `阶段 P${phaseNum} 不允许修改代码文件(P2 起可用,P1 仅允许原型 HTML/CSS)。`);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const isShellTool = toolName === 'shell_command' || toolName === 'exec_command' || toolName === 'Bash';
|
|
730
|
+
if (isShellTool) {
|
|
731
|
+
if (DANGEROUS_PATTERNS.some((pattern) => pattern.test(command))) {
|
|
732
|
+
return; // 危险命令交给 Codex 自身确认
|
|
733
|
+
}
|
|
734
|
+
if (/^git\s+(status|diff|log|branch|stash)\b/i.test(command)) {
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
if (GIT_COMMIT_PATTERNS.some((pattern) => pattern.test(command))) {
|
|
738
|
+
if (phaseNum < 5) {
|
|
739
|
+
emitDecision('deny', 'Git 提交/推送/打标签/合并操作仅 P5 阶段允许。');
|
|
740
|
+
}
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
if (TEST_PATTERNS.some((pattern) => pattern.test(command))) {
|
|
744
|
+
if (phaseNum < 3) {
|
|
745
|
+
emitDecision('deny', '测试命令仅 P3 起允许。');
|
|
746
|
+
}
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
if (BUILD_PATTERNS.some((pattern) => pattern.test(command))) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (phaseNum < 2) {
|
|
753
|
+
emitDecision('deny', `阶段 P${phaseNum} 不允许执行该命令,请先完成 P1 设计。`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* session-start:会话启动时注入项目状态上下文
|
|
760
|
+
*
|
|
761
|
+
* @param {object} input Codex hook 输入
|
|
762
|
+
* @param {string} root 项目根目录
|
|
763
|
+
* @param {string} stateText 状态文件文本
|
|
764
|
+
*/
|
|
765
|
+
function sessionStart(input, root, stateText) {
|
|
766
|
+
if (!stateText) {
|
|
767
|
+
// R1: Claude 单侧管理的项目(已有 .claude 状态)→ 不提示,避免误报未启用
|
|
768
|
+
if (fs.existsSync(path.join(root, '.claude', 'project-state.md'))) {
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
// R1/R2: 未启用 SDLC 的项目在 startup 时提示一次;.flowcraft.optout 存在则跳过
|
|
772
|
+
const source = (input && input.source) || '';
|
|
773
|
+
if (source === 'startup' && !fs.existsSync(path.join(root, '.flowcraft.optout'))) {
|
|
774
|
+
emitContext(
|
|
775
|
+
'SessionStart',
|
|
776
|
+
'此项目未启用 FlowCraft SDLC(无 .codex/project-state.md)。请询问用户是否启用;' +
|
|
777
|
+
'用户确认后从 ~/.flowcraft/templates/ 复制模板初始化' +
|
|
778
|
+
'(codex-project-state.md→.codex/project-state.md + AGENTS.md,已存在不覆盖);' +
|
|
779
|
+
'用户拒绝则创建 .flowcraft.optout 标记,后续不再提示。'
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
const task = getStateField(stateText, 'task_description');
|
|
785
|
+
const phase = getCurrentPhase(stateText);
|
|
786
|
+
const filesCount = countModifiedFiles(stateText);
|
|
787
|
+
const roadmap = getStateField(stateText, 'project_roadmap');
|
|
788
|
+
const completed = (stateText.match(/^\s*-\s+task:/gm) || []).length;
|
|
789
|
+
const source = (input && input.source) || '';
|
|
790
|
+
|
|
791
|
+
const persistParts = [];
|
|
792
|
+
if (roadmap) {
|
|
793
|
+
persistParts.push(`有长期规划(${roadmap})`);
|
|
794
|
+
}
|
|
795
|
+
if (completed > 0) {
|
|
796
|
+
persistParts.push(`已完成${completed}个任务`);
|
|
797
|
+
}
|
|
798
|
+
const persist = persistParts.length > 0 ? persistParts.join(',') + ',' : '';
|
|
799
|
+
|
|
800
|
+
let context = '';
|
|
801
|
+
if (!task) {
|
|
802
|
+
context = `SDLC 状态:等待新任务。${persist}请先读取 .codex/project-state.md 了解已有规划。`;
|
|
803
|
+
} else if (source === 'compact') {
|
|
804
|
+
context = `SDLC 压缩恢复:阶段=${phase},任务=${task},已修改${filesCount}个文件。${persist}请读取 .codex/project-state.md 后继续。`;
|
|
805
|
+
} else {
|
|
806
|
+
context = `SDLC 状态恢复:阶段=${phase},任务=${task},已修改${filesCount}个文件。${persist}请读取 .codex/project-state.md 确认状态。`;
|
|
807
|
+
}
|
|
808
|
+
// R5 兜底: 过期待决决策 → 注入自动执行上下文(懒检查通道)
|
|
809
|
+
context += expiredDecisionContext(root);
|
|
810
|
+
emitContext('SessionStart', context);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* user-prompt-submit:记录用户问题到 session_log.json
|
|
815
|
+
*
|
|
816
|
+
* @param {object} input Codex hook 输入
|
|
817
|
+
* @param {string} root 项目根目录
|
|
818
|
+
* @param {string} stateText 状态文件文本
|
|
819
|
+
*/
|
|
820
|
+
function userPromptSubmit(input, root, stateText) {
|
|
821
|
+
if (!stateText) {
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
const prompt = extractUserPrompt(input);
|
|
825
|
+
if (prompt) {
|
|
826
|
+
// R7: 任意用户输入即视为已响应 → 删除待决标记
|
|
827
|
+
clearDecisionMarker(root);
|
|
828
|
+
const ok = logSession(root, 'user', prompt);
|
|
829
|
+
if (!ok) {
|
|
830
|
+
logDiagnostic(root, 'UserPromptSubmit', '用户问题未写入 session_log.json');
|
|
831
|
+
}
|
|
832
|
+
} else {
|
|
833
|
+
const keys = input && typeof input === 'object' ? Object.keys(input).join(',') : 'non-object';
|
|
834
|
+
logDiagnostic(root, 'UserPromptSubmit', `未找到 prompt 字段,输入顶层键: ${keys}`);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* pre-compact:压缩前状态自检提示
|
|
840
|
+
*
|
|
841
|
+
* @param {object} input Codex hook 输入
|
|
842
|
+
* @param {string} root 项目根目录
|
|
843
|
+
* @param {string} stateText 状态文件文本
|
|
844
|
+
*/
|
|
845
|
+
function preCompact(input, root, stateText) {
|
|
846
|
+
if (!stateText) {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
const task = getStateField(stateText, 'task_description');
|
|
850
|
+
const keyContext = getStateField(stateText, 'key_context');
|
|
851
|
+
const filesCount = countModifiedFiles(stateText);
|
|
852
|
+
const warnings = [];
|
|
853
|
+
if (!keyContext) {
|
|
854
|
+
warnings.push('key_context 为空');
|
|
855
|
+
}
|
|
856
|
+
if (filesCount === 0) {
|
|
857
|
+
warnings.push('modified_files 为空');
|
|
858
|
+
}
|
|
859
|
+
emitContext(
|
|
860
|
+
'PreCompact',
|
|
861
|
+
`[压缩前] 任务=${task || '(无)'},已修改${filesCount}个文件。${warnings.join(',') || '状态正常'}。请确保 project-state.md 中 modified_files 与 key_context 已更新。`
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* stop-check:停止时记录 AI 回复并轻量自检
|
|
867
|
+
*
|
|
868
|
+
* @param {object} input Codex hook 输入
|
|
869
|
+
* @param {string} root 项目根目录
|
|
870
|
+
* @param {string} stateText 状态文件文本
|
|
871
|
+
*/
|
|
872
|
+
function stopCheck(input, root, stateText) {
|
|
873
|
+
if (!stateText) {
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
const aiMessage = extractAssistantMessage(input);
|
|
877
|
+
if (aiMessage) {
|
|
878
|
+
const ok = logSession(root, 'assistant', aiMessage);
|
|
879
|
+
if (!ok) {
|
|
880
|
+
logDiagnostic(root, 'Stop', 'AI 回复未写入 session_log.json');
|
|
881
|
+
}
|
|
882
|
+
// R4/R5: 待决决策标记——决策卡回合写入,非决策卡回合清除
|
|
883
|
+
updateDecisionMarker(root, aiMessage);
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
const task = getStateField(stateText, 'task_description');
|
|
887
|
+
if (!task) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
const keyContext = getStateField(stateText, 'key_context');
|
|
891
|
+
const filesCount = countModifiedFiles(stateText);
|
|
892
|
+
const checks = [];
|
|
893
|
+
if (!keyContext) {
|
|
894
|
+
checks.push('key_context 为空,请更新 project-state.md');
|
|
895
|
+
}
|
|
896
|
+
if (filesCount > 0) {
|
|
897
|
+
checks.push(`已修改 ${filesCount} 个文件`);
|
|
898
|
+
}
|
|
899
|
+
const completedTasks = (stateText.match(/^ {2}- task:/gm) || []).length;
|
|
900
|
+
if (completedTasks >= 5) {
|
|
901
|
+
checks.push(`completed_tasks 已达 ${completedTasks} 条(≥5),建议运行 /archive 归档历史任务`);
|
|
902
|
+
}
|
|
903
|
+
if (checks.length > 0) {
|
|
904
|
+
emitContext('Stop', `[自检] 任务=${task}。${checks.join(';')}`);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* session-end:生成会话摘要日志(session_log.md),不同步 ES(接口需 JWT,脚本无凭证)
|
|
910
|
+
*
|
|
911
|
+
* @param {object} input Codex hook 输入
|
|
912
|
+
* @param {string} root 项目根目录
|
|
913
|
+
* @param {string} stateText 状态文件文本
|
|
914
|
+
*/
|
|
915
|
+
function sessionEnd(input, root, stateText) {
|
|
916
|
+
if (!stateText) {
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
const sessionLogPath = path.join(root, '.codex', 'session_log.md');
|
|
920
|
+
const phase = getCurrentPhase(stateText);
|
|
921
|
+
const task = getStateField(stateText, 'task_description');
|
|
922
|
+
const keyContext = getStateField(stateText, 'key_context');
|
|
923
|
+
const filesCount = countModifiedFiles(stateText);
|
|
924
|
+
const timestamp = new Date().toLocaleString('zh-CN', { hour12: false });
|
|
925
|
+
|
|
926
|
+
const lines = [
|
|
927
|
+
'# Session Log',
|
|
928
|
+
'',
|
|
929
|
+
`**生成时间**: ${timestamp}`,
|
|
930
|
+
`**阶段**: P${phase} - ${task || '(无任务)'}`,
|
|
931
|
+
`**上下文**: ${keyContext || '(空)'}`,
|
|
932
|
+
'',
|
|
933
|
+
`**当前会话修改的文件数**: ${filesCount}`,
|
|
934
|
+
'',
|
|
935
|
+
'---',
|
|
936
|
+
'',
|
|
937
|
+
];
|
|
938
|
+
|
|
939
|
+
let historyTail = '';
|
|
940
|
+
try {
|
|
941
|
+
const history = fs.readFileSync(sessionLogPath, 'utf-8').split('\n');
|
|
942
|
+
historyTail = history.slice(-200).join('\n');
|
|
943
|
+
} catch {
|
|
944
|
+
historyTail = '';
|
|
945
|
+
}
|
|
946
|
+
try {
|
|
947
|
+
fs.writeFileSync(sessionLogPath, lines.join('\n') + (historyTail ? '\n' + historyTail : ''), 'utf-8');
|
|
948
|
+
} catch {
|
|
949
|
+
// 写入失败静默
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* subagent-start/subagent-stop:子 Agent 上下文注入与完成验证
|
|
955
|
+
*
|
|
956
|
+
* @param {object} input Codex hook 输入
|
|
957
|
+
* @param {string} root 项目根目录
|
|
958
|
+
* @param {string} stateText 状态文件文本
|
|
959
|
+
* @param {boolean} isStart 是否为启动事件
|
|
960
|
+
*/
|
|
961
|
+
function subagentHook(input, root, stateText, isStart) {
|
|
962
|
+
if (!stateText) {
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
const task = getStateField(stateText, 'task_description');
|
|
966
|
+
if (!task) {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
if (isStart) {
|
|
970
|
+
const keyContext = getStateField(stateText, 'key_context');
|
|
971
|
+
emitContext(
|
|
972
|
+
'SubagentStart',
|
|
973
|
+
`[子 Agent] 任务=${task}。上下文=${keyContext || '(空)'}。编码规范:函数≤50行、嵌套≤3层、完成后报告修改文件列表。`
|
|
974
|
+
);
|
|
975
|
+
} else {
|
|
976
|
+
const filesCount = countModifiedFiles(stateText);
|
|
977
|
+
emitContext(
|
|
978
|
+
'SubagentStop',
|
|
979
|
+
`[子 Agent 完成] 任务=${task},已修改${filesCount}个文件。请验证:(1) 代码符合需求 (2) 无多余变更 (3) modified_files 已更新。`
|
|
980
|
+
);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* 主入口:按事件参数分发处理
|
|
986
|
+
*/
|
|
987
|
+
function main() {
|
|
988
|
+
try {
|
|
989
|
+
const { raw, parsed } = readStdin();
|
|
990
|
+
const input = parsed || { raw };
|
|
991
|
+
const root = findProjectRoot(input);
|
|
992
|
+
const stateText = readStateFile(root);
|
|
993
|
+
|
|
994
|
+
switch (EVENT) {
|
|
995
|
+
case 'check-phase-write':
|
|
996
|
+
checkPhaseWrite(input, root, stateText);
|
|
997
|
+
break;
|
|
998
|
+
case 'check-phase-advancement':
|
|
999
|
+
checkPhaseAdvancement(input, root, stateText);
|
|
1000
|
+
break;
|
|
1001
|
+
case 'check-phase-test':
|
|
1002
|
+
checkPhaseTest(input, root, stateText);
|
|
1003
|
+
break;
|
|
1004
|
+
case 'permission-request':
|
|
1005
|
+
permissionRequest(input, root, stateText);
|
|
1006
|
+
break;
|
|
1007
|
+
case 'session-start':
|
|
1008
|
+
sessionStart(input, root, stateText);
|
|
1009
|
+
break;
|
|
1010
|
+
case 'user-prompt-submit':
|
|
1011
|
+
userPromptSubmit(input, root, stateText);
|
|
1012
|
+
break;
|
|
1013
|
+
case 'pre-compact':
|
|
1014
|
+
preCompact(input, root, stateText);
|
|
1015
|
+
break;
|
|
1016
|
+
case 'stop-check':
|
|
1017
|
+
stopCheck(input, root, stateText);
|
|
1018
|
+
break;
|
|
1019
|
+
case 'session-end':
|
|
1020
|
+
sessionEnd(input, root, stateText);
|
|
1021
|
+
break;
|
|
1022
|
+
case 'subagent-start':
|
|
1023
|
+
subagentHook(input, root, stateText, true);
|
|
1024
|
+
break;
|
|
1025
|
+
case 'subagent-stop':
|
|
1026
|
+
subagentHook(input, root, stateText, false);
|
|
1027
|
+
break;
|
|
1028
|
+
default:
|
|
1029
|
+
break;
|
|
1030
|
+
}
|
|
1031
|
+
} catch (err) {
|
|
1032
|
+
// hook 异常时记录错误日志,避免全静默导致守卫失效难排查;仍保持退出码 0 不阻断 Codex
|
|
1033
|
+
appendHookLog(path.join(__dirname, 'hook-errors.log'), `EVENT=${EVENT} ERROR=${(err && err.stack) || err}`);
|
|
1034
|
+
}
|
|
1035
|
+
process.exit(0);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
main();
|
|
1039
|
+
|