From 7774d6fc285e36fb3568b5f310255c1bc139a2bc Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 17:21:52 +0800 Subject: [PATCH] fix: mount auto-review listeners through ctx.inject so the host activates on a clean profile (v1.0.2) Cordis throws when a service is read before it is declared in inject: 'cannot get property "permissionPresets" without inject'. apply() probed ctx.permissionPresets first, so a profile without permissionPresets/agents died during activation and its else-branch ctx.inject fallback was dead code. The plugin now always wires the listeners through a scoped ctx.inject, which also re-mounts them when the auto-review services appear later. --- CHANGELOG.md | 5 +++ lib/host.js | 3 +- package.json | 2 +- src/host.ts | 11 +++++-- tests/chat/permission-policy.test.ts | 9 +++-- tests/host-activation.test.ts | 49 ++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 tests/host-activation.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 582584a..5037e89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.0.2 — 2026-11-14 + +- 修复 host 插件在干净 profile 里激活失败(`1 entry did not activate`):`apply()` 先读 `ctx.permissionPresets` 再判断,而 Cordis 对未在 `inject` 声明的服务读取会直接抛 `cannot get property "permissionPresets" without inject`,于是 `else` 里的 `ctx.inject` 兜底永远走不到。现在统一用 `ctx.inject(['permissionPresets', 'agents'], …)` 挂载自动审查监听,服务缺失时插件照常激活,服务就绪后再挂载;reveal / metrics 路由不受影响。 +- 新增 `tests/host-activation.test.ts`:用真实 Cordis `Context` 在只提供 `webServer` 的干净环境加载 host 入口,断言 fiber 为 `ACTIVE`,并验证服务就绪后空白会话仍会被切到 `auto`。 + ## 1.0.1 — 2026-11-13 - 恢复并完善「默认打开模式为自动审查(`auto`)」功能: diff --git a/lib/host.js b/lib/host.js index aef1987..efb5973 100644 --- a/lib/host.js +++ b/lib/host.js @@ -410,8 +410,7 @@ function apply(ctx, options) { const flags = options?.flags ?? loadFlags(); log.info("[dsh-better-chat] loaded", { readerEnabled: flags.readerEnabled }); const anyCtx = ctx; - if (anyCtx.permissionPresets && anyCtx.agents) mountAutoReviewListeners(anyCtx); - else if (typeof anyCtx.inject === "function") anyCtx.inject(["permissionPresets", "agents"], (childCtx) => { + if (typeof anyCtx.inject === "function") anyCtx.inject(["permissionPresets", "agents"], (childCtx) => { mountAutoReviewListeners(childCtx); }); if (!flags.readerEnabled) { diff --git a/package.json b/package.json index 9f4292a..65b1c4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-better-chat", - "version": "1.0.1", + "version": "1.0.2", "description": "DSH 聊天增强插件:审批回车批准 / Esc 拒绝、权限 UI、插件商店更新,以及 reading view(合并自 dsh-better-display)", "type": "module", "license": "MIT", diff --git a/src/host.ts b/src/host.ts index 99ebae7..e4d1c69 100644 --- a/src/host.ts +++ b/src/host.ts @@ -139,10 +139,15 @@ export function apply(ctx: Context, options?: { readonly flags?: Flags }): void const flags = options?.flags ?? loadFlags(); log.info('[dsh-better-chat] loaded', { readerEnabled: flags.readerEnabled }); + // Cordis throws when a service is read before it is declared in `inject` + // ("cannot get property \"permissionPresets\" without inject"), so never probe the + // services on the context directly: that read aborted apply() on a clean + // profile and the host reported "1 entry did not activate". A scoped + // `ctx.inject` mounts the listeners once both services exist and unmounts + // them when they go away; a plain object context (unit tests) has no inject + // and is simply skipped. const anyCtx = ctx as any; - if (anyCtx.permissionPresets && anyCtx.agents) { - mountAutoReviewListeners(anyCtx); - } else if (typeof anyCtx.inject === 'function') { + if (typeof anyCtx.inject === 'function') { anyCtx.inject(['permissionPresets', 'agents'], (childCtx: any) => { mountAutoReviewListeners(childCtx); }); diff --git a/tests/chat/permission-policy.test.ts b/tests/chat/permission-policy.test.ts index d0c2e97..d11cd79 100644 --- a/tests/chat/permission-policy.test.ts +++ b/tests/chat/permission-policy.test.ts @@ -243,7 +243,10 @@ test('host auto-review default: switches fresh blank sessions and skips used/sub } const agents = { list: () => [{ session: session1 }] } - applyHost({ + // Cordis 只在 inject 声明的服务可用时才把带服务的上下文交给回调;这里同步调用 + // 即可复现实机行为。直接读 ctx.permissionPresets 会在真实 Context 上抛 + // `cannot get property "permissionPresets" without inject`。 + const ctxStub: any = { permissionPresets, agents, effect(run: () => void) { run() }, @@ -251,7 +254,9 @@ test('host auto-review default: switches fresh blank sessions and skips used/sub listeners.set(event, fn) return () => { listeners.delete(event) } }, - } as any, { flags: { readerEnabled: false } }) + inject(_deps: string[], callback: (child: any) => void) { callback(ctxStub) }, + } + applyHost(ctxStub, { flags: { readerEnabled: false } }) // Before 'auto' is registered in permissionPresets.names, nothing is switched yet. assert.deepEqual(setCalls, []) diff --git a/tests/host-activation.test.ts b/tests/host-activation.test.ts new file mode 100644 index 0000000..3faba25 --- /dev/null +++ b/tests/host-activation.test.ts @@ -0,0 +1,49 @@ +import assert from 'node:assert/strict' +import { test } from 'vitest' +import { Context } from '@deepseek-ai/cordis' +import * as host from '../src/host.js' + +/** + * Regression: dsh-better-chat 1.0.1 read `ctx.permissionPresets` before declaring + * it in `inject`. Cordis throws `cannot get property "permissionPresets" without + * inject` on that read, so a clean profile (§ only provides webServer) failed the + * host activation with "1 entry did not activate". The plugin must activate and + * leave the auto-review wiring to a scoped `ctx.inject` callback. + */ +function createContext(services: Record): Context { + const ctx = new Context() + for (const [name, value] of Object.entries(services)) { + ctx.provide(name as never) + ctx.set(name as never, value as never) + } + return ctx +} + +test('host plugin activates when permissionPresets/agents are absent', async () => { + const ctx = createContext({ webServer: { register: () => () => undefined } }) + const fiber = ctx.plugin(host as never) + await fiber + assert.equal(fiber.state, 2, 'fiber should be ACTIVE, not FAILED/PENDING') +}) + +test('host plugin wires the auto-review default through inject', async () => { + const setCalls: Array<[string, string]> = [] + const ctx = createContext({ + webServer: { register: () => () => undefined }, + permissionPresets: { + names: ['read-only', 'auto'], + current: () => 'workspace-write', + set: (session: { id: string }, name: string) => setCalls.push([session.id, name]), + }, + agents: { + list: () => [{ + session: { id: 'fresh-1', header: { id: 'fresh-1' }, snapshotEvents: () => [] }, + }], + }, + }) + const fiber = ctx.plugin(host as never) + await fiber + assert.equal(fiber.state, 2) + await new Promise(resolve => setTimeout(resolve, 50)) + assert.deepEqual(setCalls, [['fresh-1', 'auto']], 'blank top-level session should default to auto review') +})