@@ -164,6 +164,7 @@ interface FakePi { |
164 | 164 | readonly queueEntries: (data: unknown) => void; |
165 | 165 | readonly queueMessages: (data: unknown) => void; |
166 | 166 | readonly vetoNextSwitch: () => void; |
| 167 | + readonly failNextFork: () => void; |
167 | 168 | readonly queueState: (data: Record<string, unknown>) => void; |
168 | 169 | readonly setStreaming: (streaming: boolean) => void; |
169 | 170 | readonly setCompacting: (compacting: boolean) => void; |
@@ -237,6 +238,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () { |
237 | 238 | let failStateAfter: number | null = null; |
238 | 239 | let failThinking = false; |
239 | 240 | let vetoSwitch = false; |
| 241 | + let failFork = false; |
240 | 242 | let vetoNewSession = false; |
241 | 243 | let deferredLifecycle: string | undefined; |
242 | 244 | let sessionFile = FAKE_SESSION_FILE; |
@@ -354,6 +356,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () { |
354 | 356 | case "get_session_stats": |
355 | 357 | return { ...base, data: statsQueue.shift() ?? {} }; |
356 | 358 | case "fork": |
| 359 | + if (failFork) { |
| 360 | + failFork = false; |
| 361 | + return { ...base, success: false, error: "Invalid entry ID for forking" }; |
| 362 | + } |
357 | 363 | serviceTier = null; |
358 | 364 | return { ...base, data: { text: "Hello pi", cancelled: false } }; |
359 | 365 | case "clear_queue": |
@@ -498,6 +504,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () { |
498 | 504 | vetoNextSwitch: () => { |
499 | 505 | vetoSwitch = true; |
500 | 506 | }, |
| 507 | + failNextFork: () => { |
| 508 | + failFork = true; |
| 509 | + }, |
501 | 510 | queueState: (data) => stateQueue.push(data), |
502 | 511 | setStreaming: (streaming) => { |
503 | 512 | isStreaming = streaming; |
@@ -2806,6 +2815,16 @@ describe("PiAdapterV2", () => { |
2806 | 2815 | yield* fake.emit({ type: "agent_start" }); |
2807 | 2816 | yield* fake.emit({ type: "agent_end", messages: [], willRetry: false }); |
2808 | 2817 | yield* fake.emit({ type: "agent_settled" }); |
| 2818 | + const completed = yield* takeEvent( |
| 2819 | + (event) => |
| 2820 | + event.type === "provider_turn.updated" && event.providerTurn.status === "completed", |
| 2821 | + ); |
| 2822 | + assert.equal( |
| 2823 | + completed.type === "provider_turn.updated" |
| 2824 | + ? completed.providerTurn.nativeThreadRef?.nativeId |
| 2825 | + : undefined, |
| 2826 | + childFile, |
| 2827 | + ); |
2809 | 2828 | const idle = yield* takeEvent( |
2810 | 2829 | (event) => |
2811 | 2830 | event.type === "provider_thread.updated" && event.providerThread.status === "idle", |
@@ -2965,9 +2984,21 @@ describe("PiAdapterV2", () => { |
2965 | 2984 | ); |
2966 | 2985 |
|
2967 | 2986 | it.effect.each([ |
2968 | | - { historical: false, label: "the latest turn" }, |
2969 | | - { historical: true, label: "a historical turn" }, |
2970 | | - ])("natively forks $label into an independent session", ({ historical }) => |
| 2987 | + { historical: false, moved: false, split: false, label: "the latest turn" }, |
| 2988 | + { historical: true, moved: false, split: false, label: "a historical turn" }, |
| 2989 | + { |
| 2990 | + historical: true, |
| 2991 | + moved: true, |
| 2992 | + split: false, |
| 2993 | + label: "a historical turn after a session switch", |
| 2994 | + }, |
| 2995 | + { |
| 2996 | + historical: true, |
| 2997 | + moved: true, |
| 2998 | + split: true, |
| 2999 | + label: "the last turn before a switch to a fresh session", |
| 3000 | + }, |
| 3001 | + ])("natively forks $label into an independent session", ({ historical, moved, split }) => |
2971 | 3002 | Effect.gen(function* () { |
2972 | 3003 | const fake = yield* makeFakePi; |
2973 | 3004 | const forkFake = yield* makeFakePi; |
@@ -2984,12 +3015,22 @@ describe("PiAdapterV2", () => { |
2984 | 3015 | modelSelection: modelSelection("default"), |
2985 | 3016 | runtimePolicy, |
2986 | 3017 | }); |
| 3018 | + const freshSession = { |
| 3019 | + driver: PI_PROVIDER, |
| 3020 | + nativeId: "/fake/new-session.jsonl", |
| 3021 | + strength: "strong", |
| 3022 | + } as const; |
2987 | 3023 | const turn = (ordinal: number): OrchestrationV2ProviderTurn => ({ |
2988 | 3024 | id: ProviderTurnId.make(`turn-${ordinal}`), |
2989 | 3025 | providerThreadId: source.id, |
2990 | 3026 | nodeId: NodeId.make(`node-${ordinal}`), |
2991 | 3027 | runAttemptId: null, |
2992 | 3028 | nativeTurnRef: { driver: PI_PROVIDER, nativeId: `u${ordinal}`, strength: "strong" }, |
| 3029 | + ...(split && ordinal === 2 |
| 3030 | + ? { nativeThreadRef: freshSession } |
| 3031 | + : moved && source.nativeThreadRef !== null |
| 3032 | + ? { nativeThreadRef: source.nativeThreadRef } |
| 3033 | + : {}), |
2993 | 3034 | ordinal, |
2994 | 3035 | status: "completed", |
2995 | 3036 | startedAt: null, |
@@ -3005,7 +3046,7 @@ describe("PiAdapterV2", () => { |
3005 | 3046 | fake.queueState({ sessionFile: forkFile }); |
3006 | 3047 | const target = ThreadId.make("fork-target"); |
3007 | 3048 | const forked = yield* runtime.forkThread({ |
3008 | | - sourceProviderThread: source, |
| 3049 | + sourceProviderThread: moved ? { ...source, nativeThreadRef: freshSession } : source, |
3009 | 3050 | sourceProviderTurns: historical ? [turn(1), turn(2)] : [turn(1)], |
3010 | 3051 | providerTurnId: turn(1).id, |
3011 | 3052 | targetThreadId: target, |
@@ -3025,7 +3066,7 @@ describe("PiAdapterV2", () => { |
3025 | 3066 | .allRequests() |
3026 | 3067 | .filter((request) => request.type === "fork") |
3027 | 3068 | .map((request) => request.entryId), |
3028 | | - historical ? ["u2"] : [], |
| 3069 | + historical && !split ? ["u2"] : [], |
3029 | 3070 | ); |
3030 | 3071 | assert.isFalse( |
3031 | 3072 | fake.allRequests().some((request) => request.type === "fork" || request.type === "clone"), |
@@ -3061,6 +3102,64 @@ describe("PiAdapterV2", () => { |
3061 | 3102 | }).pipe(Effect.scoped, Effect.provide(layerTest)), |
3062 | 3103 | ); |
3063 | 3104 |
|
| 3105 | + it.effect("recovers a historical Pi fork recorded before turns stored the session file", () => |
| 3106 | + Effect.gen(function* () { |
| 3107 | + const fs = yield* FileSystem.FileSystem; |
| 3108 | + const dir = yield* fs.makeTempDirectoryScoped(); |
| 3109 | + const oldFile = `${dir}/original.jsonl`; |
| 3110 | + const unrelatedFile = `${dir}/unrelated.jsonl`; |
| 3111 | + const currentFile = `${dir}/current.jsonl`; |
| 3112 | + const header = { type: "session", version: 3 }; |
| 3113 | + const writeSession = (file: string, entries: ReadonlyArray<unknown>) => |
| 3114 | + fs.writeFileString( |
| 3115 | + file, |
| 3116 | + [header, ...entries].map((entry) => encodeJsonLine(entry)).join("\n") + "\n", |
| 3117 | + ); |
| 3118 | + yield* writeSession(oldFile, UNCAPTURED_TURN_TREE.entries); |
| 3119 | + yield* writeSession(unrelatedFile, [ |
| 3120 | + sessionMessage("u1", null, "user", 10), |
| 3121 | + sessionMessage("u2", null, "user", 110), |
| 3122 | + ]); |
| 3123 | + yield* writeSession(currentFile, [sessionMessage("new", null, "user", 120)]); |
| 3124 | + |
| 3125 | + const fake = yield* makeFakePi; |
| 3126 | + const forkFake = yield* makeFakePi; |
| 3127 | + const { runtime } = yield* openRuntime(fake, "default", THREAD_ID, SESSION_ID, forkFake); |
| 3128 | + const source = yield* runtime.ensureThread({ |
| 3129 | + threadId: THREAD_ID, |
| 3130 | + modelSelection: modelSelection("default"), |
| 3131 | + runtimePolicy, |
| 3132 | + }); |
| 3133 | + const forkFile = `${dir}/forked.jsonl`; |
| 3134 | + forkFake.failNextFork(); |
| 3135 | + forkFake.queueState({ sessionFile: forkFile }); |
| 3136 | + fake.queueState({ sessionFile: forkFile }); |
| 3137 | + const [target, boundary] = uncapturedTurns(source.id, { |
| 3138 | + startedAt: 100, |
| 3139 | + completedAt: 145, |
| 3140 | + }); |
| 3141 | + if (target === undefined || boundary === undefined) throw new Error("missing fork turns"); |
| 3142 | + const forked = yield* runtime.forkThread({ |
| 3143 | + sourceProviderThread: { |
| 3144 | + ...source, |
| 3145 | + nativeThreadRef: { driver: PI_PROVIDER, nativeId: currentFile, strength: "strong" }, |
| 3146 | + }, |
| 3147 | + sourceProviderTurns: [ |
| 3148 | + target, |
| 3149 | + { |
| 3150 | + ...boundary, |
| 3151 | + nativeTurnRef: { driver: PI_PROVIDER, nativeId: "u2", strength: "strong" }, |
| 3152 | + }, |
| 3153 | + ], |
| 3154 | + providerTurnId: target.id, |
| 3155 | + targetThreadId: ThreadId.make("fork-target"), |
| 3156 | + }); |
| 3157 | + const args = forkFake.lastSpawn().args; |
| 3158 | + assert.equal(args[args.indexOf("--fork") + 1], oldFile); |
| 3159 | + assert.equal(forked.nativeThreadRef?.nativeId, forkFile); |
| 3160 | + }).pipe(Effect.scoped, Effect.provide(layerTest)), |
| 3161 | + ); |
| 3162 | + |
3064 | 3163 | it.effect("natively forks before a turn whose start entry was never captured", () => |
3065 | 3164 | Effect.gen(function* () { |
3066 | 3165 | const fake = yield* makeFakePi; |
|