Skip to content

Commit b2f589c

Browse files
shivamhwpclaude
andcommitted
fix(client-runtime): a change whose send never left the device stays cancellable
A not-connected failure on this path is raised before the request is sent, so the sent mark made in that pass is taken off and Undo can cancel the change on the device again. The mark no longer wakes replay. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 80d5595 commit b2f589c

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

‎packages/client-runtime/src/state/threadCommands.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,25 @@ describe("settle and snooze while the environment is offline", () => {
766766
}),
767767
);
768768

769+
it.effect("keeps a change cancellable when its send never left the device", () =>
770+
Effect.gen(function* () {
771+
const unmarked = yield* Queue.unbounded<void>();
772+
const storage: { value: string | null; onModify?: () => void } = { value: null };
773+
const h = yield* queued("settle", storage);
774+
let calls = 0;
775+
// Replay reads the queue, marks the change, and takes the mark off again.
776+
storage.onModify = () => {
777+
if (++calls === 3) Queue.offerUnsafe(unmarked, undefined);
778+
};
779+
// The shell reports synchronized from a stale stream while the session is already gone.
780+
h.registry.set(h.statusAtom(ENVIRONMENT_ID), "live");
781+
yield* Queue.take(unmarked);
782+
const result = yield* Effect.promise(() => h.commands.unsettle.run(h.registry, undo));
783+
expect(result._tag === "Success" && ThreadUndo.cancelledPendingSync(result.value)).toBe(true);
784+
expect(stored(storage)).toEqual([]);
785+
}),
786+
);
787+
769788
it.effect("sends a change another tab queued after this tab loaded", () =>
770789
Effect.gen(function* () {
771790
const storage = { value: null as string | null };

‎packages/client-runtime/src/state/threadTriageOutbox.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ const without = (entries: PendingThreadTriages, commandId: CommandId) =>
6767
? entries.filter((entry) => entry.commandId !== commandId)
6868
: entries;
6969

70+
/** The sent mark is left out: it is only ever read from storage. */
7071
const sameEntries = (a: PendingThreadTriages, b: PendingThreadTriages) =>
71-
a.length === b.length &&
72-
a.every(
73-
(entry, index) =>
74-
entry.commandId === b[index]!.commandId && entry.attempted === b[index]!.attempted,
75-
);
72+
a.length === b.length && a.every((entry, index) => entry.commandId === b[index]!.commandId);
73+
74+
/** Raised before a request is sent, when the environment has no session. */
75+
const isOffline = (cause: Cause.Cause<unknown>) =>
76+
cause.reasons.some((reason) => Cause.isFailReason(reason) && isUnavailable(reason.error));
7677

7778
/**
7879
* Settle and snooze for an environment that is not connected: the change is stored on the
@@ -229,7 +230,16 @@ export function createThreadTriageOutbox<R, E>(input: {
229230
continue;
230231
}
231232
const result = await input.send(registry, entry);
232-
if (lifetime.ended || (!AsyncResult.isSuccess(result) && isTransient(result.cause))) return;
233+
if (lifetime.ended) return;
234+
if (!AsyncResult.isSuccess(result) && isTransient(result.cause)) {
235+
// It never left the device, so the mark this pass made comes off and Undo can cancel it.
236+
if (isOffline(result.cause) && entry.attempted !== true) {
237+
await write(registry, (current) =>
238+
current.map((item) => (item.commandId === entry.commandId ? entry : item)),
239+
);
240+
}
241+
return;
242+
}
233243
await forget(registry, entry);
234244
}
235245
};
@@ -287,10 +297,7 @@ export function createThreadTriageOutbox<R, E>(input: {
287297
if (previous !== undefined) await forget(registry, previous);
288298
return result;
289299
}
290-
const offline = result.cause.reasons.some(
291-
(reason) => Cause.isFailReason(reason) && isUnavailable(reason.error),
292-
);
293-
if (!offline || snapshot == null || thread === undefined) return result;
300+
if (!isOffline(result.cause) || snapshot == null || thread === undefined) return result;
294301
const action = toAction(target.input);
295302
const queuedAt = DateTime.nowUnsafe();
296303
if (input.apply(thread, action, queuedAt) === thread) return result;

0 commit comments

Comments
 (0)