20260813_496_model_gateway_agent_runtime.py 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Add durable default-deny model gateway runtime governance.
  2. Revision ID: 20260813_496
  3. Revises: 20260811_495
  4. """
  5. from alembic import op
  6. revision = "20260813_496"
  7. down_revision = "20260811_495"
  8. branch_labels = None
  9. depends_on = None
  10. def upgrade() -> None:
  11. op.execute("""
  12. CREATE TABLE IF NOT EXISTS public.model_gateway_routes (
  13. route_id text PRIMARY KEY, tenant_id text NOT NULL,
  14. principal_id uuid NOT NULL, business_domain_uid uuid NOT NULL,
  15. environment text NOT NULL, provider text NOT NULL, model text NOT NULL,
  16. prompt_version text NOT NULL, generation text NOT NULL,
  17. canary_status text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
  18. UNIQUE (tenant_id, principal_id, business_domain_uid, environment,
  19. provider, model, prompt_version, generation)
  20. )
  21. """)
  22. op.execute("""
  23. CREATE TABLE IF NOT EXISTS public.agent_runtime_budgets (
  24. tenant_id text PRIMARY KEY, token_remaining bigint NOT NULL CHECK (token_remaining >= 0),
  25. cost_remaining_micros bigint NOT NULL CHECK (cost_remaining_micros >= 0),
  26. tool_remaining integer NOT NULL CHECK (tool_remaining >= 0),
  27. time_remaining_ms bigint NOT NULL CHECK (time_remaining_ms >= 0),
  28. concurrency_remaining integer NOT NULL CHECK (concurrency_remaining >= 0),
  29. lease_fence bigint NOT NULL DEFAULT 0, updated_at timestamptz NOT NULL DEFAULT now()
  30. )
  31. """)
  32. op.execute("""
  33. CREATE TABLE IF NOT EXISTS public.agent_runtime_model_budgets (
  34. tenant_id text NOT NULL REFERENCES public.agent_runtime_budgets(tenant_id) ON DELETE CASCADE,
  35. model text NOT NULL, remaining integer NOT NULL CHECK (remaining >= 0),
  36. PRIMARY KEY (tenant_id, model)
  37. )
  38. """)
  39. op.execute("""
  40. CREATE TABLE IF NOT EXISTS public.agent_runtime_reservations (
  41. reservation_id uuid PRIMARY KEY, tenant_id text NOT NULL,
  42. idempotency_key text NOT NULL, model text NOT NULL, token_count bigint NOT NULL,
  43. cost_micros bigint NOT NULL, tool_count integer NOT NULL, time_ms bigint NOT NULL,
  44. lease_fence bigint NOT NULL, status text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
  45. UNIQUE (tenant_id, idempotency_key)
  46. )
  47. """)
  48. op.execute("""
  49. CREATE TABLE IF NOT EXISTS public.agent_runtime_capabilities (
  50. capability_id uuid PRIMARY KEY, tenant_id text NOT NULL,
  51. business_domain_uid uuid NOT NULL, environment text NOT NULL,
  52. interface_type text NOT NULL, tool_name text NOT NULL, action text NOT NULL,
  53. capability_set jsonb NOT NULL, risk_level text NOT NULL,
  54. enabled boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now(),
  55. UNIQUE (tenant_id, business_domain_uid, environment, interface_type, tool_name, action)
  56. )
  57. """)
  58. op.execute("""
  59. CREATE TABLE IF NOT EXISTS public.agent_runtime_states (
  60. tenant_id text PRIMARY KEY, state text NOT NULL, incident_ref text,
  61. approval_ref text, lease_fence bigint NOT NULL DEFAULT 0,
  62. updated_at timestamptz NOT NULL DEFAULT now()
  63. )
  64. """)
  65. op.execute("""
  66. CREATE TABLE IF NOT EXISTS public.agent_generation_canaries (
  67. tenant_id text NOT NULL, generation text NOT NULL, parent_generation text,
  68. status text NOT NULL, lease_fence bigint NOT NULL DEFAULT 0,
  69. approval_ref text, created_at timestamptz NOT NULL DEFAULT now(),
  70. PRIMARY KEY (tenant_id, generation)
  71. )
  72. """)
  73. op.execute("""
  74. CREATE TABLE IF NOT EXISTS public.agent_invocation_audits (
  75. invocation_id uuid PRIMARY KEY, tenant_id text NOT NULL,
  76. idempotency_key text NOT NULL, route_id text NOT NULL, generation text NOT NULL,
  77. lease_fence bigint NOT NULL, input_hash text NOT NULL,
  78. evidence_digests jsonb NOT NULL, decision text NOT NULL,
  79. created_at timestamptz NOT NULL DEFAULT now(),
  80. UNIQUE (tenant_id, idempotency_key)
  81. )
  82. """)
  83. def downgrade() -> None:
  84. op.execute("DROP TABLE IF EXISTS public.agent_invocation_audits")
  85. op.execute("DROP TABLE IF EXISTS public.agent_generation_canaries")
  86. op.execute("DROP TABLE IF EXISTS public.agent_runtime_states")
  87. op.execute("DROP TABLE IF EXISTS public.agent_runtime_capabilities")
  88. op.execute("DROP TABLE IF EXISTS public.agent_runtime_reservations")
  89. op.execute("DROP TABLE IF EXISTS public.agent_runtime_model_budgets")
  90. op.execute("DROP TABLE IF EXISTS public.agent_runtime_budgets")
  91. op.execute("DROP TABLE IF EXISTS public.model_gateway_routes")