#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11,<3.12"
# dependencies = ["pydantic>=2", "pyyaml", "sc-compose>=1.7.0,<2"]
# ///
# pyright: strict
"""Split one dev sanity check into one assignment per numbered deliverable.

Usage:
  sanity-split --task <sanity bead> --bead <dev bead> --worktree <path>
               --branch <branch> --commit <commit> --base <base>
               --lint-command '<cmd>' --scratch <dir> [--layer-pr <n> ...]
               [--bead-json <file>] [--split-only] [--template <path>]
               [--lint-timeout-seconds <n>]
  sanity-split lint-supervisor --worktree <path> --command '<cmd>' --log <file>
               --exit-file <file> [--timeout-seconds <n>]

Reads the checked bead (`bd show <bead> --json`, or `--bead-json`), parses the
numbered list under `## Deliverables` (a poured bead without one, or without
`owned_paths`, takes it from its sprint container `metadata.sprint_bead`), pins the target commit, lists the files
changed since the base (with `--layer-pr`, the union of those PRs' own ranges,
one `gh api repos/{owner}/{repo}/pulls --paginate` listing joined locally, the checked PR last), renders `dev-sanity-assignment.json.j2` once per
deliverable with the sc-compose wheel (`external_evidence`: the checked bead's notes when it has any,
and the checked PR's body with `--layer-pr`, then each `--external-evidence <json-or-file>`, repeatable: one
well-formed reference such as a `pr_comment`), starts the lint command detached in the
worktree, and prints one JSON manifest to stdout:

  {run_id, reviewers, operational_reviewer, sha, base_sha, sanity_bead, dev_bead, branch, worktree_path,
   deliverables_total, changed_files, files_outside_owned_paths,
   lint: {command, log, exit_file, pid, timeout_seconds} | null,
   assignments: [{number, assignment: <rendered object>}]}

`--split-only` parses and renders only: no git pinning and no lint.

The lint command runs under `lint-supervisor`, a detached process (its pid is
`lint.pid`) that runs the command in a process group of its own, redirects the
command's stdout and stderr to the log, waits at most `timeout_seconds`, and
always writes the exit file: the exit code, or `timeout` (the command's group
was killed), `cancelled` (the supervisor received SIGTERM or SIGINT) or `error`.
On timeout and on cancellation the supervisor sends SIGTERM to the command's
whole process group, waits up to 2 seconds, SIGKILLs every survivor and
confirms the group is empty before writing; a group that still survives is
written as `error`. The exit file appears atomically, complete or not at all.
`kill -- -<pid>` or `kill <pid>` stops the supervisor and the lint command
with all its descendants.

`## Deliverables` must be one top-level numbered list `1.` .. `X.`; wrapped
lines, indented sub-bullets and blank lines belong to the item above them,
fenced blocks (CommonMark: closed only by a line of the same fence character
repeated at least the opening length) are text even when they contain headings
or numbered lines, and anything else in the section, sub-headings included, is
PLAN_INVALID. The section ends at the first `#` or `##` heading outside a fence.

Exit codes (the code and message are printed on stderr):
  0  ok
  1  usage
  2  SANITY.PLAN_INVALID      no numbered `## Deliverables` list (planning failed)
  3  SANITY.TARGET_UNREADABLE worktree, commit, base or remote unreadable
  4  SANITY.COMMIT_MISMATCH   HEAD, branch, dirty tree, or branch not pushed at SHA
  5  RENDER_FAILED            sc-compose could not render an assignment
"""
import argparse
from dataclasses import dataclass
import json
import os
from pathlib import Path
import re
import signal
import subprocess
import sys
import time
from types import FrameType
from typing import NoReturn, TypedDict
import uuid

# Tool lock and log files a clean worktree may hold; the same as `IGNORED_STATUS_PATHS` in assignment-gates.py.
IGNORED_STATUS_PATHS = (".beads.gate.lock", ".sc-compose/")

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parents[1] / "atm-beads" / "scripts"))
sys.path.insert(0, str(HERE))
import sc_render  # noqa: E402
from jev_client import evidence_ref  # noqa: E402
from operation_models import matches_typeddict  # noqa: E402
from sprint_index_common import JsonObject, JsonValue, is_json_object, is_json_value  # noqa: E402

