Skip to content

feat(identitybroker): load agent definition records for the broker - #2829

Merged
rdimitrov merged 8 commits into
mainfrom
feat/identitybroker-definition-loader
Nov 18, 2026
Merged

rdimitrov merged 8 commits into
mainfrom
feat/identitybroker-definition-loader

Conversation

@rdimitrov

@rdimitrov rdimitrov commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

The identity broker cannot tell which agent definitions exist: identitybroker.New takes []DefinitionRecord, but startup passes nil, and the built-in definitions the design relies on exist nowhere as records. This adds the adapter that produces those records, ready for per-agent registration (#2527). It is not switched on: startup still passes no records until #2553.

The broker must not depend on the rest of Mecatl, because it will move into its own service. So the definition format moves out of the engine into a new module, agentdef, and the loader reads definitions with that module instead of the engine's parser.

  • New module agentdef (github.com/stacklok/mecatl/agentdef). It holds the definition format the broker and the harness share, and imports only the standard library and the YAML parser. It contains:

    • Definition and MCPServer;
    • Parse, which is the engine's agentfs parser moved over, with the same caps, list splitting, Bash to Shell alias, mcpServers handling, and value-free errors;
    • Canonical, the input of a definition digest. This is JSON with named keys and sorted maps, and it leaves empty fields out, so adding a field changes only the digests of definitions that set it. It rejects text that is not valid UTF-8. The form is versioned by FormVersion;
    • Digest, the digest rule itself: lowercase hexadecimal SHA-256 of Canonical, so the engine adopts the same rule rather than copying it.

    Only Parse, Canonical, Digest, the types and the limits are exported; the parser's helpers stay unexported until something outside needs them.

  • New adapter internal/identitybroker/adapter/definitionloader. Load(ctx, Config{Dirs, BuildID}) returns the []identitybroker.DefinitionRecord for identitybroker.New. It loads:

    • the built-in system/main and system/explorer. Their revision is the build stamp of a published build: a release tag such as v0.0.52, or a main build such as v0.0.53-main.812.1.gabcdef12. Local, dirty, and untagged builds get the fixed, documented dev (BuiltinRevision, pinned by tests);
    • the operator's global definitions, read from the explicit agent directories (the harness's --agents-dir) and parsed with agentdef.
  • Failure. Load fails when a configured directory is missing, is not a directory, or cannot be read, so a mistyped --agents-dir or an unmounted volume shows at startup. (The harness still treats a missing directory as harmless.) It then returns no records, built-ins included, plus the problems found so far. Whether that stops startup or degrades to built-ins only is Switch on real loading and registration #2553's decision.

  • Record fields:

    • source (global or system);
    • name;
    • revision;
    • provenance profile: file/v1 with the file path, or mecatl-release/v1;
    • provenance metadata definition-form: the canonical form version (agentdef.FormVersion) the digest covers, so a change of form shows in the record rather than only as new digests;
    • SourceDigest: SHA-256 of the bytes read;
    • DefinitionDigest: agentdef.Digest.

    The revision is never a digest.

  • Bad definitions. These are left out and reported as a Problem, and loading continues:

    • a file that cannot be read or parsed, such as a dangling symlink;
    • a file that is not valid UTF-8 anywhere, frontmatter included: the YAML parser reads an invalid byte as U+FFFD, so two different files could otherwise share a DefinitionDigest;
    • a definition whose record fails DefinitionRecord.Validate, such as a name outside the SPIFFE segment grammar.

    The first file to define a name wins it: directories in the order given, files in name order within one, the same rule the harness applies. A winner left out for a bad record still holds the name, so the broker never records a later file's definition under a name the harness gives to another.

  • Shared names. SourceGlobal, SourceSystem, BuiltinMain and BuiltinExplorer live in the broker core (internal/identitybroker), since they become SPIFFE ID segments that callers of the broker need too. The conformance suite uses them.

  • Depguard. The loader has its own strict rule: the standard library, the broker core, and agentdef. The new module has a strict rule too: the standard library and the YAML parser. The broker core stays free of file access.

  • Design doc. One sentence in docs/drafts/agent-identity/implementation.md ("Modulith constraints", "One tree") said every broker adapter but AS-1's allows only the standard library, the broker core, and the JWT library. It now records the loader's agentdef exception. The open agentdef design questions are not written into the design here.

  • Module registration. The module is registered in go.work and in the root go.mod, with a local replace like authn/oidc, so nothing needs publishing for this PR. It is also registered in integration/microvm's go.mod. The Taskfile, the CI build, lint and govulncheck steps, the release cache keys, and the k8s end-to-end path filters include it, and docs/module-development.md and docs/architecture.md list it.

