Coverage for app / services / agent_grants.py: 88%
25 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""An agent's grants (repositories, skillsets, toolkits) and the version
2each one writes.
4A grant or a revoke is a change to the agent: it changes what the agent can
5read, run or call. So it is a version of the agent, like an edit to its
6prompt, with the grants in its ``children_snapshot``. Grants used to write
7nothing, so an agent's history could not say when it gained a repository,
8and evaluation evidence about "this version" could outlive a change to what
9the version could reach.
11The agent routes and anything else that grants (scenario runs,
12``app/services/scenarios``) write the same versions through here.
13"""
15from __future__ import annotations
17from uuid import UUID
19from sqlalchemy.orm import Session
21from app.db.postgres.enums import HistoryEnum
22from app.db.postgres.models import (
23 Agent,
24 AgentRepository,
25 AgentSkillset,
26 AgentToolkit,
27 KnowledgeRepository,
28 Skillset,
29 Toolkit,
30 User,
31)
32from app.services import history as history_service
35def children_snapshot(db: Session, agent_id: UUID) -> dict:
36 """Toolkit/skillset/repository grants for an agent, for its version's
37 ``children_snapshot`` — this is how an agent's history reflects changes
38 to its child resources, not just its own fields."""
40 def _grants(model, id_field: str, name_model, name_field: str) -> list[dict]:
41 rows = db.query(model).filter(model.agent_id == agent_id).all()
42 grants = []
43 for row in rows:
44 target_id = getattr(row, id_field)
45 target = db.get(name_model, target_id)
46 grants.append(
47 {
48 id_field: str(target_id),
49 "name": getattr(target, name_field, None) if target else None,
50 "config": row.config,
51 }
52 )
53 return grants
55 return {
56 "toolkits": _grants(AgentToolkit, "toolkit_id", Toolkit, "name"),
57 "skillsets": _grants(AgentSkillset, "skillset_id", Skillset, "name"),
58 "repositories": _grants(
59 AgentRepository, "repository_id", KnowledgeRepository, "name"
60 ),
61 }
64def record_grant_version(db: Session, agent: Agent, actor: User, kind: str) -> None:
65 """The agent's version for a grant or revoke of ``kind``
66 (``repositories``, ``skillsets``, ``toolkits``), which is added to its
67 changed fields: the agent's own fields are unchanged, and the history
68 would otherwise show an edit to nothing."""
69 version = history_service.record_version(
70 db,
71 resource_type=HistoryEnum.ResourceType.agent,
72 model=Agent,
73 resource=agent,
74 action=HistoryEnum.Action.updated,
75 actor=actor,
76 children_resolver=lambda: children_snapshot(db, agent.id),
77 )
78 version.changed_fields = sorted({*(version.changed_fields or []), kind})
81def grant_repository(
82 db: Session,
83 agent: Agent,
84 repository: KnowledgeRepository,
85 actor: User,
86 config: dict | None = None,
87) -> AgentRepository:
88 """Grant ``repository`` to ``agent`` and write its version, unflushed
89 past the grant. The caller has already checked that ``actor`` may — as
90 the route does — and commits."""
91 row = AgentRepository(agent_id=agent.id, repository_id=repository.id, config=config)
92 db.add(row)
93 db.flush()
94 record_grant_version(db, agent, actor, "repositories")
95 return row