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 { describe, expect, it } from "vite-plus/test"; |
| 2 | import { |
| 3 | type ThreadPullRequestLink, |
| 4 | type OrchestrationV2DomainEvent, |
| 5 | type OrchestrationV2Run, |
| 6 | type OrchestrationV2ProviderThread, |
| 7 | type OrchestrationV2ThreadProjection, |
| 8 | type OrchestrationV2TurnItem, |
| 9 | MessageId, |
| 10 | ProjectId, |
| 11 | ProviderInstanceId, |
| 12 | NodeId, |
| 13 | ProviderThreadId, |
| 14 | ProviderTurnId, |
| 15 | RunId, |
| 16 | ThreadId, |
| 17 | TurnItemId, |
| 18 | } from "@t3tools/contracts"; |
| 19 | import * as DateTime from "effect/DateTime"; |
| 20 | |
| 21 | import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts"; |
| 22 | |
| 23 | const now = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"); |
| 24 | const threadId = ThreadId.make("thread-reducer"); |
| 25 | const runId = RunId.make("run-reducer"); |
| 26 | const run = { |
| 27 | id: runId, |
| 28 | threadId, |
| 29 | ordinal: 1, |
| 30 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 31 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 32 | providerThreadId: null, |
| 33 | userMessageId: MessageId.make("message-reducer"), |
| 34 | rootNodeId: null, |
| 35 | activeAttemptId: null, |
| 36 | status: "completed", |
| 37 | requestedAt: now, |
| 38 | startedAt: now, |
| 39 | completedAt: now, |
| 40 | checkpointId: null, |
| 41 | contextHandoffId: null, |
| 42 | } satisfies OrchestrationV2Run; |
| 43 | |
| 44 | function commandItem(id: string, output = "done", ordinal = 1): OrchestrationV2TurnItem { |
| 45 | return { |
| 46 | id: TurnItemId.make(id), |
| 47 | threadId, |
| 48 | runId, |
| 49 | nodeId: null, |
| 50 | providerThreadId: null, |
| 51 | providerTurnId: null, |
| 52 | nativeItemRef: null, |
| 53 | parentItemId: null, |
| 54 | ordinal, |
| 55 | status: "completed", |
| 56 | title: null, |
| 57 | startedAt: now, |
| 58 | completedAt: now, |
| 59 | updatedAt: now, |
| 60 | type: "command_execution", |
| 61 | input: "pwd", |
| 62 | output, |
| 63 | exitCode: 0, |
| 64 | }; |
| 65 | } |
| 66 | const emptyProjection = { |
| 67 | thread: { |
| 68 | id: threadId, |
| 69 | projectId: ProjectId.make("project-reducer"), |
| 70 | title: "Reducer", |
| 71 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 72 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 73 | runtimeMode: "full-access", |
| 74 | interactionMode: "default", |
| 75 | branch: null, |
| 76 | worktreePath: null, |
| 77 | activeProviderThreadId: null, |
| 78 | lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null }, |
| 79 | forkedFrom: null, |
| 80 | createdBy: "user", |
| 81 | creationSource: "web", |
| 82 | createdAt: now, |
| 83 | updatedAt: now, |
| 84 | archivedAt: null, |
| 85 | settledOverride: null, |
| 86 | settledAt: null, |
| 87 | lastVisitedAt: null, |
| 88 | deletedAt: null, |
| 89 | }, |
| 90 | runs: [], |
| 91 | attempts: [], |
| 92 | nodes: [], |
| 93 | subagents: [], |
| 94 | providerSessions: [], |
| 95 | providerThreads: [], |
| 96 | providerTurns: [], |
| 97 | runtimeRequests: [], |
| 98 | messages: [], |
| 99 | plans: [], |
| 100 | turnItems: [], |
| 101 | checkpointScopes: [], |
| 102 | checkpoints: [], |
| 103 | contextHandoffs: [], |
| 104 | contextTransfers: [], |
| 105 | visibleTurnItems: [], |
| 106 | updatedAt: now, |
| 107 | } as OrchestrationV2ThreadProjection; |
| 108 | |
| 109 | describe("applyOrchestrationV2ProjectionEvent", () => { |
| 110 | it("applies a compact PR sync without replacing other links or thread metadata", () => { |
| 111 | const link = (number: number): ThreadPullRequestLink => ({ |
| 112 | host: "github.com", |
| 113 | repository: "pingdotgg/t3code", |
| 114 | number, |
| 115 | url: `https://github.com/pingdotgg/t3code/pull/${number}`, |
| 116 | source: "manual", |
| 117 | linkedAt: DateTime.formatIso(now), |
| 118 | snapshot: null, |
| 119 | stack: null, |
| 120 | }); |
| 121 | const links = [link(1), link(2)]; |
| 122 | const projection = { |
| 123 | ...emptyProjection, |
| 124 | thread: { ...emptyProjection.thread, pullRequests: links }, |
| 125 | }; |
| 126 | const updated = { ...links[1]!, source: "agent" as const }; |
| 127 | const next = applyOrchestrationV2ProjectionEvent(projection, { |
| 128 | id: "compact-sync" as OrchestrationV2DomainEvent["id"], |
| 129 | type: "thread.pull-request-link-updated", |
| 130 | threadId, |
| 131 | occurredAt: now, |
| 132 | payload: updated, |
| 133 | }); |
| 134 | expect(next?.thread).toEqual({ ...projection.thread, pullRequests: [links[0], updated] }); |
| 135 | expect(next?.thread.pullRequests?.[0]).toBe(links[0]); |
| 136 | }); |
| 137 | |
| 138 | it("keeps live token usage when the terminal provider turn omits it", () => { |
| 139 | const providerTurnId = ProviderTurnId.make("provider-turn-reducer"); |
| 140 | const running = { |
| 141 | id: providerTurnId, |
| 142 | providerThreadId: ProviderThreadId.make("provider-thread-reducer"), |
| 143 | nodeId: NodeId.make("provider-node-reducer"), |
| 144 | runAttemptId: null, |
| 145 | nativeTurnRef: null, |
| 146 | ordinal: 1, |
| 147 | status: "running" as const, |
| 148 | startedAt: now, |
| 149 | completedAt: null, |
| 150 | tokenUsage: { |
| 151 | usedTokens: 50_000, |
| 152 | maxTokens: 200_000, |
| 153 | updatedAt: "2026-08-29T00:00:00.000Z", |
| 154 | }, |
| 155 | }; |
| 156 | const projection = { ...emptyProjection, providerTurns: [running] }; |
| 157 | const event = { |
| 158 | id: "event-provider-turn-terminal", |
| 159 | type: "provider-turn.updated", |
| 160 | threadId, |
| 161 | driver: "codex", |
| 162 | occurredAt: now, |
| 163 | payload: { |
| 164 | ...running, |
| 165 | status: "completed", |
| 166 | completedAt: now, |
| 167 | tokenUsage: undefined, |
| 168 | }, |
| 169 | } as OrchestrationV2DomainEvent; |
| 170 | |
| 171 | const next = applyOrchestrationV2ProjectionEvent(projection, event); |
| 172 | |
| 173 | expect(next?.providerTurns[0]?.status).toBe("completed"); |
| 174 | expect(next?.providerTurns[0]?.tokenUsage).toEqual(running.tokenUsage); |
| 175 | }); |
| 176 | |
| 177 | it("applies thread lifecycle payloads instead of leaving stale metadata", () => { |
| 178 | const archivedAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"); |
| 179 | const event = { |
| 180 | id: "event-archive", |
| 181 | type: "thread.archived", |
| 182 | threadId, |
| 183 | occurredAt: archivedAt, |
| 184 | payload: { ...emptyProjection.thread, archivedAt, updatedAt: archivedAt }, |
| 185 | } as OrchestrationV2DomainEvent; |
| 186 | |
| 187 | const next = applyOrchestrationV2ProjectionEvent(emptyProjection, event); |
| 188 | expect(next?.thread.archivedAt).toEqual(archivedAt); |
| 189 | expect(next?.updatedAt).toEqual(archivedAt); |
| 190 | }); |
| 191 | |
| 192 | it("preserves visible row identity when run updates do not change membership", () => { |
| 193 | const item = commandItem("item-stable"); |
| 194 | const visibleTurnItems = [ |
| 195 | { |
| 196 | position: 0, |
| 197 | visibility: "local" as const, |
| 198 | sourceThreadId: threadId, |
| 199 | sourceItemId: item.id, |
| 200 | item, |
| 201 | }, |
| 202 | ]; |
| 203 | const projection = { |
| 204 | ...emptyProjection, |
| 205 | runs: [run], |
| 206 | turnItems: [item], |
| 207 | visibleTurnItems, |
| 208 | }; |
| 209 | const event = { |
| 210 | id: "event-run-update", |
| 211 | type: "run.updated", |
| 212 | threadId, |
| 213 | runId, |
| 214 | occurredAt: now, |
| 215 | payload: { ...run, status: "completed" }, |
| 216 | } as OrchestrationV2DomainEvent; |
| 217 | |
| 218 | const next = applyOrchestrationV2ProjectionEvent(projection, event); |
| 219 | expect(next?.visibleTurnItems).toBe(visibleTurnItems); |
| 220 | expect(next?.visibleTurnItems[0]).toBe(visibleTurnItems[0]); |
| 221 | }); |
| 222 | |
| 223 | it("replaces only the updated visible item and retains its deferred work cursor", () => { |
| 224 | const deferredWork = { cursor: "remaining-work", itemCount: 100, lastOrdinal: 101 }; |
| 225 | const first = { ...commandItem("item-first", "first"), deferredWork }; |
| 226 | const second = commandItem("item-second", "second"); |
| 227 | const firstRow = { |
| 228 | position: 0, |
| 229 | visibility: "local" as const, |
| 230 | sourceThreadId: threadId, |
| 231 | sourceItemId: first.id, |
| 232 | item: first, |
| 233 | }; |
| 234 | const secondRow = { |
| 235 | position: 1, |
| 236 | visibility: "local" as const, |
| 237 | sourceThreadId: threadId, |
| 238 | sourceItemId: second.id, |
| 239 | item: second, |
| 240 | }; |
| 241 | const updated = commandItem("item-first", "streamed output"); |
| 242 | const projection = { |
| 243 | ...emptyProjection, |
| 244 | runs: [run], |
| 245 | turnItems: [first, second], |
| 246 | visibleTurnItems: [firstRow, secondRow], |
| 247 | }; |
| 248 | const event = { |
| 249 | id: "event-item-update", |
| 250 | type: "turn-item.updated", |
| 251 | threadId, |
| 252 | runId, |
| 253 | occurredAt: now, |
| 254 | payload: updated, |
| 255 | } as OrchestrationV2DomainEvent; |
| 256 | |
| 257 | const next = applyOrchestrationV2ProjectionEvent(projection, event); |
| 258 | expect(next?.visibleTurnItems[0]?.item.deferredWork).toEqual(deferredWork); |
| 259 | expect(next?.visibleTurnItems).not.toBe(projection.visibleTurnItems); |
| 260 | expect(next?.visibleTurnItems[0]).not.toBe(firstRow); |
| 261 | expect(next?.visibleTurnItems[0]?.item).toEqual({ ...updated, deferredWork }); |
| 262 | expect(next?.visibleTurnItems[1]).toBe(secondRow); |
| 263 | }); |
| 264 | |
| 265 | it("inserts live turn items by authoritative ordinal", () => { |
| 266 | const queuedFuture = commandItem("item-queued-future", "queued", 300); |
| 267 | const activeAssistant = commandItem("item-active-assistant", "done", 201); |
| 268 | const queuedRow = { |
| 269 | position: 0, |
| 270 | visibility: "local" as const, |
| 271 | sourceThreadId: threadId, |
| 272 | sourceItemId: queuedFuture.id, |
| 273 | item: queuedFuture, |
| 274 | }; |
| 275 | const projection = { |
| 276 | ...emptyProjection, |
| 277 | runs: [run], |
| 278 | turnItems: [queuedFuture], |
| 279 | visibleTurnItems: [queuedRow], |
| 280 | }; |
| 281 | const event = { |
| 282 | id: "event-active-assistant", |
| 283 | type: "turn-item.updated", |
| 284 | threadId, |
| 285 | runId, |
| 286 | occurredAt: now, |
| 287 | payload: activeAssistant, |
| 288 | } as OrchestrationV2DomainEvent; |
| 289 | |
| 290 | const next = applyOrchestrationV2ProjectionEvent(projection, event); |
| 291 | expect(next?.visibleTurnItems.map((row) => row.item.id)).toEqual([ |
| 292 | activeAssistant.id, |
| 293 | queuedFuture.id, |
| 294 | ]); |
| 295 | expect(next?.visibleTurnItems.map((row) => row.position)).toEqual([0, 1]); |
| 296 | }); |
| 297 | |
| 298 | it("moves an updated item to its new ordinal and renumbers the rows it passes", () => { |
| 299 | const inherited = { |
| 300 | ...commandItem("item-inherited", "parent", 0), |
| 301 | threadId: ThreadId.make("parent"), |
| 302 | }; |
| 303 | const items = [ |
| 304 | commandItem("item-a", "a", 1), |
| 305 | commandItem("item-b", "b", 2), |
| 306 | commandItem("item-c", "c", 2), |
| 307 | ]; |
| 308 | const rows = [ |
| 309 | { |
| 310 | position: 0, |
| 311 | visibility: "inherited" as const, |
| 312 | sourceThreadId: inherited.threadId, |
| 313 | sourceItemId: inherited.id, |
| 314 | item: inherited, |
| 315 | }, |
| 316 | ...items.map((item, index) => ({ |
| 317 | position: index + 1, |
| 318 | visibility: "local" as const, |
| 319 | sourceThreadId: threadId, |
| 320 | sourceItemId: item.id, |
| 321 | item, |
| 322 | })), |
| 323 | ]; |
| 324 | const moved = commandItem("item-a", "moved", 2); |
| 325 | const next = applyOrchestrationV2ProjectionEvent( |
| 326 | { ...emptyProjection, runs: [run], turnItems: items, visibleTurnItems: rows }, |
| 327 | { |
| 328 | id: "event-move", |
| 329 | type: "turn-item.updated", |
| 330 | threadId, |
| 331 | runId, |
| 332 | occurredAt: now, |
| 333 | payload: moved, |
| 334 | } as OrchestrationV2DomainEvent, |
| 335 | ); |
| 336 | |
| 337 | // Equal ordinals order by id, so item-a stays ahead of item-b. |
| 338 | expect(next?.visibleTurnItems.map((row) => [row.item.id, row.position])).toEqual([ |
| 339 | ["item-inherited", 0], |
| 340 | ["item-a", 1], |
| 341 | ["item-b", 2], |
| 342 | ["item-c", 3], |
| 343 | ]); |
| 344 | expect(next?.visibleTurnItems[0]).toBe(rows[0]); |
| 345 | expect(next?.visibleTurnItems[1]?.item).toBe(moved); |
| 346 | expect(next?.turnItems.map((item) => item.id)).toEqual(["item-a", "item-b", "item-c"]); |
| 347 | |
| 348 | const later = commandItem("item-a", "later", 3); |
| 349 | const last = applyOrchestrationV2ProjectionEvent(next, { |
| 350 | id: "event-move-later", |
| 351 | type: "turn-item.updated", |
| 352 | threadId, |
| 353 | runId, |
| 354 | occurredAt: now, |
| 355 | payload: later, |
| 356 | } as OrchestrationV2DomainEvent); |
| 357 | expect(last?.visibleTurnItems.map((row) => [row.item.id, row.position])).toEqual([ |
| 358 | ["item-inherited", 0], |
| 359 | ["item-b", 1], |
| 360 | ["item-c", 2], |
| 361 | ["item-a", 3], |
| 362 | ]); |
| 363 | expect(last?.turnItems.at(0)).toBe(later); |
| 364 | }); |
| 365 | |
| 366 | describe("partial timeline", () => { |
| 367 | const queuedRunId = RunId.make("run-queued"); |
| 368 | const steerRunId = RunId.make("run-steer"); |
| 369 | const queuedRun = { ...run, id: queuedRunId, ordinal: 2, status: "queued" } as const; |
| 370 | const steerRun = { ...run, id: steerRunId, ordinal: 3, status: "completed" } as const; |
| 371 | const runItem = (id: string, itemRunId: RunId, ordinal: number) => ({ |
| 372 | ...commandItem(id, id, ordinal), |
| 373 | runId: itemRunId, |
| 374 | }); |
| 375 | const windowOf = ( |
| 376 | runs: ReadonlyArray<OrchestrationV2Run>, |
| 377 | items: ReadonlyArray<OrchestrationV2TurnItem>, |
| 378 | ): OrchestrationV2ThreadProjection => ({ |
| 379 | ...emptyProjection, |
| 380 | runs, |
| 381 | turnItems: items, |
| 382 | visibleTurnItems: items.map((item, position) => ({ |
| 383 | position, |
| 384 | visibility: "local" as const, |
| 385 | sourceThreadId: threadId, |
| 386 | sourceItemId: item.id, |
| 387 | item, |
| 388 | })), |
| 389 | }); |
| 390 | const itemEvent = (payload: OrchestrationV2TurnItem) => |
| 391 | ({ |
| 392 | id: `event-${payload.id}`, |
| 393 | type: "turn-item.updated", |
| 394 | threadId, |
| 395 | occurredAt: now, |
| 396 | payload, |
| 397 | }) as OrchestrationV2DomainEvent; |
| 398 | const runEvent = (payload: OrchestrationV2Run) => |
| 399 | ({ |
| 400 | id: `event-${payload.id}-${payload.status}`, |
| 401 | type: "run.updated", |
| 402 | threadId, |
| 403 | occurredAt: now, |
| 404 | payload, |
| 405 | }) as OrchestrationV2DomainEvent; |
| 406 | |
| 407 | it("shows a queued run that starts after a newer steer raised the watermark", () => { |
| 408 | const steerItems = [ |
| 409 | runItem("item-steer-user", steerRunId, 3_000_001), |
| 410 | runItem("item-steer-reply", steerRunId, 3_000_002), |
| 411 | ]; |
| 412 | let projection: OrchestrationV2ThreadProjection | null = windowOf( |
| 413 | [run, queuedRun, steerRun], |
| 414 | steerItems, |
| 415 | ); |
| 416 | let latestLocalTurnOrdinal = 3_000_002; |
| 417 | const queuedUser = runItem("item-queued-user", queuedRunId, 2_000_001); |
| 418 | const queuedReply = runItem("item-queued-reply", queuedRunId, 2_000_002); |
| 419 | for (const event of [ |
| 420 | itemEvent(queuedUser), |
| 421 | runEvent({ ...queuedRun, status: "running" }), |
| 422 | itemEvent(queuedReply), |
| 423 | runEvent({ ...queuedRun, status: "completed" }), |
| 424 | ]) { |
| 425 | projection = applyOrchestrationV2ProjectionEvent(projection, event, { |
| 426 | partialTimeline: true, |
| 427 | latestLocalTurnOrdinal, |
| 428 | }); |
| 429 | if (event.type === "turn-item.updated") { |
| 430 | latestLocalTurnOrdinal = Math.max(latestLocalTurnOrdinal, event.payload.ordinal); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | expect(projection?.visibleTurnItems.map((row) => row.item.id)).toEqual([ |
| 435 | queuedUser.id, |
| 436 | queuedReply.id, |
| 437 | ...steerItems.map((item) => item.id), |
| 438 | ]); |
| 439 | expect(projection?.turnItems.map((item) => item.id)).toContain(queuedReply.id); |
| 440 | }); |
| 441 | |
| 442 | it("keeps a running run's item when the window starts after its earlier items", () => { |
| 443 | const runningRun = { ...queuedRun, status: "running" } as const; |
| 444 | const steerItems = [ |
| 445 | runItem("item-steer-user", steerRunId, 3_000_001), |
| 446 | runItem("item-steer-reply", steerRunId, 3_000_002), |
| 447 | ]; |
| 448 | const projection = windowOf([run, runningRun, steerRun], steerItems); |
| 449 | const reply = runItem("item-running-reply", queuedRunId, 2_000_003); |
| 450 | |
| 451 | const next = applyOrchestrationV2ProjectionEvent(projection, itemEvent(reply), { |
| 452 | partialTimeline: true, |
| 453 | latestLocalTurnOrdinal: 3_000_002, |
| 454 | }); |
| 455 | |
| 456 | expect(next?.visibleTurnItems.map((row) => row.item.id)).toEqual([ |
| 457 | reply.id, |
| 458 | ...steerItems.map((item) => item.id), |
| 459 | ]); |
| 460 | }); |
| 461 | |
| 462 | it.each([ |
| 463 | { name: "keeps an item of a run already in the window", itemRunId: steerRunId, kept: true }, |
| 464 | { name: "drops an item of a finished run outside the window", itemRunId: runId, kept: false }, |
| 465 | ])("$name that arrives below the watermark", ({ itemRunId, kept }) => { |
| 466 | const first = runItem("item-first", steerRunId, 3_000_001); |
| 467 | const later = runItem("item-later", steerRunId, 3_000_003); |
| 468 | const late = runItem("item-late", itemRunId, 3_000_002); |
| 469 | const projection = windowOf([run, steerRun], [first, later]); |
| 470 | |
| 471 | const next = applyOrchestrationV2ProjectionEvent(projection, itemEvent(late), { |
| 472 | partialTimeline: true, |
| 473 | latestLocalTurnOrdinal: later.ordinal, |
| 474 | }); |
| 475 | if (kept) { |
| 476 | expect(next?.visibleTurnItems.map((row) => row.item.id)).toEqual([ |
| 477 | first.id, |
| 478 | late.id, |
| 479 | later.id, |
| 480 | ]); |
| 481 | } else { |
| 482 | expect(next).toBe(projection); |
| 483 | } |
| 484 | }); |
| 485 | }); |
| 486 | |
| 487 | it("hides a steer that fell back to the queue until its run starts", () => { |
| 488 | const queuedRunId = RunId.make("run-requeued-steer"); |
| 489 | const queuedRun = { ...run, id: queuedRunId, ordinal: 2, status: "queued" } as const; |
| 490 | const reply = commandItem("item-reply", "reply", 102); |
| 491 | const steer: OrchestrationV2TurnItem = { |
| 492 | id: TurnItemId.make("item-steer"), |
| 493 | threadId, |
| 494 | runId, |
| 495 | nodeId: null, |
| 496 | providerThreadId: null, |
| 497 | providerTurnId: null, |
| 498 | nativeItemRef: null, |
| 499 | parentItemId: null, |
| 500 | ordinal: 101, |
| 501 | status: "completed", |
| 502 | title: null, |
| 503 | startedAt: now, |
| 504 | completedAt: now, |
| 505 | updatedAt: now, |
| 506 | type: "user_message", |
| 507 | messageId: MessageId.make("message-steer"), |
| 508 | inputIntent: "steer", |
| 509 | text: "steer", |
| 510 | attachments: [], |
| 511 | createdBy: "user", |
| 512 | creationSource: "web", |
| 513 | }; |
| 514 | const event = (type: "run.updated" | "turn-item.updated", payload: unknown) => |
| 515 | ({ |
| 516 | id: `event-${type}`, |
| 517 | type, |
| 518 | threadId, |
| 519 | occurredAt: now, |
| 520 | payload, |
| 521 | }) as OrchestrationV2DomainEvent; |
| 522 | const apply = ( |
| 523 | projection: OrchestrationV2ThreadProjection | null, |
| 524 | events: ReadonlyArray<OrchestrationV2DomainEvent>, |
| 525 | options?: Parameters<typeof applyOrchestrationV2ProjectionEvent>[2], |
| 526 | ) => |
| 527 | events.reduce( |
| 528 | (next, item) => applyOrchestrationV2ProjectionEvent(next, item, options), |
| 529 | projection, |
| 530 | ); |
| 531 | const visibleIds = (projection: OrchestrationV2ThreadProjection | null) => |
| 532 | projection?.visibleTurnItems.map((row) => row.item.id); |
| 533 | |
| 534 | const accepted = apply(emptyProjection, [ |
| 535 | event("run.updated", { ...run, status: "running" }), |
| 536 | event("turn-item.updated", steer), |
| 537 | event("turn-item.updated", reply), |
| 538 | ]); |
| 539 | expect(visibleIds(accepted)).toEqual([steer.id, reply.id]); |
| 540 | |
| 541 | const queuedSteer = event("turn-item.updated", { |
| 542 | ...steer, |
| 543 | runId: queuedRunId, |
| 544 | inputIntent: "queued_turn", |
| 545 | status: "pending", |
| 546 | startedAt: null, |
| 547 | completedAt: null, |
| 548 | }); |
| 549 | const requeued = apply(accepted, [event("run.updated", queuedRun), queuedSteer]); |
| 550 | expect(visibleIds(requeued)).toEqual([reply.id]); |
| 551 | |
| 552 | // Servers publish the start's user item and run update in either order. |
| 553 | const starting = event("run.updated", { ...queuedRun, status: "starting" }); |
| 554 | const dispatchedSteer = event("turn-item.updated", { |
| 555 | ...steer, |
| 556 | runId: queuedRunId, |
| 557 | inputIntent: "queued_turn", |
| 558 | }); |
| 559 | expect(visibleIds(apply(requeued, [dispatchedSteer, starting]))).toEqual([steer.id, reply.id]); |
| 560 | expect(visibleIds(apply(requeued, [starting, dispatchedSteer]))).toEqual([steer.id, reply.id]); |
| 561 | const cancelled = event("run.updated", { ...queuedRun, status: "cancelled" }); |
| 562 | expect(visibleIds(apply(requeued, [cancelled]))).toEqual([reply.id]); |
| 563 | |
| 564 | // A bounded snapshot taken while queued omits the hidden item, and the |
| 565 | // newer reply puts it below the watermark. |
| 566 | const bounded = { |
| 567 | ...requeued!, |
| 568 | turnItems: requeued!.turnItems.filter((item) => item.id !== steer.id), |
| 569 | }; |
| 570 | const partial = { partialTimeline: true, latestLocalTurnOrdinal: reply.ordinal }; |
| 571 | const failed = event("run.updated", { ...queuedRun, status: "failed" }); |
| 572 | const failedSteer = event("turn-item.updated", { |
| 573 | ...steer, |
| 574 | runId: queuedRunId, |
| 575 | inputIntent: "queued_turn", |
| 576 | status: "failed", |
| 577 | startedAt: null, |
| 578 | }); |
| 579 | const retained = apply(bounded, [failedSteer], partial); |
| 580 | expect(visibleIds(retained)).toEqual([reply.id]); |
| 581 | expect(visibleIds(apply(retained, [failed], partial))).toEqual([steer.id, reply.id]); |
| 582 | // Leaving the queue never invents an item the client did not receive. |
| 583 | expect(visibleIds(apply(bounded, [failed], partial))).toEqual([reply.id]); |
| 584 | }); |
| 585 | |
| 586 | it("removes only hidden local items while preserving inherited rows", () => { |
| 587 | const inherited = commandItem("item-inherited"); |
| 588 | const local = commandItem("item-local"); |
| 589 | const inheritedRow = { |
| 590 | position: 0, |
| 591 | visibility: "inherited" as const, |
| 592 | sourceThreadId: ThreadId.make("thread-source"), |
| 593 | sourceItemId: inherited.id, |
| 594 | item: inherited, |
| 595 | }; |
| 596 | const localRow = { |
| 597 | position: 1, |
| 598 | visibility: "local" as const, |
| 599 | sourceThreadId: threadId, |
| 600 | sourceItemId: local.id, |
| 601 | item: local, |
| 602 | }; |
| 603 | const projection = { |
| 604 | ...emptyProjection, |
| 605 | runs: [run], |
| 606 | turnItems: [local], |
| 607 | visibleTurnItems: [inheritedRow, localRow], |
| 608 | }; |
| 609 | const event = { |
| 610 | id: "event-run-rollback", |
| 611 | type: "run.updated", |
| 612 | threadId, |
| 613 | runId, |
| 614 | occurredAt: now, |
| 615 | payload: { ...run, status: "rolled_back" }, |
| 616 | } as OrchestrationV2DomainEvent; |
| 617 | |
| 618 | const next = applyOrchestrationV2ProjectionEvent(projection, event); |
| 619 | expect(next?.visibleTurnItems).toEqual([inheritedRow]); |
| 620 | expect(next?.visibleTurnItems[0]).toBe(inheritedRow); |
| 621 | }); |
| 622 | }); |
| 623 | |
| 624 | it("does not scan every row against every run for a streaming item update", () => { |
| 625 | let runReads = 0; |
| 626 | const runs = Array.from({ length: 100 }, (_, index) => ({ |
| 627 | ...run, |
| 628 | get id() { |
| 629 | runReads++; |
| 630 | return RunId.make(`run-${index}`); |
| 631 | }, |
| 632 | })); |
| 633 | const items = Array.from({ length: 1000 }, (_, index) => |
| 634 | commandItem(`item-${index}`, "before", index), |
| 635 | ); |
| 636 | const projection = { |
| 637 | ...emptyProjection, |
| 638 | runs, |
| 639 | turnItems: items, |
| 640 | visibleTurnItems: items.map((item, position) => ({ |
| 641 | item, |
| 642 | position, |
| 643 | visibility: "local" as const, |
| 644 | sourceThreadId: threadId, |
| 645 | sourceItemId: item.id, |
| 646 | })), |
| 647 | }; |
| 648 | const payload = commandItem("item-999", "after", 999); |
| 649 | const next = applyOrchestrationV2ProjectionEvent(projection, { |
| 650 | id: "stream-update", |
| 651 | type: "turn-item.updated", |
| 652 | threadId, |
| 653 | occurredAt: now, |
| 654 | payload, |
| 655 | } as OrchestrationV2DomainEvent); |
| 656 | expect(next?.visibleTurnItems.at(-1)?.item).toBe(payload); |
| 657 | expect(next?.visibleTurnItems[0]).toBe(projection.visibleTurnItems[0]); |
| 658 | expect(runReads).toBeLessThanOrEqual(100); |
| 659 | }); |
| 660 | |
| 661 | it("does not scan ordering or positions for a streamed replacement", () => { |
| 662 | const size = 100; |
| 663 | let ordinalReads = 0; |
| 664 | let positionReads = 0; |
| 665 | const items = Array.from({ length: size }, (_, index) => ({ |
| 666 | ...commandItem(`item-${index}`, "before", index), |
| 667 | get ordinal() { |
| 668 | ordinalReads++; |
| 669 | return index; |
| 670 | }, |
| 671 | })); |
| 672 | const rows = items.map((item, index) => ({ |
| 673 | item, |
| 674 | get position() { |
| 675 | positionReads++; |
| 676 | return index; |
| 677 | }, |
| 678 | visibility: "local" as const, |
| 679 | sourceThreadId: threadId, |
| 680 | sourceItemId: item.id, |
| 681 | })); |
| 682 | const payload = commandItem(`item-${size - 1}`, "after", size - 1); |
| 683 | const next = applyOrchestrationV2ProjectionEvent( |
| 684 | { ...emptyProjection, runs: [run], turnItems: items, visibleTurnItems: rows }, |
| 685 | { |
| 686 | id: "stable-replacement", |
| 687 | type: "turn-item.updated", |
| 688 | threadId, |
| 689 | occurredAt: now, |
| 690 | payload, |
| 691 | } as OrchestrationV2DomainEvent, |
| 692 | ); |
| 693 | |
| 694 | expect(ordinalReads).toBeLessThanOrEqual(2); |
| 695 | expect(positionReads).toBeLessThanOrEqual(2); |
| 696 | expect(next?.visibleTurnItems.at(-1)).toMatchObject({ item: payload, position: size - 1 }); |
| 697 | expect(next?.visibleTurnItems[0]).toBe(rows[0]); |
| 698 | expect(items.at(-1)).toMatchObject({ output: "before" }); |
| 699 | expect(rows.at(-1)?.item).toBe(items.at(-1)); |
| 700 | }); |
| 701 | |
| 702 | it("rechecks visibility when an existing item changes runs without changing ordinal", () => { |
| 703 | const first = commandItem("item-first", "first", 1); |
| 704 | const second = commandItem("item-second", "second", 2); |
| 705 | const rolledBackRun = { ...run, id: RunId.make("rolled-back"), status: "rolled_back" as const }; |
| 706 | const projection = { |
| 707 | ...emptyProjection, |
| 708 | runs: [run, rolledBackRun], |
| 709 | turnItems: [first, second], |
| 710 | visibleTurnItems: [first, second].map((item, position) => ({ |
| 711 | item, |
| 712 | position, |
| 713 | visibility: "local" as const, |
| 714 | sourceThreadId: threadId, |
| 715 | sourceItemId: item.id, |
| 716 | })), |
| 717 | }; |
| 718 | const update = (payload: OrchestrationV2TurnItem) => |
| 719 | ({ |
| 720 | id: "visibility-update", |
| 721 | type: "turn-item.updated", |
| 722 | threadId, |
| 723 | occurredAt: now, |
| 724 | payload, |
| 725 | }) as OrchestrationV2DomainEvent; |
| 726 | const hidden = applyOrchestrationV2ProjectionEvent( |
| 727 | projection, |
| 728 | update({ |
| 729 | ...first, |
| 730 | runId: rolledBackRun.id, |
| 731 | }), |
| 732 | ); |
| 733 | expect(hidden?.visibleTurnItems.map((row) => [row.item.id, row.position])).toEqual([ |
| 734 | [second.id, 0], |
| 735 | ]); |
| 736 | const restored = applyOrchestrationV2ProjectionEvent(hidden, update(first)); |
| 737 | expect(restored?.visibleTurnItems.map((row) => [row.item.id, row.position])).toEqual([ |
| 738 | [first.id, 0], |
| 739 | [second.id, 1], |
| 740 | ]); |
| 741 | expect(projection.visibleTurnItems[0]?.item).toBe(first); |
| 742 | }); |
| 743 | |
| 744 | it("restores deduped bodies from the records it already holds, also in a partial timeline", () => { |
| 745 | const messageId = MessageId.make("message-deduped"); |
| 746 | const subagentId = NodeId.make("node-deduped"); |
| 747 | const reply = { |
| 748 | ...commandItem("item-reply", "", 400), |
| 749 | type: "assistant_message", |
| 750 | messageId, |
| 751 | text: "the full reply", |
| 752 | streaming: false, |
| 753 | } as unknown as OrchestrationV2TurnItem; |
| 754 | const subagent = { |
| 755 | id: subagentId, |
| 756 | threadId, |
| 757 | runId, |
| 758 | parentNodeId: NodeId.make("node-root"), |
| 759 | origin: "app_owned", |
| 760 | createdBy: "agent", |
| 761 | driver: "codex", |
| 762 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 763 | providerThreadId: null, |
| 764 | childThreadId: null, |
| 765 | nativeTaskRef: null, |
| 766 | prompt: "the task brief", |
| 767 | title: null, |
| 768 | model: null, |
| 769 | status: "running", |
| 770 | result: null, |
| 771 | startedAt: now, |
| 772 | completedAt: null, |
| 773 | updatedAt: now, |
| 774 | } as OrchestrationV2ThreadProjection["subagents"][number]; |
| 775 | const projection = { |
| 776 | ...emptyProjection, |
| 777 | runs: [run], |
| 778 | messages: [ |
| 779 | { |
| 780 | id: messageId, |
| 781 | threadId, |
| 782 | runId, |
| 783 | nodeId: null, |
| 784 | role: "assistant", |
| 785 | text: "the full reply", |
| 786 | attachments: [], |
| 787 | streaming: false, |
| 788 | createdBy: "agent", |
| 789 | creationSource: "provider", |
| 790 | createdAt: now, |
| 791 | updatedAt: now, |
| 792 | }, |
| 793 | ], |
| 794 | subagents: [subagent], |
| 795 | } as OrchestrationV2ThreadProjection; |
| 796 | const event = (type: string, payload: unknown) => |
| 797 | ({ |
| 798 | id: `event-${type}`, |
| 799 | type, |
| 800 | threadId, |
| 801 | runId, |
| 802 | occurredAt: now, |
| 803 | payload, |
| 804 | }) as OrchestrationV2DomainEvent; |
| 805 | |
| 806 | for (const options of [undefined, { partialTimeline: true, latestLocalTurnOrdinal: 300 }]) { |
| 807 | const withReply = applyOrchestrationV2ProjectionEvent( |
| 808 | projection, |
| 809 | event("turn-item.updated", { ...reply, text: "", textOmitted: true }), |
| 810 | options, |
| 811 | ); |
| 812 | expect(withReply?.turnItems).toEqual([reply]); |
| 813 | expect(withReply?.visibleTurnItems.map((row) => row.item)).toEqual([reply]); |
| 814 | } |
| 815 | const progressed = applyOrchestrationV2ProjectionEvent( |
| 816 | projection, |
| 817 | event("subagent.updated", { ...subagent, prompt: "", promptOmitted: true, progress: "step 2" }), |
| 818 | ); |
| 819 | expect(progressed?.subagents).toEqual([{ ...subagent, progress: "step 2" }]); |
| 820 | }); |
| 821 | |
| 822 | describe("provider-thread.updated", () => { |
| 823 | const providerThread = ( |
| 824 | id: string, |
| 825 | overrides: Partial<OrchestrationV2ProviderThread> = {}, |
| 826 | ): OrchestrationV2ProviderThread => |
| 827 | ({ |
| 828 | id: ProviderThreadId.make(id), |
| 829 | driver: "codex", |
| 830 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 831 | providerSessionId: null, |
| 832 | appThreadId: threadId, |
| 833 | ownerNodeId: null, |
| 834 | nativeThreadRef: null, |
| 835 | nativeConversationHeadRef: null, |
| 836 | status: "active", |
| 837 | firstRunOrdinal: 1, |
| 838 | lastRunOrdinal: 1, |
| 839 | handoffIds: [], |
| 840 | forkedFrom: null, |
| 841 | createdAt: now, |
| 842 | updatedAt: now, |
| 843 | ...overrides, |
| 844 | }) as OrchestrationV2ProviderThread; |
| 845 | const apply = (payload: unknown) => |
| 846 | applyOrchestrationV2ProjectionEvent(emptyProjection, { |
| 847 | id: "event-provider-thread", |
| 848 | type: "provider-thread.updated", |
| 849 | threadId, |
| 850 | occurredAt: now, |
| 851 | payload, |
| 852 | } as OrchestrationV2DomainEvent); |
| 853 | |
| 854 | it("moves the thread's active provider thread to the new one", () => { |
| 855 | // The shell already knows the old thread, as it would after a provider switch. |
| 856 | const projection = applyOrchestrationV2ProjectionEvent(emptyProjection, { |
| 857 | id: "event-previous-thread", |
| 858 | type: "provider-thread.updated", |
| 859 | threadId, |
| 860 | occurredAt: now, |
| 861 | payload: providerThread("provider-thread-previous"), |
| 862 | } as OrchestrationV2DomainEvent); |
| 863 | expect(projection?.thread.activeProviderThreadId).toBe( |
| 864 | ProviderThreadId.make("provider-thread-previous"), |
| 865 | ); |
| 866 | |
| 867 | const next = applyOrchestrationV2ProjectionEvent(projection, { |
| 868 | id: "event-next-thread", |
| 869 | type: "provider-thread.updated", |
| 870 | threadId, |
| 871 | occurredAt: now, |
| 872 | payload: providerThread("provider-thread-next"), |
| 873 | } as OrchestrationV2DomainEvent); |
| 874 | expect(next?.thread.activeProviderThreadId).toBe(ProviderThreadId.make("provider-thread-next")); |
| 875 | }); |
| 876 | |
| 877 | it("keeps the active provider thread for queued placeholders and other threads", () => { |
| 878 | const queued = apply( |
| 879 | providerThread("provider-thread-queued", { |
| 880 | status: "not_loaded", |
| 881 | firstRunOrdinal: null, |
| 882 | providerSessionId: null, |
| 883 | }), |
| 884 | ); |
| 885 | expect(queued?.thread.activeProviderThreadId).toBeNull(); |
| 886 | |
| 887 | const foreign = apply( |
| 888 | providerThread("provider-thread-foreign", { appThreadId: ThreadId.make("other-thread") }), |
| 889 | ); |
| 890 | expect(foreign?.thread.activeProviderThreadId).toBeNull(); |
| 891 | expect(foreign?.providerThreads.map((thread) => thread.id)).toEqual([ |
| 892 | ProviderThreadId.make("provider-thread-foreign"), |
| 893 | ]); |
| 894 | }); |
| 895 | }); |