@averyyy/pi-coding-agent 0.82.1-piclient.3 → 0.82.1-piclient.5

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 (58) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cli/args.d.ts.map +1 -1
  3. package/dist/cli/args.js +2 -1
  4. package/dist/cli/args.js.map +1 -1
  5. package/dist/cli/tool-effects.d.ts +6 -0
  6. package/dist/cli/tool-effects.d.ts.map +1 -0
  7. package/dist/cli/tool-effects.js +164 -0
  8. package/dist/cli/tool-effects.js.map +1 -0
  9. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  10. package/dist/core/agent-session-runtime.js +5 -0
  11. package/dist/core/agent-session-runtime.js.map +1 -1
  12. package/dist/core/agent-session.d.ts +12 -1
  13. package/dist/core/agent-session.d.ts.map +1 -1
  14. package/dist/core/agent-session.js +314 -89
  15. package/dist/core/agent-session.js.map +1 -1
  16. package/dist/core/pi-server-client.d.ts +53 -2
  17. package/dist/core/pi-server-client.d.ts.map +1 -1
  18. package/dist/core/pi-server-client.js +2082 -290
  19. package/dist/core/pi-server-client.js.map +1 -1
  20. package/dist/core/pi-server-compact-state.d.ts +81 -0
  21. package/dist/core/pi-server-compact-state.d.ts.map +1 -0
  22. package/dist/core/pi-server-compact-state.js +438 -0
  23. package/dist/core/pi-server-compact-state.js.map +1 -0
  24. package/dist/core/pi-server-protocol.d.ts +2 -0
  25. package/dist/core/pi-server-protocol.d.ts.map +1 -1
  26. package/dist/core/pi-server-protocol.js +24 -1
  27. package/dist/core/pi-server-protocol.js.map +1 -1
  28. package/dist/core/pi-server-request.d.ts +28 -0
  29. package/dist/core/pi-server-request.d.ts.map +1 -1
  30. package/dist/core/pi-server-request.js +492 -75
  31. package/dist/core/pi-server-request.js.map +1 -1
  32. package/dist/core/pi-server-run-state.d.ts +41 -0
  33. package/dist/core/pi-server-run-state.d.ts.map +1 -0
  34. package/dist/core/pi-server-run-state.js +318 -0
  35. package/dist/core/pi-server-run-state.js.map +1 -0
  36. package/dist/core/sdk.d.ts.map +1 -1
  37. package/dist/core/sdk.js +69 -53
  38. package/dist/core/sdk.js.map +1 -1
  39. package/dist/core/session-manager.d.ts +86 -2
  40. package/dist/core/session-manager.d.ts.map +1 -1
  41. package/dist/core/session-manager.js +737 -144
  42. package/dist/core/session-manager.js.map +1 -1
  43. package/dist/core/tool-effect-journal.d.ts +103 -0
  44. package/dist/core/tool-effect-journal.d.ts.map +1 -0
  45. package/dist/core/tool-effect-journal.js +1442 -0
  46. package/dist/core/tool-effect-journal.js.map +1 -0
  47. package/dist/core/tools/output-accumulator.d.ts +3 -1
  48. package/dist/core/tools/output-accumulator.d.ts.map +1 -1
  49. package/dist/core/tools/output-accumulator.js +46 -26
  50. package/dist/core/tools/output-accumulator.js.map +1 -1
  51. package/dist/main.d.ts.map +1 -1
  52. package/dist/main.js +13 -0
  53. package/dist/main.js.map +1 -1
  54. package/dist/modes/interactive/components/session-selector.d.ts +15 -0
  55. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  56. package/dist/modes/interactive/components/session-selector.js +98 -29
  57. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  58. package/package.json +4 -4
@@ -1,8 +1,12 @@
1
- import { randomUUID } from "node:crypto";
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { resolve } from "node:path";
2
3
  import { setTimeout as sleep } from "node:timers/promises";
4
+ import { buildSessionContext, convertToLlm, } from "@earendil-works/pi-agent-core";
3
5
  import { EventStream, parseStreamingJson, } from "@earendil-works/pi-ai";
