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:
@@ -234,6 +234,13 @@ func (s *DefinitionsService) CreateAgent(ctx context.Context, ident authctx.Iden
|
||||
input.OwnerUserID = &ident.UserID
|
||||
}
|
||||
|
||||
if err := s.refuseVersionGoingBackwards(ctx, ident, repo.KindAgent,
|
||||
agent.ID, agent.Status, agent.Version); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.refuseSubagentCycle(ctx, ident, agent.ID, agent.Subagents); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.refusePublishedRewrite(ctx, ident, repo.KindAgent,
|
||||
agent.ID, markdown, agent.Status, agent.Version); err != nil {
|
||||
return nil, err
|
||||
@@ -302,6 +309,13 @@ func (s *DefinitionsService) UpdateAgent(ctx context.Context, ident authctx.Iden
|
||||
input.Version = &agent.Version
|
||||
input.Pages = agent.Pages
|
||||
|
||||
if err := s.refuseVersionGoingBackwards(ctx, ident, repo.KindAgent,
|
||||
agent.ID, agent.Status, agent.Version); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.refuseSubagentCycle(ctx, ident, agent.ID, agent.Subagents); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.refusePublishedRewrite(ctx, ident, repo.KindAgent,
|
||||
agent.ID, markdown, agent.Status, agent.Version); err != nil {
|
||||
return nil, err
|
||||
@@ -535,6 +549,80 @@ func (s *DefinitionsService) DeleteSkill(ctx context.Context, ident authctx.Iden
|
||||
|
||||
/* ── Publishing ─────────────────────────────────────────────────────────── */
|
||||
|
||||
// refuseVersionGoingBackwards enforces §3's "monotonic".
|
||||
//
|
||||
// Nothing enforced it before. A spec edited from an older copy republishes an
|
||||
// older number whose content still matches what was published under it, so the
|
||||
// rewrite guard sees no conflict and the deployed agent quietly goes
|
||||
// backwards — the version in the UI reads 2 while the newest thing anybody
|
||||
// approved was 3.
|
||||
func (s *DefinitionsService) refuseVersionGoingBackwards(ctx context.Context,
|
||||
ident authctx.Identity, kind repo.VersionKind, definitionID, status string, version int) error {
|
||||
|
||||
if s.versions == nil || status != "published" || definitionID == "" || version < 1 {
|
||||
return nil
|
||||
}
|
||||
latest, err := s.versions.LatestVersion(ctx, ident, kind, definitionID)
|
||||
if err != nil || latest == 0 {
|
||||
// Unreadable, or nothing published yet. Neither is grounds to refuse a
|
||||
// save: the rewrite guard is what protects published text, and this
|
||||
// only orders the numbers.
|
||||
return nil
|
||||
}
|
||||
if version < latest {
|
||||
return domain.Conflict(definition.ErrVersionWentBackwards(definitionID, latest, version).Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// refuseSubagentCycle enforces §3's DAG at publish.
|
||||
//
|
||||
// The graph is every organization-visible agent plus the one being published,
|
||||
// with 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 not included. They are invisible to everyone else, so
|
||||
// they cannot complete a loop for anybody else, and loading them would mean
|
||||
// reading other people's drafts to validate your own.
|
||||
func (s *DefinitionsService) refuseSubagentCycle(ctx context.Context,
|
||||
ident authctx.Identity, definitionID string, subagents []string) error {
|
||||
|
||||
if len(subagents) == 0 {
|
||||
return nil
|
||||
}
|
||||
rows, _, err := s.repo.ListAgents(ctx, ident, repo.DefinitionListParams{
|
||||
Visibility: "organization", Limit: 500,
|
||||
})
|
||||
if err != nil {
|
||||
// A graph we could not read is not a graph we can call cyclic. The
|
||||
// runtime depth cap is what holds when this cannot run.
|
||||
return nil
|
||||
}
|
||||
|
||||
graph := make(map[string][]string, len(rows)+1)
|
||||
for _, rec := range rows {
|
||||
id, _ := rec["definition_id"].(string)
|
||||
markdown, _ := rec["markdown"].(string)
|
||||
if id == "" || markdown == "" || id == definitionID {
|
||||
continue // the incoming definition replaces its stored self, below
|
||||
}
|
||||
if parsed, err := definition.ParseAgent(markdown, definition.Options{}); err == nil && parsed != nil {
|
||||
graph[id] = parsed.Subagents
|
||||
}
|
||||
}
|
||||
graph[definitionID] = subagents
|
||||
|
||||
if cycle := definition.FindSubagentCycle(graph); cycle != "" {
|
||||
return domain.Validation(
|
||||
"that would create a delegation cycle: "+cycle+
|
||||
". Delegation follows these edges, so a loop is a run that delegates "+
|
||||
"until it runs out of budget.",
|
||||
map[string]string{"subagents": "cycle"})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// refusePublishedRewrite fails a publish that would change a version already
|
||||
// published, BEFORE anything is written.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user