Repository navigation
Expand file tree
/
Copy pathcompact.html
More file actions
273 lines (220 loc) · 20.8 KB
/
Copy pathcompact.html
File metadata and controls
273 lines (220 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>threecode/compact</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="../nimdoc.out.css?v=2.2.10">
<!-- JS -->
<script type="text/javascript" src="../dochack.js?v=2.2.10"></script>
</head>
<body>
<div class="document" id="documentId">
<input type="checkbox" id="nav-toggle" hidden>
<label for="nav-toggle" id="nav-burger">☰</label>
<label for="nav-toggle" id="nav-overlay"></label>
<div class="container">
<h1 class="title">threecode/compact</h1>
<div class="row">
<div class="three columns">
<div class="theme-select-wrapper">
<label for="theme-select">Theme: </label>
<select id="theme-select" onchange="setTheme(this.value)">
<option value="auto">🌗 Match OS</option>
<option value="dark">🌑 Dark</option>
<option value="light">🌕 Light</option>
</select>
</div>
<div id="global-links">
<ul class="simple">
<li><a id="indexLink" href="../theindex.html">Index</a></li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="search" id="searchInput" oninput="search()"/>
</div>
<div>
Group by:
<select onchange="groupBy(this.value)">
<option value="section">Section</option>
<option value="type">Type</option>
</select>
</div>
<ul class="simple simple-toc" id="toc-list">
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#ContextAction" title="ContextAction = enum
caNone, ## within budget, nothing to do
caSummarize ## over threshold and enough history to make it worthwhile">ContextAction</a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#10" id="60">Consts</a></summary>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#SummarizeKeepRecent" title="SummarizeKeepRecent = 8">SummarizeKeepRecent</a></li>
<li><a class="reference" href="#SummarizeMaxTokens" title="SummarizeMaxTokens = 500">SummarizeMaxTokens</a></li>
<li><a class="reference" href="#SummarizerSystemPrompt" title="SummarizerSystemPrompt = """You are summarizing an earlier coding session for later recall. Compress the messages below into one paragraph covering: files read/written, commands run and outcomes, current state (tests green? uncommitted changes? what decision was reached?). Omit everything that's been superseded. No filler."""">SummarizerSystemPrompt</a></li>
<li><a class="reference" href="#SummarizeThresholdFrac" title="SummarizeThresholdFrac = 0.8">SummarizeThresholdFrac</a></li>
<li><a class="reference" href="#SummaryPrefix" title="SummaryPrefix = "Earlier in this session: "">SummaryPrefix</a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">applySummary
<li><a class="reference" href="#applySummary%2CJsonNode%2Cstring" title="applySummary(messages: JsonNode; summary: string;
keepRecent = SummarizeKeepRecent): int">applySummary(messages: JsonNode; summary: string;
keepRecent = SummarizeKeepRecent): int</a></li>
</ul>
<ul class="simple nested-toc-section">contextWindowFor
<li><a class="reference" href="#contextWindowFor%2Cstring" title="contextWindowFor(model: string): int">contextWindowFor(model: string): int</a></li>
<li><a class="reference" href="#contextWindowFor%2CProfile" title="contextWindowFor(p: Profile): int">contextWindowFor(p: Profile): int</a></li>
</ul>
<ul class="simple nested-toc-section">decideContextAction
<li><a class="reference" href="#decideContextAction%2Cint%2Cint%2Cint" title="decideContextAction(promptTokens, windowTokens, msgCount: int;
keepRecent = SummarizeKeepRecent;
threshold = SummarizeThresholdFrac): ContextAction">decideContextAction(promptTokens, windowTokens, msgCount: int;
keepRecent = SummarizeKeepRecent;
threshold = SummarizeThresholdFrac): ContextAction</a></li>
</ul>
<ul class="simple nested-toc-section">summarizeHistory
<li><a class="reference" href="#summarizeHistory%2CJsonNode%2CProfile" title="summarizeHistory(messages: JsonNode; p: Profile;
keepRecent = SummarizeKeepRecent): int">summarizeHistory(messages: JsonNode; p: Profile;
keepRecent = SummarizeKeepRecent): int</a></li>
</ul>
</ul>
</details>
</li>
</ul>
</div>
<div class="nine columns" id="content">
<div id="tocRoot"></div>
<p class="module-desc">Context management: when the conversation approaches the model's context window, older turns are collapsed into a single synthetic recap via a meta-call to the same model (with a dedicated terse system prompt). That recap replaces everything except the system prompt and the last <tt class="docutils literal"><span class="pre"><span class="Identifier">SummarizeKeepRecent</span></span></tt> messages. Summarization is lossy and expensive, so it runs at most once per turn, only when the context crosses the threshold.</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
<a class="reference external" href="util.html">util</a>, <a class="reference external" href="types.html">types</a>, <a class="reference external" href="prompts.html">prompts</a>
</dl>
</div>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">
<div id="ContextAction">
<dt><pre><a href="#ContextAction"><span class="Identifier">ContextAction</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
<span class="Identifier">caNone</span><span class="Other">,</span> <span class="Comment">## within budget, nothing to do</span>
<span class="Identifier">caSummarize</span> <span class="Comment">## over threshold and enough history to make it worthwhile</span></pre></dt>
<dd>
</dd>
</div>
</dl>
</div>
<div class="section" id="10">
<h1><a class="toc-backref" href="#10">Consts</a></h1>
<dl class="item">
<div id="SummarizeKeepRecent">
<dt><pre><a href="#SummarizeKeepRecent"><span class="Identifier">SummarizeKeepRecent</span></a> <span class="Other">=</span> <span class="DecNumber">8</span></pre></dt>
<dd>
</dd>
</div>
<div id="SummarizeMaxTokens">
<dt><pre><a href="#SummarizeMaxTokens"><span class="Identifier">SummarizeMaxTokens</span></a> <span class="Other">=</span> <span class="DecNumber">500</span></pre></dt>
<dd>
</dd>
</div>
<div id="SummarizerSystemPrompt">
<dt><pre><a href="#SummarizerSystemPrompt"><span class="Identifier">SummarizerSystemPrompt</span></a> <span class="Other">=</span> <span class="StringLit">"""You are summarizing an earlier coding session for later recall. Compress the messages below into one paragraph covering: files read/written, commands run and outcomes, current state (tests green? uncommitted changes? what decision was reached?). Omit everything that's been superseded. No filler."""</span></pre></dt>
<dd>
</dd>
</div>
<div id="SummarizeThresholdFrac">
<dt><pre><a href="#SummarizeThresholdFrac"><span class="Identifier">SummarizeThresholdFrac</span></a> <span class="Other">=</span> <span class="FloatNumber">0.8</span></pre></dt>
<dd>
</dd>
</div>
<div id="SummaryPrefix">
<dt><pre><a href="#SummaryPrefix"><span class="Identifier">SummaryPrefix</span></a> <span class="Other">=</span> <span class="StringLit">"Earlier in this session: "</span></pre></dt>
<dd>
</dd>
</div>
</dl>
</div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<dl class="item">
<div id="applySummary-procs-all">
<div id="applySummary,JsonNode,string">
<dt><pre><span class="Keyword">proc</span> <a href="#applySummary%2CJsonNode%2Cstring"><span class="Identifier">applySummary</span></a><span class="Other">(</span><span class="Identifier">messages</span><span class="Other">:</span> <span class="Identifier">JsonNode</span><span class="Other">;</span> <span class="Identifier">summary</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">;</span>
<span class="Identifier">keepRecent</span> <span class="Other">=</span> <span class="Identifier">SummarizeKeepRecent</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Rewrites <tt class="docutils literal"><span class="pre"><span class="Identifier">messages</span></span></tt> in place to <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Identifier">system</span><span class="Punctuation">,</span> <span class="Identifier">synthetic_user_summary</span><span class="Punctuation">,</span> <span class="Operator">...</span><span class="Identifier">last</span> <span class="Identifier">keepRecent</span><span class="Punctuation">]</span></span></tt> and returns the number of messages collapsed (i.e. removed from the middle). Returns 0 without touching <tt class="docutils literal"><span class="pre"><span class="Identifier">messages</span></span></tt> if the prerequisites are not met: array shape, a system message at index 0, and at least <tt class="docutils literal"><span class="pre"><span class="Identifier">keepRecent</span> <span class="Operator">+</span> <span class="DecNumber">4</span></span></tt> messages to justify the call.
</dd>
</div>
</div>
<div id="contextWindowFor-procs-all">
<div id="contextWindowFor,string">
<dt><pre><span class="Keyword">proc</span> <a href="#contextWindowFor%2Cstring"><span class="Identifier">contextWindowFor</span></a><span class="Other">(</span><span class="Identifier">model</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Heuristic fallback for models off the known-good table (experimental, or a provider/model pair we haven't curated). Substring match on well-known slugs; no known collisions in practice. Known-good pairs should go through <tt class="docutils literal"><span class="pre"><span class="Identifier">contextWindowFor</span><span class="Punctuation">(</span><span class="Identifier">p</span><span class="Punctuation">)</span></span></tt> instead, which consults <tt class="docutils literal"><span class="pre"><span class="Identifier">KnownGoodCombos</span></span></tt> for the exact window.
</dd>
</div>
<div id="contextWindowFor,Profile">
<dt><pre><span class="Keyword">proc</span> <a href="#contextWindowFor%2CProfile"><span class="Identifier">contextWindowFor</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">:</span> <a href="types.html#Profile"><span class="Identifier">Profile</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Known-good-aware context window. A curated (provider, model) pair returns its exact advertised window; everything else falls back to the substring heuristic in the <tt class="docutils literal"><span class="pre"><span class="Identifier">model</span><span class="Punctuation">:</span> <span class="Identifier">string</span></span></tt> overload. The table is the source of truth because provider curation (which GLM build a provider actually serves) matters as much as the family.
</dd>
</div>
</div>
<div id="decideContextAction-procs-all">
<div id="decideContextAction,int,int,int">
<dt><pre><span class="Keyword">proc</span> <a href="#decideContextAction%2Cint%2Cint%2Cint"><span class="Identifier">decideContextAction</span></a><span class="Other">(</span><span class="Identifier">promptTokens</span><span class="Other">,</span> <span class="Identifier">windowTokens</span><span class="Other">,</span> <span class="Identifier">msgCount</span><span class="Other">:</span> <span class="Identifier">int</span><span class="Other">;</span>
<span class="Identifier">keepRecent</span> <span class="Other">=</span> <span class="Identifier">SummarizeKeepRecent</span><span class="Other">;</span>
<span class="Identifier">threshold</span> <span class="Other">=</span> <span class="Identifier">SummarizeThresholdFrac</span><span class="Other">)</span><span class="Other">:</span> <a href="#ContextAction"><span class="Identifier">ContextAction</span></a> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Pure policy helper. Given a fresh usage reading and the current message count, decide whether to run the lossy summarizer or do nothing.
</dd>
</div>
</div>
<div id="summarizeHistory-procs-all">
<div id="summarizeHistory,JsonNode,Profile">
<dt><pre><span class="Keyword">proc</span> <a href="#summarizeHistory%2CJsonNode%2CProfile"><span class="Identifier">summarizeHistory</span></a><span class="Other">(</span><span class="Identifier">messages</span><span class="Other">:</span> <span class="Identifier">JsonNode</span><span class="Other">;</span> <span class="Identifier">p</span><span class="Other">:</span> <a href="types.html#Profile"><span class="Identifier">Profile</span></a><span class="Other">;</span>
<span class="Identifier">keepRecent</span> <span class="Other">=</span> <span class="Identifier">SummarizeKeepRecent</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">IOError</span><span class="Other">,</span> <span class="Identifier">KeyError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">,</span> <span class="Identifier">ReadDirEffect</span><span class="Other">,</span>
<span class="Identifier">ReadEnvEffect</span><span class="Other">,</span> <span class="Identifier">ReadIOEffect</span><span class="Other">,</span> <span class="Identifier">WriteIOEffect</span><span class="Other">,</span> <span class="Identifier">TimeEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Collapse old turns into one synthetic user recap via a meta-model call. Returns the number of messages dropped; 0 if we bailed (too few messages, missing system prompt, empty profile, or the summarizer call failed). On failure <tt class="docutils literal"><span class="pre"><span class="Identifier">messages</span></span></tt> is left untouched.
</dd>
</div>
</div>
</dl>
</div>
</div>
</div>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2026-06-27 09:42:01 UTC</small>
</div>
</div>
</div>
</body>
</html>