The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | RunId, |
| 3 | ThreadId, |
| 4 | TurnItemId, |
| 5 | type OrchestrationV2ProjectedTurnItem, |
| 6 | type OrchestrationV2ThreadProjection, |
| 7 | type OrchestrationV2TurnItem, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { describe, expect, it } from "vite-plus/test"; |
| 10 | |
| 11 | import { |
| 12 | boundedSnapshotProjection, |
| 13 | omitLocalVisibleTurnItems, |
| 14 | restoreLocalVisibleTurnItems, |
| 15 | } from "./orchestrationV2BoundedSnapshot.ts"; |
| 16 | |
| 17 | const PARENT = ThreadId.make("thread:compact-parent"); |
| 18 | const CHILD = ThreadId.make("thread:compact-child"); |
| 19 | const RUN = RunId.make("run:compact"); |
| 20 | |
| 21 | function item( |
| 22 | threadId: ThreadId, |
| 23 | id: string, |
| 24 | ordinal: number, |
| 25 | type: "command_execution" | "run_interrupt_request" | "fork" = "command_execution", |
| 26 | ): OrchestrationV2TurnItem { |
| 27 | return { |
| 28 | id: TurnItemId.make(id), |
| 29 | type, |
| 30 | threadId, |
| 31 | runId: type === "command_execution" ? null : RUN, |
| 32 | nodeId: null, |
| 33 | providerThreadId: null, |
| 34 | providerTurnId: null, |
| 35 | nativeItemRef: null, |
| 36 | parentItemId: null, |
| 37 | ordinal, |
| 38 | status: "completed", |
| 39 | title: id, |
| 40 | input: id, |
| 41 | exitCode: 0, |
| 42 | startedAt: null, |
| 43 | completedAt: null, |
| 44 | updatedAt: "2026-10-01T00:00:00.000Z", |
| 45 | } as unknown as OrchestrationV2TurnItem; |
| 46 | } |
| 47 | |
| 48 | function row( |
| 49 | visibility: OrchestrationV2ProjectedTurnItem["visibility"], |
| 50 | sourceThreadId: ThreadId, |
| 51 | value: OrchestrationV2TurnItem, |
| 52 | ): Omit<OrchestrationV2ProjectedTurnItem, "position"> { |
| 53 | return { visibility, sourceThreadId, sourceItemId: value.id, item: value }; |
| 54 | } |
| 55 | |
| 56 | function projection(input: { |
| 57 | readonly visible: ReadonlyArray<Omit<OrchestrationV2ProjectedTurnItem, "position">>; |
| 58 | readonly turnItems: ReadonlyArray<OrchestrationV2TurnItem>; |
| 59 | }): OrchestrationV2ThreadProjection { |
| 60 | return { |
| 61 | thread: { id: CHILD }, |
| 62 | turnItems: [...input.turnItems], |
| 63 | visibleTurnItems: input.visible.map((value, position) => ({ ...value, position })), |
| 64 | } as unknown as OrchestrationV2ThreadProjection; |
| 65 | } |
| 66 | |
| 67 | function forkWindow() { |
| 68 | const inherited = [item(PARENT, "parent-1", 1), item(PARENT, "parent-2", 2)]; |
| 69 | const marker = item(PARENT, "fork-marker", 3, "fork"); |
| 70 | const local = [item(CHILD, "child-1", 1), item(CHILD, "child-2", 2), item(CHILD, "child-3", 3)]; |
| 71 | const retainedRequest = item(CHILD, "interrupt-request", 0, "run_interrupt_request"); |
| 72 | return { |
| 73 | local, |
| 74 | retainedRequest, |
| 75 | projection: projection({ |
| 76 | visible: [ |
| 77 | ...inherited.map((value) => row("inherited", PARENT, value)), |
| 78 | row("synthetic", PARENT, marker), |
| 79 | ...local.map((value) => row("local", CHILD, value)), |
| 80 | ], |
| 81 | turnItems: [...local, retainedRequest], |
| 82 | }), |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | describe("compact bounded snapshot turnItems", () => { |
| 87 | it("round-trips through JSON as a separately decoded client would see it", () => { |
| 88 | const { projection: full } = forkWindow(); |
| 89 | const wire = JSON.parse(JSON.stringify(omitLocalVisibleTurnItems(full))); |
| 90 | expect( |
| 91 | boundedSnapshotProjection({ projection: wire, turnItemsOmitLocalVisible: true }), |
| 92 | ).toEqual(JSON.parse(JSON.stringify(full))); |
| 93 | }); |
| 94 | |
| 95 | it("treats rows sourced from the thread itself as local", () => { |
| 96 | const own = item(CHILD, "own", 1); |
| 97 | const full = projection({ visible: [row("inherited", CHILD, own)], turnItems: [own] }); |
| 98 | const compact = omitLocalVisibleTurnItems(full); |
| 99 | expect(compact?.turnItems).toEqual([]); |
| 100 | expect(restoreLocalVisibleTurnItems(compact!)).toEqual(full); |
| 101 | }); |
| 102 | |
| 103 | it("refuses shapes it cannot restore exactly", () => { |
| 104 | const [first, second] = [item(CHILD, "a", 1), item(CHILD, "b", 2)]; |
| 105 | const visible = [row("local", CHILD, first), row("local", CHILD, second)]; |
| 106 | expect(omitLocalVisibleTurnItems(projection({ visible, turnItems: [second, first] }))).toBe( |
| 107 | null, |
| 108 | ); |
| 109 | expect( |
| 110 | omitLocalVisibleTurnItems(projection({ visible, turnItems: [{ ...first }, second] })), |
| 111 | ).toBe(null); |
| 112 | expect(omitLocalVisibleTurnItems(projection({ visible, turnItems: [first] }))).toBe(null); |
| 113 | }); |
| 114 | }); |