dsh-lean-router 2.2.1 → 2.2.3

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.
@@ -12,6 +12,12 @@
12
12
  - `npm pack --dry-run` / `npm publish` 把 notice 写到 stderr,PowerShell 会把它当错误并使退出码变成 1。用 `cmd /c "npm ... 2>&1"` 或只看 stdout 判断。
13
13
  - `npm publish` 返回成功后 registry 仍要约 90 秒才可见,立刻 `npm view` 会 404。不要把 404 当发布失败。
14
14
  - `npm.ps1` 在本机可能因 `npmNodeDir` 为空失败,一律用 `npm.cmd`。
15
+ - 发版后升级本机 profile,一律用 `node scripts/install-profile.mjs <version>`。它会拉包、替换 `node_modules`、用 Node 写回清单版本并自检(无 BOM、可解析、版本一致)。**不要手工用 PowerShell 改 profile 清单**。
16
+ - profile 的 `package.json` 一旦带 UTF-8 BOM,内核 `JSON.parse` 直接抛 `Unexpected token '', "{ "depe"... is not valid JSON`,desktop 配置档加载失败,所有会话起不来(对话界面表现为热重载后每个进行中的对话都报这句)。PowerShell 5.1 的 `Set-Content -Encoding utf8` / `Out-File -Encoding utf8` 会写 BOM;必须写时的安全写法:
17
+ - PowerShell:`[IO.File]::WriteAllText($p, $text, (New-Object System.Text.UTF8Encoding($false)))`
18
+ - Node:`writeFileSync(p, JSON.stringify(manifest, null, 2) + '\n')`
19
+ 写完后必须复核:文件头 3 字节不得是 `EF BB BF`,且 `JSON.parse` 通过。
20
+ - 不要在 DSH 运行中替换 profile 的 `node_modules`:文件监听会触发客户端插件热重载,正在进行的对话会重读缓存并报错。正确顺序是**先完全退出 DSH → 再装 → 再启动**。
15
21
 
16
22
  ## 版本号判断
17
23
 
@@ -8,7 +8,21 @@
8
8
 
9
9
  复测的做法:从宿主类型定义或现有实现里确认真实结构,构造同样的形状跑一遍,对比前后长度、可解析性、关键字段是否保留。
10
10
 
11
- ## arch 图操作复核
11
+ ## PowerShell 会谎报退出码
12
+
13
+ 原生命令只要往 stderr 写任何东西(`npm` 的 notice/progress、Node 的 `DeprecationWarning`、`git push` 的进度行),PowerShell 就会把它包成 `NativeCommandError`,工具层看到的退出码变成 1——**即使命令实际完全成功**。
14
+
15
+ 判据:不要只信退出码,用能独立复核的状态判断。
16
+
17
+ - git 推送:看 `git status -sb` 还有没有 `ahead`,而不是看 `git push` 的退出码。
18
+ - npm 发布:看 `npm view <pkg>@<ver> version`,而不是看 `npm publish` 的退出码。
19
+ - 脚本内部:Windows 上避免 `execFileSync` + `shell:true` + args 数组(会触发 DEP0190 警告写 stderr),改用 `execSync` 的字符串形式。
20
+
21
+ ## 用结构验证,不要用 grep 验证
22
+
23
+ `grep` 到关键词只证明这段文字在文件里存在,不证明它生效:它可能整行被注释掉、可能在错误的缩进层级、可能被后面的定义覆盖。
24
+
25
+ 验证配置类文件时,解析它、取出**最终生效的那一份**、断言字段存在。补丁 YAML 的典型陷阱是同一个 `id` 有多个定义,最后写入的才生效。
12
26
 
13
27
  `.arch/` 是架构引擎自己的 shadow Git,不要 `git add` 进去;仓库已用 `.gitignore` 忽略,克隆后它不存在,需要时重新 `arch init`。
14
28
 
@@ -1,6 +1,20 @@
1
1
  # Changelog
2
2
 
