Merge pull request 'fix: host plugin activates on a clean profile (v1.0.2)' (#2) from fix/host-activation-inject into main

This commit is contained in:
QQ 2026-11-14 17:28:28 +08:00
commit f87055b75b
6 changed files with 71 additions and 8 deletions

View File

@ -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`)」功能:

View File

@ -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) {

View File

@ -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",

View File

@ -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);
});

View File

@ -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, [])

View File

@ -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<string, unknown>): 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')
})