"""Durable subagent records in ``state.db`` (``resumable_subagents``): what ``delegate_task(action='continue')``
rebuilds a child from after a restart or crash, how a stopped child may resume, and which worktrees the startup
sweep must keep for it.

One row per subagent_id, written by the run itself (``record_start`` when its workspace is seeded, ``checkpoint`` on
each heartbeat while its terminal cwd moves, ``record_stop`` after its cleanup ran) and by boot recovery for a run
whose owner process died (``recover_abandoned``). The row's ``pinned`` flag is the worktree pin: set for a child that
did not complete and was not stopped by a person, for ``delegation.resumable_ttl_hours`` (newest
``delegation.max_resumable`` per profile; the oldest beyond the cap loses its pin, the row stays). Automatic
resumption is ``delegate_tool_auto_resume`` (gated on ``delegation.auto_resume``), which reads ``resume_mode`` /
``next_attempt_at`` and marks a record ``superseded`` when its parent moved on.
"""

from __future__ import annotations

import hashlib
import json
import logging
import os
import subprocess
import threading
import time
from pathlib import Path
from typing import Any, Iterable, Optional

logger = logging.getLogger("tools.delegate_tool")  # log-record parity with the origin module

AUTO_AT, AUTO_ON_BOOT, ON_FIX = "AUTO_AT", "AUTO_ON_BOOT", "ON_FIX"
REPAIR_THEN_CONTINUE, HANDOFF = "REPAIR_THEN_CONTINUE", "HANDOFF"
GATEWAY_SHUTDOWN = "gateway_shutdown"
# A record ``action='continue'`` can rebuild from (``claimed`` while a continue is starting, ``running`` live).
_FINISHED = ("resumable", "completed", "stopped", "superseded")
_DEFAULT_TTL_HOURS, _DEFAULT_MAX_RESUMABLE = 48.0, 32

# DESIGN.md per-reason table (agent/error_classifier.py::FailoverReason): mode, and the default delay before an
# AUTO_AT attempt when the failure carried no reset time.
_MODE_BY_REASON: dict[str, tuple[str, Optional[float]]] = {
    "rate_limit": (AUTO_AT, 15 * 60.0), "upstream_rate_limit": (AUTO_AT, 15 * 60.0),
    "timeout": (AUTO_AT, 5 * 60.0), "overloaded": (AUTO_AT, 5 * 60.0), "server_error": (AUTO_AT, 5 * 60.0),
    "session_busy": (AUTO_AT, 30.0), "interpreter_shutdown": (AUTO_ON_BOOT, None),
    "content_policy_blocked": (HANDOFF, None),
    **dict.fromkeys(("context_overflow", "payload_too_large", "image_too_large", "image_corrupt",
                     "thinking_signature", "invalid_encrypted_content"), (REPAIR_THEN_CONTINUE, None)),
}


def _settings() -> tuple[float, int]:
    from tools.delegate_tool_config import _load_config
    cfg = _load_config()
    try:
        ttl = max(0.0, float(cfg.get("resumable_ttl_hours", _DEFAULT_TTL_HOURS)))
        cap = max(0, int(cfg.get("max_resumable", _DEFAULT_MAX_RESUMABLE)))
    except (TypeError, ValueError):
        ttl, cap = _DEFAULT_TTL_HOURS, _DEFAULT_MAX_RESUMABLE
    return ttl, cap


def classify(entry: dict[str, Any], stop_cause: Optional[str]) -> tuple[str, Optional[str], Optional[float]]:
    """``(state, resume_mode, next_attempt_at)`` for a stopped child's result entry. ``state`` is ``resumable``
    (pinned), ``completed`` or ``stopped`` (a person or the parent stopped it: no pin)."""
    status, exit_reason = entry.get("status"), entry.get("exit_reason")
    if status == "completed" and exit_reason == "completed":
        return "completed", None, None
    if status == "interrupted":
        return ("resumable", AUTO_ON_BOOT, None) if stop_cause == GATEWAY_SHUTDOWN else ("stopped", None, None)
    if status == "unknown":
        return "resumable", AUTO_ON_BOOT, None
    reason = str(entry.get("failure_reason") or "")
    resets_at = entry.get("failure_resets_at")
    if isinstance(resets_at, (int, float)) and not isinstance(resets_at, bool) and reason in (
            "billing", "rate_limit", "upstream_rate_limit"):
        return "resumable", AUTO_AT, float(resets_at)
    mode, delay = _MODE_BY_REASON.get(reason, (ON_FIX, None))
    return "resumable", mode, (time.time() + delay if delay else None)


# ── storage ────────────────────────────────────────────────────────────────
# A write computed from a record it read lands only while the row is unchanged; a row that moved is read again.
_CAS_TRIES = 8


def _db_path_for(parent_agent: Any = None) -> Any:
    """The profile store the parent's children write their transcripts to (its ``state.db`` path, or the scope of a
    storage handle), else this scope's profile; None in a managed worker, which owns no ledger."""
    from agent.runtime_session_store import is_worker_process
    if is_worker_process():
        return None
    from persistence_assembly import Persistence, profile_scope
    db = getattr(parent_agent, "_session_db", None)
    if isinstance(db, Persistence):
        return db.scope
    path = getattr(db, "db_path", None)
    if isinstance(path, (str, Path)):
        return Path(path)
    return profile_scope()


