/**
 * Types for the Wander Detector — a metacognitive layer that detects
 * when the agent is looping, oscillating, or producing filler.
 */

export type WanderSignalType =
  | 'repeated_tool_call'
  | 'same_error'
  | 'oscillation'
  | 'filler_turn'
  | 'file_reread'

export interface WanderSignal {
  type: WanderSignalType
  weight: number
  description: string
  turnIndex: number
}

export interface WanderState {
  /** Cumulative wander score for the current session */
  score: number
  /** Number of consecutive escalation triggers */
  consecutiveEscalations: number
  /** All detected signals */
  signals: WanderSignal[]
  /** Hash of recent tool calls: (toolName, inputHash) → count */
  recentToolCalls: Map<string, number>
  /** Recent error strings for dedup detection */
  recentErrors: string[]
  /** Recently read file paths for re-read detection */
  recentFileReads: string[]
  /** Approach descriptions extracted from tool use patterns */
  approachesTried: string[]
  /** Current turn count */
  turnCount: number
}

export type EscalationLevel = 'nudge' | 'compact' | 'halt'

export interface EscalationAction {
  level: EscalationLevel
  message: string
}

export function createFreshWanderState(): WanderState {
  return {
    score: 0,
    consecutiveEscalations: 0,
    signals: [],
    recentToolCalls: new Map(),
    recentErrors: [],
    recentFileReads: [],
    approachesTried: [],
    turnCount: 0,
  }
}
