pi-agy-acp 0.2.5 → 0.2.7
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/CHANGELOG.md +14 -0
- package/README.md +1 -1
- package/docs/architecture.md +16 -7
- package/docs/development.md +1 -1
- package/docs/reference.md +7 -6
- package/package.json +4 -4
- package/src/acp-process.ts +1 -0
- package/src/config.ts +1 -1
- package/src/conversation.ts +18 -14
- package/src/format.ts +3 -3
- package/src/history.ts +99 -1
- package/src/preflight.ts +2 -2
- package/src/quota.ts +8 -8
- package/src/runtime.ts +74 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.7](https://github.com/fdkevin0/pi-agy-acp/compare/v0.2.6...v0.2.7) (2026-12-03)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **history:** split oversized opening instructions into prelude prompts ([#65](https://github.com/fdkevin0/pi-agy-acp/issues/65)) ([adebbb2](https://github.com/fdkevin0/pi-agy-acp/commit/adebbb2816901b8fe4f3b5f427ec9867026bd713))
|
|
9
|
+
|
|
10
|
+
## [0.2.6](https://github.com/fdkevin0/pi-agy-acp/compare/v0.2.5...v0.2.6) (2026-12-03)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **agy:** adapt to agy 1.5.23 usage subcommand, retry notices, and -32002 errors ([#62](https://github.com/fdkevin0/pi-agy-acp/issues/62)) ([5f9c705](https://github.com/fdkevin0/pi-agy-acp/commit/5f9c705e83ecc4798cb6d7b705dddd33464f1b2e))
|
|
16
|
+
|
|
3
17
|
## [0.2.5](https://github.com/fdkevin0/pi-agy-acp/compare/v0.2.4...v0.2.5) (2026-11-28)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ parallel tool calls.
|
|
|
12
12
|
|
|
13
13
|
- Node.js >= 22.19.0.
|
|
14
14
|
- Pi (`@earendil-works/pi-coding-agent`) 1.5.x.
|
|
15
|
-
- The official `agy` CLI >= 1.5.
|
|
15
|
+
- The official `agy` CLI >= 1.5.23, signed in with your Google account.
|
|
16
16
|
Run `agy` (or `agy acp --login` in headless/SSH terminals) to complete sign-in
|
|
17
17
|
before using the extension.
|
|
18
18
|
|
package/docs/architecture.md
CHANGED
|
@@ -35,7 +35,13 @@ Working directory is `<PI_AGY_HOME>/workspace`. ACP JSON-RPC timeouts:
|
|
|
35
35
|
(`<harness_tool_instructions>`), and trailing live input are always emitted
|
|
36
36
|
complete. `<pi_history>` fills the remaining bytes of the 160,000-byte replay
|
|
37
37
|
budget (`REPLAY_BUDGET_BYTES`, kept under `agy`'s 192,000-byte single
|
|
38
|
-
`USER_INPUT` limit) newest-first.
|
|
38
|
+
`USER_INPUT` limit) newest-first. When `<system_instructions>` +
|
|
39
|
+
`<harness_tool_instructions>` + live input would starve history or exceed
|
|
40
|
+
`PRELUDE_CHUNK_BYTES` (`184,000` bytes), `openingPrompts()` sends the
|
|
41
|
+
instructions first as one or more prelude prompts (asking for `OK` with no
|
|
42
|
+
bound turn, while still inspecting SQLite hidden errors and marking the
|
|
43
|
+
session dirty on abort/failure) so `<pi_history>` and the live input stay
|
|
44
|
+
intact in the final prompt.
|
|
39
45
|
- If the single newest history item is an oversized tool result and remaining
|
|
40
46
|
budget exceeds 1,024 bytes, it is head-truncated on UTF-8 code-point
|
|
41
47
|
boundaries. Historical images render as `[image]`; trailing user images and
|
|
@@ -93,8 +99,9 @@ Working directory is `<PI_AGY_HOME>/workspace`. ACP JSON-RPC timeouts:
|
|
|
93
99
|
`tier`, `contextWindows`, and the subprocess-free binary fingerprint
|
|
94
100
|
(`${realpath}:${dev}:${ino}:${size}:${mtimeNs}`).
|
|
95
101
|
- Both `agy --version` (30 s timeout) and ACP `initialize` (`agentInfo.version`)
|
|
96
|
-
enforce `MIN_AGY_VERSION` (`1.5.
|
|
97
|
-
|
|
102
|
+
enforce `MIN_AGY_VERSION` (`1.5.23`, which adds `agy usage`, standardizes
|
|
103
|
+
`-32002` unknown-session errors across ACP methods, and updates combined
|
|
104
|
+
reasoning+output retry guidance).
|
|
98
105
|
- Non-forced refreshes reuse `catalog.json` without spawning `agy` whenever
|
|
99
106
|
cached models match the current binary fingerprint. Loading a cache with a
|
|
100
107
|
mismatched fingerprint drops cached `contextWindows` while keeping models
|
|
@@ -133,7 +140,9 @@ Working directory is `<PI_AGY_HOME>/workspace`. ACP JSON-RPC timeouts:
|
|
|
133
140
|
tenant, re-applies model/effort via `select()`, and sends only the delta
|
|
134
141
|
input. Any `JsonRpcRemoteError` from `session/resume` (including `-32603` and
|
|
135
142
|
`-32002`) or history divergence cleans the dormant DB and falls back to
|
|
136
|
-
`session/new` + `replayPrompt()`.
|
|
143
|
+
`session/new` + `replayPrompt()`. Unknown-session `-32002` errors from
|
|
144
|
+
`session/set_config_option` or `session/prompt` mark the conversation dirty
|
|
145
|
+
so the next turn rebuilds cleanly. Explicit teardown (`resetConversations()`,
|
|
137
146
|
`dispose()`, binary upgrade, LRU eviction) deletes dormant DBs immediately.
|
|
138
147
|
- **Pi lifecycle resets:** Compaction, tree navigation, and `session_start`
|
|
139
148
|
(`new`, `resume`, `fork`) perform a light reset (closing conversation sessions
|
|
@@ -220,14 +229,14 @@ Working directory is `<PI_AGY_HOME>/workspace`. ACP JSON-RPC timeouts:
|
|
|
220
229
|
- Gates streaming reads with cached context window / positive `usage_update`
|
|
221
230
|
checks and `(mtimeNs, size)` stamps across `.db`, `.db-wal`, and
|
|
222
231
|
`.db-journal`. `gen_metadata.data` remains the authoritative per-model
|
|
223
|
-
`max_context_tokens` source in `agy` 1.5.
|
|
232
|
+
`max_context_tokens` source in `agy` 1.5.23 because `usage_update.size` still
|
|
224
233
|
reports a fixed `1048576` rather than per-model limits. `gen_metadata` scans
|
|
225
234
|
are bounded (`LIMIT 64`) and advance `metaFloorIdx` on mid-session model
|
|
226
235
|
switches so prior context windows cannot bleed across models.
|
|
227
236
|
- On `end_turn` and `refusal`, inspects the newest terminal `steps` row
|
|
228
237
|
(`step_type = 17`, cross-checked against `step_type = 15` model reachability)
|
|
229
238
|
to surface hidden API errors while preserving pure safety refusals. This
|
|
230
|
-
remains necessary in `agy` 1.5.
|
|
239
|
+
remains necessary in `agy` 1.5.23 because certain API errors (such as
|
|
231
240
|
`INVALID_ARGUMENT (code 400)` on malformed images) still complete
|
|
232
241
|
`session/prompt` with `stopReason: "end_turn"`:
|
|
233
242
|
- `QUOTA_EXHAUSTED` → `QUOTA` (extracts reset timestamp/delay)
|
|
@@ -268,7 +277,7 @@ tool-heavy or continued turns:
|
|
|
268
277
|
|
|
269
278
|
## Quota reader and Pi stream contracts
|
|
270
279
|
|
|
271
|
-
- **Quota reader (`src/quota.ts`):** Runs `agy
|
|
280
|
+
- **Quota reader (`src/quota.ts`):** Runs `agy usage --output-format json`
|
|
272
281
|
with closed `stdin`, a 1 MiB `stdout` cap, a 45 s timeout, and early abort if
|
|
273
282
|
`stderr` matches `Authentication required`. Uses a monotonic clock
|
|
274
283
|
(`performance.now()`) for its 2-minute TTL, filters `disabled === true`
|
package/docs/development.md
CHANGED
|
@@ -47,7 +47,7 @@ SQLite telemetry, and transport boundary invariants.
|
|
|
47
47
|
| `src/acp-process.ts`, `src/jsonrpc.ts`, `src/mcp-bridge.ts` | ACP stdio transport, NDJSON JSON-RPC peer, loopback MCP HTTP/SSE bridge, schema compression, and tool deltas. |
|
|
48
48
|
| `src/config.ts` | Default runtime thresholds, effort/display types, and atomic `settings.json` persistence. |
|
|
49
49
|
| `src/host.ts`, `src/preflight.ts`, `src/session-db.ts` | Subprocess management and env/stderr hardening, binary discovery/fingerprinting/OAuth sync, and read-only session SQLite telemetry/error inspection and artifact cleanup. |
|
|
50
|
-
| `src/catalog.ts`, `src/quota.ts`, `src/format.ts`, `src/util.ts` | Model catalog persistence,
|
|
50
|
+
| `src/catalog.ts`, `src/quota.ts`, `src/format.ts`, `src/util.ts` | Model catalog persistence, `agy usage` quota reader and login-log email lookup, TUI notice formatting and model-to-group matching, and shared utilities. |
|
|
51
51
|
| `src/version.ts` | Package version read from `package.json` for ACP/MCP handshakes. |
|
|
52
52
|
| `tests/` | Scripted local `agy` fixture (`fake-agy.mjs`), unit tests, and Pi integration tests. |
|
|
53
53
|
|
package/docs/reference.md
CHANGED
|
@@ -21,7 +21,7 @@ In interactive TUI mode, `/agy-acp quota` (`Fetching Antigravity quota...`) and
|
|
|
21
21
|
`/agy-acp refresh` (`Refreshing Antigravity models...`) display a cancellable
|
|
22
22
|
loader. Pressing Escape returns to the editor: `/agy-acp refresh` cancels the
|
|
23
23
|
caller's wait, while `/agy-acp quota` hides the pending panel even if the
|
|
24
|
-
underlying
|
|
24
|
+
underlying `agy usage` query finishes updating the cache. Non-TUI modes run
|
|
25
25
|
directly without a loader.
|
|
26
26
|
|
|
27
27
|
### Status panel
|
|
@@ -41,7 +41,7 @@ healthy runtime uses a warning.
|
|
|
41
41
|
|
|
42
42
|
### Quota panel
|
|
43
43
|
|
|
44
|
-
Quota is read through `agy
|
|
44
|
+
Quota is read through `agy usage --output-format json`. Disabled quota
|
|
45
45
|
buckets are excluded. Each model group renders one row per usable quota window
|
|
46
46
|
with a 12-cell bar, a rounded percentage, and a relative reset countdown:
|
|
47
47
|
|
|
@@ -95,7 +95,7 @@ unavailable fields:
|
|
|
95
95
|
|
|
96
96
|
### Quota status and warnings
|
|
97
97
|
|
|
98
|
-
After an `antigravity-acp` run settles, the extension refreshes
|
|
98
|
+
After an `antigravity-acp` run settles, the extension refreshes `agy usage` in the
|
|
99
99
|
background (sharing any in-flight request and respecting the 2-minute TTL) and
|
|
100
100
|
displays a compact status bar indicator such as `AGY 20%` (or `AGY 80%` in
|
|
101
101
|
`remaining` mode):
|
|
@@ -164,7 +164,7 @@ session databases).
|
|
|
164
164
|
refusing symlinked paths. Sync failures warn rather than aborting startup. A
|
|
165
165
|
custom `PI_AGY_HOME` stays isolated and never copies tokens from default `agy`
|
|
166
166
|
directories.
|
|
167
|
-
- **Global quota scope:** `agy
|
|
167
|
+
- **Global quota scope:** `agy usage` does not accept `--gemini-dir`, so
|
|
168
168
|
quota queries always reflect the global `agy` CLI account. Do not commit or
|
|
169
169
|
share `PI_AGY_HOME`.
|
|
170
170
|
|
|
@@ -225,8 +225,9 @@ or invokes/distributes binary patches.
|
|
|
225
225
|
## Compatibility limits
|
|
226
226
|
|
|
227
227
|
- Requires Node.js >= 22.19.0, Pi (`@earendil-works/*`) 1.5.x, and `agy` >=
|
|
228
|
-
1.5.
|
|
229
|
-
|
|
228
|
+
1.5.23 (which adds the standalone `agy usage` subcommand, standardizes
|
|
229
|
+
`-32002` unknown-session ACP errors, and updates combined reasoning+output
|
|
230
|
+
retry handling). Older `@mariozechner/*` Pi interfaces are not supported.
|
|
230
231
|
- Sampling parameters, output limits, forced `toolChoice`, and provider-scoped
|
|
231
232
|
environment overrides are not mapped to ACP.
|
|
232
233
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-agy-acp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Google Antigravity ACP provider extension for Pi Coding Agent",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"typescript": "7.0.2",
|
|
42
|
-
"@types/node": "22.20.
|
|
43
|
-
"@earendil-works/pi-ai": "1.
|
|
44
|
-
"@earendil-works/pi-coding-agent": "1.
|
|
42
|
+
"@types/node": "22.20.7",
|
|
43
|
+
"@earendil-works/pi-ai": "1.7.0",
|
|
44
|
+
"@earendil-works/pi-coding-agent": "1.7.0"
|
|
45
45
|
},
|
|
46
46
|
"keywords": [
|
|
47
47
|
"pi-package",
|
package/src/acp-process.ts
CHANGED
package/src/config.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface Config {
|
|
|
38
38
|
relayAfterMs: number
|
|
39
39
|
/** Automatic continuations after an output-length stop (long decode). */
|
|
40
40
|
longDecodeContinuations: number
|
|
41
|
-
/** Minimum interval between quota refreshes through `agy
|
|
41
|
+
/** Minimum interval between quota refreshes through `agy usage`. */
|
|
42
42
|
quotaTtlMs: number
|
|
43
43
|
}
|
|
44
44
|
|
package/src/conversation.ts
CHANGED
|
@@ -33,12 +33,11 @@ import { AsyncQueue, deferred, type Deferred } from './util.ts'
|
|
|
33
33
|
|
|
34
34
|
/** Agent-side transport retry notice streamed as message text, e.g. `⚠ API error (attempt 1): …`. */
|
|
35
35
|
const AGENT_RETRY_NOTICE = /^\s*⚠\s*API error \(attempt \d+\)/
|
|
36
|
-
/**
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
'please continue from where you left off, keeping your response shorter'
|
|
36
|
+
/** Discarded-attempt prefix agy 1.5.23+ emits on retryable generation failures (`%s\n%s\nRetries remaining: %d`). */
|
|
37
|
+
const DISCARDED_ATTEMPT_PREFIX = 'a response attempt was discarded'
|
|
38
|
+
/** Standalone retry guidance agy 1.5.23+ pairs with combined reasoning+output limit discards. */
|
|
39
|
+
const REASONING_OUTPUT_LIMIT_TAIL =
|
|
40
|
+
'please keep your reasoning and response shorter'
|
|
42
41
|
/** Internal retry notice agy emits when an upstream stream is interrupted mid-turn. */
|
|
43
42
|
const STREAM_INTERRUPTED =
|
|
44
43
|
'the stream was interrupted. please continue the task you were working on.'
|
|
@@ -49,7 +48,7 @@ const EMPTY_MODEL_OUTPUT_BODY =
|
|
|
49
48
|
'model output must contain either output text or tool calls, these cannot both be empty, please try again'
|
|
50
49
|
/** Generic retry notice prefix paired with a trailing `Retries remaining: <N>`. */
|
|
51
50
|
const PROBLEM_RETRY_PREFIX = 'there was a problem'
|
|
52
|
-
/** Cap on held characters while waiting for `Retries remaining: <N>` after
|
|
51
|
+
/** Cap on held characters while waiting for `Retries remaining: <N>` after a generic retry prefix. */
|
|
53
52
|
const MAX_PROBLEM_NOTICE_CHARS = 512
|
|
54
53
|
|
|
55
54
|
/** Collapse agy's warning mark, line-leading `Error:`, and line breaks so notices match across chunks. */
|
|
@@ -87,11 +86,9 @@ export function classifyOutputLimitNotice(
|
|
|
87
86
|
if (/^retries remaining:\s*[1-9]\d*$/i.test(text)) return 'drop'
|
|
88
87
|
return /^retries remaining:\s*$/i.test(text) ? 'hold' : 'emit'
|
|
89
88
|
}
|
|
90
|
-
const
|
|
89
|
+
const normalized = stripped.replace(/\./g, '').replace(/\s+/g, ' ').trim()
|
|
91
90
|
if (
|
|
92
|
-
|
|
93
|
-
stripped === OUTPUT_LIMIT_HEAD ||
|
|
94
|
-
stripped === OUTPUT_LIMIT_TAIL ||
|
|
91
|
+
normalized === REASONING_OUTPUT_LIMIT_TAIL ||
|
|
95
92
|
stripped === STREAM_INTERRUPTED ||
|
|
96
93
|
stripped === EMPTY_MODEL_OUTPUT ||
|
|
97
94
|
stripped === EMPTY_MODEL_OUTPUT_BODY
|
|
@@ -100,11 +97,18 @@ export function classifyOutputLimitNotice(
|
|
|
100
97
|
return 'drop'
|
|
101
98
|
return /^retries remaining:\s*$/i.test(retry) ? 'hold' : 'emit'
|
|
102
99
|
}
|
|
103
|
-
if (
|
|
100
|
+
if (/^a response attempt was discarded\b/.test(text)) {
|
|
101
|
+
if (/\bretries remaining:\s*[1-9]\d*$/.test(text)) return 'drop'
|
|
102
|
+
return text.length <= MAX_PROBLEM_NOTICE_CHARS ? 'hold' : 'emit'
|
|
103
|
+
}
|
|
104
|
+
if (
|
|
105
|
+
DISCARDED_ATTEMPT_PREFIX.startsWith(text) &&
|
|
106
|
+
text.startsWith('a response attempt')
|
|
107
|
+
)
|
|
104
108
|
return 'hold'
|
|
105
109
|
if (
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
REASONING_OUTPUT_LIMIT_TAIL.startsWith(normalized) &&
|
|
111
|
+
normalized.startsWith('please keep your reasoning')
|
|
108
112
|
)
|
|
109
113
|
return 'hold'
|
|
110
114
|
if (
|
package/src/format.ts
CHANGED
|
@@ -22,13 +22,13 @@ function formatAccount(account?: AccountInfo): string {
|
|
|
22
22
|
return [account?.email, account?.plan].filter(Boolean).join(' · ')
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/** Active model identity from Pi (`id` plus optional display `name` for
|
|
25
|
+
/** Active model identity from Pi (`id` plus optional display `name` for `agy usage` labels such as `Orchestrator (Carbon)`). */
|
|
26
26
|
export interface QuotaModelRef {
|
|
27
27
|
readonly id: string
|
|
28
28
|
readonly name?: string
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// `agy
|
|
31
|
+
// `agy usage` prefixes closed, exhaustive member lists with "Models within this group:";
|
|
32
32
|
// unprefixed descriptions are free-form prose notes.
|
|
33
33
|
const EXPLICIT_MEMBERS_PREFIX = /^\s*models within this group:\s*/i
|
|
34
34
|
const CLAUSE_SPLIT = /[,;\n/|&+]+|\b(?:and|or)\b/i
|
|
@@ -40,7 +40,7 @@ const EFFORT_QUALIFIER_PATTERN = [
|
|
|
40
40
|
'extra[-_]low',
|
|
41
41
|
...[...EFFORTS].sort((a, b) => b.length - a.length),
|
|
42
42
|
].join('|')
|
|
43
|
-
// Effort or thinking-mode suffixes in model IDs (`-thinking`) and
|
|
43
|
+
// Effort or thinking-mode suffixes in model IDs (`-thinking`) and `agy usage` labels (`(Thinking)`).
|
|
44
44
|
const ID_QUALIFIER_SUFFIX = new RegExp(
|
|
45
45
|
`[-_](?:(${EFFORT_QUALIFIER_PATTERN})(?:[-_]effort)?|(thinking))$`,
|
|
46
46
|
'i',
|
package/src/history.ts
CHANGED
|
@@ -30,6 +30,13 @@ export const CALL_TOOL_NAME = 'pi_call_tool'
|
|
|
30
30
|
*/
|
|
31
31
|
export const REPLAY_BUDGET_BYTES = 160_000
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Byte ceiling for one prelude prompt that carries session instructions
|
|
35
|
+
* ahead of the live turn, kept below `agy`'s 192,000-byte single-`USER_INPUT`
|
|
36
|
+
* truncation limit with room for the acknowledgement line.
|
|
37
|
+
*/
|
|
38
|
+
export const PRELUDE_CHUNK_BYTES = 184_000
|
|
39
|
+
|
|
33
40
|
const HISTORY_OPEN = '<pi_history>\n'
|
|
34
41
|
const HISTORY_CLOSE = '\n</pi_history>\n\n'
|
|
35
42
|
/** Upper bound of the omission notice, reserved before history is selected. */
|
|
@@ -111,7 +118,7 @@ export function bridgeSystemPrompt(): string {
|
|
|
111
118
|
return [
|
|
112
119
|
'You are the primary model of a Pi coding agent session. The harness owns the conversation, the tools, and their permissions.',
|
|
113
120
|
`Every harness tool is available to you under the prefix \`${AGENT_TOOL_PREFIX}\`: a tool named \`X\` in the harness instructions is the tool \`${AGENT_TOOL_PREFIX}X\`. If \`${AGENT_TOOL_PREFIX}X\` is absent from your direct tool declarations, invoke it via \`${AGENT_TOOL_PREFIX}${CALL_TOOL_NAME}\` with \`{"name":"X","arguments":{...}}\`. Use only these tools; pass arguments exactly as their schemas define.`,
|
|
114
|
-
'When the
|
|
121
|
+
'When the opening user messages contain <system_instructions>, <harness_tool_instructions>, or <pi_history> blocks, treat <system_instructions> and <harness_tool_instructions> as harness instructions for the entire session and <pi_history> as the earlier part of this conversation; continue from the newest input after them without repeating completed actions. Large instructions may arrive split across several opening messages marked as harness instruction parts; answer each of those with only OK.',
|
|
115
122
|
].join('\n')
|
|
116
123
|
}
|
|
117
124
|
|
|
@@ -279,6 +286,97 @@ export function replayPrompt(
|
|
|
279
286
|
return blocks
|
|
280
287
|
}
|
|
281
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Plan the prompts that (re)create a session. While the session instructions
|
|
291
|
+
* and live input fit in one prompt without starving history or exceeding
|
|
292
|
+
* `agy`'s single-input truncation limit, this returns the single
|
|
293
|
+
* {@link replayPrompt}. Otherwise the instructions go first as one or more
|
|
294
|
+
* prelude prompts (each <= {@link PRELUDE_CHUNK_BYTES} plus a short
|
|
295
|
+
* acknowledgement footer) and the final prompt carries `<pi_history>` and the
|
|
296
|
+
* live input with the full replay budget.
|
|
297
|
+
* @param history - messages before the trailing input.
|
|
298
|
+
* @param input - trailing input messages sent as live content.
|
|
299
|
+
* @param options - session instructions and replay byte ceiling.
|
|
300
|
+
* @returns prompts in send order; only the last one is the live turn.
|
|
301
|
+
*/
|
|
302
|
+
export function openingPrompts(
|
|
303
|
+
history: readonly RequestMessage[],
|
|
304
|
+
input: readonly RequestMessage[],
|
|
305
|
+
options: ReplayPromptOptions = {},
|
|
306
|
+
): AcpPromptBlock[][] {
|
|
307
|
+
const preamble = renderSessionInstructions(
|
|
308
|
+
options.system ?? '',
|
|
309
|
+
options.supplementalTools ?? [],
|
|
310
|
+
)
|
|
311
|
+
const maxBytes = options.maxHistoryBytes ?? REPLAY_BUDGET_BYTES
|
|
312
|
+
const inputBlocks =
|
|
313
|
+
input.length > 0
|
|
314
|
+
? inputPrompt(input)
|
|
315
|
+
: [{ type: 'text' as const, text: EMPTY_INPUT_CONTINUATION }]
|
|
316
|
+
const historyOverhead =
|
|
317
|
+
history.length > 0
|
|
318
|
+
? utf8Bytes(HISTORY_OPEN) +
|
|
319
|
+
utf8Bytes(HISTORY_CLOSE) +
|
|
320
|
+
OMISSION_NOTICE_RESERVE_BYTES
|
|
321
|
+
: 0
|
|
322
|
+
// Without history, avoid an extra prelude LLM round-trip while preamble + input still fit under agy's 192 KB cap.
|
|
323
|
+
const singlePromptLimit =
|
|
324
|
+
history.length > 0 || options.maxHistoryBytes !== undefined
|
|
325
|
+
? maxBytes
|
|
326
|
+
: Math.max(maxBytes, PRELUDE_CHUNK_BYTES)
|
|
327
|
+
if (
|
|
328
|
+
preamble.length === 0 ||
|
|
329
|
+
utf8Bytes(preamble) + textBytes(inputBlocks) + historyOverhead <=
|
|
330
|
+
singlePromptLimit
|
|
331
|
+
)
|
|
332
|
+
return [replayPrompt(history, input, options)]
|
|
333
|
+
const parts = splitWithinBytes(preamble, PRELUDE_CHUNK_BYTES)
|
|
334
|
+
const preludes = parts.map((text, index) => [
|
|
335
|
+
{
|
|
336
|
+
type: 'text' as const,
|
|
337
|
+
text: `${text}${preludeAck(index + 1, parts.length)}`,
|
|
338
|
+
},
|
|
339
|
+
])
|
|
340
|
+
return [
|
|
341
|
+
...preludes,
|
|
342
|
+
replayPrompt(history, input, { maxHistoryBytes: maxBytes }),
|
|
343
|
+
]
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function preludeAck(part: number, total: number): string {
|
|
347
|
+
const next =
|
|
348
|
+
part < total
|
|
349
|
+
? 'More harness instructions follow.'
|
|
350
|
+
: 'The conversation history and the newest input arrive in the next message.'
|
|
351
|
+
return `\n[Harness instructions part ${part} of ${total}. ${next} Reply with only OK and do not call any tools.]`
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function textBytes(blocks: readonly AcpPromptBlock[]): number {
|
|
355
|
+
return blocks.reduce(
|
|
356
|
+
(total, block) =>
|
|
357
|
+
total + (block.type === 'text' ? utf8Bytes(block.text) : 0),
|
|
358
|
+
0,
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Split text into chunks of at most `maxBytes`, breaking after a newline when
|
|
364
|
+
* one falls in the second half of a chunk.
|
|
365
|
+
*/
|
|
366
|
+
function splitWithinBytes(text: string, maxBytes: number): string[] {
|
|
367
|
+
const chunks: string[] = []
|
|
368
|
+
let rest = text
|
|
369
|
+
while (utf8Bytes(rest) > maxBytes) {
|
|
370
|
+
let head = headWithinBytes(rest, maxBytes)
|
|
371
|
+
const newline = head.lastIndexOf('\n')
|
|
372
|
+
if (newline > head.length / 2) head = head.slice(0, newline + 1)
|
|
373
|
+
chunks.push(head)
|
|
374
|
+
rest = rest.slice(head.length)
|
|
375
|
+
}
|
|
376
|
+
if (rest.length > 0) chunks.push(rest)
|
|
377
|
+
return chunks
|
|
378
|
+
}
|
|
379
|
+
|
|
282
380
|
/**
|
|
283
381
|
* Render the newest messages that fit `budget`, announcing any omitted prefix.
|
|
284
382
|
* @param history - messages before the trailing input.
|
package/src/preflight.ts
CHANGED
|
@@ -33,8 +33,8 @@ export { atomicWriteFile }
|
|
|
33
33
|
|
|
34
34
|
/** Agent id installed in the isolated home. */
|
|
35
35
|
export const BRIDGE_AGENT = 'pi-bridge'
|
|
36
|
-
/** Minimum official CLI version supported (`agy 1.5.
|
|
37
|
-
export const MIN_AGY_VERSION: readonly [number, number, number] = [1, 5,
|
|
36
|
+
/** Minimum official CLI version supported (`agy 1.5.23+`). */
|
|
37
|
+
export const MIN_AGY_VERSION: readonly [number, number, number] = [1, 5, 23]
|
|
38
38
|
|
|
39
39
|
/** Output of one finished helper command. */
|
|
40
40
|
export interface CommandOutput {
|
package/src/quota.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Plan quota read through the official CLI's local
|
|
3
|
-
* reports each model group's weekly and five-hour windows without
|
|
4
|
-
* model request.
|
|
2
|
+
* Plan quota read through the official CLI's local `agy usage` command, which
|
|
3
|
+
* reports each model group's weekly and five-hour windows without starting a
|
|
4
|
+
* conversation or sending a model request.
|
|
5
5
|
* @module pi-agy-acp/quota
|
|
6
6
|
*/
|
|
7
7
|
|
|
@@ -216,7 +216,7 @@ function parseBucket(raw: unknown): QuotaBucket | undefined {
|
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
/**
|
|
219
|
-
* Parse `agy
|
|
219
|
+
* Parse `agy usage --output-format json` output.
|
|
220
220
|
* @param stdout - command stdout.
|
|
221
221
|
* @returns the quota groups.
|
|
222
222
|
*/
|
|
@@ -235,7 +235,7 @@ export function parseUsage(stdout: string): QuotaGroup[] {
|
|
|
235
235
|
? (data as { groups?: unknown }).groups
|
|
236
236
|
: undefined
|
|
237
237
|
if (!Array.isArray(groups))
|
|
238
|
-
throw new Error('agy
|
|
238
|
+
throw new Error('agy usage returned no quota groups')
|
|
239
239
|
return groups
|
|
240
240
|
.filter(
|
|
241
241
|
(raw): raw is Record<string, unknown> =>
|
|
@@ -309,7 +309,7 @@ export class QuotaReader {
|
|
|
309
309
|
try {
|
|
310
310
|
const binary = this.binary()
|
|
311
311
|
const result = await this.run(
|
|
312
|
-
[binary, '
|
|
312
|
+
[binary, 'usage', '--output-format', 'json'],
|
|
313
313
|
45_000,
|
|
314
314
|
{
|
|
315
315
|
maxStdoutBytes: USAGE_MAX_STDOUT_BYTES,
|
|
@@ -326,12 +326,12 @@ export class QuotaReader {
|
|
|
326
326
|
}
|
|
327
327
|
if (result.abortedBy === 'maxStdout') {
|
|
328
328
|
throw new Error(
|
|
329
|
-
`agy
|
|
329
|
+
`agy usage stdout exceeded ${USAGE_MAX_STDOUT_BYTES} bytes`,
|
|
330
330
|
)
|
|
331
331
|
}
|
|
332
332
|
if (result.code !== 0)
|
|
333
333
|
throw new Error(
|
|
334
|
-
`agy
|
|
334
|
+
`agy usage failed (exit ${result.code}): ${result.stderr.trim() || result.stdout.trim()}`,
|
|
335
335
|
)
|
|
336
336
|
const snapshot: QuotaSnapshot = {
|
|
337
337
|
status: 'ok',
|
package/src/runtime.ts
CHANGED
|
@@ -51,9 +51,9 @@ import {
|
|
|
51
51
|
bridgeSystemPrompt,
|
|
52
52
|
inputPrompt,
|
|
53
53
|
MCP_SERVER_NAME,
|
|
54
|
+
openingPrompts,
|
|
54
55
|
renderSessionInstructions,
|
|
55
56
|
renderTranscript,
|
|
56
|
-
replayPrompt,
|
|
57
57
|
splitSystem,
|
|
58
58
|
type AcpPromptBlock,
|
|
59
59
|
} from './history.ts'
|
|
@@ -399,6 +399,13 @@ export class AntigravityRuntime {
|
|
|
399
399
|
}
|
|
400
400
|
return result
|
|
401
401
|
} catch (error) {
|
|
402
|
+
if (
|
|
403
|
+
error instanceof JsonRpcRemoteError &&
|
|
404
|
+
error.rpc.code === -32002 &&
|
|
405
|
+
this.owns(proc, conv, sessionId)
|
|
406
|
+
) {
|
|
407
|
+
conv.dirty = true
|
|
408
|
+
}
|
|
402
409
|
if (!harvested) {
|
|
403
410
|
this.harvestSessionTurnData(conv, sessionId, model, modelFloor, {
|
|
404
411
|
turn,
|
|
@@ -812,11 +819,14 @@ export class AntigravityRuntime {
|
|
|
812
819
|
effort = await this.select(conv, proc, options.model, effort)
|
|
813
820
|
this.assertOwner(proc, conv, conv.sessionId)
|
|
814
821
|
let blocks: AcpPromptBlock[]
|
|
822
|
+
let preludes: AcpPromptBlock[][] = []
|
|
815
823
|
if (plan.rebuild) {
|
|
816
|
-
|
|
824
|
+
const prompts = openingPrompts(plan.history, plan.input, {
|
|
817
825
|
system: split.system,
|
|
818
826
|
supplementalTools,
|
|
819
827
|
})
|
|
828
|
+
blocks = prompts[prompts.length - 1] as AcpPromptBlock[]
|
|
829
|
+
preludes = prompts.slice(0, -1)
|
|
820
830
|
} else {
|
|
821
831
|
blocks = inputPrompt(plan.input)
|
|
822
832
|
if (supplementalTools.length > 0) {
|
|
@@ -831,6 +841,17 @@ export class AntigravityRuntime {
|
|
|
831
841
|
if (toolDelta.length > 0)
|
|
832
842
|
blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
|
|
833
843
|
}
|
|
844
|
+
for (const prelude of preludes) {
|
|
845
|
+
if (options.signal?.aborted) break
|
|
846
|
+
await this.runPrelude(conv, proc, prelude, options.signal)
|
|
847
|
+
}
|
|
848
|
+
// A session holding instructions without its history/input must be rebuilt.
|
|
849
|
+
if (
|
|
850
|
+
preludes.length > 0 &&
|
|
851
|
+
options.signal?.aborted &&
|
|
852
|
+
this.owns(proc, conv, conv.sessionId)
|
|
853
|
+
)
|
|
854
|
+
conv.dirty = true
|
|
834
855
|
if (options.signal?.aborted) {
|
|
835
856
|
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
836
857
|
yield {
|
|
@@ -1443,6 +1464,13 @@ export class AntigravityRuntime {
|
|
|
1443
1464
|
const configOptions = await proc
|
|
1444
1465
|
.setConfigOption(sessionId, optionId, value)
|
|
1445
1466
|
.catch((error: unknown) => {
|
|
1467
|
+
if (
|
|
1468
|
+
error instanceof JsonRpcRemoteError &&
|
|
1469
|
+
error.rpc.code === -32002 &&
|
|
1470
|
+
this.owns(proc, conv, sessionId)
|
|
1471
|
+
) {
|
|
1472
|
+
conv.dirty = true
|
|
1473
|
+
}
|
|
1446
1474
|
throw mapError(error)
|
|
1447
1475
|
})
|
|
1448
1476
|
this.assertOwner(proc, conv, sessionId)
|
|
@@ -1646,6 +1674,50 @@ export class AntigravityRuntime {
|
|
|
1646
1674
|
conv.reportedToolCalls.clear()
|
|
1647
1675
|
}
|
|
1648
1676
|
|
|
1677
|
+
/**
|
|
1678
|
+
* Deliver one prelude of session instructions before the live turn. No turn
|
|
1679
|
+
* is bound on `conv` while it runs, so streamed chunks are dropped and tool
|
|
1680
|
+
* calls/permissions are refused, while hidden SQLite API errors still surface.
|
|
1681
|
+
*/
|
|
1682
|
+
private async runPrelude(
|
|
1683
|
+
conv: Conversation,
|
|
1684
|
+
proc: AcpProcess,
|
|
1685
|
+
blocks: AcpPromptBlock[],
|
|
1686
|
+
signal: AbortSignal | undefined,
|
|
1687
|
+
): Promise<void> {
|
|
1688
|
+
this.assertOwner(proc, conv, conv.sessionId)
|
|
1689
|
+
const sessionId = conv.sessionId as string
|
|
1690
|
+
if (signal?.aborted) {
|
|
1691
|
+
conv.dirty = true
|
|
1692
|
+
return
|
|
1693
|
+
}
|
|
1694
|
+
const cancel = () => proc.cancel(sessionId)
|
|
1695
|
+
signal?.addEventListener('abort', cancel, { once: true })
|
|
1696
|
+
try {
|
|
1697
|
+
const result = await this.runPromptSegment(
|
|
1698
|
+
conv,
|
|
1699
|
+
new AcpTurn(`prelude-${randomBytes(4).toString('hex')}`),
|
|
1700
|
+
proc,
|
|
1701
|
+
sessionId,
|
|
1702
|
+
conv.model,
|
|
1703
|
+
conv.metaFloorIdx,
|
|
1704
|
+
blocks,
|
|
1705
|
+
)
|
|
1706
|
+
if (
|
|
1707
|
+
(result.stopReason === 'cancelled' || signal?.aborted) &&
|
|
1708
|
+
this.owns(proc, conv, sessionId)
|
|
1709
|
+
) {
|
|
1710
|
+
conv.dirty = true
|
|
1711
|
+
}
|
|
1712
|
+
} catch (error) {
|
|
1713
|
+
if (this.owns(proc, conv, sessionId)) conv.dirty = true
|
|
1714
|
+
throw mapError(error)
|
|
1715
|
+
} finally {
|
|
1716
|
+
signal?.removeEventListener('abort', cancel)
|
|
1717
|
+
}
|
|
1718
|
+
this.assertOwner(proc, conv, sessionId)
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1649
1721
|
private startPrompt(
|
|
1650
1722
|
conv: Conversation,
|
|
1651
1723
|
turn: AcpTurn,
|