KB-2821

Agent Prompt DRAFT — Phase 5C2-R1 DIEU-35 Pilot Migration Execution — TAC→Information Unit — 2026-05-14

12 min read Revision 1
agentdraftphase5c2r1dieu35pilot-migrationtac-to-iuexecution-promptrequires-user-go2026-05-14

Agent Prompt DRAFT — Phase 5C2-R1 DIEU-35 Pilot Migration Execution — TAC→Information Unit — 2026-05-14

Status: DRAFT — NOT DISPATCHED / NOT APPROVED FOR EXECUTION Mode if approved: BOUNDED WRITE EXECUTION — 36-row DIEU-35 pilot only Requires before dispatch: explicit User GO + GPT final approval of this prompt

0. Mission

Execute Phase 5C2-R1: migrate exactly 36 live TAC DIEU-35 units into native information_unit / unit_version using the canonical fn_iu_create writer, preserving TAC as canonical UI source and producing rollback keys.

This prompt is currently a draft for review only. Do not execute unless User and GPT explicitly approve dispatch.

1. Controlling evidence

Read first:

  1. GPT R0 mapping review: knowledge/dev/laws/dieu44-trien-khai/reviews/gpt-review-phase5c2-r0-readonly-mapping-artifact-approve-r1-prompt-draft-2026-05-14.md
  2. R0 mapping report: knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r0-readonly-mapping-artifact-report.md
  3. R0 mapping JSON artifact: knowledge/dev/laws/dieu44-trien-khai/artifacts/p3d-phase5c2-r0-dieu35-mapping-artifact.json
  4. R0 design: knowledge/dev/laws/dieu44-trien-khai/design/p3d-phase5c2-resume-tac-to-information-unit-migration-plan.md
  5. Agent current-position investigation: knowledge/dev/laws/dieu44-trien-khai/reports/agent-readonly-investigation-iu-current-position-before-next-design-2026-05-14.md
  6. IU create gateway README: knowledge/dev/laws/dieu44-trien-khai/readme/iu-create-gateway-readme.md
  7. Birth Rev3 contract: knowledge/dev/laws/dieu44-trien-khai/design/p3d-birth-system-b2-contract-design-rev3.md

2. Non-negotiable boundaries

Allowed if and only if this prompt is explicitly approved for dispatch:

Create exactly 36 information_unit rows for DIEU-35.
Create exactly 36 unit_version rows through fn_iu_create.
Create exactly 36 birth_registry rows via existing trg_birth_information_unit.
Patch identity_profile/content_profile/provenance only for the newly captured rows.
Write rollback-key artifacts to KB + VPS log before COMMIT.

Still forbidden:

No DDL.
No schema changes.
No trigger/function changes.
No birth-system changes.
No direct INSERT into information_unit or unit_version.
No UPDATE/DELETE of TAC source tables.
No UI cutover.
No Nuxt/Directus/config changes.
No vector/Qdrant mutation or reindex.
No event_outbox/IU event emission work.
No bulk migration beyond DIEU-35 36 rows.
No touching existing 12 pilot/test IU rows.
No pattern matching deletes.
No migration of DIEU-28 or DIEU-32.
No automatic fix of source content anomalies.

3. Required execution structure

The Agent must execute in these stages and stop on failure.

Stage 0 — Authorization checkpoint

Before connecting to PG, confirm the dispatch context includes explicit approval:

USER_GO=YES
GPT_FINAL_APPROVAL=YES
PROMPT_PATH=knowledge/dev/laws/dieu44-trien-khai/prompts/agent-phase5c2-r1-dieu35-pilot-migration-execution-prompt-DRAFT.md

If either approval is missing, STOP and produce a no-execution report.

Stage 1 — Read-only preflight

Run SELECT-only checks and write them to the final report.

