Metacognition

CortexPrism assesses every user task before execution and reflects on every agent turn afterwards. The metacognition system scores confidence, classifies task types, escalates low-confidence assessments, and runs adversarial second-pass critiques.

Task Assessment (assessTask)

packages/ai/src/agent/metacog.ts analyzes user messages for:

  • Ambiguity — unclear intent triggers ask_first decision
  • Complexity — multi-step tasks trigger plan_with_rollback
  • Code tasks — patterns like "fix bug", "implement", "refactor"
  • Destructive intent — patterns like "rm", "delete", "drop"
  • Independent subtasks — "and also", "then after" triggers parallelize

Decisions

DecisionMeaning
directExecute immediately
ask_firstRequest clarification before proceeding
delegateHand off to a sub-agent
plan_with_rollbackGenerate a plan artifact before executing
parallelizeFan out to multiple sub-agents concurrently

Confidence Escalation

When confidence falls below 0.35 for a direct decision, the system auto-escalates to ask_first:

if confidence < 0.35 and decision === 'direct':
    escalate to ask_first with clarification prompt
    log escalation event to lens_events

Escalation events appear in the Metacognition page history with a red ⚡ escalated badge.

Adversarial Self-Critique

After each turn (when reflection is enabled), a second-pass critique runs using a skeptical/adversarial system prompt that actively looks for:

  • Missed edge cases and error handling gaps
  • Validation omissions
  • Security concerns the agent may have overlooked
  • Alternative approaches

Results are stored in reflection_memory with category adversarial and surfaced as critique cards in the Metacognition web UI.

API Endpoints

EndpointDescription
GET /api/metacognition/historyRecent assessments, reflections, and escalations (limit 80)
GET /api/metacognition/summaryDecision distribution, total escalations, recent critiques
POST /api/metacognition/testTest assessment with full signal breakdown and confidence

Web UI

The Metacognition page shows:

  • Decision Distribution — bar chart of decision types
  • Decision History — timeline with color-coded decisions and escalation badges
  • Adversarial Critiques — critique cards showing issues from the adversarial reflection pass

See Also