%% Automatic test failure triage and response.
%% Hermes reads and recommends. Ariadne validates and performs every write.
%% Relationships carry the detail; node text stays concise.
%%{init: {"flowchart": {"defaultRenderer": "elk", "curve": "stepAfter", "nodeSpacing": 32, "rankSpacing": 48, "useMaxWidth": false}, "elk": {"mergeEdges": true, "nodePlacementStrategy": "LINEAR_SEGMENTS", "forceNodeModelOrder": true, "considerModelOrder": "NODES_AND_EDGES"}, "themeVariables": {"background": "#000000"}, "themeCSS": "& { background-color: #000000 !important; }"}}%%
flowchart TD

    subgraph system["Hermes Test Failure Triage"]
        direction TB

        subgraph main_row["Main flow"]
            direction TB

            subgraph upper_flow["Evidence and analysis"]
                direction LR

                subgraph reference["Legend"]
                    direction TB

                    key_build["Blue<br/>Build and validation"]:::build
                    key_evidence["Green<br/>Evidence"]:::evidence
                    key_ariadne["Teal<br/>Ariadne orchestration"]:::ariadne
                    key_hermes["Purple<br/>Hermes"]:::hermes
                    key_policy["Gold<br/>Policy and action"]:::policy
                    key_human["Red<br/>Human required"]:::human
                    key_limit["Dashed gray<br/>Current boundary"]:::limited
                end

                subgraph intake["Detect and gather"]
                    direction TB

                    detector["Jenkins detector<br/>Allowlisted jobs<br/>Terminal failures only"]:::ariadne
                    evidence_sources["Evidence sources<br/>Jenkins and OpenSearch<br/>Metrics, Kubernetes, Flux<br/>Gitea and Grafana"]:::evidence
                    collector["Console reader<br/>Full console<br/>2 MB cap"]:::ariadne
                    failure_ranker["Failure ranker<br/>Strong evidence first<br/>Tool output alone is neutral"]:::ariadne
                    context_filter["Context filter<br/>Passing quotes removed<br/>Repeats and overlaps merged"]:::ariadne
                    bundle["Incident bundle<br/>Failures, logs, metrics<br/>Health, revisions, links"]:::evidence

                    detector --> evidence_sources
                    evidence_sources --> collector
                    collector --> failure_ranker
                    failure_ranker --> context_filter
                    context_filter --> bundle
                end

                subgraph hermes_plane["Hermes analysis"]
                    direction TB

                    hermes_access["Read-only access<br/>Jenkins, metrics, Kubernetes<br/>Flux, Gitea, Grafana<br/>Investigation by incident ID"]:::hermes
                    model_gate["Model gate<br/>openai-codex/gpt-5.6-terra<br/>local gpt-oss:20b fallback"]:::model
                    skills["Triage skills<br/>Jenkins evidence<br/>Quality and cluster health<br/>Flux and Git correlation"]:::skill
                    recommendation["Structured recommendation<br/>Classification and confidence<br/>Sourced facts<br/>Action request or patch data"]:::hermes
                    tool_stream["Agent event stream<br/>Evidence and tool calls"]:::audit

                    hermes_access --> skills
                    model_gate -.-> skills
                    skills --> recommendation
                    hermes_access -.-> tool_stream
                end
            end

            subgraph lower_flow["Policy and response"]
                direction LR

                subgraph controls["Ariadne policy gates"]
                    direction TB

                response_check["Response check<br/>Schema, incident, sources<br/>Confidence and action"]:::policy
                repair_guard["Scoped repair guard<br/>Classification and action<br/>must match the owning job"]:::policy
                marker_check["Transient marker check<br/>DNS, connection, TLS<br/>image pull, upstream 5xx<br/>Disk full is excluded"]:::policy
                authorizer["Action authorizer<br/>Allowlist, evidence, confidence<br/>One action, kill switch"]:::policy

                response_check -->|scoped repair| repair_guard
                repair_guard --> authorizer
                response_check -->|transient claim| marker_check
                marker_check -->|marker confirmed| authorizer
                response_check -->|other result| authorizer
                end

                subgraph response["Ariadne response"]
                    direction LR

                    subgraph response_paths["Response paths"]
                        direction TB

                        route{"Policy result"}:::decision

                        subgraph immediate_paths["Action or escalation"]
                            direction LR

                            subgraph operational_path["Allowlisted action"]
                                direction TB

                                registry["Action registry"]:::policy
                                repair_fixture["Scoped ConfigMap repair<br/>In-process patch<br/>No pod created"]:::action
                                retry_infra["Transient retry<br/>One Jenkins rebuild<br/>No cluster write"]:::action
                                operational_result["Action result<br/>One attempt<br/>One validation build"]:::action

                                registry --> repair_fixture
                                registry --> retry_infra
                                repair_fixture --> operational_result
                                retry_infra --> operational_result
                            end

                            subgraph escalation_path["Human-required response"]
                                direction TB

                                human_required["Diagnosis and next checks<br/>Incident stays human required"]:::human
                                gitea_issue["Ariadne opens an issue<br/>Evidence and suggested fix"]:::human

                                human_required --> gitea_issue
                            end
                        end

                        route -->|authorized action| registry
                        route -->|human required| human_required
                        operational_result -->|action failed| human_required
                    end

                    subgraph code_path["Optional source proposal"]
                        direction TB

                        subgraph proposal_stage["Prepare and check"]
                            direction TB

                            duplicate_guard["Open proposal check<br/>Existing repair branch"]:::policy
                            candidate_files["Candidate files<br/>Path and line hints<br/>Read failing test"]:::evidence
                            source_target["Patch target<br/>Follow test imports<br/>Tests remain read only"]:::policy
                            patch_proposal["Hermes patch data<br/>Path, anchor, replacement<br/>Reason"]:::hermes
                            patch_validator["Patch validator<br/>Allowed path and branch<br/>20 lines, 4 KB<br/>Exact anchor"]:::policy

                            duplicate_guard --> candidate_files
                            candidate_files --> source_target
                            source_target --> patch_proposal
                            patch_proposal --> patch_validator
                        end

                        subgraph pr_stage["Deliver for review"]
                            direction TB

                            gitea_pr["Ariadne opens a pull request<br/>hermes-repair branch<br/>Ariadne holds the token"]:::git
                            branch_build["Branch validation<br/>Configured repositories only"]:::limited
                            human_merge["Human review and merge<br/>No automatic merge"]:::human
                            flux_delivery["Flux delivery<br/>After human merge"]:::git

                            gitea_pr -.->|where configured| branch_build
                            branch_build --> human_merge
                            gitea_pr -->|otherwise| human_merge
                            human_merge --> flux_delivery
                        end

                        patch_validator -->|validated patch| gitea_pr
                    end

                    response_paths -.->|eligible source defect| code_path
                end

                subgraph outputs["Inspectable outputs"]
                    direction TB

                output_entry["Ariadne records every outcome"]:::audit
                audit_events["Audit events<br/>Incident, diagnosis, action<br/>Patch proposal and agent IDs"]:::audit
                metrics_output["Triage metrics<br/>VictoriaMetrics and Grafana"]:::metrics
                alert_output["Narrow alerts<br/>Failed repair<br/>Escalation untouched for six hours"]:::metrics
                issue_output["Gitea issue<br/>Human-required incident<br/>Written by Ariadne"]:::git
                pr_output["Gitea pull request<br/>Source proposal<br/>Written by Ariadne"]:::git

                output_entry --> audit_events
                output_entry --> metrics_output
                metrics_output --> alert_output
                end
            end

            reference ~~~ intake
            intake ==>|incident bundle| hermes_plane
            controls ==>|policy result| response
            response ==>|records and artifacts| outputs
            upper_flow ==>|recommendation| lower_flow
        end

    end

    classDef build fill:#16324f,stroke:#6cb6ff,color:#ffffff
    classDef evidence fill:#173f2b,stroke:#61d095,color:#ffffff
    classDef ariadne fill:#24414a,stroke:#69c5d1,color:#ffffff
    classDef hermes fill:#3c3155,stroke:#c1a7ff,color:#ffffff
    classDef skill fill:#42345b,stroke:#c1a7ff,color:#ffffff
    classDef model fill:#342b4f,stroke:#a78bfa,color:#ffffff
    classDef policy fill:#3d3525,stroke:#e4bd55,color:#ffffff
    classDef decision fill:#47391c,stroke:#e4bd55,color:#ffffff,stroke-width:3px
    classDef action fill:#4a3b16,stroke:#e4bd55,color:#ffffff
    classDef human fill:#4a2428,stroke:#ff6b78,color:#ffffff
    classDef git fill:#253f35,stroke:#78c89a,color:#ffffff
    classDef metrics fill:#243750,stroke:#65a9e8,color:#ffffff
    classDef audit fill:#353125,stroke:#d9b55b,color:#ffffff
    classDef limited fill:#1f2227,stroke:#9ba4b1,color:#c4c9d1,stroke-width:2px,stroke-dasharray:8 6

    style system fill:#030303,stroke:#6b7280,stroke-width:3px
    style main_row fill:#050607,stroke:#6b7280,stroke-width:2px
    style upper_flow fill:#050607,stroke:#61d095,stroke-width:2px
    style lower_flow fill:#070605,stroke:#e4bd55,stroke-width:2px
    style reference fill:#07090c,stroke:#9ba4b1,stroke-width:2px
    style intake fill:#070d09,stroke:#61d095,stroke-width:2px
    style hermes_plane fill:#0c0912,stroke:#c1a7ff,stroke-width:3px
    style controls fill:#0d0b07,stroke:#e4bd55,stroke-width:2px
    style response fill:#090806,stroke:#e4bd55,stroke-width:3px
    style response_paths fill:#070707,stroke:#6b7280,stroke-width:1px
    style immediate_paths fill:#080706,stroke:#8b8170,stroke-width:1px
    style operational_path fill:#0d0b07,stroke:#e4bd55,stroke-width:2px
    style escalation_path fill:#0d090a,stroke:#ff6b78,stroke-width:2px
    style code_path fill:#0b090d,stroke:#c1a7ff,stroke-width:2px
    style proposal_stage fill:#09080b,stroke:#9f86da,stroke-width:1px
    style pr_stage fill:#07100a,stroke:#78c89a,stroke-width:1px
    style outputs fill:#07100d,stroke:#78c89a,stroke-width:2px
