@timmyjl12/pi-agy-bridge 0.4.2 → 0.4.3

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.
package/ARCHITECTURE.md CHANGED
@@ -114,7 +114,7 @@ pi waits for the provider's availability `check` while it starts. The first desi
114
114
 
115
115
  Models carry a reference list price from pi's built-in catalog (`referenceCost`: the id, then without a baked-in level or `-thinking`, then with version dots/dashes swapped; first match among google, anthropic, google-vertex, openai, opencode, openrouter; zero when unknown). On a Google-login account pi shows it as covered by the subscription; on an API-key account it approximates what Google bills.
116
116
 
117
- Measured on agy 1.5.3 (`evidence/cache-and-usage-semantics.md`, pinned by `tests/usage.test.ts`): `inputTokens` is the uncached input, `cachedReadTokens` is separate, `outputTokens` already contains `thoughtTokens`, and `totalTokens == input + output + cachedRead`. pi's `Usage` has the same shape, so the mapping is a copy; the prompt size pi-context sees is `input + cacheRead + cacheWrite`.
117
+ Measured on agy 1.5.3 (`evidence/cache-and-usage-semantics.md`, pinned by `tests/usage.test.ts`): `inputTokens` is the uncached input, `cachedReadTokens` is separate, `outputTokens` already contains `thoughtTokens`, and `totalTokens == input + output + cachedRead`. For a single turn the pi usage fields map directly. When a request includes internal replay or an answer retry, `input`, `output`, `cacheRead`, `cacheWrite`, and cost still cover all reported turns. Pi uses `totalTokens` to estimate context, so that field holds the final turn's context size (or a transcript estimate when the final prompt usage is unavailable), not the sum of repeated billable prefixes.
118
118
 
119
119
  ## Errors
120
120
 