Required gates:

  1. TAC tables exist.
  2. DIEU-35 exists.
  3. DIEU-35 member count = 36 live.
  4. render_order contiguous 0..35.
  5. owner NULL count = 0.
  6. body NULL count = 0.
  7. Collision count with existing information_unit.canonical_address = 0.
  8. Existing IU rows count and IDs captured before migration.
  9. fn_iu_create signature exists and returns jsonb.
  10. fn_content_hash exists.
  11. Gateway mode = enforced.
  12. Allowed marker values include fn_iu_create and fn_iu_apply_edit_draft.
  13. Edit policy default = require_review.
  14. trg_birth_information_unit exists on information_unit.
  15. species_collection_map primary IU mapping exists.
  16. Required vocab seeds exist:
    • vocab.publication_authority.incomex_council
    • vocab.publication_type.law
    • vocab.unit_kind.law_unit
    • all vocab.section_type.<source section_type> for DIEU-35.
  17. birth_registry current count for collection_name='information_unit' captured.
  18. TAC table pre-counts captured for 4 TAC tables.
  19. Soft-flag row D38-DIEU35-S8-P3 with body_char_count=1 detected and reported as source-preservation flag, not blocker.

If any hard gate fails, STOP before BEGIN.

Stage 2 — Prepare transaction inputs

Build a 36-row in-memory mapping from live TAC source, equivalent to R0 artifact.

Do not use stale R0 JSON as the only source of truth. Use R0 as plan of record, but source rows must be live-read immediately before transaction.

For each source row, prepare:

  • p_canonical_address = tac_logical_unit.canonical_address
  • p_title = tac_unit_version.title
  • p_body = tac_unit_version.body
  • p_actor = 'agent:p3d-phase5c2-r1'
  • p_unit_kind = 'law_unit'
  • p_section_type = tac_logical_unit.section_type
  • p_owner_ref = tac_logical_unit.owner
  • p_publication_type = 'law'
  • p_parent_ref = NULL
  • planned identity_profile patch per R0 design §5.3
  • planned content_profile patch per R0 design §5.4

Stage 3 — Transaction execution

Open one transaction.

For each of 36 rows, in render_order order:

  1. Call canonical writer only:
SELECT public.fn_iu_create(
  p_canonical_address := :canonical_address,
  p_title             := :title,
  p_body              := :body,
  p_actor             := 'agent:p3d-phase5c2-r1',
  p_unit_kind         := 'law_unit',
  p_section_type      := :section_type,
  p_owner_ref         := :owner,
  p_publication_type  := 'law',
  p_parent_ref        := NULL
);
  1. Parse returned JSONB to capture at minimum:

    • IU id
    • UV id / version id
    • canonical_address
    • status
    • any warnings/errors
  2. If returned status is not successful/created/expected idempotent success, ROLLBACK and STOP. Do not continue.

  3. Patch only the newly created/captured IU row's identity_profile using exact captured IU id or canonical_address, with allowed gateway marker:

SET LOCAL "app.canonical_writer" = 'fn_iu_apply_edit_draft';
UPDATE information_unit
SET identity_profile = COALESCE(identity_profile, '{}'::jsonb) || :identity_profile_patch::jsonb,
    doc_code = 'DIEU-35',
    sort_order = :src_render_order,
    section_type = :src_section_type,
    section_code = :src_section_code
WHERE id = :captured_iu_id;
  1. Patch only the newly created/captured UV row's content_profile and provenance, with allowed gateway marker still local:
SET LOCAL "app.canonical_writer" = 'fn_iu_apply_edit_draft';
UPDATE unit_version
SET content_profile = COALESCE(content_profile, '{}'::jsonb) || :content_profile_patch::jsonb,
    provenance = 'tac:DIEU-35:' || :src_unit_version_id::text
WHERE id = :captured_uv_id;

Notes:

  • If gateway rejects these updates, ROLLBACK and STOP.
  • Do not update any TAC table.
  • Do not update existing pilot/test IU rows.
  • Do not enrich birth_registry ELD columns; leave placeholder behavior as Rev3 defines.

Stage 4 — Capture rollback keys before COMMIT

Before COMMIT, collect exact keys:

  • captured_iu_ids uuid[]
  • captured_uv_ids uuid[]
  • captured_iu_entity_codes text[] where format is information_unit::<iu_uuid>
  • source src_logical_unit_id, src_unit_version_id, canonical_address, render_order mapping

Write rollback keys to BOTH:

  1. KB:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-rollback-keys-<utc>.md
  1. VPS log file:
/opt/incomex/logs/p3d-phase5c2-r1-dieu35-<utc>.log

If either write fails, ROLLBACK and STOP.

