@devries-cameron20 / t3code-1 / commits / a4bf7922f5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(clients): undoing a settle puts the thread back where you placed it (#33248)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Nov 15, 2026 at 04:28 UTC a4bf7922f57615a4f5bffdc81a51b75f5b77a800
9 files changed +135 -11
apps/mobile/src/features/home/useThreadListActions.ts
+19 -4
@@ -116,6 +116,7 @@ function useThreadActionExecutor(
116 action: ThreadListAction,
117 thread: EnvironmentThreadShell,
118 onStarted?: () => void,
119 + restoreUnsettledAt?: string | null,
120 ) => {
121 if (!checkThreadOperationPermission(thread, actionFailureTitle(action))) return false;
122 const key = scopedThreadKey(thread.environmentId, thread.id);
@@ -155,7 +156,11 @@ function useThreadActionExecutor(
156 // suppressed until real activity clears the pin server-side.
157 await unsettleMutation({
158 environmentId: thread.environmentId,
158 - input: { threadId: thread.id, reason: "user" },
159 + input: {
160 + threadId: thread.id,
161 + reason: "user",
162 + ...(restoreUnsettledAt === undefined ? {} : { restoreUnsettledAt }),
163 + },
164 })
165 : await (
166 action === "settle"
@@ -265,11 +270,19 @@ export function useThreadListActions(): {
270 async (step) => {
271 switch (step.action) {
272 case "unsettle":
268 - return unsettleThread(thread);
273 + return unsettleThread(thread, step.restoreUnsettledAt);
274 case "unsnooze":
275 return unsnoozeThread(thread);
276 case "pin":
277 return pinThread(thread, step.orderKey);
278 + case "reorderActive": {
279 + const result = await reorderActiveMutation({
280 + environmentId: thread.environmentId,
281 + input: { threadId: thread.id, orderKey: step.orderKey },
282 + });
283 + if (result._tag === "Failure") throw Cause.squash(result.cause);
284 + return true;
285 + }
286 case "snooze": {
287 const failureTitle =
288 action === "Unpinned" ? "Could not undo unpin" : "Could not undo settle";
@@ -413,8 +426,10 @@ export function useThreadListActions(): {
426 snoozeInFlightThreadKeys.current.delete(key);
427 return succeeded;
428 };
416 - const unsettleThread = async (thread: EnvironmentThreadShell) =>
417 - (await executeAction("unsettle", thread)) === true;
429 + const unsettleThread = async (
430 + thread: EnvironmentThreadShell,
431 + restoreUnsettledAt?: string | null,
432 + ) => (await executeAction("unsettle", thread, undefined, restoreUnsettledAt)) === true;
433 const pinThread = async (thread: EnvironmentThreadShell, restoreOrderKey?: string) => {
434 if (!checkThreadOperationPermission(thread, "Could not pin thread")) return false;
435 if (!environmentConfigLoaded(thread.environmentId)) {
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+21
@@ -2005,6 +2005,27 @@ it.effect("explicit settle clears snooze atomically and stays idempotent after r
2005 } as const;
2006 assert.deepInclude(yield* projections.getThreadShell(threadId), active);
2007 assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);
2008 +
2009 + // Undo of a settle restores the cleared Active anchor instead of stamping a new one.
2010 + for (const restoreUnsettledAt of [DateTime.formatIso(now), null]) {
2011 + yield* TestClock.adjust("1 second");
2012 + yield* orchestrator.dispatch({
2013 + type: "thread.settle",
2014 + commandId: CommandId.make(`settle-before-undo-${restoreUnsettledAt}`),
2015 + threadId,
2016 + });
2017 + yield* orchestrator.dispatch({
2018 + type: "thread.unsettle",
2019 + commandId: CommandId.make(`undo-settle-${restoreUnsettledAt}`),
2020 + threadId,
2021 + reason: "user",
2022 + restoreUnsettledAt,
2023 + });
2024 + assert.deepInclude(yield* projections.getThreadShell(threadId), {
2025 + ...active,
2026 + unsettledAt: restoreUnsettledAt === null ? null : now,
2027 + });
2028 + }
2029 }).pipe(Effect.provide(layerTest)),
2030 );
2031
apps/server/src/orchestration-v2/Orchestrator.ts
+20 -1
@@ -4499,6 +4499,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4499 });
4500 }
4501 }
4502 + let restoredUnsettledAt: DateTime.Utc | null | undefined;
4503 + if (command.type === "thread.unsettle" && command.restoreUnsettledAt !== undefined) {
4504 + const parsed =
4505 + command.restoreUnsettledAt === null
4506 + ? Option.some(null)
4507 + : DateTime.make(command.restoreUnsettledAt);
4508 + if (Option.isNone(parsed)) {
4509 + return yield* new OrchestratorDispatchError({
4510 + commandId: command.commandId,
4511 + commandType: command.type,
4512 + cause: `Thread ${command.threadId} unsettle anchor ${command.restoreUnsettledAt} is not a valid time.`,
4513 + });
4514 + }
4515 + restoredUnsettledAt = parsed.value;
4516 + }
4517 let snoozedUntil: DateTime.Utc | null = null;
4518 if (command.type === "thread.snooze") {
4519 const projection = yield* loadProjectionForCommand(command, ["runs", "runtimeRequests"], {
@@ -4628,7 +4643,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4643 ...thread,
4644 settledOverride: "active",
4645 settledAt: null,
4631 - unsettledAt: alreadyPinnedActive ? (thread.unsettledAt ?? null) : now,
4646 + unsettledAt: alreadyPinnedActive
4647 + ? (thread.unsettledAt ?? null)
4648 + : restoredUnsettledAt === undefined
4649 + ? now
4650 + : restoredUnsettledAt,
4651 updatedAt: alreadyPinnedActive ? thread.updatedAt : now,
4652 };
4653 }
apps/web/src/hooks/useThreadActions.ts
+9 -3
@@ -704,7 +704,7 @@ export function useThreadActions() {
704 return deleteResult;
705 };
706
707 - const unsettleThread = async (target: ScopedThreadRef) => {
707 + const unsettleThread = async (target: ScopedThreadRef, restoreUnsettledAt?: string | null) => {
708 if (!readEnvironmentServerConfigLoaded(target.environmentId)) {
709 return AsyncResult.failure(
710 Cause.fail(
@@ -719,7 +719,11 @@ export function useThreadActions() {
719 // reason "user" pins active until real activity, suppressing auto-settle.
720 return unsettleThreadMutation({
721 environmentId: target.environmentId,
722 - input: { threadId: target.threadId, reason: "user" },
722 + input: {
723 + threadId: target.threadId,
724 + reason: "user",
725 + ...(restoreUnsettledAt === undefined ? {} : { restoreUnsettledAt }),
726 + },
727 });
728 };
729
@@ -962,11 +966,13 @@ export function useThreadActions() {
966 async (step) => {
967 switch (step.action) {
968 case "unsettle":
965 - return unsettleThread(target);
969 + return unsettleThread(target, step.restoreUnsettledAt);
970 case "unsnooze":
971 return unsnoozeThread(target);
972 case "pin":
973 return sendPin(target, step.orderKey === undefined ? {} : { orderKey: step.orderKey });
974 + case "reorderActive":
975 + return reorderActiveThread(target, step.orderKey);
976 case "snooze":
977 return snoozeThreadMutation({
978 environmentId: target.environmentId,
apps/web/src/hooks/useThreadActions.undo.test.ts
+36
@@ -18,6 +18,7 @@ const commands = vi.hoisted(() => ({
18 unsettle: vi.fn(),
19 snooze: vi.fn(),
20 unsnooze: vi.fn(),
21 + reorderActive: vi.fn(),
22 }));
23 const router = vi.hoisted(() => {
24 const listeners = new Set<(event: { action: { type: string } }) => void>();
@@ -61,6 +62,8 @@ const threadShell = vi.hoisted(() => ({
62 title: "Thread",
63 pinOrderKey: "a0",
64 pinnedAt: null as string | null,
65 + activeOrderKey: null as string | null,
66 + unsettledAt: null as string | null,
67 snoozedUntil: null as string | null,
68 projectId: "project",
69 environmentId: "undo-env",
@@ -94,6 +97,8 @@ vi.mock("../state/use-atom-command", () => ({
97 return commands.snooze;
98 case threadEnvironment.unsnooze:
99 return commands.unsnooze;
100 + case threadEnvironment.reorderActive:
101 + return commands.reorderActive;
102 default:
103 return vi.fn();
104 }
@@ -119,6 +124,8 @@ beforeEach(() => {
124 router.state.location.href = "/";
125 router.state.matches[0]!.params = {};
126 threadShell.pinnedAt = null;
127 + threadShell.activeOrderKey = null;
128 + threadShell.unsettledAt = null;
129 threadShell.snoozedUntil = null;
130 shellLoaded.current = true;
131 });
@@ -311,6 +318,35 @@ describe("settle and snooze Undo", () => {
318 });
319 });
320
321 + it("puts a manually ordered thread back in its active position", async () => {
322 + threadShell.activeOrderKey = "b5";
323 + await useThreadActions().settleThread(target);
324 + await currentUndo()();
325 + expect(commands.unsettle).toHaveBeenCalledOnce();
326 + expect(commands.reorderActive).toHaveBeenCalledExactlyOnceWith({
327 + environmentId: target.environmentId,
328 + input: { threadId: target.threadId, orderKey: "b5" },
329 + });
330 + expect(commands.unsettle.mock.invocationCallOrder[0]).toBeLessThan(
331 + commands.reorderActive.mock.invocationCallOrder[0]!,
332 + );
333 + });
334 +
335 + it("puts an automatically ordered thread back at its Active anchor", async () => {
336 + threadShell.unsettledAt = "2026-01-02T00:00:00.000Z";
337 + await useThreadActions().settleThread(target);
338 + await currentUndo()();
339 + expect(commands.unsettle).toHaveBeenCalledExactlyOnceWith({
340 + environmentId: target.environmentId,
341 + input: {
342 + threadId: target.threadId,
343 + reason: "user",
344 + restoreUnsettledAt: "2026-01-02T00:00:00.000Z",
345 + },
346 + });
347 + expect(commands.reorderActive).not.toHaveBeenCalled();
348 + });
349 +
350 it("stops settle restoration if restoring the pin fails", async () => {
351 threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
352 threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z";
packages/client-runtime/src/operations/commands.ts
+4
@@ -105,6 +105,7 @@ export type SettleThreadInput = ThreadCommandInput;
105
106 export interface UnsettleThreadInput extends ThreadCommandInput {
107 readonly reason: "user";
108 + readonly restoreUnsettledAt?: string | null;
109 }
110
111 export interface PinThreadInput extends ThreadCommandInput {
@@ -629,6 +630,9 @@ export const unsettleThread = Effect.fn("EnvironmentCommands.unsettleThread")(fu
630 commandId: yield* allocateCommandId(input),
631 threadId: input.threadId,
632 reason: input.reason,
633 + ...(input.restoreUnsettledAt === undefined
634 + ? {}
635 + : { restoreUnsettledAt: input.restoreUnsettledAt }),
636 });
637 });
638
packages/client-runtime/src/state/threadCommands.ts
+8 -1
@@ -493,7 +493,14 @@ export function createThreadEnvironmentAtoms<R, E>(
493 ...thread,
494 settledOverride: input.reason === "user" ? "active" : null,
495 settledAt: null,
496 - unsettledAt: thread.settledOverride === "active" ? (thread.unsettledAt ?? null) : now,
496 + unsettledAt:
497 + thread.settledOverride === "active"
498 + ? (thread.unsettledAt ?? null)
499 + : input.restoreUnsettledAt === undefined
500 + ? now
501 + : input.restoreUnsettledAt === null
502 + ? null
503 + : DateTime.makeUnsafe(input.restoreUnsettledAt),
504 })),
505 snooze: optimistic.wrap(commands.snooze, (thread, input, now, accepted) =>
506 (!accepted &&
packages/client-runtime/src/state/threadUndo.ts
+16 -2
@@ -36,9 +36,10 @@ export function invalidate(kind: string, threadKey: string) {
36 }
37
38 export type ThreadUndoStep =
39 - | { readonly action: "unsettle" }
39 + | { readonly action: "unsettle"; readonly restoreUnsettledAt?: string | null }
40 | { readonly action: "unsnooze" }
41 | { readonly action: "pin"; readonly orderKey?: string }
42 + | { readonly action: "reorderActive"; readonly orderKey: string }
43 | { readonly action: "snooze"; readonly snoozedUntil: string };
44
45 export type ThreadUndoPlan = readonly [ThreadUndoStep, ...ThreadUndoStep[]];
@@ -48,6 +49,8 @@ export function planThreadUndo(
49 thread?: {
50 readonly pinnedAt?: string | null;
51 readonly pinOrderKey?: string | null;
52 + readonly activeOrderKey?: string | null;
53 + readonly unsettledAt?: string | null;
54 readonly snoozedUntil?: string | null;
55 } | null,
56 ): ThreadUndoPlan {
@@ -62,7 +65,18 @@ export function planThreadUndo(
65 ? [{ action: "snooze" as const, snoozedUntil: thread.snoozedUntil }]
66 : [];
67 if (action === "unpin") return [pin, ...snooze];
65 - return [{ action: "unsettle" }, ...(thread?.pinnedAt != null ? [pin] : []), ...snooze];
68 + // Settling clears a manual active position; pinned threads are placed by their pin instead.
69 + const placement: ThreadUndoStep[] =
70 + thread?.pinnedAt != null
71 + ? [pin]
72 + : thread?.activeOrderKey != null
73 + ? [{ action: "reorderActive", orderKey: thread.activeOrderKey }]
74 + : [];
75 + const unsettle: ThreadUndoStep =
76 + thread == null
77 + ? { action: "unsettle" }
78 + : { action: "unsettle", restoreUnsettledAt: thread.unsettledAt ?? null };
79 + return [unsettle, ...placement, ...snooze];
80 }
81
82 export async function executeThreadUndo<Result>(
packages/contracts/src/orchestrationV2.ts
+2
@@ -3174,6 +3174,8 @@ export const OrchestrationV2Command = Schema.Union([
3174 commandId: CommandId,
3175 threadId: ThreadId,
3176 reason: Schema.Literal("user"),
3177 + // Undo of a settle restores the Active anchor it cleared instead of moving the thread to the top.
3178 + restoreUnsettledAt: Schema.optional(Schema.NullOr(IsoDateTime)),
3179 }),
3180 Schema.Struct({
3181 type: Schema.Literal("thread.snooze"),