Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId, EventId, TurnItemId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import * as Schema from "effect/Schema";
6
7import {
8 ORCHESTRATION_CACHE_SCHEMA_VERSION,
9 ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
10 StoredOrchestrationShellSnapshot,
11 StoredOrchestrationThreadSnapshot,
12 decodeOrDiscardOrchestrationCache,
13} from "./orchestrationCache.ts";
14import {
15 v2Now,
16 v2Projection,
17 v2ShellSnapshot,
18 v2ThreadId,
19} from "../state/orchestrationV2TestFixtures.ts";
20
21import { applyOrchestrationV2ProjectionEvent } from "../state/orchestrationV2Projection.ts";
22
23const environmentId = EnvironmentId.make("environment-cache-test");
24const StoredThreadSnapshotJson = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
25const encodeStoredShellSnapshot = Schema.encodeSync(StoredOrchestrationShellSnapshot);
26const decodeStoredShellSnapshotSync = Schema.decodeUnknownSync(StoredOrchestrationShellSnapshot);
27const encodeStoredThreadSnapshotJson = Schema.encodeSync(StoredThreadSnapshotJson);
28const decodeStoredThreadSnapshotJson = Schema.decodeUnknownSync(StoredThreadSnapshotJson);
29
30class TestCacheDecodeError extends Schema.TaggedError<TestCacheDecodeError>()(
31 "TestCacheDecodeError",
32 {
33 message: Schema.String,
34 },
35) {}
36
37describe("orchestration cache envelopes", () => {
38 it("preserves live provider notices through persisted thread snapshots", () => {
39 const item = {
40 id: TurnItemId.make("notice-cache"),
41 threadId: v2ThreadId,
42 runId: null,
43 nodeId: null,
44 providerThreadId: null,
45 providerTurnId: null,
46 nativeItemRef: null,
47 parentItemId: null,
48 ordinal: 1,
49 status: "completed" as const,
50 title: null,
51 startedAt: v2Now,
52 completedAt: v2Now,
53 updatedAt: v2Now,
54 type: "system_notice" as const,
55 message: "Safeguards flagged this message. Switched to Opus 4.8.",
56 };
57 const projection = applyOrchestrationV2ProjectionEvent(v2Projection, {
58 id: EventId.make("notice-cache-event"),
59 type: "turn-item.updated",
60 threadId: v2ThreadId,
61 occurredAt: v2Now,
62 payload: item,
63 });
64 expect(projection).not.toBeNull();
65 if (projection === null) throw new Error("Expected live notice projection");
66 const encoded = encodeStoredThreadSnapshotJson({
67 schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
68 environmentId,
69 threadId: v2ThreadId,
70 snapshot: { snapshotSequence: 5, projection },
71 });
72 const decoded = decodeStoredThreadSnapshotJson(encoded).snapshot.projection;
73 expect(decoded.turnItems.find((entry) => entry.id === item.id)).toEqual(item);
74 expect(decoded.visibleTurnItems.find((entry) => entry.item.id === item.id)?.item).toEqual(item);
75 });
76
77 it("rejects thread snapshots saved before queued-run items were kept, but not shells", () => {
78 const staleThread = encodeStoredThreadSnapshotJson({
79 schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
80 environmentId,
81 threadId: v2ThreadId,
82 snapshot: { snapshotSequence: 4, projection: v2Projection },
83 }).replace(`"schemaVersion":${ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION}`, `"schemaVersion":3`);
84 expect(() => decodeStoredThreadSnapshotJson(staleThread)).toThrow();
85 const shell = encodeStoredShellSnapshot({
86 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
87 environmentId,
88 snapshot: v2ShellSnapshot,
89 });
90 expect(decodeStoredShellSnapshotSync(shell).schemaVersion).toBe(3);
91 });
92
93 it.effect("discards V1-versioned cache envelopes after a decode failure", () =>
94 Effect.gen(function* () {
95 let discardCount = 0;
96 const encodedShell = encodeStoredShellSnapshot({
97 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
98 environmentId,
99 snapshot: v2ShellSnapshot,
100 });
101 const decoded = Effect.try({
102 try: () => decodeStoredShellSnapshotSync({ ...encodedShell, schemaVersion: 1 }),
103 catch: (cause) => new TestCacheDecodeError({ message: String(cause) }),
104 }).pipe(Effect.map(Option.some));
105
106 const result = yield* decodeOrDiscardOrchestrationCache(
107 decoded,
108 Effect.sync(() => {
109 discardCount += 1;
110 }),
111 );
112
113 expect(Option.isNone(result)).toBe(true);
114 expect(discardCount).toBe(1);
115 }),
116 );
117});