feat(threads): add Pause session to park provider sessions and save RAM #12172
ClosedOmarAlaaeldein(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/12172/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -147,3 +147,6 @@147147 if (thread.session?.status === "error") {148148 return "failed";149149 }150+ // Stopped = user-paused to save RAM (resume is the next message). Muted151+ // presence, not an alert: mirrors the web sidebar's Paused pill.152+ if (thread.session != null && thread.session.status === "stopped") {@@ -3110,3 +3127,21 @@31103127 },31113128 [unsnoozeThread],31123129 );3130+ const attemptPauseSession = useCallback(3131+ (threadRef: ScopedThreadRef) => {3132+ void (async () => {3133+ const result = await pauseThreadSession(threadRef);3134+ if (result._tag === "Failure") {3135+ if (!isAtomCommandInterrupted(result)) {3136+ const error = squashAtomCommandFailure(result);3137+ toastManager.add(3138+ stackedThreadToast({3139+ type: "error",3140+ title: "Failed to pause session",3141+ description: error instanceof Error ? error.message : "An error occurred.",3142+ }),3143+ );3144+ }3145+ return;3146+ }3147+ // No navigation: the thread stays where it is, only its provider@@ -311,0 +315,21 @@315+ * `thread.session.stop` command: the provider subprocess is torn down while316+ * the thread stays active, and the next turn recreates the session317+ * (`ensureSessionForThread` treats `stopped` as absent). Resume is therefore318+ * "send a message", not a second command — no contract change needed.319+ *320+ * A paused session is `session != null && status === "stopped"`. A null321+ * session (never started) is not "paused", it is just idle.322+ */323+export function isSessionPaused(shell: Pick<OrchestrationThreadShell, "session">): boolean {324+ return shell.session != null && shell.session.status === "stopped";325+}326+327+/**328+ * Pause mirrors the archive guard: never yank a thread mid-turn. Interrupt329+ * first, then pause. Idle sessions (ready / error / no active turn) are330+ * pausable; already-paused or never-started threads are not.331+ */332+export function canPauseSession(shell: Pick<OrchestrationThreadShell, "session">): boolean {333+ const session = shell.session;334+ if (session == null || session.status === "stopped") return false;335+ if (session.status === "running" && session.activeTurnId != null) return false;
- @@ -573,3 +581,10 @@573581 ],574582 [props.titleRegenerationSupported, thread.titleRegeneration],575583 );584+ // Pause parks the provider session (saves RAM) while the thread stays585+ // active; resume is the next message. Offered only for pausable cards —586+ // already-paused and never-started threads show no item.587+ const pauseMenuItems = useMemo<MenuAction[]>(588+ () =>589+ canPauseSession(thread) && !snoozedRow && !canUnsettle590+ ? [{ id: "pause", title: "Pause session", image: "pause" }]@@ -311,0 +315,21 @@315+ * `thread.session.stop` command: the provider subprocess is torn down while316+ * the thread stays active, and the next turn recreates the session317+ * (`ensureSessionForThread` treats `stopped` as absent). Resume is therefore318+ * "send a message", not a second command — no contract change needed.319+ *320+ * A paused session is `session != null && status === "stopped"`. A null321+ * session (never started) is not "paused", it is just idle.322+ */323+export function isSessionPaused(shell: Pick<OrchestrationThreadShell, "session">): boolean {324+ return shell.session != null && shell.session.status === "stopped";325+}326+327+/**328+ * Pause mirrors the archive guard: never yank a thread mid-turn. Interrupt329+ * first, then pause. Idle sessions (ready / error / no active turn) are330+ * pausable; already-paused or never-started threads are not.331+ */332+export function canPauseSession(shell: Pick<OrchestrationThreadShell, "session">): boolean {333+ const session = shell.session;334+ if (session == null || session.status === "stopped") return false;335+ if (session.status === "running" && session.activeTurnId != null) return false;
- packages/client-runtime/src/state/threadSettled.ts line 327Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -309,3 +309,19 @@309309 export function localSnoozeTime(date: Date): string {310310 return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;311311 }312+313+/**314+ * Manual pause ("park the provider to save RAM") rides on the existing315+ * `thread.session.stop` command: the provider subprocess is torn down while316+ * the thread stays active, and the next turn recreates the session317+ * (`ensureSessionForThread` treats `stopped` as absent). Resume is therefore318+ * "send a message", not a second command — no contract change needed.319+ *320+ * A paused session is a cleanly stopped one: `session != null`, status321+ * `"stopped"`, no `lastError`. A null session (never started) is not322+ * "paused", it is just idle — and a stopped session carrying a failure323+ * still reads as failed, never as the calm Paused state.324+ */325+export function isSessionPaused(shell: Pick<OrchestrationThreadShell, "session">): boolean {326+ return (327+ shell.session != null && shell.session.status === "stopped" && shell.session.lastError == null
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed