dsh-zcode-startplan 1.0.2 → 1.0.5

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
@@ -3,6 +3,114 @@
3
3
  本文件记录**每次修复背后的宿主契约**。只看 diff 看不出「为什么不能改回去」,
4
4
  所以把约束与症状一起写下来。
5
5
 
6
+ ## 1.0.5 — issue #7:system 块数超 3 即 3012,改发 2 块给外部 fetch 包装器留余量
7
+
8
+ ### 根因(报告者实测,issue #7)
9
+
10
+ 上游对 `system` **数组长度**有硬上限 **3 块**:第 4 块无论内容必 `405 + 3012`。
11
+ 插件原发 3 块(cliPrefix / stable / `# Environment` + caller 追加)直连 200,
12
+ 但若 profile 里同时装了包装 `globalThis.fetch` 的插件(实测元凶
13
+ billion-context 0.1.216,对 `/messages` 注入一段「Compression Philosophy」system),
14
+ 转发时被追加一块 → 4 块 → 必然 3012。**插件侧日志全是正常的** —— 它记录的是
15
+ 交给 `#fetchImpl` 的字节,包装器在这之后运行;`bin/cli.mjs probe` 也不经过宿主
16
+ fetch 链,probe 通 ≠ 宿主对话通。所以「身份块齐全、日志正常、上游却 3012」
17
+ 这第三种触发方式(前两种见 issue #6 / #5)此前极难排查。
18
+
19
+ ### 修复
20
+
21
+ - `buildZcodeSystemBlocks` 改为**只发 2 块**:block[0] = cliPrefix(独占,
22
+ 准入判据是「以身份块开头」),block[1] = stable + `# Environment` + caller
23
+ system `\n\n` 合并。2 块直连 200;被包装器追加 1 块后正好 3 块,仍 200。
24
+ - 新增 `clampSystemBlocks`(`MAX_SYSTEM_BLOCKS = 3`)在 `buildUpstreamBody`
25
+ 出口兜底:超过 3 块时把溢出并入最后一块(cache_control 跟最后一块走)。
26
+ 防御调用方拿到 body 后自行追加块的路径。
27
+ - 文档 `docs/upstream-protocol.md`:消融矩阵补「4 块 → 3012」「2 块 → 200」
28
+ 两行,新增「第三方 fetch 包装器会让发出去的 ≠ 日志里的」一节。
29
+
30
+ ### ⚠ 不要改回去
31
+
32
+ 看似「更官方」的 3 块形态(stable / Environment / caller 各一块)在没有包装器时
33
+ 也 200,但它把余量用满 —— 任何 fetch 包装插件都会把请求推过上限,且插件侧
34
+ 无任何可见症状。2 块不是风格选择,是给宿主 fetch 链上不可控的第三方留的余量。
35
+ 报 3012 时若插件日志显示 3 块以内,先查有没有包装 `globalThis.fetch` 的插件。
36
+
37
+ ## 1.0.4 — issue #6 的三个真实缺陷:imageRequestTarget / probe 误报 / last_error 不清
38
+
39
+ issue #6(0.2.2-rc.3 全链路核对)确认的三个**与宿主版本无关**的缺陷,
40
+ 外加两个顺带发现。0.2.2 特有行为变化:无(`peerDependencies` 已覆盖)。
41
+
42
+ ### 缺陷 1:CUA 截图必崩 `TypeError: adapter.imageRequestTarget is not a function`
43
+
44
+ - **症状**:装了 CUA / 截图类工具、把模型切到 `zcode-start-plan/GLM-5.3-Flash`
45
+ 后,截图动作必崩。纯文本对话不受影响 —— 所以日常用起来完全隐蔽。
46
+ - **根因**:`LlmAdapter.imageRequestTarget` 自 0.2.1-rc.3 起就存在,基类默认实现
47
+ `{ width, height, maxBytes: image.bytes }`,宿主转发**没有可选调用保护**;
48
+ 唯一调用方是 `@deepseek-ai/dsh-cua`。本插件 `toModelInfo` 对
49
+ `supportsImage !== false` 的模型默认声明 `['text', 'image']`(兜底表里
50
+ `GLM-5.3-Flash` 就是 `supportsImage: true`),而两个适配器补齐了
51
+ `prepareCall` / `imageRequestPricing` 等直调方法,**漏了它**。
52
+ - **修复**:模块级 `imageRequestTargetDefault`(与宿主基类默认实现逐字同形),
53
+ 两个适配器各挂一个方法;顺带补齐 pi-ai 适配器缺的 `resolveModel`
54
+ (宿主 `resolveModelInfoFor` 同样直调无保护)。
55
+ - **守卫**:新增 `tests/adapter-contract.test.mjs`,锁定宿主直调方法清单。
56
+
57
+ ### 缺陷 2:probe 对 thinking-first 模型恒显示「流里没有文本」
58
+
59
+ - **症状**:`node bin/cli.mjs probe` 输出「✓ 全链路通过 + 回复:(流里没有文本)」,
60
+ 看着像失败、实际链路通。GLM-5.3-Flash 现在先吐 `thinking` 块,正文可能为空。
61
+ - **修复**:probe 分轨累加 `text_delta` 与 `thinking_delta`,回复行按
62
+ 「正文优先、thinking 兜底(标注「只有 thinking,无正文」)」展示。
63
+ 真实对话不受影响(`sse.js` / `stream-translator.js` 本来就认 thinking)。
64
+
65
+ ### 连带发现(缺陷 2 排查时):probe 从来只读了 head 段事件
66
+
67
+ `readContentHead` 的续流解码器在 `result.decoder` 上;probe 原来读
68
+ `result.head.decoder` 恒为 `undefined`(`head` 是已缓冲事件数组,没有该字段),
69
+ 超过 head 缓冲的内容从未被收集。已改为读 `result.decoder`。
70
+
71
+ ### 缺陷 3:`last_error` 只写不清,status 输出自相矛盾
72
+
73
+ - **症状**:账号额度恢复后 `status` 同时输出「剩余 98.7%」与
74
+ 「最近错误:额度用尽(1005)」两行互相打脸的数字;面板红字也一直挂着。
75
+ - **根因**:`pool.noteError` 只写不清,调用点全在失败分支;`reset` 只清
76
+ `model_rate_limits`。
77
+ - **修复**:新增 `pool.clearError`(落库 **NULL** 而非空串,读回 `undefined`
78
+ 让 status 的真值判断自然不打印);`plan-client` 在**真实命中**后调用
79
+ `clearError?.()`。失败路径不动(错误文案必须留给面板/status)。
80
+ 手动「清除限流」入口(`manage-routes`)本就用 `noteError(id, '')` 清文案,两者等价。
81
+ - **测试**:命中即清 / 失败不清 / NULL 读回语义 / 幂等不抛,4 个用例。
82
+
83
+ ### 连带发现(跑全量测试时暴露):e2e 的版本探测真发外呼,污染 upstream 测试
84
+
85
+ `apply()` 挂载时探测官方最新版本(`lib/assemble.js`),探测结果写入模块级
86
+ 运行期门控且不恢复。官方已发 3.18.2 > 内置常量 3.17.2,于是按文件序执行的
87
+ `upstream.test.mjs` 断言「初值 = 编译期常量」开始随机失败。修复:
88
+ e2e stub 掉 `zcode.z.ai` 的 fetch(测试进程不许有外呼),upstream 门控
89
+ 测试开头 `resetGatedMinAppVersion()` 防御顺序变化。
90
+
91
+ ### 顺带:README 宿主要求更新 + `check:compat` 脚本
92
+
93
+ 0.2.2-rc.3 已全链路实测通过(issue #6 报告人的核对结论),README 的
94
+ 「已实测」范围更新为 `0.2.1-rc.3 ~ 0.2.2-rc.3`;`scripts/check-compat-rc3.mjs`
95
+ 依赖 cwd 下的 `.tmp-dsh-extract-full`(需手动解 asar),登记成
96
+ `npm run check:compat` 并在 README 说明,避免「直接跑就报错」。
97
+
98
+ ### 风控(issue #6 第六节)的两个落点
99
+
100
+ 第六节的实证结论:3012 是**请求形状指纹**问题——同账号同时刻,手写 fetch
101
+ (无身份块)405 + 3012,带 cliPrefix+stable 的 200。已落到两处:
102
+
103
+ 1. **`docs/upstream-protocol.md`** 新增「3012 的消融矩阵与请求形状指纹」小节,
104
+ 记录三级消融表与推论(准入判据必须长在 `buildUpstreamBody` 结构里,
105
+ 任何新上游路径都要过这条组装管线;换号救不了形状不变的 3012)。
106
+ 2. **`lib/openai-gateway.js` 的 system 丢弃缺陷**(顺着这条结论排查发现):
107
+ `toGenerateOptions` 把 `role:'system'` 映射成消息,而 `buildUpstreamBody`
108
+ 过滤所有消息级 system、只认 `options.system` ⇒ OpenAI 客户端(Codex/Cline 等)
109
+ 的 system prompt 被静默丢弃。准入不受影响(身份块是结构性注入的),但
110
+ 「模型完全不遵循客户端人设」的根源就是它。现在多条 system/developer 消息
111
+ 按序折叠进 `options.system`,经 `buildZcodeSystemBlocks` 追加在官方身份块之后。
112
+ (pi-ai 的 `local-endpoint` 路径本来就保留 `body.system`,无此问题。)
113
+
6
114
  ## 未发布 — 活动列表为空:版本门控动态化 + 领取链路三处修复
7
115
 
8
116
  完整排查记录见 [`docs/claim-forensics-2026-11.md`](docs/claim-forensics-2026-11.md)。
package/README.md CHANGED
@@ -15,6 +15,12 @@
15
15
 
16
16
  ## 安装
17
17
 
18
+ **宿主要求**:DSH desktop **`0.2.1-rc.3` ~ `0.2.2-rc.3`(已实测)**;`peerDependencies`
19
+ 的 semver 范围覆盖到 `0.3.0` 之前的预发布版,更高版本原则上兼容——如遇 `0.3.0+`
20
+ 行为变化请开 issue。兼容性自检:`npm run verify:compat`(免提取,校验 peer 区间)
21
+ 或 `npm run check:compat`(需先手动解包宿主 asar 到 `.tmp-dsh-extract-full/`,
22
+ 用宿主自带的 `evaluatePluginCompatibility` 逐版本校验)。
23
+
18
24
  **插件页(最简单)**:DSH Web UI 插件页 → 安装 → 输入 `dsh-zcode-startplan`。
19
25
 
20
26
  **npm**:`npm install -g dsh-zcode-startplan`,然后在 DSH 会话里
package/bin/cli.mjs CHANGED
@@ -118,7 +118,8 @@ function printAccounts(pool) {
118
118
  `jwt=${maskJwt(entry.credential?.zcode_jwt)} ` +
119
119
  `device=${entry.credential?.device_mid?.slice(0, 8) ?? '?'} ` +
120
120
  `${limits.length > 0 ? `限流:${limits.join(',')}` : ''}` +
121
- `${entry.last_error !== undefined ? ` 最近错误:${entry.last_error}` : ''}`,
121
+ /** last_error 可为 NULL(读回 undefined)或空串(手动清除限流入口写入):两者都视为「没有错误」。 */
122
+ `${entry.last_error ? ` 最近错误:${entry.last_error}` : ''}`,
122
123
  )
123
124
  })