def _write(target: Any, fn) -> Any:
    """``fn(ledger)`` on the subagent records of *target*'s profile store (a ``state.db`` path or a profile scope);
    None when there is no store to write or the write failed."""
    if target is None:
        return None
    from persistence import ProfileScope
    from persistence_assembly import open_persistence, profile_scope
    scope = target if isinstance(target, ProfileScope) else profile_scope(home=Path(target).parent)
    try:
        with open_persistence(scope, eager=True) as store:
            return fn(store.coordination)
    except FileNotFoundError:  # its profile was removed: a late write must not recreate it
        logger.debug("resumable subagent record skipped: %s is gone", scope.profile_dir)
        return None
    except Exception:
        logger.warning("resumable subagent record write failed (%s)", target, exc_info=True)
        return None


def _as_record(got: Any) -> Optional[dict[str, Any]]:
    """The run record of a stored row, with the columns a claim moves without rewriting the record (state, owner)."""
    if got is None:
        return None
    return {**got.document, "state": got.state.value, "stop_cause": got.stop_cause, "owner_pid": got.owner_pid,
            "owner_started_at": got.owner_started_at}


def _row(ledger, sid: str) -> Optional[dict[str, Any]]:
    """The record, with the columns a claim moves without rewriting the JSON (state, owner) taken from the row."""
    return _as_record(ledger.get_subagent(subagent_id=sid))


def _limits():
    from persistence.authority.subagents import SubagentLimits
    ttl, cap = _settings()
    return SubagentLimits(ttl_hours=ttl, max_resumable=cap)


def _subagent_write(record: dict[str, Any], *, state: str, pinned: bool, expires_at: Optional[float],
                    stop_cause: Optional[str] = None):
    from persistence.authority.subagents import SubagentState, SubagentWrite
    # ``auto_claim`` / ``auto_cancel`` mark only a claim no run has started yet: a written run record never carries them.
    record = {k: v for k, v in record.items() if k not in ("state", "stop_cause", "auto_claim", "auto_cancel")}
    document = json.loads(json.dumps(record, ensure_ascii=False, default=str))
    return SubagentWrite(state=SubagentState(state), pinned=pinned, stop_cause=stop_cause,
                         owner_pid=record.get("owner_pid"), owner_started_at=record.get("owner_started_at"),
                         document=document, expires_at=expires_at)


def _upsert(ledger, sid: str, record: dict[str, Any], *, state: str, pinned: bool, expires_at: Optional[float],
            stop_cause: Optional[str] = None, expected: Any, limits: Any = None) -> bool:
    """Write the whole row while the stored one is still *expected* (None: no row yet); False when it moved."""
    return ledger.put_subagent(subagent_id=sid, write=_subagent_write(
        record, state=state, pinned=pinned, expires_at=expires_at, stop_cause=stop_cause), expected=expected,
        limits=limits)


def _apply_limits(ledger) -> None:
    """TTL un-pins and retires a record; beyond the cap the oldest pins drop (the row stays continuable)."""
    ledger.apply_subagent_limits(limits=_limits())


# ── worktrees ──────────────────────────────────────────────────────────────
def _git_lines(args: list[str], cwd: str) -> list[str]:
    from hermes_cli._subprocess_compat import noninteractive_repo_git_env
    env = noninteractive_repo_git_env(cwd)
    if env is None:
        return []
    try:
        res = subprocess.run(["git", *args], cwd=cwd, env=env, capture_output=True, text=True, encoding="utf-8",
                             errors="replace", timeout=10, stdin=subprocess.DEVNULL, check=False)
    except (OSError, subprocess.SubprocessError):
        return []
    return res.stdout.splitlines() if res.returncode == 0 else []


def _worktrees_of(cwd: Optional[str]) -> list[str]:
    """Every worktree of the repository *cwd* is in (``git worktree list``), or ``[]``."""
    if not cwd or not os.path.isdir(cwd):
        return []
    return [os.path.realpath(line[9:]) for line in _git_lines(["worktree", "list", "--porcelain"], cwd)
            if line.startswith("worktree ")]


def _toplevel(cwd: Optional[str]) -> Optional[str]:
    if not cwd or not os.path.isdir(cwd):
        return None
    lines = _git_lines(["rev-parse", "--show-toplevel"], cwd)
    return os.path.realpath(lines[0]) if lines else None


def _new_worktrees(cwd: Optional[str], baseline: Iterable[str]) -> list[str]:
    known = set(baseline)
    return [p for p in _worktrees_of(cwd) if p not in known] if known else []


def _pin_paths(record: dict[str, Any]) -> list[str]:
    """Where the child was when it STOPPED: its last terminal cwd's worktree, its isolation worktree, and every
    worktree a repository it worked in gained since it first entered it (a ``git worktree add`` it ran without
    cd-ing into it), wherever its cwd went afterwards. Each repository is compared with its own baseline."""
    cwd = record.get("cwd")
    paths = [p for p in (_toplevel(cwd), (record.get("worktree") or {}).get("path")) if p]
    paths += _new_worktrees(record.get("start_cwd") or cwd, record.get("worktrees_at_start") or ())
    for main, baseline in (record.get("repo_baselines") or {}).items():
        paths += _new_worktrees(main, baseline)
    return sorted({os.path.realpath(p) for p in paths})


