patchwarden 0.6.1 → 0.6.4

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 (65) hide show
  1. package/PatchWarden-Control-Tray.cmd +11 -0
  2. package/PatchWarden-Control.cmd +6 -0
  3. package/PatchWarden-Desktop.cmd +5 -0
  4. package/PatchWarden.cmd +1 -1
  5. package/README.en.md +106 -18
  6. package/README.md +30 -19
  7. package/Restart-PatchWarden-Control.cmd +6 -0
  8. package/Stop-PatchWarden.cmd +11 -0
  9. package/dist/controlCenter.d.ts +14 -0
  10. package/dist/controlCenter.js +2002 -0
  11. package/dist/doctor.js +3 -3
  12. package/dist/version.d.ts +1 -1
  13. package/dist/version.js +1 -1
  14. package/docs/control-center/README.md +33 -0
  15. package/docs/control-center/control-center-daily-driver.md +211 -0
  16. package/docs/control-center/control-center-mvp.md +205 -0
  17. package/docs/control-center/control-center-phase2.md +159 -0
  18. package/docs/demo.md +3 -0
  19. package/docs/release-v0.6.4.md +45 -0
  20. package/examples/openai-tunnel/README.md +5 -5
  21. package/examples/openai-tunnel/tunnel-client.example.yaml +3 -3
  22. package/package.json +23 -15
  23. package/scripts/README.md +47 -0
  24. package/scripts/{brand-check.js → checks/brand-check.js} +2 -2
  25. package/scripts/checks/control-center-smoke.js +1098 -0
  26. package/scripts/{control-smoke.js → checks/control-smoke.js} +17 -3
  27. package/scripts/{doctor-smoke.js → checks/doctor-smoke.js} +1 -1
  28. package/scripts/{http-mcp-smoke.js → checks/http-mcp-smoke.js} +2 -2
  29. package/scripts/{lifecycle-smoke.js → checks/lifecycle-smoke.js} +12 -12
  30. package/scripts/{mcp-manifest-check.js → checks/mcp-manifest-check.js} +2 -2
  31. package/scripts/{mcp-smoke.js → checks/mcp-smoke.js} +4 -2
  32. package/scripts/{package-manifest-check.js → checks/package-manifest-check.js} +2 -1
  33. package/scripts/{tunnel-supervisor-smoke.js → checks/tunnel-supervisor-smoke.js} +2 -2
  34. package/scripts/{unit-tests.js → checks/unit-tests.js} +1 -1
  35. package/scripts/{watcher-supervisor-smoke.js → checks/watcher-supervisor-smoke.js} +11 -5
  36. package/scripts/control/control-center-tray.ps1 +281 -0
  37. package/scripts/{get-patchwarden-health.ps1 → control/get-patchwarden-health.ps1} +3 -3
  38. package/scripts/{manage-patchwarden.ps1 → control/manage-patchwarden.ps1} +30 -4
  39. package/scripts/control/restart-control-center.ps1 +173 -0
  40. package/scripts/{restart-patchwarden.ps1 → control/restart-patchwarden.ps1} +1 -1
  41. package/scripts/control/start-control-center.ps1 +263 -0
  42. package/scripts/{start-patchwarden-tunnel.ps1 → control/start-patchwarden-tunnel.ps1} +48 -6
  43. package/scripts/control/stop-patchwarden.ps1 +114 -0
  44. package/scripts/launchers/Check-PatchWarden-Health.cmd +1 -1
  45. package/scripts/launchers/Reset-PatchWarden-Tunnel-Key.cmd +1 -1
  46. package/scripts/launchers/Restart-PatchWarden.cmd +1 -1
  47. package/scripts/launchers/Start-PatchWarden-Direct-Tunnel.cmd +1 -1
  48. package/scripts/launchers/Start-PatchWarden-Tunnel.cmd +1 -1
  49. package/scripts/{patchwarden-mcp-direct.cmd → mcp/patchwarden-mcp-direct.cmd} +1 -1
  50. package/scripts/{patchwarden-mcp-stdio.cmd → mcp/patchwarden-mcp-stdio.cmd} +1 -1
  51. package/scripts/{pack-clean.js → release/pack-clean.js} +9 -1
  52. package/src/controlCenter.ts +2152 -0
  53. package/src/doctor.ts +4 -4
  54. package/src/version.ts +1 -1
  55. package/ui/colors_and_type.css +141 -0
  56. package/ui/pages/audit.html +743 -0
  57. package/ui/pages/dashboard.html +1154 -0
  58. package/ui/pages/direct-sessions.html +652 -0
  59. package/ui/pages/logs.html +502 -0
  60. package/ui/pages/task-detail.html +1229 -0
  61. package/ui/pages/tasks.html +702 -0
  62. package/ui/pages/workspace.html +947 -0
  63. package/ui/partials/project-shell.html +362 -0
  64. package/ui/vendor/lucide.js +12 -0
  65. package/ui/vendor/tailwindcss-browser.js +947 -0