No engine change. engine/ is identical to main. The harness keeps parsing with agentfs until a follow-up switches agentfs to agentdef. That follow-up needs agentdef tagged first, since the engine's standalone check builds without the workspace. Until then there are two copies of the parser. internal/app/agentdef_parity_test.go runs both over the same files and fails on any difference in fields, notes, or rejections. Nothing builds agents from the broker's records before #2548 (phase 3), so the two parses never meet in phase 2.

Issue relationship

Relates to #2526 (part of #2550). Not closing: the origin decision below needs confirmation first.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

Baseline checks

  • Linting (task lint), including the new module
  • Pre-review race suite (task test:race), every module including agentdef: see notes
  • Offline demo (go run ./cmd/mecademo): tool call, permission ask and approval, result
  • Markdown changed: docs generation/link checks (task docs)
  • User docs/user-facing behavior changed: site build (task site:build): no user-facing change
  • Guarded engine API affected: compatibility check (task api:check): no engine change
  • Intentional engine API change: task api:update + engine/CHANGELOG.md: not applicable

The tests cover:

  • agentdef parsing: the engine's parse tests, carried over unchanged apart from type names.
  • agentdef.Canonical and agentdef.Digest:
    • pinned bytes and the digest rule;
    • unset fields left out;
    • equal forms for two spellings of one definition;
    • UTF-8 rejection for every kind of text field, without quoting the value.
  • Parser parity: the agentfs/agentdef parity test, over 19 cases plus the engine's YAML compatibility fixture for agent frontmatter.
  • Built-ins: revisions and pinned digests.
  • Global records:
    • every record field and a pinned DefinitionDigest;
    • reformatting changes only SourceDigest;
    • an adjusted (truncated) definition has the same DefinitionDigest as a file already holding the truncated text.
  • Bad definitions: dropped and reported at their exact path, including invalid UTF-8 in the body, a frontmatter value, a hook, a header name, and an unknown key (a literal U+FFFD loads), a dangling symlink, and a symlink to a directory. A symlink to a definition loads with its target's bytes.
  • Form version: both profiles record definition-form, pinned to agentdef-canonical/v1 next to the pinned digests, so bumping FormVersion fails the loader tests.
  • Directory handling:
    • only .md files directly in a directory count;
    • duplicates within and across directories;
    • a winner with a bad record still holds its name;
    • the missing- and unreadable-directory errors;
    • cancellation;
    • UTC revisions whatever the time zone;
    • definitions planted in the user-tier HOME/XDG_CONFIG_HOME locations are never loaded.

Each new guard was checked by planting a bug that the guard should catch:

  • the Bash alias and the list splitting (caught by the parity test);
  • the name hold;
  • the UTF-8 check;
  • the trailing-newline rule;
  • skipping directories;
  • the cancellation check.

That check found a redundant within-directory duplicate check, which was removed. A second review pass found that the whole-file UTF-8 check from the first version had been lost in the rework; it is back, and the same planted-bug check covers it, the form metadata, the digest rule, and symlinked reads. Both depguard rules were probed with a forbidden import.

Changes

