<!--
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/extensions-and-federation.livemd)

# Extensions and federation

This notebook inspects the extension registry and negotiation outcomes, then decodes the shipped Federation envelope and exercises its supported state and carrier codecs.

## 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 and decode the shipped Blueprint and Federation example files.

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

{:ok, base_value} =
  File.read!(Application.app_dir(:agent_blueprint_protocol, "priv/examples/echo-blueprint.json"))
  |> AgentBlueprintProtocol.Json.decode()

{:ok, federation} =
  AgentBlueprintProtocol.decode_federation_envelope(
    File.read!(Application.app_dir(:agent_blueprint_protocol, "priv/examples/federation-envelope.json"))
  )

%{blueprint_value: true, envelope_decoded: true}
```

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

```
%{blueprint_value: true, envelope_decoded: true}
```

## Inspect the registry

Registry states may grow. This reader matches the names used here and preserves an unknown state as unknown.

The shipped registry contains only the active and deprecated federation registrations. Read both entries.

```elixir
alias AgentBlueprintProtocol.ExtensionRegistry

registry = ExtensionRegistry.registered_extensions()

registry_view =
  Enum.map(registry, fn entry ->
    state =
      case entry.state do
        :active -> :active
        :deprecated -> :deprecated
        :retired -> :retired
        other -> {:unknown_state, other}
      end

    {entry.namespace, entry.criticality, state, entry.schema_digest != nil}
  end)

registry_view
```

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

```
[
  {"com.example/federation", :critical, :deprecated, true},
  {"io.github.baselabs/federation", :critical, :active, true}
]
```

## Negotiate extension outcomes

The helper composes each extension map from the shipped Blueprint value and recomputes the Blueprint digest. A supplied support registry can include a retired namespace without changing the compiled registry.

Create Blueprints for active critical, deprecated, unknown, and retired namespace cases, then negotiate each one.

```elixir
alias AgentBlueprintProtocol.{Blueprint, Canonicalization, Digest, Negotiation}
alias AgentBlueprintProtocol.ExtensionRegistry

put_member = fn {:object, members}, name, value ->
  {:object, List.keystore(members, name, 0, {name, value})}
end

make_blueprint = fn regions ->
  value = put_member.(base_value, "extensions", regions)
  {:ok, draft} = Blueprint.from_value(value)
  digest = draft |> Blueprint.content_digest() |> Digest.to_tagged()
  value = put_member.(value, "content_digest", {:string, digest})
  {:ok, bytes} = Canonicalization.encode(value)
  AgentBlueprintProtocol.decode_blueprint(bytes)
end

empty_body = {:object, []}

regions = fn critical, optional ->
  {:object,
   [
     {"critical", {:object, critical}},
     {"optional", {:object, optional}}
   ]}
end

{:ok, active_critical} =
  make_blueprint.(regions.([{"io.github.baselabs/federation", empty_body}], []))

{:ok, deprecated_critical} =
  make_blueprint.(regions.([{"com.example/federation", empty_body}], []))

{:ok, unknown_optional} =
  make_blueprint.(regions.([], [{"com.example.vendor/unknown", empty_body}]))

{:ok, unknown_critical} =
  make_blueprint.(regions.([{"com.example.vendor/absent", empty_body}], []))

retired_entry = %ExtensionRegistry{
  namespace: "com.example.vendor/archive",
  owner: "ExampleVendor",
  criticality: :optional,
  state: :retired,
  schema_digest: nil,
  a2a_uri: "https://example.com/extensions/archive",
  promoted_at_revision: nil
}

{:ok, retired_optional} =
  make_blueprint.(regions.([], [{"com.example.vendor/archive", empty_body}]))

