@diyor28/context 1.0.0

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.
Files changed (280) hide show
  1. package/README.md +270 -0
  2. package/dist/__tests__/attachment-selector.test.d.ts +11 -0
  3. package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
  4. package/dist/__tests__/attachment-selector.test.js +449 -0
  5. package/dist/__tests__/attachment-selector.test.js.map +1 -0
  6. package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
  7. package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
  8. package/dist/__tests__/cache-breakpoints.test.js +398 -0
  9. package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
  10. package/dist/__tests__/codecs.test.d.ts +7 -0
  11. package/dist/__tests__/codecs.test.d.ts.map +1 -0
  12. package/dist/__tests__/codecs.test.js +331 -0
  13. package/dist/__tests__/codecs.test.js.map +1 -0
  14. package/dist/__tests__/compactor.test.d.ts +11 -0
  15. package/dist/__tests__/compactor.test.d.ts.map +1 -0
  16. package/dist/__tests__/compactor.test.js +519 -0
  17. package/dist/__tests__/compactor.test.js.map +1 -0
  18. package/dist/__tests__/context-graph.test.d.ts +7 -0
  19. package/dist/__tests__/context-graph.test.d.ts.map +1 -0
  20. package/dist/__tests__/context-graph.test.js +262 -0
  21. package/dist/__tests__/context-graph.test.js.map +1 -0
  22. package/dist/__tests__/hash.test.d.ts +7 -0
  23. package/dist/__tests__/hash.test.d.ts.map +1 -0
  24. package/dist/__tests__/hash.test.js +228 -0
  25. package/dist/__tests__/hash.test.js.map +1 -0
  26. package/dist/__tests__/integration.test.d.ts +15 -0
  27. package/dist/__tests__/integration.test.d.ts.map +1 -0
  28. package/dist/__tests__/integration.test.js +728 -0
  29. package/dist/__tests__/integration.test.js.map +1 -0
  30. package/dist/__tests__/kind-order.test.d.ts +7 -0
  31. package/dist/__tests__/kind-order.test.d.ts.map +1 -0
  32. package/dist/__tests__/kind-order.test.js +243 -0
  33. package/dist/__tests__/kind-order.test.js.map +1 -0
  34. package/dist/__tests__/phase2-integration.test.d.ts +5 -0
  35. package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
  36. package/dist/__tests__/phase2-integration.test.js +222 -0
  37. package/dist/__tests__/phase2-integration.test.js.map +1 -0
  38. package/dist/__tests__/queries.test.d.ts +7 -0
  39. package/dist/__tests__/queries.test.d.ts.map +1 -0
  40. package/dist/__tests__/queries.test.js +254 -0
  41. package/dist/__tests__/queries.test.js.map +1 -0
  42. package/dist/__tests__/token-estimator.test.d.ts +7 -0
  43. package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
  44. package/dist/__tests__/token-estimator.test.js +267 -0
  45. package/dist/__tests__/token-estimator.test.js.map +1 -0
  46. package/dist/adapters/anthropic-estimator.d.ts +38 -0
  47. package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
  48. package/dist/adapters/anthropic-estimator.js +108 -0
  49. package/dist/adapters/anthropic-estimator.js.map +1 -0
  50. package/dist/adapters/attachment-resolver.d.ts +96 -0
  51. package/dist/adapters/attachment-resolver.d.ts.map +1 -0
  52. package/dist/adapters/attachment-resolver.js +176 -0
  53. package/dist/adapters/attachment-resolver.js.map +1 -0
  54. package/dist/adapters/attachment-selector.d.ts +59 -0
  55. package/dist/adapters/attachment-selector.d.ts.map +1 -0
  56. package/dist/adapters/attachment-selector.js +163 -0
  57. package/dist/adapters/attachment-selector.js.map +1 -0
  58. package/dist/adapters/gemini-estimator.d.ts +27 -0
  59. package/dist/adapters/gemini-estimator.d.ts.map +1 -0
  60. package/dist/adapters/gemini-estimator.js +80 -0
  61. package/dist/adapters/gemini-estimator.js.map +1 -0
  62. package/dist/adapters/index.d.ts +12 -0
  63. package/dist/adapters/index.d.ts.map +1 -0
  64. package/dist/adapters/index.js +28 -0
  65. package/dist/adapters/index.js.map +1 -0
  66. package/dist/adapters/memory-store.d.ts +139 -0
  67. package/dist/adapters/memory-store.d.ts.map +1 -0
  68. package/dist/adapters/memory-store.js +187 -0
  69. package/dist/adapters/memory-store.js.map +1 -0
  70. package/dist/adapters/openai-estimator.d.ts +35 -0
  71. package/dist/adapters/openai-estimator.d.ts.map +1 -0
  72. package/dist/adapters/openai-estimator.js +89 -0
  73. package/dist/adapters/openai-estimator.js.map +1 -0
  74. package/dist/adapters/summarizer.d.ts +121 -0
  75. package/dist/adapters/summarizer.d.ts.map +1 -0
  76. package/dist/adapters/summarizer.js +121 -0
  77. package/dist/adapters/summarizer.js.map +1 -0
  78. package/dist/adapters/token-estimator.d.ts +63 -0
  79. package/dist/adapters/token-estimator.d.ts.map +1 -0
  80. package/dist/adapters/token-estimator.js +37 -0
  81. package/dist/adapters/token-estimator.js.map +1 -0
  82. package/dist/builder/context-builder.d.ts +186 -0
  83. package/dist/builder/context-builder.d.ts.map +1 -0
  84. package/dist/builder/context-builder.js +305 -0
  85. package/dist/builder/context-builder.js.map +1 -0
  86. package/dist/builder/context-fork.d.ts +166 -0
  87. package/dist/builder/context-fork.d.ts.map +1 -0
  88. package/dist/builder/context-fork.js +282 -0
  89. package/dist/builder/context-fork.js.map +1 -0
  90. package/dist/builder/index.d.ts +6 -0
  91. package/dist/builder/index.d.ts.map +1 -0
  92. package/dist/builder/index.js +22 -0
  93. package/dist/builder/index.js.map +1 -0
  94. package/dist/codecs/base.d.ts +18 -0
  95. package/dist/codecs/base.d.ts.map +1 -0
  96. package/dist/codecs/base.js +39 -0
  97. package/dist/codecs/base.js.map +1 -0
  98. package/dist/codecs/conversation-history.codec.d.ts +81 -0
  99. package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
  100. package/dist/codecs/conversation-history.codec.js +89 -0
  101. package/dist/codecs/conversation-history.codec.js.map +1 -0
  102. package/dist/codecs/index.d.ts +31 -0
  103. package/dist/codecs/index.d.ts.map +1 -0
  104. package/dist/codecs/index.js +71 -0
  105. package/dist/codecs/index.js.map +1 -0
  106. package/dist/codecs/redacted-stub.codec.d.ts +32 -0
  107. package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
  108. package/dist/codecs/redacted-stub.codec.js +64 -0
  109. package/dist/codecs/redacted-stub.codec.js.map +1 -0
  110. package/dist/codecs/structured-reference.codec.d.ts +40 -0
  111. package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
  112. package/dist/codecs/structured-reference.codec.js +81 -0
  113. package/dist/codecs/structured-reference.codec.js.map +1 -0
  114. package/dist/codecs/system-rules.codec.d.ts +32 -0
  115. package/dist/codecs/system-rules.codec.d.ts.map +1 -0
  116. package/dist/codecs/system-rules.codec.js +62 -0
  117. package/dist/codecs/system-rules.codec.js.map +1 -0
  118. package/dist/codecs/tool-output.codec.d.ts +66 -0
  119. package/dist/codecs/tool-output.codec.d.ts.map +1 -0
  120. package/dist/codecs/tool-output.codec.js +95 -0
  121. package/dist/codecs/tool-output.codec.js.map +1 -0
  122. package/dist/codecs/tool-schema.codec.d.ts +36 -0
  123. package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
  124. package/dist/codecs/tool-schema.codec.js +74 -0
  125. package/dist/codecs/tool-schema.codec.js.map +1 -0
  126. package/dist/codecs/unsafe-text.codec.d.ts +28 -0
  127. package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
  128. package/dist/codecs/unsafe-text.codec.js +63 -0
  129. package/dist/codecs/unsafe-text.codec.js.map +1 -0
  130. package/dist/graph/context-graph.d.ts +121 -0
  131. package/dist/graph/context-graph.d.ts.map +1 -0
  132. package/dist/graph/context-graph.js +166 -0
  133. package/dist/graph/context-graph.js.map +1 -0
  134. package/dist/graph/index.d.ts +8 -0
  135. package/dist/graph/index.d.ts.map +1 -0
  136. package/dist/graph/index.js +24 -0
  137. package/dist/graph/index.js.map +1 -0
  138. package/dist/graph/kind-order.d.ts +60 -0
  139. package/dist/graph/kind-order.d.ts.map +1 -0
  140. package/dist/graph/kind-order.js +113 -0
  141. package/dist/graph/kind-order.js.map +1 -0
  142. package/dist/graph/queries.d.ts +68 -0
  143. package/dist/graph/queries.d.ts.map +1 -0
  144. package/dist/graph/queries.js +240 -0
  145. package/dist/graph/queries.js.map +1 -0
  146. package/dist/graph/views.d.ts +90 -0
  147. package/dist/graph/views.d.ts.map +1 -0
  148. package/dist/graph/views.js +173 -0
  149. package/dist/graph/views.js.map +1 -0
  150. package/dist/index.d.ts +16 -0
  151. package/dist/index.d.ts.map +1 -0
  152. package/dist/index.js +40 -0
  153. package/dist/index.js.map +1 -0
  154. package/dist/pipeline/compactor.d.ts +128 -0
  155. package/dist/pipeline/compactor.d.ts.map +1 -0
  156. package/dist/pipeline/compactor.js +346 -0
  157. package/dist/pipeline/compactor.js.map +1 -0
  158. package/dist/pipeline/index.d.ts +6 -0
  159. package/dist/pipeline/index.d.ts.map +1 -0
  160. package/dist/pipeline/index.js +22 -0
  161. package/dist/pipeline/index.js.map +1 -0
  162. package/dist/pipeline/summarizer.d.ts +18 -0
  163. package/dist/pipeline/summarizer.d.ts.map +1 -0
  164. package/dist/pipeline/summarizer.js +68 -0
  165. package/dist/pipeline/summarizer.js.map +1 -0
  166. package/dist/policies/default-policy.d.ts +29 -0
  167. package/dist/policies/default-policy.d.ts.map +1 -0
  168. package/dist/policies/default-policy.js +58 -0
  169. package/dist/policies/default-policy.js.map +1 -0
  170. package/dist/policies/index.d.ts +5 -0
  171. package/dist/policies/index.d.ts.map +1 -0
  172. package/dist/policies/index.js +21 -0
  173. package/dist/policies/index.js.map +1 -0
  174. package/dist/providers/anthropic-compiler.d.ts +58 -0
  175. package/dist/providers/anthropic-compiler.d.ts.map +1 -0
  176. package/dist/providers/anthropic-compiler.js +182 -0
  177. package/dist/providers/anthropic-compiler.js.map +1 -0
  178. package/dist/providers/capabilities.d.ts +54 -0
  179. package/dist/providers/capabilities.d.ts.map +1 -0
  180. package/dist/providers/capabilities.js +87 -0
  181. package/dist/providers/capabilities.js.map +1 -0
  182. package/dist/providers/gemini-compiler.d.ts +51 -0
  183. package/dist/providers/gemini-compiler.d.ts.map +1 -0
  184. package/dist/providers/gemini-compiler.js +206 -0
  185. package/dist/providers/gemini-compiler.js.map +1 -0
  186. package/dist/providers/index.d.ts +8 -0
  187. package/dist/providers/index.d.ts.map +1 -0
  188. package/dist/providers/index.js +24 -0
  189. package/dist/providers/index.js.map +1 -0
  190. package/dist/providers/openai-compiler.d.ts +46 -0
  191. package/dist/providers/openai-compiler.d.ts.map +1 -0
  192. package/dist/providers/openai-compiler.js +149 -0
  193. package/dist/providers/openai-compiler.js.map +1 -0
  194. package/dist/types/attachment.d.ts +62 -0
  195. package/dist/types/attachment.d.ts.map +1 -0
  196. package/dist/types/attachment.js +6 -0
  197. package/dist/types/attachment.js.map +1 -0
  198. package/dist/types/block.d.ts +61 -0
  199. package/dist/types/block.d.ts.map +1 -0
  200. package/dist/types/block.js +8 -0
  201. package/dist/types/block.js.map +1 -0
  202. package/dist/types/codec.d.ts +58 -0
  203. package/dist/types/codec.d.ts.map +1 -0
  204. package/dist/types/codec.js +6 -0
  205. package/dist/types/codec.js.map +1 -0
  206. package/dist/types/compiled.d.ts +91 -0
  207. package/dist/types/compiled.d.ts.map +1 -0
  208. package/dist/types/compiled.js +6 -0
  209. package/dist/types/compiled.js.map +1 -0
  210. package/dist/types/hash.d.ts +24 -0
  211. package/dist/types/hash.d.ts.map +1 -0
  212. package/dist/types/hash.js +49 -0
  213. package/dist/types/hash.js.map +1 -0
  214. package/dist/types/index.d.ts +10 -0
  215. package/dist/types/index.d.ts.map +1 -0
  216. package/dist/types/index.js +26 -0
  217. package/dist/types/index.js.map +1 -0
  218. package/dist/types/policy.d.ts +128 -0
  219. package/dist/types/policy.d.ts.map +1 -0
  220. package/dist/types/policy.js +55 -0
  221. package/dist/types/policy.js.map +1 -0
  222. package/package.json +55 -0
  223. package/postcss.config.js +4 -0
  224. package/src/__tests__/attachment-selector.test.ts +559 -0
  225. package/src/__tests__/cache-breakpoints.test.ts +566 -0
  226. package/src/__tests__/codecs.test.ts +417 -0
  227. package/src/__tests__/compactor.test.ts +608 -0
  228. package/src/__tests__/context-graph.test.ts +383 -0
  229. package/src/__tests__/hash.test.ts +274 -0
  230. package/src/__tests__/integration.test.ts +866 -0
  231. package/src/__tests__/kind-order.test.ts +312 -0
  232. package/src/__tests__/phase2-integration.test.ts +253 -0
  233. package/src/__tests__/queries.test.ts +387 -0
  234. package/src/__tests__/token-estimator.test.ts +326 -0
  235. package/src/adapters/anthropic-estimator.ts +125 -0
  236. package/src/adapters/attachment-resolver.ts +295 -0
  237. package/src/adapters/attachment-selector.ts +218 -0
  238. package/src/adapters/gemini-estimator.ts +93 -0
  239. package/src/adapters/index.ts +12 -0
  240. package/src/adapters/memory-store.ts +299 -0
  241. package/src/adapters/openai-estimator.ts +105 -0
  242. package/src/adapters/summarizer.ts +250 -0
  243. package/src/adapters/token-estimator.ts +74 -0
  244. package/src/builder/context-builder.ts +467 -0
  245. package/src/builder/context-fork.ts +471 -0
  246. package/src/builder/index.ts +6 -0
  247. package/src/codecs/base.ts +36 -0
  248. package/src/codecs/conversation-history.codec.ts +108 -0
  249. package/src/codecs/index.ts +57 -0
  250. package/src/codecs/redacted-stub.codec.ts +76 -0
  251. package/src/codecs/structured-reference.codec.ts +96 -0
  252. package/src/codecs/system-rules.codec.ts +74 -0
  253. package/src/codecs/tool-output.codec.ts +109 -0
  254. package/src/codecs/tool-schema.codec.ts +87 -0
  255. package/src/codecs/unsafe-text.codec.ts +74 -0
  256. package/src/graph/context-graph.ts +205 -0
  257. package/src/graph/index.ts +8 -0
  258. package/src/graph/kind-order.ts +125 -0
  259. package/src/graph/queries.ts +306 -0
  260. package/src/graph/views.ts +255 -0
  261. package/src/index.ts +31 -0
  262. package/src/pipeline/compactor.ts +563 -0
  263. package/src/pipeline/index.ts +6 -0
  264. package/src/pipeline/summarizer.ts +76 -0
  265. package/src/policies/default-policy.ts +69 -0
  266. package/src/policies/index.ts +5 -0
  267. package/src/providers/anthropic-compiler.ts +294 -0
  268. package/src/providers/capabilities.ts +144 -0
  269. package/src/providers/gemini-compiler.ts +272 -0
  270. package/src/providers/index.ts +8 -0
  271. package/src/providers/openai-compiler.ts +191 -0
  272. package/src/types/attachment.ts +86 -0
  273. package/src/types/block.ts +84 -0
  274. package/src/types/codec.ts +68 -0
  275. package/src/types/compiled.ts +109 -0
  276. package/src/types/hash.ts +58 -0
  277. package/src/types/index.ts +10 -0
  278. package/src/types/policy.ts +194 -0
  279. package/tsconfig.json +21 -0
  280. package/vitest.config.ts +21 -0
