dsh-vscode-mode 0.37.0 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +54 -33
  2. package/lib/client.js +69 -66
  3. package/lib/client.js.map +1 -1
  4. package/lib/index.js +6774 -1925
  5. package/lib/index.js.map +1 -1
  6. package/package.json +3 -2
  7. package/skills/dsh-vscodemode-html-plan/SKILL.md +44 -42
  8. package/src/archiveDb.ts +24 -0
  9. package/src/archivePurge.ts +120 -28
  10. package/src/client/htmlPlan/HtmlPlanModeChip.ts +181 -0
  11. package/src/client/htmlPlan/HtmlPlanPanel.ts +230 -61
  12. package/src/client/htmlPlan/planActions.ts +38 -8
  13. package/src/client/htmlPlan/planPage.ts +111 -9
  14. package/src/client/index.ts +3 -0
  15. package/src/client/planBridge.ts +27 -2
  16. package/src/client/planState.ts +51 -4
  17. package/src/client/reviewBatch.ts +48 -1
  18. package/src/client/state/diffSummary.ts +5 -1
  19. package/src/client/styles/editor.css +16 -1
  20. package/src/client/ui/EditorView.ts +16 -10
  21. package/src/client/ui/McpSettings.ts +2 -2
  22. package/src/client/ui/OfficialReview.ts +6 -4
  23. package/src/client/ui/SvnCommitDialog.ts +256 -31
  24. package/src/client/ui/svnCommitModel.ts +63 -0
  25. package/src/htmlPlanCommand.ts +87 -177
  26. package/src/htmlPlanGuidance.ts +67 -0
  27. package/src/htmlPlanMode.ts +547 -0
  28. package/src/index.ts +32 -54
  29. package/src/officialCapture.ts +58 -9
  30. package/src/pending/capture.ts +201 -34
  31. package/src/pending/decide.ts +33 -1
  32. package/src/pending/externalCapture.ts +194 -333
  33. package/src/pending/kernel.ts +36 -2
  34. package/src/pending/noteTool.ts +96 -0
  35. package/src/pending/notes.ts +83 -0
  36. package/src/pending/persist.ts +312 -62
  37. package/src/pending/provenance.ts +50 -39
  38. package/src/pending/receipt.ts +177 -0
  39. package/src/pending/store.ts +230 -164
  40. package/src/pending/tracking.ts +81 -0
  41. package/src/pending/verdict.ts +170 -0
  42. package/src/pendingCompat.ts +40 -2
  43. package/src/pendingRpc.ts +8 -56
  44. package/src/planAutoPage.ts +306 -0
  45. package/src/planPagePack.ts +148 -0
  46. package/src/registry.ts +7 -2
  47. package/src/reviewDb.ts +64 -3
  48. package/src/reviewRpc.ts +61 -7
  49. package/src/rpc.ts +47 -7
  50. package/src/shared/htmlPlanMode.ts +26 -0
  51. package/src/shared/pendingReview.ts +35 -9
  52. package/src/shared/review.ts +22 -0
  53. package/src/shared/reviewRegions.ts +48 -3
  54. package/src/shared/rpc.ts +13 -9
  55. package/src/slowLog.ts +74 -0
  56. package/src/store.ts +17 -1
  57. package/src/textEncoding.probe.ts +1 -0
  58. package/src/pending/externalAlign.ts +0 -297
  59. package/src/pending/vcs.ts +0 -584
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-vscode-mode",
3
- "version": "0.37.0",
3
+ "version": "0.39.0",
4
4
  "description": "DSH 上的类 VSCode 编码体验:Monaco 编辑器(文件页签/QuickOpen/状态栏,可驻 DSH 0.1.5+ 官方右侧 Sidebar 与对话同屏)+ 命令栏(Ctrl+Shift+P)与可扩展指令系统 + VS Code 兼容代码片段(.code-snippets 全局/项目,IntelliSense 展开)+ Agent 编辑差异审查(整文件差异/采纳/拒绝/归档/回滚)+ SVN 集成(侧栏变更面板/日志弹窗/并排差异)+ 语言服务器(LSP)智能(跳转定义/引用/hover/大纲 + VSIX 扩展市场安装),状态持久化到工作区旁车,界面状态(页签/光标/侧栏/编辑 Tab/导航历史)镜像到 DSH home 跨重启恢复",
5
5
  "icon": "assets/icon.svg",
6
6
  "keywords": [
@@ -81,7 +81,8 @@
81
81
  "semver": "^7.8.5",
82
82
  "tsdown": "^0.22.14",
83
83
  "typescript": "^5.9.0",
84
- "vitest": "^3.2.4"
84
+ "vitest": "^3.2.4",
85
+ "zod": "4.4.3"
85
86
  },