{:ok, blueprint_members} = AgentBlueprintProtocol.Blueprint.members(1)
{:ok, deployment_members} = AgentBlueprintProtocol.Deployment.members(1)
core_fields = MapSet.new(blueprint_members ++ deployment_members)
support = %Negotiation.Support{
  revisions: MapSet.new([1]),
  core_fields: core_fields,
  registry: %{"com.example.vendor/archive" => retired_entry},
  schemas: %{
    "com.example/federation" => ExtensionRegistry.federation_schema(),
    "io.github.baselabs/federation" => ExtensionRegistry.federation_schema()
  }
}

outcome = fn result ->
  case result do
    {:ok, value} ->
      Enum.map(value.notices, fn
        :extension_deprecated -> :deprecated
        :extension_retired -> :retired
        :extension_unknown_optional_retained -> :unknown_optional
        other -> {:unknown_notice, other}
      end)

    {:error, error} ->
      error.code
  end
end

%{
  active_critical: outcome.(AgentBlueprintProtocol.negotiate(active_critical, support)),
  deprecated_critical: outcome.(AgentBlueprintProtocol.negotiate(deprecated_critical, support)),
  unknown_optional: outcome.(AgentBlueprintProtocol.negotiate(unknown_optional, support)),
  unknown_critical: outcome.(AgentBlueprintProtocol.negotiate(unknown_critical, support)),
  retired_optional: outcome.(AgentBlueprintProtocol.negotiate(retired_optional, support))
}
```

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

```
%{
  unknown_optional: [:unknown_optional],
  active_critical: [],
  deprecated_critical: [:deprecated],
  unknown_critical: :extension_unknown_critical,
  retired_optional: [:retired]
}
```

## Supply a schema-pinned critical extension

The active federation namespace has a registered schema digest. Negotiation checks the supplied schema against that registry entry.

Negotiate an empty federation extension body using the registered schema document.

```elixir
federation_namespace = "io.github.baselabs/federation"
schema = ExtensionRegistry.federation_schema()

schema_support = %Negotiation.Support{
  revisions: MapSet.new([1]),
  core_fields: core_fields,
  schemas: %{federation_namespace => schema}
}

%{
  schema_available: is_map(schema_support.schemas),
  negotiated:
    match?(
      {:ok, %Negotiation.Outcome{critical_extensions: [^federation_namespace]}},
      AgentBlueprintProtocol.negotiate(active_critical, schema_support)
    )
}
```

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

```
%{schema_available: true, negotiated: true}
```

## Register a host-owned critical namespace

The host supplies a generic example registration and its schema document. It
computes the pin from canonical schema bytes. The compiled registry is unchanged.

```elixir
namespace = "com.example.vendor/record"

{:ok, document} =
  AgentBlueprintProtocol.Json.decode(
    ~s({"additionalProperties":false,"properties":{"value":{"type":"string"}},"required":["value"],"type":"object"})
  )

{:ok, schema_bytes} = Canonicalization.encode(document)
pin = Digest.to_tagged(Digest.hash(:extension_schema, schema_bytes))

host_entry = %ExtensionRegistry{
  namespace: namespace,
  owner: "ExampleVendor",
  criticality: :critical,
  state: :active,
  schema_digest: pin,
  a2a_uri: "https://example.com/extensions/record",
  promoted_at_revision: nil
}

host_support = %Negotiation.Support{
  revisions: MapSet.new([1]),
  core_fields: core_fields,
  registry: %{namespace => host_entry},
  schemas: %{namespace => document}
}

{:ok, host_blueprint} =
  make_blueprint.(regions.([{namespace, {:object, [{"value", {:string, "one"}}]}}], []))

