The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | CommandId, |
| 4 | RuntimeRequestId, |
| 5 | EnvironmentId, |
| 6 | MessageId, |
| 7 | RunId, |
| 8 | ORCHESTRATION_V2_WS_METHODS, |
| 9 | ProjectId, |
| 10 | ProviderInstanceId, |
| 11 | ThreadId, |
| 12 | type OrchestrationV2Command, |
| 13 | type OrchestrationV2ShellSnapshot, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import * as DateTime from "effect/DateTime"; |
| 16 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 17 | import * as Deferred from "effect/Deferred"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as Exit from "effect/Exit"; |
| 20 | import * as Fiber from "effect/Fiber"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as Option from "effect/Option"; |
| 23 | import * as Queue from "effect/Queue"; |
| 24 | import * as Scope from "effect/Scope"; |
| 25 | import * as Stream from "effect/Stream"; |
| 26 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 27 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 28 | |
| 29 | import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 30 | import { |
| 31 | AVAILABLE_CONNECTION_STATE, |
| 32 | BearerConnectionTarget, |
| 33 | type SupervisorConnectionState, |
| 34 | } from "../connection/model.ts"; |
| 35 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 36 | import type { RpcSession } from "../rpc/session.ts"; |
| 37 | import { createThreadEnvironmentAtoms } from "./threadCommands.ts"; |
| 38 | import * as ThreadHistoryController from "./threadHistoryController.ts"; |
| 39 | |
| 40 | const ENVIRONMENT_ID = EnvironmentId.make("remote"); |
| 41 | const THREAD_ID = ThreadId.make("thread"); |
| 42 | const NOW = DateTime.makeUnsafe("2026-09-12T10:00:00.000Z"); |
| 43 | const FUTURE = DateTime.makeUnsafe("2099-01-01T00:00:00.000Z"); |
| 44 | const APPROVAL = { |
| 45 | id: RuntimeRequestId.make("approval"), |
| 46 | kind: "command" as const, |
| 47 | createdAt: NOW, |
| 48 | }; |
| 49 | const USER_INPUT = { |
| 50 | id: RuntimeRequestId.make("input"), |
| 51 | kind: "user_input" as const, |
| 52 | createdAt: NOW, |
| 53 | }; |
| 54 | const SNAPSHOT: OrchestrationV2ShellSnapshot = { |
| 55 | snapshotSequence: 1, |
| 56 | schemaVersion: 2, |
| 57 | archivedThreads: [], |
| 58 | projects: [], |
| 59 | threads: [ |
| 60 | { |
| 61 | id: THREAD_ID, |
| 62 | projectId: ProjectId.make("project"), |
| 63 | title: "Remote thread", |
| 64 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 65 | runtimeMode: "full-access", |
| 66 | interactionMode: "default", |
| 67 | branch: null, |
| 68 | worktreePath: null, |
| 69 | createdBy: "user", |
| 70 | creationSource: "web", |
| 71 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 72 | lineage: { rootThreadId: THREAD_ID, parentThreadId: null, relationshipToParent: null }, |
| 73 | forkedFrom: null, |
| 74 | activeProviderThreadId: null, |
| 75 | latestRunId: null, |
| 76 | activeRunId: null, |
| 77 | status: "idle", |
| 78 | pendingRuntimeRequest: null, |
| 79 | latestVisibleMessage: null, |
| 80 | itemCount: 0, |
| 81 | visibleItemCount: 0, |
| 82 | deletedAt: null, |
| 83 | createdAt: NOW, |
| 84 | updatedAt: NOW, |
| 85 | archivedAt: null, |
| 86 | settledOverride: null, |
| 87 | settledAt: null, |
| 88 | pullRequests: [], |
| 89 | latestUserMessageAt: null, |
| 90 | hasActionableProposedPlan: false, |
| 91 | }, |
| 92 | ], |
| 93 | }; |
| 94 | |
| 95 | const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* (options?: { |
| 96 | readonly loadEarlier?: () => Effect.Effect<ThreadHistoryController.ThreadHistoryLoadEarlierResult>; |
| 97 | readonly reconcilePendingWork?: (threadId: ThreadId) => Effect.Effect<void>; |
| 98 | }) { |
| 99 | const requests = yield* Queue.unbounded<{ |
| 100 | command: OrchestrationV2Command; |
| 101 | reply: Deferred.Deferred<{ sequence: number }, Error>; |
| 102 | }>(); |
| 103 | const reconciled = yield* Queue.unbounded<ThreadId>(); |
| 104 | const connectionFollowed = yield* Deferred.make<void>(); |
| 105 | const connection = yield* SubscriptionRef.make<SupervisorConnectionState>({ |
| 106 | ...AVAILABLE_CONNECTION_STATE, |
| 107 | desired: true, |
| 108 | phase: "connected", |
| 109 | }); |
| 110 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 111 | target: new BearerConnectionTarget({ |
| 112 | environmentId: ENVIRONMENT_ID, |
| 113 | label: "Test", |
| 114 | connectionId: "test", |
| 115 | }), |
| 116 | state: connection, |
| 117 | session: yield* SubscriptionRef.make( |
| 118 | Option.some({ |
| 119 | client: { |
| 120 | [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) => |
| 121 | Effect.gen(function* () { |
| 122 | const reply = yield* Deferred.make<{ sequence: number }, Error>(); |
| 123 | yield* Queue.offer(requests, { command, reply }); |
| 124 | return yield* Deferred.await(reply); |
| 125 | }), |
| 126 | }, |
| 127 | } as unknown as RpcSession), |
| 128 | ), |
| 129 | } as EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 130 | const runtime = Atom.runtime( |
| 131 | Layer.mergeAll( |
| 132 | Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, { |
| 133 | run: (_environmentId, effect) => |
| 134 | Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 135 | followStream: (_environmentId, stream) => |
| 136 | Stream.provideService( |
| 137 | stream, |
| 138 | EnvironmentSupervisor.EnvironmentSupervisor, |
| 139 | supervisor, |
| 140 | ).pipe(Stream.onStart(Deferred.succeed(connectionFollowed, undefined))), |
| 141 | } as EnvironmentRegistry.EnvironmentRegistry["Service"]), |
| 142 | Layer.succeed( |
| 143 | SecureRandom.SecureRandom, |
| 144 | SecureRandom.make((size) => new Uint8Array(size)), |
| 145 | ), |
| 146 | Layer.succeed( |
| 147 | ThreadHistoryController.ThreadHistoryController, |
| 148 | ThreadHistoryController.ThreadHistoryController.of({ |
| 149 | register: () => Effect.die("unused"), |
| 150 | unregister: () => Effect.void, |
| 151 | loadEarlier: () => options?.loadEarlier?.() ?? Effect.die("unused"), |
| 152 | reconcilePendingWork: (_environmentId, threadId) => |
| 153 | options?.reconcilePendingWork?.(threadId) ?? Queue.offer(reconciled, threadId), |
| 154 | }), |
| 155 | ), |
| 156 | ), |
| 157 | ); |
| 158 | const snapshotAtom = Atom.family((_environmentId: EnvironmentId) => |
| 159 | Atom.make<OrchestrationV2ShellSnapshot | null>(SNAPSHOT), |
| 160 | ); |
| 161 | const commands = createThreadEnvironmentAtoms(runtime, snapshotAtom); |
| 162 | const registry = AtomRegistry.make(); |
| 163 | yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 164 | const visibleAtom = commands.snapshotAtom(ENVIRONMENT_ID); |
| 165 | registry.mount(visibleAtom); |
| 166 | return { |
| 167 | registry, |
| 168 | commands, |
| 169 | snapshotAtom, |
| 170 | visibleAtom, |
| 171 | requests, |
| 172 | reconciled, |
| 173 | connection, |
| 174 | connectionFollowed, |
| 175 | }; |
| 176 | }); |
| 177 | |
| 178 | describe("history loading", () => { |
| 179 | it.effect("sends a message on the thread while earlier history is loading", () => |
| 180 | Effect.gen(function* () { |
| 181 | const historyStarted = yield* Deferred.make<void>(); |
| 182 | const historyReply = |
| 183 | yield* Deferred.make<ThreadHistoryController.ThreadHistoryLoadEarlierResult>(); |
| 184 | const h = yield* makeHarness({ |
| 185 | loadEarlier: () => |
| 186 | Deferred.succeed(historyStarted, undefined).pipe( |
| 187 | Effect.andThen(Deferred.await(historyReply)), |
| 188 | ), |
| 189 | }); |
| 190 | yield* Effect.addFinalizer(() => Deferred.succeed(historyReply, { _tag: "loaded" })); |
| 191 | const history = yield* Effect.forkChild( |
| 192 | Effect.exit( |
| 193 | h.commands.loadEarlierHistory.run(h.registry, { |
| 194 | environmentId: ENVIRONMENT_ID, |
| 195 | input: { threadId: THREAD_ID }, |
| 196 | }), |
| 197 | ), |
| 198 | { startImmediately: true }, |
| 199 | ); |
| 200 | yield* Deferred.await(historyStarted); |
| 201 | const send = yield* Effect.forkChild( |
| 202 | Effect.exit( |
| 203 | h.commands.startTurn.run(h.registry, { |
| 204 | environmentId: ENVIRONMENT_ID, |
| 205 | input: { |
| 206 | threadId: THREAD_ID, |
| 207 | message: { |
| 208 | messageId: MessageId.make("message"), |
| 209 | role: "user", |
| 210 | text: "Send while history loads", |
| 211 | attachments: [], |
| 212 | }, |
| 213 | runtimeMode: "full-access", |
| 214 | interactionMode: "default", |
| 215 | dispatchMode: "start", |
| 216 | }, |
| 217 | }), |
| 218 | ), |
| 219 | { startImmediately: true }, |
| 220 | ); |
| 221 | // Dispatched after Send on another thread, so it arrives first only if Send is queued. |
| 222 | const controlThreadId = ThreadId.make("control-thread"); |
| 223 | const control = yield* Effect.forkChild( |
| 224 | Effect.exit( |
| 225 | h.commands.interruptTurn.run(h.registry, { |
| 226 | environmentId: ENVIRONMENT_ID, |
| 227 | input: { threadId: controlThreadId, runId: RunId.make("control-run") }, |
| 228 | }), |
| 229 | ), |
| 230 | { startImmediately: true }, |
| 231 | ); |
| 232 | const first = yield* Queue.take(h.requests); |
| 233 | yield* Deferred.succeed(first.reply, { sequence: 1 }); |
| 234 | yield* Deferred.succeed(historyReply, { _tag: "loaded" }); |
| 235 | const second = yield* Queue.take(h.requests); |
| 236 | yield* Deferred.succeed(second.reply, { sequence: 2 }); |
| 237 | const results = yield* Fiber.joinAll([history, send, control]); |
| 238 | |
| 239 | expect(first.command).toMatchObject({ type: "message.dispatch", threadId: THREAD_ID }); |
| 240 | expect(second.command).toMatchObject({ type: "run.interrupt", threadId: controlThreadId }); |
| 241 | expect(results.map((value) => value._tag)).toEqual(["Success", "Success", "Success"]); |
| 242 | }), |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | describe("interrupt turn", () => { |
| 247 | // A stale cached item has nothing left to interrupt, so the server can reject Stop. |
| 248 | it.effect("reconciles pending work when the server rejects the interrupt", () => |
| 249 | Effect.gen(function* () { |
| 250 | const h = yield* makeHarness(); |
| 251 | const result = yield* Effect.forkChild( |
| 252 | Effect.exit( |
| 253 | h.commands.interruptTurn.run(h.registry, { |
| 254 | environmentId: ENVIRONMENT_ID, |
| 255 | input: { threadId: THREAD_ID, runId: RunId.make("finished-run") }, |
| 256 | }), |
| 257 | ), |
| 258 | { startImmediately: true }, |
| 259 | ); |
| 260 | const request = yield* Queue.take(h.requests); |
| 261 | expect(request.command.type).toBe("run.interrupt"); |
| 262 | yield* Deferred.fail(request.reply, new Error("Run is not interruptible")); |
| 263 | expect((yield* Fiber.join(result))._tag).toBe("Failure"); |
| 264 | expect(yield* Queue.take(h.reconciled)).toBe(THREAD_ID); |
| 265 | }), |
| 266 | ); |
| 267 | |
| 268 | // Mirrors the thread state's handler: the lookup runs in the thread's scope and is joined. |
| 269 | const stopWithBlockedLookup = Effect.fn("stopWithBlockedLookup")(function* () { |
| 270 | const threadScope = yield* Scope.make(); |
| 271 | const asked = yield* Deferred.make<void>(); |
| 272 | const h = yield* makeHarness({ |
| 273 | reconcilePendingWork: () => |
| 274 | Deferred.succeed(asked, undefined).pipe( |
| 275 | Effect.andThen(Effect.never), |
| 276 | Effect.forkIn(threadScope), |
| 277 | Effect.flatMap(Fiber.join), |
| 278 | ), |
| 279 | }); |
| 280 | const result = yield* Effect.forkChild( |
| 281 | Effect.exit( |
| 282 | h.commands.interruptTurn.run(h.registry, { |
| 283 | environmentId: ENVIRONMENT_ID, |
| 284 | input: { threadId: THREAD_ID, runId: RunId.make("running-run") }, |
| 285 | }), |
| 286 | ), |
| 287 | { startImmediately: true }, |
| 288 | ); |
| 289 | const request = yield* Queue.take(h.requests); |
| 290 | yield* Deferred.succeed(request.reply, { sequence: 1 }); |
| 291 | yield* Deferred.await(asked); |
| 292 | return { threadScope, result }; |
| 293 | }); |
| 294 | |
| 295 | it.effect("resolves Stop without waiting for the pending work lookup", () => |
| 296 | Effect.gen(function* () { |
| 297 | const { threadScope, result } = yield* stopWithBlockedLookup(); |
| 298 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 299 | yield* Scope.close(threadScope, Exit.void); |
| 300 | }), |
| 301 | ); |
| 302 | |
| 303 | it.effect("keeps an accepted Stop when the thread closes during the lookup", () => |
| 304 | Effect.gen(function* () { |
| 305 | const { threadScope, result } = yield* stopWithBlockedLookup(); |
| 306 | yield* Scope.close(threadScope, Exit.void); |
| 307 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 308 | }), |
| 309 | ); |
| 310 | }); |
| 311 | |
| 312 | describe("title regeneration", () => { |
| 313 | it.effect("resolves with the outcome recorded for its own request", () => |
| 314 | Effect.gen(function* () { |
| 315 | const h = yield* makeHarness(); |
| 316 | const source = h.snapshotAtom(ENVIRONMENT_ID); |
| 317 | const thread = SNAPSHOT.threads[0]!; |
| 318 | const target = { environmentId: ENVIRONMENT_ID, input: { threadId: THREAD_ID } }; |
| 319 | const earlier = { |
| 320 | requestId: CommandId.make("earlier"), |
| 321 | status: "renamed" as const, |
| 322 | completedAt: NOW, |
| 323 | }; |
| 324 | |
| 325 | h.registry.set(source, null); |
| 326 | const result = yield* Effect.forkChild( |
| 327 | Effect.exit(h.commands.regenerateTitle.run(h.registry, target)), |
| 328 | { startImmediately: true }, |
| 329 | ); |
| 330 | const request = yield* Queue.take(h.requests); |
| 331 | const requestId = request.command.commandId; |
| 332 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 333 | yield* Deferred.await(h.connectionFollowed); |
| 334 | h.registry.set(source, { |
| 335 | ...SNAPSHOT, |
| 336 | snapshotSequence: 2, |
| 337 | threads: [ |
| 338 | { |
| 339 | ...thread, |
| 340 | titleRegeneration: { requestId, startedAt: NOW }, |
| 341 | lastTitleRegeneration: earlier, |
| 342 | }, |
| 343 | ], |
| 344 | }); |
| 345 | h.registry.set(source, { |
| 346 | ...SNAPSHOT, |
| 347 | snapshotSequence: 3, |
| 348 | threads: [], |
| 349 | archivedThreads: [ |
| 350 | { |
| 351 | ...thread, |
| 352 | archivedAt: NOW, |
| 353 | titleRegeneration: null, |
| 354 | lastTitleRegeneration: { |
| 355 | requestId, |
| 356 | status: "failed", |
| 357 | message: "model unavailable", |
| 358 | completedAt: NOW, |
| 359 | }, |
| 360 | }, |
| 361 | ], |
| 362 | }); |
| 363 | const outcome = yield* Fiber.join(result); |
| 364 | expect(outcome).toMatchObject({ |
| 365 | _tag: "Success", |
| 366 | value: { status: "failed", message: "model unavailable" }, |
| 367 | }); |
| 368 | |
| 369 | const superseded = yield* Effect.forkChild( |
| 370 | Effect.exit(h.commands.regenerateTitle.run(h.registry, target)), |
| 371 | { startImmediately: true }, |
| 372 | ); |
| 373 | const second = yield* Queue.take(h.requests); |
| 374 | h.registry.set(source, { |
| 375 | ...SNAPSHOT, |
| 376 | snapshotSequence: 4, |
| 377 | threads: [{ ...thread, titleRegeneration: null, lastTitleRegeneration: earlier }], |
| 378 | }); |
| 379 | yield* Deferred.succeed(second.reply, { sequence: 4 }); |
| 380 | expect(yield* Fiber.join(superseded)).toMatchObject({ |
| 381 | _tag: "Success", |
| 382 | value: null, |
| 383 | }); |
| 384 | }), |
| 385 | ); |
| 386 | |
| 387 | it.effect("settles without an outcome when the environment disconnects while waiting", () => |
| 388 | Effect.gen(function* () { |
| 389 | const h = yield* makeHarness(); |
| 390 | const result = yield* Effect.forkChild( |
| 391 | Effect.exit( |
| 392 | h.commands.regenerateTitle.run(h.registry, { |
| 393 | environmentId: ENVIRONMENT_ID, |
| 394 | input: { threadId: THREAD_ID }, |
| 395 | }), |
| 396 | ), |
| 397 | { startImmediately: true }, |
| 398 | ); |
| 399 | const request = yield* Queue.take(h.requests); |
| 400 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 401 | yield* SubscriptionRef.update(h.connection, (state) => ({ |
| 402 | ...state, |
| 403 | phase: "backoff" as const, |
| 404 | })); |
| 405 | expect(yield* Fiber.join(result)).toMatchObject({ _tag: "Success", value: null }); |
| 406 | }), |
| 407 | ); |
| 408 | }); |
| 409 | |
| 410 | describe("remote thread lifecycle commands", () => { |
| 411 | const actions = [ |
| 412 | ["settle", {}, { settledOverride: "settled", pinnedAt: null, snoozedUntil: null }], |
| 413 | ["unsettle", { reason: "user" }, { settledOverride: "active", settledAt: null }], |
| 414 | ["snooze", { snoozedUntil: "2099-01-01T00:00:00.000Z" }, { snoozedUntil: FUTURE }], |
| 415 | ["unsnooze", { reason: "user" }, { snoozedUntil: null, snoozedAt: null }], |
| 416 | ["pin", { orderKey: "a" }, { pinnedAt: expect.any(Object), pinOrderKey: "a" }], |
| 417 | ["unpin", {}, { pinnedAt: null, pinOrderKey: null }], |
| 418 | ["setAutoSettle", { enabled: false }, { autoSettleDisabledAt: expect.any(Object) }], |
| 419 | ["reorderPin", { orderKey: "b" }, { pinOrderKey: "b" }], |
| 420 | ["reorderActive", { orderKey: "b" }, { activeOrderKey: "b" }], |
| 421 | ] as const; |
| 422 | |
| 423 | it.effect.each(actions)( |
| 424 | "shows %s before a delayed remote reply and rolls back a rejection", |
| 425 | ([action, input, expected]) => |
| 426 | Effect.gen(function* () { |
| 427 | const h = yield* makeHarness(); |
| 428 | const source = h.snapshotAtom(ENVIRONMENT_ID); |
| 429 | const initial = { |
| 430 | ...SNAPSHOT, |
| 431 | threads: [ |
| 432 | { |
| 433 | ...SNAPSHOT.threads[0]!, |
| 434 | ...(action === "unsettle" || action === "pin" |
| 435 | ? { settledOverride: "settled" as const, settledAt: NOW } |
| 436 | : {}), |
| 437 | ...(action === "unsnooze" || action === "settle" || action === "pin" |
| 438 | ? { snoozedUntil: FUTURE, snoozedAt: NOW } |
| 439 | : {}), |
| 440 | ...(action === "unpin" || action === "settle" |
| 441 | ? { pinnedAt: NOW, pinOrderKey: "a" } |
| 442 | : {}), |
| 443 | }, |
| 444 | ], |
| 445 | }; |
| 446 | h.registry.set(source, initial); |
| 447 | const result = yield* Effect.forkChild( |
| 448 | Effect.exit( |
| 449 | h.commands[action].run(h.registry, { |
| 450 | environmentId: ENVIRONMENT_ID, |
| 451 | input: { |
| 452 | threadId: THREAD_ID, |
| 453 | commandId: CommandId.make(action), |
| 454 | reason: "user", |
| 455 | enabled: false, |
| 456 | orderKey: "a", |
| 457 | snoozedUntil: "2099-01-01T00:00:00.000Z", |
| 458 | ...input, |
| 459 | }, |
| 460 | }), |
| 461 | ), |
| 462 | { startImmediately: true }, |
| 463 | ); |
| 464 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject(expected); |
| 465 | const request = yield* Queue.take(h.requests); |
| 466 | expect(h.registry.get(source)).toBe(initial); |
| 467 | yield* Deferred.fail(request.reply, new Error("Remote rejected the action")); |
| 468 | expect((yield* Fiber.join(result))._tag).toBe("Failure"); |
| 469 | expect(h.registry.get(h.visibleAtom)).toBe(initial); |
| 470 | }), |
| 471 | ); |
| 472 | |
| 473 | it.effect("restamps snoozedAt when re-snoozing to the same wake time", () => |
| 474 | Effect.gen(function* () { |
| 475 | const h = yield* makeHarness(); |
| 476 | const earlierSnoozedAt = DateTime.makeUnsafe("2000-01-01T00:00:00.000Z"); |
| 477 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), { |
| 478 | ...SNAPSHOT, |
| 479 | threads: [{ ...SNAPSHOT.threads[0]!, snoozedUntil: FUTURE, snoozedAt: earlierSnoozedAt }], |
| 480 | }); |
| 481 | const result = yield* Effect.forkChild( |
| 482 | Effect.exit( |
| 483 | h.commands.snooze.run(h.registry, { |
| 484 | environmentId: ENVIRONMENT_ID, |
| 485 | input: { threadId: THREAD_ID, snoozedUntil: DateTime.formatIso(FUTURE) }, |
| 486 | }), |
| 487 | ), |
| 488 | { startImmediately: true }, |
| 489 | ); |
| 490 | const snoozedAt = h.registry.get(h.visibleAtom)?.threads[0]?.snoozedAt; |
| 491 | expect(DateTime.toEpochMillis(snoozedAt!)).toBeGreaterThan( |
| 492 | DateTime.toEpochMillis(earlierSnoozedAt), |
| 493 | ); |
| 494 | const request = yield* Queue.take(h.requests); |
| 495 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 496 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 497 | }), |
| 498 | ); |
| 499 | |
| 500 | it.effect("keeps the preview after acknowledgement until the matching shell update arrives", () => |
| 501 | Effect.gen(function* () { |
| 502 | const h = yield* makeHarness(); |
| 503 | const result = yield* Effect.forkChild( |
| 504 | Effect.exit( |
| 505 | h.commands.settle.run(h.registry, { |
| 506 | environmentId: ENVIRONMENT_ID, |
| 507 | input: { threadId: THREAD_ID }, |
| 508 | }), |
| 509 | ), |
| 510 | { startImmediately: true }, |
| 511 | ); |
| 512 | const request = yield* Queue.take(h.requests); |
| 513 | yield* Deferred.succeed(request.reply, { sequence: 3 }); |
| 514 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 515 | const changed = { |
| 516 | ...SNAPSHOT, |
| 517 | snapshotSequence: 2, |
| 518 | threads: [{ ...SNAPSHOT.threads[0]!, title: "Renamed remotely" }], |
| 519 | }; |
| 520 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), changed); |
| 521 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject({ |
| 522 | title: "Renamed remotely", |
| 523 | settledOverride: "settled", |
| 524 | }); |
| 525 | const confirmed = { |
| 526 | ...changed, |
| 527 | snapshotSequence: 3, |
| 528 | threads: [ |
| 529 | { |
| 530 | ...changed.threads[0]!, |
| 531 | settledOverride: "settled" as const, |
| 532 | settledAt: DateTime.makeUnsafe("2026-09-12T12:00:00.000Z"), |
| 533 | }, |
| 534 | ], |
| 535 | }; |
| 536 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), confirmed); |
| 537 | expect(h.registry.get(h.visibleAtom)).toBe(confirmed); |
| 538 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), { ...SNAPSHOT, snapshotSequence: 4 }); |
| 539 | expect(h.registry.get(h.visibleAtom)?.threads[0]?.settledOverride).toBeNull(); |
| 540 | }), |
| 541 | ); |
| 542 | |
| 543 | it.effect( |
| 544 | "shows a queued reverse action immediately and preserves it if the earlier action fails", |
| 545 | () => |
| 546 | Effect.gen(function* () { |
| 547 | const h = yield* makeHarness(); |
| 548 | const settle = yield* Effect.forkChild( |
| 549 | Effect.exit( |
| 550 | h.commands.settle.run(h.registry, { |
| 551 | environmentId: ENVIRONMENT_ID, |
| 552 | input: { threadId: THREAD_ID }, |
| 553 | }), |
| 554 | ), |
| 555 | { startImmediately: true }, |
| 556 | ); |
| 557 | const first = yield* Queue.take(h.requests); |
| 558 | const unsettle = yield* Effect.forkChild( |
| 559 | Effect.exit( |
| 560 | h.commands.unsettle.run(h.registry, { |
| 561 | environmentId: ENVIRONMENT_ID, |
| 562 | input: { threadId: THREAD_ID, reason: "user" }, |
| 563 | }), |
| 564 | ), |
| 565 | { startImmediately: true }, |
| 566 | ); |
| 567 | expect(h.registry.get(h.visibleAtom)?.threads[0]?.settledOverride).toBe("active"); |
| 568 | yield* Deferred.fail(first.reply, new Error("Settle rejected")); |
| 569 | yield* Fiber.join(settle); |
| 570 | expect(h.registry.get(h.visibleAtom)?.threads[0]?.settledOverride).toBe("active"); |
| 571 | const second = yield* Queue.take(h.requests); |
| 572 | expect(second.command.type).toBe("thread.unsettle"); |
| 573 | const confirmed = { |
| 574 | ...SNAPSHOT, |
| 575 | snapshotSequence: 2, |
| 576 | threads: [{ ...SNAPSHOT.threads[0]!, settledOverride: "active" as const }], |
| 577 | }; |
| 578 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), confirmed); |
| 579 | yield* Deferred.succeed(second.reply, { sequence: 2 }); |
| 580 | yield* Fiber.join(unsettle); |
| 581 | expect(h.registry.get(h.visibleAtom)).toBe(confirmed); |
| 582 | }), |
| 583 | ); |
| 584 | |
| 585 | it.effect("isolates environments and does not restore a remotely removed thread", () => |
| 586 | Effect.gen(function* () { |
| 587 | const h = yield* makeHarness(); |
| 588 | const otherEnvironment = EnvironmentId.make("other-remote"); |
| 589 | const result = yield* Effect.forkChild( |
| 590 | Effect.exit( |
| 591 | h.commands.settle.run(h.registry, { |
| 592 | environmentId: ENVIRONMENT_ID, |
| 593 | input: { threadId: THREAD_ID }, |
| 594 | }), |
| 595 | ), |
| 596 | { startImmediately: true }, |
| 597 | ); |
| 598 | const request = yield* Queue.take(h.requests); |
| 599 | expect(h.registry.get(h.commands.snapshotAtom(otherEnvironment))).toBe(SNAPSHOT); |
| 600 | expect(h.registry.get(h.commands.rowActionsAtom(ENVIRONMENT_ID))).toBe(1); |
| 601 | expect(h.registry.get(h.commands.rowActionsAtom(otherEnvironment))).toBe(0); |
| 602 | const removed = { ...SNAPSHOT, snapshotSequence: 2, threads: [] }; |
| 603 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), removed); |
| 604 | expect(h.registry.get(h.visibleAtom)?.threads).toEqual([]); |
| 605 | yield* Deferred.fail(request.reply, new Error("Thread removed")); |
| 606 | yield* Fiber.join(result); |
| 607 | expect(h.registry.get(h.visibleAtom)).toBe(removed); |
| 608 | }), |
| 609 | ); |
| 610 | |
| 611 | const undoableActions = ["settle", "snooze"] as const; |
| 612 | |
| 613 | it.effect.each(undoableActions)("restores a confirmed %s when a queued undo fails", (action) => |
| 614 | Effect.gen(function* () { |
| 615 | const h = yield* makeHarness(); |
| 616 | const parked = |
| 617 | action === "settle" ? { settledOverride: "settled" as const } : { snoozedUntil: FUTURE }; |
| 618 | const awake = action === "settle" ? { settledOverride: "active" } : { snoozedUntil: null }; |
| 619 | const result = yield* Effect.forkChild( |
| 620 | Effect.exit( |
| 621 | h.commands[action].run(h.registry, { |
| 622 | environmentId: ENVIRONMENT_ID, |
| 623 | input: { threadId: THREAD_ID, snoozedUntil: "2099-01-01T00:00:00.000Z" }, |
| 624 | }), |
| 625 | ), |
| 626 | { startImmediately: true }, |
| 627 | ); |
| 628 | const first = yield* Queue.take(h.requests); |
| 629 | const undo = yield* Effect.forkChild( |
| 630 | Effect.exit( |
| 631 | h.commands[action === "settle" ? "unsettle" : "unsnooze"].run(h.registry, { |
| 632 | environmentId: ENVIRONMENT_ID, |
| 633 | input: { threadId: THREAD_ID, reason: "user" }, |
| 634 | }), |
| 635 | ), |
| 636 | { startImmediately: true }, |
| 637 | ); |
| 638 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject(awake); |
| 639 | yield* Deferred.succeed(first.reply, { sequence: 2 }); |
| 640 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 641 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject(awake); |
| 642 | const confirmed = { |
| 643 | ...SNAPSHOT, |
| 644 | snapshotSequence: 2, |
| 645 | threads: [{ ...SNAPSHOT.threads[0]!, ...parked }], |
| 646 | }; |
| 647 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), confirmed); |
| 648 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject(awake); |
| 649 | const second = yield* Queue.take(h.requests); |
| 650 | expect(second.command.type).toBe(action === "settle" ? "thread.unsettle" : "thread.unsnooze"); |
| 651 | yield* Deferred.fail(second.reply, new Error("Undo rejected")); |
| 652 | expect((yield* Fiber.join(undo))._tag).toBe("Failure"); |
| 653 | expect(h.registry.get(h.visibleAtom)).toBe(confirmed); |
| 654 | }), |
| 655 | ); |
| 656 | |
| 657 | it.effect.each(undoableActions)( |
| 658 | "preserves a newer approval when the %s reply arrives after the shell", |
| 659 | (action) => |
| 660 | Effect.gen(function* () { |
| 661 | const h = yield* makeHarness(); |
| 662 | const result = yield* Effect.forkChild( |
| 663 | Effect.exit( |
| 664 | h.commands[action].run(h.registry, { |
| 665 | environmentId: ENVIRONMENT_ID, |
| 666 | input: { threadId: THREAD_ID, snoozedUntil: "2099-01-01T00:00:00.000Z" }, |
| 667 | }), |
| 668 | ), |
| 669 | { startImmediately: true }, |
| 670 | ); |
| 671 | const request = yield* Queue.take(h.requests); |
| 672 | const newer = { |
| 673 | ...SNAPSHOT, |
| 674 | snapshotSequence: 3, |
| 675 | threads: [{ ...SNAPSHOT.threads[0]!, pendingRuntimeRequest: APPROVAL }], |
| 676 | }; |
| 677 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), newer); |
| 678 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toBe(newer.threads[0]); |
| 679 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 680 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 681 | expect(h.registry.get(h.visibleAtom)).toBe(newer); |
| 682 | }), |
| 683 | ); |
| 684 | |
| 685 | it.effect.each(undoableActions)( |
| 686 | "shows an accepted %s while the shell still has an old input request", |
| 687 | (action) => |
| 688 | Effect.gen(function* () { |
| 689 | const h = yield* makeHarness(); |
| 690 | const stale = { |
| 691 | ...SNAPSHOT, |
| 692 | threads: [{ ...SNAPSHOT.threads[0]!, pendingRuntimeRequest: USER_INPUT }], |
| 693 | }; |
| 694 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), stale); |
| 695 | const result = yield* Effect.forkChild( |
| 696 | Effect.exit( |
| 697 | h.commands[action].run(h.registry, { |
| 698 | environmentId: ENVIRONMENT_ID, |
| 699 | input: { threadId: THREAD_ID, snoozedUntil: "2099-01-01T00:00:00.000Z" }, |
| 700 | }), |
| 701 | ), |
| 702 | { startImmediately: true }, |
| 703 | ); |
| 704 | const request = yield* Queue.take(h.requests); |
| 705 | if (action === "settle") { |
| 706 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toBe(stale.threads[0]); |
| 707 | } else { |
| 708 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject({ |
| 709 | snoozedUntil: FUTURE, |
| 710 | pendingRuntimeRequest: USER_INPUT, |
| 711 | }); |
| 712 | } |
| 713 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 714 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 715 | expect(h.registry.get(h.visibleAtom)?.threads[0]).toMatchObject( |
| 716 | action === "settle" ? { settledOverride: "settled" } : { snoozedUntil: FUTURE }, |
| 717 | ); |
| 718 | expect(h.registry.get(h.visibleAtom)?.threads[0]?.pendingRuntimeRequest).toEqual( |
| 719 | action === "settle" ? null : USER_INPUT, |
| 720 | ); |
| 721 | expect(h.registry.get(h.snapshotAtom(ENVIRONMENT_ID))).toBe(stale); |
| 722 | }), |
| 723 | ); |
| 724 | it.effect("acknowledges a new question when snoozing again to the same wake time", () => |
| 725 | Effect.gen(function* () { |
| 726 | const h = yield* makeHarness(); |
| 727 | const initial = { |
| 728 | ...SNAPSHOT, |
| 729 | threads: [ |
| 730 | { |
| 731 | ...SNAPSHOT.threads[0]!, |
| 732 | pendingRuntimeRequest: USER_INPUT, |
| 733 | snoozedAt: DateTime.makeUnsafe("2026-09-12T09:00:00.000Z"), |
| 734 | snoozedUntil: FUTURE, |
| 735 | }, |
| 736 | ], |
| 737 | }; |
| 738 | h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), initial); |
| 739 | const result = yield* Effect.forkChild( |
| 740 | Effect.exit( |
| 741 | h.commands.snooze.run(h.registry, { |
| 742 | environmentId: ENVIRONMENT_ID, |
| 743 | input: { threadId: THREAD_ID, snoozedUntil: "2099-01-01T00:00:00.000Z" }, |
| 744 | }), |
| 745 | ), |
| 746 | { startImmediately: true }, |
| 747 | ); |
| 748 | const request = yield* Queue.take(h.requests); |
| 749 | const snoozed = h.registry.get(h.visibleAtom)?.threads[0]; |
| 750 | expect(DateTime.toEpochMillis(snoozed!.snoozedAt!)).toBeGreaterThanOrEqual( |
| 751 | DateTime.toEpochMillis(USER_INPUT.createdAt), |
| 752 | ); |
| 753 | expect(snoozed?.pendingRuntimeRequest).toEqual(USER_INPUT); |
| 754 | yield* Deferred.succeed(request.reply, { sequence: 2 }); |
| 755 | expect((yield* Fiber.join(result))._tag).toBe("Success"); |
| 756 | }), |
| 757 | ); |
| 758 | }); |