# ERR-20260509-005: verify-integrity 跨 suite flaky—cn/default 域 ms tie-break 破链

## 现象

CI 全量集成测试（36 suites / 449 tests / ~393s）偶发失败一个：

```
backend/integration/audit/integrity.integration.test.ts
  ● [AUDIT-VI-001] verify-integrity 返回 verified=true（默认 cn/default 域）
    Expected: true / Received: false
```

单跑该文件、跑 audit 三个 suite 串跑均通过，只在和其它 33 个 suite 一起跑时挂。

## 根因（两层叠加）

1. **`audit_log.when` 是 `timestamptz(3)`（ms 精度）**
   `verifyIntegrity` 用 `orderBy: { when: 'asc' }` 单字段排序，同毫秒多条记录 PostgreSQL 不保证 tie-break 顺序。verify 拿到的顺序若与写入顺序不同，`previousHash` 链就断 → `verified=false` + `HASH_CHAIN_BROKEN`。

2. **`cleanupAllData` 完全不清 `audit_log` 表**
   `testing/backend/helpers/cleanup.helper.ts` 的全量清理路径（不传 sessionId）只 wipe organization/performance 等模块，`audit_log` 跨 suite 累积上百条到 cn/default 域，ms 碰撞概率随测试数量线性放大。

写入侧是单进程串行 `await getLatestLog → create`，顺序确定；读取侧排序键不足以恢复写入顺序，是单边问题。

## 复现/确认成本

- 单跑：稳过（数据量小）
- audit 全 suite：稳过（21 条记录够稀疏）
- 全量 36 suites：偶现破链

## 修法

测试侧隔离（已落）：把 `[AUDIT-VI-001]` 改成用 `uniqueTenantId()` sandbox + 直接调 `AuditService.verifyIntegrity`，不打 cn/default 共享域。HTTP 路径覆盖由 `[AUDIT-VI-002]` (POST /integrity-check) 承担。

未做但应该做（产品代码侧，单独 PR 评估生产影响）：
- `verifyIntegrity` 的 `orderBy` 加二级排序键，或干脆按 hash 链拓扑遍历（GENESIS → prev → curr）。当前 `previousHash` 字段已构成显式拓扑，比时间排序更可靠
- `getLatestLog` 大小写敏感 vs `verifyIntegrity` `mode: 'insensitive'` 不一致——seed 层已防御（PR #247），运行时仍有隐患

## 教训

**`@db.Timestamptz(3)` 不是排序稳定键**。任何依赖时间顺序重建状态的逻辑（hash chain、event sourcing、CDC replay）都不能假设 ms 精度排序在 ties 上稳定。要么加二级键（自增列 / `created_at` 微秒 / `id` 比较），要么按显式拓扑字段（`previousHash`、`parentId`）走。

**测试 isolation 优于跨 suite 共享状态**：cn/default 是生产语义上的"默认域"，但在测试里它就是无法保证清理的共享黑板。新写测试如果要走 audit_log，默认用 `uniqueTenantId()` sandbox。

## 文件锚点

- 测试：[testing/backend/integration/audit/integrity.integration.test.ts](../../testing/backend/integration/audit/integrity.integration.test.ts)
- verify 排序：[backend/src/core/observability/audit/audit.service.ts:1020](../../backend/src/core/observability/audit/audit.service.ts#L1020)
- when 类型：[backend/prisma/schema/platform_audit.prisma:11](../../backend/prisma/schema/platform_audit.prisma#L11)
- cleanup 漏 audit_log：[testing/backend/helpers/cleanup.helper.ts:807](../../testing/backend/helpers/cleanup.helper.ts#L807)
