@gajae-code/agent-core 0.18.0 → 0.18.1

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,13 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.18.1] - 2026-09-29
6
+
7
+ ### Changed
8
+
9
+ - Cap the default non-adaptive auto-compaction threshold at 300,000 tokens; explicit thresholds and adaptive compaction remain unchanged. Opt-in context promotion retains the promoted model's reserve-based headroom.
10
+ - Keep recent context strictly below the default ceiling even when the configured keep floor or token correction would otherwise reach it.
11
+
5
12
  ## [0.18.0] - 2026-09-27
6
13
 
7
14
  ### Added
@@ -54,6 +54,8 @@ export type RemoteCompactionFallbackHealthEvent = {
54
54
  export interface RemoteCompactionFallbackHealthHooks {
55
55
  recordRemoteCompactionFallback(event: RemoteCompactionFallbackHealthEvent): void;
56
56
  }
57
+ export declare const DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS = 300000;
58
+ export declare function isDefaultAutoThresholdCeilingApplied(contextWindow: number, settings: CompactionSettings): boolean;
57
59
  export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings;
58
60
  export declare function computeAdaptiveThresholdPercent(basePercent: number, contextTokens: number, contextWindow: number, state: AdaptiveCompactionDecisionState | undefined, options: AdaptiveCompactionOptions | undefined): number;
59
61
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@gajae-code/agent-core",
4
- "version": "0.18.0",
4
+ "version": "0.18.1",
5
5
  "description": "General-purpose agent with transport abstraction, state management, and attachment support",
6
6
  "homepage": "https://gajae-code.com",
7
7
  "author": "Yeachan-Heo and Gajae Code Contributors",
@@ -32,9 +32,9 @@
32
32
  "fmt": "biome format --write ."
33
33
  },
34
34
  "dependencies": {
35
- "@gajae-code/ai": "0.18.0",
36
- "@gajae-code/natives": "0.18.0",
37
- "@gajae-code/utils": "0.18.0",
35
+ "@gajae-code/ai": "0.18.1",
36
+ "@gajae-code/natives": "0.18.1",
37
+ "@gajae-code/utils": "0.18.1",
38
38
  "@opentelemetry/api": "^1.9.0"
39
39
  },
