Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Planning and Reflection

The agent generates multi-step plans for each goal and periodically reflects on its progress, adjusting priorities and strategies.

Planning

Before the OODA loop begins work on a goal, the agent generates a Plan -- an ordered sequence of tool calls designed to achieve the goal's success criteria.

Plan Structure

#![allow(unused)]
fn main() {
Plan {
    goal_id: SymbolId,
    steps: Vec<PlanStep>,
    status: PlanStatus,       // Active, Completed, Failed, Superseded
    attempt: u32,             // Incremented on backtrack
    strategy: String,         // Summary of the approach
}

PlanStep {
    tool_name: String,
    tool_input: ToolInput,
    rationale: String,
    status: StepStatus,       // Pending, Active, Completed, Failed, Skipped
    index: usize,
}
}

Strategy Selection

The planner analyzes the goal description and success criteria using VSA-based semantic analysis. It measures interference between the goal text and five strategy patterns:

StrategyKeywordsWhen Selected
Knowledgefind, query, search, discover, explore, list, identifyGoal is about finding information
Reasoningreason, infer, deduce, classify, analyze, whyGoal requires logical derivation
Creationcreate, add, build, connect, link, store, writeGoal is about constructing knowledge
Externalfile, http, command, shell, fetch, downloadGoal involves external data
Similaritysimilar, like, related, compare, clusterGoal is about finding relationships

Alternating Strategies

To avoid getting stuck in local optima, the planner alternates between two meta-strategies based on the attempt number:

  • Even attempts (0, 2, 4...): Explore-first -- knowledge gathering tools, then reasoning tools.
  • Odd attempts (1, 3, 5...): Reason-first -- reasoning tools, then knowledge tools.

Backtracking

When a plan step fails:

  1. The plan is marked as PlanStatus::Failed.
  2. A new plan is generated with attempt += 1.
  3. The alternating strategy ensures a different approach.
  4. After max_backtrack_attempts (default: 3), the goal fails.

CLI

# Generate and display a plan for the current goal
akh-medu agent plan

# In the REPL
> p
> plan

Reflection

After every N cycles (configurable), the agent pauses to reflect on its performance.

What Reflection Examines

  1. Goal progress: Which goals advanced since the last reflection?
  2. Stagnation: Which goals have been stuck for many cycles?
  3. Tool effectiveness: Which tools produced useful results?
  4. Memory pressure: Is working memory getting full?

Adjustments

Reflection produces a list of Adjustment actions:

AdjustmentTriggerEffect
Boost priorityGoal made progressPriority increased to keep momentum
Demote priorityGoal stagnantPriority decreased to try other goals
Suggest decompositionGoal stalled beyond thresholdRecommends splitting the goal
Trigger consolidationMemory pressure highSaves episodic memories, frees WM

Psyche Evolution

If a psyche is configured, reflection also triggers Psyche::evolve():

  • Archetype weights adjust based on tool success rates.
  • Shadow encounter counter grows individuation level.
  • Dominant archetype is recalculated.

Configuration

#![allow(unused)]
fn main() {
ReflectionConfig {
    interval: usize,            // Reflect every N cycles (default: 5)
    min_cycles_worked: usize,   // Minimum work before reflecting
}
}

CLI

# Trigger reflection manually
akh-medu agent reflect

# In the REPL
> r
> reflect

Plan-Reflection Interaction

Plans and reflection work together:

  1. A plan is generated for a goal.
  2. The OODA loop executes plan steps cycle-by-cycle.
  3. If a step fails, backtracking generates an alternative plan.
  4. During periodic reflection, the agent assesses whether the current plan strategy is working.
  5. Reflection may boost or demote the goal, triggering plan regeneration on the next cycle.