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.

12.4 KB
Raw Blame
"""The bounded ``ask_model`` step: a question with a closed answer set.

Wave-2 Phase 6.2 adds one step type that asks the model a bounded question
and branches on the answer. Everything about it is a deliberate narrowing
of what "let the model decide" usually means, because a playbook step the
model can steer is the shortest path from a prompt injection to a
containment action.

Four properties, each enforced rather than documented
-----------------------------------------------------
**It cannot call a verb.** This module imports nothing from
``action_bridge``, the live-action registry or any vendor client, and the
engine routes it through the control-flow table rather than through
``_HANDLERS``, so there is no dispatch path to reach. A test asserts the
import graph stays that way by trying to make the step act and watching it
fail.

**The answer set is closed and declared by the playbook author.** The model
chooses among ``choices``; it does not supply a string that becomes a
target, a query or a branch name. An answer outside the set is invalid.

**An invalid answer takes a declared fallback branch.** ``on_invalid`` is
required, so a model that returns prose, nothing, or a cheerfully confident
option nobody offered lands somewhere the author chose. The common failure
of "let the model decide" is that nobody decided what happens when it does
not decide.

**The branch is selected from the playbook's own steps.** The answer picks
a key in ``branches``, which the author wrote; the model's text never
becomes a step id. So the worst an injected instruction can achieve is to
pick a branch the playbook already contains — which is a decision the
author already signed off on.

What it is for
--------------
The judgement calls a playbook cannot express as a condition and a human
should not have to be woken for: "does this evidence describe a user's own
scheduled job, or someone else's?", "is this hostname a build agent or a
workstation?". The answer is a label the playbook branches on, ledgered
with the question that produced it.
"""

from __future__ import annotations

import json
import logging
from dataclasses import dataclass
from typing import Any

from .bounds import clamp_timeout

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

#: Hard ceilings. A bounded question that can carry four kilobytes of
#: attacker-influenced alert text is not bounded.
MAX_QUESTION_CHARS = 1000
MAX_CHOICES = 8
MIN_CHOICES = 2
MAX_CONTEXT_KEYS = 6
MAX_CONTEXT_VALUE_CHARS = 600
#: What is kept of the model's reply when it does not validate. Enough to
#: diagnose, bounded so a model that returns a megabyte of prose cannot
#: become a megabyte of ledger.
MAX_RAW_ANSWER_CHARS = 500


class AskModelConfigError(ValueError):
    """The step is not usable as written. Raised at validation, not at run time."""


@dataclass(frozen=True)
class AskModelStepConfig:
    """A validated ``ask_model`` step.

    Validation is separate from execution so the schema gate, the editor
    and the engine agree about what a usable step looks like, and so a
    playbook with a malformed one fails when it is saved rather than at
    three in the morning.
    """

    question: str
    choices: tuple[str, ...]
    branches: dict[str, str]
    on_invalid: str
    context_keys: tuple[str, ...]
    timeout_seconds: int

    @classmethod
    def from_params(cls, params: dict[str, Any], *, timeout_seconds: int = 30) -> AskModelStepConfig:
        question = str(params.get("question") or "").strip()
        if not question:
            raise AskModelConfigError("ask_model needs a 'question'")
        if len(question) > MAX_QUESTION_CHARS:
            raise AskModelConfigError(f"question is {len(question)} characters; the ceiling is {MAX_QUESTION_CHARS}")

        raw_choices = params.get("choices") or []
        if not isinstance(raw_choices, list):
            raise AskModelConfigError("'choices' must be a list of allowed answers")
        choices = tuple(str(choice).strip() for choice in raw_choices if str(choice).strip())
        if len(choices) < MIN_CHOICES:
            raise AskModelConfigError(f"ask_model needs at least {MIN_CHOICES} choices; a question with one answer is not a question")
        if len(choices) > MAX_CHOICES:
            raise AskModelConfigError(f"{len(choices)} choices exceeds the ceiling of {MAX_CHOICES}")
        if len(set(choices)) != len(choices):
            raise AskModelConfigError("'choices' contains duplicates, so two branches would be unreachable")

        raw_branches = params.get("branches") or {}
        if not isinstance(raw_branches, dict):
            raise AskModelConfigError("'branches' must map an allowed answer to a step id")
        branches = {str(key): str(value) for key, value in raw_branches.items() if str(value).strip()}
        unknown = sorted(set(branches) - set(choices))
        if unknown:
            raise AskModelConfigError(f"'branches' names {unknown}, which are not in 'choices'; those branches can never be taken")

        on_invalid = str(params.get("on_invalid") or "").strip()
        if not on_invalid:
            raise AskModelConfigError(
                "ask_model needs 'on_invalid': the step id to take when the "
                "model's answer does not validate. A step that only says what "
                "to do when the model cooperates has not decided the case that "
                "matters."
            )

        raw_context = params.get("context_keys") or []
        if not isinstance(raw_context, list):
            raise AskModelConfigError("'context_keys' must be a list of run-context keys")
        context_keys = tuple(str(key) for key in raw_context[:MAX_CONTEXT_KEYS])

        return cls(
            question=question,
            choices=choices,
            branches=branches,
            on_invalid=on_invalid,
            context_keys=context_keys,
            timeout_seconds=clamp_timeout(timeout_seconds),
        )


