Skip to content

Commit 494bd70

Browse files
committed
fix(client): preserve parking navigation through delayed updates
1 parent 0b8d143 commit 494bd70

6 files changed

Lines changed: 75 additions & 12 deletions

File tree

‎apps/web/src/components/Sidebar.logic.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,16 +2501,34 @@ describe("navigation after parking a thread", () => {
25012501
["settle", "settled", null, "other-thread", false],
25022502
["snooze", null, "2099-01-01T00:00:00.000Z", "thread", true],
25032503
["snooze", null, null, "thread", false],
2504+
["snooze", null, "2026-09-12T09:00:00.000Z", "thread", false],
2505+
["snooze", null, "2099-01-01T00:00:00.000Z", "thread", false, true],
25042506
["snooze", null, "2099-01-01T00:00:00.000Z", "other-thread", false],
25052507
] as const)(
25062508
"%s with state %s / %s on %s navigates: %s",
2507-
(action, settledOverride, snoozedUntil, currentThreadKey, expected) => {
2509+
(
2510+
action,
2511+
settledOverride,
2512+
snoozedUntil,
2513+
currentThreadKey,
2514+
expected,
2515+
hasPendingApprovals: boolean = false,
2516+
) => {
25082517
expect(
25092518
shouldNavigateAfterThreadPark({
25102519
threadKey: "thread",
25112520
currentThreadKey,
25122521
action,
2513-
thread: { settledOverride, snoozedUntil },
2522+
now: "2026-09-12T10:00:00.000Z",
2523+
thread: {
2524+
settledOverride,
2525+
snoozedUntil,
2526+
snoozedAt: null,
2527+
session: null,
2528+
latestTurn: null,
2529+
hasPendingApprovals,
2530+
hasPendingUserInput: false,
2531+
},
25142532
}),
25152533
).toBe(expected);
25162534
},

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import type { ContextMenuItem } from "@t3tools/contracts";
99
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
1010
import type { AsyncResult } from "effect/unstable/reactivity";
1111
import { planPinnedReorder } from "@t3tools/client-runtime/state/thread-sort";
12+
import {
13+
effectiveSnoozed,
14+
type ThreadSnoozeShell,
15+
} from "@t3tools/client-runtime/state/thread-settled";
1216
import {
1317
getThreadSortTimestamp,
1418
resolveSettledThreadTimestamp,
@@ -25,13 +29,15 @@ export function shouldNavigateAfterThreadPark(input: {
2529
readonly threadKey: string;
2630
readonly currentThreadKey: string | null;
2731
readonly action: "settle" | "snooze";
28-
readonly thread: Pick<SidebarThreadSummary, "settledOverride" | "snoozedUntil"> | null;
32+
readonly now: string;
33+
readonly thread: (ThreadSnoozeShell & Pick<SidebarThreadSummary, "settledOverride">) | null;
2934
}): boolean {
3035
return (
3136
input.threadKey === input.currentThreadKey &&
37+
input.thread !== null &&
3238
(input.action === "settle"
33-
? input.thread?.settledOverride === "settled"
34-
: input.thread?.snoozedUntil != null)
39+
? input.thread.settledOverride === "settled"
40+
: effectiveSnoozed(input.thread, { now: input.now }))
3541
);
3642
}
3743

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,7 @@ export default function Sidebar() {
30443044
threadKey,
30453045
currentThreadKey: routeThreadKeyRef.current,
30463046
action: "settle",
3047+
now: new Date().toISOString(),
30473048
thread: readThreadShell(threadRef),
30483049
})
30493050
) {
@@ -3581,6 +3582,7 @@ export default function Sidebar() {
35813582
threadKey: activeKey,
35823583
currentThreadKey: routeThreadKeyRef.current,
35833584
action: "settle",
3585+
now: new Date().toISOString(),
35843586
thread: readThreadShell(threadRef),
35853587
})
35863588
)
@@ -3686,6 +3688,7 @@ export default function Sidebar() {
36863688
threadKey,
36873689
currentThreadKey: routeThreadKeyRef.current,
36883690
action: "snooze",
3691+
now: new Date().toISOString(),
36893692
thread: readThreadShell(threadRef),
36903693
})
36913694
) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,32 @@ describe("remote thread lifecycle commands", () => {
267267
yield* Effect.promise(() => result);
268268
}),
269269
);
270+
271+
for (const action of ["settle", "snooze"] as const) {
272+
it.effect(`shows an accepted ${action} while the shell still has an old input request`, () =>
273+
Effect.gen(function* () {
274+
const h = yield* makeHarness();
275+
const stale = {
276+
...SNAPSHOT,
277+
threads: [{ ...SNAPSHOT.threads[0]!, hasPendingUserInput: true }],
278+
};
279+
h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), stale);
280+
const result = h.commands[action].run(h.registry, {
281+
environmentId: ENVIRONMENT_ID,
282+
input: { threadId: THREAD_ID, snoozedUntil: "2099-01-01T00:00:00.000Z" },
283+
});
284+
const request = yield* Queue.take(h.requests);
285+
expect(h.registry.get(h.visibleAtom)?.threads[0]).toBe(stale.threads[0]);
286+
yield* Deferred.succeed(request.reply, { sequence: 2 });
287+
expect((yield* Effect.promise(() => result))._tag).toBe("Success");
288+
expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject(
289+
action === "settle"
290+
? { settledOverride: "settled" }
291+
: { snoozedUntil: "2099-01-01T00:00:00.000Z" },
292+
);
293+
expect(h.registry.get(h.visibleAtom)?.threads[0]?.hasPendingUserInput).toBe(false);
294+
expect(h.registry.get(h.snapshotAtom(ENVIRONMENT_ID))).toBe(stale);
295+
}),
296+
);
297+
}
270298
});

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,16 @@ export function createThreadEnvironmentAtoms<R, E>(
259259
return {
260260
...commands,
261261
snapshotAtom: optimistic.snapshotAtom,
262-
settle: optimistic.wrap(commands.settle, (thread, _input, now) =>
263-
!canSnooze(thread, { now }) ||
264-
thread.session?.status === "starting" ||
265-
thread.session?.status === "running"
262+
settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>
263+
!accepted &&
264+
(!canSnooze(thread, { now }) ||
265+
thread.session?.status === "starting" ||
266+
thread.session?.status === "running")
266267
? thread
267268
: {
268269
...thread,
270+
hasPendingApprovals: false,
271+
hasPendingUserInput: false,
269272
settledOverride: "settled",
270273
settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,
271274
unsettledAt: null,
@@ -282,11 +285,14 @@ export function createThreadEnvironmentAtoms<R, E>(
282285
settledAt: null,
283286
unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
284287
})),
285-
snooze: optimistic.wrap(commands.snooze, (thread, input, now) =>
286-
!canSnooze(thread, { now }) || !(Date.parse(input.snoozedUntil) > Date.parse(now))
288+
snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
289+
(!accepted && !canSnooze(thread, { now })) ||
290+
!(Date.parse(input.snoozedUntil) > Date.parse(now))
287291
? thread
288292
: {
289293
...thread,
294+
hasPendingApprovals: false,
295+
hasPendingUserInput: false,
290296
snoozedUntil: input.snoozedUntil,
291297
snoozedAt: thread.snoozedUntil === input.snoozedUntil ? (thread.snoozedAt ?? now) : now,
292298
},

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function createOptimisticThreadLifecycle(
5858
thread: OrchestrationThreadShell,
5959
input: Input,
6060
now: string,
61+
accepted: boolean,
6162
) => OrchestrationThreadShell,
6263
): typeof command {
6364
return {
@@ -68,7 +69,7 @@ export function createOptimisticThreadLifecycle(
6869
const source = sourceSnapshotAtom(target.environmentId);
6970
const update: PendingThreadUpdate = {
7071
threadId: target.input.threadId,
71-
apply: (thread) => apply(thread, target.input, now),
72+
apply: (thread) => apply(thread, target.input, now, update.sequence !== undefined),
7273
};
7374
const remove = () =>
7475
registry.update(pending, (current) => current.filter((item) => item !== update));
@@ -78,6 +79,7 @@ export function createOptimisticThreadLifecycle(
7879
const result = await command.run(registry, target);
7980
if (result._tag === "Success") {
8081
update.sequence = result.value.sequence;
82+
registry.update(pending, (current) => [...current]);
8183
const reconcile = (snapshot: OrchestrationShellSnapshot | null) => {
8284
if (snapshot === null || snapshot.snapshotSequence >= result.value.sequence) {
8385
remove();

0 commit comments

Comments
 (0)