# LAPORAN STAGE 0C — AUDIT KODE READ-ONLY
## Inkonsistensi `section_id` pada Content Block Section Container BAB

**Status:** READ-ONLY. Tidak ada kode, database, konfigurasi, atau data yang diubah.
Tidak ada generator atau migrasi yang dijalankan.
**Sumber data:** dump `manuputr_perpus_kitab.sql` (dibaca read-only oleh [`stage0c_audit_readonly.py`](database/stage0c_audit_readonly.py)).
**Sumber kode:** pembacaan statis file PHP (tanpa eksekusi).

---

## 0. RINGKASAN EKSEKUTIF

| Pertanyaan | Jawaban singkat |
|---|---|
| Mengapa section 65 & 68 menyimpan blok dengan `section_id = ID`? | Karena keduanya di-generate melalui **cakupan `subbab`** (bukan `bab_direct`). Pada jalur `subbab`, `section_id` selalu diisi ID section tanpa pemeriksaan `tipe_bagian`. |
| Apakah kode saat ini mencegah hal itu? | **Tidak.** Tidak ada satu pun pemeriksaan `tipe_bagian = 'bab'` di jalur tulis Content Block. |
| Apakah analisis BAB membaca blok `section_id = ID`? | **Tidak.** Analisis BAB hanya membaca `section_id IS NULL` ([`byBabDirect()`](app/Repositories/ContentBlockRepository.php:142)). |
| Apakah analisis SUBBAB membaca blok `section_id = ID`? | **Ya**, tetapi diblokir lebih awal oleh [`ensureSubbabContainer()`](app/Services/ContentCategoryService.php:589) yang menolak section `tipe_bagian='bab'`. |
| Dampak regenerate section 65 & 68? | **Blok lama tidak akan terhapus** (karena `deleteBySubbab` hanya menghapus `section_id = ID`, sedangkan regenerate via `bab_direct` menghapus `section_id IS NULL`). Hasilnya **duplikasi/tumpukan**, bukan penggantian. |

**Kesimpulan inti:** inkonsistensi ini **bukan bug integritas data**, melainkan **celah validasi pada jalur tulis** yang memungkinkan container BAB diperlakukan sebagai SUBBAB. Blok yang ada saat ini **tidak boleh dipindahkan** sebelum jalur baca dan tulis diverifikasi — dan laporan ini menunjukkan bahwa **memindahkan saja tidak cukup**: jalur tulis harus ditutup lebih dulu.

---

## 1. KODE GENERATOR & REPOSITORY PENYIMPANAN

### 1.1 Titik masuk generator

| File | Fungsi | Baris |
|---|---|---|
| [`admin/content-blocks/action.php`](admin/content-blocks/action.php:1) | Handler POST aksi generate/hapus | 1–338 |
| [`ContentBlockService::generateByScope()`](app/Services/ContentBlockService.php:408) | Orkestrator per cakupan | 408–597 |
| [`ContentBlockService::generateUnit()`](app/Services/ContentBlockService.php:619) | Eksekusi satu unit (BAB-direct atau satu SUBBAB) | 619–727 |
| [`ContentBlockService::parse()`](app/Services/ContentBlockService.php:134) | Jalur lama: parse + simpan + replace | 134–367 |
| [`ContentBlockService::createBlocks()`](app/Services/ContentBlockService.php:764) | Simpan blok hasil `parseText()` manual | 764–830 |
| [`ContentBlockRepository::createMany()`](app/Repositories/ContentBlockRepository.php:240) | INSERT batch ke `content_blocks` | 240–297 |

### 1.2 Daftar cakupan (scope) yang valid

[`ContentBlockService::SCOPES`](app/Services/ContentBlockService.php:63):

```php
public const SCOPE_BAB_DIRECT     = 'bab_direct';
public const SCOPE_ALL_BAB_DIRECT = 'all_bab_direct';
public const SCOPE_SUBBAB         = 'subbab';
public const SCOPE_ALL_SUBBAB     = 'all_subbab';
public const SCOPE_ALL_CONTENT    = 'all_content';
```

### 1.3 Bagaimana unit ditentukan — `generateByScope()`

[`generateByScope()`](app/Services/ContentBlockService.php:446) membangun daftar `$units`:

