from __future__ import annotations from sqlalchemy import ( ForeignKey, Index, Integer, String, Text, UniqueConstraint, text, ) from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column class Base(DeclarativeBase): pass class HostRow(Base): __tablename__ = "host_registrations" __table_args__ = ( UniqueConstraint( "agent_instance_id", name="uq_host_registrations_agent_instance_id", ), UniqueConstraint( "credential_digest", name="uq_host_registrations_credential_digest", ), UniqueConstraint( "enrollment_token_digest", name="uq_host_registrations_enrollment_token_digest", ), ) host_id: Mapped[str] = mapped_column(String, primary_key=True) address: Mapped[str | None] = mapped_column(String, nullable=True) last_seen_at: Mapped[str] = mapped_column(String, nullable=False) agent_instance_id: Mapped[str | None] = mapped_column( String, nullable=True, ) credential_digest: Mapped[str | None] = mapped_column( String, nullable=True, ) enrollment_token_digest: Mapped[str | None] = mapped_column( String, nullable=True, ) display_name: Mapped[str | None] = mapped_column(String, nullable=True) enrolled_at: Mapped[str | None] = mapped_column(String, nullable=True) revoked_at: Mapped[str | None] = mapped_column(String, nullable=True) planner_transport: Mapped[str] = mapped_column( String, nullable=False, default="direct", server_default=text("'direct'") ) class DeviceEnrollmentRow(Base): __tablename__ = "device_enrollments" __table_args__ = ( UniqueConstraint( "host_id", "local_device_id", name="uq_device_enrollments_host_local", ), Index("ix_device_enrollments_host_id", "host_id"), ) device_id: Mapped[str] = mapped_column(String, primary_key=True) host_id: Mapped[str] = mapped_column(String, nullable=False) local_device_id: Mapped[str] = mapped_column(String, nullable=False) driver_type: Mapped[str] = mapped_column(String, nullable=False) name: Mapped[str | None] = mapped_column(String, nullable=True) capability_tags_json: Mapped[str] = mapped_column(Text, nullable=False) enrolled_at: Mapped[str] = mapped_column(String, nullable=False) revoked_at: Mapped[str | None] = mapped_column(String, nullable=True) class PooledDeviceRow(Base): __tablename__ = "pooled_devices" host_id: Mapped[str] = mapped_column(String, primary_key=True) device_id: Mapped[str] = mapped_column(String, primary_key=True) driver_type: Mapped[str] = mapped_column(String, nullable=False) status: Mapped[str] = mapped_column(String, nullable=False) capability_tags_json: Mapped[str] = mapped_column(Text, nullable=False) synced_at: Mapped[str | None] = mapped_column(String, nullable=True) class ScheduledTaskRow(Base): __tablename__ = "scheduled_tasks" id: Mapped[str] = mapped_column(String, primary_key=True) goal: Mapped[str | None] = mapped_column(Text, nullable=True) workflow_definition_id: Mapped[str | None] = mapped_column(String, nullable=True) constraints_json: Mapped[str] = mapped_column(Text, nullable=False) status: Mapped[str] = mapped_column(String, nullable=False) assigned_device_id: Mapped[str | None] = mapped_column(String, nullable=True) assigned_host_id: Mapped[str | None] = mapped_column(String, nullable=True) attempt_count: Mapped[int] = mapped_column( Integer, nullable=False, default=0, server_default=text("0"), ) lease_id: Mapped[str | None] = mapped_column(String, nullable=True) lease_expires_at: Mapped[str | None] = mapped_column(String, nullable=True) progress_step_index: Mapped[int | None] = mapped_column(Integer, nullable=True) progress_step_status: Mapped[str | None] = mapped_column(String, nullable=True) progress_summary: Mapped[str | None] = mapped_column(String, nullable=True) progress_updated_at: Mapped[str | None] = mapped_column(String, nullable=True) failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True) result_json: Mapped[str | None] = mapped_column(Text, nullable=True) updated_at: Mapped[str | None] = mapped_column(String, nullable=True) created_at: Mapped[str] = mapped_column(String, nullable=False) class TaskAttemptRow(Base): __tablename__ = "task_attempts" task_id: Mapped[str] = mapped_column(String, primary_key=True) attempt: Mapped[int] = mapped_column(Integer, primary_key=True) lease_id: Mapped[str] = mapped_column(String, nullable=False) host_id: Mapped[str] = mapped_column(String, nullable=False) device_id: Mapped[str] = mapped_column(String, nullable=False) status: Mapped[str] = mapped_column(String, nullable=False) lease_expires_at: Mapped[str] = mapped_column(String, nullable=False) created_at: Mapped[str] = mapped_column(String, nullable=False) completed_at: Mapped[str | None] = mapped_column(String, nullable=True) failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True) result_json: Mapped[str | None] = mapped_column(Text, nullable=True) class PlannerDecisionLogRow(Base): __tablename__ = "planner_decision_log" __table_args__ = ( Index( "ix_planner_decision_log_task_attempt", "task_id", "attempt", "step_index", unique=True, ), Index("ix_planner_decision_log_host_created", "host_id", "created_at"), ) id: Mapped[str] = mapped_column(String, primary_key=True) host_id: Mapped[str] = mapped_column(String, nullable=False) task_id: Mapped[str] = mapped_column(String, nullable=False) attempt: Mapped[int] = mapped_column(Integer, nullable=False) step_index: Mapped[int] = mapped_column(Integer, nullable=False) system_prompt: Mapped[str] = mapped_column(Text, nullable=False) user_prompt: Mapped[str] = mapped_column(Text, nullable=False) tool_name: Mapped[str] = mapped_column(String, nullable=False) arguments_json: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[str] = mapped_column(String, nullable=False) class PluginRow(Base): __tablename__ = "plugins" name: Mapped[str] = mapped_column(String, primary_key=True) version: Mapped[str] = mapped_column(String, nullable=False) entry_point_kind: Mapped[str] = mapped_column(String, nullable=False) target: Mapped[str] = mapped_column(String, nullable=False) wired: Mapped[int] = mapped_column(Integer, nullable=False) class UserRow(Base): __tablename__ = "cloud_users" __table_args__ = ( UniqueConstraint( "username_normalized", name="uq_cloud_users_username_normalized" ), Index("ix_cloud_users_enabled_role", "enabled", "role"), ) id: Mapped[str] = mapped_column(String, primary_key=True) username: Mapped[str] = mapped_column(String, nullable=False) username_normalized: Mapped[str] = mapped_column(String, nullable=False) display_name: Mapped[str] = mapped_column(String, nullable=False) password_hash: Mapped[str] = mapped_column(Text, nullable=False) role: Mapped[str] = mapped_column(String, nullable=False) enabled: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1") ) must_change_password: Mapped[int] = mapped_column( Integer, nullable=False, default=0, server_default=text("0"), ) authentication_version: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1"), ) created_at: Mapped[str] = mapped_column(String, nullable=False) updated_at: Mapped[str] = mapped_column(String, nullable=False) last_login_at: Mapped[str | None] = mapped_column(String, nullable=True) class UserSessionRow(Base): __tablename__ = "cloud_user_sessions" __table_args__ = ( UniqueConstraint("token_digest", name="uq_cloud_user_sessions_token_digest"), Index("ix_cloud_user_sessions_user_id", "user_id"), Index("ix_cloud_user_sessions_absolute_expires_at", "absolute_expires_at"), ) id: Mapped[str] = mapped_column(String, primary_key=True) user_id: Mapped[str] = mapped_column( ForeignKey("cloud_users.id", ondelete="CASCADE"), nullable=False, ) token_digest: Mapped[str] = mapped_column(String, nullable=False) csrf_digest: Mapped[str] = mapped_column(String, nullable=False) authentication_version: Mapped[int] = mapped_column(Integer, nullable=False) issued_at: Mapped[str] = mapped_column(String, nullable=False) last_seen_at: Mapped[str] = mapped_column(String, nullable=False) idle_expires_at: Mapped[str] = mapped_column(String, nullable=False) absolute_expires_at: Mapped[str] = mapped_column(String, nullable=False) revoked_at: Mapped[str | None] = mapped_column(String, nullable=True) class LoginThrottleRow(Base): __tablename__ = "cloud_login_throttles" username_normalized: Mapped[str] = mapped_column(String, primary_key=True) client_bucket: Mapped[str] = mapped_column(String, primary_key=True) failure_count: Mapped[int] = mapped_column(Integer, nullable=False) window_started_at: Mapped[str] = mapped_column(String, nullable=False) last_attempt_at: Mapped[str] = mapped_column(String, nullable=False) blocked_until: Mapped[str | None] = mapped_column(String, nullable=True) class AuthAuditRow(Base): __tablename__ = "cloud_auth_audit_events" __table_args__ = ( Index("ix_cloud_auth_audit_events_occurred_at", "occurred_at"), Index("ix_cloud_auth_audit_events_target_user_id", "target_user_id"), ) id: Mapped[str] = mapped_column(String, primary_key=True) occurred_at: Mapped[str] = mapped_column(String, nullable=False) actor_principal_id: Mapped[str | None] = mapped_column(String, nullable=True) target_user_id: Mapped[str | None] = mapped_column(String, nullable=True) action: Mapped[str] = mapped_column(String, nullable=False) outcome: Mapped[str] = mapped_column(String, nullable=False) correlation_id: Mapped[str | None] = mapped_column(String, nullable=True) metadata_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}") class UserSubmissionPolicyRow(Base): __tablename__ = "cloud_user_submission_policies" user_id: Mapped[str] = mapped_column( ForeignKey("cloud_users.id", ondelete="CASCADE"), primary_key=True ) revision: Mapped[int] = mapped_column(Integer, nullable=False) submission_enabled: Mapped[int] = mapped_column(Integer, nullable=False) allowed_host_ids_json: Mapped[str | None] = mapped_column(Text, nullable=True) allowed_device_targets_json: Mapped[str | None] = mapped_column(Text, nullable=True) updated_at: Mapped[str] = mapped_column(String, nullable=False) class HostGovernancePolicyRow(Base): __tablename__ = "cloud_host_governance_policies" host_id: Mapped[str] = mapped_column( ForeignKey("host_registrations.host_id", ondelete="CASCADE"), primary_key=True ) revision: Mapped[int] = mapped_column(Integer, nullable=False) self_submission_enabled: Mapped[int] = mapped_column(Integer, nullable=False) max_active_tasks: Mapped[int | None] = mapped_column(Integer, nullable=True) daily_token_budget: Mapped[int | None] = mapped_column(Integer, nullable=True) updated_at: Mapped[str] = mapped_column(String, nullable=False) class TokenReservationRow(Base): __tablename__ = "cloud_token_reservations" __table_args__ = ( Index("ix_cloud_token_reservations_host_day", "host_id", "usage_day"), Index("ix_cloud_token_reservations_expires_at", "expires_at"), ) id: Mapped[str] = mapped_column(String, primary_key=True) host_id: Mapped[str] = mapped_column( ForeignKey("host_registrations.host_id", ondelete="CASCADE"), nullable=False ) usage_day: Mapped[str] = mapped_column(String, nullable=False) reserved_tokens: Mapped[int] = mapped_column(Integer, nullable=False) task_id: Mapped[str | None] = mapped_column(String, nullable=True) attempt: Mapped[int | None] = mapped_column(Integer, nullable=True) created_at: Mapped[str] = mapped_column(String, nullable=False) expires_at: Mapped[str] = mapped_column(String, nullable=False) class TokenUsageEventRow(Base): __tablename__ = "cloud_token_usage_events" __table_args__ = ( Index("ix_cloud_token_usage_events_host_day", "host_id", "usage_day"), Index("ix_cloud_token_usage_events_occurred_at", "occurred_at"), ) id: Mapped[str] = mapped_column(String, primary_key=True) host_id: Mapped[str] = mapped_column( ForeignKey("host_registrations.host_id", ondelete="CASCADE"), nullable=False ) usage_day: Mapped[str] = mapped_column(String, nullable=False) task_id: Mapped[str | None] = mapped_column(String, nullable=True) attempt: Mapped[int | None] = mapped_column(Integer, nullable=True) provider: Mapped[str] = mapped_column(String, nullable=False) model: Mapped[str] = mapped_column(String, nullable=False) input_tokens: Mapped[int | None] = mapped_column(Integer, nullable=True) output_tokens: Mapped[int | None] = mapped_column(Integer, nullable=True) total_tokens: Mapped[int] = mapped_column(Integer, nullable=False) occurred_at: Mapped[str] = mapped_column(String, nullable=False) class LlmProviderProfileRow(Base): __tablename__ = "cloud_llm_provider_profiles" __table_args__ = ( UniqueConstraint( "name_normalized", name="uq_cloud_llm_provider_profiles_name_normalized", ), Index("ix_cloud_llm_provider_profiles_enabled", "enabled"), ) id: Mapped[str] = mapped_column(String, primary_key=True) name: Mapped[str] = mapped_column(String, nullable=False) name_normalized: Mapped[str] = mapped_column(String, nullable=False) provider_type: Mapped[str] = mapped_column(String, nullable=False) model: Mapped[str] = mapped_column(String, nullable=False) base_url: Mapped[str | None] = mapped_column(String, nullable=True) timeout_seconds: Mapped[float] = mapped_column(nullable=False) api_key_ciphertext: Mapped[str] = mapped_column(Text, nullable=False) key_last_rotated_at: Mapped[str] = mapped_column(String, nullable=False) enabled: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1") ) revision: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1") ) created_at: Mapped[str] = mapped_column(String, nullable=False) updated_at: Mapped[str] = mapped_column(String, nullable=False) class LlmProviderSettingsRow(Base): __tablename__ = "cloud_llm_provider_settings" id: Mapped[str] = mapped_column(String, primary_key=True) active_profile_id: Mapped[str | None] = mapped_column( ForeignKey("cloud_llm_provider_profiles.id"), nullable=True ) revision: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1") ) updated_at: Mapped[str] = mapped_column(String, nullable=False) class CloudSkillRow(Base): __tablename__ = "cloud_skills" __table_args__ = ( UniqueConstraint( "name_normalized", name="uq_cloud_skills_name_normalized", ), Index("ix_cloud_skills_kind", "kind"), ) id: Mapped[str] = mapped_column(String, primary_key=True) name: Mapped[str] = mapped_column(String, nullable=False) name_normalized: Mapped[str] = mapped_column(String, nullable=False) kind: Mapped[str] = mapped_column(String, nullable=False) description: Mapped[str] = mapped_column(Text, nullable=False) tags_json: Mapped[str] = mapped_column(Text, nullable=False) content: Mapped[str] = mapped_column(Text, nullable=False) steps_json: Mapped[str] = mapped_column(Text, nullable=False) parameters_json: Mapped[str] = mapped_column(Text, nullable=False) revision: Mapped[int] = mapped_column( Integer, nullable=False, default=1, server_default=text("1") ) created_at: Mapped[str] = mapped_column(String, nullable=False) updated_at: Mapped[str] = mapped_column(String, nullable=False) class CloudSkillEntitlementRow(Base): __tablename__ = "cloud_skill_entitlements" __table_args__ = ( UniqueConstraint( "skill_id", "host_id", name="uq_cloud_skill_entitlements_skill_host", ), Index("ix_cloud_skill_entitlements_host_id", "host_id"), Index("ix_cloud_skill_entitlements_skill_id", "skill_id"), ) skill_id: Mapped[str] = mapped_column( String, ForeignKey("cloud_skills.id"), primary_key=True, ) host_id: Mapped[str] = mapped_column(String, primary_key=True) granted_at: Mapped[str] = mapped_column(String, nullable=False) class CloudSkillSyncStateRow(Base): __tablename__ = "cloud_skill_sync_state" host_id: Mapped[str] = mapped_column(String, primary_key=True) last_version: Mapped[int] = mapped_column( Integer, nullable=False, default=0, server_default=text("0") ) updated_at: Mapped[str] = mapped_column(String, nullable=False) class CloudSkillHostChangeRow(Base): """Per-host changelog enabling incremental sync (design D3).""" __tablename__ = "cloud_skill_host_changes" __table_args__ = ( Index("ix_cloud_skill_host_changes_host_version", "host_id", "version"), ) host_id: Mapped[str] = mapped_column(String, primary_key=True) version: Mapped[int] = mapped_column(Integer, primary_key=True) skill_id: Mapped[str] = mapped_column(String, nullable=False) change_type: Mapped[str] = mapped_column(String, nullable=False) recorded_at: Mapped[str] = mapped_column(String, nullable=False) class HostSkillInventoryRow(Base): __tablename__ = "host_skill_inventory" host_id: Mapped[str] = mapped_column(String, primary_key=True) payload_json: Mapped[str] = mapped_column(Text, nullable=False) reported_at: Mapped[str] = mapped_column(String, nullable=False)