Commit 2c582a7
committed
fix(orchestrator): comprehensive workflow engine overhaul - 8 major fixes
Complete redesign of agent orchestrator workflow engine to fix critical bugs
in todo tracking, message alternation, and continuation guidance. Evolved from
rigid "force tools" approach to intelligent context-aware orchestration that
follows the orchestrator.txt flow diagram correctly.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SUMMARY OF FIXES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
**Fix #1: Todo Workflow Infinite Loops & Update-Before-Create**
Agent tried to update todos before creating list, causing crashes and loops.
Graduated interventions gave contradictory instructions.
Solution:
- Added CRITICAL ERROR TO AVOID section in todo_operations tool
- Visual step-by-step workflow added to tool description
- Rewrote all 3 graduated intervention levels with consistent guidance
- Implemented proper Continue Flag pattern matching orchestrator.txt
- Added shouldContinueAfterChecks flag for correct workflow flow
Result: No more update-before-create crashes, workflow matches design exactly
**Fix #2: Response Loops (Agent Repeating Same Text)**
Agent stuck repeating same response infinitely when todos incomplete.
pendingAutoContinueMessage was set but never injected.
Solution:
- Inject pendingAutoContinueMessage at iteration start
- Call injectAutoContinueIfTodosIncomplete() when no tools + active todos
- Graduated intervention (Level 1 → 2 → 3) now works as designed
- Remove last assistant message to prevent loops
Result: No more infinite response loops, graduated intervention working
**Fix #3: Tool Result Infinite Loop (read_tool_result Stuck)**
Agent stuck seeing SAME tool result chunk repeatedly for 15+ iterations.
TOOL_RESULT_CHUNK messages preserved across iterations incorrectly.
Solution:
- Removed preservation logic for TOOL_RESULT_CHUNK messages
- Chunks now appear once when tool executes
- Agent must call read_tool_result to get more chunks
- Proper pagination flow restored
Result: No more chunk re-injection loops, clean pagination behavior
**Fix #4: Message Alternation Violations**
Multiple consecutive assistant messages broke Claude API compatibility.
Evolved through 3 iterations: Rigid → Binary → Flexible → Context-Aware
Final Solution - Todo-Aware Continuation Guidance:
- 4 guidance variants: (has todos YES/NO) × (tools used YES/NO)
- With todos + tools: "MANDATORY TODO WORKFLOW: mark → work → complete"
- With todos + no tools: "You have incomplete todos - MUST follow workflow"
- Without todos + tools: "Need more data? → tools. Have enough? → respond"
- Without todos + no tools: "Already answered? Use tools for follow-up"
Result: Fixes consecutive messages, allows flexibility, enforces discipline
**Fix #5: Planning Loop False Positives**
Planning loop detector flagged normal workflow (mark todo → work → complete)
as infinite loop because it saw consecutive todo_operations calls.
Solution:
- Added isTodoCompletionCall() helper function
- Marking todos complete now counts as progress
- Only flag as loop if NO work tools AND NO todo completions
Result: Normal workflow allowed, actual loops still detected
**Fix #6: Stale Todo List (Workflow Stopped Early)**
Workflow stopped when todos incomplete because orchestrator saw stale "all
complete" state. currentTodoList only updated after tool execution.
Solution:
- Read fresh todo list from MCP BEFORE every workflow check
- Only if currentTodoList.count > 0 (known active list exists)
- TodoReminderInjector: Clarified workflow guidance wording
- Makes explicit: mark in-progress → DO THE WORK → mark completed
Result: Workflows continue correctly, no premature stops, fresh state accurate
**Fix #7: Duplicate Tool Cards in Streaming Mode**
Tool cards appeared twice in UI - once from streaming, once from main loop.
Solution:
- Skip tool message creation in main loop when streaming active
- Check streamContinuation - if present, streaming created messages
- Non-streaming unchanged
Result: Clean UI, no duplicate cards
**Fix #8: Web Research Error Card Clutter**
Red error cards for expected situations (empty pages, no results).
VectorRAGError helpful messages wrapped with confusing text.
Solution:
- Pass through VectorRAGError messages without wrapping
- Handle partial failures gracefully (some sources succeed = green card)
- Only show error if ALL sources fail
Result: Clean UI, helpful guidance preserved
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
TESTING RESULTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Simple workflow (3 stories): All tracked, completed, brief summary
✅ Complex workflow (3 research tasks): All tracked, completed, brief summary
✅ Fresh todo reads detect incomplete todos correctly
✅ Agent doesn't repeat work in final summary
✅ No more response loops or chunk re-injection
✅ Planning loop detector allows normal workflow
✅ Context-aware continuation guidance works
✅ Streaming mode: no duplicate tool cards
✅ Web research: clean error handling
✅ Build: PASS (all commits)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DOCUMENTATION
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Added comprehensive documentation:
- project-docs/AGENT_ORCHESTRATOR.md (complete architecture)
* Complete workflow flow diagram (Mermaid)
* Detailed 8-step decision tree
* Fresh todo state read documentation
* Continuation priority table
* All fixes documented with root causes
* Known Issues updated with resolutions
- ai-assisted/2026-01-04/workflow-alternation-fix/
* CONTINUATION_PROMPT.md (session handoff)
* AGENT_PLAN.md (remaining work breakdown)
- .github/copilot-instructions.md
* Added isBackground=false requirement (CRITICAL)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
FILES MODIFIED
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Primary:
- Sources/APIFramework/AgentOrchestrator.swift
* Fresh todo reads before workflow checks
* Context-aware continuation guidance system
* Graduated intervention injection fixed
* Planning loop detection improvements
* Duplicate tool card fix for streaming
- Sources/MCPFramework/TodoReminderInjector.swift
* Todo-aware workflow guidance (4 variants)
* Clear final message guidance when all tasks complete
- Sources/MCPFramework/Tools/TodoOperationsTool.swift
* CREATE FIRST requirement documentation
* Visual step-by-step workflow added
- Sources/ConfigurationSystem/SimpleSystemPromptManager.swift
* Todo workflow discipline documentation
Supporting:
- Sources/MCPFramework/Tools/WebResearchTool.swift
* VectorRAGError pass-through without wrapping
- Sources/ConversationEngine/WebResearchService.swift
* Partial failure handling (some sources succeed)
Documentation:
- project-docs/AGENT_ORCHESTRATOR.md (new + updated)
- ai-assisted/2026-01-04/workflow-alternation-fix/* (new)
- .github/copilot-instructions.md (updated)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ARCHITECTURAL IMPROVEMENTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Context-Aware Guidance System:
- 4 guidance variants adapt to workflow state
- Based on: (has incomplete todos?) × (tools called?)
- Each variant provides specific, actionable instructions
- Prevents workflow violations through clear communication
Fresh State Reads:
- Todo list read fresh from MCP before every workflow check
- Prevents stale cache bugs that caused premature stops
- Only reads when active todo list exists (performance optimization)
Graduated Intervention Pressure:
- Level 1: Polite reminder about incomplete todos
- Level 2: Warning about loop behavior
- Level 3: Final warning before failure
- Escalates pressure if agent keeps ignoring todos
Proper Continue Flag Pattern:
- Matches orchestrator.txt flow diagram exactly
- Can be set by: tool execution, incomplete todos, workflow mode
- Priority ordering ensures correct continuation behavior
Unified Todo Workflow Discipline:
- All guidance sources enforce same workflow
- CREATE list first, THEN mark in-progress
- Mark in-progress → DO THE WORK → mark completed
- Never skip status updates
Code Quality:
- Eliminated ~100 lines of redundant code
- Cleaner separation: streaming vs non-streaming paths
- Better error handling and logging
Result: Intelligent orchestrator that adapts guidance based on workflow
context while enforcing todo discipline and preventing loops.1 parent 8a54afe commit 2c582a7
10 files changed
Lines changed: 1571 additions & 680 deletions
File tree
- .github
- Sources
- APIFramework
- ConfigurationSystem
- MCPFramework
- Tools
- UserInterface/Web
- project-docs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
158 | 185 | | |
159 | 186 | | |
160 | 187 | | |
| |||
Large diffs are not rendered by default.
Lines changed: 23 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
237 | 255 | | |
238 | 256 | | |
239 | 257 | | |
| |||
Lines changed: 26 additions & 53 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
520 | 520 | | |
521 | 521 | | |
522 | 522 | | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
576 | 549 | | |
577 | 550 | | |
578 | 551 | | |
| |||
Lines changed: 195 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
0 commit comments