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
63 changes: 63 additions & 0 deletions packages/provider-pi/src/server/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,69 @@ describe("PiAdapterV2", () => {
}).pipe(Effect.scoped, Effect.provide(layerTest)),
);

it.effect("nests tools called through another tool under their caller", () =>
Effect.gen(function* () {
const fake = yield* makeFakePi;
const { runtime, takeEvent } = yield* openRuntime(fake);
const providerThread = yield* runtime.ensureThread({
threadId: THREAD_ID,
modelSelection: modelSelection("default"),
runtimePolicy,
});
yield* startTurn(runtime, providerThread);
yield* fake.takeRequest("prompt");
yield* fake.emit({ type: "agent_start" });
const takeToolEvents = Effect.fnUntraced(function* (toolCallId: string) {
const node = yield* takeEvent(
(event) =>
event.type === "node.updated" && event.node.nativeItemRef?.nativeId === toolCallId,
);
const item = yield* takeEvent(
(event) =>
event.type === "turn_item.updated" &&
event.turnItem.nativeItemRef?.nativeId === toolCallId,
);
if (node.type !== "node.updated" || item.type !== "turn_item.updated")
return yield* Effect.die("Expected tool node and item events");
return { node: node.node, item: item.turnItem };
});

yield* fake.emit({
type: "tool_execution_start",
toolCallId: "call_code",
toolName: "codemode",
args: { code: "await read({ path: 'a.ts' })" },
});
const caller = yield* takeToolEvents("call_code");
assert.equal(caller.node.parentNodeId, caller.node.rootNodeId);
assert.isNull(caller.item.parentItemId);

// Pi tags every phase, so an update that arrives before its start still nests.
const phases = [
{ type: "tool_execution_update", partialResult: { content: [] }, status: "running" },
{ type: "tool_execution_start", args: { path: "a.ts" }, status: "running" },
{
type: "tool_execution_end",
isError: true,
result: { content: [{ type: "text", text: "ENOENT" }] },
status: "failed",
},
] as const;
for (const { status, ...phase } of phases) {
yield* fake.emit({
...phase,
toolCallId: "call_code/1",
toolName: "read",
parentToolCallId: "call_code",
});
const nested = yield* takeToolEvents("call_code/1");
assert.equal(nested.node.status, status);
assert.equal(nested.node.parentNodeId, caller.node.id);
assert.equal(nested.item.parentItemId, caller.item.id);
}
}).pipe(Effect.scoped, Effect.provide(layerTest)),
);

it.effect(
"keeps native and MCP images available to the shared asset reader with typed results",
() =>
Expand Down
23 changes: 20 additions & 3 deletions packages/provider-pi/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
nativeItemId: string,
startedAt: DateTime.Utc,
updatedAt: DateTime.Utc,
parentNativeItemId?: string,
) => ({
id: idAllocator.derive.turnItemFromProviderItem({
driver: PI_PROVIDER,
Expand All @@ -743,7 +744,13 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
providerThreadId: turn.turnInput.providerThread.id,
providerTurnId: turn.providerTurn.id,
nativeItemRef: providerRef(nativeItemId),
parentItemId: null,
parentItemId:
parentNativeItemId === undefined
? null
: idAllocator.derive.turnItemFromProviderItem({
driver: PI_PROVIDER,
nativeItemId: parentNativeItemId,
}),
ordinal: itemOrdinal(turn, nativeItemId),
startedAt,
updatedAt,
Expand All @@ -756,6 +763,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
status: OrchestrationV2ExecutionNode["status"],
startedAt: DateTime.Utc,
completedAt: DateTime.Utc | null,
parentNativeItemId?: string,
) =>
emit({
type: "node.updated",
Expand All @@ -767,7 +775,13 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
}),
threadId: turn.turnInput.threadId,
runId: turn.turnInput.runId,
parentNodeId: turn.turnInput.rootNodeId,
parentNodeId:
parentNativeItemId === undefined
? turn.turnInput.rootNodeId
: idAllocator.derive.nodeFromProviderItem({
driver: PI_PROVIDER,
nativeItemId: parentNativeItemId,
}),
rootNodeId: turn.turnInput.rootNodeId,
kind,
status,
Expand Down Expand Up @@ -980,6 +994,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
) {
const toolCallId = recordString(event, "toolCallId");
const toolName = recordString(event, "toolName") ?? "tool";
// Pi tags every phase of a call made through ctx.executeTool, such as from a codemode script.
const parentToolCallId = recordString(event, "parentToolCallId");
if (toolCallId === undefined) return;
if (phase === "start") {
turn.toolArgs.set(toolCallId, event["args"]);
Expand All @@ -999,14 +1015,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
: "failed"
: "completed"
: "running";
const base = baseItemFields(turn, toolCallId, startedAt, emittedAt);
const base = baseItemFields(turn, toolCallId, startedAt, emittedAt, parentToolCallId);
yield* emitItemNode(
turn,
toolCallId,
"tool_call",
status,
startedAt,
completed ? emittedAt : null,
parentToolCallId,
);
const shared = {
...base,
Expand Down
Loading