micro-models-agent 0.40.1 → 0.41.2
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/bin/mma.mjs +41 -41
- package/dist/cli/commands.js +9 -19
- package/dist/cli/completer.js +36 -37
- package/dist/cli/index.js +2 -2
- package/dist/cli/main.js +48 -23
- package/dist/cli/plugin-commands.js +36 -0
- package/dist/cli/repl-commands.js +40 -12
- package/dist/cli/repl.js +217 -87
- package/dist/cli/run-result.js +22 -0
- package/dist/cli/security-commands.js +5 -7
- package/dist/cli/setup.js +8 -26
- package/dist/config/config.js +52 -5
- package/dist/config/defaults.js +29 -5
- package/dist/config/experts.js +1 -1
- package/dist/config/index.js +3 -3
- package/dist/config/security.js +3 -10
- package/dist/core/agent-moe.js +2 -10
- package/dist/core/agent.js +273 -82
- package/dist/core/bootstrap.js +80 -13
- package/dist/core/index.js +2 -2
- package/dist/core/prompt-builder.js +23 -2
- package/dist/core/session-logger.js +46 -4
- package/dist/core/version.js +24 -0
- package/dist/i18n/en.json +75 -2
- package/dist/i18n/ru.json +74 -1
- package/dist/index.js +1 -1
- package/dist/llm/image-utils.js +4 -5
- package/dist/llm/index.js +4 -4
- package/dist/llm/model-loader.js +6 -6
- package/dist/llm/openai-compat.js +40 -34
- package/dist/llm/orchestrator.js +33 -29
- package/dist/llm/response.js +9 -9
- package/dist/logger/app-logger.js +1 -1
- package/dist/logger/index.js +1 -1
- package/dist/main.js +2489 -2186
- package/dist/migration/backup.js +13 -13
- package/dist/migration/detect.js +11 -11
- package/dist/migration/index.js +2 -2
- package/dist/modules/artifacts/store.js +61 -0
- package/dist/modules/browser/actions.js +34 -4
- package/dist/modules/browser/bridge-client.js +199 -0
- package/dist/modules/browser/bridge-path.js +10 -0
- package/dist/modules/browser/bridge-server.mjs +202 -202
- package/dist/modules/browser/cookie-store.js +6 -6
- package/dist/modules/browser/driver.js +136 -0
- package/dist/modules/browser/index.js +7 -5
- package/dist/modules/browser/module.js +8 -7
- package/dist/modules/browser/session.js +87 -84
- package/dist/modules/browser/snapshot.js +92 -58
- package/dist/modules/browser/types.js +4 -1
- package/dist/modules/certification/cli.js +2 -4
- package/dist/modules/certification/fact-checker.js +1 -3
- package/dist/modules/certification/loader.js +3 -9
- package/dist/modules/certification/runner.js +1 -4
- package/dist/modules/context/chunk-query.js +100 -0
- package/dist/modules/context/fact-extractor.js +162 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -1
- package/dist/modules/context/manager.js +160 -86
- package/dist/modules/execution/audit-runners.js +152 -0
- package/dist/modules/execution/auditor.js +177 -25
- package/dist/modules/execution/execution-plugin.js +272 -0
- package/dist/modules/execution/module.js +201 -544
- package/dist/modules/execution/moe-executor.js +25 -0
- package/dist/modules/execution/plan-store.js +1 -3
- package/dist/modules/execution/plan-tool.js +508 -0
- package/dist/modules/execution/plan-validator.js +10 -10
- package/dist/modules/execution/planner.js +6 -1
- package/dist/modules/execution/stuck-detector.js +173 -10
- package/dist/modules/execution/verifier.js +86 -42
- package/dist/modules/execution/windows-commands.js +41 -0
- package/dist/modules/hallucination/confidence.js +8 -1
- package/dist/modules/hallucination/detector.js +2 -5
- package/dist/modules/hallucination/factual.js +3 -64
- package/dist/modules/hallucination/index.js +1 -1
- package/dist/modules/hallucination/js-identifiers.js +190 -0
- package/dist/modules/hallucination/llm-judge.js +1 -3
- package/dist/modules/indexer/cache.js +9 -7
- package/dist/modules/indexer/index.js +3 -3
- package/dist/modules/indexer/module.js +95 -42
- package/dist/modules/indexer/project-profile.js +183 -0
- package/dist/modules/indexer/walker.js +17 -17
- package/dist/modules/lsp/check-tool.js +58 -0
- package/dist/modules/lsp/client.js +74 -31
- package/dist/modules/lsp/command.js +60 -0
- package/dist/modules/lsp/config.js +87 -33
- package/dist/modules/lsp/index.js +3 -3
- package/dist/modules/lsp/module.js +185 -21
- package/dist/modules/lsp/probe.js +76 -0
- package/dist/modules/lsp/project-root.js +32 -0
- package/dist/modules/lsp/startup-check.js +141 -0
- package/dist/modules/mcp/module.js +2 -6
- package/dist/modules/memory/index.js +1 -1
- package/dist/modules/memory/module.js +71 -23
- package/dist/modules/memory/search.js +11 -9
- package/dist/modules/memory/store.js +13 -13
- package/dist/modules/pipelines/engine.js +10 -10
- package/dist/modules/pipelines/index.js +3 -3
- package/dist/modules/pipelines/parser.js +17 -14
- package/dist/modules/pipelines/template.js +1 -1
- package/dist/modules/plugins/builtin/lint-on-write.js +21 -16
- package/dist/modules/plugins/builtin/notify.js +3 -2
- package/dist/modules/plugins/index.js +1 -1
- package/dist/modules/plugins/loader.js +59 -17
- package/dist/modules/plugins/manager.js +73 -17
- package/dist/modules/processes/detect.js +34 -0
- package/dist/modules/processes/index.js +1 -1
- package/dist/modules/processes/registry.js +135 -46
- package/dist/modules/registry.js +4 -2
- package/dist/modules/security/audit-notifier.js +39 -39
- package/dist/modules/security/command-validator.js +2 -8
- package/dist/modules/security/data-sanitizer.js +1 -9
- package/dist/modules/security/encryption.js +58 -56
- package/dist/modules/security/network-validator.js +1 -9
- package/dist/modules/security/path-validator.js +1 -3
- package/dist/modules/security/security-policies.js +3 -19
- package/dist/modules/security/session-encryption.js +1 -1
- package/dist/modules/security/session-isolation.js +8 -8
- package/dist/modules/session/index.js +3 -3
- package/dist/modules/session/module.js +5 -5
- package/dist/modules/session/store.js +3 -9
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +1 -2
- package/dist/modules/updater/checker.js +70 -6
- package/dist/modules/updater/index.js +2 -1
- package/dist/modules/updater/module.js +116 -0
- package/dist/modules/user-profile/compressor.js +2 -2
- package/dist/modules/user-profile/index.js +1 -1
- package/dist/modules/user-profile/profile.js +9 -9
- package/dist/tools/attach-image.js +1 -1
- package/dist/tools/bash.js +178 -19
- package/dist/tools/browser.js +46 -29
- package/dist/tools/chunk-query.js +99 -0
- package/dist/tools/download-file.js +116 -0
- package/dist/tools/enable-tools.js +58 -0
- package/dist/tools/executor.js +4 -5
- package/dist/tools/file-info.js +13 -12
- package/dist/tools/filter-tools.js +9 -2
- package/dist/tools/glob-tool.js +11 -11
- package/dist/tools/grep-tool.js +1 -3
- package/dist/tools/hidden-tools-block.js +37 -0
- package/dist/tools/index.js +13 -2
- package/dist/tools/list-dir.js +18 -17
- package/dist/tools/load-skill.js +1 -3
- package/dist/tools/path-utils.js +4 -4
- package/dist/tools/pipeline-run.js +25 -25
- package/dist/tools/process-kill.js +11 -11
- package/dist/tools/process-list.js +20 -22
- package/dist/tools/process-log.js +22 -18
- package/dist/tools/question.js +1 -3
- package/dist/tools/read-file.js +10 -2
- package/dist/tools/recall.js +44 -37
- package/dist/tools/registry.js +15 -4
- package/dist/tools/remember.js +29 -29
- package/dist/tools/scope-check.js +9 -9
- package/dist/tools/subagent.js +54 -9
- package/dist/tools/user-input.js +1 -1
- package/dist/tools/web-browse.js +3 -3
- package/dist/tools/web-fetch.js +3 -3
- package/dist/tools/web-search.js +3 -3
- package/dist/tools/write-file.js +1 -3
- package/dist/ui/box.js +1 -5
- package/dist/ui/index.js +6 -6
- package/dist/ui/line-editor.js +703 -0
- package/dist/ui/line-math.js +69 -0
- package/dist/ui/md-formatter.js +33 -33
- package/dist/ui/output.js +5 -5
- package/dist/ui/plan-view.js +103 -0
- package/dist/ui/renderer.js +15 -10
- package/dist/ui/table.js +1 -1
- package/package.json +48 -48
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export const DEFAULT_CHUNK_SYSTEM_PROMPT = 'Answer the query using ONLY the provided text. Be concise. If the text does not contain the answer, say "NO_EVIDENCE".';
|
|
2
|
+
export const DEFAULT_SYNTHESIS_SYSTEM_PROMPT = "You are given a query and per-chunk answers over a large text. Produce the final answer to the query, combining evidence from the chunks. If no chunk had evidence, say so.";
|
|
3
|
+
/** Split text into newline-aware chunks, each <= chunkChars chars. */
|
|
4
|
+
export function splitTextIntoChunks(text, chunkChars) {
|
|
5
|
+
if (chunkChars <= 0)
|
|
6
|
+
throw new Error("chunkChars must be > 0");
|
|
7
|
+
if (text.length <= chunkChars)
|
|
8
|
+
return [text];
|
|
9
|
+
const lines = text.split("\n");
|
|
10
|
+
const chunks = [];
|
|
11
|
+
let current = [];
|
|
12
|
+
let size = 0;
|
|
13
|
+
for (const line of lines) {
|
|
14
|
+
if (current.length > 0 && size + line.length + 1 > chunkChars) {
|
|
15
|
+
chunks.push(current.join("\n"));
|
|
16
|
+
current = [];
|
|
17
|
+
size = 0;
|
|
18
|
+
}
|
|
19
|
+
current.push(line);
|
|
20
|
+
size += line.length + 1;
|
|
21
|
+
}
|
|
22
|
+
if (current.length > 0)
|
|
23
|
+
chunks.push(current.join("\n"));
|
|
24
|
+
return chunks;
|
|
25
|
+
}
|
|
26
|
+
async function chatText(provider, messages, maxTokens, reasoningEffort) {
|
|
27
|
+
let out = "";
|
|
28
|
+
for await (const chunk of provider.chat(messages, undefined, undefined, {
|
|
29
|
+
maxTokens,
|
|
30
|
+
reasoningEffort,
|
|
31
|
+
})) {
|
|
32
|
+
if (chunk.type === "text" && chunk.content)
|
|
33
|
+
out += chunk.content;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* RLM-style llm_query: answer `query` over `text` by chunking and querying the
|
|
39
|
+
* model per chunk with a fixed system prompt (KV-cache friendly), bounded
|
|
40
|
+
* concurrency, and an optional synthesis pass.
|
|
41
|
+
*/
|
|
42
|
+
export async function runChunkQuery(provider, text, query, opts = {}) {
|
|
43
|
+
const chunkChars = opts.chunkChars ?? 12000;
|
|
44
|
+
const maxParallel = Math.max(1, opts.maxParallel ?? 4);
|
|
45
|
+
const maxChunkTokens = opts.maxChunkTokens ?? 1024;
|
|
46
|
+
const maxSynthesisTokens = opts.maxSynthesisTokens ?? 2048;
|
|
47
|
+
const reasoningEffort = opts.reasoningEffort ?? "none";
|
|
48
|
+
const chunks = splitTextIntoChunks(text, chunkChars);
|
|
49
|
+
const answers = new Array(chunks.length);
|
|
50
|
+
const chunkSystem = opts.querySystemPrompt ?? DEFAULT_CHUNK_SYSTEM_PROMPT;
|
|
51
|
+
const logger = opts.logger;
|
|
52
|
+
if (chunks.length === 1) {
|
|
53
|
+
logger?.debug(`runChunkQuery: single chunk (${text.length} chars), no chunking needed`);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
logger?.debug(`runChunkQuery: ${chunks.length} chunks over ${text.length} chars (chunkChars=${chunkChars}, maxParallel=${maxParallel})`);
|
|
57
|
+
}
|
|
58
|
+
let cursor = 0;
|
|
59
|
+
let inFlight = 0;
|
|
60
|
+
let maxParallelObserved = 0;
|
|
61
|
+
const workers = Array.from({ length: Math.min(maxParallel, chunks.length) }, async () => {
|
|
62
|
+
while (cursor < chunks.length) {
|
|
63
|
+
const idx = cursor++;
|
|
64
|
+
inFlight++;
|
|
65
|
+
maxParallelObserved = Math.max(maxParallelObserved, inFlight);
|
|
66
|
+
try {
|
|
67
|
+
answers[idx] = await chatText(provider, [
|
|
68
|
+
{ role: "system", content: chunkSystem },
|
|
69
|
+
{ role: "user", content: `Query: ${query}\n\nText:\n${chunks[idx]}` },
|
|
70
|
+
], maxChunkTokens, reasoningEffort);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
inFlight--;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
await Promise.all(workers);
|
|
78
|
+
let synthesis = "";
|
|
79
|
+
if (opts.synthesize !== false && chunks.length > 0) {
|
|
80
|
+
const joined = answers
|
|
81
|
+
.map((a, i) => `--- Chunk ${i + 1}/${chunks.length} ---\n${a}`)
|
|
82
|
+
.join("\n\n");
|
|
83
|
+
logger?.debug(`runChunkQuery: synthesis pass over ${chunks.length} answers`);
|
|
84
|
+
synthesis = await chatText(provider, [
|
|
85
|
+
{
|
|
86
|
+
role: "system",
|
|
87
|
+
content: opts.synthesisSystemPrompt ?? DEFAULT_SYNTHESIS_SYSTEM_PROMPT,
|
|
88
|
+
},
|
|
89
|
+
{ role: "user", content: `Query: ${query}\n\nChunk answers:\n${joined}` },
|
|
90
|
+
], maxSynthesisTokens, reasoningEffort);
|
|
91
|
+
}
|
|
92
|
+
logger?.debug(`runChunkQuery: done, ${chunks.length} chunks, max parallel observed ${maxParallelObserved}, synthesis ${synthesis.length} chars`);
|
|
93
|
+
return {
|
|
94
|
+
chunks: chunks.length,
|
|
95
|
+
answers,
|
|
96
|
+
synthesis,
|
|
97
|
+
totalChars: text.length,
|
|
98
|
+
maxParallelObserved,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { getMessageText } from "../../llm/provider";
|
|
2
|
+
/**
|
|
3
|
+
* Regex-based fact extraction from tool/assistant messages. Owns the file /
|
|
4
|
+
* deleted / decision / error fact lists that survive compaction. Extracted
|
|
5
|
+
* from ContextManager so the ~115-line regex logic has one home (rule #2).
|
|
6
|
+
*/
|
|
7
|
+
export class FactExtractor {
|
|
8
|
+
fileFacts = [];
|
|
9
|
+
deletedFacts = [];
|
|
10
|
+
decisionFacts = [];
|
|
11
|
+
errorFacts = [];
|
|
12
|
+
readFacts = [];
|
|
13
|
+
static MAX_FACTS = 20;
|
|
14
|
+
get errorCount() {
|
|
15
|
+
return this.errorFacts.length;
|
|
16
|
+
}
|
|
17
|
+
/** Scan a batch of compacted turns and fold their facts into the lists. */
|
|
18
|
+
extract(turns) {
|
|
19
|
+
// Absolute Windows paths ("E:\proj\src\a.ts") need an optional drive prefix;
|
|
20
|
+
// without it the colon breaks the match and the fact is silently dropped.
|
|
21
|
+
const DRIVE = "(?:[a-zA-Z]:[\\\\/])?";
|
|
22
|
+
const createdPatterns = [
|
|
23
|
+
new RegExp(`(?:Created|Updated|Written|Moved|Replaced in) ${DRIVE}([\\w./\\\\-]+\\.[a-z]+)`, "gi"),
|
|
24
|
+
new RegExp(`Файл (?:создан|обновлён|записан|перемещён):? ${DRIVE}([\\w./\\\\-]+\\.[a-z]+)`, "gi"),
|
|
25
|
+
new RegExp(`file (?:created|updated|written|moved):? ${DRIVE}([\\w./\\\\-]+\\.[a-z]+)`, "gi"),
|
|
26
|
+
];
|
|
27
|
+
const deletedPatterns = [
|
|
28
|
+
new RegExp(`Deleted ${DRIVE}([\\w./\\\\-]+\\.[a-z]+)`, "gi"),
|
|
29
|
+
new RegExp(`(?:Удалён|Файл удалён):? ${DRIVE}([\\w./\\\\-]+\\.[a-z]+)`, "gi"),
|
|
30
|
+
];
|
|
31
|
+
// read_file output header — locale-independent prefix `── path (ext, …`
|
|
32
|
+
// (en: "lines", ru: "строк"). The path is already prefixed with the same
|
|
33
|
+
// box-drawing marker in both locales, so the pattern needs no localization.
|
|
34
|
+
const readPatterns = [new RegExp(`── (${DRIVE}[\\w./\\\\-]+\\.[a-z]+) \\(`, "gi")];
|
|
35
|
+
const newFiles = [];
|
|
36
|
+
const newDeleted = [];
|
|
37
|
+
const newDecisions = [];
|
|
38
|
+
const newErrors = [];
|
|
39
|
+
const newReads = [];
|
|
40
|
+
// Track the LAST tool event per path so a delete followed by a re-create
|
|
41
|
+
// (or vice versa) in the same window resolves to its final state instead of
|
|
42
|
+
// being both filtered out of [Files:] and reported as [Deleted:].
|
|
43
|
+
const lastEvent = new Map();
|
|
44
|
+
for (const msg of turns) {
|
|
45
|
+
const content = getMessageText(msg.content);
|
|
46
|
+
// The system prompt is never a fact source — the memory module embeds
|
|
47
|
+
// errors.md (with historical "Tool X failed Nx" entries from PAST days)
|
|
48
|
+
// there, and extracting that text as current [Errors:] misleads the model
|
|
49
|
+
// into thinking IT is failing (observed: ses_msvuao0h — a stale
|
|
50
|
+
// "edit_file failed 3x" from 2026-08-15 landed in every compaction
|
|
51
|
+
// summary as if it were the live session's failure).
|
|
52
|
+
if (msg.role === "system")
|
|
53
|
+
continue;
|
|
54
|
+
// Never feed a previous <system-summary> block into new facts — that is
|
|
55
|
+
// the source of recursive summary nesting (summary inside summary).
|
|
56
|
+
if (msg.role === "user" &&
|
|
57
|
+
(content.startsWith("<system-summary>") || content.includes("[Compressed:"))) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (msg.role === "tool") {
|
|
61
|
+
for (const pattern of createdPatterns) {
|
|
62
|
+
for (const match of content.matchAll(pattern)) {
|
|
63
|
+
newFiles.push(match[1]);
|
|
64
|
+
lastEvent.set(match[1], "file");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const pattern of deletedPatterns) {
|
|
68
|
+
for (const match of content.matchAll(pattern)) {
|
|
69
|
+
newDeleted.push(match[1]);
|
|
70
|
+
lastEvent.set(match[1], "deleted");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const pattern of readPatterns) {
|
|
74
|
+
for (const match of content.matchAll(pattern)) {
|
|
75
|
+
newReads.push(match[1]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (content.includes("Plan:") && content.includes("[")) {
|
|
79
|
+
const planLine = content.split("\n").find((line) => line.includes("Plan:"));
|
|
80
|
+
if (planLine)
|
|
81
|
+
newDecisions.push(planLine.trim());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (msg.role === "assistant") {
|
|
85
|
+
if (content.includes("decided:") || content.includes("decision:")) {
|
|
86
|
+
const line = content
|
|
87
|
+
.split("\n")
|
|
88
|
+
.find((l) => l.includes("decided:") || l.includes("decision:"));
|
|
89
|
+
if (line)
|
|
90
|
+
newDecisions.push(line.trim().slice(0, 200));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (msg.role === "tool" || msg.role === "assistant") {
|
|
94
|
+
if (content.includes("Error:") ||
|
|
95
|
+
content.includes("failed") ||
|
|
96
|
+
content.includes("Ошибка:") ||
|
|
97
|
+
content.includes("не удалось")) {
|
|
98
|
+
const line = content
|
|
99
|
+
.split("\n")
|
|
100
|
+
.find((l) => l.includes("Error:") ||
|
|
101
|
+
l.includes("failed") ||
|
|
102
|
+
l.includes("Ошибка:") ||
|
|
103
|
+
l.includes("не удалось"));
|
|
104
|
+
if (line)
|
|
105
|
+
newErrors.push(line.trim().slice(0, 250));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Deduplicate and cap facts to prevent unbounded growth. Files that were
|
|
110
|
+
// DELETED leave the "known files" list — a deleted file must not keep
|
|
111
|
+
// being reported as existing (observed: deleted .js files listed in
|
|
112
|
+
// every [Files: ...] block for the whole session). A path that is deleted
|
|
113
|
+
// then RE-CREATED (delete_file A → write_file A) is un-deleted: the last
|
|
114
|
+
// event in the window wins, so it returns to [Files:] and leaves [Deleted:].
|
|
115
|
+
const dedup = (arr) => [...new Set(arr)];
|
|
116
|
+
const finalFiles = dedup(newFiles.filter((p) => lastEvent.get(p) === "file"));
|
|
117
|
+
const finalDeleted = dedup(newDeleted.filter((p) => lastEvent.get(p) === "deleted"));
|
|
118
|
+
this.fileFacts = dedup([...this.fileFacts, ...finalFiles].filter((p) => !finalDeleted.includes(p))).slice(-FactExtractor.MAX_FACTS);
|
|
119
|
+
this.deletedFacts = dedup([...this.deletedFacts, ...finalDeleted].filter((p) => !finalFiles.includes(p))).slice(-FactExtractor.MAX_FACTS);
|
|
120
|
+
this.decisionFacts = dedup([...this.decisionFacts, ...newDecisions]).slice(-FactExtractor.MAX_FACTS);
|
|
121
|
+
this.errorFacts = dedup([...this.errorFacts, ...newErrors]).slice(-FactExtractor.MAX_FACTS);
|
|
122
|
+
this.readFacts = dedup([...this.readFacts, ...newReads]).slice(-FactExtractor.MAX_FACTS);
|
|
123
|
+
}
|
|
124
|
+
/** `[Files: a; b]` line for a compaction summary, or null. */
|
|
125
|
+
filesLine() {
|
|
126
|
+
const deletedSet = new Set(this.deletedFacts);
|
|
127
|
+
const liveFiles = this.fileFacts.filter((p) => !deletedSet.has(p));
|
|
128
|
+
if (liveFiles.length === 0)
|
|
129
|
+
return null;
|
|
130
|
+
return `[Files: ${liveFiles.slice(-15).join("; ")}]`;
|
|
131
|
+
}
|
|
132
|
+
/** `[Deleted: c]` line, or null. */
|
|
133
|
+
deletedLine() {
|
|
134
|
+
if (this.deletedFacts.length === 0)
|
|
135
|
+
return null;
|
|
136
|
+
return `[Deleted: ${this.deletedFacts.slice(-10).join("; ")}]`;
|
|
137
|
+
}
|
|
138
|
+
/** `[Decisions: d]` line, or null. */
|
|
139
|
+
decisionsLine() {
|
|
140
|
+
if (this.decisionFacts.length === 0)
|
|
141
|
+
return null;
|
|
142
|
+
return `[Decisions: ${this.decisionFacts.slice(-5).join("; ")}]`;
|
|
143
|
+
}
|
|
144
|
+
/** `[Errors: e]` line, or null. */
|
|
145
|
+
errorsLine() {
|
|
146
|
+
if (this.errorFacts.length === 0)
|
|
147
|
+
return null;
|
|
148
|
+
return `[Errors: ${this.errorFacts.slice(-3).join("; ")}]`;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* `[Read: p1; p2]` line — files already inspected via read_file. Lets the
|
|
152
|
+
* model re-orient after compaction without rescanning the whole project: it
|
|
153
|
+
* knows which files were already read and only re-reads what changed
|
|
154
|
+
* (observed: post-compaction the model re-read the same files it had already
|
|
155
|
+
* inspected, because "already read" never survived compaction).
|
|
156
|
+
*/
|
|
157
|
+
readLine() {
|
|
158
|
+
if (this.readFacts.length === 0)
|
|
159
|
+
return null;
|
|
160
|
+
return `[Read: ${this.readFacts.slice(-12).join("; ")}]`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, existsSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
export class SessionHistoryWriter {
|
|
4
|
+
historyPath;
|
|
5
|
+
constructor(sessionsDir, sessionId) {
|
|
6
|
+
const sessionDir = join(sessionsDir, sessionId);
|
|
7
|
+
if (!existsSync(sessionDir)) {
|
|
8
|
+
mkdirSync(sessionDir, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
this.historyPath = join(sessionDir, 'history.jsonl');
|
|
11
|
+
}
|
|
12
|
+
append(msg) {
|
|
13
|
+
appendFileSync(this.historyPath, JSON.stringify(msg) + '\n', 'utf-8');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ContextManager } from
|
|
1
|
+
export { ContextManager } from "./manager";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getMessageText } from "../../llm/provider";
|
|
2
|
+
import { FactExtractor } from "./fact-extractor";
|
|
2
3
|
const COMPACTION_INTERVAL = 15;
|
|
3
4
|
const KEEP_LAST_N = 6;
|
|
4
5
|
function summarizeArgs(args) {
|
|
@@ -19,15 +20,28 @@ function truncate(s, max) {
|
|
|
19
20
|
return s;
|
|
20
21
|
return s.slice(0, max - 3) + "...";
|
|
21
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
|
+
}
|
|
22
38
|
export function extractTriedAndFailed(messages) {
|
|
23
39
|
const failures = new Map();
|
|
24
40
|
for (const msg of messages) {
|
|
25
41
|
if (msg.role === "tool" && msg.name && msg.success === false) {
|
|
26
42
|
const key = `${msg.name}:${summarizeArgs(msg.arguments)}`;
|
|
27
43
|
const existing = failures.get(key);
|
|
28
|
-
const errorText = truncate(typeof msg.content === "string"
|
|
29
|
-
? msg.content
|
|
30
|
-
: getMessageText(msg.content), 100);
|
|
44
|
+
const errorText = truncate(typeof msg.content === "string" ? msg.content : getMessageText(msg.content), 100);
|
|
31
45
|
if (existing) {
|
|
32
46
|
existing.count++;
|
|
33
47
|
}
|
|
@@ -52,20 +66,38 @@ export class ContextManager {
|
|
|
52
66
|
peakTokens = 0;
|
|
53
67
|
budget;
|
|
54
68
|
compactionThreshold;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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 = "";
|
|
59
76
|
tokenCounter;
|
|
60
77
|
pendingImageParts = [];
|
|
61
78
|
toolTokens = 0;
|
|
62
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;
|
|
63
88
|
constructor(contextWindow, contextBudget, tokenCounter) {
|
|
64
89
|
this.contextWindow = contextWindow;
|
|
65
90
|
this.compactionThreshold = contextBudget?.compactionThreshold ?? 0.75;
|
|
66
91
|
this.budget = this.calculateBudget(contextWindow, contextBudget);
|
|
67
92
|
this.tokenCounter = tokenCounter ?? null;
|
|
68
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
|
+
}
|
|
69
101
|
calculateBudget(window, contextBudget) {
|
|
70
102
|
if (contextBudget) {
|
|
71
103
|
const systemPrompt = Math.floor(window * contextBudget.systemPrompt);
|
|
@@ -92,27 +124,67 @@ export class ContextManager {
|
|
|
92
124
|
noteIteration() {
|
|
93
125
|
this.iterationsSinceCompaction++;
|
|
94
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Start a fresh user turn. The 15-iteration compaction interval must not
|
|
129
|
+
* leak across user messages — otherwise a turn that ends at iteration 14
|
|
130
|
+
* forces a compaction on the very first iteration of the next user message
|
|
131
|
+
* (observed: compaction fired on iteration 8 of a new turn, deleting the
|
|
132
|
+
* user's freshly-sent task alongside 40+ old turns). Facts are kept.
|
|
133
|
+
*/
|
|
134
|
+
resetUserTurn() {
|
|
135
|
+
this.iterationsSinceCompaction = 0;
|
|
136
|
+
// A new user message begins a new turn — drop the previous turn's task
|
|
137
|
+
// so the fresh instruction becomes the one preserved at compaction. The
|
|
138
|
+
// session mission survives: an error/log paste in the new turn must not
|
|
139
|
+
// erase the original goal.
|
|
140
|
+
this.lastUserTask = "";
|
|
141
|
+
this.lastUserFeedback = "";
|
|
142
|
+
}
|
|
95
143
|
getQuality() {
|
|
96
144
|
const usedTokens = this.getEstimatedTokens();
|
|
97
145
|
const tokenLoad = Math.max(0, 1 - usedTokens / this.budget.history);
|
|
98
|
-
|
|
146
|
+
// Recoverable penalty: each compaction loses some information, but the
|
|
147
|
+
// penalty is CAPPED so cumulative compactions can never permanently pin
|
|
148
|
+
// quality below the forced-compaction trigger (40%). Before this cap, a
|
|
149
|
+
// session with ~7+ compactions clamped the term to 0 forever; combined
|
|
150
|
+
// with heavy token load + error density, quality stuck below 40 and a
|
|
151
|
+
// compaction fired on EVERY iteration (observed: 56 compactions in 28 min).
|
|
152
|
+
const COMPACTION_PENALTY_FLOOR = 0.5;
|
|
153
|
+
const compactionLoss = Math.max(COMPACTION_PENALTY_FLOOR, 1 - this.compactionCount * 0.15);
|
|
99
154
|
const msgCount = this.messages.length || 1;
|
|
100
|
-
const errorDensity = Math.max(0, 1 - Math.min(1, this.
|
|
155
|
+
const errorDensity = Math.max(0, 1 - Math.min(1, this.facts.errorCount / msgCount));
|
|
101
156
|
const freshness = Math.max(0, 1 - this.iterationsSinceCompaction / COMPACTION_INTERVAL);
|
|
102
|
-
const score = tokenLoad * 0.4 +
|
|
103
|
-
compactionLoss * 0.25 +
|
|
104
|
-
errorDensity * 0.2 +
|
|
105
|
-
freshness * 0.15;
|
|
157
|
+
const score = tokenLoad * 0.4 + compactionLoss * 0.25 + errorDensity * 0.2 + freshness * 0.15;
|
|
106
158
|
return Math.round(Math.min(100, Math.max(0, score * 100)));
|
|
107
159
|
}
|
|
108
160
|
addMessage(msg) {
|
|
161
|
+
// Track the task the agent is working on so it survives compaction. The
|
|
162
|
+
// user's real instruction is the FIRST non-system-summary user message of
|
|
163
|
+
// the turn; everything after it (tool results, retry nudges) are injected
|
|
164
|
+
// `user` envelopes that must not overwrite it (observed: after compaction
|
|
165
|
+
// the model had forgotten "удали /bicycles, почини стили" and re-read
|
|
166
|
+
// files aimlessly for 40+ iterations).
|
|
167
|
+
if (msg.role === "user") {
|
|
168
|
+
const text = typeof msg.content === "string" ? msg.content : getMessageText(msg.content);
|
|
169
|
+
if (text.trim() && !text.startsWith("<system-summary>") && !text.includes("[Compressed:")) {
|
|
170
|
+
if (looksLikeErrorPaste(text) && (this.lastUserTask || this.sessionMission)) {
|
|
171
|
+
this.lastUserFeedback = text.trim().slice(0, 400);
|
|
172
|
+
}
|
|
173
|
+
else if (!this.lastUserTask && !this.lastUserFeedback) {
|
|
174
|
+
// No new task inside a turn that already captured an error/log paste:
|
|
175
|
+
// the paste's follow-up messages are tool-result continuations, not a
|
|
176
|
+
// fresh instruction, so the session mission must win at compaction.
|
|
177
|
+
this.lastUserTask = text.trim();
|
|
178
|
+
if (!this.sessionMission)
|
|
179
|
+
this.sessionMission = text.trim();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
109
183
|
// Auto-attach pending images to the next user message
|
|
110
184
|
if (msg.role === "user" && this.pendingImageParts.length > 0) {
|
|
111
185
|
const textPart = {
|
|
112
186
|
type: "text",
|
|
113
|
-
text: typeof msg.content === "string"
|
|
114
|
-
? msg.content
|
|
115
|
-
: getMessageText(msg.content),
|
|
187
|
+
text: typeof msg.content === "string" ? msg.content : getMessageText(msg.content),
|
|
116
188
|
};
|
|
117
189
|
msg = {
|
|
118
190
|
...msg,
|
|
@@ -206,27 +278,53 @@ export class ContextManager {
|
|
|
206
278
|
return totalTokens > this.budget.history * this.compactionThreshold;
|
|
207
279
|
}
|
|
208
280
|
compact() {
|
|
281
|
+
// Capture the pre-compaction state BEFORE any counters reset or rise —
|
|
282
|
+
// qualityBefore must reflect the context the model actually saw.
|
|
283
|
+
const tokensBefore = this.getEstimatedTokens();
|
|
284
|
+
const qualityBefore = this.getQuality();
|
|
285
|
+
const messagesBefore = this.messages.length;
|
|
209
286
|
// Reset the counter even when nothing to compact — otherwise
|
|
210
287
|
// needsCompaction() returns true forever after 15 iterations with few messages.
|
|
211
288
|
this.iterationsSinceCompaction = 0;
|
|
212
289
|
if (this.messages.length <= KEEP_LAST_N * 2)
|
|
213
|
-
return;
|
|
290
|
+
return null;
|
|
214
291
|
this.compactionCount++;
|
|
215
292
|
const cutoff = this.messages.length - KEEP_LAST_N * 2;
|
|
216
293
|
const oldTurns = this.messages.slice(0, cutoff);
|
|
217
294
|
const recentTurns = this.messages.slice(cutoff);
|
|
218
|
-
this.
|
|
295
|
+
this.facts.extract(oldTurns);
|
|
219
296
|
const parts = [];
|
|
220
297
|
parts.push(`[Compressed: ${oldTurns.length} old turns removed]`);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
298
|
+
// Carry the user's actual instruction forward — the model must never lose
|
|
299
|
+
// the task it is working on just because 40 turns got compacted away. The
|
|
300
|
+
// per-turn task falls back to the session mission (first real instruction)
|
|
301
|
+
// so a pasted error/log in the current turn cannot erase the goal.
|
|
302
|
+
const task = (this.lastUserTask || this.sessionMission).slice(0, 400);
|
|
303
|
+
if (task)
|
|
304
|
+
parts.push(`[Task: ${task}]`);
|
|
305
|
+
if (this.lastUserFeedback && !task.startsWith(this.lastUserFeedback)) {
|
|
306
|
+
parts.push(`[User feedback: ${this.lastUserFeedback}]`);
|
|
229
307
|
}
|
|
308
|
+
// Orient the model after compaction: an active plan must not be re-created
|
|
309
|
+
// from scratch (see planSummaryProvider doc).
|
|
310
|
+
const planLine = this.planSummaryProvider?.();
|
|
311
|
+
if (planLine)
|
|
312
|
+
parts.push(`[Plan: ${planLine}]`);
|
|
313
|
+
const filesLine = this.facts.filesLine();
|
|
314
|
+
if (filesLine)
|
|
315
|
+
parts.push(filesLine);
|
|
316
|
+
const deletedLine = this.facts.deletedLine();
|
|
317
|
+
if (deletedLine)
|
|
318
|
+
parts.push(deletedLine);
|
|
319
|
+
const readLine = this.facts.readLine();
|
|
320
|
+
if (readLine)
|
|
321
|
+
parts.push(readLine);
|
|
322
|
+
const decisionsLine = this.facts.decisionsLine();
|
|
323
|
+
if (decisionsLine)
|
|
324
|
+
parts.push(decisionsLine);
|
|
325
|
+
const errorsLine = this.facts.errorsLine();
|
|
326
|
+
if (errorsLine)
|
|
327
|
+
parts.push(errorsLine);
|
|
230
328
|
const triedAndFailed = extractTriedAndFailed(oldTurns);
|
|
231
329
|
if (triedAndFailed.length > 0) {
|
|
232
330
|
const lines = triedAndFailed.map((t) => `- ${t.tool}(${t.args}): ${t.error} (failed ${t.count}x)`);
|
|
@@ -245,69 +343,38 @@ export class ContextManager {
|
|
|
245
343
|
const text = getMessageText(m.content);
|
|
246
344
|
return !text.startsWith("<system-summary>");
|
|
247
345
|
});
|
|
248
|
-
this.messages = [
|
|
249
|
-
...(firstSystem ? [firstSystem] : []),
|
|
250
|
-
summary,
|
|
251
|
-
...freshRecent,
|
|
252
|
-
];
|
|
346
|
+
this.messages = [...(firstSystem ? [firstSystem] : []), summary, ...freshRecent];
|
|
253
347
|
this.iterationsSinceCompaction = 0;
|
|
254
348
|
if (this.onCompact) {
|
|
255
349
|
this.onCompact(summary);
|
|
256
350
|
}
|
|
351
|
+
return {
|
|
352
|
+
removedTurns: oldTurns.length,
|
|
353
|
+
keptTurns: freshRecent.length,
|
|
354
|
+
tokensBefore,
|
|
355
|
+
tokensAfter: this.getEstimatedTokens(),
|
|
356
|
+
qualityBefore,
|
|
357
|
+
qualityAfter: this.getQuality(),
|
|
358
|
+
messagesBefore,
|
|
359
|
+
messagesAfter: this.messages.length,
|
|
360
|
+
summary: this.compactedBlock ?? "",
|
|
361
|
+
};
|
|
257
362
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (content.includes("Plan:") && content.includes("[")) {
|
|
276
|
-
const planLine = content
|
|
277
|
-
.split("\n")
|
|
278
|
-
.find((line) => line.includes("Plan:"));
|
|
279
|
-
if (planLine)
|
|
280
|
-
newDecisions.push(planLine.trim());
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
if (msg.role === "assistant") {
|
|
284
|
-
if (content.includes("decided:") || content.includes("decision:")) {
|
|
285
|
-
const line = content
|
|
286
|
-
.split("\n")
|
|
287
|
-
.find((l) => l.includes("decided:") || l.includes("decision:"));
|
|
288
|
-
if (line)
|
|
289
|
-
newDecisions.push(line.trim().slice(0, 200));
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
if (content.includes("Error:") ||
|
|
293
|
-
content.includes("failed") ||
|
|
294
|
-
content.includes("Ошибка:") ||
|
|
295
|
-
content.includes("не удалось")) {
|
|
296
|
-
const line = content
|
|
297
|
-
.split("\n")
|
|
298
|
-
.find((l) => l.includes("Error:") ||
|
|
299
|
-
l.includes("failed") ||
|
|
300
|
-
l.includes("Ошибка:") ||
|
|
301
|
-
l.includes("не удалось"));
|
|
302
|
-
if (line)
|
|
303
|
-
newErrors.push(line.trim().slice(0, 250));
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
// Deduplicate and cap facts to prevent unbounded growth
|
|
307
|
-
const dedup = (arr) => [...new Set(arr)];
|
|
308
|
-
this.fileFacts = dedup([...this.fileFacts, ...newFiles]).slice(-ContextManager.MAX_FACTS);
|
|
309
|
-
this.decisionFacts = dedup([...this.decisionFacts, ...newDecisions]).slice(-ContextManager.MAX_FACTS);
|
|
310
|
-
this.errorFacts = dedup([...this.errorFacts, ...newErrors]).slice(-ContextManager.MAX_FACTS);
|
|
363
|
+
/**
|
|
364
|
+
* Snapshot of the current context state — used to log the initial context
|
|
365
|
+
* structure at session start and per-iteration context stats.
|
|
366
|
+
*/
|
|
367
|
+
getSnapshot() {
|
|
368
|
+
return {
|
|
369
|
+
window: this.contextWindow,
|
|
370
|
+
budget: { ...this.budget },
|
|
371
|
+
tokens: this.getEstimatedTokens(),
|
|
372
|
+
toolTokens: this.toolTokens,
|
|
373
|
+
messageCount: this.messages.length,
|
|
374
|
+
quality: this.getQuality(),
|
|
375
|
+
compactionCount: this.compactionCount,
|
|
376
|
+
iterationsSinceCompaction: this.iterationsSinceCompaction,
|
|
377
|
+
};
|
|
311
378
|
}
|
|
312
379
|
/**
|
|
313
380
|
* Replace the system prompt in place (keeps it first) or prepend a new one.
|
|
@@ -325,16 +392,23 @@ export class ContextManager {
|
|
|
325
392
|
getActiveHistory() {
|
|
326
393
|
return [...this.messages];
|
|
327
394
|
}
|
|
395
|
+
/**
|
|
396
|
+
* Reset everything that belongs to the current conversation turn.
|
|
397
|
+
* Fact lists (FactExtractor) intentionally survive clear() — they are
|
|
398
|
+
* session-scoped compaction state, not per-turn.
|
|
399
|
+
*/
|
|
328
400
|
clear() {
|
|
329
401
|
this.messages = [];
|
|
330
402
|
this.compactedBlock = null;
|
|
331
403
|
this.iterationsSinceCompaction = 0;
|
|
332
404
|
this.compactionCount = 0;
|
|
333
405
|
this.peakTokens = 0;
|
|
406
|
+
this.lastUserTask = "";
|
|
407
|
+
this.lastUserFeedback = "";
|
|
408
|
+
this.sessionMission = "";
|
|
334
409
|
}
|
|
335
410
|
getEstimatedTokens() {
|
|
336
|
-
return (this.messages.reduce((sum, m) => sum + this.estimateMessageTokens(m), 0) +
|
|
337
|
-
this.toolTokens);
|
|
411
|
+
return (this.messages.reduce((sum, m) => sum + this.estimateMessageTokens(m), 0) + this.toolTokens);
|
|
338
412
|
}
|
|
339
413
|
setToolTokens(tokens) {
|
|
340
414
|
this.toolTokens = tokens;
|