3
- > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 2.2.1).
3
+ > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 2.2.3).
4
+
5
+ ## 2.2.3 (2026-11-29)
6
+
7
+ ### Changed
8
+
9
+ - **Removed the forced opener**: the `spec` band no longer asks reasoning and the first reply to start with "We need …", and `react` no longer asks for "I'm doing …". Real-world use showed no benefit to routing or output quality — only extra fixed tokens and stylistic noise. `guideText` now keeps just the task tone plus the fixed convergence and anti-drift anchors (`GUIDE_TAIL`). `personaText`, `ROUTER_PROMPT` and the preset shards are untouched.
10
+ - **Tests inverted**: `test/router-bands.test.mjs` now asserts that every band's `guideText` contains neither "We need", "I'm doing", nor any first-reply constraint, so the opener cannot creep back in.
11
+
12
+ ## 2.2.2 (2026-11-29)
13
+
14
+ ### Fixed
15
+
16
+ - **Session creation failed outright (`agent-preset/invalid`)**: after 2.2.1 moved `lean-usage-ledger` into the presets' `plugins` array, the plugin read `ctx.commands` directly inside `apply`; cordis 4 requires a declared injection before that read, so it threw `cannot get property "commands" without inject` and both creating and restoring sessions failed. The plugin now declares `inject: ['commands']` (named export and default export alike): while `commands` is missing cordis waits instead of loading, and when the command seam never appears the plugin simply does not mount — neither path disturbs the host anymore, and `/token-report` behaves as before.
17
+ - **Regression test**: new `test/usage-ledger-inject.test.mjs` loads the plugin with real cordis in the "plugin first, `commands` later" order and asserts it does not throw and registers `/token-report` once the service appears.
4
18
 
5
19
  ## 2.2.1 (2026-11-29)
6
20
 
package/docs/changelog.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # 变更记录(changelog)
2
2
 
3
- > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 2.2.1)。
3
+ > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 2.2.3)。
4
+
5
+ ## 2.2.3 (2026-11-29)
6
+
7
+ ### 变更
8
+
9
+ - **移除强制开头句式**:`spec` 带不再要求推理与回复首句以 "We need …" 开头,`react` 带不再要求 "I'm doing …"。实机使用后判定该句式对路由与产出无收益,只额外增加固定 token 与表达噪音;`guideText` 现只保留任务基调与固定的收敛/反跑题锚(`GUIDE_TAIL`)。`personaText`、`ROUTER_PROMPT` 与分片均未改动。
10
+ - **测试改为反向断言**:`test/router-bands.test.mjs` 现在断言三条带的 `guideText` 都不含 "We need"、"I'm doing" 或对首句的约束,防止句式被重新加回。
11
+
12
+ ## 2.2.2 (2026-11-29)
13
+
14
+ ### 修复
15
+
16
+ - **创建会话直接失败(`agent-preset/invalid`)**:2.2.1 把 `lean-usage-ledger` 移进 preset 的 `plugins` 后,插件在 `apply` 里直接读 `ctx.commands`;cordis 4 要求先声明注入,未声明就读取会抛 `cannot get property "commands" without inject`,宿主新建与恢复会话都因此失败。插件现声明 `inject: ['commands']`(命名导出与默认导出同体):`commands` 未就绪时 cordis 等待而不是加载,命令缝始终缺席时插件静默不挂载,两种情况下都不再打断宿主;`/token-report` 行为不变。
17
+ - **回归测试**:新增 `test/usage-ledger-inject.test.mjs`,用真实 cordis 复现“先加载插件、后提供 `commands`”的顺序,断言不抛错、`commands` 出现后注册 `/token-report`。
4
18
 
5
19
  ## 2.2.1 (2026-11-29)
6
20
 
package/lib/bands.js CHANGED
@@ -35,8 +35,7 @@ export const BANDS = {
35
35
  GUIDE_TAIL,
36
36
  guideText:
37
37
  "Near-field guide (inspect-and-plan): you are working a fix/inspection task. Read and locate before changing; state the plan when your information is complete, then apply the smallest safe change and verify it. " +
38
- GUIDE_TAIL +
39
- ' Start reasoning and your first reply with "We need …", naming the gap before acting.',
38
+ GUIDE_TAIL,
40
39
  },
