pi-condense 2.10.4 → 2.10.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.
- package/CHANGELOG.md +6 -0
- package/PRUNING.md +3 -1
- package/index.ts +24 -3
- package/package.json +1 -1
- package/src/batch-capture.test.ts +40 -0
- package/src/batch-capture.ts +21 -2
- package/src/pruner.test.ts +21 -0
- package/src/reload-rearm.integration.test.ts +296 -1
- package/src/types.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ Published to npm as [`pi-condense`](https://www.npmjs.com/package/pi-condense) (
|
|
|
7
7
|
Pushing a `vX.Y.Z` tag triggers `.github/workflows/release.yml`, which runs the tests and
|
|
8
8
|
publishes via OIDC trusted publishing. See `.agents/skills/release/SKILL.md`.
|
|
9
9
|
|
|
10
|
+
## [2.10.5] - 2026-09-15
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Mid-run auto-flush triggers (budget, delta, frontier-gap) stayed dead after every human reply until the new run's per-run turn index caught up with the session-wide persisted frontier: live `turn_end` batches carried Pi's run-local `event.turnIndex` while the frontier counts assistant messages session-wide. Live capture now derives the session-wide index from the session branch. Flush metrics entries gain a `stubCount` field. (#16)
|
|
15
|
+
|
|
10
16
|
## [2.10.4] - 2026-09-15
|
|
11
17
|
|
|
12
18
|
### Fixed
|
package/PRUNING.md
CHANGED
|
@@ -301,7 +301,7 @@ Custom session entry types written by the extension (NOT in LLM context unless n
|
|
|
301
301
|
| `context-prune-dedup-alias` | `indexer.registerDuplicate` | One entry per content-hash dedup hit; rebuilt on `session_start` to repopulate `dedupAliasToOriginal` |
|
|
302
302
|
| `context-prune-chain` | `chain-compressor.compressEligible` (called from `flushPending` in `index.ts` and from `/pruner compact`) | One entry per chain that has been range-dropped from LLM context; drops are decided **positionally** by `resolveRange` (`src/chain-range-prune.ts`), not by id. `droppedToolCallIds` is a diagnostic cross-check only (recorded-vs-actual mismatch emits `range-id-mismatch`); `droppedOccurrenceKeys` (optional) is load-bearing - it's what the occurrence-keyed synthetic-body lookup (per-batch summary text/coverage) is keyed against; protected-output text is NOT keyed off it - `src/chain-range-prune.ts` pulls `protectedToolCallIds` live by bare id within the resolved range instead. Also carries optional `rangeSummaryText` (fused LLM range summary) when `fuseRangeSummary` is on, and optional `protectedToolCallIds` (verbatim protected outputs - ids protected by tool name or path glob - are relocated into the synthetic body as `<protected-output>` tags at render time). Optional `bodySource: "deterministic"` marks a chain that had zero per-batch summary coverage: `rangeSummaryText` then holds a zero-LLM stub (call count, tool histogram, span duration, `t<N>` refs) built by the uncovered-chain backfill path in `chain-compressor.ts`, instead of a summarizer-derived body. Rebuilt on `session_start` to repopulate the chain registry. |
|
|
303
303
|
| `context-prune-diagnostic` | `pruneMessages` / `applyChainCompressions` / `chain-compressor.compressEligible` (via `DiagnosticSink.report`, `src/diagnostics.ts`) | One entry per distinct `(kind, dedupKey)` prune-time degradation (`unresolved-range` / `range-id-mismatch` / `orphan-sweep` / `backfill-empty`). Never in LLM context; deduped in-memory; reset on `session_start` and `session_tree`. Surfaced on the footer status widget as `diag u<N>/m<N>/o<N>/b<N>`. See [Diagnostics](#diagnostics). |
|
|
304
|
-
| `context-prune-flush-metrics` | `flushPending` (end of every non-concurrent attempt, single `finally` emit site, outside the chain-compression try/catch) | One entry per flush attempt, all outcomes (incl. `empty`/`error`): trigger, batch counts, pre-flush `ContextMetricsSnapshot` (open-cycle thinking, largest-chain share, frontier gap). Append-only observability log - never in LLM context, never reconstructed on `session_start`. |
|
|
304
|
+
| `context-prune-flush-metrics` | `flushPending` (end of every non-concurrent attempt, single `finally` emit site, outside the chain-compression try/catch) | One entry per flush attempt, all outcomes (incl. `empty`/`error`): trigger, batch counts, `stubCount` (tool calls newly made stub-eligible by the flush: dedup aliases on processed batches plus calls from actually-indexed batches; `0` when nothing was indexed or aliased), and the pre-flush `ContextMetricsSnapshot` (open-cycle thinking, largest-chain share, frontier gap). Append-only observability log - never in LLM context, never reconstructed on `session_start`. |
|
|
305
305
|
|
|
306
306
|
## How the Model Re-reads Raw Outputs
|
|
307
307
|
|
|
@@ -708,6 +708,8 @@ This is rare in practice once `minBatchChars` is on, because the cases where sum
|
|
|
708
708
|
|
|
709
709
|
The last attempted prune boundary is persisted as `context-prune-frontier` so `flushPending` knows where the previous attempt left off, even if that attempt was a skip rather than a real summary. Without this, a batch that's been skipped as oversized would be re-attempted (with the same LLM call, the same oversize result, the same skip) on every subsequent flush.
|
|
710
710
|
|
|
711
|
+
The frontier's `lastAttemptedTurnIndex` uses a session-wide numbering domain: it counts every projected assistant message from the start of the session, matching the rescan rule in `src/batch-capture.ts`. Pi's `event.turnIndex` resets on each `agent_start`, so live `turn_end` capture instead derives this session-wide index from the current session branch via `deriveLiveTurnIndex`. If `getBranch()` throws, capture temporarily falls back to the run-local index. When the branch is healthy at flush time, the rescan re-derives the session-wide index and the fallback value never persists; when branch reads keep failing through the flush, the queued run-local index persists and numbering self-corrects on the next healthy flush. This domain never decreases across compaction because compaction appends entries rather than removing them.
|
|
712
|
+
|
|
711
713
|
### Other UI / observability features
|
|
712
714
|
|
|
713
715
|
- **Tree browser (`/pruner tree`):** interactive, foldable tree of pruned tool calls grouped under their summaries. `Ctrl-O` on a summary node opens the full markdown summary in a bordered overlay.
|
package/index.ts
CHANGED
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
* Usage: pi -e .
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import type { ExtensionAPI, SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
17
17
|
import { loadConfig } from "./src/config.js";
|
|
18
|
-
import { captureBatch, captureUnindexedBatchesFromSession, groupBatchesByMode, projectBranchMessages } from "./src/batch-capture.js";
|
|
18
|
+
import { captureBatch, captureUnindexedBatchesFromSession, deriveLiveTurnIndex, groupBatchesByMode, projectBranchMessages } from "./src/batch-capture.js";
|
|
19
19
|
import { summarizeBatch, summarizeBatches, summarizeRange } from "./src/summarizer.js";
|
|
20
20
|
import { FallbackController } from "./src/summarizer-fallback.js";
|
|
21
21
|
import { ToolCallIndexer } from "./src/indexer.js";
|
|
@@ -241,6 +241,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
241
241
|
// the emitter falls back to pi.appendEntry.
|
|
242
242
|
let capturedBatches = 0;
|
|
243
243
|
let processedCount = 0;
|
|
244
|
+
let stubCount = 0;
|
|
244
245
|
let outcome: FlushMetricsEntry["outcome"] = "empty";
|
|
245
246
|
let appendEntry: ((customType: string, data?: unknown) => void) | undefined;
|
|
246
247
|
|
|
@@ -251,6 +252,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
251
252
|
trigger,
|
|
252
253
|
capturedBatches,
|
|
253
254
|
processedBatches: processedCount,
|
|
255
|
+
stubCount,
|
|
254
256
|
outcome,
|
|
255
257
|
metrics: entryMetrics,
|
|
256
258
|
};
|
|
@@ -486,6 +488,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
486
488
|
totalRawCharCount += dedupRawChars;
|
|
487
489
|
totalToolCallCount += dedupCount;
|
|
488
490
|
totalDedupedCount += dedupCount;
|
|
491
|
+
stubCount += dedupCount;
|
|
489
492
|
dedupedBatches.push(batch);
|
|
490
493
|
processedBatches.push(batch);
|
|
491
494
|
continue;
|
|
@@ -500,6 +503,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
500
503
|
totalRawCharCount += batchRawCharCount + dedupRawChars;
|
|
501
504
|
totalToolCallCount += batch.toolCalls.length + dedupCount;
|
|
502
505
|
totalDedupedCount += dedupCount;
|
|
506
|
+
stubCount += dedupCount;
|
|
503
507
|
trivialBatches.push(batch);
|
|
504
508
|
processedBatches.push(batch);
|
|
505
509
|
continue;
|
|
@@ -541,8 +545,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
541
545
|
// Keep the in-memory summary-body registry current so chain compression
|
|
542
546
|
// can build synthetic chain messages without rescanning session entries.
|
|
543
547
|
indexer.registerSummaryBody(batchOccurrenceKeys, summaryText);
|
|
548
|
+
stubCount += batch.toolCalls.length + dedupCount;
|
|
544
549
|
floorSources.push(...batch.toolCalls);
|
|
545
550
|
} else {
|
|
551
|
+
stubCount += dedupCount;
|
|
546
552
|
oversizedBatches.push(batch);
|
|
547
553
|
}
|
|
548
554
|
} catch (err) {
|
|
@@ -889,10 +895,25 @@ export default function (pi: ExtensionAPI) {
|
|
|
889
895
|
|
|
890
896
|
let pushedBatch = false;
|
|
891
897
|
if (hasToolResults) {
|
|
898
|
+
// Live batches must be numbered in the frontier's session-wide domain, not
|
|
899
|
+
// Pi's run-local event.turnIndex (which resets on agent_start, #16). The
|
|
900
|
+
// branch at turn_end already holds the just-ended assistant message (pi
|
|
901
|
+
// persists it at message_end, a strictly earlier event), so the derived
|
|
902
|
+
// index is the rescan index of this turn.
|
|
903
|
+
let liveTurnIndex = event.turnIndex;
|
|
904
|
+
let branch: SessionEntry[] | undefined;
|
|
905
|
+
try {
|
|
906
|
+
branch = ctx.sessionManager.getBranch();
|
|
907
|
+
} catch {
|
|
908
|
+
// Transient getBranch failure must never block the turn; fall back to
|
|
909
|
+
// the run-local index (pre-#16 behavior). The flush-time rescan still
|
|
910
|
+
// recovers the batch.
|
|
911
|
+
}
|
|
912
|
+
if (branch) liveTurnIndex = deriveLiveTurnIndex(branch);
|
|
892
913
|
const capturedBatch = captureBatch(
|
|
893
914
|
event.message,
|
|
894
915
|
event.toolResults,
|
|
895
|
-
|
|
916
|
+
liveTurnIndex,
|
|
896
917
|
Date.now()
|
|
897
918
|
);
|
|
898
919
|
// Drop user-protected tool/path results so they stay verbatim in context.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-condense",
|
|
3
|
-
"version": "2.10.
|
|
3
|
+
"version": "2.10.5",
|
|
4
4
|
"description": "Pi coding-agent extension that summarizes completed tool-call batches, replaces raw outputs with short stubs, compresses closed tool-call chains, and recovers any original on demand via context_tree_query.",
|
|
5
5
|
"author": "Jacek Juraszek",
|
|
6
6
|
"license": "MIT",
|
|
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
|
|
|
2
2
|
import {
|
|
3
3
|
captureBatch,
|
|
4
4
|
captureUnindexedBatchesFromSession,
|
|
5
|
+
deriveLiveTurnIndex,
|
|
5
6
|
projectBranchMessages,
|
|
6
7
|
serializeBatchForSummarizer,
|
|
7
8
|
} from "./batch-capture.js";
|
|
@@ -180,6 +181,45 @@ describe("projectBranchMessages", () => {
|
|
|
180
181
|
});
|
|
181
182
|
});
|
|
182
183
|
|
|
184
|
+
describe("deriveLiveTurnIndex (#16)", () => {
|
|
185
|
+
// Branch mixing every entry class the projection must classify: tool-calling
|
|
186
|
+
// assistants, a text-only assistant, a custom_message steer, a pruner custom
|
|
187
|
+
// entry, a compaction entry. The last assistant carries a ready, unsummarized
|
|
188
|
+
// tool call so the rescan emits a batch for it.
|
|
189
|
+
function mixedBranch(): any[] {
|
|
190
|
+
return [
|
|
191
|
+
{ type: "message", message: { role: "user", content: [{ type: "text", text: "start" }] } },
|
|
192
|
+
{ type: "message", message: { role: "assistant", content: [{ type: "toolCall", id: "tc-old", name: "read", arguments: {} }] } },
|
|
193
|
+
{ type: "message", message: { role: "toolResult", toolCallId: "tc-old", toolName: "read", content: [{ type: "text", text: "y".repeat(200) }], timestamp: 1 } },
|
|
194
|
+
{ type: "message", message: { role: "assistant", content: [{ type: "text", text: "text only" }] } },
|
|
195
|
+
{ type: "custom_message", customType: "gauntlet-gate", content: "go", display: true },
|
|
196
|
+
{ type: "custom", customType: "context-prune-summary", data: {} },
|
|
197
|
+
{ type: "compaction", summary: "..." },
|
|
198
|
+
{ type: "message", message: { role: "user", content: [{ type: "text", text: "again" }] } },
|
|
199
|
+
{ type: "message", message: { role: "assistant", content: [{ type: "toolCall", id: "tc-live", name: "read", arguments: {} }] } },
|
|
200
|
+
{ type: "message", message: { role: "toolResult", toolCallId: "tc-live", toolName: "read", content: [{ type: "text", text: "x".repeat(200) }], timestamp: 2 } },
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
test("returns the rescan index of the branch's last assistant message (AC5 parity)", () => {
|
|
205
|
+
const branch = mixedBranch();
|
|
206
|
+
// 3 projected assistant messages (tc-old, text-only, tc-live) -> last index 2.
|
|
207
|
+
// custom_message/custom/compaction entries never count.
|
|
208
|
+
expect(deriveLiveTurnIndex(branch)).toBe(2);
|
|
209
|
+
const rescan = captureUnindexedBatchesFromSession(branch, { isSummarized: () => false });
|
|
210
|
+
const liveBatch = rescan.find((b) => b.toolCalls.some((tc) => tc.toolCallId === "tc-live"));
|
|
211
|
+
expect(liveBatch).toBeDefined();
|
|
212
|
+
expect(liveBatch!.turnIndex).toBe(deriveLiveTurnIndex(branch));
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("returns -1 when the branch has no projected assistant message", () => {
|
|
216
|
+
expect(deriveLiveTurnIndex([])).toBe(-1);
|
|
217
|
+
expect(
|
|
218
|
+
deriveLiveTurnIndex([{ type: "message", message: { role: "user", content: [{ type: "text", text: "hi" }] } }]),
|
|
219
|
+
).toBe(-1);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
183
223
|
describe("custom-anchor group boundary", () => {
|
|
184
224
|
function buildBranch(customType: string) {
|
|
185
225
|
const entry = (message: any) => ({ type: "message", message });
|
package/src/batch-capture.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
1
2
|
import type { CapturedBatch, CapturedToolCall, BatchingMode } from "./types.js";
|
|
2
3
|
import { occKey, resultTimestampOf } from "./occurrence-key.js";
|
|
3
4
|
import { isChainAnchorCustom } from "./chain-detector.js";
|
|
@@ -17,6 +18,22 @@ export function projectBranchMessages(branch: any[]): any[] {
|
|
|
17
18
|
.map((e: any) => (e.type === "custom_message" ? projectCustomMessageEntry(e) : e.message));
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Session-wide index for the live turn at `turn_end`: the index the rescan
|
|
23
|
+
* below assigns to the branch's last assistant message. Shares the rescan's
|
|
24
|
+
* counting rule (every projected assistant message, text-only included) so
|
|
25
|
+
* live capture and the persisted flush frontier live in one numbering domain.
|
|
26
|
+
* Returns -1 when the branch has no projected assistant message (harness-only;
|
|
27
|
+
* a real `turn_end` always follows a persisted assistant message).
|
|
28
|
+
*/
|
|
29
|
+
export function deriveLiveTurnIndex(branch: SessionEntry[]): number {
|
|
30
|
+
let count = 0;
|
|
31
|
+
for (const msg of projectBranchMessages(branch)) {
|
|
32
|
+
if (msg.role === "assistant") count++;
|
|
33
|
+
}
|
|
34
|
+
return count - 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
20
37
|
/** True for SessionEntry shapes that project into an AgentMessage-like object (see projectBranchMessages). */
|
|
21
38
|
function isProjectableEntry(e: any): boolean {
|
|
22
39
|
return (e.type === "message" && e.message) || e.type === "custom_message";
|
|
@@ -109,8 +126,10 @@ export function captureUnindexedBatchesFromSession(
|
|
|
109
126
|
// turnCounter increments for EVERY assistant message (not just prunable ones).
|
|
110
127
|
// This makes turnIndex stable across multiple prune cycles: pruning removes
|
|
111
128
|
// ToolResultMessages from the context event but leaves AssistantMessages in the
|
|
112
|
-
// session branch, so the count of all assistant messages never decreases
|
|
113
|
-
//
|
|
129
|
+
// session branch, so the count of all assistant messages never decreases. This
|
|
130
|
+
// session-wide count is the frontier's numbering domain; Pi's event.turnIndex
|
|
131
|
+
// matches it only inside one agent run (it resets on agent_start), so the live
|
|
132
|
+
// capture path derives the same index from the branch via deriveLiveTurnIndex.
|
|
114
133
|
let turnCounter = 0;
|
|
115
134
|
|
|
116
135
|
// userTurnGroup increments on every user message or eligible custom anchor seen
|
package/src/pruner.test.ts
CHANGED
|
@@ -76,6 +76,27 @@ describe("pruneMessages", () => {
|
|
|
76
76
|
expect(out[1].content[0].text).toContain("context_tree_query");
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
+
it("two renders of unchanged history are byte-identical (AC6 cache-prefix guard)", () => {
|
|
80
|
+
const indexer = makeMockIndexer({
|
|
81
|
+
summarized: new Set(["tc1"]),
|
|
82
|
+
shortRefs: new Map([["tc1", "t1"]]),
|
|
83
|
+
});
|
|
84
|
+
const build = () => [
|
|
85
|
+
{ role: "assistant", content: [{ type: "toolCall", id: "tc1", name: "bash", input: {} }], timestamp: 0 },
|
|
86
|
+
{
|
|
87
|
+
role: "toolResult",
|
|
88
|
+
toolCallId: "tc1",
|
|
89
|
+
toolName: "bash",
|
|
90
|
+
content: [{ type: "text", text: "big output" }],
|
|
91
|
+
isError: false,
|
|
92
|
+
timestamp: 1,
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
const first = pruneMessages(build(), indexer);
|
|
96
|
+
const second = pruneMessages(build(), indexer);
|
|
97
|
+
expect(JSON.stringify(second.messages)).toBe(JSON.stringify(first.messages));
|
|
98
|
+
});
|
|
99
|
+
|
|
79
100
|
it("returns original array reference when nothing is summarized or compressed", () => {
|
|
80
101
|
const indexer = makeMockIndexer();
|
|
81
102
|
const messages = [{ role: "user", content: "hello", timestamp: 1 }];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, mock } from "bun:test";
|
|
2
|
-
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import * as actualCompat from "@earendil-works/pi-ai/compat";
|
|
@@ -150,6 +150,44 @@ function pendingBatchEntries(toolCallId: string, text: string, timestamp: number
|
|
|
150
150
|
];
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
function textOnlyTurns(count: number, startTs: number): any[] {
|
|
154
|
+
const msgs: any[] = [];
|
|
155
|
+
let t = startTs;
|
|
156
|
+
for (let i = 0; i < count; i++) {
|
|
157
|
+
msgs.push({ type: "message", message: { role: "user", content: [{ type: "text", text: `turn ${i}` }], timestamp: (t += 1000) } });
|
|
158
|
+
msgs.push({ type: "message", message: { role: "assistant", content: [{ type: "text", text: `answer ${i}` }], timestamp: (t += 1000) } });
|
|
159
|
+
}
|
|
160
|
+
return msgs;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function frontierEntry(turnIndex: number, toolCallId: string): any {
|
|
164
|
+
return {
|
|
165
|
+
type: "custom",
|
|
166
|
+
customType: "context-prune-frontier",
|
|
167
|
+
data: {
|
|
168
|
+
lastAttemptedToolCallId: toolCallId,
|
|
169
|
+
lastAttemptedToolName: "read",
|
|
170
|
+
lastAttemptedTurnIndex: turnIndex,
|
|
171
|
+
lastAttemptedTimestamp: Date.now(),
|
|
172
|
+
attemptedBatchCount: 1,
|
|
173
|
+
attemptedToolCallCount: 1,
|
|
174
|
+
rawCharCount: 1000,
|
|
175
|
+
summaryCharCount: 100,
|
|
176
|
+
outcome: "summarized",
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function fireTurn(handlers: Map<string, any>, ctx: any, branch: any[], toolCallId: string, text: string, runLocalIndex: number) {
|
|
182
|
+
const t = Date.now();
|
|
183
|
+
const [, assistant, result] = pendingBatchEntries(toolCallId, text, t);
|
|
184
|
+
branch.push(assistant, result);
|
|
185
|
+
await handlers.get("turn_end")!(
|
|
186
|
+
{ message: assistant.message, toolResults: [result.message], turnIndex: runLocalIndex },
|
|
187
|
+
ctx,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
153
191
|
// Boots a fresh index.ts extension instance against an isolated agent dir +
|
|
154
192
|
// session, mirroring the fixtures shared across the three scenarios below.
|
|
155
193
|
//
|
|
@@ -288,6 +326,110 @@ async function boot(options?: Parameters<typeof bootExtension>[0]) {
|
|
|
288
326
|
}
|
|
289
327
|
|
|
290
328
|
describe("reload rearm (issue #6)", () => {
|
|
329
|
+
it("falls back to the run-local index at capture when getBranch throws, and the flush-time rescan re-derives the session-wide index", async () => {
|
|
330
|
+
const { handlers, ctx, notifications, appended } = await boot();
|
|
331
|
+
|
|
332
|
+
await handlers.get("session_start")!({}, ctx);
|
|
333
|
+
ctx.getContextUsage = () => ({ tokens: 10, contextWindow: 1000000 });
|
|
334
|
+
const healthyGetBranch = ctx.sessionManager.getBranch;
|
|
335
|
+
ctx.sessionManager.getBranch = () => {
|
|
336
|
+
throw new Error("boom");
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
await expect(
|
|
340
|
+
handlers.get("turn_end")!(
|
|
341
|
+
{
|
|
342
|
+
message: { role: "assistant", content: [{ type: "toolCall", id: "tc2", name: "read", arguments: {} }] },
|
|
343
|
+
toolResults: [
|
|
344
|
+
{ role: "toolResult", toolCallId: "tc2", toolName: "read", content: [{ type: "text", text: "result" }], timestamp: Date.now() },
|
|
345
|
+
],
|
|
346
|
+
turnIndex: 7,
|
|
347
|
+
},
|
|
348
|
+
ctx,
|
|
349
|
+
),
|
|
350
|
+
).resolves.toBeUndefined();
|
|
351
|
+
expect(notifications.filter((message) => message.includes("pruner: 1 turn queued"))).toHaveLength(1);
|
|
352
|
+
|
|
353
|
+
ctx.sessionManager.getBranch = healthyGetBranch;
|
|
354
|
+
await handlers.get("message_end")!(
|
|
355
|
+
{ message: { role: "assistant", content: [{ type: "text", text: "done" }] } },
|
|
356
|
+
ctx,
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const flushMetrics = appended.find((entry) => entry.type === "context-prune-flush-metrics");
|
|
360
|
+
expect(flushMetrics).toBeDefined();
|
|
361
|
+
expect((flushMetrics!.data as any).outcome).not.toBe("error");
|
|
362
|
+
|
|
363
|
+
const frontier = appended.find((entry) => entry.type === "context-prune-frontier");
|
|
364
|
+
expect(frontier).toBeDefined();
|
|
365
|
+
// The flush-time rescan re-derives the session-wide index; the run-local fallback value never persists.
|
|
366
|
+
expect((frontier!.data as any).lastAttemptedTurnIndex).toBe(0);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("persists the queued batch's run-local index when the flush itself runs on the getBranch fallback", async () => {
|
|
370
|
+
const { handlers, ctx, appended } = await boot();
|
|
371
|
+
|
|
372
|
+
await handlers.get("session_start")!({}, ctx);
|
|
373
|
+
ctx.getContextUsage = () => ({ tokens: 10, contextWindow: 1000000 });
|
|
374
|
+
ctx.sessionManager.getBranch = () => {
|
|
375
|
+
throw new Error("boom");
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// Long result text so the queued batch clears the oversized-summary guard
|
|
379
|
+
// and reaches indexer.addBatch (a 6-char result would be skipped-oversized
|
|
380
|
+
// and leave no index entry to assert against).
|
|
381
|
+
await handlers.get("turn_end")!(
|
|
382
|
+
{
|
|
383
|
+
message: { role: "assistant", content: [{ type: "toolCall", id: "tc2", name: "read", arguments: {} }] },
|
|
384
|
+
toolResults: [
|
|
385
|
+
{ role: "toolResult", toolCallId: "tc2", toolName: "read", content: [{ type: "text", text: "x".repeat(400) }], timestamp: Date.now() },
|
|
386
|
+
],
|
|
387
|
+
turnIndex: 7,
|
|
388
|
+
},
|
|
389
|
+
ctx,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
// getBranch is still down at flush time: capturePendingBatches' catch
|
|
393
|
+
// branch serves pendingBatches.slice() (index.ts), so the queued batch's
|
|
394
|
+
// captured run-local index (7) is what reaches persistence.
|
|
395
|
+
await handlers.get("message_end")!(
|
|
396
|
+
{ message: { role: "assistant", content: [{ type: "text", text: "done" }] } },
|
|
397
|
+
ctx,
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const flushMetrics = appended.find((entry) => entry.type === "context-prune-flush-metrics");
|
|
401
|
+
expect(flushMetrics).toBeDefined();
|
|
402
|
+
expect((flushMetrics!.data as any).outcome).toBe("summarized");
|
|
403
|
+
|
|
404
|
+
const indexEntry = appended.find((entry) => entry.type === "context-prune-index");
|
|
405
|
+
expect(indexEntry).toBeDefined();
|
|
406
|
+
expect((indexEntry!.data as any).toolCalls[0].turnIndex).toBe(7);
|
|
407
|
+
|
|
408
|
+
const frontier = appended.find((entry) => entry.type === "context-prune-frontier");
|
|
409
|
+
expect(frontier).toBeDefined();
|
|
410
|
+
expect((frontier!.data as any).lastAttemptedTurnIndex).toBe(7);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it("propagates turn-index derivation errors at turn_end", async () => {
|
|
414
|
+
const { handlers, ctx, branch } = await boot();
|
|
415
|
+
|
|
416
|
+
await handlers.get("session_start")!({}, ctx);
|
|
417
|
+
branch.push(null);
|
|
418
|
+
|
|
419
|
+
await expect(
|
|
420
|
+
handlers.get("turn_end")!(
|
|
421
|
+
{
|
|
422
|
+
message: { role: "assistant", content: [{ type: "toolCall", id: "tc2", name: "read", arguments: {} }] },
|
|
423
|
+
toolResults: [
|
|
424
|
+
{ role: "toolResult", toolCallId: "tc2", toolName: "read", content: [{ type: "text", text: "result" }], timestamp: Date.now() },
|
|
425
|
+
],
|
|
426
|
+
turnIndex: 0,
|
|
427
|
+
},
|
|
428
|
+
ctx,
|
|
429
|
+
),
|
|
430
|
+
).rejects.toThrow();
|
|
431
|
+
});
|
|
432
|
+
|
|
291
433
|
it("rearms the turn_end budget gate after a reload so recovered pending work still flushes", async () => {
|
|
292
434
|
const { handlers, ctx, appended } = await boot();
|
|
293
435
|
|
|
@@ -784,6 +926,8 @@ describe("reload rearm (issue #6)", () => {
|
|
|
784
926
|
expect(flushMetricsEntries.length).toBe(1);
|
|
785
927
|
const fm = flushMetricsEntries[0].data as any;
|
|
786
928
|
expect(fm.trigger).toBe("frontier-gap");
|
|
929
|
+
// defaultBranch's single tc1 call is summarized+indexed by this flush.
|
|
930
|
+
expect(fm.stubCount).toBe(1);
|
|
787
931
|
expect(fm.metrics.frontierGapTokens).toBeGreaterThanOrEqual(10);
|
|
788
932
|
|
|
789
933
|
expect(notifications.some((n) => n.includes("un-pruned tail exceeded frontier gap threshold"))).toBe(true);
|
|
@@ -895,6 +1039,9 @@ describe("reload rearm (issue #6)", () => {
|
|
|
895
1039
|
expect(frontierEntries.length).toBe(1);
|
|
896
1040
|
const firstFrontier = frontierEntries[0].data as any;
|
|
897
1041
|
expect(firstFrontier.lastAttemptedToolCallId).toBe("tc-a");
|
|
1042
|
+
// Turn 2's flush indexed tc-a (1 call) and aliased nothing else.
|
|
1043
|
+
const firstMetrics = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1044
|
+
expect((firstMetrics[0].data as any).stubCount).toBe(1);
|
|
898
1045
|
|
|
899
1046
|
// Grow the branch again (tc-b is still unsummarized/pending after the
|
|
900
1047
|
// restore; add tc-c as this turn's new work) — gap stays over threshold.
|
|
@@ -1228,12 +1375,47 @@ describe("supersede floor cadence (spec 2026-09-07)", () => {
|
|
|
1228
1375
|
const frontierEntries = appended.filter((e) => e.type === "context-prune-frontier");
|
|
1229
1376
|
expect(frontierEntries.length).toBe(1);
|
|
1230
1377
|
expect((frontierEntries[0].data as any).outcome).toBe("skipped-trivial");
|
|
1378
|
+
const flushMetricsEntries = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1379
|
+
expect(flushMetricsEntries.length).toBe(1);
|
|
1380
|
+
expect((flushMetricsEntries[0].data as any).outcome).toBe("skipped-trivial");
|
|
1381
|
+
expect((flushMetricsEntries[0].data as any).stubCount).toBe(0);
|
|
1231
1382
|
|
|
1232
1383
|
const rendered = await render(branch, handlers, ctx);
|
|
1233
1384
|
expect(toolResultText(rendered, "r1")).toBe("content-r1");
|
|
1234
1385
|
expect(toolResultText(rendered, "r2")).toBe("content-r2");
|
|
1235
1386
|
});
|
|
1236
1387
|
|
|
1388
|
+
it("a partially deduped skipped-trivial batch counts only its alias in stubCount", async () => {
|
|
1389
|
+
const R = "R".repeat(400);
|
|
1390
|
+
const branch: any[] = [];
|
|
1391
|
+
const { handlers, ctx, appended } = await boot({ protectedPaths: [PROTECTED_GLOB], branch });
|
|
1392
|
+
|
|
1393
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1394
|
+
await fireTurn(handlers, ctx, branch, "bashOrig", R, 1);
|
|
1395
|
+
expect(appended.some((e) => e.type === "context-prune-index")).toBe(true);
|
|
1396
|
+
|
|
1397
|
+
const assistant = {
|
|
1398
|
+
role: "assistant",
|
|
1399
|
+
content: [
|
|
1400
|
+
{ type: "toolCall", id: "bashDup", name: "read", arguments: {} },
|
|
1401
|
+
{ type: "toolCall", id: "trivial", name: "read", arguments: {} },
|
|
1402
|
+
],
|
|
1403
|
+
};
|
|
1404
|
+
const toolResults = [
|
|
1405
|
+
{ role: "toolResult", toolCallId: "bashDup", toolName: "read", content: [{ type: "text", text: R }], timestamp: 90 },
|
|
1406
|
+
{ role: "toolResult", toolCallId: "trivial", toolName: "read", content: [{ type: "text", text: "" }], timestamp: 91 },
|
|
1407
|
+
];
|
|
1408
|
+
branch.push({ type: "message", message: assistant }, ...toolResults.map((message) => ({ type: "message", message })));
|
|
1409
|
+
await handlers.get("turn_end")!({ message: assistant, toolResults, turnIndex: 2 }, ctx);
|
|
1410
|
+
|
|
1411
|
+
const aliasEntries = appended.filter((e) => e.type === "context-prune-dedup-alias");
|
|
1412
|
+
expect(aliasEntries.length).toBe(1);
|
|
1413
|
+
const flushMetricsEntries = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1414
|
+
const metrics = flushMetricsEntries[flushMetricsEntries.length - 1].data as any;
|
|
1415
|
+
expect(metrics.outcome).toBe("skipped-trivial");
|
|
1416
|
+
expect(metrics.stubCount).toBe(1);
|
|
1417
|
+
});
|
|
1418
|
+
|
|
1237
1419
|
it("a skipped-oversized batch (summary longer than raw) sets no floor", async () => {
|
|
1238
1420
|
const branch: any[] = [];
|
|
1239
1421
|
const { handlers, ctx, appended } = await boot({ protectedPaths: [PROTECTED_GLOB], branch });
|
|
@@ -1263,6 +1445,10 @@ describe("supersede floor cadence (spec 2026-09-07)", () => {
|
|
|
1263
1445
|
const frontierEntries = appended.filter((e) => e.type === "context-prune-frontier");
|
|
1264
1446
|
expect(frontierEntries.length).toBe(1);
|
|
1265
1447
|
expect((frontierEntries[0].data as any).outcome).toBe("skipped-oversized");
|
|
1448
|
+
const flushMetricsEntries = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1449
|
+
expect(flushMetricsEntries.length).toBe(1);
|
|
1450
|
+
expect((flushMetricsEntries[0].data as any).outcome).toBe("skipped-oversized");
|
|
1451
|
+
expect((flushMetricsEntries[0].data as any).stubCount).toBe(0);
|
|
1266
1452
|
|
|
1267
1453
|
const rendered = await render(branch, handlers, ctx);
|
|
1268
1454
|
expect(toolResultText(rendered, "r1")).toBe("content-r1");
|
|
@@ -1316,6 +1502,10 @@ describe("supersede floor cadence (spec 2026-09-07)", () => {
|
|
|
1316
1502
|
expect(frontierEntries[frontierEntries.length - 1].data && (frontierEntries[frontierEntries.length - 1].data as any).outcome).toBe(
|
|
1317
1503
|
"skipped-deduped",
|
|
1318
1504
|
);
|
|
1505
|
+
const flushMetricsEntries = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1506
|
+
const dedupMetrics = flushMetricsEntries[flushMetricsEntries.length - 1].data as any;
|
|
1507
|
+
expect(dedupMetrics.outcome).toBe("skipped-deduped");
|
|
1508
|
+
expect(dedupMetrics.stubCount).toBe(1);
|
|
1319
1509
|
// The alias's own resultTimestamp (90) is <= the older read's (100) — the
|
|
1320
1510
|
// property that lets it, alone, explain the activation below.
|
|
1321
1511
|
|
|
@@ -1434,3 +1624,108 @@ describe("supersede floor cadence (spec 2026-09-07)", () => {
|
|
|
1434
1624
|
expect(toolResultText(rendered, "r_new")).toBe("content-r_new");
|
|
1435
1625
|
});
|
|
1436
1626
|
});
|
|
1627
|
+
|
|
1628
|
+
describe("session-wide live turn index (#16)", () => {
|
|
1629
|
+
it("AC8: pre-fix fixture with frontier 83 flushes one eligible live turn past the frontier", async () => {
|
|
1630
|
+
const fixture = readFileSync(join(import.meta.dirname, "fixtures", "gh16-frontier-83.jsonl"), "utf8")
|
|
1631
|
+
.trim()
|
|
1632
|
+
.split("\n")
|
|
1633
|
+
.map((line) => JSON.parse(line));
|
|
1634
|
+
const { handlers, ctx, appended } = await boot({
|
|
1635
|
+
branch: fixture,
|
|
1636
|
+
autoBudgetThreshold: null,
|
|
1637
|
+
budgetTurnDelta: null,
|
|
1638
|
+
frontierGapThresholdTokens: 100,
|
|
1639
|
+
});
|
|
1640
|
+
ctx.getContextUsage = () => ({ tokens: 10, contextWindow: 1000000 });
|
|
1641
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1642
|
+
|
|
1643
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-live", "x".repeat(800), 0);
|
|
1644
|
+
|
|
1645
|
+
const fm = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1646
|
+
expect(fm.length).toBe(1);
|
|
1647
|
+
expect((fm[0].data as any).trigger).toBe("frontier-gap");
|
|
1648
|
+
expect((fm[0].data as any).outcome).toBe("summarized");
|
|
1649
|
+
expect((fm[0].data as any).stubCount).toBeGreaterThan(0);
|
|
1650
|
+
|
|
1651
|
+
const frontierEntries = appended.filter((e) => e.type === "context-prune-frontier");
|
|
1652
|
+
const next = frontierEntries[frontierEntries.length - 1].data as any;
|
|
1653
|
+
expect(next.lastAttemptedTurnIndex).toBeGreaterThanOrEqual(83);
|
|
1654
|
+
});
|
|
1655
|
+
|
|
1656
|
+
it("AC1: a new run's first batch survives the frontier and reaches the budget gate", async () => {
|
|
1657
|
+
const branch = [...textOnlyTurns(84, 1700000000000), frontierEntry(83, "tc-old")];
|
|
1658
|
+
const { handlers, ctx, appended } = await boot({ branch, autoBudgetThreshold: 0.5 });
|
|
1659
|
+
ctx.getContextUsage = () => ({ tokens: 900000, contextWindow: 1000000 });
|
|
1660
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1661
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-new", "x".repeat(400), 0);
|
|
1662
|
+
const fm = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1663
|
+
expect(fm.length).toBe(1);
|
|
1664
|
+
expect((fm[0].data as any).trigger).toBe("budget");
|
|
1665
|
+
expect((fm[0].data as any).trigger).not.toBe("rearmed");
|
|
1666
|
+
});
|
|
1667
|
+
|
|
1668
|
+
it("AC2: an already-summarized live batch is still dropped (no re-summarization)", async () => {
|
|
1669
|
+
const branch = [...textOnlyTurns(84, 1700000000000), frontierEntry(83, "tc-old")];
|
|
1670
|
+
const { handlers, ctx, appended } = await boot({ branch, autoBudgetThreshold: 0.5 });
|
|
1671
|
+
ctx.getContextUsage = () => ({ tokens: 900000, contextWindow: 1000000 });
|
|
1672
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1673
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-new", "x".repeat(400), 0);
|
|
1674
|
+
const callsAfterFirst = summarizerCalls;
|
|
1675
|
+
expect(appended.filter((e) => e.type === "context-prune-flush-metrics").length).toBe(1);
|
|
1676
|
+
const [assistant, result] = ctx.sessionManager.getBranch().slice(-2);
|
|
1677
|
+
await handlers.get("turn_end")!({ message: assistant.message, toolResults: [result.message], turnIndex: 1 }, ctx);
|
|
1678
|
+
expect(summarizerCalls).toBe(callsAfterFirst);
|
|
1679
|
+
expect(appended.filter((e) => e.type === "context-prune-flush-metrics").length).toBe(1);
|
|
1680
|
+
});
|
|
1681
|
+
|
|
1682
|
+
it("AC3: above-frontier live batches in a continuing run keep flushing", async () => {
|
|
1683
|
+
const branch = [...textOnlyTurns(84, 1700000000000), frontierEntry(83, "tc-old")];
|
|
1684
|
+
const { handlers, ctx, appended } = await boot({ branch, autoBudgetThreshold: 0.5 });
|
|
1685
|
+
ctx.getContextUsage = () => ({ tokens: 900000, contextWindow: 1000000 });
|
|
1686
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1687
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-a", "a".repeat(400), 0);
|
|
1688
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-b", "b".repeat(400), 1);
|
|
1689
|
+
const fm = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1690
|
+
expect(fm.length).toBe(2);
|
|
1691
|
+
expect((fm[1].data as any).trigger).toBe("budget");
|
|
1692
|
+
});
|
|
1693
|
+
|
|
1694
|
+
it("AC4: equal-index partial turn keeps only the suffix after the recorded call", async () => {
|
|
1695
|
+
const branch = [...textOnlyTurns(2, 1700000000000), frontierEntry(2, "tc-2")];
|
|
1696
|
+
const { handlers, ctx, appended } = await boot({ branch, autoBudgetThreshold: 0.5 });
|
|
1697
|
+
ctx.getContextUsage = () => ({ tokens: 900000, contextWindow: 1000000 });
|
|
1698
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1699
|
+
const t = Date.now();
|
|
1700
|
+
const assistant = { type: "message", message: { role: "assistant", content: [
|
|
1701
|
+
{ type: "toolCall", id: "tc-1", name: "read", arguments: {} },
|
|
1702
|
+
{ type: "toolCall", id: "tc-2", name: "read", arguments: {} },
|
|
1703
|
+
{ type: "toolCall", id: "tc-3", name: "read", arguments: {} },
|
|
1704
|
+
] }, timestamp: t };
|
|
1705
|
+
const results = ["tc-1", "tc-2", "tc-3"].map((id, i) => ({
|
|
1706
|
+
type: "message", message: { role: "toolResult", toolCallId: id, toolName: "read", content: [{ type: "text", text: "x".repeat(400) }], timestamp: t + 1 + i },
|
|
1707
|
+
}));
|
|
1708
|
+
ctx.sessionManager.getBranch().push(assistant, ...results);
|
|
1709
|
+
await handlers.get("turn_end")!({ message: assistant.message, toolResults: results.map((r) => r.message), turnIndex: 0 }, ctx);
|
|
1710
|
+
const fm = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1711
|
+
expect(fm.length).toBe(1);
|
|
1712
|
+
expect((fm[0].data as any).stubCount).toBe(1);
|
|
1713
|
+
const indexEntries = appended.filter((e) => e.type === "context-prune-index");
|
|
1714
|
+
const indexPayload = JSON.stringify(indexEntries.map((e) => e.data));
|
|
1715
|
+
expect(indexPayload).toContain("tc-3");
|
|
1716
|
+
expect(indexPayload).not.toContain("tc-1");
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
it("AC7: frontier-gap fires on the second post-reply turn, not before", async () => {
|
|
1720
|
+
const branch = [...textOnlyTurns(51, 1700000000000), frontierEntry(50, "tc-old")];
|
|
1721
|
+
const { handlers, ctx, appended } = await boot({ branch, autoBudgetThreshold: null, budgetTurnDelta: null, frontierGapThresholdTokens: 1000 });
|
|
1722
|
+
ctx.getContextUsage = () => ({ tokens: 10, contextWindow: 1000000 });
|
|
1723
|
+
await handlers.get("session_start")!({}, ctx);
|
|
1724
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-g1", "g".repeat(3500), 0);
|
|
1725
|
+
expect(appended.some((e) => e.type === "context-prune-flush-metrics")).toBe(false);
|
|
1726
|
+
await fireTurn(handlers, ctx, ctx.sessionManager.getBranch(), "tc-g2", "g".repeat(3500), 1);
|
|
1727
|
+
const fm = appended.filter((e) => e.type === "context-prune-flush-metrics");
|
|
1728
|
+
expect(fm.length).toBe(1);
|
|
1729
|
+
expect((fm[0].data as any).trigger).toBe("frontier-gap");
|
|
1730
|
+
});
|
|
1731
|
+
});
|
package/src/types.ts
CHANGED
|
@@ -732,6 +732,8 @@ export interface FlushMetricsEntry {
|
|
|
732
732
|
/** Batches after rescan+trim, before processing. */
|
|
733
733
|
capturedBatches: number;
|
|
734
734
|
processedBatches: number;
|
|
735
|
+
/** Tool calls this flush newly made stub-eligible: dedup aliases on processed batches plus calls of batches actually indexed. 0 when nothing was indexed or aliased (all-trivial/oversized, or failure before any batch was processed). */
|
|
736
|
+
stubCount: number;
|
|
735
737
|
outcome: "summarized" | "skipped-oversized" | "skipped-deduped" | "skipped-trivial" | "empty" | "error";
|
|
736
738
|
/** Computed at flush ENTRY (pre-flush pressure). */
|
|
737
739
|
metrics: ContextMetricsSnapshot;
|