File Change
agentdef/ New module: definition type, parser, canonical form, tests
internal/identitybroker/adapter/definitionloader/loader.go New loader adapter on agentdef
internal/identitybroker/adapter/definitionloader/loader_test.go, loader_internal_test.go Loader tests
internal/app/agentdef_parity_test.go Parity between the harness's and the broker's parser
internal/identitybroker/definition.go, conformance/conformance.go Shared source and built-in names
.golangci.yml Depguard rules for the loader and the module
go.work, go.mod, integration/microvm/go.mod, Taskfile.yml, .github/workflows/*.yml Module registration
docs/module-development.md, docs/architecture.md The new module
docs/drafts/agent-identity/implementation.md The loader's depguard exception

User-facing change

None. Nothing calls the loader yet.

Special notes for reviewers

  • Origin set: needs the design author's confirmation. Minimal definition loader, including built-in definitions #2526 left open which origins count as the operator's global definitions. This PR loads only explicit:

    • it is the only origin the harness treats as operator-managed (internal/app/root_authority.go);
    • user in mecak8s is just the service account's home directory;
    • driver definitions arrive over gRPC with no file bytes for SourceDigest.

    Widening later is additive; narrowing later would not be.

  • Revision for global definitions is the file's modification time (UTC, RFC 3339 with nanoseconds), marked as a shortcut that Full definition loader and registration rules #2551 removes. It doesn't follow the content, in either direction:

    • A copy that keeps modification times (rsync -a, cp -p, tar) can change the bytes under the same revision, which only the digests reveal.
    • A rewrite, remount, checkout, or second replica gives unchanged bytes a new revision, so the broker treats them as a different definition.

    A content digest would fix both, but Minimal definition loader, including built-in definitions #2526 says the revision is never a digest, so the package doc documents the limitation instead.

  • Built-in contents are placeholders (a name plus a one-line description). The package doc marks this as a shortcut that Switch on real loading and registration #2553 removes. Changing them changes the pinned digests on purpose.

  • DefinitionDigest covers the whole parsed definition, as the design defines the accepted definition digest (agent-definitions.md, "Definition provenance"). Whether the broker should also record an authority-only digest for policy ([Phase 3] Express the tool limit as generated Cedar policy #2849) is open.

  • Secret-shaped headers. Inline MCP server headers can sit in definition files. The loader keeps only digests, which are unsalted SHA-256. The docs say they are unsafe to expose next to the rest of a file when a header secret is short.

  • Non-UTF-8 path test skips on macOS, whose file system refuses such names; it runs on Linux.

  • Unrelated test failures under full-suite load. On a laptop, task test:race hit subprocess or latency timeouts in:

    • cmd/mecatui (TestWorkspaceSnapshotHostileHelpers);
    • cmd/mecatui/agenthook;
    • internal/adapter/microvmmanager, including its microvm_dev run.

    Each passes when rerun alone with -race. task check:pr passes up to its SDK e2e step, which was not run locally because it needs bun.

🤖 Generated with Claude Code

rdimitrov and others added 3 commits November 17, 2026 13:20
Add the definition loader adapter for #2526. It returns the records
identitybroker.New takes: the built-in system/main and system/explorer
definitions, and the operator's global definitions from the explicit
agent directories, parsed with the harness's agentfs parser.

- Only the explicit origin is loaded, never the project or user tiers or
  a remote driver: only explicit is operator-managed, and a driver
  definition has no file bytes to digest.
- A built-in's revision is the release tag, or "dev" for any other build.
  A global definition's revision is its file's modification time in UTC,
  a shortcut until #2551.
- SourceDigest covers the bytes read; DefinitionDigest covers the
  accepted definition's canonical JSON form.
- A definition the parser or DefinitionRecord.Validate rejects is left
  out and reported, and loading continues.

agentfs.Discovered gains a File field with the path, exact bytes, and
modification time DirSource read, so the loader never reads a file
twice. The loader gets its own depguard rule allowing agentfs and
engine/tool alongside the broker core.

Startup still passes no records; #2553 switches the loader on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address review findings on the definition loader:

- Load returns Definition values pairing each DefinitionRecord with the
  accepted tool.AgentDef its DefinitionDigest covers, so #2553 can build
  agents from the same read the broker recorded instead of parsing the
  files a second time. The built-ins are returned the same way, named by
  the exported BuiltinMain and BuiltinExplorer, as fresh copies per call.
- The shortcut note now states both ways the modification-time revision
  diverges from the content: copies that keep modification times change
  bytes under one revision, and rewrites, remounts, checkouts, or
  replicas give unchanged bytes a new revision.
- The acceptedForm comment now says that any field added to or renamed
  in tool.AgentDef changes every definition's digest, since the struct
  has no JSON tags.
- New tests cover a kept-but-adjusted definition (reported with
  Dropped false) and an unreadable directory (an error, no definitions,
  earlier problems kept), and pin a global definition's DefinitionDigest
  so clearing Origin stays covered. Each was mutation-checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- agentfs.FSSource no longer keeps file bytes in its long-lived
  snapshot: the harness's registry never reads them, and they can hold
  an inline MCP server's secret-shaped headers. The loader reads through
  MultiSource, so it still gets them.
- SourceFile documents that Content is the file as read, before any
  truncation, that it is never logged or projected, and that a symlink
  is followed to its target.
- The loader documents that built-in digests cover stand-ins until #2553
  builds system/main and system/explorer from them, and that its
  unsalted digests let anyone who knows the rest of a file test guesses
  at a short inline secret.
- The identity broker adapter depguard comment names the definition
  loader's exception.
- New tests cover the load order, built-ins kept beside global
  definitions (including a global main), a definition under a non-UTF-8
  path being dropped (skipped where the file system refuses such names),
  a symlinked definition file, and the snapshot dropping file bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a careful PR. Each record is built from the same read the parser used, the depguard rule is narrow, the file bytes leave the long-lived harness snapshot, and the shortcuts are written down where the next person will find them. Nothing here blocks merging, because nothing calls the loader yet. Most of what follows is about making #2553 safe to switch on. Every comment carries a concrete proposal.

Worth settling here (inline):

  • Every build from main gets the built-in revision dev (loader.go:73). Suggested fix included.
  • A configured --agents-dir that does not exist is silent (loader.go:103).
  • Explicit-only is the right origin set, but the documented mecak8s setup puts operator agents in the user tier, so the broker would not see them (loader.go:165).
  • Two different files can get the same DefinitionDigest when the body has invalid UTF-8 (loader.go:244).
  • A few test assertions are weaker than their names promise.

For follow-up issues (not code in this PR):

  • Ordering: "Full definition loader" (#2551) has to land before or with durable broker state (#2563). Otherwise every mecak8s rollout makes restored instances fail closed: the kubelet writes ConfigMap files with a fresh modification time in each new pod. See the inline comment on loader.go:226.
  • Loader failure policy: the PR description hands "stop startup or degrade to built-ins only" to "Switch on real loading and registration" (#2553), but that issue doesn't list it. Proposal: add it as an explicit item there.
  • Audit event for each load: the design says the loader records each load with its source and revision (credentials-and-lifecycle.md, step "(0) Broker startup"; audit.md). Neither "Broker records of who acted" (#2541) nor "Audit records and delivery" (#2566) lists a loader event. Proposal: add it to one of them. Load would then also need to return load-level facts: configured directories, built-in revision, and time.
  • Digests in audit: the Definition doc says to keep digests out of records people can read, but the design puts provenance, digests included, into audit. Proposal: decide on #2541 or #2551 whether audit carries the digests as they are, or a keyed digest for definitions that can hold inline header secrets.
  • Origin decision: "Minimal definition loader, including built-in definitions" (#2526) asked to settle the origin set with the design author before implementation, and proposed explicit, user, and driver. Proposal: record the explicit-only decision and its reason on #2526. "Broker mode: the harness uses loader output" (#2548) assumes the harness stops discovering operator definitions itself, so it needs to know who owns user-tier and driver definitions. In driver mode the loader yields no global definitions, because --agents-dir and --agent-source-url are mutually exclusive.

The suggestions are in order of importance, so stop wherever you think the rest is better left for later.

Comment thread engine/adapter/agentfs/fssource_test.go Resolved
Comment thread engine/adapter/agentfs/source.go Resolved
- Give each published main build its own built-in revision, so two main
  builds never share one; local, dirty, and untagged builds stay "dev".
- Fail the load on a configured directory that does not exist, so a
  mistyped path or unmounted volume shows at startup.
- Leave out a definition whose file is not valid UTF-8: JSON encodes an
  invalid byte as U+FFFD, so two files could share a DefinitionDigest.
- Move the source and built-in names into the broker core, where callers
  of the broker can use them, and use them in the conformance suite.
- Document why the user tier is excluded, that file revisions must not
  back durable broker state, what the digest does not cover, and that
  the canonical form is Go-specific.
- Tighten tests: exact adjusted digests, exact problem locations, one
  copy per name, the snapshot keeping the definition, UTC revisions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rdimitrov and others added 4 commits November 18, 2026 13:37
The definition loader imported the engine's agentfs parser, but the
identity broker must not depend on the rest of Mecatl, since it will move
into its own service. The definition format moves into a new module,
agentdef, which imports only the standard library and the YAML parser:
the definition type, the parser (moved from agentfs), and a versioned
canonical form for the definition digest.

The loader now reads the operator's explicit directories itself, parses
with agentdef, and returns records only: nothing uses the parsed
definition before the harness builds agents from the broker's records.
A winner left out for a bad record still holds its name, as it does in
the harness. The earlier agentfs changes are reverted, so the engine is
unchanged; agentfs keeps its copy of the parser until a follow-up
switches it to agentdef, and a parity test pins that both read every file
the same way.

The module is registered in go.work, the root and microVM integration
manifests, the Taskfile, CI, release cache keys, and the module docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The modulith constraints said every broker adapter other than AS-1's
allows only the standard library, the broker core, and the JWT library.
The definition loader now also imports agentdef, which imports nothing
from Mecatl, so the broker still moves out as one tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…form

The rework checked UTF-8 only in the parsed fields, but the YAML parser
reads an invalid byte in the frontmatter as U+FFFD, so two different
files could load with one DefinitionDigest. The loader checks the whole
file again, as the first version did; the parser itself keeps accepting
such files, as the harness's copy does.

The digest rule moves into agentdef (Digest, DigestAlgorithm), so the
engine adopts it rather than copying it, and both provenance profiles
record the canonical form version under "definition-form", so a change
of form shows in the record. The parser's helpers are unexported before
the module is first tagged. New tests cover invalid UTF-8 in each part of
a file and dangling and directory symlinks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reworking this after the Slack thread. The loader now depends only on agentdef and the broker core, the engine matches main again, and the parity test keeps the two parsers from drifting apart until they are merged. Approving.

Two follow-ups, neither blocking:

  1. "Broker mode: the harness uses loader output" needs a new description. It still says the loader hands tool.AgentDef to the harness and exposes a tool.AgentDefSource. After this PR the loader can't import those types. The new shape is:

    • the broker returns the record plus the raw bytes it read;
    • the harness checks those bytes against SourceDigest;
    • the harness parses them with agentdef, or with agentfs once that is built on agentdef.

    This is also what lets the broker run as a separate service later without knowing the engine's types.

  2. Consider filing the "switch agentfs to agentdef" follow-up as an issue, including the step of tagging the agentdef module first. Until it lands we carry two copies of the parser, so an issue would keep it from getting lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants