KB-42A6

IU-0 Pack 1 — Execution Prompt cho Claude Code

9 min read Revision 1
iu-0pack-1execution-promptclaude-codeddlbirth-gates191

IU-0 Pack 1 — Execution Prompt cho Claude Code

Trạng thái: PROMPT — chưa dispatch, chờ User approve C10 (mở P44-6) Agent: Claude Code (claude-go) — SSH vào VPS contabo SSOT: knowledge/dev/laws/dieu44-trien-khai/design/09a-iu0-pack1-vocab-schema-ddl-design.md rev 3 DB: docker exec postgres psql -U directus -d directus (KHÔNG -U postgres)


Lời mở đầu cho Agent

Chạy đúng Pack 1 từ 09a rev 3. Đây là IU-0 implementation đầu tiên — tạo 2 tables + birth gate cho information_unit. Mọi SQL đã draft sẵn trong 09a. Bạn execute tuần tự, verify mỗi step, rollback nếu fail.


Hard boundaries

# CẤM
HB-1 KHÔNG sửa tables hiện có (tac_*, dot_config schema, etc.)
HB-2 KHÔNG đụng production_documents (Qdrant)
HB-3 KHÔNG tạo Qdrant collection mới
HB-4 KHÔNG tạo outbox table/worker (Pack 3)
HB-5 KHÔNG sửa 07/07b/07c/P38-XC content
HB-6 KHÔNG tạo extension (pgcrypto etc.) nếu chưa APR — báo và dừng
HB-7 Vi phạm bất kỳ HB → STOP ngay, rollback, báo cáo

Preflight (Step 0)

Trước khi chạy DDL, verify tất cả:

# 0a. SSH vào VPS
ssh contabo

# 0b. PG accessible
docker exec postgres psql -U directus -d directus -c "SELECT version();"

# 0c. gen_random_uuid() available
docker exec postgres psql -U directus -d directus -c "SELECT gen_random_uuid();"
# Nếu fail → STOP. Báo cáo. Không tự CREATE EXTENSION.

# 0d. dot_config table exists + config_key UNIQUE
docker exec postgres psql -U directus -d directus -c "\d dot_config"
# Verify: config_key column exists, UNIQUE constraint active

# 0e. No conflict vocab keys
docker exec postgres psql -U directus -d directus -c "
  SELECT config_key FROM dot_config
  WHERE config_key LIKE 'vocab.unit_kind.%'
     OR config_key LIKE 'vocab.section_type.%'
     OR config_key LIKE 'vocab.publication_type.%';
"
# Expected: no rows with our 3 keys. Nếu conflict → STOP.

# 0f. Tables don't exist yet
docker exec postgres psql -U directus -d directus -c "\dt information_unit"
docker exec postgres psql -U directus -d directus -c "\dt unit_version"
# Expected: "Did not find any relation". Nếu tồn tại → STOP.

# 0g. PG backup snapshot trước DDL
docker exec postgres pg_dump -U directus -d directus --schema-only > /opt/incomex/backups/pre-pack1-schema-$(date +%Y%m%d-%H%M%S).sql
docker exec postgres pg_dump -U directus -d directus --data-only -t dot_config > /opt/incomex/backups/pre-pack1-dot-config-$(date +%Y%m%d-%H%M%S).sql

Preflight PASS = tất cả 0a-0g OK. Nếu bất kỳ fail → STOP, báo cáo, không tiếp.


Execute (Step 1-6)

Chạy tuần tự. Mỗi step verify trước step tiếp.

Step 1: Vocab INSERT

docker exec postgres psql -U directus -d directus -c "
INSERT INTO dot_config (config_key, config_value, description, created_at)
VALUES
  ('vocab.unit_kind.design_doc_section', 'design_doc_section', 'Section trong design document — proposed_pilot', NOW()),
  ('vocab.section_type.section', 'section', 'Generic section type cho design docs — proposed_pilot', NOW()),
  ('vocab.publication_type.design_doc', 'design_doc', 'Design document publication type — proposed_pilot', NOW())
ON CONFLICT (config_key) DO NOTHING;
"

Verify Step 1:

docker exec postgres psql -U directus -d directus -c "
  SELECT config_key, config_value FROM dot_config
  WHERE config_key IN (
    'vocab.unit_kind.design_doc_section',
    'vocab.section_type.section',
    'vocab.publication_type.design_doc'
  );
"
# Expected: 3 rows

Step 2: CREATE information_unit

Copy DDL từ 09a §2.1. Execute.

Verify Step 2:

docker exec postgres psql -U directus -d directus -c "\d information_unit"
# Expected: table with all columns
docker exec postgres psql -U directus -d directus -c "\di" | grep idx_iu
# Expected: 5 indexes

Step 3: CREATE unit_version + deferred FK

Copy DDL từ 09a §2.2. Execute.

Verify Step 3:

docker exec postgres psql -U directus -d directus -c "\d unit_version"
docker exec postgres psql -U directus -d directus -c "
  SELECT conname, condeferrable, condeferred
  FROM pg_constraint WHERE conname = 'fk_iu_version_anchor';
"
# Expected: condeferrable=true, condeferred=true

Step 4: Birth gate Layer 1 trigger

Copy DDL từ 09a §3.1. Execute.

Verify Step 4:

