pi-claude-tether 0.0.0-stage → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +21 -0
  3. package/README.md +51 -2
  4. package/SOURCE.md +17 -0
  5. package/assets/flow-dark.svg +1 -0
  6. package/assets/flow-light.svg +1 -0
  7. package/assets/flow.mmd +16 -0
  8. package/docs/ACCOUNT-FAILOVER.md +88 -0
  9. package/docs/ARCHITECTURE.md +758 -0
  10. package/docs/CONTEXT-POLICY.md +58 -0
  11. package/docs/DETAILS.md +140 -0
  12. package/docs/REQUEST-LEDGER.md +30 -0
  13. package/index.ts +355 -0
  14. package/package.json +84 -4
  15. package/scripts/continuity-proof.py +1429 -0
  16. package/scripts/package-dirs.sh +25 -0
  17. package/scripts/resolve-pi-cli.mjs +235 -0
  18. package/scripts/update-cli.sh +256 -0
  19. package/src/accounts.ts +212 -0
  20. package/src/autocompact.ts +95 -0
  21. package/src/cli-process.ts +849 -0
  22. package/src/cli-version.ts +138 -0
  23. package/src/context-policy.ts +152 -0
  24. package/src/control-handler.ts +80 -0
  25. package/src/data-dir.ts +114 -0
  26. package/src/env-flag.ts +27 -0
  27. package/src/event-bridge.ts +873 -0
  28. package/src/handoff-broker.ts +324 -0
  29. package/src/mcp-config.ts +244 -0
  30. package/src/mcp-schema-server.cjs +188 -0
  31. package/src/overflow.ts +63 -0
  32. package/src/pi-context.ts +44 -0
  33. package/src/process-manager.ts +700 -0
  34. package/src/prompt-builder.ts +584 -0
  35. package/src/provider.ts +1962 -0
  36. package/src/rate-limit.ts +163 -0
  37. package/src/receipt-image.ts +90 -0
  38. package/src/request-ledger.ts +257 -0
  39. package/src/runtime-dir.ts +3 -0
  40. package/src/session-map.ts +182 -0
  41. package/src/stream-parser.ts +37 -0
  42. package/src/system-prompt-mode.ts +53 -0
  43. package/src/task-tracker.ts +240 -0
  44. package/src/thinking-config.ts +260 -0
  45. package/src/tool-mapping.ts +156 -0
  46. package/src/tool-markers.ts +508 -0
  47. package/src/types.ts +315 -0
  48. package/tests/e2e/continuity-extension.ts +103 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## 0.11.0 — 2026-10-28
