@compilr-dev/agents 0.3.16 → 0.3.18

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/LICENSE ADDED
@@ -0,0 +1,108 @@
1
+ Functional Source License, Version 1.1, MIT Future License
2
+
3
+ Abbreviation: FSL-1.1-MIT
4
+
5
+ Notice
6
+
7
+ Copyright 2026 Carmelo Scozzola
8
+
9
+ Terms and Conditions
10
+
11
+ Licensor ("We")
12
+
13
+ The party offering the Software under these Terms and Conditions.
14
+
15
+ The Software
16
+
17
+ The "Software" is each version of the software that we make available under
18
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
19
+ Conditions with the Software.
20
+
21
+ License Grant
22
+
23
+ Subject to your compliance with this License Grant and the Patents,
24
+ Redistribution and Trademark clauses below, we hereby grant you the right to
25
+ use, copy, modify, create derivative works, publicly perform, publicly display
26
+ and redistribute the Software for any Permitted Purpose identified below.
27
+
28
+ Permitted Purpose
29
+
30
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
31
+ means making the Software available to others in a commercial product or
32
+ service that:
33
+
34
+ 1. substitutes for the Software;
35
+
36
+ 2. substitutes for any other product or service we offer using the Software
37
+ that exists as of the date we make the Software available; or
38
+
39
+ 3. offers the same or substantially similar functionality as the Software.
40
+
41
+ Permitted Purposes specifically include using the Software:
42
+
43
+ 1. for your internal use and access;
44
+
45
+ 2. for non-commercial education;
46
+
47
+ 3. for non-commercial research; and
48
+
49
+ 4. in connection with professional services that you provide to a licensee
50
+ using the Software in accordance with these Terms and Conditions.
51
+
52
+ Patents
53
+
54
+ To the extent your use for a Permitted Purpose would necessarily infringe our
55
+ patents, the license grant above includes a license under our patents. If you
56
+ make a claim against any party that the Software infringes or contributes to
57
+ the infringement of any patent, then your patent license to the Software ends
58
+ immediately.
59
+
60
+ Redistribution
61
+
62
+ The Terms and Conditions apply to all copies, modifications and derivatives of
63
+ the Software.
64
+
65
+ If you redistribute any copies, modifications or derivatives of the Software,
66
+ you must include a copy of or a link to these Terms and Conditions and not
67
+ remove any copyright notices provided in or with the Software.
68
+
69
+ Disclaimer
70
+
71
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
72
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
73
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
74
+
75
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
76
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
77
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
78
+
79
+ Trademarks
80
+
81
+ Except for displaying the License Details and identifying us as the origin of
82
+ the Software, you have no right under these Terms and Conditions to use our
83
+ trademarks, trade names, service marks or product names.
84
+
85
+ Grant of Future License
86
+
87
+ We hereby irrevocably grant you an additional license to use the Software under
88
+ the MIT license that is effective on the second anniversary of the date we make
89
+ the Software available. On or after that date, you may use the Software under
90
+ the MIT license, in which case the following will apply:
91
+
92
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
93
+ this software and associated documentation files (the "Software"), to deal in
94
+ the Software without restriction, including without limitation the rights to
95
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
96
+ of the Software, and to permit persons to whom the Software is furnished to do
97
+ so, subject to the following conditions:
98
+
99
+ The above copyright notice and this permission notice shall be included in all
100
+ copies or substantial portions of the Software.
101
+
102
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
103
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
104
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
105
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
106
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
107
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
108
+ SOFTWARE.
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  > Lightweight multi-LLM agent library for building CLI AI assistants
14
14
 
