KB-2744 rev 2

18b-P0 — DOT-119 Read-only Preflight Prompt (Rev 2)

9 min read Revision 2
dot-119repairp0promptread-onlyclaude-coderev2

18b-P0 — DOT-119 Repair: Read-only Preflight + Mapping Report

Controlling: 18a rev4 FINAL + 18b rev3 PASS Mode: READ-ONLY. Không DDL, không UPDATE, không INSERT, không DELETE, không script rewrite, không function edit, không IU trigger, không IU rows, không Pack 2B. Rev 2: 6 patches GPT review — STOP phải upload report, không nói DDL authorized, schema-qualified names, IU/UV registry check, full mapping = all collections, runtime date.


Mục tiêu

Khảo sát hiện trường DOT-119 + birth pipeline. Produce mapping table. Upload report lên KB. HARD STOP.


Bước 0: Đọc controlling docs

Đọc 3 file trên KB trước khi chạy bất kỳ query nào:

  1. knowledge/dev/laws/dieu44-trien-khai/design/18a-dot119-metadata-driven-no-clobber-redesign.md
  2. knowledge/dev/laws/dieu44-trien-khai/design/18b-dot119-repair-execution-pack.md
  3. knowledge/dev/laws/dieu44-trien-khai/reports/existing-birth-code-column-patterns-report.md

Xác nhận đã đọc cả 3 trước khi tiếp.


Bước 1: Baselines

1a: 3 fields chưa tồn tại?

SELECT column_name FROM information_schema.columns
WHERE table_name = 'collection_registry'
  AND column_name IN ('birth_code_strategy','birth_code_column','birth_identity_source');
  • Expected: 0 rows.
  • Nếu > 0 → STOP after uploading partial P0 report ghi rõ "3 fields already exist" + giá trị tìm được. Không tiếp nhưng vẫn upload report.

1b: fn_birth_registry_auto hash baseline

SELECT md5(pg_get_functiondef('fn_birth_registry_auto'::regproc)) AS fn_hash_baseline;

Ghi lại hash. Đây là baseline cho toàn bộ 18b.

1c: Birth trigger count

SELECT count(*) AS current_birth_triggers FROM pg_trigger
WHERE tgname LIKE 'trg_birth_%' AND NOT tgisinternal;

Ghi lại. Expected: ~30.

1d: Birth registry count

SELECT count(*) AS current_birth_records FROM birth_registry;

Ghi lại. Expected: ~75k.

1e: DOT-119 script metadata

ls -la /opt/incomex/dot/bin/dot-birth-trigger-setup
md5sum /opt/incomex/dot/bin/dot-birth-trigger-setup
wc -l /opt/incomex/dot/bin/dot-birth-trigger-setup

Ghi lại: file size, modification date, md5, line count.


Bước 2: Script inspection

grep -c 'CREATE OR REPLACE FUNCTION fn_birth_registry_auto' \
  /opt/incomex/dot/bin/dot-birth-trigger-setup
  • Expected: > 0 → confirms v1 stale (chứa function body, sẽ clobber v2 nếu chạy).
  • Nếu 0 → STOP after uploading partial P0 report. Script đã sửa ngoài scope. Report + investigate. Vẫn upload report trước khi dừng.

SELECT code, name, status, operation, file_path
FROM dot_tools
WHERE code ILIKE '%SCHEMA%'
   OR code ILIKE '%MIGR%'
   OR code ILIKE '%ALTER%'
   OR name ILIKE '%schema%'
   OR name ILIKE '%migration%'
   OR name ILIKE '%alter%'
ORDER BY code;
  • Nếu tìm tool relevant cho ALTER TABLE → ghi vào report, flag schema_migration_tool_found = yes. STOP chờ GPT/User decision.
  • Nếu không → ghi "No schema/migration tool found. Raw DDL may be candidate for P1, pending GPT/User review."

★ P0 KHÔNG authorize DDL. P0 chỉ report finding. GPT/User quyết ở P1.


Bước 4: Birth trigger listing + mapping table

4a: List tất cả birth triggers

SELECT
  tgrelid::regclass AS table_name,
  tgname AS trigger_name,
  tgfoid::regproc AS function_name,
  pg_get_triggerdef(oid) AS trigger_def
FROM pg_trigger
WHERE tgname LIKE 'trg_birth_%' AND NOT tgisinternal
ORDER BY table_name;

★ Lưu ý: tgrelid::regclass có thể trả schema-qualified name (ví dụ public.foo). Agent phải tách schema/table name. Khi verify column (4c), dùng table_schema = 'public' AND table_name = '<unqualified_name>'. Nếu schema khác public, report schema ambiguity, KHÔNG tự mark high confidence.

4b: List tất cả collections với governance_role

SELECT collection_name, governance_role
FROM collection_registry
ORDER BY collection_name;

4b-extra: Verify IU/UV registry rows (★ Rev 2)

