Преглед изворни кода

fix(provider-endpoints): close #742 endpoint update regressions (#746)

* fix: sync overrideResponse and overrideStatusCode in syncDefaultErrorRules

The update path for isDefault=true rules was missing overrideResponse and
overrideStatusCode fields, causing code-level changes to default error
override messages to never propagate to the database on restart.

* fix(providers): resolve endpoint update and sync issues (#742)

- Fix UI consistency: ensure operations return single authoritative result (success/error)
- Fix sync logic: prevent aggressive soft-deletion of sibling endpoints
- Fix unique constraints: align constraints to ignore soft-deleted rows
- Add comprehensive regression tests for direct edit and sync scenarios
- Add data repair tool and documentation for troubleshooting

* test: add tests for EditEndpointDialog toast behavior

* fix(provider-endpoints): apply review consistency follow-ups

* fix(ci): satisfy lint checks after dev merge

* fix(provider-endpoints): centralize direct edit error codes
Ding пре 3 дана
родитељ
комит
b16a9dce08

+ 80 - 0
docs/troubleshooting/provider-endpoint-742.md

@@ -0,0 +1,80 @@
+# Troubleshooting Guide: Provider Endpoint Issues (Issue #742)
+
+This guide addresses issues related to provider endpoint updates, disappearances, and the "dual JSON" response format observed in network logs.
+
+## 1. Understanding "Dual JSON" Responses (React Flight Protocol)
+
+Users may observe network responses containing multiple JSON-like structures or lines starting with `0:`, `1:`, etc. This is **expected behavior** for Next.js Server Actions using the React Flight protocol.
+
+### What is React Flight?
+React Flight is the underlying protocol used by React Server Components (RSC) and Server Actions to stream UI updates and data from the server to the client.
+
+### Example Response Format
+A typical Server Action response might look like this in the raw response tab:
+
+```text
+0:["$@1",["$","div",null,{"children":"..."}]]
+1:{"ok":true,"data":{...}}
+```
+
+- **Line `0:`**: Often contains UI updates, revalidated path data, or serialized component trees (RSC payload).
+- **Line `1:`**: Contains the actual return value of the Server Action (e.g., your `ok: true` or `error` object).
+
+### Why it looks like "Two JSONs"
+When inspecting the response in browser DevTools:
+1. **Preview Tab**: May only show the parsed result of the last chunk or a merged object, which can be confusing.
+2. **Response Tab**: Shows the raw stream with multiple lines.
+
+**Conclusion**: This is not a bug in the API logic but the transport protocol of Next.js. Focus on the `1:` (or subsequent) lines for the actual business logic result.
+
+## 2. Issue: Endpoint Updates Not Persisting ("Success but no change")
+
+### Symptoms
+- User edits an endpoint URL.
+- UI shows a success toast.
+- The URL reverts to the old value after refresh or re-opening the dialog.
+
+### Root Cause
+A unique constraint violation (e.g., duplicate URL for the same vendor/type) was occurring, but the generic error handler masked it as a "successful" operation in some UI paths, or a silent failure occurred where the cache wasn't updated.
+
+### Resolution (Fix #742)
+- **Stable Error Codes**: The backend now returns deterministic error codes (e.g., `PROVIDER_ENDPOINT_CONFLICT`) instead of generic failures.
+- **Read-After-Write Consistency**: The update action now explicitly verifies the written value matches the requested value before returning success.
+- **UI Feedback**: The frontend now displays specific error messages for conflicts and only shows "Success" when the data is truly persisted.
+
+## 3. Issue: Sibling Endpoints Disappearing
+
+### Symptoms
+- Editing a Provider (e.g., changing the main API Key or unrelated field) causes secondary Endpoints (e.g., a custom model endpoint) to disappear from the list.
+
+### Root Cause
+The `syncProviderEndpointOnProviderEdit` logic was overly aggressive. It treated "endpoints not matching the new URL" as "obsolete" and soft-deleted them, even if they were distinct endpoints meant to exist alongside the main one.
+
+### Resolution (Fix #742)
+- **Conservative Sync Strategy**: The sync logic now defaults to *keeping* sibling endpoints unless explicitly replaced.
+- **Soft-Delete Alignment**: We aligned the unique constraints to ignore soft-deleted rows, preventing "zombie" rows from blocking active endpoint creation/updates.
+
+## 4. How to Verify Fixes
+
+### 1. Direct Endpoint Edit
+- **Action**: Edit an endpoint URL to a value that already exists (active) for the same vendor.
+- **Expected**: Error toast "Endpoint conflict" (or similar), *not* success.
+- **Action**: Edit an endpoint URL to a new valid value.
+- **Expected**: Success toast, and the new value persists after refresh.
+
+### 2. Provider Edit Sync
+- **Pre-condition**: Have a Provider with URL A and a separate Endpoint with URL B under the same vendor.
+- **Action**: Edit the Provider's URL from A to C.
+- **Expected**:
+    - Provider URL updates to C.
+    - Endpoint with URL B *remains* visible and active (is not deleted).
+
+## 5. Data Repair (Dry-Run)
+
+If you suspect data loss (missing endpoints) from previous versions:
+
+Run the repair tool in dry-run mode (integration test):
+```bash
+bunx vitest run tests/integration/provider-endpoint-index-and-repair.test.ts -t "dry-run"
+```
+This will report any active Providers that are missing a corresponding active Endpoint, categorized by risk.

+ 2 - 0
drizzle/0065_stale_vertigo.sql

@@ -0,0 +1,2 @@
+DROP INDEX IF EXISTS "uniq_provider_endpoints_vendor_type_url";--> statement-breakpoint
+CREATE UNIQUE INDEX IF NOT EXISTS "uniq_provider_endpoints_vendor_type_url" ON "provider_endpoints" USING btree ("vendor_id","provider_type","url") WHERE "provider_endpoints"."deleted_at" IS NULL;

+ 2976 - 0
drizzle/meta/0065_snapshot.json

@@ -0,0 +1,2976 @@
+{
+  "id": "5d3e46c6-0881-4776-ae60-4b8e4d06f999",
+  "prevId": "9fd69a68-7794-42af-ac5f-83f874adeecf",
+  "version": "7",
+  "dialect": "postgresql",
+  "tables": {
+    "public.error_rules": {
+      "name": "error_rules",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "pattern": {
+          "name": "pattern",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "match_type": {
+          "name": "match_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'regex'"
+        },
+        "category": {
+          "name": "category",
+          "type": "varchar(50)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "description": {
+          "name": "description",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "override_response": {
+          "name": "override_response",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "override_status_code": {
+          "name": "override_status_code",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "is_default": {
+          "name": "is_default",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "priority": {
+          "name": "priority",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "idx_error_rules_enabled": {
+          "name": "idx_error_rules_enabled",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "priority",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "unique_pattern": {
+          "name": "unique_pattern",
+          "columns": [
+            {
+              "expression": "pattern",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": true,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_category": {
+          "name": "idx_category",
+          "columns": [
+            {
+              "expression": "category",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_match_type": {
+          "name": "idx_match_type",
+          "columns": [
+            {
+              "expression": "match_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.keys": {
+      "name": "keys",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "user_id": {
+          "name": "user_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "key": {
+          "name": "key",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "name": {
+          "name": "name",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": true
+        },
+        "expires_at": {
+          "name": "expires_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "can_login_web_ui": {
+          "name": "can_login_web_ui",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "limit_5h_usd": {
+          "name": "limit_5h_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_daily_usd": {
+          "name": "limit_daily_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_reset_mode": {
+          "name": "daily_reset_mode",
+          "type": "daily_reset_mode",
+          "typeSchema": "public",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'fixed'"
+        },
+        "daily_reset_time": {
+          "name": "daily_reset_time",
+          "type": "varchar(5)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'00:00'"
+        },
+        "limit_weekly_usd": {
+          "name": "limit_weekly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_monthly_usd": {
+          "name": "limit_monthly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_total_usd": {
+          "name": "limit_total_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_concurrent_sessions": {
+          "name": "limit_concurrent_sessions",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "provider_group": {
+          "name": "provider_group",
+          "type": "varchar(200)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'default'"
+        },
+        "cache_ttl_preference": {
+          "name": "cache_ttl_preference",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "deleted_at": {
+          "name": "deleted_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        }
+      },
+      "indexes": {
+        "idx_keys_user_id": {
+          "name": "idx_keys_user_id",
+          "columns": [
+            {
+              "expression": "user_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_keys_created_at": {
+          "name": "idx_keys_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_keys_deleted_at": {
+          "name": "idx_keys_deleted_at",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.message_request": {
+      "name": "message_request",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "provider_id": {
+          "name": "provider_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "user_id": {
+          "name": "user_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "key": {
+          "name": "key",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "model": {
+          "name": "model",
+          "type": "varchar(128)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "duration_ms": {
+          "name": "duration_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cost_usd": {
+          "name": "cost_usd",
+          "type": "numeric(21, 15)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0'"
+        },
+        "cost_multiplier": {
+          "name": "cost_multiplier",
+          "type": "numeric(10, 4)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "session_id": {
+          "name": "session_id",
+          "type": "varchar(64)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "request_sequence": {
+          "name": "request_sequence",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 1
+        },
+        "provider_chain": {
+          "name": "provider_chain",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "status_code": {
+          "name": "status_code",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "api_type": {
+          "name": "api_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "endpoint": {
+          "name": "endpoint",
+          "type": "varchar(256)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "original_model": {
+          "name": "original_model",
+          "type": "varchar(128)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "input_tokens": {
+          "name": "input_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "output_tokens": {
+          "name": "output_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "ttfb_ms": {
+          "name": "ttfb_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_creation_input_tokens": {
+          "name": "cache_creation_input_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_read_input_tokens": {
+          "name": "cache_read_input_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_creation_5m_input_tokens": {
+          "name": "cache_creation_5m_input_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_creation_1h_input_tokens": {
+          "name": "cache_creation_1h_input_tokens",
+          "type": "bigint",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_ttl_applied": {
+          "name": "cache_ttl_applied",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "context_1m_applied": {
+          "name": "context_1m_applied",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "special_settings": {
+          "name": "special_settings",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "error_message": {
+          "name": "error_message",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "error_stack": {
+          "name": "error_stack",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "error_cause": {
+          "name": "error_cause",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "blocked_by": {
+          "name": "blocked_by",
+          "type": "varchar(50)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "blocked_reason": {
+          "name": "blocked_reason",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "user_agent": {
+          "name": "user_agent",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "messages_count": {
+          "name": "messages_count",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "deleted_at": {
+          "name": "deleted_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        }
+      },
+      "indexes": {
+        "idx_message_request_user_date_cost": {
+          "name": "idx_message_request_user_date_cost",
+          "columns": [
+            {
+              "expression": "user_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "cost_usd",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_user_query": {
+          "name": "idx_message_request_user_query",
+          "columns": [
+            {
+              "expression": "user_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_session_id": {
+          "name": "idx_message_request_session_id",
+          "columns": [
+            {
+              "expression": "session_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_session_id_prefix": {
+          "name": "idx_message_request_session_id_prefix",
+          "columns": [
+            {
+              "expression": "\"session_id\" varchar_pattern_ops",
+              "asc": true,
+              "isExpression": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL AND (\"message_request\".\"blocked_by\" IS NULL OR \"message_request\".\"blocked_by\" <> 'warmup')",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_session_seq": {
+          "name": "idx_message_request_session_seq",
+          "columns": [
+            {
+              "expression": "session_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "request_sequence",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_endpoint": {
+          "name": "idx_message_request_endpoint",
+          "columns": [
+            {
+              "expression": "endpoint",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_blocked_by": {
+          "name": "idx_message_request_blocked_by",
+          "columns": [
+            {
+              "expression": "blocked_by",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"message_request\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_provider_id": {
+          "name": "idx_message_request_provider_id",
+          "columns": [
+            {
+              "expression": "provider_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_user_id": {
+          "name": "idx_message_request_user_id",
+          "columns": [
+            {
+              "expression": "user_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_key": {
+          "name": "idx_message_request_key",
+          "columns": [
+            {
+              "expression": "key",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_created_at": {
+          "name": "idx_message_request_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_message_request_deleted_at": {
+          "name": "idx_message_request_deleted_at",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.model_prices": {
+      "name": "model_prices",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "model_name": {
+          "name": "model_name",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "price_data": {
+          "name": "price_data",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "source": {
+          "name": "source",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'litellm'"
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "idx_model_prices_latest": {
+          "name": "idx_model_prices_latest",
+          "columns": [
+            {
+              "expression": "model_name",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": false,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_model_prices_model_name": {
+          "name": "idx_model_prices_model_name",
+          "columns": [
+            {
+              "expression": "model_name",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_model_prices_created_at": {
+          "name": "idx_model_prices_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": false,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_model_prices_source": {
+          "name": "idx_model_prices_source",
+          "columns": [
+            {
+              "expression": "source",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.notification_settings": {
+      "name": "notification_settings",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "enabled": {
+          "name": "enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "use_legacy_mode": {
+          "name": "use_legacy_mode",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "circuit_breaker_enabled": {
+          "name": "circuit_breaker_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "circuit_breaker_webhook": {
+          "name": "circuit_breaker_webhook",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_leaderboard_enabled": {
+          "name": "daily_leaderboard_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "daily_leaderboard_webhook": {
+          "name": "daily_leaderboard_webhook",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_leaderboard_time": {
+          "name": "daily_leaderboard_time",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'09:00'"
+        },
+        "daily_leaderboard_top_n": {
+          "name": "daily_leaderboard_top_n",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 5
+        },
+        "cost_alert_enabled": {
+          "name": "cost_alert_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "cost_alert_webhook": {
+          "name": "cost_alert_webhook",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cost_alert_threshold": {
+          "name": "cost_alert_threshold",
+          "type": "numeric(5, 2)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0.80'"
+        },
+        "cost_alert_check_interval": {
+          "name": "cost_alert_check_interval",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 60
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {},
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.notification_target_bindings": {
+      "name": "notification_target_bindings",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "notification_type": {
+          "name": "notification_type",
+          "type": "notification_type",
+          "typeSchema": "public",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "target_id": {
+          "name": "target_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "schedule_cron": {
+          "name": "schedule_cron",
+          "type": "varchar(100)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "schedule_timezone": {
+          "name": "schedule_timezone",
+          "type": "varchar(50)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "template_override": {
+          "name": "template_override",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "unique_notification_target_binding": {
+          "name": "unique_notification_target_binding",
+          "columns": [
+            {
+              "expression": "notification_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "target_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": true,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_notification_bindings_type": {
+          "name": "idx_notification_bindings_type",
+          "columns": [
+            {
+              "expression": "notification_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_notification_bindings_target": {
+          "name": "idx_notification_bindings_target",
+          "columns": [
+            {
+              "expression": "target_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {
+        "notification_target_bindings_target_id_webhook_targets_id_fk": {
+          "name": "notification_target_bindings_target_id_webhook_targets_id_fk",
+          "tableFrom": "notification_target_bindings",
+          "tableTo": "webhook_targets",
+          "columnsFrom": [
+            "target_id"
+          ],
+          "columnsTo": [
+            "id"
+          ],
+          "onDelete": "cascade",
+          "onUpdate": "no action"
+        }
+      },
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.provider_endpoint_probe_logs": {
+      "name": "provider_endpoint_probe_logs",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "endpoint_id": {
+          "name": "endpoint_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "source": {
+          "name": "source",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'scheduled'"
+        },
+        "ok": {
+          "name": "ok",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "status_code": {
+          "name": "status_code",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "latency_ms": {
+          "name": "latency_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "error_type": {
+          "name": "error_type",
+          "type": "varchar(64)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "error_message": {
+          "name": "error_message",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "idx_provider_endpoint_probe_logs_endpoint_created_at": {
+          "name": "idx_provider_endpoint_probe_logs_endpoint_created_at",
+          "columns": [
+            {
+              "expression": "endpoint_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": false,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_endpoint_probe_logs_created_at": {
+          "name": "idx_provider_endpoint_probe_logs_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {
+        "provider_endpoint_probe_logs_endpoint_id_provider_endpoints_id_fk": {
+          "name": "provider_endpoint_probe_logs_endpoint_id_provider_endpoints_id_fk",
+          "tableFrom": "provider_endpoint_probe_logs",
+          "tableTo": "provider_endpoints",
+          "columnsFrom": [
+            "endpoint_id"
+          ],
+          "columnsTo": [
+            "id"
+          ],
+          "onDelete": "cascade",
+          "onUpdate": "no action"
+        }
+      },
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.provider_endpoints": {
+      "name": "provider_endpoints",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "vendor_id": {
+          "name": "vendor_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "provider_type": {
+          "name": "provider_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'claude'"
+        },
+        "url": {
+          "name": "url",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "label": {
+          "name": "label",
+          "type": "varchar(200)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "sort_order": {
+          "name": "sort_order",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "last_probed_at": {
+          "name": "last_probed_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_probe_ok": {
+          "name": "last_probe_ok",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_probe_status_code": {
+          "name": "last_probe_status_code",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_probe_latency_ms": {
+          "name": "last_probe_latency_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_probe_error_type": {
+          "name": "last_probe_error_type",
+          "type": "varchar(64)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_probe_error_message": {
+          "name": "last_probe_error_message",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "deleted_at": {
+          "name": "deleted_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        }
+      },
+      "indexes": {
+        "uniq_provider_endpoints_vendor_type_url": {
+          "name": "uniq_provider_endpoints_vendor_type_url",
+          "columns": [
+            {
+              "expression": "vendor_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "provider_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "url",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": true,
+          "where": "\"provider_endpoints\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_endpoints_vendor_type": {
+          "name": "idx_provider_endpoints_vendor_type",
+          "columns": [
+            {
+              "expression": "vendor_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "provider_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"provider_endpoints\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_endpoints_enabled": {
+          "name": "idx_provider_endpoints_enabled",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "vendor_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "provider_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"provider_endpoints\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_endpoints_created_at": {
+          "name": "idx_provider_endpoints_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_endpoints_deleted_at": {
+          "name": "idx_provider_endpoints_deleted_at",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {
+        "provider_endpoints_vendor_id_provider_vendors_id_fk": {
+          "name": "provider_endpoints_vendor_id_provider_vendors_id_fk",
+          "tableFrom": "provider_endpoints",
+          "tableTo": "provider_vendors",
+          "columnsFrom": [
+            "vendor_id"
+          ],
+          "columnsTo": [
+            "id"
+          ],
+          "onDelete": "cascade",
+          "onUpdate": "no action"
+        }
+      },
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.provider_vendors": {
+      "name": "provider_vendors",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "website_domain": {
+          "name": "website_domain",
+          "type": "varchar(255)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "display_name": {
+          "name": "display_name",
+          "type": "varchar(200)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "website_url": {
+          "name": "website_url",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "favicon_url": {
+          "name": "favicon_url",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "uniq_provider_vendors_website_domain": {
+          "name": "uniq_provider_vendors_website_domain",
+          "columns": [
+            {
+              "expression": "website_domain",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": true,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_provider_vendors_created_at": {
+          "name": "idx_provider_vendors_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.providers": {
+      "name": "providers",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "name": {
+          "name": "name",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "description": {
+          "name": "description",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "url": {
+          "name": "url",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "key": {
+          "name": "key",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "provider_vendor_id": {
+          "name": "provider_vendor_id",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "weight": {
+          "name": "weight",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 1
+        },
+        "priority": {
+          "name": "priority",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "cost_multiplier": {
+          "name": "cost_multiplier",
+          "type": "numeric(10, 4)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'1.0'"
+        },
+        "group_tag": {
+          "name": "group_tag",
+          "type": "varchar(50)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "provider_type": {
+          "name": "provider_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'claude'"
+        },
+        "preserve_client_ip": {
+          "name": "preserve_client_ip",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "model_redirects": {
+          "name": "model_redirects",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "allowed_models": {
+          "name": "allowed_models",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'null'::jsonb"
+        },
+        "join_claude_pool": {
+          "name": "join_claude_pool",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "codex_instructions_strategy": {
+          "name": "codex_instructions_strategy",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'auto'"
+        },
+        "mcp_passthrough_type": {
+          "name": "mcp_passthrough_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'none'"
+        },
+        "mcp_passthrough_url": {
+          "name": "mcp_passthrough_url",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_5h_usd": {
+          "name": "limit_5h_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_daily_usd": {
+          "name": "limit_daily_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_reset_mode": {
+          "name": "daily_reset_mode",
+          "type": "daily_reset_mode",
+          "typeSchema": "public",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'fixed'"
+        },
+        "daily_reset_time": {
+          "name": "daily_reset_time",
+          "type": "varchar(5)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'00:00'"
+        },
+        "limit_weekly_usd": {
+          "name": "limit_weekly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_monthly_usd": {
+          "name": "limit_monthly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_total_usd": {
+          "name": "limit_total_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "total_cost_reset_at": {
+          "name": "total_cost_reset_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_concurrent_sessions": {
+          "name": "limit_concurrent_sessions",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "max_retry_attempts": {
+          "name": "max_retry_attempts",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "circuit_breaker_failure_threshold": {
+          "name": "circuit_breaker_failure_threshold",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 5
+        },
+        "circuit_breaker_open_duration": {
+          "name": "circuit_breaker_open_duration",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 1800000
+        },
+        "circuit_breaker_half_open_success_threshold": {
+          "name": "circuit_breaker_half_open_success_threshold",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 2
+        },
+        "proxy_url": {
+          "name": "proxy_url",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "proxy_fallback_to_direct": {
+          "name": "proxy_fallback_to_direct",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "first_byte_timeout_streaming_ms": {
+          "name": "first_byte_timeout_streaming_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "streaming_idle_timeout_ms": {
+          "name": "streaming_idle_timeout_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "request_timeout_non_streaming_ms": {
+          "name": "request_timeout_non_streaming_ms",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "website_url": {
+          "name": "website_url",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "favicon_url": {
+          "name": "favicon_url",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "cache_ttl_preference": {
+          "name": "cache_ttl_preference",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "context_1m_preference": {
+          "name": "context_1m_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "codex_reasoning_effort_preference": {
+          "name": "codex_reasoning_effort_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "codex_reasoning_summary_preference": {
+          "name": "codex_reasoning_summary_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "codex_text_verbosity_preference": {
+          "name": "codex_text_verbosity_preference",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "codex_parallel_tool_calls_preference": {
+          "name": "codex_parallel_tool_calls_preference",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "anthropic_max_tokens_preference": {
+          "name": "anthropic_max_tokens_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "anthropic_thinking_budget_preference": {
+          "name": "anthropic_thinking_budget_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "gemini_google_search_preference": {
+          "name": "gemini_google_search_preference",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "tpm": {
+          "name": "tpm",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "rpm": {
+          "name": "rpm",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "rpd": {
+          "name": "rpd",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "cc": {
+          "name": "cc",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 0
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "deleted_at": {
+          "name": "deleted_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "group_priorities": {
+          "name": "group_priorities",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'null'::jsonb"
+        }
+      },
+      "indexes": {
+        "idx_providers_enabled_priority": {
+          "name": "idx_providers_enabled_priority",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "priority",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "weight",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"providers\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_providers_group": {
+          "name": "idx_providers_group",
+          "columns": [
+            {
+              "expression": "group_tag",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"providers\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_providers_created_at": {
+          "name": "idx_providers_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_providers_deleted_at": {
+          "name": "idx_providers_deleted_at",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_providers_vendor_type": {
+          "name": "idx_providers_vendor_type",
+          "columns": [
+            {
+              "expression": "provider_vendor_id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "provider_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"providers\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {
+        "providers_provider_vendor_id_provider_vendors_id_fk": {
+          "name": "providers_provider_vendor_id_provider_vendors_id_fk",
+          "tableFrom": "providers",
+          "tableTo": "provider_vendors",
+          "columnsFrom": [
+            "provider_vendor_id"
+          ],
+          "columnsTo": [
+            "id"
+          ],
+          "onDelete": "restrict",
+          "onUpdate": "no action"
+        }
+      },
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.request_filters": {
+      "name": "request_filters",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "name": {
+          "name": "name",
+          "type": "varchar(100)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "description": {
+          "name": "description",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "scope": {
+          "name": "scope",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "action": {
+          "name": "action",
+          "type": "varchar(30)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "match_type": {
+          "name": "match_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "target": {
+          "name": "target",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "replacement": {
+          "name": "replacement",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "priority": {
+          "name": "priority",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": true,
+          "default": 0
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "binding_type": {
+          "name": "binding_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'global'"
+        },
+        "provider_ids": {
+          "name": "provider_ids",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "group_tags": {
+          "name": "group_tags",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "idx_request_filters_enabled": {
+          "name": "idx_request_filters_enabled",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "priority",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_request_filters_scope": {
+          "name": "idx_request_filters_scope",
+          "columns": [
+            {
+              "expression": "scope",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_request_filters_action": {
+          "name": "idx_request_filters_action",
+          "columns": [
+            {
+              "expression": "action",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_request_filters_binding": {
+          "name": "idx_request_filters_binding",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "binding_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.sensitive_words": {
+      "name": "sensitive_words",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "word": {
+          "name": "word",
+          "type": "varchar(255)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "match_type": {
+          "name": "match_type",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'contains'"
+        },
+        "description": {
+          "name": "description",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {
+        "idx_sensitive_words_enabled": {
+          "name": "idx_sensitive_words_enabled",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "match_type",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_sensitive_words_created_at": {
+          "name": "idx_sensitive_words_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.system_settings": {
+      "name": "system_settings",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "site_title": {
+          "name": "site_title",
+          "type": "varchar(128)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'Claude Code Hub'"
+        },
+        "allow_global_usage_view": {
+          "name": "allow_global_usage_view",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "currency_display": {
+          "name": "currency_display",
+          "type": "varchar(10)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'USD'"
+        },
+        "billing_model_source": {
+          "name": "billing_model_source",
+          "type": "varchar(20)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'original'"
+        },
+        "timezone": {
+          "name": "timezone",
+          "type": "varchar(64)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "enable_auto_cleanup": {
+          "name": "enable_auto_cleanup",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "cleanup_retention_days": {
+          "name": "cleanup_retention_days",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 30
+        },
+        "cleanup_schedule": {
+          "name": "cleanup_schedule",
+          "type": "varchar(50)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0 2 * * *'"
+        },
+        "cleanup_batch_size": {
+          "name": "cleanup_batch_size",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 10000
+        },
+        "enable_client_version_check": {
+          "name": "enable_client_version_check",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "verbose_provider_error": {
+          "name": "verbose_provider_error",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "enable_http2": {
+          "name": "enable_http2",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "intercept_anthropic_warmup_requests": {
+          "name": "intercept_anthropic_warmup_requests",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": false
+        },
+        "enable_thinking_signature_rectifier": {
+          "name": "enable_thinking_signature_rectifier",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "enable_thinking_budget_rectifier": {
+          "name": "enable_thinking_budget_rectifier",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "enable_codex_session_id_completion": {
+          "name": "enable_codex_session_id_completion",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "enable_claude_metadata_user_id_injection": {
+          "name": "enable_claude_metadata_user_id_injection",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "enable_response_fixer": {
+          "name": "enable_response_fixer",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "response_fixer_config": {
+          "name": "response_fixer_config",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'{\"fixTruncatedJson\":true,\"fixSseFormat\":true,\"fixEncoding\":true,\"maxJsonDepth\":200,\"maxFixSize\":1048576}'::jsonb"
+        },
+        "quota_db_refresh_interval_seconds": {
+          "name": "quota_db_refresh_interval_seconds",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false,
+          "default": 10
+        },
+        "quota_lease_percent_5h": {
+          "name": "quota_lease_percent_5h",
+          "type": "numeric(5, 4)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0.05'"
+        },
+        "quota_lease_percent_daily": {
+          "name": "quota_lease_percent_daily",
+          "type": "numeric(5, 4)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0.05'"
+        },
+        "quota_lease_percent_weekly": {
+          "name": "quota_lease_percent_weekly",
+          "type": "numeric(5, 4)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0.05'"
+        },
+        "quota_lease_percent_monthly": {
+          "name": "quota_lease_percent_monthly",
+          "type": "numeric(5, 4)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'0.05'"
+        },
+        "quota_lease_cap_usd": {
+          "name": "quota_lease_cap_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {},
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.users": {
+      "name": "users",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "name": {
+          "name": "name",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "description": {
+          "name": "description",
+          "type": "text",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "role": {
+          "name": "role",
+          "type": "varchar",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'user'"
+        },
+        "rpm_limit": {
+          "name": "rpm_limit",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_limit_usd": {
+          "name": "daily_limit_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "provider_group": {
+          "name": "provider_group",
+          "type": "varchar(200)",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'default'"
+        },
+        "tags": {
+          "name": "tags",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'[]'::jsonb"
+        },
+        "limit_5h_usd": {
+          "name": "limit_5h_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_weekly_usd": {
+          "name": "limit_weekly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_monthly_usd": {
+          "name": "limit_monthly_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_total_usd": {
+          "name": "limit_total_usd",
+          "type": "numeric(10, 2)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "limit_concurrent_sessions": {
+          "name": "limit_concurrent_sessions",
+          "type": "integer",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "daily_reset_mode": {
+          "name": "daily_reset_mode",
+          "type": "daily_reset_mode",
+          "typeSchema": "public",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'fixed'"
+        },
+        "daily_reset_time": {
+          "name": "daily_reset_time",
+          "type": "varchar(5)",
+          "primaryKey": false,
+          "notNull": true,
+          "default": "'00:00'"
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "expires_at": {
+          "name": "expires_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "allowed_clients": {
+          "name": "allowed_clients",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'[]'::jsonb"
+        },
+        "allowed_models": {
+          "name": "allowed_models",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "'[]'::jsonb"
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "deleted_at": {
+          "name": "deleted_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        }
+      },
+      "indexes": {
+        "idx_users_active_role_sort": {
+          "name": "idx_users_active_role_sort",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "role",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "id",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"users\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_users_enabled_expires_at": {
+          "name": "idx_users_enabled_expires_at",
+          "columns": [
+            {
+              "expression": "is_enabled",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            },
+            {
+              "expression": "expires_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "where": "\"users\".\"deleted_at\" IS NULL",
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_users_created_at": {
+          "name": "idx_users_created_at",
+          "columns": [
+            {
+              "expression": "created_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        },
+        "idx_users_deleted_at": {
+          "name": "idx_users_deleted_at",
+          "columns": [
+            {
+              "expression": "deleted_at",
+              "isExpression": false,
+              "asc": true,
+              "nulls": "last"
+            }
+          ],
+          "isUnique": false,
+          "concurrently": false,
+          "method": "btree",
+          "with": {}
+        }
+      },
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    },
+    "public.webhook_targets": {
+      "name": "webhook_targets",
+      "schema": "",
+      "columns": {
+        "id": {
+          "name": "id",
+          "type": "serial",
+          "primaryKey": true,
+          "notNull": true
+        },
+        "name": {
+          "name": "name",
+          "type": "varchar(100)",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "provider_type": {
+          "name": "provider_type",
+          "type": "webhook_provider_type",
+          "typeSchema": "public",
+          "primaryKey": false,
+          "notNull": true
+        },
+        "webhook_url": {
+          "name": "webhook_url",
+          "type": "varchar(1024)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "telegram_bot_token": {
+          "name": "telegram_bot_token",
+          "type": "varchar(256)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "telegram_chat_id": {
+          "name": "telegram_chat_id",
+          "type": "varchar(64)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "dingtalk_secret": {
+          "name": "dingtalk_secret",
+          "type": "varchar(256)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "custom_template": {
+          "name": "custom_template",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "custom_headers": {
+          "name": "custom_headers",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "proxy_url": {
+          "name": "proxy_url",
+          "type": "varchar(512)",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "proxy_fallback_to_direct": {
+          "name": "proxy_fallback_to_direct",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": false,
+          "default": false
+        },
+        "is_enabled": {
+          "name": "is_enabled",
+          "type": "boolean",
+          "primaryKey": false,
+          "notNull": true,
+          "default": true
+        },
+        "last_test_at": {
+          "name": "last_test_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "last_test_result": {
+          "name": "last_test_result",
+          "type": "jsonb",
+          "primaryKey": false,
+          "notNull": false
+        },
+        "created_at": {
+          "name": "created_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        },
+        "updated_at": {
+          "name": "updated_at",
+          "type": "timestamp with time zone",
+          "primaryKey": false,
+          "notNull": false,
+          "default": "now()"
+        }
+      },
+      "indexes": {},
+      "foreignKeys": {},
+      "compositePrimaryKeys": {},
+      "uniqueConstraints": {},
+      "policies": {},
+      "checkConstraints": {},
+      "isRLSEnabled": false
+    }
+  },
+  "enums": {
+    "public.daily_reset_mode": {
+      "name": "daily_reset_mode",
+      "schema": "public",
+      "values": [
+        "fixed",
+        "rolling"
+      ]
+    },
+    "public.notification_type": {
+      "name": "notification_type",
+      "schema": "public",
+      "values": [
+        "circuit_breaker",
+        "daily_leaderboard",
+        "cost_alert"
+      ]
+    },
+    "public.webhook_provider_type": {
+      "name": "webhook_provider_type",
+      "schema": "public",
+      "values": [
+        "wechat",
+        "feishu",
+        "dingtalk",
+        "telegram",
+        "custom"
+      ]
+    }
+  },
+  "schemas": {},
+  "sequences": {},
+  "roles": {},
+  "policies": {},
+  "views": {},
+  "_meta": {
+    "columns": {},
+    "schemas": {},
+    "tables": {}
+  }
+}

+ 7 - 0
drizzle/meta/_journal.json

@@ -456,6 +456,13 @@
       "when": 1770598056381,
       "tag": "0064_harsh_dragon_lord",
       "breakpoints": true
+    },
+    {
+      "idx": 65,
+      "version": "7",
+      "when": 1770607089556,
+      "tag": "0065_stale_vertigo",
+      "breakpoints": true
     }
   ]
 }

+ 46 - 2
src/actions/provider-endpoints.ts

@@ -8,6 +8,7 @@ import {
   resetEndpointCircuit as resetEndpointCircuitState,
 } from "@/lib/endpoint-circuit-breaker";
 import { logger } from "@/lib/logger";
+import { PROVIDER_ENDPOINT_CONFLICT_CODE } from "@/lib/provider-endpoint-error-codes";
 import { probeProviderEndpointAndRecord } from "@/lib/provider-endpoints/probe";
 import { ERROR_CODES } from "@/lib/utils/error-messages";
 import { extractZodErrorCode, formatZodError } from "@/lib/utils/zod-i18n";
@@ -130,6 +131,41 @@ async function getAdminSession() {
   return session;
 }
 
+function isDirectEndpointEditConflictError(error: unknown): boolean {
+  if (!error || typeof error !== "object") {
+    return false;
+  }
+
+  const candidate = error as {
+    code?: string;
+    message?: string;
+    cause?: { code?: string; message?: string };
+  };
+
+  if (candidate.code === PROVIDER_ENDPOINT_CONFLICT_CODE || candidate.code === "23505") {
+    return true;
+  }
+
+  if (typeof candidate.message === "string") {
+    if (candidate.message.includes("[ProviderEndpointEdit] endpoint conflict")) {
+      return true;
+    }
+
+    if (candidate.message.includes("duplicate key value")) {
+      return true;
+    }
+  }
+
+  if (candidate.cause?.code === "23505") {
+    return true;
+  }
+
+  return (
+    typeof candidate.cause?.message === "string" &&
+    candidate.cause.message.includes("duplicate key value")
+  );
+}
+
 export async function getProviderVendors(): Promise<ProviderVendor[]> {
   try {
     const session = await getAdminSession();
@@ -289,8 +325,16 @@ export async function editProviderEndpoint(
     return { ok: true, data: { endpoint } };
   } catch (error) {
     logger.error("editProviderEndpoint:error", error);
-    const message = error instanceof Error ? error.message : "更新端点失败";
-    return { ok: false, error: message, errorCode: ERROR_CODES.UPDATE_FAILED };
+
+    if (isDirectEndpointEditConflictError(error)) {
+      return {
+        ok: false,
+        error: "端点 URL 与同供应商类型下的其他端点冲突",
+        errorCode: ERROR_CODES.CONFLICT,
+      };
+    }
+
+    return { ok: false, error: "更新端点失败", errorCode: ERROR_CODES.UPDATE_FAILED };
   }
 }
 

+ 1 - 1
src/app/[locale]/dashboard/logs/_hooks/use-lazy-filter-options.ts

@@ -77,7 +77,7 @@ function createLazyFilterHook<T>(
 
       inFlightRef.current = promise;
       return promise;
-    }, [isLoaded]);
+    }, [fetcher, isLoaded]);
 
     const onOpenChange = useCallback(
       (open: boolean) => {

+ 8 - 3
src/app/[locale]/settings/providers/_components/forms/provider-form/index.tsx

@@ -368,13 +368,18 @@ function ProviderFormContent({
             return;
           }
 
-          queryClient.invalidateQueries({ queryKey: ["provider-vendors"] });
-          queryClient.invalidateQueries({ queryKey: ["provider-endpoints"] });
+          void queryClient.invalidateQueries({ queryKey: ["provider-vendors"] });
+          void queryClient.invalidateQueries({ queryKey: ["provider-endpoints"] });
 
           toast.success(t("success.created"));
           dispatch({ type: "RESET_FORM" });
         }
-        onSuccess?.();
+
+        try {
+          onSuccess?.();
+        } catch (e) {
+          console.error("onSuccess callback failed", e);
+        }
       } catch (e) {
         console.error("Form submission error:", e);
         toast.error(isEdit ? t("errors.updateFailed") : t("errors.addFailed"));

+ 26 - 19
src/app/[locale]/settings/providers/_components/provider-endpoints-table.tsx

@@ -421,21 +421,27 @@ export function AddEndpointButton({
         toast.success(t("endpointAddSuccess"));
         setOpen(false);
         // Invalidate both specific and general queries
-        queryClient.invalidateQueries({ queryKey: ["provider-endpoints", vendorId] });
+        // Explicitly suppress rejections to avoid double toast
+        queryClient
+          .invalidateQueries({ queryKey: ["provider-endpoints", vendorId] })
+          .catch(() => undefined);
         if (fixedProviderType) {
-          queryClient.invalidateQueries({
-            queryKey: ["provider-endpoints", vendorId, fixedProviderType, queryKeySuffix].filter(
-              (value) => value != null
-            ),
-          });
+          queryClient
+            .invalidateQueries({
+              queryKey: ["provider-endpoints", vendorId, fixedProviderType, queryKeySuffix].filter(
+                (value) => value != null
+              ),
+            })
+            .catch(() => undefined);
         }
-      } else {
-        toast.error(
-          res.errorCode
-            ? getErrorMessage(tErrors, res.errorCode, res.errorParams)
-            : t("endpointAddFailed")
-        );
+        return;
       }
+
+      toast.error(
+        res.errorCode
+          ? getErrorMessage(tErrors, res.errorCode, res.errorParams)
+          : t("endpointAddFailed")
+      );
     } catch (_err) {
       toast.error(t("endpointAddFailed"));
     } finally {
@@ -591,14 +597,15 @@ function EditEndpointDialog({ endpoint }: { endpoint: ProviderEndpoint }) {
       if (res.ok) {
         toast.success(t("endpointUpdateSuccess"));
         setOpen(false);
-        queryClient.invalidateQueries({ queryKey: ["provider-endpoints"] });
-      } else {
-        toast.error(
-          res.errorCode
-            ? getErrorMessage(tErrors, res.errorCode, res.errorParams)
-            : t("endpointUpdateFailed")
-        );
+        queryClient.invalidateQueries({ queryKey: ["provider-endpoints"] }).catch(() => undefined);
+        return;
       }
+
+      toast.error(
+        res.errorCode
+          ? getErrorMessage(tErrors, res.errorCode, res.errorParams)
+          : t("endpointUpdateFailed")
+      );
     } catch (_err) {
       toast.error(t("endpointUpdateFailed"));
     } finally {

+ 1 - 1
src/drizzle/schema.ts

@@ -340,7 +340,7 @@ export const providerEndpoints = pgTable('provider_endpoints', {
     table.vendorId,
     table.providerType,
     table.url
-  ),
+  ).where(sql`${table.deletedAt} IS NULL`),
   providerEndpointsVendorTypeIdx: index('idx_provider_endpoints_vendor_type').on(
     table.vendorId,
     table.providerType

+ 3 - 0
src/lib/provider-endpoint-error-codes.ts

@@ -0,0 +1,3 @@
+export const PROVIDER_ENDPOINT_CONFLICT_CODE = "PROVIDER_ENDPOINT_CONFLICT";
+export const PROVIDER_ENDPOINT_WRITE_READ_INCONSISTENCY_CODE =
+  "PROVIDER_ENDPOINT_WRITE_READ_INCONSISTENCY";

+ 1 - 1
src/lib/security/api-key-vacuum-filter.ts

@@ -1,6 +1,6 @@
 import { logger } from "@/lib/logger";
-import { VacuumFilter } from "@/lib/vacuum-filter/vacuum-filter";
 import { randomBytes } from "@/lib/vacuum-filter/random";
+import { VacuumFilter } from "@/lib/vacuum-filter/vacuum-filter";
 
 type ApiKeyVacuumFilterStats = {
   enabled: boolean;

+ 5 - 0
src/repository/error-rules.ts

@@ -917,6 +917,11 @@ export async function syncDefaultErrorRules(): Promise<{
             matchType: rule.matchType,
             category: rule.category,
             description: rule.description,
+            overrideResponse: rule.overrideResponse ?? null,
+            overrideStatusCode:
+              ("overrideStatusCode" in rule
+                ? (rule as { overrideStatusCode?: number | null }).overrideStatusCode
+                : null) ?? null,
             isEnabled: rule.isEnabled,
             isDefault: true,
             priority: rule.priority,

+ 579 - 91
src/repository/provider-endpoints.ts

@@ -1,6 +1,6 @@
 "use server";
 
-import { and, asc, desc, eq, gt, isNotNull, isNull, ne, or, sql } from "drizzle-orm";
+import { and, asc, desc, eq, gt, inArray, isNotNull, isNull, ne, or, sql } from "drizzle-orm";
 import { db } from "@/drizzle/db";
 import {
   providerEndpointProbeLogs,
@@ -10,6 +10,10 @@ import {
 } from "@/drizzle/schema";
 import { resetEndpointCircuit } from "@/lib/endpoint-circuit-breaker";
 import { logger } from "@/lib/logger";
+import {
+  PROVIDER_ENDPOINT_CONFLICT_CODE,
+  PROVIDER_ENDPOINT_WRITE_READ_INCONSISTENCY_CODE,
+} from "@/lib/provider-endpoint-error-codes";
 import type {
   ProviderEndpoint,
   ProviderEndpointProbeLog,
@@ -24,6 +28,13 @@ type QueryExecutor = Pick<
   "select" | "insert" | "update" | "delete" | "execute"
 >;
 
+const providerEndpointsConflictTarget = [
+  providerEndpoints.vendorId,
+  providerEndpoints.providerType,
+  providerEndpoints.url,
+];
+const providerEndpointsConflictWhere = sql`${providerEndpoints.deletedAt} IS NULL`;
+
 function isUniqueViolationError(error: unknown): boolean {
   if (!error || typeof error !== "object") {
     return false;
@@ -195,6 +206,94 @@ function toProviderEndpoint(row: any): ProviderEndpoint {
   };
 }
 
+const providerEndpointSelectFields = {
+  id: providerEndpoints.id,
+  vendorId: providerEndpoints.vendorId,
+  providerType: providerEndpoints.providerType,
+  url: providerEndpoints.url,
+  label: providerEndpoints.label,
+  sortOrder: providerEndpoints.sortOrder,
+  isEnabled: providerEndpoints.isEnabled,
+  lastProbedAt: providerEndpoints.lastProbedAt,
+  lastProbeOk: providerEndpoints.lastProbeOk,
+  lastProbeStatusCode: providerEndpoints.lastProbeStatusCode,
+  lastProbeLatencyMs: providerEndpoints.lastProbeLatencyMs,
+  lastProbeErrorType: providerEndpoints.lastProbeErrorType,
+  lastProbeErrorMessage: providerEndpoints.lastProbeErrorMessage,
+  createdAt: providerEndpoints.createdAt,
+  updatedAt: providerEndpoints.updatedAt,
+  deletedAt: providerEndpoints.deletedAt,
+};
+
+type EditableEndpointFields = Pick<ProviderEndpoint, "url" | "label" | "sortOrder" | "isEnabled">;
+
+function pickEditableFieldExpectations(payload: {
+  url?: string;
+  label?: string | null;
+  sortOrder?: number;
+  isEnabled?: boolean;
+}): Partial<EditableEndpointFields> {
+  const expected: Partial<EditableEndpointFields> = {};
+
+  if (payload.url !== undefined) {
+    expected.url = payload.url.trim();
+  }
+  if (payload.label !== undefined) {
+    expected.label = payload.label;
+  }
+  if (payload.sortOrder !== undefined) {
+    expected.sortOrder = payload.sortOrder;
+  }
+  if (payload.isEnabled !== undefined) {
+    expected.isEnabled = payload.isEnabled;
+  }
+
+  return expected;
+}
+
+function matchesEditableFieldExpectations(
+  endpoint: ProviderEndpoint,
+  expected: Partial<EditableEndpointFields>
+): boolean {
+  if (expected.url !== undefined && endpoint.url !== expected.url) {
+    return false;
+  }
+  if (expected.label !== undefined && endpoint.label !== expected.label) {
+    return false;
+  }
+  if (expected.sortOrder !== undefined && endpoint.sortOrder !== expected.sortOrder) {
+    return false;
+  }
+  if (expected.isEnabled !== undefined && endpoint.isEnabled !== expected.isEnabled) {
+    return false;
+  }
+  return true;
+}
+
+async function findActiveProviderEndpointById(
+  endpointId: number
+): Promise<ProviderEndpoint | null> {
+  const rows = await db
+    .select(providerEndpointSelectFields)
+    .from(providerEndpoints)
+    .where(and(eq(providerEndpoints.id, endpointId), isNull(providerEndpoints.deletedAt)))
+    .limit(1);
+
+  return rows[0] ? toProviderEndpoint(rows[0]) : null;
+}
+
+async function readConsistentProviderEndpointAfterWrite(input: {
+  endpointId: number;
+  expected: Partial<EditableEndpointFields>;
+}): Promise<ProviderEndpoint | null> {
+  const current = await findActiveProviderEndpointById(input.endpointId);
+  if (!current) {
+    return null;
+  }
+
+  return matchesEditableFieldExpectations(current, input.expected) ? current : null;
+}
+
 // eslint-disable-next-line @typescript-eslint/no-explicit-any
 function toProviderEndpointProbeLog(row: any): ProviderEndpointProbeLog {
   return {
@@ -790,7 +889,8 @@ export async function ensureProviderEndpointExistsForUrl(
       updatedAt: now,
     })
     .onConflictDoNothing({
-      target: [providerEndpoints.vendorId, providerEndpoints.providerType, providerEndpoints.url],
+      target: providerEndpointsConflictTarget,
+      where: providerEndpointsConflictWhere,
     })
     .returning({ id: providerEndpoints.id });
 
@@ -918,11 +1018,8 @@ export async function syncProviderEndpointOnProviderEdit(
             updatedAt: now,
           })
           .onConflictDoNothing({
-            target: [
-              providerEndpoints.vendorId,
-              providerEndpoints.providerType,
-              providerEndpoints.url,
-            ],
+            target: providerEndpointsConflictTarget,
+            where: providerEndpointsConflictWhere,
           })
           .returning({ id: providerEndpoints.id });
 
@@ -996,6 +1093,16 @@ export async function syncProviderEndpointOnProviderEdit(
       return "noop";
     };
 
+    const mapEnsureResultToKeptAction = (
+      ensureResult: "created-next" | "revived-next" | "noop"
+    ): ProviderEndpointSyncAction => {
+      return ensureResult === "created-next"
+        ? "kept-previous-and-created-next"
+        : ensureResult === "revived-next"
+          ? "kept-previous-and-revived-next"
+          : "kept-previous-and-kept-next";
+    };
+
     const previousKeyEqualsNextKey =
       previousVendorId === input.vendorId &&
       previousProviderType === input.providerType &&
@@ -1077,6 +1184,12 @@ export async function syncProviderEndpointOnProviderEdit(
 
         const ensureResult = await ensureNextEndpointActive();
 
+        if (keepPreviousWhenReferenced) {
+          return {
+            action: mapEnsureResultToKeptAction(ensureResult),
+          };
+        }
+
         await tx
           .update(providerEndpoints)
           .set({
@@ -1098,12 +1211,7 @@ export async function syncProviderEndpointOnProviderEdit(
 
       const ensureResult = await ensureNextEndpointActive();
       return {
-        action:
-          ensureResult === "created-next"
-            ? "kept-previous-and-created-next"
-            : ensureResult === "revived-next"
-              ? "kept-previous-and-revived-next"
-              : "kept-previous-and-kept-next",
+        action: mapEnsureResultToKeptAction(ensureResult),
       };
     }
 
@@ -1119,6 +1227,12 @@ export async function syncProviderEndpointOnProviderEdit(
         keepPreviousWhenReferenced && (await hasActiveReferencesOnPreviousUrl());
 
       if (!previousIsReferenced) {
+        if (keepPreviousWhenReferenced) {
+          return {
+            action: mapEnsureResultToKeptAction(ensureResult),
+          };
+        }
+
         await tx
           .update(providerEndpoints)
           .set({
@@ -1164,35 +1278,151 @@ export async function syncProviderEndpointOnProviderEdit(
   return result;
 }
 
-export async function backfillProviderEndpointsFromProviders(): Promise<{
+type BackfillProviderEndpointCandidate = {
+  key: string;
+  providerId: number;
+  vendorId: number;
+  providerType: ProviderType;
+  url: string;
+};
+
+type BackfillProviderEndpointRisk =
+  | "deterministic-safe-insert"
+  | "historical-ambiguous-report-only"
+  | "invalid-provider-row";
+
+type BackfillProviderEndpointReason =
+  | "missing-active-endpoint"
+  | "historical-soft-deleted-endpoint-present"
+  | "active-provider-vendor-id-invalid"
+  | "active-provider-url-empty"
+  | "active-provider-url-invalid";
+
+export interface BackfillProviderEndpointSample {
+  providerId: number | null;
+  vendorId: number | null;
+  providerType: ProviderType | null;
+  url: string;
+  key: string | null;
+  risk: BackfillProviderEndpointRisk;
+  reason: BackfillProviderEndpointReason;
+}
+
+export interface BackfillProviderEndpointsFromProvidersOptions {
+  mode?: "dry-run" | "apply";
+  sampleLimit?: number;
+  vendorIds?: number[];
+}
+
+export interface BackfillProviderEndpointsFromProvidersSummary {
   inserted: number;
   uniqueCandidates: number;
   skippedInvalid: number;
-}> {
+}
+
+export interface BackfillProviderEndpointsFromProvidersReport
+  extends BackfillProviderEndpointsFromProvidersSummary {
+  mode: "dry-run" | "apply";
+  repaired: number;
+  scannedProviders: number;
+  missingActiveEndpoints: number;
+  deterministicCandidates: number;
+  reportOnlyHistoricalCandidates: number;
+  reportOnlyTotal: number;
+  sampleLimit: number;
+  riskSummary: {
+    deterministicSafeInsert: number;
+    reportOnlyHistoricalAmbiguous: number;
+    reportOnlyInvalidProvider: number;
+  };
+  samples: {
+    deterministic: BackfillProviderEndpointSample[];
+    reportOnlyHistorical: BackfillProviderEndpointSample[];
+    invalid: BackfillProviderEndpointSample[];
+  };
+}
+
+function toProviderEndpointCandidateKey(input: {
+  vendorId: number;
+  providerType: ProviderType;
+  url: string;
+}): string {
+  return `${input.vendorId}|${input.providerType}|${input.url}`;
+}
+
+function clampSampleLimit(value: number | undefined): number {
+  if (!Number.isFinite(value) || (value ?? 0) <= 0) {
+    return 20;
+  }
+  return Math.min(Math.trunc(value ?? 20), 2000);
+}
+
+function sanitizeBackfillVendorScope(vendorIds: number[] | undefined): number[] {
+  if (!vendorIds || vendorIds.length === 0) {
+    return [];
+  }
+
+  const unique = new Set<number>();
+  for (const vendorId of vendorIds) {
+    if (Number.isInteger(vendorId) && vendorId > 0) {
+      unique.add(vendorId);
+    }
+  }
+
+  return [...unique].sort((left, right) => left - right);
+}
+
+function pushBackfillSample(
+  samples: BackfillProviderEndpointSample[],
+  sample: BackfillProviderEndpointSample,
+  sampleLimit: number
+): void {
+  if (samples.length < sampleLimit) {
+    samples.push(sample);
+  }
+}
+
+function compareBackfillCandidates(
+  left: BackfillProviderEndpointCandidate,
+  right: BackfillProviderEndpointCandidate
+): number {
+  if (left.vendorId !== right.vendorId) {
+    return left.vendorId - right.vendorId;
+  }
+  if (left.providerType !== right.providerType) {
+    return left.providerType.localeCompare(right.providerType);
+  }
+  return left.url.localeCompare(right.url);
+}
+
+export async function backfillProviderEndpointsFromProviders(): Promise<BackfillProviderEndpointsFromProvidersSummary>;
+export async function backfillProviderEndpointsFromProviders(
+  options: BackfillProviderEndpointsFromProvidersOptions
+): Promise<BackfillProviderEndpointsFromProvidersReport>;
+export async function backfillProviderEndpointsFromProviders(
+  options?: BackfillProviderEndpointsFromProvidersOptions
+): Promise<
+  BackfillProviderEndpointsFromProvidersSummary | BackfillProviderEndpointsFromProvidersReport
+> {
+  const mode = options?.mode ?? "apply";
   const pageSize = 1000;
   const insertBatchSize = 500;
+  const sampleLimit = clampSampleLimit(options?.sampleLimit);
+  const scopedVendorIds = sanitizeBackfillVendorScope(options?.vendorIds);
 
   let lastProviderId = 0;
+  let scannedProviders = 0;
   let skippedInvalid = 0;
-  let insertedTotal = 0;
-  const seen = new Set<string>();
-  const pending: Array<{ vendorId: number; providerType: ProviderType; url: string }> = [];
 
-  const flush = async (): Promise<void> => {
-    if (pending.length === 0) return;
-    const now = new Date();
-    const inserted = await db
-      .insert(providerEndpoints)
-      .values(pending.map((value) => ({ ...value, updatedAt: now })))
-      .onConflictDoNothing({
-        target: [providerEndpoints.vendorId, providerEndpoints.providerType, providerEndpoints.url],
-      })
-      .returning({ id: providerEndpoints.id });
-    insertedTotal += inserted.length;
-    pending.length = 0;
-  };
+  const candidatesByKey = new Map<string, BackfillProviderEndpointCandidate>();
+  const invalidSamples: BackfillProviderEndpointSample[] = [];
 
   while (true) {
+    const whereClauses = [isNull(providers.deletedAt), gt(providers.id, lastProviderId)];
+    if (scopedVendorIds.length > 0) {
+      whereClauses.push(inArray(providers.providerVendorId, scopedVendorIds));
+    }
+
     const rows = await db
       .select({
         id: providers.id,
@@ -1201,7 +1431,7 @@ export async function backfillProviderEndpointsFromProviders(): Promise<{
         url: providers.url,
       })
       .from(providers)
-      .where(and(isNull(providers.deletedAt), gt(providers.id, lastProviderId)))
+      .where(and(...whereClauses))
       .orderBy(asc(providers.id))
       .limit(pageSize);
 
@@ -1211,15 +1441,42 @@ export async function backfillProviderEndpointsFromProviders(): Promise<{
 
     for (const row of rows) {
       lastProviderId = Math.max(lastProviderId, row.id);
+      scannedProviders += 1;
 
       if (!row.vendorId || row.vendorId <= 0) {
-        skippedInvalid++;
+        skippedInvalid += 1;
+        pushBackfillSample(
+          invalidSamples,
+          {
+            providerId: row.id,
+            vendorId: row.vendorId ?? null,
+            providerType: row.providerType,
+            url: row.url,
+            key: null,
+            risk: "invalid-provider-row",
+            reason: "active-provider-vendor-id-invalid",
+          },
+          sampleLimit
+        );
         continue;
       }
 
       const trimmedUrl = row.url.trim();
       if (!trimmedUrl) {
-        skippedInvalid++;
+        skippedInvalid += 1;
+        pushBackfillSample(
+          invalidSamples,
+          {
+            providerId: row.id,
+            vendorId: row.vendorId,
+            providerType: row.providerType,
+            url: row.url,
+            key: null,
+            risk: "invalid-provider-row",
+            reason: "active-provider-url-empty",
+          },
+          sampleLimit
+        );
         continue;
       }
 
@@ -1227,89 +1484,320 @@ export async function backfillProviderEndpointsFromProviders(): Promise<{
         // eslint-disable-next-line no-new
         new URL(trimmedUrl);
       } catch {
-        skippedInvalid++;
+        skippedInvalid += 1;
+        pushBackfillSample(
+          invalidSamples,
+          {
+            providerId: row.id,
+            vendorId: row.vendorId,
+            providerType: row.providerType,
+            url: trimmedUrl,
+            key: null,
+            risk: "invalid-provider-row",
+            reason: "active-provider-url-invalid",
+          },
+          sampleLimit
+        );
         continue;
       }
 
-      const key = `${row.vendorId}|${row.providerType}|${trimmedUrl}`;
-      if (seen.has(key)) {
+      const key = toProviderEndpointCandidateKey({
+        vendorId: row.vendorId,
+        providerType: row.providerType,
+        url: trimmedUrl,
+      });
+
+      if (candidatesByKey.has(key)) {
         continue;
       }
-      seen.add(key);
-      pending.push({ vendorId: row.vendorId, providerType: row.providerType, url: trimmedUrl });
+
+      candidatesByKey.set(key, {
+        key,
+        providerId: row.id,
+        vendorId: row.vendorId,
+        providerType: row.providerType,
+        url: trimmedUrl,
+      });
+    }
+  }
+
+  const candidateKeys = new Set(candidatesByKey.keys());
+  const activeEndpointKeys = new Set<string>();
+
+  if (candidateKeys.size > 0) {
+    let lastEndpointId = 0;
+    while (true) {
+      const whereClauses = [
+        isNull(providerEndpoints.deletedAt),
+        gt(providerEndpoints.id, lastEndpointId),
+      ];
+      if (scopedVendorIds.length > 0) {
+        whereClauses.push(inArray(providerEndpoints.vendorId, scopedVendorIds));
+      }
+
+      const rows = await db
+        .select({
+          id: providerEndpoints.id,
+          vendorId: providerEndpoints.vendorId,
+          providerType: providerEndpoints.providerType,
+          url: providerEndpoints.url,
+        })
+        .from(providerEndpoints)
+        .where(and(...whereClauses))
+        .orderBy(asc(providerEndpoints.id))
+        .limit(pageSize);
+
+      if (rows.length === 0) {
+        break;
+      }
+
+      for (const row of rows) {
+        lastEndpointId = Math.max(lastEndpointId, row.id);
+        const key = toProviderEndpointCandidateKey({
+          vendorId: row.vendorId,
+          providerType: row.providerType,
+          url: row.url.trim(),
+        });
+        if (candidateKeys.has(key)) {
+          activeEndpointKeys.add(key);
+        }
+      }
+
+      if (activeEndpointKeys.size === candidateKeys.size) {
+        break;
+      }
+    }
+  }
+
+  const missingCandidates = [...candidatesByKey.values()]
+    .filter((candidate) => !activeEndpointKeys.has(candidate.key))
+    .sort(compareBackfillCandidates);
+
+  const missingCandidateKeys = new Set(missingCandidates.map((candidate) => candidate.key));
+  const historicalSoftDeletedKeys = new Set<string>();
+
+  if (missingCandidateKeys.size > 0) {
+    let lastEndpointId = 0;
+    while (true) {
+      const whereClauses = [
+        isNotNull(providerEndpoints.deletedAt),
+        gt(providerEndpoints.id, lastEndpointId),
+      ];
+      if (scopedVendorIds.length > 0) {
+        whereClauses.push(inArray(providerEndpoints.vendorId, scopedVendorIds));
+      }
+
+      const rows = await db
+        .select({
+          id: providerEndpoints.id,
+          vendorId: providerEndpoints.vendorId,
+          providerType: providerEndpoints.providerType,
+          url: providerEndpoints.url,
+        })
+        .from(providerEndpoints)
+        .where(and(...whereClauses))
+        .orderBy(asc(providerEndpoints.id))
+        .limit(pageSize);
+
+      if (rows.length === 0) {
+        break;
+      }
+
+      for (const row of rows) {
+        lastEndpointId = Math.max(lastEndpointId, row.id);
+        const key = toProviderEndpointCandidateKey({
+          vendorId: row.vendorId,
+          providerType: row.providerType,
+          url: row.url.trim(),
+        });
+        if (missingCandidateKeys.has(key)) {
+          historicalSoftDeletedKeys.add(key);
+        }
+      }
+
+      if (historicalSoftDeletedKeys.size === missingCandidateKeys.size) {
+        break;
+      }
+    }
+  }
+
+  const deterministicSamples: BackfillProviderEndpointSample[] = [];
+  const reportOnlyHistoricalSamples: BackfillProviderEndpointSample[] = [];
+  const deterministicCandidates: BackfillProviderEndpointCandidate[] = [];
+  let reportOnlyHistoricalCandidates = 0;
+
+  for (const candidate of missingCandidates) {
+    if (historicalSoftDeletedKeys.has(candidate.key)) {
+      reportOnlyHistoricalCandidates += 1;
+      pushBackfillSample(
+        reportOnlyHistoricalSamples,
+        {
+          providerId: candidate.providerId,
+          vendorId: candidate.vendorId,
+          providerType: candidate.providerType,
+          url: candidate.url,
+          key: candidate.key,
+          risk: "historical-ambiguous-report-only",
+          reason: "historical-soft-deleted-endpoint-present",
+        },
+        sampleLimit
+      );
+      continue;
+    }
+
+    deterministicCandidates.push(candidate);
+    pushBackfillSample(
+      deterministicSamples,
+      {
+        providerId: candidate.providerId,
+        vendorId: candidate.vendorId,
+        providerType: candidate.providerType,
+        url: candidate.url,
+        key: candidate.key,
+        risk: "deterministic-safe-insert",
+        reason: "missing-active-endpoint",
+      },
+      sampleLimit
+    );
+  }
+
+  let repaired = 0;
+  if (mode === "apply" && deterministicCandidates.length > 0) {
+    const pending: Array<{ vendorId: number; providerType: ProviderType; url: string }> = [];
+    const flush = async (): Promise<void> => {
+      if (pending.length === 0) {
+        return;
+      }
+
+      const now = new Date();
+      const inserted = await db
+        .insert(providerEndpoints)
+        .values(pending.map((value) => ({ ...value, updatedAt: now })))
+        .onConflictDoNothing({
+          target: providerEndpointsConflictTarget,
+          where: providerEndpointsConflictWhere,
+        })
+        .returning({ id: providerEndpoints.id });
+      repaired += inserted.length;
+      pending.length = 0;
+    };
+
+    for (const candidate of deterministicCandidates) {
+      pending.push({
+        vendorId: candidate.vendorId,
+        providerType: candidate.providerType,
+        url: candidate.url,
+      });
 
       if (pending.length >= insertBatchSize) {
         await flush();
       }
     }
+
+    await flush();
+  }
+
+  const report: BackfillProviderEndpointsFromProvidersReport = {
+    mode,
+    inserted: repaired,
+    repaired,
+    uniqueCandidates: candidatesByKey.size,
+    skippedInvalid,
+    scannedProviders,
+    missingActiveEndpoints: missingCandidates.length,
+    deterministicCandidates: deterministicCandidates.length,
+    reportOnlyHistoricalCandidates,
+    reportOnlyTotal: reportOnlyHistoricalCandidates + skippedInvalid,
+    sampleLimit,
+    riskSummary: {
+      deterministicSafeInsert: deterministicCandidates.length,
+      reportOnlyHistoricalAmbiguous: reportOnlyHistoricalCandidates,
+      reportOnlyInvalidProvider: skippedInvalid,
+    },
+    samples: {
+      deterministic: deterministicSamples,
+      reportOnlyHistorical: reportOnlyHistoricalSamples,
+      invalid: invalidSamples,
+    },
+  };
+
+  if (options === undefined) {
+    return {
+      inserted: report.inserted,
+      uniqueCandidates: report.uniqueCandidates,
+      skippedInvalid: report.skippedInvalid,
+    };
   }
 
-  await flush();
-  return { inserted: insertedTotal, uniqueCandidates: seen.size, skippedInvalid };
+  return report;
 }
 
 export async function updateProviderEndpoint(
   endpointId: number,
   payload: { url?: string; label?: string | null; sortOrder?: number; isEnabled?: boolean }
 ): Promise<ProviderEndpoint | null> {
-  if (Object.keys(payload).length === 0) {
-    const existing = await db
-      .select({
-        id: providerEndpoints.id,
-        vendorId: providerEndpoints.vendorId,
-        providerType: providerEndpoints.providerType,
-        url: providerEndpoints.url,
-        label: providerEndpoints.label,
-        sortOrder: providerEndpoints.sortOrder,
-        isEnabled: providerEndpoints.isEnabled,
-        lastProbedAt: providerEndpoints.lastProbedAt,
-        lastProbeOk: providerEndpoints.lastProbeOk,
-        lastProbeStatusCode: providerEndpoints.lastProbeStatusCode,
-        lastProbeLatencyMs: providerEndpoints.lastProbeLatencyMs,
-        lastProbeErrorType: providerEndpoints.lastProbeErrorType,
-        lastProbeErrorMessage: providerEndpoints.lastProbeErrorMessage,
-        createdAt: providerEndpoints.createdAt,
-        updatedAt: providerEndpoints.updatedAt,
-        deletedAt: providerEndpoints.deletedAt,
-      })
-      .from(providerEndpoints)
-      .where(and(eq(providerEndpoints.id, endpointId), isNull(providerEndpoints.deletedAt)))
-      .limit(1);
+  const expectedEditableFields = pickEditableFieldExpectations(payload);
 
-    return existing[0] ? toProviderEndpoint(existing[0]) : null;
+  if (Object.keys(payload).length === 0) {
+    return findActiveProviderEndpointById(endpointId);
   }
 
   const now = new Date();
   const updates: Partial<typeof providerEndpoints.$inferInsert> = { updatedAt: now };
-  if (payload.url !== undefined) updates.url = payload.url;
-  if (payload.label !== undefined) updates.label = payload.label;
-  if (payload.sortOrder !== undefined) updates.sortOrder = payload.sortOrder;
-  if (payload.isEnabled !== undefined) updates.isEnabled = payload.isEnabled;
+  if (expectedEditableFields.url !== undefined) updates.url = expectedEditableFields.url;
+  if (expectedEditableFields.label !== undefined) updates.label = expectedEditableFields.label;
+  if (expectedEditableFields.sortOrder !== undefined)
+    updates.sortOrder = expectedEditableFields.sortOrder;
+  if (expectedEditableFields.isEnabled !== undefined)
+    updates.isEnabled = expectedEditableFields.isEnabled;
 
-  const rows = await db
-    .update(providerEndpoints)
-    .set(updates)
-    .where(and(eq(providerEndpoints.id, endpointId), isNull(providerEndpoints.deletedAt)))
-    .returning({
-      id: providerEndpoints.id,
-      vendorId: providerEndpoints.vendorId,
-      providerType: providerEndpoints.providerType,
-      url: providerEndpoints.url,
-      label: providerEndpoints.label,
-      sortOrder: providerEndpoints.sortOrder,
-      isEnabled: providerEndpoints.isEnabled,
-      lastProbedAt: providerEndpoints.lastProbedAt,
-      lastProbeOk: providerEndpoints.lastProbeOk,
-      lastProbeStatusCode: providerEndpoints.lastProbeStatusCode,
-      lastProbeLatencyMs: providerEndpoints.lastProbeLatencyMs,
-      lastProbeErrorType: providerEndpoints.lastProbeErrorType,
-      lastProbeErrorMessage: providerEndpoints.lastProbeErrorMessage,
-      createdAt: providerEndpoints.createdAt,
-      updatedAt: providerEndpoints.updatedAt,
-      deletedAt: providerEndpoints.deletedAt,
+  try {
+    const rows = await db
+      .update(providerEndpoints)
+      .set(updates)
+      .where(and(eq(providerEndpoints.id, endpointId), isNull(providerEndpoints.deletedAt)))
+      .returning(providerEndpointSelectFields);
+
+    const updated = rows[0] ? toProviderEndpoint(rows[0]) : null;
+    if (!updated) {
+      return null;
+    }
+
+    if (matchesEditableFieldExpectations(updated, expectedEditableFields)) {
+      return updated;
+    }
+
+    const consistentAfterRead = await readConsistentProviderEndpointAfterWrite({
+      endpointId,
+      expected: expectedEditableFields,
     });
 
-  return rows[0] ? toProviderEndpoint(rows[0]) : null;
+    if (consistentAfterRead) {
+      return consistentAfterRead;
+    }
+
+    throw Object.assign(new Error("[ProviderEndpointEdit] write-read consistency check failed"), {
+      code: PROVIDER_ENDPOINT_WRITE_READ_INCONSISTENCY_CODE,
+    });
+  } catch (error) {
+    if (!isUniqueViolationError(error)) {
+      throw error;
+    }
+
+    const consistentAfterConflict = await readConsistentProviderEndpointAfterWrite({
+      endpointId,
+      expected: expectedEditableFields,
+    });
+
+    if (consistentAfterConflict) {
+      return consistentAfterConflict;
+    }
+
+    throw Object.assign(new Error("[ProviderEndpointEdit] endpoint conflict"), {
+      code: PROVIDER_ENDPOINT_CONFLICT_CODE,
+      cause: error,
+    });
+  }
 }
 
 export async function softDeleteProviderEndpoint(endpointId: number): Promise<boolean> {

+ 409 - 0
tests/integration/provider-endpoint-index-and-repair.test.ts

@@ -0,0 +1,409 @@
+import { and, eq, isNull } from "drizzle-orm";
+import { describe, expect, test } from "vitest";
+import { db } from "../../src/drizzle/db";
+import { providerEndpoints, providers, providerVendors } from "../../src/drizzle/schema";
+import { backfillProviderEndpointsFromProviders } from "../../src/repository/provider-endpoints";
+
+const run = process.env.DSN ? describe : describe.skip;
+
+function makeSuffix() {
+  return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
+}
+
+async function createVendor(suffix: string): Promise<number> {
+  const [vendor] = await db
+    .insert(providerVendors)
+    .values({
+      websiteDomain: `endpoint-index-${suffix}.example.com`,
+      websiteUrl: `https://endpoint-index-${suffix}.example.com`,
+    })
+    .returning({ id: providerVendors.id });
+
+  if (!vendor) {
+    throw new Error("failed to create test vendor");
+  }
+
+  return vendor.id;
+}
+
+async function cleanupVendor(vendorId: number): Promise<void> {
+  await db.delete(providers).where(eq(providers.providerVendorId, vendorId));
+  await db.delete(providerEndpoints).where(eq(providerEndpoints.vendorId, vendorId));
+  await db.delete(providerVendors).where(eq(providerVendors.id, vendorId));
+}
+
+async function createProviderRow(input: {
+  suffix: string;
+  vendorId: number;
+  url: string;
+  providerType?: "claude";
+}): Promise<number> {
+  const [provider] = await db
+    .insert(providers)
+    .values({
+      name: `endpoint-repair-${input.suffix}`,
+      url: input.url,
+      key: `sk-endpoint-repair-${input.suffix}`,
+      providerVendorId: input.vendorId,
+      providerType: input.providerType ?? "claude",
+    })
+    .returning({ id: providers.id });
+
+  if (!provider) {
+    throw new Error("failed to create test provider");
+  }
+
+  return provider.id;
+}
+
+async function countActiveEndpoints(input: {
+  vendorId: number;
+  providerType: "claude";
+  url: string;
+}): Promise<number> {
+  const rows = await db
+    .select({ id: providerEndpoints.id })
+    .from(providerEndpoints)
+    .where(
+      and(
+        eq(providerEndpoints.vendorId, input.vendorId),
+        eq(providerEndpoints.providerType, input.providerType),
+        eq(providerEndpoints.url, input.url),
+        isNull(providerEndpoints.deletedAt)
+      )
+    );
+
+  return rows.length;
+}
+
+function unwrapUniqueConstraintError(error: unknown): {
+  code: string | null;
+  constraint: string | null;
+} {
+  const value = error as {
+    code?: string;
+    constraint_name?: string;
+    constraintName?: string;
+    cause?: {
+      code?: string;
+      constraint_name?: string;
+      constraintName?: string;
+    };
+  };
+
+  return {
+    code: value.code ?? value.cause?.code ?? null,
+    constraint:
+      value.constraint_name ??
+      value.constraintName ??
+      value.cause?.constraint_name ??
+      value.cause?.constraintName ??
+      null,
+  };
+}
+
+async function expectUniqueViolation(query: Promise<unknown>): Promise<void> {
+  try {
+    await query;
+    throw new Error("expected unique constraint violation");
+  } catch (error) {
+    const violation = unwrapUniqueConstraintError(error);
+    expect(violation.code).toBe("23505");
+    expect(violation.constraint).toBe("uniq_provider_endpoints_vendor_type_url");
+  }
+}
+
+run("provider_endpoints active-only unique index", () => {
+  test("active duplicates remain unique-guarded", async () => {
+    const suffix = makeSuffix();
+    const vendorId = await createVendor(suffix);
+    const url = `https://active-dup-${suffix}.example.com/v1/messages`;
+
+    try {
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url,
+      });
+
+      await expectUniqueViolation(
+        db.insert(providerEndpoints).values({
+          vendorId,
+          providerType: "claude",
+          url,
+        })
+      );
+    } finally {
+      await cleanupVendor(vendorId);
+    }
+  });
+
+  test("soft-deleted duplicates no longer block active inserts", async () => {
+    const suffix = makeSuffix();
+    const vendorId = await createVendor(suffix);
+    const url = `https://soft-delete-insert-${suffix}.example.com/v1/messages`;
+
+    try {
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url,
+        deletedAt: new Date(),
+        isEnabled: false,
+      });
+
+      const inserted = await db
+        .insert(providerEndpoints)
+        .values({
+          vendorId,
+          providerType: "claude",
+          url,
+        })
+        .returning({ id: providerEndpoints.id });
+
+      expect(inserted).toHaveLength(1);
+
+      const activeRows = await db
+        .select({ id: providerEndpoints.id })
+        .from(providerEndpoints)
+        .where(
+          and(
+            eq(providerEndpoints.vendorId, vendorId),
+            eq(providerEndpoints.providerType, "claude"),
+            eq(providerEndpoints.url, url),
+            isNull(providerEndpoints.deletedAt)
+          )
+        );
+
+      expect(activeRows).toHaveLength(1);
+    } finally {
+      await cleanupVendor(vendorId);
+    }
+  });
+
+  test("soft-deleted duplicates no longer block active updates", async () => {
+    const suffix = makeSuffix();
+    const vendorId = await createVendor(suffix);
+    const oldUrl = `https://update-old-${suffix}.example.com/v1/messages`;
+    const revivedUrl = `https://update-revive-${suffix}.example.com/v1/messages`;
+    const conflictUrl = `https://update-conflict-${suffix}.example.com/v1/messages`;
+
+    try {
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url: revivedUrl,
+        deletedAt: new Date(),
+        isEnabled: false,
+      });
+
+      const [activeRow] = await db
+        .insert(providerEndpoints)
+        .values({
+          vendorId,
+          providerType: "claude",
+          url: oldUrl,
+        })
+        .returning({ id: providerEndpoints.id });
+
+      expect(activeRow).toBeDefined();
+
+      const updated = await db
+        .update(providerEndpoints)
+        .set({ url: revivedUrl, updatedAt: new Date() })
+        .where(eq(providerEndpoints.id, activeRow!.id))
+        .returning({ id: providerEndpoints.id, url: providerEndpoints.url });
+
+      expect(updated).toHaveLength(1);
+      expect(updated[0]?.url).toBe(revivedUrl);
+
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url: conflictUrl,
+      });
+
+      await expectUniqueViolation(
+        db
+          .update(providerEndpoints)
+          .set({ url: conflictUrl, updatedAt: new Date() })
+          .where(eq(providerEndpoints.id, activeRow!.id))
+          .returning({ id: providerEndpoints.id })
+      );
+    } finally {
+      await cleanupVendor(vendorId);
+    }
+  });
+});
+
+run("provider_endpoints deterministic repair flow", () => {
+  test("dry-run reports deterministic and historical report-only candidates", async () => {
+    const suffix = makeSuffix();
+    const vendorId = await createVendor(suffix);
+    const deterministicUrl = `https://repair-deterministic-${suffix}.example.com/v1/messages`;
+    const historicalUrl = `https://repair-historical-${suffix}.example.com/v1/messages`;
+
+    try {
+      await createProviderRow({
+        suffix: `${suffix}-deterministic`,
+        vendorId,
+        url: deterministicUrl,
+      });
+      await createProviderRow({
+        suffix: `${suffix}-historical`,
+        vendorId,
+        url: historicalUrl,
+      });
+
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url: historicalUrl,
+        deletedAt: new Date(),
+        isEnabled: false,
+      });
+
+      const report = await backfillProviderEndpointsFromProviders({
+        mode: "dry-run",
+        vendorIds: [vendorId],
+        sampleLimit: 200,
+      });
+
+      expect(report.mode).toBe("dry-run");
+      expect(report.inserted).toBe(0);
+      expect(report.repaired).toBe(0);
+      expect(report.missingActiveEndpoints).toBe(2);
+      expect(report.deterministicCandidates).toBe(1);
+      expect(report.reportOnlyHistoricalCandidates).toBe(1);
+      expect(report.riskSummary).toEqual({
+        deterministicSafeInsert: 1,
+        reportOnlyHistoricalAmbiguous: 1,
+        reportOnlyInvalidProvider: 0,
+      });
+
+      expect(report.samples.deterministic).toEqual(
+        expect.arrayContaining([
+          expect.objectContaining({
+            vendorId,
+            url: deterministicUrl,
+            risk: "deterministic-safe-insert",
+            reason: "missing-active-endpoint",
+          }),
+        ])
+      );
+      expect(report.samples.reportOnlyHistorical).toEqual(
+        expect.arrayContaining([
+          expect.objectContaining({
+            vendorId,
+            url: historicalUrl,
+            risk: "historical-ambiguous-report-only",
+            reason: "historical-soft-deleted-endpoint-present",
+          }),
+        ])
+      );
+
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: deterministicUrl,
+        })
+      ).toBe(0);
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: historicalUrl,
+        })
+      ).toBe(0);
+    } finally {
+      await cleanupVendor(vendorId);
+    }
+  });
+
+  test("apply repairs deterministic candidates and remains idempotent", async () => {
+    const suffix = makeSuffix();
+    const vendorId = await createVendor(suffix);
+    const deterministicUrl = `https://repair-apply-${suffix}.example.com/v1/messages`;
+    const historicalUrl = `https://repair-apply-historical-${suffix}.example.com/v1/messages`;
+
+    try {
+      await createProviderRow({
+        suffix: `${suffix}-apply-deterministic`,
+        vendorId,
+        url: deterministicUrl,
+      });
+      await createProviderRow({
+        suffix: `${suffix}-apply-historical`,
+        vendorId,
+        url: historicalUrl,
+      });
+
+      await db.insert(providerEndpoints).values({
+        vendorId,
+        providerType: "claude",
+        url: historicalUrl,
+        deletedAt: new Date(),
+        isEnabled: false,
+      });
+
+      const firstApply = await backfillProviderEndpointsFromProviders({
+        mode: "apply",
+        vendorIds: [vendorId],
+        sampleLimit: 200,
+      });
+
+      expect(firstApply.mode).toBe("apply");
+      expect(firstApply.missingActiveEndpoints).toBe(2);
+      expect(firstApply.deterministicCandidates).toBe(1);
+      expect(firstApply.reportOnlyHistoricalCandidates).toBe(1);
+      expect(firstApply.repaired).toBe(1);
+      expect(firstApply.inserted).toBe(1);
+
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: deterministicUrl,
+        })
+      ).toBe(1);
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: historicalUrl,
+        })
+      ).toBe(0);
+
+      const secondApply = await backfillProviderEndpointsFromProviders({
+        mode: "apply",
+        vendorIds: [vendorId],
+        sampleLimit: 200,
+      });
+
+      expect(secondApply.mode).toBe("apply");
+      expect(secondApply.repaired).toBe(0);
+      expect(secondApply.inserted).toBe(0);
+      expect(secondApply.missingActiveEndpoints).toBe(1);
+      expect(secondApply.deterministicCandidates).toBe(0);
+      expect(secondApply.reportOnlyHistoricalCandidates).toBe(1);
+
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: deterministicUrl,
+        })
+      ).toBe(1);
+      expect(
+        await countActiveEndpoints({
+          vendorId,
+          providerType: "claude",
+          url: historicalUrl,
+        })
+      ).toBe(0);
+    } finally {
+      await cleanupVendor(vendorId);
+    }
+  });
+});

+ 84 - 0
tests/integration/provider-endpoint-regression-742.test.ts

@@ -0,0 +1,84 @@
+import { describe, expect, test } from "vitest";
+import { createProvider, deleteProvider, updateProvider } from "@/repository/provider";
+import {
+  createProviderEndpoint,
+  findProviderEndpointsByVendorAndType,
+  softDeleteProviderEndpoint,
+  tryDeleteProviderVendorIfEmpty,
+} from "@/repository/provider-endpoints";
+
+const run = process.env.DSN ? describe : describe.skip;
+
+run("Provider endpoint regression #742", () => {
+  test("sibling disappearance before fix: provider edit should keep non-target sibling endpoint visible", async () => {
+    const suffix = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
+    const oldUrl = `https://742-${suffix}.example.com/v1/messages`;
+    const siblingUrl = `https://742-${suffix}.example.com/v2/messages`;
+
+    let providerId: number | null = null;
+    let vendorId: number | null = null;
+    let providerType:
+      | "claude"
+      | "claude-auth"
+      | "codex"
+      | "gemini-cli"
+      | "gemini"
+      | "openai-compatible"
+      | null = null;
+
+    try {
+      const created = await createProvider({
+        name: `Regression 742 ${suffix}`,
+        url: oldUrl,
+        key: `sk-742-${suffix}`,
+        provider_type: "claude",
+        website_url: `https://vendor-${suffix}.example.com`,
+        favicon_url: null,
+        tpm: null,
+        rpm: null,
+        rpd: null,
+        cc: null,
+      });
+
+      providerId = created.id;
+      vendorId = created.providerVendorId;
+      providerType = created.providerType;
+
+      expect(vendorId).not.toBeNull();
+
+      await createProviderEndpoint({
+        vendorId: vendorId!,
+        providerType: providerType!,
+        url: siblingUrl,
+        label: "sibling",
+      });
+
+      const updated = await updateProvider(providerId, { url: siblingUrl });
+      expect(updated?.url).toBe(siblingUrl);
+
+      const activeEndpoints = await findProviderEndpointsByVendorAndType(vendorId!, providerType!);
+      const activeUrls = activeEndpoints.map((endpoint) => endpoint.url).sort();
+
+      expect(activeUrls).toEqual([oldUrl, siblingUrl].sort());
+    } finally {
+      if (providerId != null) {
+        await deleteProvider(providerId).catch(() => false);
+      }
+
+      if (vendorId != null && providerType != null) {
+        const activeEndpoints = await findProviderEndpointsByVendorAndType(
+          vendorId,
+          providerType
+        ).catch(() => []);
+
+        await Promise.all(
+          activeEndpoints.map((endpoint) =>
+            softDeleteProviderEndpoint(endpoint.id).catch(() => false)
+          )
+        );
+
+        await tryDeleteProviderVendorIfEmpty(vendorId).catch(() => false);
+      }
+    }
+  });
+});

+ 5 - 3
tests/integration/provider-endpoint-sync-race.test.ts

@@ -115,8 +115,8 @@ run("Provider endpoint sync on edit (integration race)", () => {
 
       expect(previousAfter).toBeDefined();
       expect(previousAfter?.url).toBe(oldUrl);
-      expect(previousAfter?.deletedAt).toBeTruthy();
-      expect(previousAfter?.isEnabled).toBe(false);
+      expect(previousAfter?.deletedAt).toBeNull();
+      expect(previousAfter?.isEnabled).toBe(true);
 
       const activeEndpoints = await findProviderEndpointsByVendorAndType(
         vendorId!,
@@ -124,9 +124,11 @@ run("Provider endpoint sync on edit (integration race)", () => {
       );
 
       const nextActive = activeEndpoints.filter((endpoint) => endpoint.url === nextUrl);
+      const previousActive = activeEndpoints.filter((endpoint) => endpoint.url === oldUrl);
       expect(nextActive).toHaveLength(1);
       expect(nextActive[0]?.isEnabled).toBe(true);
-      expect(activeEndpoints.some((endpoint) => endpoint.url === oldUrl)).toBe(false);
+      expect(previousActive).toHaveLength(1);
+      expect(previousActive[0]?.isEnabled).toBe(true);
 
       const providerAfter = await findProviderById(created.id);
       expect(providerAfter?.url).toBe(nextUrl);

+ 64 - 1
tests/unit/actions/provider-endpoints.test.ts

@@ -9,6 +9,7 @@ const publishProviderCacheInvalidationMock = vi.fn();
 const findProviderEndpointByIdMock = vi.fn();
 const softDeleteProviderEndpointMock = vi.fn();
 const tryDeleteProviderVendorIfEmptyMock = vi.fn();
+const updateProviderEndpointMock = vi.fn();
 
 vi.mock("@/lib/auth", () => ({
   getSession: getSessionMock,
@@ -60,7 +61,7 @@ vi.mock("@/repository", () => ({
   findProviderVendors: vi.fn(async () => []),
   softDeleteProviderEndpoint: softDeleteProviderEndpointMock,
   tryDeleteProviderVendorIfEmpty: tryDeleteProviderVendorIfEmptyMock,
-  updateProviderEndpoint: vi.fn(async () => null),
+  updateProviderEndpoint: updateProviderEndpointMock,
   updateProviderVendor: updateProviderVendorMock,
 }));
 
@@ -139,6 +140,68 @@ describe("provider-endpoints actions", () => {
     );
   });
 
+  it("editProviderEndpoint: conflict maps to CONFLICT errorCode", async () => {
+    getSessionMock.mockResolvedValue({ user: { role: "admin" } });
+    updateProviderEndpointMock.mockRejectedValue(
+      Object.assign(new Error("[ProviderEndpointEdit] endpoint conflict"), {
+        code: "PROVIDER_ENDPOINT_CONFLICT",
+      })
+    );
+
+    const { editProviderEndpoint } = await import("@/actions/provider-endpoints");
+    const res = await editProviderEndpoint({
+      endpointId: 42,
+      url: "https://next.example.com/v1/messages",
+    });
+
+    expect(res.ok).toBe(false);
+    expect(res.errorCode).toBe("CONFLICT");
+    expect(res.error).not.toContain("duplicate key value");
+  });
+
+  it("editProviderEndpoint: success returns ok with endpoint payload", async () => {
+    getSessionMock.mockResolvedValue({ user: { role: "admin" } });
+
+    const endpoint = {
+      id: 42,
+      vendorId: 123,
+      providerType: "claude" as const,
+      url: "https://next.example.com/v1/messages",
+      label: "primary",
+      sortOrder: 7,
+      isEnabled: false,
+      lastProbedAt: null,
+      lastProbeOk: null,
+      lastProbeStatusCode: null,
+      lastProbeLatencyMs: null,
+      lastProbeErrorType: null,
+      lastProbeErrorMessage: null,
+      createdAt: new Date("2026-01-01T00:00:00.000Z"),
+      updatedAt: new Date("2026-01-01T00:00:00.000Z"),
+      deletedAt: null,
+    };
+
+    updateProviderEndpointMock.mockResolvedValue(endpoint);
+
+    const { editProviderEndpoint } = await import("@/actions/provider-endpoints");
+    const res = await editProviderEndpoint({
+      endpointId: 42,
+      url: endpoint.url,
+      label: endpoint.label,
+      sortOrder: endpoint.sortOrder,
+      isEnabled: endpoint.isEnabled,
+    });
+
+    expect(res.ok).toBe(true);
+    expect(res.data?.endpoint).toEqual(endpoint);
+    expect(updateProviderEndpointMock).toHaveBeenCalledWith(42, {
+      url: endpoint.url,
+      label: endpoint.label,
+      sortOrder: endpoint.sortOrder,
+      isEnabled: endpoint.isEnabled,
+    });
+  });
+
   it("removeProviderVendor: deletes vendor and publishes cache invalidation", async () => {
     getSessionMock.mockResolvedValue({ user: { role: "admin" } });
 

+ 68 - 0
tests/unit/repository/provider-endpoint-742-direct-edit.test.ts

@@ -0,0 +1,68 @@
+import { describe, expect, test, vi } from "vitest";
+
+describe("provider-endpoints repository - #742 direct edit", () => {
+  test("conflict fallback: direct edit should return read-after-write consistent endpoint", async () => {
+    vi.resetModules();
+
+    const duplicateKeyError = Object.assign(
+      new Error("duplicate key value violates unique constraint"),
+      {
+        code: "23505",
+      }
+    );
+
+    const endpointRow = {
+      id: 42,
+      vendorId: 7,
+      providerType: "claude",
+      url: "https://next.example.com/v1/messages",
+      label: null,
+      sortOrder: 0,
+      isEnabled: true,
+      lastProbedAt: null,
+      lastProbeOk: null,
+      lastProbeStatusCode: null,
+      lastProbeLatencyMs: null,
+      lastProbeErrorType: null,
+      lastProbeErrorMessage: null,
+      createdAt: new Date("2026-01-01T00:00:00.000Z"),
+      updatedAt: new Date("2026-01-01T00:00:00.000Z"),
+      deletedAt: null,
+    };
+
+    const updateReturningMock = vi.fn(async () => {
+      throw duplicateKeyError;
+    });
+    const updateWhereMock = vi.fn(() => ({ returning: updateReturningMock }));
+    const updateSetMock = vi.fn(() => ({ where: updateWhereMock }));
+    const updateMock = vi.fn(() => ({ set: updateSetMock }));
+
+    const selectLimitMock = vi.fn(async () => [endpointRow]);
+    const selectWhereMock = vi.fn(() => ({ limit: selectLimitMock }));
+    const selectFromMock = vi.fn(() => ({ where: selectWhereMock }));
+    const selectMock = vi.fn(() => ({ from: selectFromMock }));
+
+    vi.doMock("@/drizzle/db", () => ({
+      db: {
+        update: updateMock,
+        select: selectMock,
+      },
+    }));
+
+    const { updateProviderEndpoint } = await import("@/repository/provider-endpoints");
+
+    await expect(
+      updateProviderEndpoint(42, {
+        url: "https://next.example.com/v1/messages",
+      })
+    ).resolves.toEqual(
+      expect.objectContaining({
+        id: 42,
+        url: "https://next.example.com/v1/messages",
+      })
+    );
+
+    expect(updateMock).toHaveBeenCalledTimes(1);
+    expect(selectMock).toHaveBeenCalledTimes(1);
+  });
+});

+ 33 - 5
tests/unit/repository/provider-endpoint-sync-helper.test.ts

@@ -221,7 +221,7 @@ describe("syncProviderEndpointOnProviderEdit", () => {
     expect(resetEndpointCircuitMock).not.toHaveBeenCalled();
   });
 
-  test("in-place move unique conflict should fallback to keep-next and soft-delete previous", async () => {
+  test("in-place move unique conflict should fallback to conservative keep-previous behavior", async () => {
     const oldUrl = "https://old.example.com/v1/messages";
     const newUrl = "https://new.example.com/v1/messages";
     const { syncProviderEndpointOnProviderEdit, updatePayloads, mocks, resetEndpointCircuitMock } =
@@ -250,17 +250,45 @@ describe("syncProviderEndpointOnProviderEdit", () => {
       keepPreviousWhenReferenced: true,
     });
 
-    expect(result).toEqual({ action: "soft-deleted-previous-and-kept-next" });
+    expect(result).toEqual({ action: "kept-previous-and-kept-next" });
     expect(mocks.insertMock).toHaveBeenCalledTimes(1);
-    expect(mocks.updateMock).toHaveBeenCalledTimes(2);
-    expect(updatePayloads[1]).toEqual(
+    expect(mocks.updateMock).toHaveBeenCalledTimes(1);
+    expect(updatePayloads[0]).toEqual(
       expect.objectContaining({
-        isEnabled: false,
+        url: newUrl,
       })
     );
     expect(resetEndpointCircuitMock).not.toHaveBeenCalled();
   });
 
+  test("when next endpoint already exists, should keep previous endpoint under conservative policy", async () => {
+    const oldUrl = "https://old.example.com/v1/messages";
+    const newUrl = "https://new.example.com/v1/messages";
+    const { syncProviderEndpointOnProviderEdit, mocks, resetEndpointCircuitMock } =
+      await arrangeSyncTest([
+        [{ id: 7, deletedAt: null, isEnabled: true }],
+        [{ id: 9, deletedAt: null, isEnabled: true }],
+        [{ id: 9, deletedAt: null, isEnabled: true }],
+        [],
+      ]);
+
+    const result = await syncProviderEndpointOnProviderEdit({
+      providerId: 1,
+      vendorId: 11,
+      providerType: "claude",
+      previousVendorId: 11,
+      previousProviderType: "claude",
+      previousUrl: oldUrl,
+      nextUrl: newUrl,
+      keepPreviousWhenReferenced: true,
+    });
+
+    expect(result).toEqual({ action: "kept-previous-and-kept-next" });
+    expect(mocks.updateMock).not.toHaveBeenCalled();
+    expect(mocks.insertMock).not.toHaveBeenCalled();
+    expect(resetEndpointCircuitMock).not.toHaveBeenCalled();
+  });
+
   test("kept-previous with concurrent noop should return kept-previous-and-kept-next", async () => {
     const oldUrl = "https://old.example.com/v1/messages";
     const newUrl = "https://new.example.com/v1/messages";

+ 6 - 0
tests/unit/repository/provider-endpoints.test.ts

@@ -96,6 +96,12 @@ describe("provider-endpoints repository", () => {
         label: null,
       })
     );
+    expect(onConflictDoNothing).toHaveBeenCalledWith(
+      expect.objectContaining({
+        target: expect.any(Array),
+        where: expect.any(Object),
+      })
+    );
   });
 
   test("ensureProviderEndpointExistsForUrl: 冲突不插入时返回 false", async () => {

+ 168 - 0
tests/unit/settings/providers/provider-endpoints-table.test.tsx

@@ -367,6 +367,102 @@ describe("ProviderEndpointsTable", () => {
 
     unmount();
   });
+
+  test("edit dialog shows ONLY success toast on success", async () => {
+    providerEndpointsActionMocks.getProviderEndpointsByVendor.mockResolvedValueOnce([
+      {
+        id: 11,
+        vendorId: 1,
+        providerType: "claude",
+        url: "https://original.example.com/v1",
+        label: null,
+        sortOrder: 0,
+        isEnabled: true,
+        lastProbedAt: null,
+        lastProbeOk: null,
+        lastProbeLatencyMs: null,
+        createdAt: "2026-01-01",
+        updatedAt: "2026-01-01",
+      },
+    ]);
+
+    const { unmount } = renderWithProviders(<ProviderEndpointsTable vendorId={1} />);
+
+    await flushTicks(6);
+
+    const editButtons = document.querySelectorAll("button");
+    const editButton = Array.from(editButtons).find((btn) => btn.querySelector("svg.lucide-pen"));
+    expect(editButton).toBeDefined();
+
+    act(() => {
+      editButton?.click();
+    });
+
+    await flushTicks(4);
+
+    const form = document.querySelector("form");
+    act(() => {
+      form?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+    });
+
+    await flushTicks(4);
+
+    expect(sonnerMocks.toast.success).toHaveBeenCalledTimes(1);
+    expect(sonnerMocks.toast.error).toHaveBeenCalledTimes(0);
+
+    unmount();
+  });
+
+  test("edit dialog shows ONLY error toast on failure", async () => {
+    providerEndpointsActionMocks.getProviderEndpointsByVendor.mockResolvedValueOnce([
+      {
+        id: 12,
+        vendorId: 1,
+        providerType: "claude",
+        url: "https://original.example.com/v1",
+        label: null,
+        sortOrder: 0,
+        isEnabled: true,
+        lastProbedAt: null,
+        lastProbeOk: null,
+        lastProbeLatencyMs: null,
+        createdAt: "2026-01-01",
+        updatedAt: "2026-01-01",
+      },
+    ]);
+
+    providerEndpointsActionMocks.editProviderEndpoint.mockResolvedValueOnce({
+      ok: false,
+      error: "Update failed",
+      errorCode: "UPDATE_FAILED",
+    } as any);
+
+    const { unmount } = renderWithProviders(<ProviderEndpointsTable vendorId={1} />);
+
+    await flushTicks(6);
+
+    const editButtons = document.querySelectorAll("button");
+    const editButton = Array.from(editButtons).find((btn) => btn.querySelector("svg.lucide-pen"));
+    expect(editButton).toBeDefined();
+
+    act(() => {
+      editButton?.click();
+    });
+
+    await flushTicks(4);
+
+    const form = document.querySelector("form");
+    act(() => {
+      form?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+    });
+
+    await flushTicks(4);
+
+    expect(sonnerMocks.toast.success).toHaveBeenCalledTimes(0);
+    expect(sonnerMocks.toast.error).toHaveBeenCalledTimes(1);
+
+    unmount();
+  });
 });
 
 describe("AddEndpointButton", () => {
@@ -492,6 +588,78 @@ describe("AddEndpointButton", () => {
 
     unmount();
   });
+
+  test("should show ONLY success toast on success", async () => {
+    const { unmount } = renderWithProviders(<AddEndpointButton vendorId={1} />);
+
+    await flushTicks(2);
+
+    const addButton = document.querySelector("button");
+    act(() => {
+      addButton?.click();
+    });
+
+    await flushTicks(2);
+
+    const urlInput = document.querySelector<HTMLInputElement>('input[name="url"]');
+    act(() => {
+      if (urlInput) {
+        urlInput.value = "https://test.example.com/v1";
+        urlInput.dispatchEvent(new Event("input", { bubbles: true }));
+      }
+    });
+
+    const form = document.querySelector("form");
+    act(() => {
+      form?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+    });
+
+    await flushTicks(4);
+
+    expect(sonnerMocks.toast.success).toHaveBeenCalledTimes(1);
+    expect(sonnerMocks.toast.error).toHaveBeenCalledTimes(0);
+
+    unmount();
+  });
+
+  test("should show ONLY error toast on failure", async () => {
+    providerEndpointsActionMocks.addProviderEndpoint.mockResolvedValueOnce({
+      ok: false,
+      error: "Some error",
+      errorCode: "CREATE_FAILED",
+    } as any);
+
+    const { unmount } = renderWithProviders(<AddEndpointButton vendorId={1} />);
+
+    await flushTicks(2);
+
+    const addButton = document.querySelector("button");
+    act(() => {
+      addButton?.click();
+    });
+
+    await flushTicks(2);
+
+    const urlInput = document.querySelector<HTMLInputElement>('input[name="url"]');
+    act(() => {
+      if (urlInput) {
+        urlInput.value = "https://test.example.com/v1";
+        urlInput.dispatchEvent(new Event("input", { bubbles: true }));
+      }
+    });
+
+    const form = document.querySelector("form");
+    act(() => {
+      form?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+    });
+
+    await flushTicks(4);
+
+    expect(sonnerMocks.toast.success).toHaveBeenCalledTimes(0);
+    expect(sonnerMocks.toast.error).toHaveBeenCalledTimes(1);
+
+    unmount();
+  });
 });
 
 describe("ProviderEndpointsSection", () => {

+ 1 - 0
vitest.integration.config.ts

@@ -23,6 +23,7 @@ export default defineConfig({
       "tests/integration/notification-bindings.test.ts",
       "tests/integration/auth.test.ts",
       "tests/integration/provider-endpoint-sync-race.test.ts",
+      "tests/integration/provider-endpoint-regression-742.test.ts",
       // 需要 DB 的 API 测试(从主配置排除,在此运行)
       "tests/api/users-actions.test.ts",
       "tests/api/providers-actions.test.ts",