The best way to control Claude Code, Codex and more from any computer or phone
fix(pi): nest tools called through another tool under their caller (#27521)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: GPT 6.1 Sol <codex@openai.com>
Wout Stiens committed
Oct 30, 2026 at 17:11 UTC
77336a54a75c593ceb6d9f1d97789d0ccd616460
2 files changed
+83
-3
packages/provider-pi/src/server/adapter.test.ts
+63
@@ -2041,6 +2041,69 @@ describe("PiAdapterV2", () => {
2041
}).pipe(Effect.scoped, Effect.provide(layerTest)),
2042
);
2043
2044
+ it.effect("nests tools called through another tool under their caller", () =>
2045
+ Effect.gen(function* () {
2046
+ const fake = yield* makeFakePi;
2047
+ const { runtime, takeEvent } = yield* openRuntime(fake);
2048
+ const providerThread = yield* runtime.ensureThread({
2049
+ threadId: THREAD_ID,
2050
+ modelSelection: modelSelection("default"),
2051
+ runtimePolicy,
2052
+ });
2053
+ yield* startTurn(runtime, providerThread);
2054
+ yield* fake.takeRequest("prompt");
2055
+ yield* fake.emit({ type: "agent_start" });
2056
+ const takeToolEvents = Effect.fnUntraced(function* (toolCallId: string) {
2057
+ const node = yield* takeEvent(
2058
+ (event) =>
2059
+ event.type === "node.updated" && event.node.nativeItemRef?.nativeId === toolCallId,
2060
+ );
2061
+ const item = yield* takeEvent(
2062
+ (event) =>
2063
+ event.type === "turn_item.updated" &&
2064
+ event.turnItem.nativeItemRef?.nativeId === toolCallId,
2065
+ );
2066
+ if (node.type !== "node.updated" || item.type !== "turn_item.updated")
2067
+ return yield* Effect.die("Expected tool node and item events");
2068
+ return { node: node.node, item: item.turnItem };
2069
+ });
2070
+
2071
+ yield* fake.emit({
2072
+ type: "tool_execution_start",
2073
+ toolCallId: "call_code",
2074
+ toolName: "codemode",
2075
+ args: { code: "await read({ path: 'a.ts' })" },
2076
+ });
2077
+ const caller = yield* takeToolEvents("call_code");
2078
+ assert.equal(caller.node.parentNodeId, caller.node.rootNodeId);
2079
+ assert.isNull(caller.item.parentItemId);
2080
+
2081
+ // Pi tags every phase, so an update that arrives before its start still nests.
2082
+ const phases = [
2083
+ { type: "tool_execution_update", partialResult: { content: [] }, status: "running" },
2084
+ { type: "tool_execution_start", args: { path: "a.ts" }, status: "running" },
2085
+ {
2086
+ type: "tool_execution_end",
2087
+ isError: true,
2088
+ result: { content: [{ type: "text", text: "ENOENT" }] },
2089
+ status: "failed",
2090
+ },
2091
+ ] as const;
2092
+ for (const { status, ...phase } of phases) {
2093
+ yield* fake.emit({
2094
+ ...phase,
2095
+ toolCallId: "call_code/1",
2096
+ toolName: "read",
2097
+ parentToolCallId: "call_code",
2098
+ });
2099
+ const nested = yield* takeToolEvents("call_code/1");
2100
+ assert.equal(nested.node.status, status);
2101
+ assert.equal(nested.node.parentNodeId, caller.node.id);
2102
+ assert.equal(nested.item.parentItemId, caller.item.id);
2103
+ }
2104
+ }).pipe(Effect.scoped, Effect.provide(layerTest)),
2105
+ );
2106
+
2107
it.effect(
2108
"keeps native and MCP images available to the shared asset reader with typed results",
2109
() =>
packages/provider-pi/src/server/adapter.ts
+20
-3
@@ -729,6 +729,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
729
nativeItemId: string,
730
startedAt: DateTime.Utc,
731
updatedAt: DateTime.Utc,
732
+ parentNativeItemId?: string,
733
) => ({
734
id: idAllocator.derive.turnItemFromProviderItem({
735
driver: PI_PROVIDER,
@@ -743,7 +744,13 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
744
providerThreadId: turn.turnInput.providerThread.id,
745
providerTurnId: turn.providerTurn.id,
746
nativeItemRef: providerRef(nativeItemId),
746
- parentItemId: null,
747
+ parentItemId:
748
+ parentNativeItemId === undefined
749
+ ? null
750
+ : idAllocator.derive.turnItemFromProviderItem({
751
+ driver: PI_PROVIDER,
752
+ nativeItemId: parentNativeItemId,
753
+ }),
754
ordinal: itemOrdinal(turn, nativeItemId),
755
startedAt,
756
updatedAt,
@@ -756,6 +763,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
763
status: OrchestrationV2ExecutionNode["status"],
764
startedAt: DateTime.Utc,
765
completedAt: DateTime.Utc | null,
766
+ parentNativeItemId?: string,
767
) =>
768
emit({
769
type: "node.updated",
@@ -767,7 +775,13 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
775
}),
776
threadId: turn.turnInput.threadId,
777
runId: turn.turnInput.runId,
770
- parentNodeId: turn.turnInput.rootNodeId,
778
+ parentNodeId:
779
+ parentNativeItemId === undefined
780
+ ? turn.turnInput.rootNodeId
781
+ : idAllocator.derive.nodeFromProviderItem({
782
+ driver: PI_PROVIDER,
783
+ nativeItemId: parentNativeItemId,
784
+ }),
785
rootNodeId: turn.turnInput.rootNodeId,
786
kind,
787
status,
@@ -980,6 +994,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
994
) {
995
const toolCallId = recordString(event, "toolCallId");
996
const toolName = recordString(event, "toolName") ?? "tool";
997
+ // Pi tags every phase of a call made through ctx.executeTool, such as from a codemode script.
998
+ const parentToolCallId = recordString(event, "parentToolCallId");
999
if (toolCallId === undefined) return;
1000
if (phase === "start") {
1001
turn.toolArgs.set(toolCallId, event["args"]);
@@ -999,7 +1015,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1015
: "failed"
1016
: "completed"
1017
: "running";
1002
- const base = baseItemFields(turn, toolCallId, startedAt, emittedAt);
1018
+ const base = baseItemFields(turn, toolCallId, startedAt, emittedAt, parentToolCallId);
1019
yield* emitItemNode(
1020
turn,
1021
toolCallId,
@@ -1007,6 +1023,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
1023
status,
1024
startedAt,
1025
completed ? emittedAt : null,
1026
+ parentToolCallId,
1027
);
1028
const shared = {
1029
...base,