|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + MessageId, |
| 4 | + TurnItemId, |
| 5 | + type OrchestrationV2ThreadDetailSnapshot, |
| 6 | + type OrchestrationV2ThreadProjection, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { describe, expect, it } from "@effect/vitest"; |
| 9 | +import * as Effect from "effect/Effect"; |
| 10 | +import * as Schema from "effect/Schema"; |
| 11 | + |
| 12 | +import { v2Now, v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts"; |
| 13 | +import { |
| 14 | + ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION, |
| 15 | + StoredOrchestrationThreadSnapshot, |
| 16 | +} from "./orchestrationCache.ts"; |
| 17 | +import { |
| 18 | + COMPACT_THREAD_CACHE_SCHEMA_VERSION, |
| 19 | + decodeStoredThreadSnapshot, |
| 20 | + encodeStoredThreadSnapshot, |
| 21 | +} from "./threadCacheEncoding.ts"; |
| 22 | + |
| 23 | +const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 24 | +const BODY = "a body long enough to count ".repeat(4); |
| 25 | +const COMMAND_OUTPUT = "only the items carry this output"; |
| 26 | + |
| 27 | +const base = (id: string, ordinal: number) => ({ |
| 28 | + id: TurnItemId.make(id), |
| 29 | + threadId: v2ThreadId, |
| 30 | + runId: null, |
| 31 | + nodeId: null, |
| 32 | + providerThreadId: null, |
| 33 | + providerTurnId: null, |
| 34 | + nativeItemRef: null, |
| 35 | + parentItemId: null, |
| 36 | + ordinal, |
| 37 | + status: "completed" as const, |
| 38 | + title: null, |
| 39 | + startedAt: v2Now, |
| 40 | + completedAt: v2Now, |
| 41 | + updatedAt: v2Now, |
| 42 | +}); |
| 43 | + |
| 44 | +const messageId = MessageId.make("message-1"); |
| 45 | +const answer = { |
| 46 | + ...base("answer", 1), |
| 47 | + type: "assistant_message" as const, |
| 48 | + messageId, |
| 49 | + text: BODY, |
| 50 | + streaming: false, |
| 51 | +}; |
| 52 | +const command = { |
| 53 | + ...base("command", 2), |
| 54 | + type: "command_execution" as const, |
| 55 | + input: "pwd", |
| 56 | + output: COMMAND_OUTPUT, |
| 57 | + exitCode: 0, |
| 58 | +}; |
| 59 | +const rowOf = <Item extends { readonly id: TurnItemId }>(item: Item, position: number) => ({ |
| 60 | + position, |
| 61 | + visibility: "local" as const, |
| 62 | + sourceThreadId: v2ThreadId, |
| 63 | + sourceItemId: item.id, |
| 64 | + item, |
| 65 | +}); |
| 66 | +const projection: OrchestrationV2ThreadProjection = { |
| 67 | + ...v2Projection, |
| 68 | + messages: [ |
| 69 | + { |
| 70 | + id: messageId, |
| 71 | + threadId: v2ThreadId, |
| 72 | + runId: null, |
| 73 | + nodeId: null, |
| 74 | + role: "assistant", |
| 75 | + text: BODY, |
| 76 | + attachments: [], |
| 77 | + streaming: false, |
| 78 | + createdBy: "agent", |
| 79 | + creationSource: "provider", |
| 80 | + createdAt: v2Now, |
| 81 | + updatedAt: v2Now, |
| 82 | + }, |
| 83 | + ], |
| 84 | + turnItems: [answer, command], |
| 85 | + visibleTurnItems: [rowOf(answer, 0), rowOf(command, 1)], |
| 86 | +}; |
| 87 | +const snapshot: OrchestrationV2ThreadDetailSnapshot = { snapshotSequence: 3, projection }; |
| 88 | +const stored = { environmentId: ENVIRONMENT_ID, threadId: v2ThreadId, snapshot }; |
| 89 | +const encodeFull = Schema.encodeEffect(Schema.fromJsonString(StoredOrchestrationThreadSnapshot)); |
| 90 | +const occurrences = (text: string, part: string) => text.split(part).length - 1; |
| 91 | + |
| 92 | +describe("thread cache records", () => { |
| 93 | + it.effect("writes each item once", () => |
| 94 | + Effect.gen(function* () { |
| 95 | + const raw = yield* encodeStoredThreadSnapshot(stored); |
| 96 | + expect(JSON.parse(raw).schemaVersion).toBe(COMPACT_THREAD_CACHE_SCHEMA_VERSION); |
| 97 | + expect(occurrences(raw, COMMAND_OUTPUT)).toBe(1); |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
| 101 | + it.effect("decodes to the same projection, with one object per item", () => |
| 102 | + Effect.gen(function* () { |
| 103 | + const decoded = yield* decodeStoredThreadSnapshot(yield* encodeStoredThreadSnapshot(stored)); |
| 104 | + const { projection: loaded } = decoded.snapshot; |
| 105 | + expect(loaded).toEqual(projection); |
| 106 | + expect(loaded.visibleTurnItems[0]!.item).toBe(loaded.turnItems[0]); |
| 107 | + expect(loaded.visibleTurnItems[1]!.item).toBe(loaded.turnItems[1]); |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + it.effect("shares the copies of a record that could not be compacted", () => |
| 112 | + Effect.gen(function* () { |
| 113 | + // A hidden item between two visible ones: `turnItems` no longer starts with the visible rows. |
| 114 | + const hidden = { |
| 115 | + ...base("hidden", 3), |
| 116 | + type: "reasoning" as const, |
| 117 | + text: "not shown", |
| 118 | + streaming: false, |
| 119 | + }; |
| 120 | + const uncompacted = { |
| 121 | + ...projection, |
| 122 | + turnItems: [answer, hidden, command], |
| 123 | + }; |
| 124 | + const raw = yield* encodeStoredThreadSnapshot({ |
| 125 | + ...stored, |
| 126 | + snapshot: { ...snapshot, projection: uncompacted }, |
| 127 | + }); |
| 128 | + expect(JSON.parse(raw).snapshot.turnItemsOmitLocalVisible).toBe(false); |
| 129 | + const { projection: loaded } = (yield* decodeStoredThreadSnapshot(raw)).snapshot; |
| 130 | + expect(loaded).toEqual(uncompacted); |
| 131 | + expect(loaded.visibleTurnItems[0]!.item).toBe(loaded.turnItems[0]); |
| 132 | + expect(loaded.visibleTurnItems[1]!.item).toBe(loaded.turnItems[2]); |
| 133 | + }), |
| 134 | + ); |
| 135 | + |
| 136 | + it.effect("shares the matching copies of a full record", () => |
| 137 | + Effect.gen(function* () { |
| 138 | + const raw = yield* encodeFull({ |
| 139 | + schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION, |
| 140 | + ...stored, |
| 141 | + }); |
| 142 | + expect(occurrences(raw, COMMAND_OUTPUT)).toBe(2); |
| 143 | + const { projection: loaded } = (yield* decodeStoredThreadSnapshot(raw)).snapshot; |
| 144 | + expect(loaded).toEqual(projection); |
| 145 | + expect(loaded.visibleTurnItems[1]!.item).toBe(loaded.turnItems[1]); |
| 146 | + }), |
| 147 | + ); |
| 148 | + |
| 149 | + it.effect("keeps both copies of a full record when they differ", () => |
| 150 | + Effect.gen(function* () { |
| 151 | + const stale = { ...command, output: "an older output" }; |
| 152 | + const raw = yield* encodeFull({ |
| 153 | + schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION, |
| 154 | + ...stored, |
| 155 | + snapshot: { |
| 156 | + ...snapshot, |
| 157 | + projection: { |
| 158 | + ...projection, |
| 159 | + visibleTurnItems: [rowOf(answer, 0), rowOf(stale, 1)], |
| 160 | + }, |
| 161 | + }, |
| 162 | + }); |
| 163 | + const { projection: loaded } = (yield* decodeStoredThreadSnapshot(raw)).snapshot; |
| 164 | + expect(loaded.turnItems[1]).toMatchObject({ output: COMMAND_OUTPUT }); |
| 165 | + expect(loaded.visibleTurnItems[1]!.item).toMatchObject({ output: "an older output" }); |
| 166 | + }), |
| 167 | + ); |
| 168 | +}); |
0 commit comments