%{
  registered: outcome.(AgentBlueprintProtocol.negotiate(host_blueprint, host_support)),
  missing_registration:
    outcome.(
      AgentBlueprintProtocol.negotiate(host_blueprint, %Negotiation.Support{
        revisions: MapSet.new([1])
      })
    ),
  missing_schema:
    outcome.(AgentBlueprintProtocol.negotiate(host_blueprint, %{host_support | schemas: %{}})),
  mismatched_schema:
    outcome.(
      AgentBlueprintProtocol.negotiate(host_blueprint, %{
        host_support
        | schemas: %{namespace => {:object, [{"type", {:string, "object"}}]}}
      })
    ),
  shipped_entries: length(ExtensionRegistry.registered_extensions())
}
```

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

```
%{
  registered: [],
  missing_registration: :extension_unknown_critical,
  missing_schema: :extension_schema_unavailable,
  mismatched_schema: :extension_schema_digest_mismatch,
  shipped_entries: 2
}
```

## Read the shipped envelope and state codecs

The decoder reads the package example. The state codecs map a logical state to each carrier vocabulary, and the carrier codecs round-trip the decoded envelope.

```elixir
alias AgentBlueprintProtocol.Federation

{:ok, envelope_bytes} = Federation.canonical_bytes(federation)
{:ok, a2a_carrier} = Federation.to_a2a_carrier(federation)
{:ok, mcp_carrier} = Federation.to_mcp_carrier(federation)
{:ok, a2a_round_trip} = Federation.from_a2a_carrier(a2a_carrier)
{:ok, mcp_round_trip} = Federation.from_mcp_carrier(mcp_carrier)

{:ok, a2a_state} = Federation.from_a2a_state("TASK_STATE_COMPLETED")
{:ok, mcp_state} = Federation.from_mcp_state("completed")
{:ok, a2a_spelling} = Federation.to_a2a_state(a2a_state)
{:ok, mcp_spelling} = Federation.to_mcp_state(mcp_state)

%{
  envelope_decoded: is_struct(federation, Federation),
  state_codecs_agree: a2a_state == mcp_state,
  a2a_spelling: a2a_spelling,
  mcp_spelling: mcp_spelling,
  a2a_round_trip: Federation.canonical_bytes(a2a_round_trip) == {:ok, envelope_bytes},
  mcp_round_trip: Federation.canonical_bytes(mcp_round_trip) == {:ok, envelope_bytes}
}
```

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

```
%{
  envelope_decoded: true,
  a2a_round_trip: true,
  mcp_round_trip: true,
  a2a_spelling: "TASK_STATE_COMPLETED",
  mcp_spelling: "completed",
  state_codecs_agree: true
}
```

## Inspect native carrier fields

The carriers above are tagged JSON objects. Convert them to ordinary JSON for a selected reading excerpt; the omitted extension members remain present in the complete carrier. The Elixir codecs create and consume these values. npm 1.1.0 exports envelope decoding and mapping rows, but no state/carrier/commitment helpers; use only its package root.

```elixir
{:ok, a2a_json} = Canonicalization.encode(a2a_carrier)
{:ok, mcp_json} = Canonicalization.encode(mcp_carrier)
a2a_plain = JSON.decode!(a2a_json)
mcp_plain = JSON.decode!(mcp_json)
a2a_key = "https://baselabs.github.io/agent_blueprint_protocol/extensions/federation"
mcp_key = "io.github.baselabs/federation"
{:ok, :canceled} = Federation.from_mcp_state("cancelled")
{:ok, "TASK_STATE_CANCELED"} = Federation.to_a2a_state(:canceled)
{:error, state_error} = Federation.from_a2a_state("TASK_STATE_UNSPECIFIED")

