ariadne/ariadne/services/hermes_suggested_remediation.py
codex 9f24f8d999
All checks were successful
Tests / Declarative: Post Actions passed: 1275
feat(hermes): let a diagnosis propose a remediation that does not exist yet
Every action in the allowlist got there because someone hit the failure by
hand, recognised the pattern, and wired a job for it. That loop only closes if
a person happens to read enough issues to notice the same failure recurring,
so a failure nobody reviews twice never earns an action. Until now a diagnosis
that fit nothing could only say a human was needed; it could not say what the
human should build.

An optional suggested_remediation field closes the other half of the loop.
When nothing in the allowlist fits, Hermes may name the remediation it
believes would work and the evidence that should be required before running it
is safe. It lands in the incident issue under a heading that states plainly
the remediation does not exist and was not performed, and in the audit event,
where the same proposal recurring across unrelated incidents is the evidence
that building it is worth the effort.

The field is inert by construction. No gate reads it, and an id that is not
already allowlisted still fails action_not_allowlisted exactly as before -
naming a remediation and being granted one stay different things, and only the
second needs a human to change a deployment. It is rejected outright alongside
a requested action: the field reports that nothing fit, so something fitting
contradicts it, and allowing both would invite a rationale to be attached to a
request the gates must judge on evidence alone. The key is optional rather
than required so a response written before today still validates unchanged,
and unknown keys are still refused.

Also names reclaim_workspace_storage, clear_stuck_agent_pods and
abort_hung_build in KNOWN_ACTION_LABELS. They were reporting as "unknown" in
metrics on any deployment that had not enabled them, which is the one case
where you most want to see the real name.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 21:15:08 -03:00

164 lines
6.2 KiB
Python

"""Let Hermes describe a remediation Ariadne cannot yet perform.
Every action in the allowlist exists because a person hit the failure by hand,
recognised the pattern, and wired a job for it. That is a slow loop, and it
only closes when someone happens to read enough issues to notice the same
failure recurring. A failure nobody reviews twice never earns an action.
This is the other half of that loop. When no allowlisted action fits, Hermes
may describe what it believes *would* fix the failure and what evidence should
have to be present before that fix is safe. The description is recorded on the
incident and printed in the issue, so a suggestion that keeps reappearing
across unrelated builds becomes visible as a pattern rather than staying buried
in one issue nobody re-reads.
The suggestion is inert by construction, and deliberately so. It carries no
authority: the authorization gates never read it, and an id that is not already
in the allowlist fails `action_not_allowlisted` exactly as it always has. Its
only destination is prose a person reads. That separation is what makes it safe
to let the model name its own remediation - naming one and being granted one
are different things, and only the second requires a deployment change made by
a human.
A suggestion is accepted only when `requested_action` is null. If an existing
action already fit, the gap this field exists to report is not there, and
allowing both would invite the model to attach a rationale to a request the
gates are supposed to judge on evidence alone.
"""
from __future__ import annotations
from dataclasses import dataclass
import re
from typing import Any
SUGGESTION_KEY = "suggested_remediation"
_KEYS = {"action_id", "summary", "evidence_required"}
# The same shape as a real action id, so a suggestion that graduates can be
# deployed under the name it was proposed with.
_ACTION_ID = re.compile(r"^[a-z][a-z0-9_]{2,63}$")
_MAX_TEXT = 400
@dataclass(frozen=True)
class SuggestedRemediation:
"""Represent a remediation Hermes proposes but Ariadne cannot perform.
Inputs: a validated `suggested_remediation` object from the response
schema. Outputs: the proposed action id, what it would do, and the evidence
that should be required before it runs.
Holds no authority of any kind. Nothing in the gate chain reads this.
"""
action_id: str
summary: str
evidence_required: str
def validate(payload: dict[str, Any]) -> str | None:
"""Validate the optional suggested_remediation field of a response.
Inputs: the whole parsed response payload. Outputs: a specific reject
reason, or None when the field is absent, null, or well-formed.
Rejects a suggestion offered alongside a requested action: the field
reports that nothing fit, so having something fit contradicts it.
"""
suggestion = payload.get(SUGGESTION_KEY)
if suggestion is None:
return None
if payload.get("requested_action") is not None:
return "suggested_remediation_invalid: must be null when an action is requested"
if not isinstance(suggestion, dict):
return "suggested_remediation_invalid: must be an object or null"
if set(suggestion) != _KEYS:
return (
"suggested_remediation_invalid: must have exactly action_id, summary, evidence_required"
)
return _validate_fields(suggestion)
def _validate_fields(suggestion: dict[str, Any]) -> str | None:
"""Check each field of a suggestion whose key set is already correct."""
for key in sorted(_KEYS):
if not isinstance(suggestion[key], str) or not suggestion[key].strip():
return f"suggested_remediation_invalid: {key} must be a non-empty string"
if not _ACTION_ID.match(suggestion["action_id"]):
return f"suggested_remediation_invalid: action_id {suggestion['action_id']!r} is not snake_case"
return None
def from_payload(payload: dict[str, Any]) -> SuggestedRemediation | None:
"""Build the typed suggestion from an already-validated payload.
Inputs: the parsed response payload. Outputs: a SuggestedRemediation, or
None when the field is absent or null. Text is clipped here so a long
proposal cannot crowd the diagnosis out of a bounded issue body.
"""
suggestion = payload.get(SUGGESTION_KEY)
if not isinstance(suggestion, dict):
return None
return SuggestedRemediation(
action_id=suggestion["action_id"],
summary=_clip(suggestion["summary"]),
evidence_required=_clip(suggestion["evidence_required"]),
)
def as_detail(suggestion: Any) -> dict[str, str] | None:
"""Summarize a suggestion for the audit event detail.
Inputs: a SuggestedRemediation or None. Outputs: the three fields as a
plain dict for the event store, or None. Recording it durably is the point:
the same proposal recurring across unrelated incidents is the evidence that
a real action is worth building.
"""
if suggestion is None:
return None
return {
"action_id": suggestion.action_id,
"summary": suggestion.summary,
"evidence_required": suggestion.evidence_required,
}
def issue_section(suggestion: Any) -> str:
"""Render the suggestion as a markdown section for the incident issue.
Inputs: a SuggestedRemediation or None. Outputs: the section, or "" when
there is no suggestion.
The heading states plainly that this remediation does not exist, so nobody
reads it as something that was attempted, declined, or is about to happen.
"""
if suggestion is None:
return ""
return "\n".join(
[
"## Suggested remediation (not available)",
"No automated remediation exists for this failure. Hermes proposes one; "
"it was not performed, and Ariadne cannot perform it until a person builds "
"and deploys it.",
f"- Proposed action: `{suggestion.action_id}`",
f"- What it would do: {suggestion.summary}",
f"- Evidence that should be required first: {suggestion.evidence_required}",
]
)
def _clip(value: str) -> str:
"""Clip one proposal field so it cannot crowd out the diagnosis."""
text = " ".join(str(value).split())
if len(text) <= _MAX_TEXT:
return text
return text[: _MAX_TEXT - 3] + "..."