erosolar-cli 2.1.191 → 2.1.192

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 (53) hide show
  1. package/README.md +54 -0
  2. package/dist/contracts/models.schema.json +9 -0
  3. package/dist/core/agent.d.ts +13 -11
  4. package/dist/core/agent.d.ts.map +1 -1
  5. package/dist/core/agent.js +33 -84
  6. package/dist/core/agent.js.map +1 -1
  7. package/dist/core/agentOrchestrator.d.ts +49 -0
  8. package/dist/core/agentOrchestrator.d.ts.map +1 -0
  9. package/dist/core/agentOrchestrator.js +313 -0
  10. package/dist/core/agentOrchestrator.js.map +1 -0
  11. package/dist/core/schemaValidator.d.ts +5 -0
  12. package/dist/core/schemaValidator.d.ts.map +1 -1
  13. package/dist/core/schemaValidator.js +65 -0
  14. package/dist/core/schemaValidator.js.map +1 -1
  15. package/dist/core/taskCompletionDetector.d.ts.map +1 -0
  16. package/dist/{shell → core}/taskCompletionDetector.js +1 -1
  17. package/dist/core/taskCompletionDetector.js.map +1 -0
  18. package/dist/core/toolRuntime.d.ts +4 -0
  19. package/dist/core/toolRuntime.d.ts.map +1 -1
  20. package/dist/core/toolRuntime.js +40 -64
  21. package/dist/core/toolRuntime.js.map +1 -1
  22. package/dist/runtime/agentSession.d.ts.map +1 -1
  23. package/dist/runtime/agentSession.js +1 -2
  24. package/dist/runtime/agentSession.js.map +1 -1
  25. package/dist/shell/interactiveShell.d.ts +19 -4
  26. package/dist/shell/interactiveShell.d.ts.map +1 -1
  27. package/dist/shell/interactiveShell.js +409 -227
  28. package/dist/shell/interactiveShell.js.map +1 -1
  29. package/dist/shell/shellApp.js +8 -1
  30. package/dist/shell/shellApp.js.map +1 -1
  31. package/dist/tools/bashTools.d.ts +0 -1
  32. package/dist/tools/bashTools.d.ts.map +1 -1
  33. package/dist/tools/bashTools.js +3 -47
  34. package/dist/tools/bashTools.js.map +1 -1
  35. package/dist/tools/buildTools.js +1 -1
  36. package/dist/tools/buildTools.js.map +1 -1
  37. package/dist/tools/grepTools.js +6 -4
  38. package/dist/tools/grepTools.js.map +1 -1
  39. package/dist/tools/repoChecksTools.d.ts.map +1 -1
  40. package/dist/tools/repoChecksTools.js +5 -7
  41. package/dist/tools/repoChecksTools.js.map +1 -1
  42. package/dist/ui/ShellUIAdapter.d.ts +3 -2
  43. package/dist/ui/ShellUIAdapter.d.ts.map +1 -1
  44. package/dist/ui/ShellUIAdapter.js +9 -7
  45. package/dist/ui/ShellUIAdapter.js.map +1 -1
  46. package/package.json +1 -1
  47. package/dist/runtime/flowOrchestrator.d.ts +0 -72
  48. package/dist/runtime/flowOrchestrator.d.ts.map +0 -1
  49. package/dist/runtime/flowOrchestrator.js +0 -486
  50. package/dist/runtime/flowOrchestrator.js.map +0 -1
  51. package/dist/shell/taskCompletionDetector.d.ts.map +0 -1
  52. package/dist/shell/taskCompletionDetector.js.map +0 -1
  53. /package/dist/{shell → core}/taskCompletionDetector.d.ts +0 -0
@@ -4,6 +4,7 @@ import { exec } from 'node:child_process';
4
4
  import { promisify } from 'node:util';
5
5
  import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
6
6
  import { join, resolve } from 'node:path';
7
+ import { AgentOrchestrator } from '../core/agentOrchestrator.js';
7
8
  import { display } from '../ui/display.js';
8
9
  import { theme } from '../ui/theme.js';
9
10
  import { getTerminalColumns } from '../ui/layout.js';
