|
| 1 | +# SPDX-FileCopyrightText: Sudo Apt Holdings LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +defmodule Mix.Tasks.Trinity.Effects.Catalog do |
| 4 | + @shortdoc "Prints the effect catalogue, or checks the committed artifact against the tree" |
| 5 | + |
| 6 | + @moduledoc """ |
| 7 | + The effect catalogue as a published, versioned artifact (slice 027 AC3 and AC4). |
| 8 | +
|
| 9 | + The population that can cause an effect is the thing a reader of this project most needs and |
| 10 | + is least able to derive: it is spread across a behaviour, a config list, a closed catalogue |
| 11 | + module and an authority adapter. This task collects it into one file, `docs/effects-catalog.md`, |
| 12 | + and the gate fails when that file and the tree disagree. A reader does not have to trust the |
| 13 | + file, because `mix trinity.effects.catalog --check` is the same check they can run. |
| 14 | +
|
| 15 | + **The version is the content.** It is a SHA-256 over the rows, not a number anyone types, so |
| 16 | + two trees with the same effect surface have the same version and a tree that adds a tool has a |
| 17 | + different one whether or not anybody remembered to say so. |
| 18 | +
|
| 19 | + **Read at compile time, not from the registry.** The population is the configured core modules |
| 20 | + and their behaviour callbacks. The running registry also holds dynamic tools (MCP, skills), |
| 21 | + which are exactly what this artifact is a statement *against*: they can propose, and they can |
| 22 | + never be in this list. Reading the registry would let a runtime registration into a |
| 23 | + compile-time claim. |
| 24 | +
|
| 25 | + **`--write` and `--check` refuse outside `MIX_ENV=prod`, and that is the point of them.** The |
| 26 | + artifact is a claim about what *ships*. `config/test.exs` adds scaffolding tools to the same list, |
| 27 | + so a catalogue generated under `:test` names them beside `fs_read`, and a gate running in `:test` |
| 28 | + would then compare a test surface against a production one and fail forever, or, worse, be |
| 29 | + "fixed" by committing the test surface as though it were the product. Measured while writing this |
| 30 | + task: under `:test` the list is five entries longer. Printing works in any environment and stamps |
| 31 | + the environment it read. |
| 32 | +
|
| 33 | + The first draft of this paragraph named one of those scaffolding modules as an example, and slice |
| 34 | + 020's census (`test/trinity/tools/registry_test.exs`, "no core module references a test tool") |
| 35 | + failed the build for it. That census is right and the example was not worth it: a shipped tree |
| 36 | + that names its own test scaffolding has a seam a reader has to check. |
| 37 | +
|
| 38 | + ## Usage |
| 39 | +
|
| 40 | + mix trinity.effects.catalog # print |
| 41 | + mix trinity.effects.catalog --check # exit 1 if docs/effects-catalog.md differs |
| 42 | + mix trinity.effects.catalog --write # regenerate docs/effects-catalog.md |
| 43 | + """ |
| 44 | + |
| 45 | + use Boundary, classify_to: Trinity |
| 46 | + use Mix.Task |
| 47 | + |
| 48 | + @artifact "docs/effects-catalog.md" |
| 49 | + |
| 50 | + @impl Mix.Task |
| 51 | + def run(argv) do |
| 52 | + Mix.Task.run("app.config") |
| 53 | + rendered = render() |
| 54 | + |
| 55 | + cond do |
| 56 | + "--check" in argv -> prod!("--check") && check(rendered) |
| 57 | + "--write" in argv -> prod!("--write") && write(rendered) |
| 58 | + true -> Mix.shell().info(rendered) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + defp prod!(flag) do |
| 63 | + if Mix.env() == :prod do |
| 64 | + true |
| 65 | + else |
| 66 | + Mix.raise( |
| 67 | + "#{flag} needs MIX_ENV=prod. This artifact is a claim about what ships, and " <> |
| 68 | + "config/test.exs adds test tools to the same list, so a catalogue built under " <> |
| 69 | + "#{inspect(Mix.env())} would name them. Run `MIX_ENV=prod mix trinity.effects.catalog " <> |
| 70 | + "#{flag}`." |
| 71 | + ) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + @doc "Every configured core tool as a row, sorted by name." |
| 76 | + @spec rows() :: [map()] |
| 77 | + def rows do |
| 78 | + :trinity |
| 79 | + |> Application.get_env(:tools, []) |
| 80 | + |> Keyword.get(:modules, []) |
| 81 | + |> Enum.map(&row/1) |
| 82 | + |> Enum.sort_by(& &1.name) |
| 83 | + end |
| 84 | + |
| 85 | + @doc "The catalogue's version: a SHA-256 over the rows, so the version is the content." |
| 86 | + @spec version([map()]) :: String.t() |
| 87 | + def version(rows) do |
| 88 | + rows |
| 89 | + |> Enum.map_join( |
| 90 | + "\n", |
| 91 | + &"#{&1.name}\t#{&1.risk}\t#{&1.effect}\t#{&1.catalogued}\t#{&1.escalates}" |
| 92 | + ) |
| 93 | + |> then(&:crypto.hash(:sha256, &1)) |
| 94 | + |> Base.encode16(case: :lower) |
| 95 | + |> binary_part(0, 16) |
| 96 | + end |
| 97 | + |
| 98 | + defp row(module) do |
| 99 | + Code.ensure_loaded!(module) |
| 100 | + |
| 101 | + %{ |
| 102 | + name: module.name(), |
| 103 | + risk: module.risk(), |
| 104 | + effect: module.effect(), |
| 105 | + catalogued: module.name() in Trinity.Tools.Catalog.names(), |
| 106 | + escalates: function_exported?(module, :escalate, 2), |
| 107 | + module: inspect(module) |
| 108 | + } |
| 109 | + end |
| 110 | + |
| 111 | + defp render do |
| 112 | + rows = rows() |
| 113 | + |
| 114 | + """ |
| 115 | + <!-- SPDX-FileCopyrightText: Sudo Apt Holdings LLC --> |
| 116 | + <!-- SPDX-License-Identifier: Apache-2.0 --> |
| 117 | + <!-- Generated by `mix trinity.effects.catalog --write`. Do not edit by hand: the gate |
| 118 | + regenerates it and fails when this file and the tree disagree. --> |
| 119 | + # The effect catalogue |
| 120 | +
|
| 121 | + Version `#{version(rows)}`, which is a SHA-256 over the rows below rather than a number anyone |
| 122 | + typed. Two trees with the same effect surface carry the same version. Built from `MIX_ENV=#{Mix.env()}`, |
| 123 | + which `--write` and `--check` require to be `prod`, because this is a statement about what ships. |
| 124 | +
|
| 125 | + This is the population that can cause an effect, in one place. It is otherwise spread across a |
| 126 | + behaviour (`Trinity.Tools.Tool`), a configuration list, a closed catalogue module |
| 127 | + (`Trinity.Tools.Catalog`) and an authority adapter, and a reader would have to assemble it to |
| 128 | + check any claim this project makes about least privilege. |
| 129 | +
|
| 130 | + What is *not* here is the point of it. Tools registered at runtime, from an MCP server or a |
| 131 | + skill, never appear: they can propose a call, and the membrane refuses an effectful one that is |
| 132 | + not in this list. `mix trinity.effects.catalog --check` is in the quality gate, so a tool added |
| 133 | + without this file changing fails the build. |
| 134 | +
|
| 135 | + | Tool | Risk tier | Effect class | In the closed catalogue | Can escalate | Module | |
| 136 | + |---|---|---|---|---|---| |
| 137 | + #{Enum.map_join(rows, "\n", &line/1)} |
| 138 | +
|
| 139 | + ## How to read the columns |
| 140 | +
|
| 141 | + - **Risk tier** is a function of the tool's *name* alone. Arguments can raise what a call |
| 142 | + requires (the next column); they can never change the tier. |
| 143 | + - **Effect class** is what the membrane reads. `:none` never reaches `Trinity.Effects`; |
| 144 | + `:artifact` and `:catalog` both cross it and both leave a signed receipt. |
| 145 | + - **In the closed catalogue** means the name is in `Trinity.Tools.Catalog`, the compile-time |
| 146 | + list of external effects. The registry refuses a runtime registration claiming `:catalog`. |
| 147 | + - **Can escalate** means the tool implements `escalate/2`, which may raise a single call's |
| 148 | + required tier from its arguments. It is one-way by the behaviour's contract. |
| 149 | +
|
| 150 | + ## Where execution happens |
| 151 | +
|
| 152 | + Under the default authority (`TRINITY_AUTHORITY=local`, ADR-0010) `Trinity.Authority.Local` is |
| 153 | + the executor and runs the tool's own `execute/2`. Under an external authority adapter Trinity |
| 154 | + keeps no executor for catalogued effects: it proposes, the adapter decides and acts, and the |
| 155 | + census test for that mode asserts the absence rather than describing it. The mode is chosen |
| 156 | + once at boot, recorded in the boot receipt, and no runtime path changes it. |
| 157 | + """ |
| 158 | + end |
| 159 | + |
| 160 | + defp line(r) do |
| 161 | + "| `#{r.name}` | `#{r.risk}` | `#{r.effect}` | #{yes(r.catalogued)} | #{yes(r.escalates)} | `#{r.module}` |" |
| 162 | + end |
| 163 | + |
| 164 | + defp yes(true), do: "yes" |
| 165 | + defp yes(false), do: "no" |
| 166 | + |
| 167 | + defp write(rendered) do |
| 168 | + File.write!(@artifact, rendered) |
| 169 | + Mix.shell().info("trinity.effects.catalog: wrote #{@artifact}") |
| 170 | + end |
| 171 | + |
| 172 | + defp check(rendered) do |
| 173 | + case File.read(@artifact) do |
| 174 | + {:ok, ^rendered} -> |
| 175 | + Mix.shell().info("trinity.effects.catalog: OK. #{@artifact} matches the tree") |
| 176 | + |
| 177 | + {:ok, _other} -> |
| 178 | + Mix.raise( |
| 179 | + "#{@artifact} does not match the tree. The effect surface changed and the published " <> |
| 180 | + "artifact did not. Run `mix trinity.effects.catalog --write` and commit the result." |
| 181 | + ) |
| 182 | + |
| 183 | + {:error, :enoent} -> |
| 184 | + Mix.raise("#{@artifact} is missing. Run `mix trinity.effects.catalog --write`.") |
| 185 | + end |
| 186 | + end |
| 187 | +end |
0 commit comments