acp-kernel 0.0.100-pr.499.359 → 0.0.100-pr.500.360

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/dist/index.js CHANGED
@@ -180,8 +180,11 @@ function collectSummaryAnchors(state, baseIndexById, summaryIndexById) {
180
180
  anchors.sort((left, right) => left.insertAt - right.insertAt);
181
181
  return anchors;
182
182
  }
183
- function buildAnchorPairingIndex(messages) {
184
- const n = messages.length;
183
+ function pairSafeAnchorIndex(messages, index) {
184
+ let safe = index;
185
+ while (safe > 0 && safe < messages.length && messages[safe - 1].role === "assistant" && messages[safe].role === "assistant") {
186
+ safe--;
187
+ }
185
188
  const resultIndexByCallId = /* @__PURE__ */ new Map();
186
189
  messages.forEach((message, at) => {
187
190
  if (message.contentType !== "tool-result") return;
@@ -190,51 +193,27 @@ function buildAnchorPairingIndex(messages) {
190
193
  resultIndexByCallId.set(message.toolCallId, at);
191
194
  }
192
195
  });
193
- const runStart = new Int32Array(n).fill(-1);
194
- for (let i = 0; i < n; ) {
195
- if (messages[i].role !== "assistant") {
196
- i++;
197
- continue;
198
- }
199
- let j = i;
200
- while (j + 1 < n && messages[j + 1].role === "assistant") j++;
201
- for (let k = i; k <= j; k++) runStart[k] = i;
202
- i = j + 1;
203
- }
204
- const prefixEnd = new Int32Array(n + 1);
205
- for (let i = 0; i < n; i++) {
206
- const message = messages[i];
207
- let end = 0;
208
- if (message.role === "assistant" && message.contentType === "tool-call" && typeof message.toolCallId === "string") {
196
+ for (let guard = 0; guard < messages.length; guard++) {
197
+ let moved = safe;
198
+ for (let at = 0; at < safe && at < messages.length; at++) {
199
+ const message = messages[at];
200
+ if (message.role !== "assistant") continue;
201
+ if (message.contentType !== "tool-call") continue;
202
+ if (typeof message.toolCallId !== "string") continue;
209
203
  const resultIndex = resultIndexByCallId.get(message.toolCallId);
210
- if (resultIndex !== void 0) end = resultIndex + 1;
211
- }
212
- prefixEnd[i + 1] = Math.max(prefixEnd[i], end);
213
- }
214
- return { runStart, prefixEnd };
215
- }
216
- function pairSafeAnchorIndex(messages, index, ix) {
217
- const n = messages.length;
218
- let safe = index;
219
- if (safe > 0 && safe < n) {
220
- const start = ix.runStart[safe];
221
- if (start >= 0 && messages[safe - 1].role === "assistant") {
222
- safe = start;
204
+ if (resultIndex !== void 0 && resultIndex >= safe && resultIndex + 1 > moved) {
205
+ moved = resultIndex + 1;
206
+ }
223
207
  }
224
- }
225
- if (safe < 0 || safe > n) return safe;
226
- for (let guard = 0; guard < n; guard++) {
227
- const next = Math.max(safe, ix.prefixEnd[safe]);
228
- if (next === safe) break;
229
- safe = next;
208
+ if (moved === safe) break;
209
+ safe = moved;
230
210
  }
231
211
  return safe;
232
212
  }
233
213
  function rebuildMessages(messages, coveredBases, firstUserIndex, anchors) {
234
- const ix = anchors.length > 0 ? buildAnchorPairingIndex(messages) : null;
235
214
  const safeAnchors = anchors.map((anchor) => ({
236
215
  ...anchor,
237
- insertAt: ix ? pairSafeAnchorIndex(messages, anchor.insertAt, ix) : anchor.insertAt
216
+ insertAt: pairSafeAnchorIndex(messages, anchor.insertAt)
238
217
  })).sort((left, right) => left.insertAt - right.insertAt);
239
218
  const result = [];
240
219
  const pending = [...safeAnchors];
@@ -4429,18 +4408,22 @@ function segmentGroups(items) {
4429
4408
  function estimateTextTokens(text) {
4430
4409
  return Math.ceil(text.length / 4);
4431
4410
  }
4432
- function isSyntheticOrPruned(message, covered) {
4433
- if (message.text?.startsWith(SUMMARY_HEADER)) return true;
4434
- return covered.has(message.id);
4411
+ function isSyntheticOrPruned(message, state) {
4412
+ if (message.text?.startsWith("[Compressed conversation section]"))
4413
+ return true;
4414
+ for (const block of state.blocks) {
4415
+ if (block.active && block.effectiveMessageIds.includes(message.id))
4416
+ return true;
4417
+ }
4418
+ return false;
4435
4419
  }
4436
4420
  function computeProtectedRefs(messages, state, config, countTokens = estimateTextTokens) {
4437
4421
  const preserveN = config.preserveRecentMessages;
4438
4422
  const preserveTokens = config.preserveRecentTokens;
4439
- const covered = coveredMessageIds(state);
4440
4423
  const result = /* @__PURE__ */ new Set();
4441
4424
  const visible = [];
4442
4425
  for (const msg of messages) {
4443
- if (isSyntheticOrPruned(msg, covered)) continue;
4426
+ if (isSyntheticOrPruned(msg, state)) continue;
4444
4427
  if (isNeverPreserveRecent(
4445
4428
  msg,
4446
4429
  config.neverPreserveRecentTools,
@@ -4466,7 +4449,7 @@ function computeProtectedRefs(messages, state, config, countTokens = estimateTex
4466
4449
  if (preserveN > 0) {
4467
4450
  for (let i = messages.length - 1; i >= 0; i--) {
4468
4451
  const msg = messages[i];
4469
- if (msg.role !== "user" || isSyntheticOrPruned(msg, covered)) continue;
4452
+ if (msg.role !== "user" || isSyntheticOrPruned(msg, state)) continue;
4470
4453
  const ref = state.messageRefs.byRaw[msg.id];
4471
4454
  if (ref && ref !== "BLOCKED") result.add(ref);
4472
4455
  break;
@@ -4477,7 +4460,6 @@ function computeProtectedRefs(messages, state, config, countTokens = estimateTex
4477
4460
  function buildCompressibleRanges(messages, state, config, protectedZoneRefs, countTokens = estimateTextTokens) {
4478
4461
  let compressibleMsgs = [];
4479
4462
  const protectedMsgs = [];
4480
- const covered = coveredMessageIds(state);
4481
4463
  const protectedCallIds = collectProtectedToolCallIds(messages, config);
4482
4464
  const latest = collectLatestProtected(messages, config);
4483
4465
  for (const id of latest.callIds) protectedCallIds.add(id);
@@ -4488,7 +4470,7 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
4488
4470
  msgIndex++;
4489
4471
  const ref = state.messageRefs.byRaw[msg.id];
4490
4472
  if (!ref || ref === "BLOCKED") continue;
4491
- if (isSyntheticOrPruned(msg, covered)) {
4473
+ if (isSyntheticOrPruned(msg, state)) {
4492
4474
  skipSinceCompressible = true;
4493
4475
  skipSinceProtected = true;
4494
4476
  continue;