// SmallCode — Features 1-6 MarrowScript Declarations
// Compiles to deterministic runtime for:
//   1. repair_tool_call — self-repair malformed tool calls
//   2. summarize_file   — LLM file summarization for large files
//   3. policy enforcement — in-memory token + call rate caps
//   4. edit_with_approval — checkpoint flow for write operations
//   5. retrieve_context — semantic slice via code graph
//   6. validate_edit    — self-critique after writes

system SmallCodeFeatures {
  domain: cognitive_scaffold

  // ═══════════════════════════════════════════════════════════════════════════
  // MODELS
  // ═══════════════════════════════════════════════════════════════════════════

  model TinyClassifier {
    provider: openai_compat
    endpoint: "http://10.0.0.20:1234/v1"
    name: "huihui-gemma-4-e4b-it-abliterated"
    context_window: 128000
    max_output: 512
    temperature: 0.0
    cost_class: tiny
    latency_class: fast
  }

  model SmallCoder {
    provider: openai_compat
    endpoint: "http://10.0.0.20:1234/v1"
    name: "huihui-gemma-4-e4b-it-abliterated"
    context_window: 128000
    max_output: 4096
    temperature: 0.1
    cost_class: small
    latency_class: medium
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 1: repair_tool_call
  // When model produces malformed JSON, bad tool name, or wrong args,
  // send back a repair prompt instead of silently failing.
  // ═══════════════════════════════════════════════════════════════════════════

  prompt repair_tool_call(original_call: string, error: string, tool_schema: string) {
    model: SmallCoder
    template: "extension_point:tmpl_repair_tool_call"
    returns: string
    timeout: 15s
    validate: schema_only
    on_invalid: fail
    retry: { max_attempts: 1, backoff: fixed, interval: 200ms }
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 2: summarize_file
  // LLM-backed file summarization for large files (>200 lines).
  // Reduces to function signatures + key logic descriptions.
  // ═══════════════════════════════════════════════════════════════════════════

  prompt summarize_file(path: string, content: string, target_tokens: uint) {
    model: TinyClassifier
    template: "extension_point:tmpl_summarize_file"
    returns: string
    timeout: 20s
    idempotent: true
    validate: schema_only
    on_invalid: retry
    retry: { max_attempts: 2, backoff: fixed, interval: 500ms }
    cache: { key: hash(content), ttl: 1h }
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 3: policy enforcement
  // In-memory token cap and call rate limiting.
  // Prevents runaway agent loops from spinning for minutes.
  // ═══════════════════════════════════════════════════════════════════════════

  capability run_turn(user_message: string) {
    cognition: semantic_slice using {
      task: user_message,
      max_files: 0,
      hop_depth: 0
    }
    returns: string
    sync: eventual
  }

  policy agent_limits {
    rate_limit: 60 per 1m
    audit: true
    cost_budgets: [
      {
        scope: per_feature: "run_turn"
        window: 1m
        cap_calls: 30
        on_exceeded: { action: error, code: "TURN_RATE_LIMIT" }
      },
      {
        scope: per_user
        window: 1h
        cap_tokens: 500000
        on_exceeded: { action: throttle, retry_after: 5m }
      }
    ]
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 4: edit_with_approval
  // Checkpoint flow — pause before applying writes, show diff, wait for y/n.
  // The checkpoint uses the compiled flow runtime with backward compensation.
  // ═══════════════════════════════════════════════════════════════════════════

  entity EditProposal {
    owns: [
      file_path: string,
      diff_preview: string,
      tool_name: string,
      proposed_content: string
    ]
  }

  capability apply_edit(e: EditProposal) {
    cognition: semantic_slice using {
      task: e.file_path,
      max_files: 1,
      hop_depth: 0
    }
    returns: string
    sync: transactional
  }

  capability rollback_edit(e: EditProposal) {
    cognition: semantic_slice using {
      task: e.file_path,
      max_files: 1,
      hop_depth: 0
    }
    returns: string
    sync: transactional
  }

  flow edit_with_approval {
    step propose: apply_edit(e)
      checkpoint: review_edit {
        shows: [e.file_path, e.diff_preview]
        allow: ["approve", "reject", "edit"]
        timeout: 5m
        on_timeout: cancel
      }
    step apply: apply_edit(e)
      compensate: rollback_edit(e)
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 5: retrieve_context
  // Before each turn, walk code graph from user message keywords.
  // Auto-inject relevant file signatures (max 8 files, 2 hops).
  // ═══════════════════════════════════════════════════════════════════════════

  entity ContextSlice {
    owns: [
      files: string,
      symbols: string,
      token_count: uint
    ]
  }

  capability retrieve_context(user_message: string, max_files: uint) {
    cognition: semantic_slice using {
      task: user_message,
      max_files: 8,
      hop_depth: 2
    }
    returns: ContextSlice
    sync: eventual
    idempotent: true
  }

  // ═══════════════════════════════════════════════════════════════════════════
  // FEATURE 6: validate_edit (self-critique)
  // After write_file or patch, ask model: "does this look correct?"
  // before marking the tool call successful.
  // ═══════════════════════════════════════════════════════════════════════════

  prompt validate_edit(file_path: string, content: string, original_task: string) {
    model: TinyClassifier
    template: "extension_point:tmpl_validate_edit"
    returns: string
    timeout: 10s
    validate: schema_only
    on_invalid: fail
    retry: { max_attempts: 1, backoff: fixed, interval: 200ms }
    cache: { key: hash(content), ttl: 10m }
  }

// SmallCode — Features 1-6 MarrowScript Declarations
// ... (existing content above unchanged)

// ═══════════════════════════════════════════════════════════════════════════
// FEATURE 7: intent_clarifier (MarrowScript Roadmap #1)
// Replaces hand-rolled regex in src/session/clarify.js with a compiled
// classifier that uses caching, retry, and constrained output.
// ═══════════════════════════════════════════════════════════════════════════

  prompt intent_clarifier(user_message: string) {
    model: TinyClassifier
    template: "extension_point:tmpl_intent_clarifier"
    returns: string
    timeout: 2s
    idempotent: true
    validate: schema_only
    on_invalid: retry
    retry: { max_attempts: 2, backoff: fixed, interval: 100ms }
    cache: { key: hash(user_message), ttl: 30m }
    constraints: [
      output in ["clear", "vague"]
    ]
  }

  extension_point tmpl_intent_clarifier(user_message: string) {
    returns: string
    stable: true
  }

  extension_point tmpl_repair_tool_call(original_call: string, error: string, tool_schema: string) {
    returns: string
    stable: true
  }

  extension_point tmpl_summarize_file(path: string, content: string, target_tokens: uint) {
    returns: string
    stable: true
  }

  extension_point tmpl_validate_edit(file_path: string, content: string, original_task: string) {
    returns: string
    stable: true
  }
}