docker exec postgres psql -U directus -d directus -c "
  SELECT tgname FROM pg_trigger WHERE tgname = 'trg_iu_birth_gate_layer1';
"
# Expected: 1 row

Step 5: Birth gate Layer 2 constraint trigger

Copy DDL từ 09a §3.2. Execute.

Verify Step 5:

docker exec postgres psql -U directus -d directus -c "
  SELECT tgname, tgdeferrable, tginitdeferred
  FROM pg_trigger WHERE tgname = 'trg_iu_birth_gate_layer2';
"
# Expected: tgdeferrable=true, tginitdeferred=true

Step 6: Smoke tests T1-T14

Chạy tất cả 14 tests từ 09a §4. Mỗi test ghi PASS/FAIL.

Critical tests (must PASS):

# T1: Missing title → L1 block
docker exec postgres psql -U directus -d directus -c "
  INSERT INTO information_unit (canonical_address, unit_kind, owner_ref, created_by, updated_by, identity_profile)
  VALUES ('test.t1', 'design_doc_section', 'test', 'test', 'test', '{}');
"
# Expected: ERROR Birth gate L1: P-id1 title required

# T12: Unregistered unit_kind → L1 block
docker exec postgres psql -U directus -d directus -c "
  INSERT INTO information_unit (canonical_address, unit_kind, owner_ref, created_by, updated_by,
    identity_profile)
  VALUES ('test.t12', 'FAKE_KIND', 'test', 'test', 'test',
    '{\"title\":\"t\",\"owner_lookup_ref\":\"t\",\"primary_section_type_ref\":\"section\"}');
"
# Expected: ERROR unit_kind "FAKE_KIND" not in vocab

# T14: Full positive transaction
docker exec postgres psql -U directus -d directus -c "
BEGIN;
  INSERT INTO information_unit (
    id, canonical_address, unit_kind, owner_ref, created_by, updated_by, identity_profile
  ) VALUES (
    gen_random_uuid(), 'test.t14', 'design_doc_section', 'test', 'test', 'test',
    '{\"title\":\"Test T14\",\"owner_lookup_ref\":\"test\",\"primary_section_type_ref\":\"section\"}'
  );

  INSERT INTO unit_version (unit_id, body, content_hash, version_seq, created_by)
  SELECT id, 'Test body', 'sha256_placeholder', 1, 'test'
  FROM information_unit WHERE canonical_address = 'test.t14';

  UPDATE information_unit
  SET version_anchor_ref = (SELECT id FROM unit_version WHERE unit_id = information_unit.id),
      content_anchor_ref = (SELECT id::text FROM unit_version WHERE unit_id = information_unit.id)
  WHERE canonical_address = 'test.t14';
COMMIT;
"
# Expected: SUCCESS — no errors

# Verify T14 data
docker exec postgres psql -U directus -d directus -c "
  SELECT iu.canonical_address, iu.version_anchor_ref, uv.body
  FROM information_unit iu
  JOIN unit_version uv ON uv.id = iu.version_anchor_ref
  WHERE iu.canonical_address = 'test.t14';
"
# Expected: 1 row, body = 'Test body'

Cleanup test data sau smoke:

docker exec postgres psql -U directus -d directus -c "
  DELETE FROM unit_version WHERE unit_id IN (SELECT id FROM information_unit WHERE canonical_address LIKE 'test.%');
  DELETE FROM information_unit WHERE canonical_address LIKE 'test.%';
"

Nếu bất kỳ T1-T14 FAIL → chạy full rollback (09a §6) → báo cáo failure.


Rollback (nếu cần)

docker exec postgres psql -U directus -d directus -c "
  -- Birth gate
  DROP TRIGGER IF EXISTS trg_iu_birth_gate_layer1 ON information_unit;
  DROP TRIGGER IF EXISTS trg_iu_birth_gate_layer2 ON information_unit;
  DROP FUNCTION IF EXISTS fn_iu_birth_gate_layer1();
  DROP FUNCTION IF EXISTS fn_iu_birth_gate_layer2();
  -- Tables
  DROP TRIGGER IF EXISTS trg_iu_updated_at ON information_unit;
  ALTER TABLE information_unit DROP CONSTRAINT IF EXISTS fk_iu_version_anchor;
  DROP TABLE IF EXISTS unit_version CASCADE;
  DROP TABLE IF EXISTS information_unit CASCADE;
  DROP FUNCTION IF EXISTS fn_iu_updated_at();
  -- Vocab
  DELETE FROM dot_config WHERE config_key IN (
    'vocab.unit_kind.design_doc_section',
    'vocab.section_type.section',
    'vocab.publication_type.design_doc'
  );
"

Report

Upload execution report vào: knowledge/dev/laws/dieu44-trien-khai/reports/iu0-pack1-execution-report.md

Report gồm:

  • Preflight 0a-0g: PASS/FAIL per item
  • Step 1-5: PASS/FAIL + verify output
  • Smoke T1-T14: PASS/FAIL per test
  • Cleanup: done/not needed
  • Overall: PASS (Pack 1 DONE) hoặc FAIL + rollback evidence
  • PG version + gen_random_uuid() source

HARD STOP

Sau upload report: DỪNG. Không sang Pack 2. Không tạo outbox. Không tạo Qdrant collection. Report là output cuối.


Pack 1 Execution Prompt | Agent: Claude Code (claude-go) | SSH: contabo | DB: directus | Chờ User C10