_LIVE_PIN_TTL_S = 30.0
_live_pin_cache: dict[str, tuple[float, list[str]]] = {}
_live_pin_lock = threading.Lock()


def _live_pins(record: dict[str, Any], *, memo: bool = True) -> list[str]:
    """Pins of a ``running``/``claimed`` record: where the child is NOW, plus where its previous run stopped (a
    continue's stored ``pin_paths``); a cached set never hides the live location.

    The sweep asks again right before each tree it touches, so the git discovery behind the live part is memoized
    for a few seconds, keyed on every record field that drives it: a checkpointed move changes the key and is
    discovered at once, while the record itself is always read fresh. The only thing a cached answer can miss is a
    worktree created within the TTL, and a tree that new is never past the sweep's idle tier. Recovery and reclaim
    store their answer as the stopped record's pins, so they pass ``memo=False``."""
    if not memo:
        return sorted({*(record.get("pin_paths") or ()), *_pin_paths(record)})
    key = json.dumps([record.get(k) for k in ("cwd", "start_cwd", "worktrees_at_start", "repo_baselines")]
                     + [(record.get("worktree") or {}).get("path")], sort_keys=True, default=str)
    now = time.monotonic()
    with _live_pin_lock:
        hit = _live_pin_cache.get(key)
    if hit is None or now - hit[0] > _LIVE_PIN_TTL_S:
        hit = (now, _pin_paths(record))  # git runs outside the lock
        with _live_pin_lock:
            for stale in [k for k, (at, _p) in _live_pin_cache.items() if now - at > _LIVE_PIN_TTL_S]:
                del _live_pin_cache[stale]
            _live_pin_cache[key] = hit
    return sorted({*(record.get("pin_paths") or ()), *hit[1]})


def _note_repo(baselines: dict[str, list[str]], start_main: Optional[str], cwd: Optional[str]) -> None:
    """Record the baseline of the repository *cwd* is in when the child first enters it (the start repository's
    is ``worktrees_at_start``)."""
    trees = _worktrees_of(cwd)
    if trees and trees[0] != start_main and trees[0] not in baselines:  # the main worktree is always listed first
        baselines[trees[0]] = trees


# ── run lifecycle ──────────────────────────────────────────────────────────
def endpoint_identity(provider: Any, base_url: Any) -> tuple[Any, Optional[str]]:
    """``(provider, sha256 of the exact base_url)``: what a continue compares to refuse a changed backend. The URL
    itself is never stored (it can carry a key in its query or userinfo); the real one is re-resolved from the
    task's route at continue. Exact bytes, so any change to the URL still refuses."""
    return provider, (hashlib.sha256(str(base_url).encode("utf-8")).hexdigest() if base_url is not None else None)


def _owner_identity() -> dict[str, Any]:
    from gateway.status import get_process_start_time
    started = get_process_start_time(os.getpid())
    return {"owner_pid": os.getpid(), "owner_started_at": started}


def _spec(child: Any) -> Optional[dict[str, Any]]:
    """The rebuild spec without live objects or secrets: the route is kept as the request that re-routes it."""
    build = getattr(child, "_delegate_resume", None)
    session_id = getattr(child, "session_id", None)
    if not isinstance(build, dict) or not isinstance(session_id, str) or not session_id:
        return None
    route = build.get("route")
    return {"goal": build.get("goal"), "context": build.get("context"), "toolsets": build.get("toolsets"),
            "endpoint": list(build.get("endpoint") or ()), "model": getattr(child, "model", None),
            "session_id": session_id, "route_request": getattr(route, "request", None)}


def record_start(run: Any) -> None:
    """A child began (or resumed) running: durable identity, rebuild spec, owner and starting workspace. A write
    that fails stays pending on the run (``_flush``) and is retried, whole, at the next checkpoint and at the stop."""
    child = run.child
    spec = _spec(child)
    sid = run.subagent_id
    if spec is None or not sid:
        return
    path = _db_path_for(run.parent_agent)
    cwd = _child_cwd(run)
    from tools.delegate_tool_registry import _recent_subagents, _active_subagents_lock
    owners = {str(getattr(child, "_parent_session_id", "") or "") or str(getattr(run.parent_agent, "session_id", "")
                                                                         or "")}
    # The git probes run before the write transaction (seconds on a slow repo; no state.db lock held through them).
    earlier = _write(path, lambda ledger: _row(ledger, sid)) or {}
    start_cwd = earlier.get("start_cwd") or cwd
    start_trees = _worktrees_of(start_cwd)
    # ``lock`` serializes the start-record retries (heartbeat thread vs the stop path) with the terminal write, so a
    # retry still waiting on the busy timeout can never land after ``record_stop`` and flip the row back to running.
    # Who started this run: a parent's spawn, a parent's continue (no automatic resume after it), the scheduler.
    origin = getattr(child, "_delegate_run_origin", None)
    origin = origin if origin in ("continue", "auto") else "spawn"
    auto = {"auto_attempts": int(earlier.get("auto_attempts") or 0), "auto_off": earlier.get("auto_off")
            or ("the parent continued it itself" if origin == "continue" else None),
            # The automatic-resume deadline counts from the FIRST stop, across every resumed run.
            "auto_first_stopped_at": earlier.get("auto_first_stopped_at")}
    durable: dict[str, Any] = {"db": path, "cwd": cwd, "start_main": start_trees[0] if start_trees else None,
                               "baselines": dict(earlier.get("repo_baselines") or {}), "lock": threading.RLock(),
                               "auto": auto}
    _note_repo(durable["baselines"], durable["start_main"], cwd)

    def _do(ledger):
        for _ in range(_CAS_TRIES):
            stored = ledger.get_subagent(subagent_id=sid)
            prev = _as_record(stored) or {}
            with _active_subagents_lock:
                owners.update((_recent_subagents.get(sid) or {}).get("ancestor_owner_session_ids") or ())
            owners.update(prev.get("owners") or ())
            owners.update(_ancestor_sessions(child))
            owners.discard("")
            record = {**prev, **spec, **_owner_identity(), "subagent_id": sid,
                      "delegation_id": getattr(child, "_delegation_id", None), "owners": sorted(owners),
                      # The async unit whose ledger row reports this run (``<call>-k`` with independent completions).
                      "unit_id": getattr(child, "_delegation_unit_id", None),
                      "start_cwd": start_cwd, "cwd": durable["cwd"], "worktree": prev.get("worktree")
                      or run.worktree_info, "outcome": "running", "resume_mode": None,
                      # Kept across continues: a tree the child made in an earlier run is still its own.
                      "worktrees_at_start": prev.get("worktrees_at_start") or start_trees,
                      # A promise belongs to one stop: this run's own stop (or boot recovery) makes it afresh.
                      "run_origin": origin, "run_started_at": time.time(), "auto_promised": False, **auto}
            record["repo_baselines"] = durable["baselines"]
            # A running record pins too (its paths are resolved at sweep time): a crash before boot recovery ran
            # must not hand its tree to the sweep. Bounded by the same TTL.
            columns: dict[str, Any] = dict(state="running", pinned=True,
                                           expires_at=time.time() + _settings()[0] * 3600)
            if origin == "auto":  # the parent's last word: a stop, /stop or replacement since the claim cancels it
                from tools.delegate_tool_auto_resume import CANCELLED
                if stored is None:
                    return f"{CANCELLED}: the record moved (gone)"
                started = ledger.start_auto_resumed_run(subagent_id=sid, write=_subagent_write(record, **columns),
                                                        expected=stored, owner_pid=os.getpid())
                if started.cancelled:
                    return started.cancelled
                if started.written:
                    return True
            elif _upsert(ledger, sid, record, **columns, expected=stored):
                return True
        return None

    outcome = _write(path, _do)
    if isinstance(outcome, str):  # cancelled by the parent before it ran: no run record, the child does not start
        child._delegate_auto_cancelled = outcome
        child._delegate_claim = None  # the row is the parent's now: cleanup must not hand any claim back
        return
    if origin == "auto" and not outcome:
        # Fail closed: without its run record the parent's last word could not be checked, and a later retry would
        # check it while the child already runs. It does not start; cleanup hands the claim back to ``resumable``.
        child._delegate_auto_cancelled = "Not continued: its run record could not be written"
        return
    durable["pending"] = None if outcome else _do
    child._delegate_durable = durable


def _flush(durable: dict[str, Any]) -> bool:
    """Retry the run's pending start record in its own transaction; True once it is persisted. Cleared only after
    that transaction committed, so a later write failing never leaves "persisted" set over a rolled-back row."""
    with durable["lock"]:
        pending = durable.get("pending")
        if pending is not None and not durable.get("stopped") and _write(durable.get("db"), pending):
            durable["pending"] = pending = None
        return pending is None


def _ancestor_sessions(child: Any) -> set[str]:
    owners, cur = set(), child
    for _ in range(8):
        ref = getattr(cur, "_delegate_parent_ref", None)
        cur = ref() if callable(ref) else None
        if cur is None:
            break
        sid = getattr(cur, "session_id", None)
        if isinstance(sid, str) and sid:
            owners.add(sid)
    return owners


def _child_cwd(run: Any) -> Optional[str]:
    from tools.delegate_tool_routes import child_profile_scope
    from tools.terminal_tool import get_session_cwd
    with child_profile_scope(run.child):
        cwd = get_session_cwd(run.child_task_id)
    return os.path.realpath(cwd) if isinstance(cwd, str) and cwd else None


def checkpoint(run: Any) -> None:
    """Heartbeat: persist the child's terminal cwd when it moved, so a hard kill still knows where it was."""
    durable = getattr(run.child, "_delegate_durable", None)
    if not isinstance(durable, dict) or not run.subagent_id:
        return
    cwd = _child_cwd(run)
    if not cwd or cwd == durable.get("cwd"):
        _flush(durable)
        return
    durable["cwd"] = cwd
    baselines = durable["baselines"]
    _note_repo(baselines, durable["start_main"], cwd)

    if not _flush(durable):
        return  # the full start record goes first; this cwd rides on its next retry or on the stop record

    def _do(ledger):
        ledger.checkpoint_subagent(subagent_id=run.subagent_id, cwd=cwd, repo_baselines=baselines)
    _write(durable.get("db"), _do)