41
40
  react: {
42
41
  id: "react",
@@ -47,8 +46,7 @@ export const BANDS = {
47
46
  GUIDE_TAIL,
48
47
  guideText:
49
48
  "Near-field guide (hands-on production): you are working a build task. Produce the next working increment now; verify by running it before claiming progress. " +
50
- GUIDE_TAIL +
51
- ' Start reasoning and your first reply with "I\'m doing …", naming the action you take.',
49
+ GUIDE_TAIL,
52
50
  },
53
51
  weak: {
54
52
  id: "weak",
@@ -14,6 +14,12 @@ import { createLiveUsageAdapter } from '../token-ledger/live.js';
14
14
 
15
15
  export const name = 'lean-usage-ledger';
16
16
 
17
+ // cordis 4 的注入声明:未声明的服务属性在 apply 里读取会抛
18
+ // `cannot get property "commands" without inject`,宿主创建会话即失败
19
+ // (agent-preset/invalid)。声明后 cordis 会等 commands 就绪再加载本插件,
20
+ // 命令缝不存在时插件只是不挂载,绝不打断宿主启动。
21
+ export const inject = ['commands'];
22
+
17
23
  const TAG = '[lean-usage-ledger]';
18
24
  const COMMAND_NAME = 'token-report';
19
25
  const SCOPES = ['session', 'all'];
@@ -231,7 +237,7 @@ export function apply(ctx, config) {
231
237
  }
232
238
 
233
239
  /** 默认导出(插件行兼容形态,与命名导出同体)。 */
234
- export default { name, apply };
240
+ export default { name, inject, apply };
235
241
 
236
242
  /** 测试与集成内用的内部实现出口(不进公开契约文档)。 */
237
243
  export const __internals = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lean-router",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "lean 省 token × 思考链路由 × code_run 工具遮蔽的 DSH cordis 插件(单 bundle:预设)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -0,0 +1,113 @@
1
+ // dsh-lean-router - 把指定版本装进 desktop profile 的 node_modules,并同步 profile 清单版本号。
2
+ //
3
+ // 为什么必须用这个脚本而不是手工替换:
4
+ // profile 的 package.json 一旦带 UTF-8 BOM,内核 JSON.parse 会抛
5
+ // `Unexpected token '', "{ "depe"... is not valid JSON`,桌面配置档加载失败,
6
+ // 所有会话起不来。PowerShell 5.1 的 Set-Content/Out-File -Encoding utf8 会写 BOM。
7
+ // 本脚本一律用 writeFileSync 写 UTF-8(无 BOM),并在写完后自检。
8
+ //
9
+ // 用法:node scripts/install-profile.mjs <version> [--profile <dir>] [--registry <url>]
10
+ import { execFileSync, execSync } from "node:child_process";
11
+ import { cpSync, existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+
15
+ const PKG = "dsh-lean-router";
16
+ const DEFAULT_PROFILE = "C:\\Users\\charl\\.dsh\\profiles\\desktop";
17
+ const DEFAULT_REGISTRY = "https://registry.npmjs.org";
18
+
19
+ function parseArgs(argv) {
20
+ const options = { version: undefined, profile: DEFAULT_PROFILE, registry: DEFAULT_REGISTRY };
21
+ for (let i = 0; i < argv.length; i += 1) {
22
+ const arg = argv[i];
23
+ if (arg === "--profile") options.profile = argv[++i];
24
+ else if (arg === "--registry") options.registry = argv[++i];
25
+ else if (!arg.startsWith("--") && options.version === undefined) options.version = arg;
26
+ }
27
+ return options;
28
+ }
29
+
30
+ function fail(message) {
31
+ console.error("[install-profile] 失败:" + message);
32
+ process.exit(1);
33
+ }
34
+
35
+ function tarCommand() {
36
+ // Windows 上 GNU tar 会解析到 MSYS 版并对 C:\ 路径报错,必须用系统自带 bsdtar。
37
+ if (process.platform === "win32") {
38
+ const bsdtar = "C:\\Windows\\System32\\tar.exe";
39
+ if (existsSync(bsdtar)) return bsdtar;
40
+ }
41
+ return "tar";
42
+ }
43
+
44
+ function assertNoBom(file, label) {
45
+ const buf = readFileSync(file);
46
+ if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
47
+ fail(label + " 含 UTF-8 BOM(会被内核 JSON.parse 拒绝):" + file);
48
+ }
49
+ return buf;
50
+ }
51
+
52
+ function readJson(file, label) {
53
+ const buf = assertNoBom(file, label);
54
+ try {
55
+ return JSON.parse(buf.toString("utf8"));
56
+ } catch (error) {
57
+ fail(label + " 不是合法 JSON:" + file + " :: " + error.message);
58
+ }
59
+ }
60
+
61
+ const options = parseArgs(process.argv.slice(2));
62
+ if (options.version === undefined) fail("缺少版本号。用法:node scripts/install-profile.mjs <version>");
63
+
64
+ const { version, profile, registry } = options;
65
+ if (!/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(version)) fail("版本号不合法:" + version);
66
+ if (!existsSync(join(profile, "package.json"))) fail("profile 不存在:" + profile);
67
+
68
+ // 1) 拉包到临时目录并解压
69
+ const stage = mkdtempSync(join(tmpdir(), "dsh-lean-router-install-"));
70
+ try {
71
+ // npm 在 Windows 上是 .cmd,Node 无法直接 spawn(EINVAL);用拼接字符串的 execSync 绕开,
72
+ // 同时避免 execFileSync + shell:true + 数组参数触发的 DEP0190 警告。
73
+ execSync(["npm", "pack", PKG + "@" + version, "--registry", registry, "--silent"].join(" "), {
74
+ cwd: stage,
75
+ stdio: ["ignore", "pipe", "inherit"],
76
+ });
77
+ const tarball = readdirSync(stage).find((name) => name.endsWith(".tgz"));
78
+ if (tarball === undefined) fail("npm pack 没有产出 tarball");
79
+ execFileSync(tarCommand(), ["-xzf", join(stage, tarball), "-C", stage], { stdio: "inherit" });
80
+
81
+ const source = join(stage, "package");
82
+ if (!existsSync(join(source, "package.json"))) fail("tarball 解压后缺少 package/package.json");
83
+ const packed = readJson(join(source, "package.json"), "打包内的 package.json");
84
+ if (packed.name !== PKG || packed.version !== version) {
85
+ fail("tarball 内容不符:" + packed.name + "@" + packed.version);
86
+ }
87
+
88
+ // 2) 替换 node_modules 里的包目录
89
+ const target = join(profile, "node_modules", PKG);
90
+ rmSync(target, { recursive: true, force: true });
91
+ cpSync(source, target, { recursive: true });
92
+
93
+ // 3) 同步 profile 清单版本号(Node 写 UTF-8,永不产生 BOM)
94
+ const manifestPath = join(profile, "package.json");
95
+ const manifest = readJson(manifestPath, "profile 的 package.json");
96
+ manifest.dependencies = { ...(manifest.dependencies ?? {}), [PKG]: version };
97
+ writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8");
98
+
99
+ // 4) 自检:无 BOM、可解析、版本一致、插件文件存在
100
+ const written = readFileSync(manifestPath);
101
+ if (written[0] === 0xef && written[1] === 0xbb && written[2] === 0xbf) fail("写回后仍含 BOM");
102
+ const reloaded = JSON.parse(written.toString("utf8"));
103
+ if (reloaded.dependencies?.[PKG] !== version) fail("写回后清单版本不一致");
104
+ const installed = readJson(join(target, "package.json"), "已安装的 package.json");
105
+ if (installed.version !== version) fail("node_modules 内版本不一致:" + installed.version);
106
+ if (!existsSync(join(target, "lib", "lean", "usage-ledger.js"))) fail("安装结果缺少 lib/lean/usage-ledger.js");
107
+
108
+ console.log("[install-profile] 完成:" + PKG + "@" + version + " -> " + target);
109
+ console.log("[install-profile] 清单 " + manifestPath + " 无 BOM、可解析、版本一致");
110
+ console.log("[install-profile] 下一步:完全退出并重启 DSH,然后在宿主运行时不要替换这些文件(热重载会重读缓存)。");
111
+ } finally {
112
+ rmSync(stage, { recursive: true, force: true });
113
+ }
@@ -33,24 +33,19 @@ test("BANDS 三带形状:id/label/personaText/guideText 齐备且工具面宽
33
33
  assert.equal(BANDS.weak.toolsWide, false);
34
34
  });
35
35
 
36
- test("思考句式:spec/react 引导带固定开头句式,weak 不强制,persona 保持原样", () => {
37
- // spec:先陈述缺口(We need …)再行动。
38
- assert.ok(BANDS.spec.guideText.includes("We need"), "spec 引导含 We need 句式");
39
- assert.ok(BANDS.spec.guideText.includes("reasoning"), "spec 引导覆盖内部推理");
40
- assert.ok(BANDS.spec.guideText.includes("first reply"), "spec 引导覆盖回复第一句");
41
- assert.ok(!BANDS.spec.guideText.includes("I'm doing"), "spec 不混入 react 句式");
42
- // react:直接说明正在做的动作(I'm doing …)。
43
- assert.ok(BANDS.react.guideText.includes("I'm doing"), "react 引导含 I'm doing 句式");
44
- assert.ok(BANDS.react.guideText.includes("reasoning"), "react 引导覆盖内部推理");
45
- assert.ok(BANDS.react.guideText.includes("first reply"), "react 引导覆盖回复第一句");
46
- assert.ok(!BANDS.react.guideText.includes("We need"), "react 不混入 spec 句式");
47
- // weak:交给模型自判,不加任何强制句式。
48
- assert.ok(!BANDS.weak.guideText.includes("We need"), "weak 不强制 spec 句式");
49
- assert.ok(!BANDS.weak.guideText.includes("I'm doing"), "weak 不强制 react 句式");
36
+ test("引导文本:不再强制任何开头句式,只保留固定前缀与固定基调", () => {
37
+ // 曾经要求 spec 以 "We need …"、react 以 "I'm doing …" 开头;实机使用后判定无收益,已移除。
38
+ // 这里反向断言,防止句式被重新加回来。
39
+ for (const id of BAND_IDS) {
40
+ assert.ok(!BANDS[id].guideText.includes("We need"), id + " 引导不含 We need 句式");
41
+ assert.ok(!BANDS[id].guideText.includes("I'm doing"), id + " 引导不含 I'm doing 句式");
42
+ assert.ok(!BANDS[id].guideText.includes("first reply"), id + " 引导不约束回复首句");
43
+ }
50
44
  // 固定前缀与固定基调不变(GUIDE_TAIL 仍保留)。
51
45
  for (const id of BAND_IDS) {
52
46
  assert.ok(BANDS[id].guideText.startsWith("Near-field guide"), id + " 引导固定前缀");
53
47
  assert.ok(BANDS[id].guideText.includes("Recall what you have already done"), id + " 保留 GUIDE_TAIL");
48
+ assert.ok(BANDS[id].guideText.includes("Converge"), id + " 保留收敛锚");
54
49
  }
55
50
  // personaText 当前无运行时消费者:保持原样,不新增句式。
56
51
  for (const id of BAND_IDS) {
@@ -0,0 +1,50 @@
1
+ // test/usage-ledger-inject.test.mjs —— 回归:未注入 commands 时读取属性会抛
2
+ // `cannot get property "commands" without inject`,宿主创建会话直接失败
3
+ // (agent-preset/invalid)。本测试用真实 cordis 加载插件,保证加载顺序不再致命。
4
+ import assert from 'node:assert/strict';
5
+ import test from 'node:test';
6
+ import { Context } from '@deepseek-ai/cordis';
7
+ import * as plugin from 'dsh-lean-router/lean/usage-ledger';
8
+
9
+ const tick = () => new Promise((resolve) => setTimeout(resolve, 20));
10
+
11
+ test('插件声明 commands 依赖(命名导出与默认导出同体)', () => {
12
+ assert.deepEqual(plugin.inject, ['commands'], 'inject 必须声明 commands');
13
+ assert.deepEqual(plugin.default.inject, ['commands'], '默认导出也要带 inject');
14
+ });
15
+
16
+ test('commands 尚未就绪时加载不抛错,服务出现后自动注册 /token-report', async () => {
17
+ const app = new Context();
18
+ const registered = [];
19
+ let thrown = null;
20
+
21
+ // 先加载插件、后提供 commands:正是 2.2.1 里让宿主报 agent-preset/invalid 的顺序。
22
+ try {
23
+ app.plugin(plugin, {});
24
+ } catch (error) {
25
+ thrown = error;
26
+ }
27
+ await tick();
28
+ assert.equal(thrown, null, '加载阶段不得抛错');
29
+ assert.deepEqual(registered, [], 'commands 缺席时不应注册任何命令');
30
+
31
+ app.provide('commands', {
32
+ register(command) {
33
+ registered.push(command.name);
34
+ },
35
+ });
36
+ await tick();
37
+ assert.deepEqual(registered, ['token-report'], 'commands 出现后应挂上 /token-report');
38
+ });
39
+
40
+ test('commands 始终缺席时保持静默:不抛错、不注册', async () => {
41
+ const app = new Context();
42
+ let thrown = null;
43
+ try {
44
+ app.plugin(plugin, {});
45
+ } catch (error) {
46
+ thrown = error;
47
+ }
48
+ await tick();
49
+ assert.equal(thrown, null, '没有命令缝也不能打断宿主');
50
+ });