# ERR-20260519-008: Prisma migrate reset 在 AI agent 上下文中被强制阻断

## 症状

在 slot DB 上执行 `prisma migrate reset --skip-seed --force` 报错：

```
Error: Prisma Migrate detected that it was invoked by Claude Code.
You are attempting a highly dangerous action that can lead to devastating consequences...
You must ask the user if they want to proceed with this action.
```

Prisma 6.x 起新增 AI agent 检测逻辑，会拒绝 reset / db push 这类销毁性命令，除非传 `PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION=<user 原话>`。

## 触发条件

- `prisma migrate reset` / `prisma db push --force-reset` 等销毁性命令
- 检测到调用者是 AI agent（Claude Code / 类似环境变量）
- 没有 `PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION`

## 解决方案

### 1. 涉及销毁数据的命令 → 必须先问用户

按 prisma 输出的协议询问用户明示同意，然后用 `PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION="<原话>"` 重跑。

### 2. 仅生成 migration 文件 → 手工写 SQL 绕过

`prisma migrate dev --create-only` 在 DB 和本地 migration 历史不一致时也会要求 reset。
如果任务只是 **创建新 migration 文件**（不需要 apply），直接手工：

1. 在 `prisma/migrations/<timestamp>_<name>/` 建目录
2. 写 `migration.sql`（按业界 prisma 输出风格：`-- AlterEnum` / `-- AlterTable` 等注释 + 真实 SQL）
3. 修改对应 `prisma/schema/*.prisma`
4. 后续真正 apply 由用户在 dev / CI 上跑 `migrate dev`

特别适合 `ALTER TYPE ... ADD VALUE` 这种 prisma 自动生成会包 BEGIN/COMMIT、必须手工拆的场景。

### 3. 排查 slot DB 漂移

slot DB 出现 "applied to the database but absent from the migrations directory" 通常是：
- 上一个 feature 分支在该 slot 跑过 `migrate dev`，但分支被丢弃 / 没 merge
- 切回 develop / 新 feature 时 DB 历史还留着旧 migration

不一定是真的需要 reset；如果新 migration 不冲突，可以走"只追加 SQL"路径。

## 关联

- `docs/standards/05-development-workflow.md` 「数据库」段强调禁止跑生产 reset
- agent pool slot DB 是开发库，理论上可 reset，但要按 prisma 协议走 explicit user consent