Stage 5 — In-transaction validation before COMMIT

Run V-1..V-7 before COMMIT.

V-1 row accounting:

captured_iu_count=36
captured_uv_count=36
source_count_live=36

V-2 render fidelity:

  • identity_profile.rendering.render_order multiset = 0..35.

V-3 content hash fidelity:

  • each captured UV content_hash = fn_content_hash(body).
  • compare with source tac_unit_version.content_hash where source non-null.

V-4 authority:

  • all captured IU identity_profile.publication_authority_ref = incomex_council.

V-5 birth coverage:

  • 36 birth rows exist for captured IU entity codes.
  • all species_code = information_unit_atom.
  • all composition_level = atom.
  • 0 NULL species.

V-6 TAC source untouched:

  • 4 TAC table counts equal preflight counts.

V-7 IU invariants:

  • If fn_iu_verify_invariants is safe to execute as read-only, run per captured canonical_address and require PASS.
  • If safety is uncertain, do not execute and mark V-7=SKIPPED_SAFETY_UNKNOWN, then ROLLBACK unless GPT/User pre-approved skipping it. Default: require PASS or rollback.

If V-1..V-7 PASS, COMMIT. If any FAIL, ROLLBACK.

Stage 6 — Post-COMMIT validation

After COMMIT, run V-8..V-10.

V-8 gateway integrity:

  • trg_aa_iu_gateway_write_guard and trg_aa_uv_gateway_write_guard still attached.
  • no marker leakage in a fresh session.

V-9 pre-existing 12 pilot/test rows untouched:

  • compare pre-captured IDs, canonical_addresses, version_anchor_ref if practical.

V-10 rollback key artifact presence:

  • KB rollback key document exists.
  • VPS log contains rollback key block.

Also verify:

  • final IU row count = pre_count + 36.
  • final UV row count = pre_count + 36.
  • final birth_registry IU count = pre_count + 36.
  • TAC table counts unchanged.
  • UI cutover not performed.
  • Qdrant untouched.

If post-COMMIT validation fails, execute exact-key rollback only, then report failure and rollback evidence.

4. Soft-flag handling

The row below is a source-content soft-flag, not a blocker:

render_order=26
canonical_address=D38-DIEU35-S8-P3
body_char_count=1

R1 must:

  • migrate the source body exactly if execution proceeds;
  • report this row in source_soft_flags;
  • not repair, infer, expand, normalize, or skip it;
  • state that semantic repair, if any, is a separate content workflow.

5. Required reports

Create these KB reports:

  1. Execution report:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-pilot-migration-execution-report.md
  1. Rollback keys report, only if transaction reaches key-capture stage:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-rollback-keys-<utc>.md

If execution stops before BEGIN, create only the execution report with execution_started=false.

6. Required final response fields

phase5c2_r1_execution_status=PASS|FAILED_ROLLED_BACK|BLOCKED_BEFORE_BEGIN|FAILED_POST_COMMIT_ROLLBACK_FAILED
user_go_confirmed=true|false
gpt_final_approval_confirmed=true|false
execution_started=true|false
commit_performed=true|false
rollback_performed=true|false
rollback_success=true|false|not_applicable
source_count_live=<N>
created_iu_count=<N>
created_uv_count=<N>
created_birth_count=<N>
pre_existing_iu_untouched=true|false|unknown
tac_source_untouched=true|false|unknown
render_fidelity_pass=true|false|unknown
content_hash_fidelity_pass=true|false|unknown
birth_coverage_pass=true|false|unknown
gateway_integrity_pass=true|false|unknown
soft_flag_rows=[D38-DIEU35-S8-P3]
ui_cutover_performed=false
vector_work_performed=false
bulk_migration_performed=false
rollback_keys_report_path=<path or none>
execution_report_path=knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-pilot-migration-execution-report.md
next_recommended_action=GPT_OPUS_REVIEW_R1_EXECUTION_REPORT

7. Important warning

This prompt is a DRAFT. Do not run it from KB unless the user and GPT explicitly approve execution.

R1 is the first real TAC→IU write step. Treat failure conservatively: rollback and report. Do not improvise.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/prompts/agent-phase5c2-r1-dieu35-pilot-migration-execution-prompt-DRAFT.md