acp-kernel 0.0.82-pr.343.218 → 0.0.82-pr.345.220

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
@@ -2233,6 +2233,27 @@ function computeProtectedRefs(messages, state, config, countTokens = estimateTex
2233
2233
  }
2234
2234
  return result;
2235
2235
  }
2236
+ function computeOpenTurnMemberIds(messages) {
2237
+ const resultCallIds = /* @__PURE__ */ new Set();
2238
+ for (const msg of messages) {
2239
+ if (msg.contentType === "tool-result" && typeof msg.toolCallId === "string") {
2240
+ resultCallIds.add(msg.toolCallId);
2241
+ }
2242
+ }
2243
+ const openCallIds = /* @__PURE__ */ new Set();
2244
+ for (const msg of messages) {
2245
+ if (msg.id && msg.role === "assistant" && msg.contentType === "tool-call" && typeof msg.toolCallId === "string" && !resultCallIds.has(msg.toolCallId)) {
2246
+ openCallIds.add(msg.id);
2247
+ }
2248
+ }
2249
+ if (openCallIds.size === 0) return /* @__PURE__ */ new Set();
2250
+ const excluded = /* @__PURE__ */ new Set();
2251
+ for (const group of computeTurnGroups(messages)) {
2252
+ if (!group.some((id) => openCallIds.has(id))) continue;
2253
+ for (const id of group) excluded.add(id);
2254
+ }
2255
+ return excluded;
2256
+ }
2236
2257
  function buildCompressibleRanges(messages, state, config, protectedZoneRefs, countTokens = estimateTextTokens) {
2237
2258
  let compressibleMsgs = [];
2238
2259
  const protectedMsgs = [];
@@ -2281,6 +2302,20 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
2281
2302
  skipSinceCompressible = false;
2282
2303
  skipSinceProtected = true;
2283
2304
  }
2305
+ const openTurnIds = computeOpenTurnMemberIds(messages);
2306
+ if (openTurnIds.size > 0) {
2307
+ let gapPending = false;
2308
+ const kept = [];
2309
+ for (const info of compressibleMsgs) {
2310
+ if (openTurnIds.has(info.id)) {
2311
+ gapPending = true;
2312
+ continue;
2313
+ }
2314
+ kept.push(gapPending ? { ...info, gapBefore: true } : info);
2315
+ gapPending = false;
2316
+ }
2317
+ compressibleMsgs = kept;
2318
+ }
2284
2319
  const unfoldedIds = computeIntegrityWithdrawals(
2285
2320
  messages,
2286
2321
  new Set(compressibleMsgs.map((info) => info.id))
@@ -2298,6 +2333,13 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
2298
2333
  }
2299
2334
  compressibleMsgs = kept;
2300
2335
  }
2336
+ const gapCount = compressibleMsgs.reduce(
2337
+ (s, info) => s + (info.gapBefore ? 1 : 0),
2338
+ 0
2339
+ );
2340
+ if (gapCount > 0) {
2341
+ compressibleMsgs = refineSegmentsForFoldability(messages, compressibleMsgs);
2342
+ }
2301
2343
  const compressible = [];
2302
2344
  let cur = null;
2303
2345
  for (const info of compressibleMsgs) {
@@ -2367,6 +2409,41 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
2367
2409
  protected: protectedRanges
2368
2410
  };
2369
2411
  }
2412
+ var MAX_REFINEMENT_PASSES = 8;
2413
+ function refineSegmentsForFoldability(messages, infos) {
2414
+ let current = infos;
2415
+ for (let pass = 0; pass < MAX_REFINEMENT_PASSES; pass++) {
2416
+ const segments = [];
2417
+ for (const info of current) {
2418
+ const seg = segments[segments.length - 1];
2419
+ if (seg === void 0 || info.gapBefore) segments.push([info]);
2420
+ else seg.push(info);
2421
+ }
2422
+ const removed = /* @__PURE__ */ new Set();
2423
+ for (const seg of segments) {
2424
+ const withdrawn = computeIntegrityWithdrawals(
2425
+ messages,
2426
+ new Set(seg.map((info) => info.id))
2427
+ ).withdrawn;
2428
+ for (const info of seg) {
2429
+ if (withdrawn.has(info.id)) removed.add(info.id);
2430
+ }
2431
+ }
2432
+ if (removed.size === 0) break;
2433
+ let gapPending = false;
2434
+ const kept = [];
2435
+ for (const info of current) {
2436
+ if (removed.has(info.id)) {
2437
+ gapPending = true;
2438
+ continue;
2439
+ }
2440
+ kept.push(gapPending ? { ...info, gapBefore: true } : info);
2441
+ gapPending = false;
2442
+ }
2443
+ current = kept;
2444
+ }
2445
+ return current;
2446
+ }
2370
2447
  function mergeBatch(batch) {
2371
2448
  const first = batch[0];
2372
2449
  const last = batch[batch.length - 1];
@@ -2403,30 +2480,18 @@ function mergeRangesToThreshold(ranges, minChars) {
2403
2480
  const result = [];
2404
2481
  let batch = [];
2405
2482
  let batchChars = 0;
2406
- const closeBatch = () => {
2407
- if (batch.length > 0) {
2408
- if (batchChars >= minChars) result.push(mergeBatch(batch));
2409
- batch = [];
2410
- batchChars = 0;
2411
- }
2412
- };
2413
2483
  for (const r of ranges) {
2414
- const prev = batch[batch.length - 1];
2415
- if (prev && prev.endIndex !== void 0 && r.startIndex !== void 0 && r.startIndex > prev.endIndex + 1) {
2416
- closeBatch();
2417
- }
2418
2484
  batch.push(r);
2419
2485
  batchChars += rangeChars(r);
2420
- if (batchChars >= minChars) closeBatch();
2486
+ if (batchChars >= minChars) {
2487
+ result.push(mergeBatch(batch));
2488
+ batch = [];
2489
+ batchChars = 0;
2490
+ }
2421
2491
  }
2422
2492
  if (batch.length > 0 && result.length > 0) {
2423
2493
  const prev = result[result.length - 1];
2424
- const contiguous = prev.endIndex === void 0 || batch[0].startIndex === void 0 || batch[0].startIndex <= prev.endIndex + 1;
2425
- if (contiguous) {
2426
- result[result.length - 1] = mergeBatch([prev, ...batch]);
2427
- } else if (batchChars >= minChars) {
2428
- result.push(mergeBatch(batch));
2429
- }
2494
+ result[result.length - 1] = mergeBatch([prev, ...batch]);
2430
2495
  }
2431
2496
  return result;
2432
2497
  }