Skip to content

Commit 120ccb7

Browse files
committed
fix(sessions): count only retained messages in the reset byte window
1 parent dffe42a commit 120ccb7

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎src/config/sessions/session-accessor.sqlite-active-events.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,45 @@ describe("SQLite active transcript event projection", () => {
191191
expect(afterReset.sizeBytes).toBeLessThan(1_000);
192192
});
193193

194+
it("excludes discarded tool results from the retained pre-reset window", async () => {
195+
await persistSessionTranscriptTurn(scope, {
196+
messages: [
197+
{ eventId: "old", parentId: null, message: { role: "user", content: "old" } },
198+
{
199+
eventId: "kept-user",
200+
parentId: "old",
201+
message: { role: "user", content: "kept question" },
202+
},
203+
{
204+
eventId: "kept-tool",
205+
parentId: "kept-user",
206+
message: { role: "toolResult", content: `hidden tool ${"x".repeat(20_000)}` },
207+
},
208+
{
209+
eventId: "kept-assistant",
210+
parentId: "kept-tool",
211+
message: { role: "assistant", content: "kept answer" },
212+
},
213+
],
214+
touchSessionEntry: false,
215+
});
216+
await appendTranscriptEvent(scope, {
217+
type: "reset",
218+
id: "reset-boundary",
219+
parentId: "kept-assistant",
220+
timestamp: "2026-07-22T00:00:00.000Z",
221+
reason: "new",
222+
firstKeptEntryId: "kept-user",
223+
});
224+
225+
// The reset retains kept-user and kept-assistant only; kept-tool is hidden from the
226+
// next turn, so its bytes must not keep the byte fuse latched.
227+
expect(readSessionTranscriptMessageEventById(scope, "kept-tool")).toBeUndefined();
228+
const stats = readSessionTranscriptActiveStats(scope);
229+
expect(stats.eventCount).toBe(2);
230+
expect(stats.sizeBytes).toBeLessThan(1_000);
231+
});
232+
194233
it("defers mixed legacy and canonical rebuilds off request stacks", async () => {
195234
await persistSessionTranscriptTurn(scope, {
196235
messages: [

‎src/config/sessions/session-accessor.sqlite-reset-window.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ type ResetMessageWindow = {
4444
postBoundaryMessagePosition: number;
4545
/** Active position of the reset row; events at or before it are pre-boundary. */
4646
boundaryActivePosition: number;
47-
/** Start of the retained span before the boundary, when firstKeptEntryId applies. */
48-
keptFromActivePosition: number | undefined;
4947
};
5048

5149
type ResetMessageWindowCacheEntry = {
@@ -171,7 +169,6 @@ function findLatestResetMessageWindow(
171169
.limit(1),
172170
)?.message_position ?? projection.state.activeMessageCount;
173171
let keptMessagePositions: number[] = [];
174-
let keptFromActivePosition: number | undefined;
175172
if (typeof reset.firstKeptEntryId === "string") {
176173
const firstKept = executeSqliteQueryTakeFirstSync(
177174
projection.database.db,
@@ -187,7 +184,6 @@ function findLatestResetMessageWindow(
187184
.where("identity.event_id", "=", reset.firstKeptEntryId),
188185
);
189186
if (firstKept && firstKept.active_position < resetRow.active_position) {
190-
keptFromActivePosition = firstKept.active_position;
191187
keptMessagePositions = executeSqliteQuerySync(
192188
projection.database.db,
193189
db
@@ -223,7 +219,6 @@ function findLatestResetMessageWindow(
223219
keptMessagePositions,
224220
postBoundaryMessagePosition,
225221
boundaryActivePosition: resetRow.active_position,
226-
keptFromActivePosition,
227222
};
228223
}
229224

@@ -342,16 +337,18 @@ export function readVisibleTranscriptByteStats(projection: ResetWindowProjection
342337
window
343338
? base.where((eb) => {
344339
const afterBoundary = eb("active.active_position", ">", window.boundaryActivePosition);
345-
const keptFrom = window.keptFromActivePosition;
346-
if (keptFrom === undefined) {
340+
const keptMessagePositions = window.keptMessagePositions;
341+
if (keptMessagePositions.length === 0) {
347342
return afterBoundary;
348343
}
349-
// Retained pre-boundary span stays visible, so its bytes still count.
344+
// A reset retains only the user/assistant messages named by the window, so
345+
// count those exact rows rather than the whole pre-boundary span. Including
346+
// the span would keep discarded tool results on the byte fuse after `/new`.
350347
return eb.or([
351348
afterBoundary,
352349
eb.and([
353-
eb("active.active_position", ">=", keptFrom),
354350
eb("active.active_position", "<", window.boundaryActivePosition),
351+
eb("active.message_position", "in", keptMessagePositions),
355352
]),
356353
]);
357354
})

0 commit comments

Comments
 (0)