GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test(pi): reproduce legacy forks after a session handoff · pingdotgg/t3code@5229e42

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/provider-pi/src/server
    • rollback.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/provider-pi/src/server/rollback.test.ts‎

Copy file name to clipboardExpand all lines: packages/provider-pi/src/server/rollback.test.ts
+98-59Lines changed: 98 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import * as Schema from "effect/Schema";
2424
import * as Sink from "effect/Sink";
2525
import * as Stream from "effect/Stream";
2626
import * as TestClock from "effect/testing/TestClock";
27+
import * as ChildProcess from "effect/process/ChildProcess";
2728
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
29+
import { expect } from "vite-plus/test";
2830
import { makePiAdapterV2, PI_PROVIDER } from "./adapter.ts";
2931

3032
const layerTest = Layer.mergeAll(
@@ -64,6 +66,7 @@ const decodeRequest = Schema.decodeUnknownSync(
6466
);
6567

6668
it.effect.each([
69+
"fork across a legacy session switch",
6770
"earlier checkpoint",
6871
"last checkpoint",
6972
"uncaptured handoff",
@@ -112,65 +115,76 @@ it.effect.each([
112115
);
113116
}
114117

115-
const output = yield* Queue.unbounded<Uint8Array>();
116118
const switchApplied = yield* Deferred.make<void>();
117119
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) =>
123122
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+
}
141170
}
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+
);
153184
}
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({
174188
pid: ChildProcessSpawner.ProcessId(999_999_999),
175189
exitCode: Effect.never,
176190
isRunning: Effect.succeed(true),
@@ -182,8 +196,8 @@ it.effect.each([
182196
all: Stream.empty,
183197
getInputFd: () => Sink.drain,
184198
getOutputFd: () => Stream.empty,
185-
}),
186-
),
199+
});
200+
}),
187201
);
188202
const instanceId = ProviderInstanceId.make("pi");
189203
const adapter = yield* makePiAdapterV2({
@@ -218,7 +232,8 @@ it.effect.each([
218232
: ordinal === 3 && scenario === "uncaptured handoff"
219233
? ref("uncaptured", "weak")
220234
: ref(`u${ordinal}`),
221-
...(scenario === "legacy turns"
235+
...(scenario === "legacy turns" ||
236+
(scenario === "fork across a legacy session switch" && ordinal < 3)
222237
? {}
223238
: {
224239
nativeThreadRef: ref(
@@ -233,6 +248,30 @@ it.effect.each([
233248
);
234249
const shouldFork = scenario === "earlier checkpoint" || scenario === "legacy turns";
235250
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+
}
236275
const rollback = runtime.rollbackThread({
237276
providerThread,
238277
target: {
@@ -246,7 +285,7 @@ it.effect.each([
246285
if (scenario === "switch timed out after attaching") {
247286
const attempt = yield* rollback.pipe(Effect.result, Effect.forkChild);
248287
yield* Deferred.await(switchApplied);
249-
assert.equal(liveFile, oldFile);
288+
assert.equal(attachedFile, oldFile);
250289
yield* TestClock.adjust("61 seconds");
251290
assert.equal((yield* Fiber.join(attempt))._tag, "Failure");
252291
yield* Fiber.join(events);
@@ -264,7 +303,7 @@ it.effect.each([
264303
shouldFork ? "a1" : "a2",
265304
);
266305
assert.deepEqual(
267-
sessions.get(liveFile)?.map(({ id }) => id),
306+
sessions.get(restored.providerThread.nativeThreadRef?.nativeId ?? "")?.map(({ id }) => id),
268307
shouldFork ? ["u1", "a1"] : ["u1", "a1", "u2", "steer", "a2"],
269308
);
270309
assert.deepEqual(

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.