Skip to content

Commit aa82609

Browse files
shivamhwpclaude
andcommitted
fix(client-runtime): leave a change unmarked when the server cannot replay it
Replay now asks the connected server for the version guard before marking anything as sent. A server without it refuses before dispatch, so the change stays unmarked and an offline Undo can still cancel it on the device. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 881b7df commit aa82609

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎packages/client-runtime/src/operations/commands.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export class ThreadVersionGuardUnsupportedError extends Schema.TaggedError<Threa
294294
) {}
295295

296296
// Older servers drop snapshotSequence, so a replay there could overwrite newer changes.
297-
const requireThreadVersionGuard = Effect.fn("EnvironmentCommands.requireThreadVersionGuard")(
297+
export const requireThreadVersionGuard = Effect.fn("EnvironmentCommands.requireThreadVersionGuard")(
298298
function* () {
299299
const config = yield* getInitialServerConfig();
300300
if (config.environment.capabilities.threadTriageVersionGuard === true) return;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,16 @@ describe("settle and snooze while the environment is offline", () => {
999999
yield* Queue.take(h.configReads);
10001000
expect(yield* Queue.size(h.requests)).toBe(0);
10011001
expect(h.registry.get(pending).has(THREAD_ID)).toBe(true);
1002-
expect(stored(h.storage)).toHaveLength(1);
1002+
// Never sent, so it stays unmarked and Undo still cancels it on the device.
1003+
expect(stored(h.storage)[0]).not.toHaveProperty("attempted");
1004+
yield* h.disconnect;
1005+
const undo = yield* Effect.promise(() =>
1006+
h.commands.unsettle.run(h.registry, {
1007+
environmentId: ENVIRONMENT_ID,
1008+
input: { threadId: THREAD_ID, reason: "user" },
1009+
}),
1010+
);
1011+
expect(undo._tag === "Success" && ThreadUndo.cancelledPendingSync(undo.value)).toBe(true);
10031012
}),
10041013
);
10051014

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
reorderPinnedThread,
5050
reorderActiveThread,
5151
setThreadAutoSettle,
52+
requireThreadVersionGuard,
5253
settleThread,
5354
snoozeThread,
5455
startThreadTurn,
@@ -428,6 +429,10 @@ export function createThreadEnvironmentAtoms<R, E>(
428429
concurrency,
429430
}),
430431
};
432+
const versionGuard = createEnvironmentCommand(runtime, {
433+
label: "environment-data:commands:thread:version-guard",
434+
execute: () => requireThreadVersionGuard(),
435+
});
431436
const triage = createThreadTriageOutbox({
432437
runtime,
433438
sourceSnapshotAtom: snapshotAtom,
@@ -438,6 +443,8 @@ export function createThreadEnvironmentAtoms<R, E>(
438443
applySettle: (thread, queuedAt) => applySettle(thread, queuedAt, false),
439444
applySnooze: (thread, snoozedUntil, queuedAt) =>
440445
applySnooze(thread, snoozedUntil, queuedAt, false),
446+
checkServer: (registry, environmentId) =>
447+
versionGuard.run(registry, { environmentId, input: undefined }),
441448
replay: (registry, { environmentId, threadId, commandId, action, snapshotSequence }) =>
442449
action.type === "settle"
443450
? settle.run(registry, { environmentId, input: { threadId, commandId, snapshotSequence } })

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export function createThreadTriageOutbox<R, E>(input: {
107107
snoozedUntil: string,
108108
queuedAt: DateTime.Utc,
109109
) => OrchestrationV2ThreadShell;
110+
/** Fails before anything is sent when the connected server cannot check the snapshot. */
111+
readonly checkServer: (
112+
registry: AtomRegistry.AtomRegistry,
113+
environmentId: EnvironmentId,
114+
) => Promise<AtomCommandResult<unknown, unknown>>;
110115
/** Sends the queued action with its command id and snapshot sequence. */
111116
readonly replay: (
112117
registry: AtomRegistry.AtomRegistry,
@@ -292,6 +297,10 @@ export function createThreadTriageOutbox<R, E>(input: {
292297
const added = stored.filter((entry) => !entries.some((current) => sameEntry(current, entry)));
293298
return kept.length === entries.length && added.length === 0 ? entries : [...kept, ...added];
294299
});
300+
if (stored.length === 0) return;
301+
// An older server would refuse every entry, which must stay unmarked so Undo can cancel it.
302+
const server = await input.checkServer(registry, environmentId);
303+
if (lifetime.ended || !AsyncResult.isSuccess(server)) return;
295304
for (const entry of stored) {
296305
if (
297306
entry.action.type === "snooze" &&

0 commit comments

Comments
 (0)