86
87
  "scripts": {
87
88
  "build": "tsdown && node scripts/check-client-artifacts.mjs lib",
@@ -1,59 +1,58 @@
1
1
  ---
2
2
  name: dsh-vscodemode-html-plan
3
- description: 把实施计划写成一份单文件交互式 HTML 计划页(主张树:为什么 › 能做什么 › 怎么做 › 在哪;每条主张配一个图示;需要用户拍板的决策挂在它影响的主张上),并接入 DSH 计划模式的状态、审批与退出流程。
4
- whenToUse: 用户输入 /html-plan;或 plan 模式下要求给出可逐项作答的 HTML 计划/方案/设计稿/RFC;或计划里有 2~5 个必须用户拍板的岔口时。
3
+ description: 把实施计划写成一份单文件交互式 HTML 计划页(主张树:为什么 › 能做什么 › 怎么做 › 在哪;每条主张配一个图示;需要用户拍板的决策挂在它影响的主张上),用于 DSH 的 HTML 计划模式(独立模式,规则由插件每请求注入,页面由插件打包或兜底渲染)。
4
+ whenToUse: 用户输入 /html-plan;或处于 HTML 计划模式;或计划里有 2~5 个必须用户拍板的岔口、用户想要可逐项作答的 HTML 计划/方案/设计稿/RFC 时。
5
5
  ---
6
6
 
7
7
  # html-plan(DSH 版)
8
8
 
9
9
  本技能包含上游 anthropics/claude-plugins-community 的 html-plan 运行时与规则(MIT,作者 Thariq Shihipar),
10
- 按 DSH 计划模式改写:页面在 dsh-vscode-mode 的计划页签中渲染,审批与退出仍走官方计划模式。
10
+ 按 DSH 的 HTML 计划模式改写:模式由 dsh-vscode-mode 提供(独立投影 + 系统提示规则 + 退出工具 + 底部栏 chip),
11
+ 页面在计划签里渲染;审批与退出走官方 `userQuestions` 的 `plan-review` 通道。
11
12
 
12
13
  ## 0. 快捷入口
13
14
 
14
- - 用户输入 `/html-plan <任务>`:宿主命令会进入计划模式,并把「用本技能产出 HTML 计划页」的指令作为用户消息发出。
15
- - 只输入 `/html-plan`:同样进入计划模式,但指令要求先问清要规划的任务。
16
- - 用户已 `/plan`:按本技能正常产出即可,插件在 `exit_plan_mode` 时渲染 HTML 计划页。
15
+ - `/html-plan <任务>`:进入 **HTML 计划模式**,任务作为普通用户消息送进会话;规则由插件注入。
16
+ - `/html-plan`:只进入模式;没有任务时先用一句话问清要规划什么。
17
+ - `/html-plan off` 或批准计划:离开模式;底部栏 chip 与这里等价。
18
+ - `/plan`:切回官方计划模式(两个模式互斥)。
17
19
 
18
20
  ## 1. 何时用
19
21
 
20
- - 当前处于 plan 模式(/plan),或用户明确要求「先出计划/方案/设计稿/RFC」。
22
+ - 当前处于 HTML 计划模式,或用户明确要求「先出计划/方案/设计稿/RFC」。
21
23
  - 计划里存在必须用户拍板的岔口(2~5 个):范围、取舍、默认值。
22
24
 
23
25
  ## 2. 产出约定(DSH 特有,必读)
24
26
 
25
- **先落盘,再声明。** 计划模式允许写 `plans/` 下的计划交付物(`plan.html`、`plan.packed.html`、`00-plan.md`),
26
- 不需要用户退出计划模式。**绝不要**要求用户「点退出计划模式 / 点同意执行」才能生成页面。
27
+ **先落盘,再提交。** 模式规则允许写 `plans/<slug>/plan.html`(本模式唯一允许的写操作),
28
+ 不需要用户退出模式。**绝不要**要求用户「点退出计划模式 / 点同意执行」才能生成页面。
27
29
 
28
- 1. 位置:plans/<slug>/plan.html。slug 用 kebab-case,例如 plans/html-plan/plan.html。
29
- 2. 页面引用(相对路径,运行时由第 3 步的脚本自动放到位):
30
+ 1. 位置:plans/<slug>/plan.html(可选;不写也能交付,插件会用计划正文渲染兜底页)。
31
+ 2. 页面引用(相对路径;插件打包时会自动找到运行时):
30
32
  <link rel="stylesheet" href="runtime/htmlplan.css">
31
33
  <script src="runtime/htmlplan.js" defer></script>
32
- 3. 一条命令打包(推荐):
33
- `node <本技能目录>/runtime/plan.mjs <slug> --root <工作区绝对路径>`
34
- 它复制运行时 → 跑 pack.mjs → 成功后删除 runtime/ 与 plan.html,只留 plans/<slug>/plan.packed.html。
35
- pack 报 error 时它会保留现场,按报告逐条修再跑;`--keep` 可保留中间文件。
36
- 手动等价步骤:把 runtime/htmlplan.css、htmlplan.js、pack.mjs 复制到 plans/<slug>/runtime/,
37
- 运行 `node plans/<slug>/runtime/pack.mjs plans/<slug>/plan.html --root <工作区>`,成功后删掉那两样。
38
- 4. 提交前自检(必做):`node <本技能目录>/runtime/plan.mjs <slug> --check` 打印 ✓ 才提交。
39
- 页面没生成成功**不要**写「HTML 计划页:」那一行——宁可只交付 Markdown 计划并说明失败原因,
40
- 也不要留下指向不存在文件的路径(界面会打开失败,用户拿不到任何东西)。
41
- 5. 提交审批:调用 exit_plan_mode。plan 正文第一行必须是「# 计划:<标题>」,并且**必须包含一行**:
42
-
43
- HTML 计划页:plans/<slug>/plan.packed.html
44
-
45
- 插件据这一行在计划页签渲染 HTML;缺少该行则回落 Markdown 计划预览。
46
- 6. 正文同时写 3~6 行摘要与决策清单:官方审批卡片只显示标题与两行纯文本摘要,
34
+ 3. **不需要你自己打包**:计划签打开时插件自动调用 `pack.mjs`,输出到系统临时文件再读回;
35
+ 不删你的 `plan.html` 与 `assets/`。不要为页面跑 `plan.mjs` 或任何 node 命令。
36
+ 只有在没有插件、要手动看文件时,才按旧流程跑
37
+ `node <本技能目录>/runtime/plan.mjs <slug> --root <工作区绝对路径>`。
38
+ 4. 提交(HTML 计划模式):调用 **exit_html_plan_mode**;plan 正文第一行必须是「# 计划:<标题>」,
39
+ 有作者页时再单独一行:
40
+
41
+ HTML 计划页:plans/<slug>/plan.html
42
+
43
+ (指向未打包的 `.html` 也可以;插件会自动打包。页面缺失或打包失败时插件用正文渲染兜底页。)
44
+ 5. 提交(官方 `/plan` 模式):调用 `exit_plan_mode`;有作者页同样加「HTML 计划页:…」一行,
45
+ 插件会自动打包后渲染。
46
+ 6. 不要调用另一个模式的退出工具:HTML 计划模式里不要用 `exit_plan_mode`,普通计划模式里不要用 `exit_html_plan_mode`。
47
+ 7. 正文同时写 3~6 行摘要与决策清单:官方审批卡片只显示标题与两行纯文本摘要,
47
48
  正文是官方「查看全文」与历史卡片的内容来源。
48
- 7. 顺序固定:写 plan.html → 打包 → `--check` 通过 → 才写那一行并调 exit_plan_mode。
49
- 顺序颠倒或漏打包,是本技能最常见的失败:界面只剩一句「文件不存在」。
50
- 8. 用户在计划页签点「生成 HTML 计划页」时,你会收到一条同样的修复指令:照第 3、4 步做完再重提交。
51
- 不要回「计划模式不能写文件」——能写,写的是 `plans/` 下的计划交付物。
49
+ 8. 顺序:写 plan.html(可选)→ 调对应退出工具。计划签的「重新解析 / ⟳」只重新解析,不会再让 agent 写页。
50
+ 页面确实生成失败时说清原因即可,不要为了页面退出模式。
52
51
 
53
52
  ### 2.1 附件(图片与文件)
54
53
 
55
54
  用户把图片/文件拖进对话输入框、连 `/html-plan <任务>` 一起发时,命令会先把它们复制到
56
- `plans/<slug>/assets/`,并把固定 slug 与文件清单写进给你的指令:
55
+ `plans/<slug>/assets/`,并把固定 slug 与文件清单写进送给你的任务消息:
57
56
 
58
57
  - slug 已由命令定好:页面写到 `plans/<slug>/plan.html`,不要另起目录。
59
58
  - 图片先用 `read_image` 看清内容再写主张;文档、日志用 `read` 读原文。
@@ -66,12 +65,12 @@ whenToUse: 用户输入 /html-plan;或 plan 模式下要求给出可逐项作
66
65
  </doc-attach>
67
66
  ```
68
67
 
69
- - `pack.mjs` 把每个 `doc-file` 内联成 data URI,并补 `data-mime` / `data-bytes`;
68
+ - 插件自动打包时把每个 `doc-file` 内联成 data URI,并补 `data-mime` / `data-bytes`;
70
69
  图片出缩略图(点开看原图),文件出卡片(带下载)。单文件 >4 MB 不内联,
71
70
  卡片显示「未内联」;全页附件 >12 MB 会警告——大文件先裁小或改成文字引用。
72
- - `plan.mjs --check` 会拒绝仍有未内联附件的页面;打包成功后 `plans/<slug>/assets/` 默认删除。
73
- 要再打包一次(改页、重跑)就先给 `plan.mjs` 加 `--keep-assets` 保留原图。
74
- - 页面只引用工作区里的文件:pack 只读 `--root` 与页面目录,围栏与密钥过滤对附件同样生效。
71
+ - 自动打包**不会删除** `plans/<slug>/assets/`;旧的手动 `plan.mjs` 流程打包成功后会删除该目录。
72
+ - 页面只引用工作区里的文件:打包时只读工作区与页面目录,围栏与密钥过滤对附件同样生效。
73
+
75
74
 
76
75
  ## 3. 主张树(沿用上游)
77
76
 
@@ -164,18 +163,21 @@ HTML 骨架(可复制后改写):
164
163
 
165
164
  ## 6. DSH 计划页签与状态
166
165
 
167
- - 计划页签显示状态:计划中 / 待审批 / 已批准(已退出计划模式)/ 继续规划 / 已退出计划模式。
168
- -「同意执行」「继续规划」等价于官方审批卡片的同名操作,走官方问答通道;
169
- 「退出计划模式」等价于 composer 计划徽标的 /plan off;官方审批卡片仍是权威通道。
166
+ - HTML 计划模式下,计划页签显示:HTML 计划模式中 / 待审批 / 已批准(已退出模式)/ 继续规划 / 已退出模式;
167
+ 页面来源徽标会标出「作者页 / 已打包 / 自动页」。
168
+ - 「同意执行」「继续规划」等价于官方审批卡片的同名操作,走官方问答通道;
169
+ 「退出计划模式」在 HTML 计划模式下等价于 `/html-plan off`,在普通计划模式下等价于 `/plan off`;
170
+ 输入框底部工具行的「HTML 计划模式」chip 也执行 `/html-plan off`。官方审批卡片仍是权威通道。
170
171
  - 任一官方客户端服务缺失时按钮降级为提示,不会伪造批准。
171
172
 
172
173
  ## 7. 委派写页(可选)
173
174
 
174
- - 默认自己写。满足任一条件时,先按 Agent 协作规则征求同意再委派:预计页 >12 KB、主张/图示 ≥4、上下文紧张。
175
- - 用户写 `/html-plan --delegate` 视为本任务已授权;`--no-delegate` 明确不委派。
175
+ - 默认自己写。HTML 计划模式不再提供 `--delegate` 开关;只有用户明确要求、且页规模大(>12 KB)或主张 ≥4 时,
176
+ 才按 Agent 协作规则征求同意后委派。
176
177
  - 用 subagent_fork(cwd = 工作区),父级先定稿主张树,再交固定摘要(模板见 references/subagent-brief.md)。
177
- - 子级只写页并 pack,返回 packed 路径、lint 结果、主张/决策计数;父级只验 lint、计数与三处抽查,不读全文。
178
- - 子级失败(不能写文件 / 缺 pack / lint 错)时父级自己写,不中断计划。
178
+ - 子级只写 `plans/<slug>/plan.html`,不要跑打包命令:插件打开计划签时会自动打包。
179
+ - 子级返回页路径与主张/决策计数;父级只验计数与三处抽查,不读全文。
180
+ - 子级失败(不能写文件 / 结构错误)时父级自己写;页面最终写不出来也不影响交付——插件会用计划正文渲染兜底页。
179
181
 
180
182
  ## 8. 上游资产与许可
181
183
 
package/src/archiveDb.ts CHANGED
@@ -179,6 +179,30 @@ export function readActive(cwd: string): Map<string, DiffRecord> {
179
179
  return new Map(recs.map((rec) => [rec.callId, rec]))
180
180
  }
181
181
 
182
+ /**
183
+ * @author ddj 2026年11月25号
184
+ * @description: 打上「旧活跃记录待重置」标记:待审库 v7 迁移时调用一次,由记录桶侧消费。
185
+ * @param cwd 工作区
186
+ * @returns 无
187
+ */
188
+ export function markActiveReset(cwd: string): void {
189
+ dbFor(cwd).prepare("INSERT INTO metadata(name,value) VALUES('active_reset_v1','pending') ON CONFLICT(name) DO UPDATE SET value='pending'").run()
190
+ }
191
+
192
+ /**
193
+ * @author ddj 2026年11月25号
194
+ * @description: 消费一次重置标记:读到时原子改成 done,只有第一个调用方返回 true。
195
+ * @param cwd 工作区
196
+ * @returns 本次是否抢到标记
197
+ */
198
+ export function takeActiveReset(cwd: string): boolean {
199
+ const db = dbFor(cwd)
200
+ const row = db.prepare("SELECT value FROM metadata WHERE name='active_reset_v1'").get() as Row | undefined
201
+ if (row?.value !== 'pending') return false
202
+ db.prepare("UPDATE metadata SET value='done' WHERE name='active_reset_v1'").run()
203
+ return true
204
+ }
205
+
182
206
  /**
183
207
  * 单次 SQLite 事务提交活跃行变化及可选归档,绝不重写整个记录桶。
184
208
  * @author ddj 2026年09月30号
@@ -10,19 +10,18 @@
10
10
  * - 真删之前先留一份同名备份(`<db>.before-purge.bak`,覆盖写),每个工作区只占一份。
11
11
  * 作者 ddj 2026-11-24
12
12
  */
13
- import { readdirSync, rmSync } from 'node:fs'
14
- import { join } from 'node:path'
13
+ import { spawn } from 'node:child_process'
14
+ import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
15
+ import { dirname, join } from 'node:path'
15
16
  import { DatabaseSync } from 'node:sqlite'
16
17
  import { archiveDbFile, dshHome, PLUGIN_ID } from './paths.js'
17
- import { dbFor, activeReady } from './archiveDb.js'
18
+ import { dbFor, activeReady, closeArchiveDbs } from './archiveDb.js'
18
19
  import { log } from './log.js'
19
20
 
20
21
  /** 已判定历史默认保留天数。 */
21
22
  export const RETENTION_DAYS = 30
22
23
  /** 同一路径默认保留多少个归档批次。 */
23
24
  export const KEEP_BATCHES = 20
24
- /** 同一待审条目默认保留多少步撤销栈。 */
25
- export const KEEP_HISTORY = 10
26
25
  /** 幂等操作表默认保留多少条(该表无时间列,按 rowid 保留最近的)。 */
27
26
  export const KEEP_OPS = 500
28
27
  /** 真删前备份文件名后缀。 */
@@ -34,8 +33,8 @@ export interface ArchivePurgeOptions {
34
33
  retentionDays?: number
35
34
  /** 每路径保留批次数(缺省 {@link KEEP_BATCHES})。 */
36
35
  keepBatches?: number
37
- /** 每条目保留的撤销步数(缺省 {@link KEEP_HISTORY})。 */
38
- keepHistory?: number
36
+ /** 幂等表保留条数(缺省 {@link KEEP_OPS})。 */
37
+ keepOps?: number
39
38
  /** true 只报数不删。 */
40
39
  dryRun?: boolean
41
40
  /** 删除后 VACUUM(缺省 true)。 */
@@ -103,20 +102,16 @@ function pickCandidates(db: ReturnType<typeof dbFor>, options: ArchivePurgeOptio
103
102
  }
104
103
 
105
104
  /**
106
- * @author ddj 2026-11-24
107
- * @description: 截断撤销栈与幂等表:每个条目只留最近若干步,避免这两张表跟着天长日久。
105
+ * @author ddj 2026年11月25号
106
+ * @description: 截断幂等表:撤销栈已改成「一条路径一行」,不再按步裁剪。
108
107
  * @param db 工作区归档库连接
109
- * @param keepHistory 每条目保留步数
110
- * @returns 删除的历史行数
108
+ * @param keepOps 幂等表保留条数
109
+ * @returns 删除的历史行数(恒为 0,保留字段只为回执口径不变)
111
110
  */
112
- function trimPendingTables(db: ReturnType<typeof dbFor>, keepHistory: number): number {
113
- const trimmed = db.prepare(`DELETE FROM pending_history WHERE id NOT IN (
114
- SELECT id FROM (
115
- SELECT id, ROW_NUMBER() OVER (PARTITION BY entry_id ORDER BY id DESC) AS rn FROM pending_history
116
- ) WHERE rn <= ?)`).run(keepHistory)
111
+ function trimPendingTables(db: ReturnType<typeof dbFor>, keepOps: number): number {
117
112
  db.prepare(`DELETE FROM pending_ops WHERE rowid NOT IN (
118
- SELECT rowid FROM pending_ops ORDER BY rowid DESC LIMIT ?)`).run(KEEP_OPS)
119
- return Number(trimmed.changes)
113
+ SELECT rowid FROM pending_ops ORDER BY rowid DESC LIMIT ?)`).run(keepOps)
114
+ return 0
120
115
  }
