skills: approved_for_firm on a version and a file read for the checking service

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uwxqJYwbXKWMeNZfrFuXq
This commit is contained in:
tobayah
2026-11-18 18:50:10 +01:00
co-authored by Claude Opus 5.5
parent 52e95a9d3a
commit 0a265ebafb
4 changed files with 96 additions and 3 deletions
+7
View File
@@ -129,6 +129,13 @@ def get_version(skill_id: uuid.UUID, n: int, db: Session = Depends(get_db),
return service.get_version(db, user, skill_id, n)
@router.get("/{skill_id}/versions/{n}/files/{path:path}")
def get_file(skill_id: uuid.UUID, n: int, path: str, db: Session = Depends(get_db),
user: AppUser = Depends(get_current_user)) -> Response:
media_type, content = service.get_file(db, user, skill_id, n, path)
return Response(content=content, media_type=media_type)
@router.post("/{skill_id}/archive")
def archive_skill(skill_id: uuid.UUID, request: Request, db: Session = Depends(get_db),
user: AppUser = Depends(get_current_user)) -> dict:
+27 -3
View File
@@ -235,14 +235,38 @@ def get_skill(db: DbSession, user: AppUser, skill_id: uuid.UUID) -> dict:
if current is not None and current.id in readable else None)}
def get_version(db: DbSession, user: AppUser, skill_id: uuid.UUID, n: int) -> dict:
"""Version `n`, if this person may read it — an unapproved firm version is a 404 to others."""
def _readable_version(db: DbSession, user: AppUser, skill_id: uuid.UUID,
n: int) -> tuple[Skill, SkillVersion]:
"""The skill and its version `n`, if this person may read it — an unapproved firm version is a
404 to others."""
skill = _get(db, user, skill_id)
version = db.scalar(select(SkillVersion).where(SkillVersion.skill_id == skill.id,
SkillVersion.n == n))
if version is None or not readable_version_ids(user, skill, [version.id]):
raise NotFoundError("Version not found.")
return version_out(version, _files(db, version.id))
return skill, version
def get_version(db: DbSession, user: AppUser, skill_id: uuid.UUID, n: int) -> dict:
"""Version `n`, if this person may read it. `approved_for_firm` tells the checking service
whether this is the version the firm approved: an editor can read a proposal too, and a check
run against one must not pass as the firm's own."""
skill, version = _readable_version(db, user, skill_id, n)
return {**version_out(version, _files(db, version.id)), "skill_name": skill.name,
"approved_for_firm": (skill.scope == SkillScope.firm
and skill.current_version_id == version.id)}
def get_file(db: DbSession, user: AppUser, skill_id: uuid.UUID, n: int,
path: str) -> tuple[str, bytes]:
"""(media_type, bytes) of one file of a readable version; the same read rule as `get_version`.
A read, so no audit entry."""
_, version = _readable_version(db, user, skill_id, n)
stored = db.scalar(select(SkillFile).where(SkillFile.skill_version_id == version.id,
SkillFile.path == path))
if stored is None:
raise NotFoundError("File not found.")
return stored.media_type, stored.content
# ---- writes ------------------------------------------------------------------------------------
+1
View File
@@ -27,6 +27,7 @@ def test_every_skills_route_is_registered_under_the_api_prefix():
for method, path in [
("GET", "/skills"), ("POST", "/skills"), ("GET", "/skills/{skill_id}"),
("POST", "/skills/{skill_id}/versions"), ("GET", "/skills/{skill_id}/versions/{n}"),
("GET", "/skills/{skill_id}/versions/{n}/files/{path:path}"),
("POST", "/skills/{skill_id}/archive"), ("POST", "/skills/{skill_id}/publish"),
("GET", "/skills/{skill_id}/export"), ("POST", "/skills/import"),
]:
+61
View File
@@ -291,3 +291,64 @@ def test_an_agent_file_name_is_ascii_kebab():
assert skills._slugish("Asystent umów NDA!") == "asystent-umow-nda"
assert skills._slugish("Łódź") == "lodz"
assert skills._slugish("—") == "agent"
# ---- reading a version for the checking service ------------------------------------------------
class _ReadDb:
"""`get` answers the skill; `scalar` the queued answers in order (version, then file);
`scalars` the files of the version."""
def __init__(self, skill, scalars_=(), files=()):
self.skill, self._queue, self._files = skill, list(scalars_), list(files)
def get(self, _model, _id):
return self.skill
def scalar(self, _stmt):
return self._queue.pop(0)
def scalars(self, _stmt):
return iter(self._files)
def _full_version(n=1):
return SimpleNamespace(id=uuid.uuid4(), n=n, description="d", body_md="b", license="l",
meta={}, content_sha256="0" * 64, provenance=None,
created_at=None, created_by=OWNER.id)
def test_version_says_whether_it_is_the_firms_approved_one():
approved, proposal = _full_version(1), _full_version(2)
firm = _skill("firm", current=approved.id)
out = skills.get_version(_ReadDb(firm, [approved]), OWNER, firm.id, 1)
assert out["approved_for_firm"] is True and out["skill_name"] == "NDA"
out = skills.get_version(_ReadDb(firm, [proposal]), OWNER, firm.id, 2)
assert out["approved_for_firm"] is False
personal = _skill("personal", current=approved.id)
out = skills.get_version(_ReadDb(personal, [approved]), OWNER, personal.id, 1)
assert out["approved_for_firm"] is False
def test_get_file_returns_the_bytes_of_a_readable_version():
version = _full_version()
skill = _skill("firm", current=version.id)
stored = SimpleNamespace(media_type="application/yaml", content=b"steps: []\n")
db = _ReadDb(skill, [version, stored])
assert skills.get_file(db, OTHER, skill.id, 1, "sprawdzanie.yaml") == (
"application/yaml", b"steps: []\n")
def test_get_file_is_404_for_a_version_the_person_cannot_read():
pending = _full_version(2)
skill = _skill("firm", current=uuid.uuid4())
stored = SimpleNamespace(media_type="application/yaml", content=b"x")
with pytest.raises(NotFoundError):
skills.get_file(_ReadDb(skill, [pending, stored]), OTHER, skill.id, 2, "sprawdzanie.yaml")
def test_get_file_is_404_for_a_missing_path():
version = _full_version()
skill = _skill("firm", current=version.id)
with pytest.raises(NotFoundError):
skills.get_file(_ReadDb(skill, [version, None]), OTHER, skill.id, 1, "nope.yaml")