Skip to content

feat(agents)!: support Pi 1.6 - #2925

Merged
ben-reitz merged 14 commits into
mainfrom
feat/pi-1.6
Nov 20, 2026
Merged

ben-reitz merged 14 commits into
mainfrom
feat/pi-1.6

Conversation

@ben-reitz

@ben-reitz ben-reitz commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Moves the Pi harness, the pi-ai models and the Pi tool adapters from Pi 1.5 to Pi 1.6.0. Pi 1.6 writes stored data that Pi 1.5 can't run, so agents drops Pi 1.5 (~1.5.0 → ~1.6.0; pi-tui exact). Nothing in agents used the parts of Pi's API that 1.6 renamed or removed, so the only API changes here are additions.

 PiHarness
+  signal(taskId, signal, key, value)  // wake a parked tool, wake job scheduled first
 PiSession
   submit(input, {
+    notify: { taskId, key },  // wake a parked Pi task when this input settles
+    onAbort: "keep",          // a queued input survives an abort
   })
   continue({
+    notify: { taskId, key },
   })
 agents/models/pi-ai, Anthropic
+  a thread Anthropic can't continue is resent in full, as pi-ai does
 agents/models/pi-ai, chat-completions usage
+  usage.reasoning_tokens  // top-level, as pi-ai reads it
+  usage.cached_tokens     // top-level (Kimi), as pi-ai reads it
 Channels TUI
-  render(): string[]
+  render(): readonly string[]  // pi-tui 1.6's Component contract
  • Pi 1.6's suspendable work runs on PiHarness: signals, tools that park, beforeTool gates, and hook tasks. A tool parked on a signal alone sets no wake job or alarm, survives eviction, and finishes when the signal arrives.
  • Pi needs the host to arm its wake before a signal, as before a submit. harness.signal() does that; harness.pi().signal() doesn't, so without onWake a crash while the woken call ran left it stranded. abort() and reset(), which can start work too, now go through the same path.
  • The Anthropic wire hands pi-ai a fetch instead of a stand-in client. pi-ai builds its own SDK client, so a vendor error is the SDK's own APIError, which is what pi-ai's thread and strict-tools fallbacks check for. Before, a turn after a thread expired failed instead of starting a new thread (on main too).
  • pi.md's "Tools that wait" covers signals, cancel(), gates and parked calls. Its subagent sample is Pi's own pattern: the child is owned by the call, so aborting the parent stops it.
  • The Pi and browser example UIs show parked tool calls instead of dropping them.
  • New tests cover a signal without onWake surviving a crash, a park with a deadline, aborting a parked call, a beforeTool gate and a tool waiting on a subagent. Others pin Pi behavior we rely on without code of our own, some of it from Pi 1.5.x: Pi's multi-row SQLite commits round-trip text on a real Durable Object, the chat-completions wire never sends "tools": [], and OpenAI's openai_input_transformations diagnostic arrives next to ours.
  • Docs and the Pi example README name ~1.6.0, and pi.md warns against rolling back below Pi 1.6.
  • The pending "supports Pi 1.5.2" changeset is dropped, as it would ship beside "Pi 1.5 is no longer supported".

Pi 1.6 also fixes, with no code here: O(n²) owner-chain walks, unhandled rejections from Submission.wait() on close, and more retried provider socket errors. Harness.close() (via PiHarness.dispose()) now frees pi-ai's per-session state.

createAI from agents/models/pi-ai is 17.1 KiB gzip larger, almost all from pi-ai's openai SDK going from 7.19 to 7.59, which the chat-completions wire pulls in through convertMessages.

Merge Danger

Once a conversation runs on Pi 1.6, it writes pi.tool v4 and pi.generation v3, which Pi 1.5 (agents 0.33) can't run. Rolling a Worker back below this release blocks those conversations' tool calls and model requests (task_too_old). Rolling forward again is fine.

They must move to Pi 1.6. Code that uses pi-durable directly follows its breaking changes: RunEndTask is HookTask, a tool's execute() may return a park, until is optional on sleeping tasks, and code-mode namespaces come from Extension.namespace. pi-durable is documented as experimental, with API changes in minor releases.


Devin Review

Pi packages to ~1.6.0 (pi-tui 1.6.0 exact) in agents, the Pi examples and experimental/pi-recovery, and the pi-agent-core>pi-ai override to ~1.6.0. Pi 1.6 stores pi.tool v4 and pi.generation v3, which Pi 1.5 does not run, so the harness docs now warn against rolling back to an agents version on Pi 1.5. The changeset names the version and links its changelog.
pi-ai continues an Anthropic message thread for a session on api.anthropic.com, sending only the new messages. A live check showed AI Gateway forwards the thread field and the message-threads beta header on both the provider-native and /run routes, so the wire keeps pi-ai's default. Pin that a second turn continues the thread with only the new message and the beta header.
…ols array

pi-ai openai-completions stopped sending "tools": [] when a request has no tools but its history has tool calls (Pi 7a6bd5a), with compat.requiresToolsWithToolHistory to restore it. Our wire builds tools itself and fills the flag as false; pin that a turn with no tools after tool calls omits tools.
pi-durable added signals, tools that park, gates and hook tasks (earendil-works/pi@473515f).

