Skip to content

[Phase 4] Implement Goal Recitation for Long-Running Tasks #70

@frankbria

Description

@frankbria

Summary

Implement periodic goal recitation to prevent agent drift during long-running tasks with many tool calls.

Background: State of the Art

From Philipp Schmid's 5 Practical Tips for Context Engineering:

"Recite Goals to not get lost: Prevent the model from 'getting lost' by having it periodically restate its objectives. This keeps the primary goal in its recent attention span."

Key data point: "A typical task in Manus requires around 50 tool calls on average."

With 50+ tool calls, the original task description may have scrolled far out of the model's attention window. Combined with accumulated tool outputs (error messages, file contents, test results), the model can:

  • Forget the original objective
  • Optimize for intermediate goals (e.g., fixing a test) instead of the actual task
  • Enter repetitive loops without progress toward completion

Goal recitation is a forcing function: periodically inject "Reminder: Your current objective is X. Progress so far: Y. Remaining: Z."

Current State in CodeFRAME

The Lead Agent decomposes PRD → tasks and assigns to workers. Questions:

  1. Task visibility: Do workers have the original task description in every context window?
  2. Progress tracking: Do workers know what they've accomplished vs. what remains?
  3. Drift detection: Is there any mechanism to detect when a worker is "off track"?
  4. Long tasks: For tasks requiring 20+ tool calls, is goal visibility maintained?

The explicit planning pillar (PRD decomposition) is a form of goal structure, but per-call goal injection may not be implemented.

Investigation Tasks

  1. Audit goal visibility in context

    • For a long-running task, examine what goal/task information is in context at call N vs. call N+20
    • Check if original task description is still visible or scrolled out
    • Measure context position of goal info (beginning? middle? end?)
  2. Identify drift patterns

    • Review logs for tasks that took unusually long
    • Look for patterns: repeated similar tool calls, circular behavior, abandoning original objective
    • Check if workers sometimes complete "something" that isn't what was asked
  3. Design goal recitation mechanism

    Option A: Periodic injection

    • Every N tool calls (e.g., 10), inject goal reminder
    • Include: original objective, progress summary, remaining steps

    Option B: Context position guarantee

    • Always place goal summary in final 500 tokens (recent attention)
    • Update summary after each step

    Option C: Self-recitation prompt

    • Before each tool call, agent states: "I am doing X because Y toward goal Z"
    • Explicit chain-of-thought linking action to objective
  4. Integrate with existing planning

    • The Lead Agent's task decomposition could provide "checkpoints"
    • At each checkpoint, force goal recitation
    • Workers could have a "recite goal" tool they must call periodically

Success Criteria

  • Documented goal visibility patterns in current implementation
  • Identified any drift patterns in historical task logs
  • Implemented goal recitation mechanism
  • Measured reduction in drift/circular behavior
  • Task completion rate improvement for long tasks (20+ tool calls)

Implementation Sketch

class GoalRecitationMiddleware:
    def __init__(self, recite_every_n_calls: int = 10):
        self.call_count = 0
        self.recite_interval = recite_every_n_calls
    
    def wrap_context(self, context: Context, task: Task) -> Context:
        self.call_count += 1
        
        if self.call_count % self.recite_interval == 0:
            recitation = f"""
            === GOAL RECITATION ===
            Original Objective: {task.description}
            Completed Steps: {task.completed_steps}
            Remaining: {task.remaining_steps}
            Current Focus: {task.current_subtask}
            ========================
            """
            context.append_to_end(recitation)
        
        return context

References

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions