🎺
Movement 2 of 4 Chapter 15 of 42 Ready to Read

The Improvement Cycle - Auto-Correction

Movimento 15 di 42

Chapter 15: Self-Healing System – Automatic Resilience

Our system had become an excellent student. Thanks to WorkspaceMemory, it learned from every success and failure, accumulating invaluable strategic knowledge. But there was still a missing link in the feedback cycle: action.

The system was like a brilliant consultant who wrote perfect reports on what was wrong, but then left them on a desk to gather dust. It detected problems, memorized lessons, but didnt act autonomously to course-correct.

To realize our vision of a truly autonomous system, we had to implement Pillar #13 (Automatic Course-Correction). We had to give the system not only the ability to know what to do, but also the power to do it.

The Architectural Decision: A Proactive "Nervous System"

We designed our self-correction system not as a separate process, but as an automatic "reflex" integrated into the heart of the Executor. The idea was that, at regular intervals and after significant events (like task completion), the system should pause for a moment to "reflect" and, if necessary, correct its own strategy.

We created a new component, the GoalValidator, whose purpose wasnt just to validate quality, but to compare the current project state with final objectives.

Reference code: backend/ai_quality_assurance/goal_validator.py

Self-Correction Flow:

System Architecture

graph TD A[Trigger Event: Task Completed or Periodic Timer] B{GoalValidator activates} C[Gap Analysis: Compare Current State vs. Objectives] D[Continue Normal Operations] E{Memory Consultation} F{Generate Corrective Plan} G[Create Corrective Tasks] H{Added to Executor Queue} A --> B B --> C C --> D C --> E E --> F F --> G G --> H H --> D C -.->|No Relevant Gap| D C -.->|Critical Gap Detected| E E -.->|Search for Related Failure Lessons| F F -.->|AI defines new tasks| G G -.->|CRITICAL Priority| H

System Architecture

"War Story": The Validator Who Cried "Wolf!"

Our first implementation of the GoalValidator was too sensitive.

Disaster Logbook (July 28th):

CRITICAL goal validation failures: 4 issues
⚠️ GOAL SHORTFALL: 0/50.0 contacts for contacts (100.0% gap, missing 50.0);
INFO: Creating corrective task: "URGENT: Collect 50.0 missing contacts"
... (5 minutes later)
CRITICAL goal validation failures: 4 issues
⚠️ GOAL SHORTFALL: 0/50.0 contacts for contacts (100.0% gap, missing 50.0)
INFO: Creating corrective task: "URGENT: Collect 50.0 missing contacts"

The system had entered a panic loop. It detected a gap, created a corrective task, but before the Executor could even assign and execute that task, the validator restarted, detected the same gap, and created another identical corrective task. Within hours, our task queue was flooded with hundreds of duplicate tasks.

The Lesson Learned: Self-Correction Needs "Patience" and "Awareness"

A proactive system without awareness of the state of its own corrective actions creates more problems than it solves. The solution required making our GoalValidator more intelligent and "patient".

  1. Existing Corrective Task Check: Before creating a new corrective task, the validator now checks if theres already a pending or in_progress task trying to solve the same gap. If it exists, it does nothing.
  2. Cooldown Period: After creating a corrective task, the system enters a "grace period" (e.g., 30 minutes) for that specific goal, during which no new corrective actions are generated, giving the agent team time to act.
  3. AI-Driven Priority and Urgency: Instead of always creating "URGENT" tasks, we taught the AI to evaluate gap severity in relation to project timeline. A 10% gap at project start might generate a medium priority task; the same gap one day before deadline would generate a critical priority task.

The Prompt That Guides Correction

The heart of this system is the prompt that generates corrective tasks. It doesnt just say "solve the problem", but asks for a mini strategic analysis.

Reference code: _generate_corrective_task logic in goal_validator.py

prompt = f"""
You are an expert Project Manager in crisis management. A critical gap has been detected between the current project state and the preset objectives.

**Failed Objective:** {goal.description}
**Current State:** {current_progress}
**Detected Gap:** {failure_details}

**Lessons from the Past (from Memory):**
{relevant_failure_lessons}

**Required Analysis:**
1.  **Root Cause Analysis:** Based on past lessons and the gap, what is the most likely cause of this failure? (e.g., "Tasks were too theoretical", "Missing email verification tool").
2.  **Specific Corrective Action:** Define ONE SINGLE task, as specific and actionable as possible, to start bridging this gap. Dont be generic.
3.  **Optimal Assignment:** Which team role is best suited to solve this problem?

**Output Format (JSON only):**
{{
  "root_cause": "The main cause of the failure.",
  "corrective_task": {{
    "name": "Name of the corrective task (e.g., 'Verify Email of 50 Existing Contacts')",
    "description": "Detailed description of the task and expected result.",
    "assigned_to_role": "Specialized Role",
    "priority": "high"
  }}
}}
"""

This prompt doesnt just solve the problem, but does so intelligently, learning from the past and delegating to the right role, perfectly closing the feedback loop.

📝 Key Takeaways del Capitolo:

Detection Isnt Enough, Action is Needed: An autonomous system doesn't just identify problems, but must be able to generate and prioritize actions to solve them.

Autonomy Requires Self-Awareness: A self-correction system must be aware of actions it has already taken to avoid entering panic loops and creating duplicate work.

Use Memory to Guide Correction: The best corrective actions are those informed by past mistakes. Tightly integrate your validation system with your memory system.

Chapter Conclusion

With the implementation of the self-correction system, our AI team had developed a "nervous system". Now it could perceive when something was wrong and react proactively and intelligently.

We had a system that planned, executed, collaborated, produced quality results, learned, and self-corrected. It was almost complete. The last major challenge was of a different nature: how could we be sure that such a complex system was stable and reliable over time? This led us to develop a robust Monitoring and Integrity Testing system.

graph TD A[Task Completed] B{Post-Execution Analysis} C{AI analyzes the result and process} D{Extracts a Key Insight} E[Types the Insight:
Success, Failure, etc.] F[Generates Relevance Tags] G{Saves Structured Insight in WorkspaceMemory} A --> B B --> C C --> D D --> E E --> F F --> G

System Architecture

"War Story": The Polluted Memory

Our first attempts to implement memory were a disaster. We simply asked the agent at the end of each task: "What did you learn?"

Disaster Logbook (July 28th):

INSIGHT 1: "I completed the task successfully." (Useless)
INSIGHT 2: "Market analysis is important." (Banal)
INSIGHT 3: "Using a friendly tone in emails seems to work." (Vague)

Our memory was filling up with useless banalities. It was "polluted" by low-value information that made it impossible to find the real gems.

The Lesson Learned: Learning Must Be Specific and Measurable.

Its not enough to ask AI to "learn". You have to force it to formulate its lessons in a way thats specific, measurable, and actionable.

We completely rewrote the prompt for insight extraction:

Reference code: Logic within AIMemoryIntelligence

prompt = f"""
Analyze the following completed task and its result. Extract ONE SINGLE actionable insight that can be used to improve future performance.

**Executed Task:** {task.name}
**Result:** {task.result}
**Quality Score Achieved:** {quality_score}/100

**Required Analysis:**
1.  **Identify the Cause:** What single action, pattern, or technique contributed most to the success (or failure) of this task?
2.  **Quantify the Impact:** If possible, quantify the impact. (E.g., "Using the {{company}} token in the subject increased open rate by 15%").
3.  **Formulate the Lesson:** Write the lesson as a general rule applicable to future tasks.
4.  **Create Tags:** Generate 3-5 specific tags to make this insight easy to find.

**Example Success Insight:**
- **content:** "Emails that include a specific numerical statistic in the first paragraph achieve 20% higher click-through rates."
- **relevance_tags:** ["email_copywriting", "ctr_optimization", "data_driven"]

**Example Lesson from Failure:**
- **content:** "Generating contact lists without an email verification process leads to 40% bounce rates, making campaigns ineffective."
- **relevance_tags:** ["contact_generation", "email_verification", "bounce_rate"]

**Output Format (JSON only):**
{{
  "insight_type": "SUCCESS_PATTERN" | "FAILURE_LESSON",
  "content": "The specific and quantified lesson.",
  "relevance_tags": ["tag1", "tag2"],
  "confidence_score": 0.95
}}
"""

This prompt changed everything. It forced the AI to stop producing banalities and start generating strategic knowledge.

📝 Key Takeaways del Capitolo:

Memory isnt an Archive, its a Learning System: Dont save everything. Design a system to extract and save only high-value insights.

Structure Your Memories: Use data models (like Pydantic) to give shape to your "memories". This makes them queryable and usable.

Force AI to Be Specific: Always ask to quantify impact and formulate lessons that are general and actionable rules.

Use Tags for Contextualization: A good tagging system is fundamental for retrieving the right insight at the right time.

Chapter Conclusion

With a functioning memory system, our agent team had finally acquired the ability to learn. Every executed project was no longer an isolated event, but an opportunity to make the entire system more intelligent.

But learning is useless if it doesn't lead to behavioral change. Our next challenge was closing the loop: how could we use stored lessons to automatically course-correct when a project was going badly? This led us to develop our Course Correction system.

🌙 Theme
🔖 Bookmark
📚 My Bookmarks
🌐
🔤 Font Size
Bookmark saved!

📚 My Bookmarks

Bookmark saved!
Bookmark saved!

📚 I Miei Bookmark