121
116
 
122
117
  /**
@@ -161,14 +156,14 @@ export function purgeArchiveDb(cwd: string, options: ArchivePurgeOptions = {}):
161
156
  const db = dbFor(cwd)
162
157
  const { ids, expired, overCap, keptPending } = pickCandidates(db, options)
163
158
  const dryRun = options.dryRun !== false
164
- const keepHistory = options.keepHistory ?? KEEP_HISTORY
159
+ const keepOps = options.keepOps ?? KEEP_OPS
165
160
  if (dryRun) {
166
161
  return { expired, overCap, keptPending, removed: { records: 0, batches: 0, history: 0 } }
167
162
  }
168
163
  // 没有要删的批次就不备份、不动库:每日扫描绝大多数时候是空跑
169
164
  if (ids.length) backupBeforePurge(db, cwd)
170
165
  const removedBatches = removeBatches(db, ids)
171
- const history = activeReady(cwd) ? trimPendingTables(db, keepHistory) : 0
166
+ const history = activeReady(cwd) ? trimPendingTables(db, keepOps) : 0
172
167
  const outcome: ArchivePurgeOutcome = {
173
168
  expired, overCap, keptPending,
174
169
  removed: { ...removedBatches, history },
@@ -231,6 +226,16 @@ export interface ArchiveSweepOptions {
231
226
  run?: (cwd: string, options: ArchivePurgeOptions) => ArchivePurgeOutcome
232
227
  /** 覆盖 DSH home(测试注入)。 */
