# Next.js Turbopack dev `.next/` cache 损坏 → 整站 500（登录页都打不开）

**日期**：2026-05-17
**场景**：slot-3 frontend dev server 跑了一上午（07:54 启动），中途加了 my-work 增强（stage-fields.ts + TaskDetailDrawer.tsx）。用户尝试登录 https://slot-3.chentao.test.jiachentao.com/login 失败，怀疑是凭据问题。
**实际**：frontend dev server 返 500（HTTP 500，body `"Internal Server Error"`，21 字节），**所有页面**（包括 /、/login、/robot-manager/my-work）都 500，**不是登录失败**。

`/tmp/slot3-fe-mywork.log` 报错：

```
⨯ Error: Cannot find module '../chunks/ssr/[turbopack]_runtime.js'
  Require stack:
  - .next/dev/server/app/page.js
  ...
Error: ENOENT: no such file or directory,
  open '.next/dev/routes-manifest.json'
GET / 500 in 232ms (compile: 199ms, render: 33ms)
```

backend 完全健康（`/api/v1/auth/login` 测试返 `success: true` + accessToken），跟登录后端无关。

## 根因

Next.js 16 Turbopack 的增量 dev cache 在以下情况会损坏：
- dev server 跑了几个小时 + 多次代码热重载
- 新增大 component / 新 import 拓扑
- 进程信号异常（如 SIGKILL 而非 SIGTERM）
- 磁盘空间不足导致 chunk 写入中断（slot-3 磁盘 87.9% — 接近告警阈值）

`.next/dev/server/app/page.js` 编译时 require 了 `../chunks/ssr/[turbopack]_runtime.js`，但该 chunk 文件实际不存在。Turbopack 索引和真实文件系统不一致 → require 失败 → 整页 500。

routes-manifest.json 也 ENOENT 是同因（chunk 写入失败时 manifest 也未生成）。

## 修复

```bash
# 1. 杀掉当前 frontend dev
kill <pid>

# 2. 清干净 .next/ 缓存
rm -rf frontend/.next

# 3. 重启 dev server
cd frontend && PORT=<port> nohup npm run dev > /tmp/slot<N>-fe.log 2>&1 &

# 4. 等编译（首次 ~8s）
sleep 15

# 5. 验证
curl -sk -o /dev/null -w "HTTP=%{http_code}\n" http://127.0.0.1:<port>/
```

恢复后第一次 GET `/` 编译时间 ~8s（cold compile），第二次 GET 就 80ms。

## 项目相关

slot 池模式（`scripts/dev/agent-pool/`）下 frontend dev 在 slot 启动时自动跑、**跨 claim/release 不重启**——这意味着：

- slot 被某 agent 用了一阵 + release，frontend dev 可能保留旧 user 的 hot reload 状态
- 下一个 agent claim 同 slot，frontend 看似"在跑"但 cache 不一致
- 切换分支（如本次 stash + checkout）后没 reload，新代码跟旧 cache 冲突更易触发

**症状识别**：HTTP 500 + 响应 body 21 字节 `Internal Server Error`（next 默认错误页）。**先看 dev log 找 `Cannot find module` / `ENOENT.*routes-manifest` 这两个特征**，不是改业务代码引起的。

## 工程化保险建议

- 给 agent-pool slot 加一个 `agent-claim` 时的"frontend dev cache 健康检查"：探测 `/` 返 500 + log 有 turbopack 错误 → 自动 clean + restart
- 或者在 `agent-release` 时清 `.next/`，下一个 claim 时干净重启（trade-off：每次首次访问要 8s cold compile）
- 监控磁盘空间——87% 是 alerts 阈值，超 90% 会增加 cache 写入失败概率

## 类似的"dev server 看似正常实则坏掉"陷阱

- Vite HMR 后 component import 改名 → vite cache stale
- Webpack DLL 编译失败但 server 没崩
- Nest dev `dist/` 跟 ts source 不一致（很少见，nodemon 会 reload）

通用规则：**dev server 异常先看 dev log 找 cache / chunk / manifest 错误，再排查代码**。先 nuke cache，再追根因。
