@sayknow-cli/agent-core 0.7.21 → 0.7.23

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,7 +2,13 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
- ## [0.7.21] - 2026-11-11
5
+ ## [0.7.23] - 2026-11-12
6
+
7
+ ## [0.7.22] - 2026-11-12
8
+
9
+ ### Added
10
+
11
+ - `shouldPruneAtThreshold(...)` decides whether a session over its compaction threshold prunes tool outputs or compacts: a prune must leave `CompactionSettings.pruneMinHeadroomPercent` (default 20) percent of the threshold free.
6
12
 
7
13
  ## [0.7.17] - 2026-11-05
8
14
 
@@ -33,6 +33,12 @@ export interface CompactionSettings {
33
33
  strategy?: "context-full" | "handoff" | "off";
34
34
  thresholdPercent?: number;
35
35
  thresholdTokens?: number;
36
+ /**
37
+ * Share of the threshold (0-100) a prune at the threshold must leave free to run
38
+ * instead of compaction. Absent uses `DEFAULT_PRUNE_MIN_HEADROOM_PERCENT`; 0 lets
39
+ * any prune that gets back under the threshold run.
40
+ */
41
+ pruneMinHeadroomPercent?: number;
36
42
  /** Opt-in adaptive threshold controls. Absent or disabled keeps the fixed threshold. */
37
43
  adaptive?: AdaptiveCompactionOptions;
38
44
  /** Call-rate state a threshold decision reads; supplied by the session's tracker. */
@@ -7,6 +7,7 @@
7
7
  * covered file are pruned in preference to merely-old results. Protect-window
8
8
  * and minimum-savings hysteresis semantics are unchanged.
9
9
  */
10
+ import { type CompactionSettings } from "./compaction";
10
11
  import type { SessionEntry, SessionMessageEntry } from "./entries";
11
12
  export interface PruneConfig {
12
13
  /** Keep the most recent tool output tokens intact. */
@@ -66,6 +67,19 @@ export declare function shouldRunMaintenancePrune(args: {
66
67
  minSavings: number;
67
68
  cacheEpochResetCost: number;
68
69
  }): boolean;
70
+ export declare const DEFAULT_PRUNE_MIN_HEADROOM_PERCENT = 20;
71
+ /**
72
+ * Whether a session over its compaction threshold should prune tool outputs
73
+ * instead of compacting.
74
+ *
75
+ * Pruning rewrites history the provider has already cached, so the request
76
+ * after it re-writes almost the whole prompt cache. A prune that only just
77
+ * gets back under the threshold is at the threshold again a few requests later
78
+ * and pays that rewrite each time, which costs more than the compaction it
79
+ * postpones. It runs only when it leaves `pruneMinHeadroomPercent` of the
80
+ * threshold free.
81
+ */
82
+ export declare function shouldPruneAtThreshold(contextTokens: number, pruneSavingsTokens: number, contextWindow: number, settings: CompactionSettings, maxOutputTokens?: number): boolean;
69
83
  export interface PruneToolOutputsOptions {
70
84
  /** Lower the usual minimum only when the caller is already over its compaction threshold. */
71
85
  relaxedMinimum?: number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@sayknow-cli/agent-core",
4
- "version": "0.7.21",
4
+ "version": "0.7.23",
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.7.21",
39
- "@sayknow-cli/natives": "0.7.21",
40
- "@sayknow-cli/utils": "0.7.21",
38
+ "@sayknow-cli/ai": "0.7.23",
39
+ "@sayknow-cli/natives": "0.7.23",
40
+ "@sayknow-cli/utils": "0.7.23",
41
41
  "@opentelemetry/api": "^1.9.0"
42
42
  },
43
43
  "devDependencies": {
@@ -138,6 +138,12 @@ export interface CompactionSettings {
138
138
  strategy?: "context-full" | "handoff" | "off";
139
139
  thresholdPercent?: number;
140
140
  thresholdTokens?: number;
141
+ /**
142
+ * Share of the threshold (0-100) a prune at the threshold must leave free to run
143
+ * instead of compaction. Absent uses `DEFAULT_PRUNE_MIN_HEADROOM_PERCENT`; 0 lets
144
+ * any prune that gets back under the threshold run.
145
+ */
146
+ pruneMinHeadroomPercent?: number;
141
147
  /** Opt-in adaptive threshold controls. Absent or disabled keeps the fixed threshold. */
142
148
  adaptive?: AdaptiveCompactionOptions;
143
149
  /** Call-rate state a threshold decision reads; supplied by the session's tracker. */
@@ -11,7 +11,12 @@
11
11
  import type { ToolCall, ToolResultMessage } from "@sayknow-cli/ai";
12
12
  import { sanitizeText } from "@sayknow-cli/utils";
13
13
  import type { AgentMessage } from "../types";
14
- import { estimateEntryTokens, estimateTextTokensHeuristic } from "./compaction";
14
+ import {
15
+ type CompactionSettings,
16
+ estimateEntryTokens,
17
+ estimateTextTokensHeuristic,
18
+ resolveThresholdTokens,
19
+ } from "./compaction";
15
20
  import type { SessionEntry, SessionMessageEntry } from "./entries";
16
21
 
17
22
  export interface PruneConfig {
@@ -753,6 +758,38 @@ export function shouldRunMaintenancePrune(args: {
753
758
  return args.estimatedSavings > args.cacheEpochResetCost;
754
759
  }
755
760
 
761
+ export const DEFAULT_PRUNE_MIN_HEADROOM_PERCENT = 20;
762
+
763
+ function pruneMinHeadroomPercent(settings: CompactionSettings): number {
764
+ const percent = settings.pruneMinHeadroomPercent;
765
+ if (typeof percent !== "number" || !Number.isFinite(percent)) return DEFAULT_PRUNE_MIN_HEADROOM_PERCENT;
766
+ return Math.min(100, Math.max(0, percent));
767
+ }
768
+
769
+ /**
770
+ * Whether a session over its compaction threshold should prune tool outputs
771
+ * instead of compacting.
772
+ *
773
+ * Pruning rewrites history the provider has already cached, so the request
774
+ * after it re-writes almost the whole prompt cache. A prune that only just
775
+ * gets back under the threshold is at the threshold again a few requests later
776
+ * and pays that rewrite each time, which costs more than the compaction it
777
+ * postpones. It runs only when it leaves `pruneMinHeadroomPercent` of the
778
+ * threshold free.
779
+ */
780
+ export function shouldPruneAtThreshold(
781
+ contextTokens: number,
782
+ pruneSavingsTokens: number,
783
+ contextWindow: number,
784
+ settings: CompactionSettings,
785
+ maxOutputTokens = 0,
786
+ ): boolean {
787
+ if (pruneSavingsTokens <= 0) return false;
788
+ const remainingTokens = Math.max(0, contextTokens - pruneSavingsTokens);
789
+ const thresholdTokens = resolveThresholdTokens(contextWindow, settings, maxOutputTokens, remainingTokens);
790
+ return remainingTokens * 100 <= thresholdTokens * (100 - pruneMinHeadroomPercent(settings));
791
+ }
792
+
756
793
  export interface PruneToolOutputsOptions {
757
794
  /** Lower the usual minimum only when the caller is already over its compaction threshold. */
758
795
  relaxedMinimum?: number;