124
125
  return entries
@@ -289,21 +290,39 @@ async function cmdProbe() {
289
290
  maxTokens: 64,
290
291
  onRetry: (info) => warn(`重试(${info.reason}):${info.message}`),
291
292
  })
293
+ /**
294
+ * 收集可读输出(issue #6 缺陷 2):GLM-5.3-Flash 是 thinking-first ——
295
+ * 流的开头常是 `thinking` 块的 `thinking_delta`,正文 `text_delta` 可能为空
296
+ * 甚至没有。只累加 `text_delta` 会把成功链路显示成「流里没有文本」的误报。
297
+ * 这里 thinking 与正文分轨累加,回复行按「正文优先、thinking 兜底」展示。
298
+ */
292
299
  let text = ''
300
+ let thinking = ''
301
+ const collectDelta = (data) => {
302
+ if (data?.type !== 'content_block_delta') return
303
+ if (data.delta?.type === 'text_delta') text += data.delta.text
304
+ else if (data.delta?.type === 'thinking_delta') thinking += data.delta.thinking ?? ''
305
+ }
293
306
  for (const event of result.head) {
294
- const data = event.data
295
- if (data?.type === 'content_block_delta' && data.delta?.type === 'text_delta') text += data.delta.text
307
+ collectDelta(event.data)
296
308
  }
