Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationCache.test.ts

186 lines6.5 KB

1import { describe, expect, it } from "@effect/vitest";
2import {
3 CommandId,
4 EnvironmentId,
5 MessageId,
6 EventId,
7 TurnItemId,
8 RuntimeRequestId,
9 type OrchestrationV2ShellSnapshot,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as Option from "effect/Option";
14import * as Schema from "effect/Schema";
15​
16import {
17 ORCHESTRATION_CACHE_SCHEMA_VERSION,
18 StoredOrchestrationShellSnapshot,
19 StoredOrchestrationThreadSnapshot,
20 decodeOrDiscardOrchestrationCache,
21} from "./orchestrationCache.ts";
22import {
23 v2Now,
24 v2Projection,
25 v2ShellSnapshot,
26 v2ThreadShell,
27 v2ThreadId,
28} from "../state/orchestrationV2TestFixtures.ts";
29​
30import { applyOrchestrationV2ProjectionEvent } from "../state/orchestrationV2Projection.ts";
31​
32const environmentId = EnvironmentId.make("environment-cache-test");
33const StoredShellSnapshotJson = Schema.fromJsonString(StoredOrchestrationShellSnapshot);
34const StoredThreadSnapshotJson = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
35const encodeStoredShellSnapshot = Schema.encodeSync(StoredOrchestrationShellSnapshot);
36const decodeStoredShellSnapshotSync = Schema.decodeUnknownSync(StoredOrchestrationShellSnapshot);
37const encodeStoredShellSnapshotJson = Schema.encodeSync(StoredShellSnapshotJson);
38const decodeStoredShellSnapshotJson = Schema.decodeUnknownSync(StoredShellSnapshotJson);
39const encodeStoredThreadSnapshotJson = Schema.encodeSync(StoredThreadSnapshotJson);
40const decodeStoredThreadSnapshotJson = Schema.decodeUnknownSync(StoredThreadSnapshotJson);
41​
42class TestCacheDecodeError extends Schema.TaggedError<TestCacheDecodeError>()(
43 "TestCacheDecodeError",
44 {
45 message: Schema.String,
46 },
47) {}
48​
49const shellSnapshotWithSummaries: OrchestrationV2ShellSnapshot = {
50 ...v2ShellSnapshot,
51 threads: [
52 {
53 ...v2ThreadShell,
54 pendingRuntimeRequest: {
55 id: RuntimeRequestId.make("runtime-request-v2"),
56 kind: "command",
57 createdAt: v2Now,
58 },
59 latestVisibleMessage: {
60 id: MessageId.make("message-v2"),
61 role: "assistant",
62 text: "Done",
63 updatedAt: v2Now,
64 },
65 latestUserMessageAt: v2Now,
66 settledAt: v2Now,
67 titleRegeneration: {
68 requestId: CommandId.make("title-regeneration-v2"),
69 startedAt: v2Now,
70 },
71 },
72 ],
73};
74​
75describe("orchestration cache envelopes", () => {
76 it("preserves live provider notices through persisted thread snapshots", () => {
77 const item = {
78 id: TurnItemId.make("notice-cache"),
79 threadId: v2ThreadId,
80 runId: null,
81 nodeId: null,
82 providerThreadId: null,
83 providerTurnId: null,
84 nativeItemRef: null,
85 parentItemId: null,
86 ordinal: 1,
87 status: "completed" as const,
88 title: null,
89 startedAt: v2Now,
90 completedAt: v2Now,
91 updatedAt: v2Now,
92 type: "system_notice" as const,
93 message: "Safeguards flagged this message. Switched to Opus 4.8.",
94 };
95 const projection = applyOrchestrationV2ProjectionEvent(v2Projection, {
96 id: EventId.make("notice-cache-event"),
97 type: "turn-item.updated",
98 threadId: v2ThreadId,
99 occurredAt: v2Now,
100 payload: item,
101 });
102 expect(projection).not.toBeNull();
103 if (projection === null) throw new Error("Expected live notice projection");
104 const encoded = encodeStoredThreadSnapshotJson({
105 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
106 environmentId,
107 threadId: v2ThreadId,
108 snapshot: { snapshotSequence: 5, projection },
109 });
110 const decoded = decodeStoredThreadSnapshotJson(encoded).snapshot.projection;
111 expect(decoded.turnItems.find((entry) => entry.id === item.id)).toEqual(item);
112 expect(decoded.visibleTurnItems.find((entry) => entry.item.id === item.id)?.item).toEqual(item);
113 });
114​
115 it.effect("round-trips V2 shell and thread cache envelopes as JSON", () =>
116 Effect.sync(() => {
117 const encodedShell = encodeStoredShellSnapshotJson({
118 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
119 environmentId,
120 snapshot: shellSnapshotWithSummaries,
121 });
122 const shell = decodeStoredShellSnapshotJson(encodedShell);
123 const encodedThread = encodeStoredThreadSnapshotJson({
124 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
125 environmentId,
126 threadId: v2ThreadId,
127 snapshot: { snapshotSequence: 4, projection: v2Projection },
128 });
129 const thread = decodeStoredThreadSnapshotJson(encodedThread);
130 const [threadShell] = shell.snapshot.threads;
131​
132 expect(threadShell?.latestVisibleMessage?.text).toBe("Done");
133 expect(
134 threadShell?.latestVisibleMessage === null ||
135 threadShell?.latestVisibleMessage === undefined
136 ? null
137 : DateTime.formatIso(threadShell.latestVisibleMessage.updatedAt),
138 ).toBe("2026-06-20T00:00:00.000Z");
139 expect(
140 threadShell?.latestUserMessageAt === null || threadShell?.latestUserMessageAt === undefined
141 ? null
142 : DateTime.formatIso(threadShell.latestUserMessageAt),
143 ).toBe("2026-06-20T00:00:00.000Z");
144 expect(
145 threadShell?.settledAt === null || threadShell?.settledAt === undefined
146 ? null
147 : DateTime.formatIso(threadShell.settledAt),
148 ).toBe("2026-06-20T00:00:00.000Z");
149 expect(
150 threadShell?.titleRegeneration === null || threadShell?.titleRegeneration === undefined
151 ? null
152 : DateTime.formatIso(threadShell.titleRegeneration.startedAt),
153 ).toBe("2026-06-20T00:00:00.000Z");
154 expect(DateTime.formatIso(thread.snapshot.projection.thread.updatedAt)).toBe(
155 "2026-06-20T00:00:00.000Z",
156 );
157 expect(thread.snapshot.projection).toEqual(v2Projection);
158 expect(thread.snapshot.snapshotSequence).toBe(4);
159 }),
160 );
161​
162 it.effect("discards V1-versioned cache envelopes after a decode failure", () =>
163 Effect.gen(function* () {
164 let discardCount = 0;
165 const encodedShell = encodeStoredShellSnapshot({
166 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
167 environmentId,
168 snapshot: v2ShellSnapshot,
169 });
170 const decoded = Effect.try({
171 try: () => decodeStoredShellSnapshotSync({ ...encodedShell, schemaVersion: 1 }),
172 catch: (cause) => new TestCacheDecodeError({ message: String(cause) }),
173 }).pipe(Effect.map(Option.some));
174​
175 const result = yield* decodeOrDiscardOrchestrationCache(
176 decoded,
177 Effect.sync(() => {
178 discardCount += 1;
179 }),
180 );
181​
182 expect(Option.isNone(result)).toBe(true);
183 expect(discardCount).toBe(1);
184 }),
185 );
186});