TEMPLATE = HERE.parent / "templates" / "dev-sanity-assignment.json.j2"
CODES = {2: "SANITY.PLAN_INVALID", 3: "SANITY.TARGET_UNREADABLE", 4: "SANITY.COMMIT_MISMATCH", 5: "RENDER_FAILED"}


class Fail(Exception):
    def __init__(self, code: int, message: str) -> None:
        super().__init__(message)
        self.code = code


class SplitArguments(argparse.Namespace):
    task: str
    bead: str
    worktree: str
    branch: str
    commit: str
    base: str
    lint_command: str
    scratch: str
    bead_json: str | None
    close_context: Path | None
    layer_pr: list[str]
    external_evidence: list[str]
    split_only: bool
    template: Path
    lint_timeout_seconds: int


class SupervisorArguments(argparse.Namespace):
    worktree: str
    command: str
    log: str
    exit_file: str
    timeout_seconds: float


class LintRun(TypedDict):
    command: str
    log: str
    exit_file: str
    pid: int
    timeout_seconds: int


def die(code: int, message: str) -> int:
    print(f"{CODES.get(code, 'USAGE')}: {message}", file=sys.stderr)
    return code


def extra_evidence(values: list[str]) -> list[JsonObject]:
    """The `--external-evidence` entries: each value is one JSON evidence reference, or a path to a file holding one."""
    entries: list[JsonObject] = []
    for value in values:
        text = value
        if not value.lstrip().startswith("{"):
            try:
                text = Path(value).read_text()
            except OSError as error:
                raise Fail(1, f"--external-evidence {value!r} is not JSON and cannot be read as a file: {error}") from error
        try:
            entry = json.loads(text)
        except ValueError as error:
            raise Fail(1, f"--external-evidence {value!r} is not valid JSON: {error}") from error
        if not is_json_object(entry) or not evidence_ref(entry):
            raise Fail(1, f"--external-evidence {value!r} is not a bead_notes, pr_body or pr_comment reference")
        entries.append(entry)
    return entries


@dataclass(frozen=True)
class ParsedBead:
    raw: JsonObject
    id: str
    title: str
    description: str
    notes: str
    sprint: str | None
    owned_paths: list[str]


class BeadText(TypedDict):
    description: str
    title: str
    notes: str


class BeadOwnedPaths(TypedDict):
    owned_paths: list[str]


class BeadSprint(TypedDict):
    sprint_bead: str | None


def parse_bead(data: JsonObject, bead_id: str) -> ParsedBead:
    metadata = data.get("metadata") or {}
    if not is_json_object(metadata):
        raise Fail(3, f"bead {bead_id} metadata must be an object")
    owned = {"owned_paths": metadata.get("owned_paths") or []}
    if not matches_typeddict(owned, BeadOwnedPaths):
        raise Fail(3, f"bead {bead_id} metadata.owned_paths must be a list of strings")
    sprint = {"sprint_bead": metadata.get("sprint_bead")}
    if not matches_typeddict(sprint, BeadSprint):
        raise Fail(3, f"bead {bead_id} metadata.sprint_bead must be a string")
    fields = {key: data.get(key) or "" for key in BeadText.__required_keys__}
    if not matches_typeddict(fields, BeadText):
        raise Fail(3, f"bead {bead_id} title, description, and notes must be strings")
    return ParsedBead(data, bead_id, fields["title"], fields["description"], fields["notes"],
                      sprint["sprint_bead"], owned["owned_paths"])


def load_bead(args: SplitArguments, bead_id: str | None = None) -> ParsedBead:
    """One bead by id: `bd show <id> --json`, or the entry with that id in `--bead-json` (a bead or a list)."""
    bead_id = bead_id or args.bead
    if args.bead_json:
        try:
            data = json.loads(Path(args.bead_json).read_text())
        except (OSError, ValueError) as exc:
            raise Fail(1, f"cannot read --bead-json: {exc}")
    else:
        run = subprocess.run(["bd", "show", bead_id, "--json"], capture_output=True, text=True)
        if run.returncode != 0:
            raise Fail(3, f"bd show {bead_id} failed: {run.stderr.strip()}")
        try:
            data = json.loads(run.stdout)
        except ValueError:
            raise Fail(3, f"bd show {bead_id} did not print JSON")
    if not is_json_value(data):
        raise Fail(3, f"bead {bead_id} JSON contains an invalid value")
    if isinstance(data, list):
        data = next((item for item in data if isinstance(item, dict) and item.get("id") == bead_id), None)
    if not is_json_object(data) or data.get("id") != bead_id:
        raise Fail(3, f"bead {bead_id} not found in bead JSON")
    return parse_bead(data, bead_id)


