import type { FlowNode, FlowEdge } from "@/types/agent-data";

export interface FlowDefinition {
  nodes: FlowNode[];
  edges: FlowEdge[];
}

const FLOW_WIDTH = 600;
const COL_CENTER = FLOW_WIDTH / 2;
const COL_LEFT = 140;
const COL_RIGHT = FLOW_WIDTH - 140;

export const EXECUTION_FLOWS: Record<string, FlowDefinition> = {
  s01: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "bash", label: "Execute Bash", type: "subprocess", x: COL_LEFT, y: 280 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 360 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "bash", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "bash", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s02: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "dispatch", label: "Tool Dispatch", type: "process", x: COL_LEFT, y: 280 },
      { id: "exec", label: "bash / read / write / edit", type: "subprocess", x: COL_LEFT, y: 360 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 440 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "dispatch", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "dispatch", to: "exec" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s03: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "todo", label: "Create Todos", type: "process", x: COL_CENTER, y: 100 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 180 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 260 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT, y: 340 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 410 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 340 },
    ],
    edges: [
      { from: "start", to: "todo" },
      { from: "todo", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "exec", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s04: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "is_task", label: "task tool?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "spawn", label: "Spawn Subagent\n(fresh messages[])", type: "subprocess", x: 60, y: 380 },
      { id: "sub_loop", label: "Subagent Loop", type: "process", x: 60, y: 460 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 380 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 540 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_task", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_task", to: "spawn", label: "task" },
      { from: "is_task", to: "exec", label: "other" },
      { from: "spawn", to: "sub_loop" },
      { from: "sub_loop", to: "append" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s05: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "is_skill", label: "load_skill?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "load", label: "Read SKILL.md", type: "subprocess", x: 60, y: 370 },
      { id: "inject", label: "Inject via\ntool_result", type: "process", x: 60, y: 450 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 370 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 530 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_skill", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_skill", to: "load", label: "skill" },
      { from: "is_skill", to: "exec", label: "other" },
      { from: "load", to: "inject" },
      { from: "inject", to: "append" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s06: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "compress_check", label: "Over token\nlimit?", type: "decision", x: COL_CENTER, y: 110 },
      { id: "compress", label: "Compress Context", type: "subprocess", x: COL_RIGHT, y: 110 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 200 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 280 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT, y: 360 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 430 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 360 },
    ],
    edges: [
      { from: "start", to: "compress_check" },
      { from: "compress_check", to: "compress", label: "yes" },
      { from: "compress_check", to: "llm", label: "no" },
      { from: "compress", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "exec", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "exec", to: "append" },
      { from: "append", to: "compress_check" },
    ],
  },
  s07: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "is_task", label: "task_manager?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "crud", label: "CRUD Task\n(file-based)", type: "subprocess", x: 60, y: 370 },
      { id: "dep_check", label: "Check\nDependencies", type: "process", x: 60, y: 450 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 370 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 530 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_task", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_task", to: "crud", label: "task" },
      { from: "is_task", to: "exec", label: "other" },
      { from: "crud", to: "dep_check" },
      { from: "dep_check", to: "append" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s08: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "bg_check", label: "Background?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "bg_spawn", label: "Spawn Thread", type: "subprocess", x: 60, y: 370 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 370 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 450 },
      { id: "notify", label: "Notification\nQueue", type: "process", x: 60, y: 450 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "bg_check", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "bg_check", to: "bg_spawn", label: "bg" },
      { from: "bg_check", to: "exec", label: "fg" },
      { from: "bg_spawn", to: "notify" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
      { from: "notify", to: "llm" },
    ],
  },
  s09: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call\n(team lead)", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 200 },
      { id: "is_team", label: "Team tool?", type: "decision", x: COL_LEFT, y: 290 },
      { id: "spawn", label: "Spawn\nTeammate", type: "subprocess", x: 60, y: 390 },
      { id: "msg", label: "Send Message\n(JSONL inbox)", type: "subprocess", x: 60, y: 470 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 390 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 550 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 290 },
      { id: "teammate", label: "Teammate Agent\n(own loop)", type: "process", x: COL_RIGHT, y: 470 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_team", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_team", to: "spawn", label: "spawn" },
      { from: "is_team", to: "exec", label: "other" },
      { from: "spawn", to: "teammate" },
      { from: "spawn", to: "msg" },
      { from: "msg", to: "append" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s10: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call\n(team lead)", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 200 },
      { id: "is_proto", label: "Protocol?", type: "decision", x: COL_LEFT, y: 290 },
      { id: "shutdown", label: "Shutdown\nRequest", type: "subprocess", x: 60, y: 390 },
      { id: "fsm", label: "FSM:\npending->approved", type: "process", x: 60, y: 470 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 80, y: 390 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 550 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 290 },
      { id: "teammate", label: "Teammate\nreceives request_id", type: "process", x: COL_RIGHT, y: 470 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_proto", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_proto", to: "shutdown", label: "shutdown" },
      { from: "is_proto", to: "exec", label: "other" },
      { from: "shutdown", to: "fsm" },
      { from: "fsm", to: "teammate" },
      { from: "teammate", to: "append" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s11: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "inbox", label: "Check Inbox", type: "process", x: COL_CENTER, y: 100 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 180 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 260 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT, y: 340 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 410 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 340 },
      { id: "idle", label: "Idle Cycle", type: "process", x: COL_RIGHT, y: 420 },
      { id: "poll", label: "Poll Tasks\n+ Auto-Claim", type: "subprocess", x: COL_RIGHT, y: 500 },
    ],
    edges: [
      { from: "start", to: "inbox" },
      { from: "inbox", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "exec", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "exec", to: "append" },
      { from: "append", to: "llm" },
      { from: "end", to: "idle" },
      { from: "idle", to: "poll" },
      { from: "poll", to: "inbox" },
    ],
  },
  s12: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "is_wt", label: "worktree tool?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "task", label: "Task Board\\n(.tasks)", type: "process", x: 60, y: 360 },
      { id: "wt_create", label: "Allocate / Enter\\nWorktree", type: "subprocess", x: 60, y: 440 },
      { id: "wt_run", label: "Run in\\nIsolated Dir", type: "subprocess", x: COL_LEFT + 80, y: 360 },
      { id: "wt_close", label: "Closeout:\\nworktree_keep / remove", type: "process", x: COL_LEFT + 80, y: 440 },
      { id: "events", label: "Emit Lifecycle Events\\n(side-channel)", type: "process", x: COL_RIGHT, y: 420 },
      { id: "events_read", label: "Optional Read\\nworktree_events", type: "subprocess", x: COL_RIGHT, y: 520 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 530 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "is_wt", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "is_wt", to: "task", label: "task ops" },
      { from: "is_wt", to: "wt_create", label: "create/bind" },
      { from: "is_wt", to: "wt_run", label: "run/status" },
      { from: "task", to: "wt_create", label: "allocate lane" },
      { from: "wt_create", to: "wt_run" },
      { from: "task", to: "append", label: "task result" },
      { from: "wt_create", to: "events", label: "emit create" },
      { from: "wt_create", to: "append", label: "create result" },
      { from: "wt_run", to: "wt_close" },
      { from: "wt_run", to: "append", label: "run/status result" },
      { from: "wt_close", to: "events", label: "emit closeout" },
      { from: "wt_close", to: "append", label: "closeout result" },
      { from: "events", to: "events_read", label: "optional query" },
      { from: "events_read", to: "append", label: "events result" },
      { from: "append", to: "llm" },
    ],
  },
  s13: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "bg_check", label: "background?", type: "decision", x: COL_LEFT, y: 280 },
      { id: "spawn", label: "Spawn Thread", type: "subprocess", x: 70, y: 370 },
      { id: "placeholder", label: "Return Placeholder", type: "process", x: 70, y: 450 },
      { id: "notify", label: "Notification\nQueue", type: "process", x: COL_RIGHT, y: 500 },
      { id: "collect", label: "Collect Results", type: "process", x: COL_RIGHT, y: 590 },
      { id: "exec", label: "Execute Tool", type: "subprocess", x: COL_LEFT + 110, y: 370 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 690 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "bg_check", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "bg_check", to: "spawn", label: "bg" },
      { from: "bg_check", to: "exec", label: "fg" },
      { from: "spawn", to: "placeholder" },
      { from: "spawn", to: "notify", label: "done" },
      { from: "notify", to: "collect" },
      { from: "placeholder", to: "append" },
      { from: "exec", to: "append" },
      { from: "collect", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s14: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "tool_check", label: "cron tool?", type: "decision", x: COL_CENTER, y: 190 },
      { id: "schedule", label: "schedule_cron", type: "subprocess", x: COL_LEFT, y: 280 },
      { id: "store", label: "Durable Store\n.scheduled_tasks", type: "process", x: COL_LEFT, y: 360 },
      { id: "scheduler", label: "Scheduler Loop", type: "process", x: COL_CENTER, y: 450 },
      { id: "match", label: "cron_matches?", type: "decision", x: COL_CENTER, y: 540 },
      { id: "queue", label: "Cron Queue", type: "process", x: COL_RIGHT, y: 540 },
      { id: "processor", label: "Queue Processor", type: "process", x: COL_RIGHT, y: 630 },
      { id: "agent", label: "Agent Loop", type: "process", x: COL_CENTER, y: 720 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 280 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "schedule", label: "yes" },
      { from: "tool_check", to: "end", label: "no" },
      { from: "schedule", to: "store" },
      { from: "store", to: "scheduler" },
      { from: "scheduler", to: "match" },
      { from: "match", to: "queue", label: "due" },
      { from: "queue", to: "processor" },
      { from: "processor", to: "agent" },
      { from: "agent", to: "llm" },
    ],
  },
  s15: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "lead", label: "Lead LLM", type: "process", x: COL_CENTER, y: 110 },
      { id: "team_tool", label: "team tool?", type: "decision", x: COL_CENTER, y: 200 },
      { id: "spawn", label: "Spawn Teammate", type: "subprocess", x: COL_LEFT, y: 300 },
      { id: "send", label: "Send Message", type: "subprocess", x: COL_CENTER, y: 300 },
      { id: "bus", label: "MessageBus\n.mailboxes", type: "process", x: COL_CENTER, y: 400 },
      { id: "teammate", label: "Teammate Loop", type: "process", x: COL_RIGHT, y: 500 },
      { id: "tools", label: "Scoped Tools", type: "subprocess", x: COL_RIGHT, y: 590 },
      { id: "inbox", label: "Lead Inbox", type: "process", x: COL_CENTER, y: 700 },
      { id: "append", label: "Append Result", type: "process", x: COL_LEFT, y: 700 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 300 },
    ],
    edges: [
      { from: "start", to: "lead" },
      { from: "lead", to: "team_tool" },
      { from: "team_tool", to: "spawn", label: "spawn" },
      { from: "team_tool", to: "send", label: "send" },
      { from: "team_tool", to: "end", label: "no" },
      { from: "spawn", to: "bus", label: "register" },
      { from: "send", to: "bus" },
      { from: "bus", to: "teammate" },
      { from: "teammate", to: "tools" },
      { from: "tools", to: "bus", label: "reply" },
      { from: "bus", to: "inbox" },
      { from: "inbox", to: "append" },
      { from: "append", to: "lead" },
    ],
  },
  s16: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "lead", label: "Lead LLM", type: "process", x: COL_CENTER, y: 110 },
      { id: "protocol", label: "protocol?", type: "decision", x: COL_CENTER, y: 200 },
      { id: "request", label: "request_plan /\nrequest_shutdown", type: "subprocess", x: COL_LEFT, y: 300 },
      { id: "pending", label: "Pending Requests\nrequest_id", type: "process", x: COL_LEFT, y: 390 },
      { id: "dispatch", label: "Dispatch Message", type: "process", x: COL_CENTER, y: 470 },
      { id: "teammate", label: "Teammate Handler", type: "process", x: COL_RIGHT, y: 470 },
      { id: "response", label: "submit_plan /\nack shutdown", type: "subprocess", x: COL_RIGHT, y: 560 },
      { id: "match", label: "match_response?", type: "decision", x: COL_CENTER, y: 640 },
      { id: "append", label: "Append Protocol\nResult", type: "process", x: COL_CENTER, y: 730 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 300 },
    ],
    edges: [
      { from: "start", to: "lead" },
      { from: "lead", to: "protocol" },
      { from: "protocol", to: "request", label: "yes" },
      { from: "protocol", to: "end", label: "no" },
      { from: "request", to: "pending" },
      { from: "pending", to: "dispatch" },
      { from: "dispatch", to: "teammate" },
      { from: "teammate", to: "response" },
      { from: "response", to: "match" },
      { from: "match", to: "append", label: "matched" },
      { from: "append", to: "lead" },
    ],
  },
  s17: {
    nodes: [
      { id: "start", label: "System Tick", type: "start", x: COL_CENTER, y: 30 },
      { id: "idle", label: "Idle Poll", type: "process", x: COL_CENTER, y: 110 },
      { id: "scan", label: "Scan Tasks", type: "subprocess", x: COL_CENTER, y: 190 },
      { id: "claimable", label: "claimable?", type: "decision", x: COL_CENTER, y: 280 },
      { id: "claim", label: "claim_task\n(owner check)", type: "subprocess", x: COL_LEFT, y: 380 },
      { id: "work", label: "WORK State", type: "process", x: COL_LEFT, y: 470 },
      { id: "complete", label: "complete_task", type: "subprocess", x: COL_LEFT, y: 560 },
      { id: "inbox", label: "Check Inbox", type: "process", x: COL_RIGHT, y: 380 },
      { id: "shutdown", label: "Shutdown?", type: "decision", x: COL_RIGHT, y: 470 },
      { id: "done", label: "IDLE / SHUTDOWN", type: "end", x: COL_RIGHT, y: 560 },
    ],
    edges: [
      { from: "start", to: "idle" },
      { from: "idle", to: "scan" },
      { from: "scan", to: "claimable" },
      { from: "claimable", to: "claim", label: "yes" },
      { from: "claimable", to: "inbox", label: "no" },
      { from: "claim", to: "work" },
      { from: "work", to: "complete" },
      { from: "complete", to: "idle" },
      { from: "inbox", to: "shutdown" },
      { from: "shutdown", to: "done", label: "yes" },
      { from: "shutdown", to: "idle", label: "no" },
    ],
  },
  s18: {
    nodes: [
      { id: "start", label: "Task Selected", type: "start", x: COL_CENTER, y: 30 },
      { id: "create", label: "create_worktree", type: "subprocess", x: COL_CENTER, y: 110 },
      { id: "validate", label: "Validate Name", type: "process", x: COL_CENTER, y: 190 },
      { id: "git", label: "git worktree add", type: "subprocess", x: COL_LEFT, y: 290 },
      { id: "bind", label: "Bind Task\nworktree field", type: "process", x: COL_LEFT, y: 380 },
      { id: "run", label: "Run in Isolated\nDirectory", type: "subprocess", x: COL_CENTER, y: 470 },
      { id: "events", label: "Lifecycle Events\n.events.jsonl", type: "process", x: COL_RIGHT, y: 190 },
      { id: "close", label: "keep / remove", type: "decision", x: COL_CENTER, y: 560 },
      { id: "cleanup", label: "remove_worktree", type: "subprocess", x: COL_LEFT, y: 650 },
      { id: "keep", label: "keep_worktree", type: "process", x: COL_RIGHT, y: 650 },
      { id: "end", label: "Task Result", type: "end", x: COL_CENTER, y: 740 },
    ],
    edges: [
      { from: "start", to: "create" },
      { from: "create", to: "validate" },
      { from: "validate", to: "git" },
      { from: "git", to: "bind" },
      { from: "bind", to: "run" },
      { from: "create", to: "events", label: "emit" },
      { from: "run", to: "events", label: "status" },
      { from: "run", to: "close" },
      { from: "close", to: "cleanup", label: "remove" },
      { from: "close", to: "keep", label: "keep" },
      { from: "cleanup", to: "end" },
      { from: "keep", to: "end" },
    ],
  },
  s19: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 110 },
      { id: "connect", label: "connect_mcp?", type: "decision", x: COL_CENTER, y: 200 },
      { id: "client", label: "MCP Client", type: "process", x: COL_LEFT, y: 300 },
      { id: "discover", label: "Discover Tools", type: "subprocess", x: COL_LEFT, y: 390 },
      { id: "pool", label: "Assemble Tool Pool\nmcp__server__tool", type: "process", x: COL_CENTER, y: 480 },
      { id: "call", label: "MCP Tool Call", type: "subprocess", x: COL_RIGHT, y: 390 },
      { id: "server", label: "External Server", type: "process", x: COL_RIGHT, y: 480 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 580 },
      { id: "end", label: "Output", type: "end", x: 520, y: 200 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "connect" },
      { from: "connect", to: "client", label: "connect" },
      { from: "connect", to: "call", label: "use" },
      { from: "connect", to: "end", label: "done" },
      { from: "client", to: "discover" },
      { from: "discover", to: "pool" },
      { from: "pool", to: "llm" },
      { from: "call", to: "server" },
      { from: "server", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s20: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "context", label: "Assemble Context\nmemory + tasks", type: "process", x: COL_CENTER, y: 115 },
      { id: "policy", label: "Policy + Hooks", type: "process", x: COL_LEFT, y: 210 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 300 },
      { id: "router", label: "Route Tool", type: "decision", x: COL_CENTER, y: 390 },
      { id: "builtin", label: "Built-in Tools", type: "subprocess", x: 85, y: 500 },
      { id: "team", label: "Teams /\nProtocols", type: "subprocess", x: 230, y: 500 },
      { id: "async", label: "Background /\nCron", type: "subprocess", x: 370, y: 500 },
      { id: "external", label: "Worktree /\nMCP", type: "subprocess", x: 515, y: 500 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 610 },
      { id: "recover", label: "Recover /\nCompact", type: "process", x: COL_LEFT, y: 700 },
      { id: "end", label: "Final Output", type: "end", x: COL_RIGHT, y: 390 },
    ],
    edges: [
      { from: "start", to: "context" },
      { from: "context", to: "policy" },
      { from: "policy", to: "llm" },
      { from: "context", to: "llm" },
      { from: "llm", to: "router" },
      { from: "router", to: "builtin", label: "local" },
      { from: "router", to: "team", label: "team" },
      { from: "router", to: "async", label: "async" },
      { from: "router", to: "external", label: "ext" },
      { from: "router", to: "end", label: "done" },
      { from: "builtin", to: "append" },
      { from: "team", to: "append" },
      { from: "async", to: "append" },
      { from: "external", to: "append" },
      { from: "append", to: "recover" },
      { from: "recover", to: "context" },
    ],
  },
};