def build_prompt(config: AskModelStepConfig, context: dict[str, Any]) -> str:
    """Assemble the user message, with every context value sanitised.

    The context values are the attacker-influenced half: they come from
    alert text, vendor payloads and earlier step results. They go through
    the same sanitiser the investigator uses and are wrapped so the model
    sees them as quoted evidence rather than as further instructions.
    """
    from app.investigator.prompt_sanitizer import (  # noqa: PLC0415 - deferred: the sanitiser pulls in the guard corpus
        sanitize_text,
        wrap_untrusted,
    )

    lines = [f"Question: {config.question}", "", "Allowed answers:"]
    lines.extend(f"- {choice}" for choice in config.choices)

    if config.context_keys:
        lines.extend(["", "Evidence:"])
        for key in config.context_keys:
            value = context.get(key)
            if value is None:
                lines.append(f"- {key}: (absent)")
                continue
            rendered = value if isinstance(value, str) else json.dumps(value, default=str)
            trimmed = rendered[:MAX_CONTEXT_VALUE_CHARS]
            lines.append(f"- {key}: {wrap_untrusted(sanitize_text(trimmed), label=f'evidence:{key}')}")

    return "\n".join(lines)


def parse_answer(raw: Any, config: AskModelStepConfig) -> tuple[str | None, str]:
    """Return ``(validated_answer, reason)``; ``None`` means invalid.

    Deliberately strict. A model that replies "I think it is probably
    scheduled_job" has not answered the question this step asked, and
    accepting it by substring would let the reply pick a branch by
    accident — most likely the branch whose name happens to be a prefix of
    another.
    """
    text = raw if isinstance(raw, str) else str(getattr(raw, "content", "") or "")
    text = text.strip()
    if not text:
        return None, "the model returned nothing"

    # Models fence JSON often enough that refusing a fenced reply would
    # mean taking the fallback on a well-formed answer.
    if text.startswith("```"):
        text = text.strip("`")
        if text.lower().startswith("json"):
            text = text[4:]
        text = text.strip()

    try:
        payload = json.loads(text)
    except (TypeError, ValueError):
        return None, "the model's reply was not JSON"
    if not isinstance(payload, dict):
        return None, "the model's reply was JSON but not an object"

    answer = payload.get("answer")
    if not isinstance(answer, str):
        return None, "the reply carried no 'answer' string"
    answer = answer.strip()
    if answer not in config.choices:
        return None, f"{answer!r} is not one of the allowed answers"

    reason = payload.get("reason")
    return answer, (reason.strip()[:300] if isinstance(reason, str) else "")


def _model_factory() -> Any | None:
    """The configured chat model for the ``triage`` role, or ``None``.

    Separate and module-level so a test can replace it without reaching
    into the provider stack, and so "no model configured" is one branch
    rather than an exception shape every caller re-derives.
    """
    try:
        from app.llm.factory import make_chat_model  # noqa: PLC0415 - deferred: heavy module
    except Exception:  # noqa: BLE001 - absent provider stack is "no model"
        return None
    try:
        return make_chat_model("triage")
    except Exception as exc:  # noqa: BLE001
        logger.info("ask_model: no chat model available (%s)", exc)
        return None


async def ask(config: AskModelStepConfig, context: dict[str, Any], *, dry_run: bool) -> dict[str, Any]:
    """Run the question and report what happened, including the branch taken.

    Never raises. Every failure — no model configured, a provider error, a
    reply that does not validate — resolves to the declared fallback
    branch, because a step whose purpose is to decide must decide even when
    the thing it asked did not.
    """
    record: dict[str, Any] = {
        "question": config.question,
        "choices": list(config.choices),
        "context_keys": list(config.context_keys),
        "answer": None,
        "valid": False,
        "fallback_taken": True,
        "reason": "",
        "next_step_id": config.on_invalid,
    }

    if dry_run:
        # A preview must not place a model call. Reporting the fallback
        # here would be a lie in the other direction — it says the step
        # decided — so the preview says plainly that it did not ask.
        record["reason"] = "dry run: the model was not asked"
        record["dry_run"] = True
        record["next_step_id"] = None
        return record

    prompt = build_prompt(config, context)

    model = _model_factory()
    if model is None:
        record["reason"] = "no model is configured, so the fallback branch was taken"
        return record

    try:
        from app.llm import safe_ainvoke  # noqa: PLC0415 - deferred: heavy module
        from app.llm.prompt_registry import prompt_text  # noqa: PLC0415 - deferred: same package

        # Every call from this service goes through safe_ainvoke, which
        # enforces the LLM input contract. The AST gate that proves no
        # module bypasses it reads this name, so routing around it would
        # fail the build rather than merely be a bad idea.
        # Named literally rather than through a constant: the prompt-lock gate
        # reads the name at the call site, and a pin nothing can be seen to
        # ask for is the appearance of coverage rather than coverage.
        reply = await safe_ainvoke(model, [("system", prompt_text("playbook.ask_model.system")), ("user", prompt)])
    except Exception as exc:  # noqa: BLE001 - a provider error is a fallback, not a crash
        record["reason"] = f"the model call failed, so the fallback branch was taken: {exc}"
        record["raw_answer"] = ""
        logger.warning("ask_model.call_failed", extra={"error": str(exc)})
        return record

    raw_text = reply if isinstance(reply, str) else str(getattr(reply, "content", "") or "")
    record["raw_answer"] = raw_text[:MAX_RAW_ANSWER_CHARS]

    answer, reason = parse_answer(reply, config)
    record["reason"] = reason
    if answer is None:
        logger.info("ask_model.invalid_answer", extra={"reason": reason})
        return record

    record["answer"] = answer
    record["valid"] = True
    record["fallback_taken"] = False
    # The answer selects a key the author wrote. If the author declared no
    # branch for this answer, the step falls through to the next step in
    # order rather than inventing a destination.
    record["next_step_id"] = config.branches.get(answer)
    return record