40
40
  "devDependencies": {
package/src/agent.ts CHANGED
@@ -2326,15 +2326,15 @@ export class Agent {
2326
2326
  // Store the sanitized message only: the raw provider error may carry request
2327
2327
  // bodies, credentials, or tokens (exact-head review P1).
2328
2328
  this.#state.error = sanitizeAgentFailure(err).message;
2329
- this.#emit({
2330
- type: "agent_failed",
2331
- // Runtime-authenticated classifiers only: abort comes from the
2332
- // signal, and a local staging failure comes from the identity-
2333
- // checked managedLocalErrorDiagnostic — a foreign error that
2334
- // self-declares a local kind still maps to agent_failed.
2335
- error: sanitizeAgentFailure(err, runtimeFailureCode),
2336
- scope: handle.scope,
2337
- });
2329
+ if (!abortController.signal.aborted) {
2330
+ this.#emit({
2331
+ type: "agent_failed",
2332
+ // Only the identity-checked local staging diagnostic may assert a
2333
+ // local failure kind; a foreign self-declaration stays agent_failed.
2334
+ error: sanitizeAgentFailure(err, runtimeFailureCode),
2335
+ scope: handle.scope,
2336
+ });
2337
+ }
2338
2338
  this.requestRunTerminal(managedLogicalRunOwner ?? runId, {
2339
2339
  stopReason: abortController.signal.aborted ? "cancelled" : "error",
2340
2340
  messages: [errorMsg],
@@ -158,6 +158,17 @@ function isAbortError(error: unknown): boolean {
158
158
  return error instanceof Error && error.name === "AbortError";
159
159
  }
160
160
 
161
+ export const DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS = 300_000;
162
+
163
+ export function isDefaultAutoThresholdCeilingApplied(contextWindow: number, settings: CompactionSettings): boolean {
164
+ const thresholdTokens = settings.thresholdTokens;
165
+ if (typeof thresholdTokens === "number" && Number.isFinite(thresholdTokens) && thresholdTokens > 0) return false;
166
+ const thresholdPercent = settings.thresholdPercent;
167
+ if (typeof thresholdPercent === "number" && Number.isFinite(thresholdPercent) && thresholdPercent > 0) return false;
168
+ if (settings.adaptive?.enabled) return false;
169
+ return contextWindow - effectiveReserveTokens(contextWindow, settings) > DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS;
170
+ }
171
+
161
172
  export const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {
162
173
  enabled: true,
163
174
  strategy: "context-full",
@@ -431,7 +442,10 @@ export function resolveThresholdTokens(
431
442
  const thresholdPercent = settings.thresholdPercent;
432
443
  if (typeof thresholdPercent !== "number" || !Number.isFinite(thresholdPercent) || thresholdPercent <= 0) {
433
444
  if (!settings.adaptive?.enabled) {
434
- return contextWindow - effectiveReserveTokens(contextWindow, settings, maxOutputTokens);
445
+ return Math.min(
446
+ contextWindow - effectiveReserveTokens(contextWindow, settings, maxOutputTokens),
447
+ DEFAULT_AUTO_THRESHOLD_CEILING_TOKENS,
448
+ );
435
449
  }
436
450
  const adaptiveBasePercent = Number.isFinite(settings.adaptive.baseThresholdPercent)
437
451
  ? Math.min(99, Math.max(1, settings.adaptive.baseThresholdPercent))
@@ -1218,6 +1232,22 @@ export interface PrepareCompactionOptions {
1218
1232
  contextWindow?: number;
1219
1233
  }
1220
1234
 
1235
+ /**
1236
+ * Leave reserve headroom below the compaction trigger for the kept history.
1237
+ * When the default 300K ceiling applies, cap that headroom to half the threshold
1238
+ * so large context windows do not reduce the safe keep window to zero. The caller
1239
+ * separately clamps an oversized configured keep floor and corrected budget below
1240
+ * the threshold so they cannot prevent an actual compaction.
1241
+ */
1242
+ function resolveThresholdSafeKeepRecentTokens(contextWindow: number, settings: CompactionSettings): number {
1243
+ const thresholdTokens = resolveThresholdTokens(contextWindow, settings);
1244
+ const reserveTokens = effectiveReserveTokens(contextWindow, settings, 0);
1245
+ const headroomTokens = isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1246
+ ? Math.min(reserveTokens, Math.floor(thresholdTokens * 0.5))
1247
+ : reserveTokens;
1248
+ return Math.max(1, thresholdTokens - headroomTokens);
1249
+ }
1250
+
1221
1251
  export function prepareCompaction(
1222
1252
  pathEntries: SessionEntry[],
1223
1253
  settings: CompactionSettings,
@@ -1250,11 +1280,9 @@ export function prepareCompaction(
1250
1280
  const contextWindow = options.contextWindow;
1251
1281
  const thresholdSafeKeepRecentTokens =
1252
1282
  contextWindow !== undefined && Number.isFinite(contextWindow) && contextWindow > 1
1253
- ? Math.max(
1254
- 1,
1255
- resolveThresholdTokens(contextWindow, settings) - effectiveReserveTokens(contextWindow, settings, 0),
1256
- )
1283
+ ? resolveThresholdSafeKeepRecentTokens(contextWindow, settings)
1257
1284
  : configuredKeepRecentTokens;
1285
+
1258
1286
  const keepRecentTokens = Math.min(configuredKeepRecentTokens, thresholdSafeKeepRecentTokens);
1259
1287
  // Preserve the legacy fixed window for smaller models. At 66k and above,
1260
1288
  // retain up to 30% of the model context, but never enough to leave the
@@ -1275,13 +1303,26 @@ export function prepareCompaction(
1275
1303
  const historyTokens = pathEntries
1276
1304
  .slice(boundaryStart, boundaryEnd)
1277
1305
  .reduce((tokens, entry) => tokens + estimateEntryTokens(entry), 0);
1306
+ // Under the default ceiling a configured floor above the threshold-safe window
1307
+ // would keep more than the trigger allows (or skip compaction entirely), so the
1308
+ // floor is bounded by that window there.
1309
+ const keepFloorTokens =
1310
+ contextWindow !== undefined && isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1311
+ ? keepRecentTokens
1312
+ : configuredKeepRecentTokens;
1278
1313
  const effectiveKeepRecentTokens =
1279
- configuredKeepRecentTokens > historyTokens
1280
- ? configuredKeepRecentTokens
1314
+ keepFloorTokens > historyTokens
1315
+ ? keepFloorTokens
1281
1316
  : scaledKeepRecentTokens > keepRecentTokens && scaledKeepRecentTokens > historyTokens
1282
1317
  ? keepRecentTokens
1283
1318
  : scaledKeepRecentTokens;
1284
- const keepRecentTokensCorrected = Math.max(1, Math.round(effectiveKeepRecentTokens / appliedRatio));
1319
+ const correctedKeepRecentTokens = Math.max(1, Math.round(effectiveKeepRecentTokens / appliedRatio));
1320
+ const keepRecentTokensCorrected =
1321
+ contextWindow !== undefined &&
1322
+ Number.isFinite(contextWindow) &&
1323
+ isDefaultAutoThresholdCeilingApplied(contextWindow, settings)
1324
+ ? Math.min(correctedKeepRecentTokens, resolveThresholdTokens(contextWindow, settings) - 1)
1325
+ : correctedKeepRecentTokens;
1285
1326
 
1286
1327
  const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, keepRecentTokensCorrected);
1287
1328