15
15
  [![npm version](https://img.shields.io/npm/v/@compilr-dev/agents.svg)](https://www.npmjs.com/package/@compilr-dev/agents)
16
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
16
+ [![License: FSL-1.1-MIT](https://img.shields.io/badge/License-FSL--1.1--MIT-blue.svg)](https://fsl.software/)
17
17
 
18
18
  > [!WARNING]
19
19
  > This package is in beta. APIs may change between minor versions.
@@ -1314,7 +1314,7 @@ const agent = new Agent({ provider, tracing });
1314
1314
 
1315
1315
  ## License
1316
1316
 
1317
- MIT - See [LICENSE](LICENSE) for details.
1317
+ [FSL-1.1-MIT](https://fsl.software/) - See [LICENSE](LICENSE) for details. Converts to MIT after 2 years per version.
1318
1318
 
1319
1319
  ---
1320
1320
 
package/dist/agent.d.ts CHANGED
@@ -15,6 +15,7 @@ import type { DelegationConfig } from './context/delegation-types.js';
15
15
  import { PermissionManager } from './permissions/manager.js';
16
16
  import { ContextManager } from './context/manager.js';
17
17
  import { FileAccessTracker } from './context/file-tracker.js';
18
+ import type { ObservationMaskConfig } from './context/observation-masker.js';
18
19
  import { AnchorManager } from './anchors/manager.js';
19
20
  import { GuardrailManager } from './guardrails/manager.js';
20
21
  import { type RetryConfig } from './utils/index.js';
@@ -278,6 +279,17 @@ export interface AgentConfig {
278
279
  * Requires contextManager to be set. Default: true when contextManager is provided.
279
280
  */
280
281
  autoContextManagement?: boolean;
282
+ /**
283
+ * Observation masking configuration. Masks old tool results in history to reduce tokens.
284
+ * Enabled by default when contextManager is provided. Set to `false` to disable.
285
+ */
286
+ observationMask?: Partial<ObservationMaskConfig> | false;
287
+ /**
288
+ * Use compact text format for tool results in LLM messages.
289
+ * Strips JSON wrappers and metadata, reducing token usage.
290
+ * Enabled by default when contextManager is provided. Set to `false` to disable.
291
+ */
292
+ compactToolResults?: boolean;
281
293
  /**
282
294
  * Event handler for monitoring agent execution
283
295
  */
@@ -878,6 +890,14 @@ export declare class Agent {
878
890
  * File restoration options for post-compaction content injection
879
891
  */
880
892
  private readonly fileRestorationConfig?;
893
+ /**
894
+ * Observation masker for reducing token usage by masking old tool results
895
+ */
896
+ private readonly observationMasker?;
897
+ /**
898
+ * Whether to use compact text format for tool results in LLM messages
899
+ */
900
+ private readonly compactToolResults;
881
901
  constructor(config: AgentConfig);
882
902
  /**
883
903
  * Create an agent with project memory loaded from files.
@@ -1280,6 +1300,14 @@ export declare class Agent {
1280
1300
  * Get context statistics
1281
1301
  */
1282
1302
  getContextStats(): ContextStats | undefined;
1303
+ /**
1304
+ * Get observation masking statistics (tokens saved, observations masked).
1305
+ */
1306
+ getObservationMaskStats(): {
1307
+ maskedCount: number;
1308
+ tokensSaved: number;
1309
+ activeStamps: number;
1310
+ } | undefined;
1283
1311
  /**
1284
1312
  * Get current verbosity level based on context pressure
1285
1313
  */
package/dist/agent.js CHANGED
@@ -10,6 +10,8 @@ import { ContextManager } from './context/manager.js';
10
10
  import { FileAccessTracker } from './context/file-tracker.js';
11
11
  import { createFileTrackingHook } from './context/file-tracking-hook.js';
12
12
  import { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './context/tool-result-delegator.js';
13
+ import { ObservationMasker } from './context/observation-masker.js';
14
+ import { compactToolResult } from './context/result-compactor.js';
13
15
  import { createRecallResultTool } from './tools/builtin/recall-result.js';
14
16
  import { AnchorManager } from './anchors/manager.js';
15
17
  import { GuardrailManager } from './guardrails/manager.js';
@@ -97,6 +99,14 @@ export class Agent {
97
99
  * File restoration options for post-compaction content injection
98
100
  */
99
101
  fileRestorationConfig;
102
+ /**
103
+ * Observation masker for reducing token usage by masking old tool results
104
+ */
105
+ observationMasker;
106
+ /**
107
+ * Whether to use compact text format for tool results in LLM messages
108
+ */
109
+ compactToolResults;
100
110
  constructor(config) {
101
111
  this.provider = config.provider;
102
112
  this.systemPrompt = config.systemPrompt ?? '';
@@ -112,6 +122,12 @@ export class Agent {
112
122
  this.contextManager = config.contextManager;
113
123
  this.autoContextManagement =
114
124
  config.autoContextManagement ?? config.contextManager !== undefined;
125
+ // Observation masking: enabled by default when contextManager is provided, unless explicitly false
126
+ if (config.observationMask !== false && this.contextManager) {
127
+ this.observationMasker = new ObservationMasker(config.observationMask === undefined ? undefined : config.observationMask);
128
+ }
129
+ // Compact tool results: enabled by default when contextManager is provided
130
+ this.compactToolResults = config.compactToolResults ?? this.contextManager !== undefined;
115
131
  this.onEvent = config.onEvent;
116
132
  this.onIterationLimitReached = config.onIterationLimitReached;
117
133
  // State management
@@ -752,6 +768,7 @@ export class Agent {
752
768
  clearHistory() {
753
769
  this.conversationHistory = [];
754
770
  this.contextManager?.reset();
771
+ this.observationMasker?.reset();
755
772
  return this;
756
773
  }
757
774
  /**
@@ -807,6 +824,12 @@ export class Agent {
807
824
  getContextStats() {
808
825
  return this.contextManager?.getStats(this.conversationHistory.length);
809
826
  }
827
+ /**
828
+ * Get observation masking statistics (tokens saved, observations masked).
829
+ */
830
+ getObservationMaskStats() {
831
+ return this.observationMasker?.getStats();
832
+ }
810
833
  /**
811
834
  * Get current verbosity level based on context pressure
812
835
  */
@@ -1915,7 +1938,9 @@ export class Agent {
1915
1938
  type: 'tool_result',
1916
1939
  toolUseId: toolUse.id,
1917
1940
  content: result.success
1918
- ? JSON.stringify(result.result)
1941
+ ? this.compactToolResults
1942
+ ? compactToolResult(toolUse.name, result.result, toolUse.input)
1943
+ : JSON.stringify(result.result)
1919
1944
  : `Error: ${result.error ?? 'Unknown error'}`,
1920
1945
  isError: !result.success,
1921
1946
  },
@@ -1998,7 +2023,9 @@ export class Agent {
1998
2023
  emit({ type: 'tool_end', name: toolUse.name, result, toolUseId: toolUse.id });
1999
2024
  // Build tool result content
2000
2025
  let toolResultContent = result.success
2001
- ? JSON.stringify(result.result)
2026
+ ? this.compactToolResults
2027
+ ? compactToolResult(toolUse.name, result.result, toolUse.input)
2028
+ : JSON.stringify(result.result)
2002
2029
  : `Error: ${result.error ?? 'Unknown error'}`;
2003
2030
  // Context management (only for sequential - parallel handles this after)
2004
2031
  if (!inParallelGroup && this.contextManager && this.autoContextManagement) {
@@ -2071,6 +2098,11 @@ export class Agent {
2071
2098
  iterationToolCalls.push(toolCallEntry);
2072
2099
  messages.push(toolResultMsg);
2073
2100
  newMessages.push(toolResultMsg);
2101
+ // Stamp for observation masking
2102
+ if (this.observationMasker) {
2103
+ const block = toolResultMsg.content[0];
2104
+ this.observationMasker.stamp(toolUse.id, toolUse.name, toolUse.input, block.content.length, this.contextManager?.getTurnCount() ?? 0);
2105
+ }
2074
2106
  }
2075
2107
  }
2076
2108
  else {
@@ -2105,6 +2137,11 @@ export class Agent {
2105
2137
  iterationToolCalls.push(toolCallEntry);
2106
2138
  messages.push(toolResultMsg);
2107
2139
  newMessages.push(toolResultMsg);
2140
+ // Stamp for observation masking
2141
+ if (this.observationMasker) {
2142
+ const block = toolResultMsg.content[0];
2143
+ this.observationMasker.stamp(toolUse.id, toolUse.name, toolUse.input, block.content.length, this.contextManager?.getTurnCount() ?? 0);
2144
+ }
2108
2145
  if (skipped) {
2109
2146
  continue;
2110
2147
  }
@@ -2223,6 +2260,10 @@ export class Agent {
2223
2260
  // Context management: increment turn count and update token count
2224
2261
  if (this.contextManager) {
2225
2262
  this.contextManager.incrementTurn();
2263
+ // Observation masking: mask old tool results in-place before token update
2264
+ if (this.observationMasker) {
2265
+ this.observationMasker.maskHistory(messages, this.contextManager.getTurnCount());
2266
+ }
2226
2267
  await this.contextManager.updateTokenCount(messages);
2227
2268
  }
2228
2269
  // Update internal state tracking
@@ -20,3 +20,6 @@ export { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './tool-result-del
20
20
  export type { ToolResultDelegatorOptions } from './tool-result-delegator.js';
21
21
  export { DEFAULT_DELEGATION_CONFIG } from './delegation-types.js';
22
22
  export type { DelegationConfig, StoredResult, DelegationEvent } from './delegation-types.js';
23
+ export { compactToolResult } from './result-compactor.js';
24
+ export { ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './observation-masker.js';
25
+ export type { ObservationMaskConfig, MaskResult, ObservationMaskStats, } from './observation-masker.js';
@@ -15,3 +15,7 @@ export { createFileTrackingHook, TRACKED_TOOLS } from './file-tracking-hook.js';
15
15
  export { DelegatedResultStore } from './delegated-result-store.js';
16
16
  export { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './tool-result-delegator.js';
17
17
  export { DEFAULT_DELEGATION_CONFIG } from './delegation-types.js';
18
+ // Compact Tool Result Formatting (Phase 2 Token Optimization)
19
+ export { compactToolResult } from './result-compactor.js';
20
+ // Observation Masking (Phase 1 Token Optimization)
21
+ export { ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './observation-masker.js';
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Observation Masker — Phase 1 of Advanced Token Optimization
3
+ *
4
+ * Masks old tool results (observations) in conversation history to reduce token usage.
5
+ * Based on JetBrains Research (Dec 2025): observation masking outperforms LLM summarization,
6
+ * achieving 52% cost reduction with +2.6% higher solve rates.
7
+ *
8
+ * Strategy: In-place masking of conversationHistory after N turns.
9
+ * The agent can re-read from the environment if needed (files, git, etc.).
10
+ */
11
+ import type { Message } from '../providers/types.js';
12
+ export interface ObservationMaskConfig {
13
+ /** Turns after which tool results are masked (default: 6) */
14
+ maskAfterTurns: number;
15
+ /** Minimum content length (chars) to mask — skip tiny results (default: 400 ≈ 100 tokens) */
16
+ minCharsToMask: number;
17
+ /** Tool names to NEVER mask (e.g., recall tools, state queries) */
18
+ neverMask: string[];
19
+ /** Tool names to mask after just 1 turn (large reads, bash output) */
20
+ alwaysMaskEarly: string[];
21
+ }
22
+ export declare const DEFAULT_MASK_CONFIG: ObservationMaskConfig;
23
+ interface TurnStamp {
24
+ turn: number;
25
+ toolName: string;
26
+ /** Short summary of input for mask text (e.g., file path, command) */
27
+ inputSummary: string;
28
+ /** Original content length in chars */
29
+ contentLength: number;
30
+ }
31
+ export interface MaskResult {
32
+ maskedCount: number;
33
+ tokensSaved: number;
34
+ }
35
+ export interface ObservationMaskStats {
36
+ /** Total observations masked this session */
37
+ maskedCount: number;
38
+ /** Estimated tokens saved this session */
39
+ tokensSaved: number;
40
+ /** Active stamps (pending masking) */
41
+ activeStamps: number;
42
+ }
43
+ export declare class ObservationMasker {
44
+ private readonly stamps;
45
+ private readonly config;
46
+ private stats;
47
+ constructor(config?: Partial<ObservationMaskConfig>);
48
+ /**
49
+ * Register a tool result with its turn number and input context.
50
+ * Called immediately after a tool result is added to the messages array.
51
+ */
52
+ stamp(toolUseId: string, toolName: string, input: Record<string, unknown>, contentLength: number, turn: number): void;
53
+ /**
54
+ * Mask old tool results in-place in the messages array.
55
+ * Modifies ToolResultBlock.content directly.
56
+ */
57
+ maskHistory(messages: Message[], currentTurn: number): MaskResult;
58
+ getStats(): ObservationMaskStats;
59
+ /**
60
+ * Reset all state (stamps and stats). Used when clearing history.
61
+ */
62
+ reset(): void;
63
+ /**
64
+ * Get current configuration (for testing/inspection).
65
+ */
66
+ getConfig(): ObservationMaskConfig;
67
+ }
68
+ /**
69
+ * Extract a short summary from tool input for the mask text.
70
+ */
71
+ export declare function extractInputSummary(toolName: string, input: Record<string, unknown>): string;
72
+ /**
73
+ * Build the compact mask text that replaces the original content.
74
+ */
75
+ export declare function buildMaskText(stamp: TurnStamp): string;
76
+ /**
77
+ * Check if a tool result content string is already masked.
78
+ */
79
+ export declare function isMasked(content: string): boolean;
80
+ export {};
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Observation Masker — Phase 1 of Advanced Token Optimization
3
+ *
4
+ * Masks old tool results (observations) in conversation history to reduce token usage.
5
+ * Based on JetBrains Research (Dec 2025): observation masking outperforms LLM summarization,
6
+ * achieving 52% cost reduction with +2.6% higher solve rates.
7
+ *
8
+ * Strategy: In-place masking of conversationHistory after N turns.
9
+ * The agent can re-read from the environment if needed (files, git, etc.).
10
+ */
11
+ export const DEFAULT_MASK_CONFIG = {
12
+ maskAfterTurns: 6,
13
+ minCharsToMask: 400,
14
+ neverMask: ['recall_full_result', 'recall_work'],
15
+ alwaysMaskEarly: ['read_file', 'bash', 'bash_output', 'grep', 'glob'],
16
+ };
17
+ // ============================================================
18
+ // ObservationMasker
19
+ // ============================================================
20
+ export class ObservationMasker {
21
+ stamps = new Map();
22
+ config;
23
+ stats = { maskedCount: 0, tokensSaved: 0 };
24
+ constructor(config) {
25
+ this.config = { ...DEFAULT_MASK_CONFIG, ...config };
26
+ }
27
+ // ----------------------------------------------------------
28
+ // Stamping — called when tool results are added to history
29
+ // ----------------------------------------------------------
30
+ /**
31
+ * Register a tool result with its turn number and input context.
32
+ * Called immediately after a tool result is added to the messages array.
33
+ */
34
+ stamp(toolUseId, toolName, input, contentLength, turn) {
35
+ this.stamps.set(toolUseId, {
36
+ turn,
37
+ toolName,
38
+ inputSummary: extractInputSummary(toolName, input),
39
+ contentLength,
40
+ });
41
+ }
42
+ // ----------------------------------------------------------
43
+ // Masking — called after incrementTurn()
44
+ // ----------------------------------------------------------
45
+ /**
46
+ * Mask old tool results in-place in the messages array.
47
+ * Modifies ToolResultBlock.content directly.
48
+ */
49
+ maskHistory(messages, currentTurn) {
50
+ let tokensSaved = 0;
51
+ let maskedCount = 0;
52
+ for (const msg of messages) {
53
+ if (msg.role !== 'user' || typeof msg.content === 'string')
54
+ continue;
55
+ for (const block of msg.content) {
56
+ if (block.type !== 'tool_result')
57
+ continue;
58
+ if (isMasked(block.content))
59
+ continue;
60
+ const stamp = this.stamps.get(block.toolUseId);
61
+ if (!stamp)
62
+ continue;
63
+ if (this.config.neverMask.includes(stamp.toolName))
64
+ continue;
65
+ const age = currentTurn - stamp.turn;
66
+ const threshold = this.config.alwaysMaskEarly.includes(stamp.toolName)
67
+ ? 1
68
+ : this.config.maskAfterTurns;
69
+ if (age < threshold)
70
+ continue;
71
+ if (stamp.contentLength < this.config.minCharsToMask)
72
+ continue;
73
+ // Build mask and calculate savings
74
+ const maskText = buildMaskText(stamp);
75
+ const savedChars = stamp.contentLength - maskText.length;
76
+ const savedTokens = Math.max(0, Math.ceil(savedChars / 4));
77
+ // Mask in-place
78
+ block.content = maskText;
79
+ tokensSaved += savedTokens;
80
+ maskedCount++;
81
+ // Clean up stamp
82
+ this.stamps.delete(block.toolUseId);
83
+ }
84
+ }
85
+ this.stats.maskedCount += maskedCount;
86
+ this.stats.tokensSaved += tokensSaved;
87
+ return { maskedCount, tokensSaved };
88
+ }
89
+ // ----------------------------------------------------------
90
+ // Stats
91
+ // ----------------------------------------------------------
92
+ getStats() {
93
+ return {
94
+ ...this.stats,
95
+ activeStamps: this.stamps.size,
96
+ };
97
+ }
98
+ /**
99
+ * Reset all state (stamps and stats). Used when clearing history.
100
+ */
101
+ reset() {
102
+ this.stamps.clear();
103
+ this.stats = { maskedCount: 0, tokensSaved: 0 };
104
+ }
105
+ /**
106
+ * Get current configuration (for testing/inspection).
107
+ */
108
+ getConfig() {
109
+ return { ...this.config };
110
+ }
111
+ }
112
+ // ============================================================
113
+ // Pure functions (exported for testing)
114
+ // ============================================================
115
+ /**
116
+ * Extract a short summary from tool input for the mask text.
117
+ */
118
+ export function extractInputSummary(toolName, input) {
119
+ // File operations — use path
120
+ if (toolName === 'read_file' || toolName === 'edit' || toolName === 'write_file') {
121
+ const path = input.path ?? input.file_path;
122
+ if (typeof path === 'string')
123
+ return path;
124
+ }
125
+ // Shell commands — use command (truncated)
126
+ if (toolName === 'bash' || toolName === 'bash_output') {
127
+ const cmd = input.command;
128
+ if (typeof cmd === 'string') {
129
+ return cmd.length > 40 ? cmd.slice(0, 40) + '...' : cmd;
130
+ }
131
+ }
132
+ // Search — use pattern
133
+ if (toolName === 'grep') {
134
+ const pattern = input.pattern;
135
+ if (typeof pattern === 'string')
136
+ return `"${pattern}"`;
137
+ }
138
+ // Glob — use pattern
139
+ if (toolName === 'glob') {
140
+ const pattern = input.pattern;
141
+ if (typeof pattern === 'string')
142
+ return pattern;
143
+ }
144
+ // Git tools — use subcommand or operation
145
+ if (toolName.startsWith('git_')) {
146
+ return toolName.slice(4); // "git_diff" → "diff"
147
+ }
148
+ // Web fetch — use URL
149
+ if (toolName === 'web_fetch') {
150
+ const url = input.url;
151
+ if (typeof url === 'string') {
152
+ return url.length > 60 ? url.slice(0, 60) + '...' : url;
153
+ }
154
+ }
155
+ // Default — just the tool name
156
+ return toolName;
157
+ }
158
+ /**
159
+ * Build the compact mask text that replaces the original content.
160
+ */
161
+ export function buildMaskText(stamp) {
162
+ const { toolName, inputSummary, turn, contentLength } = stamp;
163
+ const lines = String(Math.ceil(contentLength / 80));
164
+ const t = String(turn);
165
+ if (toolName === 'read_file') {
166
+ return `[file:${inputSummary} ~${lines}L read@turn:${t}]`;
167
+ }
168
+ if (toolName === 'bash' || toolName === 'bash_output') {
169
+ return `[cmd:${inputSummary} ~${lines}L@turn:${t}]`;
170
+ }
171
+ if (toolName === 'grep') {
172
+ return `[search:${inputSummary}@turn:${t}]`;
173
+ }
174
+ if (toolName === 'glob') {
175
+ return `[glob:${inputSummary}@turn:${t}]`;
176
+ }
177
+ if (toolName.startsWith('git_')) {
178
+ return `[git:${inputSummary}@turn:${t}]`;
179
+ }
180
+ if (toolName === 'edit' || toolName === 'write_file') {
181
+ return `[write:${inputSummary}@turn:${t}]`;
182
+ }
183
+ // Generic
184
+ const tokens = String(Math.ceil(contentLength / 4));
185
+ return `[tool:${toolName} ${tokens}tok@turn:${t}]`;
186
+ }
187
+ /**
188
+ * Check if a tool result content string is already masked.
189
+ */
190
+ export function isMasked(content) {
191
+ return content.startsWith('[') && content.endsWith(']') && content.includes('@turn:');
192
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Compact Tool Result Formatting — Phase 2 of Advanced Token Optimization
3
+ *
4
+ * Replaces JSON.stringify for tool results in the LLM messages array.
5
+ * Per-tool formatters strip JSON overhead (escaped newlines, metadata fields)
6
+ * and produce a text format the LLM can parse just as well.
7
+ *
8
+ * Only affects what the LLM reads in its message history.
9
+ * CLI formatters, events, and hooks still receive the raw ToolExecutionResult.
10
+ */
11
+ /**
12
+ * Format a tool result as compact text for LLM context.
13
+ * Falls back to JSON.stringify for unknown tools or non-object results.
14
+ */
15
+ export declare function compactToolResult(toolName: string, result: unknown, input?: Record<string, unknown>): string;