Repository navigation
feat(identitybroker): load agent definition records for the broker - #2829
Conversation
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
left a comment
There was a problem hiding this comment.
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
maingets the built-in revisiondev(loader.go:73). Suggested fix included. - A configured
--agents-dirthat 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
DefinitionDigestwhen 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.
Loadwould then also need to return load-level facts: configured directories, built-in revision, and time. - Digests in audit: the
Definitiondoc 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, anddriver. 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 ownsuser-tier anddriverdefinitions. In driver mode the loader yields no global definitions, because--agents-dirand--agent-source-urlare mutually exclusive.
The suggestions are in order of importance, so stop wherever you think the rest is better left for later.
- 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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
left a comment
There was a problem hiding this comment.
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:
-
"Broker mode: the harness uses loader output" needs a new description. It still says the loader hands
tool.AgentDefto the harness and exposes atool.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 withagentfsonce that is built onagentdef.
This is also what lets the broker run as a separate service later without knowing the engine's types.
-
Consider filing the "switch
agentfstoagentdef" follow-up as an issue, including the step of tagging theagentdefmodule first. Until it lands we carry two copies of the parser, so an issue would keep it from getting lost.
Summary
The identity broker cannot tell which agent definitions exist:
identitybroker.Newtakes[]DefinitionRecord, but startup passesnil, 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:DefinitionandMCPServer;Parse, which is the engine'sagentfsparser moved over, with the same caps, list splitting,BashtoShellalias,mcpServershandling, 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 byFormVersion;Digest, the digest rule itself: lowercase hexadecimal SHA-256 ofCanonical, 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.DefinitionRecordforidentitybroker.New. It loads:system/mainandsystem/explorer. Their revision is the build stamp of a published build: a release tag such asv0.0.52, or a main build such asv0.0.53-main.812.1.gabcdef12. Local, dirty, and untagged builds get the fixed, documenteddev(BuiltinRevision, pinned by tests);--agents-dir) and parsed withagentdef.Failure.
Loadfails when a configured directory is missing, is not a directory, or cannot be read, so a mistyped--agents-diror 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:
globalorsystem);file/v1with the file path, ormecatl-release/v1;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:DefinitionDigest;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,BuiltinMainandBuiltinExplorerlive 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'sagentdefexception. The openagentdefdesign questions are not written into the design here.Module registration. The module is registered in
go.workand in the rootgo.mod, with a localreplacelikeauthn/oidc, so nothing needs publishing for this PR. It is also registered inintegration/microvm'sgo.mod. The Taskfile, the CI build, lint and govulncheck steps, the release cache keys, and the k8s end-to-end path filters include it, anddocs/module-development.mdanddocs/architecture.mdlist it.No engine change.
engine/is identical tomain. The harness keeps parsing withagentfsuntil a follow-up switchesagentfstoagentdef. That follow-up needsagentdeftagged 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.goruns 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
Test plan
Baseline checks
task lint), including the new moduletask test:race), every module includingagentdef: see notesgo run ./cmd/mecademo): tool call, permission ask and approval, resulttask docs)task site:build): no user-facing changetask api:check): no engine changetask api:update+engine/CHANGELOG.md: not applicableThe tests cover:
agentdefparsing: the engine's parse tests, carried over unchanged apart from type names.agentdef.Canonicalandagentdef.Digest:DefinitionDigest;SourceDigest;DefinitionDigestas a file already holding the truncated text.definition-form, pinned toagentdef-canonical/v1next to the pinned digests, so bumpingFormVersionfails the loader tests..mdfiles directly in a directory count;HOME/XDG_CONFIG_HOMElocations are never loaded.Each new guard was checked by planting a bug that the guard should catch:
Bashalias and the list splitting (caught by the parity test);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
agentdef/internal/identitybroker/adapter/definitionloader/loader.goagentdefinternal/identitybroker/adapter/definitionloader/loader_test.go,loader_internal_test.gointernal/app/agentdef_parity_test.gointernal/identitybroker/definition.go,conformance/conformance.go.golangci.ymlgo.work,go.mod,integration/microvm/go.mod,Taskfile.yml,.github/workflows/*.ymldocs/module-development.md,docs/architecture.mddocs/drafts/agent-identity/implementation.mdUser-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:internal/app/root_authority.go);userin mecak8s is just the service account's home directory;driverdefinitions arrive over gRPC with no file bytes forSourceDigest.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:
rsync -a,cp -p, tar) can change the bytes under the same revision, which only the digests reveal.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.
DefinitionDigestcovers 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:racehit subprocess or latency timeouts in:cmd/mecatui(TestWorkspaceSnapshotHostileHelpers);cmd/mecatui/agenthook;internal/adapter/microvmmanager, including itsmicrovm_devrun.Each passes when rerun alone with
-race.task check:prpasses up to its SDK e2e step, which was not run locally because it needsbun.🤖 Generated with Claude Code