- **`bab_direct`** → [`baris 463`](app/Services/ContentBlockService.php:463):
  ```php
  $units[] = ['kind' => 'bab_direct', 'chapter_id' => $chapterId, 'section_id' => null];
  ```
- **`subbab`** → [`baris 493`](app/Services/ContentBlockService.php:493):
  ```php
  $units[] = ['kind' => 'subbab', 'chapter_id' => $chapterId, 'section_id' => $sectionId];
  ```
  Validasi di [`baris 483–491`](app/Services/ContentBlockService.php:483) **hanya** memeriksa:
  1. section ada,
  2. `section.chapter_id === chapterId`.

  **Tidak ada pemeriksaan `tipe_bagian`.** Section container BAB **lolos** validasi ini.
- **`all_subbab` / `all_content`** → [`baris 513–527`](app/Services/ContentBlockService.php:513) memakai [`subbabByChapter()`](app/Repositories/SectionRepository.php:273) yang **mengecualikan** `tipe_bagian='bab'`. Jadi jalur ini **aman**.

> **[TERBUKTI]** Hanya cakupan `subbab` (satu section terpilih) yang dapat mengirim container BAB ke generator. Cakupan `all_subbab`/`all_content` tidak.

### 1.4 Penentuan `section_id` saat menyimpan — `generateUnit()`

[`generateUnit()`](app/Services/ContentBlockService.php:636):

```php
$textRows = $sectionId !== null
    ? $this->texts->bySection($sectionId)
    : $this->collectBabDirectTexts($chapterId);
```

[`baris 673–679`](app/Services/ContentBlockService.php:673):

```php
foreach ($this->parser->parse($teksArab, $pageStart) as $block) {
    $block['book_id']    = $bookId;
    $block['chapter_id'] = $chapterId;
    // BAB langsung -> section_id NULL; SUBBAB -> ID_SUBBAB.
    $block['section_id'] = $sectionId;
    $blocks[]            = $block;
}
```

**Ini adalah akar masalah.** `$block['section_id'] = $sectionId;` bersifat **tanpa syarat**. Bila `$sectionId = 65` (container BAB), maka seluruh 575 blok disimpan dengan `section_id = 65`.

Penghapusan lama ([`baris 700–704`](app/Services/ContentBlockService.php:700)):

```php
if ($sectionId !== null) {
    $this->blocks->deleteBySubbab($sectionId);
} else {
    $this->blocks->deleteByBabDirect($chapterId);
}
```

### 1.5 Jalur lama `parse()` — di sinilah RM-4 diperbaiki

[`parse()`](app/Services/ContentBlockService.php:202) mendeteksi `$babDirectScope`:

```php
$babDirectScope = false;
if ($sectionId === null) {                       // <-- HANYA bila sectionId null
    $container = $this->sections->findBabContainer($chapterId);
    if ($container !== null) {
        $containerId = (int) ($container['id'] ?? 0);
        if ($containerId > 0) {
            foreach ($textRows as $textRow) {
                if ((int) ($textRow['section_id'] ?? 0) === $containerId) {
                    $babDirectScope = true;
                    break;
                }
            }
        }
    }
}
```

[`baris 274`](app/Services/ContentBlockService.php:274):

```php
$storedSectionId = $babDirectScope ? null : $rowSectionId;
```

> **[TERBUKTI]** Perbaikan RM-4 **hanya berlaku bila `$sectionId === null`**. Bila container dikirim sebagai `$sectionId = 65`, `$babDirectScope` tetap `false`, sehingga `$storedSectionId = 65`. **Perbaikan RM-4 tidak menjangkau jalur `subbab`.**

### 1.6 `createBlocks()` — jalur simpan manual

[`createBlocks()`](app/Services/ContentBlockService.php:777) juga hanya memvalidasi relasi section↔chapter:

```php
if ($sectionId !== null) {
    $sectionRow = $this->sections->find($sectionId);
    if ($sectionRow === null) { throw ... }
    if ((int) ($sectionRow['chapter_id'] ?? 0) !== $chapterId) { throw ... }
}
```

[`baris 818`](app/Services/ContentBlockService.php:818): `'section_id' => $sectionId,` — **tanpa syarat** juga.

### 1.7 Repository penyimpanan

