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

CodexThreadRevert.test.ts

187 lines5.8 KB

1import { assert, it } from "@effect/vitest";
2import * as CodexClient from "effect-codex-app-server/client";
3import * as CodexReplay from "effect-codex-app-server/replay";
4import * as Effect from "effect/Effect";
5​
6import {
7 revertCodexThread,
8 revertCodexThreadAfterTurn,
9 revertCodexThreadFromKnownTurns,
10} from "./CodexThreadRevert.ts";
11​
12const page = (
13 id: number,
14 cursor: string | null,
15 limit: number,
16 turns: string[],
17 nextCursor: string | null,
18) => [
19 {
20 type: "expect_outbound" as const,
21 frame: {
22 id,
23 method: "thread/turns/list",
24 params: {
25 threadId: "thread",
26 cursor,
27 limit,
28 sortDirection: "desc",
29 itemsView: "summary",
30 },
31 },
32 },
33 {
34 type: "emit_inbound" as const,
35 frame: {
36 id,
37 result: {
38 data: turns.map((id) => ({ id, items: [], status: "completed", error: null })),
39 nextCursor,
40 },
41 },
42 },
43];
44​
45it.effect("finds the revert boundary across pages of newest-first turns", () =>
46 Effect.gen(function* () {
47 const client = yield* CodexClient.CodexAppServerClient;
48 const result = yield* revertCodexThread(client, "thread", 3).pipe(Effect.flip);
49 assert.equal(result.message, "boundary reached");
50 }).pipe(
51 Effect.provide(
52 CodexReplay.layerReplay({
53 provider: "codex",
54 protocol: "codex.app-server",
55 version: "test",
56 scenario: "revert-pages",
57 entries: [
58 ...page(1, null, 3, ["newest", "middle"], "older"),
59 ...page(2, "older", 1, ["boundary"], null),
60 {
61 type: "expect_outbound",
62 frame: {
63 id: 3,
64 method: "thread/revert",
65 params: { threadId: "thread", beforeTurnId: "boundary" },
66 },
67 },
68 {
69 type: "emit_inbound",
70 frame: { id: 3, error: { code: -32603, message: "boundary reached" } },
71 },
72 ],
73 }),
74 ),
75 ),
76);
77​
78it.effect("rejects repeated cursors instead of looping or reverting incomplete history", () =>
79 Effect.gen(function* () {
80 const client = yield* CodexClient.CodexAppServerClient;
81 const result = yield* revertCodexThread(client, "thread", 3).pipe(Effect.flip);
82 assert.equal(result.message, "Thread history pagination repeated a cursor.");
83 }).pipe(
84 Effect.provide(
85 CodexReplay.layerReplay({
86 provider: "codex",
87 protocol: "codex.app-server",
88 version: "test",
89 scenario: "revert-loop",
90 entries: [...page(1, null, 3, ["newest"], "again"), ...page(2, "again", 2, [], "again")],
91 }),
92 ),
93 ),
94);
95​
96it.effect("reverts from the turn just newer than the kept turn across pages", () =>
97 Effect.gen(function* () {
98 const client = yield* CodexClient.CodexAppServerClient;
99 const result = yield* revertCodexThreadAfterTurn(client, "thread", "kept", 1).pipe(Effect.flip);
100 assert.equal(result.message, "boundary reached");
101 }).pipe(
102 Effect.provide(
103 CodexReplay.layerReplay({
104 provider: "codex",
105 protocol: "codex.app-server",
106 version: "test",
107 scenario: "revert-after-turn-pages",
108 entries: [
109 ...page(1, null, 2, ["newest", "unrecorded"], "older"),
110 ...page(2, "older", 2, ["kept", "oldest"], null),
111 {
112 type: "expect_outbound",
113 frame: {
114 id: 3,
115 method: "thread/revert",
116 params: { threadId: "thread", beforeTurnId: "unrecorded" },
117 },
118 },
119 {
120 type: "emit_inbound",
121 frame: { id: 3, error: { code: -32603, message: "boundary reached" } },
122 },
123 ],
124 }),
125 ),
126 ),
127);
128​
129it.effect("fails instead of reverting when the kept turn is not in the thread", () =>
130 Effect.gen(function* () {
131 const client = yield* CodexClient.CodexAppServerClient;
132 const result = yield* revertCodexThreadAfterTurn(client, "thread", "kept", 1).pipe(Effect.flip);
133 assert.equal(result.message, "Turn kept is not in Codex thread thread.");
134 }).pipe(
135 Effect.provide(
136 CodexReplay.layerReplay({
137 provider: "codex",
138 protocol: "codex.app-server",
139 version: "test",
140 scenario: "revert-after-missing-turn",
141 entries: page(1, null, 2, ["newest", "older"], null),
142 }),
143 ),
144 ),
145);
146​
147it.effect("rewinds from the oldest known turn across pages and keeps older turns on retry", () =>
148 Effect.gen(function* () {
149 const client = yield* CodexClient.CodexAppServerClient;
150 const rewind = revertCodexThreadFromKnownTurns(client, "thread", ["first-t3", "second-t3"]);
151 assert.equal((yield* rewind.pipe(Effect.flip)).message, "reverted");
152 assert.equal((yield* rewind.pipe(Effect.flip)).message, "read only");
153 }).pipe(
154 Effect.provide(
155 CodexReplay.layerReplay({
156 provider: "codex",
157 protocol: "codex.app-server",
158 version: "test",
159 scenario: "revert-known-turns-pages",
160 entries: [
161 ...page(1, null, 100, ["second-t3"], "first"),
162 ...page(2, "first", 100, ["first-t3"], "prefix"),
163 {
164 type: "expect_outbound",
165 frame: {
166 id: 3,
167 method: "thread/revert",
168 params: { threadId: "thread", beforeTurnId: "first-t3" },
169 },
170 },
171 { type: "emit_inbound", frame: { id: 3, error: { code: -32603, message: "reverted" } } },
172 ...page(4, null, 100, ["newer-kept"], "older-kept"),
173 ...page(5, "older-kept", 100, ["older-kept"], null),
174 {
175 type: "expect_outbound",
176 frame: {
177 id: 6,
178 method: "thread/read",
179 params: { threadId: "thread", includeTurns: false },
180 },
181 },
182 { type: "emit_inbound", frame: { id: 6, error: { code: -32603, message: "read only" } } },
183 ],
184 }),
185 ),
186 ),
187);