@@ -12,13 +13,11 @@ import { ensureSecretForProvider, getSecretDefinitionForProvider, getSecretValue
12
13
  import { saveActiveProfilePreference, saveModelPreference, loadToolSettings, saveToolSettings, clearToolSettings, clearActiveProfilePreference, loadSessionPreferences, saveSessionPreferences, loadFeatureFlags, saveFeatureFlags, toggleFeatureFlag, FEATURE_FLAG_INFO, } from '../core/preferences.js';
13
14
  import { getLearningSummary, getRecentLearning, commitLearning, exportAllLearning, getLearningDir, } from '../core/learningPersistence.js';
14
15
  import { buildEnabledToolSet, evaluateToolPermissions, getToolToggleOptions, } from '../capabilities/toolRegistry.js';
15
- import { FlowOrchestrator } from '../runtime/flowOrchestrator.js';
16
16
  import { detectApiKeyError } from '../core/errors/apiKeyErrors.js';
17
17
  import { detectPromptBlockError, } from '../core/errors/promptBlockErrors.js';
18
18
  import { detectNetworkError } from '../core/errors/networkErrors.js';
19
19
  import { buildWorkspaceContext } from '../workspace.js';
20
20
  import { buildInteractiveSystemPrompt } from './systemPrompt.js';
21
- import { getTaskCompletionDetector, resetTaskCompletionDetector, WRITE_TOOLS, } from './taskCompletionDetector.js';
22
21
  import { discoverAllModels, quickCheckProviders, getCachedDiscoveredModels, sortModelsByPriority } from '../core/modelDiscovery.js';
23
22
  import { getModels, getSlashCommands, getProviders } from '../core/agentSchemaLoader.js';
24
23
  import { loadMcpServers } from '../mcp/config.js';
@@ -86,10 +85,6 @@ const CONTEXT_CLEANUP_SYSTEM_PROMPT = `Summarize earlier IDE collaboration so th
86
85
  - Respond in plain Markdown only (no tool or shell calls).`;
87
86
  const MAX_ATTACHMENT_BYTES = 200 * 1024; // 200KB per attachment
88
87
  const MAX_ATTACHMENT_CHARS = 16000; // Guardrail to avoid flooding context
89
- const WRITE_TOOL_NAMES = new Set(Array.from(WRITE_TOOLS)
90
- .map((tool) => tool.toLowerCase())
91
- // Bash/execute commands can be read-only; guard only on clear mutating tools
92
- .filter((tool) => !tool.includes('bash') && !tool.startsWith('execute')));
93
88
  export class InteractiveShell {
94
89
  agent = null;
95
90
  profile;
@@ -124,7 +119,6 @@ export class InteractiveShell {
124
119
  uiUpdates;
125
120
  _fileChangeTracker = new FileChangeTracker(); // Reserved for future file tracking features
126
121
  alphaZeroMetrics; // Alpha Zero 2 performance tracking
127
- flowOrchestrator = new FlowOrchestrator();
128
122
  maxNetworkRetries = 2;
129
123
  statusSubscription = null;
130
124
  followUpQueue = [];
@@ -140,6 +134,7 @@ export class InteractiveShell {
140
134
  lastContextWarningLevel = null;
141
135
  sessionPreferences;
142
136
  autosaveEnabled;
137
+ orchestrationNoticeShown = false;
143
138
  verificationEnabled = false;
144
139
  criticalApprovalMode = 'auto';
145
140
  editGuardMode = 'display-edits';
@@ -1997,9 +1992,8 @@ export class InteractiveShell {
1997
1992
  return;
1998
1993
  }
1999
1994
  const isReasoning = type === 'reasoning';
2000
- // Approximate token count (roughly 4 chars per token)
2001
- this.streamingTokenCount += Math.ceil(chunk.length / 4);
2002
- this.renderer?.updateStreamingTokens(this.streamingTokenCount);
1995
+ // Token count is updated from real provider usage data in onAssistantMessage
1996
+ // Do NOT estimate tokens from chunk length - wait for actual API response
2003
1997
  // Keep pinned status updated for all streaming chunks
2004
1998
  this.updateStreamingStatusFromChunk(chunk);
2005
1999
  // Handle <thinking> tags as separate events in the queue
@@ -2276,14 +2270,17 @@ export class InteractiveShell {
2276
2270
  // Check for continuous/infinite loop commands or auto-escalation to completion mode
2277
2271
  const explicitContinuous = this.isContinuousCommand(trimmed);
2278
2272
  const autoContinuous = this.shouldAutoRunToCompletion(trimmed);
2279
- if (explicitContinuous || autoContinuous) {
2280
- if (autoContinuous && !explicitContinuous) {
2281
- display.showSystemMessage('⚡ Actionable request detected; running continuously until complete (Ctrl+C to stop).');
2282
- }
2273
+ if (explicitContinuous) {
2283
2274
  await this.processContinuousRequest(trimmed);
2284
2275
  this.syncRendererInput();
2285
2276
  return;
2286
2277
  }
2278
+ if (autoContinuous) {
2279
+ display.showSystemMessage('⚡ Actionable request detected; orchestrating until complete (Ctrl+C to stop).');
2280
+ await this.processRequest(trimmed, { orchestrate: true });
2281
+ this.syncRendererInput();
2282
+ return;
2283
+ }
2287
2284
  // Direct execution for all inputs, including multi-line pastes
2288
2285
  await this.processRequest(trimmed);
2289
2286
  this.syncRendererInput();
@@ -2320,19 +2317,113 @@ export class InteractiveShell {
2320
2317
  ];
2321
2318
  const strongMaintenanceIntent = maintenancePatterns.some((pattern) => pattern.test(normalized));
2322
2319
  // General action intent in a code context
2323
- const actionVerb = /\b(fix|resolve|address|refactor|implement|upgrade|migrate|optimi[sz]e|modernize|stabilize|harden|ship|complete|finish|clean|remove|delete|prune|tidy|audit|cleanup)\b/;
2320
+ const actionVerb = /\b(fix|resolve|address|refactor|implement|upgrade|migrate|optimi[sz]e|modernize|stabilize|harden|ship|complete|finish|clean|remove|delete|prune|tidy|audit|cleanup|debug|investigate|triage|scan|check|diagnos(e|is))\b/;
2324
2321
  const codeContext = /\b(repo|codebase|project|app|service|package|module|component|workspace|cli|tests?|source|files?|artifacts?)\b/;
2325
- const strongActionIntent = actionVerb.test(normalized) && codeContext.test(normalized) && normalized.length > 20;
2326
- // Allow maintenance/refactor tasks to auto-run even if phrased as questions ("got any junk to remove?")
2327
- if (strongMaintenanceIntent || strongActionIntent) {
2328
- // But still avoid purely informational questions like "what is this repo?"
2329
- if (startsWithInfoWord && !strongMaintenanceIntent) {
2330
- return false;
2331
- }
2332
- return true;
2322
+ const hasActionVerb = actionVerb.test(normalized);
2323
+ const hasCodeContext = codeContext.test(normalized);
2324
+ // Bug/issue hunt or health-check intents, even when phrased as questions
2325
+ const bugSweepIntent = /\b(got\s+any|any|open|known)\s+(bugs?|issues?|defects?)\b/;
2326
+ const triageIntent = /\b(find|check|hunt|triage|detect|scan|review|look\s+for)\s+(bugs?|issues?|errors?|failures?)\b/;
2327
+ const failingTestsIntent = /\b(failing|broken|red)\s+tests?\b|\btests?\s+(are\s+)?failing\b/;
2328
+ const qualitySweepIntent = /\b(audit|health\s+check|stability\s+check|bug\s+scan|issue\s+scan|regression\s+pass)\b/;
2329
+ const bugOrHealthIntent = [bugSweepIntent, triageIntent, failingTestsIntent, qualitySweepIntent].some((pattern) => pattern.test(normalized));
2330
+ // Score-based intent detection keeps questions like "what is this repo?" out
2331
+ let actionScore = 0;
2332
+ if (strongMaintenanceIntent)
2333
+ actionScore += 2;
2334
+ if (bugOrHealthIntent)
2335
+ actionScore += 2;
2336
+ if (hasActionVerb)
2337
+ actionScore += 1;
2338
+ if (hasCodeContext)
2339
+ actionScore += 1;
2340
+ const decisiveAction = actionScore >= 2 || (actionScore === 1 && !isQuestion && normalized.length > 10);
2341
+ if (!decisiveAction) {
2342
+ return false;
2333
2343
  }
2334
- return false;
2344
+ // Respect informational openers unless intent is clearly actionable
2345
+ if (startsWithInfoWord && actionScore < 3 && !bugOrHealthIntent && !strongMaintenanceIntent) {
2346
+ return false;
2347
+ }
2348
+ return true;
2335
2349
  }
2350
+ shouldContinueOrchestrating(originalRequest, result) {
2351
+ if (!result) {
2352
+ return { shouldContinue: false, reason: null };
2353
+ }
2354
+ const actionable = this.shouldAutoRunToCompletion(originalRequest);
2355
+ if (!actionable) {
2356
+ const incomplete = result.exitReason !== 'complete';
2357
+ return { shouldContinue: incomplete, reason: incomplete ? result.exitReason : null };
2358
+ }
2359
+ if (result.exitReason !== 'complete') {
2360
+ return { shouldContinue: true, reason: result.exitReason };
2361
+ }
2362
+ const executedTools = result.passes.some(pass => (pass.toolsUsed?.length ?? 0) > 0);
2363
+ const lastPass = result.passes[result.passes.length - 1];
2364
+ const lastPlanOnly = Boolean(lastPass?.planOnly) && !lastPass?.tookAction && (!lastPass?.toolsUsed?.length);
2365
+ const lastEmpty = !lastPass?.response?.trim();
2366
+ if (!executedTools) {
2367
+ return { shouldContinue: true, reason: 'no-action' };
2368
+ }
2369
+ if (lastPlanOnly) {
2370
+ return { shouldContinue: true, reason: 'plan-only' };
2371
+ }
2372
+ if (lastEmpty) {
2373
+ return { shouldContinue: true, reason: 'empty-response' };
2374
+ }
2375
+ return { shouldContinue: false, reason: null };
2376
+ }
2377
+ describeContinuationReason(reason) {
2378
+ switch (reason) {
2379
+ case 'no-action':
2380
+ return 'No tools or concrete actions were executed; continuing until real work is done.';
2381
+ case 'plan-only':
2382
+ return 'Last pass was just planning; executing the next concrete step now.';
2383
+ case 'empty-response':
2384
+ case 'empty':
2385
+ return 'Previous pass returned nothing; resuming with a concrete action.';
2386
+ case 'stalled':
2387
+ return 'Previous orchestration stalled; forcing continuation.';
2388
+ case 'max-passes':
2389
+ return 'Reached pass limit; extending the run to finish the task.';
2390
+ default:
2391
+ return 'Continuing orchestration until the task is actually finished.';
2392
+ }
2393
+ }
2394
+ buildForcedContinuationPrompt(originalRequest, lastResult, continuationReason) {
2395
+ const lastPass = lastResult.passes[lastResult.passes.length - 1];
2396
+ const rawResponse = lastPass?.response?.trim() ?? '';
2397
+ const truncatedResponse = rawResponse ? rawResponse.slice(0, 1200) : '';
2398
+ const responseNote = rawResponse && rawResponse.length > truncatedResponse.length
2399
+ ? '\n\n[Last response truncated]'
2400
+ : '';
2401
+ const usedTools = lastResult.passes.flatMap(pass => pass.toolsUsed ?? []);
2402
+ const hasToolUsage = usedTools.length > 0;
2403
+ const toolsUsed = hasToolUsage
2404
+ ? `Tools used so far: ${usedTools.join(', ')}.`
2405
+ : 'No tools have been used yet - start with a quick workspace scan (list_files + glob/grep/search) and then take concrete actions now.';
2406
+ const responseSection = truncatedResponse
2407
+ ? `Last response snapshot:\n${truncatedResponse}${responseNote}\n`
2408
+ : '';
2409
+ const exitReason = continuationReason ?? lastResult.exitReason ?? 'incomplete';
2410
+ const reasonLine = this.describeContinuationReason(exitReason);
2411
+ const missingActionLine = hasToolUsage
2412
+ ? ''
2413
+ : 'No tool calls or file edits have been made yet. Start with list_files + glob/grep/search to sweep the repo, then run a command (read/search/test/edit) and show the output before summarizing.';
2414
+ return `${originalRequest.trim()}
2415
+
2416
+ The previous orchestration stopped early (reason: ${exitReason}). Continue working until the task is truly finished. Use tools, run checks, and only declare completion when nothing remains.
2417
+
2418
+ ${reasonLine}
2419
+ ${toolsUsed}
2420
+ ${responseSection}
2421
+ ${missingActionLine ? `${missingActionLine}\n` : ''}Resume with the next concrete action now.`;
2422
+ }
2423
+ /**
2424
+ * Decide whether to automatically continue execution when the model stops after a plan/summary
2425
+ * without taking actions. This keeps flows moving toward completion instead of stalling on planning.
2426
+ */
2336
2427
  isExitCommand(input) {
2337
2428
  const lower = input.trim().toLowerCase();
2338
2429
  return (lower === 'exit' ||
@@ -5780,22 +5871,20 @@ export class InteractiveShell {
5780
5871
  this.clearInlinePanel();
5781
5872
  this.syncRendererInput();
5782
5873
  }
5783
- async runFlowControlledTask(initialRequest, options) {
5784
- const { mode } = options;
5785
- const followUpType = mode === 'continuous' ? 'continuous' : 'request';
5874
+ async processRequest(request, options) {
5786
5875
  if (this.isProcessing) {
5787
- this.enqueueFollowUpAction({ type: followUpType, text: initialRequest });
5788
- return null;
5876
+ this.enqueueFollowUpAction({ type: 'request', text: request });
5877
+ return;
5789
5878
  }
5790
5879
  if (!this.agent && !this.rebuildAgent()) {
5791
5880
  display.showWarning('Configure an API key via /secrets before sending requests.');
5792
- return null;
5881
+ return;
5793
5882
  }
5794
5883
  this.inlinePanelScopeActive = false;
5795
5884
  this.clearInlinePanel();
5796
5885
  const agent = this.agent;
5797
5886
  if (!agent) {
5798
- return null;
5887
+ return;
5799
5888
  }
5800
5889
  this.toolsUsedThisRun = [];
5801
5890
  this.currentToolCalls = [];
@@ -5806,169 +5895,166 @@ export class InteractiveShell {
5806
5895
  else {
5807
5896
  this.resetNetworkRetryState();
5808
5897
  }
5898
+ // Reset per-request render tracking
5809
5899
  this.responseRendered = false;
5810
- if (this.shouldLogPrompt(initialRequest)) {
5811
- this.logUserPrompt(initialRequest);
5900
+ if (this.shouldLogPrompt(request)) {
5901
+ this.logUserPrompt(request);
5812
5902
  }
5813
5903
  this.isProcessing = true;
5814
5904
  this.uiUpdates.setMode('processing');
5815
- this.streamingTokenCount = 0;
5905
+ this.streamingTokenCount = 0; // Reset token counter for new request
5816
5906
  this.terminalInput.setStreaming(true);
5907
+ // Keep the persistent input/control bar active as we transition into streaming.
5817
5908
  this.syncRendererInput();
5818
5909
  this.renderer?.render();
5819
- const overallStartTime = Date.now();
5910
+ const requestStartTime = Date.now(); // Alpha Zero 2 timing
5911
+ // Clear previous parallel agents and start fresh for new request
5820
5912
  const parallelManager = getParallelAgentManager();
5821
5913
  parallelManager.clear();
5822
5914
  parallelManager.startBatch();
5823
- this.lastUserQuery = initialRequest;
5824
- this.currentTaskType = classifyTaskType(initialRequest);
5915
+ // AlphaZero: Track task for learning
5916
+ this.lastUserQuery = request;
5917
+ this.currentTaskType = classifyTaskType(request);
5825
5918
  this.currentToolCalls = [];
5826
5919
  this.clearToolUsageMeta();
5827
5920
  this.renderer?.setActivity('Starting...');
5828
- this.uiAdapter.startProcessing(mode === 'continuous' ? 'Continuous execution mode' : 'Working on your request');
5921
+ this.uiAdapter.startProcessing('Working on your request');
5829
5922
  this.setProcessingStatus();
5830
5923
  this.beginAiRuntime();
5831
- this.startStreamingHeartbeat(mode === 'continuous' ? 'Streaming' : 'Streaming response');
5832
- const completionDetector = getTaskCompletionDetector();
5833
- completionDetector.reset();
5834
- this.flowOrchestrator.start(initialRequest);
5835
- this.updateStatusMessage('Orchestrating request...');
5836
- if (mode === 'continuous') {
5837
- display.showSystemMessage('Continuous mode active. Ctrl+C to stop.');
5838
- }
5839
- else {
5840
- display.showSystemMessage('Flow orchestrator engaged; running until the request is satisfied.');
5924
+ let responseText = '';
5925
+ let orchestratorResult = null;
5926
+ const orchestrate = options?.orchestrate ?? true;
5927
+ const orchestratorPassLimit = options?.maxPasses ?? 10;
5928
+ if (orchestrate && !this.orchestrationNoticeShown) {
5929
+ display.showSystemMessage('⚡ Orchestrating every prompt until completion. Press Ctrl+C to stop a run early.');
5930
+ this.orchestrationNoticeShown = true;
5841
5931
  }
5842
- const informationalGuardrail = this.flowOrchestrator.isInformationalRequest(initialRequest)
5843
- ? 'This is an informational request. Keep actions lightweight: avoid running full test/build/lint suites unless explicitly required to answer the question or to confirm a change you made. Prefer quick reads/searches and summarize findings instead of long validations.'
5844
- : '';
5845
- const applyInformationalGuardrail = (prompt) => {
5846
- if (!informationalGuardrail) {
5847
- return prompt;
5848
- }
5849
- return prompt.includes(informationalGuardrail)
5850
- ? prompt
5851
- : `${prompt}\n\n${informationalGuardrail}`;
5852
- };
5853
- let currentPrompt = applyInformationalGuardrail(initialRequest);
5854
- if (this.isSelfImprovementRequest(initialRequest)) {
5855
- currentPrompt = applyInformationalGuardrail(`${initialRequest}
5856
-
5857
- IMPORTANT: You have full git access. After making improvements:
5858
- 1. Use bash to run: git status (see changes)
5859
- 2. Use bash to run: git add -A (stage changes)
5860
- 3. Use bash to run: git commit -m "descriptive message" (commit)
5861
- 4. Use bash to run: git push (when milestone reached)
5862
-
5863
- Commit frequently with descriptive messages. Push when ready.
5864
- When truly finished with ALL tasks, explicitly state "TASK_FULLY_COMPLETE".`);
5865
- }
5866
- let iteration = 0;
5867
- let lastResponseText = '';
5868
- let lastToolsUsed = [];
5869
- let result = null;
5870
5932
  try {
5871
- while (true) {
5872
- iteration++;
5873
- this.toolsUsedThisRun = [];
5874
- this.updateStatusMessage('Orchestrating request...');
5875
- try {
5876
- display.showThinking('Responding...');
5877
- this.refreshStatusLine(true);
5878
- const response = await agent.send(currentPrompt, true);
5879
- lastResponseText = response ?? '';
5880
- this.finishStreamingFormatter(undefined, { refreshPrompt: false, mode: 'complete' });
5881
- await this.awaitPendingCleanup();
5882
- this.captureHistorySnapshot();
5883
- this.autosaveIfEnabled();
5884
- const elapsedMs = Date.now() - overallStartTime;
5885
- this.alphaZeroMetrics.recordMessage(elapsedMs);
5886
- if (!response?.trim()) {
5887
- display.showWarning('Model returned an empty response. Continuing orchestration...');
5888
- currentPrompt = applyInformationalGuardrail(`${initialRequest}
5889
-
5890
- The previous reply was empty. Resume the task now: take the next action, call the necessary tools, and report progress.`);
5891
- continue;
5892
- }
5893
- const toolsUsed = this.getExecutedTools(response);
5894
- lastToolsUsed = toolsUsed;
5895
- toolsUsed.forEach(tool => completionDetector.recordToolCall(tool, true, true));
5896
- const completionAnalysis = completionDetector.analyzeCompletion(response, toolsUsed);
5897
- display.showSystemMessage(`📈 Completion confidence: ${(completionAnalysis.confidence * 100).toFixed(0)}%`);
5898
- const decision = this.flowOrchestrator.decide({
5899
- iteration,
5900
- response,
5901
- toolsUsed,
5902
- completionAnalysis,
5903
- verificationConfirmed: this.flowOrchestrator.isVerificationPending()
5904
- ? completionDetector.isVerificationConfirmed(response)
5905
- : false,
5906
- });
5907
- if (decision.type === 'stop') {
5908
- display.showSystemMessage(decision.message);
5909
- break;
5910
- }
5911
- if (decision.type === 'stagnation-stop') {
5912
- display.showWarning(decision.message);
5933
+ // Start streaming - no header needed, the input area already provides context
5934
+ this.startStreamingHeartbeat('Streaming response');
5935
+ if (orchestrate) {
5936
+ const orchestrator = new AgentOrchestrator(agent);
5937
+ orchestratorResult = await orchestrator.runToCompletion(request, {
5938
+ streaming: true,
5939
+ maxPasses: orchestratorPassLimit,
5940
+ maxStagnantPasses: 3,
5941
+ verificationMode: 'auto',
5942
+ enforceActions: true,
5943
+ });
5944
+ const MAX_CONTINUATIONS = 2;
5945
+ let continuationRuns = 0;
5946
+ while (orchestratorResult && continuationRuns < MAX_CONTINUATIONS) {
5947
+ const continuationDecision = this.shouldContinueOrchestrating(request, orchestratorResult);
5948
+ if (!continuationDecision.shouldContinue) {
5913
5949
  break;
5914
5950
  }
5915
- if (decision.type === 'execute-plan') {
5916
- display.showSystemMessage(decision.message);
5917
- currentPrompt = applyInformationalGuardrail(decision.prompt);
5918
- await new Promise(resolve => setTimeout(resolve, 500));
5919
- continue;
5920
- }
5921
- if (decision.type === 'verify') {
5922
- display.showSystemMessage(decision.message);
5923
- currentPrompt = applyInformationalGuardrail(decision.prompt);
5924
- await new Promise(resolve => setTimeout(resolve, 500));
5925
- continue;
5926
- }
5927
- if (decision.type === 'continue') {
5928
- if (decision.message) {
5929
- display.showSystemMessage(decision.message);
5930
- }
5931
- currentPrompt = applyInformationalGuardrail(decision.prompt);
5932
- }
5933
- await new Promise(resolve => setTimeout(resolve, 500));
5951
+ const reasonMessage = this.describeContinuationReason(continuationDecision.reason);
5952
+ display.showSystemMessage(`🔁 ${reasonMessage}`);
5953
+ const continuationPrompt = this.buildForcedContinuationPrompt(request, orchestratorResult, continuationDecision.reason ?? undefined);
5954
+ const continuationResult = await orchestrator.runToCompletion(continuationPrompt, {
5955
+ streaming: true,
5956
+ maxPasses: Math.max(orchestratorPassLimit, 12),
5957
+ maxStagnantPasses: 3,
5958
+ verificationMode: 'auto',
5959
+ enforceActions: true,
5960
+ });
5961
+ orchestratorResult = {
5962
+ finalResponse: continuationResult.finalResponse,
5963
+ passes: [...orchestratorResult.passes, ...continuationResult.passes],
5964
+ exitReason: continuationResult.exitReason,
5965
+ };
5966
+ continuationRuns++;
5934
5967
  }
5935
- catch (error) {
5936
- display.stopThinking(false);
5937
- if (this.isContextOverflowError(error)) {
5938
- display.showSystemMessage(`⚡ Context overflow handled. Continuing with reduced context...`);
5939
- continue;
5940
- }
5941
- const handled = this.handleProviderError(error, () => this.runFlowControlledTask(initialRequest, options));
5942
- if (!handled) {
5943
- display.showError(error instanceof Error ? error.message : String(error), error);
5944
- break;
5968
+ responseText = orchestratorResult.finalResponse;
5969
+ }
5970
+ else {
5971
+ responseText = await agent.send(request, true);
5972
+ }
5973
+ this.finishStreamingFormatter(undefined, { refreshPrompt: false, mode: 'complete' });
5974
+ await this.awaitPendingCleanup();
5975
+ this.captureHistorySnapshot();
5976
+ this.autosaveIfEnabled();
5977
+ // Track metrics with Alpha Zero 2
5978
+ const elapsedMs = Date.now() - requestStartTime;
5979
+ this.alphaZeroMetrics.recordMessage(elapsedMs);
5980
+ if (!responseText?.trim()) {
5981
+ display.showWarning('The provider returned an empty response. Check your API key/provider selection or retry the prompt.');
5982
+ }
5983
+ // AlphaZero: Extract and track tool calls from response
5984
+ const toolsUsed = orchestratorResult
5985
+ ? orchestratorResult.passes.flatMap(pass => pass.toolsUsed)
5986
+ : this.getExecutedTools(responseText);
5987
+ this.currentToolCalls = toolsUsed.map(name => ({
5988
+ name,
5989
+ arguments: {},
5990
+ success: true, // Assume success if we got here
5991
+ duration: 0,
5992
+ }));
5993
+ if (orchestratorResult && orchestratorResult.exitReason !== 'complete') {
5994
+ const exitDetail = (() => {
5995
+ switch (orchestratorResult?.exitReason) {
5996
+ case 'max-passes':
5997
+ return 'Reached orchestrator pass limit; showing last response.';
5998
+ case 'empty-response':
5999
+ return 'Received empty replies while orchestrating; showing last response.';
6000
+ case 'stalled':
6001
+ return 'Orchestrator detected stagnation; showing last response.';
6002
+ default:
6003
+ return null;
5945
6004
  }
6005
+ })();
6006
+ if (exitDetail) {
6007
+ display.showSystemMessage(`⚠️ ${exitDetail}`);
5946
6008
  }
5947
6009
  }
5948
- if (!this.responseRendered && lastResponseText.trim()) {
5949
- const finalText = lastResponseText.trim();
6010
+ // AlphaZero: Check for failure in response
6011
+ const failure = detectFailure(responseText, {
6012
+ toolCalls: this.currentToolCalls,
6013
+ userMessage: request,
6014
+ });
6015
+ if (failure) {
6016
+ this.lastFailure = failure;
6017
+ // Check if we have a recovery strategy
6018
+ const strategy = findRecoveryStrategy(failure);
6019
+ if (strategy) {
6020
+ display.showSystemMessage(`🔄 Found recovery strategy for this type of issue (success rate: ${Math.round(strategy.successRate * 100)}%)`);
6021
+ }
6022
+ }
6023
+ else {
6024
+ // Success - record the tool pattern for this task type
6025
+ if (this.currentToolCalls.length > 0) {
6026
+ const toolPattern = {
6027
+ taskType: this.currentTaskType,
6028
+ toolSequence: this.currentToolCalls.map(t => t.name),
6029
+ successRate: 1.0,
6030
+ avgDuration: elapsedMs,
6031
+ occurrences: 1,
6032
+ };
6033
+ addToolPattern(this.currentTaskType, toolPattern);
6034
+ }
6035
+ // Clear action history on success
6036
+ clearActionHistory();
6037
+ this.lastFailure = null;
6038
+ }
6039
+ }
6040
+ catch (error) {
6041
+ const handled = this.handleProviderError(error, () => this.processRequest(request, options));
6042
+ if (!handled) {
6043
+ // Pass full error object for enhanced formatting with stack trace
6044
+ display.showError(error instanceof Error ? error.message : String(error), error);
6045
+ }
6046
+ }
6047
+ finally {
6048
+ // Fallback: if no assistant message was rendered (e.g., streaming hiccup), show the full response
6049
+ if (!this.responseRendered && responseText.trim()) {
6050
+ const finalText = responseText.trim();
5950
6051
  display.showAssistantMessage(finalText, { isFinal: true });
5951
6052
  this.ui.controller.recordAssistantResponse(finalText, {
5952
6053
  source: 'final',
5953
6054
  });
5954
6055
  this.responseRendered = true;
5955
6056
  }
5956
- result = {
5957
- finalResponse: lastResponseText,
5958
- toolsUsed: lastToolsUsed,
5959
- elapsedMs: Date.now() - overallStartTime,
5960
- };
5961
- }
5962
- finally {
5963
6057
  this.finishStreamingFormatter(undefined, { refreshPrompt: false, mode: 'complete' });
5964
- const totalElapsed = Date.now() - overallStartTime;
5965
- const minutes = Math.floor(totalElapsed / 60000);
5966
- const seconds = Math.floor((totalElapsed % 60000) / 1000);
5967
- const completionLabel = mode === 'continuous'
5968
- ? `\n🏁 Continuous execution completed in ${minutes}m ${seconds}s total`
5969
- : `\n🏁 Flow orchestration completed in ${minutes}m ${seconds}s total`;
5970
- display.showSystemMessage(completionLabel);
5971
- resetTaskCompletionDetector();
5972
6058
  display.stopThinking(false);
5973
6059
  this.uiUpdates.setMode('processing');
5974
6060
  this.stopStreamingHeartbeat('complete', { quiet: true });
@@ -5981,60 +6067,20 @@ The previous reply was empty. Resume the task now: take the next action, call th
5981
6067
  this.updateStatusMessage(null);
5982
6068
  this.toolsUsedThisRun = [];
5983
6069
  queueMicrotask(() => this.uiUpdates.setMode('idle'));
6070
+ // CRITICAL: Ensure readline prompt is active for user input
6071
+ // Erosolar-CLI style: New prompt naturally appears at bottom
5984
6072
  this.ensureReadlineReady();
5985
6073
  this.scheduleQueueProcessing();
5986
6074
  this.maybeProcessPromptInbox();
5987
6075
  this.refreshQueueIndicators();
5988
6076
  }
5989
- return result;
5990
- }
5991
- handleFlowRunOutcome(request, result) {
5992
- this.currentToolCalls = result.toolsUsed.map((name) => ({
5993
- name,
5994
- arguments: {},
5995
- success: true,
5996
- duration: 0,
5997
- }));
5998
- const failure = detectFailure(result.finalResponse, {
5999
- toolCalls: this.currentToolCalls,
6000
- userMessage: request,
6001
- });
6002
- if (failure) {
6003
- this.lastFailure = failure;
6004
- const strategy = findRecoveryStrategy(failure);
6005
- if (strategy) {
6006
- display.showSystemMessage(`🔄 Found recovery strategy for this type of issue (success rate: ${Math.round(strategy.successRate * 100)}%)`);
6007
- }
6008
- return;
6009
- }
6010
- if (this.currentToolCalls.length > 0) {
6011
- const toolPattern = {
6012
- taskType: this.currentTaskType,
6013
- toolSequence: this.currentToolCalls.map((t) => t.name),
6014
- successRate: 1.0,
6015
- avgDuration: result.elapsedMs,
6016
- occurrences: 1,
6017
- };
6018
- addToolPattern(this.currentTaskType, toolPattern);
6019
- }
6020
- clearActionHistory();
6021
- this.lastFailure = null;
6022
- }
6023
- async processRequest(request) {
6024
- const result = await this.runFlowControlledTask(request, {
6025
- mode: 'standard',
6026
- });
6027
- if (!result) {
6028
- return;
6029
- }
6030
- this.handleFlowRunOutcome(request, result);
6031
6077
  }
6032
6078
  /**
6033
6079
  * Process a continuous/infinite loop request.
6034
6080
  * Runs the agent in a loop until:
6035
6081
  * 1. The agent indicates completion (verified by AI confirmation)
6036
6082
  * 2. User interrupts (Ctrl+C)
6037
- * 3. The orchestrator halts due to stagnation/verification exhaustion
6083
+ * 3. Maximum iterations reached (safety limit)
6038
6084
  *
6039
6085
  * Uses intelligent task completion detection with AI verification
6040
6086
  * to ensure tasks are truly complete before stopping.
@@ -6042,13 +6088,29 @@ The previous reply was empty. Resume the task now: take the next action, call th
6042
6088
  * Context is automatically managed - overflow errors trigger auto-recovery.
6043
6089
  */
6044
6090
  async processContinuousRequest(initialRequest) {
6045
- const result = await this.runFlowControlledTask(initialRequest, {
6046
- mode: 'continuous',
6047
- });
6048
- if (!result) {
6091
+ const MAX_PASSES = 100; // Safety limit to prevent truly infinite loops
6092
+ if (this.isProcessing) {
6093
+ this.enqueueFollowUpAction({ type: 'continuous', text: initialRequest });
6049
6094
  return;
6050
6095
  }
6051
- this.handleFlowRunOutcome(initialRequest, result);
6096
+ if (!this.agent && !this.rebuildAgent()) {
6097
+ display.showWarning('Configure an API key via /secrets before sending requests.');
6098
+ return;
6099
+ }
6100
+ display.showSystemMessage(`Continuous mode active. Ctrl+C to stop.`);
6101
+ const preparedRequest = this.isSelfImprovementRequest(initialRequest)
6102
+ ? `${initialRequest}
6103
+
6104
+ IMPORTANT: You have full git access. After making improvements:
6105
+ 1. Use bash to run: git status (see changes)
6106
+ 2. Use bash to run: git add -A (stage changes)
6107
+ 3. Use bash to run: git commit -m "descriptive message" (commit)
6108
+ 4. Use bash to run: git push (when milestone reached)
6109
+
6110
+ Commit frequently with descriptive messages. Push when ready.
6111
+ When truly finished with ALL tasks, explicitly state "TASK_FULLY_COMPLETE".`
6112
+ : initialRequest;
6113
+ await this.processRequest(preparedRequest, { orchestrate: true, maxPasses: MAX_PASSES });
6052
6114
  }
6053
6115
  /**
6054
6116
  * Resolve executed tools for the current turn. Prefer the actual tool
@@ -6070,6 +6132,129 @@ The previous reply was empty. Resume the task now: take the next action, call th
6070
6132
  }
6071
6133
  return this.extractToolsFromResponse(responseText);
6072
6134
  }
6135
+ /**
6136
+ * Detect plan-only responses that narrate intent without executing actions.
6137
+ */
6138
+ isPlanOnlyResponse(response) {
6139
+ const normalized = response.trim().toLowerCase();
6140
+ if (!normalized) {
6141
+ return false;
6142
+ }
6143
+ // If the assistant is clearly declaring completion, don't treat it as plan-only
6144
+ const completionGuards = [
6145
+ /\bnothing\s+(left|else)\s+(to\s+do|pending)\b/i,
6146
+ /\b(already|now)\s+(clean|complete|done)\b/i,
6147
+ /\b(no\s+(junk|issues?|changes?)\s+found)\b/i,
6148
+ ];
6149
+ if (completionGuards.some((pattern) => pattern.test(response))) {
6150
+ return false;
6151
+ }
6152
+ const planIndicators = [
6153
+ /\bplan\b/i,
6154
+ /\bapproach\b/i,
6155
+ /\bsteps?:\b/i,
6156
+ /\bstep\s+1\b/i,
6157
+ /\bstart by\b/i,
6158
+ /\bfirst[, ]/i,
6159
+ /\bthen\b/i,
6160
+ /\bnext\b/i,
6161
+ /\bi['’]?\s*will\b/i,
6162
+ /\bi['’]?\s*ll\b/i,
6163
+ /\bi['’]?\s*can\b.{0,40}\bthen\b/i,
6164
+ /\bi['’]?\s*(?:will|ll)\s+begin\b/i,
6165
+ ];
6166
+ return planIndicators.some((pattern) => pattern.test(response));
6167
+ }
6168
+ /**
6169
+ * Check if a response contains indicators that work is actually incomplete,
6170
+ * even if it also contains TASK_FULLY_COMPLETE marker.
6171
+ * This catches contradictory responses where the AI says "done" but also "not integrated yet".
6172
+ */
6173
+ responseIndicatesIncompleteWork(response) {
6174
+ // Patterns that indicate work isn't actually complete
6175
+ // Organized by category for maintainability
6176
+ const incompletePatterns = [
6177
+ // === INTEGRATION/DEPLOYMENT STATE ===
6178
+ // "hasn't been integrated/implemented/connected yet"
6179
+ /hasn'?t\s+been\s+(integrated|implemented|connected|deployed|added|completed|tested|verified)\s*(yet|still)?/i,
6180
+ // "not yet integrated/implemented" or "not integrated"
6181
+ /not\s+(yet\s+)?(integrated|implemented|connected|deployed|functional|working|complete|tested|verified)/i,
6182
+ // "ready for integration" = NOT integrated
6183
+ /ready\s+(for|to\s+be)\s+(integration|integrated|connected|deployed|testing|review)/i,
6184
+ // "needs to be integrated"
6185
+ /needs?\s+to\s+be\s+(integrated|connected|deployed|added|hooked|wired|tested|reviewed|merged)/i,
6186
+ // Passive voice: "was not performed/completed"
6187
+ /was\s+not\s+(performed|completed|implemented|deployed|integrated|tested)/i,
6188
+ // "the [X] service hasn't been"
6189
+ /the\s+\w+\s+(service|module|component|feature)\s+hasn'?t\s+been/i,
6190
+ // === PARTIAL/INCOMPLETE STATE ===
6191
+ // "still stores/uses/has" (current bad state persists)
6192
+ /still\s+(stores?|uses?|has|contains?|needs?|requires?|missing|lacks?|broken)/i,
6193
+ // Partial completion: "partially", "mostly", "almost"
6194
+ /\b(partially|mostly|almost|nearly|not\s+fully)\s+(complete|done|finished|implemented|working)/i,
6195
+ // Explicit partial: "part of", "some of", "half of"
6196
+ /\b(only\s+)?(part|some|half|portion)\s+of\s+(the\s+)?(task|work|feature|implementation)/i,
6197
+ // === QUALIFIER WORDS (uncertain completion) ===
6198
+ // "should be complete", "appears complete", "theoretically"
6199
+ /\b(should|might|may|could|appears?\s+to)\s+be\s+(complete|done|working|functional)/i,
6200
+ /\btheoretically\s+(complete|done|working|functional)/i,
6201
+ // "assuming", "if everything works"
6202
+ /\b(assuming|provided|if)\s+(everything|it|this|that)\s+(works?|is\s+correct)/i,
6203
+ // === SELF-CONTRADICTION PHRASES ===
6204
+ // "done but...", "complete except...", "finished however..."
6205
+ /\b(done|complete|finished)\s+(but|except|however|although|though)/i,
6206
+ // "however" followed by incomplete indicator
6207
+ /however[,\s].{0,50}?(hasn'?t|not\s+yet|still\s+needs?|pending|remains?|missing|broken|failing)/i,
6208
+ // "but" followed by negative state
6209
+ /\bbut\s+.{0,30}?(not|hasn'?t|won'?t|can'?t|doesn'?t|isn'?t|wasn'?t)/i,
6210
+ // === FUTURE TENSE / DEFERRED WORK ===
6211
+ // "will need to", "will require"
6212
+ /will\s+(need\s+to|require|have\s+to)\s+(integrate|connect|deploy|complete|implement|test|fix)/i,
6213
+ // Deferred: "left as", "deferred", "postponed", "out of scope"
6214
+ /\b(left\s+as|deferred|postponed|out\s+of\s+scope|for\s+later|in\s+a\s+future)/i,
6215
+ // Time-dependent: "after restart", "takes effect after", "once you"
6216
+ /\b(after\s+(restart|reboot|redeploy)|takes?\s+effect\s+after|once\s+you)/i,
6217
+ // === REMAINING WORK INDICATORS ===
6218
+ // "remaining tasks", "outstanding items"
6219
+ /\b(remaining|outstanding|pending|leftover)\s+(tasks?|items?|work|issues?|steps?)/i,
6220
+ // "X more to do", "still have to"
6221
+ /\b(more\s+to\s+do|still\s+have\s+to|yet\s+to\s+be\s+done)/i,
6222
+ // Explicit blockers
6223
+ /\b(blocker|blocked\s+by|waiting\s+(for|on)|depends?\s+on)/i,
6224
+ // === ERROR/FAILURE STATE ===
6225
+ // "failing tests", "build errors"
6226
+ /\b(failing|broken|erroring)\s+(tests?|builds?|checks?|validations?)/i,
6227
+ // "tests? (are )?(still )?failing"
6228
+ /\btests?\s+(are\s+)?(still\s+)?failing/i,
6229
+ // "errors? to (address|fix)"
6230
+ /\b(errors?|warnings?|issues?)\s+to\s+(address|fix|resolve)/i,
6231
+ // "doesn't work", "isn't working", "not working"
6232
+ /\b(doesn'?t|isn'?t|not)\s+(work|working|functional|functioning)/i,
6233
+ // === MANUAL STEPS REQUIRED ===
6234
+ // "you'll need to", "manually run", "requires user"
6235
+ /\b(you('ll|\s+will)\s+need\s+to|manually\s+(run|configure|set|update)|requires?\s+user)/i,
6236
+ // "run this command", "execute the following"
6237
+ /\b(run\s+this|execute\s+the\s+following|apply\s+the\s+migration)/i,
6238
+ // === TODO/FIXME IN PROSE ===
6239
+ // TODO or FIXME mentioned as remaining work (not in code blocks)
6240
+ /\b(todo|fixme|hack|xxx):\s/i,
6241
+ // "need to add", "should implement"
6242
+ /\b(need\s+to|should|must)\s+(add|implement|create|write|build|fix)\b/i,
6243
+ // === SCOPE LIMITATIONS ===
6244
+ // "didn't have time", "ran out of time"
6245
+ /\b(didn'?t|did\s+not)\s+have\s+(time|chance|opportunity)/i,
6246
+ // "beyond scope", "outside scope"
6247
+ /\b(beyond|outside)\s+(the\s+)?scope/i,
6248
+ // "for now" (temporary state)
6249
+ /\b(for\s+now|at\s+this\s+point|currently)\s*.{0,20}?(not|without|lacks?|missing)/i,
6250
+ ];
6251
+ for (const pattern of incompletePatterns) {
6252
+ if (pattern.test(response)) {
6253
+ return true;
6254
+ }
6255
+ }
6256
+ return false;
6257
+ }
6073
6258
  /**
6074
6259
  * Extract tool names from a response by looking for tool call patterns
6075
6260
  */
@@ -6562,19 +6747,12 @@ Return ONLY JSON array:
6562
6747
  const activity = normalized ? `Working: ${normalized}` : 'Working';
6563
6748
  this.renderer?.setActivity(activity);
6564
6749
  },
6565
- onBeforeFirstToolCall: (toolNames, hasNarration) => {
6750
+ onBeforeFirstToolCall: (toolNames) => {
6566
6751
  const primaryTool = toolNames[0];
6567
6752
  if (primaryTool) {
6568
6753
  this.renderer?.setActivity(`Running ${primaryTool}`);
6569
6754
  }
6570
- if (hasNarration) {
6571
- return undefined;
6572
- }
6573
- const toolList = toolNames.length ? toolNames.join(', ') : 'tools';
6574
- const ack = `💭 Plan before tools:\n- Goal: ${this.lastUserQuery || 'address request'}\n- Tools: ${toolList}\n- Executing now.`;
6575
- display.showNarrative(ack);
6576
- this.ui.controller.recordAssistantThought(ack);
6577
- return ack;
6755
+ return undefined;
6578
6756
  },
6579
6757
  onStreamChunk: (chunk, type) => {
6580
6758
  this.handleStreamChunk(chunk, type ?? 'content');
@@ -6722,14 +6900,10 @@ Return ONLY JSON array:
6722
6900
  activity = `Reading ${path}`;
6723
6901
  }
6724
6902
  this.renderer?.setActivity(activity);
6725
- // Estimate tokens for tool call (~50 tokens per call)
6726
- this.streamingTokenCount += 50;
6727
- this.renderer?.updateStreamingTokens(this.streamingTokenCount);
6903
+ // Token count updated from real provider usage - do not estimate
6728
6904
  }
6729
6905
  else {
6730
- // Tool finished - estimate result tokens (~100 per result)
6731
- this.streamingTokenCount += 100;
6732
- this.renderer?.updateStreamingTokens(this.streamingTokenCount);
6906
+ // Tool finished - token count updated from real provider usage
6733
6907
  // Reset to thinking state while model generates next response
6734
6908
  this.renderer?.setActivity('Thinking');
6735
6909
  }
@@ -6738,6 +6912,14 @@ Return ONLY JSON array:
6738
6912
  this.lastAssistantResponse = response;
6739
6913
  void this.runAutoQualityChecks('verification', response, context);
6740
6914
  },
6915
+ // Real token usage from provider during streaming
6916
+ onUsage: (usage) => {
6917
+ const totalTokens = this.totalTokens(usage);
6918
+ if (totalTokens !== null) {
6919
+ this.streamingTokenCount = totalTokens;
6920
+ this.renderer?.updateStreamingTokens(this.streamingTokenCount);
6921
+ }
6922
+ },
6741
6923
  // Retry notification for transient errors
6742
6924
  onRetrying: (attempt, maxAttempts, error) => {
6743
6925
  const shortError = error.message.slice(0, 100);