| Method | Baris | Kondisi SQL |
|---|---|---|
| [`byBabDirect()`](app/Repositories/ContentBlockRepository.php:142) | 142–152 | `chapter_id = :x AND section_id IS NULL` |
| [`bySubbab()`](app/Repositories/ContentBlockRepository.php:159) | 159–162 | `all(null, $sectionId, null)` → `section_id = :x` |
| [`byBab()`](app/Repositories/ContentBlockRepository.php:129) | 129–132 | `chapter_id = :x` (semua cakupan) |
| [`deleteByBabDirect()`](app/Repositories/ContentBlockRepository.php:326) | 326–333 | `chapter_id = :x AND section_id IS NULL` |
| [`deleteBySubbab()`](app/Repositories/ContentBlockRepository.php:359) | 359–365 | `section_id = :x` |
| [`deleteByBab()`](app/Repositories/ContentBlockRepository.php:309) | 309–315 | `chapter_id = :x` (semua) |
| [`deleteByBabSubbabs()`](app/Repositories/ContentBlockRepository.php:345) | 345–352 | `chapter_id = :x AND section_id IS NOT NULL` |
| [`countBabDirect()`](app/Repositories/ContentBlockRepository.php:371) | 371–378 | `chapter_id = :x AND section_id IS NULL` |

> **[TERBUKTI]** Repository **tidak** memiliki konsep "container BAB". Ia hanya membedakan `section_id IS NULL` vs `section_id = ID`. Semantik container sepenuhnya bergantung pada pemanggil.

---

## 2. ATURAN PENENTUAN `section_id` UNTUK `tipe_bagian = 'bab'`

### 2.1 Aturan yang **seharusnya** (keputusan RM-4)

Dari [`LAPORAN_AUDIT_CONTENT_BLOCK.md:54-58`](database/LAPORAN_AUDIT_CONTENT_BLOCK.md:54):

> **Keputusan final (diselesaikan):** BAB langsung disimpan dengan `section_id = NULL`; SUBBAB disimpan dengan `section_id = ID_SUBBAB`; relasi ke BAB tetap lewat `chapter_id`.

### 2.2 Aturan yang **benar-benar diterapkan di kode**

| Jalur | Kondisi | `section_id` tersimpan | Sesuai RM-4? |
|---|---|---|---|
| `parse()` dengan `$sectionId === null` + sumber container | `$babDirectScope = true` | `NULL` | ✅ |
| `parse()` dengan `$sectionId === null` + sumber SUBBAB asli | `$babDirectScope = false` | ID SUBBAB | ✅ |
| `parse()` dengan `$sectionId = 65` (container) | `$babDirectScope` tidak pernah dihitung | **`65`** | ❌ |
| `generateUnit()` kind `bab_direct` | `$sectionId = null` | `NULL` | ✅ |
| `generateUnit()` kind `subbab` dengan section container | `$sectionId = 65` | **`65`** | ❌ |
| `createBlocks()` dengan `$sectionId = 65` | tanpa syarat | **`65`** | ❌ |

### 2.3 Pemeriksaan `tipe_bagian` yang ADA di kode (dan yang TIDAK ada)

**Ada** (jalur baca / UI / analisis):
- [`SectionRepository::subbabByChapter()`](app/Repositories/SectionRepository.php:283) — mengecualikan `tipe_bagian='bab'`.
- [`SectionRepository::findBabContainer()`](app/Repositories/SectionRepository.php:340) — mencari `tipe_bagian='bab'`.
- [`ContentCategoryService::isBabContainerSection()`](app/Services/ContentCategoryService.php:549).
- [`ContentCategoryService::ensureSubbabContainer()`](app/Services/ContentCategoryService.php:601) — **menolak** container BAB.
- [`ContentBlockService::getUnitStatus()`](app/Services/ContentBlockService.php:912) — memisahkan container sebagai unit `bab_direct`.

**Tidak ada** (jalur tulis Content Block):
- [`generateByScope()`](app/Services/ContentBlockService.php:464) cabang `SCOPE_SUBBAB` — tidak memeriksa `tipe_bagian`.
- [`generateUnit()`](app/Services/ContentBlockService.php:619) — tidak memeriksa `tipe_bagian`.
- [`parse()`](app/Services/ContentBlockService.php:134) — tidak memeriksa `tipe_bagian` saat `$sectionId !== null`.
- [`createBlocks()`](app/Services/ContentBlockService.php:764) — tidak memeriksa `tipe_bagian`.