def stop_cause_live(run: Any) -> Optional[str]:
    """``gateway_shutdown`` when the gateway's shutdown stopped this child (``note_gateway_shutdown`` marked it, or
    its own turn recorded the shutdown interrupt), else None."""
    if getattr(run.child, "_delegate_stop_cause", None) == GATEWAY_SHUTDOWN or GATEWAY_SHUTDOWN in str(
            getattr(run, "turn_exit_reason", "") or ""):
        return GATEWAY_SHUTDOWN
    return None


def keeps_worktree(run: Any, entry: dict[str, Any]) -> bool:
    """Whether the stopped child is resumable, so its isolation worktree must survive the stop: only with its
    record persisted (a pending one is retried first); without one nothing pins the tree, so it is finalized as
    before (archived when it holds work)."""
    durable = getattr(run.child, "_delegate_durable", None)
    if not run.subagent_id or not isinstance(durable, dict) or not _flush(durable):
        return False
    return classify(entry, stop_cause_live(run))[0] == "resumable"


def stamp_entry(run: Any, entry: dict[str, Any]) -> None:
    """The result entry says how this stopped child may resume (absent when it completed or a person stopped it)."""
    durable = getattr(run.child, "_delegate_durable", None)
    if not run.subagent_id or not isinstance(durable, dict):
        return
    durable["cwd"] = _child_cwd(run) or durable.get("cwd")  # before cleanup drops the child's cwd record
    if not _flush(durable):
        return  # no record to continue from: the entry must not offer a resume
    state, mode, next_at = classify(entry, stop_cause_live(run))
    if state == "resumable":
        entry["resume_mode"] = mode
        if getattr(run.child, "_delegation_unit_id", None) and not durable["auto"]["auto_off"]:
            from tools.delegate_tool_auto_resume import promise
            from tools.delegate_tool_auto_resume import gave_up, gave_up_line
            candidate = {**durable["auto"], "resume_mode": mode, "next_attempt_at": next_at}
            if auto := promise(candidate):
                entry["auto_resume"] = auto
                durable["promised"] = True  # only a stop that promised a resume may be resumed automatically
            elif reason := gave_up(candidate):  # this stop's own completion is the one terminal failure
                entry["auto_gave_up"] = reason
                entry["error"] = f"{entry.get('error') or entry.get('status')}; {gave_up_line(run.subagent_id, reason)}"
                durable["gave_up"] = reason


def record_stop(run: Any, entry: Optional[dict[str, Any]]) -> None:
    """The child's run ended and its resources are closed: its outcome, resume mode, last cwd and pins."""
    durable = getattr(run.child, "_delegate_durable", None)
    sid = run.subagent_id
    if not isinstance(durable, dict) or not sid:
        return
    entry = entry or {"status": "unknown"}
    cwd = durable.get("cwd") or _child_cwd(run)
    live_cause = stop_cause_live(run)
    # The segment it stopped in: with ``compression.in_place: false`` each rotation moved it to a new session.
    session_id = getattr(run.child, "session_id", None)

    def _do(ledger):
        for _ in range(_CAS_TRIES):
            stored = ledger.get_subagent(subagent_id=sid)
            record = _as_record(stored) or {}
            if isinstance(session_id, str) and session_id:
                record["session_id"] = session_id
            record["auto_promised"] = bool(durable.get("promised"))
            if durable.get("gave_up"):
                record["auto_gave_up"] = record["auto_note"] = durable["gave_up"]
            if _finish(ledger, sid, record, entry, live_cause or record.get("stop_cause"), cwd=cwd, expected=stored):
                return True
        return None

    with durable["lock"]:
        flushed = _flush(durable)
        durable["stopped"] = True  # no start retry may run after the stop, landed or not
        if flushed:  # never a partial row (no goal, route or owners) the parent cannot claim
            _write(durable.get("db"), _do)


def _finish(ledger, sid: str, record: dict[str, Any], entry: dict[str, Any], cause: Optional[str], *,
            cwd: Optional[str], expected: Any, pins: Optional[list[str]] = None) -> bool:
    """Write the stopped record while the row is still *expected*, with the limits in the same write; False when it
    moved. *pins*: its pin paths computed before the write (git probes take seconds; no write may wait on them)."""
    document, columns = _stopped(record, entry, cause, cwd=cwd, pins=pins)
    return _upsert(ledger, sid, document, **columns, expected=expected, limits=_limits())


def _stopped(record: dict[str, Any], entry: dict[str, Any], cause: Optional[str], *, cwd: Optional[str],
             pins: Optional[list[str]] = None) -> tuple[dict[str, Any], dict[str, Any]]:
    """``(record, columns)`` of the stopped record ``_upsert`` writes."""
    ttl, _cap = _settings()
    state, mode, next_at = classify(entry, cause)
    record = {**record, "cwd": cwd or record.get("cwd"), "outcome": entry.get("status"), "resume_mode": mode,
              "next_attempt_at": next_at, "failure_reason": entry.get("failure_reason"),
              "failure_resets_at": entry.get("failure_resets_at"), "usage_limit": entry.get("usage_limit"),
              "stopped_at": time.time()}
    if state == "resumable" and not record.get("auto_first_stopped_at"):
        record["auto_first_stopped_at"] = record["stopped_at"]
    if isinstance(entry.get("worktree"), dict):
        record["worktree"] = {**(record.get("worktree") or {}), **entry["worktree"]}
    record["pin_paths"] = (_pin_paths(record) if pins is None else pins) if state == "resumable" else []
    return record, {"state": state, "pinned": state == "resumable" and bool(record["pin_paths"]),
                    "expires_at": time.time() + ttl * 3600, "stop_cause": cause}


