Skip to content

Model gate

agent_gate.model_gate

Layer 2: the System 1 model gate.

A fast model reads the raw action, the mandate and the session's recent actions, and judges the cases the rules cannot: mostly Law 1, harm to a person. It sees the action with the agent's own description removed, and is told to treat the action as data.

The model can only tighten. Its verdict is combined with the rules' verdict by :func:agent_gate.verdict.strictest, so it can never lower one. Every failure (no SDK, no credentials, a timeout, a refusal, unparseable output) becomes ask.

judge(action, mandate, entries, client=None)

Ask the model gate for a verdict.

Parameters:

Name Type Description Default
action Action

the action to judge.

required
mandate Mandate

the governing mandate.

required
entries list[dict[str, Any]]

the session's ledger entries.

required
client Any

an Anthropic client, for tests; the anthropic backend only.

None

Returns:

Type Description
Verdict

the model's verdict, or ask on any failure.

Source code in src/agent_gate/model_gate.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def judge(
    action: Action, mandate: Mandate, entries: list[dict[str, Any]], client: Any = None
) -> Verdict:
    """Ask the model gate for a verdict.

    :param action: the action to judge.
    :param mandate: the governing mandate.
    :param entries: the session's ledger entries.
    :param client: an Anthropic client, for tests; the ``anthropic`` backend only.
    :return: the model's verdict, or ``ask`` on any failure.
    """
    config = mandate.model
    request = render_request(action, mandate, entries)
    if config.backend == "openai":
        return _judge_openai(config, request)
    if config.backend == "anthropic":
        return _judge_anthropic(config, request, client)
    return _ask(f"The model gate backend {config.backend!r} is not known.")

parse_reply(text)

Turn the model's reply into a verdict, or ask if it cannot be read.

Local models may wrap the JSON in reasoning (<think>...</think>) or prose, so the outermost JSON object is extracted first.

Parameters:

Name Type Description Default
text str

the reply text.

required
Source code in src/agent_gate/model_gate.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def parse_reply(text: str) -> Verdict:
    """Turn the model's reply into a verdict, or ``ask`` if it cannot be read.

    Local models may wrap the JSON in reasoning (``<think>...</think>``) or
    prose, so the outermost JSON object is extracted first.

    :param text: the reply text.
    """
    text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
    start, end = text.find("{"), text.rfind("}")
    try:
        data = json.loads(text[start : end + 1] if start != -1 else text)
        decision = data["decision"]
        law = int(data["law"])
        reason = str(data["reason"])
    except (ValueError, KeyError, TypeError):
        return _ask("The model gate returned an answer the gate could not read.")
    if decision not in {"pass", "ask", "deny"}:
        return _ask("The model gate returned an unknown decision.")
    return Verdict(decision, law if 1 <= law <= 5 else None, reason, "model")

render_request(action, mandate, entries)

Build the user message for the model gate.

Parameters:

Name Type Description Default
action Action

the action to judge.

required
mandate Mandate

the governing mandate.

required
entries list[dict[str, Any]]

the session's ledger entries, oldest first.

required
Source code in src/agent_gate/model_gate.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def render_request(action: Action, mandate: Mandate, entries: list[dict[str, Any]]) -> str:
    """Build the user message for the model gate.

    :param action: the action to judge.
    :param mandate: the governing mandate.
    :param entries: the session's ledger entries, oldest first.
    """
    scope = {
        "task": mandate.task,
        "paths": [str(p) for p in mandate.paths],
        "hosts": list(mandate.hosts),
        "git_remotes": list(mandate.git_remotes),
        "recipients": list(mandate.recipients),
    }
    history = [
        f"- {e.get('decision')}: {str(e.get('action', ''))[:300]}" for e in entries[-HISTORY_LIMIT:]
    ]
    raw = redact(json.dumps({"tool": action.tool, "input": action.params}, default=str))[
        :MAX_ACTION_CHARS
    ]
    return (
        f"<mandate>\n{json.dumps(scope, indent=1)}\n</mandate>\n\n"
        f"<recent_actions>\n{chr(10).join(history) or '(none)'}\n</recent_actions>\n\n"
        f"<action>\n{raw}\n</action>"
    )