> **[TERBUKTI]** Ada **asimetri**: lapisan baca/analisis sadar container, lapisan tulis Content Block **tidak**.

### 2.4 Bagaimana section 65 & 68 bisa masuk jalur `subbab`?

[`getScopeAvailability()`](app/Services/ContentBlockService.php:1038) membangun `subbab_total` dari `subbabByChapter()` (mengecualikan container), sehingga **dropdown UI tidak menampilkan container**. Namun:

- [`action.php`](admin/content-blocks/action.php:187) hanya memvalidasi `$sectionId !== null` — tidak memvalidasi bahwa section itu benar-benar SUBBAB.
- [`action.php:210-232`](admin/content-blocks/action.php:210) memanggil `getScopeAvailability($chapterId, $sectionId)`; untuk container, `$result['subbab']` akan `false` (karena `$selectedRows` hanya diisi bila `$rowSectionId === $sectionId` di dalam loop `subbabByChapter`, yang tidak memuat container). Artinya **request POST manual** ke container akan **ditolak** oleh guard ini.

> **[ASUMSI]** Karena guard `action.php` saat ini menolak container, blok section 65 & 68 **kemungkinan besar dibuat sebelum guard tersebut ada**, atau melalui pemanggilan service langsung (CLI/skrip/test), bukan melalui UI saat ini. Ini tidak dapat dipastikan dari dump.

---

## 3. RIWAYAT / DOKUMENTASI KEPUTUSAN RM-4

| Sumber | Baris | Isi |
|---|---|---|
| [`LAPORAN_AUDIT_CONTENT_BLOCK.md`](database/LAPORAN_AUDIT_CONTENT_BLOCK.md:43) | 43–58 | Definisi masalah RM-4 + keputusan final |
| [`LAPORAN_AUDIT_CONTENT_BLOCK.md`](database/LAPORAN_AUDIT_CONTENT_BLOCK.md:78) | 78–88 | Implementasi `$babDirectScope` di `parse()` |
| [`LAPORAN_AUDIT_CONTENT_BLOCK_DELETE.md`](database/LAPORAN_AUDIT_CONTENT_BLOCK_DELETE.md:15) | 15 | RM-4: "kadang ID wadah, kadang NULL" |
| [`LAPORAN_AUDIT_MENYELURUH...md`](database/LAPORAN_AUDIT_MENYELURUH_CONTENT_BLOCK_DAN_JALUR_ANALISIS.md:34) | 34 | Rujukan keputusan RM-4 |
| [`LAPORAN_STAGE0B_AUDIT_READONLY.md`](database/LAPORAN_STAGE0B_AUDIT_READONLY.md:134) | 134–137 | Temuan empiris 65/68 vs 63/177/178 |

**Kutipan kunci RM-4** ([`LAPORAN_AUDIT_CONTENT_BLOCK.md:44-52`](database/LAPORAN_AUDIT_CONTENT_BLOCK.md:44)):

> `ContentBlockService::parse()` menyimpan blok BAB langsung dengan `section_id` = ID WADAH BAB (bukan NULL), sementara `ContentBlockRepository::deleteByBabDirect()` hanya menghapus baris `section_id IS NULL`. Akibatnya: Hasil generate lama (tersimpan dengan `section_id` = ID wadah) TIDAK terhapus saat generate ulang, sehingga **hasil lama tertinggal dan menumpuk**.

> **[TERBUKTI]** Dokumentasi RM-4 **secara eksplisit** menyebut gejala "hasil lama tertinggal dan menumpuk" — dan itulah **persis** risiko yang masih hidup untuk section 65 & 68 (lihat §5).

**Celah dokumentasi:** RM-4 hanya membahas `parse()` dengan `$sectionId === null`. Tidak ada dokumentasi yang membahas skenario "container BAB dikirim sebagai `$sectionId`". Tidak ada catatan migrasi data untuk blok lama yang sudah terlanjur tersimpan dengan `section_id = ID container`.

---

## 4. JALUR BACA ANALISIS BAB

### 4.1 Analisis level BAB

[`AiAnalysisService::collectBabBlocks()`](app/Services/AiAnalysisService.php:319):

```php
$rows = $this->blocks->byBabDirect($babId);   // baris 326
```

[`byBabDirect()`](app/Repositories/ContentBlockRepository.php:142) → `WHERE chapter_id = :x AND section_id IS NULL`.