4
- import { buildPiServerTreePrefixHashes, hashPiServerSessionEntries, hashPiServerStaticContext, } from "./pi-server-protocol.js";
5
- import { ChunkRequest } from "./pi-server-request.js";
6
+ import { acknowledgePiServerPendingCompact, readPiServerPendingCompact, writePiServerAppliedCompact, writePiServerPendingCompact, writePiServerTerminalCompact, } from "./pi-server-compact-state.js";
7
+ import { appendPiServerTreeHash, canonicalJsonStringify, hashPiServerSessionEntries, hashPiServerStaticContext, hashPiServerTreeEntry, PI_SERVER_EMPTY_TREE_HASH, PI_SERVER_PROTOCOL_VERSION, } from "./pi-server-protocol.js";
8
+ import { ChunkRequest, PiServerTransportBodyLimitError, readPiServerResponseText } from "./pi-server-request.js";
9
+ import { acknowledgePiServerPendingRun, acquirePiServerRunStateLease, hashPiServerIdentity, readPiServerPendingRun, releasePiServerRunStateLease, writePiServerPendingRun, } from "./pi-server-run-state.js";
6
10
  class PiServerEventStream extends EventStream {
7
11
  constructor() {
8
12
  super((event) => event.type === "done" || event.type === "error", (event) => {
@@ -28,14 +32,25 @@ function createPiServerRequest(signal) {
28
32
  });
29
33
  }
30
34
  const sessionStaticContextHashes = new Map();
31
- const sessionSyncedEntryIds = new Map();
32
35
  const sessionTreeHashes = new Map();
33
36
  const sessionTreeEntryCounts = new Map();
34
37
  const sessionTreeLeafIds = new Map();
38
+ const sessionTreeRevisions = new Map();
35
39
  const sessionHasTemporaryTree = new Set();
40
+ const sessionLocalTreeHashes = new Map();
41
+ const sessionPendingRuns = new Map();
42
+ const activePersistentRunLeases = new Map();
36
43
  const RESPONSE_BODY_EXCERPT_CHARS = 500;
44
+ const PI_SERVER_CANCEL_TIMEOUT_MS = 5000;
45
+ const PI_SERVER_COMPACT_DEFAULT_RECOVERY_WINDOW_MS = 6 * 60 * 60_000;
46
+ const PI_SERVER_COMPACT_MAX_RECONNECT_DELAY_MS = 30_000;
47
+ const PI_SERVER_STREAM_DEFAULT_RECOVERY_WINDOW_MS = 6 * 60 * 60_000;
48
+ const PI_SERVER_STREAM_MAX_RECONNECT_DELAY_MS = 30_000;
49
+ const PI_SERVER_RESPONSE_NO_PROGRESS_TIMEOUT_MS = 90_000;
50
+ const PI_SERVER_ERROR_BODY_MAX_BYTES = 64 * 1024;
51
+ const PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES = 256 * 1024 * 1024;
37
52
  const TRANSIENT_PI_SERVER_RETRY_DELAYS_MS = [250, 750, 1500];
38
- const TRANSIENT_PI_SERVER_STATUS_CODES = new Set([502, 503, 504, 530]);
53
+ const TRANSIENT_PI_SERVER_STATUS_CODES = new Set([408, 425, 429, 502, 503, 504, 530]);
39
54
  export function hashStaticContext(ctx) {
40
55
  return hashPiServerStaticContext(ctx);
41
56
  }
@@ -60,23 +75,219 @@ function getLinearTreeFromMessages(messages) {
60
75
  }
61
76
  export function resetSessionTracking(sessionId) {
62
77
  sessionStaticContextHashes.delete(sessionId);
63
- sessionSyncedEntryIds.delete(sessionId);
64
78
  sessionTreeHashes.delete(sessionId);
65
79
  sessionTreeEntryCounts.delete(sessionId);
66
80
  sessionTreeLeafIds.delete(sessionId);
81
+ sessionTreeRevisions.delete(sessionId);
67
82
  sessionHasTemporaryTree.delete(sessionId);
83
+ sessionLocalTreeHashes.delete(sessionId);
68
84
  }
69
85
  export function resetAllSessionTracking() {
86
+ for (const { lease } of activePersistentRunLeases.values()) {
87
+ releasePiServerRunStateLease(lease);
88
+ }
89
+ activePersistentRunLeases.clear();
70
90
  sessionStaticContextHashes.clear();
71
- sessionSyncedEntryIds.clear();
72
91
  sessionTreeHashes.clear();
73
92
  sessionTreeEntryCounts.clear();
74
93
  sessionTreeLeafIds.clear();
94
+ sessionTreeRevisions.clear();
75
95
  sessionHasTemporaryTree.clear();
96
+ sessionLocalTreeHashes.clear();
97
+ sessionPendingRuns.clear();
98
+ }
99
+ function acquirePersistentRunLease(sessionId, path) {
100
+ const normalizedPath = resolve(path);
101
+ if (activePersistentRunLeases.has(normalizedPath)) {
102
+ throw new Error(`Cannot start pi-server run for ${sessionId}; this process already owns the session lease`);
103
+ }
104
+ const lease = acquirePiServerRunStateLease(normalizedPath);
105
+ activePersistentRunLeases.set(normalizedPath, { sessionId, lease, scope: "run" });
106
+ return lease;
107
+ }
108
+ export function releasePiServerRunLease(sessionId, path) {
109
+ if (!path)
110
+ return;
111
+ const normalizedPath = resolve(path);
112
+ const active = activePersistentRunLeases.get(normalizedPath);
113
+ if (!active || active.sessionId !== sessionId || active.scope !== "run")
114
+ return;
115
+ activePersistentRunLeases.delete(normalizedPath);
116
+ releasePiServerRunStateLease(active.lease);
117
+ }
118
+ export function acquirePiServerSessionLease(sessionId, path) {
119
+ const normalizedPath = resolve(path);
120
+ if (activePersistentRunLeases.has(normalizedPath)) {
121
+ throw new Error(`Cannot start pi-server session ${sessionId}; this process already owns the session lease`);
122
+ }
123
+ const lease = acquirePiServerRunStateLease(normalizedPath);
124
+ activePersistentRunLeases.set(normalizedPath, { sessionId, lease, scope: "session" });
125
+ }
126
+ export function releasePiServerSessionLease(sessionId, path) {
127
+ if (!path)
128
+ return;
129
+ const normalizedPath = resolve(path);
130
+ const active = activePersistentRunLeases.get(normalizedPath);
131
+ if (!active || active.sessionId !== sessionId || active.scope !== "session")
132
+ return;
133
+ activePersistentRunLeases.delete(normalizedPath);
134
+ releasePiServerRunStateLease(active.lease);
135
+ }
136
+ function getPersistentRunLease(sessionId, path) {
137
+ const active = activePersistentRunLeases.get(resolve(path));
138
+ if (!active)
139
+ return undefined;
140
+ if (active.sessionId !== sessionId) {
141
+ throw new Error(`Pi-server run lease belongs to session ${active.sessionId}, not ${sessionId}`);
142
+ }
143
+ return active;
144
+ }
145
+ class PiServerCompactOperationError extends Error {
146
+ payload;
147
+ constructor(message, payload) {
148
+ super(message);
149
+ this.name = "PiServerCompactOperationError";
150
+ this.payload = payload;
151
+ }
152
+ }
153
+ class PiServerCompactProtocolError extends Error {
154
+ constructor(message) {
155
+ super(message);
156
+ this.name = "PiServerCompactProtocolError";
157
+ }
158
+ }
159
+ class PiServerCompactInterruptionError extends Error {
160
+ observedProgress;
161
+ constructor(message, observedProgress) {
162
+ super(message);
163
+ this.name = "PiServerCompactInterruptionError";
164
+ this.observedProgress = observedProgress;
165
+ }
166
+ }
167
+ class PiServerStreamProtocolError extends Error {
168
+ constructor(message) {
169
+ super(message);
170
+ this.name = "PiServerStreamProtocolError";
171
+ }
172
+ }
173
+ class PiServerHttpResponseError extends Error {
174
+ status;
175
+ retryable;
176
+ constructor(message, status, retryable) {
177
+ super(message);
178
+ this.name = "PiServerHttpResponseError";
179
+ this.status = status;
180
+ this.retryable = retryable;
181
+ }
76
182
  }
77
183
  function isObject(value) {
78
184
  return typeof value === "object" && value !== null && !Array.isArray(value);
79
185
  }
186
+ function isNonNegativeInteger(value) {
187
+ return typeof value === "number" && Number.isInteger(value) && value >= 0;
188
+ }
189
+ export function getPiServerRunDiagnostic(message) {
190
+ const diagnostics = message.diagnostics?.filter((diagnostic) => diagnostic.type === "pi_server_run") ?? [];
191
+ if (diagnostics.length === 0)
192
+ return undefined;
193
+ if (diagnostics.length !== 1) {
194
+ throw new Error("pi-server assistant message contained multiple run diagnostics");
195
+ }
196
+ const details = diagnostics[0].details;
197
+ if (!isObject(details) ||
198
+ typeof details.sessionId !== "string" ||
199
+ details.sessionId.length === 0 ||
200
+ typeof details.runId !== "string" ||
201
+ details.runId.length === 0 ||
202
+ typeof details.requestMac !== "string" ||
203
+ !/^[a-f0-9]{64}$/u.test(details.requestMac) ||
204
+ (details.restartUnknown !== undefined && typeof details.restartUnknown !== "boolean")) {
205
+ throw new Error("pi-server assistant message contained an invalid run diagnostic");
206
+ }
207
+ return {
208
+ sessionId: details.sessionId,
209
+ runId: details.runId,
210
+ requestMac: details.requestMac,
211
+ restartUnknown: details.restartUnknown === true,
212
+ };
213
+ }
214
+ function hashPiServerStreamRequest(value) {
215
+ const serialized = canonicalJsonStringify(value);
216
+ if (serialized === undefined) {
217
+ throw new Error("Failed to serialize pi-server stream request identity");
218
+ }
219
+ return createHash("sha256").update(serialized).digest("hex");
220
+ }
221
+ function parsePiServerSessionSummary(value, expectedSessionId, errorPrefix) {
222
+ if (!isObject(value)) {
223
+ throw new Error(`${errorPrefix} (response was not an object)`);
224
+ }
225
+ if (value.protocolVersion !== PI_SERVER_PROTOCOL_VERSION) {
226
+ const received = value.protocolVersion === undefined ? "missing" : JSON.stringify(value.protocolVersion);
227
+ throw new Error(`${errorPrefix} (unsupported pi-server protocol version: ${received}; expected ${PI_SERVER_PROTOCOL_VERSION})`);
228
+ }
229
+ if (value.sessionId !== expectedSessionId) {
230
+ throw new Error(`${errorPrefix} (response sessionId did not match the requested session)`);
231
+ }
232
+ if (typeof value.staticContextHash !== "string" ||
233
+ (value.staticContextHash !== "" && !/^[a-f0-9]{64}$/i.test(value.staticContextHash))) {
234
+ throw new Error(`${errorPrefix} (response staticContextHash was not a valid digest)`);
235
+ }
236
+ if (typeof value.treeHash !== "string" || !/^[a-f0-9]{64}$/i.test(value.treeHash)) {
237
+ throw new Error(`${errorPrefix} (response treeHash was not a valid digest)`);
238
+ }
239
+ if (!isNonNegativeInteger(value.messageCount)) {
240
+ throw new Error(`${errorPrefix} (response messageCount was not a non-negative integer)`);
241
+ }
242
+ if (!isNonNegativeInteger(value.entryCount)) {
243
+ throw new Error(`${errorPrefix} (response entryCount was not a non-negative integer)`);
244
+ }
245
+ if (value.leafId !== null && typeof value.leafId !== "string") {
246
+ throw new Error(`${errorPrefix} (response leafId was not a string or null)`);
247
+ }
248
+ if (!isNonNegativeInteger(value.revision)) {
249
+ throw new Error(`${errorPrefix} (response revision was not a non-negative integer)`);
250
+ }
251
+ return value;
252
+ }
253
+ function recordPiServerSessionSummary(sessionId, summary) {
254
+ sessionTreeHashes.set(sessionId, summary.treeHash);
255
+ sessionTreeEntryCounts.set(sessionId, summary.entryCount);
256
+ sessionTreeLeafIds.set(sessionId, summary.leafId);
257
+ sessionTreeRevisions.set(sessionId, summary.revision);
258
+ }
259
+ function extendsCachedImmutableTree(cachedEntries, entries) {
260
+ if (cachedEntries.length > entries.length)
261
+ return false;
262
+ if (cachedEntries.length === 0)
263
+ return true;
264
+ return cachedEntries[cachedEntries.length - 1] === entries[cachedEntries.length - 1];
265
+ }
266
+ function hashLocalTree(sessionId, entries) {
267
+ const cached = sessionLocalTreeHashes.get(sessionId);
268
+ let treeHash = PI_SERVER_EMPTY_TREE_HASH;
269
+ let startIndex = 0;
270
+ if (cached && extendsCachedImmutableTree(cached.entries, entries)) {
271
+ treeHash = cached.treeHash;
272
+ startIndex = cached.entries.length;
273
+ }
274
+ for (let index = startIndex; index < entries.length; index++) {
275
+ treeHash = appendPiServerTreeHash(treeHash, hashPiServerTreeEntry(entries[index]));
276
+ }
277
+ sessionLocalTreeHashes.set(sessionId, { entries, treeHash });
278
+ return treeHash;
279
+ }
280
+ function hashLocalTreePrefix(sessionId, entries, entryCount) {
281
+ const cached = sessionLocalTreeHashes.get(sessionId);
282
+ if (cached && cached.entries.length === entryCount && extendsCachedImmutableTree(cached.entries, entries)) {
283
+ return cached.treeHash;
284
+ }
285
+ let treeHash = PI_SERVER_EMPTY_TREE_HASH;
286
+ for (let index = 0; index < entryCount; index++) {
287
+ treeHash = appendPiServerTreeHash(treeHash, hashPiServerTreeEntry(entries[index]));
288
+ }
289
+ return treeHash;
290
+ }
80
291
  function getResponseStatus(response) {
81
292
  const status = response.statusText ? `${response.status} ${response.statusText}` : String(response.status);
82
293
  return response.status >= 500 ? `${status} (server error)` : status;
@@ -91,6 +302,60 @@ function getBodyExcerpt(text) {
91
302
  const excerpt = trimmed.slice(0, RESPONSE_BODY_EXCERPT_CHARS);
92
303
  return excerpt.length < trimmed.length ? `${excerpt}...` : excerpt;
93
304
  }
305
+ function getPiServerResponseNoProgressTimeoutMs() {
306
+ const environmentValue = process.env.PI_SERVER_RESPONSE_NO_PROGRESS_TIMEOUT_MS;
307
+ const value = environmentValue === undefined ? PI_SERVER_RESPONSE_NO_PROGRESS_TIMEOUT_MS : Number(environmentValue);
308
+ if (!Number.isFinite(value) || value <= 0) {
309
+ throw new Error("PI_SERVER_RESPONSE_NO_PROGRESS_TIMEOUT_MS must be positive finite milliseconds");
310
+ }
311
+ return value;
312
+ }
313
+ function createPiServerBodyNoProgressState() {
314
+ const timeoutMs = getPiServerResponseNoProgressTimeoutMs();
315
+ return { timeoutMs, deadlineMs: Date.now() + timeoutMs };
316
+ }
317
+ function createPiServerResponseNoProgressError(timeoutMs) {
318
+ const error = new Error(`pi-server response made no progress for ${timeoutMs}ms`);
319
+ error.name = "PiServerResponseNoProgressError";
320
+ return error;
321
+ }
322
+ async function readPiServerBodyChunk(reader, state) {
323
+ while (true) {
324
+ const remainingMs = state.deadlineMs - Date.now();
325
+ if (remainingMs <= 0) {
326
+ throw createPiServerResponseNoProgressError(state.timeoutMs);
327
+ }
328
+ const result = await new Promise((resolve, reject) => {
329
+ let settled = false;
330
+ const timer = setTimeout(() => {
331
+ if (settled)
332
+ return;
333
+ settled = true;
334
+ reject(createPiServerResponseNoProgressError(state.timeoutMs));
335
+ }, remainingMs);
336
+ timer.unref();
337
+ void reader.read().then((value) => {
338
+ if (settled)
339
+ return;
340
+ settled = true;
341
+ clearTimeout(timer);
342
+ resolve(value);
343
+ }, (error) => {
344
+ if (settled)
345
+ return;
346
+ settled = true;
347
+ clearTimeout(timer);
348
+ reject(error);
349
+ });
350
+ });
351
+ if (result.done || result.value.byteLength > 0) {
352
+ if (!result.done)
353
+ state.deadlineMs = Date.now() + state.timeoutMs;
354
+ return result;
355
+ }
356
+ await new Promise((resolve) => setTimeout(resolve, 0));
357
+ }
358
+ }
94
359
  function getJsonErrorText(text) {
95
360
  if (!text)
96
361
  return undefined;
@@ -110,16 +375,17 @@ function formatResponseDetails(response, bodyText) {
110
375
  return `${getResponseStatus(response)}; content-type: ${getResponseContentType(response)}; body excerpt: ${body}`;
111
376
  }
112
377
  async function readPiServerFailure(response) {
113
- const bodyText = await response.text();
378
+ const bodyText = await readPiServerResponseText(response, PI_SERVER_ERROR_BODY_MAX_BYTES);
114
379
  return {
115
380
  details: formatResponseDetails(response, bodyText),
116
381
  matchText: getJsonErrorText(bodyText) ?? bodyText,
382
+ bodyText,
117
383
  };
118
384
  }
119
385
  async function readPiServerJson(response, errorPrefix) {
120
- const bodyText = await response.text();
386
+ const bodyText = await readPiServerResponseText(response, response.ok ? Number.POSITIVE_INFINITY : PI_SERVER_ERROR_BODY_MAX_BYTES);
121
387
  if (!response.ok) {
122
- throw new Error(`${errorPrefix} (${formatResponseDetails(response, bodyText)})`);
388
+ throw new PiServerHttpResponseError(`${errorPrefix} (${formatResponseDetails(response, bodyText)})`, response.status, isTransientPiServerResponse(response, bodyText));
123
389
  }
124
390
  try {
125
391
  return JSON.parse(bodyText);
@@ -133,41 +399,41 @@ function getServerSentEventFieldValue(line, field) {
133
399
  const value = line.slice(prefix.length);
134
400
  return value.startsWith(" ") ? value.slice(1) : value;
135
401
  }
136
- function parseServerSentEvents(bodyText) {
137
- const events = [];
138
- let event = "message";
139
- let dataLines = [];
140
- const flush = () => {
141
- if (dataLines.length === 0)
142
- return;
143
- events.push({ event, data: dataLines.join("\n") });
144
- event = "message";
145
- dataLines = [];
146
- };
147
- for (const line of bodyText.split(/\r?\n/)) {
148
- if (line === "") {
149
- flush();
150
- continue;
151
- }
152
- if (line.startsWith(":"))
153
- continue;
154
- if (line.startsWith("event:")) {
155
- event = getServerSentEventFieldValue(line, "event");
402
+ function updateEventStreamLineBytes(chunk, currentLineBytes) {
403
+ let lineBytes = currentLineBytes;
404
+ for (const byte of chunk) {
405
+ if (byte === 0x0a) {
406
+ lineBytes = 0;
156
407
  continue;
157
408
  }
158
- if (line.startsWith("data:")) {
159
- dataLines.push(getServerSentEventFieldValue(line, "data"));
160
- }
409
+ lineBytes++;
410
+ if (lineBytes > PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES)
411
+ return -1;
412
+ }
413
+ return lineBytes;
414
+ }
415
+ function extractDecodedEventStreamLines(decoded, incompleteLineParts) {
416
+ const lines = [];
417
+ let start = 0;
418
+ let newlineIndex = decoded.indexOf("\n");
419
+ while (newlineIndex >= 0) {
420
+ incompleteLineParts.push(decoded.slice(start, newlineIndex));
421
+ lines.push(incompleteLineParts.join(""));
422
+ incompleteLineParts.length = 0;
423
+ start = newlineIndex + 1;
424
+ newlineIndex = decoded.indexOf("\n", start);
425
+ }
426
+ if (start < decoded.length) {
427
+ incompleteLineParts.push(decoded.slice(start));
161
428
  }
162
- flush();
163
- return events;
429
+ return lines;
164
430
  }
165
431
  function parseServerSentEventData(event, errorPrefix) {
166
432
  try {
167
433
  return JSON.parse(event.data);
168
434
  }
169
435
  catch {
170
- throw new Error(`${errorPrefix} (invalid event-stream JSON data: ${getBodyExcerpt(event.data)})`);
436
+ throw new PiServerCompactProtocolError(`${errorPrefix} (invalid event-stream JSON data: ${getBodyExcerpt(event.data)})`);
171
437
  }
172
438
  }
173
439
  function formatServerSentEventError(response, payload) {
@@ -175,37 +441,139 @@ function formatServerSentEventError(response, payload) {
175
441
  return `${getResponseStatus(response)}; content-type: ${getResponseContentType(response)}; body excerpt: ${getBodyExcerpt(message)}`;
176
442
  }
177
443
  async function readPiServerEventStreamJson(response, errorPrefix) {
178
- const bodyText = await response.text();
179
- for (const event of parseServerSentEvents(bodyText)) {
444
+ if (!response.body) {
445
+ throw new PiServerCompactInterruptionError(`${errorPrefix} (response body was missing)`, false);
446
+ }
447
+ const reader = response.body.getReader();
448
+ const decoder = new TextDecoder();
449
+ const noProgress = createPiServerBodyNoProgressState();
450
+ let observedProgress = false;
451
+ const incompleteLineParts = [];
452
+ let eventName = "message";
453
+ let dataLines = [];
454
+ let eventDataBytes = 0;
455
+ let incompleteLineBytes = 0;
456
+ const dispatchEvent = () => {
457
+ if (dataLines.length === 0) {
458
+ eventName = "message";
459
+ return undefined;
460
+ }
461
+ const event = { event: eventName, data: dataLines.join("\n") };
462
+ eventName = "message";
463
+ dataLines = [];
464
+ eventDataBytes = 0;
180
465
  if (event.event === "error") {
181
- throw new Error(`${errorPrefix} (${formatServerSentEventError(response, parseServerSentEventData(event, errorPrefix))})`);
466
+ const payload = parseServerSentEventData(event, errorPrefix);
467
+ throw new PiServerCompactOperationError(`${errorPrefix} (${formatServerSentEventError(response, payload)})`, payload);
182
468
  }
183
469
  if (event.event === "result") {
184
- return parseServerSentEventData(event, errorPrefix);
470
+ return { result: parseServerSentEventData(event, errorPrefix) };
471
+ }
472
+ return undefined;
473
+ };
474
+ const consumeLine = (rawLine) => {
475
+ const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
476
+ if (line === "")
477
+ return dispatchEvent();
478
+ if (line.startsWith(":"))
479
+ return undefined;
480
+ if (line.startsWith("event:")) {
481
+ eventName = getServerSentEventFieldValue(line, "event");
482
+ }
483
+ else if (line.startsWith("data:")) {
484
+ const data = getServerSentEventFieldValue(line, "data");
485
+ eventDataBytes += Buffer.byteLength(data, "utf-8") + (dataLines.length === 0 ? 0 : 1);
486
+ if (eventDataBytes > PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES) {
487
+ throw new PiServerCompactProtocolError(`${errorPrefix} (event-stream frame exceeded ${PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES} bytes)`);
488
+ }
489
+ dataLines.push(data);
490
+ }
491
+ return undefined;
492
+ };
493
+ try {
494
+ while (true) {
495
+ let readResult;
496
+ try {
497
+ readResult = await readPiServerBodyChunk(reader, noProgress);
498
+ }
499
+ catch (error) {
500
+ throw new PiServerCompactInterruptionError(error instanceof Error ? error.message : String(error), observedProgress);
501
+ }
502
+ if (readResult.done) {
503
+ const finalLines = extractDecodedEventStreamLines(decoder.decode(), incompleteLineParts);
504
+ for (const line of finalLines) {
505
+ const result = consumeLine(line);
506
+ if (result)
507
+ return result.result;
508
+ }
509
+ if (incompleteLineParts.length > 0) {
510
+ const result = consumeLine(incompleteLineParts.join(""));
511
+ if (result)
512
+ return result.result;
513
+ incompleteLineParts.length = 0;
514
+ }
515
+ const result = dispatchEvent();
516
+ if (result)
517
+ return result.result;
518
+ throw new PiServerCompactInterruptionError(`${errorPrefix} (event stream ended before a result event)`, observedProgress);
519
+ }
520
+ if (readResult.value.byteLength > 0)
521
+ observedProgress = true;
522
+ incompleteLineBytes = updateEventStreamLineBytes(readResult.value, incompleteLineBytes);
523
+ if (incompleteLineBytes < 0) {
524
+ throw new PiServerCompactProtocolError(`${errorPrefix} (event-stream line exceeded ${PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES} bytes)`);
525
+ }
526
+ const lines = extractDecodedEventStreamLines(decoder.decode(readResult.value, { stream: true }), incompleteLineParts);
527
+ for (const line of lines) {
528
+ const result = consumeLine(line);
529
+ if (result)
530
+ return result.result;
531
+ }
185
532
  }
186
533
  }
187
- throw new Error(`${errorPrefix} (${formatResponseDetails(response, bodyText)}; expected result event)`);
534
+ finally {
535
+ void reader.cancel().catch(() => undefined);
536
+ }
188
537
  }
189
538
  async function readPiServerCompactResponse(response, errorPrefix) {
190
539
  const contentType = getResponseContentType(response).toLowerCase().split(";")[0]?.trim();
191
540
  if (contentType === "text/event-stream") {
192
541
  return readPiServerEventStreamJson(response, errorPrefix);
193
542
  }
194
- return readPiServerJson(response, errorPrefix);
543
+ let bodyText;
544
+ try {
545
+ bodyText = await readPiServerResponseText(response);
546
+ }
547
+ catch (error) {
548
+ throw new PiServerCompactInterruptionError(error instanceof Error ? error.message : String(error), false);
549
+ }
550
+ try {
551
+ return JSON.parse(bodyText);
552
+ }
553
+ catch {
554
+ throw new PiServerCompactProtocolError(`${errorPrefix} (${formatResponseDetails(response, bodyText)}; expected JSON)`);
555
+ }
195
556
  }
196
557
  function isAbortError(error, signal) {
197
558
  return signal?.aborted === true || (error instanceof Error && error.name === "AbortError");
198
559
  }
199
560
  function isTransientPiServerFetchError(error) {
200
561
  const message = error instanceof Error ? error.message : String(error);
201
- return /fetch failed|network|socket hang up|terminated|ECONNRESET|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|CONNECT timeout/i.test(message);
562
+ return /fetch failed|network|socket (?:hang up|closed|lost)|terminated|ECONNRESET|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|CONNECT timeout|response made no progress/i.test(message);
202
563
  }
203
- function isTransientPiServerResponse(response, bodyText) {
204
- if (!TRANSIENT_PI_SERVER_STATUS_CODES.has(response.status))
205
- return false;
206
- if (response.status === 530)
564
+ function isTransientPiServerResponse(response, _bodyText) {
565
+ return TRANSIENT_PI_SERVER_STATUS_CODES.has(response.status);
566
+ }
567
+ function isRetryablePiServerInterruption(error) {
568
+ if (error instanceof PiServerHttpResponseError)
569
+ return error.retryable;
570
+ if (error instanceof PiServerCompactInterruptionError)
207
571
  return true;
208
- return /CONNECT timeout|Bad Gateway|upstream|timeout/i.test(`${response.statusText}\n${bodyText}`);
572
+ if (error instanceof PiServerCompactProtocolError || error instanceof PiServerStreamProtocolError)
573
+ return false;
574
+ if (error instanceof PiServerTransportBodyLimitError)
575
+ return false;
576
+ return isTransientPiServerFetchError(error);
209
577
  }
210
578
  async function waitForPiServerRetry(attempt, signal) {
211
579
  await sleep(TRANSIENT_PI_SERVER_RETRY_DELAYS_MS[attempt], undefined, { signal });
@@ -225,7 +593,19 @@ async function postPiServerJsonWithTransientRetry(request, endpoint, body, error
225
593
  }
226
594
  throw error;
227
595
  }
228
- const bodyText = await response.text();
596
+ let bodyText;
597
+ try {
598
+ bodyText = await readPiServerResponseText(response, response.ok ? Number.POSITIVE_INFINITY : PI_SERVER_ERROR_BODY_MAX_BYTES);
599
+ }
600
+ catch (error) {
601
+ if (attempt < TRANSIENT_PI_SERVER_RETRY_DELAYS_MS.length &&
602
+ !request.options.signal?.aborted &&
603
+ isTransientPiServerFetchError(error)) {
604
+ await waitForPiServerRetry(attempt, request.options.signal);
605
+ continue;
606
+ }
607
+ throw error;
608
+ }
229
609
  if (!response.ok) {
230
610
  if (attempt < TRANSIENT_PI_SERVER_RETRY_DELAYS_MS.length &&
231
611
  !request.options.signal?.aborted &&
@@ -233,7 +613,7 @@ async function postPiServerJsonWithTransientRetry(request, endpoint, body, error
233
613
  await waitForPiServerRetry(attempt, request.options.signal);
234
614
  continue;
235
615
  }
236
- throw new Error(`${errorPrefix} (${formatResponseDetails(response, bodyText)})`);
616
+ throw new PiServerHttpResponseError(`${errorPrefix} (${formatResponseDetails(response, bodyText)})`, response.status, isTransientPiServerResponse(response, bodyText));
237
617
  }
238
618
  try {
239
619
  return JSON.parse(bodyText);
@@ -248,46 +628,37 @@ async function ensurePiServerEventStream(response) {
248
628
  if (contentType.toLowerCase().split(";")[0]?.trim() === "text/event-stream") {
249
629
  return;
250
630
  }
251
- const bodyText = await response.text();
631
+ const bodyText = await readPiServerResponseText(response, PI_SERVER_ERROR_BODY_MAX_BYTES);
252
632
  throw new Error(`pi-server error: ${formatResponseDetails(response, bodyText)}; expected text/event-stream`);
253
633
  }
254
634
  async function ensureSessionInit(sessionId, context, request) {
255
635
  const currentHash = hashStaticContext(context);
256
636
  const previousHash = sessionStaticContextHashes.get(sessionId);
257
637
  if (previousHash === currentHash) {
258
- return {
259
- sessionId,
260
- staticContextHash: previousHash,
261
- messageCount: 0,
262
- fromCache: true,
263
- };
638
+ return;
264
639
  }
265
640
  const staticContext = {
266
641
  systemPrompt: context.systemPrompt,
267
642
  tools: context.tools,
268
643
  };
269
- const endpoint = previousHash === undefined ? "/api/session/init" : "/api/session/update";
270
- const result = await postPiServerJsonWithTransientRetry(request, endpoint, { sessionId, staticContext }, "Session init failed");
271
- sessionStaticContextHashes.set(sessionId, currentHash);
272
- if (result.treeHash !== undefined) {
273
- sessionTreeHashes.set(sessionId, result.treeHash);
274
- sessionTreeLeafIds.set(sessionId, result.leafId ?? null);
275
- if (result.entryCount !== undefined) {
276
- sessionTreeEntryCounts.set(sessionId, result.entryCount);
277
- }
644
+ const probeValue = await postPiServerJsonWithTransientRetry(request, "/api/session/init", { sessionId, staticContextHash: currentHash }, "Session init failed");
645
+ const probe = parsePiServerSessionSummary(probeValue, sessionId, "Session init failed");
646
+ if (!isObject(probeValue) || typeof probeValue.staticContextRequired !== "boolean") {
647
+ throw new Error("Session init failed (protocol v2 response did not include staticContextRequired)");
278
648
  }
279
- return result;
280
- }
281
- function getStaticContext(context) {
282
- return {
283
- systemPrompt: context.systemPrompt,
284
- tools: context.tools,
285
- };
649
+ let result = probe;
650
+ if (probeValue.staticContextRequired) {
651
+ const updateValue = await postPiServerJsonWithTransientRetry(request, "/api/session/update", { sessionId, staticContext }, "Session static context update failed");
652
+ result = parsePiServerSessionSummary(updateValue, sessionId, "Session static context update failed");
653
+ }
654
+ if (result.staticContextHash !== currentHash) {
655
+ throw new Error("Session init failed (server staticContextHash did not match the requested context)");
656
+ }
657
+ sessionStaticContextHashes.set(sessionId, currentHash);
658
+ recordPiServerSessionSummary(sessionId, result);
286
659
  }
287
660
  function markTreeSynced(sessionId, tree) {
288
- sessionSyncedEntryIds.set(sessionId, new Set(tree.entries.map((entry) => entry.id)));
289
- const prefixHashes = buildPiServerTreePrefixHashes(tree.entries);
290
- sessionTreeHashes.set(sessionId, prefixHashes[prefixHashes.length - 1]);
661
+ sessionTreeHashes.set(sessionId, hashLocalTree(sessionId, tree.entries));
291
662
  sessionTreeEntryCounts.set(sessionId, tree.entries.length);
292
663
  sessionTreeLeafIds.set(sessionId, tree.leafId);
293
664
  if (tree.replace) {
@@ -298,58 +669,275 @@ function markTreeSynced(sessionId, tree) {
298
669
  }
299
670
  }
300
671
  async function postTreeJson(request, endpoint, body, errorPrefix) {
301
- await postPiServerJsonWithTransientRetry(request, endpoint, body, errorPrefix);
672
+ if (!isObject(body) || typeof body.sessionId !== "string") {
673
+ throw new Error(`${errorPrefix} (request sessionId was missing)`);
674
+ }
675
+ const value = await postPiServerJsonWithTransientRetry(request, endpoint, body, errorPrefix);
676
+ const summary = parsePiServerSessionSummary(value, body.sessionId, errorPrefix);
677
+ recordPiServerSessionSummary(body.sessionId, summary);
678
+ }
679
+ function parsePiServerTreeEntries(value, errorPrefix) {
680
+ if (!Array.isArray(value) ||
681
+ value.some((entry) => !isObject(entry) ||
682
+ typeof entry.type !== "string" ||
683
+ typeof entry.id !== "string" ||
684
+ (entry.parentId !== null && typeof entry.parentId !== "string") ||
685
+ typeof entry.timestamp !== "string")) {
686
+ throw new Error(`${errorPrefix} (response entries were not a valid session tree array)`);
687
+ }
688
+ return value;
689
+ }
690
+ function buildPiServerActiveBranch(entries, leafId, errorPrefix) {
691
+ const entriesById = new Map();
692
+ for (const entry of entries) {
693
+ if (entriesById.has(entry.id)) {
694
+ throw new Error(`${errorPrefix} (response session tree contained duplicate entry ids)`);
695
+ }
696
+ entriesById.set(entry.id, entry);
697
+ }
698
+ const branch = [];
699
+ const branchIds = new Set();
700
+ let current = leafId === null ? undefined : entriesById.get(leafId);
701
+ if (leafId !== null && !current) {
702
+ throw new Error(`${errorPrefix} (response leafId did not exist in the session tree)`);
703
+ }
704
+ while (current) {
705
+ if (branchIds.has(current.id)) {
706
+ throw new Error(`${errorPrefix} (response session tree contained a parent cycle)`);
707
+ }
708
+ branchIds.add(current.id);
709
+ branch.push(current);
710
+ if (current.parentId === null)
711
+ break;
712
+ const parent = entriesById.get(current.parentId);
713
+ if (!parent) {
714
+ throw new Error(`${errorPrefix} (response session tree contained a missing parent)`);
715
+ }
716
+ current = parent;
717
+ }
718
+ branch.reverse();
719
+ return branch;
720
+ }
721
+ function buildPiServerHistorySnapshot(summary, entries, errorPrefix) {
722
+ if (entries.length !== summary.entryCount) {
723
+ throw new Error(`${errorPrefix} (response entryCount did not match the session tree)`);
724
+ }
725
+ if (hashEntries(entries) !== summary.treeHash) {
726
+ throw new Error(`${errorPrefix} (response treeHash did not match the session tree)`);
727
+ }
728
+ const branch = buildPiServerActiveBranch(entries, summary.leafId, errorPrefix);
729
+ const messages = convertToLlm(buildSessionContext(branch).messages);
730
+ if (messages.length !== summary.messageCount) {
731
+ throw new Error(`${errorPrefix} (response messageCount did not match the active session branch)`);
732
+ }
733
+ return {
734
+ entries,
735
+ leafId: summary.leafId,
736
+ messages,
737
+ treeHash: summary.treeHash,
738
+ revision: summary.revision,
739
+ };
302
740
  }
303
741
  async function fetchPiServerHistory(sessionId, request, localTree) {
304
- const prefixHashes = localTree ? buildPiServerTreePrefixHashes(localTree.entries) : undefined;
305
- const historyUrl = localTree && prefixHashes
306
- ? `/api/session/${encodeURIComponent(sessionId)}/history?entriesFrom=${localTree.entries.length}&baseTreeHash=${encodeURIComponent(prefixHashes[prefixHashes.length - 1])}`
307
- : `/api/session/${encodeURIComponent(sessionId)}/history`;
742
+ const query = new URLSearchParams({ protocolVersion: String(PI_SERVER_PROTOCOL_VERSION) });
743
+ if (localTree) {
744
+ query.set("entriesFrom", String(localTree.entries.length));
745
+ query.set("baseTreeHash", hashLocalTree(sessionId, localTree.entries));
746
+ const revision = sessionTreeRevisions.get(sessionId);
747
+ if (revision !== undefined)
748
+ query.set("revision", String(revision));
749
+ }
750
+ const historyUrl = `/api/session/${encodeURIComponent(sessionId)}/history?${query.toString()}`;
308
751
  const response = await request.getJson(historyUrl);
309
752
  if (response.status === 404) {
310
- return undefined;
753
+ const failure = await readPiServerFailure(response);
754
+ if (/session not found/i.test(failure.matchText))
755
+ return undefined;
756
+ throw new Error(`Session history reconciliation failed (pi-server protocol v${PI_SERVER_PROTOCOL_VERSION} history endpoint unavailable; ${failure.details})`);
311
757
  }
312
- const history = await readPiServerJson(response, "Session history reconciliation failed");
313
- if (history.treePatch && localTree) {
314
- return {
315
- entries: [...localTree.entries.slice(0, history.treePatch.entriesFrom), ...history.treePatch.entries],
316
- leafId: history.treePatch.leafId,
317
- messages: history.messages ?? [],
318
- };
758
+ const historyValue = await readPiServerJson(response, "Session history reconciliation failed");
759
+ const summary = parsePiServerSessionSummary(historyValue, sessionId, "Session history reconciliation failed");
760
+ const expectedStaticContextHash = sessionStaticContextHashes.get(sessionId);
761
+ if (expectedStaticContextHash !== undefined && summary.staticContextHash !== expectedStaticContextHash) {
762
+ throw new Error("Session history reconciliation failed (server staticContextHash changed unexpectedly)");
319
763
  }
320
- if (!history.entries || history.leafId === undefined || !history.messages) {
321
- throw new Error("Session history reconciliation failed (response did not include the session tree)");
764
+ const history = historyValue;
765
+ if (history.treePatch !== undefined) {
766
+ if (!localTree || !isObject(history.treePatch)) {
767
+ throw new Error("Session history reconciliation failed (unexpected tree patch response)");
768
+ }
769
+ const patch = history.treePatch;
770
+ const expectedBaseRevision = sessionTreeRevisions.get(sessionId);
771
+ if (!isNonNegativeInteger(patch.entriesFrom) ||
772
+ patch.entriesFrom !== localTree.entries.length ||
773
+ !isNonNegativeInteger(patch.revision) ||
774
+ patch.revision !== summary.revision ||
775
+ (patch.baseRevision !== undefined && !isNonNegativeInteger(patch.baseRevision)) ||
776
+ (expectedBaseRevision !== undefined && patch.baseRevision !== expectedBaseRevision) ||
777
+ (patch.leafId !== null && typeof patch.leafId !== "string") ||
778
+ patch.leafId !== summary.leafId) {
779
+ throw new Error("Session history reconciliation failed (response tree patch metadata was invalid)");
780
+ }
781
+ const patchEntries = parsePiServerTreeEntries(patch.entries, "Session history reconciliation failed");
782
+ return buildPiServerHistorySnapshot(summary, [...localTree.entries.slice(0, patch.entriesFrom), ...patchEntries], "Session history reconciliation failed");
322
783
  }
323
- return {
324
- entries: history.entries,
325
- leafId: history.leafId,
326
- messages: history.messages,
327
- };
784
+ return buildPiServerHistorySnapshot(summary, parsePiServerTreeEntries(history.entries, "Session history reconciliation failed"), "Session history reconciliation failed");
328
785
  }
329
786
  async function applyPiServerHistory(sessionId, snapshot, onHistoryReconciled) {
330
787
  markTreeSynced(sessionId, snapshot);
788
+ sessionTreeRevisions.set(sessionId, snapshot.revision);
331
789
  await onHistoryReconciled?.(snapshot);
332
790
  }
333
791
  async function fetchPiServerRun(sessionId, runId, request) {
334
792
  const response = await request.getJson(`/api/session/${encodeURIComponent(sessionId)}/runs/${encodeURIComponent(runId)}`);
335
793
  if (response.status === 404) {
336
- return undefined;
794
+ const contentType = getResponseContentType(response).toLowerCase();
795
+ const failure = await readPiServerFailure(response);
796
+ if (contentType.includes("application/json") && /stream run not found|run not found/i.test(failure.matchText)) {
797
+ return undefined;
798
+ }
799
+ throw new Error(`Session run recovery failed (${failure.details}; expected structured run-not-found response)`);
800
+ }
801
+ const value = await readPiServerJson(response, "Session run recovery failed");
802
+ if (!isObject(value)) {
803
+ throw new Error("Session run recovery failed (response was not an object)");
804
+ }
805
+ if (value.sessionId !== sessionId) {
806
+ throw new Error("Session run recovery failed (response sessionId did not match the requested session)");
807
+ }
808
+ if (value.runId !== runId) {
809
+ throw new Error("Session run recovery failed (response runId did not match the requested run)");
810
+ }
811
+ if (typeof value.requestMac !== "string" || !/^[a-f0-9]{64}$/u.test(value.requestMac)) {
812
+ throw new Error("Session run recovery failed (response requestMac was not a lowercase SHA-256 digest)");
813
+ }
814
+ if (value.status !== "running" && value.status !== "completed" && value.status !== "failed") {
815
+ throw new Error("Session run recovery failed (response included an unsupported run status)");
816
+ }
817
+ if (!isNonNegativeInteger(value.nextSeq)) {
818
+ throw new Error("Session run recovery failed (response nextSeq was not a non-negative integer)");
819
+ }
820
+ if (value.message !== undefined && !isObject(value.message)) {
821
+ throw new Error("Session run recovery failed (response message was not an object)");
822
+ }
823
+ if (value.errorMessage !== undefined && typeof value.errorMessage !== "string") {
824
+ throw new Error("Session run recovery failed (response errorMessage was not a string)");
825
+ }
826
+ if (value.acknowledgedAt !== undefined && !isNonNegativeInteger(value.acknowledgedAt)) {
827
+ throw new Error("Session run recovery failed (response acknowledgedAt was not a non-negative integer)");
828
+ }
829
+ return value;
830
+ }
831
+ async function cancelPiServerRun(sessionId, runId) {
832
+ const request = createPiServerRequest(AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS));
833
+ const response = await request.postJson("/api/session/run/cancel", { sessionId, runId });
834
+ await readPiServerJson(response, "Session run cancel failed");
835
+ }
836
+ export async function acknowledgePiServerRunMessage(sessionId, message, runStatePath, signal) {
837
+ const activeLease = runStatePath ? getPersistentRunLease(sessionId, runStatePath) : undefined;
838
+ let lease = activeLease?.lease;
839
+ let releaseLeaseAfterAcknowledgement = activeLease?.scope === "run";
840
+ try {
841
+ const diagnostic = getPiServerRunDiagnostic(message);
842
+ if (!diagnostic)
843
+ return;
844
+ if (runStatePath && !lease) {
845
+ lease = acquirePersistentRunLease(sessionId, runStatePath);
846
+ releaseLeaseAfterAcknowledgement = true;
847
+ }
848
+ const pending = runStatePath ? readPiServerPendingRun(runStatePath, lease) : sessionPendingRuns.get(sessionId);
849
+ if (!pending)
850
+ return;
851
+ if (diagnostic.sessionId !== sessionId ||
852
+ diagnostic.sessionId !== pending.sessionId ||
853
+ diagnostic.runId !== pending.runId ||
854
+ diagnostic.requestMac !== pending.requestHash) {
855
+ throw new Error("Cannot acknowledge pi-server run: terminal diagnostic did not match durable pending state");
856
+ }
857
+ const ackSignal = signal?.aborted ? AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS) : signal;
858
+ const request = createPiServerRequest(ackSignal);
859
+ const response = await request.postJson("/api/session/run/ack", { sessionId, runId: pending.runId });
860
+ if (response.status === 404) {
861
+ const failure = await readPiServerFailure(response);
862
+ if (!/stream run not found|run not found/i.test(failure.matchText)) {
863
+ throw new Error(`Session run acknowledgement failed (${failure.details})`);
864
+ }
865
+ // The caller only reaches this point after the matching terminal assistant and tree
866
+ // are fsynced locally. A server-side acknowledged journal may already have expired.
867
+ if (runStatePath) {
868
+ acknowledgePiServerPendingRun(runStatePath, pending.runId, Date.now(), lease);
869
+ }
870
+ else {
871
+ sessionPendingRuns.delete(sessionId);
872
+ }
873
+ return;
874
+ }
875
+ const value = await readPiServerJson(response, "Session run acknowledgement failed");
876
+ if (!isObject(value) ||
877
+ value.acknowledged !== true ||
878
+ value.sessionId !== sessionId ||
879
+ value.runId !== pending.runId ||
880
+ value.requestMac !== pending.requestHash ||
881
+ (value.status !== "completed" && value.status !== "failed") ||
882
+ !isNonNegativeInteger(value.acknowledgedAt)) {
883
+ throw new Error("Session run acknowledgement failed (response did not match the terminal pending run)");
884
+ }
885
+ if (runStatePath) {
886
+ acknowledgePiServerPendingRun(runStatePath, pending.runId, Date.now(), lease);
887
+ }
888
+ else {
889
+ sessionPendingRuns.delete(sessionId);
890
+ }
891
+ }
892
+ finally {
893
+ if (runStatePath && lease && releaseLeaseAfterAcknowledgement) {
894
+ releasePiServerRunLease(sessionId, runStatePath);
895
+ }
896
+ }
897
+ }
898
+ async function cancelPiServerCompact(sessionId, operationId, requestHash) {
899
+ const request = createPiServerRequest(AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS));
900
+ const response = await request.postJson("/api/session/compact/cancel", { sessionId, operationId, requestHash });
901
+ const value = await readPiServerJson(response, "Session compaction cancel failed");
902
+ if (!isObject(value) ||
903
+ (value.canceled !== true && value.canceled !== false) ||
904
+ value.sessionId !== sessionId ||
905
+ value.operationId !== operationId ||
906
+ value.requestHash !== requestHash ||
907
+ value.status !== "failed" ||
908
+ value.resultStatus !== 499) {
909
+ throw new PiServerCompactProtocolError("Session compaction cancel failed (response identity was invalid)");
910
+ }
911
+ return parsePiServerCompactTerminalPayload(value.terminal, sessionId, operationId, requestHash, "Session compaction cancel failed");
912
+ }
913
+ async function recoverPiServerRun(sessionId, runId, request, signal) {
914
+ try {
915
+ const run = await fetchPiServerRun(sessionId, runId, request);
916
+ return run ? { status: run.status, requestMac: run.requestMac } : { status: "not_found" };
917
+ }
918
+ catch (error) {
919
+ if (signal?.aborted) {
920
+ throw error;
921
+ }
922
+ if (!isRetryablePiServerInterruption(error)) {
923
+ throw error;
924
+ }
925
+ return {
926
+ status: "unavailable",
927
+ details: `recovery request failed: ${error instanceof Error ? error.message : String(error)}`,
928
+ };
337
929
  }
338
- return readPiServerJson(response, "Session run recovery failed");
339
930
  }
340
- function getKnownServerPrefixIds(sessionId, entries) {
931
+ function getKnownServerPrefixCount(sessionId, entries) {
341
932
  const entryCount = sessionTreeEntryCounts.get(sessionId);
342
933
  const treeHash = sessionTreeHashes.get(sessionId);
343
934
  if (entryCount === undefined || treeHash === undefined || entryCount > entries.length) {
344
935
  return undefined;
345
936
  }
346
- const prefixHashes = buildPiServerTreePrefixHashes(entries);
347
- if (prefixHashes[entryCount] !== treeHash) {
937
+ if (hashLocalTreePrefix(sessionId, entries, entryCount) !== treeHash) {
348
938
  return undefined;
349
939
  }
350
- const ids = new Set(entries.slice(0, entryCount).map((entry) => entry.id));
351
- sessionSyncedEntryIds.set(sessionId, ids);
352
- return ids;
940
+ return entryCount;
353
941
  }
354
942
  function isRecoverableTreeDivergenceError(error) {
355
943
  const message = error instanceof Error ? error.message : String(error);
@@ -360,19 +948,18 @@ function isRecoverableMissingServerState(response, errorBody) {
360
948
  return false;
361
949
  return /Session has no static context|session not found|parent entry .* does not exist|leafId .* does not exist|entry .* already exists/i.test(errorBody);
362
950
  }
363
- async function syncFullPiServerTree(sessionId, context, tree, request) {
951
+ async function syncFullPiServerTree(sessionId, tree, request) {
364
952
  await postTreeJson(request, "/api/session/tree/sync", {
365
953
  sessionId,
366
954
  entries: tree.entries,
367
955
  leafId: tree.leafId,
368
- staticContext: getStaticContext(context),
369
956
  }, "Session tree sync failed");
370
957
  markTreeSynced(sessionId, tree);
371
958
  }
372
959
  function shouldUseServerHistory(sessionId, tree) {
373
960
  return !tree.replace && !sessionHasTemporaryTree.has(sessionId) && (sessionTreeEntryCounts.get(sessionId) ?? 0) > 0;
374
961
  }
375
- async function recoverPiServerTreeDivergence(sessionId, context, tree, request, onHistoryReconciled) {
962
+ async function recoverPiServerTreeDivergence(sessionId, tree, request, onHistoryReconciled) {
376
963
  if (shouldUseServerHistory(sessionId, tree)) {
377
964
  const snapshot = await fetchPiServerHistory(sessionId, request, tree);
378
965
  if (snapshot && snapshot.entries.length > 0) {
@@ -380,16 +967,19 @@ async function recoverPiServerTreeDivergence(sessionId, context, tree, request,
380
967
  throw new Error("pi-server history differed from local history; local session was reconciled to server history");
381
968
  }
382
969
  }
383
- await syncFullPiServerTree(sessionId, context, tree, request);
970
+ await syncFullPiServerTree(sessionId, tree, request);
384
971
  }
385
972
  export async function syncPiServerTree(sessionId, context, tree, options) {
386
973
  const request = createPiServerRequest(options?.signal);
387
974
  await ensureSessionInit(sessionId, context, request);
388
- await syncPiServerTreeWithRequest(sessionId, context, tree, request, options?.onHistoryReconciled);
975
+ await syncPiServerTreeWithRequest(sessionId, tree, request, options?.onHistoryReconciled);
389
976
  }
390
- async function syncPiServerTreeWithRequest(sessionId, context, tree, request, onHistoryReconciled) {
977
+ async function syncPiServerTreeWithRequest(sessionId, tree, request, onHistoryReconciled) {
391
978
  const syncTree = tree;
392
- const currentHash = hashEntries(syncTree.entries);
979
+ const knownServerPrefixCount = !tree.replace && !sessionHasTemporaryTree.has(sessionId)
980
+ ? getKnownServerPrefixCount(sessionId, syncTree.entries)
981
+ : undefined;
982
+ const currentHash = hashLocalTree(sessionId, syncTree.entries);
393
983
  const previousHash = sessionTreeHashes.get(sessionId);
394
984
  const previousLeafId = sessionTreeLeafIds.get(sessionId);
395
985
  if (!tree.replace && previousHash === currentHash) {
@@ -401,7 +991,7 @@ async function syncPiServerTreeWithRequest(sessionId, context, tree, request, on
401
991
  if (!isRecoverableTreeDivergenceError(error)) {
402
992
  throw error;
403
993
  }
404
- await recoverPiServerTreeDivergence(sessionId, context, syncTree, request, onHistoryReconciled);
994
+ await recoverPiServerTreeDivergence(sessionId, syncTree, request, onHistoryReconciled);
405
995
  return;
406
996
  }
407
997
  sessionTreeLeafIds.set(sessionId, syncTree.leafId);
@@ -409,32 +999,28 @@ async function syncPiServerTreeWithRequest(sessionId, context, tree, request, on
409
999
  markTreeSynced(sessionId, syncTree);
410
1000
  return;
411
1001
  }
412
- const syncedIds = !tree.replace && !sessionHasTemporaryTree.has(sessionId)
413
- ? (sessionSyncedEntryIds.get(sessionId) ?? getKnownServerPrefixIds(sessionId, syncTree.entries))
414
- : undefined;
415
- if (syncedIds) {
416
- const deltaEntries = syncTree.entries.filter((entry) => !syncedIds.has(entry.id));
1002
+ if (knownServerPrefixCount !== undefined) {
1003
+ const deltaEntries = syncTree.entries.slice(knownServerPrefixCount);
417
1004
  if (deltaEntries.length > 0) {
418
1005
  try {
419
1006
  await postTreeJson(request, "/api/session/tree/append", {
420
1007
  sessionId,
421
1008
  entries: deltaEntries,
422
1009
  leafId: syncTree.leafId,
423
- staticContext: getStaticContext(context),
424
1010
  }, "Session tree append failed");
425
1011
  }
426
1012
  catch (error) {
427
1013
  if (!isRecoverableTreeDivergenceError(error)) {
428
1014
  throw error;
429
1015
  }
430
- await recoverPiServerTreeDivergence(sessionId, context, syncTree, request, onHistoryReconciled);
1016
+ await recoverPiServerTreeDivergence(sessionId, syncTree, request, onHistoryReconciled);
431
1017
  return;
432
1018
  }
433
1019
  markTreeSynced(sessionId, syncTree);
434
1020
  return;
435
1021
  }
436
1022
  }
437
- await recoverPiServerTreeDivergence(sessionId, context, syncTree, request, onHistoryReconciled);
1023
+ await recoverPiServerTreeDivergence(sessionId, syncTree, request, onHistoryReconciled);
438
1024
  }
439
1025
  function serializeOptions(options) {
440
1026
  return {
@@ -452,93 +1038,1100 @@ function serializeOptions(options) {
452
1038
  websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,
453
1039
  maxRetries: options?.maxRetries,
454
1040
  maxRetryDelayMs: options?.maxRetryDelayMs,
1041
+ env: options?.env,
455
1042
  };
456
1043
  }
457
- export async function compactPiServer(model, context, options) {
458
- const sessionId = options?.sessionId ?? "default";
459
- const request = createPiServerRequest(options?.signal);
460
- await ensureSessionInit(sessionId, context, request);
461
- const tree = options?.sessionTree ?? getLinearTreeFromMessages(context.messages);
462
- await syncPiServerTreeWithRequest(sessionId, context, tree, request, options?.onHistoryReconciled);
463
- const makeBody = () => ({
464
- sessionId,
465
- model,
466
- options: serializeOptions(options),
467
- settings: options?.settings,
468
- preparation: options?.preparation,
469
- customInstructions: options?.customInstructions,
470
- baseTreeHash: sessionTreeHashes.get(sessionId) ?? hashEntries(tree.entries),
471
- streamResponse: true,
1044
+ export function hashPiServerCompactRequest(body) {
1045
+ const serialized = canonicalJsonStringify({
1046
+ protocolVersion: body.protocolVersion,
1047
+ sessionId: body.sessionId,
1048
+ model: body.model,
1049
+ options: body.options,
1050
+ settings: body.settings,
1051
+ preparation: body.preparation,
1052
+ extensionCompaction: body.extensionCompaction,
1053
+ customInstructions: body.customInstructions,
1054
+ baseStaticContextHash: body.baseStaticContextHash,
1055
+ baseTreeHash: body.baseTreeHash,
1056
+ baseEntryCount: body.baseEntryCount,
1057
+ baseLeafId: body.baseLeafId,
1058
+ baseRevision: body.baseRevision,
1059
+ streamResponse: body.streamResponse,
1060
+ retry: body.retry,
472
1061
  });
473
- let response = await request.postJson("/api/session/compact", makeBody());
474
- if (!response.ok) {
475
- let failure = await readPiServerFailure(response);
476
- if (!options?.signal?.aborted && isRecoverableMissingServerState(response, failure.matchText)) {
477
- resetSessionTracking(sessionId);
478
- await ensureSessionInit(sessionId, context, request);
479
- await syncPiServerTreeWithRequest(sessionId, context, tree, request, options?.onHistoryReconciled);
480
- response = await request.postJson("/api/session/compact", makeBody());
481
- if (response.ok) {
482
- failure = { details: "", matchText: "" };
483
- }
484
- else {
485
- failure = await readPiServerFailure(response);
486
- }
487
- }
488
- if (!response.ok) {
489
- throw new Error(`Server compaction failed (${failure.details})`);
490
- }
1062
+ if (serialized === undefined) {
1063
+ throw new Error("Failed to serialize pi-server compaction request identity");
491
1064
  }
492
- const result = await readPiServerCompactResponse(response, "Server compaction failed");
493
- if (!result.compaction) {
494
- throw new Error("Server compaction response did not include a compaction result");
1065
+ return createHash("sha256").update(serialized).digest("hex");
1066
+ }
1067
+ function createPiServerCompactAbortError(cancelFailure) {
1068
+ const cancelDetails = cancelFailure === undefined
1069
+ ? ""
1070
+ : ` (cancel request failed: ${cancelFailure instanceof Error ? cancelFailure.message : String(cancelFailure)})`;
1071
+ const error = new Error(`Compaction cancelled${cancelDetails}`);
1072
+ error.name = "AbortError";
1073
+ return error;
1074
+ }
1075
+ function getPiServerCompactReconnectDelay(attempt) {
1076
+ if (attempt === 0)
1077
+ return 0;
1078
+ return Math.min(1000 * 2 ** Math.min(attempt - 1, 5), PI_SERVER_COMPACT_MAX_RECONNECT_DELAY_MS);
1079
+ }
1080
+ function getPiServerCompactRecoveryWindowMs(configuredWindowMs) {
1081
+ const environmentValue = process.env.PI_SERVER_COMPACT_RECOVERY_WINDOW_MS;
1082
+ const value = configuredWindowMs ??
1083
+ (environmentValue === undefined ? PI_SERVER_COMPACT_DEFAULT_RECOVERY_WINDOW_MS : Number(environmentValue));
1084
+ if ((!Number.isFinite(value) && value !== Number.POSITIVE_INFINITY) || value <= 0) {
1085
+ throw new Error("pi-server compact recovery window must be positive milliseconds or Infinity (PiServerCompactOptions.piServerRecoveryWindowMs or PI_SERVER_COMPACT_RECOVERY_WINDOW_MS)");
495
1086
  }
496
- const resultEntries = result.entries ??
497
- (result.treePatch
498
- ? [...tree.entries.slice(0, result.treePatch.entriesFrom), ...result.treePatch.entries]
499
- : undefined);
500
- const resultLeafId = result.leafId ?? result.treePatch?.leafId;
501
- if (!result.compactionEntry || !resultEntries || resultLeafId === undefined) {
502
- throw new Error("Server compaction response did not include the updated session tree");
1087
+ return value;
1088
+ }
1089
+ async function waitForPiServerCompactReconnect(delayMs, signal) {
1090
+ if (signal?.aborted) {
1091
+ throw createPiServerCompactAbortError();
503
1092
  }
504
- markTreeSynced(sessionId, { entries: resultEntries, leafId: resultLeafId });
505
- return {
506
- compaction: result.compaction,
507
- compactionEntry: result.compactionEntry,
508
- entries: resultEntries,
509
- leafId: resultLeafId,
510
- messages: result.messages ?? [],
511
- };
1093
+ if (delayMs === 0)
1094
+ return;
1095
+ await new Promise((resolve, reject) => {
1096
+ let timer;
1097
+ const onAbort = () => {
1098
+ if (timer !== undefined)
1099
+ clearTimeout(timer);
1100
+ signal?.removeEventListener("abort", onAbort);
1101
+ reject(createPiServerCompactAbortError());
1102
+ };
1103
+ timer = setTimeout(() => {
1104
+ signal?.removeEventListener("abort", onAbort);
1105
+ resolve();
1106
+ }, delayMs);
1107
+ timer.unref();
1108
+ signal?.addEventListener("abort", onAbort, { once: true });
1109
+ if (signal?.aborted)
1110
+ onAbort();
1111
+ });
512
1112
  }
513
- export async function dropLastPiServerAssistantError(sessionId) {
514
- const request = createPiServerRequest();
515
- const response = await request.postJson("/api/session/drop-last-assistant-error", { sessionId });
516
- await readPiServerJson(response, "Dropping server assistant error failed");
1113
+ function getPiServerStreamReconnectDelay(attempt) {
1114
+ if (attempt === 0)
1115
+ return 0;
1116
+ return Math.min(1000 * 2 ** Math.min(attempt - 1, 5), PI_SERVER_STREAM_MAX_RECONNECT_DELAY_MS);
517
1117
  }
518
- export async function streamPiServer(model, context, options) {
519
- const sessionId = options?.sessionId ?? randomUUID();
520
- const runId = randomUUID();
521
- const isEphemeralSession = options?.sessionId === undefined;
522
- const stream = new PiServerEventStream();
523
- const partial = {
524
- role: "assistant",
525
- stopReason: "stop",
526
- content: [],
527
- api: model.api,
528
- provider: model.provider,
529
- model: model.id,
530
- usage: {
531
- input: 0,
532
- output: 0,
533
- cacheRead: 0,
534
- cacheWrite: 0,
535
- totalTokens: 0,
536
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
537
- },
538
- timestamp: Date.now(),
1118
+ function getPiServerStreamRecoveryWindowMs(configuredWindowMs) {
1119
+ const environmentValue = process.env.PI_SERVER_STREAM_RECOVERY_WINDOW_MS;
1120
+ const value = configuredWindowMs ??
1121
+ (environmentValue === undefined ? PI_SERVER_STREAM_DEFAULT_RECOVERY_WINDOW_MS : Number(environmentValue));
1122
+ if ((!Number.isFinite(value) && value !== Number.POSITIVE_INFINITY) || value <= 0) {
1123
+ throw new Error("pi-server stream recovery window must be positive milliseconds or Infinity (PiServerStreamOptions.piServerRecoveryWindowMs or PI_SERVER_STREAM_RECOVERY_WINDOW_MS)");
1124
+ }
1125
+ return value;
1126
+ }
1127
+ function createPiServerStreamAbortError() {
1128
+ const error = new Error("Request aborted by user");
1129
+ error.name = "AbortError";
1130
+ return error;
1131
+ }
1132
+ async function waitForPiServerStreamReconnect(delayMs, signal) {
1133
+ if (signal?.aborted) {
1134
+ throw createPiServerStreamAbortError();
1135
+ }
1136
+ if (delayMs === 0)
1137
+ return;
1138
+ await new Promise((resolve, reject) => {
1139
+ let timer;
1140
+ const onAbort = () => {
1141
+ if (timer !== undefined)
1142
+ clearTimeout(timer);
1143
+ signal?.removeEventListener("abort", onAbort);
1144
+ reject(createPiServerStreamAbortError());
1145
+ };
1146
+ timer = setTimeout(() => {
1147
+ signal?.removeEventListener("abort", onAbort);
1148
+ resolve();
1149
+ }, delayMs);
1150
+ timer.unref();
1151
+ signal?.addEventListener("abort", onAbort, { once: true });
1152
+ if (signal?.aborted)
1153
+ onAbort();
1154
+ });
1155
+ }
1156
+ function isRecoverablePiServerCompactResponse(response) {
1157
+ return response.status >= 500 || response.status === 408 || response.status === 425 || response.status === 429;
1158
+ }
1159
+ function parsePiServerCompactTerminalPayload(value, sessionId, operationId, requestHash, errorPrefix) {
1160
+ if (!isObject(value) ||
1161
+ value.protocolVersion !== PI_SERVER_PROTOCOL_VERSION ||
1162
+ value.sessionId !== sessionId ||
1163
+ value.operationId !== operationId ||
1164
+ value.requestHash !== requestHash ||
1165
+ (value.operationDisposition !== "terminal" && value.operationDisposition !== "not_started") ||
1166
+ (value.status !== "failed" && value.status !== "rejected") ||
1167
+ (value.operationDisposition === "terminal" && value.status !== "failed") ||
1168
+ (value.operationDisposition === "not_started" && value.status !== "rejected") ||
1169
+ !Number.isSafeInteger(value.httpStatus) ||
1170
+ value.httpStatus < 100 ||
1171
+ value.httpStatus > 599 ||
1172
+ typeof value.error !== "string" ||
1173
+ value.error.length === 0) {
1174
+ throw new PiServerCompactProtocolError(`${errorPrefix} (terminal operation identity was invalid)`);
1175
+ }
1176
+ return value;
1177
+ }
1178
+ function normalizePiServerCompactionResult(result, sessionId, operationId, requestHash, tree, base, compactStatePath) {
1179
+ const errorPrefix = "Server compaction failed";
1180
+ const protocolError = (details) => new PiServerCompactProtocolError(`${errorPrefix} (${details})`);
1181
+ if (!isObject(result)) {
1182
+ throw protocolError("protocol v2 response was not an object");
1183
+ }
1184
+ if (result.protocolVersion !== PI_SERVER_PROTOCOL_VERSION) {
1185
+ const received = result.protocolVersion === undefined ? "missing" : JSON.stringify(result.protocolVersion);
1186
+ throw protocolError(`unsupported pi-server protocol version: ${received}; expected ${PI_SERVER_PROTOCOL_VERSION}`);
1187
+ }
1188
+ if (result.sessionId !== sessionId) {
1189
+ throw protocolError("response sessionId did not match the requested session");
1190
+ }
1191
+ if (result.operationId !== operationId) {
1192
+ throw protocolError("response operationId did not match the requested operation");
1193
+ }
1194
+ if (result.requestHash !== requestHash) {
1195
+ throw protocolError("response requestHash did not match the requested operation");
1196
+ }
1197
+ if (!isObject(result.treePatch)) {
1198
+ throw protocolError("protocol v2 response did not include a treePatch");
1199
+ }
1200
+ const patch = result.treePatch;
1201
+ if (patch.baseStaticContextHash !== base.staticContextHash) {
1202
+ throw protocolError("treePatch baseStaticContextHash did not match the submitted context");
1203
+ }
1204
+ if (patch.baseTreeHash !== base.treeHash) {
1205
+ throw protocolError("treePatch baseTreeHash did not match the submitted tree");
1206
+ }
1207
+ if (patch.baseEntryCount !== base.entryCount ||
1208
+ patch.entriesFrom !== base.entryCount ||
1209
+ base.entryCount !== tree.entries.length) {
1210
+ throw protocolError("treePatch entriesFrom did not match the submitted tree");
1211
+ }
1212
+ if (patch.baseLeafId !== base.leafId || tree.leafId !== base.leafId) {
1213
+ throw protocolError("treePatch baseLeafId did not match the submitted tree");
1214
+ }
1215
+ if (patch.baseRevision !== base.revision) {
1216
+ throw protocolError("treePatch baseRevision did not match the submitted revision");
1217
+ }
1218
+ if (!Array.isArray(patch.entries) || patch.entries.length !== 1) {
1219
+ throw protocolError("treePatch must contain exactly one compaction entry");
1220
+ }
1221
+ const rawEntry = patch.entries[0];
1222
+ if (!isObject(rawEntry) ||
1223
+ rawEntry.type !== "compaction" ||
1224
+ typeof rawEntry.id !== "string" ||
1225
+ rawEntry.id.length === 0 ||
1226
+ typeof rawEntry.timestamp !== "string" ||
1227
+ typeof rawEntry.summary !== "string" ||
1228
+ typeof rawEntry.firstKeptEntryId !== "string" ||
1229
+ rawEntry.firstKeptEntryId.length === 0 ||
1230
+ typeof rawEntry.tokensBefore !== "number" ||
1231
+ !Number.isFinite(rawEntry.tokensBefore) ||
1232
+ rawEntry.tokensBefore < 0 ||
1233
+ (rawEntry.retainedTail !== undefined && !Array.isArray(rawEntry.retainedTail)) ||
1234
+ (rawEntry.usage !== undefined && !isObject(rawEntry.usage)) ||
1235
+ (rawEntry.fromHook !== undefined && typeof rawEntry.fromHook !== "boolean") ||
1236
+ !isObject(rawEntry.piServerCompactOperation)) {
1237
+ throw protocolError("treePatch entry was not a valid compaction entry");
1238
+ }
1239
+ const operation = rawEntry.piServerCompactOperation;
1240
+ if (operation.version !== 1 ||
1241
+ operation.operationId !== operationId ||
1242
+ operation.requestHash !== requestHash ||
1243
+ operation.baseStaticContextHash !== base.staticContextHash ||
1244
+ operation.baseTreeHash !== base.treeHash ||
1245
+ operation.baseEntryCount !== base.entryCount ||
1246
+ operation.baseLeafId !== base.leafId ||
1247
+ operation.baseRevision !== base.revision) {
1248
+ throw protocolError("compaction entry durable operation metadata did not match the request");
1249
+ }
1250
+ if (rawEntry.parentId !== tree.leafId) {
1251
+ throw protocolError("compaction entry parentId did not match the submitted leaf");
1252
+ }
1253
+ if (tree.entries.some((entry) => entry.id === rawEntry.id)) {
1254
+ throw protocolError("compaction entry id already existed in the submitted tree");
1255
+ }
1256
+ if (patch.leafId !== rawEntry.id) {
1257
+ throw protocolError("treePatch leafId did not match the compaction entry");
1258
+ }
1259
+ if (!isNonNegativeInteger(patch.revision) || patch.revision !== base.revision + 1) {
1260
+ throw protocolError("treePatch revision did not advance the submitted revision exactly once");
1261
+ }
1262
+ const compactionEntry = rawEntry;
1263
+ const entries = [...tree.entries, compactionEntry];
1264
+ if (typeof patch.treeHash !== "string" || !/^[a-f0-9]{64}$/i.test(patch.treeHash)) {
1265
+ throw protocolError("treePatch treeHash was not a valid digest");
1266
+ }
1267
+ if (hashEntries(entries) !== patch.treeHash) {
1268
+ throw protocolError("treePatch treeHash did not match the updated tree");
1269
+ }
1270
+ const branch = buildPiServerActiveBranch(entries, compactionEntry.id, errorPrefix);
1271
+ const compaction = {
1272
+ summary: compactionEntry.summary,
1273
+ firstKeptEntryId: compactionEntry.firstKeptEntryId,
1274
+ tokensBefore: compactionEntry.tokensBefore,
1275
+ retainedTail: compactionEntry.retainedTail,
1276
+ details: compactionEntry.details,
1277
+ usage: compactionEntry.usage,
1278
+ };
1279
+ return {
1280
+ sessionId,
1281
+ compaction,
1282
+ compactionEntry,
1283
+ entries,
1284
+ leafId: compactionEntry.id,
1285
+ messages: convertToLlm(buildSessionContext(branch).messages),
1286
+ operationId,
1287
+ requestHash,
1288
+ revision: patch.revision,
1289
+ treeHash: patch.treeHash,
1290
+ compactStatePath,
1291
+ };
1292
+ }
1293
+ function parseCompactTerminalFailureBody(bodyText, sessionId, operationId, requestHash, errorPrefix) {
1294
+ let value;
1295
+ try {
1296
+ value = JSON.parse(bodyText);
1297
+ }
1298
+ catch {
1299
+ return undefined;
1300
+ }
1301
+ if (!isObject(value) || value.operationDisposition === undefined)
1302
+ return undefined;
1303
+ return parsePiServerCompactTerminalPayload(value, sessionId, operationId, requestHash, errorPrefix);
1304
+ }
1305
+ async function acknowledgeObservedPiServerCompactTerminal(pending, compactStatePath, signal) {
1306
+ const observation = pending.observation;
1307
+ if (!observation || observation.kind !== "terminal") {
1308
+ throw new Error("Cannot acknowledge pi-server compaction failure without a durable terminal observation");
1309
+ }
1310
+ if (observation.operationDisposition === "not_started") {
1311
+ acknowledgePiServerPendingCompact(compactStatePath, pending.operationId, "server_rejected_not_started");
1312
+ return;
1313
+ }
1314
+ const ackSignal = signal?.aborted ? AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS) : signal;
1315
+ const request = createPiServerRequest(ackSignal);
1316
+ const response = await request.postJson("/api/session/compact/ack", {
1317
+ sessionId: pending.sessionId,
1318
+ operationId: pending.operationId,
1319
+ requestHash: pending.requestHash,
1320
+ });
1321
+ if (response.status === 404) {
1322
+ const contentType = getResponseContentType(response).toLowerCase();
1323
+ const failure = await readPiServerFailure(response);
1324
+ if (!contentType.includes("application/json") || !/compaction operation not found/i.test(failure.matchText)) {
1325
+ throw new Error(`Session compaction acknowledgement failed (${failure.details})`);
1326
+ }
1327
+ acknowledgePiServerPendingCompact(compactStatePath, pending.operationId, "server_missing_after_terminal_failure");
1328
+ return;
1329
+ }
1330
+ const value = await readPiServerJson(response, "Session compaction acknowledgement failed");
1331
+ if (!isObject(value) ||
1332
+ value.acknowledged !== true ||
1333
+ value.sessionId !== pending.sessionId ||
1334
+ value.operationId !== pending.operationId ||
1335
+ value.requestHash !== pending.requestHash ||
1336
+ value.status !== "failed" ||
1337
+ !isNonNegativeInteger(value.acknowledgedAt)) {
1338
+ throw new Error("Session compaction acknowledgement failed (terminal response identity was invalid)");
1339
+ }
1340
+ acknowledgePiServerPendingCompact(compactStatePath, pending.operationId);
1341
+ }
1342
+ async function persistAndCompletePiServerCompactTerminal(payload, compactStatePath, signal) {
1343
+ await persistAndAcknowledgePiServerCompactTerminal(payload, compactStatePath, signal);
1344
+ throw new PiServerCompactOperationError(`Server compaction failed (${payload.error})`, payload);
1345
+ }
1346
+ async function persistAndAcknowledgePiServerCompactTerminal(payload, compactStatePath, signal) {
1347
+ if (!compactStatePath) {
1348
+ throw new Error("Cannot complete pi-server compaction failure without a durable compact-state path");
1349
+ }
1350
+ writePiServerTerminalCompact(compactStatePath, {
1351
+ operationId: payload.operationId,
1352
+ requestHash: payload.requestHash,
1353
+ httpStatus: payload.httpStatus,
1354
+ error: payload.error,
1355
+ operationDisposition: payload.operationDisposition,
1356
+ status: payload.status,
1357
+ });
1358
+ const pending = readPiServerPendingCompact(compactStatePath);
1359
+ if (!pending) {
1360
+ throw new Error("Pi-server compact terminal observation disappeared before acknowledgement");
1361
+ }
1362
+ await acknowledgeObservedPiServerCompactTerminal(pending, compactStatePath, signal);
1363
+ }
1364
+ export async function compactPiServer(model, context, options) {
1365
+ const sessionId = options?.sessionId ?? "default";
1366
+ const signal = options?.signal;
1367
+ const recoveryWindowMs = getPiServerCompactRecoveryWindowMs(options?.piServerRecoveryWindowMs);
1368
+ const request = createPiServerRequest(options?.signal);
1369
+ const tree = options?.sessionTree ?? getLinearTreeFromMessages(context.messages);
1370
+ const compactStatePath = options?.piServerCompactStatePath;
1371
+ if (!compactStatePath) {
1372
+ throw new Error("Durable pi-server compaction requires a compact-state path");
1373
+ }
1374
+ const recovered = await recoverPendingPiServerCompaction(sessionId, tree, compactStatePath, {
1375
+ signal,
1376
+ recoveryWindowMs,
1377
+ });
1378
+ if (recovered)
1379
+ return recovered;
1380
+ const operationId = randomUUID();
1381
+ let operationSubmissionStarted = false;
1382
+ let requestHash;
1383
+ let cancelPromise;
1384
+ const cancelOperation = () => {
1385
+ if (!operationSubmissionStarted || !requestHash)
1386
+ return Promise.resolve(undefined);
1387
+ cancelPromise ??= cancelPiServerCompact(sessionId, operationId, requestHash);
1388
+ void cancelPromise.catch(() => undefined);
1389
+ return cancelPromise;
1390
+ };
1391
+ const onAbort = () => {
1392
+ void cancelOperation();
1393
+ };
1394
+ signal?.addEventListener("abort", onAbort, { once: true });
1395
+ try {
1396
+ await ensureSessionInit(sessionId, context, request);
1397
+ await syncPiServerTreeWithRequest(sessionId, tree, request, options?.onHistoryReconciled);
1398
+ const base = {
1399
+ staticContextHash: sessionStaticContextHashes.get(sessionId) ?? hashStaticContext(context),
1400
+ treeHash: sessionTreeHashes.get(sessionId) ?? hashEntries(tree.entries),
1401
+ entryCount: sessionTreeEntryCounts.get(sessionId) ?? tree.entries.length,
1402
+ leafId: sessionTreeLeafIds.get(sessionId) ?? tree.leafId,
1403
+ revision: sessionTreeRevisions.get(sessionId) ?? 0,
1404
+ };
1405
+ if (base.entryCount !== tree.entries.length || base.leafId !== tree.leafId) {
1406
+ throw new Error("Cannot begin pi-server compaction from an unverified local/server tree identity");
1407
+ }
1408
+ const body = Object.freeze(JSON.parse(JSON.stringify({
1409
+ protocolVersion: PI_SERVER_PROTOCOL_VERSION,
1410
+ sessionId,
1411
+ operationId,
1412
+ model,
1413
+ options: serializeOptions(options),
1414
+ settings: options?.settings,
1415
+ preparation: options?.preparation,
1416
+ extensionCompaction: options?.extensionCompaction,
1417
+ customInstructions: options?.customInstructions,
1418
+ retry: options?.retry,
1419
+ baseStaticContextHash: base.staticContextHash,
1420
+ baseTreeHash: base.treeHash,
1421
+ baseEntryCount: base.entryCount,
1422
+ baseLeafId: base.leafId,
1423
+ baseRevision: base.revision,
1424
+ streamResponse: true,
1425
+ })));
1426
+ const compactRequestHash = hashPiServerCompactRequest(body);
1427
+ requestHash = compactRequestHash;
1428
+ writePiServerPendingCompact(compactStatePath, {
1429
+ serverHash: hashPiServerIdentity(getServerUrl()),
1430
+ sessionId,
1431
+ operationId,
1432
+ requestHash: compactRequestHash,
1433
+ baseStaticContextHash: base.staticContextHash,
1434
+ baseTreeHash: base.treeHash,
1435
+ baseEntryCount: base.entryCount,
1436
+ baseLeafId: base.leafId,
1437
+ baseRevision: base.revision,
1438
+ });
1439
+ let recoveryDeadline;
1440
+ let reconnectAttempt = 0;
1441
+ let lastInterruption;
1442
+ for (;;) {
1443
+ if (signal?.aborted) {
1444
+ throw createPiServerCompactAbortError();
1445
+ }
1446
+ if (recoveryDeadline !== undefined) {
1447
+ const remainingMs = recoveryDeadline - Date.now();
1448
+ if (remainingMs <= 0) {
1449
+ const details = lastInterruption instanceof Error ? lastInterruption.message : String(lastInterruption);
1450
+ throw new Error(`Server compaction recovery exhausted for operation ${operationId}: ${details}`);
1451
+ }
1452
+ const delayMs = Math.min(getPiServerCompactReconnectDelay(reconnectAttempt), remainingMs);
1453
+ reconnectAttempt++;
1454
+ await waitForPiServerCompactReconnect(delayMs, signal);
1455
+ if (Date.now() >= recoveryDeadline) {
1456
+ const details = lastInterruption instanceof Error ? lastInterruption.message : String(lastInterruption);
1457
+ throw new Error(`Server compaction recovery exhausted for operation ${operationId}: ${details}`);
1458
+ }
1459
+ }
1460
+ let response;
1461
+ try {
1462
+ operationSubmissionStarted = true;
1463
+ response = await request.postJson("/api/session/compact", body);
1464
+ }
1465
+ catch (error) {
1466
+ if (isAbortError(error, signal)) {
1467
+ throw error;
1468
+ }
1469
+ if (!isRetryablePiServerInterruption(error)) {
1470
+ throw error;
1471
+ }
1472
+ lastInterruption = error;
1473
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1474
+ continue;
1475
+ }
1476
+ if (!response.ok) {
1477
+ let failure;
1478
+ try {
1479
+ failure = await readPiServerFailure(response);
1480
+ }
1481
+ catch (error) {
1482
+ if (isAbortError(error, signal)) {
1483
+ throw error;
1484
+ }
1485
+ if (!isRecoverablePiServerCompactResponse(response) || !isRetryablePiServerInterruption(error)) {
1486
+ throw error;
1487
+ }
1488
+ lastInterruption = error;
1489
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1490
+ continue;
1491
+ }
1492
+ const terminal = parseCompactTerminalFailureBody(failure.bodyText, sessionId, operationId, compactRequestHash, "Server compaction failed");
1493
+ if (terminal) {
1494
+ if (terminal.httpStatus !== response.status) {
1495
+ throw new PiServerCompactProtocolError("Server compaction failed (terminal HTTP status did not match the response)");
1496
+ }
1497
+ return await persistAndCompletePiServerCompactTerminal(terminal, compactStatePath, signal);
1498
+ }
1499
+ if (isRecoverablePiServerCompactResponse(response)) {
1500
+ lastInterruption = new Error(`Server compaction failed (${failure.details})`);
1501
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1502
+ continue;
1503
+ }
1504
+ throw new Error(`Server compaction failed (${failure.details})`);
1505
+ }
1506
+ try {
1507
+ const responseResult = await readPiServerCompactResponse(response, "Server compaction failed");
1508
+ const result = normalizePiServerCompactionResult(responseResult, sessionId, operationId, compactRequestHash, tree, base, compactStatePath);
1509
+ if (signal?.aborted) {
1510
+ throw createPiServerCompactAbortError();
1511
+ }
1512
+ markTreeSynced(sessionId, { entries: result.entries, leafId: result.leafId });
1513
+ sessionTreeRevisions.set(sessionId, result.revision);
1514
+ return result;
1515
+ }
1516
+ catch (error) {
1517
+ if (error instanceof PiServerCompactOperationError) {
1518
+ const terminal = parsePiServerCompactTerminalPayload(error.payload, sessionId, operationId, compactRequestHash, "Server compaction failed");
1519
+ return await persistAndCompletePiServerCompactTerminal(terminal, compactStatePath, signal);
1520
+ }
1521
+ if (error instanceof PiServerCompactProtocolError) {
1522
+ throw error;
1523
+ }
1524
+ if (isAbortError(error, signal)) {
1525
+ throw error;
1526
+ }
1527
+ if (!isRetryablePiServerInterruption(error)) {
1528
+ throw error;
1529
+ }
1530
+ lastInterruption = error;
1531
+ if (error instanceof PiServerCompactInterruptionError && error.observedProgress) {
1532
+ recoveryDeadline = Date.now() + recoveryWindowMs;
1533
+ reconnectAttempt = 0;
1534
+ }
1535
+ else {
1536
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1537
+ }
1538
+ }
1539
+ }
1540
+ }
1541
+ catch (error) {
1542
+ if (isAbortError(error, signal)) {
1543
+ let cancelFailure;
1544
+ try {
1545
+ if (!operationSubmissionStarted && requestHash) {
1546
+ await persistAndAcknowledgePiServerCompactTerminal({
1547
+ protocolVersion: PI_SERVER_PROTOCOL_VERSION,
1548
+ sessionId,
1549
+ operationId,
1550
+ requestHash,
1551
+ status: "rejected",
1552
+ httpStatus: 499,
1553
+ operationDisposition: "not_started",
1554
+ error: "Compaction cancelled before server submission",
1555
+ }, compactStatePath);
1556
+ }
1557
+ else {
1558
+ const terminal = await cancelOperation();
1559
+ if (terminal) {
1560
+ await persistAndAcknowledgePiServerCompactTerminal(terminal, compactStatePath, AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS));
1561
+ }
1562
+ }
1563
+ }
1564
+ catch (cancelError) {
1565
+ cancelFailure = cancelError;
1566
+ }
1567
+ throw createPiServerCompactAbortError(cancelFailure);
1568
+ }
1569
+ throw error;
1570
+ }
1571
+ finally {
1572
+ signal?.removeEventListener("abort", onAbort);
1573
+ }
1574
+ }
1575
+ function assertPendingPiServerCompactMatches(pending, sessionId, tree) {
1576
+ if (pending.serverHash !== hashPiServerIdentity(getServerUrl()) ||
1577
+ pending.sessionId !== sessionId ||
1578
+ pending.baseEntryCount > tree.entries.length) {
1579
+ throw new Error(`Cannot safely resume pending pi-server compaction ${pending.operationId}: server, session, or tree identity changed`);
1580
+ }
1581
+ const baseEntries = tree.entries.slice(0, pending.baseEntryCount);
1582
+ if (hashEntries(baseEntries) !== pending.baseTreeHash ||
1583
+ (pending.baseLeafId !== null && !baseEntries.some((entry) => entry.id === pending.baseLeafId))) {
1584
+ throw new Error(`Cannot safely resume pending pi-server compaction ${pending.operationId}: local base tree no longer matches its durable marker`);
1585
+ }
1586
+ if (!pending.observation || pending.observation.kind === "terminal") {
1587
+ if (tree.entries.length !== pending.baseEntryCount || tree.leafId !== pending.baseLeafId) {
1588
+ throw new Error(`Cannot safely resume pending pi-server compaction ${pending.operationId}: local tree changed before a committed compaction was observed`);
1589
+ }
1590
+ }
1591
+ return { entries: baseEntries, leafId: pending.baseLeafId };
1592
+ }
1593
+ function recoverLocallyAppliedPiServerCompaction(pending, tree, baseTree, compactStatePath) {
1594
+ const applied = pending.observation;
1595
+ if (!applied || applied.kind !== "applied") {
1596
+ throw new Error("Pending pi-server compaction does not contain a durable local apply observation");
1597
+ }
1598
+ const entry = tree.entries[pending.baseEntryCount];
1599
+ if (tree.entries.length !== pending.baseEntryCount + 1 ||
1600
+ !entry ||
1601
+ entry.id !== applied.entryId ||
1602
+ hashPiServerTreeEntry(entry) !== applied.entryHash ||
1603
+ tree.leafId !== applied.updatedLeafId ||
1604
+ applied.updatedLeafId !== applied.entryId ||
1605
+ hashEntries(tree.entries) !== applied.updatedTreeHash ||
1606
+ applied.updatedRevision !== pending.baseRevision + 1) {
1607
+ throw new Error(`Cannot safely resume pending pi-server compaction ${pending.operationId}: local applied tree does not match its durable observation`);
1608
+ }
1609
+ return normalizePiServerCompactionResult({
1610
+ protocolVersion: PI_SERVER_PROTOCOL_VERSION,
1611
+ sessionId: pending.sessionId,
1612
+ operationId: pending.operationId,
1613
+ requestHash: pending.requestHash,
1614
+ treePatch: {
1615
+ baseStaticContextHash: pending.baseStaticContextHash,
1616
+ baseTreeHash: pending.baseTreeHash,
1617
+ baseEntryCount: pending.baseEntryCount,
1618
+ baseLeafId: pending.baseLeafId,
1619
+ baseRevision: pending.baseRevision,
1620
+ entriesFrom: pending.baseEntryCount,
1621
+ entries: [entry],
1622
+ leafId: applied.updatedLeafId,
1623
+ revision: applied.updatedRevision,
1624
+ treeHash: applied.updatedTreeHash,
1625
+ },
1626
+ }, pending.sessionId, pending.operationId, pending.requestHash, baseTree, {
1627
+ staticContextHash: pending.baseStaticContextHash,
1628
+ treeHash: pending.baseTreeHash,
1629
+ entryCount: pending.baseEntryCount,
1630
+ leafId: pending.baseLeafId,
1631
+ revision: pending.baseRevision,
1632
+ }, compactStatePath);
1633
+ }
1634
+ export async function recoverPendingPiServerCompaction(sessionId, tree, compactStatePath, options) {
1635
+ const pending = readPiServerPendingCompact(compactStatePath);
1636
+ if (!pending)
1637
+ return undefined;
1638
+ const baseTree = assertPendingPiServerCompactMatches(pending, sessionId, tree);
1639
+ const locallyApplied = pending.observation?.kind === "applied"
1640
+ ? recoverLocallyAppliedPiServerCompaction(pending, tree, baseTree, compactStatePath)
1641
+ : undefined;
1642
+ if (pending.observation?.kind === "terminal" && pending.observation.operationDisposition === "not_started") {
1643
+ return await persistAndCompletePiServerCompactTerminal({
1644
+ protocolVersion: PI_SERVER_PROTOCOL_VERSION,
1645
+ sessionId: pending.sessionId,
1646
+ operationId: pending.operationId,
1647
+ requestHash: pending.requestHash,
1648
+ status: pending.observation.status,
1649
+ httpStatus: pending.observation.httpStatus,
1650
+ operationDisposition: pending.observation.operationDisposition,
1651
+ error: pending.observation.error,
1652
+ }, compactStatePath, options?.signal);
1653
+ }
1654
+ const recoveryWindowMs = getPiServerCompactRecoveryWindowMs(options?.recoveryWindowMs);
1655
+ const request = createPiServerRequest(options?.signal);
1656
+ let recoveryDeadline;
1657
+ let reconnectAttempt = 0;
1658
+ let lastInterruption = new Error("pending compaction has not reached a terminal response");
1659
+ for (;;) {
1660
+ if (options?.signal?.aborted) {
1661
+ let cancelFailure;
1662
+ try {
1663
+ const terminal = await cancelPiServerCompact(sessionId, pending.operationId, pending.requestHash);
1664
+ await persistAndAcknowledgePiServerCompactTerminal(terminal, compactStatePath, AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS));
1665
+ }
1666
+ catch (error) {
1667
+ cancelFailure = error;
1668
+ }
1669
+ throw createPiServerCompactAbortError(cancelFailure);
1670
+ }
1671
+ if (recoveryDeadline !== undefined) {
1672
+ const remainingMs = recoveryDeadline - Date.now();
1673
+ if (remainingMs <= 0) {
1674
+ throw new Error(`Pending server compaction recovery exhausted for operation ${pending.operationId}: ${lastInterruption instanceof Error ? lastInterruption.message : String(lastInterruption)}`);
1675
+ }
1676
+ await waitForPiServerCompactReconnect(Math.min(getPiServerCompactReconnectDelay(reconnectAttempt), remainingMs), options?.signal);
1677
+ reconnectAttempt++;
1678
+ }
1679
+ let response;
1680
+ try {
1681
+ response = await request.getJson(`/api/session/${encodeURIComponent(sessionId)}/compactions/${encodeURIComponent(pending.operationId)}?requestHash=${encodeURIComponent(pending.requestHash)}`);
1682
+ }
1683
+ catch (error) {
1684
+ if (!isRetryablePiServerInterruption(error))
1685
+ throw error;
1686
+ lastInterruption = error;
1687
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1688
+ continue;
1689
+ }
1690
+ if (!response.ok) {
1691
+ const failure = await readPiServerFailure(response);
1692
+ if (response.status === 404) {
1693
+ if (locallyApplied)
1694
+ return locallyApplied;
1695
+ if (pending.observation?.kind === "terminal") {
1696
+ await acknowledgeObservedPiServerCompactTerminal(pending, compactStatePath, options?.signal);
1697
+ throw new PiServerCompactOperationError(`Pending server compaction failed (${pending.observation.error})`, pending.observation);
1698
+ }
1699
+ throw new Error(`Pending pi-server compaction ${pending.operationId} is missing from the durable server journal; ` +
1700
+ "the local marker was retained and automatic provider resubmission is disabled");
1701
+ }
1702
+ if (!isRecoverablePiServerCompactResponse(response)) {
1703
+ throw new Error(`Pending server compaction recovery failed (${failure.details})`);
1704
+ }
1705
+ lastInterruption = new Error(`Pending server compaction recovery failed (${failure.details})`);
1706
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1707
+ continue;
1708
+ }
1709
+ try {
1710
+ const responseResult = await readPiServerCompactResponse(response, "Pending server compaction recovery failed");
1711
+ const result = normalizePiServerCompactionResult(responseResult, sessionId, pending.operationId, pending.requestHash, baseTree, {
1712
+ staticContextHash: pending.baseStaticContextHash,
1713
+ treeHash: pending.baseTreeHash,
1714
+ entryCount: pending.baseEntryCount,
1715
+ leafId: pending.baseLeafId,
1716
+ revision: pending.baseRevision,
1717
+ }, compactStatePath);
1718
+ if (locallyApplied &&
1719
+ (canonicalJsonStringify(result.compactionEntry) !==
1720
+ canonicalJsonStringify(locallyApplied.compactionEntry) ||
1721
+ result.treeHash !== locallyApplied.treeHash ||
1722
+ result.revision !== locallyApplied.revision)) {
1723
+ throw new PiServerCompactProtocolError("Pending server compaction recovery returned a result that differed from the durably applied tree");
1724
+ }
1725
+ markTreeSynced(sessionId, { entries: result.entries, leafId: result.leafId });
1726
+ sessionTreeRevisions.set(sessionId, result.revision);
1727
+ return result;
1728
+ }
1729
+ catch (error) {
1730
+ if (error instanceof PiServerCompactOperationError) {
1731
+ const terminal = parsePiServerCompactTerminalPayload(error.payload, sessionId, pending.operationId, pending.requestHash, "Pending server compaction recovery failed");
1732
+ return await persistAndCompletePiServerCompactTerminal(terminal, compactStatePath, options?.signal);
1733
+ }
1734
+ if (error instanceof PiServerCompactProtocolError) {
1735
+ throw error;
1736
+ }
1737
+ if (!isRetryablePiServerInterruption(error))
1738
+ throw error;
1739
+ lastInterruption = error;
1740
+ if (error instanceof PiServerCompactInterruptionError && error.observedProgress) {
1741
+ recoveryDeadline = Date.now() + recoveryWindowMs;
1742
+ reconnectAttempt = 0;
1743
+ }
1744
+ else {
1745
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
1746
+ }
1747
+ }
1748
+ }
1749
+ }
1750
+ export function recordPiServerCompactionApplied(result) {
1751
+ const compactStatePath = result.compactStatePath;
1752
+ if (!compactStatePath) {
1753
+ throw new Error("Cannot record pi-server compaction apply without a durable compact-state path");
1754
+ }
1755
+ const pending = readPiServerPendingCompact(compactStatePath);
1756
+ if (!pending ||
1757
+ pending.sessionId !== result.sessionId ||
1758
+ pending.operationId !== result.operationId ||
1759
+ pending.requestHash !== result.requestHash) {
1760
+ throw new Error("Cannot record pi-server compaction apply: durable pending marker did not match the result");
1761
+ }
1762
+ writePiServerAppliedCompact(compactStatePath, {
1763
+ operationId: result.operationId,
1764
+ requestHash: result.requestHash,
1765
+ entryId: result.compactionEntry.id,
1766
+ entryHash: hashPiServerTreeEntry(result.compactionEntry),
1767
+ updatedTreeHash: result.treeHash,
1768
+ updatedLeafId: result.compactionEntry.id,
1769
+ updatedRevision: result.revision,
1770
+ });
1771
+ }
1772
+ export async function acknowledgePiServerCompaction(result, signal) {
1773
+ const compactStatePath = result.compactStatePath;
1774
+ if (!compactStatePath) {
1775
+ throw new Error("Cannot acknowledge pi-server compaction without a durable compact-state path");
1776
+ }
1777
+ const pending = readPiServerPendingCompact(compactStatePath);
1778
+ if (!pending ||
1779
+ pending.sessionId !== result.sessionId ||
1780
+ pending.operationId !== result.operationId ||
1781
+ pending.requestHash !== result.requestHash) {
1782
+ throw new Error("Cannot acknowledge pi-server compaction: durable pending marker did not match the result");
1783
+ }
1784
+ const applied = pending.observation;
1785
+ if (!applied ||
1786
+ applied.kind !== "applied" ||
1787
+ applied.entryId !== result.compactionEntry.id ||
1788
+ applied.entryHash !== hashPiServerTreeEntry(result.compactionEntry) ||
1789
+ applied.updatedTreeHash !== result.treeHash ||
1790
+ applied.updatedLeafId !== result.leafId ||
1791
+ applied.updatedRevision !== result.revision ||
1792
+ result.entries.length !== pending.baseEntryCount + 1 ||
1793
+ hashEntries(result.entries) !== result.treeHash) {
1794
+ throw new Error("Cannot acknowledge pi-server compaction: durable local apply observation did not match the result");
1795
+ }
1796
+ const request = createPiServerRequest(signal);
1797
+ const response = await request.postJson("/api/session/compact/ack", {
1798
+ sessionId: pending.sessionId,
1799
+ operationId: pending.operationId,
1800
+ requestHash: pending.requestHash,
1801
+ });
1802
+ if (response.status === 404) {
1803
+ const contentType = getResponseContentType(response).toLowerCase();
1804
+ const failure = await readPiServerFailure(response);
1805
+ if (!contentType.includes("application/json") || !/compaction operation not found/i.test(failure.matchText)) {
1806
+ throw new Error(`Session compaction acknowledgement failed (${failure.details})`);
1807
+ }
1808
+ acknowledgePiServerPendingCompact(compactStatePath, pending.operationId, "server_missing_after_tree_applied");
1809
+ return;
1810
+ }
1811
+ const value = await readPiServerJson(response, "Session compaction acknowledgement failed");
1812
+ if (!isObject(value) ||
1813
+ value.acknowledged !== true ||
1814
+ value.sessionId !== pending.sessionId ||
1815
+ value.operationId !== pending.operationId ||
1816
+ value.requestHash !== pending.requestHash ||
1817
+ value.status !== "completed" ||
1818
+ !isNonNegativeInteger(value.acknowledgedAt)) {
1819
+ throw new Error("Session compaction acknowledgement failed (response identity was invalid)");
1820
+ }
1821
+ acknowledgePiServerPendingCompact(compactStatePath, pending.operationId);
1822
+ }
1823
+ export async function dropLastPiServerAssistantError(sessionId) {
1824
+ const request = createPiServerRequest();
1825
+ const response = await request.postJson("/api/session/drop-last-assistant-error", { sessionId });
1826
+ await readPiServerJson(response, "Dropping server assistant error failed");
1827
+ }
1828
+ function findPendingPiServerRunTerminal(entries, pending) {
1829
+ for (let index = entries.length - 1; index >= 0; index--) {
1830
+ const entry = entries[index];
1831
+ if (entry.type !== "message" || entry.message.role !== "assistant")
1832
+ continue;
1833
+ const message = entry.message;
1834
+ const diagnostic = getPiServerRunDiagnostic(message);
1835
+ if (!diagnostic || diagnostic.runId !== pending.runId)
1836
+ continue;
1837
+ if (diagnostic.sessionId !== pending.sessionId || diagnostic.requestMac !== pending.requestHash) {
1838
+ throw new Error("Cannot recover pi-server run: persisted terminal diagnostic did not match pending state");
1839
+ }
1840
+ return message;
1841
+ }
1842
+ return undefined;
1843
+ }
1844
+ function assertPendingPiServerRunMatches(pending, serverHash, sessionId, tree) {
1845
+ if (pending.serverHash !== serverHash ||
1846
+ pending.sessionId !== sessionId ||
1847
+ pending.baseEntryCount > tree.entries.length ||
1848
+ hashLocalTreePrefix(sessionId, tree.entries, pending.baseEntryCount) !== pending.baseTreeHash ||
1849
+ pending.baseLeafId !== tree.leafId) {
1850
+ throw new Error(`Cannot safely resume pending pi-server run ${pending.runId}: server, tree, or provider request identity changed`);
1851
+ }
1852
+ }
1853
+ function missingDurablePiServerRunError(runId, cause) {
1854
+ const prefix = cause ? `${cause}; ` : "";
1855
+ return new Error(`${prefix}pi-server no longer has durable journal for pending run ${runId}. ` +
1856
+ "The provider outcome is unknown and the pending marker was retained. Restore the server journal, " +
1857
+ "or after independently verifying that the provider did not execute, start an explicit new session or fork; " +
1858
+ "automatic provider resubmission is disabled.");
1859
+ }
1860
+ export async function streamPiServer(model, context, options) {
1861
+ const runMode = options?.runMode;
1862
+ if (runMode === "auxiliary-transient" && options?.piServerRunStatePath) {
1863
+ throw new Error("Auxiliary pi-server streams cannot use the main durable run-state path");
1864
+ }
1865
+ const sessionId = options?.sessionId ?? randomUUID();
1866
+ let runId = randomUUID();
1867
+ const recoveryWindowMs = getPiServerStreamRecoveryWindowMs(options?.piServerRecoveryWindowMs);
1868
+ const isEphemeralSession = runMode === "auxiliary-transient" || options?.sessionId === undefined;
1869
+ const stream = new PiServerEventStream();
1870
+ const partial = {
1871
+ role: "assistant",
1872
+ stopReason: "stop",
1873
+ content: [],
1874
+ api: model.api,
1875
+ provider: model.provider,
1876
+ model: model.id,
1877
+ usage: {
1878
+ input: 0,
1879
+ output: 0,
1880
+ cacheRead: 0,
1881
+ cacheWrite: 0,
1882
+ totalTokens: 0,
1883
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
1884
+ },
1885
+ timestamp: Date.now(),
539
1886
  };
540
1887
  (async () => {
541
1888
  let phase = "session_init";
1889
+ let receivedProxyEventCount = 0;
1890
+ let runSubmissionStarted = false;
1891
+ let unresolvedRunPending = false;
1892
+ let cancelFailure;
1893
+ let cancelPromise;
1894
+ let transientRunAcknowledged = false;
1895
+ let pendingTransientTerminalEvent;
1896
+ const runStatePath = options?.piServerRunStatePath;
1897
+ let runStateLease;
1898
+ const attachRunStateLease = () => {
1899
+ if (!runStatePath)
1900
+ return;
1901
+ const active = getPersistentRunLease(sessionId, runStatePath);
1902
+ if (active) {
1903
+ if (active.scope !== "session") {
1904
+ throw new Error(`Cannot start another pi-server run while session ${sessionId} is already streaming`);
1905
+ }
1906
+ runStateLease = active.lease;
1907
+ return;
1908
+ }
1909
+ runStateLease = acquirePersistentRunLease(sessionId, runStatePath);
1910
+ };
1911
+ const releaseRunStateLease = () => {
1912
+ if (!runStatePath || !runStateLease)
1913
+ return;
1914
+ releasePiServerRunLease(sessionId, runStatePath);
1915
+ runStateLease = undefined;
1916
+ };
1917
+ const requestRunCancel = () => {
1918
+ if (!runSubmissionStarted)
1919
+ return Promise.resolve();
1920
+ cancelPromise ??= cancelPiServerRun(sessionId, runId).catch((error) => {
1921
+ cancelFailure = error;
1922
+ });
1923
+ return cancelPromise;
1924
+ };
1925
+ const onAbort = () => {
1926
+ void requestRunCancel();
1927
+ };
1928
+ options?.signal?.addEventListener("abort", onAbort, { once: true });
1929
+ const endStream = () => {
1930
+ options?.signal?.removeEventListener("abort", onAbort);
1931
+ if (isEphemeralSession) {
1932
+ resetSessionTracking(sessionId);
1933
+ }
1934
+ stream.end();
1935
+ };
1936
+ const validateTerminalPendingRun = () => {
1937
+ const diagnostic = getPiServerRunDiagnostic(partial);
1938
+ const pending = runStatePath
1939
+ ? readPiServerPendingRun(runStatePath, runStateLease)
1940
+ : sessionPendingRuns.get(sessionId);
1941
+ if (!diagnostic ||
1942
+ !pending ||
1943
+ diagnostic.sessionId !== sessionId ||
1944
+ diagnostic.sessionId !== pending.sessionId ||
1945
+ diagnostic.runId !== pending.runId ||
1946
+ diagnostic.requestMac !== pending.requestHash) {
1947
+ throw new PiServerStreamProtocolError("Pi-server terminal run diagnostic did not match the pending run");
1948
+ }
1949
+ return runStatePath !== undefined && runStateLease !== undefined;
1950
+ };
1951
+ const getDurableRunEvents = (request, eventCursor) => request.getJson(`/api/session/${encodeURIComponent(sessionId)}/runs/${encodeURIComponent(runId)}/events?from=${eventCursor}`);
1952
+ const acknowledgeTransientTerminal = async (event) => {
1953
+ const message = event.type === "done" ? event.message : event.error;
1954
+ if (!transientRunAcknowledged) {
1955
+ validateTerminalPendingRun();
1956
+ await acknowledgePiServerRunMessage(sessionId, message, undefined, options?.signal);
1957
+ if (sessionPendingRuns.has(sessionId)) {
1958
+ throw new PiServerStreamProtocolError("Auxiliary pi-server terminal acknowledgement did not clear transient pending state");
1959
+ }
1960
+ transientRunAcknowledged = true;
1961
+ }
1962
+ const cleanupSignal = options?.signal?.aborted
1963
+ ? AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS)
1964
+ : options?.signal;
1965
+ const cleanupRequest = createPiServerRequest(cleanupSignal);
1966
+ const cleanupResponse = await cleanupRequest.deleteJson(`/api/session/${encodeURIComponent(sessionId)}`);
1967
+ const cleanupResult = await readPiServerJson(cleanupResponse, "Auxiliary pi-server session cleanup failed");
1968
+ if (!isObject(cleanupResult) || cleanupResult.deleted !== sessionId) {
1969
+ throw new PiServerStreamProtocolError("Auxiliary pi-server session cleanup response did not match the transient session");
1970
+ }
1971
+ unresolvedRunPending = false;
1972
+ pendingTransientTerminalEvent = undefined;
1973
+ stream.push(event);
1974
+ };
1975
+ const settleStreamFailure = async (failure, retryable = phase === "provider_stream") => {
1976
+ const reason = options?.signal?.aborted ? "aborted" : "error";
1977
+ if (reason === "aborted") {
1978
+ await requestRunCancel();
1979
+ if (cancelFailure === undefined && runSubmissionStarted) {
1980
+ try {
1981
+ const recoveryRequest = createPiServerRequest(AbortSignal.timeout(PI_SERVER_CANCEL_TIMEOUT_MS));
1982
+ const response = await getDurableRunEvents(recoveryRequest, receivedProxyEventCount);
1983
+ if (response.ok) {
1984
+ const recovered = await readStreamResponse(response, receivedProxyEventCount, true);
1985
+ if (recovered.terminal) {
1986
+ endStream();
1987
+ return;
1988
+ }
1989
+ cancelFailure = recovered.failure;
1990
+ }
1991
+ else {
1992
+ const cancelRecoveryFailure = await readPiServerFailure(response);
1993
+ cancelFailure = new Error(`pi-server canceled run replay failed: ${cancelRecoveryFailure.details}`);
1994
+ }
1995
+ }
1996
+ catch (error) {
1997
+ cancelFailure = error;
1998
+ }
1999
+ }
2000
+ }
2001
+ const initialErrorMessage = failure instanceof Error ? failure.message : String(failure);
2002
+ const errorMessage = cancelFailure === undefined
2003
+ ? initialErrorMessage
2004
+ : `${initialErrorMessage}; pi-server run cancel failed: ${cancelFailure instanceof Error ? cancelFailure.message : String(cancelFailure)}`;
2005
+ partial.stopReason = reason;
2006
+ partial.errorMessage = errorMessage;
2007
+ partial.diagnostics = partial.diagnostics?.filter((diagnostic) => diagnostic.type !== "pi_server_run");
2008
+ partial.diagnostics = [
2009
+ ...(partial.diagnostics ?? []),
2010
+ {
2011
+ type: "pi_server_failure",
2012
+ timestamp: Date.now(),
2013
+ error: { name: failure instanceof Error ? failure.name : "Error", message: errorMessage },
2014
+ details: {
2015
+ phase,
2016
+ source: "pi-server",
2017
+ retryable: reason === "aborted" ? false : retryable,
2018
+ runUnresolved: unresolvedRunPending,
2019
+ },
2020
+ },
2021
+ ];
2022
+ stream.push({
2023
+ type: "error",
2024
+ reason,
2025
+ error: partial,
2026
+ });
2027
+ releaseRunStateLease();
2028
+ endStream();
2029
+ };
2030
+ const readStreamResponse = async (response, eventCursor, ignoreCallerAbort = false) => {
2031
+ let reader;
2032
+ let observedProgress = false;
2033
+ try {
2034
+ try {
2035
+ await ensurePiServerEventStream(response);
2036
+ }
2037
+ catch (error) {
2038
+ throw new PiServerStreamProtocolError(`Invalid pi-server stream response: ${error instanceof Error ? error.message : String(error)}`);
2039
+ }
2040
+ reader = response.body.getReader();
2041
+ const decoder = new TextDecoder();
2042
+ const noProgress = createPiServerBodyNoProgressState();
2043
+ const incompleteLineParts = [];
2044
+ let incompleteLineBytes = 0;
2045
+ let replayEventIndex = eventCursor;
2046
+ while (true) {
2047
+ const { done, value } = await readPiServerBodyChunk(reader, noProgress);
2048
+ if (done) {
2049
+ return {
2050
+ terminal: false,
2051
+ failure: new Error("pi-server stream ended before a terminal event"),
2052
+ observedProgress,
2053
+ recoverable: true,
2054
+ };
2055
+ }
2056
+ if (value.byteLength > 0)
2057
+ observedProgress = true;
2058
+ if (!ignoreCallerAbort && options?.signal?.aborted) {
2059
+ throw new Error("Request aborted by user");
2060
+ }
2061
+ incompleteLineBytes = updateEventStreamLineBytes(value, incompleteLineBytes);
2062
+ if (incompleteLineBytes < 0) {
2063
+ throw new PiServerStreamProtocolError(`Invalid pi-server stream response: event line exceeded ${PI_SERVER_EVENT_STREAM_MAX_LINE_BYTES} bytes`);
2064
+ }
2065
+ const lines = extractDecodedEventStreamLines(decoder.decode(value, { stream: true }), incompleteLineParts);
2066
+ for (const rawLine of lines) {
2067
+ const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
2068
+ if (!line.startsWith("data: "))
2069
+ continue;
2070
+ const data = line.slice(6).trim();
2071
+ if (!data)
2072
+ continue;
2073
+ const eventIndex = replayEventIndex;
2074
+ replayEventIndex++;
2075
+ if (eventIndex < receivedProxyEventCount)
2076
+ continue;
2077
+ let proxyEvent;
2078
+ try {
2079
+ proxyEvent = JSON.parse(data);
2080
+ }
2081
+ catch {
2082
+ throw new PiServerStreamProtocolError(`Invalid pi-server stream event JSON: ${getBodyExcerpt(data)}`);
2083
+ }
2084
+ let event;
2085
+ try {
2086
+ event = processProxyEvent(proxyEvent, partial);
2087
+ }
2088
+ catch (error) {
2089
+ throw new PiServerStreamProtocolError(`Invalid pi-server stream event: ${error instanceof Error ? error.message : String(error)}`);
2090
+ }
2091
+ receivedProxyEventCount++;
2092
+ if (!event)
2093
+ continue;
2094
+ if (event.type === "done" || event.type === "error") {
2095
+ if (runMode === "auxiliary-transient") {
2096
+ pendingTransientTerminalEvent = event;
2097
+ try {
2098
+ await acknowledgeTransientTerminal(event);
2099
+ }
2100
+ catch (error) {
2101
+ return {
2102
+ terminal: false,
2103
+ failure: error,
2104
+ observedProgress: true,
2105
+ recoverable: isRetryablePiServerInterruption(error),
2106
+ };
2107
+ }
2108
+ }
2109
+ else {
2110
+ const retainLease = runMode === "main-durable" || runStatePath !== undefined
2111
+ ? validateTerminalPendingRun()
2112
+ : false;
2113
+ if (!retainLease) {
2114
+ releaseRunStateLease();
2115
+ }
2116
+ stream.push(event);
2117
+ }
2118
+ void reader.cancel().catch(() => undefined);
2119
+ return { terminal: true };
2120
+ }
2121
+ stream.push(event);
2122
+ }
2123
+ }
2124
+ }
2125
+ catch (error) {
2126
+ void reader?.cancel().catch(() => undefined);
2127
+ return {
2128
+ terminal: false,
2129
+ failure: error,
2130
+ observedProgress,
2131
+ recoverable: isRetryablePiServerInterruption(error),
2132
+ };
2133
+ }
2134
+ };
542
2135
  try {
543
2136
  const request = createPiServerRequest(options?.signal);
544
2137
  await ensureSessionInit(sessionId, context, request);
@@ -547,120 +2140,286 @@ export async function streamPiServer(model, context, options) {
547
2140
  replace: true,
548
2141
  };
549
2142
  const syncTree = tree;
550
- phase = "tree_sync";
551
- await syncPiServerTreeWithRequest(sessionId, context, syncTree, request, options?.onHistoryReconciled);
552
- const makeBody = () => ({
2143
+ if (runStatePath) {
2144
+ attachRunStateLease();
2145
+ }
2146
+ let pending = runStatePath
2147
+ ? readPiServerPendingRun(runStatePath, runStateLease)
2148
+ : sessionPendingRuns.get(sessionId);
2149
+ unresolvedRunPending = pending !== undefined;
2150
+ let treeSynced = false;
2151
+ if (pending) {
2152
+ const persistedTerminal = findPendingPiServerRunTerminal(syncTree.entries, pending);
2153
+ if (persistedTerminal) {
2154
+ phase = "tree_sync";
2155
+ await syncPiServerTreeWithRequest(sessionId, syncTree, request, options?.onHistoryReconciled);
2156
+ treeSynced = true;
2157
+ await acknowledgePiServerRunMessage(sessionId, persistedTerminal, runStatePath, options?.signal);
2158
+ if (runStatePath) {
2159
+ attachRunStateLease();
2160
+ }
2161
+ pending = undefined;
2162
+ unresolvedRunPending = false;
2163
+ }
2164
+ }
2165
+ const buildRequestIdentity = (baseTreeHash, baseEntryCount, baseLeafId, baseStaticContextHash, baseRevision) => JSON.parse(JSON.stringify({
553
2166
  sessionId,
554
- runId,
2167
+ runMode,
555
2168
  model,
556
2169
  options: serializeOptions(options),
557
2170
  ephemeralMessages: options?.ephemeralMessages,
558
2171
  contextOverlay: options?.contextOverlay,
2172
+ baseTreeHash,
2173
+ baseEntryCount,
2174
+ baseLeafId,
2175
+ baseStaticContextHash,
2176
+ baseRevision,
2177
+ }));
2178
+ let baseTreeHash = pending?.baseTreeHash ?? hashLocalTree(sessionId, syncTree.entries);
2179
+ let baseEntryCount = pending?.baseEntryCount ?? syncTree.entries.length;
2180
+ let baseLeafId = pending?.baseLeafId ?? syncTree.leafId;
2181
+ let baseStaticContextHash = hashStaticContext(context);
2182
+ let baseRevision = sessionTreeRevisions.get(sessionId) ?? 0;
2183
+ let requestIdentity = buildRequestIdentity(baseTreeHash, baseEntryCount, baseLeafId, baseStaticContextHash, baseRevision);
2184
+ let requestHash = hashPiServerStreamRequest(requestIdentity);
2185
+ let resumedPendingRun = false;
2186
+ let recoveredPendingRun;
2187
+ if (pending) {
2188
+ assertPendingPiServerRunMatches(pending, hashPiServerIdentity(getServerUrl()), sessionId, syncTree);
2189
+ runId = pending.runId;
2190
+ phase = "provider_stream";
2191
+ recoveredPendingRun = await recoverPiServerRun(sessionId, runId, request, options?.signal);
2192
+ if (recoveredPendingRun.status === "not_found") {
2193
+ throw missingDurablePiServerRunError(pending.runId);
2194
+ }
2195
+ else {
2196
+ resumedPendingRun = true;
2197
+ runSubmissionStarted = true;
2198
+ requestHash = pending.requestHash;
2199
+ }
2200
+ }
2201
+ if (!pending) {
2202
+ if (!treeSynced) {
2203
+ phase = "tree_sync";
2204
+ await syncPiServerTreeWithRequest(sessionId, syncTree, request, options?.onHistoryReconciled);
2205
+ treeSynced = true;
2206
+ }
2207
+ baseTreeHash = hashLocalTree(sessionId, syncTree.entries);
2208
+ baseEntryCount = syncTree.entries.length;
2209
+ baseLeafId = syncTree.leafId;
2210
+ baseStaticContextHash = sessionStaticContextHashes.get(sessionId) ?? hashStaticContext(context);
2211
+ baseRevision = sessionTreeRevisions.get(sessionId) ?? 0;
2212
+ requestIdentity = buildRequestIdentity(baseTreeHash, baseEntryCount, baseLeafId, baseStaticContextHash, baseRevision);
2213
+ requestHash = hashPiServerStreamRequest(requestIdentity);
2214
+ const pendingInput = {
2215
+ serverHash: hashPiServerIdentity(getServerUrl()),
2216
+ sessionId,
2217
+ runId,
2218
+ baseTreeHash,
2219
+ baseEntryCount,
2220
+ baseLeafId,
2221
+ requestHash,
2222
+ };
2223
+ const newPending = runStatePath
2224
+ ? writePiServerPendingRun(runStatePath, pendingInput, runStateLease)
2225
+ : {
2226
+ version: 1,
2227
+ kind: "run",
2228
+ sequence: 0,
2229
+ timestamp: Date.now(),
2230
+ ...pendingInput,
2231
+ };
2232
+ if (!runStatePath)
2233
+ sessionPendingRuns.set(sessionId, newPending);
2234
+ unresolvedRunPending = true;
2235
+ }
2236
+ const makeBody = (eventCursor) => ({
2237
+ ...requestIdentity,
2238
+ runId,
2239
+ eventCursor,
559
2240
  });
2241
+ const getRunEvents = (eventCursor) => getDurableRunEvents(request, eventCursor);
560
2242
  phase = "provider_stream";
561
- let response = await request.postJson("/api/stream", makeBody());
562
- if (!response.ok) {
2243
+ const postStream = async (eventCursor) => {
2244
+ runSubmissionStarted = true;
2245
+ if (options?.signal?.aborted) {
2246
+ throw new Error("Request aborted by user");
2247
+ }
2248
+ return request.postJson("/api/stream", makeBody(eventCursor));
2249
+ };
2250
+ let response;
2251
+ let activeResponse;
2252
+ let activeEventCursor = 0;
2253
+ let interruption = new Error("pi-server stream ended before a terminal event");
2254
+ try {
2255
+ if (resumedPendingRun) {
2256
+ if (recoveredPendingRun?.status === "running" ||
2257
+ recoveredPendingRun?.status === "completed" ||
2258
+ recoveredPendingRun?.status === "failed") {
2259
+ if (recoveredPendingRun.requestMac !== requestHash) {
2260
+ throw new Error("Pending pi-server run request identity did not match durable server state");
2261
+ }
2262
+ response = await getRunEvents(0);
2263
+ }
2264
+ else {
2265
+ interruption = new Error(`pi-server pending run recovery was unavailable: ${recoveredPendingRun?.status === "unavailable"
2266
+ ? recoveredPendingRun.details
2267
+ : "unknown recovery state"}`);
2268
+ }
2269
+ }
2270
+ else {
2271
+ response = await postStream(0);
2272
+ }
2273
+ }
2274
+ catch (error) {
2275
+ if (isAbortError(error, options?.signal))
2276
+ throw error;
2277
+ if (!isRetryablePiServerInterruption(error))
2278
+ throw error;
2279
+ interruption = error;
2280
+ }
2281
+ if (response && !response.ok) {
563
2282
  let failure = await readPiServerFailure(response);
564
2283
  if (!options?.signal?.aborted && isRecoverableMissingServerState(response, failure.matchText)) {
565
2284
  resetSessionTracking(sessionId);
2285
+ phase = "session_init";
566
2286
  await ensureSessionInit(sessionId, context, request);
567
- await syncPiServerTreeWithRequest(sessionId, context, syncTree, request, options?.onHistoryReconciled);
568
- response = await request.postJson("/api/stream", makeBody());
569
- if (response.ok) {
570
- failure = { details: "", matchText: "" };
2287
+ phase = "tree_sync";
2288
+ await syncPiServerTreeWithRequest(sessionId, syncTree, request, options?.onHistoryReconciled);
2289
+ phase = "provider_stream";
2290
+ try {
2291
+ response = await postStream(receivedProxyEventCount);
571
2292
  }
572
- else {
2293
+ catch (error) {
2294
+ if (isAbortError(error, options?.signal))
2295
+ throw error;
2296
+ if (!isRetryablePiServerInterruption(error))
2297
+ throw error;
2298
+ response = undefined;
2299
+ interruption = error;
2300
+ }
2301
+ if (response && !response.ok) {
573
2302
  failure = await readPiServerFailure(response);
574
2303
  }
575
2304
  }
576
- if (!response.ok) {
577
- throw new Error(`pi-server error: ${failure.details}`);
2305
+ if (response && !response.ok) {
2306
+ const retryable = isTransientPiServerResponse(response, failure.bodyText);
2307
+ const responseError = new PiServerHttpResponseError(`pi-server error: ${failure.details}`, response.status, retryable);
2308
+ if (!retryable)
2309
+ throw responseError;
2310
+ interruption = responseError;
578
2311
  }
579
2312
  }
580
- await ensurePiServerEventStream(response);
581
- const reader = response.body.getReader();
582
- const decoder = new TextDecoder();
583
- let buffer = "";
2313
+ if (response?.ok) {
2314
+ activeResponse = response;
2315
+ activeEventCursor = receivedProxyEventCount;
2316
+ }
2317
+ let reconnectAttempt = 0;
2318
+ let recoveryDeadline;
584
2319
  while (true) {
585
- const { done, value } = await reader.read();
586
- if (done)
587
- break;
2320
+ if (pendingTransientTerminalEvent) {
2321
+ try {
2322
+ await acknowledgeTransientTerminal(pendingTransientTerminalEvent);
2323
+ endStream();
2324
+ return;
2325
+ }
2326
+ catch (error) {
2327
+ if (!isRetryablePiServerInterruption(error)) {
2328
+ await settleStreamFailure(error, false);
2329
+ return;
2330
+ }
2331
+ interruption = error;
2332
+ recoveryDeadline = Date.now() + recoveryWindowMs;
2333
+ reconnectAttempt = 0;
2334
+ }
2335
+ }
2336
+ if (activeResponse) {
2337
+ const result = await readStreamResponse(activeResponse, activeEventCursor);
2338
+ if (result.terminal) {
2339
+ endStream();
2340
+ return;
2341
+ }
2342
+ if (!result.recoverable) {
2343
+ await settleStreamFailure(result.failure, false);
2344
+ return;
2345
+ }
2346
+ interruption = result.failure;
2347
+ activeResponse = undefined;
2348
+ if (result.observedProgress) {
2349
+ recoveryDeadline = Date.now() + recoveryWindowMs;
2350
+ reconnectAttempt = 0;
2351
+ }
2352
+ else {
2353
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
2354
+ }
2355
+ }
588
2356
  if (options?.signal?.aborted) {
589
2357
  throw new Error("Request aborted by user");
590
2358
  }
591
- buffer += decoder.decode(value, { stream: true });
592
- const lines = buffer.split("\n");
593
- buffer = lines.pop() || "";
594
- for (const line of lines) {
595
- if (line.startsWith("data: ")) {
596
- const data = line.slice(6).trim();
597
- if (!data)
598
- continue;
599
- const proxyEvent = JSON.parse(data);
600
- const event = processProxyEvent(proxyEvent, partial);
601
- if (event) {
602
- stream.push(event);
603
- }
604
- }
2359
+ recoveryDeadline ??= Date.now() + recoveryWindowMs;
2360
+ const recoveredRun = await recoverPiServerRun(sessionId, runId, request, options?.signal);
2361
+ const hasDurableRun = recoveredRun.status === "running" ||
2362
+ recoveredRun.status === "completed" ||
2363
+ recoveredRun.status === "failed";
2364
+ if (hasDurableRun && recoveredRun.requestMac !== requestHash) {
2365
+ await settleStreamFailure(new Error("pi-server recovered run request identity did not match the pending provider request"), false);
2366
+ return;
2367
+ }
2368
+ if (recoveredRun.status === "not_found") {
2369
+ const failureMessage = interruption instanceof Error ? interruption.message : String(interruption);
2370
+ await settleStreamFailure(missingDurablePiServerRunError(runId, failureMessage), false);
2371
+ return;
2372
+ }
2373
+ if (recoveredRun.status === "unavailable") {
2374
+ const failureMessage = interruption instanceof Error ? interruption.message : String(interruption);
2375
+ interruption = new Error(`${failureMessage}; pi-server run recovery was unavailable: ${recoveredRun.details}`);
2376
+ }
2377
+ const remainingMs = recoveryDeadline - Date.now();
2378
+ if (remainingMs <= 0) {
2379
+ const failureMessage = interruption instanceof Error ? interruption.message : String(interruption);
2380
+ await settleStreamFailure(new Error(`${failureMessage}; pi-server stream recovery exhausted after ${recoveryWindowMs}ms`), false);
2381
+ return;
2382
+ }
2383
+ const delayMs = Math.min(getPiServerStreamReconnectDelay(reconnectAttempt), remainingMs);
2384
+ reconnectAttempt++;
2385
+ await waitForPiServerStreamReconnect(delayMs, options?.signal);
2386
+ if (Date.now() >= recoveryDeadline) {
2387
+ const failureMessage = interruption instanceof Error ? interruption.message : String(interruption);
2388
+ await settleStreamFailure(new Error(`${failureMessage}; pi-server stream recovery exhausted after ${recoveryWindowMs}ms`), false);
2389
+ return;
605
2390
  }
606
- }
607
- if (options?.signal?.aborted) {
608
- throw new Error("Request aborted by user");
609
- }
610
- if (isEphemeralSession) {
611
- resetSessionTracking(sessionId);
612
- }
613
- stream.end();
614
- }
615
- catch (error) {
616
- const errorMessage = error instanceof Error ? error.message : String(error);
617
- if (!options?.signal?.aborted && phase === "provider_stream") {
618
2391
  try {
619
- const recoveredRun = await fetchPiServerRun(sessionId, runId, createPiServerRequest());
620
- if (recoveredRun?.status === "completed" && recoveredRun.message) {
621
- switch (recoveredRun.message.stopReason) {
622
- case "stop":
623
- case "length":
624
- case "toolUse":
625
- stream.push({
626
- type: "done",
627
- reason: recoveredRun.message.stopReason,
628
- message: recoveredRun.message,
629
- });
630
- break;
631
- case "aborted":
632
- stream.push({ type: "error", reason: "aborted", error: recoveredRun.message });
633
- break;
634
- case "error":
635
- stream.push({ type: "error", reason: "error", error: recoveredRun.message });
636
- break;
2392
+ const eventCursor = receivedProxyEventCount;
2393
+ if (recoveredRun.status === "unavailable") {
2394
+ continue;
2395
+ }
2396
+ const reconnectResponse = await getRunEvents(eventCursor);
2397
+ if (!reconnectResponse.ok) {
2398
+ const failure = await readPiServerFailure(reconnectResponse);
2399
+ if (!isTransientPiServerResponse(reconnectResponse, failure.bodyText)) {
2400
+ await settleStreamFailure(new Error(`pi-server reconnect failed: ${failure.details}`), false);
2401
+ return;
637
2402
  }
638
- stream.end();
639
- return;
2403
+ interruption = new Error(`pi-server reconnect failed: ${failure.details}`);
2404
+ continue;
640
2405
  }
2406
+ activeResponse = reconnectResponse;
2407
+ activeEventCursor = eventCursor;
641
2408
  }
642
- catch {
643
- // Keep the original stream error authoritative.
2409
+ catch (error) {
2410
+ if (isAbortError(error, options?.signal)) {
2411
+ throw error;
2412
+ }
2413
+ if (!isRetryablePiServerInterruption(error)) {
2414
+ await settleStreamFailure(error, false);
2415
+ return;
2416
+ }
2417
+ interruption = error;
644
2418
  }
645
2419
  }
646
- const reason = options?.signal?.aborted ? "aborted" : "error";
647
- partial.stopReason = reason;
648
- partial.errorMessage = errorMessage;
649
- partial.diagnostics = [
650
- ...(partial.diagnostics ?? []),
651
- {
652
- type: "pi_server_failure",
653
- timestamp: Date.now(),
654
- error: { name: error instanceof Error ? error.name : "Error", message: errorMessage },
655
- details: { phase, source: "pi-server", retryable: phase === "provider_stream" },
656
- },
657
- ];
658
- stream.push({
659
- type: "error",
660
- reason,
661
- error: partial,
662
- });
663
- stream.end();
2420
+ }
2421
+ catch (error) {
2422
+ await settleStreamFailure(error, false);
664
2423
  }
665
2424
  })();
666
2425
  return stream;
@@ -683,6 +2442,8 @@ function processProxyEvent(proxyEvent, partial) {
683
2442
  case "text_end": {
684
2443
  const content = partial.content[proxyEvent.contentIndex];
685
2444
  if (content?.type === "text") {
2445
+ if (proxyEvent.content !== undefined)
2446
+ content.text = proxyEvent.content;
686
2447
  content.textSignature = proxyEvent.contentSignature;
687
2448
  return { type: "text_end", contentIndex: proxyEvent.contentIndex, content: content.text, partial };
688
2449
  }
@@ -702,25 +2463,34 @@ function processProxyEvent(proxyEvent, partial) {
702
2463
  case "thinking_end": {
703
2464
  const content = partial.content[proxyEvent.contentIndex];
704
2465
  if (content?.type === "thinking") {
2466
+ if (proxyEvent.content !== undefined)
2467
+ content.thinking = proxyEvent.content;
705
2468
  content.thinkingSignature = proxyEvent.contentSignature;
2469
+ content.redacted = proxyEvent.redacted;
706
2470
  return { type: "thinking_end", contentIndex: proxyEvent.contentIndex, content: content.thinking, partial };
707
2471
  }
708
2472
  throw new Error("Received thinking_end for non-thinking content");
709
2473
  }
710
- case "toolcall_start":
711
- partial.content[proxyEvent.contentIndex] = {
2474
+ case "toolcall_start": {
2475
+ const partialToolCall = {
712
2476
  type: "toolCall",
713
2477
  id: proxyEvent.id,
714
2478
  name: proxyEvent.toolName,
715
2479
  arguments: {},
716
2480
  partialJson: "",
717
2481
  };
2482
+ partial.content[proxyEvent.contentIndex] = partialToolCall;
718
2483
  return { type: "toolcall_start", contentIndex: proxyEvent.contentIndex, partial };
2484
+ }
719
2485
  case "toolcall_delta": {
720
2486
  const content = partial.content[proxyEvent.contentIndex];
721
2487
  if (content?.type === "toolCall") {
722
- content.partialJson += proxyEvent.delta;
723
- content.arguments = parseStreamingJson(content.partialJson) || {};
2488
+ const partialToolCall = content;
2489
+ if (partialToolCall.partialJson === undefined) {
2490
+ throw new Error("Received toolcall_delta without an active partial tool call");
2491
+ }
2492
+ partialToolCall.partialJson += proxyEvent.delta;
2493
+ content.arguments = parseStreamingJson(partialToolCall.partialJson) || {};
724
2494
  partial.content[proxyEvent.contentIndex] = { ...content };
725
2495
  return { type: "toolcall_delta", contentIndex: proxyEvent.contentIndex, delta: proxyEvent.delta, partial };
726
2496
  }
@@ -729,24 +2499,46 @@ function processProxyEvent(proxyEvent, partial) {
729
2499
  case "toolcall_end": {
730
2500
  const content = partial.content[proxyEvent.contentIndex];
731
2501
  if (content?.type === "toolCall") {
732
- delete content.partialJson;
733
- return { type: "toolcall_end", contentIndex: proxyEvent.contentIndex, toolCall: content, partial };
2502
+ const toolCall = proxyEvent.toolCall ?? content;
2503
+ delete toolCall.partialJson;
2504
+ toolCall.thoughtSignature = proxyEvent.thoughtSignature ?? toolCall.thoughtSignature;
2505
+ partial.content[proxyEvent.contentIndex] = toolCall;
2506
+ return { type: "toolcall_end", contentIndex: proxyEvent.contentIndex, toolCall, partial };
734
2507
  }
735
2508
  return undefined;
736
2509
  }
737
2510
  case "done":
2511
+ if (proxyEvent.reason !== "stop" && proxyEvent.reason !== "length" && proxyEvent.reason !== "toolUse") {
2512
+ throw new Error(`Received invalid terminal done reason: ${String(proxyEvent.reason)}`);
2513
+ }
738
2514
  partial.stopReason = proxyEvent.reason;
739
2515
  partial.usage = proxyEvent.usage;
2516
+ partial.api = proxyEvent.api ?? partial.api;
2517
+ partial.provider = proxyEvent.provider ?? partial.provider;
2518
+ partial.model = proxyEvent.model ?? partial.model;
2519
+ partial.timestamp = proxyEvent.timestamp ?? partial.timestamp;
2520
+ partial.responseModel = proxyEvent.responseModel;
2521
+ partial.responseId = proxyEvent.responseId;
2522
+ partial.diagnostics = proxyEvent.diagnostics;
740
2523
  return { type: "done", reason: proxyEvent.reason, message: partial };
741
2524
  case "error":
2525
+ if (proxyEvent.reason !== "aborted" && proxyEvent.reason !== "error") {
2526
+ throw new Error(`Received invalid terminal error reason: ${String(proxyEvent.reason)}`);
2527
+ }
742
2528
  partial.stopReason = proxyEvent.reason;
743
2529
  partial.errorMessage = proxyEvent.errorMessage;
744
2530
  partial.usage = proxyEvent.usage;
2531
+ partial.api = proxyEvent.api ?? partial.api;
2532
+ partial.provider = proxyEvent.provider ?? partial.provider;
2533
+ partial.model = proxyEvent.model ?? partial.model;
2534
+ partial.timestamp = proxyEvent.timestamp ?? partial.timestamp;
2535
+ partial.responseModel = proxyEvent.responseModel;
2536
+ partial.responseId = proxyEvent.responseId;
2537
+ partial.diagnostics = proxyEvent.diagnostics;
745
2538
  return { type: "error", reason: proxyEvent.reason, error: partial };
746
2539
  default: {
747
2540
  const _exhaustiveCheck = proxyEvent;
748
- console.warn(`Unhandled proxy event type: ${proxyEvent.type}`);
749
- return undefined;
2541
+ throw new Error(`Unhandled proxy event: ${String(_exhaustiveCheck)}`);
750
2542
  }
751
2543
  }
752
2544
  }