Skip to content

Commit d85f586

Browse files
t3dotggclaude
andcommitted
fix(desktop): hold a stopped thread until its detail is live
The keep-alive released a stopped thread as soon as its detail did not show a running session. A detail that had not loaded or caught up yet also looked that way, so its stream closed before it applied the stop. Now a stopped thread stays mounted until its own detail is live and settled. A deleted or failed stream still releases at once. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d8079be commit d85f586

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

‎apps/web/src/state/threads.test.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function shell(id: string, status: OrchestrationSessionStatus | null) {
4343
function detail(
4444
id: string,
4545
status: OrchestrationSessionStatus,
46-
streamStatus: EnvironmentThreadState["status"] = "live",
46+
overrides: Partial<EnvironmentThreadState> = {},
4747
) {
4848
const threadId = ThreadId.make(id);
4949
const thread: OrchestrationThread = {
@@ -69,10 +69,11 @@ function detail(
6969
checkpoints: [],
7070
session: session(threadId, status),
7171
};
72-
return AsyncResult.success({
72+
return AsyncResult.success<EnvironmentThreadState>({
7373
...EMPTY_ENVIRONMENT_THREAD_STATE,
74-
status: streamStatus,
74+
status: "live",
7575
data: Option.some(thread),
76+
...overrides,
7677
});
7778
}
7879

@@ -150,26 +151,33 @@ describe("createRunningThreadKeepAliveAtom", () => {
150151
expect(h.registry.get(h.stateAtom(LOCAL, "a"))).toBe(live);
151152
});
152153

153-
it("holds a stopped thread until its own connected stream shows the stop", () => {
154+
it("holds a stopped thread until its own stream is live and shows the stop", () => {
154155
const h = makeHarness();
155156
h.registry.set(h.threads(LOCAL), [
156157
shell("a", "running"),
157158
shell("b", "running"),
158159
shell("c", "running"),
159160
]);
161+
// "b" has not loaded yet. "c" hit a stream error.
160162
h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "running"));
161-
h.registry.set(h.stateAtom(LOCAL, "c"), detail("c", "running", "cached"));
163+
h.registry.set(
164+
h.stateAtom(LOCAL, "c"),
165+
detail("c", "running", { status: "cached", error: Option.some("Could not sync.") }),
166+
);
162167

163-
// The shell reports the stops first. Only the connected stream that
164-
// still shows a running session is held open.
168+
// The shell reports the stops first. A failed stream cannot deliver its
169+
// stop, so only it is released now.
165170
h.registry.set(h.threads(LOCAL), [
166171
shell("a", "ready"),
167172
shell("b", "ready"),
168173
shell("c", "ready"),
169174
]);
170-
expect(h.openStreams()).toEqual(["local:a"]);
175+
expect(h.openStreams()).toEqual(["local:a", "local:b"]);
171176

172177
h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "ready"));
178+
h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready", { status: "synchronizing" }));
179+
expect(h.openStreams()).toEqual(["local:b"]);
180+
h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready"));
173181
expect(h.openStreams()).toEqual([]);
174182
});
175183

‎apps/web/src/state/threads.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ export function useEnvironmentThread(
5252

5353
type KeptThreads = ReadonlyMap<EnvironmentId, ReadonlySet<ThreadId>>;
5454

55-
// True while the thread's own stream is up and still shows a running session.
56-
// A failed or disconnected stream cannot deliver the stop, so it does not wait.
57-
function isDetailStillRunning<E>(
58-
result: AsyncResult.AsyncResult<EnvironmentThreadState, E>,
59-
): boolean {
60-
if (!AsyncResult.isSuccess(result)) return false;
61-
const { status, data } = result.value;
55+
// True once a thread's own stream no longer needs to stay open: it is in sync
56+
// and shows a settled session, or it cannot progress (deleted or failed). A
57+
// stream that is still loading or reconnecting keeps waiting for the stop.
58+
function isDetailDone<E>(result: AsyncResult.AsyncResult<EnvironmentThreadState, E>): boolean {
59+
if (!AsyncResult.isSuccess(result)) return true;
60+
const { status, data, error } = result.value;
61+
if (status === "deleted" || Option.isSome(error)) return true;
6262
return (
63-
(status === "live" || status === "synchronizing") &&
64-
Option.exists(data, (thread) => isThreadSessionRunning(thread.session))
63+
status === "live" && !Option.exists(data, (thread) => isThreadSessionRunning(thread.session))
6564
);
6665
}
6766

@@ -70,8 +69,9 @@ function isDetailStillRunning<E>(
7069
* environments. Mount the result; its value is only bookkeeping.
7170
*
7271
* The shell and detail streams are independent, so the shell can report a
73-
* stop first. A stopped thread stays mounted until its own detail shows the
74-
* stop too. Then the stream closes and saves the settled state to disk.
72+
* stop before the detail loads or catches up. A stopped thread stays mounted
73+
* until its own detail is live and shows the stop too. Then the stream closes
74+
* and saves the settled state to disk.
7575
*/
7676
export function createRunningThreadKeepAliveAtom<E>(input: {
7777
readonly environmentIdsAtom: Atom.Atom<ReadonlyArray<EnvironmentId>>;
@@ -108,11 +108,11 @@ export function createRunningThreadKeepAliveAtom<E>(input: {
108108
const stateAtom = input.stateAtom(environmentId, threadId);
109109
// `once`, not `get`: a dependency on a stopped thread would hold its
110110
// stream open until some other change rebuilds this atom.
111-
if (!isDetailStillRunning(get.once(stateAtom))) continue;
111+
if (isDetailDone(get.once(stateAtom))) continue;
112112
threadIds.add(threadId);
113-
// Rebuild when this detail shows the stop, not on each update.
113+
// Rebuild when this detail is done, not on each update.
114114
get.subscribe(stateAtom, (state) => {
115-
if (!isDetailStillRunning(state)) get.refreshSelf();
115+
if (isDetailDone(state)) get.refreshSelf();
116116
});
117117
}
118118
for (const threadId of threadIds) get.mount(input.stateAtom(environmentId, threadId));

‎docs/internals/connection-runtime.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ The desktop app adds one consumer: a
6565
[keep-alive](../../apps/web/src/state/threads.ts) mounts every thread whose
6666
session is starting or running, in each enabled environment. Opening a running
6767
thread then needs no replay. The shell and detail streams are independent, so
68-
the shell can report a stop first. A stopped thread stays mounted until its own
69-
stream shows the stop, and the stream then closes and saves the settled state.
68+
the shell can report a stop before the detail loads or catches up. A stopped
69+
thread stays mounted until its own stream is live and shows the stop, and the
70+
stream then closes and saves the settled state.
7071
Web and mobile do not keep threads alive.
7172

7273
Retain state and cursor together only after an update finishes. Cancellation must

0 commit comments

Comments
 (0)