> **[TERBUKTI]** Analisis BAB **HANYA** membaca blok `section_id IS NULL`. Blok section 65 (575) dan 68 (38) **TIDAK terlihat** oleh analisis BAB.

Konsekuensi untuk chapter 52 dan 54 (yang hanya punya container, tanpa SUBBAB asli):

| Chapter | Container | Blok BAB-direct | Blok `section_id=ID` | Yang dibaca analisis BAB |
|---|---|---|---|---|
| 52 | 65 | **0** | 575 | **0 blok** |
| 54 | 68 | **0** | 38 | **0 blok** |

> **[TERBUKTI]** Untuk kedua chapter ini, analisis per-block (perlafadz/nahwu/shorof) **tidak akan menemukan blok sama sekali**, sehingga jatuh ke fallback teks langsung. Ini adalah **dampak fungsional nyata** dari inkonsistensi, bukan sekadar kosmetik.

### 4.2 Analisis level SUBBAB

[`ContentCategoryService::collectSubbabBlocks()`](app/Services/ContentCategoryService.php:870):

```php
$rows = $this->blocks->bySubbab($sectionId);   // baris 876
```

Namun [`ensureSubbabContainer()`](app/Services/ContentCategoryService.php:601) menolak container:

```php
if ((string) ($section['tipe_bagian'] ?? '') === 'bab') {
    log_error('Menolak membuat container analisis Subbab untuk section container BAB', [...]);
    return null;
}
```

> **[TERBUKTI]** Analisis SUBBAB **tidak dapat dijalankan** pada section 65/68 karena ditolak sebelum blok dibaca. Jadi blok 575 & 38 itu **tidak terbaca oleh jalur analisis mana pun** saat ini.

### 4.3 Ringkasan jalur baca

| Jalur baca | Method | Filter | Melihat blok 65/68? |
|---|---|---|---|
| Analisis BAB per-block | [`byBabDirect()`](app/Repositories/ContentBlockRepository.php:142) | `section_id IS NULL` | ❌ |
| Analisis SUBBAB per-block | [`bySubbab()`](app/Repositories/ContentBlockRepository.php:159) | `section_id = ID` | ⛔ diblokir `ensureSubbabContainer()` |
| Status unit UI | [`getUnitStatus()`](app/Services/ContentBlockService.php:928) | `countBabDirect()` | ❌ (dilaporkan 0) |
| Semua blok BAB | [`byBab()`](app/Repositories/ContentBlockRepository.php:129) | `chapter_id` | ✅ (tetapi tidak dipakai analisis) |

---

## 5. DAMPAK JIKA GENERATOR DIJALANKAN ULANG PADA SECTION 65 & 68

### 5.1 Skenario A — regenerate via cakupan `bab_direct` (yang benar secara RM-4)

Alur: `generateByScope('bab_direct', book, chapter)` → `generateUnit(kind='bab_direct', sectionId=null)`.

1. `$textRows = collectBabDirectTexts($chapterId)` → [`findBabContainer()`](app/Services/ContentBlockService.php:740) → teks section 65/68. **Sumber ditemukan** (65.665 dan 5.578 char).
2. Parse → blok baru dengan `section_id = null`.
3. Hapus: [`deleteByBabDirect($chapterId)`](app/Services/ContentBlockService.php:703) → `section_id IS NULL` → **0 baris terhapus** (karena blok lama punya `section_id = 65/68`).
4. Insert blok baru dengan `section_id = NULL`.

**Hasil:** 575 blok lama (`section_id=65`) **tetap ada** + 575 blok baru (`section_id=NULL`) → **total 1.150 blok**. Ini **persis** gejala "hasil lama tertinggal dan menumpuk" yang diperingatkan RM-4.

| Chapter | Sebelum | Sesudah regenerate `bab_direct` | Delta |
|---|---|---|---|
| 52 | 575 (`section_id=65`) | 575 (`section_id=65`) + ~575 (`NULL`) | **+575 duplikat** |
| 54 | 38 (`section_id=68`) | 38 (`section_id=68`) + ~38 (`NULL`) | **+38 duplikat** |

> **[TERBUKTI dari kode]** Penghapusan dan penyimpanan memakai predikat `section_id` yang **berbeda**, sehingga tidak saling mengganti.