package/README.md CHANGED
@@ -4,7 +4,7 @@ Use Google Antigravity models (Gemini, Claude, GPT-OSS) in [pi](https://github.c
4
4
 
5
5
  It follows the design of `pi-claude-code`: **pi keeps the conversation, the approvals and the tool execution; agy is only the model.** Every pi request becomes one ACP prompt turn. When the model wants a pi tool it writes a small tool-call block, pi runs the tool, and the next request carries the result. Nothing is parked between requests, so a slow tool (a long `pytest` run, a user dialog) can take as long as it needs and there is no turn time limit.
6
6
 
7
- > Status: 0.4.2. agy 1.5.18 retries a failing backend (503 "high demand", 429) silently for minutes; the bridge now reads agy's failed-attempt steps from its conversation store and stops a turn with no output after `PI_AGY_BACKEND_RETRIES` (3) attempts, reporting `agy backend unavailable: …` or `agy quota exceeded: …` so fallback chains move on in seconds instead of after the 120 s silence guard. The account menu has *Use agy login (remove API key)* (also `/agy-bridge use-agy-login <id>`) to go back from an API key to the account's own agy login folder. 0.4.1: Version bump for the npm release of 0.4.0 (no code change). 0.4.0: Continued conversations reattach to agy's own stored conversation (ACP `session/resume`) instead of re-sending history; a read-only check of agy's conversation store falls back to replay when another client or branch continued it. Any account can optionally sign in with a Gemini API key set in `/agy-bridge menu` (agy's `gemini-api-key` method); without one, every account keeps using the real agy CLI with its own Google login. agy's transient "⚠ API error (attempt N)" retry lines no longer leak into answers mid-stream, models carry reference prices from pi's catalog, and a new end-to-end test drives the real pi CLI against a scripted agy. agy 1.5.18 is tested (compat probe identical to 1.5.17); google-antigravity 0.1.24 is still the latest SDK. 0.3.3: Leading quota warnings now trigger fallback even after billable thinking, concurrent pi processes use isolated per-account ACP logs, internal replay ACKs can no longer abort the real request as tool calls, and reasoning-only or whitespace-only answers get one bounded same-session recovery retry without losing buffered text or truncated tool calls. 0.3.2: Large conversation replay is now sequential and bounded, model-window overflow triggers pi compaction before any model call, and switching providers recomputes context from the full pi transcript. agy 1.5.17 and google-antigravity SDK 0.1.24 are tested. 0.3.1: active agy turns no longer hit pi's idle request timeout merely because their total duration exceeds it: every ACP update refreshes that deadline, while the independent silence guard still cancels inactive turns. 0.3.0: agy 1.5.16 and the `google-antigravity` SDK 0.1.24 are now tested; agy 1.5.17 is also recorded and tested with an identical ACP contract. agy 1.5.16 retired `gemini-pro-orchestrator-g1-pro`; its orchestrators are now `gemini-pro-orchestrator` (Carbon), `gemini-pro-orchestrator-argon` and `gemini-pro-orchestrator-neon`, and Gemini 4 Carbon (low, medium, high) is new. The SDK change (a removed `InputConfig.use_interactions_api` field) does not touch the bridge, which uses the SDK only as a drift canary. Stalls and timeouts are now easier to diagnose: an extra account keeps its previous agy log as `acp.prev.log`, and with `PI_AGY_TRACE=1` a stalled or timed-out turn records what agy last sent and when. agy 1.5.16 also forwards subagent and workflow questions to the editor; the bridge's carrier agent has no agy tools, subagents or skills, so agy never asks them here. 0.2.1: Extra-account sign-in works on agy 1.5.14: agy now prints a link and waits for the authorization code, so the bridge opens the link and asks for the code in a dialog. 0.2.0: Multiple Antigravity accounts: each extra account runs as its own provider `agy-<alias or id>` with its own login, managed from `/agy-bridge menu` or the `/agy-bridge` account commands; verified live on agy 1.5.14 with two Google accounts answering side by side. agy 1.5.14 is now tested: it refuses pi's appended system prompt silently (no notice for ~2 min), which `auto` now detects with a 30 s refusal probe and answers inline (see [Multiple Antigravity accounts](#multiple-antigravity-accounts)). 0.1.5: the default bridge home now honors Pi's configured agent directory (`PI_CODING_AGENT_DIR`). Every Pi session shutdown now recycles the outgoing runtime instead of permanently latching it shut, so a late pi-advisor retry can start a fresh ACP process; explicit terminal disposal remains terminal. The compatibility probe passed on `agy 1.5.8`, `1.5.9` and `1.5.12`; the drift from 1.5.9 to 1.5.12 is additive only (the `gemini-4-neon` family, `gemini-pro-orchestrator-neon`, and `xhigh` as a `thought_level`). The `google-antigravity` SDK was then current at `0.1.23`. On 2026-11-14 (agy 1.5.9) the Antigravity backend began refusing pi's default system prompt sent through the append hook with `RESOURCE_EXHAUSTED`; live `gemini-4-hydrogen` turns and pi-subagents runs succeed through the `auto` inline fallback (see `PI_AGY_SYSTEM_PROMPT`). Earlier live bridge checks passed on `agy 1.5.8`. Live turns succeeded on `gemini-3.8-flash`, `gemini-4-hydrogen` (including `xhigh`), and `gemini-pro-orchestrator-g1-pro`. A real pi surface check with the configured extensions also passed. Claude, GPT-OSS and the `gemini-4-neon` family have not been exercised in a live model turn.
7
+ > Status: 0.4.3. Large conversation replay no longer inflates Pi's context estimate and triggers unnecessary compaction. Billing still includes every reported replay turn. 0.4.2: agy 1.5.18 retries a failing backend (503 "high demand", 429) silently for minutes; the bridge now reads agy's failed-attempt steps from its conversation store and stops a turn with no output after `PI_AGY_BACKEND_RETRIES` (3) attempts, reporting `agy backend unavailable: …` or `agy quota exceeded: …` so fallback chains move on in seconds instead of after the 120 s silence guard. The account menu has *Use agy login (remove API key)* (also `/agy-bridge use-agy-login <id>`) to go back from an API key to the account's own agy login folder. 0.4.1: Version bump for the npm release of 0.4.0 (no code change). 0.4.0: Continued conversations reattach to agy's own stored conversation (ACP `session/resume`) instead of re-sending history; a read-only check of agy's conversation store falls back to replay when another client or branch continued it. Any account can optionally sign in with a Gemini API key set in `/agy-bridge menu` (agy's `gemini-api-key` method); without one, every account keeps using the real agy CLI with its own Google login. agy's transient "⚠ API error (attempt N)" retry lines no longer leak into answers mid-stream, models carry reference prices from pi's catalog, and a new end-to-end test drives the real pi CLI against a scripted agy. agy 1.5.18 is tested (compat probe identical to 1.5.17); google-antigravity 0.1.24 is still the latest SDK. 0.3.3: Leading quota warnings now trigger fallback even after billable thinking, concurrent pi processes use isolated per-account ACP logs, internal replay ACKs can no longer abort the real request as tool calls, and reasoning-only or whitespace-only answers get one bounded same-session recovery retry without losing buffered text or truncated tool calls. 0.3.2: Large conversation replay is now sequential and bounded, model-window overflow triggers pi compaction before any model call, and switching providers recomputes context from the full pi transcript. agy 1.5.17 and google-antigravity SDK 0.1.24 are tested. 0.3.1: active agy turns no longer hit pi's idle request timeout merely because their total duration exceeds it: every ACP update refreshes that deadline, while the independent silence guard still cancels inactive turns. 0.3.0: agy 1.5.16 and the `google-antigravity` SDK 0.1.24 are now tested; agy 1.5.17 is also recorded and tested with an identical ACP contract. agy 1.5.16 retired `gemini-pro-orchestrator-g1-pro`; its orchestrators are now `gemini-pro-orchestrator` (Carbon), `gemini-pro-orchestrator-argon` and `gemini-pro-orchestrator-neon`, and Gemini 4 Carbon (low, medium, high) is new. The SDK change (a removed `InputConfig.use_interactions_api` field) does not touch the bridge, which uses the SDK only as a drift canary. Stalls and timeouts are now easier to diagnose: an extra account keeps its previous agy log as `acp.prev.log`, and with `PI_AGY_TRACE=1` a stalled or timed-out turn records what agy last sent and when. agy 1.5.16 also forwards subagent and workflow questions to the editor; the bridge's carrier agent has no agy tools, subagents or skills, so agy never asks them here. 0.2.1: Extra-account sign-in works on agy 1.5.14: agy now prints a link and waits for the authorization code, so the bridge opens the link and asks for the code in a dialog. 0.2.0: Multiple Antigravity accounts: each extra account runs as its own provider `agy-<alias or id>` with its own login, managed from `/agy-bridge menu` or the `/agy-bridge` account commands; verified live on agy 1.5.14 with two Google accounts answering side by side. agy 1.5.14 is now tested: it refuses pi's appended system prompt silently (no notice for ~2 min), which `auto` now detects with a 30 s refusal probe and answers inline (see [Multiple Antigravity accounts](#multiple-antigravity-accounts)). 0.1.5: the default bridge home now honors Pi's configured agent directory (`PI_CODING_AGENT_DIR`). Every Pi session shutdown now recycles the outgoing runtime instead of permanently latching it shut, so a late pi-advisor retry can start a fresh ACP process; explicit terminal disposal remains terminal. The compatibility probe passed on `agy 1.5.8`, `1.5.9` and `1.5.12`; the drift from 1.5.9 to 1.5.12 is additive only (the `gemini-4-neon` family, `gemini-pro-orchestrator-neon`, and `xhigh` as a `thought_level`). The `google-antigravity` SDK was then current at `0.1.23`. On 2026-11-14 (agy 1.5.9) the Antigravity backend began refusing pi's default system prompt sent through the append hook with `RESOURCE_EXHAUSTED`; live `gemini-4-hydrogen` turns and pi-subagents runs succeed through the `auto` inline fallback (see `PI_AGY_SYSTEM_PROMPT`). Earlier live bridge checks passed on `agy 1.5.8`. Live turns succeeded on `gemini-3.8-flash`, `gemini-4-hydrogen` (including `xhigh`), and `gemini-pro-orchestrator-g1-pro`. A real pi surface check with the configured extensions also passed. Claude, GPT-OSS and the `gemini-4-neon` family have not been exercised in a live model turn.
8
8
 
9
9
  ## Requirements
10
10
 
package/compat/probe.ts CHANGED
@@ -40,7 +40,7 @@ export const PROBE_PROTOCOL_VERSION = 1;
40
40
  export const PROBE_CLIENT_INFO = {
41
41
  name: "pi-agy-bridge-compat-probe",
42
42
  title: "pi-agy-bridge zero-cost compat probe",
43
- version: "0.4.2",
43
+ version: "0.4.3",
44
44
  } as const;
45
45
 
46
46
  /** One failed probe step. `step` names the command or exchange. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timmyjl12/pi-agy-bridge",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "pi provider for Google Antigravity (agy) models over the official ACP server, modelled on pi-claude-code: pi keeps the loop, history and tool execution",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -194,7 +194,7 @@ export class AcpProcess {
194
194
  const result = await this.#rpc.request("initialize", {
195
195
  protocolVersion: 1,
196
196
  clientCapabilities: { fs: { readTextFile: false, writeTextFile: false }, terminal: false },
197
- clientInfo: { name: "pi-agy-bridge", title: "pi-agy-bridge", version: "0.4.2" },
197
+ clientInfo: { name: "pi-agy-bridge", title: "pi-agy-bridge", version: "0.4.3" },
198
198
  });
199
199
  return (result ?? {}) as AcpInitializeResult;
200
200
  }
package/src/stream.ts CHANGED
@@ -48,6 +48,10 @@ const BACKEND_POLL_MS = 1_000;
48
48
  const CONTEXT_SAFETY_RATIO = 0.15;
49
49
  const MIN_OUTPUT_RESERVE = 32_768;
50
50
 
51
+ function estimateTranscriptTokens(messages: TranscriptContext["messages"]): number {
52
+ return messages.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
53
+ }
54
+
51
55
  /** Give pi a context-overflow signal before agy can silently truncate or bill a request that cannot fit. Uses the
52
56
  * same token estimate and safety-margin shape as pi's own request option handling, scaled to each model's window. */
53
57
  function assertContextWindow(model: Model<any>, context: TranscriptContext, options: SimpleStreamOptions | undefined): void {
@@ -58,7 +62,7 @@ function assertContextWindow(model: Model<any>, context: TranscriptContext, opti
58
62
  // recompute every message instead of importing that provider-specific view of context size.
59
63
  const tokens =
60
64
  usageMessage?.role === "assistant" && (usageMessage.provider !== model.provider || usageMessage.api !== model.api)
61
- ? context.messages.reduce((sum, message) => sum + estimateMessageTokens(message), 0)
65
+ ? estimateTranscriptTokens(context.messages)
62
66
  : estimate.tokens;
63
67
  const safety = Math.max(4096, Math.ceil(tokens * CONTEXT_SAFETY_RATIO));
64
68
  const outputReserve = Math.min(options?.maxTokens ?? model.maxTokens, MIN_OUTPUT_RESERVE);
@@ -571,7 +575,18 @@ async function run(
571
575
  }
572
576
  translator.finish();
573
577
 
574
- if (turn.result) mapUsage(model, mergeUsage([...replayUsages, turn.result.usage]), output.usage);
578
+ if (turn.result) {
579
+ const finalUsage = turn.result.usage;
580
+ mapUsage(model, mergeUsage([...replayUsages, finalUsage]), output.usage);
581
+ if (replayUsages.some((usage) => usage !== undefined) && output.usage.totalTokens > 0) {
582
+ // The breakdown and cost are billable totals. Pi reads totalTokens as a single context size,
583
+ // so don't count the same growing prefix once per replay or recovery request.
584
+ const finalPromptTokens = (finalUsage?.inputTokens ?? 0) + (finalUsage?.cachedReadTokens ?? 0) + (finalUsage?.cachedWriteTokens ?? 0);
585
+ output.usage.totalTokens = finalPromptTokens > 0
586
+ ? finalPromptTokens + (finalUsage?.outputTokens ?? 0)
587
+ : estimateTranscriptTokens(context.messages) + estimateMessageTokens(output);
588
+ }
589
+ }
575
590
  output.rawStopReason = turn.result?.stopReason;
576
591
  runtime.trace(
577
592
  `turn: model=${model.id} reused=${lease.reused} agy=${turn.result?.stopReason ?? "none"} in=${output.usage.input} cacheRead=${output.usage.cacheRead} out=${output.usage.output} replayParts=${replayUsages.length} promptChars=${promptChars}`,