- SubmissionDraft gained notify ({ taskId, key }), which delivers SubmissionSettled to a parked task when the submission settles. The contract test flagged it; PiSubmitOptions now passes it through, with docs.
- A tool parked on a signal alone makes pi.wake() report { active: false } with no time. A new test proves the harness then sets no wake job or alarm, survives eviction, and finishes the call when the signal is delivered. The lifecycle docs say so.
…wire

pi-ai's parseChunkUsage now falls back to usage.reasoning_tokens when completion_tokens_details.reasoning_tokens is missing or 0 (earendil-works/pi@86918f9). usageFromOpenAI mirrors it field for field, so it does too.
pi-durable now takes `onAbort: "keep"` on an input submission: the input
stays queued through a conversation abort, and the next input places it.
Expose it as `PiSubmitOptions.onAbort` (type `PiOnAbort`), keep the
contract type test in step, and document how `abort()` treats such inputs.

Upstream: earendil-works/pi@e67aa1a
pi-tui b7bb7ca made Component.render() return readonly string[] and Container cache its output (breaking). The Channels TUI's TextBlock returned Markdown's cached lines as string[], which no longer typechecks; it now declares readonly string[]. No Channels component modifies render output in place, and TranscriptView builds a new array, so the caching needs no other change.
…esponses wire

pi-ai now records the replayed reasoning items OpenAI withheld from the model (input_transformations, such as prefix_mismatch after compaction on GPT-6.2) as an openai_input_transformations diagnostic inside processResponsesStream. The Responses wire uses that processor and appends its own cloudflare diagnostic beside it, so the new diagnostic reaches users with no code change; this test keeps it that way. It fails with the recording removed from pi-ai.
pi-ai's parseChunkUsage falls back to usage.cached_tokens, which Kimi reports on its final usage chunk (earendil-works/pi@d3ab2af). usageFromOpenAI now does the same, so those reads count as cacheRead and come out of input. The new test fails without the fallback.
@changeset-bot

changeset-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3e899ee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Minor
@cloudflare/agent-think Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@2925

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@2925

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@2925

hono-agents

npm i https://pkg.pr.new/hono-agents@2925

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@2925

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@2925

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@2925

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@2925

commit: 3e899ee

@agent-think

agent-think Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

🔴 agents import sizes: 1 entry point over threshold

Entry point Exports Largest gzip change Size now
🔴 agents/models/pi-ai 1 resized +17.2 KiB (+10.42%) 182.1 KiB
Changed exports (1)
Import Gzip change Size now
🔴 agents/models/pi-ai#createAI +17.2 KiB (+10.42%) 182.1 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared e981dac5 → 3e899ee0 · workflow run · reported by agent-think[bot]

@ben-reitz
ben-reitz added this pull request to stack #2927 November 20, 2026 14:15
pi-ai resends a rejected thread continue in full, and retries strict
tools over Anthropic's limits without strict, only when the error is the
Anthropic SDK's own APIError. The wire handed pi-ai a stand-in client
that threw CloudflareAIError, so neither fallback ever ran: a turn after
a thread expired (a few minutes idle, while pi-ai's thread state outlives
the turn) failed instead of starting a new thread.

Hand pi-ai a fetch instead of a client. pi-ai builds its own SDK client,
the fetch routes the SDK's request through the gateway, and the SDK turns
a vendor error response into its own error. The typed Cloudflare error is
still what a failed stream reports.

The thread test's comment no longer claims it shows AI Gateway forwards
`thread`: it uses a fake binding. That was checked live.
Pi requires a host to arm its wake before anything that can start work:
submit, signal, abort, or a registry change. The only way to signal a
parked tool was harness.pi().signal(), which skips that. With onWake, Pi
arms the job right after the commit, but without it the woken call had
no job and no alarm, so a crash while it ran left it stranded until
something else touched the object.

- Add harness.signal(), which schedules the wake first.
- Abort, withdraw and reset go through the same admission: stopping a
  parked call runs its cancel(), and reset runs kept inputs.
- continue() takes notify, as Pi accepts it on every draft; the contract
  test now checks continue's options too.
- Tests for a signal without onWake surviving a crash, a park with a
  deadline waking from the alarm, aborting a parked call after eviction, a
  beforeTool gate, and a tool waiting on a subagent, including a crash and
  an abort while the subagent runs.
- pi.md: "Tools that wait" replaces the notify section. Its subagent
  sample is Pi's own pattern: the child is owned by the call, so aborting
  the parent stops it and a rerun finds it again, and the result is the
  child's answer. It also covers cancel(), gates rerunning on each open,
  and pending()/busy() while a call is parked.
The Pi and browser example views kept only tool slots that were running,
so a call parked on a signal or a time disappeared and the UI showed
"Waiting for the model". Keep sleeping and waiting slots, marked parked,
and say the tool is waiting.
…ccurately

Cached tokens are counted as cache reads, not reasoning tokens. Mention
harness.signal(), continue()'s notify and the Anthropic thread fallback.
Drop the pending "supports Pi 1.5.2" changeset, which would ship in the
same release as "Pi 1.5 is no longer supported".
@ben-reitz
ben-reitz merged commit cf2ad50 into main Nov 20, 2026
26 checks passed
@ben-reitz
ben-reitz deleted the feat/pi-1.6 branch November 20, 2026 15:25
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant