# Rule 1 Chunk 入库检查报告

> Date: 2026-04-28 20:00
> File UUID: 384b0ff44aaaa1f14cb2cd63b3fea966

---

## 流程概述

### Rule 1 执行流程

```
execute_rule1 (rule1_ingest.rs)
  ↓
1. fetch_asr_segments()     → pre_chunks (ASR) ✅
2. fetch_asrx_segments()    → pre_chunks (ASRX) ❌ (empty)
3. fetch_yolo_frames()      → pre_chunks (YOLO) ❌ (empty)
4. fetch_face_frames()      → pre_chunks (Face) ❌ (empty)
  ↓
for each ASR segment:
  - find_best_speaker()     → speaker_id = "UNKNOWN"
  - find_yolo_objects()     → yolo_objects = []
  - find_face_ids()         → face_ids = []
  ↓
store_chunk_in_tx() → chunks 表 ✅
```

---

## 数据库状态

### pre_chunks 表

| processor_type | Count | Status |
|---------------|-------|--------|
| **ASR** | 3664 | ✅ Normal |
| **CUT** | 1332 | ✅ Normal |
| **ASRX** | 0 | ❌ Missing |
| **YOLO** | 0 | ❌ Missing |
| **Face** | 0 | ❌ Missing |

### chunks 表

| Field | Value | Issue |
|-------|-------|-------|
| **uuid** | 384b0ff44aaaa1f14cb2cd63b3fea966 | ✅ file_uuid |
| **file_id** | 29 | ✅ videos.id |
| **chunk_type** | sentence | ✅ Correct |
| **content** | `{"data": {"text": "..."}, "rule": "rule_1"}` | ✅ Correct |
| **metadata** | `{"chunk_identity": {"faces": [], "speakers": []}}` | ❌ Missing speaker_id/face_ids |

### face_detections 表

| file_uuid | Count | Status |
|-----------|-------|--------|
| 384b0ff44aaaa1f14cb2cd63b3fea966 | ? | ✅ Exists |

---

## 问题根源

### 1. ASRX 数据未写入 pre_chunks

**位置**: `src/worker/processor.rs:773-802`

```rust
pub async fn store_asrx_chunks(
    db: &PostgresDb,
    uuid: &str,
    asrx_result: &AsrxResult,
) -> Result<()> {
    // ... 
    db.store_raw_pre_chunks_batch(uuid, "asrx", &pre_chunks_to_store).await?;
}
```

**问题**: 
- processing_status 显示 `"ASRX": {"chunks_produced": 0}`
- 说明 `store_asrx_chunks` 没有成功执行或数据为空

### 2. Face 数据存储位置错误

**位置**: `src/worker/processor.rs:710-740`

```rust
pub async fn store_face_chunks(...) {
    // Face data stored in face_detections / face_clusters tables
}
```

**问题**: 
- Face 处理器将数据写入 `face_detections` 和 `face_clusters` 表
- `rule1_ingest.rs:fetch_face_frames()` 从 `pre_chunks` 读取
- 数据源不匹配

### 3. YOLO 数据未写入 pre_chunks

**问题**: 
- processing_status 显示 `"YOLO": {"chunks_produced": 0}`
- YOLO 数据可能存储在其他位置或未成功写入

---

## 影响

### Chunk Metadata 缺失

```json
// Expected (rule1_ingest.rs)
{
  "speaker_id": "SPEAKER_0",
  "yolo_objects": ["person", "car"],
  "face_ids": ["Person_176"],
  "language": "en"
}

// Actual (chunks table)
{
  "chunk_identity": {
    "faces": [],
    "speakers": []
  }
}
```

### 功能影响

1. **Speaker 识别**: 无法知道 chunk 属于哪个 speaker
2. **Face 关联**: 无法将 chunk 与人物关联
3. **YOLO Objects**: 无法知道 chunk 中出现的物体
4. **Identity 绑定**: 无法实现 Face → Identity → Chunk 链路

---

## 解决方案

### 方案 A: 修复 pre_chunks 写入（推荐）

1. **修复 ASRX 写入**
   - 检查 `store_asrx_chunks` 执行时机
   - 确保 ASRX 处理器完成后调用
   - 验证 `store_raw_pre_chunks_batch` 正常工作

2. **修复 YOLO 写入**
   - 添加 `store_yolo_chunks` 方法
   - 将 YOLO detections 写入 pre_chunks

3. **修改 Face 数据源**
   - Face 数据保持写入 `face_detections` / `face_clusters`
   - `rule1_ingest.rs` 改为从 `face_detections` 读取

### 方案 B: 直接读取 JSON 文件

修改 `rule1_ingest.rs`:
- `fetch_asrx_segments()` → 读取 `*.asrx.json`
- `fetch_face_frames()` → 读取 `*.face.json` 或查询 `face_detections`
- `fetch_yolo_frames()` → 读取 `*.yolo.json`

---

## 建议修复顺序

| Priority | Task | File |
|----------|------|------|
| 1 | 检查 ASRX processor 执行 | `src/worker/processor.rs` |
| 2 | 验证 store_raw_pre_chunks_batch | `src/core/db/postgres_db.rs:1867` |
| 3 | 修改 fetch_face_frames 数据源 | `src/core/chunk/rule1_ingest.rs:269-316` |
| 4 | 添加 YOLO 写入 pre_chunks | `src/worker/processor.rs` |
| 5 | 重新运行 rule1 处理 | - |

---

## 验证命令

```bash
# 检查 pre_chunks 数据
psql -U accusys -d momentry -c "
SELECT DISTINCT processor_type, COUNT(*) 
FROM dev.pre_chunks 
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966' 
GROUP BY processor_type;
"

# 检查 face_detections
psql -U accusys -d momentry -c "
SELECT COUNT(*) FROM dev.face_detections WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';
"

# 检查 chunk metadata
psql -U accusys -d momentry -c "
SELECT chunk_id, metadata FROM dev.chunks 
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966' AND chunk_type = 'sentence' 
LIMIT 5;
"
```

---

## 相关文件

- `src/core/chunk/rule1_ingest.rs` - Rule 1 入库逻辑
- `src/worker/processor.rs` - 处理器执行
- `src/core/db/postgres_db.rs:1867` - store_raw_pre_chunks_batch
- `migrations/017_create_pre_chunks.sql` - pre_chunks 表结构
