§22 S6: per-collection enabled_models — the §22.12 model-universe chain

A collection's .collection.yaml may carry an enabled_models list that NARROWS
its project's universe, which narrows the deployment ENABLED_MODELS. Resolution
(extending §6.6/§6.7): funder ∩ per-entry models ∩ collection ∩ project, with
the operator providers as the ceiling.

- migration 031: additive collections.config_json (parallels projects.config_json).
- registry.parse_collection_manifest: read enabled_models into CollectionEntry.config
  (absent = inherit, present incl. [] = narrow; [] opts the collection out of AI);
  reject a non-list. _upsert_named_collection persists config_json. The default
  collection (from projects.yaml) leaves it NULL — it inherits the project.
- models_resolver: _scope_narrowed_universe narrows the operator universe by the
  entry's project then collection enabled_models; the funder universe is bounded
  by it too. A collection cannot widen its project (narrowing from the ceiling).
- collections.get_collection surfaces enabled_models; GET
  /api/projects/:id/collections/:cid returns it.
- test_s6_collection_models_vertical.py: 9 cases (parse, the narrowing chain
  incl. opt-out + cannot-widen, API surfacing).

Backend: 525 passed. Per SPEC §22.12. Releases as part of v0.45.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-06 01:30:16 -07:00
parent 26f3680197
commit 79a27a946b
5 changed files with 252 additions and 14 deletions
+24 -4
View File
@@ -8,11 +8,26 @@ authz (auth.py) recovers a row's project by joining `collections` on
"""
from __future__ import annotations
import json
from . import db
DEFAULT_COLLECTION_ID = "default"
def _enabled_models_from_config(config_json: str | None) -> list[str] | None:
"""§22.12 per-collection enabled_models from a `config_json` blob, or None
when unset (the collection inherits its project's universe)."""
if not config_json:
return None
try:
cfg = json.loads(config_json)
except (json.JSONDecodeError, TypeError):
return None
em = cfg.get("enabled_models") if isinstance(cfg, dict) else None
return [str(m) for m in em] if isinstance(em, list) else None
def default_collection_id(project_id: str) -> str:
"""The id of a project's default (S1: sole) collection. Falls back to the
literal 'default' when the project has no collection row yet."""
@@ -60,13 +75,18 @@ def subfolder_of(collection_id: str) -> str:
def get_collection(collection_id: str) -> dict | None:
"""The full collection row as a dict, or None if unknown."""
"""The full collection row as a dict, or None if unknown. `enabled_models`
(§22.12) is unpacked from `config_json` as a list, or None when unset."""
row = db.conn().execute(
"SELECT id, project_id, type, subfolder, initial_state, visibility, name "
"FROM collections WHERE id = ?",
"SELECT id, project_id, type, subfolder, initial_state, visibility, name, "
"config_json FROM collections WHERE id = ?",
(collection_id,),
).fetchone()
return dict(row) if row else None
if row is None:
return None
out = dict(row)
out["enabled_models"] = _enabled_models_from_config(out.pop("config_json", None))
return out
def list_collections(project_id: str, include_unlisted: bool = False) -> list[dict]:
+62 -6
View File
@@ -38,22 +38,78 @@ from . import db, funder
from .providers import BaseProvider
def _models_from_config(config_json: str | None) -> list[str] | None:
"""The `enabled_models` list inside a project/collection `config_json`,
or None when the key is absent (meaning "no narrowing at this tier")."""
if not config_json:
return None
try:
cfg = json.loads(config_json)
except (json.JSONDecodeError, TypeError):
return None
em = cfg.get("enabled_models") if isinstance(cfg, dict) else None
return [str(m) for m in em] if isinstance(em, list) else None
def _narrow(universe: list[str], allowed: list[str] | None) -> list[str]:
"""Intersect `universe` with `allowed`, preserving universe order. `allowed`
None means no narrowing at this tier; an empty list narrows to empty (an
opt-out), exactly like the §6.6 per-entry `models: []`."""
if allowed is None:
return universe
allow = set(allowed)
return [k for k in universe if k in allow]
def _scope_narrowed_universe(
collection_id: str | None, operator_keys: list[str]
) -> list[str]:
"""§22.12 — narrow the operator (deployment) universe by the entry's
project then its collection `enabled_models`. Each tier may only narrow;
a missing config at a tier is a no-op. The collection cannot widen its
project because narrowing composes from the operator ceiling downward."""
if collection_id is None:
return list(operator_keys)
conn = db.conn()
crow = conn.execute(
"SELECT project_id, config_json FROM collections WHERE id = ?",
(collection_id,),
).fetchone()
universe = list(operator_keys)
if crow is None:
return universe
prow = conn.execute(
"SELECT config_json FROM projects WHERE id = ?", (crow["project_id"],)
).fetchone()
universe = _narrow(universe, _models_from_config(prow["config_json"] if prow else None))
universe = _narrow(universe, _models_from_config(crow["config_json"]))
return universe
def resolve_models_for_rfc(
slug: str, providers: dict[str, BaseProvider]
) -> list[str]:
"""Return the per-RFC resolved model keys per §6.6, extended by §6.7.
"""Return the per-RFC resolved model keys per §6.6, extended by §6.7 and
§22.12.
The first entry is the RFC's default model. An empty list means
AI is unavailable on this RFC and callers refuse the AI surface.
"""
# §6.7: the funder universe (if any) replaces the operator universe
# as the base set the §6.6 frontmatter intersects against.
funder_universe = funder.resolve_funder_universe(slug, providers)
base_universe = funder_universe if funder_universe is not None else list(providers.keys())
row = db.conn().execute(
"SELECT models_json FROM cached_rfcs WHERE slug = ?",
"SELECT collection_id, models_json FROM cached_rfcs WHERE slug = ?",
(slug,),
).fetchone()
collection_id = row["collection_id"] if row is not None else None
# §22.12: first narrow the operator universe by the entry's project +
# collection enabled_models (the deployment → project → collection chain).
scope_universe = _scope_narrowed_universe(collection_id, list(providers.keys()))
# §6.7: a consenting funder universe (if any) replaces the operator universe
# as the base set — still bounded by the §22.12 scope narrowing above.
funder_universe = funder.resolve_funder_universe(slug, providers)
if funder_universe is not None:
base_universe = _narrow(list(funder_universe), scope_universe)
else:
base_universe = scope_universe
if row is None or row["models_json"] is None:
return list(base_universe)
try:
+16 -4
View File
@@ -64,6 +64,7 @@ class CollectionEntry:
visibility: str | None
initial_state: str
name: str | None
config: dict = field(default_factory=dict) # §22.12 enabled_models
@dataclass
@@ -149,7 +150,16 @@ def parse_collection_manifest(text: str) -> CollectionEntry:
raise RegistryError(f"collection has invalid initial_state {initial_state!r}")
name = raw.get("name")
name = str(name).strip() if name else None
return CollectionEntry(ctype, vis, initial_state, name)
# §22.12: an optional per-collection enabled_models list that narrows the
# project's universe. Absent → no narrowing (inherit). Present (incl. empty)
# → narrowing applies; [] opts the collection out of AI.
cfg: dict = {}
if raw.get("enabled_models") is not None:
em = raw["enabled_models"]
if not isinstance(em, list):
raise RegistryError("collection enabled_models must be a list")
cfg["enabled_models"] = [str(m) for m in em]
return CollectionEntry(ctype, vis, initial_state, name, cfg)
def _default_collection_id(project_id: str, default_id: str) -> str:
@@ -268,17 +278,19 @@ def _upsert_named_collection(
conn.execute(
"""
INSERT INTO collections
(id, project_id, type, subfolder, initial_state, visibility, name, registry_sha, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
(id, project_id, type, subfolder, initial_state, visibility, name, config_json, registry_sha, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
ON CONFLICT(id) DO UPDATE SET
project_id = excluded.project_id,
initial_state = excluded.initial_state,
visibility = excluded.visibility,
name = excluded.name,
config_json = excluded.config_json,
registry_sha = excluded.registry_sha,
updated_at = datetime('now')
""",
(subdir, proj.id, ce.type, subdir, ce.initial_state, visibility, ce.name, sha),
(subdir, proj.id, ce.type, subdir, ce.initial_state, visibility, ce.name,
json.dumps(ce.config), sha),
)