micro-models-agent 0.57.0 → 0.57.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/CHANGELOG.md +518 -476
- package/README.md +358 -358
- package/dist/certification/certifications.json +493 -493
- package/dist/cli/commands.js +447 -0
- package/dist/cli/completer.js +167 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/main.js +153 -0
- package/dist/cli/plugin-commands.js +36 -0
- package/dist/cli/repl-commands.js +761 -0
- package/dist/cli/repl.js +702 -0
- package/dist/cli/run-result.js +33 -0
- package/dist/cli/security-commands.js +164 -0
- package/dist/cli/setup.js +237 -0
- package/dist/config/config.js +276 -0
- package/dist/config/defaults.js +141 -0
- package/dist/config/domains.js +179 -0
- package/dist/config/experts.js +15 -0
- package/dist/config/index.js +4 -0
- package/dist/config/security.js +213 -0
- package/dist/config/types.js +1 -0
- package/dist/core/agent-moe.js +102 -0
- package/dist/core/agent.js +1018 -0
- package/dist/core/bootstrap.js +481 -0
- package/dist/core/crash-handler.js +51 -0
- package/dist/core/environment.js +199 -0
- package/dist/core/index.js +2 -0
- package/dist/core/prompt-builder.js +76 -0
- package/dist/core/session-logger.js +251 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.js +26 -0
- package/dist/core/workspace.js +76 -0
- package/dist/i18n/en.json +679 -0
- package/dist/i18n/index.js +46 -0
- package/dist/i18n/ru.json +679 -0
- package/dist/index.js +22 -0
- package/dist/llm/image-utils.js +143 -0
- package/dist/llm/index.js +4 -0
- package/dist/llm/model-loader.js +78 -0
- package/dist/llm/openai-compat.js +497 -0
- package/dist/llm/orchestrator.js +200 -0
- package/dist/llm/provider.js +10 -0
- package/dist/llm/response.js +39 -0
- package/dist/llm/token-counter.js +39 -0
- package/dist/llm/types.js +1 -0
- package/dist/logger/app-logger.js +189 -0
- package/dist/logger/file-log.js +151 -0
- package/dist/logger/index.js +1 -0
- package/dist/main.js +2351 -1640
- package/dist/migration/backup.js +45 -0
- package/dist/migration/detect.js +50 -0
- package/dist/migration/index.js +2 -0
- package/dist/modules/artifacts/store.js +61 -0
- package/dist/modules/browser/actions.js +76 -0
- 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 +219 -219
- package/dist/modules/browser/cookie-store.js +24 -0
- package/dist/modules/browser/driver.js +136 -0
- package/dist/modules/browser/index.js +7 -0
- package/dist/modules/browser/module.js +29 -0
- package/dist/modules/browser/session.js +342 -0
- package/dist/modules/browser/snapshot.js +148 -0
- package/dist/modules/browser/types.js +12 -0
- package/dist/modules/certification/cli.js +213 -0
- package/dist/modules/certification/fact-checker.js +82 -0
- package/dist/modules/certification/loader.js +106 -0
- package/dist/modules/certification/manifest.js +58 -0
- package/dist/modules/certification/runner.js +245 -0
- package/dist/modules/certification/scenarios.js +407 -0
- package/dist/modules/certification/types.js +1 -0
- package/dist/modules/context/chunk-query.js +100 -0
- package/dist/modules/context/fact-extractor.js +168 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -0
- package/dist/modules/context/manager.js +440 -0
- package/dist/modules/execution/audit-runners.js +206 -0
- package/dist/modules/execution/auditor.js +218 -0
- package/dist/modules/execution/execution-plugin.js +431 -0
- package/dist/modules/execution/index.js +8 -0
- package/dist/modules/execution/module.js +625 -0
- package/dist/modules/execution/moe-executor.js +304 -0
- package/dist/modules/execution/plan-coverage.js +68 -0
- package/dist/modules/execution/plan-persister.js +46 -0
- package/dist/modules/execution/plan-store.js +196 -0
- package/dist/modules/execution/plan-tool.js +677 -0
- package/dist/modules/execution/plan-validator.js +153 -0
- package/dist/modules/execution/planner.js +94 -0
- package/dist/modules/execution/stuck-detector.js +746 -0
- package/dist/modules/execution/tracker.js +69 -0
- package/dist/modules/execution/types.js +1 -0
- package/dist/modules/execution/verifier.js +235 -0
- package/dist/modules/execution/windows-commands.js +41 -0
- package/dist/modules/hallucination/confidence.js +66 -0
- package/dist/modules/hallucination/consistency.js +26 -0
- package/dist/modules/hallucination/detector.js +47 -0
- package/dist/modules/hallucination/factual.js +169 -0
- package/dist/modules/hallucination/index.js +5 -0
- package/dist/modules/hallucination/js-identifiers.js +262 -0
- package/dist/modules/hallucination/llm-judge.js +101 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/indexer/cache.js +40 -0
- package/dist/modules/indexer/index.js +3 -0
- package/dist/modules/indexer/module.js +246 -0
- package/dist/modules/indexer/project-profile.js +183 -0
- package/dist/modules/indexer/walker.js +101 -0
- package/dist/modules/lsp/check-tool.js +58 -0
- package/dist/modules/lsp/client.js +389 -0
- package/dist/modules/lsp/command.js +60 -0
- package/dist/modules/lsp/config.js +135 -0
- package/dist/modules/lsp/index.js +3 -0
- package/dist/modules/lsp/module.js +260 -0
- package/dist/modules/lsp/probe.js +86 -0
- package/dist/modules/lsp/project-root.js +32 -0
- package/dist/modules/lsp/startup-check.js +144 -0
- package/dist/modules/lsp/types.js +1 -0
- package/dist/modules/mcp/client.js +399 -0
- package/dist/modules/mcp/index.js +3 -0
- package/dist/modules/mcp/module.js +142 -0
- package/dist/modules/mcp/registry.js +15 -0
- package/dist/modules/memory/index.js +1 -0
- package/dist/modules/memory/module.js +96 -0
- package/dist/modules/memory/search.js +42 -0
- package/dist/modules/memory/store.js +69 -0
- package/dist/modules/pipelines/engine.js +60 -0
- package/dist/modules/pipelines/index.js +3 -0
- package/dist/modules/pipelines/parser.js +56 -0
- package/dist/modules/pipelines/template.js +14 -0
- package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
- package/dist/modules/plugins/builtin/notify.js +9 -0
- package/dist/modules/plugins/index.js +1 -0
- package/dist/modules/plugins/loader.js +70 -0
- package/dist/modules/plugins/manager.js +261 -0
- package/dist/modules/plugins/types.js +1 -0
- package/dist/modules/pricing/index.js +61 -0
- package/dist/modules/pricing/prices.js +129 -0
- package/dist/modules/processes/detect.js +34 -0
- package/dist/modules/processes/index.js +2 -0
- package/dist/modules/processes/registry.js +327 -0
- package/dist/modules/processes/runner.js +23 -0
- package/dist/modules/providers/create.js +22 -0
- package/dist/modules/providers/fallback.js +79 -0
- package/dist/modules/providers/health.js +46 -0
- package/dist/modules/providers/index.js +5 -0
- package/dist/modules/providers/manager.js +161 -0
- package/dist/modules/providers/presets.js +128 -0
- package/dist/modules/providers/registry.js +22 -0
- package/dist/modules/providers/types.js +1 -0
- package/dist/modules/registry.js +48 -0
- package/dist/modules/security/audit-log.js +136 -0
- package/dist/modules/security/audit-notifier.js +292 -0
- package/dist/modules/security/command-validator.js +219 -0
- package/dist/modules/security/content-scanner.js +53 -0
- package/dist/modules/security/data-sanitizer.js +89 -0
- package/dist/modules/security/encryption.js +242 -0
- package/dist/modules/security/index.js +14 -0
- package/dist/modules/security/network-validator.js +88 -0
- package/dist/modules/security/path-validator.js +203 -0
- package/dist/modules/security/rate-limiter.js +119 -0
- package/dist/modules/security/security-policies.js +531 -0
- package/dist/modules/security/session-encryption.js +210 -0
- package/dist/modules/security/session-isolation.js +95 -0
- package/dist/modules/session/index.js +3 -0
- package/dist/modules/session/manager.js +172 -0
- package/dist/modules/session/module.js +24 -0
- package/dist/modules/session/store.js +222 -0
- package/dist/modules/session/types.js +1 -0
- package/dist/modules/skills/index.js +2 -0
- package/dist/modules/skills/loader.js +72 -0
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +129 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/updater/checker.js +96 -0
- package/dist/modules/updater/index.js +2 -0
- package/dist/modules/updater/module.js +116 -0
- package/dist/modules/user-profile/compressor.js +16 -0
- package/dist/modules/user-profile/index.js +1 -0
- package/dist/modules/user-profile/profile.js +68 -0
- package/dist/skills/builtin/git.md +36 -36
- package/dist/skills/builtin/typescript.md +35 -35
- package/dist/tools/approve.js +33 -0
- package/dist/tools/attach-image.js +101 -0
- package/dist/tools/bash.js +519 -0
- package/dist/tools/browser.js +115 -0
- package/dist/tools/chunk-query.js +100 -0
- package/dist/tools/create-dir.js +56 -0
- package/dist/tools/delete-file.js +63 -0
- package/dist/tools/download-file.js +117 -0
- package/dist/tools/edit-file.js +80 -0
- package/dist/tools/enable-tools.js +59 -0
- package/dist/tools/executor.js +154 -0
- package/dist/tools/file-info.js +47 -0
- package/dist/tools/filter-tools.js +17 -0
- package/dist/tools/glob-tool.js +27 -0
- package/dist/tools/grep-tool.js +125 -0
- package/dist/tools/hidden-tools-block.js +37 -0
- package/dist/tools/index.js +78 -0
- package/dist/tools/list-dir.js +49 -0
- package/dist/tools/load-skill.js +43 -0
- package/dist/tools/mcp-call.js +69 -0
- package/dist/tools/move-file.js +86 -0
- package/dist/tools/path-utils.js +101 -0
- package/dist/tools/pipeline-run.js +145 -0
- package/dist/tools/preview.js +2 -0
- package/dist/tools/process-kill.js +40 -0
- package/dist/tools/process-list.js +37 -0
- package/dist/tools/process-log.js +54 -0
- package/dist/tools/question.js +141 -0
- package/dist/tools/read-file.js +179 -0
- package/dist/tools/recall.js +118 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/remember.js +68 -0
- package/dist/tools/scope-check.js +32 -0
- package/dist/tools/search-history.js +85 -0
- package/dist/tools/subagent.js +196 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/user-input.js +123 -0
- package/dist/tools/web-browse.js +87 -0
- package/dist/tools/web-fetch.js +119 -0
- package/dist/tools/web-search.js +105 -0
- package/dist/tools/write-file.js +82 -0
- package/dist/ui/box.js +77 -0
- package/dist/ui/colors.js +4 -0
- package/dist/ui/diff.js +178 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/line-editor.js +822 -0
- package/dist/ui/line-math.js +73 -0
- package/dist/ui/md-formatter.js +212 -0
- package/dist/ui/output.js +13 -0
- package/dist/ui/plan-view.js +103 -0
- package/dist/ui/renderer.js +259 -0
- package/dist/ui/spinner.js +70 -0
- package/dist/ui/table.js +144 -0
- package/package.json +51 -51
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { getExpertConfig } from "../../config/experts";
|
|
2
|
+
import { filterToolsByTags } from "../../tools/filter-tools";
|
|
3
|
+
import { StuckDetector } from "./stuck-detector";
|
|
4
|
+
import { t } from "../../i18n/index";
|
|
5
|
+
import { ArtifactStore } from "../artifacts/store";
|
|
6
|
+
const TRANSIENT_ERROR_PATTERNS = [
|
|
7
|
+
"timeout",
|
|
8
|
+
"Timeout",
|
|
9
|
+
"TIMEOUT",
|
|
10
|
+
"5xx",
|
|
11
|
+
"500",
|
|
12
|
+
"502",
|
|
13
|
+
"503",
|
|
14
|
+
"network",
|
|
15
|
+
"Network",
|
|
16
|
+
"ECONNREFUSED",
|
|
17
|
+
"ECONNRESET",
|
|
18
|
+
"fetch failed",
|
|
19
|
+
"abort",
|
|
20
|
+
"Abort",
|
|
21
|
+
];
|
|
22
|
+
function isTransientError(error) {
|
|
23
|
+
return TRANSIENT_ERROR_PATTERNS.some((p) => error.includes(p));
|
|
24
|
+
}
|
|
25
|
+
function sleep(ms) {
|
|
26
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
27
|
+
}
|
|
28
|
+
export function topologicalSort(subtasks) {
|
|
29
|
+
const sorted = [];
|
|
30
|
+
const remaining = new Set(subtasks.map((s) => s.id));
|
|
31
|
+
const subtaskMap = new Map(subtasks.map((s) => [s.id, s]));
|
|
32
|
+
while (remaining.size > 0) {
|
|
33
|
+
const ready = [];
|
|
34
|
+
for (const id of remaining) {
|
|
35
|
+
const sub = subtaskMap.get(id);
|
|
36
|
+
const depsSatisfied = (sub.depends_on || []).every((d) => !remaining.has(d));
|
|
37
|
+
if (depsSatisfied) {
|
|
38
|
+
ready.push(sub);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (ready.length === 0) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
sorted.push(ready);
|
|
45
|
+
for (const r of ready) {
|
|
46
|
+
remaining.delete(r.id);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return sorted;
|
|
50
|
+
}
|
|
51
|
+
async function executeSubtask(subtask, deps, _sharedContext) {
|
|
52
|
+
const startTime = Date.now();
|
|
53
|
+
const expertConfig = getExpertConfig(deps.config, subtask.expert_tag);
|
|
54
|
+
if (!expertConfig) {
|
|
55
|
+
return {
|
|
56
|
+
subtaskId: subtask.id,
|
|
57
|
+
success: false,
|
|
58
|
+
summary: `Unknown expert_tag: ${subtask.expert_tag}`,
|
|
59
|
+
result: "",
|
|
60
|
+
error: `No expert config found for tag "${subtask.expert_tag}"`,
|
|
61
|
+
durationMs: Date.now() - startTime,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const subagentTool = deps.toolRegistry.get("subagent");
|
|
65
|
+
if (!subagentTool) {
|
|
66
|
+
return {
|
|
67
|
+
subtaskId: subtask.id,
|
|
68
|
+
success: false,
|
|
69
|
+
summary: "Subagent tool not registered",
|
|
70
|
+
result: "",
|
|
71
|
+
error: "Subagent tool not found in registry",
|
|
72
|
+
durationMs: Date.now() - startTime,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const allTools = deps.toolRegistry.getAll();
|
|
76
|
+
const filteredTools = filterToolsByTags(allTools, expertConfig.tool_tags);
|
|
77
|
+
if (filteredTools.length === 0) {
|
|
78
|
+
return {
|
|
79
|
+
subtaskId: subtask.id,
|
|
80
|
+
success: false,
|
|
81
|
+
summary: `No tools available for expert "${subtask.expert_tag}" (tags: ${expertConfig.tool_tags.join(", ")})`,
|
|
82
|
+
result: "",
|
|
83
|
+
error: "No matching tools",
|
|
84
|
+
durationMs: Date.now() - startTime,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const maxAttempts = expertConfig.max_attempts || 3;
|
|
88
|
+
let lastError = "";
|
|
89
|
+
const stuckDetector = new StuckDetector(maxAttempts * 2, maxAttempts);
|
|
90
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
91
|
+
stuckDetector.recordIteration(1);
|
|
92
|
+
try {
|
|
93
|
+
const allowedFiles = subtask.allowed_files || [];
|
|
94
|
+
const readOnlyFiles = subtask.read_only_files || [];
|
|
95
|
+
const scopePrompt = allowedFiles.length > 0 || readOnlyFiles.length > 0
|
|
96
|
+
? `\n\nAllowed files to write: ${allowedFiles.join(", ") || "(none)"}\nRead-only files: ${readOnlyFiles.join(", ") || "(none)"}`
|
|
97
|
+
: "";
|
|
98
|
+
const taskPrompt = `${subtask.description}\n\nExpected output: ${subtask.expected_output}\nSuccess criteria:\n${(subtask.success_criteria || []).map((c) => `- ${c}`).join("\n")}${scopePrompt}`;
|
|
99
|
+
const subagentArgs = {
|
|
100
|
+
task: taskPrompt,
|
|
101
|
+
allowed_files: allowedFiles,
|
|
102
|
+
read_only_files: readOnlyFiles,
|
|
103
|
+
tool_tags: expertConfig.tool_tags,
|
|
104
|
+
};
|
|
105
|
+
if (deps.config.subagent?.resultMode === "file") {
|
|
106
|
+
subagentArgs.result_mode = "file";
|
|
107
|
+
subagentArgs.artifact_name = subtask.id;
|
|
108
|
+
}
|
|
109
|
+
const result = await subagentTool.handler({
|
|
110
|
+
config: deps.config,
|
|
111
|
+
baseDir: deps.baseDir,
|
|
112
|
+
logger: deps.logger,
|
|
113
|
+
llmProvider: deps.llmProvider,
|
|
114
|
+
toolExecutor: deps.toolExecutor,
|
|
115
|
+
sessionId: "moe",
|
|
116
|
+
}, subagentArgs);
|
|
117
|
+
if (result.success) {
|
|
118
|
+
if (deps.config.subagent?.resultMode === "file") {
|
|
119
|
+
const pathMatch = result.output.match(/saved to artifact: (.+)/);
|
|
120
|
+
const abs = pathMatch ? pathMatch[1].trim() : "";
|
|
121
|
+
if (abs) {
|
|
122
|
+
const store = new ArtifactStore({
|
|
123
|
+
baseDir: deps.baseDir,
|
|
124
|
+
sessionId: "moe",
|
|
125
|
+
});
|
|
126
|
+
const full = store.read(abs.split(/[\\/]/).pop() || ""); // file name only, scoped to root
|
|
127
|
+
deps.logger.debug(`MoE subtask "${subtask.id}" attached artifact ${abs} (${full?.length ?? 0} chars) to Router`);
|
|
128
|
+
return {
|
|
129
|
+
subtaskId: subtask.id,
|
|
130
|
+
success: true,
|
|
131
|
+
summary: `Artifact: ${abs}`,
|
|
132
|
+
result: full ?? result.output,
|
|
133
|
+
durationMs: Date.now() - startTime,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
subtaskId: subtask.id,
|
|
139
|
+
success: true,
|
|
140
|
+
summary: `Completed: ${subtask.id}`,
|
|
141
|
+
result: result.output,
|
|
142
|
+
durationMs: Date.now() - startTime,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
lastError = result.output;
|
|
146
|
+
stuckDetector.recordToolError("subagent", result.output);
|
|
147
|
+
// Inject actionable hints into the next retry's prompt
|
|
148
|
+
if (attempt < maxAttempts) {
|
|
149
|
+
const hints = stuckDetector.getActionableHints();
|
|
150
|
+
const recovery = stuckDetector.getRecoveryMessage();
|
|
151
|
+
if (hints.length > 0) {
|
|
152
|
+
deps.logger.debug(`Hint for subtask "${subtask.id}" retry: ${hints.join("; ")}`);
|
|
153
|
+
}
|
|
154
|
+
if (recovery) {
|
|
155
|
+
deps.logger.debug(`Recovery for subtask "${subtask.id}": ${recovery}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (isTransientError(lastError) && attempt < maxAttempts) {
|
|
159
|
+
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 8000);
|
|
160
|
+
deps.logger.debug(`Retrying subtask "${subtask.id}" (attempt ${attempt}/${maxAttempts}) after ${delay}ms: transient error`);
|
|
161
|
+
await sleep(delay);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
// Non-transient failure — build detailed error with hints
|
|
165
|
+
const hints = stuckDetector.getActionableHints();
|
|
166
|
+
let errorDetail = result.output;
|
|
167
|
+
if (hints.length > 0) {
|
|
168
|
+
errorDetail += `\n\n${t("exec.hints", { hints: hints.map((h) => `- ${h}`).join("\n") })}`;
|
|
169
|
+
}
|
|
170
|
+
if (lastError) {
|
|
171
|
+
errorDetail +=
|
|
172
|
+
"\n\nIf relevant skills are available, consider loading one with load_skill for expert guidance.";
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
subtaskId: subtask.id,
|
|
176
|
+
success: false,
|
|
177
|
+
summary: `Failed: ${subtask.id}`,
|
|
178
|
+
result: result.output,
|
|
179
|
+
error: errorDetail,
|
|
180
|
+
durationMs: Date.now() - startTime,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
lastError = e.message;
|
|
185
|
+
stuckDetector.recordToolError("subagent", e.message);
|
|
186
|
+
if (isTransientError(lastError) && attempt < maxAttempts) {
|
|
187
|
+
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 8000);
|
|
188
|
+
deps.logger.debug(`Retrying subtask "${subtask.id}" (attempt ${attempt}/${maxAttempts}) after ${delay}ms: ${lastError}`);
|
|
189
|
+
await sleep(delay);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const hints = stuckDetector.getActionableHints();
|
|
193
|
+
let errorDetail = e.message;
|
|
194
|
+
if (hints.length > 0) {
|
|
195
|
+
errorDetail += `\n\n${t("exec.hints", { hints: hints.map((h) => `- ${h}`).join("\n") })}`;
|
|
196
|
+
}
|
|
197
|
+
if (lastError) {
|
|
198
|
+
errorDetail +=
|
|
199
|
+
"\n\nIf relevant skills are available, consider loading one with load_skill for expert guidance.";
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
subtaskId: subtask.id,
|
|
203
|
+
success: false,
|
|
204
|
+
summary: `Error: ${subtask.id}`,
|
|
205
|
+
result: "",
|
|
206
|
+
error: errorDetail,
|
|
207
|
+
durationMs: Date.now() - startTime,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Exhausted retries — build detailed failure
|
|
212
|
+
const stuckReason = stuckDetector.getStuckReason();
|
|
213
|
+
let finalError = lastError;
|
|
214
|
+
if (stuckReason) {
|
|
215
|
+
finalError += `\n${stuckReason}`;
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
subtaskId: subtask.id,
|
|
219
|
+
success: false,
|
|
220
|
+
summary: `Failed after ${maxAttempts} attempts: ${subtask.id}`,
|
|
221
|
+
result: "",
|
|
222
|
+
error: finalError,
|
|
223
|
+
durationMs: Date.now() - startTime,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
export class MoEExecutor {
|
|
227
|
+
deps;
|
|
228
|
+
constructor(deps) {
|
|
229
|
+
this.deps = deps;
|
|
230
|
+
}
|
|
231
|
+
async executePlan(plan) {
|
|
232
|
+
const results = [];
|
|
233
|
+
const errors = [];
|
|
234
|
+
const warnings = [];
|
|
235
|
+
const waves = topologicalSort(plan.subtasks);
|
|
236
|
+
if (waves.length === 0) {
|
|
237
|
+
return {
|
|
238
|
+
success: false,
|
|
239
|
+
results: [],
|
|
240
|
+
errors: ["Failed to topologically sort subtasks (possible cycle)"],
|
|
241
|
+
warnings: [],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
// Verify all subtasks are included — catch silent drops from unresolved dependencies
|
|
245
|
+
const sortedCount = waves.flat().length;
|
|
246
|
+
if (sortedCount < plan.subtasks.length) {
|
|
247
|
+
const missing = plan.subtasks
|
|
248
|
+
.filter((s) => !waves.flat().some((w) => w.id === s.id))
|
|
249
|
+
.map((s) => s.id);
|
|
250
|
+
return {
|
|
251
|
+
success: false,
|
|
252
|
+
results: [],
|
|
253
|
+
errors: [
|
|
254
|
+
`Missing subtasks after topological sort: ${missing.join(", ")} (dangling or invalid depends_on)`,
|
|
255
|
+
],
|
|
256
|
+
warnings: [],
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
for (let waveIdx = 0; waveIdx < waves.length; waveIdx++) {
|
|
260
|
+
const wave = waves[waveIdx];
|
|
261
|
+
// Cap concurrent subtasks per wave (rateLimits.maxParallelTasks, default 5).
|
|
262
|
+
const maxParallel = this.deps.config?.security?.rateLimits?.maxParallelTasks ?? 5;
|
|
263
|
+
let active = 0;
|
|
264
|
+
const waiters = [];
|
|
265
|
+
const acquire = () => active < maxParallel
|
|
266
|
+
? Promise.resolve(active++)
|
|
267
|
+
: new Promise((resolve) => waiters.push(() => resolve(active++)));
|
|
268
|
+
const release = () => {
|
|
269
|
+
active--;
|
|
270
|
+
const next = waiters.shift();
|
|
271
|
+
if (next)
|
|
272
|
+
next();
|
|
273
|
+
};
|
|
274
|
+
const wavePromises = wave.map(async (subtask) => {
|
|
275
|
+
await acquire();
|
|
276
|
+
let result;
|
|
277
|
+
try {
|
|
278
|
+
result = await executeSubtask(subtask, this.deps, plan.shared_context);
|
|
279
|
+
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
result = {
|
|
282
|
+
subtaskId: subtask.id,
|
|
283
|
+
success: false,
|
|
284
|
+
summary: `Unhandled error: ${subtask.id}`,
|
|
285
|
+
result: "",
|
|
286
|
+
error: e instanceof Error ? e.message : String(e),
|
|
287
|
+
durationMs: 0,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
release();
|
|
292
|
+
}
|
|
293
|
+
results.push(result);
|
|
294
|
+
return result;
|
|
295
|
+
});
|
|
296
|
+
await Promise.all(wavePromises);
|
|
297
|
+
}
|
|
298
|
+
const failed = results.filter((r) => !r.success);
|
|
299
|
+
if (failed.length > 0) {
|
|
300
|
+
errors.push(...failed.map((f) => `[${f.subtaskId}] ${f.error || f.summary}`));
|
|
301
|
+
}
|
|
302
|
+
return { success: failed.length === 0, results, errors, warnings };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const FILE_PATH_RE = /\b[\w./\\-]+\.[a-z]+\b/gi;
|
|
2
|
+
const IGNORED_EXT = new Set([
|
|
3
|
+
"tsx",
|
|
4
|
+
"jsx",
|
|
5
|
+
"json5",
|
|
6
|
+
"mdx",
|
|
7
|
+
"yml",
|
|
8
|
+
"yaml",
|
|
9
|
+
"toml",
|
|
10
|
+
"lock",
|
|
11
|
+
"svg",
|
|
12
|
+
"png",
|
|
13
|
+
"jpg",
|
|
14
|
+
"jpeg",
|
|
15
|
+
"gif",
|
|
16
|
+
"webp",
|
|
17
|
+
"ico",
|
|
18
|
+
"css",
|
|
19
|
+
"scss",
|
|
20
|
+
"less",
|
|
21
|
+
"html",
|
|
22
|
+
]);
|
|
23
|
+
/**
|
|
24
|
+
* Extract file paths (with extensions) mentioned in a task statement.
|
|
25
|
+
* Used to check whether a generated plan covers every file the task requires.
|
|
26
|
+
*/
|
|
27
|
+
export function extractFilePaths(text) {
|
|
28
|
+
if (!text)
|
|
29
|
+
return [];
|
|
30
|
+
const seen = new Set();
|
|
31
|
+
const result = [];
|
|
32
|
+
for (const m of text.matchAll(FILE_PATH_RE)) {
|
|
33
|
+
let p = m[0];
|
|
34
|
+
// Trim trailing punctuation that regex greedily keeps (e.g. "test.ts.")
|
|
35
|
+
p = p.replace(/[.,;:)\]>]+$/g, "");
|
|
36
|
+
const ext = p.split(".").pop()?.toLowerCase() ?? "";
|
|
37
|
+
if (!p || IGNORED_EXT.has(ext))
|
|
38
|
+
continue;
|
|
39
|
+
const key = p.toLowerCase();
|
|
40
|
+
if (seen.has(key))
|
|
41
|
+
continue;
|
|
42
|
+
seen.add(key);
|
|
43
|
+
result.push(p);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
function basename(p) {
|
|
48
|
+
const parts = p.split(/[/\\]/);
|
|
49
|
+
return parts[parts.length - 1] ?? p;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Compare the file requirements in the task statement against the plan's step
|
|
53
|
+
* descriptions. A requirement is "covered" when its path (or its basename)
|
|
54
|
+
* appears in at least one step. Returns the missing paths.
|
|
55
|
+
*/
|
|
56
|
+
export function checkPlanCoverage(taskText, stepDescriptions) {
|
|
57
|
+
const taskPaths = extractFilePaths(taskText);
|
|
58
|
+
if (taskPaths.length === 0)
|
|
59
|
+
return { missing: [] };
|
|
60
|
+
const stepText = stepDescriptions.map((s) => s.toLowerCase()).join("\n");
|
|
61
|
+
const missing = taskPaths.filter((p) => {
|
|
62
|
+
const lower = p.toLowerCase();
|
|
63
|
+
const base = basename(lower);
|
|
64
|
+
// A step mentioning either the full relative path or its basename counts.
|
|
65
|
+
return !stepText.includes(lower) && !stepText.includes(base);
|
|
66
|
+
});
|
|
67
|
+
return { missing };
|
|
68
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
export class PlanPersister {
|
|
4
|
+
filePath;
|
|
5
|
+
constructor(baseDir) {
|
|
6
|
+
const mmaDir = join(baseDir, ".mma");
|
|
7
|
+
if (!existsSync(mmaDir)) {
|
|
8
|
+
mkdirSync(mmaDir, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
this.filePath = join(mmaDir, "plan.json");
|
|
11
|
+
}
|
|
12
|
+
save(plan) {
|
|
13
|
+
const file = {
|
|
14
|
+
id: plan.id,
|
|
15
|
+
title: plan.title,
|
|
16
|
+
steps: plan.steps,
|
|
17
|
+
createdAt: plan.createdAt,
|
|
18
|
+
updatedAt: new Date().toISOString(),
|
|
19
|
+
baseDir: plan.baseDir,
|
|
20
|
+
};
|
|
21
|
+
writeFileSync(this.filePath, JSON.stringify(file, null, 2), "utf-8");
|
|
22
|
+
}
|
|
23
|
+
load() {
|
|
24
|
+
if (!existsSync(this.filePath))
|
|
25
|
+
return null;
|
|
26
|
+
try {
|
|
27
|
+
const raw = readFileSync(this.filePath, "utf-8");
|
|
28
|
+
const file = JSON.parse(raw);
|
|
29
|
+
return {
|
|
30
|
+
id: file.id || "plan_legacy",
|
|
31
|
+
title: file.title,
|
|
32
|
+
steps: file.steps,
|
|
33
|
+
createdAt: file.createdAt,
|
|
34
|
+
baseDir: file.baseDir || process.cwd(),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
clear() {
|
|
42
|
+
if (existsSync(this.filePath)) {
|
|
43
|
+
writeFileSync(this.filePath, "", "utf-8");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync, rmSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
const LEGACY_FILE = "plan.json";
|
|
4
|
+
function readPlanFile(path, fallbackBaseDir) {
|
|
5
|
+
try {
|
|
6
|
+
const raw = readFileSync(path, "utf-8");
|
|
7
|
+
if (!raw.trim())
|
|
8
|
+
return null;
|
|
9
|
+
const parsed = JSON.parse(raw);
|
|
10
|
+
if (!parsed || !Array.isArray(parsed.steps))
|
|
11
|
+
return null;
|
|
12
|
+
return {
|
|
13
|
+
id: parsed.id || "plan_legacy",
|
|
14
|
+
title: parsed.title || "Legacy plan",
|
|
15
|
+
steps: parsed.steps,
|
|
16
|
+
createdAt: parsed.createdAt || new Date().toISOString(),
|
|
17
|
+
baseDir: parsed.baseDir || fallbackBaseDir,
|
|
18
|
+
name: parsed.name,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function writePlanFile(path, plan) {
|
|
26
|
+
writeFileSync(path, JSON.stringify(plan, null, 2), "utf-8");
|
|
27
|
+
}
|
|
28
|
+
function listDir(dir, baseDir) {
|
|
29
|
+
if (!existsSync(dir))
|
|
30
|
+
return [];
|
|
31
|
+
const files = readdirSync(dir).filter((f) => f.endsWith(".json"));
|
|
32
|
+
return files.map((f) => readPlanFile(join(dir, f), baseDir)).filter((p) => p !== null);
|
|
33
|
+
}
|
|
34
|
+
function toMeta(plan, status) {
|
|
35
|
+
return {
|
|
36
|
+
id: plan.id,
|
|
37
|
+
name: plan.name,
|
|
38
|
+
title: plan.title,
|
|
39
|
+
status,
|
|
40
|
+
createdAt: plan.createdAt,
|
|
41
|
+
stepCount: plan.steps.length,
|
|
42
|
+
doneCount: plan.steps.filter((s) => s.status === "done" || s.status === "skipped").length,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export class PlanStore {
|
|
46
|
+
baseDir;
|
|
47
|
+
plansDir;
|
|
48
|
+
draftsDir;
|
|
49
|
+
archiveDir;
|
|
50
|
+
legacyPath;
|
|
51
|
+
constructor(baseDir) {
|
|
52
|
+
const mmaDir = join(baseDir, ".mma");
|
|
53
|
+
if (!existsSync(mmaDir))
|
|
54
|
+
mkdirSync(mmaDir, { recursive: true });
|
|
55
|
+
this.baseDir = baseDir;
|
|
56
|
+
this.plansDir = join(mmaDir, "plans");
|
|
57
|
+
this.draftsDir = join(this.plansDir, "drafts");
|
|
58
|
+
this.archiveDir = join(this.plansDir, "archive");
|
|
59
|
+
this.legacyPath = join(mmaDir, LEGACY_FILE);
|
|
60
|
+
for (const dir of [this.plansDir, this.draftsDir, this.archiveDir]) {
|
|
61
|
+
if (!existsSync(dir))
|
|
62
|
+
mkdirSync(dir, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
activePath() {
|
|
66
|
+
return join(this.plansDir, "active.json");
|
|
67
|
+
}
|
|
68
|
+
// ---- Active plan ----
|
|
69
|
+
saveActive(plan) {
|
|
70
|
+
writePlanFile(this.activePath(), plan);
|
|
71
|
+
}
|
|
72
|
+
loadActive() {
|
|
73
|
+
const activePath = this.activePath();
|
|
74
|
+
if (existsSync(activePath)) {
|
|
75
|
+
const plan = readPlanFile(activePath, this.baseDir);
|
|
76
|
+
if (plan)
|
|
77
|
+
return plan;
|
|
78
|
+
}
|
|
79
|
+
// Legacy migration: read .mma/plan.json and move to active
|
|
80
|
+
if (existsSync(this.legacyPath)) {
|
|
81
|
+
const legacy = readPlanFile(this.legacyPath, this.baseDir);
|
|
82
|
+
if (legacy) {
|
|
83
|
+
this.saveActive(legacy);
|
|
84
|
+
try {
|
|
85
|
+
rmSync(this.legacyPath, { force: true });
|
|
86
|
+
}
|
|
87
|
+
catch { }
|
|
88
|
+
return legacy;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
clearActive() {
|
|
94
|
+
const p = this.activePath();
|
|
95
|
+
if (existsSync(p))
|
|
96
|
+
rmSync(p, { force: true });
|
|
97
|
+
}
|
|
98
|
+
// ---- Drafts (saved plans) ----
|
|
99
|
+
saveDraft(plan) {
|
|
100
|
+
writePlanFile(join(this.draftsDir, `${plan.id}.json`), plan);
|
|
101
|
+
}
|
|
102
|
+
loadDraft(id) {
|
|
103
|
+
const p = join(this.draftsDir, `${id}.json`);
|
|
104
|
+
return existsSync(p) ? readPlanFile(p, this.baseDir) : null;
|
|
105
|
+
}
|
|
106
|
+
removeDraft(id) {
|
|
107
|
+
const p = join(this.draftsDir, `${id}.json`);
|
|
108
|
+
if (existsSync(p))
|
|
109
|
+
rmSync(p, { force: true });
|
|
110
|
+
}
|
|
111
|
+
listDrafts() {
|
|
112
|
+
return listDir(this.draftsDir, this.baseDir);
|
|
113
|
+
}
|
|
114
|
+
// ---- Archive ----
|
|
115
|
+
archivePlan(plan) {
|
|
116
|
+
writePlanFile(join(this.archiveDir, `${plan.id}.json`), plan);
|
|
117
|
+
// Remove from drafts if present
|
|
118
|
+
this.removeDraft(plan.id);
|
|
119
|
+
// If archived plan was active, clear active
|
|
120
|
+
const active = this.loadActive();
|
|
121
|
+
if (active && active.id === plan.id) {
|
|
122
|
+
this.clearActive();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
listArchived() {
|
|
126
|
+
return listDir(this.archiveDir, this.baseDir);
|
|
127
|
+
}
|
|
128
|
+
removeArchived(id) {
|
|
129
|
+
const p = join(this.archiveDir, `${id}.json`);
|
|
130
|
+
if (existsSync(p))
|
|
131
|
+
rmSync(p, { force: true });
|
|
132
|
+
}
|
|
133
|
+
// ---- Cross-status queries ----
|
|
134
|
+
listAll() {
|
|
135
|
+
const metas = [];
|
|
136
|
+
const active = this.loadActive();
|
|
137
|
+
if (active)
|
|
138
|
+
metas.push(toMeta(active, "active"));
|
|
139
|
+
for (const p of this.listDrafts())
|
|
140
|
+
metas.push(toMeta(p, "draft"));
|
|
141
|
+
for (const p of this.listArchived())
|
|
142
|
+
metas.push(toMeta(p, "archived"));
|
|
143
|
+
return metas;
|
|
144
|
+
}
|
|
145
|
+
find(id) {
|
|
146
|
+
const active = this.loadActive();
|
|
147
|
+
if (active && active.id === id)
|
|
148
|
+
return { plan: active, status: "active" };
|
|
149
|
+
const draft = this.loadDraft(id);
|
|
150
|
+
if (draft)
|
|
151
|
+
return { plan: draft, status: "draft" };
|
|
152
|
+
const archived = this.listArchived().find((p) => p.id === id);
|
|
153
|
+
if (archived)
|
|
154
|
+
return { plan: archived, status: "archived" };
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
// ---- Deletion (the user asked for plans to actually go away) ----
|
|
158
|
+
/** Permanently delete a plan wherever it lives. Returns its status, or null
|
|
159
|
+
* when no plan with that id exists. */
|
|
160
|
+
deletePlan(id) {
|
|
161
|
+
const active = this.loadActive();
|
|
162
|
+
if (active && active.id === id) {
|
|
163
|
+
this.clearActive();
|
|
164
|
+
return "active";
|
|
165
|
+
}
|
|
166
|
+
const draftPath = join(this.draftsDir, `${id}.json`);
|
|
167
|
+
if (existsSync(draftPath)) {
|
|
168
|
+
rmSync(draftPath, { force: true });
|
|
169
|
+
return "draft";
|
|
170
|
+
}
|
|
171
|
+
const archivedPath = join(this.archiveDir, `${id}.json`);
|
|
172
|
+
if (existsSync(archivedPath)) {
|
|
173
|
+
rmSync(archivedPath, { force: true });
|
|
174
|
+
return "archived";
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
/** Delete every plan (active, drafts, archive). Returns the number removed. */
|
|
179
|
+
purgeAll() {
|
|
180
|
+
let n = 0;
|
|
181
|
+
const active = this.loadActive();
|
|
182
|
+
if (active) {
|
|
183
|
+
this.clearActive();
|
|
184
|
+
n++;
|
|
185
|
+
}
|
|
186
|
+
for (const p of this.listDrafts()) {
|
|
187
|
+
this.removeDraft(p.id);
|
|
188
|
+
n++;
|
|
189
|
+
}
|
|
190
|
+
for (const p of this.listArchived()) {
|
|
191
|
+
this.removeArchived(p.id);
|
|
192
|
+
n++;
|
|
193
|
+
}
|
|
194
|
+
return n;
|
|
195
|
+
}
|
|
196
|
+
}
|