4
+
5
+ Prepared first public release as `pi-claude-tether` (not yet published).
6
+
7
+ - Explicit public export and npm allowlists; policy-backed public-tree scan.
8
+ - Public data, state and ledger roots remain unchanged. Unknown package names now use a generic legacy data directory; no automatic credential migration.
9
+
10
+ - Accept qualified derivative-image receipts with exact companion text and compatible supported headers/dimensions, and exact newline-joined multi-text error receipts (including nonempty outer-trimmed joins). Image equivalence is not pixel identity; missing images, denials, changed companions and error status remain strict.
11
+ - Standalone development setup and Pi 1.5.0 tests; test fixtures derive ledger paths from the package name.
12
+
13
+ - Runs Pi on the official Claude Code CLI with your own subscription, keeping one live CLI process per session and account. Pi owns the prompt, tools, conversation and compaction; the CLI handles authentication and model access.
14
+ - Warm CLI reuse only while the acknowledged history prefix and launch configuration match. Cancelled tools, steering during running tools, and settled transient failures keep prompt-cache continuity; anything else retires safely and reimports.
15
+ - Multiple Claude accounts with manual selection: `PI_CLAUDE_CLI_ACCOUNTS` declares the ordered set, `PI_CLAUDE_CLI_ACCOUNT` picks this process's profile. Each profile is a separate clean Claude login under the package data directory; the prompt cache is per account.
16
+ - Claude Code CLI version policy: declared tested range (floor plus newest qualified CLI); newer warns once and continues unless `PI_CLAUDE_CLI_STRICT_VERSION=1`; older errors. The bundled `scripts/update-cli.sh` stages a version, self-tests, then swaps the package CLI directory, keeping the previous one for rollback.
17
+ - Usage-credit guard: turns billed to overage fail non-retryably unless `PI_CLAUDE_CLI_ALLOW_USAGE_CREDITS=1`; the error states which effects stand.
18
+ - Rate-limit visibility: CLI rate-limit state forwarded as `anthropic-ratelimit-unified-*` headers; prompt-cache lifetimes declared for Pi's own session economics. Pi-driven refreshes are declined for this provider.
19
+ - Tested with pi 1.4.0 and Claude Code 2.1.311.
20
+
21
+ Honest limits: history imports are not byte-identical to a direct API provider; warm reuse does not survive every change, crash or provider switch; a parked CLI consumes memory; SIGKILL can leave a private temporary directory behind. Keeping a process warm does not guarantee a cache hit, reduce token usage, or change your subscription allowance. Check Anthropic's current terms yourself.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rebecca Chernoff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,52 @@
1
- # Temporary Holding Version
1
+ # pi-claude-tether
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+
4
+ Use your Claude subscription in [Pi](https://github.com/earendil-works/pi), through the official Claude Code CLI.
5
+
6
+ Hi, I do most of my work in Pi and I have a Claude subscription. While Anthropic has not been magnanimous enough to let us use our subscription directly in third-party harnesses, it still allows its usage through [`claude -p`](https://support.claude.com/en/articles/15036540-use-the-claude-agent-sdk-with-your-claude-plan), in its installed coding agent CLI. Pi is my main harness and I don't want to be swapping coding agents, and most importantly I don't want to juggle identities and system prompts. The goal of this tether is to unlock the high limits a subscription allows while using your own Pi custom prompt as much as possible, staying lighter on the system prompt than the less-than-optimal vendor harnesses, and not having to go through the API. I wrote it with a coding agent.
7
+
8
+ <picture>
9
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/kyraneth/pi-claude-tether/main/assets/flow-dark.svg">
10
+ <img alt="Pi sends the prompt, tools and history through the tether and the official Claude Code CLI to the model. A tool call comes back the same way to Pi, which runs the tool and saves the result. The result then goes back to the model." width="700" src="https://raw.githubusercontent.com/kyraneth/pi-claude-tether/main/assets/flow-light.svg">
11
+ </picture>
12
+
13
+ ## Who it's for
14
+
15
+ This is for you if you've built your workflow around Pi and you have a Claude plan. Claude models appear in `/model`, and you can move between them and other providers within one session. Pi's skills, hooks and extensions keep working.
16
+
17
+ ## Install
18
+
19
+ You need Node 22 or newer and Claude Code 2.1.263 or newer. Opus 5.5 needs 2.1.280. I tested this with Pi 1.5.0.
20
+
21
+ ```sh
22
+ pi install npm:pi-claude-tether
23
+ CLAUDE_CONFIG_DIR="$HOME/.local/share/pi-claude-tether/claude-config" claude auth login --claudeai
24
+ ```
25
+
26
+ The tether reads that folder by default, so this login stays separate from any Claude Code you use yourself. If it worked, Claude models appear under `pi-claude-cli` in `/model`. The name comes from the project this started from.
27
+
28
+ ## How it works
29
+
30
+ The tether runs the official Claude Code CLI and keeps one process alive per session and account, so follow-up turns and tool calls stay on the same process. Pi's system prompt replaces the Claude Code one, every tool runs in Pi, and Pi owns the history. Your login stays in the CLI, and the tether never reads it.
31
+
32
+ In short live tests on Sonnet 5.5, 98.3% of a warm turn's input came from the cache on one machine and 99.1% on another. I haven't published a comparison with the direct API yet.
33
+
34
+ ## Why not something else
35
+
36
+ | Option | Why I didn't use it |
37
+ |---|---|
38
+ | Pi with the direct API | It works well, but you pay per token instead of using your plan. |
39
+ | Packages that reuse your Claude login and imitate Claude Code | I don't want my account depending on imitating a client. Those packages also have to keep up each time the client changes. |
40
+ | [pi-claude-bridge](https://github.com/elidickinson/pi-claude-bridge), on the Agent SDK | It's a good official route and Pi runs the tools there too. I went with the CLI because this project started as a fork of pi-claude-cli. |
41
+ | [pi-claude-cli](https://github.com/rchern/pi-claude-cli) | This is where the project started. I forked it to add cache continuity across cancelled tools, retries and steering, several accounts, and a safe way to update the CLI. |
42
+
43
+ ## When not to use it
44
+
45
+ - You restart Pi often in long sessions. When the process restarts, the tether imports the history as text and the cache can go cold.
46
+ - You're short on memory. Each parked session keeps a Claude process in memory.
47
+ - You expect to run into usage credits. A turn billed to credits fails, unless you set `PI_CLAUDE_CLI_ALLOW_USAGE_CREDITS=1`.
48
+ - You want several accounts without reading the rules. The tether supports them, but check Anthropic's terms first.
49
+
50
+ ## More
51
+
52
+ This started as a fork of [pi-claude-cli](https://github.com/rchern/pi-claude-cli) through [Agustín Sacco's fork](https://github.com/agustinsacco/pi-claude-cli), and I'm grateful to Rebecca Chernoff and Agustín for their work. It's an independent project, and Anthropic didn't make it. The technical details are in [docs/DETAILS.md](https://github.com/kyraneth/pi-claude-tether/blob/main/docs/DETAILS.md). Please report problems in the [issues](https://github.com/kyraneth/pi-claude-tether/issues). [MIT license](https://github.com/kyraneth/pi-claude-tether/blob/main/LICENSE).
package/SOURCE.md ADDED
@@ -0,0 +1,17 @@
1
+ # pi-claude-tether — source provenance
2
+
3
+ This package is based on MIT-licensed [`@saccolabs/pi-claude-cli` 0.10.0](https://github.com/agustinsacco/pi-claude-cli) (upstream tag `v0.10.0`, commit `964ae7180d88bda1ee9266a2ff45074765f06195`), itself a maintained fork of [rchern/pi-claude-cli](https://github.com/rchern/pi-claude-cli). The original copyright and license are retained verbatim in `LICENSE`.
4
+
5
+ What changed relative to upstream 0.10.0:
6
+
7
+ - Package identity: renamed to `pi-claude-tether`; on-disk state moved from the old shared location to the package's own data directory (`${XDG_DATA_HOME:-~/.local/share}/pi-claude-tether/`), derived at runtime from the package's own `package.json` name (`src/data-dir.ts`).
8
+ - Pi-owned history/prompt/tools/compaction and warm CLI reuse are upstream architecture, retained.
9
+ - Continuity hardening: prompt-cache continuity across cancelled Pi tools, native steering during running tools, and settled transient retries; safe retirement/reimport otherwise.
10
+ - Receipt confirmation and recovery without repeating completed tool effects; malformed MCP input returns through Pi validation.
11
+ - Pi request/response hooks, a dedicated clean Claude profile per account, restrictive-umask permissions and long Unix socket-path handling.
12
+ - Multiple Claude accounts with manual selection (`PI_CLAUDE_CLI_ACCOUNT` / `PI_CLAUDE_CLI_ACCOUNTS`); the prompt cache is per account.
13
+ - Claude Code CLI version policy: a declared tested range, installed-vs-latest status, and a safe tested update path (`scripts/update-cli.sh`). The CLI is never updated automatically.
14
+ - Usage-credit guard: turns the CLI bills to overage stop with a non-retryable error unless explicitly opted in.
15
+ - Rate-limit visibility: CLI rate-limit state forwarded to Pi's `after_provider_response` as `anthropic-ratelimit-unified-*` headers, plus prompt-cache lifetime declarations for Pi's own session economics.
16
+
17
+ Warm-process retention is passive: keeping a compatible CLI process avoids reimporting history. It is not periodic cache refreshing, not a cache-hit guarantee, and not a billing change. Server-side caching and subscription policies remain outside the extension's control.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" class="pele pele-sequence" viewBox="0 0 687 469" width="687" height="469" style="max-width:100%;height:auto;color:#f0f6fc;font:16px sans-serif;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif" fill="#f0f6fc" role="graphics-document document" aria-roledescription="sequence" aria-labelledby="pele-title" aria-describedby="pele-desc"><title id="pele-title">The tool call path through the Claude tether</title><desc id="pele-desc">Pi sends the prompt to the model through the tether and the official CLI. The model's tool call comes back the same way to Pi, which runs the tool and saves the result, and only then does the result go back to the model.</desc><g transform="translate(109.43,8)"><g class="pele-lifelines" fill="none" stroke="#3d444d"><path class="pele-lifeline" data-id="P" d="M0,44V409"/><path class="pele-lifeline" data-id="T" d="M111.43,44V409"/><path class="pele-lifeline" data-id="C" d="M294.59,44V409"/><path class="pele-lifeline" data-id="S" d="M503.97,44V409"/></g><g class="pele-edges" fill="none" stroke="#9198a1" stroke-linecap="round"><g class="pele-edge pele-message" data-id="i0"><path d="M0,87H496.97"/><path class="pele-marker" d="M503.97,87L495.97,90.36L495.97,83.64Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i1"><path d="M503.97,124H301.59" stroke-dasharray="4 4"/><path class="pele-marker" d="M294.59,124L302.59,120.64L302.59,127.36Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i2"><path d="M294.59,161H118.43" stroke-dasharray="4 4"/><path class="pele-marker" d="M111.43,161L119.43,157.64L119.43,164.36Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i3"><path d="M111.43,198H7" stroke-dasharray="4 4"/><path class="pele-marker" d="M0,198L8,194.64L8,201.36Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i5"><path d="M0,278H104.43"/><path class="pele-marker" d="M111.43,278L103.43,281.36L103.43,274.64Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i6"><path d="M111.43,315H287.59"/><path class="pele-marker" d="M294.59,315L286.59,318.36L286.59,311.64Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i7"><path d="M294.59,352H496.97"/><path class="pele-marker" d="M503.97,352L495.97,355.36L495.97,348.64Z" fill="#9198a1" stroke="none"/></g><g class="pele-edge pele-message" data-id="i8"><path d="M503.97,389H7" stroke-dasharray="4 4"/><path class="pele-marker" d="M0,389L8,385.64L8,392.36Z" fill="#9198a1" stroke="none"/></g></g><g class="pele-edge-labels" font-size="14"><g class="pele-edge-label" data-id="i0"><rect x="86.73" y="64" width="330.51" height="17" rx="3" fill="#0d1117" stroke="none"/><text x="251.99" y="77.4" text-anchor="middle">prompt, tools and history, through the tether and CLI</text></g><g class="pele-edge-label" data-id="i1"><text x="399.28" y="114.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i2"><text x="203.01" y="151.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i3"><text x="55.72" y="188.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i5"><text x="55.72" y="268.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i6"><text x="203.01" y="305.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i7"><text x="399.28" y="342.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i8"><text x="251.99" y="379.4" text-anchor="middle">answer</text></g></g><g class="pele-notes" font-size="14" fill="rgb(33,40,48)" stroke="#3d444d"><g class="pele-note" data-id="i4"><rect x="-101.43" y="208" width="202.87" height="33" style="rx:4px"/><text class="pele-label" stroke="none" fill="#f0f6fc" x="0" y="229.4" text-anchor="middle">runs the tool, saves the result</text></g></g><g class="pele-nodes"><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="P"><rect x="-32" y="409" width="64" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="0" y="436.6" text-anchor="middle">Pi</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="T"><rect x="72.31" y="409" width="78.24" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="111.43" y="436.6" text-anchor="middle">Tether</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="C"><rect x="190.55" y="409" width="208.06" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="294.59" y="436.6" text-anchor="middle">Official Claude Code CLI</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="S"><rect x="438.62" y="409" width="130.7" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="503.97" y="436.6" text-anchor="middle">Claude model</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="P"><rect x="-32" y="0" width="64" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="0" y="27.6" text-anchor="middle">Pi</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="T"><rect x="72.31" y="0" width="78.24" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="111.43" y="27.6" text-anchor="middle">Tether</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="C"><rect x="190.55" y="0" width="208.06" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="294.59" y="27.6" text-anchor="middle">Official Claude Code CLI</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="S"><rect x="438.62" y="0" width="130.7" height="44" fill="#151b23" stroke="#3d444d" style="rx:4px"/><text class="pele-label" x="503.97" y="27.6" text-anchor="middle">Claude model</text></g></g></g></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" class="pele pele-sequence" viewBox="0 0 687 469" width="687" height="469" style="max-width:100%;height:auto;color:#1f2328;font:16px sans-serif;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif" fill="#1f2328" role="graphics-document document" aria-roledescription="sequence" aria-labelledby="pele-title" aria-describedby="pele-desc"><title id="pele-title">The tool call path through the Claude tether</title><desc id="pele-desc">Pi sends the prompt to the model through the tether and the official CLI. The model's tool call comes back the same way to Pi, which runs the tool and saves the result, and only then does the result go back to the model.</desc><g transform="translate(109.43,8)"><g class="pele-lifelines" fill="none" stroke="#d1d9e0"><path class="pele-lifeline" data-id="P" d="M0,44V409"/><path class="pele-lifeline" data-id="T" d="M111.43,44V409"/><path class="pele-lifeline" data-id="C" d="M294.59,44V409"/><path class="pele-lifeline" data-id="S" d="M503.97,44V409"/></g><g class="pele-edges" fill="none" stroke="#59636e" stroke-linecap="round"><g class="pele-edge pele-message" data-id="i0"><path d="M0,87H496.97"/><path class="pele-marker" d="M503.97,87L495.97,90.36L495.97,83.64Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i1"><path d="M503.97,124H301.59" stroke-dasharray="4 4"/><path class="pele-marker" d="M294.59,124L302.59,120.64L302.59,127.36Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i2"><path d="M294.59,161H118.43" stroke-dasharray="4 4"/><path class="pele-marker" d="M111.43,161L119.43,157.64L119.43,164.36Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i3"><path d="M111.43,198H7" stroke-dasharray="4 4"/><path class="pele-marker" d="M0,198L8,194.64L8,201.36Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i5"><path d="M0,278H104.43"/><path class="pele-marker" d="M111.43,278L103.43,281.36L103.43,274.64Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i6"><path d="M111.43,315H287.59"/><path class="pele-marker" d="M294.59,315L286.59,318.36L286.59,311.64Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i7"><path d="M294.59,352H496.97"/><path class="pele-marker" d="M503.97,352L495.97,355.36L495.97,348.64Z" fill="#59636e" stroke="none"/></g><g class="pele-edge pele-message" data-id="i8"><path d="M503.97,389H7" stroke-dasharray="4 4"/><path class="pele-marker" d="M0,389L8,385.64L8,392.36Z" fill="#59636e" stroke="none"/></g></g><g class="pele-edge-labels" font-size="14"><g class="pele-edge-label" data-id="i0"><rect x="86.73" y="64" width="330.51" height="17" rx="3" fill="#ffffff" stroke="none"/><text x="251.99" y="77.4" text-anchor="middle">prompt, tools and history, through the tether and CLI</text></g><g class="pele-edge-label" data-id="i1"><text x="399.28" y="114.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i2"><text x="203.01" y="151.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i3"><text x="55.72" y="188.4" text-anchor="middle">tool call</text></g><g class="pele-edge-label" data-id="i5"><text x="55.72" y="268.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i6"><text x="203.01" y="305.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i7"><text x="399.28" y="342.4" text-anchor="middle">result</text></g><g class="pele-edge-label" data-id="i8"><text x="251.99" y="379.4" text-anchor="middle">answer</text></g></g><g class="pele-notes" font-size="14" fill="#e6eaef" stroke="#d1d9e0"><g class="pele-note" data-id="i4"><rect x="-101.43" y="208" width="202.87" height="33" style="rx:4px"/><text class="pele-label" stroke="none" fill="#1f2328" x="0" y="229.4" text-anchor="middle">runs the tool, saves the result</text></g></g><g class="pele-nodes"><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="P"><rect x="-32" y="409" width="64" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="0" y="436.6" text-anchor="middle">Pi</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="T"><rect x="72.31" y="409" width="78.24" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="111.43" y="436.6" text-anchor="middle">Tether</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="C"><rect x="190.55" y="409" width="208.06" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="294.59" y="436.6" text-anchor="middle">Official Claude Code CLI</text></g><g class="pele-node pele-actor pele-actor-participant pele-actor-footer" data-id="S"><rect x="438.62" y="409" width="130.7" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="503.97" y="436.6" text-anchor="middle">Claude model</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="P"><rect x="-32" y="0" width="64" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="0" y="27.6" text-anchor="middle">Pi</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="T"><rect x="72.31" y="0" width="78.24" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="111.43" y="27.6" text-anchor="middle">Tether</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="C"><rect x="190.55" y="0" width="208.06" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="294.59" y="27.6" text-anchor="middle">Official Claude Code CLI</text></g><g class="pele-node pele-actor pele-actor-participant" data-id="S"><rect x="438.62" y="0" width="130.7" height="44" fill="#f6f8fa" stroke="#d1d9e0" style="rx:4px"/><text class="pele-label" x="503.97" y="27.6" text-anchor="middle">Claude model</text></g></g></g></svg>
@@ -0,0 +1,16 @@
1
+ sequenceDiagram
2
+ accTitle: The tool call path through the Claude tether
3
+ accDescr: Pi sends the prompt to the model through the tether and the official CLI. The model's tool call comes back the same way to Pi, which runs the tool and saves the result, and only then does the result go back to the model.
4
+ participant P as Pi
5
+ participant T as Tether
6
+ participant C as Official Claude Code CLI
7
+ participant S as Claude model
8
+ P->>S: prompt, tools and history, through the tether and CLI
9
+ S-->>C: tool call
10
+ C-->>T: tool call
11
+ T-->>P: tool call
12
+ Note over P: runs the tool, saves the result
13
+ P->>T: result
14
+ T->>C: result
15
+ C->>S: result
16
+ S-->>P: answer
@@ -0,0 +1,88 @@
1
+ # Automatic account failover (opt-in)
2
+
3
+ > Opt-in only: automatic failover is OFF unless `PI_CLAUDE_CLI_FAILOVER=1`.
4
+ > When off, a rejected subscription limit fails the turn exactly as before —
5
+ > no replay, no exhaustion is recorded, and no exhaustion file is consulted.
6
+ > Manual profile selection (`PI_CLAUDE_CLI_ACCOUNT`, see README) works with
7
+ > or without this gate.
8
+
9
+ When a Claude subscription hits its limit mid-session, Pi 1.4.0 does not
10
+ retry: its classifier treats subscription-limit errors ("You've hit your
11
+ weekly limit · resets …", "You've hit your session limit · resets …") as
12
+ non-retryable, so the turn simply fails. With this gate on, the provider
13
+ itself moves that one request to the next healthy account instead of failing
14
+ the turn.
15
+
16
+ ## Eligibility: the observed production shape only
17
+
18
+ A failover happens only when ALL of these hold for the current attempt:
19
+
20
+ - the CLI emitted a `rate_limit_event` with status `"rejected"` and a finite
21
+ `resetsAt` (the structured signal, not error-text matching);
22
+ - the turn then failed having emitted NO assistant content and NO `tool_use`
23
+ (the observed rows are zero-output `result_error`s);
24
+ - no handoff tool was delivered this turn (mode is not `handoff`, no
25
+ unconfirmed handoff results pending).
26
+
27
+ If anything was emitted or a tool was handed off, the turn fails exactly as
28
+ before — a replay could double-execute a tool or present content the user
29
+ never saw answered. A rejection that arrives after a tool call, or after any
30
+ prose, never replays.
31
+
32
+ A turn billed to overage usage credits never replays onto another account
33
+ either: the usage-credit guard fails it before failover is considered (see
34
+ README "Usage credits (overage) guard").
35
+
36
+ ## Mechanism
37
+
38
+ 1. The failing account is marked exhausted until the `resetsAt` the CLI
39
+ itself reported — one small JSON file per account under the existing
40
+ state dir (`PI_CLAUDE_CLI_STATE_DIR` or the package state dir from README,
41
+ `account-exhausted-<name>.json`), written atomically (write + rename).
42
+ No lock; values come from CLI events and the last writer wins.
43
+ 2. The process is retired and the same request is re-run on the first
44
+ account in `PI_CLAUDE_CLI_ACCOUNTS` order that is neither exhausted nor
45
+ already tried in this provider call, with a full history import, inside
46
+ the same provider call — reusing the existing replay machinery, so pi
47
+ sees a single turn. The tried set lives in memory and bounds the loop:
48
+ each iteration tries a new account, so the call ends after at most one
49
+ attempt per declared account with the all-exhausted error — even when a
50
+ `resetsAt` is stale or an exhaustion write silently failed. The
51
+ persisted files stay the cross-call signal only.
52
+ 3. No switch-back mid-warm-process: a parked process keeps serving until it
53
+ retires naturally. At the next spawn, selection returns to earlier
54
+ accounts on its own once their exhaustion has expired.
55
+ 4. If every account is exhausted, the turn fails with the original CLI
56
+ error plus the per-account reset times.
57
+
58
+ ## Expiry and limits
59
+
60
+ The only clock is the CLI-reported `resetsAt` (unix seconds, compared
61
+ against the current time). Nothing here invents a timeout, count,
62
+ threshold or budget — a rejected window without a usable `resetsAt` fails
63
+ the turn as before rather than guessing an expiry.
64
+
65
+ ## Visibility
66
+
67
+ - The `claude-rate-limit` status payload and every request-ledger row
68
+ carry the account NAME (never the config path) when multiple profiles
69
+ are declared. Single-profile payloads and ledgers are unchanged.
70
+ - The startup auth probe checks every declared profile under its own
71
+ `CLAUDE_CONFIG_DIR`, and the login hint names the exact directory that
72
+ needs `CLAUDE_CONFIG_DIR=<dir> claude auth login --claudeai`.
73
+
74
+ ## Cost and terms notes
75
+
76
+ The prompt cache is per account: a failover costs a full context reimport
77
+ plus a cache write on the other account. Users must check Anthropic's
78
+ current consumer terms themselves before enabling automatic failover
79
+ across subscriptions.
80
+
81
+ ## Proof
82
+
83
+ Unit: `tests/accounts.test.ts` (parsing, selection, exhaustion expiry) and
84
+ `tests/account-failover.test.ts` (failover fires only with the gate on and
85
+ only for zero-output rejections; failover env unset → no replay; tool and
86
+ content cases → no replay; all-exhausted error). End to end: the
87
+ continuity-proof failover scenario (real installed Pi RPC, stub CLI
88
+ rejecting under the default profile and answering under account `b`).