SELECT collection_name, governance_role
FROM collection_registry
WHERE collection_name IN ('information_unit','unit_version');
  • Expected: 2 rows (cả 2 đã đăng ký từ Pack 2A).
  • Nếu thiếu 1 hoặc 2 → report blocker: manual classification không thể seed nếu collection chưa đăng ký.

4c: Verify trigger args match actual columns

Cho mỗi trigger có arg (từ 4a), verify column tồn tại trên table:

-- Pattern (agent lặp cho mỗi trigger):
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = '<unqualified_table_from_trigger>'
  AND column_name = '<arg_from_trigger_def>';

4d: Produce mapping table

★ Full mapping table = one row per collection_registry.collection_name, không chỉ trigger tables. Trigger info filled when present, NULL when absent.

Parse trigger_def → extract TG_ARGV[0] → map theo rules:

Pattern → strategy → birth_code_column → source confidence
fn_birth_registry_auto('code') column code inferred_from_existing_trigger high
fn_birth_registry_auto('<X>') (X ≠ 'code') column <X> (exact arg) inferred_from_existing_trigger high
fn_birth_registry_auto_id() legacy_id_single_colon NULL inferred_from_existing_trigger high
No trigger, governance_role = 'excluded' disabled NULL system_default high
No trigger, governance_role ∈ (governed, observed, locked) unclassified NULL unclassified high

Confidence downgrade:

  • trigger_arg parse fail → low
  • column in arg does NOT exist on table (4c verify fail) → medium
  • schema ambiguity (non-public schema) → medium, report

Manual rows (add explicitly):

collection_name strategy column source confidence provenance
information_unit synthetic_id NULL manual high 18a rev4 §3. No code column. Synthetic shell information_unit::<uuid> per birth-code patterns report. GPT/User decision.
unit_version subordinate NULL manual high 18a rev4 §3. Child of information_unit, no independent birth. GPT/User decision.

Output full table (one row per collection):

| collection_name | governance_role | trigger_name | function_name | trigger_arg | proposed_strategy | proposed_birth_code_column | proposed_source | confidence | notes/provenance |

Bước 5: Distribution preview

Count per proposed_strategy từ mapping table:

| strategy | count |
|---|---|
| column | ? |
| synthetic_id | ? |
| legacy_id_single_colon | ? |
| subordinate | ? |
| disabled | ? |
| unclassified | ? |
| TOTAL | ? |

Bước 6: Decision flags

Print rõ ràng:

schema_migration_tool_found: yes/no
any_confidence_not_high: yes/no (nếu yes → list rows)
three_fields_already_present: yes/no
dot119_stale_v1_confirmed: yes/no
fn_hash_baseline_captured: yes/no
fn_hash_value: <hash>
trigger_count: <N>
birth_registry_count: <N>
script_md5: <hash>
script_lines: <N>
iu_uv_registered: yes/no (nếu no → blocker)

Bước 7: Upload P0 report

Upload report lên KB qua Agent Data:

Path: knowledge/dev/laws/dieu44-trien-khai/reports/dot119-repair-p0-readonly-mapping-report.md

Title: DOT-119 Repair — P0 Read-only Mapping Report

Tags: ['dot-119', 'repair', 'p0', 'mapping', 'read-only', 'preflight']

Report structure:

# DOT-119 Repair — P0 Read-only Mapping Report

> Date: <actual runtime date> | Phase: P0 (read-only)
> Controlling: 18a rev4 + 18b rev3

## §1. Baselines
(fn hash, trigger count, birth count, script md5/size/lines)

## §2. Script Inspection
(v1 stale confirmed: yes/no, grep count)

## §3. Schema/Migration Tool Search
(query result, conclusion — finding only, no authorization)

## §4. IU/UV Registry Check
(both registered? blocker if not)

## §5. Full Mapping Table
(one row per collection_registry.collection_name)

## §6. Manual Classifications + Provenance
(information_unit, unit_version)

## §7. Distribution Preview
(count per strategy)

## §8. Confidence Summary
(all high? any medium/low? list if any)

## §9. Decision Flags
(all flags including iu_uv_registered)

## §10. STOP Conditions Checked
(list all, none triggered / which triggered)

★ Bất kỳ STOP condition nào triggered vẫn PHẢI upload partial report trước khi dừng.


Bước 8: HARD STOP

Sau upload report → DỪNG.

  • Không DDL.
  • Không UPDATE/INSERT/DELETE.
  • Không DOT-119 execution.
  • Không script rewrite.
  • Không function edit.
  • Không IU trigger.
  • Không IU rows.
  • Không Pack 2B.
  • P0 report = findings only. KHÔNG authorize bất kỳ mutation nào.

Chờ GPT/User review P0 report trước khi soạn P1.


18b-P0 prompt rev 2 | 2026-05-05 | Opus 4.6 | 6 GPT patches: STOP upload report, no DDL authorization, schema-qualified, IU/UV check, full mapping all collections, runtime date. Read-only. Zero mutation.