def note_gateway_shutdown() -> int:
    """Gateway shutdown is about to stop this process's children: mark them first (in memory and on every profile's
    running records), so each reads as a restart (resumable) rather than a user stop however its run ends, a
    SIGKILL mid-unwind included. Returns how many records were marked."""
    from tools.delegate_tool_registry import _active_subagents, _active_subagents_lock
    with _active_subagents_lock:
        live = [r.get("agent") for r in _active_subagents.values()]
    for agent in live:
        if agent is not None:
            agent._delegate_stop_cause = GATEWAY_SHUTDOWN
    marked = 0
    for db in _profile_dbs():
        marked += _write(db, lambda ledger: ledger.mark_subagents_gateway_shutdown(owner_pid=os.getpid())) or 0
    return marked


def _abandoned_entry(record: dict[str, Any]) -> tuple[dict[str, Any], Optional[str]]:
    """``(entry, cause)`` for a run whose owner died: a gateway shutdown when that was marked, else unknown."""
    cause = record.get("stop_cause")
    return {"status": "interrupted" if cause == GATEWAY_SHUTDOWN else "unknown"}, cause


def plan_recovery(path: Any, alive) -> dict[str, tuple[Any, Any, list[str], Any]]:
    """Boot, before the recovery write: ``{subagent_id: (owner_pid, owner_started_at, pin_paths, stored row)}`` for
    every ``running``/``claimed`` record of *path*'s store whose owner is gone."""
    return recovery_plan(_write(path, lambda ledger: ledger.list_inflight_subagents()) or (), alive)


def recovery_plan(rows: Iterable[Any], alive) -> dict[str, tuple[Any, Any, list[str], Any]]:
    """``plan_recovery`` over stored ``running``/``claimed`` rows. The git probes behind the pins run here, before
    any write."""
    return {row.subagent_id: (row.owner_pid, row.owner_started_at, _live_pins(dict(row.document), memo=False), row)
            for row in rows if not alive(row.owner_pid, row.owner_started_at)}


def _still_abandoned(stored: Any, pid: Any, started: Any) -> Optional[dict[str, Any]]:
    """The record, when *stored* is still the same dead owner's ``running``/``claimed`` row."""
    if stored is None or stored.state.value not in ("running", "claimed") or (
            stored.owner_pid, stored.owner_started_at) != (pid, started):
        return None
    return _as_record(stored)


def recovered_records(plan: dict[str, tuple[Any, Any, list[str], Any]]) -> dict[str, tuple[dict[str, Any], Any]]:
    """Boot: each planned record (``plan_recovery``) still held by its dead owner, ``{subagent_id: (record, write)}``:
    the record as stored and the resumable record it becomes (outcome ``unknown``, or a gateway shutdown when that
    was marked), pinned on where it last was. The recovery write lands it only while its dead owner still holds the
    row; a row that died after the plan was made is reclaimed by ``claim``."""
    out = {}
    for sid, (pid, started, pins, stored) in plan.items():
        record = _still_abandoned(stored, pid, started)
        if record is None:
            continue
        entry, cause = _abandoned_entry(record)
        document, columns = _stopped(record, entry, cause, cwd=record.get("cwd"), pins=pins)
        out[sid] = (record, (document, columns))
    return out


def subagent_recoveries(recovered: dict[str, tuple[dict[str, Any], Any]], promised: Iterable[str] = ()) -> list[Any]:
    """The recovery write of each ``recovered_records`` entry, ``auto_promised`` set on the *promised* ids (the
    scheduler then resumes each of them, or delivers its one give-up)."""
    from persistence.authority.subagents import SubagentRecovery
    promised = set(promised)
    out = []
    for sid, (record, (document, columns)) in recovered.items():
        document = {**document, "auto_promised": True} if sid in promised else document
        out.append(SubagentRecovery(subagent_id=sid, owner_pid=record.get("owner_pid"),
                                    owner_started_at=record.get("owner_started_at"),
                                    write=_subagent_write(document, **columns)))
    return out


def continuable_of(ledger, unit_id: str, recovered: dict[str, tuple[dict[str, Any], Any]]) -> list[str]:
    """Every child of the async unit *unit_id* its parent can continue once the boot recovery lands: the ones it
    finishes (*recovered*), and the ones that recorded their own resumable stop before the owner exited (a gateway
    restart stops each child cleanly, while the unit's ledger row is left running); oldest first."""
    now, rows = time.time(), {}
    for sid in ledger.list_continuable_subagents(unit_id=unit_id):
        if sid not in recovered and (got := ledger.get_subagent(subagent_id=sid)) is not None:
            rows[sid] = (got.created_at, got.expires_at)
    for sid, (record, (document, columns)) in recovered.items():
        unit = document.get("unit_id") if document.get("unit_id") is not None else document.get("delegation_id")
        if columns["state"] == "resumable" and unit == unit_id:
            stored = ledger.get_subagent(subagent_id=sid)
            rows[sid] = (stored.created_at if stored is not None else now, columns["expires_at"])
    # The recovery's limits pass retires a record whose TTL already passed.
    live = [(created, sid) for sid, (created, expires) in rows.items() if expires is None or expires > now]
    return [sid for _created, sid in sorted(live)]