233
228
  home?: string
229
+ /** 覆盖「重写库文件」的执行体(测试注入;缺省起子进程做 VACUUM)。 */
230
+ rewrite?: (jobs: RewriteJob[]) => void
231
+ }
232
+
233
+ /** 交给子进程的重写任务:备份 + 回收页,两件都是整库 IO,必须离开宿主事件循环。 */
234
+ export interface RewriteJob {
235
+ /** 库文件绝对路径。 */
236
+ file: string
237
+ /** 清理前备份路径;空串表示不备份。 */
238
+ backup: string
234
239
  }
235
240
 
236
241
  /**
@@ -267,27 +272,113 @@ export function listArchiveWorkspaces(home = dshHome()): string[] {
267
272
 
268
273
  /**
269
274
  * @public
270
- * @author ddj 2026-11-24
271
- * @description: 对所有工作区归档库跑一遍清理;单个库失败只记日志,不影响其他库。
275
+ * @author ddj 2026-11-24 / 2026年11月25号
276
+ * @description: 对所有工作区归档库跑一遍清理;删除在进程内做完(快),整库重写交给子进程。
277
+ * 单个库失败只记日志,不影响其他库。
272
278
  * @param options 扫描选项
273
279
  * @returns 扫描到的库数与删除的行数合计
274
280
  */