const CURRENT_FLOW_OVERRIDES: Record<string, FlowDefinition> = {
  s03: {
    nodes: [
      { id: "start", label: "Tool Call", type: "start", x: COL_CENTER, y: 30 },
      { id: "hard", label: "Hard Deny?", type: "decision", x: COL_CENTER, y: 120 },
      { id: "rules", label: "Rule Match?", type: "decision", x: COL_CENTER, y: 220 },
      { id: "ask", label: "Ask User", type: "subprocess", x: COL_LEFT, y: 320 },
      { id: "allow", label: "Approved?", type: "decision", x: COL_LEFT, y: 410 },
      { id: "exec", label: "Execute Tool", type: "process", x: COL_CENTER, y: 520 },
      { id: "blocked_policy", label: "Blocked", type: "end", x: COL_RIGHT, y: 120 },
      { id: "blocked_user", label: "Blocked", type: "end", x: COL_RIGHT, y: 410 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 610 },
    ],
    edges: [
      { from: "start", to: "hard" },
      { from: "hard", to: "blocked_policy", label: "deny" },
      { from: "hard", to: "rules", label: "ok" },
      { from: "rules", to: "ask", label: "needs approval" },
      { from: "rules", to: "exec", label: "allow" },
      { from: "ask", to: "allow" },
      { from: "allow", to: "blocked_user", label: "no" },
      { from: "allow", to: "exec", label: "yes" },
      { from: "exec", to: "append" },
    ],
  },
  s04: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "user_hook", label: "UserPromptSubmit\nHooks", type: "subprocess", x: COL_CENTER, y: 120 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 220 },
      { id: "tool_check", label: "tool_use?", type: "decision", x: COL_CENTER, y: 310 },
      { id: "pre", label: "PreToolUse\nHooks", type: "subprocess", x: COL_LEFT, y: 410 },
      { id: "blocked", label: "Blocked?", type: "decision", x: COL_LEFT, y: 500 },
      { id: "exec", label: "Tool Handler", type: "process", x: COL_CENTER, y: 600 },
      { id: "post", label: "PostToolUse\nHooks", type: "subprocess", x: COL_CENTER, y: 690 },
      { id: "stop", label: "Stop Hooks", type: "subprocess", x: COL_RIGHT, y: 410 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 500 },
    ],
    edges: [
      { from: "start", to: "user_hook" },
      { from: "user_hook", to: "llm" },
      { from: "llm", to: "tool_check" },
      { from: "tool_check", to: "pre", label: "yes" },
      { from: "tool_check", to: "stop", label: "no" },
      { from: "pre", to: "blocked" },
      { from: "blocked", to: "end", label: "yes" },
      { from: "blocked", to: "exec", label: "no" },
      { from: "exec", to: "post" },
      { from: "post", to: "llm" },
      { from: "stop", to: "end" },
    ],
  },
  s05: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 120 },
      { id: "tool", label: "tool_use?", type: "decision", x: COL_CENTER, y: 210 },
      { id: "todo", label: "todo_write?", type: "decision", x: COL_LEFT, y: 310 },
      { id: "update", label: "Update\ncurrent_todos", type: "process", x: COL_LEFT, y: 410 },
      { id: "other", label: "Run Tool", type: "subprocess", x: COL_CENTER, y: 410 },
      { id: "reminder", label: "3 rounds?\nInject Reminder", type: "process", x: COL_RIGHT, y: 500 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 590 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 310 },
    ],
    edges: [
      { from: "start", to: "llm" },
      { from: "llm", to: "tool" },
      { from: "tool", to: "todo", label: "yes" },
      { from: "tool", to: "end", label: "no" },
      { from: "todo", to: "update", label: "todo" },
      { from: "todo", to: "other", label: "other" },
      { from: "update", to: "append" },
      { from: "other", to: "append" },
      { from: "append", to: "reminder" },
      { from: "reminder", to: "llm" },
    ],
  },
  s06: {
    nodes: [
      { id: "start", label: "User Input", type: "start", x: COL_CENTER, y: 30 },
      { id: "parent", label: "Parent LLM", type: "process", x: COL_CENTER, y: 120 },
      { id: "task_check", label: "task tool?", type: "decision", x: COL_CENTER, y: 220 },
      { id: "spawn", label: "Spawn Subagent\nfresh messages[]", type: "subprocess", x: COL_LEFT, y: 330 },
      { id: "subloop", label: "Subagent Loop\nmax 30 turns", type: "process", x: COL_LEFT, y: 430 },
      { id: "summary", label: "Return Summary\nOnly", type: "process", x: COL_LEFT, y: 530 },
      { id: "tool", label: "Run Parent Tool", type: "subprocess", x: COL_RIGHT, y: 330 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 630 },
      { id: "end", label: "Output", type: "end", x: COL_RIGHT, y: 220 },
    ],
    edges: [
      { from: "start", to: "parent" },
      { from: "parent", to: "task_check" },
      { from: "task_check", to: "spawn", label: "task" },
      { from: "task_check", to: "tool", label: "other" },
      { from: "task_check", to: "end", label: "done" },
      { from: "spawn", to: "subloop" },
      { from: "subloop", to: "summary" },
      { from: "summary", to: "append" },
      { from: "tool", to: "append" },
      { from: "append", to: "parent" },
    ],
  },
  s07: {
    nodes: [
      { id: "start", label: "Startup", type: "start", x: COL_CENTER, y: 30 },
      { id: "scan", label: "Scan skills/", type: "process", x: COL_CENTER, y: 120 },
      { id: "catalog", label: "Inject Catalog\nOnly", type: "process", x: COL_CENTER, y: 210 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 310 },
      { id: "need", label: "load_skill?", type: "decision", x: COL_CENTER, y: 400 },
      { id: "read", label: "Read SKILL.md", type: "subprocess", x: COL_LEFT, y: 500 },
      { id: "inject", label: "Tool Result\nFull Skill", type: "process", x: COL_LEFT, y: 590 },
      { id: "other", label: "Other Tool", type: "subprocess", x: COL_RIGHT, y: 500 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 690 },
    ],
    edges: [
      { from: "start", to: "scan" },
      { from: "scan", to: "catalog" },
      { from: "catalog", to: "llm" },
      { from: "llm", to: "need" },
      { from: "need", to: "read", label: "yes" },
      { from: "need", to: "other", label: "no" },
      { from: "read", to: "inject" },
      { from: "inject", to: "append" },
      { from: "other", to: "append" },
      { from: "append", to: "llm" },
    ],
  },
  s08: {
    nodes: [
      { id: "start", label: "messages[]", type: "start", x: COL_CENTER, y: 30 },
      { id: "budget", label: "Tool Result\nBudget", type: "process", x: COL_CENTER, y: 120 },
      { id: "snip", label: "Snip Compact", type: "process", x: COL_CENTER, y: 210 },
      { id: "micro", label: "Micro Compact", type: "process", x: COL_CENTER, y: 300 },
      { id: "threshold", label: "over limit?", type: "decision", x: COL_CENTER, y: 390 },
      { id: "summary", label: "LLM Summary\nCompact", type: "subprocess", x: COL_LEFT, y: 500 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 610 },
      { id: "too_long", label: "prompt_too_long?", type: "decision", x: COL_RIGHT, y: 610 },
      { id: "reactive", label: "Reactive Compact", type: "subprocess", x: COL_LEFT, y: 720 },
    ],
    edges: [
      { from: "start", to: "budget" },
      { from: "budget", to: "snip" },
      { from: "snip", to: "micro" },
      { from: "micro", to: "threshold" },
      { from: "threshold", to: "summary", label: "yes" },
      { from: "threshold", to: "llm", label: "no" },
      { from: "summary", to: "llm" },
      { from: "llm", to: "too_long" },
      { from: "too_long", to: "reactive", label: "yes" },
      { from: "reactive", to: "llm" },
    ],
  },
  s09: {
    nodes: [
      { id: "start", label: "Session Start", type: "start", x: COL_CENTER, y: 30 },
      { id: "index", label: "Load MEMORY.md\nIndex", type: "process", x: COL_CENTER, y: 120 },
      { id: "select", label: "Select Relevant\nMemory Files", type: "process", x: COL_CENTER, y: 220 },
      { id: "inject", label: "Inject Memory\nContent", type: "process", x: COL_CENTER, y: 320 },
      { id: "compact", label: "Compact Pipeline", type: "subprocess", x: COL_CENTER, y: 420 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 520 },
      { id: "extract", label: "Extract New\nMemories", type: "subprocess", x: COL_LEFT, y: 630 },
      { id: "write", label: "Write .memory\nFiles", type: "process", x: COL_LEFT, y: 720 },
      { id: "dream", label: "Periodic\nConsolidate", type: "process", x: COL_RIGHT, y: 720 },
    ],
    edges: [
      { from: "start", to: "index" },
      { from: "index", to: "select" },
      { from: "select", to: "inject" },
      { from: "inject", to: "compact" },
      { from: "compact", to: "llm" },
      { from: "llm", to: "extract" },
      { from: "extract", to: "write" },
      { from: "write", to: "index" },
      { from: "write", to: "dream" },
    ],
  },
  s10: {
    nodes: [
      { id: "start", label: "Runtime State", type: "start", x: COL_CENTER, y: 30 },
      { id: "sections", label: "PROMPT_SECTIONS", type: "process", x: COL_CENTER, y: 120 },
      { id: "context", label: "Build Context\nmemory/tools/workspace", type: "process", x: COL_CENTER, y: 220 },
      { id: "cache", label: "Cache Hit?", type: "decision", x: COL_CENTER, y: 320 },
      { id: "reuse", label: "Reuse Prompt", type: "process", x: COL_RIGHT, y: 420 },
      { id: "assemble", label: "Assemble Prompt", type: "subprocess", x: COL_LEFT, y: 420 },
      { id: "llm", label: "LLM Call", type: "process", x: COL_CENTER, y: 540 },
      { id: "loop", label: "Tool Loop", type: "subprocess", x: COL_CENTER, y: 640 },
    ],
    edges: [
      { from: "start", to: "sections" },
      { from: "sections", to: "context" },
      { from: "context", to: "cache" },
      { from: "cache", to: "reuse", label: "yes" },
      { from: "cache", to: "assemble", label: "no" },
      { from: "reuse", to: "llm" },
      { from: "assemble", to: "llm" },
      { from: "llm", to: "loop" },
      { from: "loop", to: "context" },
    ],
  },
  s11: {
    nodes: [
      { id: "start", label: "LLM Request", type: "start", x: COL_CENTER, y: 30 },
      { id: "try", label: "try LLM Call", type: "process", x: COL_CENTER, y: 120 },
      { id: "ok", label: "success?", type: "decision", x: COL_CENTER, y: 220 },
      { id: "tools", label: "Execute Tools", type: "process", x: COL_RIGHT, y: 330 },
      { id: "classify", label: "Classify Error", type: "decision", x: COL_LEFT, y: 330 },
      { id: "tokens", label: "max_tokens\nEscalate", type: "subprocess", x: 40, y: 440 },
      { id: "prompt", label: "prompt_too_long\nCompact", type: "subprocess", x: COL_LEFT, y: 610 },
      { id: "backoff", label: "429 / 529\nBackoff", type: "subprocess", x: COL_LEFT + 140, y: 440 },
      { id: "fallback", label: "Fallback Model", type: "process", x: COL_RIGHT, y: 540 },
      { id: "retry", label: "Retry Request", type: "process", x: COL_CENTER, y: 740 },
    ],
    edges: [
      { from: "start", to: "try" },
      { from: "try", to: "ok" },
      { from: "ok", to: "tools", label: "yes" },
      { from: "ok", to: "classify", label: "error" },
      { from: "classify", to: "tokens", label: "max_tokens" },
      { from: "classify", to: "prompt", label: "too long" },
      { from: "classify", to: "backoff", label: "429/529" },
      { from: "backoff", to: "fallback", label: "repeated 529" },
      { from: "tokens", to: "retry" },
      { from: "prompt", to: "retry" },
      { from: "backoff", to: "retry" },
      { from: "fallback", to: "retry" },
      { from: "retry", to: "try" },
    ],
  },
  s12: {
    nodes: [
      { id: "start", label: "User Goal", type: "start", x: COL_CENTER, y: 30 },
      { id: "create", label: "create_task", type: "subprocess", x: COL_CENTER, y: 120 },
      { id: "save", label: "Persist JSON\n.tasks/", type: "process", x: COL_CENTER, y: 210 },
      { id: "list", label: "list / get", type: "subprocess", x: COL_RIGHT, y: 300 },
      { id: "deps", label: "blockedBy\ncomplete?", type: "decision", x: COL_CENTER, y: 390 },
      { id: "blocked", label: "Remain Pending", type: "end", x: COL_RIGHT, y: 490 },
      { id: "claim", label: "claim_task\nowner + in_progress", type: "subprocess", x: COL_LEFT, y: 490 },
      { id: "complete", label: "complete_task", type: "subprocess", x: COL_LEFT, y: 590 },
      { id: "unblock", label: "Report\nUnblocked", type: "process", x: COL_CENTER, y: 690 },
      { id: "append", label: "Append Result", type: "process", x: COL_CENTER, y: 780 },
    ],
    edges: [
      { from: "start", to: "create" },
      { from: "create", to: "save" },
      { from: "save", to: "list" },
      { from: "save", to: "deps" },
      { from: "deps", to: "blocked", label: "no" },
      { from: "deps", to: "claim", label: "yes" },
      { from: "claim", to: "complete" },
      { from: "complete", to: "unblock" },
      { from: "unblock", to: "append" },
      { from: "append", to: "list" },
    ],
  },
};

export function getFlowForVersion(version: string): FlowDefinition | null {
  return CURRENT_FLOW_OVERRIDES[version] ?? EXECUTION_FLOWS[version] ?? null;
}
