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 — fix(pi): rewind legacy checkpoints after a session switch (#42666) · Pipyakas/t3code@0841a93

File tree

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

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

Copy file name to clipboardExpand all lines: packages/provider-pi/src/server/adapter.test.ts
+193-2Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ const recordedIdleState = (sessionFile: string) => ({
225225
const PI_COMPACTING_PROMPT_ERROR =
226226
"Cannot submit a prompt while compaction is in progress. Wait for compaction to finish and retry.";
227227

228-
const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
228+
const makeFakePiWithResponses = Effect.fnUntraced(function* (
229+
override?: (request: PiRpcRecord, response: PiRpcRecord) => Effect.Effect<PiRpcRecord>,
230+
) {
229231
const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
230232
const requests = yield* Queue.unbounded<PiRpcRecord>();
231233
const entriesQueue: Array<unknown> = [];
@@ -406,7 +408,8 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
406408
continue;
407409
}
408410
const response = respondTo(record);
409-
if (response !== null) yield* emit(response);
411+
if (response !== null)
412+
yield* emit(override === undefined ? response : yield* override(record, response));
410413
}
411414
});
412415

@@ -524,6 +527,8 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
524527
} satisfies FakePi;
525528
});
526529

530+
const makeFakePi: Effect.Effect<FakePi> = makeFakePiWithResponses();
531+
527532
const makeAdapter = Effect.fnUntraced(function* (
528533
fake: FakePi,
529534
launchArgs = "",
@@ -4534,6 +4539,192 @@ describe("PiAdapterV2", () => {
45344539
}).pipe(Effect.scoped, Effect.provide(layerTest)),
45354540
);
45364541

4542+
it.effect.each([
4543+
"captured handoff",
4544+
"command-only handoff",
4545+
"legacy uncaptured boundary",
4546+
"adopted prompt",
4547+
"retry after creating a recovery fork",
4548+
"label before the discarded prompt",
4549+
"conflicting prior fork",
4550+
"captured boundary on a sibling branch",
4551+
"dangling checkpoint parent",
4552+
"different branch attached after recovery",
4553+
] as const)("restores a legacy checkpoint after a %s", (scenario) =>
4554+
Effect.gen(function* () {
4555+
const fs = yield* FileSystem.FileSystem;
4556+
const dir = yield* fs.makeTempDirectoryScoped();
4557+
const original = `${dir}/z-original.jsonl`;
4558+
const switched = `${dir}/current.jsonl`;
4559+
const priorFork = `${dir}/fork.jsonl`;
4560+
const retained = TURN_ONE_ENTRIES;
4561+
const labelled = scenario === "label before the discarded prompt";
4562+
const retry = labelled || scenario === "retry after creating a recovery fork";
4563+
const conflicting = scenario === "conflicting prior fork";
4564+
const sibling = scenario === "captured boundary on a sibling branch";
4565+
const changedBranch = scenario === "different branch attached after recovery";
4566+
const handoff = sessionMessage(
4567+
"u3",
4568+
sibling || scenario === "dangling checkpoint parent" ? "a1b" : null,
4569+
"user",
4570+
180,
4571+
);
4572+
type Entry = { readonly id: string; readonly parentId: string | null; readonly type: string };
4573+
const sessions = new Map<string, ReadonlyArray<Entry>>();
4574+
const write = (file: string, entries: ReadonlyArray<Entry>) =>
4575+
fs
4576+
.writeFileString(
4577+
file,
4578+
[{ type: "session", version: 3 }, ...entries]
4579+
.map((entry) => encodeJsonLine(entry))
4580+
.join("\n") + "\n",
4581+
)
4582+
.pipe(Effect.tap(() => Effect.sync(() => sessions.set(file, entries))));
4583+
const extra = sessionMessage("other", "a1b", "assistant", 45);
4584+
const discarded = sessionMessage("discarded", labelled ? "label" : "a1b", "user", 160);
4585+
const label = { type: "label", id: "label", parentId: "a1b", targetId: "a1", label: "keep" };
4586+
yield* write(
4587+
original,
4588+
changedBranch
4589+
? [...retained, extra, discarded]
4590+
: retry
4591+
? [...retained, ...(labelled ? [label] : []), discarded]
4592+
: retained,
4593+
);
4594+
yield* write(
4595+
switched,
4596+
sibling
4597+
? [...retained, handoff, sessionMessage("sibling", null, "assistant", 28)]
4598+
: scenario === "dangling checkpoint parent"
4599+
? [...retained.slice(1), handoff]
4600+
: [handoff],
4601+
);
4602+
if (conflicting) yield* write(priorFork, [...retained, extra]);
4603+
4604+
let liveFile = switched;
4605+
const fake = yield* makeFakePiWithResponses((request, response) =>
4606+
Effect.gen(function* () {
4607+
const entries = sessions.get(liveFile) ?? [];
4608+
switch (request.type) {
4609+
case "get_state":
4610+
return { ...response, data: recordedIdleState(liveFile) };
4611+
case "get_entries":
4612+
return {
4613+
...response,
4614+
data: {
4615+
entries,
4616+
leafId:
4617+
changedBranch && liveFile === original ? "other" : (entries.at(-1)?.id ?? null),
4618+
},
4619+
};
4620+
case "switch_session": {
4621+
const file = request.sessionPath;
4622+
if (typeof file !== "string" || !sessions.has(file))
4623+
throw new Error("Pi switch requested an unknown fixture session");
4624+
liveFile = file;
4625+
return response;
4626+
}
4627+
case "fork": {
4628+
let parentId = entries.find(({ id }) => id === request.entryId)?.parentId;
4629+
assert.isDefined(parentId);
4630+
const path: Array<Entry> = [];
4631+
while (parentId !== null) {
4632+
const entry = entries.find(({ id }) => id === parentId);
4633+
assert.isDefined(entry);
4634+
path.unshift(entry);
4635+
parentId = entry.parentId;
4636+
}
4637+
// Like Pi, drop labels from the path and re-add them with new ids at the leaf.
4638+
const branch: Array<Entry> = [];
4639+
for (const entry of path.filter(({ type }) => type !== "label"))
4640+
branch.push({ ...entry, parentId: branch.at(-1)?.id ?? null });
4641+
for (const entry of path.filter(({ type }) => type === "label"))
4642+
branch.push({
4643+
...entry,
4644+
id: `${entry.id}-copy`,
4645+
parentId: branch.at(-1)?.id ?? null,
4646+
});
4647+
yield* write(priorFork, branch).pipe(Effect.orDie);
4648+
liveFile = priorFork;
4649+
return response;
4650+
}
4651+
default:
4652+
return response;
4653+
}
4654+
}),
4655+
);
4656+
const { runtime } = yield* openRuntime(fake);
4657+
const providerThread = yield* runtime.ensureThread({
4658+
threadId: THREAD_ID,
4659+
modelSelection: modelSelection("default"),
4660+
runtimePolicy,
4661+
});
4662+
const [first, next] = uncapturedTurns(providerThread.id);
4663+
assert.isDefined(first);
4664+
assert.isDefined(next);
4665+
const target = { ...first, startedAt: at(scenario === "adopted prompt" ? 20 : 0) };
4666+
const boundary: OrchestrationV2ProviderTurn = {
4667+
...next,
4668+
nativeTurnRef:
4669+
scenario === "command-only handoff"
4670+
? null
4671+
: scenario === "legacy uncaptured boundary"
4672+
? next.nativeTurnRef
4673+
: { driver: PI_PROVIDER, strength: "strong", nativeId: "u3" },
4674+
...(scenario === "legacy uncaptured boundary"
4675+
? {}
4676+
: {
4677+
nativeThreadRef: { driver: PI_PROVIDER, strength: "strong", nativeId: switched },
4678+
}),
4679+
};
4680+
const rewind = runtime.rollbackThread({
4681+
providerThread: { ...providerThread, createdAt: at(0) },
4682+
target: {
4683+
type: "provider_turn",
4684+
checkpointId: CheckpointId.make("legacy-checkpoint"),
4685+
appRunOrdinal: target.ordinal,
4686+
providerTurn: target,
4687+
},
4688+
providerThreadTurns: [target, boundary],
4689+
});
4690+
const requests = () =>
4691+
fake.allRequests().flatMap((request) => {
4692+
if (request.type === "switch_session")
4693+
return [{ type: request.type, value: request.sessionPath }];
4694+
if (request.type === "fork") return [{ type: request.type, value: request.entryId }];
4695+
return [];
4696+
});
4697+
if (conflicting || changedBranch) {
4698+
assert.equal((yield* rewind.pipe(Effect.result))._tag, "Failure");
4699+
assert.deepEqual(
4700+
requests(),
4701+
changedBranch ? [{ type: "switch_session", value: original }] : [],
4702+
);
4703+
return;
4704+
}
4705+
const expected = retry || sibling ? priorFork : original;
4706+
const assertRestored = ({ providerThread: restored }: Effect.Success<typeof rewind>) => {
4707+
assert.equal(restored.nativeThreadRef?.nativeId, expected);
4708+
assert.equal(restored.nativeConversationHeadRef?.nativeId, labelled ? "label-copy" : "a1b");
4709+
assert.equal(liveFile, expected);
4710+
assert.deepEqual(
4711+
sessions.get(liveFile)?.filter(({ type }) => type !== "label"),
4712+
retained,
4713+
);
4714+
};
4715+
const expectedRequests = [
4716+
...(sibling ? [] : [{ type: "switch_session", value: original }]),
4717+
...(retry || sibling ? [{ type: "fork", value: sibling ? "u3" : "discarded" }] : []),
4718+
];
4719+
assertRestored(yield* rewind);
4720+
assert.deepEqual(requests(), expectedRequests);
4721+
if (retry) {
4722+
assertRestored(yield* rewind);
4723+
assert.deepEqual(requests(), expectedRequests);
4724+
}
4725+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
4726+
);
4727+
45374728
it.effect("rolls back past a turn whose start entry was never captured", () =>
45384729
Effect.gen(function* () {
45394730
const fake = yield* makeFakePi;

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.