package/dist/doctor.js CHANGED
@@ -210,7 +210,7 @@ async function main() {
210
210
  check("Read-only task artifact allowlist", requiredReadOnlyFiles.every((name) => TASK_READ_ONLY_FILES.includes(name)), requiredReadOnlyFiles.join(", "));
211
211
  const packageJson = JSON.parse(readFileSync(resolve(process.cwd(), "package.json"), "utf-8"));
212
212
  check("Server version matches package.json", packageJson.version === PATCHWARDEN_VERSION, `${PATCHWARDEN_VERSION} vs ${packageJson.version}`);
213
- check("Manifest preflight script exists", existsSync(resolve(process.cwd(), "scripts/mcp-manifest-check.js")), "scripts/mcp-manifest-check.js");
213
+ check("Manifest preflight script exists", existsSync(resolve(process.cwd(), "scripts/checks/mcp-manifest-check.js")), "scripts/checks/mcp-manifest-check.js");
214
214
  const previousProfile = process.env.PATCHWARDEN_TOOL_PROFILE;
215
215
  try {
216
216
  process.env.PATCHWARDEN_TOOL_PROFILE = "full";
@@ -403,9 +403,9 @@ async function main() {
403
403
  "examples/openai-tunnel/README.md",
404
404
  "examples/openai-tunnel/tunnel-client.example.yaml",
405
405
  "examples/openai-tunnel/chatgpt-test-prompt.md",
406
- "scripts/patchwarden-mcp-direct.cmd",
406
+ "scripts/mcp/patchwarden-mcp-direct.cmd",
407
407
  "PatchWarden.cmd",
408
- "scripts/manage-patchwarden.ps1",
408
+ "scripts/control/manage-patchwarden.ps1",
409
409
  "scripts/launchers/Start-PatchWarden-Direct-Tunnel.cmd",
410
410
  ];
411
411
  for (const tf of tunnelFiles) {
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const PATCHWARDEN_VERSION = "0.6.1";
1
+ export declare const PATCHWARDEN_VERSION = "0.6.4";
2
2
  export declare const TOOL_SCHEMA_EPOCH = "2026-06-24-v7";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export const PATCHWARDEN_VERSION = "0.6.1";
1
+ export const PATCHWARDEN_VERSION = "0.6.4";
2
2
  export const TOOL_SCHEMA_EPOCH = "2026-06-24-v7";
@@ -0,0 +1,33 @@
1
+ # PatchWarden Control Center
2
+
3
+ PatchWarden has three control layers:
4
+
5
+ 1. Web dashboard: the daily management surface for status, setup checks, tasks,
6
+ stale-task actions, Direct sessions, audit logs, and long logs.
7
+ 2. Tray entry: a lightweight quick-control surface for opening the dashboard,
8
+ checking compact status, Start/Stop/Restart, opening logs, and quitting the
9
+ tray.
10
+ 3. CLI/scripts: the lower-level fallback for automation, smoke tests, package
11
+ checks, and troubleshooting.
12
+
13
+ ## User Entrypoints
14
+
15
+ From the repository root:
16
+
17
+ ```powershell
18
+ .\PatchWarden-Desktop.cmd
19
+ .\PatchWarden-Control.cmd
20
+ .\PatchWarden-Control-Tray.cmd --foreground
21
+ .\PatchWarden.cmd status all
22
+ ```
23
+
24
+ Use `PatchWarden-Control.cmd` for normal desktop use. Use the tray when you only
25
+ need quick controls. Use `PatchWarden.cmd` when you need explicit CLI output or
26
+ automation-friendly commands.
27
+
28
+ ## Design Notes
29
+
30
+ - `control-center-mvp.md`: first Web dashboard scope.
31
+ - `control-center-phase2.md`: follow-up management and diagnostics scope.
32
+ - `control-center-daily-driver.md`: current daily-use contract.
33
+
@@ -0,0 +1,211 @@
1
+ # PatchWarden Control Center Daily Driver
2
+
3
+ > v0.6.4 在 v0.6.2 MVP 与 v0.6.3 Phase 2 基础上,把 Control Center 从「启动页面脚本」
4
+ > 升级为日常入口:单实例启动、后台运行、托盘入口、健康修复建议、页面通知和活动时间线。
5
+ > 不重构架构,不合并 Core/Direct MCP,不改现有 MCP 工具注册逻辑。
6
+ > 不发布 npm,不打 git tag。
7
+
8
+ ## 概述
9
+
10
+ Daily Driver 完全在 Control Center HTTP 层与启动脚本层增强,未触动 MCP 工具注册、
11
+ watcher 核心循环或 Direct session 写入逻辑。新增的 API 沿用既有 token 校验、路径围栏
12
+ 和脱敏机制;托盘与后台模式仅调用现有 Control Center API,不引入新的进程管理路径。
13
+
14
+ - **单实例启动**:status 文件 + 端口探测,重复运行只开浏览器,不启第二个服务
15
+ - **后台运行**:`-Background`(默认)/ `-Foreground` / `-NoBrowser`,日志写入
16
+ `%LOCALAPPDATA%\patchwarden\control-center\control-center.{stdout,stderr}.log`
17
+ - **托盘入口**:基于 .NET NotifyIcon 的轻量 PowerShell 托盘,无需 Electron
18
+ - **健康修复建议**:`/api/status` 新增 `suggestions` 字段,dashboard 显示建议卡片
19
+ - **页面通知**:dashboard 轮询状态变化时弹出 toast,同一事件只提示一次
20
+ - **Activity Timeline**:`runtime/control-center-events.jsonl` 记录关键事件,
21
+ `GET /api/events` 返回最近事件,dashboard 显示时间线区块
22
+
23
+ ## 启动方式
24
+
25
+ ### 1. 日常入口(推荐)
26
+
27
+ ```powershell
28
+ .\PatchWarden-Control.cmd
29
+ ```
30
+
31
+ 默认后台启动 Control Center 并打开浏览器。再次运行时:
32
+
33
+ - 如果 127.0.0.1:8090 已经是当前 Control Center —— 直接打开浏览器页面,不重复启动
34
+ - 如果端口被其他程序占用 —— 给出明确错误提示并退出
35
+ - 如果 status 文件指向已死的 PID —— 清理孤儿进程后启动新实例
36
+
37
+ ### 2. 启动参数
38
+
39
+ ```powershell
40
+ .\PatchWarden-Control.cmd -NoBrowser # 启动但不打开浏览器
41
+ .\PatchWarden-Control.cmd -Foreground # 前台运行,日志直接输出到控制台
42
+ .\PatchWarden-Control.cmd -Background # 后台运行(默认),脚本启动后即退出
43
+ ```
44
+
45
+ `-Foreground` 与 `-Background` 互斥;都不传时默认 `-Background`。
46
+
47
+ ### 3. 托盘入口
48
+
49
+ ```powershell
50
+ .\PatchWarden-Control-Tray.cmd
51
+ ```
52
+
53
+ 启动 Windows 系统托盘图标(基于 .NET NotifyIcon,无需额外依赖)。如果 Control Center
54
+ 未运行,托盘脚本会先在后台启动它。托盘菜单:
55
+
56
+ | 菜单项 | 行为 |
57
+ | --- | --- |
58
+ | Open Control Center | 在默认浏览器打开 dashboard |
59
+ | Start All | `POST /api/start-all`(带 token) |
60
+ | Stop All | `POST /api/stop-all`(带 token) |
61
+ | Restart All | `POST /api/restart-all`(带 token) |
62
+ | Open Workspace | 通过 `GET /api/workspace` 获取 workspaceRoot 后用 explorer 打开 |
63
+ | Open Logs Folder | `POST /api/open-logs-folder`(带 token) |
64
+ | Exit | 关闭托盘(不停止 Control Center 后台进程) |
65
+
66
+ 双击托盘图标打开 dashboard。所有 API 调用通过 `/control-token.json` 获取 token 后
67
+ 带 `X-PatchWarden-Control-Token` 头发起请求。
68
+
69
+ ## 改动文件
70
+
71
+ | 文件 | 改动 |
72
+ | --- | --- |
73
+ | `src/version.ts` | `PATCHWARDEN_VERSION` 0.6.3 → 0.6.4 |
74
+ | `package.json` | `version` 0.6.3 → 0.6.4 |
75
+ | `src/controlCenter.ts` | 新增 status 文件、events JSONL、suggestions 引擎、`/api/events`、`/api/control-center-status`;`getControlCenterLogDir` 支持 `PATCHWARDEN_CONTROL_LOG_DIR` 覆盖 |
76
+ | `scripts/control/start-control-center.ps1` | 重写:单实例检测、`-NoBrowser`/`-Foreground`/`-Background`、孤儿清理、端口冲突检测 |
77
+ | `scripts/control/control-center-tray.ps1` | 新文件:.NET NotifyIcon 托盘,菜单驱动 Control Center API |
78
+ | `PatchWarden-Control-Tray.cmd` | 新文件:托盘启动入口 |
79
+ | `ui/pages/dashboard.html` | 新增 Health Suggestions 卡片、toast 通知、Activity Timeline 区块 |
80
+ | `scripts/checks/control-center-smoke.js` | 新增 Test 16–20 覆盖 Daily Driver API;`PATCHWARDEN_CONTROL_LOG_DIR` 重定向到项目内临时目录 |
81
+ | `scripts/release/pack-clean.js` | forbidden 列表新增 `.tmp/` |
82
+ | `.gitignore` | 新增 `.tmp/` |
83
+ | `docs/control-center/control-center-daily-driver.md` | 本文档 |
84
+
85
+ ## 新增 API
86
+
87
+ ### GET 接口(免 token)
88
+
89
+ | 路径 | 说明 |
90
+ | --- | --- |
91
+ | `GET /api/events?limit=<N>` | 返回最近 N 条活动事件(最大 1000),结构 `{events, total, limit}` |
92
+ | `GET /api/control-center-status` | 读取 status 文件,返回 `{running, pid, port, started_at, url, version}` 或 `{running: false}` |
93
+
94
+ ### /api/status 增强字段
95
+
96
+ `GET /api/status` 响应新增 `suggestions` 数组,每条建议包含:
97
+
98
+ ```json
99
+ {
100
+ "code": "core_stopped",
101
+ "severity": "warning",
102
+ "message": "Core 未运行,建议启动 Core profile",
103
+ "action": "/api/core/start",
104
+ "link": null
105
+ }
106
+ ```
107
+
108
+ | code | severity | 触发条件 | action |
109
+ | --- | --- | --- | --- |
110
+ | `core_stopped` | warning | Core 探测不可用 | `/api/core/start` |
111
+ | `direct_stopped` | warning | Direct 探测不可用 | `/api/direct/start` |
112
+ | `watcher_stale` | error | watcher 状态为 stale/unreadable | `/api/restart-all` |
113
+ | `stale_task` | warning | 存在 stale 任务 | 无 action,`link` 指向 tasks 页面 |
114
+ | `tunnel_not_ready` | warning | Core 或 Direct tunnel 未就绪 | `/api/restart-all` |
115
+ | `agent_missing` | info | 配置的 agent 不可用 | 无 action,提示检查路径 |
116
+
117
+ ## 活动事件
118
+
119
+ 事件写入 `%LOCALAPPDATA%\patchwarden\control-center\control-center-events.jsonl`,
120
+ 每行一个 JSON 对象 `{timestamp, type, payload?}`。文件超过 512KB 时惰性裁剪到
121
+ 2000 行。
122
+
123
+ ### 事件类型
124
+
125
+ | type | 触发时机 |
126
+ | --- | --- |
127
+ | `control_center.started` | 服务 listen 成功 |
128
+ | `control_center.stopped` | 服务收到 SIGTERM/SIGINT 关闭 |
129
+ | `manage.start.all` / `manage.stop.all` / `manage.restart.all` | 用户请求 start/stop/restart all |
130
+ | `manage.<mode>.<action>.failed` | manage-patchwarden.ps1 执行失败 |
131
+ | `core.status_changed` | Core 可用性发生变化(diff 自上次 /api/status 轮询) |
132
+ | `direct.status_changed` | Direct 可用性发生变化 |
133
+ | `watcher.status_changed` | watcher 状态发生变化 |
134
+ | `task.status_changed` | 任务状态从 running → done/failed 等 |
135
+ | `task.reconciled` | 用户对 stale 任务执行 reconcile |
136
+ | `task.audited` | 用户对任务执行 audit |
137
+
138
+ > Control Center 是无状态、拉取驱动的服务。`core/direct/watcher/task.status_changed`
139
+ > 事件在 `/api/status` 轮询时通过 digest diff 检测,因此事件时间戳反映的是「被观察到」
140
+ > 的时间,不保证是状态变化的精确时刻。
141
+
142
+ ## 页面通知(toast)
143
+
144
+ dashboard.html 轮询 `/api/status`(30s)和 `/api/audit`(90s)时检测以下状态变化并
145
+ 弹出 toast 通知:
146
+
147
+ - core/direct: available → unavailable(error)/ unavailable → available(success)
148
+ - watcher: healthy → stale/unreadable(error)/ stale → healthy(success)
149
+ - task: running → done(success)/ running → failed*(error)
150
+ - audit: 新出现的 pass(success)/ warn(warning)/ fail(error)
151
+
152
+ 同一事件通过 dedup key 只提示一次;toast 8 秒后自动消失,也可手动关闭。首次加载时
153
+ audit 列表会 seed 到 `seenAuditKeys`,不会对已有审计记录弹通知。
154
+
155
+ ## 运行时文件
156
+
157
+ | 文件 | 路径 | 说明 |
158
+ | --- | --- | --- |
159
+ | status 文件 | `%LOCALAPPDATA%\patchwarden\control-center\control-center-status.json` | pid/port/started_at/url/version,服务启动时写入,关闭时删除 |
160
+ | events 文件 | `%LOCALAPPDATA%\patchwarden\control-center\control-center-events.jsonl` | 活动事件 JSONL |
161
+ | stdout 日志 | `%LOCALAPPDATA%\patchwarden\control-center\control-center.stdout.log` | 后台模式 stdout |
162
+ | stderr 日志 | `%LOCALAPPDATA%\patchwarden\control-center\control-center.stderr.log` | 后台模式 stderr |
163
+
164
+ > `PATCHWARDEN_CONTROL_LOG_DIR` 环境变量可覆盖上述目录(必须为绝对路径),
165
+ > 主要用于 smoke test 将其重定向到项目内临时目录。
166
+
167
+ ## 测试
168
+
169
+ ```powershell
170
+ npm.cmd test
171
+ ```
172
+
173
+ ### control-center-smoke.js(21 项)
174
+
175
+ | # | 测试 | 覆盖 |
176
+ | --- | --- | --- |
177
+ | 1–1b | 静态文件 + 页面路由 | 基础服务 |
178
+ | 2 | `/api/status` JSON + 容错 | 结构校验 |
179
+ | 3–4 | tasks JSON + control-token | token 机制 |
180
+ | 5–7 | POST 无 token / 错 token → 403 | 安全门 |
181
+ | 8–9 | token 不污染 git + 无 CDN | 发布卫生 |
182
+ | 10 | 其他 GET API 可达 | workspace/audit/logs/tunnel |
183
+ | 11–15 | Phase 2 API | stale/reconcile/workspace/direct-sessions/logs |
184
+ | **16** | `/api/status` 包含 `suggestions` 数组 | Daily Driver |
185
+ | **17** | `/api/events` 返回 JSON | Daily Driver |
186
+ | **18** | `/api/logs/control-center` 返回 JSON | Daily Driver |
187
+ | **19** | POST `/api/restart-all` 无 token → 403 | Daily Driver |
188
+ | **20** | `/api/control-center-status` 报告 running 实例 | Daily Driver(单实例检测) |
189
+
190
+ ### 全量测试结果
191
+
192
+ ```
193
+ security tests: 139 passed, 0 failed
194
+ unit tests: 136 passed, 0 failed (1 skipped: Windows symlink)
195
+ lifecycle tests: 22 passed, 0 failed
196
+ control-center: 21 passed, 0 failed
197
+ ```
198
+
199
+ ## 已知限制
200
+
201
+ 1. **托盘为 PowerShell 实现**:基于 .NET NotifyIcon,不提供跨平台支持;在非 Windows
202
+ 环境下不可用。如需跨平台托盘,需引入 Electron 或类似框架(本阶段刻意避免)。
203
+ 2. **事件时间为观测时间**:Control Center 无状态,`status_changed` 事件在轮询时通过
204
+ diff 检测,时间戳是「被观察到」的时间,不保证是状态变化的精确时刻。
205
+ 3. **status 文件依赖可写目录**:如果 `%LOCALAPPDATA%` 不可写(如沙箱环境),
206
+ status 文件不会生成,单实例检测会退化为端口探测模式。可通过
207
+ `PATCHWARDEN_CONTROL_LOG_DIR` 覆盖到可写目录。
208
+ 4. **toast 为页面内通知**:不要求系统级通知;仅当 dashboard 页面打开时才会弹出。
209
+ 5. **托盘 Exit 不停止后台进程**:Exit 仅关闭托盘,Control Center 后台 node 进程继续
210
+ 运行。需通过 Stop All 或 `Stop-Process -Id <pid>` 停止。
211
+ 6. **事件文件不轮转**:超过 512KB 时惰性裁剪到 2000 行,不按日期轮转。
@@ -0,0 +1,205 @@
1
+ # PatchWarden Control Center MVP
2
+
3
+ > 本地 Web 控制中心,统一管理 PatchWarden Core 与 PatchWarden Direct 两个 MCP profile。
4
+ > 不合并工具,不发布到 npm,不打 git tag。
5
+
6
+ ## 概述
7
+
8
+ Control Center 是一个绑定 `127.0.0.1:8090` 的本地 HTTP 服务,提供静态 UI 页面和一组容错 JSON API。
9
+ 所有进程生命周期操作(start/stop/restart)均委托 `scripts/control/manage-patchwarden.ps1` 执行,
10
+ Control Center 自身不直接 kill 进程,也不读取任意 PID 文件。
11
+
12
+ - **Core profile**: `chatgpt_core`,17 个工具,HTTP 端口 8080
13
+ - **Direct profile**: `chatgpt_direct`,10 个工具,HTTP 端口 8081
14
+
15
+ ## 新增文件
16
+
17
+ | 文件 | 说明 |
18
+ | --- | --- |
19
+ | `src/controlCenter.ts` | Control Center HTTP 服务主程序(TypeScript 源) |
20
+ | `scripts/checks/control-center-smoke.js` | 测试模式冒烟测试(ESM,仅用 Node 内置模块) |
21
+ | `scripts/control/start-control-center.ps1` | PowerShell 启动脚本(含构建检查、端口轮询、浏览器自动打开;支持 `-NoBrowser`) |
22
+ | `PatchWarden-Control.cmd` | 根目录 CMD 入口,调用上述 PowerShell 脚本 |
23
+ | `ui/pages/dashboard.html` | 总控首页(真实状态,7 个工具栏按钮,两张服务卡) |
24
+ | `ui/pages/tasks.html` | 任务列表页(接 `/api/tasks`) |
25
+ | `ui/pages/task-detail.html` | 任务详情页(接 `/api/tasks/:taskId`) |
26
+ | `ui/pages/workspace.html` | 工作区页(接轻量 `/api/workspace`) |
27
+ | `ui/pages/audit.html` | 审计历史页(接 `/api/audit`) |
28
+ | `ui/partials/project-shell.html` | 页面外壳(CDN 引用已替换为本地 `/vendor/`) |
29
+ | `ui/colors_and_type.css` | 颜色与字体样式 |
30
+ | `ui/vendor/tailwindcss-browser.js` | Tailwind v4.3.1 browser build(本地副本,离线可用) |
31
+ | `ui/vendor/lucide.js` | Lucide v1.8.0 UMD(本地副本,离线可用) |
32
+ | `docs/control-center/control-center-mvp.md` | 本文档 |
33
+
34
+ ## 改动文件
35
+
36
+ | 文件 | 改动 |
37
+ | --- | --- |
38
+ | `package.json` | 新增 `start:control` 脚本(`node dist/controlCenter.js`);`files` 数组新增 `ui/`;`test` 脚本末尾追加 `&& node scripts/checks/control-center-smoke.js` |
39
+ | `.gitignore` | 新增 `/CODE_WIKI.md`、`/PatchWarden-UI.zip`、`/patchwarden-v0.6.1-SHA256SUMS.txt`(避免 brand-check 误报) |
40
+
41
+ ## 启动方式
42
+
43
+ ### 方式一:CMD 入口(推荐用户使用)
44
+
45
+ ```powershell
46
+ .\PatchWarden-Control.cmd
47
+ ```
48
+
49
+ ### 方式二:PowerShell 脚本
50
+
51
+ ```powershell
52
+ # 正常启动(构建检查 + 打开浏览器)
53
+ powershell -ExecutionPolicy Bypass -File scripts\control\start-control-center.ps1
54
+
55
+ # 测试模式(不打开浏览器)
56
+ powershell -ExecutionPolicy Bypass -File scripts\control\start-control-center.ps1 -NoBrowser
57
+ ```
58
+
59
+ ### 方式三:npm 脚本(需先 `npm run build`)
60
+
61
+ ```powershell
62
+ npm.cmd run build
63
+ npm.cmd run start:control
64
+ ```
65
+
66
+ 启动后自动打开 `http://127.0.0.1:8090`。
67
+
68
+ 端口可通过环境变量覆盖(仅用于测试):
69
+
70
+ ```powershell
71
+ $env:PATCHWARDEN_CONTROL_PORT=18090; node dist/controlCenter.js
72
+ ```
73
+
74
+ ## 控制 Token 校验
75
+
76
+ - 服务启动时通过 `crypto.randomUUID()` 生成 token,**仅保存在内存中**,不写入任何文件。
77
+ - 前端通过 `GET /control-token.json` 从内存获取 token,响应头 `Cache-Control: no-store`。
78
+ - **所有 POST 接口**(含 `/api/start-all`、`/api/stop-all`、`/api/restart-all`、`/api/core/*`、`/api/direct/*`、`/api/open-logs-folder`)必须携带 `X-PatchWarden-Control-Token` 请求头。
79
+ - token 缺失或错误时返回 `403`,不执行任何进程控制或本地系统动作。
80
+ - GET 接口(status、tasks、logs 等)不需要 token。
81
+ - `git status --porcelain` 不会出现 `ui/control-token.json`。
82
+
83
+ ## API 清单
84
+
85
+ ### GET 接口(免 token)
86
+
87
+ | 路径 | 说明 |
88
+ | --- | --- |
89
+ | `GET /` | 返回 `ui/pages/dashboard.html` |
90
+ | `GET /control-token.json` | 从内存返回控制 token,`Cache-Control: no-store` |
91
+ | `GET /colors_and_type.css` | 静态 CSS |
92
+ | `GET /pages/*` | 静态 HTML 页面 |
93
+ | `GET /partials/*` | 静态 HTML 片段 |
94
+ | `GET /vendor/*` | 本地化的 Tailwind / Lucide |
95
+ | `GET /api/status` | 聚合状态(容错,永不 500) |
96
+ | `GET /api/tasks` | 任务列表(复用 `listTasks`) |
97
+ | `GET /api/tasks/:taskId` | 任务详情(status/runtime/result/audit/diff_patch/test_log) |
98
+ | `GET /api/logs/core` | Core runtime 日志尾部 100 行(脱敏) |
99
+ | `GET /api/logs/direct` | Direct runtime 日志尾部 100 行(脱敏) |
100
+ | `GET /api/logs/watcher` | Watcher 日志尾部 100 行(脱敏) |
101
+ | `GET /api/workspace` | 轻量工作区信息(workspace_root、directories、agents、config 摘要;**不做 git 扫描**) |
102
+ | `GET /api/audit` | 审计历史(扫描 tasks 目录下含 audit.json 的任务,最多 50 条) |
103
+ | `GET /api/tunnel-ui-url` | 读取 Core/Direct 的 tunnel-health-url.txt |
104
+
105
+ ### POST 接口(必须校验 token)
106
+
107
+ | 路径 | 委托命令 | 说明 |
108
+ | --- | --- | --- |
109
+ | `POST /api/start-all` | `manage-patchwarden.ps1 start all` | 启动全部 |
110
+ | `POST /api/stop-all` | `manage-patchwarden.ps1 stop all` | 停止全部 |
111
+ | `POST /api/restart-all` | `manage-patchwarden.ps1 restart all` | 重启全部 |
112
+ | `POST /api/core/start` | `manage-patchwarden.ps1 start core` | 启动 Core |
113
+ | `POST /api/core/stop` | `manage-patchwarden.ps1 stop core` | 停止 Core |
114
+ | `POST /api/direct/start` | `manage-patchwarden.ps1 start direct` | 启动 Direct |
115
+ | `POST /api/direct/stop` | `manage-patchwarden.ps1 stop direct` | 停止 Direct |
116
+ | `POST /api/open-logs-folder` | 系统命令打开 runtime 目录 | 打开日志文件夹 |
117
+
118
+ ### `/api/status` 返回结构(容错)
119
+
120
+ ```json
121
+ {
122
+ "core": { "available": false, "reason": "...", "healthz": null, "readyz": null },
123
+ "direct": { "available": false, "reason": "...", "healthz": null, "readyz": null },
124
+ "watcher": { "status": "missing|healthy|stale|unreadable", "available": false, "reason": "..." },
125
+ "tunnel": { "core": { "observed": false }, "direct": { "observed": false } },
126
+ "tools": {
127
+ "core": { "tool_profile": null, "tool_count": null, "schema_epoch": null, "tool_manifest_sha256": null, "tool_names": null },
128
+ "direct": { "tool_profile": null, "tool_count": null, "schema_epoch": null, "tool_manifest_sha256": null, "tool_names": null }
129
+ },
130
+ "agents": [],
131
+ "workspace_root": "string | null",
132
+ "tasks": { "tasks": [], "total": 0, "active": 0, "stale": 0, "reason": null }
133
+ }
134
+ ```
135
+
136
+ 容错保证:
137
+ - core/direct health 探测失败 → `{ available: false, reason: "..." }`,接口仍 200
138
+ - runtime 文件(tunnel-status.json、tool-manifest.json)缺失 → 返回 null 或 `{ observed: false }`,接口仍 200
139
+ - 日志文件缺失 → `{ stdout: "", stderr: "", reason: "..." }`,接口仍 200
140
+ - tasks 目录缺失 → `{ tasks: [], total: 0, reason: "..." }`,接口仍 200
141
+ - Core/Direct 未启动时 `/api/status` 仍返回 200 JSON,不报 500
142
+
143
+ ## 进程安全
144
+
145
+ - Control Center **不直接 kill 进程**,所有 start/stop/restart 均委托 `scripts/control/manage-patchwarden.ps1`。
146
+ - `manage-patchwarden.ps1` 的 `Test-TunnelProcessForMode` 在 stop/restart 前校验进程命令行属于 `tunnel-client.exe --profile` 模式,仅停止匹配的进程。
147
+ - **不根据任意 PID 文件 kill 进程**。
148
+ - Control Center 仅在自身服务关闭时退出自己启动的 HTTP server。
149
+
150
+ ## CDN 本地化
151
+
152
+ - 所有 HTML 无 `https://cdn` 或 `https://unpkg` 引用。
153
+ - `ui/vendor/tailwindcss-browser.js`(Tailwind v4.3.1)和 `ui/vendor/lucide.js`(Lucide v1.8.0)为本地副本。
154
+ - 断网后页面样式和图标正常渲染。
155
+
156
+ ## 冒烟测试(测试模式)
157
+
158
+ `scripts/checks/control-center-smoke.js` 严格遵循测试模式:
159
+
160
+ - 不打开浏览器
161
+ - 不真正 start/stop Core 或 Direct
162
+ - 不 kill 任何进程(仅关闭自己 spawn 的 controlCenter 子进程)
163
+ - 使用测试端口 `18090`(通过 `PATCHWARDEN_CONTROL_PORT`),避免 8090 冲突
164
+ - 测试结束关闭 server,释放端口
165
+
166
+ 测试覆盖(10 项):
167
+
168
+ 1. 静态文件服务(`/`、`/vendor/*`、`/colors_and_type.css` 返回 200 + 正确 Content-Type)
169
+ 2. `/api/status` 返回有效 JSON 且容错(core/direct 未启动时仍 200)
170
+ 3. `/api/tasks` 返回有效 JSON
171
+ 4. `/control-token.json` 返回 token + `Cache-Control: no-store`
172
+ 5. POST `/api/start-all` 不带 token → 403
173
+ 6. POST `/api/start-all` 带错误 token → 403
174
+ 7. POST `/api/open-logs-folder` 不带 token → 403
175
+ 8. token 不污染 Git(`git status --porcelain` 不含 `ui/control-token.json`)
176
+ 9. 无 CDN 引用(HTML 文件无 `https://cdn` / `https://unpkg`)
177
+ 10. 其他 GET API 可达(`/api/workspace`、`/api/audit`、`/api/logs/core`、`/api/tunnel-ui-url`)
178
+
179
+ ## 测试结果
180
+
181
+ `npm test` 全量通过(`FINAL_EXIT_CODE: 0`):
182
+
183
+ | 测试链 | 结果 |
184
+ | --- | --- |
185
+ | smoke-test | OK |
186
+ | unit-tests | 139 total(136 pass,0 fail,1 skipped) |
187
+ | lifecycle-smoke | 22 passed, 0 failed |
188
+ | doctor-smoke | OK |
189
+ | tunnel-supervisor-smoke | OK |
190
+ | watcher-supervisor-smoke | OK |
191
+ | control-smoke | OK |
192
+ | mcp-manifest-check | 17 tools, chatgpt_core profile |
193
+ | brand-check | OK: 144 tracked files checked |
194
+ | control-center-smoke | 10 passed, 0 failed |
195
+
196
+ 未发布 npm、未打 git tag、未上传 npm。
197
+
198
+ ## 已知限制
199
+
200
+ 1. **`/api/workspace` 轻量版**:第一版只返回 workspace_root、项目列表(一级子目录)、agents、config 摘要。不做 git 扫描,不返回 repo clean 状态。workspace.html 中 "Repo Clean 状态" 显示为"按需接口待实现"占位。
201
+ 2. **task-detail / workspace / audit 为基础接入**:能加载数据并渲染,但未实现全部高级功能(如 diff 高亮、审计证据深度展示)。diff/test-log 仅做文本预览。
202
+ 3. **进程控制仅支持 Windows**:`manage-patchwarden.ps1` 为 PowerShell 脚本,POST 控制接口在非 Windows 环境下不可用。`/api/open-logs-folder` 在 macOS/Linux 下会尝试 `open`/`xdg-open`。
203
+ 4. **Health 探测端口固定**:Core 探测 `127.0.0.1:8080`,Direct 探测 `127.0.0.1:8081`,暂不支持配置覆盖。
204
+ 5. **Control token 生命周期**:token 在服务进程内存中,服务重启后 token 会变化(前端通过 `/control-token.json` 重新获取)。
205
+ 6. **日志读取**:仅返回各日志文件尾部 100 行,并对敏感内容脱敏(复用 `redactSensitiveContent`)。
@@ -0,0 +1,159 @@
1
+ # PatchWarden Control Center Phase 2
2
+
3
+ > v0.6.3 在 v0.6.2 MVP 基础上增强任务、工作区、Direct session 和日志体验。
4
+ > 不重构架构,不合并 Core/Direct MCP,不改现有 MCP 工具注册逻辑。
5
+ > 不发布 npm,不打 git tag。
6
+
7
+ ## 概述
8
+
9
+ Phase 2 完全在 Control Center HTTP 层增强,未触动 MCP 工具注册、watcher 核心循环或
10
+ Direct session 写入逻辑。新增的 API 全部沿用既有 token 校验、路径围栏和脱敏机制。
11
+
12
+ - **Stale Task 管理**:4 条规则识别僵死任务,reconcile 只标注不删除
13
+ - **task-detail 增强**:变更文件、验证摘要、警告/错误、stale 横幅 + 4 个动作按钮
14
+ - **工作区按需 git 状态**:仅在用户点击时对单个 repo 执行 `git status --short`
15
+ - **Direct Sessions 页面**:浏览 `.patchwarden/direct-sessions` 下的会话产物
16
+ - **Logs 页面**:core/direct/watcher/control-center 四类日志,支持 tail 100/300/1000
17
+
18
+ ## 启动方式
19
+
20
+ 启动方式与 MVP 完全一致,无新增入口:
21
+
22
+ ```powershell
23
+ .\PatchWarden-Control.cmd
24
+ # 或
25
+ npm.cmd run build
26
+ npm.cmd run start:control
27
+ ```
28
+
29
+ 启动后访问 `http://127.0.0.1:8090`,左侧导航新增「Direct 会话」和「日志」两项。
30
+
31
+ ## 改动文件
32
+
33
+ | 文件 | 改动 |
34
+ | --- | --- |
35
+ | `src/version.ts` | `PATCHWARDEN_VERSION` 0.6.2 → 0.6.3 |
36
+ | `package.json` | `version` 0.6.2 → 0.6.3 |
37
+ | `src/controlCenter.ts` | 新增 stale/reconcile/task-detail 增强/workspace repo status/direct sessions/logs 分类等 API |
38
+ | `ui/pages/dashboard.html` | 侧边栏新增 Direct 会话 + 日志导航 |
39
+ | `ui/pages/tasks.html` | 侧边栏新增 Direct 会话 + 日志导航 |
40
+ | `ui/pages/task-detail.html` | 新增变更文件卡、验证摘要卡、警告/错误卡、stale 横幅、Refresh/Copy task_id/Open folder/Run audit/Reconcile 按钮 |
41
+ | `ui/pages/workspace.html` | 每个 repo 新增「检查」按钮,按需调用 `/api/workspace/:repo/status`,渲染 git 状态面板 |
42
+ | `ui/pages/audit.html` | 侧边栏新增 Direct 会话 + 日志导航 |
43
+ | `ui/pages/direct-sessions.html` | 新页面:会话列表 + 会话详情(summary.md / audit.md / audit.json / diff.patch / changed-files.json) |
44
+ | `ui/pages/logs.html` | 新页面:分类选择 + tail 选择 + 自动刷新开关 |
45
+ | `scripts/checks/control-center-smoke.js` | 新增 Test 11–15 覆盖 Phase 2 API |
46
+ | `docs/control-center/control-center-phase2.md` | 本文档 |
47
+
48
+ ## 新增 API
49
+
50
+ ### GET 接口(免 token)
51
+
52
+ | 路径 | 说明 |
53
+ | --- | --- |
54
+ | `GET /api/tasks/stale` | 返回当前判定为 stale 的任务列表(含 stale_reasons) |
55
+ | `GET /api/tasks/:taskId` | 增强详情:新增 `changed_files`、`file_stats`、`verification_summary`、`warnings`、`errors`、`stale`、`reconcile`、`task_dir`、`independent_review`、`verify_log` |
56
+ | `GET /api/workspace/:repo/status` | 对单个 repo 执行 `git status --short`,返回 changed_files_count/untracked_count/modified_count/is_clean/short_status |
57
+ | `GET /api/direct-sessions` | 列出 `.patchwarden/direct-sessions` 下所有会话摘要 |
58
+ | `GET /api/direct-sessions/:sessionId` | 返回会话详情:summary/session/summary_md/diff_patch/audit_json/audit_md/changed_files |
59
+ | `GET /api/logs/:category?tail=<100\|300\|1000>` | 四类日志(core/direct/watcher/control-center),支持 tail 行数 |
60
+
61
+ ### POST 接口(必须校验 token)
62
+
63
+ | 路径 | 说明 |
64
+ | --- | --- |
65
+ | `POST /api/tasks/:taskId/reconcile` | 标注 stale/archived,写入 `reconcile.json`,不删除任务 |
66
+ | `POST /api/tasks/:taskId/audit` | 仅当任务处于终态时委托 `auditTask` 运行独立审计 |
67
+ | `POST /api/tasks/:taskId/open-folder` | 通过系统命令打开任务目录 |
68
+
69
+ ## Stale Task 判定规则
70
+
71
+ `classifyStaleTask` 仅对 `pending` / `running` 状态任务判定(终态任务直接返回非 stale)。
72
+ 判定使用 `config.watcherStaleSeconds` 作为阈值,4 条规则任一命中即标记 stale:
73
+
74
+ | 规则 | 字段值 | 说明 |
75
+ | --- | --- | --- |
76
+ | `heartbeat_stale` | status=running 且 last_heartbeat_at 超过阈值 | 运行中但心跳长时间未更新 |
77
+ | `collecting_artifacts_stale` | phase=collecting_artifacts 且心跳超过阈值 | 卡在产物收集阶段 |
78
+ | `running_no_command_watcher_healthy` | status=running 且 current_command 为空且 watcher healthy | 运行中无命令但 watcher 在工作,疑似僵死 |
79
+ | `heartbeat_far_behind_watcher` | 任务心跳比 watcher 心跳早 2× 阈值以上且 watcher healthy | 任务明显落后于 watcher |
80
+
81
+ `/api/status` 和 `/api/tasks` 返回的任务对象均带 `is_stale: boolean` 和
82
+ `stale_reasons: string[]`。`/api/status.tasks` 还聚合了 `stale` 计数和
83
+ `stale_task_ids` 列表。
84
+
85
+ ## Reconcile 行为
86
+
87
+ `POST /api/tasks/:taskId/reconcile` 的安全契约:
88
+
89
+ - **不删除任务**,不修改 `status` 枚举值
90
+ - 读取 `status.json` / `runtime.json` 重建 `TaskEntry`,重新调用 `classifyStaleTask`
91
+ - 决策(`decision` 字段):
92
+ - `marked_archived`:任务已处于终态 → 标注归档
93
+ - `marked_stale`:stale 且 watcher 未在驱动(无 current_command 或 watcher 非 healthy)→ 标注僵死
94
+ - `no_action`:不满足上述条件 → 仅记录,不标注
95
+ - 写入 `tasks/<taskId>/reconcile.json`(完整决策记录)
96
+ - 仅当 `safe=true` 时,在 `status.json` 追加 `reconcile_state`(`"stale"` 或 `"archived"`)和 `reconciled_at`,**原 status 字段保持不变**
97
+ - 返回 reconcile 记录 JSON
98
+
99
+ ## 路径围栏(workspace repo status)
100
+
101
+ `GET /api/workspace/:repo/status` 的安全约束:
102
+
103
+ - `repo` 参数经 `decodeURIComponent` 后若包含 `..` 或 `\0` → 直接 400
104
+ - 通过 `guardWorkspacePath(repo, workspaceRoot)` 解析为绝对路径,任何逃逸 `workspaceRoot` 的路径 → 400
105
+ - 仅对解析后的目录执行 `git status --short`,超时 8 秒,maxBuffer 1MB
106
+ - 非 git 仓库或 git 不可用时返回 200 + `is_git_repo: false` + error 字段,不返回 500
107
+ - smoke test 用 `encodeURIComponent("../secret")` 验证穿越被拒(400)
108
+
109
+ ## Direct Sessions 数据来源
110
+
111
+ - 会话目录:`getDirectSessionsDir(config)` → `<runtime>/.patchwarden/direct-sessions/`
112
+ - 每个会话子目录读取:`session.json`、`summary.json`、`audit.json`、`summary.md`、`diff.patch`、`audit.md`、`changed-files.json`
113
+ - `DirectSessionSummary` 字段:`session_id`、`repo_path`、`resolved_repo_path`、`created_at`、`expires_at`、`finalized`、`finalized_at`、`audited`、`changed_files_total`、`verification_summary`、`audit_decision`、`audit_checked_at`、`title`
114
+ - 目录不存在时返回 `{ sessions: [], total: 0, reason: null }`,**不返回 500**
115
+ - 会话按 `created_at` 降序排列
116
+
117
+ ## 日志脱敏
118
+
119
+ - 所有 `/api/logs/:category` 响应均经 `redactSensitiveContent` 脱敏
120
+ - tail 行数限制在 `ALLOWED_LOG_TAILS = {100, 300, 1000}`,非法值回退到 100
121
+ - 日志文件缺失时返回 `{ stdout: "", stderr: "", reason: "log file not found" }`
122
+
123
+ ## 冒烟测试
124
+
125
+ `scripts/checks/control-center-smoke.js` 在原 10 项基础上新增 5 项(共 16 项):
126
+
127
+ | # | 测试 |
128
+ | --- | --- |
129
+ | 11 | `GET /api/tasks/stale` 返回有效 JSON(`stale_tasks` 数组、`total` 数字、`stale_threshold_seconds` 数字) |
130
+ | 12 | `GET /api/workspace/<traversal>/status` 被拒绝(400,error 含 "traversal") |
131
+ | 13 | `POST /api/tasks/:taskId/reconcile` 无 token → 403 |
132
+ | 14 | `GET /api/direct-sessions` 目录缺失时返回空列表(200,非 500) |
133
+ | 15 | `GET /api/logs/core?tail=300` 响应包含 `tail: 300` |
134
+
135
+ ## 测试结果
136
+
137
+ ```
138
+ npm.cmd run build # tsc 编译通过
139
+ node scripts/checks/control-center-smoke.js
140
+ # Summary: 16 passed, 0 failed
141
+ ```
142
+
143
+ 全量 `npm test` 链路(security 139 + unit 136 + lifecycle 22 + control-center 16)
144
+ 全部通过,0 失败。
145
+
146
+ > 注:在 TRAE IDE 沙盒内运行 `npm test` 时,control-center-smoke spawn 的子进程退出
147
+ > 后 Windows 会尝试更新 jump list 临时文件,被沙盒拒绝并打印
148
+ > "TRAE Sandbox Error: hit restricted …CustomDestinations…"。该错误与测试逻辑无关,
149
+ > 所有测试用例均报告 PASS。直接运行 `node scripts/checks/control-center-smoke.js` 退出码为 0。
150
+
151
+ ## 已知限制
152
+
153
+ - `/api/workspace/:repo/status` 仅运行 `git status --short`,不返回 diff 或 stash 信息
154
+ - reconcile 不会清理任务文件,需人工后续处理或由 watcher 在下个周期自然覆盖
155
+ - `/api/tasks/:taskId/audit` 仅在任务处于终态(done/failed/failed_verification/
156
+ failed_scope_violation/failed_policy_violation/canceled/timeout)时才执行,否则返回 409
157
+ - Direct Sessions 页面只读,不提供 finalize 或 audit 触发入口(这些操作仍需通过 MCP 工具完成)
158
+ - Logs 页面的自动刷新间隔固定为 5 秒,不读取 watcher 实时事件
159
+ - task-detail 的「Open task folder」按钮通过 `explorer.exe` / `open` / `xdg-open` 打开,需要本地图形环境
package/docs/demo.md CHANGED
@@ -42,7 +42,10 @@ Expected artifacts under the configured workspace:
42
42
  ```text
43
43
  .patchwarden/tasks/<task_id>/status.json
44
44
  .patchwarden/tasks/<task_id>/result.md
45
+ .patchwarden/tasks/<task_id>/result.json
46
+ .patchwarden/tasks/<task_id>/artifact_manifest.json
45
47
  .patchwarden/tasks/<task_id>/git.diff
48
+ .patchwarden/tasks/<task_id>/verify.json
46
49
  .patchwarden/tasks/<task_id>/test.log
47
50
  ```
48
51