def _reclaim_dead_owner(path: Any, sid: str, record: dict[str, Any]) -> Optional[dict[str, Any]]:
    """A ``running``/``claimed`` row whose owner process died (after this process's boot recovery ran): finish it
    the way boot recovery would and return the finished record; None while its owner is alive."""
    from tools.async_delegation import _owner_liveness
    alive = _owner_liveness()
    pid, started = record.get("owner_pid"), record.get("owner_started_at")
    if alive is None or alive(pid, started):
        return None
    pins = _live_pins(record, memo=False)

    def _do(ledger):
        for _ in range(_CAS_TRIES):
            stored = ledger.get_subagent(subagent_id=sid)
            current = _still_abandoned(stored, pid, started)
            if current is None:
                return None
            entry, cause = _abandoned_entry(current)
            if _finish(ledger, sid, current, entry, cause, cwd=current.get("cwd"), pins=pins, expected=stored):
                return _row(ledger, sid)
        return None
    return _write(path, _do)


# ── continue ───────────────────────────────────────────────────────────────
def claim(parent_agent: Any, sid: str) -> tuple[Optional[dict[str, Any]], Optional[str]]:
    """Take a finished child's durable record for ``continue`` (``(record, None)``, or ``(None, error)``). The
    state flip to ``claimed`` is one guarded UPDATE, so two continues never run one conversation twice."""
    from tools.delegate_tool_registry import _owns_subagent_record
    path = _db_path_for(parent_agent)
    record = _write(path, lambda ledger: _as_record(ledger.apply_subagent_limits(limits=_limits(), subagent_id=sid)))
    if record is None:
        return None, None
    owned = {"owner_agent_session_id": None, "ancestor_owner_session_ids": record.get("owners") or ()}
    if not _owns_subagent_record(owned, parent_agent):
        return None, None
    if record.get("state") in ("running", "claimed"):
        record = _reclaim_dead_owner(path, sid, record) or record
    if record.get("state") not in _FINISHED:
        return None, (f"Subagent '{sid}' is still running or being continued. Try again shortly; if this persists, "
                      "spawn a new task.") if record.get("state") in ("running", "claimed") else None
    owner = _owner_identity()  # boot recovery must not reclaim a continue this live process is starting
    from persistence.authority.subagents import SubagentState  # the parent took it over: no automatic report owed
    claimed = _write(path, lambda ledger: ledger.claim_subagent(
        subagent_id=sid, expected_state=SubagentState(record["state"]), owner_pid=owner["owner_pid"],
        owner_started_at=owner["owner_started_at"]))
    return (record, None) if claimed is True else (None, f"Subagent '{sid}' is already being continued.")


def _hand_back(ledger, sid: str, state: str, *, promised: Optional[bool] = None, owner_pid: Optional[int] = None) -> None:
    """Return a claimed record no run started to *state* (with *promised* as its debt when given), in one write:
    a stop that landed during the claim (``$.auto_cancel``) wins over handing a promise back."""
    from persistence.authority.subagents import SubagentState
    ledger.hand_back_subagent(subagent_id=sid, state=SubagentState(state), promised=promised, owner_pid=owner_pid)


def release(parent_agent: Any, sid: str, record: dict[str, Any]) -> None:
    """Hand a claimed record back (the continue was refused before anything ran), with the debt it had."""
    _write(_db_path_for(parent_agent), lambda ledger: _hand_back(ledger, sid, record["state"],
                                                                 promised=bool(record.get("auto_promised"))))


def hold_claim(child: Any, parent_agent: Any, sid: str, record: dict[str, Any]) -> None:
    """The continued child carries its claim until its run records itself (``record_start``), so a run that fails
    before that hands the row back (``release_unstarted``) instead of leaving it ``claimed`` by a live process."""
    child._delegate_claim = (_db_path_for(parent_agent), sid, record["state"])


def release_unstarted(child: Any) -> bool:
    """Hand back the claim of a continued child whose run ended before ``record_start`` (idempotent). True when
    the hand-back committed (or there was none); a failed one is queued for ``retry_hand_backs``."""
    claim = getattr(child, "_delegate_claim", None)
    durable = getattr(child, "_delegate_durable", None)
    if not isinstance(claim, tuple) or (isinstance(durable, dict) and durable.get("pending") is None):
        return True
    path, sid, state = claim
    # A stop that reached this registered child before it ran (an automatic run failing closed) is the parent's
    # decision: the row ends ``stopped``, never handed back for the scheduler to resume.
    stopped = (getattr(child, "_delegate_run_origin", None) == "auto"
               and getattr(child, "_delegate_stop_requested", False) is True)
    pending = (path, sid, "stopped" if stopped else state, False if stopped else None)
    committed = _hand_back_pending(pending)
    if not committed:  # the store is busy: the queue owns the claim now, and the scheduler's tick retries it
        with _PENDING_HAND_BACKS_LOCK:
            _PENDING_HAND_BACKS.append(pending)
    child._delegate_claim = None
    return committed