### 5.2 Skenario B — regenerate via cakupan `subbab` dengan `sectionId = 65`

1. `$textRows = texts->bySection(65)` → sumber ditemukan.
2. Parse → blok dengan `section_id = 65`.
3. Hapus: [`deleteBySubbab(65)`](app/Services/ContentBlockService.php:701) → **575 baris terhapus**.
4. Insert ulang 575 blok `section_id = 65`.

**Hasil:** bersih, tetapi **tetap menyimpan `section_id = 65`** → inkonsistensi **tidak hilang**, hanya di-refresh. Analisis BAB tetap tidak melihatnya.

> **Catatan:** melalui UI saat ini skenario B **ditolak** oleh guard [`action.php:210-232`](admin/content-blocks/action.php:210) karena `getScopeAvailability()['subbab']` bernilai `false` untuk container.

### 5.3 Skenario C — regenerate via `all_content` / `all_subbab`

[`generateByScope()`](app/Services/ContentBlockService.php:513) memakai `subbabByChapter()` yang **mengecualikan** container. Unit `bab_direct` tetap dibuat ([`baris 509-511`](app/Services/ContentBlockService.php:509)).

**Hasil:** identik dengan Skenario A → **duplikasi**.

### 5.4 Matriks dampak

| Skenario | Blok lama terhapus? | Blok baru `section_id` | Hasil akhir | Analisis BAB melihat? |
|---|---|---|---|---|
| A. `bab_direct` | ❌ tidak | `NULL` | **duplikat** | hanya blok baru |
| B. `subbab` (65) | ✅ ya | `65` | bersih tapi tetap salah | ❌ |
| C. `all_content` | ❌ tidak | `NULL` | **duplikat** | hanya blok baru |
| D. Hapus dulu `hapus_semua_bab` lalu A | ✅ ya | `NULL` | bersih & benar | ✅ |

> **[TERBUKTI]** Hanya jalur **D** (hapus seluruh blok chapter, lalu generate `bab_direct`) yang menghasilkan keadaan konsisten. Namun ini **menghapus data** dan **belum boleh dijalankan** tanpa persetujuan.

### 5.5 Catatan penting: jangan pindahkan blok lebih dulu

Bila blok 575/38 dipindahkan (`UPDATE ... SET section_id = NULL`) **tanpa** menutup celah jalur tulis:

- Analisis BAB akan mulai melihat blok tersebut (perubahan perilaku pada data produksi).
- Regenerate `subbab` berikutnya akan **mengembalikan** `section_id = 65` (Skenario B), menciptakan siklus.
- Tidak ada migrasi/backfill resmi yang mendokumentasikan perubahan ini.

> **[KESIMPULAN]** Urutan yang benar adalah: **tutup jalur tulis → verifikasi jalur baca → baru putuskan migrasi data**. Memindahkan blok lebih dulu akan menutupi gejala, bukan penyebab.

---

## 6. TEMUAN TAMBAHAN DARI DATA (READ-ONLY)

Dari [`stage0c_audit_readonly.py`](database/stage0c_audit_readonly.py):

| Section | Chapter | `tipe_bagian` | SUBBAB asli di chapter | Teks (char) | Blok `section_id=ID` | Blok `NULL` |
|---|---|---|---|---|---|---|
| 63 | 51 | bab | **1** (id 64) | 474 | 0 | 3 |
| 65 | 52 | bab | **0** | 65.665 | **575** | 0 |
| 68 | 54 | bab | **0** | 5.578 | **38** | 0 |
| 177 | 49 | bab | **0** | 3.784 | 0 | 11 |
| 178 | 50 | bab | **0** | 29.845 | 0 | 117 |

**Pola yang terlihat:**
- Section 63 punya SUBBAB asli (64) → blok disimpan `NULL` (benar).
- Section 65, 68, 177, 178 **tidak** punya SUBBAB asli.
- Dari empat itu, **hanya 65 dan 68** yang menyimpan blok dengan `section_id = ID`; 177 dan 178 menyimpan `NULL` (benar).

> **[ASUMSI]** Perbedaan ini konsisten dengan hipotesis bahwa 65 & 68 dibuat lewat jalur `subbab` (mungkin sebelum guard `action.php` ada), sedangkan 177 & 178 dibuat lewat `bab_direct`.

**Kualitas blok 65 & 68:**

