The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | ProjectId, |
| 4 | RunId, |
| 5 | RuntimeRequestId, |
| 6 | ThreadId, |
| 7 | type OrchestrationV2ShellSnapshot, |
| 8 | type OrchestrationV2ThreadShell, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import * as DateTime from "effect/DateTime"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 13 | import { describe, expect, it } from "vite-plus/test"; |
| 14 | |
| 15 | import { PrimaryConnectionTarget } from "../connection/model.ts"; |
| 16 | import { v2Now, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts"; |
| 17 | import { applyShellStreamEvent } from "./shellReducer.ts"; |
| 18 | import { isThreadWorking } from "./threadInbox.ts"; |
| 19 | import { |
| 20 | childThreadInputsTitle, |
| 21 | createEnvironmentThreadShellAtoms, |
| 22 | resolveListedAncestorThreadRef, |
| 23 | } from "./threadShell.ts"; |
| 24 | |
| 25 | const environmentId = EnvironmentId.make("environment-v2"); |
| 26 | const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2"); |
| 27 | const otherProjectId = ProjectId.make("other-project"); |
| 28 | |
| 29 | function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) { |
| 30 | const snapshotAtom = Atom.family((_environmentId: EnvironmentId) => |
| 31 | Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot), |
| 32 | ); |
| 33 | const catalogValueAtom = Atom.make({ |
| 34 | isReady: true, |
| 35 | entries: new Map( |
| 36 | environmentIds.map((id) => [ |
| 37 | id, |
| 38 | { |
| 39 | target: new PrimaryConnectionTarget({ |
| 40 | environmentId: id, |
| 41 | label: "Environment", |
| 42 | httpBaseUrl: "https://example.test", |
| 43 | wsBaseUrl: "wss://example.test", |
| 44 | }), |
| 45 | profile: Option.none(), |
| 46 | enabled: true, |
| 47 | }, |
| 48 | ]), |
| 49 | ), |
| 50 | }); |
| 51 | return { |
| 52 | registry: AtomRegistry.make(), |
| 53 | snapshotAtom, |
| 54 | catalogValueAtom, |
| 55 | threads: createEnvironmentThreadShellAtoms({ |
| 56 | catalogValueAtom, |
| 57 | snapshotAtom, |
| 58 | hiddenThreadTagsAtom: () => Atom.make(null), |
| 59 | }), |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | describe("v2 thread shell lists", () => { |
| 64 | it("preserves ordered reference arrays when a middle thread changes", () => { |
| 65 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 66 | const snapshot = { |
| 67 | ...v2ShellSnapshot, |
| 68 | threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })), |
| 69 | }; |
| 70 | registry.set(snapshotAtom(environmentId), snapshot); |
| 71 | const dispose = registry.mount(threads.threadRefsAtom); |
| 72 | const before = registry.get(threads.threadRefsAtom); |
| 73 | registry.set( |
| 74 | snapshotAtom(environmentId), |
| 75 | applyShellStreamEvent(snapshot, { |
| 76 | kind: "thread.updated", |
| 77 | location: "active", |
| 78 | sequence: 1, |
| 79 | thread: { ...snapshot.threads[1]!, title: "Updated" }, |
| 80 | }), |
| 81 | ); |
| 82 | expect(registry.get(threads.threadRefsAtom)).toBe(before); |
| 83 | dispose(); |
| 84 | registry.dispose(); |
| 85 | }); |
| 86 | |
| 87 | it("keeps navigation stable on hidden subagent updates and retains user forks", () => { |
| 88 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 89 | const root = v2ThreadShell; |
| 90 | const child = { |
| 91 | ...root, |
| 92 | id: ThreadId.make("child"), |
| 93 | lineage: { |
| 94 | ...root.lineage, |
| 95 | parentThreadId: root.id, |
| 96 | relationshipToParent: "subagent" as const, |
| 97 | }, |
| 98 | }; |
| 99 | const fork = { |
| 100 | ...child, |
| 101 | id: ThreadId.make("fork"), |
| 102 | lineage: { ...child.lineage, relationshipToParent: "fork" as const }, |
| 103 | }; |
| 104 | const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] }; |
| 105 | registry.set(snapshotAtom(environmentId), snapshot); |
| 106 | const dispose = registry.mount(threads.navigationThreadShellsAtom); |
| 107 | const before = registry.get(threads.navigationThreadShellsAtom); |
| 108 | expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]); |
| 109 | registry.set(snapshotAtom(environmentId), { |
| 110 | ...snapshot, |
| 111 | threads: [root, { ...child, title: "Child streaming" }, fork], |
| 112 | }); |
| 113 | expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before); |
| 114 | expect(registry.get(threads.threadShellsAtom)).toHaveLength(3); |
| 115 | dispose(); |
| 116 | registry.dispose(); |
| 117 | }); |
| 118 | |
| 119 | it("keeps a live subagent question inside the parent without flagging its thread", () => { |
| 120 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 121 | const root = v2ThreadShell; |
| 122 | const child = { |
| 123 | ...root, |
| 124 | id: ThreadId.make("child"), |
| 125 | creationSource: "mcp" as const, |
| 126 | status: "running" as const, |
| 127 | activityRunStatus: "running" as const, |
| 128 | lineage: { |
| 129 | ...root.lineage, |
| 130 | parentThreadId: root.id, |
| 131 | relationshipToParent: "subagent" as const, |
| 132 | }, |
| 133 | pendingRuntimeRequest: { |
| 134 | id: RuntimeRequestId.make("question"), |
| 135 | kind: "user_input" as const, |
| 136 | createdAt: v2Now, |
| 137 | }, |
| 138 | }; |
| 139 | const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id }); |
| 140 | const dispose = registry.mount(inputs); |
| 141 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] }); |
| 142 | expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]); |
| 143 | expect( |
| 144 | registry.get(threads.threadShellAtom({ environmentId, threadId: root.id })) |
| 145 | ?.pendingSubagentRequest, |
| 146 | ).toBeNull(); |
| 147 | registry.set(snapshotAtom(environmentId), { |
| 148 | ...v2ShellSnapshot, |
| 149 | threads: [root, { ...child, status: "queued" as const }], |
| 150 | }); |
| 151 | expect(registry.get(inputs).map((thread) => thread.id)).toEqual([child.id]); |
| 152 | registry.set(snapshotAtom(environmentId), { |
| 153 | ...v2ShellSnapshot, |
| 154 | threads: [root, { ...child, status: "completed" as const, activityRunStatus: null }], |
| 155 | }); |
| 156 | expect(registry.get(inputs)).toEqual([]); |
| 157 | const { activityRunStatus: _omitted, ...legacyChild } = child; |
| 158 | registry.set(snapshotAtom(environmentId), { |
| 159 | ...v2ShellSnapshot, |
| 160 | threads: [root, { ...legacyChild, status: "completed" as const }], |
| 161 | }); |
| 162 | expect(registry.get(inputs)).toEqual([]); |
| 163 | dispose(); |
| 164 | registry.dispose(); |
| 165 | }); |
| 166 | |
| 167 | it("flags the parent while a live subagent waits on a secret card", () => { |
| 168 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 169 | const root = v2ThreadShell; |
| 170 | const child = { |
| 171 | ...root, |
| 172 | id: ThreadId.make("child"), |
| 173 | creationSource: "mcp" as const, |
| 174 | status: "running" as const, |
| 175 | activityRunStatus: "running" as const, |
| 176 | lineage: { |
| 177 | ...root.lineage, |
| 178 | parentThreadId: root.id, |
| 179 | relationshipToParent: "subagent" as const, |
| 180 | }, |
| 181 | pendingRuntimeRequest: { |
| 182 | id: RuntimeRequestId.make("secret-card"), |
| 183 | kind: "user_input" as const, |
| 184 | createdAt: v2Now, |
| 185 | blocking: true, |
| 186 | userOnly: true, |
| 187 | }, |
| 188 | }; |
| 189 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, child] }); |
| 190 | expect( |
| 191 | registry.get(threads.threadShellAtom({ environmentId, threadId: root.id })) |
| 192 | ?.pendingSubagentRequest, |
| 193 | ).toBe("user_input"); |
| 194 | registry.dispose(); |
| 195 | }); |
| 196 | |
| 197 | it("rolls a side chat's questions and approvals up to its parent row", () => { |
| 198 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 199 | const root = v2ThreadShell; |
| 200 | const sideChat = { |
| 201 | ...root, |
| 202 | id: ThreadId.make("side-chat"), |
| 203 | status: "running" as const, |
| 204 | activityRunStatus: "running" as const, |
| 205 | latestRunId: RunId.make("side-chat-run"), |
| 206 | lineage: { |
| 207 | ...root.lineage, |
| 208 | parentThreadId: root.id, |
| 209 | relationshipToParent: "fork" as const, |
| 210 | sideChat: true, |
| 211 | }, |
| 212 | }; |
| 213 | const request = { id: RuntimeRequestId.make("ask"), createdAt: v2Now }; |
| 214 | const parentRef = { environmentId, threadId: root.id }; |
| 215 | const parent = threads.threadShellAtom(parentRef); |
| 216 | const inputs = threads.childThreadInputsAtom(parentRef); |
| 217 | const disposeParent = registry.mount(parent); |
| 218 | const disposeInputs = registry.mount(inputs); |
| 219 | const disposeNavigation = registry.mount(threads.navigationThreadShellsAtom); |
| 220 | const withRequest = ( |
| 221 | kind: "user_input" | "command", |
| 222 | options: { readonly blocking?: boolean } = {}, |
| 223 | ) => |
| 224 | registry.set(snapshotAtom(environmentId), { |
| 225 | ...v2ShellSnapshot, |
| 226 | threads: [ |
| 227 | root, |
| 228 | { |
| 229 | ...sideChat, |
| 230 | pendingRuntimeRequest: { |
| 231 | ...request, |
| 232 | kind, |
| 233 | ...(options.blocking === undefined ? {} : { blocking: options.blocking }), |
| 234 | }, |
| 235 | }, |
| 236 | ], |
| 237 | }); |
| 238 | |
| 239 | withRequest("user_input"); |
| 240 | expect(registry.get(parent)?.pendingSubagentRequest).toBe("user_input"); |
| 241 | expect(childThreadInputsTitle(registry.get(inputs))).toBe("Side chat needs input"); |
| 242 | expect(registry.get(threads.navigationThreadShellsAtom).map((thread) => thread.id)).toEqual([ |
| 243 | root.id, |
| 244 | ]); |
| 245 | |
| 246 | withRequest("user_input", { blocking: false }); |
| 247 | expect(registry.get(parent)?.pendingSubagentRequest).toBe("user_input"); |
| 248 | |
| 249 | withRequest("command"); |
| 250 | expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval"); |
| 251 | expect(childThreadInputsTitle(registry.get(inputs))).toBe("Side chat needs approval"); |
| 252 | |
| 253 | const helper = { |
| 254 | ...sideChat, |
| 255 | id: ThreadId.make("side-chat-helper"), |
| 256 | creationSource: "mcp" as const, |
| 257 | lineage: { |
| 258 | ...sideChat.lineage, |
| 259 | parentThreadId: sideChat.id, |
| 260 | relationshipToParent: "subagent" as const, |
| 261 | sideChat: false, |
| 262 | }, |
| 263 | pendingRuntimeRequest: { ...request, kind: "command" as const }, |
| 264 | }; |
| 265 | const sideChats = threads.sideChatsAtom(parentRef); |
| 266 | const disposeSideChats = registry.mount(sideChats); |
| 267 | registry.set(snapshotAtom(environmentId), { |
| 268 | ...v2ShellSnapshot, |
| 269 | threads: [root, sideChat, helper], |
| 270 | }); |
| 271 | expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval"); |
| 272 | expect( |
| 273 | registry.get(sideChats).map((thread) => [thread.id, thread.pendingSubagentRequest]), |
| 274 | ).toEqual([[sideChat.id, "approval"]]); |
| 275 | |
| 276 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root, sideChat] }); |
| 277 | expect(registry.get(parent)?.pendingSubagentRequest).toBeNull(); |
| 278 | expect(registry.get(sideChats).map((thread) => thread.pendingSubagentRequest)).toEqual([null]); |
| 279 | expect(registry.get(inputs)).toEqual([]); |
| 280 | disposeSideChats(); |
| 281 | disposeNavigation(); |
| 282 | disposeInputs(); |
| 283 | disposeParent(); |
| 284 | registry.dispose(); |
| 285 | }); |
| 286 | |
| 287 | it("brings a parent back from Working while a nested subagent waits for approval", () => { |
| 288 | const { registry, threads, snapshotAtom } = makeHarness(); |
| 289 | const child = { |
| 290 | ...v2ThreadShell, |
| 291 | id: ThreadId.make("review"), |
| 292 | creationSource: "mcp" as const, |
| 293 | status: "running" as const, |
| 294 | activityRunStatus: "running" as const, |
| 295 | lineage: { |
| 296 | ...v2ThreadShell.lineage, |
| 297 | parentThreadId: v2ThreadShell.id, |
| 298 | relationshipToParent: "subagent" as const, |
| 299 | }, |
| 300 | }; |
| 301 | const grandchild = { |
| 302 | ...child, |
| 303 | id: ThreadId.make("review-helper"), |
| 304 | lineage: { ...child.lineage, parentThreadId: child.id }, |
| 305 | }; |
| 306 | const root = { |
| 307 | ...v2ThreadShell, |
| 308 | latestRunId: RunId.make("root-run"), |
| 309 | status: "completed" as const, |
| 310 | pendingBackgroundTasks: [ |
| 311 | { taskId: "review", kind: "subagent" as const, childThreadId: child.id }, |
| 312 | ], |
| 313 | }; |
| 314 | const approval = { |
| 315 | id: RuntimeRequestId.make("cat-file"), |
| 316 | kind: "command" as const, |
| 317 | createdAt: v2Now, |
| 318 | }; |
| 319 | const parent = threads.threadShellAtom({ environmentId, threadId: root.id }); |
| 320 | const inputs = threads.childThreadInputsAtom({ environmentId, threadId: root.id }); |
| 321 | const disposeParent = registry.mount(parent); |
| 322 | const disposeInputs = registry.mount(inputs); |
| 323 | registry.set(snapshotAtom(environmentId), { |
| 324 | ...v2ShellSnapshot, |
| 325 | threads: [root, child, grandchild], |
| 326 | }); |
| 327 | expect(isThreadWorking(registry.get(parent)!)).toBe(true); |
| 328 | |
| 329 | registry.set(snapshotAtom(environmentId), { |
| 330 | ...v2ShellSnapshot, |
| 331 | threads: [root, child, { ...grandchild, pendingRuntimeRequest: approval }], |
| 332 | }); |
| 333 | expect(registry.get(parent)?.pendingSubagentRequest).toBe("approval"); |
| 334 | expect(isThreadWorking(registry.get(parent)!)).toBe(false); |
| 335 | const asking = registry.get(inputs); |
| 336 | expect(asking.map((thread) => thread.id)).toEqual([grandchild.id]); |
| 337 | expect(childThreadInputsTitle(asking)).toBe("Subagent needs approval"); |
| 338 | |
| 339 | registry.set(snapshotAtom(environmentId), { |
| 340 | ...v2ShellSnapshot, |
| 341 | threads: [root, child, grandchild], |
| 342 | }); |
| 343 | expect(registry.get(parent)?.pendingSubagentRequest).toBeNull(); |
| 344 | expect(isThreadWorking(registry.get(parent)!)).toBe(true); |
| 345 | disposeInputs(); |
| 346 | disposeParent(); |
| 347 | registry.dispose(); |
| 348 | }); |
| 349 | |
| 350 | it("lists a task a linked environment runs for a listed thread under that thread", () => { |
| 351 | const { registry, threads, snapshotAtom, catalogValueAtom } = makeHarness([ |
| 352 | environmentId, |
| 353 | remoteEnvironmentId, |
| 354 | ]); |
| 355 | const parent = v2ThreadShell; |
| 356 | const task = { |
| 357 | ...v2ThreadShell, |
| 358 | id: ThreadId.make("remote-task"), |
| 359 | creationSource: "mcp" as const, |
| 360 | status: "running" as const, |
| 361 | activityRunStatus: "running" as const, |
| 362 | delegatedFrom: { environmentId, threadId: parent.id, title: parent.title }, |
| 363 | }; |
| 364 | const helper = { |
| 365 | ...task, |
| 366 | id: ThreadId.make("remote-helper"), |
| 367 | delegatedFrom: undefined, |
| 368 | lineage: { |
| 369 | ...task.lineage, |
| 370 | parentThreadId: task.id, |
| 371 | relationshipToParent: "subagent" as const, |
| 372 | }, |
| 373 | }; |
| 374 | const approval = { |
| 375 | id: RuntimeRequestId.make("cat-file"), |
| 376 | kind: "command" as const, |
| 377 | createdAt: v2Now, |
| 378 | }; |
| 379 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [parent] }); |
| 380 | registry.set(snapshotAtom(remoteEnvironmentId), { |
| 381 | ...v2ShellSnapshot, |
| 382 | threads: [task, helper], |
| 383 | }); |
| 384 | const parentRef = { environmentId, threadId: parent.id }; |
| 385 | const inputs = threads.childThreadInputsAtom(parentRef); |
| 386 | const disposeNavigation = registry.mount(threads.navigationThreadShellsAtom); |
| 387 | const disposeParent = registry.mount(threads.threadShellAtom(parentRef)); |
| 388 | const disposeInputs = registry.mount(inputs); |
| 389 | const listed = () => |
| 390 | registry |
| 391 | .get(threads.navigationThreadShellsAtom) |
| 392 | .map((thread) => `${thread.environmentId}/${thread.id}`); |
| 393 | expect(listed()).toEqual([`${environmentId}/${parent.id}`]); |
| 394 | |
| 395 | // The helper the task started there asks for approval: the parent here hears of it. |
| 396 | registry.set(snapshotAtom(remoteEnvironmentId), { |
| 397 | ...v2ShellSnapshot, |
| 398 | threads: [task, { ...helper, pendingRuntimeRequest: approval }], |
| 399 | }); |
| 400 | expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBe( |
| 401 | "approval", |
| 402 | ); |
| 403 | expect(registry.get(inputs).map((thread) => `${thread.environmentId}/${thread.id}`)).toEqual([ |
| 404 | `${remoteEnvironmentId}/${helper.id}`, |
| 405 | ]); |
| 406 | |
| 407 | registry.set(snapshotAtom(remoteEnvironmentId), { |
| 408 | ...v2ShellSnapshot, |
| 409 | threads: [ |
| 410 | task, |
| 411 | { ...helper, pendingRuntimeRequest: { ...approval, kind: "user_input" as const } }, |
| 412 | ], |
| 413 | }); |
| 414 | expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBeNull(); |
| 415 | expect(registry.get(inputs).map((thread) => thread.id)).toEqual([helper.id]); |
| 416 | |
| 417 | // A secret card needs the user, so it still reaches the parent here. |
| 418 | registry.set(snapshotAtom(remoteEnvironmentId), { |
| 419 | ...v2ShellSnapshot, |
| 420 | threads: [ |
| 421 | task, |
| 422 | { |
| 423 | ...helper, |
| 424 | pendingRuntimeRequest: { ...approval, kind: "user_input" as const, userOnly: true }, |
| 425 | }, |
| 426 | ], |
| 427 | }); |
| 428 | expect(registry.get(threads.threadShellAtom(parentRef))?.pendingSubagentRequest).toBe( |
| 429 | "user_input", |
| 430 | ); |
| 431 | |
| 432 | // A task whose parent is gone there is listed on its own, as is one whose |
| 433 | // parent's environment is switched off. |
| 434 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [] }); |
| 435 | expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]); |
| 436 | // Archiving the parent here leaves the task there unarchived, so it is listed on its own. |
| 437 | registry.set(snapshotAtom(environmentId), { |
| 438 | ...v2ShellSnapshot, |
| 439 | threads: [{ ...parent, archivedAt: v2Now }], |
| 440 | }); |
| 441 | expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]); |
| 442 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [parent] }); |
| 443 | expect(listed()).toEqual([`${environmentId}/${parent.id}`]); |
| 444 | const catalog = registry.get(catalogValueAtom); |
| 445 | const entries = new Map(catalog.entries); |
| 446 | entries.set(environmentId, { ...entries.get(environmentId)!, enabled: false }); |
| 447 | registry.set(catalogValueAtom, { ...catalog, entries }); |
| 448 | expect(listed()).toEqual([`${remoteEnvironmentId}/${task.id}`]); |
| 449 | disposeInputs(); |
| 450 | disposeParent(); |
| 451 | disposeNavigation(); |
| 452 | registry.dispose(); |
| 453 | }); |
| 454 | |
| 455 | it("brings an approval up a chain of tasks across three environments to its root", () => { |
| 456 | const thirdEnvironmentId = EnvironmentId.make("third-environment-v2"); |
| 457 | const { registry, threads, snapshotAtom } = makeHarness([ |
| 458 | environmentId, |
| 459 | remoteEnvironmentId, |
| 460 | thirdEnvironmentId, |
| 461 | ]); |
| 462 | const root = v2ThreadShell; |
| 463 | const running = { |
| 464 | creationSource: "mcp" as const, |
| 465 | status: "running" as const, |
| 466 | activityRunStatus: "running" as const, |
| 467 | }; |
| 468 | const task = { |
| 469 | ...v2ThreadShell, |
| 470 | ...running, |
| 471 | id: ThreadId.make("remote-task"), |
| 472 | delegatedFrom: { environmentId, threadId: root.id, title: root.title }, |
| 473 | }; |
| 474 | const nestedTask = { |
| 475 | ...v2ThreadShell, |
| 476 | ...running, |
| 477 | id: ThreadId.make("third-task"), |
| 478 | delegatedFrom: { environmentId: remoteEnvironmentId, threadId: task.id, title: task.title }, |
| 479 | }; |
| 480 | // A task the middle one delegates back to the root's own environment. |
| 481 | const returningTask = { |
| 482 | ...nestedTask, |
| 483 | id: ThreadId.make("returning-task"), |
| 484 | }; |
| 485 | const approval = { |
| 486 | id: RuntimeRequestId.make("cat-file"), |
| 487 | kind: "command" as const, |
| 488 | createdAt: v2Now, |
| 489 | }; |
| 490 | registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [root] }); |
| 491 | registry.set(snapshotAtom(remoteEnvironmentId), { ...v2ShellSnapshot, threads: [task] }); |
| 492 | registry.set(snapshotAtom(thirdEnvironmentId), { |
| 493 | ...v2ShellSnapshot, |
| 494 | threads: [{ ...nestedTask, pendingRuntimeRequest: approval }], |
| 495 | }); |
| 496 | const rootRef = { environmentId, threadId: root.id }; |
| 497 | const taskRef = { environmentId: remoteEnvironmentId, threadId: task.id }; |
| 498 | const disposeRoot = registry.mount(threads.threadShellAtom(rootRef)); |
| 499 | const disposeTask = registry.mount(threads.threadShellAtom(taskRef)); |
| 500 | expect(registry.get(threads.threadShellAtom(taskRef))?.pendingSubagentRequest).toBe("approval"); |
| 501 | expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBe("approval"); |
| 502 | |
| 503 | // Once it is answered, neither stays flagged. |
| 504 | registry.set(snapshotAtom(thirdEnvironmentId), { |
| 505 | ...v2ShellSnapshot, |
| 506 | threads: [nestedTask], |
| 507 | }); |
| 508 | expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBeNull(); |
| 509 | |
| 510 | // Environments that delegate to each other still settle on the root's flag. |
| 511 | registry.set(snapshotAtom(environmentId), { |
| 512 | ...v2ShellSnapshot, |
| 513 | threads: [root, { ...returningTask, pendingRuntimeRequest: approval }], |
| 514 | }); |
| 515 | expect(registry.get(threads.threadShellAtom(rootRef))?.pendingSubagentRequest).toBe("approval"); |
| 516 | disposeTask(); |
| 517 | disposeRoot(); |
| 518 | registry.dispose(); |
| 519 | }); |
| 520 | |
| 521 | it.each([ |
| 522 | { reason: "switched-off", state: { enabled: false } }, |
| 523 | { |
| 524 | reason: "unsupported", |
| 525 | state: { enabled: true, unsupportedReason: "Use a compatible client." }, |
| 526 | }, |
| 527 | ])("leaves $reason environments out of navigation until restored", ({ state }) => { |
| 528 | const { registry, threads, catalogValueAtom } = makeHarness([ |
| 529 | environmentId, |
| 530 | remoteEnvironmentId, |
| 531 | ]); |
| 532 | const dispose = registry.mount(threads.navigationThreadShellsAtom); |
| 533 | const environmentsOf = () => |
| 534 | new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId)); |
| 535 | expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId])); |
| 536 | |
| 537 | const setRemotePaused = (paused: boolean) => { |
| 538 | const catalog = registry.get(catalogValueAtom); |
| 539 | const entries = new Map(catalog.entries); |
| 540 | entries.set(remoteEnvironmentId, { |
| 541 | ...entries.get(remoteEnvironmentId)!, |
| 542 | ...(paused ? state : { enabled: true, unsupportedReason: undefined }), |
| 543 | }); |
| 544 | registry.set(catalogValueAtom, { ...catalog, entries }); |
| 545 | }; |
| 546 | setRemotePaused(true); |
| 547 | expect(environmentsOf()).toEqual(new Set([environmentId])); |
| 548 | setRemotePaused(false); |
| 549 | expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId])); |
| 550 | dispose(); |
| 551 | registry.dispose(); |
| 552 | }); |
| 553 | |
| 554 | it("preserves project memberships on updates and keeps the same thread separate per environment", () => { |
| 555 | const harness = makeHarness([environmentId, remoteEnvironmentId]); |
| 556 | const otherThread = { |
| 557 | ...v2ThreadShell, |
| 558 | id: ThreadId.make("other-thread"), |
| 559 | projectId: otherProjectId, |
| 560 | }; |
| 561 | const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] }; |
| 562 | harness.registry.set(harness.snapshotAtom(environmentId), snapshot); |
| 563 | const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId); |
| 564 | const listAtom = harness.threads.threadShellsForProjectRefsAtom([ |
| 565 | { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId }, |
| 566 | { environmentId, projectId: v2ThreadShell.projectId }, |
| 567 | ]); |
| 568 | const disposeList = harness.registry.mount(listAtom); |
| 569 | try { |
| 570 | const membership = harness.registry.get(membershipAtom); |
| 571 | const before = harness.registry.get(listAtom); |
| 572 | expect(before).toHaveLength(2); |
| 573 | expect(before[0]).not.toBe(before[1]); |
| 574 | expect(before.map((thread) => thread.environmentId)).toEqual([ |
| 575 | remoteEnvironmentId, |
| 576 | environmentId, |
| 577 | ]); |
| 578 | |
| 579 | harness.registry.set(harness.snapshotAtom(environmentId), { |
| 580 | ...snapshot, |
| 581 | threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }], |
| 582 | }); |
| 583 | expect(harness.registry.get(membershipAtom)).toBe(membership); |
| 584 | expect(harness.registry.get(listAtom)).toBe(before); |
| 585 | |
| 586 | harness.registry.set(harness.snapshotAtom(environmentId), { |
| 587 | ...snapshot, |
| 588 | threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread], |
| 589 | }); |
| 590 | expect(harness.registry.get(listAtom)).toEqual([before[0]]); |
| 591 | harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), { |
| 592 | ...v2ShellSnapshot, |
| 593 | threads: [], |
| 594 | }); |
| 595 | expect(harness.registry.get(listAtom)).toEqual([]); |
| 596 | } finally { |
| 597 | disposeList(); |
| 598 | harness.registry.dispose(); |
| 599 | } |
| 600 | }); |
| 601 | |
| 602 | it("keeps the quiet shell through streaming ticks and follows every change once the run stops", () => { |
| 603 | const harness = makeHarness(); |
| 604 | const runId = RunId.make("streaming-run"); |
| 605 | const running: OrchestrationV2ThreadShell = { |
| 606 | ...v2ThreadShell, |
| 607 | latestRunId: runId, |
| 608 | activeRunId: runId, |
| 609 | status: "running", |
| 610 | itemCount: 1, |
| 611 | visibleItemCount: 1, |
| 612 | }; |
| 613 | const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) => |
| 614 | harness.registry.set(harness.snapshotAtom(environmentId), { |
| 615 | ...v2ShellSnapshot, |
| 616 | snapshotSequence: sequence, |
| 617 | threads: [thread], |
| 618 | }); |
| 619 | const at = (seconds: number) => DateTime.add(v2Now, { seconds }); |
| 620 | setThread(running, 1); |
| 621 | const ref = { environmentId, threadId: v2ThreadShell.id }; |
| 622 | const quietAtom = harness.threads.quietThreadShellAtom(ref); |
| 623 | const dispose = harness.registry.mount(quietAtom); |
| 624 | try { |
| 625 | const first = harness.registry.get(quietAtom); |
| 626 | setThread({ ...running, updatedAt: at(1), itemCount: 4, visibleItemCount: 3 }, 2); |
| 627 | expect(harness.registry.get(quietAtom)).toBe(first); |
| 628 | expect(harness.registry.get(harness.threads.threadShellAtom(ref))?.itemCount).toBe(4); |
| 629 | |
| 630 | setThread({ ...running, updatedAt: at(2), itemCount: 5, title: "Renamed" }, 3); |
| 631 | const renamed = harness.registry.get(quietAtom); |
| 632 | expect(renamed?.title).toBe("Renamed"); |
| 633 | |
| 634 | const stopped: OrchestrationV2ThreadShell = { |
| 635 | ...running, |
| 636 | activeRunId: null, |
| 637 | status: "completed", |
| 638 | }; |
| 639 | setThread({ ...stopped, title: "Renamed", updatedAt: at(3) }, 4); |
| 640 | const completed = harness.registry.get(quietAtom); |
| 641 | expect(completed).not.toBe(renamed); |
| 642 | setThread({ ...stopped, title: "Renamed", updatedAt: at(4) }, 5); |
| 643 | expect(harness.registry.get(quietAtom)?.updatedAt).toBe(DateTime.formatIso(at(4))); |
| 644 | expect(harness.registry.get(quietAtom)).not.toBe(completed); |
| 645 | } finally { |
| 646 | dispose(); |
| 647 | harness.registry.dispose(); |
| 648 | } |
| 649 | }); |
| 650 | |
| 651 | it("keeps the quiet shell list through streaming ticks", () => { |
| 652 | const harness = makeHarness(); |
| 653 | const runId = RunId.make("streaming-list-run"); |
| 654 | const running: OrchestrationV2ThreadShell = { |
| 655 | ...v2ThreadShell, |
| 656 | latestRunId: runId, |
| 657 | activeRunId: runId, |
| 658 | status: "running", |
| 659 | itemCount: 1, |
| 660 | visibleItemCount: 1, |
| 661 | }; |
| 662 | const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) => |
| 663 | harness.registry.set(harness.snapshotAtom(environmentId), { |
| 664 | ...v2ShellSnapshot, |
| 665 | snapshotSequence: sequence, |
| 666 | threads: [thread], |
| 667 | }); |
| 668 | setThread(running, 1); |
| 669 | const listAtom = harness.threads.quietThreadShellsAtom; |
| 670 | const dispose = harness.registry.mount(listAtom); |
| 671 | try { |
| 672 | const first = harness.registry.get(listAtom); |
| 673 | expect(first).toHaveLength(1); |
| 674 | setThread({ ...running, updatedAt: DateTime.add(v2Now, { seconds: 1 }), itemCount: 4 }, 2); |
| 675 | expect(harness.registry.get(listAtom)).toBe(first); |
| 676 | |
| 677 | setThread({ ...running, title: "Renamed" }, 3); |
| 678 | expect(harness.registry.get(listAtom)).not.toBe(first); |
| 679 | expect(harness.registry.get(listAtom)[0]?.title).toBe("Renamed"); |
| 680 | } finally { |
| 681 | dispose(); |
| 682 | harness.registry.dispose(); |
| 683 | } |
| 684 | }); |
| 685 | }); |
| 686 | |
| 687 | describe("resolveListedAncestorThreadRef", () => { |
| 688 | type Thread = Parameters<typeof resolveListedAncestorThreadRef>[1][number]; |
| 689 | const otherEnvironmentId = EnvironmentId.make("environment-other"); |
| 690 | const ref = (environment: EnvironmentId, threadId: ThreadId) => ({ |
| 691 | environmentId: environment, |
| 692 | threadId, |
| 693 | }); |
| 694 | const listed = ( |
| 695 | id: string, |
| 696 | environment = environmentId, |
| 697 | overrides?: Partial<Thread>, |
| 698 | ): Thread => ({ |
| 699 | id: ThreadId.make(id), |
| 700 | environmentId: environment, |
| 701 | lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null }, |
| 702 | archivedAt: null, |
| 703 | delegatedFrom: null, |
| 704 | ...overrides, |
| 705 | }); |
| 706 | const subagentOf = (id: string, parentId: ThreadId, environment = environmentId) => |
| 707 | listed(id, environment, { |
| 708 | lineage: { |
| 709 | rootThreadId: parentId, |
| 710 | parentThreadId: parentId, |
| 711 | relationshipToParent: "subagent", |
| 712 | }, |
| 713 | }); |
| 714 | const rowOf = (threadId: ThreadId, environment: EnvironmentId, threads: readonly Thread[]) => |
| 715 | resolveListedAncestorThreadRef(ref(environment, threadId), threads); |
| 716 | |
| 717 | it("maps an open subagent to its nearest listed ancestor", () => { |
| 718 | const root = listed("thread-root"); |
| 719 | const fork = listed("thread-fork", environmentId, { |
| 720 | lineage: { rootThreadId: root.id, parentThreadId: root.id, relationshipToParent: "fork" }, |
| 721 | }); |
| 722 | const child = subagentOf("thread-child", root.id); |
| 723 | const grandchild = subagentOf("thread-grandchild", child.id); |
| 724 | const forkChild = subagentOf("thread-fork-child", fork.id); |
| 725 | const orphan = subagentOf("thread-orphan", ThreadId.make("thread-missing")); |
| 726 | const elsewhere = subagentOf("thread-elsewhere", root.id, otherEnvironmentId); |
| 727 | const threads = [root, fork, child, grandchild, forkChild, orphan, elsewhere]; |
| 728 | |
| 729 | expect(rowOf(child.id, environmentId, threads)).toEqual(ref(environmentId, root.id)); |
| 730 | expect(rowOf(grandchild.id, environmentId, threads)).toEqual(ref(environmentId, root.id)); |
| 731 | expect(rowOf(forkChild.id, environmentId, threads)).toEqual(ref(environmentId, fork.id)); |
| 732 | expect(rowOf(elsewhere.id, otherEnvironmentId, threads)).toEqual( |
| 733 | ref(otherEnvironmentId, elsewhere.id), |
| 734 | ); |
| 735 | expect(rowOf(orphan.id, environmentId, threads)).toEqual(ref(environmentId, orphan.id)); |
| 736 | const loopA = subagentOf("thread-loop-a", ThreadId.make("thread-loop-b")); |
| 737 | const loopB = subagentOf("thread-loop-b", loopA.id); |
| 738 | expect(rowOf(loopA.id, environmentId, [loopA, loopB])).toEqual(ref(environmentId, loopA.id)); |
| 739 | }); |
| 740 | |
| 741 | it("maps an open task a linked environment delegated to its listed parent", () => { |
| 742 | const parent = listed("thread-parent", remoteEnvironmentId); |
| 743 | const task = listed("thread-task", environmentId, { |
| 744 | delegatedFrom: { environmentId: remoteEnvironmentId, threadId: parent.id, title: "Parent" }, |
| 745 | }); |
| 746 | |
| 747 | expect(rowOf(task.id, environmentId, [parent, task])).toEqual( |
| 748 | ref(remoteEnvironmentId, parent.id), |
| 749 | ); |
| 750 | }); |
| 751 | }); |