|
| 1 | +defmodule StackLab.Examples.GnTenDistributedStack.Receipt do |
| 2 | + @moduledoc "Distributed gn-ten proof receipt." |
| 3 | + |
| 4 | + @enforce_keys [ |
| 5 | + :receipt_ref, |
| 6 | + :schema_version, |
| 7 | + :status, |
| 8 | + :profile, |
| 9 | + :topology_ref, |
| 10 | + :monolith_baseline_receipt_ref, |
| 11 | + :context_packet_ref, |
| 12 | + :context_packet_hash, |
| 13 | + :authority_ref, |
| 14 | + :trace_refs, |
| 15 | + :node_lab_run, |
| 16 | + :distributed_envelope_scan, |
| 17 | + :node_placement, |
| 18 | + :does_not_prove |
| 19 | + ] |
| 20 | + defstruct @enforce_keys |
| 21 | + |
| 22 | + @type t :: %__MODULE__{ |
| 23 | + receipt_ref: String.t(), |
| 24 | + schema_version: String.t(), |
| 25 | + status: :pass | :open_defect, |
| 26 | + profile: String.t(), |
| 27 | + topology_ref: String.t(), |
| 28 | + monolith_baseline_receipt_ref: String.t(), |
| 29 | + context_packet_ref: String.t(), |
| 30 | + context_packet_hash: String.t(), |
| 31 | + authority_ref: String.t(), |
| 32 | + trace_refs: [String.t()], |
| 33 | + node_lab_run: map(), |
| 34 | + distributed_envelope_scan: map(), |
| 35 | + node_placement: map(), |
| 36 | + does_not_prove: [String.t()] |
| 37 | + } |
| 38 | +end |
| 39 | + |
| 40 | +defmodule StackLab.Examples.GnTenDistributedStack do |
| 41 | + @moduledoc """ |
| 42 | + Local distributed gn-ten proof scenarios. |
| 43 | + """ |
| 44 | + |
| 45 | + alias StackLab.Examples.GnTenDistributedStack.Receipt |
| 46 | + |
| 47 | + @schema_version "stack_lab.gn_ten_distributed_stack.context_6_node.v1" |
| 48 | + @profile "context_6_node" |
| 49 | + @envelope_schema_version "stack_lab.distributed_envelope.v1" |
| 50 | + @context_roundtrip Module.concat([StackLab, Examples, ContextABIRoundtrip]) |
| 51 | + @envelope_scanner Module.concat([StackLab, GnTenNodeLab, EnvelopeScanner]) |
| 52 | + @runner Module.concat([StackLab, GnTenNodeLab, Runner]) |
| 53 | + @json Module.concat([Jason]) |
| 54 | + |
| 55 | + @spec run_context_6_node(keyword()) :: {:ok, Receipt.t()} | {:error, term()} |
| 56 | + def run_context_6_node(opts \\ []) when is_list(opts) do |
| 57 | + topology_path = Keyword.get_lazy(opts, :topology_path, &default_context_topology_path/0) |
| 58 | + state_path = Keyword.get_lazy(opts, :state_path, &default_state_path/0) |
| 59 | + |
| 60 | + with {:ok, _started} <- Application.ensure_all_started(:aitrace), |
| 61 | + {:ok, baseline} <- call(@context_roundtrip, :run, []), |
| 62 | + {:ok, node_lab_run} <- |
| 63 | + call(@runner, :up, [ |
| 64 | + topology_path, |
| 65 | + [ |
| 66 | + state_path: state_path, |
| 67 | + run_id: "context-6-node", |
| 68 | + keep?: false |
| 69 | + ] |
| 70 | + ]) do |
| 71 | + envelope_scan = |
| 72 | + call(@envelope_scanner, :scan_many, [ |
| 73 | + envelopes(baseline, node_lab_run), |
| 74 | + [supported_schema_versions: [@envelope_schema_version]] |
| 75 | + ]) |
| 76 | + |
| 77 | + {:ok, |
| 78 | + %Receipt{ |
| 79 | + receipt_ref: receipt_ref(baseline), |
| 80 | + schema_version: @schema_version, |
| 81 | + status: status(baseline, node_lab_run, envelope_scan), |
| 82 | + profile: @profile, |
| 83 | + topology_ref: node_lab_run["topology_ref"], |
| 84 | + monolith_baseline_receipt_ref: baseline.receipt_ref, |
| 85 | + context_packet_ref: baseline.context_packet_ref, |
| 86 | + context_packet_hash: baseline.context_packet_hash, |
| 87 | + authority_ref: baseline.authority_ref, |
| 88 | + trace_refs: baseline.trace_refs, |
| 89 | + node_lab_run: node_lab_run, |
| 90 | + distributed_envelope_scan: envelope_scan, |
| 91 | + node_placement: node_placement(node_lab_run), |
| 92 | + does_not_prove: [ |
| 93 | + "production distribution security", |
| 94 | + "release artifact boot", |
| 95 | + "live provider behavior", |
| 96 | + "fault recovery", |
| 97 | + "TRINITY or GEPA quality" |
| 98 | + ] |
| 99 | + }} |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + @spec to_map(Receipt.t()) :: map() |
| 104 | + def to_map(%Receipt{} = receipt), do: json_safe(receipt) |
| 105 | + |
| 106 | + @spec to_json!(Receipt.t()) :: String.t() |
| 107 | + def to_json!(%Receipt{} = receipt) do |
| 108 | + call(@json, :encode!, [to_map(receipt), [pretty: true]]) |
| 109 | + end |
| 110 | + |
| 111 | + defp envelopes(baseline, node_lab_run) do |
| 112 | + node_lab_run |
| 113 | + |> Map.fetch!("boot_receipts") |
| 114 | + |> Enum.map(fn node -> |
| 115 | + %{ |
| 116 | + envelope_ref: "distributed-envelope://#{node["node_id"]}", |
| 117 | + schema_version: @envelope_schema_version, |
| 118 | + tenant_ref: "tenant://context-abi/demo", |
| 119 | + correlation_ref: "corr://context-abi/distributed/#{node["node_id"]}", |
| 120 | + idempotency_key: "idem://context-abi/distributed/#{node["node_id"]}", |
| 121 | + origin_node_ref: "node://stack_lab/controller", |
| 122 | + target_profile: node["profile"], |
| 123 | + authority_ref: baseline.authority_ref, |
| 124 | + redaction_class: "bounded_refs_only", |
| 125 | + payload_mode: "refs_only", |
| 126 | + trace_ref: List.first(baseline.trace_refs), |
| 127 | + issued_at: "2026-05-25T00:00:00Z", |
| 128 | + context_packet_ref: baseline.context_packet_ref, |
| 129 | + context_packet_hash: baseline.context_packet_hash, |
| 130 | + owner_group_membership: node["owner_group_membership"] |
| 131 | + } |
| 132 | + end) |
| 133 | + end |
| 134 | + |
| 135 | + defp node_placement(node_lab_run) do |
| 136 | + boot_receipts = Map.fetch!(node_lab_run, "boot_receipts") |
| 137 | + node_names = Enum.map(boot_receipts, &Map.fetch!(&1, "node")) |
| 138 | + |
| 139 | + %{ |
| 140 | + controller_included?: true, |
| 141 | + domain_node_count: length(node_names), |
| 142 | + distinct_domain_nodes?: length(node_names) == length(Enum.uniq(node_names)), |
| 143 | + nodes: node_names |
| 144 | + } |
| 145 | + end |
| 146 | + |
| 147 | + defp status(baseline, node_lab_run, envelope_scan) do |
| 148 | + if baseline.status == :pass and node_lab_run["status"] == "pass" and |
| 149 | + envelope_scan["status"] == "pass" do |
| 150 | + :pass |
| 151 | + else |
| 152 | + :open_defect |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + defp receipt_ref(baseline) do |
| 157 | + suffix = |
| 158 | + baseline.context_packet_hash |
| 159 | + |> String.replace_prefix("sha256:", "") |
| 160 | + |> String.slice(0, 16) |
| 161 | + |
| 162 | + "gn-ten-distributed-context://#{suffix}" |
| 163 | + end |
| 164 | + |
| 165 | + defp default_context_topology_path do |
| 166 | + Path.expand("../../../priv/topologies/context_6_node.exs", __DIR__) |
| 167 | + end |
| 168 | + |
| 169 | + defp default_state_path do |
| 170 | + Path.join(System.tmp_dir!(), "stack_lab_gn_ten_distributed_context_6_node.json") |
| 171 | + end |
| 172 | + |
| 173 | + defp json_safe(%_struct{} = value), do: value |> Map.from_struct() |> json_safe() |
| 174 | + |
| 175 | + defp json_safe(value) when is_map(value) do |
| 176 | + Map.new(value, fn {key, nested} -> {to_string(key), json_safe(nested)} end) |
| 177 | + end |
| 178 | + |
| 179 | + defp json_safe(values) when is_list(values), do: Enum.map(values, &json_safe/1) |
| 180 | + defp json_safe(value) when is_atom(value), do: Atom.to_string(value) |
| 181 | + defp json_safe(value), do: value |
| 182 | + |
| 183 | + defp call(module, function, args) |
| 184 | + when is_atom(module) and is_atom(function) and is_list(args) do |
| 185 | + unless Code.ensure_loaded?(module) do |
| 186 | + raise ArgumentError, "required module is unavailable: #{inspect(module)}" |
| 187 | + end |
| 188 | + |
| 189 | + apply(module, function, args) |
| 190 | + end |
| 191 | +end |
0 commit comments