@oh-my-pi/pi-agent-core 15.2.3 → 15.3.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [15.3.0] - 2026-05-25
6
+ ### Fixed
7
+
8
+ - Fixed `transformContext` receiving the loop config object as the `signal` argument instead of the actual `AbortSignal`, so hooks that check `signal.aborted` or call `signal.addEventListener` now work correctly under abort/timeout conditions
9
+ - Fixed `appendOnlyContext` not being re-evaluated after `setModel()` — the mode was decided once at session construction based on the initial model's provider, so switching from/to DeepSeek (or changing `provider.appendOnlyContext`) mid-session produced incorrect mode behavior
10
+
5
11
  ## [15.2.3] - 2026-05-22
6
12
  ### Added
7
13
 
@@ -2,6 +2,7 @@
2
2
  * No transport abstraction - calls streamSimple via the loop.
3
3
  */
4
4
  import { type AssistantMessage, type AssistantMessageEvent, type CursorExecHandlers, type CursorToolResultHandler, type Effort, type ImageContent, type Message, type Model, type ProviderSessionState, type ServiceTier, type SimpleStreamOptions, type ThinkingBudgets, type ToolChoice } from "@oh-my-pi/pi-ai";
5
+ import type { AppendOnlyContextManager } from "./append-only-context";
5
6
  import type { HarmonyAuditEvent } from "./harmony-leak";
6
7
  import type { AgentEvent, AgentLoopConfig, AgentMessage, AgentState, AgentTool, AgentToolContext, StreamFn, ToolCallContext } from "./types";
