dsh-goal-contract 0.2.5 → 0.2.9

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/CHANGELOG.md CHANGED
@@ -4,6 +4,118 @@
4
4
 
5
5
  本文件记录 `dsh-goal-contract` 的对外变化。每条先中文整段、再英文整段。
6
6
 
7
+ ## 0.2.9 — 2026-11-29
8
+
9
+ 中文 / English
10
+
11
+ ### 中文
12
+
13
+ 这一版没有新功能,修两处**已经存在的行为不对**。
14
+
15
+ **销账(`goal_settle_queue`)在条目变长以后就无法按文字销账。** 失败时列出候选条目的信息只带截断到 30 字的文字,而按文字匹配要求相似度 ≥ 0.5;一个 130 字的条目,前 30 字的相似度只有 0.40 —— 差 10 个字。用户拿工具自己显示的那段文字去销账,两边一模一样却一直失败,而失败信息还让他「改口再试」,等于永远试不对。三处一起修:① 候选条目加上 id,失败信息里能直接照抄 id 再销一次;② 文字被截断时点明「共 N 字」,不再把残句当整条;③ 调用方给的前缀只要 ≥ 10 字且真的是某条目的开头,直接命中,不再走相似度打分。原有的相似度路径、阈值与失败文案一个字没动。
16
+
17
+ **捕获提醒里带着 ①②③④ 四个编号。** 编号对写插件的人来说很清楚,对看到这条提醒的用户毫无意义 —— 模型经常只回一句「按第 ④ 条处理」,用插件的人根本不知道 4 指什么。现在判据改成用「哪种情况」的自然语言描述,并且明确要求模型**用一句大白话告诉用户「这句话我按什么处理了」**,例如「你让我把间距改深一点 —— 这是个具体数值改动,我改完代码就跑测试,不占验收条」。四个判据本身、错误关键词那条限制、以及「只记队列等于没记」的理由一句没少;只是去掉了可以照抄的编号,模型没有数字可引用,只能复述情况。
18
+
19
+ 合并提醒(同一轮捕获到好几句)里,原话清单前面的 ①②③④⑤ **保留**——那是给模型区分「这是第几句原话」用的,和判据编号是两回事。
20
+
21
+ 验证口径:1833 条测试全部通过、0 失败;销账那三处另用临时脚本实测过「130 字条目 + 前 30 字能销掉」「6 字前缀不会误销」「不存在的 id 会列出可照抄的 id 与总字数」。
22
+
23
+ **`goal_amend` 整段替换验收时,会把已勾过的验收点悄悄冲掉。** 修之前:勾了 3 条、传一个新清单进来,那 3 条勾全部消失,接口只回一句「⚠ 有 3 个勾被作废」——进度丢了,但操作算成功。现在这种情况直接拒绝,并把丢掉的那几条逐条点名(例如「第 1 条「跑通测试」(已打勾)」),同时给出两条出路:`appendMilestones`(只加不删),或 `confirmReplace: true`(我确实知道会丢,就这么办)。被拒时账本一行都不写、契约也没动——拒绝不等于执行。
24
+
25
+ 什么不算「丢」:旧条目的里程碑被原样带到新清单里(按 id 或标题能对上),说明进度被迁移保住了,不拦。判据和迁移逻辑用同一把尺,不会出现「迁移说你还在、闸说你丢了」。
26
+
27
+ 被冲掉时账本会留一条 `acceptance-replaced` 记录,写明冲掉几条、分别叫什么 —— 事后能查「上次那批改动到底丢了什么」。
28
+
29
+ 本版同时把 `goal_amend` 的这几条行为都写进了测试:新增 8 条(三个判据的纯函数、四条端到端、一条账本留痕),并做了变异测试——故意改错一条断言,确认它会红,证明这些断言真的在跑而不是空壳。
30
+
31
+ ### English
32
+
33
+ No new features in this release — two **existing behaviours that were wrong** are fixed.
34
+
35
+ **Settling a queue entry by its text stopped working once the entry got long.** When settling failed, the candidate list only carried the first 30 characters of each entry, while text matching required a similarity of at least 0.5. For a 130-character entry the first 30 characters score only 0.40 — ten characters short. A user copying the very text the tool had just shown them could never settle the entry, and the failure message told them to "try phrasing it differently", which could never work. Three things changed together: (1) candidates now carry their id, so the failure message can show an id to copy; (2) truncated text now says how long the full entry is instead of presenting a fragment as the whole thing; (3) a caller-supplied prefix of at least 10 characters that really is the start of an entry now matches directly instead of going through the similarity score. The existing similarity path, its threshold and its failure wording are untouched.
36
+
37
+ **The capture reminder carried the numbers ①②③④.** The numbering is clear to whoever writes the plugin and meaningless to the user who sees the reminder — models routinely answered with "handled under item ④", which tells a plugin user nothing. The criteria are now phrased as natural-language descriptions of each situation, and the model is explicitly asked to **tell the user in one plain sentence what it did with that sentence**, for example "you asked me to darken the spacing — that is a concrete value change, so I change the code and run the tests rather than touching an acceptance item". All four criteria, the bug-keyword restriction and the "a queue entry nobody can see equals no record at all" rationale are still there; only the copyable numbers are gone, so there is no digit left to quote instead of stating the situation.
38
+
39
+ In the merged reminder (several sentences captured in one turn), the ①②③④⑤ before the quoted sentences **stay** — those number the sentences for the model, and are a different thing from numbering the criteria.
40
+
41
+ Verification: all 1833 tests pass, 0 failures; the three settle changes were additionally checked with a throwaway script — a 130-character entry settles from its first 30 characters, a 6-character prefix does not match anything, and an unknown id now lists copyable ids together with each entry's length.
42
+
43
+ **Replacing the whole acceptance list through `goal_amend` used to wipe out already-checked-off items without saying so.** Before the fix: three acceptance points checked, then a new list passed in, and all three ticks vanished while the call only answered "⚠ 3 ticks were voided" — the progress was gone but the operation counted as a success. It now refuses, names every lost item (for example "item 1 "run the tests" (already checked)"), and offers two ways forward: `appendMilestones` (add only, never remove), or `confirmReplace: true` (I know progress will be lost, do it anyway). A refused call writes nothing to the ledger and leaves the contract untouched — a refusal is not an execution.
44
+
45
+ What does not count as "lost": when an old item's milestones are carried over into the new list unchanged (matching by id or title), the progress survived the migration, so it is not treated as lost. The check and the migration use the same rule, so there is no case where the migration says the item is still there while the guard claims it was lost.
46
+
47
+ When a replacement does go through, the ledger records an `acceptance-replaced` entry stating how many items were voided and what they were called — so "what exactly did that batch of changes throw away" can be answered afterwards.
48
+
49
+ All of these behaviours are covered by tests in this release: eight new cases (pure functions for the three rules, four end-to-end paths, one ledger-record check), plus a mutation test that deliberately breaks one assertion and confirms it goes red, proving the assertions actually run instead of being empty shells.
50
+
51
+ ## 0.2.8 — 2026-11-29
52
+
53
+ 中文 / English
54
+
55
+ ### 中文
56
+
57
+ 这一版没有新功能,是**版本适配**:状态路由跟上宿主自带的准入机制,另外修「测试会随仓库放在哪个目录而变红」并给 CI 加了一道检查。
58
+
59
+ **状态路由改为跟随宿主自带的准入机制。** 以前这两条路由(面板读目标状态、读锁)自己不管准入;现在把判断交给宿主已有的机制,不再自己另搞一套。在宿主没有提供该机制的运行时(例如桌面版),这段自动放行,行为与以前完全一样。
60
+
61
+ **两条测试原来只在特定目录布局下才过。** 一条把「插件目录的父目录」写死成了 `plugins`,只派生最后一段目录名——仓库一离开 `plugins/` 就后缀匹配不上,本该放行的写操作被误判成越界;另一条把夹具造在「当前目录」下的绝对路径里,一旦整个项目被放进系统临时目录,源码文件就会被判成临时物、记一条假欠账。现在两条都改成不依赖检出位置:父目录名也派生,夹具改用相对路径。
62
+
63
+ **CI 新增一道「环境无关性」检查。** 它故意把仓库检出到一个不叫 `plugins` 的目录名下再跑测试——以后任何人(包括外部贡献者)再写出依赖检出路径的测试,会在 CI 上第一次就红,而不是等本地绿、CI 红、没人知道为什么。
64
+
65
+ CI 里跑测试改用 `scripts/ci-run-tests.mjs`:退出码与 `node --test test/` 完全一致(通过 / 失败判定不变),但失败时会把失败用例名打成 GitHub 注解,红了一看就知道是哪条,不用登录网页翻长日志、也不用令牌。
66
+
67
+ **Node 最低要求提到 24。** DSH 本体要求的就是 24,插件没理由比宿主更宽松;而且实测 Node 22 的 `zlib.zstdDecompressSync` 解不了多段 zstd —— 同一个 69 MB 会话日志,Node 24 解出 126 MB,Node 22 只解出 252 字节,凡是走这条路径的能力在 22 上都不可用。CI 同步只测 24(windows + linux 两个平台)。
68
+
69
+ 验证口径:干净克隆 + 非 `plugins` 目录名 + 空 `HOME` 三重叠加下,1825 条测试 1825 通过、0 失败;真实会话日志回归在本机有日志时一并跑过,日志不在本机时按设计跳过。改前同样条件是 1813 通过、2 失败。
70
+
71
+ `src/**` 这版只动了 `state-route.js` 一处(状态路由接入宿主准入机制),另加了 4 条跟着它的回归测试;其余源码未动。
72
+
73
+ ### English
74
+
75
+ No new features in this release — it is a **compatibility update**: the state route now follows the admission mechanism the host already provides, and tests that only passed under one particular checkout layout are fixed, with a CI check for that class of problem.
76
+
77
+ **The state route now follows the admission mechanism the host already provides.** The two routes (the panel reading goal state and locks) used to do no admission of their own; the decision is now delegated to the host's existing mechanism instead of a second one being invented here. On a runtime that does not provide it (the desktop app, for example), this section admits everything, so behaviour is exactly as before.
78
+
79
+ **Two tests depended on where the repo happened to live.** One hardcoded `plugins` as the parent directory and only derived the last path segment — the moment the repo left `plugins/`, suffix matching failed and a write that should have been allowed was rejected as out of scope. The other built its fixture at an absolute path under the current directory, so whenever the project sat inside the system temp directory, a source file was classified as a temporary artefact and a false debt was recorded. Both are now location-independent: the parent directory name is derived too, and the fixture uses a relative path.
80
+
81
+ **CI gains an "environment independence" check.** It deliberately checks the repository out into a directory that is *not* named `plugins` and runs the tests there — so anyone (including outside contributors) who writes a checkout-path-dependent test sees it go red in CI immediately, instead of local green / CI red with nobody knowing why.
82
+
83
+ CI now runs the tests through `scripts/ci-run-tests.mjs`: the exit code matches `node --test test/` exactly (the pass/fail verdict is unchanged), but a failure prints the failing test names as GitHub annotations, so a red run tells you which test failed without opening the web UI for a long log or using a token.
84
+
85
+ **Minimum Node is now 24.** DSH itself requires 24, so the plugin has no reason to be laxer than its host. Measured: Node 22's `zlib.zstdDecompressSync` cannot decode multi-frame zstd — the same 69 MB session log yields 126 MB of output on Node 24 but only 252 bytes on Node 22, so every capability on that path is unusable there. CI now tests 24 only, on both windows and linux.
86
+
87
+ Verification: under a clean clone, a non-`plugins` directory name and an empty `HOME` at once, all 1,825 tests pass with 0 failures; the real-session-log regression runs when the log is present on the machine and skips by design when it is not. Before the fix the same conditions gave 1,813 pass and 2 fail.
88
+
89
+ Under `src/**` this release touches exactly one file — `state-route.js` (the state route now goes through the host's admission mechanism) — plus four regression tests that cover it; no other source file changed.
90
+
91
+ ## 0.2.6 — 2026-11-28
92
+
93
+ 中文 / English
94
+
95
+ ### 中文
96
+
97
+ 这一版修的是一个「该拦却拦不住」的缺陷,功能本身没有增加。
98
+
99
+ **思考通道以前完全不参与判据。** 那条判据只看正文,而 2026-11-28 那次事故里,重复全部发生在思考区——那一步进来 2400 个思考分片、正文分片是 0,计数器一个数都没吃到。现在思考与正文一样参与。
100
+
101
+ **原来的判据数不到真实的重复形态。** 旧判据是「同一个小碎片连续出现 30 次」,而真实形态是「同一小段循环几百遍」:拿那 2400 个真实分片喂给旧判据,最长只连了 2 次。新增「尾部同一单元连续重复」判据(单元 3~64 字符,默认阈值仍是 30),两条判据任一命中即拦截。**只认连续、不累计**:中间插进任何别的内容就归零重算,跨步跨块都不累加。
102
+
103
+ **拦下之后你能看见了。** 以前它只发一条模型可见的纠偏消息,聊天区不渲染,所以你什么都看不到。现在会在目标面板的「异常提醒(纯提醒,不用处理)」组里留一条记录,写明连续重复已达上限多少次、重复的是什么;它不挡收尾、不进未完结计数。
104
+
105
+ 真实日志回归:458 那轮连刷 699 次、459 那轮 920 次,都命中;460 那轮只连 24 次,正确地不命中。测试:1816 个全通过,进程退出码 0。
106
+
107
+ ### English
108
+
109
+ This release fixes a defect where the interruption failed to fire when it should have. No new capability was added.
110
+
111
+ **The reasoning channel never participated.** The check only looked at visible text, but in the incident on 2026-11-28 every repetition happened in the reasoning stream — that step received 2,400 reasoning deltas and zero text deltas, so the counter never saw a single one. Reasoning now takes part exactly like visible text.
112
+
113
+ **The old check also could not see the real shape.** It required the same small fragment 30 times in a row, while the real pattern was one short unit looping hundreds of times: fed the 2,400 real deltas, it never counted more than 2. A new "same unit repeating at the tail" check was added (unit length 3–64 characters, default threshold still 30); either check firing interrupts the turn. **Only contiguous runs count, never accumulated totals**: any different content in between resets the count, and it never adds up across steps or blocks.
114
+
115
+ **You can now see it happen.** Previously the interrupt only sent a steer message that the chat view does not render, so nothing was visible. It now leaves a record in the "anomaly reminders (informational only)" group of the goal panel, stating how many repetitions the run reached and what the repeated content was; it does not block completion and is not counted as an open debt.
116
+
117
+ Real-log regression: the 458 turn repeated 699 times and the 459 turn 920 times, both interrupted; the 460 turn repeated only 24 times and correctly was not. Tests: 1,816 passing, process exit code 0.
118
+
7
119
  ## 0.2.5 — 2026-11-28
8
120
 
9
121
  中文 / English
package/README.en.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [中文](README.md) | **English**
4
4
 
5
- Current version: 0.2.5
5
+ Current version: 0.2.9
6
6
 
7
7
  **While you are away, the AI's goal keeps moving forward — under control.**
8
8
 
@@ -95,6 +95,8 @@ The panel lives in the "Goal Tracking" tab on the right of the session. There is
95
95
  | Middle | Outstanding Items | Three groups: "I owe you" (debts owed), "Your pending items" (things you said that haven't been filed yet), "Notices (reminders only, nothing to act on)" |
96
96
  | Right | Goal and Milestone Change Ledger | A three-column table: what changed / why / what it became |
97
97
 
98
+ **Acceptance items can be ticked off to close**: an item that is not fully ticked shows a grey line underneath saying how many milestones / stages are still unticked; once all of them are ticked, that line says they are all complete and the goal can be closed, and the item turns grey with a strikethrough.
99
+
98
100
  **How to read the "Outstanding Items" table**:
99
101
 
100
102
  - One row per item: a settlement checkbox first, then the type / item / created time / actions.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  **中文** | [English](README.en.md)
4
4
 
5
- 当前版本:0.2.5
5
+ 当前版本:0.2.9
6
6
 
7
7
  **你走开的时候,AI 的目标还在(相对)受控地往前走。**
8
8
 
@@ -95,6 +95,8 @@ dsh plugin --profile <你的 profile 名> add dsh-goal-contract
95
95
  | 中 | 未完结事项 | 分三组:「我欠你的」(欠着的账)、「你说的待定」(你说过但还没归位的话)、「异常提醒(纯提醒,不用处理)」 |
96
96
  | 右 | 目标和里程碑变更台账 | 三列表:改了什么 / 为什么 / 变成什么样 |
97
97
 
98
+ **验收可勾选(结案)**:左列里没勾完的那条,下面灰字写「还差 N 个里程碑 / 阶段没勾」;全部勾完时那行字改成「全部里程碑 / 阶段已完成,可以结案了」,条目变删除线变灰。
99
+
98
100
  **「未完结事项」这张表怎么读**:
99
101
 
100
102
  - 一行一条,前面是结清勾选框,后面是类型 / 事项 / 产生时间 / 操作。
package/dist/client.js CHANGED
@@ -1,4 +1,4 @@
1
- /* source-sha256: cbb0959b08e7f1f4 */
1
+ /* source-sha256: 9d57d7aa4307c0df */
2
2
  /* dsh-goal-contract 客户端产物(由 `node src/client.js` 从 src/client.js 生成,请勿手改) */
3
3
  window.__ModuleLoader__.load({
4
4
  id: 'dsh-goal-contract',
@@ -1618,6 +1618,11 @@ window.__ModuleLoader__.load({
1618
1618
  if (type === ANOMALY_KIND) return true;
1619
1619
  const check = String(debt.check ?? '').trim();
1620
1620
  if (check === TOOL_ANOMALY_CHECK_ID || check === ANOMALY_CHECK_ID) return true;
1621
+ // ★ 2026-11-28:文本重复被拦下的那条。**必须与服务端 src/debts.js 的
1622
+ // TEXT_REPEAT_ANOMALY_CHECK_ID 同步**(本文件自己写着「改一处就要改另一处」)。
1623
+ // 当前运行时前端拿到的是归约后的 type='anomaly',走上面第一条就命中;
1624
+ // 这里补登记是为了「前端将来直接读原始账本条目」时不会漏判。
1625
+ if (check === 'text-repeat-interrupted') return true;
1621
1626
  const content = String(debt.content ?? '');
1622
1627
  return content.includes('可能坏了') && (type === '' || type === 'loose-end');
1623
1628
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-goal-contract",
3
- "version": "0.2.5",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "description": "Helps keep an AI agent's goal under control while you are away. It cannot silently change the goal, completing a milestone needs a real command exit code, and it stops on repeated failures, loops or text repetition instead of grinding all night.",
6
6
  "license": "MIT",
@@ -19,6 +19,7 @@
19
19
  "icon.svg",
20
20
  "cordis.patch.yml",
21
21
  "README.md",
22
+ "README.en.md",
22
23
  "ORIGIN.md",
23
24
  "CHANGELOG.md"
24
25
  ],
@@ -65,6 +66,6 @@
65
66
  },
66
67
  "author": "Eason-22",
67
68
  "engines": {
68
- "node": ">=22"
69
+ "node": ">=24"
69
70
  }
70
71
  }
package/src/client.js CHANGED
@@ -1612,6 +1612,11 @@ export function isAnomalyDebt(debt) {
1612
1612
  if (type === ANOMALY_KIND) return true;
1613
1613
  const check = String(debt.check ?? '').trim();
1614
1614
  if (check === TOOL_ANOMALY_CHECK_ID || check === ANOMALY_CHECK_ID) return true;
1615
+ // ★ 2026-11-28:文本重复被拦下的那条。**必须与服务端 src/debts.js 的
1616
+ // TEXT_REPEAT_ANOMALY_CHECK_ID 同步**(本文件自己写着「改一处就要改另一处」)。
1617
+ // 当前运行时前端拿到的是归约后的 type='anomaly',走上面第一条就命中;
1618
+ // 这里补登记是为了「前端将来直接读原始账本条目」时不会漏判。
1619
+ if (check === 'text-repeat-interrupted') return true;
1615
1620
  const content = String(debt.content ?? '');
1616
1621
  return content.includes('可能坏了') && (type === '' || type === 'loose-end');
1617
1622
  }
package/src/debts.js CHANGED
@@ -108,6 +108,18 @@ export const ANOMALY_CHECK_ID = 'tool-anomaly';
108
108
  */
109
109
  export const TOOL_ANOMALY_CHECK_ID = 'tool-failed-broken';
110
110
 
111
+ /**
112
+ * 「文本重复被拦下」那条**异常提醒**的 check id(2026-11-28)。
113
+ *
114
+ * ★ 为什么单独一个 id:这条不是「工具坏了」,是**轮次控制**拦下的输出重复 ——
115
+ * 共用旧 id 的话,面板上分不清是哪种异常。
116
+ * ★ 身份靠 `check` 认(见 {@link isAnomalyDebt} 与 `reduceDebts`):
117
+ * 所以这个 id **必须**在这两处的 check 白名单里登记 ——
118
+ * 不登记的话,这条读回来既不算异常提醒,又因为落盘时类型留空而**整条消失**
119
+ * (比看不见更糟:用户连「被拦过」都不知道)。
120
+ */
121
+ export const TEXT_REPEAT_ANOMALY_CHECK_ID = 'text-repeat-interrupted';
122
+
111
123
  /**
112
124
  * 异常提醒的**一句话说明**(面板分组标题 / 收尾文案共用)。
113
125
  */
@@ -141,6 +153,8 @@ export function isAnomalyDebt(debt) {
141
153
  if (type === ANOMALY_KIND) return true;
142
154
  const check = String(debt.check ?? '').trim();
143
155
  if (check === TOOL_ANOMALY_CHECK_ID || check === ANOMALY_CHECK_ID) return true;
156
+ // ★ 2026-11-28:文本重复被拦下的那条(专属 check id)也是异常提醒。
157
+ if (check === TEXT_REPEAT_ANOMALY_CHECK_ID) return true;
144
158
  const content = String(debt.content ?? '');
145
159
  return content.includes('可能坏了') && (type === '' || type === 'loose-end');
146
160
  }
@@ -224,6 +238,8 @@ export const AUTO_CHECK_LABELS = Object.freeze({
224
238
  'temp-leftover': '本会话写过的临时路径仍存在',
225
239
  'tool-failed': '最近有失败的工具调用',
226
240
  'tool-failed-broken': '工具异常提醒(这个工具可能坏了)',
241
+ // ★ 2026-11-28:文本重复被拦下的那条(专属 check id,见 TEXT_REPEAT_ANOMALY_CHECK_ID)。
242
+ 'text-repeat-interrupted': '文本重复被拦截(已截断本轮)',
227
243
  });
228
244
 
229
245
  /** 欠了多久开始**标黄**(24 小时)。 */
@@ -444,6 +460,9 @@ export function reduceDebts(entries) {
444
460
  const anomaly = rawType === ANOMALY_KIND
445
461
  || rawCheck === TOOL_ANOMALY_CHECK_ID
446
462
  || rawCheck === ANOMALY_CHECK_ID
463
+ // ★ 2026-11-28:文本重复被拦下的那条(专属 check id)同样归 anomaly ——
464
+ // 不登记的话,这条读回来既不是异常提醒,又因类型留空被整条丢掉。
465
+ || rawCheck === TEXT_REPEAT_ANOMALY_CHECK_ID
447
466
  || (rawContent.includes('可能坏了') && (rawType === '' || rawType === 'loose-end'));
448
467
  const type = anomaly ? ANOMALY_KIND : (isDebtKind(rawType) ? rawType : '');
449
468
  if (type === '') continue;
package/src/index.js CHANGED
@@ -123,7 +123,12 @@ import {
123
123
  resolveMaxGoalRounds,
124
124
  } from './tools.js';
125
125
  import {
126
+ // ★ 2026-11-28:文本重复被拦下时写一条「异常提醒」——
127
+ // 用户原话「代码原本的设计是拦截了要暴露在前端」,而 steer 发的是
128
+ // source:{kind:'system'} 的消息(DSH 0.1.7 起聊天区不渲染 ⇒ 看不见)。
129
+ ANOMALY_KIND,
126
130
  AUTO_CHECK_IDS,
131
+ TEXT_REPEAT_ANOMALY_CHECK_ID,
127
132
  buildDebtBriefing,
128
133
  classifyErrorClass,
129
134
  describeDebt,
@@ -335,23 +340,27 @@ export function injectedSessionMessage(text) {
335
340
  */
336
341
  export const CAPTURE_NOTICE_TEMPLATE = [
337
342
  '【捕获】你这一轮没处理用户的话:「{{quote}}」',
338
- '用户说过的话不该凭空丢掉。按下面四条判,选一个,并且必须落到工具上:',
339
- '① 出现了新的交付物 → goal_amend 加一条里程碑',
340
- '② 某条验收已经有完成证据 → goal_accept 标完成(「以前坏的 / 没做的,这一轮跑通了」也算完成证据)',
341
- '③ 用户改了口径 / 推翻了原计划 → goal_amend 修正验收(优先于 ①:先改口径再加东西)',
342
- '④ 都不是 → goal_route 记入未完结事项(纠正一个**事实细节**、改代码里的某个值都算 ④;只有改变了「什么算做完」才是 ③)',
343
- '★ 用户说「bug / 坏了 / 不对 / 要修 / 有问题」时,不许只走 ④ 记进未完结事项 —— 先判是新交付物(→ ① 加里程碑)还是已有条目没做完(→ ② 标完成 / ③ 修正验收);都不沾才轮到 ④。',
343
+ '用户说过的话不该凭空丢掉。先判这句话属于哪种情况,然后落到工具上:',
344
+ '- 用户要的是**一件新东西** → goal_amend 加一条里程碑',
345
+ '- **某条验收其实已经做完了**(以前坏的 / 没做的,这一轮跑通了)→ goal_accept 标完成',
346
+ '- 用户**改了口径或推翻了原计划** → goal_amend 修正验收;这种优先于「要新东西」——先改口径再加东西',
347
+ '- **以上都不是**(只是在纠正一个事实细节、改代码里的某个值)→ goal_route 记入未完结事项;只有改变了「什么算做完」才属于「改口径」',
348
+ '★ 用户说「bug / 坏了 / 不对 / 要修 / 有问题」时,别直接当成「记进未完结事项」——先看这是「要一件新东西」还是「已有条目没做完」;都不是才记队列。',
344
349
  ' 理由:队列里的条目面板看不到,用户也不会再说一遍 —— 只记队列等于没记。',
345
- '反例(④ 不是 ①):用户说「面板进度条颜色再深一点」——这是改代码里的一个值,**不许** goal_amend 动验收条。',
346
- // ★★ 2026-11-26(交接文档 2.1,用户原话:「捕获的注入提示里面现在有4个选项,每次模型
347
- // 就回复了一个选4,别人根本就不知道4是什么」):**只改末尾这句**,四条判据一个字不动。
348
- // ★ 为什么不能只写「已按第 N 条处理」:用户不知道 N 指什么。必须让模型把**文字判据**
349
- // 和**具体做了什么**写出来——所以给了正例/反例,光说「别写数字」模型照样写数字。
350
- // ★ 别动的地方:① / ③ 括号里的优先级说明是盲测证明「不给判据也判得对」的唯一承重句;
351
- // ④ 上面那段 bug-/坏了 关键词的前置条件是另一条独立护栏。都不在本条改动范围内。
352
- '判完在回复里写清楚你判成了哪一种、具体做了什么:',
353
- '✓ 正确:「这条是补充说明 —— 我把间距从 8px 改成 12px,记进了未完成事项」',
354
- '✗ 错误:「选4」「④」「按第 4 条处理」(用户不知道数字指什么,必须写文字)',
350
+ // ★★ 2026-11-29(用户原话:「不是已经把这个选哪个选项的编号的部分去掉了吗?……AI 会直接',
351
+ // 告诉我他选了某个编号,那别的用户用我们这个插件根本就不知道这个编号是什么意思啊!',
352
+ // 正确的应该是引导主代理表达他对用户上一句话怎么处理」):',
353
+ // **编号已全部拿掉**(原来那四条是 ①②③④),改成用「哪种情况」的自然语言描述。',
354
+ // ★ 为什么光加反例没用(实测我被自己的反例拦下来过):反例只说了「别写数字」,',
355
+ // 模型照样写「我判成 ①」——因为判据本身就长成编号的样子,它顺手就引用了。',
356
+ // 去掉编号之后没有东西可引用,模型只能复述「这是什么情况」。',
357
+ // ★ 要求的产出也换了:不再是「写清判成了哪一种」,而是「用一句大白话说清这句话你',
358
+ // 按什么处理了」——用户读得懂,不需要看见这张提示也能明白。',
359
+ '**判完之后,用一句大白话告诉用户:这句话你按什么处理了。**',
360
+ '不要说「第几条」「第 N 项」这种带编号的话 —— 用户看不到这张提示,不知道数字指什么。',
361
+ '写清「用户说了什么 → 你做了什么」,例如:',
362
+ '「你让我把间距改深一点 —— 这是个具体数值改动,我改完代码就跑测试,不占验收条。」',
363
+ '「你说面板卡住了 —— 这是个新问题,我加了一条验收来跟它,而不是记进待办就算。」',
355
364
  '这一轮结束仍没处理 → 自动记成未完结事项。',
356
365
  ].join('\n');
357
366
 
@@ -1753,7 +1762,7 @@ export const name = 'dsh-goal-contract';
1753
1762
  export const SERVICE_NAME = 'goalContract';
1754
1763
 
1755
1764
  /** 版本号。必须与 package.json 的 version 一致(测试会核对这两处不漂)。 */
1756
- export const PLUGIN_VERSION = '0.2.5';
1765
+ export const PLUGIN_VERSION = '0.2.9';
1757
1766
 
1758
1767
  // ── 配置声明(挂进 DSH 设置页) ─────────────────────────────────────────────
1759
1768
  //
@@ -4011,6 +4020,46 @@ export function buildDebtCompletionReminder({
4011
4020
  };
4012
4021
  }
4013
4022
 
4023
+ /**
4024
+ * 造「文本重复被拦下」那条**异常提醒**的载荷(纯函数,零依赖)。
4025
+ *
4026
+ * ★ 为什么单独拎出来(2026-11-28,用户原话「代码原本的设计是拦截了要暴露在前端」):
4027
+ * ① 记录**怎么写**与**怎么写进账本**分开 —— 写进账本那一段在 `apply()` 里(要账本服务),
4028
+ * 这一段是纯计算,可以被直接验(临时脚本 / 真机都能调)。
4029
+ * ② 写进账本之后它会出现在面板的「异常提醒(纯提醒,不用处理)」那一组
4030
+ * (client.js 的 DASHBOARD_ANOMALY_GROUP_TITLE)—— 这是**唯一**一条
4031
+ * 「拦了但聊天区看不见」的闸的可见出口。
4032
+ *
4033
+ * ★ detail 的字段**可能缺**(`count` / `unit` / `kind` 都可能没有)——
4034
+ * 一律容错,绝不因为字段缺失抛错(抛错会连累流内截断)。
4035
+ *
4036
+ * @param {{count?: number, unit?: string, kind?: string}} [detail] 命中详情。
4037
+ * @returns {{type: string, content: string, evidence: string, dueHint: string, check: string}}
4038
+ */
4039
+ export function buildTextRepeatAnomalyRecord(detail) {
4040
+ const rawCount = Number(detail?.count);
4041
+ const countText = Number.isFinite(rawCount) && rawCount > 0
4042
+ ? String(Math.floor(rawCount))
4043
+ : '?';
4044
+ const rawUnit = typeof detail?.unit === 'string' ? detail.unit : '';
4045
+ const unitText = rawUnit.trim() === '' ? '' : rawUnit.slice(0, 40);
4046
+ const kindText = String(detail?.kind ?? '').trim();
4047
+ const content = `文本重复被拦截(已截断本轮):同一段输出连续重复已达上限 ${countText} 次(到这个数就截断,不是总共只重复了这么多次)`
4048
+ + (unitText !== '' ? `,重复内容「${unitText}」` : '')
4049
+ + '。'
4050
+ + '这条是纯提醒:不用清账、不挡收尾;想处理可以自行处理。';
4051
+ const evidence = `llm/stream(文本重复检测):连续重复已达上限 ${countText} 次(配置阈值)`
4052
+ + (unitText !== '' ? `,样本「${unitText}」` : '')
4053
+ + (kindText !== '' ? `,类型 ${kindText}` : '');
4054
+ return {
4055
+ type: ANOMALY_KIND,
4056
+ content,
4057
+ evidence,
4058
+ dueHint: '',
4059
+ check: TEXT_REPEAT_ANOMALY_CHECK_ID,
4060
+ };
4061
+ }
4062
+
4014
4063
  export function apply(ctx, config = {}) {
4015
4064
  const logger = ctx?.logger ?? console;
4016
4065
  // ★ 配置里的可写叶子在宿主那边是「易失盒子」({ get() }),**不是值本身** ——
@@ -9084,8 +9133,86 @@ const rememberAction = (sessionId, execution) => {
9084
9133
  ? stuckState.interrupt : {};
9085
9134
  return (raw.textRepeat && typeof raw.textRepeat === 'object') ? raw.textRepeat : {};
9086
9135
  };
9136
+ // ★★★ 2026-11-28(用户原话:「代码原本的设计是拦截了要暴露在前端」):
9137
+ // 文本重复被拦下时,除了给模型发纠偏提示,还要给**用户**留一条看得见的痕迹。
9138
+ // 病根(本文件 :9060 那段注释自证):steer 发的是 `source:{kind:'system'}` 的消息,
9139
+ // 而 DSH 0.1.7 起聊天区**不渲染 context 类消息** ⇒ 提示写了也看不见 ⇒ 拦了等于静默。
9140
+ // 另外两条闸(toolFail / loop)走 `{kind:'deny', reason:…}`,屏幕上能看到 ——
9141
+ // **只有 textRepeat 这条是看不见的**。
9142
+ // 做法:走**已有的异常提醒通道**(type=anomaly,纯提醒、不用清账、不挡收尾),
9143
+ // 面板上会落在「异常提醒(纯提醒,不用处理)」那一组(client.js 的 DASHBOARD_ANOMALY_GROUP_TITLE)。
9144
+ //
9145
+ // ★ onBreak 只拿得到 agent(拿不到 sessionId)⇒ 用 agent 反查会话 id。
9146
+ // WeakMap:不拦强引用,agent 被回收后这条映射自动消失。
9147
+ /** @type {WeakMap<object, string>} */
9148
+ const agentSessionIds = new WeakMap();
9149
+ /**
9150
+ * 文本重复被拦下 → 写一条**异常提醒**(用户看得见;纯提醒,不用清账)。
9151
+ *
9152
+ * ★ 写失败**绝不能连累截断**:全程 try/catch,失败只 warn。
9153
+ *
9154
+ * @param {any} agent 被拦下那一轮的 agent(用来反查会话 id)。
9155
+ * @param {{count?: number, unit?: string, kind?: string}} detail 命中详情(字段可能缺)。
9156
+ * @returns {Promise<void>}
9157
+ */
9158
+ const noteTextRepeatAnomaly = async (agent, detail) => {
9159
+ try {
9160
+ const sid = (agent && typeof agent === 'object')
9161
+ ? (agentSessionIds.get(agent) ?? '')
9162
+ : '';
9163
+ if (sid === '') {
9164
+ logger.warn?.(`[${name}] 文本重复已命中,但反查不到会话 id,没能记异常提醒(不影响截断)`);
9165
+ return;
9166
+ }
9167
+ if (!debts || typeof debts.add !== 'function') {
9168
+ logger.warn?.(`[${name}] 欠账服务不可用,没能记异常提醒(不影响截断)`);
9169
+ return;
9170
+ }
9171
+ // ★ 载荷怎么写见 {@link buildTextRepeatAnomalyRecord}(纯函数;字段缺失也容错,不许抛错)。
9172
+ const record = buildTextRepeatAnomalyRecord(detail);
9173
+ const content = record.content;
9174
+ // ★ 同一会话里**一模一样**的那条不重复记(免得刷屏)——
9175
+ // 读清单失败时**照写不误**(读失败只是没法去重,不许因此丢掉这条提醒)。
9176
+ try {
9177
+ const existing = await debts.list(sid);
9178
+ const duplicated = (Array.isArray(existing) ? existing : []).some((one) => one
9179
+ && String(one.check ?? '') === TEXT_REPEAT_ANOMALY_CHECK_ID
9180
+ && String(one.content ?? '') === content
9181
+ && String(one.status ?? 'open') !== 'done');
9182
+ if (duplicated) {
9183
+ logger.info?.(`[${name}] 同一条文本重复提醒已经在账本里,不重复记(${TEXT_REPEAT_ANOMALY_CHECK_ID})`);
9184
+ return;
9185
+ }
9186
+ } catch (error) {
9187
+ logger.debug?.(`[${name}] 读账本去重失败(照写不误):${error?.message ?? error}`);
9188
+ }
9189
+ const result = await debts.add({
9190
+ sessionId: sid,
9191
+ type: record.type,
9192
+ content: record.content,
9193
+ evidence: record.evidence,
9194
+ dueHint: record.dueHint,
9195
+ check: record.check,
9196
+ source: 'auto',
9197
+ anchor: null,
9198
+ });
9199
+ if (result?.ok === true) {
9200
+ logger.info?.(`[${name}] 已记一条异常提醒(${TEXT_REPEAT_ANOMALY_CHECK_ID}):${content}`);
9201
+ } else {
9202
+ logger.warn?.(`[${name}] 异常提醒没记上(不影响截断):${result?.error ?? '未知原因'}`);
9203
+ }
9204
+ } catch (error) {
9205
+ logger.warn?.(`[${name}] 异常提醒写入失败(不影响截断):${error?.message ?? error}`);
9206
+ }
9207
+ };
9087
9208
  const streamBreaker = createStreamBreaker({
9088
- getAgent: (sid) => resolveAgentOfSession(null, sid),
9209
+ getAgent: (sid) => {
9210
+ const resolved = resolveAgentOfSession(null, sid);
9211
+ if (resolved && typeof resolved === 'object') {
9212
+ agentSessionIds.set(resolved, String(sid ?? ''));
9213
+ }
9214
+ return resolved;
9215
+ },
9089
9216
  // ★ 判据照抄 dsh-loop-guard 的 `TextRepetitionDetector`:
9090
9217
  // **连续 N 个规范化后完全相同的 text-delta** 才算重复。
9091
9218
  // 不用行级判据 —— 流式输出常常是碎片(「好。」「执行。」轮转),
@@ -9106,6 +9233,14 @@ const rememberAction = (sessionId, execution) => {
9106
9233
  // 下一轮很可能照旧重复,等于没修。
9107
9234
  // 形状沿用 session.append 用过、宿主已经接受的那一份 UserMessage。
9108
9235
  onBreak: (agent, detail) => {
9236
+ // ① 用户可见的那一份:**异常提醒**(写账本 ⇒ 面板「异常提醒」组里看得见)。
9237
+ // ★ 写失败绝不能连累截断 —— try/catch 包住,失败只 warn。
9238
+ try {
9239
+ void noteTextRepeatAnomaly(agent, detail);
9240
+ } catch (error) {
9241
+ logger.warn?.(`[${name}] 异常提醒写入失败(不影响截断):${error?.message ?? error}`);
9242
+ }
9243
+ // ② 给模型的纠偏提示(原有行为,一个字没改)—— 让模型别再重复下去。
9109
9244
  if (!agent || typeof agent.steer !== 'function') return;
9110
9245
  agent.steer({
9111
9246
  id: randomUUID(),
@@ -9114,7 +9249,7 @@ const rememberAction = (sessionId, execution) => {
9114
9249
  content: [{
9115
9250
  type: 'text',
9116
9251
  text: '【轮次控制】刚才那段输出被判定为**连续重复**,已截断本轮。\n'
9117
- + `依据:同一段内容连续出现 ${detail?.count ?? '?'} 次`
9252
+ + `依据:同一段内容连续重复已达上限 ${detail?.count ?? '?'} 次(到这个数就截断,不是总次数)`
9118
9253
  + (detail?.unit ? `(「${String(detail.unit).slice(0, 40)}」)` : '')
9119
9254
  + '\n'
9120
9255
  + '\n'
package/src/ledger.js CHANGED
@@ -728,6 +728,20 @@ export const LEDGER_ALL_KINDS = Object.freeze([
728
728
  // 目标被换掉时,旧验收随之结案归档 —— 不记这一条,账本里就没人说得清
729
729
  // 「那几条旧验收到底怎么了」(这正是本次要修的真 bug)。
730
730
  ...LEDGER_ACCEPTANCE_CLOSED_KINDS,
731
+ // ★★ task-16:**验收被替换**的留痕(kind='acceptance-replaced')。
732
+ // 场景:goal_amend 整段替换 acceptance,冲掉了已被勾选的里程碑 ——
733
+ // 不记这一条,账本里就没人说得清「那几条勾过的进度是怎么没的」。
734
+ // ★ 位置:紧挨 acceptance-closed(同属「验收相关的留痕」这一组)。本数组是
735
+ // **按语义分组**排的,不是字母序 —— 新 kind 按语义落在对应分组里。
736
+ // ★ append() 对这个 kind **没有额外必填字段**:kind 在白名单里就能落账
737
+ // ('owe' 的 temp-doc 绝对路径校验、'accept' 的退出码校验都不适用于它)。
738
+ // ★ 载荷口径(写入端 src/tools.js 的 goal_amend 写这三个字段):
739
+ // lostCount(冲掉了几条)/ lostTitles(标题数组)/ lostItems({index,title} 数组)。
740
+ // ★★ 这三个字段已登记在文件末尾的 LEDGER_KIND_EXTRA_FIELDS 里(task-19 补的),
741
+ // 且冻结逻辑已改成「数组按数组冻结」→ 落账后 lostTitles / lostItems **仍是数组**。
742
+ // (以前:字段没登记 → 全被丢掉;就算登记了,`{...value}` 展开也会把数组
743
+ // 摊成带数字键的对象 —— 读回来是 `{"0":"A","1":"B"}`,不是数组。)
744
+ 'acceptance-replaced',
731
745
  // ★ 2026-11-11(t22):目标变更**被拒**的留痕(不进契约归约、不进变更台账)。
732
746
  ...LEDGER_DENY_KINDS,
733
747
  // ★ M1 契约对账硬闸的三种留痕(见 ACK_GATE_KINDS 的说明)。
@@ -1066,6 +1080,36 @@ export function diffContracts(from, to) {
1066
1080
  return out;
1067
1081
  }
1068
1082
 
1083
+ /**
1084
+ * 冻结一个「按 kind 放行的额外字段」的值(唯一真相见文件末尾的 LEDGER_KIND_EXTRA_FIELDS)。
1085
+ *
1086
+ * ★ 为什么数组要单独一条:以前这里对**所有对象**一律 `{...value}` 展开 ——
1087
+ * **数组会被摊成带数字键的对象**(`['A','B']` → `{0:'A',1:'B'}`),
1088
+ * 存进账本、JSON 序列化、再读回来,得到的还是对象(`{"0":"A","1":"B"}`)——
1089
+ * 「写入端写的是数组、读回来是对象」这种坑最难查(本文件 471/661 行记着同一族坑)。
1090
+ * 写入端的口径是「数组就是数组」(如 acceptance-replaced 的 lostTitles / lostItems),
1091
+ * 所以这里按三种形状分派:
1092
+ * - 数组 → 复制一份再冻结(**数组还是数组**,元素逐个按同规则冻结)
1093
+ * - 其它对象 → 原来的展开方式(`{...value}`),行为逐字节不变
1094
+ * - 其它(字符串 / 数字 / 布尔)→ 原样
1095
+ *
1096
+ * ★ 向后兼容(逐个字段实测过):现有已登记的 kind 里**没有任何一个字段在运行时会拿到数组** ——
1097
+ * round.evidence 是普通对象(`{...计数, positive}`)、route.near 是字符串、
1098
+ * ack / action / ack-gate-blocked 的字段全是标量 → 对它们**逐字节无影响**。
1099
+ *
1100
+ * @param {any} value
1101
+ * @returns {any}
1102
+ */
1103
+ function freezeExtraFieldValue(value) {
1104
+ if (Array.isArray(value)) {
1105
+ return Object.freeze(value.map((item) => freezeExtraFieldValue(item)));
1106
+ }
1107
+ if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
1108
+ return Object.freeze({ ...value });
1109
+ }
1110
+ return value;
1111
+ }
1112
+
1069
1113
  /**
1070
1114
  * 记一条账本记录(冻结,调用方改不动)。
1071
1115
  *
@@ -1144,9 +1188,8 @@ function freezeEntry(entry, goalId) {
1144
1188
  for (const field of LEDGER_KIND_EXTRA_FIELDS[String(entry.kind ?? '')] ?? []) {
1145
1189
  const value = entry[field];
1146
1190
  if (value === undefined || value === null) continue;
1147
- extraFields[field] = value !== null && typeof value === 'object' && !Object.isFrozen(value)
1148
- ? Object.freeze({ ...value })
1149
- : value;
1191
+ // ★ 值的形状按三种分派(数组要**还是数组**)——见 freezeExtraFieldValue。
1192
+ extraFields[field] = freezeExtraFieldValue(value);
1150
1193
  }
1151
1194
  return Object.freeze({
1152
1195
  goalId: String(goalId ?? ''),
@@ -3048,6 +3091,16 @@ export const LEDGER_KIND_EXTRA_FIELDS = Object.freeze({
3048
3091
  // reopenText —— reopen 时要重开的那条(「第 N 条」或标题原文)
3049
3092
  // addText —— add 时新增的那条里程碑标题
3050
3093
  route: Object.freeze(['action', 'quote', 'appendText', 'hint', 'near', 'conf', 'reopenText', 'addText']),
3094
+ // ★★★ task-19:**「冲掉了哪几条」的留痕**(kind='acceptance-replaced')。
3095
+ // 写入端(src/tools.js 的 goal_amend 替换保护)传三样:
3096
+ // lostCount —— 数字:冲掉了几条
3097
+ // lostTitles —— 字符串数组:冲掉的那几条的标题
3098
+ // lostItems —— 对象数组:每条 {index, title}
3099
+ // ★ 以前这三个字段**不在表里** → 落账时全被丢掉(条目记上了、内容是空的)。
3100
+ // ★ lostTitles / lostItems 是**数组**:冻结逻辑已改成「数组按数组冻结」
3101
+ // (以前一律 `{...value}` 展开 → 数组被摊成带数字键的对象),所以这里登记完
3102
+ // 读回来仍是数组。
3103
+ 'acceptance-replaced': Object.freeze(['lostCount', 'lostTitles', 'lostItems']),
3051
3104
  });
3052
3105
 
3053
3106
  /** 契约的稳定文本(只取 objective + acceptance,**不含时间戳/修订号/随机量**)。 */