feat: modele skilli, agentów i próśb o publikację (E1.1)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2db3565409
commit
3fa428b3a6
+1
-1
@@ -3,7 +3,7 @@
|
||||
<!-- Źródło: docs/specs/2026-11-05-skille-i-agenci.md, zakres E1-E3. Przed każdym zadaniem przeczytaj CLAUDE.md repo i odpowiednią sekcję specu; sprzeczność ze specem = wpis w .meduza/QUESTIONS.md, nie obejście. Każde zadanie kończy się zieloną bramką (pytest unit + node --check + license_gate). -->
|
||||
|
||||
## Etap 1: modele i migracja (E1)
|
||||
- [ ] 1.1 Utwórz `backend/app/models/skills.py` z modelami `Skill`, `SkillVersion`, `SkillFile`, `Agent`, `AgentVersion`, `AgentVersionSkill`, `PublishRequest` wg sekcji „Model danych” specu (słowniki jako `Text` + CHECK wzorem `models/marketplace.py`, FK do `app_user` z `ondelete="RESTRICT"`, `UniqueConstraint(owner_id, slug)`, `(skill_id, n)`, `(agent_id, n)`, FK `agent_version_skill` z RESTRICT), zaimportuj je w `models/__init__.py`, dopisz 7 nazw tabel do `test_the_apps_own_tables_are_registered` i nowe tabele do `test_marketplace_vocabularies_are_check_constraints_not_enum_types`. Gotowe gdy: bramka zielona, a oba testy obejmują wszystkie 7 tabel.
|
||||
- [x] 1.1 Utwórz `backend/app/models/skills.py` z modelami `Skill`, `SkillVersion`, `SkillFile`, `Agent`, `AgentVersion`, `AgentVersionSkill`, `PublishRequest` wg sekcji „Model danych” specu (słowniki jako `Text` + CHECK wzorem `models/marketplace.py`, FK do `app_user` z `ondelete="RESTRICT"`, `UniqueConstraint(owner_id, slug)`, `(skill_id, n)`, `(agent_id, n)`, FK `agent_version_skill` z RESTRICT), zaimportuj je w `models/__init__.py`, dopisz 7 nazw tabel do `test_the_apps_own_tables_are_registered` i nowe tabele do `test_marketplace_vocabularies_are_check_constraints_not_enum_types`. Gotowe gdy: bramka zielona, a oba testy obejmują wszystkie 7 tabel.
|
||||
- [ ] 1.2 Napisz `backend/alembic/versions/0010_skills.py` (down_revision = 0009): idempotentne utworzenie 7 tabel (`checkfirst`), `runtime_grants_sql` wzorem `0008` (`read_only` jawnie), osobny `REVOKE UPDATE, DELETE` od roli runtime na `skill_version`, `skill_file`, `agent_version`, `agent_version_skill` z weryfikacją rzucającą błąd, gdy rola nadal ma UPDATE/DELETE; downgrade = drop w odwrotnej kolejności. Dodaj test jednostkowy na SQL generowanym przez rewizję, sprawdzający REVOKE na czterech tabelach. Gotowe gdy: bramka zielona, `alembic upgrade head --sql` pokazuje 7 tabel i REVOKE, nowy test przechodzi.
|
||||
|
||||
## Etap 2: paczka ZIP — czyste funkcje (E2.1-E2.2)
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
# Journal
|
||||
- 1.1: modele `models/skills.py` (7 tabel) + słowniki w `enums.py`; testy parity rozszerzone. Cykl skill↔skill_version i agent↔agent_version przez `use_alter`; `publish_request.version_id` bez FK (polimorficzny wg `kind`). Atrybut `meta` mapuje kolumnę `metadata` (nazwa zarezerwowana w declarative).
|
||||
- Dla 1.2: nowe FK do `app_user` — sprawdzić, czy 0004 przepina je dynamicznie, bo `0001` (create_all z metadanych) na świeżej bazie zbuduje już te tabele.
|
||||
|
||||
@@ -60,6 +60,39 @@ class AccessRequestStatus(StrEnum):
|
||||
declined = "declined"
|
||||
|
||||
|
||||
# Skills and agents (`models/skills.py`): Text + CHECK for the same reason as the marketplace ones.
|
||||
class SkillScope(StrEnum):
|
||||
personal = "personal" # the owner's own; nobody else sees it, an administrator included
|
||||
firm = "firm" # published to everybody on the instance
|
||||
|
||||
|
||||
class CatalogStatus(StrEnum):
|
||||
"""Shared by skills and agents. No `draft`: a draft is simply a personal skill."""
|
||||
active = "active"
|
||||
archived = "archived"
|
||||
|
||||
|
||||
class SkillOrigin(StrEnum):
|
||||
authored = "authored"
|
||||
imported = "imported"
|
||||
bundled = "bundled"
|
||||
|
||||
|
||||
class AgentKind(StrEnum):
|
||||
assistant = "assistant"
|
||||
|
||||
|
||||
class PublishKind(StrEnum):
|
||||
skill = "skill"
|
||||
agent = "agent"
|
||||
|
||||
|
||||
class PublishRequestStatus(StrEnum):
|
||||
pending = "pending"
|
||||
approved = "approved"
|
||||
declined = "declined"
|
||||
|
||||
|
||||
__all__ = [
|
||||
# from dredd-core, unchanged
|
||||
"NodeRole",
|
||||
@@ -71,4 +104,10 @@ __all__ = [
|
||||
"ModuleStatus",
|
||||
"Availability",
|
||||
"AccessRequestStatus",
|
||||
"SkillScope",
|
||||
"CatalogStatus",
|
||||
"SkillOrigin",
|
||||
"AgentKind",
|
||||
"PublishKind",
|
||||
"PublishRequestStatus",
|
||||
]
|
||||
|
||||
@@ -40,11 +40,22 @@ from dredd_core import (
|
||||
from dredd_core.model_providers import ModelProvider, ModelProviderKey, ModelProviderPolicy
|
||||
|
||||
from .marketplace import AccessRequest, Module, ModuleAssignment
|
||||
from .skills import (
|
||||
Agent,
|
||||
AgentVersion,
|
||||
AgentVersionSkill,
|
||||
PublishRequest,
|
||||
Skill,
|
||||
SkillFile,
|
||||
SkillVersion,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"Base", "TimestampMixin", "UUIDPkMixin", "SafeInet", "pg_enum",
|
||||
"AppUser", "Session", "ModulePermission", "AppSetting", "AuditLog",
|
||||
"Node", "PermissionGrant", "Favorite",
|
||||
"Module", "ModuleAssignment", "AccessRequest",
|
||||
"Skill", "SkillVersion", "SkillFile", "Agent", "AgentVersion", "AgentVersionSkill",
|
||||
"PublishRequest",
|
||||
"ModelProvider", "ModelProviderPolicy", "ModelProviderKey",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
"""Skills and agents: a person's own instructions, reference files and the agents built on them.
|
||||
|
||||
A stable identity (`skill`, `agent`) points at frozen versions (`skill_version`, `agent_version`):
|
||||
changing content is a new version, never an edit, which is why the runtime role holds no UPDATE or
|
||||
DELETE on the version tables (revision 0010). The identity rows stay mutable — scope, status and
|
||||
`current_version_id` move through publication and archiving.
|
||||
|
||||
These are NOT nodes of the org tree (the tree holds units only), so access is not `authz`'s: it is
|
||||
computed in one place, `services/skills.visible_to`. Every table lives in Atrium's own schema — no
|
||||
module reads the catalogue, and the shared schema is readable by every module.
|
||||
|
||||
Every foreign key to `app_user` is RESTRICT: an account is deactivated, never deleted, and a skill
|
||||
must not silently lose its author. Vocabularies are Text + CHECK built from `app/enums.py`.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import uuid
|
||||
|
||||
from dredd_core import Base, TimestampMixin, UUIDPkMixin
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
LargeBinary,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
text,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import ARRAY, JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from ..enums import (
|
||||
AgentKind,
|
||||
CatalogStatus,
|
||||
PublishKind,
|
||||
PublishRequestStatus,
|
||||
SkillOrigin,
|
||||
SkillScope,
|
||||
)
|
||||
from .marketplace import _one_of
|
||||
|
||||
|
||||
def _user_fk(nullable: bool = False):
|
||||
return mapped_column(ForeignKey("app_user.id", ondelete="RESTRICT"), nullable=nullable)
|
||||
|
||||
|
||||
def _created_at():
|
||||
return mapped_column(DateTime(timezone=True), nullable=False, server_default=text("now()"))
|
||||
|
||||
|
||||
class Skill(UUIDPkMixin, TimestampMixin, Base):
|
||||
__tablename__ = "skill"
|
||||
|
||||
slug: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
name: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
owner_id: Mapped[uuid.UUID] = _user_fk()
|
||||
scope: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=SkillScope.personal.value)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=CatalogStatus.active.value)
|
||||
# For a firm skill: only the approved version. use_alter breaks the skill ↔ version cycle.
|
||||
current_version_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
ForeignKey("skill_version.id", ondelete="RESTRICT", use_alter=True))
|
||||
origin: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=SkillOrigin.authored.value)
|
||||
|
||||
__table_args__ = (
|
||||
CheckConstraint(_one_of("scope", SkillScope), name="scope"),
|
||||
CheckConstraint(_one_of("status", CatalogStatus), name="status"),
|
||||
CheckConstraint(_one_of("origin", SkillOrigin), name="origin"),
|
||||
CheckConstraint(r"slug ~ '^[a-z0-9]+(-[a-z0-9]+)*$'", name="slug"),
|
||||
UniqueConstraint("owner_id", "slug", name="uq_skill_owner_slug"),
|
||||
Index("ix_skill_scope", "scope"),
|
||||
)
|
||||
|
||||
|
||||
class SkillVersion(UUIDPkMixin, Base):
|
||||
__tablename__ = "skill_version"
|
||||
|
||||
skill_id: Mapped[uuid.UUID] = mapped_column(
|
||||
ForeignKey("skill.id", ondelete="RESTRICT"), nullable=False)
|
||||
n: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
# 30 000 characters: the chat tool's result limit.
|
||||
body_md: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
# An imported licence is the author's and is never overwritten (the service's rule).
|
||||
license: Mapped[str] = mapped_column(Text, nullable=False, server_default="proprietary")
|
||||
# Frontmatter outside the schema. `metadata` is reserved on declarative classes, hence `meta`.
|
||||
meta: Mapped[dict] = mapped_column(
|
||||
"metadata", JSONB, nullable=False, server_default=text("'{}'::jsonb"))
|
||||
content_sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
provenance: Mapped[dict | None] = mapped_column(JSONB)
|
||||
created_at: Mapped[dt.datetime] = _created_at()
|
||||
created_by: Mapped[uuid.UUID] = _user_fk()
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("skill_id", "n", name="uq_skill_version_skill_n"),
|
||||
CheckConstraint("n >= 1", name="n_positive"),
|
||||
CheckConstraint("char_length(description) <= 1024", name="description_length"),
|
||||
CheckConstraint("char_length(body_md) <= 30000", name="body_length"),
|
||||
CheckConstraint("octet_length(metadata::text) <= 8192", name="metadata_size"),
|
||||
CheckConstraint("content_sha256 ~ '^[0-9a-f]{64}$'", name="content_sha256"),
|
||||
)
|
||||
|
||||
|
||||
class SkillFile(UUIDPkMixin, Base):
|
||||
__tablename__ = "skill_file"
|
||||
|
||||
skill_version_id: Mapped[uuid.UUID] = mapped_column(
|
||||
ForeignKey("skill_version.id", ondelete="RESTRICT"), nullable=False)
|
||||
path: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
media_type: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
size: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# The original bytes, kept for export.
|
||||
content: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("skill_version_id", "path", name="uq_skill_file_version_path"),
|
||||
CheckConstraint("size >= 0", name="size"),
|
||||
CheckConstraint("sha256 ~ '^[0-9a-f]{64}$'", name="sha256"),
|
||||
)
|
||||
|
||||
|
||||
class Agent(UUIDPkMixin, TimestampMixin, Base):
|
||||
__tablename__ = "agent"
|
||||
|
||||
name: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
owner_id: Mapped[uuid.UUID] = _user_fk()
|
||||
scope: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=SkillScope.personal.value)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=CatalogStatus.active.value)
|
||||
kind: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=AgentKind.assistant.value)
|
||||
current_version_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
ForeignKey("agent_version.id", ondelete="RESTRICT", use_alter=True))
|
||||
|
||||
__table_args__ = (
|
||||
CheckConstraint(_one_of("scope", SkillScope), name="scope"),
|
||||
CheckConstraint(_one_of("status", CatalogStatus), name="status"),
|
||||
CheckConstraint(_one_of("kind", AgentKind), name="kind"),
|
||||
Index("ix_agent_owner_id", "owner_id"),
|
||||
Index("ix_agent_scope", "scope"),
|
||||
)
|
||||
|
||||
|
||||
class AgentVersion(UUIDPkMixin, Base):
|
||||
__tablename__ = "agent_version"
|
||||
|
||||
agent_id: Mapped[uuid.UUID] = mapped_column(
|
||||
ForeignKey("agent.id", ondelete="RESTRICT"), nullable=False)
|
||||
n: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
instructions_md: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
# A hint, never a grant (D3): the platform decides which model and tools an agent gets.
|
||||
model_policy: Mapped[dict | None] = mapped_column(JSONB)
|
||||
tools_requested: Mapped[list[str]] = mapped_column(
|
||||
ARRAY(Text), nullable=False, server_default=text("'{}'::text[]"))
|
||||
created_at: Mapped[dt.datetime] = _created_at()
|
||||
created_by: Mapped[uuid.UUID] = _user_fk()
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("agent_id", "n", name="uq_agent_version_agent_n"),
|
||||
CheckConstraint("n >= 1", name="n_positive"),
|
||||
)
|
||||
|
||||
|
||||
class AgentVersionSkill(Base):
|
||||
"""A skill VERSION pinned to an agent version — a row, not a UUID array, so RESTRICT holds:
|
||||
archiving a skill leaves every agent running on the version it pinned."""
|
||||
__tablename__ = "agent_version_skill"
|
||||
|
||||
agent_version_id: Mapped[uuid.UUID] = mapped_column(
|
||||
ForeignKey("agent_version.id", ondelete="RESTRICT"), primary_key=True)
|
||||
skill_version_id: Mapped[uuid.UUID] = mapped_column(
|
||||
ForeignKey("skill_version.id", ondelete="RESTRICT"), primary_key=True)
|
||||
position: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("agent_version_id", "position",
|
||||
name="uq_agent_version_skill_version_position"),
|
||||
Index("ix_agent_version_skill_skill_version_id", "skill_version_id"),
|
||||
)
|
||||
|
||||
|
||||
class PublishRequest(UUIDPkMixin, TimestampMixin, Base):
|
||||
"""Asks an administrator to publish ONE version to the firm (T1: every version, not only the
|
||||
first). `version_id` names a `skill_version` or an `agent_version` according to `kind`, so it
|
||||
carries no foreign key; the service resolves it, and versions are never deleted."""
|
||||
__tablename__ = "publish_request"
|
||||
|
||||
kind: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
version_id: Mapped[uuid.UUID] = mapped_column(nullable=False)
|
||||
requested_by: Mapped[uuid.UUID] = _user_fk()
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, server_default=PublishRequestStatus.pending.value)
|
||||
decided_by: Mapped[uuid.UUID | None] = _user_fk(nullable=True)
|
||||
decided_at: Mapped[dt.datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
note: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
__table_args__ = (
|
||||
CheckConstraint(_one_of("kind", PublishKind), name="kind"),
|
||||
CheckConstraint(_one_of("status", PublishRequestStatus), name="status"),
|
||||
# One open request per version; decided ones accumulate as history.
|
||||
Index("uq_publish_request_pending", "kind", "version_id", unique=True,
|
||||
postgresql_where=text("status = 'pending'")),
|
||||
Index("ix_publish_request_status", "status"),
|
||||
)
|
||||
@@ -28,9 +28,14 @@ def test_core_tables_are_registered():
|
||||
assert {"app_user", "session", "app_setting", "audit_log"} <= set(_metadata().tables)
|
||||
|
||||
|
||||
SKILL_TABLES = ("skill", "skill_version", "skill_file", "agent", "agent_version",
|
||||
"agent_version_skill", "publish_request")
|
||||
|
||||
|
||||
def test_the_apps_own_tables_are_registered():
|
||||
"""A model missing from `app/models/__init__.py` silently gets no table."""
|
||||
assert {"module", "module_assignment", "access_request"} <= set(_metadata().tables)
|
||||
assert set(SKILL_TABLES) <= set(_metadata().tables)
|
||||
assert "record" not in _metadata().tables
|
||||
|
||||
|
||||
@@ -52,14 +57,30 @@ def test_marketplace_vocabularies_are_check_constraints_not_enum_types():
|
||||
and each CHECK lists exactly the StrEnum's values, so the code and the database agree."""
|
||||
from sqlalchemy import CheckConstraint
|
||||
|
||||
from app.enums import AccessRequestStatus, Availability, ModuleStatus
|
||||
from app.enums import (
|
||||
AccessRequestStatus,
|
||||
AgentKind,
|
||||
Availability,
|
||||
CatalogStatus,
|
||||
ModuleStatus,
|
||||
PublishKind,
|
||||
PublishRequestStatus,
|
||||
SkillOrigin,
|
||||
SkillScope,
|
||||
)
|
||||
|
||||
tables = _metadata().tables
|
||||
for name in ("module", "module_assignment", "access_request"):
|
||||
for name in ("module", "module_assignment", "access_request", *SKILL_TABLES):
|
||||
assert not [c for c in tables[name].columns if isinstance(c.type, PGEnum)], name
|
||||
assert "record_status" not in enum_types(_metadata())
|
||||
expected = {("module", "status"): ModuleStatus, ("module", "availability"): Availability,
|
||||
("access_request", "status"): AccessRequestStatus}
|
||||
("access_request", "status"): AccessRequestStatus,
|
||||
("skill", "scope"): SkillScope, ("skill", "status"): CatalogStatus,
|
||||
("skill", "origin"): SkillOrigin,
|
||||
("agent", "scope"): SkillScope, ("agent", "status"): CatalogStatus,
|
||||
("agent", "kind"): AgentKind,
|
||||
("publish_request", "kind"): PublishKind,
|
||||
("publish_request", "status"): PublishRequestStatus}
|
||||
for (table, column), enum in expected.items():
|
||||
sql = [str(c.sqltext) for c in tables[table].constraints
|
||||
if isinstance(c, CheckConstraint) and str(c.sqltext).startswith(f"{column} IN")]
|
||||
|
||||
Reference in New Issue
Block a user