@@ -24,7 +24,9 @@ import * as Schema from "effect/Schema"; |
24 | 24 | import * as Sink from "effect/Sink"; |
25 | 25 | import * as Stream from "effect/Stream"; |
26 | 26 | import * as TestClock from "effect/testing/TestClock"; |
| 27 | +import * as ChildProcess from "effect/process/ChildProcess"; |
27 | 28 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 29 | +import { expect } from "vite-plus/test"; |
28 | 30 | import { makePiAdapterV2, PI_PROVIDER } from "./adapter.ts"; |
29 | 31 |
|
30 | 32 | const layerTest = Layer.mergeAll( |
@@ -64,6 +66,7 @@ const decodeRequest = Schema.decodeUnknownSync( |
64 | 66 | ); |
65 | 67 |
|
66 | 68 | it.effect.each([ |
| 69 | + "fork across a legacy session switch", |
67 | 70 | "earlier checkpoint", |
68 | 71 | "last checkpoint", |
69 | 72 | "uncaptured handoff", |
@@ -112,65 +115,76 @@ it.effect.each([ |
112 | 115 | ); |
113 | 116 | } |
114 | 117 |
|
115 | | - const output = yield* Queue.unbounded<Uint8Array>(); |
116 | 118 | const switchApplied = yield* Deferred.make<void>(); |
117 | 119 | const sessionRequests: { readonly type: string; readonly file: string }[] = []; |
118 | | - let liveFile = currentFile; |
119 | | - let pending = ""; |
120 | | - const encoder = new TextEncoder(); |
121 | | - const decoder = new TextDecoder(); |
122 | | - const stdin = Sink.forEach((chunk: Uint8Array) => |
| 120 | + let attachedFile = currentFile; |
| 121 | + const spawner = ChildProcessSpawner.make((command) => |
123 | 122 | Effect.gen(function* () { |
124 | | - const lines = (pending + decoder.decode(chunk, { stream: true })).split("\n"); |
125 | | - pending = lines.pop() ?? ""; |
126 | | - for (const line of lines) { |
127 | | - const request = decodeRequest(line); |
128 | | - const entries = sessions.get(liveFile) ?? []; |
129 | | - let data: unknown = {}; |
130 | | - let error: string | undefined; |
131 | | - if (request.type === "get_state") data = { sessionFile: liveFile }; |
132 | | - if (request.type === "get_entries") |
133 | | - data = { entries, leafId: entries.at(-1)?.id ?? null }; |
134 | | - if (request.type === "switch_session") { |
135 | | - if (request.sessionPath !== undefined && sessions.has(request.sessionPath)) { |
136 | | - liveFile = request.sessionPath; |
137 | | - sessionRequests.push({ type: "switch_session", file: liveFile }); |
138 | | - if (scenario === "switch timed out after attaching") { |
139 | | - yield* Deferred.succeed(switchApplied, undefined); |
140 | | - continue; |
| 123 | + const output = yield* Queue.unbounded<Uint8Array>(); |
| 124 | + const args = ChildProcess.isStandardCommand(command) ? command.args.map(String) : []; |
| 125 | + const sourceFile = args.includes("--fork") ? args[args.indexOf("--fork") + 1] : undefined; |
| 126 | + const cloneFile = `${dir}/clone.jsonl`; |
| 127 | + let liveFile = currentFile; |
| 128 | + if (sourceFile !== undefined) { |
| 129 | + sessions.set(cloneFile, [...(sessions.get(sourceFile) ?? [])]); |
| 130 | + liveFile = cloneFile; |
| 131 | + } |
| 132 | + let pending = ""; |
| 133 | + const encoder = new TextEncoder(); |
| 134 | + const decoder = new TextDecoder(); |
| 135 | + const stdin = Sink.forEach((chunk: Uint8Array) => |
| 136 | + Effect.gen(function* () { |
| 137 | + const lines = (pending + decoder.decode(chunk, { stream: true })).split("\n"); |
| 138 | + pending = lines.pop() ?? ""; |
| 139 | + for (const line of lines) { |
| 140 | + const request = decodeRequest(line); |
| 141 | + const entries = sessions.get(liveFile) ?? []; |
| 142 | + let data: unknown = {}; |
| 143 | + let error: string | undefined; |
| 144 | + if (request.type === "get_state") data = { sessionFile: liveFile }; |
| 145 | + if (request.type === "get_entries") |
| 146 | + data = { entries, leafId: entries.at(-1)?.id ?? null }; |
| 147 | + if (request.type === "switch_session") { |
| 148 | + if (request.sessionPath !== undefined && sessions.has(request.sessionPath)) { |
| 149 | + liveFile = request.sessionPath; |
| 150 | + sessionRequests.push({ type: "switch_session", file: liveFile }); |
| 151 | + if (scenario === "switch timed out after attaching") { |
| 152 | + attachedFile = liveFile; |
| 153 | + yield* Deferred.succeed(switchApplied, undefined); |
| 154 | + continue; |
| 155 | + } |
| 156 | + data = { cancelled: false }; |
| 157 | + } else error = "Session not found"; |
| 158 | + } |
| 159 | + if (request.type === "fork") { |
| 160 | + const index = entries.findIndex(({ id }) => id === request.entryId); |
| 161 | + if (index === -1) error = "Invalid entry ID for forking"; |
| 162 | + else { |
| 163 | + sessionRequests.push({ type: "fork", file: liveFile }); |
| 164 | + const branchFile = |
| 165 | + sourceFile === undefined ? forkedFile : `${dir}/clone-branch.jsonl`; |
| 166 | + sessions.set(branchFile, entries.slice(0, index)); |
| 167 | + liveFile = branchFile; |
| 168 | + data = { cancelled: false }; |
| 169 | + } |
141 | 170 | } |
142 | | - data = { cancelled: false }; |
143 | | - } else error = "Session not found"; |
144 | | - } |
145 | | - if (request.type === "fork") { |
146 | | - const index = entries.findIndex(({ id }) => id === request.entryId); |
147 | | - if (index === -1) error = "Invalid entry ID for forking"; |
148 | | - else { |
149 | | - sessionRequests.push({ type: "fork", file: liveFile }); |
150 | | - sessions.set(forkedFile, entries.slice(0, index)); |
151 | | - liveFile = forkedFile; |
152 | | - data = { cancelled: false }; |
| 171 | + yield* Queue.offer( |
| 172 | + output, |
| 173 | + encoder.encode( |
| 174 | + JSON.stringify({ |
| 175 | + type: "response", |
| 176 | + id: request.id, |
| 177 | + command: request.type, |
| 178 | + success: error === undefined, |
| 179 | + data, |
| 180 | + error, |
| 181 | + }) + "\n", |
| 182 | + ), |
| 183 | + ); |
153 | 184 | } |
154 | | - } |
155 | | - yield* Queue.offer( |
156 | | - output, |
157 | | - encoder.encode( |
158 | | - JSON.stringify({ |
159 | | - type: "response", |
160 | | - id: request.id, |
161 | | - command: request.type, |
162 | | - success: error === undefined, |
163 | | - data, |
164 | | - error, |
165 | | - }) + "\n", |
166 | | - ), |
167 | | - ); |
168 | | - } |
169 | | - }), |
170 | | - ); |
171 | | - const spawner = ChildProcessSpawner.make(() => |
172 | | - Effect.succeed( |
173 | | - ChildProcessSpawner.makeHandle({ |
| 185 | + }), |
| 186 | + ); |
| 187 | + return ChildProcessSpawner.makeHandle({ |
174 | 188 | pid: ChildProcessSpawner.ProcessId(999_999_999), |
175 | 189 | exitCode: Effect.never, |
176 | 190 | isRunning: Effect.succeed(true), |
@@ -182,8 +196,8 @@ it.effect.each([ |
182 | 196 | all: Stream.empty, |
183 | 197 | getInputFd: () => Sink.drain, |
184 | 198 | getOutputFd: () => Stream.empty, |
185 | | - }), |
186 | | - ), |
| 199 | + }); |
| 200 | + }), |
187 | 201 | ); |
188 | 202 | const instanceId = ProviderInstanceId.make("pi"); |
189 | 203 | const adapter = yield* makePiAdapterV2({ |
@@ -218,7 +232,8 @@ it.effect.each([ |
218 | 232 | : ordinal === 3 && scenario === "uncaptured handoff" |
219 | 233 | ? ref("uncaptured", "weak") |
220 | 234 | : ref(`u${ordinal}`), |
221 | | - ...(scenario === "legacy turns" |
| 235 | + ...(scenario === "legacy turns" || |
| 236 | + (scenario === "fork across a legacy session switch" && ordinal < 3) |
222 | 237 | ? {} |
223 | 238 | : { |
224 | 239 | nativeThreadRef: ref( |
@@ -233,6 +248,30 @@ it.effect.each([ |
233 | 248 | ); |
234 | 249 | const shouldFork = scenario === "earlier checkpoint" || scenario === "legacy turns"; |
235 | 250 | const target = turns[shouldFork ? 0 : 1]!; |
| 251 | + if (scenario === "fork across a legacy session switch") { |
| 252 | + const targetThreadId = ThreadId.make("pi-fork"); |
| 253 | + const targetRuntime = yield* adapter.openSession({ |
| 254 | + threadId: targetThreadId, |
| 255 | + providerSessionId: ProviderSessionId.make("pi-fork"), |
| 256 | + modelSelection, |
| 257 | + runtimePolicy, |
| 258 | + }); |
| 259 | + yield* targetRuntime.events.pipe(Stream.runDrain, Effect.forkScoped); |
| 260 | + const forked = yield* targetRuntime.forkThread({ |
| 261 | + sourceProviderThread: providerThread, |
| 262 | + sourceProviderTurns: turns, |
| 263 | + providerTurnId: target.id, |
| 264 | + targetThreadId, |
| 265 | + }); |
| 266 | + expect(sessions.get(forked.nativeThreadRef?.nativeId ?? "")?.map(({ id }) => id)).toEqual([ |
| 267 | + "u1", |
| 268 | + "a1", |
| 269 | + "u2", |
| 270 | + "steer", |
| 271 | + "a2", |
| 272 | + ]); |
| 273 | + return; |
| 274 | + } |
236 | 275 | const rollback = runtime.rollbackThread({ |
237 | 276 | providerThread, |
238 | 277 | target: { |
@@ -246,7 +285,7 @@ it.effect.each([ |
246 | 285 | if (scenario === "switch timed out after attaching") { |
247 | 286 | const attempt = yield* rollback.pipe(Effect.result, Effect.forkChild); |
248 | 287 | yield* Deferred.await(switchApplied); |
249 | | - assert.equal(liveFile, oldFile); |
| 288 | + assert.equal(attachedFile, oldFile); |
250 | 289 | yield* TestClock.adjust("61 seconds"); |
251 | 290 | assert.equal((yield* Fiber.join(attempt))._tag, "Failure"); |
252 | 291 | yield* Fiber.join(events); |
@@ -264,7 +303,7 @@ it.effect.each([ |
264 | 303 | shouldFork ? "a1" : "a2", |
265 | 304 | ); |
266 | 305 | assert.deepEqual( |
267 | | - sessions.get(liveFile)?.map(({ id }) => id), |
| 306 | + sessions.get(restored.providerThread.nativeThreadRef?.nativeId ?? "")?.map(({ id }) => id), |
268 | 307 | shouldFork ? ["u1", "a1"] : ["u1", "a1", "u2", "steer", "a2"], |
269 | 308 | ); |
270 | 309 | assert.deepEqual( |
|