| Section | Blok | `sort_order` | Duplikat `sort_order` | Rasio kata blok/sumber |
|---|---|---|---|---|
| 65 | 575 (semua `paragraph`) | 1..575 | **0** | 0,99 |
| 68 | 38 (1 `heading` + 37 `paragraph`) | 1..38 | **0** | 0,98 |

> **[TERBUKTI]** Blok 65 & 68 **lengkap dan tidak duplikat** — rasio kata ≈1,0 berarti seluruh teks sumber terwakili. Jadi masalahnya **bukan** kualitas blok, melainkan **atribut `section_id`**.

---

## 7. KESIMPULAN

1. **Akar masalah (terbukti):** [`generateUnit()` baris 677](app/Services/ContentBlockService.php:677) dan [`createBlocks() baris 818`](app/Services/ContentBlockService.php:818) menetapkan `section_id = $sectionId` **tanpa syarat**, tanpa memeriksa `tipe_bagian`. Perbaikan RM-4 di [`parse() baris 202-219`](app/Services/ContentBlockService.php:202) hanya aktif bila `$sectionId === null`, sehingga **tidak menjangkau** jalur `subbab`.

2. **Tidak ada validasi container di jalur tulis (terbukti):** `generateByScope()` cabang `subbab` ([baris 483-491](app/Services/ContentBlockService.php:483)) hanya memvalidasi relasi section↔chapter.

3. **Jalur baca BAB hanya `section_id IS NULL` (terbukti):** [`byBabDirect()`](app/Repositories/ContentBlockRepository.php:142). Akibatnya chapter 52 & 54 saat ini **tidak punya blok yang terbaca analisis BAB**.

4. **Jalur baca SUBBAB diblokir untuk container (terbukti):** [`ensureSubbabContainer()`](app/Services/ContentCategoryService.php:601). Jadi blok 575 & 38 **tidak terbaca jalur mana pun**.

5. **Regenerate berisiko duplikasi (terbukti dari kode):** `bab_direct` menghapus `IS NULL` tetapi blok lama `= 65/68` → **menumpuk**, sesuai peringatan RM-4.

6. **Blok tidak boleh dipindahkan lebih dulu (rekomendasi berbasis bukti):** tanpa menutup jalur tulis, pemindahan akan menciptakan siklus dan mengubah perilaku analisis pada data produksi.

---

## 8. REKOMENDASI (BELUM DILAKSANAKAN — MENUNGGU PERSETUJUAN)

| # | Rekomendasi | Sifat | Catatan |
|---|---|---|---|
| R1 | Tambah validasi `tipe_bagian` di `generateByScope()` cabang `subbab` dan di `generateUnit()` | Perubahan kode | Menolak container sebagai unit `subbab` |
| R2 | Tambah validasi `tipe_bagian` di `createBlocks()` | Perubahan kode | Konsisten dengan R1 |
| R3 | Tambah unit test: container BAB via `subbab` harus ditolak | Test | Lihat [`test_content_block_section_id_decision.php`](database/test_content_block_section_id_decision.php) |
| R4 | Verifikasi ulang jalur baca setelah R1–R2 (analisis BAB & SUBBAB) | Verifikasi | Tanpa mengubah data |
| R5 | **Baru setelah itu** putuskan migrasi data blok 65 & 68 (pindah ke `NULL` atau hapus+regenerate) | Perubahan data | Butuh backup & persetujuan terpisah |
| R6 | Dokumentasikan skenario "container via `subbab`" di laporan RM-4 | Dokumentasi | Menutup celah dokumentasi §3 |

**Tidak ada satu pun rekomendasi di atas yang dijalankan pada tahap ini.**

---

## 9. BATASAN VERIFIKASI

1. **Dump vs database produksi** — tidak dapat dipastikan identik tanpa koneksi live.
2. **Riwayat pembuatan blok 65 & 68** — tidak ada `created_at`/`source_reference` yang membedakan jalur pembuatan; kesimpulan "dibuat via `subbab`" adalah **[ASUMSI]** yang konsisten dengan kode & data.
3. **Apakah guard `action.php` sudah ada saat blok dibuat** — tidak dapat diverifikasi dari dump.
4. **Perilaku runtime** — seluruh analisis kode bersifat statis; tidak ada eksekusi generator.

---

**BERHENTI — menunggu persetujuan sebelum melakukan perubahan apa pun.**
