|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import { |
| 4 | + CheckpointId, |
4 | 5 | EnvironmentId, |
5 | 6 | NodeId, |
6 | 7 | ProviderInstanceId, |
@@ -1160,6 +1161,183 @@ describe("PiAdapterV2", () => { |
1160 | 1161 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
1161 | 1162 | ); |
1162 | 1163 |
|
| 1164 | + for (const summarize of [false, true]) { |
| 1165 | + it.effect( |
| 1166 | + `follows an extension rewind ${summarize ? "with" : "without"} a branch summary`, |
| 1167 | + () => |
| 1168 | + Effect.gen(function* () { |
| 1169 | + const fake = yield* makeFakePi; |
| 1170 | + const { runtime, takeEvent } = yield* openRuntime(fake); |
| 1171 | + const root = { type: "model_change", id: "root", parentId: null }; |
| 1172 | + const user = (id: string, parentId: string) => ({ |
| 1173 | + type: "message", |
| 1174 | + id, |
| 1175 | + parentId, |
| 1176 | + message: { role: "user", content: [{ type: "text", text: id }] }, |
| 1177 | + }); |
| 1178 | + const reply = (id: string, parentId: string) => ({ |
| 1179 | + type: "message", |
| 1180 | + id, |
| 1181 | + parentId, |
| 1182 | + message: { role: "assistant", content: [] }, |
| 1183 | + }); |
| 1184 | + fake.queueEntries({ entries: [root], leafId: "root" }); |
| 1185 | + const providerThread = yield* runtime.ensureThread({ |
| 1186 | + threadId: THREAD_ID, |
| 1187 | + modelSelection: modelSelection("default"), |
| 1188 | + runtimePolicy, |
| 1189 | + }); |
| 1190 | + // Runs one turn to its terminal. `listings` answer the turn's |
| 1191 | + // get_entries requests in order. |
| 1192 | + const settle = Effect.fnUntraced(function* ( |
| 1193 | + text: string, |
| 1194 | + runOrdinal: number, |
| 1195 | + ...listings: ReadonlyArray<unknown> |
| 1196 | + ) { |
| 1197 | + yield* startTurn(runtime, providerThread, "default", [], text, undefined, runOrdinal); |
| 1198 | + yield* fake.takeRequest("prompt"); |
| 1199 | + for (const listing of listings) fake.queueEntries(listing); |
| 1200 | + // Pi acks every prompt; a command-only prompt then settles from |
| 1201 | + // an idle probe instead of agent events. |
| 1202 | + yield* fake.emit({ type: "response", command: "prompt", success: true }); |
| 1203 | + if (!text.startsWith("/")) { |
| 1204 | + yield* fake.emit({ type: "agent_start" }); |
| 1205 | + yield* fake.emit({ type: "agent_settled" }); |
| 1206 | + } |
| 1207 | + const turn = yield* takeEvent( |
| 1208 | + (event) => |
| 1209 | + event.type === "provider_turn.updated" && event.providerTurn.status === "completed", |
| 1210 | + ); |
| 1211 | + const thread = yield* takeEvent( |
| 1212 | + (event) => |
| 1213 | + event.type === "provider_thread.updated" && event.providerThread.status === "idle", |
| 1214 | + ); |
| 1215 | + yield* takeEvent((event) => event.type === "turn.terminal"); |
| 1216 | + return { |
| 1217 | + turnRef: |
| 1218 | + turn.type === "provider_turn.updated" ? turn.providerTurn.nativeTurnRef : undefined, |
| 1219 | + head: |
| 1220 | + thread.type === "provider_thread.updated" |
| 1221 | + ? thread.providerThread.nativeConversationHeadRef?.nativeId |
| 1222 | + : undefined, |
| 1223 | + retained: |
| 1224 | + thread.type === "provider_thread.updated" |
| 1225 | + ? thread.retainedNativeTurnIds |
| 1226 | + : undefined, |
| 1227 | + }; |
| 1228 | + }); |
| 1229 | + |
| 1230 | + yield* settle("prompt A", 1, { |
| 1231 | + entries: [user("uA", "root"), reply("aA", "uA")], |
| 1232 | + leafId: "aA", |
| 1233 | + }); |
| 1234 | + const promptB = yield* settle("prompt B", 2, { |
| 1235 | + entries: [user("uB", "aA"), reply("aB", "uB")], |
| 1236 | + leafId: "aB", |
| 1237 | + }); |
| 1238 | + assert.equal(promptB.turnRef?.nativeId, "uB"); |
| 1239 | + assert.isUndefined(promptB.retained); |
| 1240 | + |
| 1241 | + // The extension moves the leaf back to A's reply. With a summary, |
| 1242 | + // pi appends a branch_summary child of that reply as the new leaf. |
| 1243 | + const rewoundLeaf = summarize ? "summary" : "aA"; |
| 1244 | + const summary = summarize |
| 1245 | + ? [{ type: "branch_summary", id: "summary", parentId: "aA" }] |
| 1246 | + : []; |
| 1247 | + const rewind = yield* settle( |
| 1248 | + "/rewind", |
| 1249 | + 3, |
| 1250 | + { entries: summary, leafId: rewoundLeaf }, |
| 1251 | + { |
| 1252 | + entries: [ |
| 1253 | + root, |
| 1254 | + user("uA", "root"), |
| 1255 | + reply("aA", "uA"), |
| 1256 | + user("uB", "aA"), |
| 1257 | + reply("aB", "uB"), |
| 1258 | + ...summary, |
| 1259 | + ], |
| 1260 | + leafId: rewoundLeaf, |
| 1261 | + }, |
| 1262 | + ); |
| 1263 | + assert.deepEqual(rewind.retained, ["uA"]); |
| 1264 | + assert.equal(rewind.head, rewoundLeaf); |
| 1265 | + if (summarize) { |
| 1266 | + // The summary is this turn's only entry, and fork cannot re-root |
| 1267 | + // before a non-user entry, so rollback may not pass it. |
| 1268 | + assert.equal(rewind.turnRef?.strength, "weak"); |
| 1269 | + } else { |
| 1270 | + // Nothing of this turn is on the branch; rollback can pass it. |
| 1271 | + assert.isNull(rewind.turnRef); |
| 1272 | + } |
| 1273 | + |
| 1274 | + // get_entries is append-ordered: after a rewind to an older leaf, |
| 1275 | + // the abandoned branch still comes first in the next window. |
| 1276 | + const promptC = yield* settle("prompt C", 4, { |
| 1277 | + entries: [ |
| 1278 | + ...(summarize ? [] : [user("uB", "aA"), reply("aB", "uB")]), |
| 1279 | + user("uC", rewoundLeaf), |
| 1280 | + reply("aC", "uC"), |
| 1281 | + ], |
| 1282 | + leafId: "aC", |
| 1283 | + }); |
| 1284 | + assert.deepEqual(promptC.turnRef, { |
| 1285 | + driver: PI_PROVIDER, |
| 1286 | + nativeId: "uC", |
| 1287 | + strength: "strong", |
| 1288 | + }); |
| 1289 | + assert.isUndefined(promptC.retained); |
| 1290 | + }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1291 | + ); |
| 1292 | + } |
| 1293 | + |
| 1294 | + it.effect("rolls back past turns that left nothing in the session tree", () => |
| 1295 | + Effect.gen(function* () { |
| 1296 | + const fake = yield* makeFakePi; |
| 1297 | + const { runtime } = yield* openRuntime(fake); |
| 1298 | + const providerThread = yield* runtime.ensureThread({ |
| 1299 | + threadId: THREAD_ID, |
| 1300 | + modelSelection: modelSelection("default"), |
| 1301 | + runtimePolicy, |
| 1302 | + }); |
| 1303 | + const turn = (ordinal: number, entryId: string | null): OrchestrationV2ProviderTurn => ({ |
| 1304 | + id: ProviderTurnId.make(`turn-${ordinal}`), |
| 1305 | + providerThreadId: providerThread.id, |
| 1306 | + nodeId: NodeId.make(`node-${ordinal}`), |
| 1307 | + runAttemptId: null, |
| 1308 | + nativeTurnRef: |
| 1309 | + entryId === null ? null : { driver: PI_PROVIDER, nativeId: entryId, strength: "strong" }, |
| 1310 | + ordinal, |
| 1311 | + status: "completed", |
| 1312 | + startedAt: null, |
| 1313 | + completedAt: null, |
| 1314 | + }); |
| 1315 | + const rollBackToFirst = (turns: ReadonlyArray<OrchestrationV2ProviderTurn>) => |
| 1316 | + runtime.rollbackThread({ |
| 1317 | + providerThread, |
| 1318 | + target: { |
| 1319 | + type: "provider_turn", |
| 1320 | + checkpointId: CheckpointId.make("checkpoint-1"), |
| 1321 | + appRunOrdinal: 1, |
| 1322 | + providerTurn: turns[0]!, |
| 1323 | + }, |
| 1324 | + providerThreadTurns: turns, |
| 1325 | + }); |
| 1326 | + const forkedEntries = () => |
| 1327 | + fake |
| 1328 | + .allRequests() |
| 1329 | + .filter((request) => request.type === "fork") |
| 1330 | + .map((request) => request.entryId); |
| 1331 | + |
| 1332 | + // Only a turn that left nothing follows the target: already there. |
| 1333 | + yield* rollBackToFirst([turn(1, "u1"), turn(2, null)]); |
| 1334 | + assert.deepEqual(forkedEntries(), []); |
| 1335 | + // Pi re-roots before the first later turn that left entries. |
| 1336 | + yield* rollBackToFirst([turn(1, "u1"), turn(2, null), turn(3, "u3")]); |
| 1337 | + assert.deepEqual(forkedEntries(), ["u3"]); |
| 1338 | + }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1339 | + ); |
| 1340 | + |
1163 | 1341 | it.effect("settles a command-only prompt from its deferred ack and idle probe", () => |
1164 | 1342 | Effect.gen(function* () { |
1165 | 1343 | const fake = yield* makeFakePi; |
|
0 commit comments