Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix: 按会话顺序写入空闲模型配置

geng yue71d07c14f30d
diff --git a/apps/desktop/src/stores/app-store.ts b/apps/desktop/src/stores/app-store.ts
index 3fe846955..f89ef5b82 100644
--- a/apps/desktop/src/stores/app-store.ts
+++ b/apps/desktop/src/stores/app-store.ts
@@ -173,6 +173,7 @@ export const useAppStore = create<AppState>((set, get) => {
rememberSessionCompactions,
commitForkedSession,
persistSessionAndSelect,
+ flushPendingSessionConfiguration,
}),
...createQueueSlice({
diff --git a/apps/desktop/src/stores/slices/session-slice.ts b/apps/desktop/src/stores/slices/session-slice.ts
index 34d364034..fe8cc396d 100644
--- a/apps/desktop/src/stores/slices/session-slice.ts
+++ b/apps/desktop/src/stores/slices/session-slice.ts
@@ -61,6 +61,7 @@ import { switchWorkPanelSession } from "./work-panel-slice";
export type SessionSliceDependencies = StoreAccess & {
runtime: SessionRuntime;
+ flushPendingSessionConfiguration: (sessionId: string) => Promise<void>;
decorateSessions: (
sessions: SessionSummary[],
meta: Record<string, SessionMeta>,
@@ -156,6 +157,7 @@ export function createSessionSlice({
rememberSessionCompactions,
commitForkedSession,
persistSessionAndSelect,
+ flushPendingSessionConfiguration,
}: SessionSliceDependencies): Pick<
AppState,
| "refreshSessions"
@@ -849,8 +851,7 @@ export function createSessionSlice({
config,
);
runtime.pendingSessionConfigurations.delete(sessionId);
- const result = await api.configureSession(sessionId, payload);
- set((state) => ({
+ const write = api.configureSession(sessionId, payload).then((result) => set((state) => ({
sessions: state.sessions.map((session) =>
session.id === sessionId
? {
@@ -864,7 +865,16 @@ export function createSessionSlice({
...state.planningStates,
[sessionId]: result.session.mode === "plan" ? "planning" : "inactive",
},
- }));
+ })));
+ runtime.sessionConfigurationFlushes.set(sessionId, write);
+ try {
+ await write;
+ } finally {
+ if (runtime.sessionConfigurationFlushes.get(sessionId) === write) {
+ runtime.sessionConfigurationFlushes.delete(sessionId);
+ }
+ await flushPendingSessionConfiguration(sessionId);
+ }
},
/** Abort one session's running turn, whether or not it is the visible one. */
diff --git a/apps/desktop/test/session-configuration-staging.test.mjs b/apps/desktop/test/session-configuration-staging.test.mjs
index a10ed7125..6d5e7e85a 100644
--- a/apps/desktop/test/session-configuration-staging.test.mjs
+++ b/apps/desktop/test/session-configuration-staging.test.mjs
@@ -34,7 +34,7 @@ test("staged next-turn configuration merges per field instead of replacing", ()
// The direct (idle) path folds a leftover staged entry under the new fields.
assert.match(
sessionSlice,
- /const payload = runtime\.mergeSessionConfiguration\(\n\s*runtime\.pendingSessionConfigurations\.get\(sessionId\),\n\s*config,\n\s*\);\n\s*runtime\.pendingSessionConfigurations\.delete\(sessionId\);\n\s*const result = await api\.configureSession\(sessionId, payload\);/,
+ /const payload = runtime\.mergeSessionConfiguration\(\n\s*runtime\.pendingSessionConfigurations\.get\(sessionId\),\n\s*config,\n\s*\);\n\s*runtime\.pendingSessionConfigurations\.delete\(sessionId\);\n\s*const write = api\.configureSession\(sessionId, payload\)/,
);
});
diff --git a/apps/desktop/test/session-model-write-order.test.mjs b/apps/desktop/test/session-model-write-order.test.mjs
new file mode 100644
index 000000000..43d9a7435
--- /dev/null
+++ b/apps/desktop/test/session-model-write-order.test.mjs
@@ -0,0 +1,36 @@
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
+const { createSessionSlice } = await import("../src/stores/slices/session-slice.ts");
+const { createSessionCoordination } = await import("../src/stores/runtime/session-coordination.ts");
+const { createSessionRuntime } = await import("../src/stores/runtime/session-runtime.ts");
+const { api } = await import("../src/lib/api.ts");
+
+for (const rejectFirst of [false, true]) test(`rapid idle model changes stay ordered (first write fails: ${rejectFirst})`, async t => {
+ let state = { activeSessionId: "session", sessions: [{ id: "session", mode: "agent", modelId: "original" }], runningSessions: {}, pendingPlans: {}, sessionMeta: {}, planningStates: {}, retainedTranscripts: {} };
+ const access = { get: () => state, set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; } };
+ const runtime = createSessionRuntime(access);
+ const coordination = createSessionCoordination({ ...access, runtime });
+ const slice = createSessionSlice({ ...access, runtime, ...coordination });
+ const writes = [];
+ const resolvers = [];
+ t.mock.method(api, "configureSession", (id, config) => {
+ writes.push(config);
+ return new Promise((resolve, reject) => resolvers.push(() => {
+ if (writes[0] === config && rejectFirst) reject(new Error("configuration unavailable"));
+ else resolve({ session: { id, ...config } });
+ }));
+ });
+ const first = slice.configureActiveSession({ mode: "agent", modelId: "first", thinkingLevel: "off" });
+ const second = slice.configureActiveSession({ mode: "agent", modelId: "second", thinkingLevel: "off" });
+ const outcome = rejectFirst ? assert.rejects(first, /configuration unavailable/) : first;
+ assert.equal(writes.length, 1, "one authoritative configuration write may be in flight");
+ resolvers[0]();
+ await new Promise(resolve => setImmediate(resolve));
+ assert.equal(writes.length, 2);
+ assert.equal(writes[1].modelId, "second");
+ resolvers[1]();
+ await Promise.all([outcome, second]);
+ assert.equal(state.sessions[0].modelId, "second");
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 55ee6f22e..66f9e319f 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1,5 +1,9 @@
# 08. Component Spec
+Idle session model writes participate in the same per-session configuration
+queue as running-turn changes. Rapid choices serialize; the newest queued
+choice is applied after the in-flight write, rather than racing older replies.
+
Live delegation reconstruction crosses system notices as well as steering
messages. An ordinary user prompt remains the run boundary. A running Task
behind a system notice must not become aborted on navigation (Refs #1990).
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 8d3fd8660..8bfb4039a 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1,5 +1,12 @@
# 04. E2E Test Plan
+### E2E-MODEL-idle-write-order
+
+Hold the first idle configuration response, select a second model, then release
+both responses. Assert a single in-flight write and the final newest model.
+Coverage: `session-model-write-order.test.mjs`, using real slice/coordination
+with only Host API controlled. Running-turn configuration remains queued.
+
### E2E-SUBAGENT-system-notice-reconstruction
Reconstruct a transcript with a running Task followed by a system notice.
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index 69011ffcb..ff612f26d 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1,5 +1,8 @@
# 08. 组件规格
+空闲会话的模型写入使用与运行中配置相同的会话队列,快速选择顺序写入,
+最新排队选择在当前写入完成后生效,避免旧响应覆盖新模型。
+
运行中子代理状态重建跨过系统通知及 steering 消息,普通用户提示仍作为回合边界。
系统通知之前仍在运行的 Task 不会因导航而显示已停止(Refs #1990)。
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index a1e9b5c5d..411f1d815 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -1,5 +1,11 @@
# 04. E2E 测试 Plan
+### E2E-MODEL-idle-write-order
+
+保持空闲配置的首个响应,选择第二个模型再顺序释放响应,验证只有一个写入
+进行中且最终保留最新模型。`session-model-write-order.test.mjs` 使用真实 slice
+与协调器,仅控制 Host API 边界。运行中配置继续排队。
+
### E2E-SUBAGENT-system-notice-reconstruction
重建运行中的 Task 后接系统通知的会话,生产条目构建、运行状态选择及结果