@seanhogg/builderforce-memory 2026.10.81 → 2026.10.83

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 (42) hide show
  1. package/dist/knowledge/index.d.ts +1 -1
  2. package/dist/knowledge/index.d.ts.map +1 -1
  3. package/dist/knowledge/merkle.d.ts +20 -5
  4. package/dist/knowledge/merkle.d.ts.map +1 -1
  5. package/dist/knowledge/merkle.js +25 -7
  6. package/dist/knowledge/merkle.js.map +1 -1
  7. package/dist/knowledge/plan.d.ts.map +1 -1
  8. package/dist/knowledge/plan.js +3 -5
  9. package/dist/knowledge/plan.js.map +1 -1
  10. package/dist/working/fold.d.ts +15 -0
  11. package/dist/working/fold.d.ts.map +1 -1
  12. package/dist/working/fold.js +17 -6
  13. package/dist/working/fold.js.map +1 -1
  14. package/dist/working/index.d.ts +2 -0
  15. package/dist/working/index.d.ts.map +1 -1
  16. package/dist/working/index.js +1 -0
  17. package/dist/working/index.js.map +1 -1
  18. package/dist/working/runElision.d.ts +54 -0
  19. package/dist/working/runElision.d.ts.map +1 -0
  20. package/dist/working/runElision.js +137 -0
  21. package/dist/working/runElision.js.map +1 -0
  22. package/dist/working/transcript.d.ts +8 -2
  23. package/dist/working/transcript.d.ts.map +1 -1
  24. package/dist/working/transcript.js +40 -9
  25. package/dist/working/transcript.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/contract/exports.snapshot.json +3 -0
  28. package/src/contract/regression.assets.test.ts +1 -1
  29. package/src/contract/regression.contentHash.test.ts +2 -2
  30. package/src/contract/regression.inRunWindow.test.ts +175 -0
  31. package/src/contract/regression.listing.test.ts +1 -1
  32. package/src/contract/regression.merkleCooperative.test.ts +79 -0
  33. package/src/contract/types.contract.test.ts +20 -5
  34. package/src/knowledge/index.ts +1 -1
  35. package/src/knowledge/merkle.test.ts +84 -47
  36. package/src/knowledge/merkle.ts +38 -8
  37. package/src/knowledge/plan.ts +3 -4
  38. package/src/knowledge/review.readAhead.test.ts +4 -1
  39. package/src/working/fold.ts +19 -7
  40. package/src/working/index.ts +2 -0
  41. package/src/working/runElision.ts +149 -0
  42. package/src/working/transcript.ts +41 -7
@@ -15,6 +15,7 @@
15
15
  import { defaultEstimateTokens, messageTokens as handoffMessageTokens, tokensOf as handoffTokensOf } from '../handoff/budget.js';
16
16
  import type { HandoffMessage } from '../handoff/types.js';
17
17
  import { CONSOLIDATION_MARKER_PREFIX } from './consolidationMarker.js';
18
+ import { elideRunResults, type RunElisionBudget } from './runElision.js';
18
19
 
19
20
  /** A transcript message — the hand-off packer's shape, so both count a message alike. */
20
21
  export type TranscriptMessage = HandoffMessage;
