feat: agent version pinning (v4)

resolve_registry_agent in cloud/agent_def.py: agent_name may be
<name> or <name>@<version>, agent_version may also be given
separately; both given must agree or 400. Unpinned loads
<name>.json (default); pinned loads <name>.<version>.json and
requires the file's own version field to match, with no silent
fallback to the default file. Version strings are restricted
to alphanumerics/._- (no path separators, no ..); the
agents-dir traversal guard is a second line of defence.
Server: agent_version is only for agent_name (inline agent
with agent_version is 400); stored agent_name is the base
name, not the @ form. Snapshot fixture:
agents/research-brief.v1.json (same content as the default
v1 file). Tests: test_agent_version.py (7 tests).
This commit is contained in:
host committed 2026-11-04 18:01:44 +00:00
1 parent 8fe4ad2f38
commit 7f60f12639
4 files changed
+277 -31

No files matched your search

+75
View File
@@ -309,6 +309,81 @@ def load_agent_definition(path, agents_dir=None):
return parse_agent_definition(data)
VERSION_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
def resolve_registry_agent(name, version=None, agents_dir=None,
known_skills=None):
"""Resolve a registry definition, with optional version pinning.
``name`` may be ``<name>`` or ``<name>@<version>``; ``version``
may also be given separately. If both are given they must agree,
otherwise this is a 400 (ambiguity is never guessed). Without a
version, this loads ``<name>.json`` (the default/latest file).
With a version, it loads ``<name>.<version>.json`` and requires
the file's own ``version`` field to equal the requested version:
a misnamed snapshot (file says v1, filename says v2) is rejected
instead of being used as the wrong version, and a missing snapshot
is an error, never a silent fallback to the default file (that
fallback would defeat pinning entirely). A version string is
restricted to alphanumerics, ``.``, ``_``, ``-`` so it cannot
carry a path separator or ``..``; the agents-dir traversal guard
in load_agent_definition is a second line of defence, not the only
one. Skill existence is checked only when ``known_skills`` is
given (the server passes the pack; pure-function callers may omit
it, exactly as with parse_agent_definition).
"""
if not isinstance(name, str) or not name.strip():
raise ValueError("agent_name must be a non-empty string")
name = name.strip()
pinned_from_name = None
if "@" in name:
parts = name.split("@")
if len(parts) != 2 or not parts[0] or not parts[1]:
raise ValueError(
"agent_name with @ must be <name>@<version>")
name, pinned_from_name = parts
if version is not None:
if not isinstance(version, str) or not version.strip():
raise ValueError("agent_version must be a non-empty string")
version = version.strip()
if pinned_from_name is not None:
if version is not None and version != pinned_from_name:
raise ValueError(
"agent_version %r does not match agent_name version "
"%r" % (version, pinned_from_name))
version = pinned_from_name
if not NAME_RE.match(name):
raise ValueError(
"agent_name must match [a-z0-9][a-z0-9_-]{0,63}")
if version is not None and not VERSION_RE.match(version):
raise ValueError(
"agent_version must match [A-Za-z0-9][A-Za-z0-9._-]{0,63}")
filename = "%s.json" % name if version is None else \
"%s.%s.json" % (name, version)
definition = load_agent_definition(filename, agents_dir=agents_dir)
if version is not None and definition.version != version:
raise ValueError(
"registry file %s says version %r, not requested %r" %
(filename, definition.version, version))
if known_skills is not None:
# Re-parse for skill existence only (load already validated
# format); the re-parsed definition is the one returned, so
# callers never see a definition that failed skill checks.
definition = parse_agent_definition(
{
"name": definition.name,
"version": definition.version,
"description": definition.description,
"instructions": definition.instructions,
"skills": definition.skills,
"tools": definition.tools,
"defaults": definition.defaults,
},
known_skills=known_skills)
return definition
def resolve_task_fields(definition, body):
"""Resolve task fields: explicit body field wins, else definition.