From 6b4b524bc86747225e6a2549289c67d891b25fb0 Mon Sep 17 00:00:00 2001 From: Ben Reitz Date: Mon, 23 Nov 2026 10:17:13 +0000 Subject: [PATCH 1/4] [Agents] Explain how a parked Pi tool call waits and wakes --- src/assets/images/agents/pi-tool-park.svg | 18 +++++++++ .../agents/2026-11-20-pi-harness-pi-1-6.mdx | 6 ++- .../docs/agents/harnesses/pi/index.mdx | 37 +++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/assets/images/agents/pi-tool-park.svg diff --git a/src/assets/images/agents/pi-tool-park.svg b/src/assets/images/agents/pi-tool-park.svg new file mode 100644 index 00000000000..5c45437602b --- /dev/null +++ b/src/assets/images/agents/pi-tool-park.svg @@ -0,0 +1,18 @@ + +A parked tool call waits on disk, not in memory +A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result. + + + + + + + + + + +parkedModelToolexecute()Pistores every callYour appagent and UIcalls ask_userreadSignal(): no answer yetshows the question, with api.taskIdreturns { sleep: { signals } }stores the park: task id + signal keyharness.signal(taskId, …)runs execute() again, same argumentsreadSignal(): the answerreturns the answer as its resultholds no process, sothe object can hibernatethe tool callyour codegreen: the wake + \ No newline at end of file diff --git a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx index 6bb3038b6f9..f31b672ff2f 100644 --- a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx +++ b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx @@ -12,7 +12,7 @@ Agents SDK v0.34.0 upgrades [`PiHarness`](/agents/harnesses/pi/) to [Pi 1.6](htt ## Tools that wait -- **Park until a signal or a deadline.** A tool can return a park instead of a result, and Pi runs it again when a signal arrives or the deadline passes. While it waits, the call holds no invocation, so the Durable Object can hibernate. Deliver signals with the new `harness.signal()`. Refer to [Tools that wait](/agents/harnesses/pi/#tools-that-wait). +- **Park until a signal or a deadline.** A tool can return a "park" (Pi's `ToolPark` type) instead of a result. The park registers what the call waits for, and Pi runs it again when a signal arrives or the deadline passes. While it waits, the call holds no invocation, so the Durable Object can hibernate. Deliver signals with the new `harness.signal()`. Refer to [Tools that wait](/agents/harnesses/pi/#tools-that-wait). - **Wait for a subagent.** `submit()` and `continue()` take `notify`, so a parked tool wakes when another session's input settles. Refer to [Wait for a subagent](/agents/harnesses/pi/#wait-for-a-subagent). - **Approval gates.** A `beforeTool` hook can park a tool call before it runs, for example until a person approves it. @@ -24,7 +24,9 @@ Agents SDK v0.34.0 upgrades [`PiHarness`](/agents/harnesses/pi/) to [Pi 1.6](htt ## Wait for a person's answer -This tool asks the user a question, then parks until your agent delivers the answer: +This tool asks the user a question, then parks until your agent delivers the answer. Each time it runs, it checks for the answer first, so the question is shown once: + +![A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) diff --git a/src/content/docs/agents/harnesses/pi/index.mdx b/src/content/docs/agents/harnesses/pi/index.mdx index fc480134756..cd63ea2cbdf 100644 --- a/src/content/docs/agents/harnesses/pi/index.mdx +++ b/src/content/docs/agents/harnesses/pi/index.mdx @@ -427,7 +427,19 @@ For anything `PiHarness` does not cover, `await harness.pi()` returns the opened ## Tools that wait -A Pi tool can park instead of returning a result: it returns `{ sleep: { signals, until } }`, and Pi runs it again when one of the signals arrives or the deadline passes. While the call waits, it holds no invocation, so the Durable Object can hibernate or be evicted. Deliver a signal with `harness.signal()`, for example from the request that carries a person's answer: +A tool call usually ends when its `execute()` returns a result. A Pi tool can instead return a "park" (Pi's `ToolPark` type), which says what the call is waiting for: a signal, a deadline, or both. Returning a park registers the wait. Pi stores it with the call, and when the signal arrives or the deadline passes, Pi runs `execute()` again with the same arguments. + +Think of a park as an `await` that survives hibernation. A call cannot keep a promise open while the Durable Object sleeps, so it tells Pi what it is waiting for, and Pi keeps that in storage instead of in memory. While the call is parked, it holds no invocation, so the Durable Object can hibernate or be evicted. Your agent delivers a signal with `harness.signal()`, for example from the request that carries a person's answer. + +![A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) + +Because Pi runs the whole of `execute()` again after a wake, a tool that waits for a person's answer does three things each time it runs, in this order: + +1. Check for the answer with `api.readSignal()`. On the first run there is none. After the wake, the answer is there, and the tool returns it as its result. +2. If there is no answer yet, show the question. Send `api.taskId` with it: your agent needs the task id later to deliver the answer. +3. Return a park that names the signal key to wait for. + +Checking first means the question is shown once, not again on the run that has the answer: ```ts import { defineSignal } from "@earendil-works/pi-durable"; @@ -439,23 +451,42 @@ const Answer = defineSignal({ schema: obj({ text: str() }), }); -// In the tool's execute(args, api, context): park until the answer comes. +// In the tool's execute(args, api, context): +// 1. Check for the answer. After the wake, it is there. const answer = await api.readSignal(Answer, "answer", context); if (answer !== undefined && "value" in answer) { return { output: [{ type: "text", text: answer.value.text }] }; } +// 2. No answer yet: show the question, with the task id to answer it with. +// showQuestion() is your code, for example a message to the user's client. await showQuestion(api.taskId, args.question); +// 3. Park until a value arrives for the "answer" key. return { sleep: { signals: [Answer.key("answer")] } }; // Later, in your agent, with the task id the question was shown with: await this.harness.signal(taskId, Answer, "answer", { text: "Yes" }); ``` +The signal key (`"answer"` here) ties the three calls together: `readSignal()`, `Answer.key()` in the park, and `harness.signal()` must use the same one. A park can also set `until`, a time on Pi's clock such as `api.now()` plus two hours, to stop waiting at a deadline. After the deadline, `readSignal()` returns `{ expired: true }` instead of a value, so handle that case before showing the question again. + `harness.signal()` returns Pi's result: `delivered`, or `duplicate`, `invalid`, `closed`, or `not_found` when the call did not take the value. It schedules the harness's wake-up job before it hands Pi the value, so if the object dies while the woken call runs, the job's alarm restarts it. Deliver signals through `harness.signal()`, not through `harness.pi()`. A tool can also have a `cancel()` function, which Pi runs when a parked call is aborted, for example to cancel the work it was waiting on. A `beforeTool` hook can park a call the same way before the call runs, to hold it for an approval, a rate limit, or a budget. Pi runs the hook again each time it reopens, so keep the hook idempotent. -While a call is parked, its run is still going: `pending()` lists its operation as `running`, `busy()` is `true`, and Pi's `pi.live` shows the call's slot as `sleeping` or `waiting`. For the rules a parking tool follows, refer to [Tools that park](https://github.com/earendil-works/pi/tree/main/packages/durable#tools-that-park) and [Gates before a tool runs](https://github.com/earendil-works/pi/tree/main/packages/durable#gates-before-a-tool-runs) in the Pi Durable README. +While a call is parked, its run is still going: `pending()` lists its operation as `running`, `busy()` is `true`, and Pi's `pi.live` shows the call's slot as `sleeping` or `waiting`. The same wait appears under different names: + +| Name | Where you see it | What it means | +| --- | --- | --- | +| Park, `ToolPark` | What `execute()` returns | The call waits instead of ending. A `beforeTool` hook can return a `sleep` the same way. | +| `sleep` | In a park | Wait for signals, a deadline (`until`), or both. | +| `wait` | In a park | Wait for tasks the call owns, such as nested tool calls started with `wait: false`. | +| `sleeping`, `waiting` | A call's slot in `pi.live` | The call is parked on a `sleep` or a `wait`. | +| `running` | `PiPendingOperation.status` from `pending()` | The run has not ended. A parked call keeps its run going. | +| `invalid_park` | An error result the model sees | The park was not valid, for example it set both `sleep` and `wait`, or reused a signal key that already woke the call. | + +A park is not the `parked` status that `pending()` showed in Agents SDK 0.31.0 and 0.32.0. That status marked a task stopped by Pi's restart limit, and it was removed in 0.33.0. + +For the rules a parking tool follows, refer to [Tools that park](https://github.com/earendil-works/pi/tree/main/packages/durable#tools-that-park) and [Gates before a tool runs](https://github.com/earendil-works/pi/tree/main/packages/durable#gates-before-a-tool-runs) in the Pi Durable README. ### Wait for a subagent From 6228391296fbf0b961c95bb1ed2bf46e399f7848 Mon Sep 17 00:00:00 2001 From: Ben Reitz Date: Mon, 23 Nov 2026 15:43:44 +0000 Subject: [PATCH 2/4] [Agents] Redraw the Pi park diagram and add a two-runs figure --- src/assets/images/agents/pi-tool-park-runs.svg | 18 ++++++++++++++++++ src/assets/images/agents/pi-tool-park.svg | 10 +++++----- .../agents/2026-11-20-pi-harness-pi-1-6.mdx | 2 +- src/content/docs/agents/harnesses/pi/index.mdx | 6 ++++-- 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/assets/images/agents/pi-tool-park-runs.svg diff --git a/src/assets/images/agents/pi-tool-park-runs.svg b/src/assets/images/agents/pi-tool-park-runs.svg new file mode 100644 index 00000000000..5ca81aab2bb --- /dev/null +++ b/src/assets/images/agents/pi-tool-park-runs.svg @@ -0,0 +1,18 @@ + +The same execute() runs twice and takes a different path each time +The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again. + + + + + + + + + + +Run 1no answer yetRun 2after the wakeasync execute({ question }, api, context) {  const answer = await api.readSignal(Answer, "answer", context);  if (answer !== undefined && "value" in answer) {    return { output: [{ type: "text", text: answer.value.text }] };  }  await showQuestion(api.taskId, question);  return { sleep: { signals: [Answer.key("answer")] } };}run 2 returns the resultrun 1 returns the parkRun 2 starts again from the top, with the same arguments. Checking for the answer first is what stopsit from showing the question twice. + \ No newline at end of file diff --git a/src/assets/images/agents/pi-tool-park.svg b/src/assets/images/agents/pi-tool-park.svg index 5c45437602b..043af95d2aa 100644 --- a/src/assets/images/agents/pi-tool-park.svg +++ b/src/assets/images/agents/pi-tool-park.svg @@ -1,6 +1,6 @@ - -A parked tool call waits on disk, not in memory -A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result. + +A parked tool call leaves only a stored park behind +A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result. \ No newline at end of file diff --git a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx index f31b672ff2f..4b8afade9ec 100644 --- a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx +++ b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx @@ -26,7 +26,7 @@ Agents SDK v0.34.0 upgrades [`PiHarness`](/agents/harnesses/pi/) to [Pi 1.6](htt This tool asks the user a question, then parks until your agent delivers the answer. Each time it runs, it checks for the answer first, so the question is shown once: -![A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) +![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) diff --git a/src/content/docs/agents/harnesses/pi/index.mdx b/src/content/docs/agents/harnesses/pi/index.mdx index cd63ea2cbdf..c5ffb0c2995 100644 --- a/src/content/docs/agents/harnesses/pi/index.mdx +++ b/src/content/docs/agents/harnesses/pi/index.mdx @@ -431,7 +431,7 @@ A tool call usually ends when its `execute()` returns a result. A Pi tool can in Think of a park as an `await` that survives hibernation. A call cannot keep a promise open while the Durable Object sleeps, so it tells Pi what it is waiting for, and Pi keeps that in storage instead of in memory. While the call is parked, it holds no invocation, so the Durable Object can hibernate or be evicted. Your agent delivers a signal with `harness.signal()`, for example from the request that carries a person's answer. -![A sequence diagram of a tool that asks the user a question. The model calls the tool. The tool has no answer yet, so it shows the question with its task id and returns a park naming the signal it waits for. Pi stores the park with the call, and the Durable Object can hibernate. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored signal key and runs execute() again with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) +![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) Because Pi runs the whole of `execute()` again after a wake, a tool that waits for a person's answer does three things each time it runs, in this order: @@ -439,7 +439,9 @@ Because Pi runs the whole of `execute()` again after a wake, a tool that waits f 2. If there is no answer yet, show the question. Send `api.taskId` with it: your agent needs the task id later to deliver the answer. 3. Return a park that names the signal key to wait for. -Checking first means the question is shown once, not again on the run that has the answer: +![The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again.](~/assets/images/agents/pi-tool-park-runs.svg) + +The whole example, with the signal it waits on and the call your agent makes later: ```ts import { defineSignal } from "@earendil-works/pi-durable"; From ad4392357d346ad27fb51f03b262204ba8e73a71 Mon Sep 17 00:00:00 2001 From: Ben Reitz Date: Mon, 23 Nov 2026 15:58:22 +0000 Subject: [PATCH 3/4] [Agents] Call a returned park a "tool park" in the Pi docs --- .../images/agents/pi-tool-park-runs.svg | 4 ++-- src/assets/images/agents/pi-tool-park.svg | 6 ++--- .../agents/2026-11-20-pi-harness-pi-1-6.mdx | 4 ++-- .../docs/agents/harnesses/pi/index.mdx | 24 +++++++++---------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/assets/images/agents/pi-tool-park-runs.svg b/src/assets/images/agents/pi-tool-park-runs.svg index 5ca81aab2bb..612142f6be0 100644 --- a/src/assets/images/agents/pi-tool-park-runs.svg +++ b/src/assets/images/agents/pi-tool-park-runs.svg @@ -1,6 +1,6 @@ The same execute() runs twice and takes a different path each time -The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again. +The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the tool park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again. \ No newline at end of file diff --git a/src/assets/images/agents/pi-tool-park.svg b/src/assets/images/agents/pi-tool-park.svg index 043af95d2aa..7a5161dc509 100644 --- a/src/assets/images/agents/pi-tool-park.svg +++ b/src/assets/images/agents/pi-tool-park.svg @@ -1,6 +1,6 @@ -A parked tool call leaves only a stored park behind -A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result. +A parked tool call leaves only a stored tool park behind +A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a tool park naming the signal key it waits for. Run 1 ends there. Pi stores the tool park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored tool park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored tool park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result. \ No newline at end of file diff --git a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx index 4b8afade9ec..3664e2eb85c 100644 --- a/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx +++ b/src/content/changelog/agents/2026-11-20-pi-harness-pi-1-6.mdx @@ -12,7 +12,7 @@ Agents SDK v0.34.0 upgrades [`PiHarness`](/agents/harnesses/pi/) to [Pi 1.6](htt ## Tools that wait -- **Park until a signal or a deadline.** A tool can return a "park" (Pi's `ToolPark` type) instead of a result. The park registers what the call waits for, and Pi runs it again when a signal arrives or the deadline passes. While it waits, the call holds no invocation, so the Durable Object can hibernate. Deliver signals with the new `harness.signal()`. Refer to [Tools that wait](/agents/harnesses/pi/#tools-that-wait). +- **Park until a signal or a deadline.** A tool can return a "tool park" (Pi's `ToolPark` type) instead of a result. The tool park registers what the call waits for, and Pi runs it again when a signal arrives or the deadline passes. While it waits, the call holds no invocation, so the Durable Object can hibernate. Deliver signals with the new `harness.signal()`. Refer to [Tools that wait](/agents/harnesses/pi/#tools-that-wait). - **Wait for a subagent.** `submit()` and `continue()` take `notify`, so a parked tool wakes when another session's input settles. Refer to [Wait for a subagent](/agents/harnesses/pi/#wait-for-a-subagent). - **Approval gates.** A `beforeTool` hook can park a tool call before it runs, for example until a person approves it. @@ -26,7 +26,7 @@ Agents SDK v0.34.0 upgrades [`PiHarness`](/agents/harnesses/pi/) to [Pi 1.6](htt This tool asks the user a question, then parks until your agent delivers the answer. Each time it runs, it checks for the answer first, so the question is shown once: -![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) +![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a tool park naming the signal key it waits for. Run 1 ends there. Pi stores the tool park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored tool park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored tool park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) diff --git a/src/content/docs/agents/harnesses/pi/index.mdx b/src/content/docs/agents/harnesses/pi/index.mdx index c5ffb0c2995..6bfdfe39fc1 100644 --- a/src/content/docs/agents/harnesses/pi/index.mdx +++ b/src/content/docs/agents/harnesses/pi/index.mdx @@ -427,19 +427,19 @@ For anything `PiHarness` does not cover, `await harness.pi()` returns the opened ## Tools that wait -A tool call usually ends when its `execute()` returns a result. A Pi tool can instead return a "park" (Pi's `ToolPark` type), which says what the call is waiting for: a signal, a deadline, or both. Returning a park registers the wait. Pi stores it with the call, and when the signal arrives or the deadline passes, Pi runs `execute()` again with the same arguments. +A tool call usually ends when its `execute()` returns a result. A Pi tool can instead return a "tool park" (Pi's `ToolPark` type), which says what the call is waiting for: a signal, a deadline, or both. Returning a tool park registers the wait. Pi stores it with the call, and when the signal arrives or the deadline passes, Pi runs `execute()` again with the same arguments. -Think of a park as an `await` that survives hibernation. A call cannot keep a promise open while the Durable Object sleeps, so it tells Pi what it is waiting for, and Pi keeps that in storage instead of in memory. While the call is parked, it holds no invocation, so the Durable Object can hibernate or be evicted. Your agent delivers a signal with `harness.signal()`, for example from the request that carries a person's answer. +Think of a tool park as an `await` that survives hibernation. A call cannot keep a promise open while the Durable Object sleeps, so it tells Pi what it is waiting for, and Pi keeps that in storage instead of in memory. While the call is parked, it holds no invocation, so the Durable Object can hibernate or be evicted. Your agent delivers a signal with `harness.signal()`, for example from the request that carries a person's answer. -![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a park naming the signal key it waits for. Run 1 ends there. Pi stores the park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) +![A sequence diagram of a tool that asks the user a question. The model calls the tool, and run 1 of execute() starts. readSignal() finds no answer, so the tool shows the question with its task id and returns a tool park naming the signal key it waits for. Run 1 ends there. Pi stores the tool park: the call's task id and the signal key. Between the runs the object can hibernate, with no tool code in memory and only the stored tool park kept. Later your app calls harness.signal() with the task id and the answer. Pi matches the stored tool park and starts run 2 of execute() with the same arguments. This time readSignal() returns the answer, and the tool returns it to the model as its result.](~/assets/images/agents/pi-tool-park.svg) Because Pi runs the whole of `execute()` again after a wake, a tool that waits for a person's answer does three things each time it runs, in this order: 1. Check for the answer with `api.readSignal()`. On the first run there is none. After the wake, the answer is there, and the tool returns it as its result. 2. If there is no answer yet, show the question. Send `api.taskId` with it: your agent needs the task id later to deliver the answer. -3. Return a park that names the signal key to wait for. +3. Return a tool park that names the signal key to wait for. -![The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again.](~/assets/images/agents/pi-tool-park-runs.svg) +![The ask_user tool's execute() code, with two gutters marking which lines each run reaches. Run 1, before the answer: readSignal() returns nothing, the if is skipped, the tool shows the question and returns the tool park. Run 2, after the wake, starts again from the top with the same arguments: readSignal() returns the answer, and the tool returns it as its result, so the question is not shown again.](~/assets/images/agents/pi-tool-park-runs.svg) The whole example, with the signal it waits on and the call your agent makes later: @@ -469,7 +469,7 @@ return { sleep: { signals: [Answer.key("answer")] } }; await this.harness.signal(taskId, Answer, "answer", { text: "Yes" }); ``` -The signal key (`"answer"` here) ties the three calls together: `readSignal()`, `Answer.key()` in the park, and `harness.signal()` must use the same one. A park can also set `until`, a time on Pi's clock such as `api.now()` plus two hours, to stop waiting at a deadline. After the deadline, `readSignal()` returns `{ expired: true }` instead of a value, so handle that case before showing the question again. +The signal key (`"answer"` here) ties the three calls together: `readSignal()`, `Answer.key()` in the tool park, and `harness.signal()` must use the same one. A tool park can also set `until`, a time on Pi's clock such as `api.now()` plus two hours, to stop waiting at a deadline. After the deadline, `readSignal()` returns `{ expired: true }` instead of a value, so handle that case before showing the question again. `harness.signal()` returns Pi's result: `delivered`, or `duplicate`, `invalid`, `closed`, or `not_found` when the call did not take the value. It schedules the harness's wake-up job before it hands Pi the value, so if the object dies while the woken call runs, the job's alarm restarts it. Deliver signals through `harness.signal()`, not through `harness.pi()`. @@ -479,14 +479,14 @@ While a call is parked, its run is still going: `pending()` lists its operation | Name | Where you see it | What it means | | --- | --- | --- | -| Park, `ToolPark` | What `execute()` returns | The call waits instead of ending. A `beforeTool` hook can return a `sleep` the same way. | -| `sleep` | In a park | Wait for signals, a deadline (`until`), or both. | -| `wait` | In a park | Wait for tasks the call owns, such as nested tool calls started with `wait: false`. | +| Tool park, `ToolPark` | What `execute()` returns | The call waits instead of ending. A `beforeTool` hook can return a `sleep` the same way. | +| `sleep` | In a tool park | Wait for signals, a deadline (`until`), or both. | +| `wait` | In a tool park | Wait for tasks the call owns, such as nested tool calls started with `wait: false`. | | `sleeping`, `waiting` | A call's slot in `pi.live` | The call is parked on a `sleep` or a `wait`. | | `running` | `PiPendingOperation.status` from `pending()` | The run has not ended. A parked call keeps its run going. | -| `invalid_park` | An error result the model sees | The park was not valid, for example it set both `sleep` and `wait`, or reused a signal key that already woke the call. | +| `invalid_park` | An error result the model sees | The tool park was not valid, for example it set both `sleep` and `wait`, or reused a signal key that already woke the call. | -A park is not the `parked` status that `pending()` showed in Agents SDK 0.31.0 and 0.32.0. That status marked a task stopped by Pi's restart limit, and it was removed in 0.33.0. +A tool park is not the `parked` status that `pending()` showed in Agents SDK 0.31.0 and 0.32.0. That status marked a task stopped by Pi's restart limit, and it was removed in 0.33.0. For the rules a parking tool follows, refer to [Tools that park](https://github.com/earendil-works/pi/tree/main/packages/durable#tools-that-park) and [Gates before a tool runs](https://github.com/earendil-works/pi/tree/main/packages/durable#gates-before-a-tool-runs) in the Pi Durable README. @@ -541,7 +541,7 @@ await conversation?.submit( return { sleep: { signals: [SubmissionSettled.key("answered")] } }; ``` -When the child's input settles, Pi delivers the settled record to the call in the same commit, and the tool runs again with it. Because the call owns the child, aborting the parent session stops the child too, and a rerun before the park was stored finds the same child instead of creating another. The `requestId` makes a rerun find the same submission. If the task to notify has ended or does not exist, the input still settles as usual. `session.submit()` and `session.continue()` take the same `notify` option, for a child session that your own code created. +When the child's input settles, Pi delivers the settled record to the call in the same commit, and the tool runs again with it. Because the call owns the child, aborting the parent session stops the child too, and a rerun before the tool park was stored finds the same child instead of creating another. The `requestId` makes a rerun find the same submission. If the task to notify has ended or does not exist, the input still settles as usual. `session.submit()` and `session.continue()` take the same `notify` option, for a child session that your own code created. ## Recovery From 11db1cabf9aa3ba38906bdcc7a010748d2bccdac Mon Sep 17 00:00:00 2001 From: Ben Reitz Date: Mon, 23 Nov 2026 17:17:28 +0000 Subject: [PATCH 4/4] [Agents] Replace the Pi tool park table with shapes and short notes --- .../docs/agents/harnesses/pi/index.mdx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/content/docs/agents/harnesses/pi/index.mdx b/src/content/docs/agents/harnesses/pi/index.mdx index 6bfdfe39fc1..3f0626c5f1d 100644 --- a/src/content/docs/agents/harnesses/pi/index.mdx +++ b/src/content/docs/agents/harnesses/pi/index.mdx @@ -473,18 +473,13 @@ The signal key (`"answer"` here) ties the three calls together: `readSignal()`, `harness.signal()` returns Pi's result: `delivered`, or `duplicate`, `invalid`, `closed`, or `not_found` when the call did not take the value. It schedules the harness's wake-up job before it hands Pi the value, so if the object dies while the woken call runs, the job's alarm restarts it. Deliver signals through `harness.signal()`, not through `harness.pi()`. -A tool can also have a `cancel()` function, which Pi runs when a parked call is aborted, for example to cancel the work it was waiting on. A `beforeTool` hook can park a call the same way before the call runs, to hold it for an approval, a rate limit, or a budget. Pi runs the hook again each time it reopens, so keep the hook idempotent. - -While a call is parked, its run is still going: `pending()` lists its operation as `running`, `busy()` is `true`, and Pi's `pi.live` shows the call's slot as `sleeping` or `waiting`. The same wait appears under different names: - -| Name | Where you see it | What it means | -| --- | --- | --- | -| Tool park, `ToolPark` | What `execute()` returns | The call waits instead of ending. A `beforeTool` hook can return a `sleep` the same way. | -| `sleep` | In a tool park | Wait for signals, a deadline (`until`), or both. | -| `wait` | In a tool park | Wait for tasks the call owns, such as nested tool calls started with `wait: false`. | -| `sleeping`, `waiting` | A call's slot in `pi.live` | The call is parked on a `sleep` or a `wait`. | -| `running` | `PiPendingOperation.status` from `pending()` | The run has not ended. A parked call keeps its run going. | -| `invalid_park` | An error result the model sees | The tool park was not valid, for example it set both `sleep` and `wait`, or reused a signal key that already woke the call. | +A tool park takes one of two shapes: `{ sleep: { signals, until } }` to wait for a signal, a deadline, or both, or `{ wait: { on, policy } }` to wait for tasks the call owns. Either can also carry a `checkpoint`, which the next run of `execute()` reads as `api.checkpoint`. + +- **Cancelling:** a tool can have a `cancel()` function, which Pi runs when a parked call is aborted, for example to cancel the work it was waiting on. +- **Gates:** a `beforeTool` hook can return `{ sleep: { signals, until } }` to park a call before it runs, for example until a person approves it. Pi runs the hook again on every wake and whenever the harness reopens, so keep it idempotent. +- **Waiting on nested calls:** start them with `api.executeTool(name, args, context, { key, wait: false })`, park with `{ wait: { on: [taskId], policy: "any" } }`, and read `api.wake.ended` after the wake. For the other policies, refer to [Child Tasks](https://github.com/earendil-works/pi/tree/main/packages/durable#child-tasks). +- **What a parked call shows:** its slot in `pi.live` is `sleeping` or `waiting`. The operation that made the call stays `running` in `pending()`, and `busy()` is `true`. +- **Invalid parks:** a tool park that breaks a rule ends the call with an `invalid_park` error. Examples are setting both `sleep` and `wait`, a `sleep` with no `signals` and no `until`, a signal key that already woke the call or expired, and a `wait` on a task the call doesn't own. A tool park is not the `parked` status that `pending()` showed in Agents SDK 0.31.0 and 0.32.0. That status marked a task stopped by Pi's restart limit, and it was removed in 0.33.0.