@sayknow-cli/agent-core 0.6.7 → 0.6.9

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.6.9] - 2026-09-29
6
+
5
7
  ## [0.6.7] - 2026-09-28
6
8
 
7
9
  ### Changed
@@ -56,6 +56,13 @@ export type RemoteCompactionFallbackHealthEvent = {
56
56
  export interface RemoteCompactionFallbackHealthHooks {
57
57
  recordRemoteCompactionFallback(event: RemoteCompactionFallbackHealthEvent): void;
58
58
  }
59
+ /**
60
+ * With no configured threshold and adaptive mode off, auto-compaction triggers at the
61
+ * reserve-based limit, but never above this many tokens. On million-token models the
62
+ * reserve-based limit alone let a session carry ~1M tokens into every request.
63
+ */
64
+ export declare const DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS = 300000;
65
+ export declare function isDefaultAutoThresholdCeilingApplied(contextWindow: number, settings: CompactionSettings): boolean;
59
66
  export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings;
60
67
  /**
61
68
  * Calculate total context tokens from usage.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@sayknow-cli/agent-core",
4
- "version": "0.6.7",
4
+ "version": "0.6.9",
5
5
  "description": "General-purpose agent with transport abstraction, state management, and attachment support",
6
6
  "homepage": "https://sayknow-cli.com",
7
7
  "author": "jaybeyond",
@@ -35,9 +35,9 @@
35
35
  "fmt": "biome format --write ."
36
36
  },
37
37
  "dependencies": {
38
- "@sayknow-cli/ai": "0.6.7",
39
- "@sayknow-cli/natives": "0.6.7",
40
- "@sayknow-cli/utils": "0.6.7",
38
+ "@sayknow-cli/ai": "0.6.9",
39
+ "@sayknow-cli/natives": "0.6.9",
40
+ "@sayknow-cli/utils": "0.6.9",
41
41
  "@opentelemetry/api": "^1.9.0"
42
42
  },
43
43
  "devDependencies": {
@@ -160,6 +160,22 @@ function isAbortError(error: unknown): boolean {
160
160
  return error instanceof Error && error.name === "AbortError";
161
161
  }
162
162
 
163
+ /**
164
+ * With no configured threshold and adaptive mode off, auto-compaction triggers at the
165
+ * reserve-based limit, but never above this many tokens. On million-token models the
166
+ * reserve-based limit alone let a session carry ~1M tokens into every request.
167
+ */
168
+ export const DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS = 300_000;
169
+
170
+ export function isDefaultAutoThresholdCeilingApplied(contextWindow: number, settings: CompactionSettings): boolean {
171
+ const thresholdTokens = settings.thresholdTokens;
172
+ if (typeof thresholdTokens === "number" && Number.isFinite(thresholdTokens) && thresholdTokens > 0) return false;
173
+ const thresholdPercent = settings.thresholdPercent;
174
+ if (typeof thresholdPercent === "number" && Number.isFinite(thresholdPercent) && thresholdPercent > 0) return false;
175
+ if (settings.adaptive?.enabled) return false;
176
+ return contextWindow - effectiveReserveTokens(contextWindow, settings) > DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS;
177
+ }
178
+
163
179
  export const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {
164
180
  enabled: true,
165
181
  strategy: "context-full",
@@ -453,7 +469,10 @@ export function resolveThresholdTokens(
453
469
  const thresholdPercent = settings.thresholdPercent;
454
470
  if (typeof thresholdPercent !== "number" || !Number.isFinite(thresholdPercent) || thresholdPercent <= 0) {
455
471
  if (!settings.adaptive?.enabled) {
456
- return contextWindow - effectiveReserveTokens(contextWindow, settings, maxOutputTokens);
472
+ return Math.min(
473
+ contextWindow - effectiveReserveTokens(contextWindow, settings, maxOutputTokens),
474
+ DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS,
475
+ );
457
476
  }
458
477
  // No configured percentage: adaptive supplies its own base.
459
478
  const adaptiveBasePercent = Number.isFinite(settings.adaptive.baseThresholdPercent)
@@ -1212,6 +1231,22 @@ export interface PrepareCompactionOptions {
1212
1231
  contextWindow?: number;
1213
1232
  }
1214
1233
 
1234
+ /**
1235
+ * Leave reserve headroom below the compaction trigger for the kept history.
1236
+ * When the default 300K ceiling applies, cap that headroom to half the threshold
1237
+ * so large context windows do not reduce the safe keep window to zero. The caller
1238
+ * separately clamps an oversized configured keep floor and corrected budget below
1239
+ * the threshold so they cannot prevent an actual compaction.
1240
+ */
1241
+ function resolveThresholdSafeKeepRecentTokens(contextWindow: number, settings: CompactionSettings): number {
1242
+ const thresholdTokens = resolveThresholdTokens(contextWindow, settings);
1243
+ const reserveTokens = effectiveReserveTokens(contextWindow, settings, 0);
1244
+ const headroomTokens = isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1245
+ ? Math.min(reserveTokens, Math.floor(thresholdTokens * 0.5))
1246
+ : reserveTokens;
1247
+ return Math.max(1, thresholdTokens - headroomTokens);
1248
+ }
1249
+
1215
1250
  export function prepareCompaction(
1216
1251
  pathEntries: SessionEntry[],
1217
1252
  settings: CompactionSettings,
@@ -1244,10 +1279,7 @@ export function prepareCompaction(
1244
1279
  const contextWindow = options.contextWindow;
1245
1280
  const thresholdSafeKeepRecentTokens =
1246
1281
  contextWindow !== undefined && Number.isFinite(contextWindow) && contextWindow > 1
1247
- ? Math.max(
1248
- 1,
1249
- resolveThresholdTokens(contextWindow, settings) - effectiveReserveTokens(contextWindow, settings, 0),
1250
- )
1282
+ ? resolveThresholdSafeKeepRecentTokens(contextWindow, settings)
1251
1283
  : configuredKeepRecentTokens;
1252
1284
  const keepRecentTokens = Math.min(configuredKeepRecentTokens, thresholdSafeKeepRecentTokens);
1253
1285
  // Preserve the legacy fixed window for smaller models. At 66k and above,
@@ -1269,13 +1301,26 @@ export function prepareCompaction(
1269
1301
  const historyTokens = pathEntries
1270
1302
  .slice(boundaryStart, boundaryEnd)
1271
1303
  .reduce((tokens, entry) => tokens + estimateEntryTokens(entry), 0);
1304
+ // Under the default ceiling a configured floor above the threshold-safe window
1305
+ // would keep more than the trigger allows (or skip compaction entirely), so the
1306
+ // floor is bounded by that window there.
1307
+ const keepFloorTokens =
1308
+ contextWindow !== undefined && isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1309
+ ? keepRecentTokens
1310
+ : configuredKeepRecentTokens;
1272
1311
  const effectiveKeepRecentTokens =
1273
- configuredKeepRecentTokens > historyTokens
1274
- ? configuredKeepRecentTokens
1312
+ keepFloorTokens > historyTokens
1313
+ ? keepFloorTokens
1275
1314
  : scaledKeepRecentTokens > keepRecentTokens && scaledKeepRecentTokens > historyTokens
1276
1315
  ? keepRecentTokens
1277
1316
  : scaledKeepRecentTokens;
1278
- const keepRecentTokensCorrected = Math.max(1, Math.round(effectiveKeepRecentTokens / appliedRatio));
1317
+ const correctedKeepRecentTokens = Math.max(1, Math.round(effectiveKeepRecentTokens / appliedRatio));
1318
+ const keepRecentTokensCorrected =
1319
+ contextWindow !== undefined &&
1320
+ Number.isFinite(contextWindow) &&
1321
+ isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1322
+ ? Math.min(correctedKeepRecentTokens, resolveThresholdTokens(contextWindow, settings) - 1)
1323
+ : correctedKeepRecentTokens;
1279
1324
 
1280
1325
  const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, keepRecentTokensCorrected);
1281
1326