275
- export function sweepArchives(options: ArchiveSweepOptions = {}): { workspaces: number; removed: number } {
281
+ export function sweepArchives(options: ArchiveSweepOptions = {}): { workspaces: number; removed: number; rewrites: number } {
276
282
  const run = options.run ?? purgeArchiveDb
277
283
  const workspaces = listArchiveWorkspaces(options.home)
284
+ const jobs: RewriteJob[] = []
278
285
  let removed = 0
279
286
  for (const cwd of workspaces) {
280
287
  try {
281
- const outcome = run(cwd, { dryRun: false })
288
+ // vacuum:false 先在进程内删行;备份与 VACUUM 一律留到最后统一交给子进程
289
+ const outcome = run(cwd, { dryRun: false, vacuum: false })
282
290
  removed += outcome.removed.batches + outcome.removed.history
283
- if (outcome.removed.batches || outcome.removed.history) {
284
- log.debug(`归档库清理:${cwd} 批次 ${outcome.removed.batches},历史 ${outcome.removed.history},VACUUM ${outcome.vacuumMs ?? 0}ms`)
285
- }
291
+ if (!outcome.removed.batches && !outcome.removed.history) continue
292
+ jobs.push({ file: archiveDbFile(cwd, options.home), backup: archiveDbFile(cwd, options.home) + BACKUP_SUFFIX })
293
+ log.debug(`归档库清理:${cwd} 批次 ${outcome.removed.batches},历史 ${outcome.removed.history}`)
286
294
  } catch (error) {
287
295
  log.debug('归档库清理跳过:' + cwd + ' ' + String(error))
288
296
  }
289
297
  }
290
- return { workspaces: workspaces.length, removed }
298
+ if (jobs.length) {
299
+ const rewrite = options.rewrite ?? rewriteInChild
300
+ try { rewrite(jobs) } catch (error) { log.warn('归档库重写调度失败(库已删行,未回收空间):' + String(error)) }
301
+ }
302
+ return { workspaces: workspaces.length, removed, rewrites: jobs.length }
303
+ }
304
+
305
+ /**
306
+ * @private
307
+ * @author ddj 2026年11月25号
308
+ * @description: 起一个独立 Node 子进程做备份与 VACUUM。这两步是整库读写,留在宿主里会把
309
+ * 事件循环堵住十几秒,所有 RPC(含文件读取、差异渲染)都会一起卡住。
310
+ * 子进程只认「文件 + 备份路径」,不含任何业务规则,规则仍只有 {@link purgeArchiveDb} 一份。
311
+ * @param jobs 重写任务
312
+ * @returns 无(不等待完成)
313
+ */
314
+ function rewriteInChild(jobs: RewriteJob[]): void {
315
+ // 子进程要独占写库:先把本进程缓存的连接关掉,否则它会在 busy_timeout 里空转
316
+ try { closeArchiveDbs() } catch { /* 关闭失败仍交给子进程重试 */ }
317
+ const script = [
318
+ "const { DatabaseSync } = require('node:sqlite')",
319
+ "const { rmSync } = require('node:fs')",
320
+ 'const jobs = JSON.parse(process.argv[1])',
321
+ 'for (const job of jobs) {',
322
+ ' let db',
323
+ ' try {',
324
+ ' db = new DatabaseSync(job.file, { timeout: 15000 })',
325
+ " db.exec('PRAGMA busy_timeout=15000')",
326
+ ' if (job.backup) {',
327
+ ' try { rmSync(job.backup, { force: true }) } catch (e) { /* 备份覆盖失败不阻断 */ }',
328
+ " db.exec(\"VACUUM INTO '\" + job.backup.replace(/'/g, \"''\") + \"'\")",
329
+ ' }',
330
+ " db.exec('VACUUM')",
331
+ ' } catch (error) {',
332
+ ' console.error(String(error))',
333
+ ' process.exitCode = 1',
334
+ ' } finally {',
335
+ ' try { db && db.close() } catch (e) { /* 已关闭 */ }',
336
+ ' }',
337
+ '}',
338
+ ].join('\n')
339
+ const child = spawn(process.execPath, ['-e', script, JSON.stringify(jobs)], { stdio: ['ignore', 'ignore', 'pipe'], windowsHide: true })
340
+ let stderr = ''
341
+ child.stderr?.on('data', (chunk) => { stderr += String(chunk) })
342
+ child.on('error', (error) => log.warn('归档库重写子进程启动失败:' + String(error)))
343
+ child.on('exit', (code) => {
344
+ if (code === 0) log.debug('归档库重写完成:' + jobs.length + ' 个库')
345
+ else log.warn('归档库重写子进程退出码 ' + String(code) + ':' + stderr.slice(0, 300))
346
+ })
347
+ if (typeof child.unref === 'function') child.unref()
348
+ }
349
+
350
+ /**
351
+ * @public
352
+ * @author ddj 2026年11月25号
353
+ * @description: 单个工作区库的备份与回收页交给子进程;RPC 层删完行后调它,不再原地 VACUUM。
354
+ * @param cwd 工作区
355
+ * @param home DSH home(测试可注入)
356
+ * @returns 无
357
+ */
358
+ export function scheduleArchiveRewrite(cwd: string, home = dshHome()): void {
359
+ const file = archiveDbFile(cwd, home)
360
+ try { rewriteInChild([{ file, backup: file + BACKUP_SUFFIX }]) } catch (error) {
361
+ log.warn('归档库重写调度失败(库已删行,未回收空间):' + String(error))
362
+ }
363
+ }
364
+
365
+ /**
366
+ * @private
367
+ * @author ddj 2026年11月25号
368
+ * @description: 今日是否已经扫过:两个宿主(桌面端与 Web 端)共用同一份 home,避免一天扫两遍。
369
+ * @param home DSH home
370
+ * @returns 已扫过返回 true
371
+ */
372
+ function sweptToday(home: string): boolean {
373
+ const stamp = join(home, PLUGIN_ID, 'data', 'archive', '.sweep-' + new Date().toISOString().slice(0, 10))
374
+ try {
375
+ if (existsSync(stamp)) return true
376
+ mkdirSync(dirname(stamp), { recursive: true })
377
+ writeFileSync(stamp, new Date().toISOString(), 'utf8')
378
+ return false
379
+ } catch {
380
+ return false
381
+ }
291
382
  }
292
383
 
293
384
  /**
@@ -301,6 +392,7 @@ export function startArchiveSweep(options: ArchiveSweepOptions = {}): { dispose(
301
392
  let stopped = false
302
393
  const run = (): void => {
303
394
  if (stopped) return
395
+ if (sweptToday(options.home ?? dshHome())) return
304
396
  try { sweepArchives(options) } catch (error) { log.warn('归档库每日清理失败:' + String(error)) }
305
397
  }
306
398
  const first = setTimeout(run, options.delayMs ?? SWEEP_FIRST_DELAY_MS)
@@ -0,0 +1,181 @@
1
+ /**
2
+ * dsh-vscode-mode client — 底部栏的 HTML 计划模式 chip。
3
+ *
4
+ * 参照官方 `dsh-client-ui-plan` 的 `PlanChip`:读取会话投影判断模式是否活跃,
5
+ * 活跃时在输入框工具行显示一个 chip,点击执行 `/html-plan off` 退出模式。
6
+ *
7
+ * 槽位选 `conversation.input.left`(list,会话级):官方计划 chip 占着
8
+ * `conversation.input.plan`(single),这里不去抢;注册沿用本插件跨版本的
9
+ * `ctx.slots.inject` 写法,旧宿主没有该槽位时不报错、chip 自然不出现。
10
+ * @author ddj 2026年11月25号
11
+ */
12
+ import React from 'react'
13
+ import { log } from '../log.js'
14
+ import { HTML_PLAN_MODE_KEY, HTML_PLAN_OFF_LINE } from '../../shared/htmlPlanMode.js'
15
+
16
+ //region 结构面
17
+
18
+ /** chip 的 slot 运行时注入面。 */
19
+ export interface HtmlPlanChipInjected {
20
+ /** 离开 HTML 计划模式(执行 /html-plan off)。 */
21
+ exitHtmlPlanMode: () => Promise<string | null>
22
+ }
23
+
24
+ /** chip 组件 props(slot 标准 props + 注入面)。 */
25
+ export interface HtmlPlanModeChipProps extends Partial<HtmlPlanChipInjected> {
26
+ /** 当前会话。 */
27
+ sessionId?: string
28
+ /** 官方标准 kit:按 key 读会话投影。 */
29
+ useProjection?: (key: string) => unknown
30
+ /** 输入框是否锁定。 */
31
+ locked?: boolean
32
+ }
33
+
34
+ /** slots 服务最小结构面。 */
35
+ interface SlotsLike {
36
+ inject: (name: string, register: () => unknown) => unknown
37
+ register: (spec: unknown, render: unknown) => unknown
38
+ }
39
+
40
+ /** 客户端上下文最小结构面。 */
41
+ interface ChipCtx {
42
+ slots?: SlotsLike
43
+ get?: (name: string) => unknown
44
+ }
45
+
46
+ /** remote.commands 服务最小结构面。 */
47
+ interface RemoteCommandsLike {
48
+ execute?: (sessionId: string, line: string, attachments: readonly unknown[]) => Promise<unknown>
49
+ }
50
+
51
+ //endregion
52
+
53
+ //region 动作
54
+
55
+ /**
56
+ * 取异常的展示文案。
57
+ * @author ddj 2026年11月25号
58
+ * @param error 捕获值
59
+ * @returns 可读文本
60
+ */
61
+ function messageOf(error: unknown): string {
62
+ if (error instanceof Error) return error.message
63
+ return String(error ?? '未知错误')
64
+ }
65
+
66
+ /**
67
+ * 把官方命令执行结果归一成「成功返回 null,失败返回一行原因」。
68
+ * @author ddj 2026年11月25号
69
+ * @param raw 官方 `remote.commands.execute` 的返回值
70
+ * @returns null 或失败原因
71
+ */
72
+ export function exitFailureOf(raw: unknown): string | null {
73
+ if (raw === null || typeof raw !== 'object') return raw === false ? '官方服务拒绝了本次操作' : null
74
+ const row = raw as { ok?: unknown; value?: unknown; error?: unknown }
75
+ if (row.ok === false) {
76
+ const detail = row.error as { message?: unknown } | string | undefined
77
+ return typeof detail === 'string' ? detail : (typeof detail?.message === 'string' ? detail.message : '官方服务拒绝了本次操作')
78
+ }
79
+ if (row.ok === true && row.value === undefined) return '未知命令:/html-plan off'
80
+ return null
81
+ }
82
+
83
+ //endregion
84
+
85
+ //region 组件
86
+
87
+ /**
88
+ * 底部栏 chip:模式活跃时出现,点击离开。
89
+ * @author ddj 2026年11月25号
90
+ * @param props slot 运行时 props 与注入面
91
+ * @returns chip 元素;模式不活跃时为 null
92
+ */
93
+ export function HtmlPlanModeChip(props: HtmlPlanModeChipProps) {
94
+ const [leaving, setLeaving] = React.useState(false)
95
+ const [error, setError] = React.useState<string | null>(null)
96
+
97
+ const mode = typeof props.useProjection === 'function'
98
+ ? props.useProjection(HTML_PLAN_MODE_KEY) as { active?: boolean } | undefined
99
+ : undefined
100
+ if (mode?.active !== true) return null
101
+
102
+ const exit = props.exitHtmlPlanMode
103
+ const leave = (): void => {
104
+ if (typeof exit !== 'function' || leaving) return
105
+ setLeaving(true)
106
+ setError(null)
107
+ void exit().then((failure) => {
108
+ setLeaving(false)
109
+ setError(failure)
110
+ }, (reason: unknown) => {
111
+ setLeaving(false)
112
+ setError(messageOf(reason))
113
+ })
114
+ }
115
+
116
+ return React.createElement('span', { className: 'edrv-htmlplan-chip-wrap' },
117
+ React.createElement('button', {
118
+ type: 'button',
119
+ className: 'edrv-htmlplan-chip',
120
+ title: 'HTML 计划模式已开启:点击执行 /html-plan off 离开',
121
+ 'aria-label': '离开 HTML 计划模式',
122
+ disabled: props.locked === true || leaving,
123
+ onClick: leave,
124
+ },
125
+ React.createElement('span', { className: 'edrv-htmlplan-chip-glyph', 'aria-hidden': true }, '◆'),
126
+ 'HTML 计划模式',
127
+ React.createElement('span', { className: 'edrv-htmlplan-chip-x', 'aria-hidden': true }, '✕')),
128
+ (error ? React.createElement('span', { className: 'edrv-htmlplan-chip-error', role: 'status', title: error }, '离开失败') : null))
129
+ }
130
+
131
+ //endregion
132
+
133
+ //region 装配
134
+
135
+ /**
136
+ * 把 chip 装到 `conversation.input.left` 列表槽。
137
+ * @author ddj 2026年11月25号
138
+ * @param ctx 客户端上下文
139
+ * @returns 卸载函数;槽位/服务不可用时 null
140
+ */
141
+ export function installHtmlPlanModeChip(ctx: unknown): (() => void) | null {
142
+ const client = ctx as ChipCtx | undefined
143
+ const slots = client?.slots
144
+ if (!slots || typeof slots.inject !== 'function' || typeof slots.register !== 'function') {
145
+ log.warn('HTML 计划模式 chip 未注册:slots 服务不可用')
146
+ return null
147
+ }
148
+
149
+ /** @author ddj 2026年11月25号 @param sessionId 会话 @returns 失败原因或 null */
150
+ async function exitHtmlPlanMode(sessionId: string): Promise<string | null> {
151
+ if (!sessionId) return '当前没有活动会话'
152
+ const remote = typeof client?.get === 'function'
153
+ ? client.get('remote.commands') as RemoteCommandsLike | undefined
154
+ : undefined
155
+ if (!remote || typeof remote.execute !== 'function') return '当前 DSH 不支持在插件内执行命令'
156
+ try {
157
+ return exitFailureOf(await remote.execute(sessionId, HTML_PLAN_OFF_LINE, []))
158
+ } catch (error) {
159
+ return messageOf(error)
160
+ }
161
+ }
162
+
163
+ try {
164
+ const disposer = slots.inject('conversation.input.left', () => slots.register({
165
+ name: 'conversation.input.left',
166
+ id: 'edrv-html-plan-mode',
167
+ order: 20,
168
+ label: 'HTML 计划模式',
169
+ /** @author ddj 2026年11月25号 @param sessionId 会话 @returns 注入面 */
170
+ inject: (sessionId: string): HtmlPlanChipInjected => ({
171
+ exitHtmlPlanMode: () => exitHtmlPlanMode(String(sessionId ?? '')),
172
+ }),
173
+ }, HtmlPlanModeChip))
174
+ return typeof disposer === 'function' ? disposer as () => void : null
175
+ } catch (error) {
176
+ log.warn('HTML 计划模式 chip 注册失败(' + messageOf(error) + '),已跳过')
177
+ return null
178
+ }
179
+ }
180
+
181
+ //endregion