# Hand-backs whose write failed (``release_unstarted``): a row left ``claimed`` by this live process is invisible to
# the scheduler and to boot recovery, so a promised child would never report while the process lives.
_PENDING_HAND_BACKS: list[tuple[Optional[Path], str, str, Optional[bool]]] = []
_PENDING_HAND_BACKS_LOCK = threading.Lock()


def _hand_back_pending(pending: tuple[Optional[Path], str, str, Optional[bool]]) -> bool:
    """One hand-back attempt; True once it committed (a row no longer ``claimed`` by this process counts)."""
    path, sid, state, promised = pending
    return _write(path, lambda ledger: (_hand_back(ledger, sid, state, promised=promised, owner_pid=os.getpid()),
                                        True)[1]) is True


def retry_hand_backs() -> None:
    """Scheduler tick: retry each queued hand-back once, keeping only the ones that still failed."""
    with _PENDING_HAND_BACKS_LOCK:
        queued = list(_PENDING_HAND_BACKS)
        _PENDING_HAND_BACKS.clear()
    failed = [p for p in queued if not _hand_back_pending(p)]
    if failed:
        with _PENDING_HAND_BACKS_LOCK:
            _PENDING_HAND_BACKS.extend(failed)


def last_workspace(parent_agent: Any, sid: str) -> Optional[dict[str, Any]]:
    """``{cwd, worktree}`` the child stopped in, for a continue to start there instead of the parent's cwd."""
    record = _write(_db_path_for(parent_agent), lambda ledger: _row(ledger, sid)) or {}
    cwd = record.get("cwd")
    if not (isinstance(cwd, str) and os.path.isdir(cwd)):
        return None
    worktree = record.get("worktree") if isinstance(record.get("worktree"), dict) else None
    return {"cwd": cwd, "worktree": worktree if worktree and os.path.isdir(worktree.get("path") or "") else None}


# ── worktree sweep pin ─────────────────────────────────────────────────────
def _profile_dbs(*, include_run_elsewhere: bool = False) -> list[Any]:
    """The scope of every served profile that has a store (with *include_run_elsewhere*, also of the profiles a
    plugin runs elsewhere)."""
    from hermes_cli.profiles import profiles_to_serve
    from persistence_assembly import profile_scope, store_exists
    homes = [home for _name, home in profiles_to_serve(multiplex=True, include_standalone=True, include_parked=True,
                                                       include_run_elsewhere=include_run_elsewhere)]
    return [scope for scope in (profile_scope(home=Path(home)) for home in homes) if store_exists(scope)]


def _settings_of(home: Path) -> tuple[float, int]:
    """*home*'s own ``delegation`` limits, read under its scope (the sweep runs under whichever profile started it)."""
    from agent.runtime_scope import bind_profile_scope
    try:
        with bind_profile_scope(home, hydrate_secrets=False):
            return _settings()
    except Exception:  # health: allow BLE001 -- an unreadable profile keeps the default cap, never loses its pins
        logger.debug("delegation settings of %s unreadable", home, exc_info=True)
        return _DEFAULT_TTL_HOURS, _DEFAULT_MAX_RESUMABLE


def _read_pins(scope: Any, now: float) -> Iterable[tuple[str, dict[str, Any]]]:
    """The pinned records of *scope*'s store, read without opening it for writing."""
    from persistence import PersistenceError
    from persistence_assembly import open_persistence_reader
    _ttl, cap = _settings_of(scope.profile_dir)
    try:
        with open_persistence_reader(scope.profile_dir) as store:
            rows = store.coordination.list_pinned_subagents(max_resumable=cap, now=now)
    except PersistenceError:  # unreadable here: nothing pinned
        return []
    out = []
    for row in rows:
        record = {**row.document, "expires_at": row.expires_at}
        if row.state.value in ("running", "claimed"):
            record["pin_paths"] = _live_pins(record)
        out.append((row.subagent_id, record))
    return out


def pinned_worktrees(now: Optional[float] = None) -> dict[str, str]:
    """``{realpath: reason}`` for every worktree a live resumable record pins, across this install's profiles.
    The startup sweep's only view of the records."""
    now = now or time.time()
    pins: dict[str, str] = {}
    # A profile a plugin runs elsewhere still pins its trees; this only reads its records.
    for db in _profile_dbs(include_run_elsewhere=True):
        for sid, record in _read_pins(db, now):
            until = time.strftime("%Y-%m-%d %H:%M", time.localtime(record["expires_at"])) if record.get(
                "expires_at") else "?"
            for path in record.get("pin_paths") or ():
                pins.setdefault(os.path.realpath(path), f"resumable subagent {sid} (pinned until {until})")
    return pins


def pin_reason(path: str, pins: dict[str, str]) -> Optional[str]:
    """Why the tree at *path* is pinned (a pin on the tree or any directory inside it), else None."""
    root = os.path.realpath(path)
    for pinned, reason in pins.items():
        if pinned == root or pinned.startswith(root + os.sep):
            return reason
    return None