%{
  a2a:
    Map.put(Map.take(a2a_plain, ["id", "status"]), "metadata", %{
      a2a_key => Map.take(a2a_plain["metadata"][a2a_key], ["blueprint_digest"])
    }),
  mcp:
    Map.put(Map.take(mcp_plain, ["taskId", "status"]), "_meta", %{
      mcp_key => Map.take(mcp_plain["_meta"][mcp_key], ["blueprint_digest"])
    }),
  unmappable: state_error.code
}
```

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

```
%{
  unmappable: :federation_state_unmappable,
  a2a: %{
    "id" => "task-7f3a2c",
    "metadata" => %{
      "https://baselabs.github.io/agent_blueprint_protocol/extensions/federation" => %{
        "blueprint_digest" => "sha-256:UqU_xO8mUo_SEKFyllPQx5viwqjUU0jymkOUDjoSJVY"
      }
    },
    "status" => %{"state" => "TASK_STATE_COMPLETED"}
  },
  mcp: %{
    "_meta" => %{
      "io.github.baselabs/federation" => %{
        "blueprint_digest" => "sha-256:UqU_xO8mUo_SEKFyllPQx5viwqjUU0jymkOUDjoSJVY"
      }
    },
    "status" => "completed",
    "taskId" => "task-7f3a2c"
  }
}
```

MCP spells the canceled state `cancelled`; the logical state and A2A use `canceled`/`TASK_STATE_CANCELED`. `TASK_STATE_UNSPECIFIED` has no defined mapping.

## Read the mapping table

The mapping rows are data. Match the known row fields and keep a catch-all for rows with a future shape.

```elixir
mapping =
  AgentBlueprintProtocol.federation_mapping()
  |> Enum.map(fn
    %{logical_field: field, a2a_location: a2a, mcp_location: mcp, verdict: verdict} ->
      {field, a2a, mcp, verdict}

    other ->
      {:unknown_row, is_struct(other)}
  end)

mapping
```

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

```
[
  {"task_identity", "Task.id (a2a.proto:170)", "Task.taskId (mcp-schema.ts:47)", :native},
  {"idempotency_identity",
   "none: Message.message_id (a2a.proto:262) is a message id, not an idempotency key",
   "none", :extension},
  {"parent_execution_reference",
   "Message.reference_task_ids (a2a.proto:276), Task.context_id (a2a.proto:173): a reference, not a parent execution",
   "none", :partial},
  {"initiating_subject",
   "none: SecurityScheme/SecurityRequirement (a2a.proto:495-517) are transport-level",
   "none: transport auth", :extension},
  {"blueprint_digest", "none", "none", :extension},
  {"deployment_digest", "none", "none", :extension},
  {"input_commitment",
   "Message.parts[].data (a2a.proto:233), Part.metadata (a2a.proto:236): a payload home, not a digest commitment",
   "request params", :partial},
  {"result_schema",
   "Part.media_type (a2a.proto:241), AgentSkill.output_modes (a2a.proto:449): media type is not a schema identity",
   "CompletedTask.result {[key: string]: unknown} (mcp-schema.ts:135)", :extension},
  {"result_classification_ceiling", "none", "none", :extension},
  {"time_policy", "SendMessageConfiguration (a2a.proto:143-161) has no time bound",
   "Task.ttlMs (mcp-schema.ts:82): a storage TTL, NOT an execution bound", :partial},
  {"resource_policy", "none", "none", :extension},
  {"recovery_handle", "Task.id + GetTask (a2a.proto:45)",
   "taskId + tasks/get (mcp-tasks.md Task Polling)", :native},
  {"issuer", "transport-level only (SecurityScheme)", "transport auth", :extension},
  {"subject", "none", "none", :extension},
  {"audience", "none", "none", :extension},
  {"identity_mapping_evidence", "none", "none", :extension},
  {"checkpoint_request",
   "TASK_STATE_INPUT_REQUIRED (a2a.proto:201) + TaskStatus.message: free-form, not typed",
   "status input_required + inputRequests: InputRequest union (mcp-schema.ts:111-119, mcp-spec.types.ts:545)",
   :partial},
  {"checkpoint_status", "TaskStatus.state (a2a.proto:213)",
   "Task.status (mcp-schema.ts:31-36)", :native},
  {"checkpoint_commitment", "none",
   "tasks/update inputResponses (mcp-schema.ts:232-246) carries responses, no commitment digest",
   :extension},
  {"terminal_state", "TASK_STATE_COMPLETED/FAILED/CANCELED/REJECTED (a2a.proto:194-205)",
   "completed/failed/cancelled (mcp-schema.ts:31-36): lossy both directions", :partial},
  {"evidence_receipt",
   "AgentCardSignature (a2a.proto:455-467) signs the AgentCard, not a task result",
   "none", :extension},
  {"compatibility_reference",
   "AgentCard.version (a2a.proto:374-376): a version string, not an exact build identity",
   "none", :extension},
  {"authority_proof_references", "transport-level only", "transport auth", :extension}
]
```

## Check shipped commitment fixtures

The shipped conformance cases demonstrate a valid terminal commitment and a conflicting terminal commitment. The receiver supplies public `keys` or an `issuer_key_sets` map (which takes precedence), optional exact `issuer`/`subject`/`audience` pins, inclusive `created_after`/`created_before` timestamp strings, and previously verified `prior_receipts`. See the [receiver-context field table](../../documentation/federation.md#receiver-context-and-returned-facts). Success returns the task identity, terminal state, and terminal commitment digest as a verified fact; it supplies no host execution behavior.

```elixir
case_path =
  Path.join([
    Application.app_dir(:agent_blueprint_protocol, "priv/conformance"),
    "cases",
    "federation-verify_commitment.json"
  ])