@@ -137,8 +138,12 @@ function tokenWindowStart(convo: readonly TranscriptMessage[]): number {
137
138
  * invalid for strict vendors — Gemini rejects a request whose conversation does not
138
139
  * begin with a user turn (the cascade `[googleai] 400 INVALID_ARGUMENT` after a long
139
140
  * tool loop crossed the window boundary). The most recent user turn is never dropped:
140
- * correctness over the size cap, when a tool loop outgrows the window or a single turn
141
- * is itself oversized.
141
+ * correctness over the size cap, when a single turn is itself oversized.
142
+ *
143
+ * A RUN that outgrows the budget after that user turn is cut inside ({@link elideRunResults}):
144
+ * its older tool results become one-line stubs, keeping the user turn, every assistant turn
145
+ * and a ~{@link COMPACT_TAIL_TOKEN_BUDGET}-token verbatim tail. Without it one long run grew
146
+ * the prompt by every result it produced (chat #128305: 31k → 191k tokens).
142
147
  *
143
148
  * A consolidation marker that LEADS the transcript (a reloaded chat's seed begins with its saved
144
149
  * fold note) is never dropped: it is the note that stands for everything before the rest, and
@@ -152,8 +157,19 @@ export function windowed<M extends TranscriptMessage>(convo: M[]): M[] {
152
157
  let start = Math.min(Math.max(messageWindowStart(convo.length), tokenWindowStart(convo)), lastUser);
153
158
  while (convo[start]!.role !== 'user') start += 1;
154
159
  const lead = convo[0]!;
155
- if (start > 0 && isLeadingMarker(lead)) return [lead, ...convo.slice(start)];
156
- return convo.slice(start);
160
+ const window = start > 0 && isLeadingMarker(lead) ? [lead, ...convo.slice(start)] : convo.slice(start);
161
+ return elideRunResults(window, runElisionBudget());
162
+ }
163
+
164
+ /** The in-run cut's budget: the history budget, the compacted tail, the token drop chunk. */
165
+ function runElisionBudget(extraTokens = 0): RunElisionBudget {
166
+ return {
167
+ budgetTokens: HISTORY_TOKEN_BUDGET,
168
+ tailTokens: COMPACT_TAIL_TOKEN_BUDGET,
169
+ chunkTokens: HISTORY_TOKEN_DROP_CHUNK,
170
+ minTailMessages: COMPACT_TAIL_TURNS,
171
+ extraTokens,
172
+ };
157
173
  }
158
174
 
159
175
  /** Is this row a saved consolidation note (the visible marker the next seed starts from)? */
@@ -167,12 +183,18 @@ function isLeadingMarker(m: TranscriptMessage): boolean {
167
183
  * front. Deliberately conservative: it judges against the CURRENT transcript (which has
168
184
  * grown since the last working set was built), so it can only ever call a message gone a
169
185
  * turn early — a needless replay costs tokens, a stub for a vanished result costs the run.
186
+ * A result the in-run cut elided ({@link elideRunResults}) is gone too: the model sees its
187
+ * stub, not the row.
170
188
  */
171
189
  export function stillInWorkingContext<M extends TranscriptMessage>(state: WorkingTranscriptState<M>, anchor: unknown): boolean {
172
190
  const convo = state.transcript;
173
191
  const idx = convo.indexOf(anchor as M);
174
192
  if (idx < 0) return false;
175
- if (state.compactMemo) return idx >= verbatimStart(convo, state.compactMemo.coveredEnd);
193
+ const memo = state.compactMemo;
194
+ if (memo) {
195
+ if (idx < verbatimStart(convo, memo.coveredEnd)) return false;
196
+ return boundedCompacted('', convo, memo.note, memo.coveredEnd).includes(convo[idx]!);
197
+ }
176
198
  return windowed(convo).includes(convo[idx]!);
177
199
  }
178
200
 
@@ -269,6 +291,17 @@ export function assembleCompacted<M extends TranscriptMessage>(
269
291
  return out;
270
292
  }
271
293
 
294
+ /**
295
+ * {@link assembleCompacted}, kept inside the history budget: when the note's verbatim
296
+ * remainder has outgrown it and no new fold was made (the summary failed or timed out),
297
+ * the current run's older tool results are elided ({@link elideRunResults}) — the note and
298
+ * the system prompt are kept, the note charged to the budget as the window charges it.
299
+ */
300
+ function boundedCompacted<M extends TranscriptMessage>(systemPrompt: string, convo: M[], note: string, coveredEnd: number): M[] {
301
+ const [system, noteRow, ...rest] = assembleCompacted(systemPrompt, convo, note, coveredEnd);
302
+ return [system!, noteRow!, ...elideRunResults(rest, runElisionBudget(transcriptTokens([noteRow!])))];
303
+ }
304
+
272
305
  /** Estimated tokens of a message list — the ONE count compaction budgets by, exported so
273
306
  * a run measures what compaction kept out of a call with the same rule. */
274
307
  export function transcriptTokens(msgs: readonly TranscriptMessage[]): number {
@@ -324,9 +357,10 @@ export async function buildWorkingTranscript<M extends TranscriptMessage>(
324
357
  : convo.slice(from, to);
325
358
  const summary = to > from ? await summarize(fold) : null;
326
359
  if (summary == null) {
327
- // Nothing new to fold, or no summarizer: keep the note already held, else drop-oldest.
360
+ // Nothing new to fold, or no summarizer: keep the note already held, else drop-oldest —
361
+ // either way cut inside the run once it outgrows the budget (`elideRunResults`).
328
362
  return memo
329
- ? assembleCompacted(systemPrompt, convo, memo.note, memo.coveredEnd)
363
+ ? boundedCompacted(systemPrompt, convo, memo.note, memo.coveredEnd)
330
364
  : [{ role: 'system', content: systemPrompt } as M, ...windowed(convo)];
331
365
  }
332
366
  const note = `Compressed memory of the earlier part of this conversation:\n${summary}`;