pi-agy-acp 0.2.6 → 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 CHANGED
@@ -1,5 +1,12 @@
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
+
3
10
  ## [0.2.6](https://github.com/fdkevin0/pi-agy-acp/compare/v0.2.5...v0.2.6) (2026-12-03)
4
11
 
5
12
 
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-agy-acp",
3
- "version": "0.2.6",
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.5",
43
- "@earendil-works/pi-ai": "1.5.2",
44
- "@earendil-works/pi-coding-agent": "1.5.2"
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/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 first user message contains <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.',
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/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'
@@ -819,11 +819,14 @@ export class AntigravityRuntime {
819
819
  effort = await this.select(conv, proc, options.model, effort)
820
820
  this.assertOwner(proc, conv, conv.sessionId)
821
821
  let blocks: AcpPromptBlock[]
822
+ let preludes: AcpPromptBlock[][] = []
822
823
  if (plan.rebuild) {
823
- blocks = replayPrompt(plan.history, plan.input, {
824
+ const prompts = openingPrompts(plan.history, plan.input, {
824
825
  system: split.system,
825
826
  supplementalTools,
826
827
  })
828
+ blocks = prompts[prompts.length - 1] as AcpPromptBlock[]
829
+ preludes = prompts.slice(0, -1)
827
830
  } else {
828
831
  blocks = inputPrompt(plan.input)
829
832
  if (supplementalTools.length > 0) {
@@ -838,6 +841,17 @@ export class AntigravityRuntime {
838
841
  if (toolDelta.length > 0)
839
842
  blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
840
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
841
855
  if (options.signal?.aborted) {
842
856
  this.dropTurn(conv, 'The harness cancelled this request.')
843
857
  yield {
@@ -1660,6 +1674,50 @@ export class AntigravityRuntime {
1660
1674
  conv.reportedToolCalls.clear()
1661
1675
  }
1662
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
+
1663
1721
  private startPrompt(
1664
1722
  conv: Conversation,
1665
1723
  turn: AcpTurn,