def plan_source(args: SplitArguments, bead: ParsedBead) -> ParsedBead:
    """The bead holding the plan: the checked bead, or for a poured dev bead without `## Deliverables`
    (or without owned_paths) its sprint container `metadata.sprint_bead`."""
    if bead.sprint and not (DELIVERABLES.search(bead.description) and bead.owned_paths):
        return load_bead(args, bead.sprint)
    return bead


ITEM = re.compile(r"^(\d+)\.\s+(\S.*)$")
TOP_BULLET = re.compile(r"^[-*+]\s")
FENCE = re.compile(r"^\s*(`{3,}|~{3,})")
HEADING = re.compile(r"^(#{1,6}) ")


DELIVERABLES = re.compile(r"^## Deliverables[ \t]*$", re.M)


class Deliverable(TypedDict):
    number: int
    text: str


def parse_deliverables(description: str) -> list[Deliverable]:
    match = DELIVERABLES.search(description or "")
    if not match:
        raise Fail(2, "bead has no `## Deliverables` section")
    items: list[Deliverable] = []
    fence: str | None = None
    prev_blank = True
    for line in description[match.end():].splitlines():
        stripped = line.strip()
        if fence:                                   # fenced text belongs to the current item
            items[-1]["text"] += " " + stripped
            if re.fullmatch(re.escape(fence[0]) + "{%d,}" % len(fence), stripped):
                fence = None                        # same character, at least the opening length
            continue
        heading = HEADING.match(line)
        if heading:
            if len(heading.group(1)) <= 2:
                break                               # the section ends at the next `#`/`##` heading
            raise Fail(2, f"`## Deliverables` has a sub-heading, which is not part of a numbered list: {stripped!r}")
        if not stripped:
            prev_blank = True
            continue
        item = ITEM.match(line)
        if item:
            items.append({"number": int(item.group(1)), "text": item.group(2).strip()})
        elif not items:
            raise Fail(2, f"`## Deliverables` has content before the first numbered item: {stripped!r}")
        elif TOP_BULLET.match(line):
            raise Fail(2, f"`## Deliverables` has an unnumbered top-level bullet: {stripped!r}")
        elif FENCE.match(line):
            fence_match = FENCE.match(line)
            assert fence_match is not None
            fence = fence_match.group(1)       # closes on the same character, at least this long
            items[-1]["text"] += " " + stripped
        elif line[0] in " \t" or not prev_blank:    # indented sub-item, or wrapped item text
            items[-1]["text"] += " " + stripped
        else:
            raise Fail(2, f"`## Deliverables` has an unnumbered paragraph: {stripped!r}")
        prev_blank = False
    if fence:
        raise Fail(2, "`## Deliverables` has an unclosed fenced block")
    if not items:
        raise Fail(2, "`## Deliverables` has no numbered items (`1. ...`)")
    numbers = [item["number"] for item in items]
    if numbers != list(range(1, len(items) + 1)):
        raise Fail(2, f"`## Deliverables` numbering is not 1..{len(items)}: {numbers}")
    return items


def git(worktree: str, *argv: str, code: int = 3, what: str | None = None) -> str:
    try:
        run = subprocess.run(["git", "-C", worktree, *argv], capture_output=True, text=True)
    except OSError as exc:
        raise Fail(3, f"git unavailable: {exc}")
    if run.returncode != 0:
        raise Fail(code, what or f"git {' '.join(argv)} failed: {run.stderr.strip()}")
    return run.stdout


