micro-models-agent 0.57.0 → 0.57.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.
Files changed (232) hide show
  1. package/CHANGELOG.md +481 -476
  2. package/README.md +358 -358
  3. package/dist/certification/certifications.json +493 -493
  4. package/dist/cli/commands.js +447 -0
  5. package/dist/cli/completer.js +167 -0
  6. package/dist/cli/index.js +2 -0
  7. package/dist/cli/main.js +153 -0
  8. package/dist/cli/plugin-commands.js +36 -0
  9. package/dist/cli/repl-commands.js +761 -0
  10. package/dist/cli/repl.js +702 -0
  11. package/dist/cli/run-result.js +33 -0
  12. package/dist/cli/security-commands.js +164 -0
  13. package/dist/cli/setup.js +237 -0
  14. package/dist/config/config.js +276 -0
  15. package/dist/config/defaults.js +141 -0
  16. package/dist/config/domains.js +179 -0
  17. package/dist/config/experts.js +15 -0
  18. package/dist/config/index.js +4 -0
  19. package/dist/config/security.js +213 -0
  20. package/dist/config/types.js +1 -0
  21. package/dist/core/agent-moe.js +102 -0
  22. package/dist/core/agent.js +1018 -0
  23. package/dist/core/bootstrap.js +481 -0
  24. package/dist/core/crash-handler.js +51 -0
  25. package/dist/core/environment.js +199 -0
  26. package/dist/core/index.js +2 -0
  27. package/dist/core/prompt-builder.js +76 -0
  28. package/dist/core/session-logger.js +251 -0
  29. package/dist/core/types.js +1 -0
  30. package/dist/core/version.js +26 -0
  31. package/dist/core/workspace.js +76 -0
  32. package/dist/i18n/en.json +679 -0
  33. package/dist/i18n/index.js +46 -0
  34. package/dist/i18n/ru.json +679 -0
  35. package/dist/index.js +22 -0
  36. package/dist/llm/image-utils.js +143 -0
  37. package/dist/llm/index.js +4 -0
  38. package/dist/llm/model-loader.js +78 -0
  39. package/dist/llm/openai-compat.js +497 -0
  40. package/dist/llm/orchestrator.js +200 -0
  41. package/dist/llm/provider.js +10 -0
  42. package/dist/llm/response.js +39 -0
  43. package/dist/llm/token-counter.js +39 -0
  44. package/dist/llm/types.js +1 -0
  45. package/dist/logger/app-logger.js +189 -0
  46. package/dist/logger/file-log.js +151 -0
  47. package/dist/logger/index.js +1 -0
  48. package/dist/migration/backup.js +45 -0
  49. package/dist/migration/detect.js +50 -0
  50. package/dist/migration/index.js +2 -0
  51. package/dist/modules/artifacts/store.js +61 -0
  52. package/dist/modules/browser/actions.js +76 -0
  53. package/dist/modules/browser/bridge-client.js +199 -0
  54. package/dist/modules/browser/bridge-path.js +10 -0
  55. package/dist/modules/browser/bridge-server.mjs +219 -219
  56. package/dist/modules/browser/cookie-store.js +24 -0
  57. package/dist/modules/browser/driver.js +136 -0
  58. package/dist/modules/browser/index.js +7 -0
  59. package/dist/modules/browser/module.js +29 -0
  60. package/dist/modules/browser/session.js +342 -0
  61. package/dist/modules/browser/snapshot.js +148 -0
  62. package/dist/modules/browser/types.js +12 -0
  63. package/dist/modules/certification/cli.js +213 -0
  64. package/dist/modules/certification/fact-checker.js +82 -0
  65. package/dist/modules/certification/loader.js +106 -0
  66. package/dist/modules/certification/manifest.js +58 -0
  67. package/dist/modules/certification/runner.js +245 -0
  68. package/dist/modules/certification/scenarios.js +407 -0
  69. package/dist/modules/certification/types.js +1 -0
  70. package/dist/modules/context/chunk-query.js +100 -0
  71. package/dist/modules/context/fact-extractor.js +168 -0
  72. package/dist/modules/context/history.js +15 -0
  73. package/dist/modules/context/index.js +1 -0
  74. package/dist/modules/context/manager.js +440 -0
  75. package/dist/modules/execution/audit-runners.js +206 -0
  76. package/dist/modules/execution/auditor.js +218 -0
  77. package/dist/modules/execution/execution-plugin.js +431 -0
  78. package/dist/modules/execution/index.js +8 -0
  79. package/dist/modules/execution/module.js +625 -0
  80. package/dist/modules/execution/moe-executor.js +304 -0
  81. package/dist/modules/execution/plan-coverage.js +68 -0
  82. package/dist/modules/execution/plan-persister.js +46 -0
  83. package/dist/modules/execution/plan-store.js +196 -0
  84. package/dist/modules/execution/plan-tool.js +677 -0
  85. package/dist/modules/execution/plan-validator.js +153 -0
  86. package/dist/modules/execution/planner.js +94 -0
  87. package/dist/modules/execution/stuck-detector.js +746 -0
  88. package/dist/modules/execution/tracker.js +69 -0
  89. package/dist/modules/execution/types.js +1 -0
  90. package/dist/modules/execution/verifier.js +235 -0
  91. package/dist/modules/execution/windows-commands.js +41 -0
  92. package/dist/modules/hallucination/confidence.js +66 -0
  93. package/dist/modules/hallucination/consistency.js +26 -0
  94. package/dist/modules/hallucination/detector.js +47 -0
  95. package/dist/modules/hallucination/factual.js +169 -0
  96. package/dist/modules/hallucination/index.js +5 -0
  97. package/dist/modules/hallucination/js-identifiers.js +262 -0
  98. package/dist/modules/hallucination/llm-judge.js +101 -0
  99. package/dist/modules/index.js +5 -0
  100. package/dist/modules/indexer/cache.js +40 -0
  101. package/dist/modules/indexer/index.js +3 -0
  102. package/dist/modules/indexer/module.js +246 -0
  103. package/dist/modules/indexer/project-profile.js +183 -0
  104. package/dist/modules/indexer/walker.js +101 -0
  105. package/dist/modules/lsp/check-tool.js +58 -0
  106. package/dist/modules/lsp/client.js +389 -0
  107. package/dist/modules/lsp/command.js +60 -0
  108. package/dist/modules/lsp/config.js +135 -0
  109. package/dist/modules/lsp/index.js +3 -0
  110. package/dist/modules/lsp/module.js +260 -0
  111. package/dist/modules/lsp/probe.js +86 -0
  112. package/dist/modules/lsp/project-root.js +32 -0
  113. package/dist/modules/lsp/startup-check.js +144 -0
  114. package/dist/modules/lsp/types.js +1 -0
  115. package/dist/modules/mcp/client.js +399 -0
  116. package/dist/modules/mcp/index.js +3 -0
  117. package/dist/modules/mcp/module.js +142 -0
  118. package/dist/modules/mcp/registry.js +15 -0
  119. package/dist/modules/memory/index.js +1 -0
  120. package/dist/modules/memory/module.js +96 -0
  121. package/dist/modules/memory/search.js +42 -0
  122. package/dist/modules/memory/store.js +69 -0
  123. package/dist/modules/pipelines/engine.js +60 -0
  124. package/dist/modules/pipelines/index.js +3 -0
  125. package/dist/modules/pipelines/parser.js +56 -0
  126. package/dist/modules/pipelines/template.js +14 -0
  127. package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
  128. package/dist/modules/plugins/builtin/notify.js +9 -0
  129. package/dist/modules/plugins/index.js +1 -0
  130. package/dist/modules/plugins/loader.js +70 -0
  131. package/dist/modules/plugins/manager.js +261 -0
  132. package/dist/modules/plugins/types.js +1 -0
  133. package/dist/modules/pricing/index.js +61 -0
  134. package/dist/modules/pricing/prices.js +129 -0
  135. package/dist/modules/processes/detect.js +34 -0
  136. package/dist/modules/processes/index.js +2 -0
  137. package/dist/modules/processes/registry.js +327 -0
  138. package/dist/modules/processes/runner.js +23 -0
  139. package/dist/modules/providers/create.js +22 -0
  140. package/dist/modules/providers/fallback.js +79 -0
  141. package/dist/modules/providers/health.js +46 -0
  142. package/dist/modules/providers/index.js +5 -0
  143. package/dist/modules/providers/manager.js +161 -0
  144. package/dist/modules/providers/presets.js +128 -0
  145. package/dist/modules/providers/registry.js +22 -0
  146. package/dist/modules/providers/types.js +1 -0
  147. package/dist/modules/registry.js +48 -0
  148. package/dist/modules/security/audit-log.js +136 -0
  149. package/dist/modules/security/audit-notifier.js +292 -0
  150. package/dist/modules/security/command-validator.js +219 -0
  151. package/dist/modules/security/content-scanner.js +53 -0
  152. package/dist/modules/security/data-sanitizer.js +89 -0
  153. package/dist/modules/security/encryption.js +242 -0
  154. package/dist/modules/security/index.js +14 -0
  155. package/dist/modules/security/network-validator.js +88 -0
  156. package/dist/modules/security/path-validator.js +203 -0
  157. package/dist/modules/security/rate-limiter.js +119 -0
  158. package/dist/modules/security/security-policies.js +531 -0
  159. package/dist/modules/security/session-encryption.js +210 -0
  160. package/dist/modules/security/session-isolation.js +95 -0
  161. package/dist/modules/session/index.js +3 -0
  162. package/dist/modules/session/manager.js +172 -0
  163. package/dist/modules/session/module.js +24 -0
  164. package/dist/modules/session/store.js +222 -0
  165. package/dist/modules/session/types.js +1 -0
  166. package/dist/modules/skills/index.js +2 -0
  167. package/dist/modules/skills/loader.js +72 -0
  168. package/dist/modules/skills/matcher.js +27 -0
  169. package/dist/modules/skills/module.js +129 -0
  170. package/dist/modules/types.js +1 -0
  171. package/dist/modules/updater/checker.js +96 -0
  172. package/dist/modules/updater/index.js +2 -0
  173. package/dist/modules/updater/module.js +116 -0
  174. package/dist/modules/user-profile/compressor.js +16 -0
  175. package/dist/modules/user-profile/index.js +1 -0
  176. package/dist/modules/user-profile/profile.js +68 -0
  177. package/dist/skills/builtin/git.md +36 -36
  178. package/dist/skills/builtin/typescript.md +35 -35
  179. package/dist/tools/approve.js +33 -0
  180. package/dist/tools/attach-image.js +101 -0
  181. package/dist/tools/bash.js +519 -0
  182. package/dist/tools/browser.js +115 -0
  183. package/dist/tools/chunk-query.js +100 -0
  184. package/dist/tools/create-dir.js +56 -0
  185. package/dist/tools/delete-file.js +63 -0
  186. package/dist/tools/download-file.js +117 -0
  187. package/dist/tools/edit-file.js +80 -0
  188. package/dist/tools/enable-tools.js +59 -0
  189. package/dist/tools/executor.js +154 -0
  190. package/dist/tools/file-info.js +47 -0
  191. package/dist/tools/filter-tools.js +17 -0
  192. package/dist/tools/glob-tool.js +27 -0
  193. package/dist/tools/grep-tool.js +125 -0
  194. package/dist/tools/hidden-tools-block.js +37 -0
  195. package/dist/tools/index.js +78 -0
  196. package/dist/tools/list-dir.js +49 -0
  197. package/dist/tools/load-skill.js +43 -0
  198. package/dist/tools/mcp-call.js +69 -0
  199. package/dist/tools/move-file.js +86 -0
  200. package/dist/tools/path-utils.js +101 -0
  201. package/dist/tools/pipeline-run.js +145 -0
  202. package/dist/tools/preview.js +2 -0
  203. package/dist/tools/process-kill.js +40 -0
  204. package/dist/tools/process-list.js +37 -0
  205. package/dist/tools/process-log.js +54 -0
  206. package/dist/tools/question.js +141 -0
  207. package/dist/tools/read-file.js +179 -0
  208. package/dist/tools/recall.js +118 -0
  209. package/dist/tools/registry.js +47 -0
  210. package/dist/tools/remember.js +68 -0
  211. package/dist/tools/scope-check.js +32 -0
  212. package/dist/tools/search-history.js +85 -0
  213. package/dist/tools/subagent.js +196 -0
  214. package/dist/tools/types.js +1 -0
  215. package/dist/tools/user-input.js +123 -0
  216. package/dist/tools/web-browse.js +87 -0
  217. package/dist/tools/web-fetch.js +119 -0
  218. package/dist/tools/web-search.js +105 -0
  219. package/dist/tools/write-file.js +82 -0
  220. package/dist/ui/box.js +77 -0
  221. package/dist/ui/colors.js +4 -0
  222. package/dist/ui/diff.js +178 -0
  223. package/dist/ui/index.js +6 -0
  224. package/dist/ui/line-editor.js +822 -0
  225. package/dist/ui/line-math.js +73 -0
  226. package/dist/ui/md-formatter.js +212 -0
  227. package/dist/ui/output.js +13 -0
  228. package/dist/ui/plan-view.js +103 -0
  229. package/dist/ui/renderer.js +259 -0
  230. package/dist/ui/spinner.js +70 -0
  231. package/dist/ui/table.js +144 -0
  232. package/package.json +51 -51