297
- if (result.head.decoder !== undefined) {
309
+ /**
310
+ * ⚠ 续流在 `result.decoder`(`readContentHead` 的返回字段),不在 `result.head` 上
311
+ * —— `head` 是已缓冲的事件数组,从来没有 decoder 字段。旧代码读
312
+ * `result.head.decoder` 恒为 undefined,导致 probe 实际上只读了 head 段。
313
+ */
314
+ if (result.decoder !== undefined) {
298
315
  for (;;) {
299
- const next = await result.head.decoder.read()
316
+ const next = await result.decoder.read()
300
317
  if (next.done === true) break
301
- const data = next.value.data
302
- if (data?.type === 'content_block_delta' && data.delta?.type === 'text_delta') text += data.delta.text
318
+ collectDelta(next.value.data)
303
319
  }
304
320
  }
305
321
  log(`✓ 全链路通过(账号 ${result.accountId.slice(0, 8)},${Date.now() - started}ms)`)
306
- log(` 回复:${text.trim().slice(0, 200) || '(流里没有文本)'}`)
322
+ const reply = text.trim().length > 0
323
+ ? text.trim()
324
+ : thinking.trim().length > 0 ? `(只有 thinking,无正文)${thinking.trim()}` : '(流里没有文本)'
325
+ log(` 回复:${reply.slice(0, 200)}`)
307
326
  } catch (error) {
308
327
  warn(`✗ 失败(${error?.code ?? 'ERR'}):${error instanceof Error ? error.message : String(error)}`)
309
328
  process.exitCode = 1
package/lib/adapter.js CHANGED
@@ -85,6 +85,25 @@ function imageRequestPricingDefault(_provider, _model) {
85
85
  return undefined
86
86
  }
87
87
 
88
+ /**
89
+ * 图片请求投影的缺省实现(两个适配器共用)。
90
+ *
91
+ * ⚠ **必须实现**:宿主 `LlmAdapter.imageRequestTarget`(0.2.1-rc.3 起就存在,
92
+ * 基类默认实现见 dsh-llm `lib/index.js` 的 `imageRequestTarget(_provider, _model, image)`)
93
+ * 在 CUA / 截图链路里被**无条件直调**——唯一调用方是 `@deepseek-ai/dsh-cua`:
94
+ * `llm.imageRequestTarget(provider, model, image)`,没有可选调用保护。
95
+ * 本插件 `toModelInfo` 对 `supportsImage !== false` 的模型默认声明
96
+ * `['text', 'image']`(内置兜底表里 `GLM-5.3-Flash` 就是 `supportsImage: true`),
97
+ * 所以一旦用户装了 CUA 并把模型切到本渠道,截图动作就是必崩路径:
98
+ * `TypeError: adapter.imageRequestTarget is not a function`(issue #6 缺陷 1)。
99
+ *
100
+ * 形状与宿主基类默认实现逐字一致:保留归一化后的图片尺寸与字节量
101
+ * (「不缩放、原样上报」)。
102
+ */
103
+ function imageRequestTargetDefault(_provider, _model, image) {
104
+ return { width: image.width, height: image.height, maxBytes: image.bytes }
105
+ }
106
+
88
107
  /**
89
108
  * 原生适配器:一次 `stream()` 就是一次 plan-client 调用 + 流翻译。
90
109
  *
@@ -240,6 +259,22 @@ export class NativeZcodeStartPlanAdapter {
240
259
  return imageRequestPricingDefault(_provider, _model)
241
260
  }
242
261
 
262
+ /**
263
+ * 图片请求投影:见模块级 `imageRequestTargetDefault`(宿主直调契约 + 逐字同形实现)。
264
+ *
265
+ * ⚠ CUA / 截图链路的**唯一**适配器入口(`dsh-cua` 直调,无可选调用保护);
266
+ * `GLM-5.3-Flash` 的目录元数据声明了 image 输入,漏实现必崩
267
+ * `TypeError: adapter.imageRequestTarget is not a function`(issue #6 缺陷 1)。
268
+ *
269
+ * @param {string} _provider
270
+ * @param {string} _model
271
+ * @param {{ width: number, height: number, bytes: number }} image
272
+ * @returns {{ width: number, height: number, maxBytes: number }}
273
+ */
274
+ imageRequestTarget(_provider, _model, image) {
275
+ return imageRequestTargetDefault(_provider, _model, image)
276
+ }
277
+
243
278
  /**
244
279
  * 账号栏位的模型目录:基础模型 × 该账号(description 带额度状态)。
245
280
  *
@@ -450,6 +485,37 @@ export class PiAiZcodeStartPlanAdapter {
450
485
  return imageRequestPricingDefault(_provider, _model)
451
486
  }
452
487
 
488
+ /**
489
+ * 精确模型元数据:宿主 `resolveModelInfoFor` 会**直接调用**
490
+ * `adapter.resolveModel(provider, model)`(无可选调用保护),漏掉它是
491
+ * 潜在的 `TypeError`(issue #6 缺陷 1 的同型问题)。回落适配器的目录
492
+ * 就是 `models()` 表:查得到给完整元数据,查不到按宿主基类默认形状
493
+ * 返回 `{ provider, id, name }`(与 `prepareCall` 的兜底一致)。
494
+ *
495
+ * @param {string} provider
496
+ * @param {string} model
497
+ */
498
+ async resolveModel(provider, model) {
499
+ const found = findModel(await this.#models(), model)
500
+ return found === undefined ? { provider, id: model, name: model } : toModelInfo(provider, found)
501
+ }
502
+
503
+ /**
504
+ * 图片请求投影:见模块级 `imageRequestTargetDefault`(宿主直调契约 + 逐字同形实现)。
505
+ *
506
+ * ⚠ 回落适配器同样要补(issue #6 缺陷 1):宿主在 CUA / 截图链路直调
507
+ * `adapter.imageRequestTarget(...)`,且 `listModels` 走同一个 `toModelInfo`
508
+ * —— 只要目录声明了 image 输入,漏实现就是必崩路径。
509
+ *
510
+ * @param {string} _provider
511
+ * @param {string} _model
512
+ * @param {{ width: number, height: number, bytes: number }} image
513
+ * @returns {{ width: number, height: number, maxBytes: number }}
514
+ */
515
+ imageRequestTarget(_provider, _model, image) {
516
+ return imageRequestTargetDefault(_provider, _model, image)
517
+ }
518
+
453
519
  async listModels(provider) {
454
520
  return (await this.#models()).map((model) => toModelInfo(provider, model))
455
521
  }
@@ -2,7 +2,7 @@
2
2
  * 请求体组装:把 DSH 的 `GenerateOptions`(provider-neutral 词汇)翻成
3
3
  * zcode-plan 端点要求的 Anthropic Messages 形态。
4
4
  *
5
- * ## 三道准入闸门(缺一即 3012 / 3007,且 3012 有账号冷却惩罚)
5
+ * ## 四道准入闸门(缺一即 3012 / 3007,且 3012 有账号冷却惩罚)
6
6
  *
7
7
  * 1. **官方身份块**:`system` 必须以官方身份块结构**开头**(cliPrefix + stable 段),
8
8
  * 否则 `3012 unusual activity`。实测矩阵:无 system → 3012;只有 cliPrefix → 3012;
@@ -12,6 +12,10 @@
12
12
  * `<system-reminder>` 日期块(纯文本 content 会被转成块数组)。幂等:已以此开头则跳过。
13
13
  * 3. **tools 缓存断点**:只在**最后一个** tool 上打 `cache_control` —— Anthropic 缓存是
14
14
  * 前缀式的,一个断点即覆盖 system + 全部 tools;单请求最多 4 个断点。
15
+ * 4. **system 块数 ≤ 3(issue #7)**:上游对 `system` **数组长度**有硬上限,第 4 块
16
+ * 无论内容必 `405 + 3012`。本插件只发 **2 块**(cliPrefix / 合并尾块),给宿主
17
+ * fetch 链上可能包装 `globalThis.fetch` 的第三方插件(上下文管理、代理、遥测类)
18
+ * 留 1 块余量;`clampSystemBlocks` 在 `buildUpstreamBody` 出口兜底。
15
19
  */
16
20
 
17
21
  /**
@@ -116,29 +120,55 @@ export function buildEnvironmentSection(options) {
116
120
  /**
117
121
  * 构造官方形态的 system 块数组。
118
122
  *
119
- * 结构(逐字复刻官方,**不要优化**):
120
- * block[0] = cliPrefix ← 准入必需
121
- * block[1] = stable ← 准入必需
122
- * block[2] = # Environment
123
- * block[3..] = 调用方 system(追加在最后)
123
+ * 结构(准入所需的最小 2 块,issue #7):
124
+ * block[0] = cliPrefix ← 准入必需(结构判据看「开头是身份块」)
125
+ * block[1] = stable + `# Environment` + 调用方 system(`\n\n` 合并)
124
126
  *
127
+ * ⚠ 上游对 `system` **数组长度**有硬上限 3 块,第 4 块无论内容必 3012(issue #7)。
128
+ * 宿主的 fetch 链上可能有第三方插件包装 `globalThis.fetch`(上下文管理、代理、
129
+ * 遥测类),在转发时再追加自己的 system 块 —— 那发生在 `#fetchImpl` **之后**,
130
+ * 插件侧日志看不到。这里只发 2 块,给包装器留 1 块余量(2+1=3 通过;3+1=4 必挂)。
131
+ * ⚠ cliPrefix 必须独占 block[0]:判据是「以身份块开头」,合并任何内容进第一块
132
+ * 都可能在风控更新后破坏前缀匹配。
125
133
  * ⚠ 只在**最后一块**打 cache_control(前缀式缓存,一个断点覆盖面最大)。
126
134
  */
127
135
  export function buildZcodeSystemBlocks(callerSystem, options = {}) {
128
136
  const stable = OFFICIAL_STABLE_SECTIONS.join('\n\n')
137
+ const environment = buildEnvironmentSection(options)
138
+ const callerText =
139
+ typeof callerSystem === 'string' && callerSystem.trim().length > 0 ? callerSystem : undefined
140
+ const tail =
141
+ callerText === undefined ? `${stable}\n\n${environment}` : `${stable}\n\n${environment}\n\n${callerText}`
129
142
  const blocks = [
130
143
  { type: 'text', text: OFFICIAL_CLI_PREFIX },
131
- { type: 'text', text: stable },
132
- { type: 'text', text: buildEnvironmentSection(options) },
144
+ { type: 'text', text: tail },
133
145
  ]
134
- if (typeof callerSystem === 'string' && callerSystem.trim().length > 0) {
135
- blocks.push({ type: 'text', text: callerSystem })
136
- }
137
146
  const last = blocks[blocks.length - 1]
138
147
  if (last !== undefined) last.cache_control = { type: 'ephemeral' }
139
148
  return blocks
140
149
  }
141
150
 
151
+ /** 上游 system 块数硬上限(issue #7):超出即 3012。 */
152
+ export const MAX_SYSTEM_BLOCKS = 3
153
+
154
+ /**
155
+ * 出口兜底(issue #7):把超过 3 块的 system 数组裁剪到上限内 —— 超出部分
156
+ * `\n\n` 并入最后一块(cache_control 跟着最后一块走,语义不变)。正常情况下
157
+ * `buildZcodeSystemBlocks` 只产 2 块,走不到这里;它防御的是调用方在拿到 body
158
+ * 之后、发出去之前自己再追加块(如本地端点透传路径)。
159
+ */
160
+ export function clampSystemBlocks(system) {
161
+ if (!Array.isArray(system) || system.length <= MAX_SYSTEM_BLOCKS) return system
162
+ const head = system.slice(0, MAX_SYSTEM_BLOCKS - 1)
163
+ const overflow = system
164
+ .slice(MAX_SYSTEM_BLOCKS - 1)
165
+ .map((block) => (typeof block?.text === 'string' ? block.text : ''))
166
+ .filter((text) => text.length > 0)
167
+ .join('\n\n')
168
+ const lastBlock = system[system.length - 1]
169
+ return [...head, { ...lastBlock, text: overflow }]
170
+ }
171
+
142
172
  /** 把任意形态的 system 归一成块数组(Anthropic 允许字符串或块数组)。 */
143
173
  export function normalizeSystem(system) {
144
174
  if (typeof system === 'string') {
@@ -509,5 +539,7 @@ export function buildUpstreamBody(input) {
509
539
  const tools = convertTools(options.tools)
510
540
  if (tools.length > 0) body.tools = tools
511
541
  if (typeof options.temperature === 'number') body.temperature = options.temperature
542
+ /** issue #7:上游 system 块数硬上限 3,出口兜底裁剪。 */
543
+ body.system = clampSystemBlocks(body.system)
512
544
  return body
513
545
  }
@@ -395,14 +395,34 @@ export function toGenerateOptions(body) {
395
395
  */
396
396
  const callIds = collectCallAvailability(body.messages)
397
397
  const messages = []
398
+ /** 调用方 system 提示词的折叠处(见返回值注释:为什么进 `system` 而不是留消息)。 */
399
+ const systemParts = []
398
400
  for (const message of body.messages) {
399
401
  const converted = convertMessage(message, callIds)
400
402
  /** `undefined` = 整条剔除(绝不 push 空消息充数)。 */
401
- if (converted !== undefined) messages.push(converted)
403
+ if (converted === undefined) continue
404
+ if (converted.role === 'system') {
405
+ /** 可用文本已在 convertMessage 内过滤过(空 system 返回 undefined)。 */
406
+ for (const block of converted.content) systemParts.push(block.text)
407
+ continue
408
+ }
409
+ messages.push(converted)
402
410
  }
411
+ /** 多条 system 消息按序拼成一段(Anthropic system 是请求级属性,无顺序歧义)。 */
412
+ const system = systemParts.length > 0 ? systemParts.join('\n\n') : undefined
403
413
 
414
+ /**
415
+ * 调用方 system 提示词必须落在 `system` 字段(**不是**留在消息里):
416
+ * `buildUpstreamBody`(anthropic-body.js)只认 `options.system`,把消息里的
417
+ * `role:'system'` 全部过滤掉 —— 不折叠的话,OpenAI 客户端的 system prompt
418
+ * 会被静默丢弃。上游准入不受影响(官方身份块由 `buildZcodeSystemBlocks`
419
+ * 结构性注入并排在最前,调用方内容追加在其后),但「模型完全不遵循
420
+ * 客户端人设」这类怪象的根源就是它(issue #6 第六节「请求形状指纹」的
421
+ * 间接教训:准入判据在身份块,身份块之后的 caller system 不能丢)。
422
+ */
404
423
  return {
405
424
  messages,
425
+ ...(system === undefined ? {} : { system }),
406
426
  ...(tools === undefined ? {} : { tools }),
407
427
  ...(maxTokens === undefined ? {} : { maxTokens }),
408
428
  ...(temperature === undefined ? {} : { temperature }),
@@ -889,6 +889,13 @@ export class ZcodePlanClient {
889
889
 
890
890
  /** 有产出 = 账号×模型健康,清掉观察窗。 */
891
891
  this.#pool.clearEmptyContentWatch?.(accountId, model)
892
+ /**
893
+ * 有产出也 = 历史失败已不成立(issue #6 缺陷 3):`last_error` 是「最近一次
894
+ * 失败原因」面板字段,但此前只写不清 —— 账号额度恢复后 status/面板仍挂着
895
+ * 「额度用尽」的旧文案,与实时余额自相矛盾。真实命中即清掉历史错误;
896
+ * `clearError` 缺省(fake pool / 老池子实例)时静默跳过。
897
+ */
898
+ this.#pool.clearError?.(accountId)
892
899
 
893
900
  if (captcha === undefined) {
894
901
  /** 没带 captcha 就成功 ⇒ 上游当前不要验证,清掉记忆回到最省路径。 */
package/lib/pool.js CHANGED
@@ -747,6 +747,23 @@ export class AccountPool {
747
747
  noteError(accountId, message) {
748
748
  this.#store.patch(accountId, { last_error: message })
749
749
  }
750
+
751
+ /**
752
+ * 清空某个账号的 `last_error`(issue #6 缺陷 3)。
753
+ *
754
+ * `noteError` 只写不清:账号恢复后 `last_error` 仍挂着当日额度耗尽的历史文本,
755
+ * `status` 会同时输出「余额 98.7% 未用」与「最近错误:额度用尽」两行互相打脸的
756
+ * 数字。现在 `plan-client` 在一次**真实命中**后调用本方法清掉历史错误。
757
+ *
758
+ * ⚠ 落库 **NULL** 而不是空串:读回路径 `row.last_error ?? undefined` 会把 NULL
759
+ * 映射回 `undefined`,`cli status` 的 `entry.last_error !== undefined` 判断就
760
+ * 自然不再打印「最近错误:」这行;写成空串则会留下一行尾巴。
761
+ * 手动「清除限流」入口(manage-routes)用的是 `noteError(id, '')`(空串)——
762
+ * 面板按「空 = 没有错误」渲染,两者等价;`pool.pick` 都不读它,选号不受影响。
763
+ */
764
+ clearError(accountId) {
765
+ this.#store.patch(accountId, { last_error: undefined })
766
+ }
750
767
  }
751
768
 
752
769
  /** 默认数据目录:`~/.dsh/zcode-start-plan`(可用环境变量覆盖)。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-zcode-startplan",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "DSH 接入 ZCode 赠送免费 plan(account:bigmodel-start-plan):多账号池 + 按需阿里云 captcha + 直连 zcode-plan/anthropic 的 llm 适配器。移植自 gitee.com/iJetLi/bonus-plan-4-open-zcode。",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-or-later",
@@ -45,13 +45,14 @@
45
45
  "LICENSE"
46
46
  ],
47
47
  "bin": {
48
- "zcode-start-plan": "./bin/cli.mjs"
48
+ "zcode-start-plan": "bin/cli.mjs"
49
49
  },
50
50
  "scripts": {
51
51
  "check": "node --check index.js && node --check bin/cli.mjs",
52
52
  "test": "node scripts/run-tests.mjs",
53
53
  "smoke": "node scripts/smoke-install.mjs",
54
54
  "verify:compat": "node scripts/verify_compat.mjs",
55
+ "check:compat": "node scripts/check-compat-rc3.mjs",
55
56
  "login": "node bin/cli.mjs login",
56
57
  "status": "node bin/cli.mjs status",
57
58
  "probe": "node bin/cli.mjs probe"
@@ -0,0 +1,91 @@
1
+ /**
2
+ * 与宿主 `LlmAdapter` 直调契约的**形状级**测试(issue #6 缺陷 1)。
3
+ *
4
+ * 宿主 `dsh-llm/lib/index.js`(0.2.1-rc.3 ~ 0.2.2-rc.3 逐字同形):
5
+ *
6
+ * ```js
7
+ * // LlmAdapter 基类默认实现(:1706)
8
+ * imageRequestTarget(_provider, _model, image) {
9
+ * return { width: image.width, height: image.height, maxBytes: image.bytes };
10
+ * }
11
+ *
12
+ * // ctx.llm 的转发(:2063)——没有可选调用保护:
13
+ * imageRequestTarget(provider, model, image) {
14
+ * return this.registration(provider).adapter.imageRequestTarget(provider, model, image);
15
+ * }
16
+ * ```
17
+ *
18
+ * 本插件的两个适配器都**不 extends 基类**(鸭子类型,宿主不做 instanceof 检查),
19
+ * 所以每一个被宿主直调的方法都必须自己补齐。此前补了 `providerRetryPolicy` /
20
+ * `imageRequestPricing` / `prepareCall`,漏了 `imageRequestTarget` ——
21
+ * 唯一调用方是 `@deepseek-ai/dsh-cua`(截图链路):模型目录声明了 image 输入
22
+ * (`GLM-5.3-Flash` 的 `supportsImage` 是 true)⇒ 用户装了 CUA 并把模型切到
23
+ * 本渠道后,截图动作必崩 `TypeError: adapter.imageRequestTarget is not a function`。
24
+ * 纯文本对话不受影响,所以这个坑在日常使用里完全隐蔽。
25
+ */
26
+
27
+ import test from 'node:test'
28
+ import assert from 'node:assert/strict'
29
+
30
+ import { NativeZcodeStartPlanAdapter, PiAiZcodeStartPlanAdapter } from '../lib/adapter.js'
31
+
32
+ const noModels = async () => []
33
+
34
+ /** 宿主基类默认实现的忠实复刻(对照用)。 */
35
+ function hostDefault(image) {
36
+ return { width: image.width, height: image.height, maxBytes: image.bytes }
37
+ }
38
+
39
+ const IMAGE = { width: 1024, height: 768, bytes: 456_789, mimeType: 'image/png' }
40
+
41
+ test('NativeZcodeStartPlanAdapter.imageRequestTarget 与宿主基类默认实现同形', () => {
42
+ const adapter = new NativeZcodeStartPlanAdapter({ client: {}, models: noModels })
43
+ assert.equal(typeof adapter.imageRequestTarget, 'function', '宿主直调的方法必须存在')
44
+ assert.deepEqual(adapter.imageRequestTarget('zcode-start-plan', 'GLM-5.3-Flash', IMAGE), hostDefault(IMAGE))
45
+ })
46
+
47
+ test('PiAiZcodeStartPlanAdapter.imageRequestTarget 与宿主基类默认实现同形', () => {
48
+ const adapter = new PiAiZcodeStartPlanAdapter({ models: noModels })
49
+ assert.equal(typeof adapter.imageRequestTarget, 'function', '回落适配器同样被宿主直调')
50
+ assert.deepEqual(adapter.imageRequestTarget('zcode-start-plan', 'GLM-5.3-Flash', IMAGE), hostDefault(IMAGE))
51
+ })
52
+
53
+ test('宿主直调契约方法在两个适配器上全部齐备(回归守卫)', () => {
54
+ /**
55
+ * 这份名单来自宿主 `registerAdapter` / `prepareRoutes` / CUA / token-meter 的
56
+ * **无条件直调**清单(有可选调用的不算):
57
+ * - `providerInfo` / `providerRetryPolicy`:路由注册时直调;
58
+ * - `listModels` / `resolveModel` / `prepareCall`:模型选择与派发时直调;
59
+ * - `imageRequestPricing`:token-meter / 压缩压力判定直调;
60
+ * - `imageRequestTarget`:CUA 截图链路直调(issue #6 缺陷 1)。
61
+ */
62
+ const hostCalledMethods = [
63
+ 'providerInfo',
64
+ 'providerRetryPolicy',
65
+ 'listModels',
66
+ 'resolveModel',
67
+ 'prepareCall',
68
+ 'imageRequestPricing',
69
+ 'imageRequestTarget',
70
+ ]
71
+ for (const Ctor of [NativeZcodeStartPlanAdapter, PiAiZcodeStartPlanAdapter]) {
72
+ const adapter = new Ctor({ client: {}, models: noModels })
73
+ for (const name of hostCalledMethods) {
74
+ assert.equal(typeof adapter[name], 'function', `${Ctor.name}.${name} 缺失 —— 宿主直调会 TypeError`)
75
+ }
76
+ }
77
+ })
78
+
79
+ test('imageRequestTarget 不受重试/钉定影响:同一输入恒同形', () => {
80
+ const native = new NativeZcodeStartPlanAdapter({ client: {}, models: noModels })
81
+ const piAi = new PiAiZcodeStartPlanAdapter({ models: noModels })
82
+ const expected = { width: 1, height: 2, maxBytes: 3 }
83
+ for (const adapter of [native, piAi]) {
84
+ assert.deepEqual(adapter.imageRequestTarget('p', 'm', { width: 1, height: 2, bytes: 3 }), expected)
85
+ /** 钉定路由(provider 带 -acct- 后缀)同样走同一实现。 */
86
+ assert.deepEqual(
87
+ adapter.imageRequestTarget('zcode-start-plan-acct-abc', 'GLM-5.3-Flash', { width: 1, height: 2, bytes: 3 }),
88
+ expected,
89
+ )
90
+ }
91
+ })
@@ -6,6 +6,7 @@ import {
6
6
  OFFICIAL_STABLE_SECTIONS,
7
7
  buildUpstreamBody,
8
8
  buildZcodeSystemBlocks,
9
+ clampSystemBlocks,
9
10
  convertContent,
10
11
  convertTools,
11
12
  imageBlockFromAttachment,
@@ -16,13 +17,25 @@ import {
16
17
 
17
18
  const userMessage = (text) => ({ role: 'user', content: [{ type: 'text', text }] })
18
19
 
19
- test('身份块必须打头(3012 的准入判据)', () => {
20
+ test('身份块必须打头;尾块合并 stable / Environment / caller(issue #7 的 2 块结构)', () => {
20
21
  const blocks = buildZcodeSystemBlocks('CALLER SYSTEM', { cwd: 'C:/w' })
22
+ /** 上游 system 数组硬上限 3 块(issue #7):宿主 fetch 链上的第三方包装器
23
+ * (billion-context 等)可能在 #fetchImpl 之后追加自己的 system —— 这里只发
24
+ * 2 块,给包装器留 1 块余量。 */
25
+ assert.equal(blocks.length, 2)
21
26
  assert.equal(blocks[0].text, OFFICIAL_CLI_PREFIX)
22
- assert.equal(blocks[1].text, OFFICIAL_STABLE_SECTIONS.join('\n\n'))
23
- assert.match(blocks[2].text, /# Environment/)
24
- /** 调用方内容追加在最后 —— 前置会顶掉官方前缀结构。 */
25
- assert.equal(blocks[blocks.length - 1].text, 'CALLER SYSTEM')
27
+ assert.equal(blocks[0].cache_control, undefined, 'cache_control 只在最后一块')
28
+ assert.ok(blocks[1].text.includes(OFFICIAL_STABLE_SECTIONS.join('\n\n')), 'stable 段进尾块')
29
+ assert.match(blocks[1].text, /# Environment/, 'Environment 段进尾块')
30
+ assert.ok(blocks[1].text.endsWith('CALLER SYSTEM'), 'caller system 仍在最后')
31
+ assert.equal(blocks[1].cache_control.type, 'ephemeral')
32
+ })
33
+
34
+ test('无调用方 system 时同样是 2 块,尾块以 Environment 收尾', () => {
35
+ const blocks = buildZcodeSystemBlocks(undefined, { cwd: 'C:/w' })
36
+ assert.equal(blocks.length, 2)
37
+ assert.equal(blocks[0].text, OFFICIAL_CLI_PREFIX)
38
+ assert.match(blocks[1].text, /# Environment/)
26
39
  })
27
40
 
28
41
  test('只在最后一块打 cache_control(单请求最多 4 个断点)', () => {
@@ -32,6 +45,36 @@ test('只在最后一块打 cache_control(单请求最多 4 个断点)', ()
32
45
  assert.equal(blocks[blocks.length - 1].cache_control.type, 'ephemeral')
33
46
  })
34
47
 
48
+ test('clampSystemBlocks: ≤3 块原样返回(同引用,零拷贝热路径)', () => {
49
+ const three = [
50
+ { type: 'text', text: 'a' },
51
+ { type: 'text', text: 'b', cache_control: { type: 'ephemeral' } },
52
+ { type: 'text', text: 'c' },
53
+ ]
54
+ assert.equal(clampSystemBlocks(three), three)
55
+ assert.equal(clampSystemBlocks(undefined), undefined)
56
+ })
57
+
58
+ test('clampSystemBlocks: 4 块裁到 3 块,溢出并入最后一块且保留 cache_control(issue #7)', () => {
59
+ const blocks = clampSystemBlocks([
60
+ { type: 'text', text: 'cliPrefix' },
61
+ { type: 'text', text: 'stable' },
62
+ { type: 'text', text: 'env' },
63
+ { type: 'text', text: 'caller', cache_control: { type: 'ephemeral' } },
64
+ ])
65
+ assert.equal(blocks.length, 3)
66
+ assert.deepEqual(blocks.map((block) => block.text), ['cliPrefix', 'stable', 'env\n\ncaller'])
67
+ assert.deepEqual(blocks[2].cache_control, { type: 'ephemeral' }, '断点跟最后一块走')
68
+ })
69
+
70
+ test('clampSystemBlocks: 任意多的溢出块都并入最后一块', () => {
71
+ const blocks = clampSystemBlocks(
72
+ Array.from({ length: 6 }, (_, index) => ({ type: 'text', text: `b${index}` })),
73
+ )
74
+ assert.equal(blocks.length, 3)
75
+ assert.equal(blocks[2].text, 'b2\n\nb3\n\nb4\n\nb5')
76
+ })
77
+
35
78
  test('首轮 user 消息插入日期块,且幂等', () => {
36
79
  const once = withContextPrefix([userMessage('hi')], new Date(2026, 9, 23))
37
80
  const first = once[0].content[0]
@@ -246,6 +246,23 @@ test('apply:等 llm 出现后注册 adapter + 状态工具,dispose 时收尾
246
246
  const dir = mkdtempSync(join(tmpdir(), 'zsp-apply-'))
247
247
  t.after(() => rmSync(dir, { recursive: true, force: true }))
248
248
 
249
+ /**
250
+ * ⚠ 版本探测必须 stub(issue #6 修复时的连带发现):`apply()` 挂载时会真发一次
251
+ * `https://zcode.z.ai/` 首页探测抬高门控下限(`lib/assemble.js`),探测结果写入
252
+ * **模块级**运行期状态且不影响本测试 —— 但会污染后面按文件序执行的
253
+ * `upstream.test.mjs`(它断言门控初值 = 编译期常量)。测试进程不许有外呼。
254
+ */
255
+ const realFetch = globalThis.fetch
256
+ globalThis.fetch = async (url, init) => {
257
+ if (typeof url === 'string' && url.startsWith('https://zcode.z.ai')) {
258
+ return { ok: true, status: 200, text: async () => '<html>stubbed</html>' }
259
+ }
260
+ return realFetch(url, init)
261
+ }
262
+ t.after(() => {
263
+ globalThis.fetch = realFetch
264
+ })
265
+
249
266
  const registered = []
250
267
  const tools = []
251
268
  const disposers = []
@@ -54,9 +54,49 @@ test('toGenerateOptions: system/user/assistant 基本映射', () => {
54
54
  })
55
55
  assert.equal(options.maxTokens, 1024)
56
56
  assert.equal(options.temperature, 0.3)
57
- assert.deepEqual(options.messages[0], { role: 'system', content: [{ type: 'text', text: '你是助手' }] })
58
- assert.deepEqual(options.messages[1], { role: 'user', content: [{ type: 'text', text: '你好' }] })
59
- assert.deepEqual(options.messages[2], { role: 'assistant', content: [{ type: 'text', text: '你好呀' }] })
57
+ /**
58
+ * system 消息折叠进 `options.system`(issue #6 第六节顺带修复):
59
+ * `buildUpstreamBody` 只认 `options.system`,消息里的 `role:'system'`
60
+ * 会被它整体过滤 —— 不折叠 = OpenAI 客户端的 system prompt 静默丢失。
61
+ */
62
+ assert.equal(options.system, '你是助手')
63
+ assert.equal(options.messages.some((m) => m.role === 'system'), false, 'system 消息不再留在消息列表里')
64
+ assert.deepEqual(options.messages[0], { role: 'user', content: [{ type: 'text', text: '你好' }] })
65
+ assert.deepEqual(options.messages[1], { role: 'assistant', content: [{ type: 'text', text: '你好呀' }] })
66
+ })
67
+
68
+ test('toGenerateOptions: 多条 system 按序折叠成一段;developer 同等对待', () => {
69
+ const options = toGenerateOptions({
70
+ messages: [
71
+ { role: 'system', content: '第一段' },
72
+ { role: 'developer', content: '第二段' },
73
+ { role: 'user', content: 'hi' },
74
+ ],
75
+ })
76
+ assert.equal(options.system, '第一段\n\n第二段')
77
+ assert.equal(options.messages.length, 1)
78
+ })
79
+
80
+ test('折叠后的 system 经 buildUpstreamBody 落到上游 system 尾块(身份块之后,issue #7 的 2 块形态)', async () => {
81
+ /**
82
+ * 端到端守卫(issue #6 第六节 + issue #7):官方身份块是 3012 准入判据,结构性
83
+ * 注入排最前;调用方 system 必须出现在身份块**之后**,而不是被丢掉。
84
+ * issue #7:上游 system 数组硬上限 3 块,插件只发 2 块 —— caller 内容与
85
+ * stable / Environment 合并进尾块,但仍在身份块之后。
86
+ */
87
+ const { buildUpstreamBody } = await import('../lib/anthropic-body.js')
88
+ const options = toGenerateOptions({
89
+ model: 'GLM-5.3-Flash',
90
+ messages: [
91
+ { role: 'system', content: 'CALLER PERSONA' },
92
+ { role: 'user', content: 'hi' },
93
+ ],
94
+ })
95
+ const body = buildUpstreamBody({ options, provider: 'zcode-start-plan' })
96
+ assert.equal(body.system[0].text.startsWith('You are ZCode, an interactive coding agent'), true, '身份块必须打头')
97
+ assert.equal(body.system.length, 2, 'issue #7:只发 2 块,给第三方 fetch 包装器留余量')
98
+ assert.ok(body.system.at(-1).text.endsWith('CALLER PERSONA'), '调用方 system 并入尾块且仍在最后')
99
+ assert.equal(body.messages.some((m) => m.role === 'system'), false)
60
100
  })
61
101
 
62
102
  test('toGenerateOptions: 空 messages / 非法 tool_choice 被拒', () => {
@@ -460,6 +460,46 @@ test('3009 成功命中后清忙标记,不再绕开该账号', async () => {
460
460
  assert.equal(busy, false, '命中成功 = 账号已空闲,忙标记立即清除')
461
461
  })
462
462
 
463
+ test('真实命中后清空 last_error(issue #6 缺陷 3:历史错误不再挂在健康账号上)', async () => {
464
+ const pool = fakePool([{ id: 'acct-a', credential: CREDENTIAL }])
465
+ const errors = []
466
+ pool.noteError = (accountId, message) => {
467
+ errors.push([accountId, message])
468
+ }
469
+ let cleared = 0
470
+ pool.clearError = (accountId) => {
471
+ cleared += 1
472
+ assert.equal(accountId, 'acct-a')
473
+ }
474
+ const client = new ZcodePlanClient({
475
+ pool,
476
+ minter: fakeMinter(),
477
+ fetchImpl: async () => response(200, textStream()),
478
+ })
479
+ const result = await client.call({ options: baseOptions })
480
+ assert.equal(result.accountId, 'acct-a')
481
+ assert.equal(cleared, 1, '命中即清 last_error')
482
+ assert.ok(
483
+ errors.every(([, message]) => message !== '额度已清'),
484
+ '清空走独立的 clearError 通道,不是再写一条错误',
485
+ )
486
+ })
487
+
488
+ test('失败路径不清 last_error(clearError 只在真实命中后调用)', async () => {
489
+ const pool = fakePool([{ id: 'acct-a', credential: CREDENTIAL }])
490
+ let cleared = 0
491
+ pool.clearError = () => {
492
+ cleared += 1
493
+ }
494
+ const client = new ZcodePlanClient({
495
+ pool,
496
+ minter: fakeMinter(),
497
+ fetchImpl: async () => response(200, '{"code":1005,"msg":"exceed quota limit"}', 'application/json'),
498
+ })
499
+ await assert.rejects(() => client.call({ options: baseOptions }), (error) => error.code === 'QUOTA')
500
+ assert.equal(cleared, 0, '额度用尽时错误文案必须保留给面板/status')
501
+ })
502
+
463
503
  test('钉定账号持续 3009 → RATE_LIMIT 报错,绝不换号', async () => {
464
504
  let calls = 0
465
505
  const client = new ZcodePlanClient({
@@ -94,6 +94,28 @@ test('markModelRateLimited 只影响那一对「账号×模型」,且到期后
94
94
  assert.notEqual(pool.pick('GLM-5.3-Flash'), null)
95
95
  }))
96
96
 
97
+ test('last_error:noteError 写入、clearError 清空(issue #6 缺陷 3)', withPool(async (pool) => {
98
+ const { entry } = pool.add(credential('a'), 'A')
99
+ pool.noteError(entry.id, '额度用尽(1005 exceed quota limit)')
100
+ assert.equal(pool.get(entry.id).last_error, '额度用尽(1005 exceed quota limit)')
101
+
102
+ /**
103
+ * 清空后读回必须是 `undefined`(落库 NULL)——`cli status` 用
104
+ * `last_error !== undefined`(现按真值)判断是否打印「最近错误」,
105
+ * 面板按「空 = 无错误」渲染。
106
+ */
107
+ pool.clearError(entry.id)
108
+ assert.equal(pool.get(entry.id).last_error, undefined)
109
+ assert.equal(pool.get(entry.id).last_error ?? null, null)
110
+ }))
111
+
112
+ test('clearError 对不存在/已删除的账号不抛(patch 幂等返回 false)', withPool(async (pool) => {
113
+ pool.clearError('no-such-account')
114
+ const { entry } = pool.add(credential('a'), 'A')
115
+ pool.remove(entry.id)
116
+ pool.clearError(entry.id)
117
+ }))
118
+
97
119
  test('忙标记:pick 优先绕开忙账号;全部忙时回落排队', withPool(async (pool) => {
98
120
  const a = pool.add(credential('a'), 'A').entry
99
121
  const b = pool.add(credential('b'), 'B').entry
@@ -1,7 +1,7 @@
1
1
  import test from 'node:test'
2
2
  import assert from 'node:assert/strict'
3
3
 
4
- import { buildZcodeHeaders, compareZcodeAppVersion, nextUtc8DayStartMs, toEpochMs, zcodeVersionGatedAppVersion, ZCODE_VERSION_GATED_MIN_APP_VERSION } from '../lib/upstream.js'
4
+ import { buildZcodeHeaders, compareZcodeAppVersion, nextUtc8DayStartMs, resetGatedMinAppVersion, toEpochMs, zcodeVersionGatedAppVersion, ZCODE_VERSION_GATED_MIN_APP_VERSION } from '../lib/upstream.js'
5
5
  import {
6
6
  classifyUpstreamError,
7
7
  concurrencyRetryDelayMs,
@@ -295,6 +295,12 @@ test('fetchBalance: 单桶时行为不变(buckets 仍下发,面板走汇总
295
295
  // ── 版本门控(服务端会随官方版本抬高下限,见 upstream.js 的观测表)──
296
296
 
297
297
  test('版本门控: marketing/claim 的 app_version 与下限取较大者', () => {
298
+ /**
299
+ * 防御顺序污染(issue #6 修复时暴露):任何早于本文件的测试若真发了
300
+ * 版本探测(e2e 的 `apply()` 不 stub 就会),运行期门控会被抬高且跨文件
301
+ * 存活 —— 断言「初值 = 编译期常量」前必须先复位。
302
+ */
303
+ resetGatedMinAppVersion()
298
304
  /** 凭据旧版本 → 抬到门控下限(低版本投放恒空、claim 1004)。 */
299
305
  assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.14.3' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
300
306
  assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.16.2' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)