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): fork historical turns from their original session (#38315) · pingdotgg/t3code@2ebe7f8

File tree

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

‎packages/contracts/src/orchestrationV2.ts‎

Copy file name to clipboardExpand all lines: packages/contracts/src/orchestrationV2.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ export const OrchestrationV2ProviderTurn = Schema.Struct({
12321232
nodeId: NodeId,
12331233
runAttemptId: Schema.NullOr(RunAttemptId),
12341234
nativeTurnRef: Schema.NullOr(OrchestrationV2ProviderRef),
1235+
/** The native session that held this turn, when a provider can move a thread between sessions. */
1236+
nativeThreadRef: Schema.optional(OrchestrationV2ProviderRef),
12351237
ordinal: PositiveInt,
12361238
status: Schema.Literals([
12371239
"pending",
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
+104-5Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ interface FakePi {
164164
readonly queueEntries: (data: unknown) => void;
165165
readonly queueMessages: (data: unknown) => void;
166166
readonly vetoNextSwitch: () => void;
167+
readonly failNextFork: () => void;
167168
readonly queueState: (data: Record<string, unknown>) => void;
168169
readonly setStreaming: (streaming: boolean) => void;
169170
readonly setCompacting: (compacting: boolean) => void;
@@ -237,6 +238,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
237238
let failStateAfter: number | null = null;
238239
let failThinking = false;
239240
let vetoSwitch = false;
241+
let failFork = false;
240242
let vetoNewSession = false;
241243
let deferredLifecycle: string | undefined;
242244
let sessionFile = FAKE_SESSION_FILE;
@@ -354,6 +356,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
354356
case "get_session_stats":
355357
return { ...base, data: statsQueue.shift() ?? {} };
356358
case "fork":
359+
if (failFork) {
360+
failFork = false;
361+
return { ...base, success: false, error: "Invalid entry ID for forking" };
362+
}
357363
serviceTier = null;
358364
return { ...base, data: { text: "Hello pi", cancelled: false } };
359365
case "clear_queue":
@@ -498,6 +504,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
498504
vetoNextSwitch: () => {
499505
vetoSwitch = true;
500506
},
507+
failNextFork: () => {
508+
failFork = true;
509+
},
501510
queueState: (data) => stateQueue.push(data),
502511
setStreaming: (streaming) => {
503512
isStreaming = streaming;
@@ -2806,6 +2815,16 @@ describe("PiAdapterV2", () => {
28062815
yield* fake.emit({ type: "agent_start" });
28072816
yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
28082817
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+
);
28092828
const idle = yield* takeEvent(
28102829
(event) =>
28112830
event.type === "provider_thread.updated" && event.providerThread.status === "idle",
@@ -2965,9 +2984,21 @@ describe("PiAdapterV2", () => {
29652984
);
29662985

29672986
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 }) =>
29713002
Effect.gen(function* () {
29723003
const fake = yield* makeFakePi;
29733004
const forkFake = yield* makeFakePi;
@@ -2984,12 +3015,22 @@ describe("PiAdapterV2", () => {
29843015
modelSelection: modelSelection("default"),
29853016
runtimePolicy,
29863017
});
3018+
const freshSession = {
3019+
driver: PI_PROVIDER,
3020+
nativeId: "/fake/new-session.jsonl",
3021+
strength: "strong",
3022+
} as const;
29873023
const turn = (ordinal: number): OrchestrationV2ProviderTurn => ({
29883024
id: ProviderTurnId.make(`turn-${ordinal}`),
29893025
providerThreadId: source.id,
29903026
nodeId: NodeId.make(`node-${ordinal}`),
29913027
runAttemptId: null,
29923028
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+
: {}),
29933034
ordinal,
29943035
status: "completed",
29953036
startedAt: null,
@@ -3005,7 +3046,7 @@ describe("PiAdapterV2", () => {
30053046
fake.queueState({ sessionFile: forkFile });
30063047
const target = ThreadId.make("fork-target");
30073048
const forked = yield* runtime.forkThread({
3008-
sourceProviderThread: source,
3049+
sourceProviderThread: moved ? { ...source, nativeThreadRef: freshSession } : source,
30093050
sourceProviderTurns: historical ? [turn(1), turn(2)] : [turn(1)],
30103051
providerTurnId: turn(1).id,
30113052
targetThreadId: target,
@@ -3025,7 +3066,7 @@ describe("PiAdapterV2", () => {
30253066
.allRequests()
30263067
.filter((request) => request.type === "fork")
30273068
.map((request) => request.entryId),
3028-
historical ? ["u2"] : [],
3069+
historical && !split ? ["u2"] : [],
30293070
);
30303071
assert.isFalse(
30313072
fake.allRequests().some((request) => request.type === "fork" || request.type === "clone"),
@@ -3061,6 +3102,64 @@ describe("PiAdapterV2", () => {
30613102
}).pipe(Effect.scoped, Effect.provide(layerTest)),
30623103
);
30633104

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+
30643163
it.effect("natively forks before a turn whose start entry was never captured", () =>
30653164
Effect.gen(function* () {
30663165
const fake = yield* makeFakePi;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — 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.