package/README.md ADDED
@@ -0,0 +1,270 @@
1
+ # @foundry/context
2
+
3
+ Context management library for LLM conversations with deterministic block ordering, token budgeting, and multi-provider support.
4
+
5
+ ## Features
6
+
7
+ - **Deterministic block ordering**: KIND_ORDER ensures consistent context compilation
8
+ - **Content-addressed blocks**: Stable hashing for deduplication and caching
9
+ - **Token budgeting**: First-class token estimation and overflow handling
10
+ - **Multi-provider support**: Anthropic Claude, OpenAI GPT, Google Gemini
11
+ - **Codec system**: Extensible block rendering with validation
12
+ - **Automatic compaction**: Tool output pruning and history summarization
13
+ - **Sensitivity filtering**: Fine-grained content control for sensitive data
14
+ - **Attachment management**: Budget-aware file selection
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ pnpm add @foundry/context
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ ```typescript
25
+ import {
26
+ ContextBuilder,
27
+ DEFAULT_POLICY,
28
+ BUILT_IN_CODECS,
29
+ AnthropicTokenEstimator,
30
+ AnthropicCompiler,
31
+ } from '@foundry/context';
32
+
33
+ // 1. Create a context builder with default policy
34
+ const builder = new ContextBuilder({
35
+ policy: DEFAULT_POLICY,
36
+ codecs: BUILT_IN_CODECS,
37
+ tokenEstimator: new AnthropicTokenEstimator('claude-sonnet-4-5'),
38
+ });
39
+
40
+ // 2. Add context blocks
41
+ builder.addBlock({
42
+ kind: 'pinned',
43
+ codecId: 'system-rules',
44
+ payload: {
45
+ rules: ['Be helpful and concise', 'Always validate inputs'],
46
+ },
47
+ sensitivity: 'public',
48
+ });
49
+
50
+ builder.addBlock({
51
+ kind: 'history',
52
+ codecId: 'conversation-history',
53
+ payload: {
54
+ messages: [
55
+ { role: 'user', content: 'Hello!' },
56
+ { role: 'assistant', content: 'Hi! How can I help you?' },
57
+ ],
58
+ },
59
+ sensitivity: 'public',
60
+ });
61
+
62
+ // 3. Compile to provider-specific format
63
+ const compiled = AnthropicCompiler.compile(builder.getGraph());
64
+
65
+ // 4. Send to LLM
66
+ const response = await anthropic.messages.create({
67
+ model: 'claude-sonnet-4-5',
68
+ max_tokens: 8000,
69
+ system: compiled.system,
70
+ messages: compiled.messages,
71
+ });
72
+ ```
73
+
74
+ ## Core Concepts
75
+
76
+ ### Context Blocks
77
+
78
+ Context blocks are the fundamental unit of context management. Each block has:
79
+
80
+ - **kind**: Determines ordering and priority (`pinned`, `reference`, `memory`, `state`, `tool_output`, `history`, `turn`)
81
+ - **codecId**: Defines how the block is rendered (e.g., `system-rules`, `tool-schema`, `conversation-history`)
82
+ - **payload**: The actual content (codec-specific structure)
83
+ - **sensitivity**: Access level (`public`, `internal`, `confidential`, `secret`)
84
+
85
+ ### Block Ordering (KIND_ORDER)
86
+
87
+ Blocks are automatically ordered by kind for deterministic compilation:
88
+
89
+ 1. `pinned` - System rules, core instructions (always at top)
90
+ 2. `reference` - Documentation, schemas, guidelines
91
+ 3. `memory` - Long-term facts, user preferences
92
+ 4. `state` - Workflow state, execution context
93
+ 5. `tool_output` - Tool/function call results
94
+ 6. `history` - Conversation history
95
+ 7. `turn` - Current user message (always at bottom)
96
+
97
+ ### Token Budgeting
98
+
99
+ The library automatically manages token budgets:
100
+
101
+ ```typescript
102
+ const policy = {
103
+ ...DEFAULT_POLICY,
104
+ contextWindow: 200_000,
105
+ completionReserve: 8_000,
106
+ overflowStrategy: 'compact', // Auto-prune old tool outputs
107
+ };
108
+ ```
109
+
110
+ ### Built-in Codecs
111
+
112
+ - `system-rules` - System instructions and rules
113
+ - `tool-schema` - Function/tool definitions
114
+ - `structured-reference` - Markdown documentation
115
+ - `conversation-history` - Chat messages
116
+ - `tool-output` - Function call results
117
+ - `redacted-stub` - Placeholder for sensitive content
118
+ - `unsafe-text` - Raw text (bypasses validation)
119
+
120
+ ### Custom Codecs
121
+
122
+ Implement the `BlockCodec` interface to create custom renderers:
123
+
124
+ ```typescript
125
+ import { BlockCodec, registerCodec } from '@foundry/context';
126
+
127
+ const myCodec: BlockCodec = {
128
+ codecId: 'my-custom-codec',
129
+
130
+ encode(payload: unknown) {
131
+ // Validate and return typed payload
132
+ return payload as MyPayloadType;
133
+ },
134
+
135
+ render(payload: MyPayloadType) {
136
+ // Return rendered text
137
+ return `# Custom Content\n${payload.text}`;
138
+ },
139
+
140
+ estimateTokens(payload: MyPayloadType) {
141
+ // Return estimated token count
142
+ return payload.text.length / 4;
143
+ },
144
+ };
145
+
146
+ registerCodec(myCodec);
147
+ ```
148
+
149
+ ## Multi-Provider Support
150
+
151
+ ### Anthropic Claude
152
+
153
+ ```typescript
154
+ import { AnthropicTokenEstimator, AnthropicCompiler } from '@foundry/context';
155
+
156
+ const estimator = new AnthropicTokenEstimator('claude-sonnet-4-5');
157
+ const compiled = AnthropicCompiler.compile(graph);
158
+ ```
159
+
160
+ ### OpenAI GPT
161
+
162
+ ```typescript
163
+ import { OpenAITokenEstimator, OpenAICompiler } from '@foundry/context';
164
+
165
+ const estimator = new OpenAITokenEstimator('gpt-5.2');
166
+ const compiled = OpenAICompiler.compile(graph);
167
+ ```
168
+
169
+ ### Google Gemini
170
+
171
+ ```typescript
172
+ import { GeminiTokenEstimator, GeminiCompiler } from '@foundry/context';
173
+
174
+ const estimator = new GeminiTokenEstimator('gemini-3-pro-preview');
175
+ const compiled = GeminiCompiler.compile(graph);
176
+ ```
177
+
178
+ ## Advanced Features
179
+
180
+ ### Automatic Compaction
181
+
182
+ When token budget is exceeded, the library automatically:
183
+
184
+ 1. Prunes old tool outputs (configurable age threshold)
185
+ 2. Summarizes conversation history
186
+ 3. Removes least-recently-used attachments
187
+
188
+ ```typescript
189
+ const policy = {
190
+ ...DEFAULT_POLICY,
191
+ compaction: {
192
+ pruneToolOutputs: true,
193
+ maxToolOutputAge: 3600, // 1 hour
194
+ summarizeHistory: true,
195
+ maxHistoryMessages: 20,
196
+ },
197
+ };
198
+ ```
199
+
200
+ ### Context Forking
201
+
202
+ Create isolated branches for parallel operations:
203
+
204
+ ```typescript
205
+ const fork = builder.fork({
206
+ branchId: 'speculative-execution',
207
+ sensitivity: {
208
+ maxSensitivity: 'public', // Restrict to public data only
209
+ redactRestricted: true,
210
+ },
211
+ });
212
+
213
+ fork.addBlock({
214
+ kind: 'turn',
215
+ codecId: 'conversation-history',
216
+ payload: { messages: [{ role: 'user', content: 'What if...?' }] },
217
+ sensitivity: 'public',
218
+ });
219
+
220
+ // Original builder is unchanged
221
+ ```
222
+
223
+ ### Attachment Management
224
+
225
+ Smart selection of attachments based on budget and priority:
226
+
227
+ ```typescript
228
+ import { AttachmentSelector } from '@foundry/context';
229
+
230
+ const selector = new AttachmentSelector({
231
+ maxTokensTotal: 10_000,
232
+ selectionStrategy: {
233
+ rankBy: ['purpose', 'user_mention', 'recency'],
234
+ purposePriority: {
235
+ evidence: 1, // Highest priority
236
+ input: 2,
237
+ context: 3,
238
+ artifact: 4, // Lowest priority
239
+ },
240
+ },
241
+ });
242
+
243
+ const selected = selector.select(attachments, tokenEstimator);
244
+ ```
245
+
246
+ ## Testing
247
+
248
+ ```bash
249
+ # Run all tests
250
+ pnpm test
251
+
252
+ # Run with coverage
253
+ pnpm test:coverage
254
+
255
+ # Watch mode
256
+ pnpm test:watch
257
+ ```
258
+
259
+ ## Documentation
260
+
261
+ See `/docs` for detailed documentation:
262
+
263
+ - Architecture overview
264
+ - Token estimation guide
265
+ - Custom codec development
266
+ - Provider integration guide
267
+
268
+ ## License
269
+
270
+ MIT
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Unit tests for AttachmentSelector: budget enforcement and ranking.
3
+ *
4
+ * Tests:
5
+ * - Budget enforcement (token limit)
6
+ * - Ranking by purpose, user mention, recency
7
+ * - Deterministic ordering (same input → same selection)
8
+ * - Token estimation for different attachment types
9
+ */
10
+ export {};
11
+ //# sourceMappingURL=attachment-selector.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-selector.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/attachment-selector.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}