Skip to content

Commit cc19795

Browse files
fix(client-runtime): an offline settle no longer undoes a newer pin reorder (#48066)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent bdbc06a commit cc19795

3 files changed

Lines changed: 150 additions & 10 deletions

File tree

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,21 +905,28 @@ describe("settle and snooze while the environment is offline", () => {
905905
}),
906906
);
907907

908-
it.effect("drops a queued settle when the thread changed on the server meanwhile", () =>
908+
it.effect.each([
909+
["new work arrived", {}, { latestRunId: RunId.make("run-2"), itemCount: 3 }],
910+
[
911+
"the pinned thread was reordered",
912+
{ pinnedAt: NOW, pinOrderKey: "g" },
913+
{ pinnedAt: NOW, pinOrderKey: "t" },
914+
],
915+
["the active thread was reordered", { activeOrderKey: "g" }, { activeOrderKey: "t" }],
916+
] as const)("drops a queued settle when %s on the server meanwhile", ([, before, after]) =>
909917
Effect.gen(function* () {
910918
const h = yield* makeHarness({ connected: false });
919+
h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), {
920+
...SNAPSHOT,
921+
threads: [{ ...SNAPSHOT.threads[0]!, ...before }],
922+
});
911923
yield* Effect.exit(h.commands.settle.run(h.registry, target));
924+
expect(yield* queued(h)).toHaveLength(1);
912925
yield* h.reconnect("synchronizing");
913926
const changed = {
914927
...SNAPSHOT,
915928
snapshotSequence: 5,
916-
threads: [
917-
{
918-
...SNAPSHOT.threads[0]!,
919-
latestRunId: RunId.make("run-2"),
920-
itemCount: 3,
921-
},
922-
],
929+
threads: [{ ...SNAPSHOT.threads[0]!, ...after }],
923930
};
924931
h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), changed);
925932
h.registry.set(h.statusAtom(ENVIRONMENT_ID), "live");
@@ -1013,6 +1020,38 @@ describe("settle and snooze while the environment is offline", () => {
10131020
}),
10141021
);
10151022

