Enforce §3's monotonic version and its DAG requirement at publish
Two rules §3 states and nothing checked.
MONOTONIC. The rewrite guard added earlier compares content at ONE version
number, so republishing an OLDER number with the text originally published
under it looked like a no-op: nothing conflicted, nothing was refused, and the
live row silently reverted. The agent then reads v1 in the UI while the newest
thing anybody approved was v2. The test publishes v1, publishes v2, republishes
v1 byte-for-byte, and asserts both the refusal and that the live row is still
v2. Without the guard it answers 200 and the row goes back to version 1.
DAG. §3 says cycle detection runs at publish; only the runtime depth cap
existed. That cap means a cycle was never a safety problem — it was a budget
one. Every run entering the loop spends its whole allowance delegating in a
circle before terminating, and the author learns about it from a bill rather
than from the publish that created it.
definition.FindSubagentCycle is a pure function over id -> subagent ids, so it
is tested directly: chains, diamonds, self-reference, loops not involving the
first agent walked, and a 5000-long chain that would matter if this were
written to recurse carelessly. It REPORTS the cycle ("a -> b -> c -> a")
rather than merely detecting one, because an operator otherwise has to find it
by hand across a set of specs. The report is deterministic — a test runs it
fifty times over a graph with two cycles and requires the same answer, since Go
randomises map iteration and an error message that changes between identical
runs is one nobody trusts.
Wired into both publish paths. importagents has every spec in hand, which is
the only place that is cheaply true. The API builds the graph from
organization-visible agents plus the incoming definition standing in for its
stored self — otherwise an edit that CREATES a cycle is checked against the
version that did not have one and passes. Personal agents are excluded: they
are invisible to everyone else so cannot complete anyone else's loop, and
reading them would mean reading other people's drafts to validate your own.
An edge to an agent that is not in the set is ignored rather than reported.
That is a different failure with a different message
(runtime.unknown_subagent), and conflating them prints "cycle detected" for
what is actually a typo.
The cycle test bumps the version on the loop-closing edit. Without that the
rewrite guard refuses it for changing published text, the test passes for the
wrong reason, and it would keep passing with cycle detection deleted — which
is how it was first written, and what running it without the guard showed.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJvibeSc1JYXjatankqM1g
This commit is contained in:
@@ -116,6 +116,20 @@ func run(dir, skillDir, orgSlug string, dryRun bool, timeout time.Duration) erro
|
||||
// written. The runtime refuses to load an agent with a missing dependency,
|
||||
// so importing one without its skills produces an agent that exists and
|
||||
// cannot run — a failure that surfaces per request instead of here.
|
||||
// §3: subagents must form a DAG, checked at PUBLISH. Every spec is in hand
|
||||
// here, which is the only place that is cheaply true — so this is where the
|
||||
// check belongs. The runtime depth cap still bounds a cycle that reaches
|
||||
// run time by another route.
|
||||
graph := make(map[string][]string, len(specs))
|
||||
for _, s := range specs {
|
||||
graph[s.parsed.ID] = s.parsed.Subagents
|
||||
}
|
||||
if cycle := definition.FindSubagentCycle(graph); cycle != "" {
|
||||
return fmt.Errorf("the subagent graph has a cycle: %s\n\n"+
|
||||
"Nothing was written. Delegation follows these edges, so a loop is a "+
|
||||
"run that delegates until it runs out of budget.", cycle)
|
||||
}
|
||||
|
||||
if missing := missingSkills(specs, skills); len(missing) > 0 {
|
||||
return fmt.Errorf("%d skill(s) named by an agent are not in %s: %s",
|
||||
len(missing), skillDir, strings.Join(missing, ", "))
|
||||
@@ -494,6 +508,19 @@ func snapshotSkill(ctx context.Context, versions *repo.VersionsRepo,
|
||||
func snapshotAgent(ctx context.Context, versions *repo.VersionsRepo,
|
||||
ident authctx.Identity, s spec) (recorded bool, conflict string, err error) {
|
||||
|
||||
// §3 calls the version monotonic and nothing enforced it. A spec edited
|
||||
// from an older copy republishes an older number whose content still
|
||||
// matches what was published under it — no conflict, no complaint, and the
|
||||
// deployed agent quietly goes backwards.
|
||||
latest, err := versions.LatestVersion(ctx, ident, repo.KindAgent, s.parsed.ID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if latest > 0 && s.parsed.Version < latest {
|
||||
return false, definition.ErrVersionWentBackwards(
|
||||
s.parsed.ID, latest, s.parsed.Version).Error(), nil
|
||||
}
|
||||
|
||||
stored, err := versions.Load(ctx, ident, repo.KindAgent, s.parsed.ID, s.parsed.Version)
|
||||
if err != nil {
|
||||
var apiErr *domain.Error
|
||||
|
||||
Reference in New Issue
Block a user