/**
 * Multi-turn memory-feedback scenarios.
 *
 * Each scenario exercises ONE specific mechanism from the memory work
 * shipped in 5bca65d (catalog injection / transition auto-load /
 * conditional nudge / post-compaction nudge / append-vs-add).
 *
 * Each scenario specifies:
 *   - mechanism: which feature it tests (for grouped scoring)
 *   - seedMemories: pre-populated memories the agent starts with
 *   - seedTasks: pre-populated tasks
 *   - postCompactionFlag: whether to inject the post-compaction nudge
 *     before the first turn (simulates running right after compaction)
 *   - userTurns: scripted user messages (one per turn, in order)
 *   - expect: scoring rubric
 */

export const SCENARIOS = [
  // ─── Catalog injection ─────────────────────────────────────────
  {
    id: 'M1-catalog-recall',
    mechanism: 'catalog-injection',
    description: 'Pre-existing memory addresses the user question. Agent should consult catalog → read the relevant memory.',
    seedMemories: [
      { id: 'mem-rate-limits', title: 'finding/upstream-rate-limits',
        description: 'API limits 60 req/min, counts failed auth retries',
        content: 'The upstream provider enforces 60 req/min on our key. Failed auth retries DO count against the bucket. We hit this on the Talisman integration last sprint — the auth tests retry on 401 and burned through 20 requests in a few seconds.' },
      { id: 'mem-unrelated', title: 'plan/dashboard-redesign',
        description: 'Dashboard redesign plan, 3 phases',
        content: 'Phase 1: tokens. Phase 2: navigation. Phase 3: analytics.' },
    ],
    seedTasks: [],
    postCompactionFlag: false,
    userTurns: [
      'Our integration tests are flaky again — sometimes they pass, sometimes 5 of them fail with 429. Any idea why?',
    ],
    expect: {
      // Mechanism: "catalog injection drives the agent to USE the
      // memory's content". Three success paths, any one counts:
      //   1. memory.read on the seeded entry (most explicit)
      //   2. memory.list or memory.search (consulting the tool)
      //   3. The agent's text answer references content from the
      //      seed memory verbatim (the catalog description already
      //      contains the answer; this is "answered from catalog")
      // The only failure: agent improvises without ANY signal that
      // it saw the memory.
      action: 'memory.read',
      orActions: ['memory.search', 'memory.list'],
      memoryId: 'mem-rate-limits',
      // Content fingerprints (any-of) to grep in the agent's final
      // assistant text / jobdone summary.
      answerContentAny: ['60 req/min', '60 requests per minute', 'failed auth retries'],
      reason: 'rate-limit memory directly explains the 429 flakiness; agent should consult memory OR pull its content into the answer',
    },
  },

  // ─── Task transition auto-load (memory attached) ───────────────
  {
    id: 'M2-transition-attached',
    mechanism: 'transition-handler-attached',
    description: 'Task references a memory. When task → in_progress, the memory is auto-loaded in the result. Agent should USE that content.',
    seedMemories: [
      { id: 'mem-auth-plan', title: 'plan/auth-migration',
        description: 'Auth migration: 3-step rollout',
        content: 'Step 1: dual-issue tokens for 7 days. Step 2: switch reads to new path. Step 3: deprecate v1. Hard requirement: NO downtime; we MUST keep v1 verifying tokens until step 3.' },
    ],
    seedTasks: [
      { id: 'task-auth', title: 'Start auth migration',
        description: '[memory:mem-auth-plan] Begin rollout per the linked plan',
        status: 'pending', priority: 'high' },
    ],
    postCompactionFlag: false,
    userTurns: [
      'Go ahead and start working on the auth migration task. Tell me which step you\'re tackling first and why.',
    ],
    expect: {
      // Either path counts: (a) taskmanager.update transitions task →
      // auto-load fires → content lands in tool result; (b) the agent
      // reads the memory directly off the catalog. Both deliver the
      // plan content to the agent; both are successful outcomes for
      // the user's question.
      action: 'taskmanager.update',
      orAction: 'memory.read',
      memoryId: 'mem-auth-plan',
      reason: 'transition handler auto-loads OR catalog drives direct read; either delivers plan content',
    },
  },

  // ─── Conditional nudge (no memory attached) ────────────────────
  {
    id: 'M3-transition-nudge',
    mechanism: 'transition-handler-nudge',
    description: 'Task with NO memory marker, transitioned to in_progress. Nudge fires pointing at catalog. Catalog contains a relevant memory.',
    seedMemories: [
      { id: 'mem-style', title: 'rule/never-emojis',
        description: 'Team rule: no emojis in code or commits',
        content: 'Never use emojis in code, comments, or commit messages. Hard rule from review.' },
    ],
    seedTasks: [],  // live-probe creates the task inline via the user message; the simulated bench seeds it via the in-memory store
    postCompactionFlag: false,
    // Combined trigger: creates the task + asks the agent to begin
    // work in one message. Ensures the transition (and therefore the
    // nudge) fires within the measured window. The pre-restart
    // scenario used a 2-phase setup which leaked the measured work
    // into the setup turn.
    userTurns: [
      'Please create a task titled "Polish the README" with description "Rewrite the intro paragraph to feel less robotic", then BEGIN WORKING on it (transition it to in_progress) and tell me which step you\'re doing first.',
    ],
    expect: {
      action: 'memory.list',
      orAction: 'memory.read',
      reason: 'no attached memory → nudge → agent should at least scan catalog before improvising',
    },
  },

  // ─── Post-compaction nudge ─────────────────────────────────────
  {
    id: 'M4-post-compaction',
    mechanism: 'post-compaction-nudge',
    description: 'Compaction just happened. The relevant detail now lives only as a memory. Nudge should drive the agent to consult it.',
    seedMemories: [
      { id: 'mem-decision', title: 'finding/retry-policy',
        description: 'Decided: exponential backoff, cap at 30s, max 5 retries',
        content: 'After discussing trade-offs we landed on exponential backoff (base 2), capped at 30s, max 5 retries. Decided last Thursday.' },
    ],
    seedTasks: [],
    postCompactionFlag: true,
    userTurns: [
      'Quick check: what did we decide about retries? I forget the cap.',
    ],
    expect: {
      action: 'memory.read',
      memoryId: 'mem-decision',
      reason: 'post-compaction nudge + catalog should drive read, not improvise',
    },
  },

  // ─── Append-vs-add (memory evolution) ──────────────────────────
  {
    id: 'M5-append-evolution',
    mechanism: 'append-action',
    description: 'Plan memory exists; user shares a new constraint that augments it. Agent should `memory append`, not `add` a new entry.',
    seedMemories: [
      { id: 'mem-plan', title: 'plan/onboarding',
        description: 'New-hire onboarding flow plan',
        content: 'Day 1: accounts. Day 2-3: shadow. Week 2: first task.' },
    ],
    seedTasks: [],
    postCompactionFlag: false,
    userTurns: [
      'Add this to our onboarding plan: each new hire also gets paired with a senior eng for code reviews for the first month.',
    ],
    expect: {
      action: 'memory.append',
      memoryId: 'mem-plan',
      reason: 'augmenting an existing plan should append; creating new would duplicate the topic',
    },
  },

  // ─── Negative control: small talk ──────────────────────────────
  {
    id: 'M6-negative-control',
    mechanism: 'negative-control',
    description: 'Routine ack. Catalog injection should NOT make the agent trigger-happy on memory writes.',
    seedMemories: [
      { id: 'mem-x', title: 'finding/something', description: '...', content: 'noise' },
    ],
    seedTasks: [],
    postCompactionFlag: false,
    userTurns: [
      'Thanks, sounds good.',
    ],
    expect: {
      action: 'none',
      reason: 'agent should NOT call memory.add on a simple ack',
    },
  },
];