7
8
  export declare class AgentBusyError extends Error {
@@ -144,6 +145,11 @@ export interface AgentOptions {
144
145
  * {@link AgentLoopConfig.telemetry} for the full surface.
145
146
  */
146
147
  telemetry?: AgentLoopConfig["telemetry"];
148
+ /**
149
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
150
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
151
+ */
152
+ appendOnlyContext?: AppendOnlyContextManager;
147
153
  }
148
154
  export interface AgentPromptOptions {
149
155
  toolChoice?: ToolChoice;
@@ -261,6 +267,8 @@ export declare class Agent {
261
267
  */
262
268
  set maxRetryDelayMs(value: number | undefined);
263
269
  get state(): AgentState;
270
+ get appendOnlyContext(): AppendOnlyContextManager | undefined;
271
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void;
264
272
  subscribe(fn: (e: AgentEvent) => void): () => void;
265
273
  setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void;
266
274
  setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void;
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Append-only context mode — stabilizes the byte prefix sent to the LLM
3
+ * across turns so provider prefix caches (DeepSeek, Anthropic, etc.)
4
+ * hit at the maximum possible rate.
5
+ *
6
+ * Two mechanisms:
7
+ *
8
+ * 1. **StablePrefix** — system prompt + tool specs are computed once
9
+ * and frozen. Subsequent turns reuse the exact same byte sequence
10
+ * unless `invalidate()` is called (e.g. after MCP reconnect).
11
+ *
12
+ * 2. **AppendOnlyLog** — messages only grow; prior turns are never
13
+ * re-serialized. Combined with a stable prefix, only the user's new
14
+ * message delta is a cache miss each turn.
15
+ */
16
+ import type { Context, Message, Tool } from "@oh-my-pi/pi-ai";
17
+ import type { AgentContext } from "./types";
18
+ /** Frozen system prompt + tool spec snapshot. */
19
+ export interface StablePrefixSnapshot {
20
+ systemPrompt: string[];
21
+ tools: Tool[];
22
+ fingerprint: string;
23
+ }
24
+ /**
25
+ * A frozen prefix (system prompt + tools) that produces stable byte
26
+ * sequences across `build()` calls.
27
+ *
28
+ * The first `build()` snapshots the live state. Subsequent calls reuse
29
+ * the cached copy until `invalidate()` is called or the live state's
30
+ * fingerprint changes.
31
+ */
32
+ export declare class StablePrefix {
33
+ #private;
34
+ get fingerprint(): string;
35
+ get version(): number;
36
+ get built(): boolean;
37
+ /**
38
+ * Build or rebuild from live context.
39
+ * Returns `true` if the prefix actually changed (cache miss imminent).
40
+ */
41
+ build(context: AgentContext): boolean;
42
+ /** Force rebuild on the next `build()` call. */
43
+ invalidate(): void;
44
+ /**
45
+ * Returns the cached prefix.
46
+ * @throws if `build()` was never called.
47
+ */
48
+ toContext(): {
49
+ systemPrompt: string[];
50
+ tools: Tool[];
51
+ };
52
+ }
53
+ /**
54
+ * Append-only message log at the `Message[]` (provider-level) layer.
55
+ *
56
+ * The only mutation path is `replaceTail()`, reserved for compaction.
57
+ * Every other operation is append-only.
58
+ */
59
+ export declare class AppendOnlyLog {
60
+ #private;
61
+ get length(): number;
62
+ append(message: any): void;
63
+ extend(messages: any[]): void;
64
+ /** Replace the last entry — only legal for compaction. */
65
+ replaceTail(replacement: any): void;
66
+ /** Returns a shallow copy of all entries. */
67
+ toMessages(): Message[];
68
+ /** Direct readonly access for in-place inspection. */
69
+ entries(): readonly Message[];
70
+ clear(): void;
71
+ }
72
+ /**
73
+ * Manages a stable prefix + append-only log for the agent loop.
74
+ *
75
+ * Call `build(context)` each turn to get a `Context` with stable
76
+ * `systemPrompt` and `tools` and append-only messages. Call
77
+ * `syncMessages(normalizedMessages)` after `convertToLlm` each
78
+ * turn to keep the log in sync.
79
+ *
80
+ * Example:
81
+ * ```
82
+ * const mgr = new AppendOnlyContextManager();
83
+ * const ctx = mgr.build(context); // first call snapshots prefix
84
+ * mgr.syncMessages(normalized); // grow the log
85
+ * ctx = mgr.build(context); // subsequent calls use cache
86
+ * ```
87
+ */
88
+ export declare class AppendOnlyContextManager {
89
+ #private;
90
+ readonly prefix: StablePrefix;
91
+ readonly log: AppendOnlyLog;
92
+ build(context: AgentContext): Context;
93
+ /**
94
+ * Sync normalized (provider-level) messages into the append-only log.
95
+ *
96
+ * Detects both compaction (shorter array) and in-place rewrites
97
+ * (same length, changed content via a rolling digest).
98
+ */
99
+ syncMessages(normalizedMessages: any[]): void;
100
+ /** Reset prefix + log for a model/provider switch while mode stays active. */
101
+ invalidateForModelChange(): void;
102
+ /** Reset the sync cursor AND clear the log. */
103
+ resetSyncCursor(): void;
104
+ appendMessage(message: any): void;
105
+ replaceTailMessage(message: any): void;
106
+ invalidate(): void;
107
+ reset(context: AgentContext): void;
108
+ }
@@ -1,5 +1,6 @@
1
1
  export * from "./agent";
2
2
  export * from "./agent-loop";
3
+ export * from "./append-only-context";
3
4
  export * from "./compaction";
4
5
  export * from "./harmony-leak";
5
6
  export * from "./proxy";
@@ -1,4 +1,5 @@
1
1
  import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Effort, ImageContent, Message, Model, SimpleStreamOptions, Static, streamSimple, TextContent, Tool, ToolChoice, ToolResultMessage, TSchema } from "@oh-my-pi/pi-ai";
2
+ import type { AppendOnlyContextManager } from "./append-only-context";
2
3
  import type { HarmonyAuditEvent } from "./harmony-leak";
3
4
  import type { AgentRunCoverage, AgentRunSummary } from "./run-collector";
4
5
  import type { AgentTelemetryConfig } from "./telemetry";
@@ -122,6 +123,15 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
122
123
  * then strips from arguments before executing tools.
123
124
  */
124
125
  intentTracing?: boolean;
126
+ /**
127
+ * Append-only context mode — stabilizes system prompt + tool spec bytes
128
+ * across turns so provider prefix caches hit at maximum rate.
129
+ *
130
+ * When set, the loop reads messages from the append-only log (stable
131
+ * byte prefix) and caches system prompt + tools. Tools exclude per-turn
132
+ * `_i` intent fields.
133
+ */
134
+ appendOnlyContext?: AppendOnlyContextManager;
125
135
  /**
126
136
  * Inspect assistant streaming events before they are published to the outer agent event stream.
127
137
  * Callers may abort synchronously to stop consuming buffered provider events.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@oh-my-pi/pi-agent-core",
4
- "version": "15.2.3",
4
+ "version": "15.3.0",
5
5
  "description": "General-purpose agent with transport abstraction, state management, and attachment support",
6
6
  "homepage": "https://omp.sh",
7
7
  "author": "Can Boluk",
@@ -35,9 +35,9 @@
35
35
  "fmt": "biome format --write ."
36
36
  },
37
37
  "dependencies": {
38
- "@oh-my-pi/pi-ai": "15.2.3",
39
- "@oh-my-pi/pi-natives": "15.2.3",
40
- "@oh-my-pi/pi-utils": "15.2.3",
38
+ "@oh-my-pi/pi-ai": "15.3.0",
39
+ "@oh-my-pi/pi-natives": "15.3.0",
40
+ "@oh-my-pi/pi-utils": "15.3.0",
41
41
  "@opentelemetry/api": "^1.9.0"
42
42
  },
43
43
  "devDependencies": {
package/src/agent-loop.ts CHANGED
@@ -647,12 +647,19 @@ async function streamAssistantResponse(
647
647
  const llmMessages = await config.convertToLlm(messages);
648
648
  const normalizedMessages = normalizeMessagesForProvider(llmMessages, config.model);
649
649
 
650
- // Build LLM context
651
- const llmContext: Context = {
652
- systemPrompt: context.systemPrompt,
653
- messages: normalizedMessages,
654
- tools: normalizeTools(context.tools, !!config.intentTracing),
655
- };
650
+ // Build LLM context — append-only mode caches system prompt + tools
651
+ // AND keeps an append-only message log so prior-turn bytes are stable.
652
+ let llmContext: Context;
653
+ if (config.appendOnlyContext) {
654
+ config.appendOnlyContext.syncMessages(normalizedMessages);
655
+ llmContext = config.appendOnlyContext.build(context);
656
+ } else {
657
+ llmContext = {
658
+ systemPrompt: context.systemPrompt,
659
+ messages: normalizedMessages,
660
+ tools: normalizeTools(context.tools, !!config.intentTracing),
661
+ };
662
+ }
656
663
 
657
664
  const streamFunction = streamFn || streamSimple;
658
665
 
package/src/agent.ts CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  type ToolResultMessage,
22
22
  } from "@oh-my-pi/pi-ai";
23
23
  import { agentLoop, agentLoopContinue } from "./agent-loop";
24
+ import type { AppendOnlyContextManager } from "./append-only-context";
24
25
  import type { HarmonyAuditEvent } from "./harmony-leak";
25
26
  import type {
26
27
  AgentContext,
@@ -227,6 +228,11 @@ export interface AgentOptions {
227
228
  * {@link AgentLoopConfig.telemetry} for the full surface.
228
229
  */
229
230
  telemetry?: AgentLoopConfig["telemetry"];
231
+ /**
232
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
233
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
234
+ */
235
+ appendOnlyContext?: AppendOnlyContextManager;
230
236
  }
231
237
 
232
238
  export interface AgentPromptOptions {
@@ -292,6 +298,7 @@ export class Agent {
292
298
  #onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
293
299
  #onBeforeYield?: () => Promise<void> | void;
294
300
  #telemetry?: AgentLoopConfig["telemetry"];
301
+ #appendOnlyContext?: AppendOnlyContextManager;
295
302
 
296
303
  /** Buffered Cursor tool results with text length at time of call (for correct ordering) */
297
304
  #cursorToolResultBuffer: CursorToolResultEntry[] = [];
@@ -346,6 +353,7 @@ export class Agent {
346
353
  this.beforeToolCall = opts.beforeToolCall;
347
354
  this.afterToolCall = opts.afterToolCall;
348
355
  this.#telemetry = opts.telemetry;
356
+ this.#appendOnlyContext = opts.appendOnlyContext;
349
357
  }
350
358
 
351
359
  /**
@@ -541,6 +549,14 @@ export class Agent {
541
549
  return this.#state;
542
550
  }
543
551
 
552
+ get appendOnlyContext(): AppendOnlyContextManager | undefined {
553
+ return this.#appendOnlyContext;
554
+ }
555
+
556
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void {
557
+ this.#appendOnlyContext = manager;
558
+ }
559
+
544
560
  subscribe(fn: (e: AgentEvent) => void): () => void {
545
561
  this.#listeners.add(fn);
546
562
  return () => this.#listeners.delete(fn);
@@ -925,6 +941,7 @@ export class Agent {
925
941
  cursorOnToolResult,
926
942
  transformToolCallArguments: this.#transformToolCallArguments,
927
943
  intentTracing: this.#intentTracing,
944
+ appendOnlyContext: this.#appendOnlyContext,
928
945
  beforeToolCall: this.beforeToolCall ? (ctx, signal) => this.beforeToolCall?.(ctx, signal) : undefined,
929
946
  afterToolCall: this.afterToolCall ? (ctx, signal) => this.afterToolCall?.(ctx, signal) : undefined,
930
947
  onAssistantMessageEvent: this.#onAssistantMessageEvent,
@@ -0,0 +1,291 @@
1
+ /**
2
+ * Append-only context mode — stabilizes the byte prefix sent to the LLM
3
+ * across turns so provider prefix caches (DeepSeek, Anthropic, etc.)
4
+ * hit at the maximum possible rate.
5
+ *
6
+ * Two mechanisms:
7
+ *
8
+ * 1. **StablePrefix** — system prompt + tool specs are computed once
9
+ * and frozen. Subsequent turns reuse the exact same byte sequence
10
+ * unless `invalidate()` is called (e.g. after MCP reconnect).
11
+ *
12
+ * 2. **AppendOnlyLog** — messages only grow; prior turns are never
13
+ * re-serialized. Combined with a stable prefix, only the user's new
14
+ * message delta is a cache miss each turn.
15
+ */
16
+
17
+ import type { Context, Message, Tool } from "@oh-my-pi/pi-ai";
18
+ import type { AgentContext, AgentTool } from "./types";
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // StablePrefix (formerly ImmutablePrefix)
22
+ // ---------------------------------------------------------------------------
23
+
24
+ /** Frozen system prompt + tool spec snapshot. */
25
+ export interface StablePrefixSnapshot {
26
+ systemPrompt: string[];
27
+ tools: Tool[];
28
+ fingerprint: string;
29
+ }
30
+
31
+ /**
32
+ * A frozen prefix (system prompt + tools) that produces stable byte
33
+ * sequences across `build()` calls.
34
+ *
35
+ * The first `build()` snapshots the live state. Subsequent calls reuse
36
+ * the cached copy until `invalidate()` is called or the live state's
37
+ * fingerprint changes.
38
+ */
39
+ export class StablePrefix {
40
+ #snapshot: StablePrefixSnapshot | null = null;
41
+ #version = 0;
42
+
43
+ get fingerprint(): string {
44
+ return this.#snapshot?.fingerprint ?? "<unbuilt>";
45
+ }
46
+ get version(): number {
47
+ return this.#version;
48
+ }
49
+ get built(): boolean {
50
+ return this.#snapshot !== null;
51
+ }
52
+
53
+ /**
54
+ * Build or rebuild from live context.
55
+ * Returns `true` if the prefix actually changed (cache miss imminent).
56
+ */
57
+ build(context: AgentContext): boolean {
58
+ const snapshot = takeSnapshot(context);
59
+ if (this.#snapshot && this.#snapshot.fingerprint === snapshot.fingerprint) {
60
+ return false;
61
+ }
62
+ this.#snapshot = snapshot;
63
+ this.#version++;
64
+ return true;
65
+ }
66
+
67
+ /** Force rebuild on the next `build()` call. */
68
+ invalidate(): void {
69
+ this.#snapshot = null;
70
+ }
71
+
72
+ /**
73
+ * Returns the cached prefix.
74
+ * @throws if `build()` was never called.
75
+ */
76
+ toContext(): { systemPrompt: string[]; tools: Tool[] } {
77
+ const s = this.#snapshot;
78
+ if (!s) throw new Error("StablePrefix.toContext() called before build()");
79
+ return { systemPrompt: s.systemPrompt, tools: s.tools };
80
+ }
81
+ }
82
+
83
+ // ---------------------------------------------------------------------------
84
+ // AppendOnlyLog
85
+ // ---------------------------------------------------------------------------
86
+
87
+ /**
88
+ * Append-only message log at the `Message[]` (provider-level) layer.
89
+ *
90
+ * The only mutation path is `replaceTail()`, reserved for compaction.
91
+ * Every other operation is append-only.
92
+ */
93
+ export class AppendOnlyLog {
94
+ #entries: Message[] = [];
95
+
96
+ get length(): number {
97
+ return this.#entries.length;
98
+ }
99
+
100
+ append(message: any): void {
101
+ this.#entries.push(message);
102
+ }
103
+
104
+ extend(messages: any[]): void {
105
+ for (const m of messages) this.#entries.push(m);
106
+ }
107
+
108
+ /** Replace the last entry — only legal for compaction. */
109
+ replaceTail(replacement: any): void {
110
+ const idx = this.#entries.length - 1;
111
+ if (idx >= 0) this.#entries[idx] = replacement;
112
+ }
113
+
114
+ /** Returns a shallow copy of all entries. */
115
+ toMessages(): Message[] {
116
+ return this.#entries.slice();
117
+ }
118
+
119
+ /** Direct readonly access for in-place inspection. */
120
+ entries(): readonly Message[] {
121
+ return this.#entries;
122
+ }
123
+
124
+ clear(): void {
125
+ this.#entries = [];
126
+ }
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // AppendOnlyContextManager
131
+ // ---------------------------------------------------------------------------
132
+
133
+ /**
134
+ * Manages a stable prefix + append-only log for the agent loop.
135
+ *
136
+ * Call `build(context)` each turn to get a `Context` with stable
137
+ * `systemPrompt` and `tools` and append-only messages. Call
138
+ * `syncMessages(normalizedMessages)` after `convertToLlm` each
139
+ * turn to keep the log in sync.
140
+ *
141
+ * Example:
142
+ * ```
143
+ * const mgr = new AppendOnlyContextManager();
144
+ * const ctx = mgr.build(context); // first call snapshots prefix
145
+ * mgr.syncMessages(normalized); // grow the log
146
+ * ctx = mgr.build(context); // subsequent calls use cache
147
+ * ```
148
+ */
149
+ export class AppendOnlyContextManager {
150
+ readonly prefix = new StablePrefix();
151
+ readonly log = new AppendOnlyLog();
152
+ /** How many normalized messages were synced into the log as of the last sync. */
153
+ #lastSyncCount = 0;
154
+ /** Rolling digest of synced message content — detects in-place rewrites. */
155
+ #syncedDigest = 0;
156
+
157
+ build(context: AgentContext): Context {
158
+ this.prefix.build(context);
159
+ const { systemPrompt, tools } = this.prefix.toContext();
160
+ return { systemPrompt, messages: this.log.toMessages(), tools };
161
+ }
162
+
163
+ /**
164
+ * Sync normalized (provider-level) messages into the append-only log.
165
+ *
166
+ * Detects both compaction (shorter array) and in-place rewrites
167
+ * (same length, changed content via a rolling digest).
168
+ */
169
+ syncMessages(normalizedMessages: any[]): void {
170
+ // Detect in-place rewrites of already-synced messages.
171
+ if (
172
+ this.#lastSyncCount > 0 &&
173
+ this.#lastSyncCount <= normalizedMessages.length &&
174
+ this.#computeDigest(normalizedMessages.slice(0, this.#lastSyncCount)) !== this.#syncedDigest
175
+ ) {
176
+ this.log.clear();
177
+ this.#lastSyncCount = 0;
178
+ }
179
+
180
+ // Compaction — array shrunk.
181
+ if (normalizedMessages.length < this.#lastSyncCount) {
182
+ this.log.clear();
183
+ this.#lastSyncCount = 0;
184
+ }
185
+
186
+ const newMsgs = normalizedMessages.slice(this.#lastSyncCount);
187
+ for (const msg of newMsgs) {
188
+ this.log.append(msg);
189
+ }
190
+
191
+ this.#lastSyncCount = normalizedMessages.length;
192
+ this.#syncedDigest = this.#computeDigest(normalizedMessages);
193
+ }
194
+
195
+ /** Reset prefix + log for a model/provider switch while mode stays active. */
196
+ invalidateForModelChange(): void {
197
+ this.prefix.invalidate();
198
+ this.log.clear();
199
+ this.#lastSyncCount = 0;
200
+ this.#syncedDigest = 0;
201
+ }
202
+
203
+ /** Reset the sync cursor AND clear the log. */
204
+ resetSyncCursor(): void {
205
+ this.log.clear();
206
+ this.#lastSyncCount = 0;
207
+ this.#syncedDigest = 0;
208
+ }
209
+
210
+ appendMessage(message: any): void {
211
+ this.log.append(message);
212
+ }
213
+
214
+ replaceTailMessage(message: any): void {
215
+ this.log.replaceTail(message);
216
+ }
217
+
218
+ invalidate(): void {
219
+ this.prefix.invalidate();
220
+ }
221
+
222
+ reset(context: AgentContext): void {
223
+ this.prefix.invalidate();
224
+ this.log.clear();
225
+ this.#lastSyncCount = 0;
226
+ this.#syncedDigest = 0;
227
+ this.prefix.build(context);
228
+ }
229
+
230
+ /** Fast rolling digest of message content. */
231
+ #computeDigest(messages: any[]): number {
232
+ let hash = 0;
233
+ for (let i = 0; i < messages.length; i++) {
234
+ const msg = messages[i];
235
+ if (msg && typeof msg === "object") {
236
+ const payload =
237
+ String(msg.role) + (typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content ?? ""));
238
+ for (let j = 0; j < payload.length; j++) {
239
+ hash = ((hash << 5) - hash + payload.charCodeAt(j)) | 0;
240
+ }
241
+ }
242
+ }
243
+ return hash >>> 0;
244
+ }
245
+ }
246
+
247
+ // ---------------------------------------------------------------------------
248
+ // Snapshot helpers
249
+ // ---------------------------------------------------------------------------
250
+
251
+ /**
252
+ * Produce a stable serialization of tools that matches what
253
+ * `normalizeTools(tools, false)` outputs (no intent injection).
254
+ *
255
+ * The spread `{ ...agentTool }` preserves all own enumerable properties
256
+ * that survive JSON.stringify — functions are dropped, but strings,
257
+ * booleans, objects are included.
258
+ */
259
+ function normalizeTool(t: AgentTool): Tool {
260
+ const description = t.description ?? "";
261
+ return { ...t, parameters: t.parameters, description };
262
+ }
263
+
264
+ function takeSnapshot(context: AgentContext): StablePrefixSnapshot {
265
+ const systemPrompt = [...context.systemPrompt];
266
+ const tools = (context.tools ?? []).map(normalizeTool);
267
+ return {
268
+ systemPrompt,
269
+ tools,
270
+ fingerprint: computeFingerprint(systemPrompt, tools),
271
+ };
272
+ }
273
+
274
+ function computeFingerprint(systemPrompt: string[], tools: Tool[]): string {
275
+ const payload = JSON.stringify({
276
+ s: systemPrompt,
277
+ t: tools.map(t => ({
278
+ n: t.name,
279
+ d: t.description,
280
+ p: t.parameters,
281
+ s: t.strict,
282
+ cf: t.customFormat,
283
+ cw: t.customWireName,
284
+ })),
285
+ });
286
+ let hash = 0;
287
+ for (let i = 0; i < payload.length; i++) {
288
+ hash = ((hash << 5) - hash + payload.charCodeAt(i)) | 0;
289
+ }
290
+ return (hash >>> 0).toString(36);
291
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,8 @@
2
2
  export * from "./agent";
3
3
  // Loop functions
4
4
  export * from "./agent-loop";
5
+ // Append-only context mode
6
+ export * from "./append-only-context";
5
7
  // Compaction
6
8
  export * from "./compaction";
7
9
  export * from "./harmony-leak";
package/src/types.ts CHANGED
@@ -15,6 +15,7 @@ import type {
15
15
  ToolResultMessage,
16
16
  TSchema,
17
17
  } from "@oh-my-pi/pi-ai";
18
+ import type { AppendOnlyContextManager } from "./append-only-context";
18
19
  import type { HarmonyAuditEvent } from "./harmony-leak";
19
20
  import type { AgentRunCoverage, AgentRunSummary } from "./run-collector";
20
21
  import type { AgentTelemetryConfig } from "./telemetry";
@@ -154,6 +155,15 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
154
155
  * then strips from arguments before executing tools.
155
156
  */
156
157
  intentTracing?: boolean;
158
+ /**
159
+ * Append-only context mode — stabilizes system prompt + tool spec bytes
160
+ * across turns so provider prefix caches hit at maximum rate.
161
+ *
162
+ * When set, the loop reads messages from the append-only log (stable
163
+ * byte prefix) and caches system prompt + tools. Tools exclude per-turn
164
+ * `_i` intent fields.
165
+ */
166
+ appendOnlyContext?: AppendOnlyContextManager;
157
167
 
158
168
  /**
159
169
  * Inspect assistant streaming events before they are published to the outer agent event stream.