fixture_cases = JSON.decode!(File.read!(case_path))["cases"]
valid_case = Enum.find(fixture_cases, &(&1["id"] == "commitment-valid"))
conflict_case = Enum.find(fixture_cases, &(&1["id"] == "commitment-terminal-conflict"))

key_from = fn item ->
  {:ok, key_bytes} = Base.url_decode64(item["key"], padding: false)

  %AgentBlueprintProtocol.Signature.PublicKey{
    key_id: item["key_id"],
    algorithm: :ed25519,
    key: key_bytes
  }
end

{:ok, valid_envelope} = Federation.decode(valid_case["input"]["envelope"])
valid_keys = Enum.map(valid_case["input"]["context"]["keys"], key_from)

valid_result =
  Federation.verify_commitment(valid_envelope, %Federation.Context{keys: valid_keys})

{:ok, conflict_envelope} = Federation.decode(conflict_case["input"]["envelope"])
conflict_context = conflict_case["input"]["context"]
conflict_keys = Enum.map(conflict_context["keys"], key_from)

prior =
  Enum.map(conflict_context["prior_receipts"], fn item ->
    {:ok, digest} = AgentBlueprintProtocol.Digest.from_tagged(item["commitment"])

    %{
      task_identity: item["task_identity"],
      terminal_state: item["terminal_state"],
      terminal_commitment: digest
    }
  end)

conflict_result =
  Federation.verify_commitment(
    conflict_envelope,
    %Federation.Context{keys: conflict_keys, prior_receipts: prior}
  )

%{
  valid_fixture:
    case valid_result do
      {:ok, %{task_identity: task, terminal_state: state, terminal_commitment: digest}} ->
        %{
          task_identity: task,
          terminal_state: state,
          terminal_commitment: Digest.to_tagged(digest)
        }

      {:error, error} ->
        {:denied, error.code}
    end,
  conflict_fixture:
    case conflict_result do
      {:error, error} -> error.code
      {:ok, _} -> :accepted
    end
}
```

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

```
%{
  valid_fixture: %{
    task_identity: "task-7f3a2c",
    terminal_commitment: "sha-256:Ub1fvxQros48cb7vgXGpEzQwww5-DSZn9MDkY5XU5VU",
    terminal_state: "completed"
  },
  conflict_fixture: :federation_terminal_conflict
}
```

## Next

- [Producing artifacts](producing-artifacts.livemd)
- [Host import](host-import.livemd)
- [Conformance and attacks](conformance-and-attacks.livemd)
- [Walkthrough](abp-walkthrough.livemd)
