Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

6.6 KB
Raw Blame
"""The loop that wakes timer waits and expires pauses that nobody decided.

Why this file exists at all
---------------------------
``pause.expire_due`` was written with the approval pause and has had no
production caller since: its only reference in the tree is a test. So an
approval that nobody decided stayed ``waiting`` forever, and the `expired`
outcome the design calls "a decision, not an absence of one" was never
recorded by anything.

A ``wait`` step inherits that hole and makes it worse, because an approval
at least has a human who might come back. A timer wait with no sweeper is a
run that stops and never continues, reporting ``paused`` indefinitely.

Three properties, each the opposite of a way this could go wrong
-----------------------------------------------------------------
**Tells permanent from transient.** An unreachable database is transient and
is retried with a capped backoff. A missing playbook is permanent for that
pause, and the resume path records it rather than retrying forever.

**Says so when it cannot do its job.** A sweeper that silently stops is
indistinguishable from one with nothing to do. Every error is logged at
``warning`` with the reason, and consecutive failures escalate the interval
rather than hammering a store that is down.

**Safe to run on several replicas.** Claiming a pause is a conditional
``UPDATE ... WHERE status = 'waiting'``, so two sweepers racing the same row
resume it once; the loser sees zero rows changed and moves on.
"""

from __future__ import annotations

import asyncio
import contextlib
import logging
import os

logger = logging.getLogger("aisoc.playbook.sweeper")

#: How often to look. A wait is a coarse instrument — the shortest one that
#: reaches this loop is already longer than the inline ceiling — so a minute
#: of latency on a six-hour wait costs nothing and a tight poll would be a
#: query per second against a table that is empty most of the time.
DEFAULT_INTERVAL_SECONDS = float(os.getenv("AISOC_PLAYBOOK_SWEEP_INTERVAL_SECONDS", "60"))

#: Ceiling for the backoff a failing store earns. Past this the sweeper is
#: still trying, just not pretending the next attempt is urgent.
_MAX_BACKOFF_SECONDS = 600.0


def enabled() -> bool:
    """Off only by explicit opt-out.

    On by default, unlike everything in this wave that reaches a vendor:
    this reaches nothing outward. Its job is to finish work the product has
    already started, and the failure mode of not running it is a run that
    hangs — which is the defect, not the safe state.
    """
    return os.getenv("AISOC_PLAYBOOK_SWEEPER_DISABLE", "").strip().lower() not in {"1", "true", "yes", "on"}


async def sweep_once() -> dict[str, int]:
    """One pass: resume what is due, expire what is past its deadline.

    Resume first. A pause whose timer came due in the same tick that its
    TTL expired should run, not be cancelled — the run did what it was
    asked to, slightly late, and expiring it would discard work for a
    scheduling detail.
    """
    from app.playbook import engine
    from app.playbook import pause as playbook_pause

    resumed = await engine.resume_due_waits()
    # Depth 5.4. A verification's deadline does not mean "give up": at the
    # user stage it escalates to the manager and the run stays suspended,
    # and only at the manager stage does it resume with `timed_out`. Swept
    # before `expire_due` for the reason the waits are — a question whose
    # stage came due in the same tick as its TTL should escalate, not be
    # cancelled with no outcome for the run to read.
    verifications = await engine.resume_due_verifications()
    # Wave-2 6.3. One statement expires the due pauses and hands back what
    # each one declared about its deadline, because the declaration lives
    # on a row the expiry stops being `waiting` — a separate read would
    # come after it and find nothing. An approval that declared `rollback`
    # or `contain` has something the platform must do about nobody
    # answering; `stay_paused` declares that it does not, and is the
    # default, so every existing playbook is unaffected.
    expired = await playbook_pause.expire_due_returning()
    timeouts = await engine.run_timeout_outcomes(expired)
    if resumed or verifications or expired or timeouts:
        logger.info(
            "playbook_sweeper: resumed %d wait(s), finished %d verification(s), ran %d timeout outcome(s), expired %d pause(s)",
            len(resumed),
            len(verifications),
            len(timeouts),
            len(expired),
        )
    return {
        "resumed": len(resumed),
        "verifications": len(verifications),
        "timeout_outcomes": len(timeouts),
        "expired": len(expired),
    }


async def run_forever(*, interval_seconds: float | None = None) -> None:
    """Sweep on a fixed cadence until cancelled."""
    interval = interval_seconds or DEFAULT_INTERVAL_SECONDS
    consecutive_failures = 0
    while True:
        try:
            await sweep_once()
            consecutive_failures = 0
            delay = interval
        except asyncio.CancelledError:
            raise
        except Exception as exc:  # noqa: BLE001 — a failed sweep must not end the loop
            consecutive_failures += 1
            delay = min(interval * (2**consecutive_failures), _MAX_BACKOFF_SECONDS)
            logger.warning(
                "playbook_sweeper: pass %d failed (%s); next attempt in %.0fs",
                consecutive_failures,
                exc,
                delay,
            )
        await asyncio.sleep(delay)


def start(app: object) -> asyncio.Task | None:
    """Start the sweeper as a background task on the app, or say why not."""
    if not enabled():
        logger.info("playbook_sweeper: disabled by AISOC_PLAYBOOK_SWEEPER_DISABLE")
        return None
    task = asyncio.create_task(run_forever())
    task.add_done_callback(_log_exit)
    with contextlib.suppress(AttributeError):
        app.state.playbook_sweeper_task = task  # type: ignore[attr-defined]
    logger.info("playbook_sweeper: started, every %.0fs", DEFAULT_INTERVAL_SECONDS)
    return task


def _log_exit(task: asyncio.Task) -> None:
    """Retrieve the task's outcome so a dead sweeper is in the log.

    The same pairing the triage worker and the fusion consumer carry. A
    background task whose exception is never retrieved dies quietly, and
    the only symptom is runs that stop resuming — which looks like a
    playbook problem rather than a missing sweeper.
    """
    if task.cancelled():
        return
    exc = task.exception()
    if exc is not None:
        logger.error("playbook_sweeper: exited on %s; waits will no longer resume", exc)