Skip to content

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

fix(scheduled): recover editor options after Host invalidation

Linuxdazhao7d5abca832a6
diff --git a/apps/desktop/src/features/scheduled/use-scheduled-workspace.ts b/apps/desktop/src/features/scheduled/use-scheduled-workspace.ts
index 2a52e6d1a..c590df8b1 100644
--- a/apps/desktop/src/features/scheduled/use-scheduled-workspace.ts
+++ b/apps/desktop/src/features/scheduled/use-scheduled-workspace.ts
@@ -79,6 +79,7 @@ export function useScheduledWorkspace(
const revision = useRef(0);
const runRevision = useRef(0);
const projectRevision = useRef(0);
+ const pendingProjectGeneration = useRef<number | null>(null);
const selectedTaskIdRef = useRef<string | null>(null);
const busyRef = useRef(false);
const onErrorRef = useRef(onError);
@@ -138,6 +139,7 @@ export function useScheduledWorkspace(
const loadProjects = useCallback(async () => {
const request = ++projectRevision.current;
const generation = cache.generation;
+ pendingProjectGeneration.current = generation;
setProjectsLoaded(false);
setProjectsError("");
try {
@@ -149,6 +151,10 @@ export function useScheduledWorkspace(
if (mounted.current && request === projectRevision.current && generation === cache.generation) {
setProjectsError(failure instanceof Error ? failure.message : String(failure));
}
+ } finally {
+ if (request === projectRevision.current && generation === cache.generation) {
+ pendingProjectGeneration.current = null;
+ }
}
}, []);
@@ -159,6 +165,10 @@ export function useScheduledWorkspace(
void refresh();
const current = selectedTaskIdRef.current;
if (current) void readTaskRuns(current);
+ // Replace an invalidated editor read even if the old Host never settles it.
+ if (pendingProjectGeneration.current !== null && pendingProjectGeneration.current !== cache.generation) {
+ void loadProjects();
+ }
}, REFRESH_INTERVAL_MS);
return () => {
mounted.current = false;
@@ -167,7 +177,7 @@ export function useScheduledWorkspace(
projectRevision.current++;
clearInterval(timer);
};
- }, [refresh, readTaskRuns]);
+ }, [refresh, readTaskRuns, loadProjects]);
// Keep one task selected: the first one, or the previous selection while it
// still exists. A deleted task never leaves the pane pointing at nothing, and
diff --git a/apps/desktop/test/scheduled-workspace-loading.test.mjs b/apps/desktop/test/scheduled-workspace-loading.test.mjs
index 0f223ccf1..4e1fb8df8 100644
--- a/apps/desktop/test/scheduled-workspace-loading.test.mjs
+++ b/apps/desktop/test/scheduled-workspace-loading.test.mjs
@@ -163,6 +163,54 @@ test("invalidated lifecycle rejects late reads and does not repopulate the next
assert.deepEqual(next.output.tasks, []);
});
+for (const oldOutcome of ["resolved", "rejected", "pending"]) {
+ test(`mounted editor recovers after Host invalidation with an old ${oldOutcome} project read`, async t => {
+ const old = deferred(), replacement = deferred(); let projectReads = 0;
+ const h = harness(api({ listProjects: () => ++projectReads === 1 ? old.promise : replacement.promise }));
+ t.after(() => h.close()); await h.settle();
+ void h.output.loadProjects(); await h.settle();
+ cache.clear();
+ if (oldOutcome === "resolved") old.resolve({ projects: [{ id: 1, path: "/old-host" }] });
+ if (oldOutcome === "rejected") old.reject(new Error("Old Host unavailable"));
+ await h.settle(); h.tick(); await h.settle();
+ assert.equal(projectReads, 2, "normal polling replaces the invalidated editor read without reopening");
+ assert.equal(h.output.projectsLoaded, false);
+ assert.equal(h.output.projectsError, "");
+ const projects = [{ id: 2, path: "/new-host" }];
+ replacement.resolve({ projects }); await h.settle();
+ assert.equal(h.output.projectsLoaded, true);
+ assert.deepEqual(h.output.projects, projects);
+ if (oldOutcome === "pending") {
+ old.resolve({ projects: [{ id: 1, path: "/old-host" }] }); await h.settle();
+ assert.deepEqual(h.output.projects, projects, "a late old response cannot replace recovered options");
+ }
+ h.tick(); await h.settle();
+ assert.equal(projectReads, 2, "successful options do not become an extra project query on every poll");
+ });
+}
+
+test("a failed replacement project read exposes retry and recovers in the same editor", async t => {
+ const old = deferred(); let projectReads = 0;
+ const projects = [{ id: 3, path: "/recovered-host" }];
+ const h = harness(api({ listProjects: () => {
+ projectReads++;
+ if (projectReads === 1) return old.promise;
+ if (projectReads === 2) return Promise.reject(new Error("Synthetic Host unavailable"));
+ return Promise.resolve({ projects });
+ } }));
+ t.after(() => h.close()); await h.settle();
+ void h.output.loadProjects(); await h.settle(); cache.clear();
+ h.tick(); await h.settle();
+ assert.match(h.output.projectsError, /Synthetic Host unavailable/);
+ assert.equal(h.output.projectsLoaded, false);
+ await h.output.loadProjects(); await h.settle();
+ assert.equal(h.output.projectsLoaded, true);
+ assert.equal(h.output.projectsError, "");
+ assert.deepEqual(h.output.projects, projects);
+ old.reject(new Error("Old Host closed")); await h.settle();
+ assert.equal(h.output.projectsError, "");
+});
+
test("conversation return takes precedence over the cached task during initial loading", async t => {
returns.rememberScheduledReturn({ taskId: "b", taskTitle: "Synthetic B", runId: "restored-b", sessionId: "synthetic-session" });
const held = deferred();
diff --git a/docs/spec/04-ux/01-ui-ia.md b/docs/spec/04-ux/01-ui-ia.md
index daccdd93e..e075a9356 100644
--- a/docs/spec/04-ux/01-ui-ia.md
+++ b/docs/spec/04-ux/01-ui-ia.md
@@ -313,7 +313,10 @@ the renderer's last successful in-memory snapshot while refreshing. Host loss
or restart invalidates that snapshot and rejects late responses from the old
lifecycle. Project options load only when opening Create/Edit; the form waits
for that read to succeed and exposes retry on failure. Run now and Delete wait
-for known run status; Host admission remains authoritative.
+for known run status; Host admission remains authoritative. If Host loss/restart
+invalidates pending editor options, the next normal page poll replaces that
+read without closing the editor or waiting for the old request to settle.
+A replacement failure exposes the existing Retry action.
Settings → Behavior → Execution → Scheduled tasks controls this feature, on by default
(`scheduledTasksEnabled` absent or true). Off hides the footer and search page
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 3895e53fe..65cb9b8cc 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -20,6 +20,13 @@
`scheduled-workspace.test.mjs` checks rendered loading/error states.
These are renderer regression checks, not a native Electron E2E run.
+Also invalidate the Host while Create/Edit options are pending, with the old
+request resolving, rejecting or remaining pending. The next normal poll must
+replace that read and recover the same editor without Cancel/reopen. A failed
+replacement exposes Retry; retry success mounts the editor. Old responses must
+not overwrite recovered options. Successful options remain lazy rather than
+being fetched on every subsequent poll.
+
## E2E-PROVIDER-DASHSCOPE-terminal-parameter-error
- **Preconditions:** Isolated Agent runtime and a synthetic OpenAI-compatible
diff --git a/docs/zh-CN/spec/04-ux/01-ui-ia.md b/docs/zh-CN/spec/04-ux/01-ui-ia.md
index 5c55aae79..5c10083bd 100644
--- a/docs/zh-CN/spec/04-ux/01-ui-ia.md
+++ b/docs/zh-CN/spec/04-ux/01-ui-ia.md
@@ -227,7 +227,10 @@ the renderer's last successful in-memory snapshot while refreshing. Host loss
or restart invalidates that snapshot and rejects late responses from the old
lifecycle. Project options load only when opening Create/Edit; the form waits
for that read to succeed and exposes retry on failure. Run now and Delete wait
-for known run status; Host admission remains authoritative.
+for known run status; Host admission remains authoritative. If Host loss/restart
+invalidates pending editor options, the next normal page poll replaces that
+read without closing the editor or waiting for the old request to settle.
+A replacement failure exposes the existing Retry action.
任务列与所选任务的页面并排展示,支持创建、编辑、暂停、启用和确认删除。表单可为每个任务单独选择项目、权限、模型与会话模式(每次新开会话或复用同一会话)。周期保留下拉选择,并与时分统一为自定义主题菜单。每小时按一小时间隔执行,不显示时间选择;保存、启用、启动或上次自动准入后重新计时。
间隔直接给出跨度:数量加分钟或小时单位(5 分钟至 24 小时),从准入时刻起算。
每天通过下拉框选择上午 09:00、下午 14:00、晚上 19:00、夜间 22:00;
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 1b3b7cbb9..eeb95cae6 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
@@ -20,6 +20,13 @@
`scheduled-workspace.test.mjs` checks rendered loading/error states.
These are renderer regression checks, not a native Electron E2E run.
+Also invalidate the Host while Create/Edit options are pending, with the old
+request resolving, rejecting or remaining pending. The next normal poll must
+replace that read and recover the same editor without Cancel/reopen. A failed
+replacement exposes Retry; retry success mounts the editor. Old responses must
+not overwrite recovered options. Successful options remain lazy rather than
+being fetched on every subsequent poll.
+
> **翻译说明:** 本页是与 [英文源规格](/spec/06-delivery/04-e2e-test-plan) 一一对应的机器辅助翻译。代码、协议字段和标识符保持原文;如翻译与英文源事实有歧义,以英文版本为准。