1023+
it.effect("sends an unchanged settle stored by a build that did not compare order keys", () =>
1024+
Effect.gen(function* () {
1025+
const storage = {
1026+
value: JSON.stringify({
1027+
entries: [
1028+
{
1029+
environmentId: ENVIRONMENT_ID,
1030+
threadId: THREAD_ID,
1031+
commandId: "stored-settle",
1032+
action: { type: "settle" },
1033+
queuedAt: "2026-09-12T10:00:00.000Z",
1034+
seen: '[null,"idle",0,null,null,null,null,null,null,1789207200000]',
1035+
},
1036+
],
1037+
}),
1038+
};
1039+
const h = yield* makeHarness({ connected: false, storage });
1040+
h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), {
1041+
...SNAPSHOT,
1042+
threads: [
1043+
{ ...SNAPSHOT.threads[0]!, pinnedAt: NOW, pinOrderKey: "g", activeOrderKey: "g" },
1044+
],
1045+
});
1046+
yield* waitFor(h.registry, h.pendingSyncAtom, (threads) => threads.has(THREAD_ID));
1047+
yield* h.reconnect();
1048+
const request = yield* Queue.take(h.requests);
1049+
expect(request.command).toMatchObject({ type: "thread.settle", commandId: "stored-settle" });
1050+
yield* Deferred.succeed(request.reply, { sequence: 2 });
1051+
yield* nothingPending(h);
1052+
}),
1053+
);
1054+
10161055
it.effect("refuses a snooze while a settle is queued, keeping the settle", () =>
10171056
Effect.gen(function* () {
10181057
const h = yield* makeHarness({ connected: false });

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const isTransient = (cause: Cause.Cause<unknown>) =>
4141
* What the user acted on: the thread's work and every triage decision with its time. A rename,
4242
* tag change or visit is left out, so it does not drop the queued change.
4343
*/
44-
function seenState(thread: OrchestrationV2ThreadShell) {
44+
function seenState(thread: OrchestrationV2ThreadShell, withOrder = true) {
4545
return JSON.stringify([
4646
thread.latestRunId,
4747
thread.status,
@@ -55,6 +55,7 @@ function seenState(thread: OrchestrationV2ThreadShell) {
5555
thread.snoozedAt,
5656
thread.pinnedAt,
5757
].map((time) => (time == null ? null : DateTime.toEpochMillis(time))),
58+
...(withOrder ? [[thread.pinOrderKey ?? null, thread.activeOrderKey ?? null]] : []),
5859
]);
5960
}
6061

@@ -152,7 +153,8 @@ export function createThreadTriageOutbox<R, E, SendError, RE>(input: {
152153
const thread = find(family?.value ?? shell());
153154
if (
154155
thread !== undefined &&
155-
seenState(thread) === entry.seen &&
156+
// A change stored before order keys were compared has none, so it matches without them.
157+
(seenState(thread) === entry.seen || seenState(thread, false) === entry.seen) &&
156158
input.apply(thread, entry.action, DateTime.nowUnsafe()) !== thread
157159
) {
158160
const sent = yield* Effect.exit(input.send(registry, entry));
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/// <reference lib="dom" />
2+
import { CommandId, ThreadId } from "@t3tools/contracts";
3+
import { expect } from "e2e";
4+
import * as Effect from "effect/Effect";
5+
6+
import { T3_HOME } from "../e2e.config.ts";
7+
import { rpcClient, runPromise, test, WEB } from "./t3.ts";
8+
9+
test(
10+
"An offline settle leaves a newer pin reorder in place",
11+
WEB,
12+
async ({ t3, app, browser, screen }) => {
13+
const project = await t3.project("demo");
14+
const moved = await t3.thread({ project, title: "First pinned thread", messages: ["Start"] });
15+
const other = await t3.thread({ project, title: "Second pinned thread", messages: ["Start"] });
16+
17+
const withClient = <A, E>(
18+
use: (client: Effect.Success<ReturnType<typeof rpcClient>>) => Effect.Effect<A, E>,
19+
) => runPromise(rpcClient(T3_HOME, app.baseUrl ?? "").pipe(Effect.flatMap(use), Effect.scoped));
20+
21+
await withClient((client) =>
22+
Effect.gen(function* () {
23+
for (const [thread, orderKey] of [
24+
[moved, "g"],
25+
[other, "m"],
26+
] as const) {
27+
yield* client["orchestration.dispatchCommand"]({
28+
type: "thread.pin",
29+
commandId: CommandId.make(`scenario:pin:${thread.threadId}`),
30+
threadId: ThreadId.make(thread.threadId),
31+
orderKey,
32+
});
33+
}
34+
}),
35+
);
36+
37+
await browser.addInitScript(() => {
38+
localStorage.setItem("t3code:sidebar:settled-expanded", "true");
39+
const sockets = new Set<WebSocket>();
40+
let offline = false;
41+
Object.defineProperty(navigator, "onLine", { get: () => !offline });
42+
const NativeWebSocket = window.WebSocket;
43+
window.WebSocket = class extends NativeWebSocket {
44+
constructor(url: string | URL, protocols?: string | string[]) {
45+
super(url, protocols);
46+
if (new URL(url, location.href).pathname !== "/ws") return;
47+
sockets.add(this);
48+
this.addEventListener("close", () => sockets.delete(this), { once: true });
49+
if (offline) this.close();
50+
}
51+
};
52+
document.addEventListener("scenario-offline", () => {
53+
offline = true;
54+
window.dispatchEvent(new Event("offline"));
55+
for (const socket of sockets) socket.close();
56+
});
57+
document.addEventListener("scenario-online", () => {
58+
offline = false;
59+
window.dispatchEvent(new Event("online"));
60+
});
61+
});
62+
63+
await t3.open(other);
64+
const row = browser.locator(`li[data-thread-item$=":${moved.threadId}"]`);
65+
await row.getByRole("button", "Unpin thread", { exact: true }).waitFor();
66+
await browser.evaluate(() => {
67+
document.dispatchEvent(new Event("scenario-offline"));
68+
return null;
69+
});
70+
await screen.getByRole("button", "Environment disconnected", { exact: true }).waitFor();
71+
await row.hover();
72+
await row.getByRole("button", "Settle thread", { exact: true }).tap();
73+
const pending = row.getByText("Pending", { exact: true });
74+
await pending.waitFor();
75+
76+
await withClient((client) =>
77+
client["orchestration.dispatchCommand"]({
78+
type: "thread.pin.reorder",
79+
commandId: CommandId.make(`scenario:reorder:${moved.threadId}`),
80+
threadId: ThreadId.make(moved.threadId),
81+
orderKey: "t",
82+
}),
83+
);
84+
await browser.evaluate(() => {
85+
document.dispatchEvent(new Event("scenario-online"));
86+
return null;
87+
});
88+
await pending.waitFor({ state: "detached" });
89+
90+
await app.screenshot("pin-after-reconnect");
91+
const { thread } = await withClient((client) =>
92+
client["orchestration.getThreadProjection"]({ threadId: ThreadId.make(moved.threadId) }),
93+
);
94+
expect({ pinned: thread.pinnedAt != null, pinOrderKey: thread.pinOrderKey }).toEqual({
95+
pinned: true,
96+
pinOrderKey: "t",
97+
});
98+
},
99+
);

0 commit comments

Comments
 (0)