def pin(args: SplitArguments) -> tuple[str, str]:
    if not Path(args.worktree).is_dir():
        raise Fail(3, f"worktree {args.worktree} is not a directory")
    sha = git(args.worktree, "rev-parse", "--verify", "--quiet", f"{args.commit}^{{commit}}",
              what=f"commit {args.commit} is not in {args.worktree}").strip()
    base_sha = git(args.worktree, "rev-parse", "--verify", "--quiet", f"origin/{args.base}^{{commit}}",
                   what=f"origin/{args.base} is not in {args.worktree}").strip()
    head = git(args.worktree, "rev-parse", "HEAD").strip()
    if head != sha:
        raise Fail(4, f"HEAD is {head}, not {sha}")
    branch = git(args.worktree, "rev-parse", "--abbrev-ref", "HEAD").strip()
    if branch != args.branch:
        raise Fail(4, f"checked-out branch is {branch}, not {args.branch}")
    if any(line and not any(path in line for path in IGNORED_STATUS_PATHS)
           for line in git(args.worktree, "status", "--porcelain").splitlines()):
        raise Fail(4, "worktree has uncommitted or untracked changes")
    remote = git(args.worktree, "ls-remote", "origin", f"refs/heads/{args.branch}",
                 what=f"cannot read origin refs/heads/{args.branch}")
    remote_sha = remote.split()[0] if remote.split() else ""
    if remote_sha != sha:
        raise Fail(4, f"origin/{args.branch} is {remote_sha or 'absent'}, not {sha}")
    return sha, base_sha


PULLS = ("gh", "api", "repos/{owner}/{repo}/pulls?state=all&per_page=100", "--paginate",
         "--jq", ".[] | {number: .number, base: .base.sha, head: .head.sha}")


class PullRequestRange(TypedDict):
    number: str
    base: str
    head: str


def parse_pull_request_range(value: JsonValue) -> PullRequestRange:
    if not is_json_object(value):
        raise TypeError("pull request row must be a JSON object")
    return {"number": str(value["number"]), "base": str(value["base"]), "head": str(value["head"])}


def layer_ranges(worktree: str, prs: list[str]) -> list[tuple[str, str]]:
    """Each stack layer's own range, (base.sha, head.sha), from one paginated pulls listing joined locally."""
    try:
        run = subprocess.run(PULLS, capture_output=True, text=True, cwd=worktree)
    except OSError as exc:
        raise Fail(3, f"gh unavailable: {exc}")
    if run.returncode != 0:
        raise Fail(3, f"gh api pulls failed: {run.stderr.strip()}")
    listed: dict[str, tuple[str, str]] = {}
    line = ""
    try:
        for line in run.stdout.splitlines():
            if line.strip():
                row = parse_pull_request_range(json.loads(line))
                listed[row["number"]] = (row["base"], row["head"])
    except (ValueError, KeyError, TypeError):
        raise Fail(3, f"gh api pulls returned a row without number/base.sha/head.sha: {line[:200]}")
    missing = [pr for pr in prs if pr not in listed]
    if missing:
        raise Fail(3, f"layer PR(s) {', '.join(missing)} not in the repository's pull request listing")
    return [listed[pr] for pr in prs]


def repository_slug(worktree: str) -> str:
    """The checkout's `owner/repo`, read from origin, so a pr_body reference names the repository instead of gh's placeholder."""
    url = git(worktree, "remote", "get-url", "origin", what="cannot read the origin remote URL")
    match = re.search(r"[:/]([^/:]+)/([^/]+?)(?:\.git)?/?$", url.strip())
    if not match:
        raise Fail(1, f"origin does not name owner/repository: {url.strip()!r}")
    return f"{match.group(1)}/{match.group(2)}"


def layer_files(worktree: str, prs: list[str], sha: str) -> list[str]:
    """The union of the changed files of each layer PR's own range; the last (checked) PR's head must be the pinned sha."""
    ranges = layer_ranges(worktree, prs)
    if ranges[-1][1] != sha:
        raise Fail(4, f"PR {prs[-1]} head is {ranges[-1][1]}, not {sha}")
    return sorted({path for base, head in ranges for path in changed_files(worktree, base, head)})


def changed_files(worktree: str, base_sha: str, sha: str) -> list[str]:
    out = git(worktree, "diff", "--name-status", "-z", "--find-renames", f"{base_sha}...{sha}")
    fields = out.split("\0")
    files: list[str] = []
    i = 0
    while i < len(fields) and fields[i]:
        status = fields[i]
        if status[0] in "RC":                       # rename/copy: source, then destination
            files.extend(fields[i + 1:i + 3])
            i += 3
        else:
            files.append(fields[i + 1])
            i += 2
    return sorted(set(files))


