From 98a8645fdd2b0d02ceeaafab2f1b3fbba24bf86e Mon Sep 17 00:00:00 2001 From: qyh15 Date: Sat, 14 Nov 2026 23:36:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(host):=20apply=20=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20+=20=E7=8A=B6=E6=80=81=E6=83=B0=E6=80=A7?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=EF=BC=88=E7=9C=9F=E5=AE=9E=E5=AE=BF=E4=B8=BB?= =?UTF-8?q?=E9=87=8C=E4=BF=AE=E5=A4=8D=20cordis=20Invalid=20effect?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + sansheng/index.js | 12 ++++-------- sansheng/src/host/provision.js | 2 ++ sansheng/src/host/service.js | 6 ++++++ sansheng/src/host/store.js | 21 +++++++++++++++++++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 288efa1..b65a936 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ ds-bot/ node_modules/ +.dsh-test-*/ *.tmp *.log diff --git a/sansheng/index.js b/sansheng/index.js index d73f110..e1aa966 100644 --- a/sansheng/index.js +++ b/sansheng/index.js @@ -10,16 +10,14 @@ import { ROUTE } from './src/host/constants.js' export const name = 'sansheng' export const inject = ['workspaceRegistry', 'sessionController', 'agents', 'llm', 'systemPrompt', 'connection'] -export async function apply(ctx, config = {}) { +export function apply(ctx, config = {}) { const store = new SanshengStore( typeof config.statePath === 'string' && config.statePath !== '' ? config.statePath : sanshengStatePath(config.home), ) - try { - await store.load() - } catch (error) { - // 状态损坏不能静默丢弃;记录后仍允许插件加载,但后续操作会因空状态而需重新启用。 + // apply 必须同步返回(cordis 把异步/非函数返回值当非法 effect);状态在首次请求时惰性读入。 + void store.ensureLoaded().catch(error => { console.warn(`sansheng: ${error?.message ?? error}`) - } + }) const host = { workspaceRegistry: ctx.workspaceRegistry, @@ -45,6 +43,4 @@ export async function apply(ctx, config = {}) { return new Response(JSON.stringify(result), { status: 200, headers: { 'content-type': 'application/json' } }) }, }), 'sansheng: api route') - - return service } diff --git a/sansheng/src/host/provision.js b/sansheng/src/host/provision.js index 5d88fc5..b982a4c 100644 --- a/sansheng/src/host/provision.js +++ b/sansheng/src/host/provision.js @@ -55,6 +55,7 @@ export function createProvisioner({ store, host, now = () => Date.now() }) { } async function ensureSeat(projectId, seatId) { + await store.ensureLoaded() const seat = store.get(projectId).seats[seatId] if (seat.status === 'bound' && seat.sessionId) return { ok: true, skipped: true } @@ -103,6 +104,7 @@ export function createProvisioner({ store, host, now = () => Date.now() }) { async function enable(projectId) { return withLock(projectId, async () => { + await store.ensureLoaded() const project = store.get(projectId) if (!project) throw new ProvisionError('sansheng/not-found', `项目 ${projectId} 不存在`) await store.mutate(s => { diff --git a/sansheng/src/host/service.js b/sansheng/src/host/service.js index 45228de..d4f2501 100644 --- a/sansheng/src/host/service.js +++ b/sansheng/src/host/service.js @@ -77,6 +77,7 @@ export class SanshengService { } async list() { + await this.store.ensureLoaded() return { projects: [...this.store.list()] .sort((a, b) => String(a.name).localeCompare(String(b.name), 'zh-Hans-CN')) @@ -85,6 +86,7 @@ export class SanshengService { } async open({ workspaceId, name, path } = {}) { + await this.store.ensureLoaded() if (!workspaceId && !path) throw new ServiceError('sansheng/invalid', '需要 workspaceId 或 path') const registry = this.host.workspaceRegistry let ws @@ -108,18 +110,21 @@ export class SanshengService { } async project(projectId) { + await this.store.ensureLoaded() const p = this.store.get(projectId) if (!p) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) return projectView(p) } async enable(projectId) { + await this.store.ensureLoaded() if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) await this.provisioner.enable(projectId) return this.project(projectId) } async configure(projectId, seatId, { model, instructions } = {}) { + await this.store.ensureLoaded() if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${seatId}`) const project = this.store.get(projectId) if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) @@ -147,6 +152,7 @@ export class SanshengService { } async models() { + await this.store.ensureLoaded() const catalog = await this.host.sessionController.modelCatalog() return normalizeCatalog(catalog) } diff --git a/sansheng/src/host/store.js b/sansheng/src/host/store.js index d833aae..c767936 100644 --- a/sansheng/src/host/store.js +++ b/sansheng/src/host/store.js @@ -9,14 +9,29 @@ export class StoreError extends Error { } export class SanshengStore { - constructor(file) { this.file = file; this.state = emptyState(); this._queue = Promise.resolve() } + constructor(file) { + this.file = file + this.state = emptyState() + this._queue = Promise.resolve() + this._loaded = false + this._loadPromise = null + } + + // 惰性加载:插件 apply 必须同步返回,真实 HTTP 请求到达时再确保状态已读入。 + ensureLoaded() { + if (this._loaded) return Promise.resolve() + if (!this._loadPromise) { + this._loadPromise = this.load().catch(error => { this._loadPromise = null; throw error }) + } + return this._loadPromise + } async load() { let raw try { raw = await readFile(this.file, 'utf8') } catch (error) { - if (error?.code === 'ENOENT') { this.state = emptyState(); return } + if (error?.code === 'ENOENT') { this.state = emptyState(); this._loaded = true; return } throw new StoreError('sansheng/state-corrupt', `无法读取状态文件:${error?.message ?? error}`) } let parsed @@ -26,6 +41,7 @@ export class SanshengStore { const valid = validateState(parsed) if (!valid.ok) throw new StoreError('sansheng/state-corrupt', `状态文件损坏:${valid.error}`) this.state = valid.state + this._loaded = true } snapshot() { return structuredClone(this.state) } @@ -36,6 +52,7 @@ export class SanshengStore { // 所有写操作经此串行化:同一时刻只有一个 mutate 在跑,失败回滚内存状态。 async mutate(fn) { const run = async () => { + await this.ensureLoaded() const before = structuredClone(this.state) try { const result = await fn(this.state)