Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,8 @@ export const OrchestrationV2ProviderTurn = Schema.Struct({
nodeId: NodeId,
runAttemptId: Schema.NullOr(RunAttemptId),
nativeTurnRef: Schema.NullOr(OrchestrationV2ProviderRef),
/** The native session that held this turn, when a provider can move a thread between sessions. */
nativeThreadRef: Schema.optional(OrchestrationV2ProviderRef),
ordinal: PositiveInt,
status: Schema.Literals([
"pending",
Expand Down
109 changes: 104 additions & 5 deletions packages/provider-pi/src/server/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ interface FakePi {
readonly queueEntries: (data: unknown) => void;
readonly queueMessages: (data: unknown) => void;
readonly vetoNextSwitch: () => void;
readonly failNextFork: () => void;
readonly queueState: (data: Record<string, unknown>) => void;
readonly setStreaming: (streaming: boolean) => void;
readonly setCompacting: (compacting: boolean) => void;
Expand Down Expand Up @@ -237,6 +238,7 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
let failStateAfter: number | null = null;
let failThinking = false;
let vetoSwitch = false;
let failFork = false;
let vetoNewSession = false;
let deferredLifecycle: string | undefined;
let sessionFile = FAKE_SESSION_FILE;
Expand Down Expand Up @@ -354,6 +356,10 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
case "get_session_stats":
return { ...base, data: statsQueue.shift() ?? {} };
case "fork":
if (failFork) {
failFork = false;
return { ...base, success: false, error: "Invalid entry ID for forking" };
}
serviceTier = null;
return { ...base, data: { text: "Hello pi", cancelled: false } };
case "clear_queue":
Expand Down Expand Up @@ -498,6 +504,9 @@ const makeFakePi: Effect.Effect<FakePi> = Effect.gen(function* () {
vetoNextSwitch: () => {
vetoSwitch = true;
},
failNextFork: () => {
failFork = true;
},
queueState: (data) => stateQueue.push(data),
setStreaming: (streaming) => {
isStreaming = streaming;
Expand Down Expand Up @@ -2806,6 +2815,16 @@ describe("PiAdapterV2", () => {
yield* fake.emit({ type: "agent_start" });
yield* fake.emit({ type: "agent_end", messages: [], willRetry: false });
yield* fake.emit({ type: "agent_settled" });
const completed = yield* takeEvent(
(event) =>
event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
);
assert.equal(
completed.type === "provider_turn.updated"
? completed.providerTurn.nativeThreadRef?.nativeId
: undefined,
childFile,
);
const idle = yield* takeEvent(
(event) =>
event.type === "provider_thread.updated" && event.providerThread.status === "idle",
Expand Down Expand Up @@ -2965,9 +2984,21 @@ describe("PiAdapterV2", () => {
);

it.effect.each([
{ historical: false, label: "the latest turn" },
{ historical: true, label: "a historical turn" },
])("natively forks $label into an independent session", ({ historical }) =>
{ historical: false, moved: false, split: false, label: "the latest turn" },
{ historical: true, moved: false, split: false, label: "a historical turn" },
{
historical: true,
moved: true,
split: false,
label: "a historical turn after a session switch",
},
{
historical: true,
moved: true,
split: true,
label: "the last turn before a switch to a fresh session",
},
])("natively forks $label into an independent session", ({ historical, moved, split }) =>
Effect.gen(function* () {
const fake = yield* makeFakePi;
const forkFake = yield* makeFakePi;
Expand All @@ -2984,12 +3015,22 @@ describe("PiAdapterV2", () => {
modelSelection: modelSelection("default"),
runtimePolicy,
});
const freshSession = {
driver: PI_PROVIDER,
nativeId: "/fake/new-session.jsonl",
strength: "strong",
} as const;
const turn = (ordinal: number): OrchestrationV2ProviderTurn => ({
id: ProviderTurnId.make(`turn-${ordinal}`),
providerThreadId: source.id,
nodeId: NodeId.make(`node-${ordinal}`),
runAttemptId: null,
nativeTurnRef: { driver: PI_PROVIDER, nativeId: `u${ordinal}`, strength: "strong" },
...(split && ordinal === 2
? { nativeThreadRef: freshSession }
: moved && source.nativeThreadRef !== null
? { nativeThreadRef: source.nativeThreadRef }
: {}),
ordinal,
status: "completed",
startedAt: null,
Expand All @@ -3005,7 +3046,7 @@ describe("PiAdapterV2", () => {
fake.queueState({ sessionFile: forkFile });
const target = ThreadId.make("fork-target");
const forked = yield* runtime.forkThread({
sourceProviderThread: source,
sourceProviderThread: moved ? { ...source, nativeThreadRef: freshSession } : source,
sourceProviderTurns: historical ? [turn(1), turn(2)] : [turn(1)],
providerTurnId: turn(1).id,
targetThreadId: target,
Expand All @@ -3025,7 +3066,7 @@ describe("PiAdapterV2", () => {
.allRequests()
.filter((request) => request.type === "fork")
.map((request) => request.entryId),
historical ? ["u2"] : [],
historical && !split ? ["u2"] : [],
);
assert.isFalse(
fake.allRequests().some((request) => request.type === "fork" || request.type === "clone"),
Expand Down Expand Up @@ -3061,6 +3102,64 @@ describe("PiAdapterV2", () => {
}).pipe(Effect.scoped, Effect.provide(layerTest)),
);

it.effect("recovers a historical Pi fork recorded before turns stored the session file", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const dir = yield* fs.makeTempDirectoryScoped();
const oldFile = `${dir}/original.jsonl`;
const unrelatedFile = `${dir}/unrelated.jsonl`;
const currentFile = `${dir}/current.jsonl`;
const header = { type: "session", version: 3 };
const writeSession = (file: string, entries: ReadonlyArray<unknown>) =>
fs.writeFileString(
file,
[header, ...entries].map((entry) => encodeJsonLine(entry)).join("\n") + "\n",
);
yield* writeSession(oldFile, UNCAPTURED_TURN_TREE.entries);
yield* writeSession(unrelatedFile, [
sessionMessage("u1", null, "user", 10),
sessionMessage("u2", null, "user", 110),
]);
yield* writeSession(currentFile, [sessionMessage("new", null, "user", 120)]);

const fake = yield* makeFakePi;
const forkFake = yield* makeFakePi;
const { runtime } = yield* openRuntime(fake, "default", THREAD_ID, SESSION_ID, forkFake);
const source = yield* runtime.ensureThread({
threadId: THREAD_ID,
modelSelection: modelSelection("default"),
runtimePolicy,
});
const forkFile = `${dir}/forked.jsonl`;
forkFake.failNextFork();
forkFake.queueState({ sessionFile: forkFile });
fake.queueState({ sessionFile: forkFile });
const [target, boundary] = uncapturedTurns(source.id, {
startedAt: 100,
completedAt: 145,
});
if (target === undefined || boundary === undefined) throw new Error("missing fork turns");
const forked = yield* runtime.forkThread({
sourceProviderThread: {
...source,
nativeThreadRef: { driver: PI_PROVIDER, nativeId: currentFile, strength: "strong" },
},
sourceProviderTurns: [
target,
{
...boundary,
nativeTurnRef: { driver: PI_PROVIDER, nativeId: "u2", strength: "strong" },
},
],
providerTurnId: target.id,
targetThreadId: ThreadId.make("fork-target"),
});
const args = forkFake.lastSpawn().args;
assert.equal(args[args.indexOf("--fork") + 1], oldFile);
assert.equal(forked.nativeThreadRef?.nativeId, forkFile);
}).pipe(Effect.scoped, Effect.provide(layerTest)),
);

it.effect("natively forks before a turn whose start entry was never captured", () =>
Effect.gen(function* () {
const fake = yield* makeFakePi;
Expand Down
Loading
Loading