<!--
livebook:{"persist_outputs":true}
-->

[![Run in Livebook](https://livebook.dev/badge/v1/blue.svg)](https://livebook.dev/run?url=https://raw.githubusercontent.com/baselabs/agent_blueprint_protocol/v1.1.0/docs/livebooks/conformance-and-attacks.livemd)

# Conformance and attacks

This notebook runs the shipped corpus and inspects one case. It also presents the external JSON exchange as data and demonstrates fail-closed denials.

## Before you run

Use a tested combination: Elixir 1.19 with Erlang/OTP 28, or Elixir 1.20 with OTP 27, 28, or 29. You also need an internet connection for the first Hex install. Open this notebook with its Run in Livebook badge, choose a compatible runtime, then evaluate cells from top to bottom. No repository checkout is needed; examples and corpus files are read from the installed Hex package. The `~> 1.1.0` requirement permits later 1.1 patch releases. To replay this release's recorded outputs, change the install requirement to `== 1.1.0`.

The first cell installs the published package. Resources are read from the installed application with `Application.app_dir/2`.

```elixir
Mix.install([{:agent_blueprint_protocol, "~> 1.1.0"}])
```

Locate the package files and load the shipped conformance corpus.

```elixir
true = File.regular?(Application.app_dir(:agent_blueprint_protocol, "priv/examples/echo-blueprint.json"))
corpus_root = Application.app_dir(:agent_blueprint_protocol, "priv/conformance")

files =
  for relative <- :filelib.wildcard(~c"**/*", String.to_charlist(corpus_root)),
      path = Path.join(corpus_root, relative),
      File.regular?(path),
      into: %{},
      do: {List.to_string(relative), File.read!(path)}

{:ok, corpus} = AgentBlueprintProtocol.Conformance.Corpus.load(files)
%{corpus_loaded: true}
```

<!-- livebook:{"output":true} -->

```
%{corpus_loaded: true}
```

## Run and inspect one case

The runner compares each case with its expected projection. Its results list includes each case identifier and agreement status.

```elixir
alias AgentBlueprintProtocol.Conformance.Runner

run_results = Runner.run(corpus)
case_results = Enum.flat_map(run_results, fn {_path, rows} -> rows end)

sample =
  case_results
  |> Enum.find(&(&1.case_id == "blueprint-decode-header-Ed25519"))

%{
  agreement: Enum.all?(case_results, & &1.agree),
  total: length(case_results),
  sample: %{case_id: sample.case_id, agree: sample.agree}
}
```

<!-- livebook:{"output":true} -->

```
%{
  total: 626,
  agreement: true,
  sample: %{case_id: "blueprint-decode-header-Ed25519", agree: true}
}
```

## External execution messages

The protocol uses one JSON object per line. These values show the harness handshake and one request and response; the notebook treats them as data and starts no process.

```elixir
metadata =
  Application.app_dir(:agent_blueprint_protocol, "priv/release-metadata.json")
  |> File.read!()
  |> JSON.decode!()

handshake_request =
  ~s({"corpus_digest":"#{metadata["corpus_digest"]}","protocol":"agent-blueprint-protocol-conformance-execution","registry_digest":"#{metadata["registry_digest"]}","version":1})

handshake_response =
  ~s({"corpus_digest":"#{metadata["corpus_digest"]}","protocol":"agent-blueprint-protocol-conformance-execution","registry_digest":"#{metadata["registry_digest"]}","version":1})

case_request =
  ~s({"id":"base64url-decode-valid","input":{"base64url":"-_8"},"surface":"base64url.decode"})

case_response =
  ~s({"id":"base64url-decode-valid","result":{"decoded":"-_8","verdict":"valid"}})

%{
  handshake_request: handshake_request,
  handshake_response: handshake_response,
  case_request: case_request,
  case_response: case_response
}
```

<!-- livebook:{"output":true} -->

```
%{
  handshake_request: "{\"corpus_digest\":\"sha-256:uwbwaxugSBQKC_3Byy86_3N-Z6RBCBvW7rP79SGmSLE\",\"protocol\":\"agent-blueprint-protocol-conformance-execution\",\"registry_digest\":\"sha-256:hreDA2_XbV5J9geMJDDYG5BB42AGkh9jmQ9bqHe8kRk\",\"version\":1}",
  handshake_response: "{\"corpus_digest\":\"sha-256:uwbwaxugSBQKC_3Byy86_3N-Z6RBCBvW7rP79SGmSLE\",\"protocol\":\"agent-blueprint-protocol-conformance-execution\",\"registry_digest\":\"sha-256:hreDA2_XbV5J9geMJDDYG5BB42AGkh9jmQ9bqHe8kRk\",\"version\":1}",
  case_request: "{\"id\":\"base64url-decode-valid\",\"input\":{\"base64url\":\"-_8\"},\"surface\":\"base64url.decode\"}",
  case_response: "{\"id\":\"base64url-decode-valid\",\"result\":{\"decoded\":\"-_8\",\"verdict\":\"valid\"}}"
}
```

## Run the external exchange in a terminal

Use Node.js 24 or newer and npm. In a new working directory, install the release and copy its shipped corpus into a named directory:

```sh
npm init -y
npm install @agent-blueprint-protocol/verifier@1.1.0
node --input-type=module -e "import fs from 'node:fs'; fs.cpSync('node_modules/@agent-blueprint-protocol/verifier/dist/corpus', 'conformance', {recursive: true});"
npx @agent-blueprint-protocol/verifier@1.1.0 conformance run --corpus ./conformance --results results.json -- node node_modules/@agent-blueprint-protocol/verifier/dist/kit/cli.mjs --serve --corpus ./conformance
node --input-type=module -e "import fs from 'node:fs'; const r=JSON.parse(fs.readFileSync('results.json','utf8')); console.log(r.format, r.results.length);"
```

The adapter command after `--` is started directly without a shell. A Windows `.cmd` or `.bat` launcher must be run through its interpreter, such as `cmd.exe /c`. The installed package command above uses `node` so the same adapter command runs on Linux, macOS, and Windows. If the adapter cannot be started, the harness names the startup failure and exits `18` (`early_exit`).

The harness performs the handshake and all request/response exchanges against the shipped adapter. It writes canonical `results.json` only after all expected responses and a clean adapter exit (a disagreement can still produce results, then exits nonzero). The inspection prints `agent-blueprint-protocol-conformance-results 626` for this release. Adapter stdout is reserved for protocol lines; diagnostics go to stderr. With `--results`, the harness writes the document to the file and reports `harness: agreement` on stderr. Do not redirect diagnostics into results.

Rerun with all claim metadata to produce an unsigned claim. These generic labels describe this demonstration; replace them with your implementation's actual source revision, version, platform, and run date when making your own claim:

```sh
npx @agent-blueprint-protocol/verifier@1.1.0 conformance run --corpus ./conformance --results results.json --claim claim.json --implementation example-verifier --implementation-version 1 --source https://github.com/baselabs/agent_blueprint_protocol/tree/v1.1.0 --platform node-24-linux --date 2026-11-12 -- node node_modules/@agent-blueprint-protocol/verifier/dist/kit/cli.mjs --serve --corpus ./conformance
node --input-type=module -e "import fs from 'node:fs'; const c=JSON.parse(fs.readFileSync('claim.json','utf8')); console.log(c.format, c.implementation, c.results_digest);"
```

The claim identifies the implementation, corpus, registry, and exact results bytes. It is unsigned and does not establish how the implementation computed its answers. Keep the results, claim, command, and source revision together. To test another implementation, replace the command after `--` with its adapter; that adapter must implement the documented execution protocol.

Exit `0` means agreement. Exit `2` is usage, corpus, or claim-input failure; `3` is an internal error; `10` to `19` identify exchange failures or disagreement. Treat every nonzero value, including an unfamiliar future value, as failure; see the [CI exit table](../../documentation/verifying-in-ci.md#exit-codes). Remove the copied corpus and generated results/claim when their scope ends, or retain them deliberately as your conformance deliverables.

The preceding Elixir strings illustrate messages; they are not a captured execution. The terminal commands above perform the actual exchange.

## Tampering and an unsupported revision

Changing a Blueprint member without updating its digest fails during decode. A revision outside the consumer's support set also fails negotiation.

```elixir
blueprint_bytes = File.read!(Application.app_dir(:agent_blueprint_protocol, "priv/examples/echo-blueprint.json"))
tampered_bytes = String.replace(blueprint_bytes, "example.demo/echo", "example.demo/evil")
tampered = AgentBlueprintProtocol.decode_blueprint(tampered_bytes)

{:ok, blueprint_value} = AgentBlueprintProtocol.Json.decode(blueprint_bytes)
{:object, blueprint_members} = blueprint_value

unsupported_value =
  {:object,
   List.keystore(blueprint_members, "protocol_revision", 0, {"protocol_revision", {:integer, 2}})}

support = %AgentBlueprintProtocol.Negotiation.Support{revisions: MapSet.new([1])}
unsupported = AgentBlueprintProtocol.negotiate(unsupported_value, support)

%{
  tamper:
    case tampered do
      {:error, error} -> error.code
      {:ok, _} -> :accepted
    end,
  revision:
    case unsupported do
      {:error, error} -> error.code
      {:ok, _} -> :accepted
    end
}
```

<!-- livebook:{"output":true} -->

```
%{revision: :protocol_revision_unsupported, tamper: :digest_mismatch}
```

## Strict Ed25519 check

The verifier checks canonical point encodings and requires the signature scalar to be below the Ed25519 group order. This example shows the simplest rejection: an all-zero signature returns a typed denial.

Create a throwaway key, sign one Blueprint input, then verify the same entry with a rejected signature value.

```elixir
(
  alias AgentBlueprintProtocol.{Base64Url, Blueprint, Canonicalization, Digest, Signature}
  alias AgentBlueprintProtocol.Signature.PublicKey

  {:ok, blueprint_value} = AgentBlueprintProtocol.Json.decode(blueprint_bytes)
  {:ok, blueprint} = Blueprint.from_value(blueprint_value)
  digest = Digest.to_tagged(Blueprint.content_digest(blueprint))
  {public_key, private_key} = :crypto.generate_key(:eddsa, :ed25519)

  protected =
    {:object,
     [
       {"alg", {:string, "Ed25519"}},
       {"b64", {:boolean, false}},
       {"crit", {:array, [{:string, "b64"}]}},
       {"kid", {:string, "example-check-key"}}
     ]}

  attributes =
    {:object,
     [
       {"algorithm", {:string, "Ed25519"}},
       {"content_digest", {:string, digest}},
       {"created_at", {:string, "2026-08-20T00:00:00Z"}},
       {"key_id", {:string, "example-check-key"}},
       {"purpose", {:string, "blueprint"}}
     ]}

  entry_with = fn bytes ->
    {:object,
     [
       {"protected", protected},
       {"signed_attributes", attributes},
       {"signature", {:string, Base64Url.encode(bytes)}}
     ]}
  end

  {:ok, input} = Signature.signing_input(entry_with.(<<0::512>>))
  valid_signature = :crypto.sign(:eddsa, :none, input, [private_key, :ed25519])
  key = %PublicKey{key_id: "example-check-key", algorithm: :ed25519, key: public_key}
  rejected = Signature.verify(entry_with.(<<0::512>>), [key])

  %{
    valid_shape_created: byte_size(valid_signature) == 64,
    rejected_code:
      case rejected do
        {:error, %AgentBlueprintProtocol.Error{code: code}} -> code
        {:ok, _} -> :accepted
      end
  }
)
```

<!-- livebook:{"output":true} -->

```
%{valid_shape_created: true, rejected_code: :signature_not_verified}
```

## Portability denials

Portable artifact values use logical identifiers and digests. This example shows the typed error for a forbidden member name.

```elixir
result =
  AgentBlueprintProtocol.Portability.scan({:object, [{"credential", {:string, "example"}}]})

%{
  code:
    case result do
      {:error, %AgentBlueprintProtocol.Error{code: code}} -> code
      :ok -> :accepted
    end
}
```

<!-- livebook:{"output":true} -->

```
%{code: :forbidden_portable_value}
```

## Next

- [Producing artifacts](producing-artifacts.livemd)
- [Host import](host-import.livemd)
- [Extensions and federation](extensions-and-federation.livemd)
- [Walkthrough](abp-walkthrough.livemd)
