"""Live kanban worker E2E cases — real dispatcher + kernel worker + MLX.

Each case seeds work under an isolated tenant, waits for terminal task state via
HTTP polling, and scores the completion contract (status, run outcome, markers).

Designed as a gentler alternative to the full 124-case chat battery: fewer cases,
each exercising the claim → worker → kanban-result → verify seam.

Read cases target ``docs/validator-api.md``, which exists in the worker cwd
(``~/.mugi/workspace``). Do not point them at repo-only files — the worker runs
in the workspace, not the repo.

Imported by ``kernel_live_worker_battery.py``.
"""
from __future__ import annotations

from scripts.kernel_live_worker_types import WorkerCase

_DOC = "docs/validator-api.md"

_KANBAN_RESULT_HINT = (
    "End your final assistant message with a valid ```kanban-result``` fence "
    "(summary ≥48 chars). Use read_file/list_directory only — no web search."
)

WORKER_CASES: list[WorkerCase] = [
    # ── Baseline: general profile, single read_file handoff ─────────────────
    WorkerCase(
        id="worker_general_read_file",
        title="Worker battery: validator-api read",
        body=(
            f"Call read_file on {_DOC} (first 20 lines). "
            f"Summarize in one sentence what the file documents. {_KANBAN_RESULT_HINT}"
        ),
        assignee="general",
        priority=4,
        metadata={
            "acceptance": {
                "min_summary_chars": 48,
                "reject_deferred_prose": True,
            }
        },
        timeout_sec=240.0,
        notes="Golden path: general worker completes a minimal read-only handoff.",
    ),
    # ── code_readonly profile ─────────────────────────────────────────────────
    WorkerCase(
        id="worker_code_readonly_list",
        title="Worker battery: list + read",
        body=(
            f"Call list_directory on 'docs' then read_file on {_DOC} "
            "(first 12 lines). Report how many files are under docs and what module "
            f"the file documents. {_KANBAN_RESULT_HINT}"
        ),
        assignee="code_readonly",
        priority=4,
        metadata={
            "acceptance": {
                "min_summary_chars": 48,
                "reject_deferred_prose": True,
            }
        },
        timeout_sec=240.0,
    ),
    # ── Verifier: required transcript markers must appear in worker output ────
    WorkerCase(
        id="worker_marker_contract",
        title="Worker battery: marker acceptance",
        body=(
            f"Read {_DOC} (first 27 lines) and report, as completed findings, the "
            "validation strategy the Validator API uses and name all four phases of "
            "its validation pipeline. State the findings directly as facts. "
            f"{_KANBAN_RESULT_HINT}"
        ),
        assignee="general",
        metadata={
            "acceptance": {
                "min_summary_chars": 64,
                "reject_deferred_prose": True,
                "require_transcript_markers": [
                    "fail-fast",
                ],
            }
        },
        require_markers_in_summary=("fail-fast",),
        timeout_sec=300.0,
        notes="Locks verifier marker gate (fail-fast) on a real model run.",
    ),
    # ── DAG: parent completes, child promotes and completes ───────────────────
    WorkerCase(
        id="worker_parent_child_promotion",
        seed="batch",
        batch_items=[
            {
                "title": "Worker battery parent",
                "body": (
                    f"Parent task: read {_DOC} lines 1-9 and record the validation "
                    f"strategy (fail-fast) in kanban-result. {_KANBAN_RESULT_HINT}"
                ),
                "assignee": "general",
                "priority": 1,
            },
            {
                "title": "Worker battery child",
                "body": (
                    f"Child task: read {_DOC} lines 13-27 and note the four phases of "
                    f"the validation pipeline in kanban-result. {_KANBAN_RESULT_HINT}"
                ),
                "assignee": "general",
                "priority": 2,
                "parent_index": 0,
            },
        ],
        expect_tasks_done=2,
        timeout_sec=420.0,
        poll_sec=5.0,
        notes="Two sequential workers; child stays queued until parent is done.",
    ),
    # ── Narrowed toolset: tool_names restricts; find_tools stays preserved ────
    WorkerCase(
        id="worker_narrowed_readonly",
        title="Worker battery: narrowed read-only",
        body=(
            f"Call read_file on {_DOC} (first 15 lines) and summarize what it documents. "
            f"{_KANBAN_RESULT_HINT}"
        ),
        assignee="general",
        # tool_names is a NARROW override: the worker should see only read_file
        # (+ kanban infra + the always-preserved find_tools valve). This proves
        # narrowing works AND that read_file survives the narrow.
        metadata={"tool_names": ["read_file"]},
        timeout_sec=240.0,
        notes="Narrowing honored; find_tools preserved; mutation tools dropped.",
    ),
    # ── Memory read (no write) on worker path ─────────────────────────────────
    WorkerCase(
        id="worker_search_memory_readonly",
        title="Worker battery: search_memory",
        body=(
            "Call search_memory with query 'kernel'. If empty, say so in the summary; "
            "if hits exist, mention count only (no long quotes). "
            f"{_KANBAN_RESULT_HINT}"
        ),
        assignee="general",
        timeout_sec=240.0,
    ),
    # ── Full stack: foreground chat seeds task, dispatcher runs worker ────────
    WorkerCase(
        id="worker_chat_planner_handoff",
        seed="chat",
        chat_prompt=(
            "Worker E2E handoff test: kanban_create_task only (no batch). "
            "title 'Worker battery chat seed' assignee general priority 4 "
            f"body 'Read {_DOC} first 12 lines and complete with kanban-result.' "
            "tenant must be default. Reply with the created task id only."
        ),
        chat_required_tools=("kanban_create_task",),
        chat_timeout_sec=120.0,
        timeout_sec=300.0,
        notes="Planner chat creates task; battery polls worker completion separately.",
    ),
]
