Spaces:
Running

File size: 2,965 Bytes
fd5dd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Approval rows: bound to one exact call, expiring, single-use."""

from __future__ import annotations

import uuid
from datetime import datetime

from sqlalchemy import Boolean, DateTime, Index, String, Text
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column

from core.db.base import Base, TenantMixin, TimestampMixin, new_id


class Approval(TenantMixin, TimestampMixin, Base):
    __tablename__ = "approvals"
    __table_args__ = (Index("ix_approvals_call_hash_status", "call_hash", "status"),)

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
    run_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
    thread_id: Mapped[str | None] = mapped_column(String(200), nullable=True)
    call_hash: Mapped[str] = mapped_column(String(64), nullable=False)
    actor: Mapped[str] = mapped_column(String(200), nullable=False)
    tool: Mapped[str] = mapped_column(String(200), nullable=False)
    args: Mapped[dict] = mapped_column(JSONB, nullable=False)
    taint: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
    preview: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
    preview_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
    reason: Mapped[str] = mapped_column(Text, nullable=False)
    policy_rule: Mapped[str | None] = mapped_column(String(200), nullable=True)
    kind: Mapped[str] = mapped_column(String(20), nullable=False, default="review")
    # pending | approved | rejected | responded | edited | expired
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")
    required_role: Mapped[str] = mapped_column(String(30), nullable=False, default="manager")
    requires_stepup: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    decided_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
    decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    decision_note: Mapped[str | None] = mapped_column(Text, nullable=True)
    edited_args: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
    replaced_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
    expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    consumed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    # Phase 7 undo window: set when the approved call queued a message that waits until
    # ``send_after``; ``undo`` is the gateway call that cancels it ({"tool", "args"}).
    send_after: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    undo_url: Mapped[str | None] = mapped_column(String(300), nullable=True)
    undo: Mapped[dict | None] = mapped_column(JSONB, nullable=True)