@cubicecho/agent-core 4.4.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -815,9 +815,15 @@ once `used` (the last turn's prompt tokens, or the estimate) is past three quart
815
815
  The kept tail fills at most 35% of it and starts on a user message, since a transcript resuming
816
816
  mid-exchange is one servers refuse; leading system prompts are never folded, and an earlier
817
817
  summary is continued rather than summarised. `compactTranscript` writes the summary and tells
818
- `beforeCompact` hooks what is going while it does. Because the cut lands on
819
- a user message, one long tool run under a single question has nothing to fold — pruning is what
820
- keeps that one going.
818
+ `beforeCompact` hooks what is going while it does.
819
+
820
+ A run that is one question and a long tool loop has no user message to cut at, so there the plan
821
+ cuts inside the loop instead: the question is kept whole (the plan's `question`), the calls and
822
+ results after it are folded up to an assistant message, and the request becomes the summary, the
823
+ question, then the rest of the loop — every tool result still after its call. A transcript with a
824
+ user message to cut at is planned as before. `runAgentLoop({ compact: true })` folds such a run on
825
+ its own; a host that numbers messages across steps passes `hooks.offset`, the stored index of the
826
+ run's first message, so the ids `beforeCompact` is told match its own.
821
827
 
822
828
  ```ts
823
829
  beforeStep: async (messages, step) => {
@@ -950,8 +956,8 @@ leading `system` messages, and reads an earlier summary back out of a `SUMMARY_L
950
956
  them. A host whose system prompt is a separate argument and whose summary is a column has neither
951
957
  in the array, and knows both exactly. Given them, nothing is scanned.
952
958
 
953
- `applyCompaction(messages, fold)` is the way back — the summary as a `system` message, then
954
- everything from `through` — and it writes the same `SUMMARY_LEAD` `planCompaction` looks for, so
959
+ `applyCompaction(messages, fold)` is the way back — the summary as a `system` message, then the
960
+ fold's `question` if it was made inside one, then everything from `through` — and it writes the same `SUMMARY_LEAD` `planCompaction` looks for, so
955
961
  the next fold continues those notes rather than summarising them a second time. No fold yet hands
956
962
  back the messages themselves.
957
963
 
@@ -125,9 +125,17 @@ export interface CompactionOptions {
125
125
  export interface CompactionPlan {
126
126
  /** The first message folded away. Everything before it is a system prompt and stays. */
127
127
  from: number;
128
- /** The first message kept whole, always a user message. */
128
+ /**
129
+ * The first message kept whole: a user message, or an assistant message on a fold made inside
130
+ * one question's tool loop (see `question`).
131
+ */
129
132
  cut: number;
130
- /** The messages from `from` to `cut`, the ones the summary replaces. */
133
+ /**
134
+ * The user message kept whole between the summary and `cut`, on a fold made inside its tool
135
+ * loop. Absent on a fold that cuts at a user message.
136
+ */
137
+ question?: number;
138
+ /** The messages from `from` to `cut`, `question` left out: the ones the summary replaces. */
131
139
  toSummarise: Message[];
132
140
  /** The summary an earlier compaction left, which this one continues. */
133
141
  previous?: string;
@@ -155,6 +163,13 @@ export interface CompactionPlan {
155
163
  * than summarised as if it were conversation. No plan comes back when the window is not full
156
164
  * enough, or when the only legal cut folds too little to pay for the summary.
157
165
  *
166
+ * A transcript that is one question and a long tool loop has no user message to cut at, so where
167
+ * no user cut folds anything the plan cuts inside the last question's loop instead: the question
168
+ * is kept whole as `question`, the assistant and tool messages after it are folded up to an
169
+ * assistant message, and the tail resumes there — every tool result in it still follows its call.
170
+ * `applyCompaction` sends the question after the summary. A transcript with a user cut is planned
171
+ * exactly as it was.
172
+ *
158
173
  * Both of those are recovered by reading the transcript, which is what a host whose array holds
159
174
  * everything it sends has to do. A host that keeps its fold as a record beside an append-only
160
175
  * transcript — its system prompt a separate argument, no summary message in the array at all —
@@ -206,6 +221,11 @@ export interface CompactionRecord {
206
221
  summary: string;
207
222
  /** Index into the transcript the plan was made for: the first message still sent whole. */
208
223
  through: number;
224
+ /**
225
+ * The user message sent whole after the summary and ahead of `through`, on a fold made inside
226
+ * that question's tool loop. Absent on a fold that cut at a user message.
227
+ */
228
+ question?: number;
209
229
  /** ISO 8601, so the chat can show where history was folded and how stale the notes are. */
210
230
  at: string;
211
231
  }
@@ -213,7 +233,8 @@ export interface CompactionRecord {
213
233
  export interface CompactionRunOptions {
214
234
  /**
215
235
  * Hooks to tell. `context` is extended with `compacting` and `range`, whose indexes are the
216
- * plan's — and so the host's own, for a plan made over a stored transcript. `honourVeto` waits
236
+ * plan's — and so the host's own, for a plan made over a stored transcript. `compacting` leaves
237
+ * out a `question` the fold keeps. `honourVeto` waits
217
238
  * for the hooks and lets one stop the compaction; without it they add no latency.
218
239
  */
219
240
  hooks?: {
@@ -221,6 +242,14 @@ export interface CompactionRunOptions {
221
242
  context: HookContext;
222
243
  onNote?: OnNote;
223
244
  honourVeto?: boolean;
245
+ /**
246
+ * What the transcript's first message is numbered as in the `compacting` uuids: the host's
247
+ * stored index of `messages[0]`, for a host that numbers messages across steps or requests.
248
+ * See `turnMessages`.
249
+ *
250
+ * @defaultValue `0`
251
+ */
252
+ offset?: number;
224
253
  };
225
254
  /**
226
255
  * The window is already exceeded — the caller caught a `ContextOverflow`, or is compacting to
@@ -256,7 +285,8 @@ export declare function runCompaction(messages: Message[], plan: CompactionPlan,
256
285
  * Absent, the leading `system` messages are found by scanning, and zero of them is the ordinary
257
286
  * case for a host whose system prompt is a separate argument.
258
287
  * @returns `messages` itself when there is no record or its summary is blank, otherwise a new
259
- * array: the kept head, one summary message, then everything from the record's `through` on.
288
+ * array: the kept head, one summary message, the record's `question` when it has one and the head
289
+ * does not already hold it, then everything from the record's `through` on.
260
290
  *
261
291
  * @remarks
262
292
  * The inverse of storing a `CompactionRecord`, and the shape `planCompaction` expects to meet
@@ -264,7 +294,7 @@ export declare function runCompaction(messages: Message[], plan: CompactionPlan,
264
294
  * continues these notes rather than summarising them a second time. Any earlier summary message in
265
295
  * the kept head is dropped, since the record's already contains it.
266
296
  */
267
- export declare function applyCompaction(messages: Message[], record?: Pick<CompactionRecord, 'summary' | 'through'>, { from }?: {
297
+ export declare function applyCompaction(messages: Message[], record?: Pick<CompactionRecord, 'summary' | 'through' | 'question'>, { from }?: {
268
298
  from?: number;
269
299
  }): Message[];
270
300
  /**
@@ -278,7 +308,8 @@ export declare function applyCompaction(messages: Message[], record?: Pick<Compa
278
308
  * prompts, when the host keeps them in the array. Zero is the stored-fold case,
279
309
  * where the summary is the request's first message.
280
310
  * @returns The index in the request. One before the record's `through` gives `head`, where the
281
- * summary sits — an index among the kept head messages themselves included.
311
+ * summary sits — an index among the kept head messages themselves included — except the record's
312
+ * `question`, which is sent whole right after the summary.
282
313
  *
283
314
  * @remarks
284
315
  * A transcript that stays append-only and a request that does not are two numberings of the same
@@ -286,7 +317,7 @@ export declare function applyCompaction(messages: Message[], record?: Pick<Compa
286
317
  * hook — has to say which it is in. An index inside the folded stretch answers with the summary
287
318
  * message that now stands for it.
288
319
  */
289
- export declare const requestIndex: (index: number, record?: Pick<CompactionRecord, 'through'>, head?: number) => number;
320
+ export declare const requestIndex: (index: number, record?: Pick<CompactionRecord, 'through' | 'question'>, head?: number) => number;
290
321
  /**
291
322
  * The transcript with the plan's stretch replaced by one system message holding its summary.
292
323
  *
@@ -142,6 +142,56 @@ export function pruneToolResults(messages, { keepLast = 5, maxChars = 256 } = {}
142
142
  * @returns `index` where it lies from zero to `most`, otherwise the nearer of the two.
143
143
  */
144
144
  const clamp = (index, most) => Math.min(Math.max(index, 0), most);
145
+ /**
146
+ * Where to cut, by `target` when there is one and by `keepRatio` otherwise, among the indexes
147
+ * `isCut` allows.
148
+ *
149
+ * @param messages - The transcript. Not written to.
150
+ * @param search - The weights, the bounds and which indexes are legal cuts. See `CutSearch`.
151
+ * @returns `cut`, the length of `messages` when no legal cut was found, and on a plan made to a
152
+ * `target` the cost it leaves as `after`.
153
+ */
154
+ const findCut = (messages, { from, cost, limit, keepRatio, target, summaryTokens, estimate, isCut, kept }) => {
155
+ let cut = messages.length;
156
+ let after;
157
+ if (target !== undefined && target > 0) {
158
+ // Forward from the head, so the first cut that reaches the target is the one that folds least.
159
+ let folded = 0;
160
+ for (let at = from; at < messages.length; at++) {
161
+ if (isCut(at)) {
162
+ cut = at;
163
+ after = cost - folded + summaryTokens;
164
+ if (after <= limit * target) {
165
+ break;
166
+ }
167
+ }
168
+ if (at !== kept) {
169
+ folded += estimate(messages[at]);
170
+ }
171
+ }
172
+ return { cut, after };
173
+ }
174
+ const budget = limit * keepRatio;
175
+ let tail = 0;
176
+ for (let at = messages.length - 1; at > from; at--) {
177
+ tail += estimate(messages[at]);
178
+ if (tail > budget) {
179
+ break;
180
+ }
181
+ cut = at;
182
+ }
183
+ while (cut < messages.length && isCut(cut) === false) {
184
+ cut++;
185
+ }
186
+ return { cut };
187
+ };
188
+ /**
189
+ * The index of the last user message in a transcript.
190
+ *
191
+ * @param messages - The transcript.
192
+ * @returns The index, or `-1` when there is none.
193
+ */
194
+ const lastUser = (messages) => messages.findLastIndex((message) => message.role === Role.User);
145
195
  /**
146
196
  * Where to fold a transcript that has grown into its window, or `undefined` when it should not be.
147
197
  *
@@ -159,6 +209,13 @@ const clamp = (index, most) => Math.min(Math.max(index, 0), most);
159
209
  * than summarised as if it were conversation. No plan comes back when the window is not full
160
210
  * enough, or when the only legal cut folds too little to pay for the summary.
161
211
  *
212
+ * A transcript that is one question and a long tool loop has no user message to cut at, so where
213
+ * no user cut folds anything the plan cuts inside the last question's loop instead: the question
214
+ * is kept whole as `question`, the assistant and tool messages after it are folded up to an
215
+ * assistant message, and the tail resumes there — every tool result in it still follows its call.
216
+ * `applyCompaction` sends the question after the summary. A transcript with a user cut is planned
217
+ * exactly as it was.
218
+ *
162
219
  * Both of those are recovered by reading the transcript, which is what a host whose array holds
163
220
  * everything it sends has to do. A host that keeps its fold as a record beside an append-only
164
221
  * transcript — its system prompt a separate argument, no summary message in the array at all —
@@ -184,47 +241,53 @@ export function planCompaction(messages, { limit, used, compactAt = COMPACT_AT,
184
241
  const head = systemHead(messages);
185
242
  const from = clamp(givenFrom ?? head.from, messages.length);
186
243
  const previous = givenPrevious ?? head.previous;
187
- let cut = messages.length;
188
- let after;
189
- if (target !== undefined && target > 0) {
190
- // Forward from the head, so the first cut that reaches the target is the one that folds least.
191
- let folded = 0;
192
- for (let at = from; at < messages.length; at++) {
193
- if (messages[at].role === Role.User) {
194
- cut = at;
195
- after = cost - folded + summaryTokens;
196
- if (after <= limit * target) {
197
- break;
198
- }
199
- }
200
- folded += estimate(messages[at]);
201
- }
244
+ const search = { from, cost, limit, keepRatio, target, summaryTokens, estimate };
245
+ const atUser = findCut(messages, { ...search, isCut: (at) => messages[at].role === Role.User });
246
+ const foldsAtUser = atUser.cut < messages.length && atUser.cut - from >= FEWEST_FOLDED;
247
+ if (foldsAtUser) {
248
+ return planFor(messages, from, atUser.cut, previous, atUser.after);
202
249
  }
203
- else {
204
- const budget = limit * keepRatio;
205
- let kept = 0;
206
- for (let at = messages.length - 1; at > from; at--) {
207
- kept += estimate(messages[at]);
208
- if (kept > budget) {
209
- break;
210
- }
211
- cut = at;
212
- }
213
- while (cut < messages.length && messages[cut].role !== Role.User) {
214
- cut++;
215
- }
250
+ // A cut that folds too little but already reaches the target means there is nothing to fold.
251
+ const reachesTarget = target !== undefined && atUser.after !== undefined && atUser.after <= limit * target;
252
+ if (reachesTarget) {
253
+ return undefined;
216
254
  }
217
- if (cut >= messages.length || cut - from < FEWEST_FOLDED) {
255
+ // No user message to cut at: fold inside the last question's tool loop, keeping the question.
256
+ const question = lastUser(messages);
257
+ if (question < 0) {
218
258
  return undefined;
219
259
  }
220
- return {
221
- from,
222
- cut,
223
- toSummarise: messages.slice(from, cut),
224
- ...(previous ? { previous } : {}),
225
- ...(after === undefined ? {} : { after: Math.round(after) }),
226
- };
260
+ const inLoop = findCut(messages, {
261
+ ...search,
262
+ isCut: (at) => at > question && messages[at].role === Role.Assistant,
263
+ kept: question,
264
+ });
265
+ if (inLoop.cut >= messages.length) {
266
+ return undefined;
267
+ }
268
+ const folded = planFor(messages, from, inLoop.cut, previous, inLoop.after, question);
269
+ return folded.toSummarise.length >= FEWEST_FOLDED ? folded : undefined;
227
270
  }
271
+ /**
272
+ * A plan for a cut `planCompaction` has chosen.
273
+ *
274
+ * @param messages - The transcript. Not written to.
275
+ * @param from - The first message folded.
276
+ * @param cut - The first message kept whole.
277
+ * @param [previous] - The summary this one continues.
278
+ * @param [after] - What the request is expected to cost once folded, on a plan made to a target.
279
+ * @param [question] - The user message kept whole ahead of `cut`, on a fold inside its tool loop.
280
+ * Left out of `toSummarise` where it lies between `from` and `cut`.
281
+ * @returns The plan, `toSummarise` a new array of the same message objects.
282
+ */
283
+ const planFor = (messages, from, cut, previous, after, question) => ({
284
+ from,
285
+ cut,
286
+ ...(question === undefined ? {} : { question }),
287
+ toSummarise: messages.slice(from, cut).filter((_, at) => at + from !== question),
288
+ ...(previous ? { previous } : {}),
289
+ ...(after === undefined ? {} : { after: Math.round(after) }),
290
+ });
228
291
  /**
229
292
  * What the summariser is handed for a plan: the earlier summary if there was one, then each
230
293
  * message as its role and at most 4000 characters of its text.
@@ -256,6 +319,22 @@ export function summaryInput(plan) {
256
319
  * resolves with an empty string where the model wrote nothing.
257
320
  */
258
321
  export const summariser = (config, model, { system = SUMMARY_PROMPT, maxTokens = SUMMARY_TOKENS, ...options } = {}) => (text) => ask(config, model, system, text, { maxTokens, ...options });
322
+ /**
323
+ * What `beforeCompact` hooks are told is going: the plan's stretch as `turnMessages` reads it,
324
+ * less the question the fold keeps.
325
+ *
326
+ * @param sessionId - Prefixes every uuid.
327
+ * @param messages - The transcript the plan was made for.
328
+ * @param plan - What `planCompaction` returned for it.
329
+ * @param [offset] - What `messages[0]` is numbered as in the uuids.
330
+ * @returns The user and assistant text from `plan.from` to `plan.cut`, `plan.question` left out.
331
+ */
332
+ const foldedMessages = (sessionId, messages, plan, offset = 0) => {
333
+ const { from, cut, question } = plan;
334
+ const read = (start, end) => turnMessages(sessionId, messages, start, end, { offset });
335
+ const splitsAtQuestion = question !== undefined && question >= from && question < cut;
336
+ return splitsAtQuestion ? [...read(from, question), ...read(question + 1, cut)] : read(from, cut);
337
+ };
259
338
  /**
260
339
  * The hooks and the summariser for a plan, as a record to store rather than a transcript to send.
261
340
  *
@@ -275,7 +354,7 @@ export const summariser = (config, model, { system = SUMMARY_PROMPT, maxTokens =
275
354
  export async function runCompaction(messages, plan, summarise, { hooks, forced = false } = {}) {
276
355
  const context = hooks && {
277
356
  ...hooks.context,
278
- compacting: turnMessages(hooks.context.session.id, messages, plan.from, plan.cut),
357
+ compacting: foldedMessages(hooks.context.session.id, messages, plan, hooks.offset),
279
358
  range: { from: plan.from, through: plan.cut },
280
359
  };
281
360
  let summary;
@@ -295,7 +374,12 @@ export async function runCompaction(messages, plan, summarise, { hooks, forced =
295
374
  if (!summary.trim()) {
296
375
  return undefined;
297
376
  }
298
- return { summary: summary.trim(), through: plan.cut, at: new Date().toISOString() };
377
+ return {
378
+ summary: summary.trim(),
379
+ through: plan.cut,
380
+ ...(plan.question === undefined ? {} : { question: plan.question }),
381
+ at: new Date().toISOString(),
382
+ };
299
383
  }
300
384
  /**
301
385
  * The transcript as the server should see it: the folded head replaced by its summary.
@@ -308,7 +392,8 @@ export async function runCompaction(messages, plan, summarise, { hooks, forced =
308
392
  * Absent, the leading `system` messages are found by scanning, and zero of them is the ordinary
309
393
  * case for a host whose system prompt is a separate argument.
310
394
  * @returns `messages` itself when there is no record or its summary is blank, otherwise a new
311
- * array: the kept head, one summary message, then everything from the record's `through` on.
395
+ * array: the kept head, one summary message, the record's `question` when it has one and the head
396
+ * does not already hold it, then everything from the record's `through` on.
312
397
  *
313
398
  * @remarks
314
399
  * The inverse of storing a `CompactionRecord`, and the shape `planCompaction` expects to meet
@@ -321,12 +406,22 @@ export function applyCompaction(messages, record, { from } = {}) {
321
406
  return messages;
322
407
  }
323
408
  const head = clamp(from ?? systemHead(messages).from, record.through);
409
+ const question = keptQuestion(record, head);
324
410
  return [
325
411
  ...messages.slice(0, head).filter((message) => !isSummary(message)),
326
412
  summaryMessage(record.summary),
413
+ ...(question === undefined ? [] : [messages[question]]),
327
414
  ...messages.slice(record.through),
328
415
  ];
329
416
  }
417
+ /**
418
+ * The question a fold sends after its summary, unless the kept head already holds it.
419
+ *
420
+ * @param record - The fold.
421
+ * @param head - How many messages are kept ahead of the summary.
422
+ * @returns `record.question` where it lies from `head` up to `through`, otherwise `undefined`.
423
+ */
424
+ const keptQuestion = ({ question, through }, head) => question !== undefined && question >= head && question < through ? question : undefined;
330
425
  /**
331
426
  * Where a stored index sits in the request `applyCompaction` builds, once a fold has shifted
332
427
  * everything after it.
@@ -338,7 +433,8 @@ export function applyCompaction(messages, record, { from } = {}) {
338
433
  * prompts, when the host keeps them in the array. Zero is the stored-fold case,
339
434
  * where the summary is the request's first message.
340
435
  * @returns The index in the request. One before the record's `through` gives `head`, where the
341
- * summary sits — an index among the kept head messages themselves included.
436
+ * summary sits — an index among the kept head messages themselves included — except the record's
437
+ * `question`, which is sent whole right after the summary.
342
438
  *
343
439
  * @remarks
344
440
  * A transcript that stays append-only and a request that does not are two numberings of the same
@@ -350,7 +446,12 @@ export const requestIndex = (index, record, head = 0) => {
350
446
  if (!record) {
351
447
  return index;
352
448
  }
353
- return index < record.through ? head : index - record.through + head + 1;
449
+ const question = keptQuestion(record, head);
450
+ if (index === question) {
451
+ return head + 1;
452
+ }
453
+ const afterSummary = question === undefined ? 1 : 2;
454
+ return index < record.through ? head : index - record.through + head + afterSummary;
354
455
  };
355
456
  /**
356
457
  * The transcript with the plan's stretch replaced by one system message holding its summary.
@@ -70,6 +70,14 @@ export interface AgentLoopHooks {
70
70
  preface?: string;
71
71
  /** Hears each note, from before the request and from `afterTurn`. */
72
72
  onNote?: OnNote;
73
+ /**
74
+ * What the run's first message is numbered as in the uuids hooks are told — the host's stored
75
+ * index of `messages[0]`, for a host that numbers messages across steps or runs. Read for
76
+ * `beforeCompact`'s `compacting` and `afterTurn`'s `turn`. See `turnMessages`.
77
+ *
78
+ * @defaultValue `0`
79
+ */
80
+ offset?: number;
73
81
  }
74
82
  /** How full the window is as a step is about to be sent, as `beforeStep` is told it. */
75
83
  export interface StepWindow {
@@ -235,10 +235,10 @@ async function foldBeforeStep(transcript, reported, summarise, { options, charsP
235
235
  return transcript;
236
236
  }
237
237
  const folded = await compactTranscript(transcript, plan, summarise, {
238
- ...(hooks ? { hooks: { run: hooks.run, context: hooks.context, onNote: hooks.onNote } } : {}),
238
+ ...(hooks ? { hooks: { run: hooks.run, context: hooks.context, onNote: hooks.onNote, offset: hooks.offset } } : {}),
239
239
  });
240
240
  if (folded !== transcript) {
241
- notice(`compacted ${plan.cut - plan.from} messages into a summary`);
241
+ notice(`compacted ${plan.toSummarise.length} messages into a summary`);
242
242
  }
243
243
  return folded;
244
244
  }
@@ -260,7 +260,7 @@ const tellAfterTurn = (hooks, messages, at, reply) => notify(hooks.run, HookEven
260
260
  ? {
261
261
  turn: {
262
262
  index: turnIndex(messages, at),
263
- messages: turnMessages(hooks.context.session.id, messages, at),
263
+ messages: turnMessages(hooks.context.session.id, messages, at, undefined, { offset: hooks.offset }),
264
264
  },
265
265
  }
266
266
  : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubicecho/agent-core",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "The endpoint-agnostic half of an OpenAI-compatible agent loop: tool-schema compatibility, on-demand tool loading, one-shot side tasks, run events, and a pooled client.",
5
5
  "keywords": [
6
6
  "openai",