flower-trellis 0.6.13-beta.7 → 0.6.13-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/enhancements/0.6/.agents/skills/trellis-check-all/references/reporting-and-disposition.md +1 -1
- package/enhancements/0.6/.agents/skills/trellis-push/SKILL.md +1 -1
- package/enhancements/0.6/.agents/skills/trellis-task-brief/SKILL.md +1 -1
- package/enhancements/0.6/.claude/skills/trellis-check-all/references/reporting-and-disposition.md +1 -1
- package/enhancements/0.6/.claude/skills/trellis-push/SKILL.md +1 -1
- package/enhancements/0.6/.claude/skills/trellis-task-brief/SKILL.md +1 -1
- package/enhancements/0.6/scripts/confirmation.md +6 -6
- package/enhancements/MANIFEST.json +2 -2
- package/package.json +6 -5
- package/src/assets/flower_confirmation.py +182 -0
- package/src/assets/flower_confirmation_hook.py +7 -11
- package/src/assets/flower_confirmation_runtime/__init__.py +1 -0
- package/src/assets/flower_confirmation_runtime/claude.py +238 -0
- package/src/assets/flower_confirmation_runtime/codex.py +328 -0
- package/src/assets/flower_confirmation_runtime/runtime.py +428 -0
- package/src/assets/flower_confirmation_runtime/store.py +350 -0
- package/src/assets/flower_confirmation_runtime/transport.py +198 -0
- package/src/builtin-plugins/skill-garden/content-adapter.js +11 -7
- package/src/cli.js +1 -8
- package/src/commands/uninstall.js +2 -0
- package/src/lib/confirmation-lifecycle.js +51 -0
- package/src/lib/flower-assets.js +22 -6
- package/src/lib/trellis-control.js +1 -1
- package/src/plugin/application-service.js +2 -0
- package/src/commands/confirm.js +0 -144
- package/src/lib/claude-confirmation-client.js +0 -207
- package/src/lib/codex-confirmation-client.js +0 -93
- package/src/lib/codex-confirmation.js +0 -452
- package/src/lib/codex-queue-client.js +0 -291
- package/src/lib/codex-tui-lifetime.js +0 -171
- package/src/lib/confirmation-protocol.js +0 -34
- package/src/lib/confirmation-runtime.js +0 -189
package/enhancements/0.6/.agents/skills/trellis-check-all/references/reporting-and-disposition.md
CHANGED
|
@@ -49,7 +49,7 @@ untracked helper 不存检查证据或风险接受。通过且按下方 Gate 默
|
|
|
49
49
|
|
|
50
50
|
### 后台处置
|
|
51
51
|
|
|
52
|
-
最终回复前读取 `.trellis/scripts/confirmation.md`:普通待处置项必须 `
|
|
52
|
+
最终回复前读取 `.trellis/scripts/confirmation.md`:普通待处置项必须 项目 `prepare` 完整报告/全部 findings/版本,成功后后台 `wait`,原样完整展示 `display`。实际失败说明原因;只检查、停止、本机制自身开发验证等排除项保持人工。提醒仅 `claimed=true`、`source=policy_timeout`、`action=repair_all_and_recheck` 且 started 成功才修复重检并记终态。准备不需领取。
|
|
53
53
|
|
|
54
54
|
### 交互式下一步引导
|
|
55
55
|
|
|
@@ -11,7 +11,7 @@ description: "按确认的精确文件范围提交普通变更或完成已就绪
|
|
|
11
11
|
|
|
12
12
|
人工确认或有效 policy_timeout claim 与 started 回执之前,禁止 `git add`、`git commit` 或 `git push`。准备后台等待不执行 Git 写入。
|
|
13
13
|
|
|
14
|
-
最终回复前按 `.trellis/scripts/confirmation.md`:已有授权续接;只看计划/停止、本机制自身开发验证或未决风险保持人工。其余必须 `
|
|
14
|
+
最终回复前按 `.trellis/scripts/confirmation.md`:已有授权续接;只看计划/停止、本机制自身开发验证或未决风险保持人工。其余必须 项目 `prepare` 完整计划及精确范围;成功后后台 `wait`,原样完整展示 `display` 并结束回合。失败报告实际原因。提醒仅 `claimed=true`、`source=policy_timeout`、`action=execute_git_plan` 且 started 成功进 Step 4,后记终态。准备不需领取,回执不是人工确认,不扩大范围。
|
|
15
15
|
|
|
16
16
|
普通模式精确计划确认一次后 commit + push;仅用户要求本地提交才 commit-only,不写任务进度。task 普通计划含独立记录提交。
|
|
17
17
|
|
|
@@ -35,7 +35,7 @@ description: "从最新 prd.md、design.md、implement.md 生成、刷新、校
|
|
|
35
35
|
1. 已有内部到期提醒:先按 `.trellis/scripts/confirmation.md` 领取,不重建请求。只有实际 `claimed=true`、`source=policy_timeout`、`action=implement` 且 `started` 记账成功,才运行原 `task.py start` 与 route;回执不代表人工确认。
|
|
36
36
|
2. 有同一 Brief 的有效人工批准、明确预授权或合法 auto-loop action:按对应授权续接,不叠加计时。
|
|
37
37
|
3. 用户明确“只规划 / 不开始 / 展示后停止”、产品决定未收敛、本机制自身开发验证:展示完整 Brief,说明具体原因并保持人工。普通“创建任务 / 开始做”本身不属于显式停止。
|
|
38
|
-
4. 其余普通 Codex / Claude 交互:必须读取 `.trellis/scripts/confirmation.md`,实际执行 `
|
|
38
|
+
4. 其余普通 Codex / Claude 交互:必须读取 `.trellis/scripts/confirmation.md`,实际执行 项目 `prepare`;不要求用户额外说“开启超时”,也不以尚无领取回执为由跳过准备。`prepared=true` 后用原生后台 terminal/Bash 启动 `wait`,最终回复原样完整展示返回的 `display` 并结束回合。
|
|
39
39
|
5. 禁用、坏配置、能力缺失或工具失败:使用实际返回原因,展示完整 Brief 和人工确认提示;无法读取入口或协议时明确缺失位置。不得未尝试就声称环境不支持,不显示虚构的倒计时。
|
|
40
40
|
|
|
41
41
|
准备和展示只建立等待;业务许可必须在到期后另行领取。人工新消息优先,材料变化使旧许可失效。
|
package/enhancements/0.6/.claude/skills/trellis-check-all/references/reporting-and-disposition.md
CHANGED
|
@@ -49,7 +49,7 @@ untracked helper 不存检查证据或风险接受。通过且按下方 Gate 默
|
|
|
49
49
|
|
|
50
50
|
### 后台处置
|
|
51
51
|
|
|
52
|
-
最终回复前读取 `.trellis/scripts/confirmation.md`:普通待处置项必须 `
|
|
52
|
+
最终回复前读取 `.trellis/scripts/confirmation.md`:普通待处置项必须 项目 `prepare` 完整报告/全部 findings/版本,成功后后台 `wait`,原样完整展示 `display`。实际失败说明原因;只检查、停止、本机制自身开发验证等排除项保持人工。提醒仅 `claimed=true`、`source=policy_timeout`、`action=repair_all_and_recheck` 且 started 成功才修复重检并记终态。准备不需领取。
|
|
53
53
|
|
|
54
54
|
### 交互式下一步引导
|
|
55
55
|
|
|
@@ -11,7 +11,7 @@ description: "按确认的精确文件范围提交普通变更或完成已就绪
|
|
|
11
11
|
|
|
12
12
|
人工确认或有效 policy_timeout claim 与 started 回执之前,禁止 `git add`、`git commit` 或 `git push`。准备后台等待不执行 Git 写入。
|
|
13
13
|
|
|
14
|
-
最终回复前按 `.trellis/scripts/confirmation.md`:已有授权续接;只看计划/停止、本机制自身开发验证或未决风险保持人工。其余必须 `
|
|
14
|
+
最终回复前按 `.trellis/scripts/confirmation.md`:已有授权续接;只看计划/停止、本机制自身开发验证或未决风险保持人工。其余必须 项目 `prepare` 完整计划及精确范围;成功后后台 `wait`,原样完整展示 `display` 并结束回合。失败报告实际原因。提醒仅 `claimed=true`、`source=policy_timeout`、`action=execute_git_plan` 且 started 成功进 Step 4,后记终态。准备不需领取,回执不是人工确认,不扩大范围。
|
|
15
15
|
|
|
16
16
|
普通模式精确计划确认一次后 commit + push;仅用户要求本地提交才 commit-only,不写任务进度。task 普通计划含独立记录提交。
|
|
17
17
|
|
|
@@ -35,7 +35,7 @@ description: "从最新 prd.md、design.md、implement.md 生成、刷新、校
|
|
|
35
35
|
1. 已有内部到期提醒:先按 `.trellis/scripts/confirmation.md` 领取,不重建请求。只有实际 `claimed=true`、`source=policy_timeout`、`action=implement` 且 `started` 记账成功,才运行原 `task.py start` 与 route;回执不代表人工确认。
|
|
36
36
|
2. 有同一 Brief 的有效人工批准、明确预授权或合法 auto-loop action:按对应授权续接,不叠加计时。
|
|
37
37
|
3. 用户明确“只规划 / 不开始 / 展示后停止”、产品决定未收敛、本机制自身开发验证:展示完整 Brief,说明具体原因并保持人工。普通“创建任务 / 开始做”本身不属于显式停止。
|
|
38
|
-
4. 其余普通 Codex / Claude 交互:必须读取 `.trellis/scripts/confirmation.md`,实际执行 `
|
|
38
|
+
4. 其余普通 Codex / Claude 交互:必须读取 `.trellis/scripts/confirmation.md`,实际执行 项目 `prepare`;不要求用户额外说“开启超时”,也不以尚无领取回执为由跳过准备。`prepared=true` 后用原生后台 terminal/Bash 启动 `wait`,最终回复原样完整展示返回的 `display` 并结束回合。
|
|
39
39
|
5. 禁用、坏配置、能力缺失或工具失败:使用实际返回原因,展示完整 Brief 和人工确认提示;无法读取入口或协议时明确缺失位置。不得未尝试就声称环境不支持,不显示虚构的倒计时。
|
|
40
40
|
|
|
41
41
|
准备和展示只建立等待;业务许可必须在到期后另行领取。人工新消息优先,材料变化使旧许可失效。
|
|
@@ -19,21 +19,21 @@
|
|
|
19
19
|
|
|
20
20
|
Codex 保持普通 `codex` / `codex resume` 与默认共享服务。通过当前根会话内置 codex_tui 的本机监听者绑定原前台;需要新输入 Hook 已实际加载。缺少服务、出生身份或可靠退出能力时保持人工。Claude 保持原来的普通 TUI。
|
|
21
21
|
|
|
22
|
-
先从项目根执行 `python3 .trellis/scripts/flower_confirmation_hook.py --entry`,读取实际返回的 `entry` argv
|
|
22
|
+
先从项目根执行 `python3 .trellis/scripts/flower_confirmation_hook.py --entry`,读取实际返回的 `entry` argv,后续在其后追加 `prepare`、`wait` 等子命令。入口固定为项目内 `.trellis/scripts/flower_confirmation.py`,内部复用当前 Python 解释器,仅依赖标准库和项目脚本。`entry=null` 或工具失败才报告实际原因;不能用无 stdout 的 `test -f` 推断入口不存在。确认阶段无需 Node、npm、全局 Flower 或额外 pip 安装。
|
|
23
23
|
|
|
24
24
|
1. 在 gitignored `.trellis/.runtime/` 为当前平台和会话写一个 JSON 材料文件,路径不得复用于并行会话。不要把它放入提交集合,不生成公开报告附件。使用结构化文件写入,用户正文和 message 不拼入 shell。
|
|
25
25
|
2. JSON 的公共字段为 `kind: brief|check|push`、`task`(当前精确任务相对路径,无任务为 null)。Brief 正文读取当前任务 `brief.md`。Check/Push 另提供完整标准报告/计划的 `body`。
|
|
26
26
|
3. Check 必填 `reportVersion`、`dispositionVersion`(非空版本字符串)及 `findings: [{id:"CHK-001",status:"pending"}]`;status 只接受 pending/accepted/resolved。必须覆盖当前全部条目,禁止只传准备修复的子集来改变默认动作。
|
|
27
27
|
4. Push 必填 `mode: push|commit-only`、`evidence: {check:passed|accepted|focused,spec:no-op|written,unresolvedRisks:0}` 和有序 `repositories`。每仓包含 `root`、实际 `branch`、完整 `head`(初始空仓为 null)、实际 `upstream`(没有为 null)、精确 `message`、完整 `files` 路径列表。把已展示的生成命令、任务记录 exact files 与顺序一并保存在输入中,不在领取后扩大范围。
|
|
28
|
-
5. 从项目根执行 `
|
|
29
|
-
6. 两端运行同一个 `
|
|
28
|
+
5. 从项目根执行 `python3 -X utf8 .trellis/scripts/flower_confirmation.py prepare --platform codex|claude --input <相对路径>`;用户明确指定本次时长时加 `--seconds <正整数秒>`。仅 `prepared=true` 可继续,任何错误或 false 保持原人工提示。
|
|
29
|
+
6. 两端运行同一个 `python3 -X utf8 .trellis/scripts/flower_confirmation.py wait --platform <平台> --session <prepare.sessionId> --request <UUID>`。Claude 用 Bash `run_in_background:true`。Codex 用原生 `exec_command` 短 `yield_time_ms`(例如 1000)启动,取得持续运行的 terminal session 后结束当前回合;不等待命令完成、不轮询、不启动子代理、不用 clock.sleep。后台 worker 到期会经原生同会话队列唤醒。
|
|
30
30
|
7. 最终回复完整展示 prepare 返回的 `display`;它包含标准正文和本次时长/默认动作。可在前后补必要说明,但不能省略、改写 display。后台会核验原回合最终正文,展示完成才开始计时;工具启动和生成时间不扣减等待时间。
|
|
31
31
|
|
|
32
32
|
## 到期领取与原流程续接
|
|
33
33
|
|
|
34
|
-
原生提醒仅唤醒:先执行 `
|
|
34
|
+
原生提醒仅唤醒:先执行 `python3 -X utf8 .trellis/scripts/flower_confirmation.py claim --platform <平台> --request <UUID>`。必须实际取得 `claimed=true`、`source=policy_timeout`、预期 action 和当前冻结 context,才可续接。false、错误、材料或会话漂移时结束旧提醒,不执行旧动作、不重复领取、不假称用户已确认。
|
|
35
35
|
|
|
36
|
-
领取后、首个业务动作前,执行 `
|
|
36
|
+
领取后、首个业务动作前,执行 `python3 -X utf8 .trellis/scripts/flower_confirmation.py result --platform <平台> --request <UUID> --receipt <回执 UUID> --state started`。只有 `recorded=true` 才进入对应 owner;重复 started 不能再次执行。Brief 继续原 `task.py start` 与 route;Check 继续原“修复全部”及重检;Push 继续原精确范围和逐仓预检、提交、推送、进度/Close 及失败恢复。不得把 receipt 当成新产品决策、原生审批或任意 shell 的授权。
|
|
37
37
|
|
|
38
38
|
按实际结果用同一个回执记录 completed / interrupted / needs-verification;已开始的副作用不能伪称撤销。不确定时记录 needs-verification 并按原 owner 恢复,不重跑旧默认动作。新人工消息优先按现有流程处理,不因已有 receipt 忽略用户。
|
|
39
39
|
|
|
@@ -61,6 +61,6 @@ confirmation_timeout:
|
|
|
61
61
|
seconds: 200
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
支持正整数秒数 1~86400,超界或坏配置保持人工,不缩短为更早执行。单次 `--seconds` 优先,但不绕过关闭开关。用户任何新消息都会使旧等待失效;人工确认沿用原流程。改时或改材料先使旧请求失效,更新 JSON,再重新 prepare/wait/display。显式取消使用 `
|
|
64
|
+
支持正整数秒数 1~86400,超界或坏配置保持人工,不缩短为更早执行。单次 `--seconds` 优先,但不绕过关闭开关。用户任何新消息都会使旧等待失效;人工确认沿用原流程。改时或改材料先使旧请求失效,更新 JSON,再重新 prepare/wait/display。显式取消使用 `python3 -X utf8 .trellis/scripts/flower_confirmation.py cancel --platform <平台> [--request <UUID>]`;status 离线只读。
|
|
65
65
|
|
|
66
66
|
Codex 原窗口退出或其原生服务失效会取消旧等待;恢复同一会话不得把旧请求迁移到新窗口。同会话双开会产生未对应独立会话的额外前台,检测到后保持人工;不得借用第一窗口证明第二窗口存活。原窗口失效后旧请求不能转移到后来打开的窗口。Claude 使用原生退出事件取消。重复或迟到通知可能增加模型回合,但不能重复有效领取;已经开始的业务副作用如实记账,不宣称自动回滚。
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"syncedAt": "2026-11-
|
|
2
|
+
"syncedAt": "2026-11-10T07:32:50.766Z",
|
|
3
3
|
"syncedFrom": "vendor/skill-garden",
|
|
4
|
-
"sourceCommit": "
|
|
4
|
+
"sourceCommit": "ada1fe170443e2ffc4c0baeb1d74928628c13141",
|
|
5
5
|
"common": {
|
|
6
6
|
"codexSkills": [
|
|
7
7
|
"aliyun-ops",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flower-trellis",
|
|
3
|
-
"version": "0.6.13-beta.
|
|
3
|
+
"version": "0.6.13-beta.8",
|
|
4
4
|
"description": "一键安装/升级 Trellis 并自动融合 skill-garden 强化包(默认 Claude + agents)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -21,12 +21,13 @@
|
|
|
21
21
|
"node-pty": "^1.1.0",
|
|
22
22
|
"semver": "^7.8.5",
|
|
23
23
|
"smol-toml": "1.9.1",
|
|
24
|
-
"tar": "^7.5.22"
|
|
25
|
-
"ws": "^8.22.2"
|
|
24
|
+
"tar": "^7.5.22"
|
|
26
25
|
},
|
|
27
26
|
"files": [
|
|
28
27
|
"bin",
|
|
29
28
|
"src",
|
|
29
|
+
"!src/**/__pycache__",
|
|
30
|
+
"!src/**/*.pyc",
|
|
30
31
|
"!src/lib/patch-fixture.js",
|
|
31
32
|
"enhancements",
|
|
32
33
|
"!enhancements/**/__pycache__",
|
|
@@ -72,9 +73,9 @@
|
|
|
72
73
|
"commit-and-tag-version": "^12.7.3"
|
|
73
74
|
},
|
|
74
75
|
"flowerReleaseNotes": {
|
|
75
|
-
"version": "0.6.13-beta.
|
|
76
|
+
"version": "0.6.13-beta.8",
|
|
76
77
|
"source": "CHANGELOG.md",
|
|
77
|
-
"body": "### 🐛 修复 Bug Fixes\n\n* **confirmation:** 修复 Windows
|
|
78
|
+
"body": "### ✨ 新功能 Features\n\n* **confirmation:** 将确认运行时迁移为项目内 Python ([016cde7](https://github.com/SilentFlower/flower-trellis/commit/016cde775f1a8dec3a1a01634802915369e9c19e))\n\n\n### 🐛 修复 Bug Fixes\n\n* **confirmation:** 修复 Windows 文件共享占用竞争 ([8e24d13](https://github.com/SilentFlower/flower-trellis/commit/8e24d131abc7724413ce4bac26137e18447fed33))",
|
|
78
79
|
"truncated": false
|
|
79
80
|
},
|
|
80
81
|
"optionalDependencies": {
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""项目内原生后台确认命令,使用 Python 标准库独立运行。"""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import signal
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
import time
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from flower_confirmation_runtime.store import ConfirmationStore, cancel_project, confirmation_id, wake_message
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
HELP = """项目内原生后台确认(Linux/WSL,Codex / Claude)
|
|
18
|
+
|
|
19
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py prepare --input <项目内 JSON> [--seconds <1..86400>]
|
|
20
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py wait --request <UUID>
|
|
21
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py claim --request <UUID>
|
|
22
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py result --request <UUID> --receipt <UUID> --state <started|completed|interrupted|needs-verification>
|
|
23
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py status
|
|
24
|
+
python3 -X utf8 .trellis/scripts/flower_confirmation.py cancel [--request <UUID>]
|
|
25
|
+
|
|
26
|
+
公共参数:--platform codex|claude、--session <精确 UUID>、--target <项目根>。
|
|
27
|
+
prepare 返回完整 display;原生后台工具启动 wait,结束展示回合后计时。
|
|
28
|
+
到期只唤醒;claim 与 started 记账成功后进入返回的原流程。
|
|
29
|
+
输入、窗口退出和材料变化使旧请求失效。项目策略位于 confirmation_timeout。
|
|
30
|
+
hook 从 stdin 接收原生 JSON,由项目 Hook 使用。"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _output(value):
|
|
34
|
+
print(json.dumps(value, ensure_ascii=False, allow_nan=False), flush=True)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _arguments(argv):
|
|
38
|
+
if not argv:
|
|
39
|
+
return None, {}
|
|
40
|
+
command, flags = argv[0], argv[1:]
|
|
41
|
+
allowed = {"prepare": {"--input", "--seconds"}, "wait": {"--request"}, "claim": {"--request"},
|
|
42
|
+
"result": {"--request", "--receipt", "--state"}, "cancel": {"--request"}, "status": set(), "hook": set(),
|
|
43
|
+
"_cancel-project": {"--reason"}}
|
|
44
|
+
if command not in allowed:
|
|
45
|
+
raise ValueError("未知确认命令,使用 --help 查看用法")
|
|
46
|
+
if len(flags) % 2:
|
|
47
|
+
raise ValueError("确认参数缺少值")
|
|
48
|
+
values = {}
|
|
49
|
+
for index in range(0, len(flags), 2):
|
|
50
|
+
key, value = flags[index:index + 2]
|
|
51
|
+
if key not in allowed[command] | {"--target", "--platform", "--session"} or key in values:
|
|
52
|
+
raise ValueError("确认参数未知或重复")
|
|
53
|
+
values[key] = value
|
|
54
|
+
return command, values
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def main(argv=None):
|
|
58
|
+
"""执行一次项目确认命令并输出结构化结果。
|
|
59
|
+
|
|
60
|
+
@param argv: 可选命令参数,缺省读取 sys.argv。
|
|
61
|
+
@return: 成功 0、人工或失败 1、信号中断 130。
|
|
62
|
+
"""
|
|
63
|
+
args = sys.argv[1:] if argv is None else argv
|
|
64
|
+
if not args or any(value in ("--help", "-h") for value in args):
|
|
65
|
+
print(HELP)
|
|
66
|
+
return 0
|
|
67
|
+
client = None
|
|
68
|
+
store = None
|
|
69
|
+
owned_request = None
|
|
70
|
+
interrupted = []
|
|
71
|
+
previous_handlers = {}
|
|
72
|
+
try:
|
|
73
|
+
command, values = _arguments(args)
|
|
74
|
+
project = Path(values.get("--target", ".")).resolve(strict=True)
|
|
75
|
+
if command == "_cancel-project":
|
|
76
|
+
reason = values.get("--reason", "project_disabled")
|
|
77
|
+
if reason not in ("project_disabled", "project_updated", "project_uninstalled"):
|
|
78
|
+
raise ValueError("未知项目生命周期原因")
|
|
79
|
+
_output({"cancelled": cancel_project(project, reason)})
|
|
80
|
+
return 0
|
|
81
|
+
if command == "hook":
|
|
82
|
+
if sys.platform != "linux":
|
|
83
|
+
_output({})
|
|
84
|
+
return 0
|
|
85
|
+
raw = sys.stdin.buffer.read(1024 * 1024 + 1)
|
|
86
|
+
if len(raw) > 1024 * 1024:
|
|
87
|
+
raise ValueError("确认输入超过大小限制")
|
|
88
|
+
payload = json.loads(raw.decode("utf-8"))
|
|
89
|
+
platform = values.get("--platform") or ("codex" if os.environ.get("CODEX_THREAD_ID") else "claude")
|
|
90
|
+
if platform == "codex":
|
|
91
|
+
from flower_confirmation_runtime.codex import record_event
|
|
92
|
+
elif platform == "claude":
|
|
93
|
+
from flower_confirmation_runtime.claude import record_event
|
|
94
|
+
else:
|
|
95
|
+
raise ValueError("未知确认 Hook 平台")
|
|
96
|
+
for attempt in range(3):
|
|
97
|
+
try:
|
|
98
|
+
_output(record_event(payload))
|
|
99
|
+
return 0
|
|
100
|
+
except ValueError as error:
|
|
101
|
+
if str(error) != "确认状态锁繁忙" or attempt == 2:
|
|
102
|
+
raise
|
|
103
|
+
time.sleep(0.025)
|
|
104
|
+
environments = {"codex": os.environ.get("CODEX_THREAD_ID"), "claude": os.environ.get("CLAUDE_CODE_SESSION_ID")}
|
|
105
|
+
detected = [key for key, value in environments.items() if value]
|
|
106
|
+
platform = values.get("--platform") or (detected[0] if len(detected) == 1 else None)
|
|
107
|
+
if platform not in environments:
|
|
108
|
+
raise ValueError("缺少唯一的原生平台,保持人工确认")
|
|
109
|
+
session_id = confirmation_id(values.get("--session") or environments[platform])
|
|
110
|
+
if environments[platform] and environments[platform] != session_id:
|
|
111
|
+
raise ValueError("显式会话与当前原生会话不符")
|
|
112
|
+
if not (project / ".trellis").is_dir():
|
|
113
|
+
raise ValueError("目标不是 Trellis 项目")
|
|
114
|
+
store = ConfirmationStore(project, session_id, platform)
|
|
115
|
+
request_id = values.get("--request")
|
|
116
|
+
if request_id:
|
|
117
|
+
confirmation_id(request_id)
|
|
118
|
+
if command in ("wait", "claim", "result") and not request_id:
|
|
119
|
+
raise ValueError("必须指定精确 --request")
|
|
120
|
+
from flower_confirmation_runtime.runtime import cancel, claim, inspect_confirmation, prepare, record_result, status, wait
|
|
121
|
+
if command == "status":
|
|
122
|
+
_output(status(store))
|
|
123
|
+
return 0
|
|
124
|
+
if command == "cancel":
|
|
125
|
+
_output(cancel(store, request_id=request_id))
|
|
126
|
+
return 0
|
|
127
|
+
if command == "result":
|
|
128
|
+
confirmation_id(values.get("--receipt"))
|
|
129
|
+
if values.get("--state") != "started":
|
|
130
|
+
_output(record_result(store, None, request_id, values["--receipt"], values.get("--state")))
|
|
131
|
+
return 0
|
|
132
|
+
if command == "prepare":
|
|
133
|
+
if not values.get("--input"):
|
|
134
|
+
raise ValueError("prepare 必须提供项目内 --input 材料")
|
|
135
|
+
frozen = inspect_confirmation(store, values["--input"], values.get("--seconds"))
|
|
136
|
+
if not frozen["enabled"]:
|
|
137
|
+
_output({"prepared": False, "mode": "manual", "reason": frozen.get("reason")})
|
|
138
|
+
return 0
|
|
139
|
+
for number in (signal.SIGINT, signal.SIGTERM):
|
|
140
|
+
previous_handlers[number] = signal.getsignal(number)
|
|
141
|
+
signal.signal(number, lambda signum, frame: interrupted.append(True))
|
|
142
|
+
if platform == "codex":
|
|
143
|
+
from flower_confirmation_runtime.codex import CodexClient
|
|
144
|
+
client = CodexClient(project, session_id)
|
|
145
|
+
else:
|
|
146
|
+
from flower_confirmation_runtime.claude import ClaudeClient
|
|
147
|
+
client = ClaudeClient(project, session_id)
|
|
148
|
+
owned_request = request_id
|
|
149
|
+
client.connect()
|
|
150
|
+
if command == "prepare":
|
|
151
|
+
result = prepare(store, client, values["--input"], values.get("--seconds"))
|
|
152
|
+
owned_request = result.get("requestId")
|
|
153
|
+
if interrupted:
|
|
154
|
+
raise ValueError("确认准备已中断")
|
|
155
|
+
elif command == "claim":
|
|
156
|
+
result = claim(store, client, request_id)
|
|
157
|
+
elif command == "result":
|
|
158
|
+
result = record_result(store, client, request_id, values["--receipt"], values["--state"])
|
|
159
|
+
else:
|
|
160
|
+
result = wait(store, client, request_id, lambda: bool(interrupted), _output)
|
|
161
|
+
_output({"event": "finished", **status(store), "workerResult": result["state"],
|
|
162
|
+
**({"notification": wake_message(request_id, platform)} if result["state"] == "queued" else {})})
|
|
163
|
+
return 130 if interrupted else 1 if result["state"] in ("failed", "delivery_unknown") else 0
|
|
164
|
+
_output(result)
|
|
165
|
+
return 0
|
|
166
|
+
except (ValueError, OSError, KeyError, TypeError, AttributeError, subprocess.SubprocessError) as error:
|
|
167
|
+
_output({"prepared": False, "claimed": False, "mode": "manual", "reason": str(error)})
|
|
168
|
+
return 130 if interrupted else 1
|
|
169
|
+
finally:
|
|
170
|
+
try:
|
|
171
|
+
if interrupted and store and owned_request:
|
|
172
|
+
from flower_confirmation_runtime.runtime import cancel
|
|
173
|
+
cancel(store, client, owned_request)
|
|
174
|
+
finally:
|
|
175
|
+
if client:
|
|
176
|
+
client.close()
|
|
177
|
+
for number, handler in previous_handlers.items():
|
|
178
|
+
signal.signal(number, handler)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
if __name__ == "__main__":
|
|
182
|
+
sys.exit(main())
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""将两平台原生事件交给项目内 Python 确认运行时。"""
|
|
3
3
|
|
|
4
4
|
import json
|
|
5
|
-
import shutil
|
|
6
5
|
import subprocess
|
|
7
6
|
import sys
|
|
8
7
|
from pathlib import Path
|
|
9
8
|
|
|
10
9
|
|
|
11
10
|
def resolve_confirmation_entry(root):
|
|
12
|
-
"""
|
|
11
|
+
"""返回项目内 Python 入口,复用当前解释器。
|
|
13
12
|
|
|
14
13
|
@param root: 已解析的项目根目录。
|
|
15
14
|
@return: 精确 argv;入口缺失时抛出异常。
|
|
16
15
|
"""
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
executable
|
|
21
|
-
if not executable:
|
|
22
|
-
raise ValueError("未找到项目所需的 Node/Flower 入口,保持人工确认")
|
|
23
|
-
return [executable] + ([str(root / "bin/flower-trellis.js")] if local else [])
|
|
16
|
+
entry = root / ".trellis/scripts/flower_confirmation.py"
|
|
17
|
+
if entry.is_symlink() or not entry.is_file():
|
|
18
|
+
raise ValueError("项目内 Python 确认入口缺失,保持人工确认")
|
|
19
|
+
return [sys.executable, "-X", "utf8", str(entry)]
|
|
24
20
|
|
|
25
21
|
|
|
26
22
|
def main():
|
|
@@ -47,7 +43,7 @@ def main():
|
|
|
47
43
|
root = Path(payload["cwd"]).resolve(strict=True)
|
|
48
44
|
command = resolve_confirmation_entry(root)
|
|
49
45
|
platform = "codex" if "--platform" in sys.argv and sys.argv[-1] == "codex" else "claude"
|
|
50
|
-
result = subprocess.run(command + ["
|
|
46
|
+
result = subprocess.run(command + ["hook", "--platform", platform], input=raw, stdout=subprocess.PIPE,
|
|
51
47
|
stderr=subprocess.PIPE, cwd=str(root), timeout=4)
|
|
52
48
|
if result.returncode == 0:
|
|
53
49
|
value = json.loads(result.stdout.decode("utf-8"))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""随项目分发的原生确认运行时,仅依赖 Python 标准库。"""
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""Claude 原生事件、精确后台任务与 transcript 来源校验。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
import shlex
|
|
8
|
+
import stat
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from uuid import uuid4
|
|
12
|
+
|
|
13
|
+
from .store import ACTIVE, ConfirmationStore, confirmation_id, now_ms, wake_message
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _task_notice(text):
|
|
17
|
+
if not isinstance(text, str) or not text.startswith("<task-notification>\n") or not text.endswith("\n</task-notification>"):
|
|
18
|
+
return None
|
|
19
|
+
matches = re.findall(r"<task-id>([A-Za-z0-9_-]{1,100})</task-id>", text)
|
|
20
|
+
return matches[0] if len(matches) == 1 else None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _wait_command(command, project, session_id, request_id):
|
|
24
|
+
if not isinstance(command, str):
|
|
25
|
+
return False
|
|
26
|
+
try:
|
|
27
|
+
tokens = shlex.split(command)
|
|
28
|
+
entry = next(index for index, token in enumerate(tokens)
|
|
29
|
+
if Path(token).name == "flower_confirmation.py")
|
|
30
|
+
executable = Path(tokens[0]).name
|
|
31
|
+
if not re.fullmatch(r"(?:python(?:[0-9]+(?:\.[0-9]+)*)?|py)", executable):
|
|
32
|
+
return False
|
|
33
|
+
prefix = tokens[1:entry]
|
|
34
|
+
if prefix not in ([], ["-X", "utf8"], ["-3"], ["-3", "-X", "utf8"]):
|
|
35
|
+
return False
|
|
36
|
+
if (project / tokens[entry]).resolve(strict=True) != project / ".trellis/scripts/flower_confirmation.py":
|
|
37
|
+
return False
|
|
38
|
+
if tokens[entry + 1] != "wait":
|
|
39
|
+
return False
|
|
40
|
+
flags = tokens[entry + 2:]
|
|
41
|
+
if len(flags) % 2:
|
|
42
|
+
return False
|
|
43
|
+
values = {}
|
|
44
|
+
for index in range(0, len(flags), 2):
|
|
45
|
+
key, value = flags[index:index + 2]
|
|
46
|
+
if key not in ("--request", "--platform", "--session", "--target") or key in values:
|
|
47
|
+
return False
|
|
48
|
+
values[key] = value
|
|
49
|
+
return (values.get("--request") == request_id and values.get("--platform", "claude") == "claude"
|
|
50
|
+
and values.get("--session", session_id) == session_id
|
|
51
|
+
and (project / values.get("--target", ".")).resolve(strict=True) == project)
|
|
52
|
+
except (ValueError, OSError, StopIteration, IndexError):
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def record_event(payload):
|
|
57
|
+
"""保存四类原生事件,内部通知在领取时再次核验来源。
|
|
58
|
+
|
|
59
|
+
@param payload: 原始 Claude Hook JSON。
|
|
60
|
+
@return: 空输出或本请求的原生唤醒上下文。
|
|
61
|
+
"""
|
|
62
|
+
event = payload.get("hook_event_name")
|
|
63
|
+
if event not in ("SessionStart", "UserPromptSubmit", "Stop", "SessionEnd"):
|
|
64
|
+
return {}
|
|
65
|
+
project = Path(payload["cwd"]).resolve(strict=True)
|
|
66
|
+
if not (project / ".trellis/scripts/confirmation_policy.py").is_file():
|
|
67
|
+
return {}
|
|
68
|
+
session_id = confirmation_id(payload["session_id"])
|
|
69
|
+
transcript = payload.get("transcript_path")
|
|
70
|
+
if not isinstance(transcript, str) or Path(transcript).name != session_id + ".jsonl":
|
|
71
|
+
raise ValueError("Claude 缺少精确会话记录路径")
|
|
72
|
+
store = ConfirmationStore(project, session_id, "claude")
|
|
73
|
+
reminders = []
|
|
74
|
+
def apply(old):
|
|
75
|
+
value = old or {"version": 2, "sessionId": session_id, "generation": 0, "lastHumanMessageId": None, "request": None}
|
|
76
|
+
native = value.get("native") or {"version": 1, "project": str(project), "sessionId": session_id,
|
|
77
|
+
"tasks": {}, "runId": str(uuid4())}
|
|
78
|
+
if native.get("project") != str(project) or native.get("sessionId") != session_id:
|
|
79
|
+
raise ValueError("Claude 事件会话或项目不符")
|
|
80
|
+
native.update(transcript=transcript, at=now_ms())
|
|
81
|
+
request = value.get("request")
|
|
82
|
+
if event == "SessionStart":
|
|
83
|
+
native.update(runId=str(uuid4()), ended=False, tasks={}, humanId=None, turn=None)
|
|
84
|
+
value["generation"] += 1
|
|
85
|
+
if request and request["state"] in ACTIVE:
|
|
86
|
+
request.update(state="cancelled", error="session_restarted")
|
|
87
|
+
elif event == "UserPromptSubmit":
|
|
88
|
+
prompt_id = confirmation_id(payload.get("prompt_id"))
|
|
89
|
+
task_id = _task_notice(payload.get("prompt"))
|
|
90
|
+
owned = native.get("tasks", {}).get(task_id)
|
|
91
|
+
native.update(turn={"id": prompt_id, "status": "inProgress"}, ended=False)
|
|
92
|
+
if owned:
|
|
93
|
+
native["notification"] = {"taskId": task_id, "requestId": owned, "promptId": prompt_id}
|
|
94
|
+
reminders.append(wake_message(owned, "claude"))
|
|
95
|
+
elif native.get("humanId") != prompt_id:
|
|
96
|
+
native["humanId"] = prompt_id
|
|
97
|
+
value["lastHumanMessageId"] = prompt_id
|
|
98
|
+
value["generation"] += 1
|
|
99
|
+
if request and request["state"] in ACTIVE:
|
|
100
|
+
request.update(state="cancelled", error="user_replied")
|
|
101
|
+
elif event == "Stop":
|
|
102
|
+
prompt_id = confirmation_id(payload.get("prompt_id"))
|
|
103
|
+
text = payload.get("last_assistant_message")
|
|
104
|
+
if not isinstance(text, str) or len(text.encode("utf-8")) > 512 * 1024:
|
|
105
|
+
raise ValueError("Claude 最终展示正文不可核验")
|
|
106
|
+
native["turn"] = {"id": prompt_id, "status": "completed"}
|
|
107
|
+
native["display"] = {"turnId": prompt_id, "text": text, "at": native["at"]}
|
|
108
|
+
for task in payload.get("background_tasks", []):
|
|
109
|
+
if (not request or task.get("type") != "shell"
|
|
110
|
+
or not _wait_command(task.get("command"), project, session_id, request["id"])):
|
|
111
|
+
continue
|
|
112
|
+
task_id = task.get("id")
|
|
113
|
+
if not isinstance(task_id, str) or not re.fullmatch(r"[A-Za-z0-9_-]{1,100}", task_id):
|
|
114
|
+
raise ValueError("Claude 原生任务 ID 无效")
|
|
115
|
+
native["tasks"][task_id] = request["id"]
|
|
116
|
+
request["nativeTaskId"] = task_id
|
|
117
|
+
for identifier in list(native["tasks"])[:-8]:
|
|
118
|
+
del native["tasks"][identifier]
|
|
119
|
+
else:
|
|
120
|
+
native["ended"] = True
|
|
121
|
+
value["generation"] += 1
|
|
122
|
+
if request and request["state"] in ACTIVE:
|
|
123
|
+
request.update(state="cancelled", error="session_ended")
|
|
124
|
+
value["native"] = native
|
|
125
|
+
return value
|
|
126
|
+
store.update(apply)
|
|
127
|
+
return {"hookSpecificOutput": {"hookEventName": "UserPromptSubmit", "additionalContext": reminders[-1]}} if reminders else {}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class ClaudeClient:
|
|
131
|
+
"""观察精确原生 Hook 状态与会话记录。"""
|
|
132
|
+
|
|
133
|
+
delivery_on_exit = True
|
|
134
|
+
|
|
135
|
+
def __init__(self, project, session_id, platform=None):
|
|
136
|
+
"""创建精确会话适配器。
|
|
137
|
+
|
|
138
|
+
@param project: 项目根。
|
|
139
|
+
@param session_id: 原生会话 UUID。
|
|
140
|
+
@param platform: 可注入的平台能力条件。
|
|
141
|
+
@return: None。
|
|
142
|
+
"""
|
|
143
|
+
if (platform or sys.platform) != "linux":
|
|
144
|
+
raise ValueError("Claude 后台确认目前只验证 Linux/WSL")
|
|
145
|
+
self.store = ConfirmationStore(project, session_id, "claude")
|
|
146
|
+
self.project = self.store.project
|
|
147
|
+
self.session_id = self.store.session_id
|
|
148
|
+
|
|
149
|
+
def connect(self):
|
|
150
|
+
"""核对原生 Hook 和活动 runId。
|
|
151
|
+
|
|
152
|
+
@return: None。
|
|
153
|
+
"""
|
|
154
|
+
native = (self.store.read() or {}).get("native") or {}
|
|
155
|
+
if (native.get("version") != 1 or native.get("project") != str(self.project)
|
|
156
|
+
or native.get("sessionId") != self.session_id or native.get("ended")):
|
|
157
|
+
raise ValueError("Claude 原生确认 Hook 不可用或会话已退出,保持人工确认")
|
|
158
|
+
confirmation_id(native.get("runId"))
|
|
159
|
+
self.snapshot()
|
|
160
|
+
|
|
161
|
+
def snapshot(self):
|
|
162
|
+
"""核验有界 transcript 的真实人工和任务通知来源。
|
|
163
|
+
|
|
164
|
+
@return: 人工基线、当前回合、通知和 runId。
|
|
165
|
+
"""
|
|
166
|
+
native = (self.store.read() or {}).get("native") or {}
|
|
167
|
+
if not native or native.get("ended"):
|
|
168
|
+
raise ValueError("Claude 会话状态不可用")
|
|
169
|
+
file = Path(native["transcript"])
|
|
170
|
+
info = file.lstat()
|
|
171
|
+
if not stat.S_ISREG(info.st_mode) or info.st_size > 16 * 1024 * 1024:
|
|
172
|
+
raise ValueError("Claude 会话记录不可核验或超过预算")
|
|
173
|
+
with file.open("rb") as stream:
|
|
174
|
+
raw = stream.read(16 * 1024 * 1024 + 1)
|
|
175
|
+
if len(raw) > 16 * 1024 * 1024:
|
|
176
|
+
raise ValueError("Claude 会话记录超过预算")
|
|
177
|
+
human_id = None
|
|
178
|
+
notification = None
|
|
179
|
+
for line in raw.split(b"\n")[:-1]:
|
|
180
|
+
if not line.strip():
|
|
181
|
+
continue
|
|
182
|
+
entry = json.loads(line.decode("utf-8"))
|
|
183
|
+
if entry.get("type") != "user" or entry.get("isSidechain") or entry.get("sessionId") != self.session_id:
|
|
184
|
+
continue
|
|
185
|
+
if (entry.get("origin") or {}).get("kind") == "human" and entry.get("turnOrigin") == "human":
|
|
186
|
+
human_id = confirmation_id(entry.get("promptId"))
|
|
187
|
+
elif (entry.get("origin") or {}).get("kind") == "task-notification" and entry.get("turnOrigin") == "task_notification":
|
|
188
|
+
task_id = _task_notice((entry.get("message") or {}).get("content"))
|
|
189
|
+
if task_id and native["tasks"].get(task_id):
|
|
190
|
+
notification = {"taskId": task_id, "requestId": native["tasks"][task_id], "promptId": entry.get("promptId")}
|
|
191
|
+
elif isinstance((entry.get("message") or {}).get("content"), str) and not entry.get("isCompactSummary"):
|
|
192
|
+
raise ValueError("Claude 消息来源字段不可核验,保持人工确认")
|
|
193
|
+
if not human_id:
|
|
194
|
+
raise ValueError("Claude 缺少真实人工输入基线;关闭记录的会话保持人工确认")
|
|
195
|
+
hook_notice = native.get("notification") or {}
|
|
196
|
+
pending = (hook_notice.get("promptId") == (native.get("turn") or {}).get("id")
|
|
197
|
+
and hook_notice.get("requestId") == native.get("tasks", {}).get(hook_notice.get("taskId"))
|
|
198
|
+
and bool(hook_notice.get("requestId"))
|
|
199
|
+
and (notification or {}).get("promptId") != hook_notice.get("promptId"))
|
|
200
|
+
return {"humanId": human_id, "turn": native.get("turn"), "notification": notification,
|
|
201
|
+
"notificationPending": pending, "runId": native.get("runId")}
|
|
202
|
+
|
|
203
|
+
def assistant_text(self, turn_id):
|
|
204
|
+
"""读取 Stop 绑定的原始展示。
|
|
205
|
+
|
|
206
|
+
@param turn_id: 原回合标识。
|
|
207
|
+
@return: 完整最终正文。
|
|
208
|
+
"""
|
|
209
|
+
display = ((self.store.read() or {}).get("native") or {}).get("display") or {}
|
|
210
|
+
if display.get("turnId") != turn_id:
|
|
211
|
+
raise ValueError("Claude 最终展示回合不符")
|
|
212
|
+
return display["text"]
|
|
213
|
+
|
|
214
|
+
def add(self, request_id):
|
|
215
|
+
"""返回 Stop 已绑定的本次后台任务。
|
|
216
|
+
|
|
217
|
+
@param request_id: 请求 UUID。
|
|
218
|
+
@return: 原生任务标识;worker 随后退出以触发通知。
|
|
219
|
+
"""
|
|
220
|
+
request = (self.store.read() or {}).get("request") or {}
|
|
221
|
+
if request.get("id") != request_id or not request.get("nativeTaskId"):
|
|
222
|
+
raise ValueError("Claude 未绑定本次原生后台任务")
|
|
223
|
+
return {"id": request["nativeTaskId"]}
|
|
224
|
+
|
|
225
|
+
def remove(self, queue_id):
|
|
226
|
+
"""报告 Claude 原生完成通知的撤回边界。
|
|
227
|
+
|
|
228
|
+
@param queue_id: 精确任务 ID。
|
|
229
|
+
@return: not_retractable 或 not_pending。
|
|
230
|
+
"""
|
|
231
|
+
return "not_retractable" if queue_id else "not_pending"
|
|
232
|
+
|
|
233
|
+
def close(self):
|
|
234
|
+
"""结束本客户端的本次观察。
|
|
235
|
+
|
|
236
|
+
@return: None。
|
|
237
|
+
"""
|
|
238
|
+
return None
|