@@ -0,0 +1,440 @@
1
+ import { getMessageText } from "../../llm/provider";
2
+ import { FactExtractor } from "./fact-extractor";
3
+ const COMPACTION_INTERVAL = 15;
4
+ const KEEP_LAST_N = 6;
5
+ function summarizeArgs(args) {
6
+ if (!args)
7
+ return "";
8
+ if (typeof args === "string")
9
+ return args.slice(0, 80);
10
+ try {
11
+ const keys = Object.keys(args);
12
+ return keys.slice(0, 3).join(", ");
13
+ }
14
+ catch {
15
+ return String(args).slice(0, 80);
16
+ }
17
+ }
18
+ function truncate(s, max) {
19
+ if (s.length <= max)
20
+ return s;
21
+ return s.slice(0, max - 3) + "...";
22
+ }
23
+ function looksLikeErrorPaste(text) {
24
+ // Error markers.
25
+ if (/(?:^|\s)ERROR|Error:|error TS\d+|Transform failed|\[plugin:|SyntaxError|Cannot find|Uncaught|exception/i.test(text)) {
26
+ return true;
27
+ }
28
+ // Structural signals of pasted compiler/runtime output: source frames
29
+ // ("270| }_s(...)"), path:line:col references, and stack-trace lines.
30
+ if (/(?:^|\n)\s*\d+\s*\|/.test(text))
31
+ return true;
32
+ if (/[\w./\\-]+\.[a-z]{1,6}:\d+:\d+/.test(text))
33
+ return true;
34
+ if (/(?:^|\n)\s*at\s+\S+/.test(text))
35
+ return true;
36
+ return false;
37
+ }
38
+ export function extractTriedAndFailed(messages) {
39
+ const failures = new Map();
40
+ for (const msg of messages) {
41
+ if (msg.role === "tool" && msg.name && msg.success === false) {
42
+ const key = `${msg.name}:${summarizeArgs(msg.arguments)}`;
43
+ const existing = failures.get(key);
44
+ const errorText = truncate(typeof msg.content === "string" ? msg.content : getMessageText(msg.content), 100);
45
+ if (existing) {
46
+ existing.count++;
47
+ }
48
+ else {
49
+ failures.set(key, {
50
+ tool: msg.name,
51
+ args: summarizeArgs(msg.arguments),
52
+ error: errorText,
53
+ count: 1,
54
+ });
55
+ }
56
+ }
57
+ }
58
+ return Array.from(failures.values()).filter((f) => f.count >= 2);
59
+ }
60
+ export class ContextManager {
61
+ contextWindow;
62
+ messages = [];
63
+ compactedBlock = null;
64
+ iterationsSinceCompaction = 0;
65
+ compactionCount = 0;
66
+ peakTokens = 0;
67
+ budget;
68
+ compactionThreshold;
69
+ facts = new FactExtractor();
70
+ /** Latest real user instruction (skips <system-summary> blocks). */
71
+ lastUserTask = "";
72
+ /** First real user instruction of the session — survives resetUserTurn(). */
73
+ sessionMission = "";
74
+ /** Latest user message that looks like a pasted error/log (never a task). */
75
+ lastUserFeedback = "";
76
+ tokenCounter;
77
+ pendingImageParts = [];
78
+ toolTokens = 0;
79
+ onCompact = null;
80
+ /**
81
+ * Optional short line appended to the compaction summary so the model knows
82
+ * an active plan exists with progress. Wired from bootstrap to the execution
83
+ * module — without it, a 9B model re-creates a plan from scratch right after
84
+ * compaction (observed: ses_msvuao0h — plan_e07pb2 (1/6) was discarded for a
85
+ * fresh plan_xzq9xe the same iteration the old plan was still visible).
86
+ */
87
+ planSummaryProvider = null;
88
+ constructor(contextWindow, contextBudget, tokenCounter) {
89
+ this.contextWindow = contextWindow;
90
+ this.compactionThreshold = contextBudget?.compactionThreshold ?? 0.75;
91
+ this.budget = this.calculateBudget(contextWindow, contextBudget);
92
+ this.tokenCounter = tokenCounter ?? null;
93
+ }
94
+ /**
95
+ * Provide a short "current plan" line for the compaction summary, or null
96
+ * when no plan is active. Injected once at bootstrap (not per-turn state).
97
+ */
98
+ setPlanSummaryProvider(fn) {
99
+ this.planSummaryProvider = fn;
100
+ }
101
+ calculateBudget(window, contextBudget) {
102
+ if (contextBudget) {
103
+ const systemPrompt = Math.floor(window * contextBudget.systemPrompt);
104
+ const responseReserve = Math.floor(window * contextBudget.responseReserve);
105
+ const history = Math.max(0, window - systemPrompt - responseReserve);
106
+ return { systemPrompt, responseReserve, history };
107
+ }
108
+ // Fallback for backwards compatibility (tests, subagents without explicit budget)
109
+ return {
110
+ systemPrompt: Math.floor(window * 0.25),
111
+ responseReserve: Math.floor(window * 0.12),
112
+ history: Math.floor(window * 0.63),
113
+ };
114
+ }
115
+ getBudget() {
116
+ return { ...this.budget };
117
+ }
118
+ getCompactionCount() {
119
+ return this.compactionCount;
120
+ }
121
+ getIterationsSinceCompaction() {
122
+ return this.iterationsSinceCompaction;
123
+ }
124
+ /** Files known from compaction summaries — for seeding the hallucination
125
+ * checker's known-files set so bare filenames survive context resets. */
126
+ getKnownFiles() {
127
+ return this.facts.getKnownFiles();
128
+ }
129
+ noteIteration() {
130
+ this.iterationsSinceCompaction++;
131
+ }
132
+ /**
133
+ * Start a fresh user turn. The 15-iteration compaction interval must not
134
+ * leak across user messages — otherwise a turn that ends at iteration 14
135
+ * forces a compaction on the very first iteration of the next user message
136
+ * (observed: compaction fired on iteration 8 of a new turn, deleting the
137
+ * user's freshly-sent task alongside 40+ old turns). Facts are kept.
138
+ */
139
+ resetUserTurn() {
140
+ this.iterationsSinceCompaction = 0;
141
+ // A new user message begins a new turn — drop the previous turn's task
142
+ // so the fresh instruction becomes the one preserved at compaction. The
143
+ // session mission survives: an error/log paste in the new turn must not
144
+ // erase the original goal.
145
+ this.lastUserTask = "";
146
+ this.lastUserFeedback = "";
147
+ }
148
+ getQuality() {
149
+ const usedTokens = this.getEstimatedTokens();
150
+ const tokenLoad = Math.max(0, 1 - usedTokens / this.budget.history);
151
+ // Recoverable penalty: each compaction loses some information, but the
152
+ // penalty is CAPPED so cumulative compactions can never permanently pin
153
+ // quality below the forced-compaction trigger (40%). Before this cap, a
154
+ // session with ~7+ compactions clamped the term to 0 forever; combined
155
+ // with heavy token load + error density, quality stuck below 40 and a
156
+ // compaction fired on EVERY iteration (observed: 56 compactions in 28 min).
157
+ const COMPACTION_PENALTY_FLOOR = 0.5;
158
+ const compactionLoss = Math.max(COMPACTION_PENALTY_FLOOR, 1 - this.compactionCount * 0.15);
159
+ const msgCount = this.messages.length || 1;
160
+ const errorDensity = Math.max(0, 1 - Math.min(1, this.facts.errorCount / msgCount));
161
+ const freshness = Math.max(0, 1 - this.iterationsSinceCompaction / COMPACTION_INTERVAL);
162
+ const score = tokenLoad * 0.4 + compactionLoss * 0.25 + errorDensity * 0.2 + freshness * 0.15;
163
+ return Math.round(Math.min(100, Math.max(0, score * 100)));
164
+ }
165
+ addMessage(msg) {
166
+ // Track the task the agent is working on so it survives compaction. The
167
+ // user's real instruction is the FIRST non-system-summary user message of
168
+ // the turn; everything after it (tool results, retry nudges) are injected
169
+ // `user` envelopes that must not overwrite it (observed: after compaction
170
+ // the model had forgotten "удали /bicycles, почини стили" and re-read
171
+ // files aimlessly for 40+ iterations).
172
+ if (msg.role === "user") {
173
+ const text = typeof msg.content === "string" ? msg.content : getMessageText(msg.content);
174
+ if (text.trim() && !text.startsWith("<system-summary>") && !text.includes("[Compressed:")) {
175
+ if (looksLikeErrorPaste(text) && (this.lastUserTask || this.sessionMission)) {
176
+ this.lastUserFeedback = text.trim().slice(0, 400);
177
+ }
178
+ else if (!this.lastUserTask && !this.lastUserFeedback) {
179
+ // No new task inside a turn that already captured an error/log paste:
180
+ // the paste's follow-up messages are tool-result continuations, not a
181
+ // fresh instruction, so the session mission must win at compaction.
182
+ this.lastUserTask = text.trim();
183
+ if (!this.sessionMission)
184
+ this.sessionMission = text.trim();
185
+ }
186
+ }
187
+ }
188
+ // Auto-attach pending images to the next user message
189
+ if (msg.role === "user" && this.pendingImageParts.length > 0) {
190
+ const textPart = {
191
+ type: "text",
192
+ text: typeof msg.content === "string" ? msg.content : getMessageText(msg.content),
193
+ };
194
+ msg = {
195
+ ...msg,
196
+ content: [textPart, ...this.pendingImageParts],
197
+ };
198
+ this.pendingImageParts = [];
199
+ }
200
+ this.messages.push(msg);
201
+ const tokens = this.getEstimatedTokens();
202
+ if (tokens > this.peakTokens)
203
+ this.peakTokens = tokens;
204
+ }
205
+ /**
206
+ * Queue an image part to be attached to the next user message.
207
+ */
208
+ addPendingImage(part) {
209
+ this.pendingImageParts.push(part);
210
+ }
211
+ /**
212
+ * Check if there are pending images waiting to be attached.
213
+ */
214
+ hasPendingImages() {
215
+ return this.pendingImageParts.length > 0;
216
+ }
217
+ /**
218
+ * Get pending image parts without clearing them.
219
+ */
220
+ getPendingImages() {
221
+ return [...this.pendingImageParts];
222
+ }
223
+ /**
224
+ * Clear pending images (e.g., if user sends a text-only message).
225
+ */
226
+ clearPendingImages() {
227
+ this.pendingImageParts = [];
228
+ }
229
+ getMessageCount() {
230
+ return this.messages.length;
231
+ }
232
+ // Per-message token cache: messages are treated as immutable once added
233
+ // (compaction/updateSystemPrompt create new objects), so a WeakMap is safe
234
+ // and turns repeated full-history estimation from O(n) encodes into O(1).
235
+ tokenCache = new WeakMap();
236
+ estimateMessageTokensCached(m) {
237
+ let t = this.tokenCache.get(m);
238
+ if (t === undefined) {
239
+ t = this.estimateMessageTokens(m);
240
+ this.tokenCache.set(m, t);
241
+ }
242
+ return t;
243
+ }
244
+ estimateMessageTokens(m) {
245
+ const text = getMessageText(m.content);
246
+ if (this.tokenCounter) {
247
+ let t = this.tokenCounter.count(text);
248
+ // Image tokens: base64 ~130 tokens per 512x512 tile; rough estimate
249
+ if (Array.isArray(m.content)) {
250
+ for (const part of m.content) {
251
+ if (part.type === "image_url" && part.image_url?.url) {
252
+ const b64Len = part.image_url.url.includes(",")
253
+ ? (part.image_url.url.split(",")[1]?.length ?? 0)
254
+ : part.image_url.url.length;
255
+ // ~130 tokens per 512 bytes of base64
256
+ t += Math.ceil(b64Len / 512) * 130;
257
+ }
258
+ }
259
+ }
260
+ if (m.tool_calls) {
261
+ for (const tc of m.tool_calls) {
262
+ t += this.tokenCounter.count(tc.id);
263
+ t += this.tokenCounter.count(tc.function.name);
264
+ t += this.tokenCounter.count(tc.function.arguments);
265
+ t += 4;
266
+ }
267
+ }
268
+ return t;
269
+ }
270
+ let t = Math.ceil(text.length / 2);
271
+ if (Array.isArray(m.content)) {
272
+ for (const part of m.content) {
273
+ if (part.type === "image_url" && part.image_url?.url) {
274
+ const b64Len = part.image_url.url.includes(",")
275
+ ? (part.image_url.url.split(",")[1]?.length ?? 0)
276
+ : part.image_url.url.length;
277
+ t += Math.ceil(b64Len / 512) * 130;
278
+ }
279
+ }
280
+ }
281
+ if (m.tool_calls) {
282
+ for (const tc of m.tool_calls) {
283
+ t += Math.ceil(tc.id.length / 2);
284
+ t += Math.ceil(tc.function.name.length / 2);
285
+ t += Math.ceil(tc.function.arguments.length / 2);
286
+ t += 4;
287
+ }
288
+ }
289
+ return t;
290
+ }
291
+ needsCompaction() {
292
+ if (this.iterationsSinceCompaction >= COMPACTION_INTERVAL)
293
+ return true;
294
+ const totalTokens = this.messages.reduce((sum, m) => sum + this.estimateMessageTokensCached(m), 0);
295
+ return totalTokens > this.budget.history * this.compactionThreshold;
296
+ }
297
+ compact() {
298
+ // Capture the pre-compaction state BEFORE any counters reset or rise —
299
+ // qualityBefore must reflect the context the model actually saw.
300
+ const tokensBefore = this.getEstimatedTokens();
301
+ const qualityBefore = this.getQuality();
302
+ const messagesBefore = this.messages.length;
303
+ // Reset the counter even when nothing to compact — otherwise
304
+ // needsCompaction() returns true forever after 15 iterations with few messages.
305
+ this.iterationsSinceCompaction = 0;
306
+ if (this.messages.length <= KEEP_LAST_N * 2)
307
+ return null;
308
+ this.compactionCount++;
309
+ const cutoff = this.messages.length - KEEP_LAST_N * 2;
310
+ const oldTurns = this.messages.slice(0, cutoff);
311
+ const recentTurns = this.messages.slice(cutoff);
312
+ this.facts.extract(oldTurns);
313
+ const parts = [];
314
+ parts.push(`[Compressed: ${oldTurns.length} old turns removed]`);
315
+ // Carry the user's actual instruction forward — the model must never lose
316
+ // the task it is working on just because 40 turns got compacted away. The
317
+ // per-turn task falls back to the session mission (first real instruction)
318
+ // so a pasted error/log in the current turn cannot erase the goal.
319
+ const task = (this.lastUserTask || this.sessionMission).slice(0, 400);
320
+ if (task)
321
+ parts.push(`[Task: ${task}]`);
322
+ if (this.lastUserFeedback && !task.startsWith(this.lastUserFeedback)) {
323
+ parts.push(`[User feedback: ${this.lastUserFeedback}]`);
324
+ }
325
+ // Orient the model after compaction: an active plan must not be re-created
326
+ // from scratch (see planSummaryProvider doc).
327
+ const planLine = this.planSummaryProvider?.();
328
+ if (planLine)
329
+ parts.push(`[Plan: ${planLine}]`);
330
+ const filesLine = this.facts.filesLine();
331
+ if (filesLine)
332
+ parts.push(filesLine);
333
+ const deletedLine = this.facts.deletedLine();
334
+ if (deletedLine)
335
+ parts.push(deletedLine);
336
+ const readLine = this.facts.readLine();
337
+ if (readLine)
338
+ parts.push(readLine);
339
+ const decisionsLine = this.facts.decisionsLine();
340
+ if (decisionsLine)
341
+ parts.push(decisionsLine);
342
+ const errorsLine = this.facts.errorsLine();
343
+ if (errorsLine)
344
+ parts.push(errorsLine);
345
+ const triedAndFailed = extractTriedAndFailed(oldTurns);
346
+ if (triedAndFailed.length > 0) {
347
+ const lines = triedAndFailed.map((t) => `- ${t.tool}(${t.args}): ${t.error} (failed ${t.count}x)`);
348
+ parts.push(`[Already tried & failed — do NOT repeat:]\n${lines.join("\n")}`);
349
+ }
350
+ this.compactedBlock = parts.join(" ");
351
+ const summary = {
352
+ role: "user",
353
+ content: `<system-summary>${this.compactedBlock}</system-summary>`,
354
+ };
355
+ const firstSystem = this.messages.find((m) => m.role === "system");
356
+ // Filter out stale system-summary messages from recent turns to prevent nesting
357
+ const freshRecent = recentTurns.filter((m) => {
358
+ if (m.role !== "user")
359
+ return true;
360
+ const text = getMessageText(m.content);
361
+ return !text.startsWith("<system-summary>");
362
+ });
363
+ this.messages = [...(firstSystem ? [firstSystem] : []), summary, ...freshRecent];
364
+ this.iterationsSinceCompaction = 0;
365
+ if (this.onCompact) {
366
+ this.onCompact(summary);
367
+ }
368
+ return {
369
+ removedTurns: oldTurns.length,
370
+ keptTurns: freshRecent.length,
371
+ tokensBefore,
372
+ tokensAfter: this.getEstimatedTokens(),
373
+ qualityBefore,
374
+ qualityAfter: this.getQuality(),
375
+ messagesBefore,
376
+ messagesAfter: this.messages.length,
377
+ summary: this.compactedBlock ?? "",
378
+ };
379
+ }
380
+ /**
381
+ * Snapshot of the current context state — used to log the initial context
382
+ * structure at session start and per-iteration context stats.
383
+ */
384
+ getSnapshot() {
385
+ return {
386
+ window: this.contextWindow,
387
+ budget: { ...this.budget },
388
+ tokens: this.getEstimatedTokens(),
389
+ toolTokens: this.toolTokens,
390
+ messageCount: this.messages.length,
391
+ quality: this.getQuality(),
392
+ compactionCount: this.compactionCount,
393
+ iterationsSinceCompaction: this.iterationsSinceCompaction,
394
+ };
395
+ }
396
+ /**
397
+ * Replace the system prompt in place (keeps it first) or prepend a new one.
398
+ * Used to refresh dynamic prompt blocks (e.g. the plan checklist) mid-run.
399
+ */
400
+ updateSystemPrompt(content) {
401
+ const idx = this.messages.findIndex((m) => m.role === "system");
402
+ if (idx >= 0) {
403
+ this.messages[idx] = { ...this.messages[idx], content };
404
+ }
405
+ else {
406
+ this.messages.unshift({ role: "system", content });
407
+ }
408
+ }
409
+ getActiveHistory() {
410
+ return [...this.messages];
411
+ }
412
+ /**
413
+ * Reset everything that belongs to the current conversation turn.
414
+ * Fact lists (FactExtractor) intentionally survive clear() — they are
415
+ * session-scoped compaction state, not per-turn.
416
+ */
417
+ clear() {
418
+ this.messages = [];
419
+ this.compactedBlock = null;
420
+ this.iterationsSinceCompaction = 0;
421
+ this.compactionCount = 0;
422
+ this.peakTokens = 0;
423
+ this.lastUserTask = "";
424
+ this.lastUserFeedback = "";
425
+ this.sessionMission = "";
426
+ }
427
+ getEstimatedTokens() {
428
+ return (this.messages.reduce((sum, m) => sum + this.estimateMessageTokensCached(m), 0) + this.toolTokens);
429
+ }
430
+ setToolTokens(tokens) {
431
+ this.toolTokens = tokens;
432
+ }
433
+ resize(contextWindow, contextBudget, tokenCounter) {
434
+ this.contextWindow = contextWindow;
435
+ this.budget = this.calculateBudget(contextWindow, contextBudget);
436
+ if (tokenCounter !== undefined) {
437
+ this.tokenCounter = tokenCounter ?? null;
438
+ }
439
+ }
440
+ }
@@ -0,0 +1,206 @@
1
+ import { existsSync, readdirSync, readFileSync } from "fs";
2
+ import { dirname, join, resolve } from "path";
3
+ import { detectTestResults } from "../../tools/bash";
4
+ import { processRegistry } from "../processes";
5
+ /** Directories never searched for test files. */
6
+ export const SKIP_DIRS = new Set([
7
+ "node_modules",
8
+ ".git",
9
+ ".mma",
10
+ "dist",
11
+ "build",
12
+ "coverage",
13
+ ".next",
14
+ ".nuxt",
15
+ "vendor",
16
+ ]);
17
+ const TEST_EXT_RE = /\.(test|spec)\.[jt]sx?$/i;
18
+ const PY_TEST_RE = /^test_.*\.py$|^.*_test\.py$/i;
19
+ const TEST_STEP_RE = /\b(test(ing|s)?|тест(ы|ирование|ировать)?|провер\w*\s+тест|запустить\s+тест)\b|bun test|npm test|vitest|pytest|go test|jest|mocha/i;
20
+ /**
21
+ * Resolve the project's test command from its manifest, so the final audit
22
+ * actually runs the runner the project uses instead of always assuming
23
+ * `bun test` (a Python or Go project, or a vitest/jest setup, would fail or
24
+ * silently produce no summary under bun test).
25
+ *
26
+ * Priority: package.json scripts.test → test-framework config files → Python
27
+ * markers (pyproject/pytest.ini/conftest.py) → go.mod → Cargo.toml → bun.
28
+ */
29
+ export function resolveTestCommand(dir) {
30
+ const pkgPath = join(dir, "package.json");
31
+ if (existsSync(pkgPath)) {
32
+ try {
33
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
34
+ const script = pkg?.scripts?.test;
35
+ if (typeof script === "string" && script.trim())
36
+ return script.trim();
37
+ }
38
+ catch {
39
+ /* invalid package.json — fall through */
40
+ }
41
+ }
42
+ for (const f of [
43
+ "vitest.config.ts",
44
+ "vitest.config.js",
45
+ "vitest.config.mjs",
46
+ "jest.config.js",
47
+ "jest.config.ts",
48
+ "jest.config.mjs",
49
+ "jest.config.cjs",
50
+ "bunfig.toml",
51
+ ]) {
52
+ if (existsSync(join(dir, f))) {
53
+ if (f.startsWith("vitest"))
54
+ return "bunx vitest run";
55
+ if (f.startsWith("jest"))
56
+ return "npx --no-install jest";
57
+ if (f === "bunfig.toml")
58
+ return "bun test";
59
+ }
60
+ }
61
+ if (existsSync(join(dir, "pyproject.toml")) ||
62
+ existsSync(join(dir, "pytest.ini")) ||
63
+ existsSync(join(dir, "conftest.py"))) {
64
+ return "python -m pytest -q";
65
+ }
66
+ if (existsSync(join(dir, "go.mod")))
67
+ return "go test ./...";
68
+ if (existsSync(join(dir, "Cargo.toml")))
69
+ return "cargo test";
70
+ return "bun test";
71
+ }
72
+ /** Recursive, depth-limited walk that stops early on the first test file. */
73
+ export function findTestFile(dir, depth = 0) {
74
+ if (depth > 5)
75
+ return null;
76
+ let entries;
77
+ try {
78
+ entries = readdirSync(dir, { withFileTypes: true });
79
+ }
80
+ catch {
81
+ return null;
82
+ }
83
+ for (const e of entries) {
84
+ const full = join(dir, e.name);
85
+ if (e.isDirectory()) {
86
+ if (SKIP_DIRS.has(e.name))
87
+ continue;
88
+ const found = findTestFile(full, depth + 1);
89
+ if (found)
90
+ return found;
91
+ }
92
+ else if (TEST_EXT_RE.test(e.name) || PY_TEST_RE.test(e.name)) {
93
+ return full;
94
+ }
95
+ }
96
+ return null;
97
+ }
98
+ /** The plan's step descriptions mention running/checking tests. */
99
+ export function hasTestStep(plan) {
100
+ return plan.steps.some((s) => TEST_STEP_RE.test(s.description));
101
+ }
102
+ /** Extract failing test names from a runner's output (bun/vitest style). */
103
+ function extractFailingNames(output, limit = 5) {
104
+ const names = [];
105
+ for (const m of output.matchAll(/\(fail\)\s*([^\n]+)/g)) {
106
+ const name = m[1].trim();
107
+ if (name && !names.includes(name))
108
+ names.push(name);
109
+ if (names.length >= limit)
110
+ break;
111
+ }
112
+ return names;
113
+ }
114
+ export async function runTests(baseDir) {
115
+ const command = resolveTestCommand(baseDir);
116
+ const entry = processRegistry.start(command, baseDir);
117
+ const exited = await processRegistry.waitForExit(entry.id, 90_000);
118
+ const output = entry.log.join("\n");
119
+ processRegistry.remove(entry.id);
120
+ if (!exited) {
121
+ // Timed out — we cannot claim success or failure from an unfinished run.
122
+ return {
123
+ checked: true,
124
+ passed: true, // don't block completion on an inconclusive run
125
+ failed: 0,
126
+ passedCount: 0,
127
+ detail: `test run timed out after 90s — result unknown`,
128
+ command,
129
+ };
130
+ }
131
+ const run = detectTestResults(output);
132
+ if (!run) {
133
+ // No recognizable runner summary. A non-zero exit means the test run
134
+ // (or its runner) failed — the audit must not pass on it; report a
135
+ // single "at least one failed" since we cannot count them. Exit 0
136
+ // with unrecognized output stays checked-but-inconclusive.
137
+ return {
138
+ checked: true,
139
+ passed: entry.exitCode === 0,
140
+ failed: entry.exitCode === 0 ? 0 : 1,
141
+ passedCount: 0,
142
+ detail: output.slice(0, 200).trim(),
143
+ command,
144
+ };
145
+ }
146
+ const names = extractFailingNames(output);
147
+ return {
148
+ checked: true,
149
+ passed: run.failed === 0,
150
+ failed: run.failed,
151
+ passedCount: run.passed,
152
+ detail: names.length
153
+ ? names.join("; ")
154
+ : run.summary || `${run.failed} failed / ${run.passed} passed`,
155
+ command,
156
+ };
157
+ }
158
+ /** First `error TS…` line in a tsc output, or null when none/inconclusive. */
159
+ export function parseTypecheckErrors(output) {
160
+ const line = output.split("\n").find((l) => /error TS\d+/.test(l));
161
+ return line ? line.trim().slice(0, 300) : null;
162
+ }
163
+ /**
164
+ * Locate the nearest project root that owns a tsconfig.json.
165
+ *
166
+ * The agent frequently creates the project in a nested subfolder of its
167
+ * baseDir (e.g. `bicycle-shop/`), so the tsconfig lives at
168
+ * `baseDir/bicycle-shop/` and the old `existsSync(baseDir/tsconfig.json)`
169
+ * gate silently skipped the final typecheck — the audit passed even with
170
+ * dozens of tsc errors (observed in both analyzed sessions). This walks each
171
+ * candidate (baseDir plus the resolved step files) upward to the nearest
172
+ * tsconfig.json, preferring the shallowest hit so the baseDir project wins
173
+ * over a stray tsconfig further up. Returns null when no tsconfig exists in
174
+ * any reachable subtree. `existingFiles` must contain resolved absolute paths
175
+ * (relative tokens would resolve against the process cwd, not baseDir).
176
+ */
177
+ export function findTypecheckRoot(baseDir, existingFiles = []) {
178
+ const candidates = [baseDir, ...existingFiles];
179
+ let best = null;
180
+ for (const start of candidates) {
181
+ let dir = resolve(start);
182
+ for (let depth = 0; depth <= 10; depth++) {
183
+ if (existsSync(join(dir, "tsconfig.json"))) {
184
+ if (!best || depth < best.depth)
185
+ best = { depth, root: dir };
186
+ break;
187
+ }
188
+ const parent = dirname(dir);
189
+ if (parent === dir)
190
+ break;
191
+ dir = parent;
192
+ }
193
+ }
194
+ return best?.root ?? null;
195
+ }
196
+ export async function runTypecheck(baseDir) {
197
+ const entry = processRegistry.start("npx --no-install tsc --noEmit --skipLibCheck", baseDir);
198
+ const exited = await processRegistry.waitForExit(entry.id, 90_000);
199
+ const output = entry.log.join("\n");
200
+ processRegistry.remove(entry.id);
201
+ // A timeout, or output with no `error TS` lines (missing typescript / wrong
202
+ // working dir) is inconclusive — never block completion on those.
203
+ if (!exited)
204
+ return null;
205
+ return parseTypecheckErrors(output);
206
+ }