Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(sessions): exclude automation sessions from empty-slot reuse (#1695)
Luis Araya0d235a91a771
diff --git a/apps/desktop/src/lib/remote-project-session.ts b/apps/desktop/src/lib/remote-project-session.tsindex 9afcd6ef1..27dd63bb1 100644--- a/apps/desktop/src/lib/remote-project-session.ts+++ b/apps/desktop/src/lib/remote-project-session.ts@@ -2,6 +2,7 @@ import type { SessionSummary } from "@pi-desktop/shared";import { api } from "./api";import { isRemoteSession, safeSessionSummary } from "./remote-session-safety";import { sessionIsReusableEmpty } from "./session-create";+import { isAutomationSession } from "./session-origin";import { sessionIsArchived } from "./sidebar-preferences";import type { SessionRuntime } from "../stores/runtime/session-runtime";import type { StoreAccess } from "../stores/slices/types";@@ -26,7 +27,8 @@ export async function openRemoteProjectSession(const request = (async () => {const state = get();const latest = state.sessions.filter((session) =>- session.remoteHostKey === project.hostKey && session.remoteProjectId === project.projectId+ !isAutomationSession(session)+ && session.remoteHostKey === project.hostKey && session.remoteProjectId === project.projectId&& !sessionIsArchived(session.id, state.sessionMeta),).sort((left, right) => right.updatedAt.localeCompare(left.updatedAt))[0];if (latest && sessionIsReusableEmpty(latest, {diff --git a/apps/desktop/src/lib/session-create.ts b/apps/desktop/src/lib/session-create.tsindex d20238015..b446f2ba6 100644--- a/apps/desktop/src/lib/session-create.ts+++ b/apps/desktop/src/lib/session-create.ts@@ -1,3 +1,5 @@+import { isAutomationSession } from "./session-origin";+/*** New Task empty-slot reuse (ADR 0113) and first-frame empty reveal (ADR 0154).*@@ -14,7 +16,7 @@ export const EMPTY_SESSION_WINDOW = {} as const;export function sessionIsReusableEmpty(- session: { messageCount: number; worktree?: unknown },+ session: { messageCount: number; worktree?: unknown; scheduledRun?: boolean },options: {running?: boolean;liveMessageCount?: number;@@ -26,6 +28,9 @@ export function sessionIsReusableEmpty(// An isolated request always gets its own worktree, and an empty isolated// session is never handed out for an ordinary request (ADR 0329).if (options.isolated === true || session.worktree) return false;+ // Automation transcripts (scheduled runs) are entered only from their+ // task view and must never be reused as an interactive empty slot (#1684).+ if (isAutomationSession(session)) return false;if (session.messageCount > 0) return false;if (options.running === true) return false;if ((options.liveMessageCount ?? 0) > 0) return false;diff --git a/apps/desktop/src/stores/runtime/session-runtime.ts b/apps/desktop/src/stores/runtime/session-runtime.tsindex afa07661a..26dbb620e 100644--- a/apps/desktop/src/stores/runtime/session-runtime.ts+++ b/apps/desktop/src/stores/runtime/session-runtime.ts@@ -16,6 +16,7 @@ import {} from "../../lib/session-transcript";import { getSessionMessageSnapshot, getSessionToolMessagePositions } from "../../lib/session-transcript-updates";import { sessionReadLooksEmpty } from "../../lib/session-transcript-read";+import { isAutomationSession } from "../../lib/session-origin";import { sessionIsArchived, type SessionMeta } from "../../lib/sidebar-preferences";import {normalizeProjectPath,@@ -405,7 +406,11 @@ export function createSessionRuntime({ get, set }: StoreAccess): SessionRuntimenormalizeProjectPath(projectPath) ?? "<temporary>",latestSessionInScope: (sessions, projectPath, sessionMeta) =>sessions- .filter((session) => sessionMatchesProject(session, projectPath))+ .filter(+ (session) =>+ !isAutomationSession(session) &&+ sessionMatchesProject(session, projectPath),+ ).sort((a, b) => {const aUpdated = Date.parse(a.updatedAt);const bUpdated = Date.parse(b.updatedAt);diff --git a/apps/desktop/test/session-create.test.mjs b/apps/desktop/test/session-create.test.mjsindex e645da7a1..80abcb2a3 100644--- a/apps/desktop/test/session-create.test.mjs+++ b/apps/desktop/test/session-create.test.mjs@@ -1,11 +1,16 @@import { readStoreModule } from "./helpers/source-contracts.mjs";import assert from "node:assert/strict";import { readFile } from "node:fs/promises";+import { register } from "node:module";import test from "node:test";-import {++register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));++const {EMPTY_SESSION_WINDOW,sessionIsReusableEmpty,-} from "../src/lib/session-create.ts";+} = await import("../src/lib/session-create.ts");+const { createSessionRuntime } = await import("../src/stores/runtime/session-runtime.ts");const sessionSlice = await readStoreModule("slices/session-slice.ts");const sessionCoordination = await readStoreModule("runtime/session-coordination.ts");@@ -37,6 +42,74 @@ test("isolated sessions are never reused for a new task (ADR 0329)", () => {assert.equal(sessionIsReusableEmpty(empty, { isolated: false }), true);});+test("automation and scheduled sessions are never reused for a new task (#1684)", () => {+ const empty = { messageCount: 0 };+ const emptyScheduled = { messageCount: 0, scheduledRun: true };+ assert.equal(+ sessionIsReusableEmpty(emptyScheduled),+ false,+ "an empty scheduled run session must never be reused for a new task",+ );+ assert.equal(+ sessionIsReusableEmpty({ messageCount: 0, scheduledRun: false }),+ true,+ "an ordinary session with scheduledRun: false is reusable when empty",+ );+ assert.equal(sessionIsReusableEmpty(empty), true);+});++test("latestSessionInScope excludes automation sessions so empty scheduled runs are not reused (#1684)", () => {+ const runtime = createSessionRuntime({ get: () => ({}), set: () => {} });+ const scheduledEmpty = {+ id: "session-scheduled",+ projectPath: "/workspace/project",+ messageCount: 0,+ scheduledRun: true,+ updatedAt: "2026-10-15T12:00:00Z",+ };+ const userEmpty = {+ id: "session-user-empty",+ projectPath: "/workspace/project",+ messageCount: 0,+ scheduledRun: false,+ updatedAt: "2026-10-15T11:00:00Z",+ };+ const userNonEmpty = {+ id: "session-user-nonempty",+ projectPath: "/workspace/project",+ messageCount: 3,+ scheduledRun: false,+ updatedAt: "2026-10-15T10:00:00Z",+ };++ // Case 1: The most recent session is an empty scheduled run, but an older empty user session exists.+ // The user empty session must be selected as the latest in scope.+ const candidate1 = runtime.latestSessionInScope(+ [scheduledEmpty, userEmpty, userNonEmpty],+ "/workspace/project",+ {},+ );+ assert.equal(candidate1?.id, "session-user-empty");++ // Case 2: The only session in scope is an empty scheduled run.+ // latestSessionInScope must return undefined so a new session is created.+ const candidate2 = runtime.latestSessionInScope(+ [scheduledEmpty],+ "/workspace/project",+ {},+ );+ assert.equal(candidate2, undefined);++ // Case 3: The most recent session is an empty scheduled run, followed by a non-empty user session.+ // latestSessionInScope must return the user session (which is not empty, causing newSession to create a fresh one).+ const candidate3 = runtime.latestSessionInScope(+ [scheduledEmpty, userNonEmpty],+ "/workspace/project",+ {},+ );+ assert.equal(candidate3?.id, "session-user-nonempty");+});+test("empty sessions have a whole, already-known transcript window", () => {assert.deepEqual(EMPTY_SESSION_WINDOW, {messageStart: 0,