def glob_regex(pattern: str) -> re.Pattern[str]:
    out, i = "", 0
    while i < len(pattern):
        c = pattern[i]
        if pattern.startswith("**/", i):
            out += "(?:.*/)?"
            i += 3
            continue
        if pattern.startswith("**", i):
            out += ".*"
            i += 2
            continue
        out += "[^/]*" if c == "*" else "[^/]" if c == "?" else re.escape(c)
        i += 1
    return re.compile(f"^{out}$")


def outside_fence(files: list[str], owned_paths: list[str]) -> list[str]:
    if not owned_paths:
        return []
    fences = [glob_regex(p) for p in owned_paths]
    return [f for f in files if not any(r.match(f) for r in fences)]


def render(template: Path, variables: JsonObject) -> JsonObject:
    try:
        text = sc_render.render(template, variables)
    except sc_render.RenderError as exc:
        raise Fail(5, f"sc-compose render failed for deliverable {variables['deliverable_number']}: "
                      f"{sc_render.error_text(exc).splitlines()[0]}")
    try:
        result = json.loads(text)
        if not is_json_object(result):
            raise ValueError("rendered assignment must be an object")
        return result
    except ValueError:
        raise Fail(5, f"rendered assignment {variables['deliverable_number']} is not JSON")


def start_lint(args: SplitArguments) -> LintRun:
    scratch = Path(args.scratch)
    scratch.mkdir(parents=True, exist_ok=True)
    log = scratch / f"{args.task}-lint.log"
    exit_file = scratch / f"{args.task}-lint.exit"
    for stale in (log, exit_file):
        if stale.exists():
            stale.unlink()
    proc = subprocess.Popen(
        [sys.executable, str(Path(__file__).resolve()), "lint-supervisor", "--worktree", args.worktree,
         "--command", args.lint_command, "--log", str(log), "--exit-file", str(exit_file),
         "--timeout-seconds", str(args.lint_timeout_seconds)],
        start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    return {"command": args.lint_command, "log": str(log), "exit_file": str(exit_file),
            "pid": proc.pid, "timeout_seconds": args.lint_timeout_seconds}


GRACE_SECONDS = 2
KILL_WAIT_SECONDS = 5


class Cancelled(Exception):
    pass


def stop_group(pgid: int, child: subprocess.Popen[bytes]) -> bool:
    """TERM the lint process group, wait the grace period, KILL the survivors; True once the group is empty."""
    def empty() -> bool:
        # Reap the direct child so it does not linger as a zombie. Not child.poll(): a Cancelled raised
        # just after Popen.wait acquired its waitpid lock leaves that lock held, and poll() then never reaps.
        try:
            os.waitpid(child.pid, os.WNOHANG)
        except ChildProcessError:                   # already reaped
            pass
        try:
            os.killpg(pgid, 0)
        except ProcessLookupError:
            return True
        except PermissionError:                     # macOS: a member is still exiting; poll again
            pass
        return False

    def signal_group(sig: signal.Signals) -> None:
        try:
            os.killpg(pgid, sig)
        except (ProcessLookupError, PermissionError):   # macOS: EPERM while only exiting members remain
            pass

    def wait_empty(seconds: float) -> bool:
        deadline = time.monotonic() + seconds
        while not empty():
            if time.monotonic() > deadline:
                return False
            time.sleep(0.05)
        return True

    signal_group(signal.SIGTERM)
    if wait_empty(GRACE_SECONDS):
        return True
    signal_group(signal.SIGKILL)
    return wait_empty(KILL_WAIT_SECONDS)


def supervise(argv: list[str]) -> int:
    """Run the lint command in its own process group; always write the exit file."""
    parser = argparse.ArgumentParser(prog="sanity-split lint-supervisor")
    for flag in ("--worktree", "--command", "--log", "--exit-file"):
        parser.add_argument(flag, required=True)
    parser.add_argument("--timeout-seconds", type=float, default=1800)
    args = parser.parse_args(argv, namespace=SupervisorArguments())
    exit_file = Path(args.exit_file)
    outcome, child = "error", None

    def cancel(signum: int, frame: FrameType | None) -> NoReturn:
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        raise Cancelled()

    signal.signal(signal.SIGTERM, cancel)
    signal.signal(signal.SIGINT, cancel)
    try:
        with open(args.log, "w") as log:
            child = subprocess.Popen(["sh", "-c", args.command], cwd=args.worktree, stdin=subprocess.DEVNULL,
                                     stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
            try:
                outcome = str(child.wait(timeout=args.timeout_seconds))
            except subprocess.TimeoutExpired:
                outcome = "timeout" if stop_group(child.pid, child) else "error"
    except Cancelled:
        outcome = "cancelled" if child is None or stop_group(child.pid, child) else "error"
    except Exception:
        outcome = "error"
        if child is not None:
            stop_group(child.pid, child)
    finally:                                        # written whole, then renamed: a reader never sees it partial
        partial = exit_file.with_name(exit_file.name + ".tmp")
        partial.write_text(outcome + "\n")
        os.replace(partial, exit_file)
    return 0 if outcome not in ("cancelled", "error") else 1


def main() -> int:
    parser = argparse.ArgumentParser(add_help=True)
    for flag in ("--task", "--bead", "--worktree", "--branch", "--commit", "--base", "--lint-command", "--scratch"):
        parser.add_argument(flag, required=True)
    parser.add_argument("--bead-json")
    parser.add_argument("--close-context", type=Path)
    parser.add_argument("--layer-pr", action="append", default=[])
    parser.add_argument("--external-evidence", action="append", default=[])
    parser.add_argument("--split-only", action="store_true")
    parser.add_argument("--template", type=Path, default=TEMPLATE)
    parser.add_argument("--lint-timeout-seconds", type=int, default=1800)
    try:
        args = parser.parse_args(namespace=SplitArguments())
    except SystemExit:
        return 1
    try:
        bead = load_bead(args)
        source = plan_source(args, bead)
        own = bead.description
        deliverables = parse_deliverables(own if DELIVERABLES.search(own) else source.description)
        owned = bead.owned_paths or source.owned_paths
        if args.split_only:
            sha, base_sha, files, lint = args.commit, args.base, [], None
        else:
            sha, base_sha = pin(args)
            files = layer_files(args.worktree, args.layer_pr, sha) if args.layer_pr else changed_files(args.worktree, base_sha, sha)
            lint = None
        outside = outside_fence(files, owned)
        try:
            close_context: JsonValue = json.loads(args.close_context.read_text()) if args.close_context else {}
        except (OSError, ValueError) as error:
            raise Fail(1, f"cannot read --close-context: {error}") from error
        if not is_json_object(close_context):
            raise Fail(1, "close context must be a JSON object")
        external: list[JsonObject] = []
        if bead.notes:
            external.append({"kind": "bead_notes", "bead": bead.id, "text": bead.notes})
        if args.layer_pr and args.layer_pr[-1].isdigit():
            external.append({"kind": "pr_body", "repo": repository_slug(args.worktree), "pr": int(args.layer_pr[-1])})
        external += extra_evidence(args.external_evidence)
        assignments: list[JsonObject] = []
        for item in deliverables:
            variables: JsonObject = {
                "sanity_bead": args.task, "dev_bead_id": bead.id, "dev_bead_title": bead.title,
                "deliverable_number": item["number"], "deliverable_text": item["text"],
                "deliverables_total": len(deliverables),
                "owned_paths": [*owned], "changed_files": [*files], "files_outside_owned_paths": [*outside],
                "context": [], "external_evidence": [*external],
                "worktree_path": args.worktree, "branch": args.branch, "commit": sha, "base_sha": base_sha,
            }
            assignments.append({"number": item["number"], "assignment": render(args.template, variables)})
        if not args.split_only:
            lint = start_lint(args)
    except Fail as exc:
        return die(exc.code, str(exc))
    json.dump({"run_id": str(uuid.uuid4()),
               "reviewers": ["sanity-llm", "sanity-jev", "sanity-selected"],
               "operational_reviewer": "sanity-selected",
               "sha": sha, "base_sha": base_sha, "sanity_bead": args.task, "dev_bead": bead.id,
               "branch": args.branch, "worktree_path": args.worktree, "deliverables_total": len(deliverables),
               "changed_files": files, "files_outside_owned_paths": outside, "lint": lint,
               "close_context": close_context, "bead_notes": bead.notes,
               "checked_bead_data": bead.raw,
               "assignments": assignments}, sys.stdout, indent=2)
    print()
    return 0


if __name__ == "__main__":
    if sys.argv[1:2] == ["lint-supervisor"]:
        sys.exit(supervise(sys.argv[2:]))
    sys.exit(main())
