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, 03:37 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import type { |
| 3 | ApplicationStoredEvent, |
| 4 | OrchestrationV2ShellSnapshot, |
| 5 | OrchestrationV2ShellStreamItem, |
| 6 | OrchestrationV2ThreadShell, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
| 9 | import type * as Cause from "effect/Cause"; |
| 10 | import * as DateTime from "effect/DateTime"; |
| 11 | import * as Deferred from "effect/Deferred"; |
| 12 | import * as Duration from "effect/Duration"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as PubSub from "effect/PubSub"; |
| 15 | import * as Queue from "effect/Queue"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | import * as TestClock from "effect/testing/TestClock"; |
| 18 | |
| 19 | import { |
| 20 | activeShellRowsForSubscriber, |
| 21 | bufferShellLiveStream, |
| 22 | composeShellStreamWithEnrichment, |
| 23 | dedupeShellEnrichment, |
| 24 | omitIdleSubagentShells, |
| 25 | omitOlderSettledShells, |
| 26 | shellStreamItemFromEnrichmentRefresh, |
| 27 | shellStreamItemsFromInitialSnapshot, |
| 28 | shouldPublishShellApplicationEvent, |
| 29 | skipUnchangedThreadShells, |
| 30 | } from "./ShellStream.ts"; |
| 31 | |
| 32 | const emptyShellSnapshot = { |
| 33 | schemaVersion: 1, |
| 34 | snapshotSequence: 0, |
| 35 | projects: [], |
| 36 | threads: [], |
| 37 | archivedThreads: [], |
| 38 | } as OrchestrationV2ShellSnapshot; |
| 39 | |
| 40 | describe("subagent shell events", () => { |
| 41 | const stored = (threadId: string, type: string, itemType = "command_execution") => |
| 42 | ({ event: { threadId, type, payload: { type: itemType } } }) as ApplicationStoredEvent; |
| 43 | |
| 44 | it("keeps regular threads and subagent state changes while omitting subagent transcript updates", () => { |
| 45 | const subagents = new Set(["child"]); |
| 46 | expect( |
| 47 | shouldPublishShellApplicationEvent(stored("parent", "turn-item.updated"), subagents), |
| 48 | ).toBe(true); |
| 49 | expect( |
| 50 | shouldPublishShellApplicationEvent(stored("child", "turn-item.updated"), subagents), |
| 51 | ).toBe(false); |
| 52 | for (const itemType of ["secret_request", "link_request", "user_message"]) { |
| 53 | expect( |
| 54 | shouldPublishShellApplicationEvent( |
| 55 | stored("child", "turn-item.updated", itemType), |
| 56 | subagents, |
| 57 | ), |
| 58 | ).toBe(true); |
| 59 | } |
| 60 | expect(shouldPublishShellApplicationEvent(stored("child", "node.updated"), subagents)).toBe( |
| 61 | false, |
| 62 | ); |
| 63 | expect( |
| 64 | shouldPublishShellApplicationEvent(stored("child", "runtime-request.updated"), subagents), |
| 65 | ).toBe(true); |
| 66 | expect(shouldPublishShellApplicationEvent(stored("child", "run.updated"), subagents)).toBe( |
| 67 | true, |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | it("tracks subagents created after the subscription starts", () => { |
| 72 | const subagents = new Set<string>(); |
| 73 | const created = { |
| 74 | event: { |
| 75 | threadId: "new-child", |
| 76 | type: "thread.created", |
| 77 | payload: { lineage: { relationshipToParent: "subagent" } }, |
| 78 | }, |
| 79 | } as ApplicationStoredEvent; |
| 80 | expect(shouldPublishShellApplicationEvent(created, subagents)).toBe(true); |
| 81 | expect( |
| 82 | shouldPublishShellApplicationEvent(stored("new-child", "message.updated"), subagents), |
| 83 | ).toBe(false); |
| 84 | expect( |
| 85 | shouldPublishShellApplicationEvent(stored("new-child", "thread.deleted"), subagents), |
| 86 | ).toBe(true); |
| 87 | expect(subagents.has("new-child")).toBe(true); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | it.effect("coalesces a slow shell subscriber by identity and location in sequence order", () => |
| 92 | Effect.gen(function* () { |
| 93 | const input = yield* Queue.unbounded< |
| 94 | Extract<OrchestrationV2ShellStreamItem, { readonly sequence: number }>, |
| 95 | Cause.Done |
| 96 | >(); |
| 97 | const drained = yield* Deferred.make<void>(); |
| 98 | const shell = shellFixture({ id: ThreadId.make("same"), title: "Before" }); |
| 99 | const project = { |
| 100 | id: ProjectId.make("same"), |
| 101 | title: "Before", |
| 102 | workspaceRoot: "/workspace/same", |
| 103 | repositoryIdentity: null, |
| 104 | defaultModelSelection: null, |
| 105 | scripts: [], |
| 106 | createdAt: "2026-09-01T00:00:00Z", |
| 107 | updatedAt: "2026-09-01T00:00:00Z", |
| 108 | }; |
| 109 | const initial = { |
| 110 | kind: "thread.updated" as const, |
| 111 | sequence: 1, |
| 112 | location: "active" as const, |
| 113 | thread: shell, |
| 114 | }; |
| 115 | const updates = [ |
| 116 | { kind: "project.updated", sequence: 2, project }, |
| 117 | { ...initial, sequence: 3 }, |
| 118 | { ...initial, sequence: 4, location: "archive" }, |
| 119 | { ...initial, sequence: 5, thread: { ...shell, id: ThreadId.make("other") } }, |
| 120 | { |
| 121 | kind: "project.updated", |
| 122 | sequence: 6, |
| 123 | project: { ...project, id: ProjectId.make("other") }, |
| 124 | }, |
| 125 | { kind: "project.removed", sequence: 7, projectId: project.id }, |
| 126 | { kind: "thread.removed", sequence: 8, location: "active", threadId: shell.id }, |
| 127 | { kind: "project.updated", sequence: 9, project: { ...project, title: "Recreated" } }, |
| 128 | { ...initial, sequence: 10, thread: { ...shell, title: "Recreated" } }, |
| 129 | { kind: "thread.removed", sequence: 11, location: "archive", threadId: shell.id }, |
| 130 | ] satisfies Array<Extract<OrchestrationV2ShellStreamItem, { readonly sequence: number }>>; |
| 131 | const pull = yield* Stream.toPull( |
| 132 | bufferShellLiveStream( |
| 133 | Stream.fromQueue(input).pipe(Stream.ensuring(Deferred.succeed(drained, undefined))), |
| 134 | { maxItems: 6, maxSerializedBytes: 1_500 }, |
| 135 | ), |
| 136 | ); |
| 137 | yield* Queue.offer(input, initial); |
| 138 | expect(yield* pull).toEqual([initial]); |
| 139 | yield* Queue.offerAll(input, updates); |
| 140 | yield* Queue.end(input); |
| 141 | // Finish producing the burst while the first batch is still awaiting its ACK. |
| 142 | yield* Deferred.await(drained); |
| 143 | expect(yield* pull).toEqual([updates[3], updates[4], updates[7], updates[8], updates[9]]); |
| 144 | }), |
| 145 | ); |
| 146 | |
| 147 | function shellFixture(overrides: Partial<OrchestrationV2ThreadShell>): OrchestrationV2ThreadShell { |
| 148 | return { id: "thread-a", archivedAt: null, ...overrides } as OrchestrationV2ThreadShell; |
| 149 | } |
| 150 | |
| 151 | describe("omitIdleSubagentShells", () => { |
| 152 | const row = ( |
| 153 | id: string, |
| 154 | parent: string | null, |
| 155 | overrides: Partial<OrchestrationV2ThreadShell> = {}, |
| 156 | ) => |
| 157 | shellFixture({ |
| 158 | id: ThreadId.make(id), |
| 159 | lineage: { |
| 160 | parentThreadId: parent === null ? null : ThreadId.make(parent), |
| 161 | relationshipToParent: parent === null ? null : "subagent", |
| 162 | rootThreadId: ThreadId.make("root"), |
| 163 | }, |
| 164 | status: "completed", |
| 165 | activityRunStatus: null, |
| 166 | pendingRuntimeRequest: null, |
| 167 | pendingBackgroundTasks: [], |
| 168 | ...overrides, |
| 169 | }); |
| 170 | |
| 171 | it("drops idle leaf children but keeps live ones and the middle of nested chains", () => { |
| 172 | const threads = [ |
| 173 | row("root", null), |
| 174 | row("idle", "root"), |
| 175 | row("running", "root", { activityRunStatus: "running" }), |
| 176 | row("queued", "root", { status: "queued" }), |
| 177 | row("asking", "root", { |
| 178 | pendingRuntimeRequest: { |
| 179 | id: "request" as never, |
| 180 | kind: "user_input", |
| 181 | createdAt: DateTime.makeUnsafe(0), |
| 182 | }, |
| 183 | }), |
| 184 | row("background", "root", { |
| 185 | pendingBackgroundTasks: [{ kind: "subagent" } as never], |
| 186 | }), |
| 187 | // Idle, but a woken grandchild walks through it to reach the root's badge. |
| 188 | row("middle", "root"), |
| 189 | row("grandchild", "middle"), |
| 190 | ]; |
| 191 | expect(omitIdleSubagentShells(threads).map((thread) => thread.id)).toEqual([ |
| 192 | "root", |
| 193 | "running", |
| 194 | "queued", |
| 195 | "asking", |
| 196 | "background", |
| 197 | "middle", |
| 198 | ]); |
| 199 | }); |
| 200 | |
| 201 | it("still filters the transcript events of omitted children for a subscriber that opts in", () => { |
| 202 | const threads = [row("root", null), row("idle", "root")]; |
| 203 | const omitted = activeShellRowsForSubscriber(threads, true); |
| 204 | expect(omitted.threads.map((thread) => thread.id)).toEqual(["root"]); |
| 205 | expect( |
| 206 | shouldPublishShellApplicationEvent( |
| 207 | { |
| 208 | event: { |
| 209 | threadId: "idle", |
| 210 | type: "turn-item.updated", |
| 211 | payload: { type: "command_execution" }, |
| 212 | }, |
| 213 | } as ApplicationStoredEvent, |
| 214 | omitted.subagentThreadIds, |
| 215 | ), |
| 216 | ).toBe(false); |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | describe("omitOlderSettledShells", () => { |
| 221 | const DAY = 24 * 60 * 60 * 1000; |
| 222 | const now = DateTime.makeUnsafe(100 * DAY); |
| 223 | const row = ( |
| 224 | id: string, |
| 225 | settledDaysAgo: number | null, |
| 226 | overrides: Partial<OrchestrationV2ThreadShell> = {}, |
| 227 | ) => |
| 228 | shellFixture({ |
| 229 | id: ThreadId.make(id), |
| 230 | projectId: ProjectId.make(id.startsWith("b-") ? "project-b" : "project-a"), |
| 231 | lineage: { |
| 232 | parentThreadId: null, |
| 233 | relationshipToParent: null, |
| 234 | rootThreadId: ThreadId.make(id), |
| 235 | }, |
| 236 | status: "completed", |
| 237 | activityRunStatus: null, |
| 238 | pendingRuntimeRequest: null, |
| 239 | pendingBackgroundTasks: [], |
| 240 | settledOverride: settledDaysAgo === null ? null : "settled", |
| 241 | settledAt: |
| 242 | settledDaysAgo === null ? null : DateTime.makeUnsafe(100 * DAY - settledDaysAgo * DAY), |
| 243 | ...overrides, |
| 244 | }); |
| 245 | const newest = Array.from({ length: 25 }, (_, index) => row(`new-${index}`, index / 10)); |
| 246 | const omit = (threads: ReadonlyArray<OrchestrationV2ThreadShell>) => |
| 247 | omitOlderSettledShells({ threads, snapshotSequence: 7, now }); |
| 248 | |
| 249 | it("keeps the 25 newest settled threads and counts the rest per project", () => { |
| 250 | const sideChat = row("side-chat", 0, { |
| 251 | lineage: { |
| 252 | parentThreadId: ThreadId.make("new-0"), |
| 253 | relationshipToParent: "fork", |
| 254 | rootThreadId: ThreadId.make("new-0"), |
| 255 | sideChat: true, |
| 256 | }, |
| 257 | }); |
| 258 | const result = omit([ |
| 259 | ...newest, |
| 260 | sideChat, |
| 261 | row("old", 30), |
| 262 | row("b-old", 40), |
| 263 | row("unsettled", null), |
| 264 | ]); |
| 265 | expect(result.threads.map((thread) => thread.id)).toEqual([ |
| 266 | ...newest.map((thread) => thread.id), |
| 267 | "side-chat", |
| 268 | "unsettled", |
| 269 | ]); |
| 270 | expect(result.olderSettled).toEqual({ |
| 271 | before: DateTime.formatIso(DateTime.makeUnsafe(100 * DAY - 2.4 * DAY)), |
| 272 | asOfSequence: 7, |
| 273 | counts: [ |
| 274 | { projectId: "project-a", count: 1 }, |
| 275 | { projectId: "project-b", count: 1 }, |
| 276 | ], |
| 277 | }); |
| 278 | }); |
| 279 | |
| 280 | it("keeps threads tied with the 25th newest", () => { |
| 281 | const result = omit([...newest, row("tie", 2.4), row("older", 2.5)]); |
| 282 | expect(result.threads.map((thread) => thread.id).slice(25)).toEqual(["tie"]); |
| 283 | }); |
| 284 | |
| 285 | it("omits nothing with fewer than 25 settled threads, however old", () => { |
| 286 | const old = Array.from({ length: 5 }, (_, index) => row(`old-${index}`, 300 + index)); |
| 287 | const result = omit(old); |
| 288 | expect(result.threads).toEqual(old); |
| 289 | expect(result.olderSettled.counts).toEqual([]); |
| 290 | }); |
| 291 | |
| 292 | it("keeps older rows that are snoozed, live, or above a kept subagent or side chat, and still counts them", () => { |
| 293 | const threads = [ |
| 294 | ...newest, |
| 295 | row("snoozed", 30, { snoozedUntil: DateTime.makeUnsafe(101 * DAY) }), |
| 296 | row("woke", 30, { snoozedUntil: DateTime.makeUnsafe(99 * DAY) }), |
| 297 | row("running", 30, { activityRunStatus: "running" }), |
| 298 | row("parent", 30), |
| 299 | shellFixture({ |
| 300 | id: ThreadId.make("child"), |
| 301 | projectId: ProjectId.make("project-a"), |
| 302 | lineage: { |
| 303 | parentThreadId: ThreadId.make("parent"), |
| 304 | relationshipToParent: "subagent", |
| 305 | rootThreadId: ThreadId.make("parent"), |
| 306 | }, |
| 307 | settledOverride: null, |
| 308 | settledAt: null, |
| 309 | }), |
| 310 | row("side-chat-parent", 30), |
| 311 | row("side-chat", null, { |
| 312 | lineage: { |
| 313 | parentThreadId: ThreadId.make("side-chat-parent"), |
| 314 | relationshipToParent: "fork", |
| 315 | rootThreadId: ThreadId.make("side-chat-parent"), |
| 316 | sideChat: true, |
| 317 | }, |
| 318 | }), |
| 319 | ]; |
| 320 | const result = omit(threads); |
| 321 | expect(result.threads.map((thread) => thread.id).slice(25)).toEqual([ |
| 322 | "snoozed", |
| 323 | "running", |
| 324 | "parent", |
| 325 | "child", |
| 326 | "side-chat-parent", |
| 327 | "side-chat", |
| 328 | ]); |
| 329 | expect(result.olderSettled.counts).toEqual([{ projectId: "project-a", count: 5 }]); |
| 330 | }); |
| 331 | }); |
| 332 | |
| 333 | describe("shellStreamItemsFromInitialSnapshot", () => { |
| 334 | it("keeps thread rows out of the metadata-only enrichment frame", () => { |
| 335 | const snapshot = { |
| 336 | ...emptyShellSnapshot, |
| 337 | projects: [ |
| 338 | { id: "project-a", workspaceRoot: "/workspace/a" }, |
| 339 | { id: "project-b", workspaceRoot: "/workspace/b" }, |
| 340 | ], |
| 341 | threads: [shellFixture({})], |
| 342 | archivedThreads: [shellFixture({ id: ThreadId.make("thread-archived"), archivedAt: null })], |
| 343 | } as unknown as OrchestrationV2ShellSnapshot; |
| 344 | |
| 345 | const items = shellStreamItemsFromInitialSnapshot({ |
| 346 | snapshot, |
| 347 | resolvedRepositoryIdentityRoots: ["/workspace/a"], |
| 348 | }); |
| 349 | |
| 350 | expect(items[0]).toEqual({ kind: "snapshot", snapshot }); |
| 351 | expect(items[1]).toMatchObject({ |
| 352 | kind: "snapshot", |
| 353 | snapshot: { |
| 354 | projects: [{ id: "project-a", workspaceRoot: "/workspace/a" }], |
| 355 | threads: [], |
| 356 | archivedThreads: [], |
| 357 | }, |
| 358 | resolvedRepositoryIdentityRoots: ["/workspace/a"], |
| 359 | }); |
| 360 | expect(JSON.stringify(items[1]).length).toBeLessThan(JSON.stringify(items[0]).length); |
| 361 | }); |
| 362 | }); |
| 363 | |
| 364 | describe("composeShellStreamWithEnrichment", () => { |
| 365 | it.effect("preserves pending repository resolutions while an enrichment ACK is withheld", () => |
| 366 | Effect.gen(function* () { |
| 367 | // Match ProjectEnrichmentService's sliding notifications and the WS batching path. |
| 368 | const changes = yield* Effect.acquireRelease( |
| 369 | PubSub.sliding<{ readonly workspaceRoot: string }>(256), |
| 370 | PubSub.shutdown, |
| 371 | ); |
| 372 | const subscription = yield* PubSub.subscribe(changes); |
| 373 | const pull = yield* Stream.toPull( |
| 374 | composeShellStreamWithEnrichment({ |
| 375 | initial: Stream.empty, |
| 376 | tail: Stream.empty, |
| 377 | enrichment: Stream.fromSubscription(subscription).pipe( |
| 378 | Stream.groupedWithin(64, Duration.millis(25)), |
| 379 | Stream.map((changes) => |
| 380 | shellStreamItemFromEnrichmentRefresh({ |
| 381 | snapshot: emptyShellSnapshot, |
| 382 | changes, |
| 383 | }), |
| 384 | ), |
| 385 | ), |
| 386 | }), |
| 387 | ); |
| 388 | |
| 389 | const firstRoots = Array.from({ length: 64 }, (_, index) => `/workspace/first-${index}`); |
| 390 | yield* PubSub.publishAll( |
| 391 | changes, |
| 392 | firstRoots.map((workspaceRoot) => ({ workspaceRoot })), |
| 393 | ); |
| 394 | expect(yield* pull).toMatchObject([{ resolvedRepositoryIdentityRoots: firstRoots }]); |
| 395 | |
| 396 | // The next completion can be batched before the client acknowledges the first. |
| 397 | yield* PubSub.publish(changes, { workspaceRoot: "/workspace/pending" }); |
| 398 | yield* Effect.yieldNow; |
| 399 | yield* TestClock.adjust(Duration.millis(25)); |
| 400 | const laterRoots = Array.from({ length: 256 }, (_, index) => `/workspace/later-${index}`); |
| 401 | yield* PubSub.publishAll( |
| 402 | changes, |
| 403 | laterRoots.map((workspaceRoot) => ({ workspaceRoot })), |
| 404 | ); |
| 405 | |
| 406 | const resumed = yield* Stream.fromPull(Effect.succeed(pull)).pipe( |
| 407 | Stream.takeUntil( |
| 408 | (item) => item.resolvedRepositoryIdentityRoots?.includes("/workspace/later-255") ?? false, |
| 409 | ), |
| 410 | Stream.runCollect, |
| 411 | ); |
| 412 | expect(resumed.flatMap((item) => item.resolvedRepositoryIdentityRoots ?? [])).toEqual([ |
| 413 | "/workspace/pending", |
| 414 | ...laterRoots, |
| 415 | ]); |
| 416 | }), |
| 417 | ); |
| 418 | |
| 419 | it.effect("keeps a merged shell batch charged while the client withholds its ACK", () => |
| 420 | Effect.gen(function* () { |
| 421 | const update = yield* Deferred.make<void>(); |
| 422 | const closed = yield* Deferred.make<void>(); |
| 423 | const initial = { |
| 424 | kind: "thread.removed" as const, |
| 425 | sequence: 1, |
| 426 | location: "active" as const, |
| 427 | threadId: ThreadId.make("thread-a"), |
| 428 | }; |
| 429 | const pull = yield* Stream.toPull( |
| 430 | composeShellStreamWithEnrichment({ |
| 431 | initial: Stream.empty, |
| 432 | tail: bufferShellLiveStream( |
| 433 | Stream.concat( |
| 434 | Stream.succeed(initial), |
| 435 | Stream.fromEffect(Deferred.await(update)).pipe( |
| 436 | Stream.map(() => ({ ...initial, sequence: 2 })), |
| 437 | ), |
| 438 | ).pipe(Stream.ensuring(Deferred.succeed(closed, undefined))), |
| 439 | { maxItems: 1 }, |
| 440 | ), |
| 441 | enrichment: Stream.empty, |
| 442 | }), |
| 443 | ); |
| 444 | expect(yield* pull).toEqual([initial]); |
| 445 | // Let the merge producer run while the client still withholds its ACK. |
| 446 | yield* Effect.yieldNow; |
| 447 | yield* Deferred.succeed(update, undefined); |
| 448 | yield* Deferred.await(closed); |
| 449 | const result = yield* Stream.fromPull(Effect.succeed(pull)).pipe( |
| 450 | Stream.runDrain, |
| 451 | Effect.result, |
| 452 | ); |
| 453 | expect(result._tag).toBe("Failure"); |
| 454 | if (result._tag === "Failure") expect(result.failure._tag).toBe("LiveStreamBufferError"); |
| 455 | }), |
| 456 | ); |
| 457 | |
| 458 | it.effect( |
| 459 | "emits every initial item before enrichment even when enrichment is already ready", |
| 460 | () => |
| 461 | Effect.gen(function* () { |
| 462 | const initialSnapshot = { |
| 463 | ...emptyShellSnapshot, |
| 464 | snapshotSequence: 5, |
| 465 | } as OrchestrationV2ShellSnapshot; |
| 466 | const enrichmentSnapshot = { |
| 467 | ...emptyShellSnapshot, |
| 468 | snapshotSequence: 10, |
| 469 | } as OrchestrationV2ShellSnapshot; |
| 470 | |
| 471 | const initialItems = shellStreamItemsFromInitialSnapshot({ |
| 472 | snapshot: initialSnapshot, |
| 473 | resolvedRepositoryIdentityRoots: ["/workspace/a"], |
| 474 | }); |
| 475 | const enrichment = Stream.make( |
| 476 | shellStreamItemFromEnrichmentRefresh({ |
| 477 | snapshot: enrichmentSnapshot, |
| 478 | changes: [{ workspaceRoot: "/workspace/b" }], |
| 479 | }), |
| 480 | ); |
| 481 | const tail = Stream.make( |
| 482 | { kind: "synchronized" as const }, |
| 483 | { |
| 484 | kind: "project.removed" as const, |
| 485 | sequence: 6, |
| 486 | projectId: "project-a", |
| 487 | }, |
| 488 | ); |
| 489 | |
| 490 | const items = Array.from( |
| 491 | yield* composeShellStreamWithEnrichment({ |
| 492 | initial: Stream.fromIterable(initialItems), |
| 493 | tail, |
| 494 | enrichment, |
| 495 | }).pipe(Stream.runCollect), |
| 496 | ); |
| 497 | |
| 498 | expect(items.slice(0, initialItems.length)).toEqual(initialItems); |
| 499 | |
| 500 | const enrichmentIndex = items.findIndex( |
| 501 | (item) => |
| 502 | item.kind === "snapshot" && |
| 503 | "resolvedRepositoryIdentityRoots" in item && |
| 504 | item.resolvedRepositoryIdentityRoots?.includes("/workspace/b"), |
| 505 | ); |
| 506 | expect(enrichmentIndex).toBeGreaterThanOrEqual(initialItems.length); |
| 507 | |
| 508 | for (let index = 0; index < initialItems.length; index++) { |
| 509 | expect(items[index]).toEqual(initialItems[index]); |
| 510 | } |
| 511 | }), |
| 512 | ); |
| 513 | }); |
| 514 | |
| 515 | describe("dedupeShellEnrichment", () => { |
| 516 | const project = { |
| 517 | id: ProjectId.make("project-a"), |
| 518 | title: "A project", |
| 519 | workspaceRoot: "/workspace/a", |
| 520 | repositoryIdentity: null, |
| 521 | defaultModelSelection: null, |
| 522 | scripts: [], |
| 523 | createdAt: "2026-09-01T00:00:00Z", |
| 524 | updatedAt: "2026-09-01T00:00:00Z", |
| 525 | }; |
| 526 | const initial = { |
| 527 | kind: "snapshot" as const, |
| 528 | snapshot: { ...emptyShellSnapshot, projects: [project] }, |
| 529 | }; |
| 530 | const marked = { ...initial, resolvedRepositoryIdentityRoots: [project.workspaceRoot] }; |
| 531 | |
| 532 | it.effect( |
| 533 | "keeps initial/resume resolution and real changes but drops sequence-only repeats", |
| 534 | () => |
| 535 | Effect.gen(function* () { |
| 536 | const repeated = { |
| 537 | ...marked, |
| 538 | snapshot: { ...marked.snapshot, snapshotSequence: 10, projects: [{ ...project }] }, |
| 539 | }; |
| 540 | const progressed = { |
| 541 | ...repeated, |
| 542 | resolvedRepositoryIdentityRoots: [project.workspaceRoot, "/workspace/b"], |
| 543 | }; |
| 544 | const changed = { |
| 545 | ...progressed, |
| 546 | snapshot: { ...progressed.snapshot, projects: [{ ...project, title: "Renamed" }] }, |
| 547 | }; |
| 548 | const items = yield* Stream.make( |
| 549 | initial, |
| 550 | marked, |
| 551 | repeated, |
| 552 | progressed, |
| 553 | changed, |
| 554 | changed, |
| 555 | ).pipe(dedupeShellEnrichment, Stream.runCollect); |
| 556 | expect(items).toEqual([initial, marked, progressed, changed]); |
| 557 | const resumed = Stream.make(marked, repeated).pipe(dedupeShellEnrichment); |
| 558 | expect(yield* Stream.runCollect(resumed)).toEqual([marked]); |
| 559 | expect(yield* Stream.runCollect(resumed)).toEqual([marked]); |
| 560 | }), |
| 561 | ); |
| 562 | |
| 563 | it.effect("keeps authoritative snapshots and invalidates metadata after project deltas", () => |
| 564 | Effect.gen(function* () { |
| 565 | const removed = { kind: "project.removed" as const, projectId: project.id, sequence: 2 }; |
| 566 | const updated = { kind: "project.updated" as const, project, sequence: 3 }; |
| 567 | const withThreads = { |
| 568 | ...marked, |
| 569 | snapshot: { ...marked.snapshot, threads: [shellFixture({})] }, |
| 570 | }; |
| 571 | const values = [ |
| 572 | initial, |
| 573 | initial, |
| 574 | marked, |
| 575 | removed, |
| 576 | marked, |
| 577 | updated, |
| 578 | marked, |
| 579 | withThreads, |
| 580 | withThreads, |
| 581 | ]; |
| 582 | expect( |
| 583 | yield* Stream.fromIterable(values).pipe(dedupeShellEnrichment, Stream.runCollect), |
| 584 | ).toEqual(values); |
| 585 | }), |
| 586 | ); |
| 587 | |
| 588 | it.effect( |
| 589 | "deduplicates across thread deltas without dropping those deltas or identity clears", |
| 590 | () => |
| 591 | Effect.gen(function* () { |
| 592 | const delta = { |
| 593 | kind: "thread.removed" as const, |
| 594 | threadId: ThreadId.make("thread-a"), |
| 595 | location: "active" as const, |
| 596 | sequence: 2, |
| 597 | }; |
| 598 | const resolved = { |
| 599 | ...marked, |
| 600 | snapshot: { |
| 601 | ...marked.snapshot, |
| 602 | projects: [ |
| 603 | { |
| 604 | ...project, |
| 605 | repositoryIdentity: { |
| 606 | canonicalKey: "github.com/test/repo", |
| 607 | locator: { |
| 608 | source: "git-remote" as const, |
| 609 | remoteName: "origin", |
| 610 | remoteUrl: "https://github.com/test/repo.git", |
| 611 | }, |
| 612 | }, |
| 613 | }, |
| 614 | ], |
| 615 | }, |
| 616 | }; |
| 617 | expect( |
| 618 | yield* Stream.make(marked, delta, marked, resolved, marked).pipe( |
| 619 | dedupeShellEnrichment, |
| 620 | Stream.runCollect, |
| 621 | ), |
| 622 | ).toEqual([marked, delta, resolved, marked]); |
| 623 | }), |
| 624 | ); |
| 625 | }); |
| 626 | |
| 627 | describe("skipUnchangedThreadShells", () => { |
| 628 | const at = (ms: number) => DateTime.makeUnsafe(Date.parse("2026-10-01T00:00:00.000Z") + ms); |
| 629 | const shell = ( |
| 630 | id: string, |
| 631 | overrides: Partial<OrchestrationV2ThreadShell> = {}, |
| 632 | ): OrchestrationV2ThreadShell => ({ |
| 633 | id: ThreadId.make(id), |
| 634 | projectId: ProjectId.make("project-a"), |
| 635 | title: "Thread", |
| 636 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 637 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 638 | runtimeMode: "full-access", |
| 639 | interactionMode: "default", |
| 640 | worktreePath: null, |
| 641 | activeProviderThreadId: null, |
| 642 | lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null }, |
| 643 | forkedFrom: null, |
| 644 | createdBy: "user", |
| 645 | creationSource: "web", |
| 646 | activeRunId: null, |
| 647 | latestVisibleMessage: null, |
| 648 | hasActionableProposedPlan: false, |
| 649 | itemCount: 1, |
| 650 | visibleItemCount: 1, |
| 651 | branch: null, |
| 652 | status: "running", |
| 653 | pendingRuntimeRequest: null, |
| 654 | pendingBackgroundTasks: [], |
| 655 | latestRunId: null, |
| 656 | latestUserMessageAt: null, |
| 657 | createdAt: at(0), |
| 658 | updatedAt: at(0), |
| 659 | archivedAt: null, |
| 660 | settledOverride: null, |
| 661 | settledAt: null, |
| 662 | deletedAt: null, |
| 663 | ...overrides, |
| 664 | }); |
| 665 | type Step = |
| 666 | | { readonly advanceMs: number } |
| 667 | | Exclude<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" | "synchronized" }>; |
| 668 | const updated = (sequence: number, thread: OrchestrationV2ThreadShell): Step => ({ |
| 669 | kind: "thread.updated", |
| 670 | sequence, |
| 671 | location: "active", |
| 672 | thread, |
| 673 | }); |
| 674 | const run = (steps: ReadonlyArray<Step>) => |
| 675 | Stream.fromIterable(steps).pipe( |
| 676 | Stream.mapEffect((step) => |
| 677 | "advanceMs" in step |
| 678 | ? TestClock.adjust(Duration.millis(step.advanceMs)).pipe(Effect.as(null)) |
| 679 | : Effect.succeed(step), |
| 680 | ), |
| 681 | Stream.filter((item) => item !== null), |
| 682 | skipUnchangedThreadShells, |
| 683 | Stream.runCollect, |
| 684 | Effect.map((items) => Array.from(items, (item) => ("sequence" in item ? item.sequence : -1))), |
| 685 | ); |
| 686 | |
| 687 | it.effect("sends the next update after a removal", () => |
| 688 | Effect.gen(function* () { |
| 689 | const sent = yield* run([ |
| 690 | updated(1, shell("a")), |
| 691 | { kind: "thread.removed", sequence: 2, location: "active", threadId: ThreadId.make("a") }, |
| 692 | updated(3, shell("a")), |
| 693 | ]); |
| 694 | expect(sent).toEqual([1, 2, 3]); |
| 695 | }), |
| 696 | ); |
| 697 | }); |