KB-39AE

Agent Prompt — Phase 5C2 Retry R1 + Conditional R2 with Patched V-3 — 2026-05-14

11 min read Revision 1
agentphase5c2r1-retryr2patched-v3dieu35dieu28dieu32tac-to-iu2026-05-14

Agent Prompt — Phase 5C2 Retry R1 + Conditional R2 with Patched V-3 — 2026-05-14

Mission

Retry Phase 5C2-R1 DIEU-35 migration under patched V-3 validation. If R1 PASS, continue to conditional R2 for DIEU-28 and DIEU-32, each as an independent bounded transaction with its own preflight, rollback keys, validation, and report.

This is a controlled TAC→Information Unit migration execution. It is not UI cutover, not vector work, not bulk migration beyond the three currently live TAC publications.

Authorization

Read first:

  1. GPT review + V-3 patch + authorization: knowledge/dev/laws/dieu44-trien-khai/reviews/gpt-review-r1-execution-v3-spec-fix-authorize-retry-r1-and-conditional-r2-2026-05-14.md
  2. R1 failed/rolled-back execution report: knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-pilot-migration-execution-report.md
  3. Opus review: knowledge/dev/laws/dieu44-trien-khai/reviews/opus-review-r1-execution-and-strategic-direction-2026-05-14.md
  4. R0 design: knowledge/dev/laws/dieu44-trien-khai/design/p3d-phase5c2-resume-tac-to-information-unit-migration-plan.md
  5. R0 mapping report/artifact: knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r0-readonly-mapping-artifact-report.md knowledge/dev/laws/dieu44-trien-khai/artifacts/p3d-phase5c2-r0-dieu35-mapping-artifact.json

Authorization flags:

USER_GO=YES
GPT_FINAL_APPROVAL=YES
V3_SPEC_PATCH_APPROVED=YES
R1_RETRY_AUTHORIZED=YES
CONDITIONAL_R2_AUTHORIZED=YES

Scope

Mandatory first transaction

DIEU-35 only — migrate current live source rows, expected historical count 36 but live count is source of truth.

Conditional next transactions

Only if DIEU-35 retry PASS:

DIEU-28 current live publication — one separate transaction.
DIEU-32 current live publication — one separate transaction.

Historical expectations are reference only:

DIEU-28 historically 27 members.
DIEU-32 historically 23 members.

Agent must discover live counts.

Non-negotiable boundaries

Forbidden throughout:

No DDL.
No schema changes.
No trigger/function changes.
No birth-system changes.
No direct INSERT into information_unit or unit_version.
No TAC writes.
No UI cutover.
No Nuxt/Directus/config changes.
No vector/Qdrant mutation or reindex.
No event_outbox/IU event emission work.
No documents beyond DIEU-35/DIEU-28/DIEU-32.
No touching pre-existing 12 pilot/test IU rows.
No pattern-matching deletes.
No automatic content repair.

Allowed:

Use fn_iu_create canonical writer.
Patch identity_profile/content_profile/provenance only for newly captured rows using allowed marker fn_iu_apply_edit_draft.
Create birth rows via existing trg_birth_information_unit only.
Write rollback-key artifacts to KB + VPS logs before COMMIT.
Rollback exact captured keys only if post-COMMIT rollback is needed.

Patched V-3 validation — mandatory

Replace old V-3 with:

V-3a IU-side hash consistency:
  captured unit_version.content_hash = fn_content_hash(captured unit_version.body)
  REQUIRED PASS.

V-3b Body preservation:
  captured unit_version.body = source tac_unit_version.body for the source row
  REQUIRED PASS.

V-3c TAC hash provenance:
  captured unit_version.content_profile.src_content_hash = source tac_unit_version.content_hash
  REQUIRED PASS.

V-3d Cross-system hash equality:
  captured unit_version.content_hash = source tac_unit_version.content_hash
  NOT A GATE. Report mismatches as expected legacy hash-scheme divergence.

If V-3a/V-3b/V-3c fail, rollback that publication transaction.

Per-publication execution algorithm

For each publication in order: DIEU-35, then conditionally DIEU-28, DIEU-32.

Stage A — Read-only preflight

  1. Resolve publication by doc_code and current version/lifecycle.
  2. Resolve source rows through TAC FK join: tac_publication → tac_publication_member → tac_logical_unit → tac_unit_version.
  3. Verify:
    • source_count > 0;
    • render_order count/distinct/min/max and contiguity;
    • owner NULL count = 0;
    • body NULL count = 0;
    • collision count against information_unit.canonical_address = 0;
    • required vocab keys exist: publication_authority.incomex_council, publication_type.law, unit_kind.law_unit, all section types;
    • fn_iu_create signature exists;
    • gateway mode enforced;
    • allowed markers include fn_iu_create and fn_iu_apply_edit_draft;
    • edit policy require_review;
    • birth trigger exists;
    • species primary mapping exists;
    • pre-existing IU rows list captured;
    • TAC table pre-counts captured.
  4. Detect soft flags:
    • body_char_count <= 1;
    • section_code all NULL;
    • unusually short title/body;
    • non-contiguous hierarchy.

If any hard gate fails, skip/stop that publication before BEGIN and report.

For DIEU-35 retry, require source_count=36 unless live source changed; if changed, stop before BEGIN and report SOURCE_DRIFT_REQUIRES_GPT_REVIEW.

For DIEU-28/DIEU-32, live count is source of truth; report historical count comparison but do not gate on historical count.

Stage B — Single transaction per publication

Open one transaction per publication.

For each source row ordered by render_order:

  1. Call fn_iu_create only:
SELECT public.fn_iu_create(
  p_canonical_address := :src_canonical_address,
  p_title             := :src_title,
  p_body              := :src_body,
  p_actor             := 'agent:p3d-phase5c2-r1-r2',
  p_unit_kind         := 'law_unit',
  p_section_type      := :src_section_type,
  p_owner_ref         := :src_owner,
  p_publication_type  := 'law',
  p_parent_ref        := NULL
);
  1. Capture returned IU id, UV id, status, warnings.
  2. If status is not created/success expected, rollback and stop publication.
  3. Patch only captured information_unit row:
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 = :doc_code,
    sort_order = :src_render_order,
    section_type = :src_section_type,
    section_code = :src_section_code
WHERE id = :captured_iu_id;
  1. Patch only captured unit_version row:
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:' || :doc_code || ':' || :src_unit_version_id::text
WHERE id = :captured_uv_id;

Stage C — Rollback key dual-write before COMMIT

For each publication before COMMIT, write exact rollback keys to:

KB:

knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-<doc_code_lower>-rollback-keys-<utc>.md

VPS log:

/opt/incomex/logs/p3d-phase5c2-<doc_code_lower>-<utc>.log

If either write fails, rollback that publication transaction.

Stage D — In-transaction validation before COMMIT

Run V-1..V-7:

  • V-1 captured counts equal source count.
  • V-2 render_order fidelity.
  • V-3 patched hash/body/provenance validation.
  • V-4 authority incomex_council on all captured IU.
  • V-5 birth coverage: one birth per captured IU, species information_unit_atom, composition atom, 0 NULL species.
  • V-6 TAC source untouched.
  • V-7 fn_iu_verify_invariants if safe; if uncertain, do not execute and fail closed unless GPT-approved skip exists. Current prior R1 proved safe, so run it.

If any required gate fails, rollback that publication transaction.

Stage E — COMMIT and post-COMMIT validation

After COMMIT for each publication:

  • V-8 gateway triggers remain attached and marker not leaked.
  • V-9 pre-existing IU rows untouched.
  • V-10 rollback keys exist in KB + VPS log.
  • final row counts = pre + source count for IU/UV/birth.
  • TAC table counts unchanged.
  • UI/vector/bulk flags false.

If post-COMMIT validation fails, execute exact-key rollback for that publication only and report.

Reporting requirements

Create reports:

  1. R1 retry report:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-dieu35-retry-patched-v3-execution-report.md
  1. R2 D28 report, if reached:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r2-dieu28-pilot-migration-execution-report.md
  1. R2 D32 report, if reached:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r2-dieu32-pilot-migration-execution-report.md
  1. Consolidated status:
knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-r2-consolidated-status-report.md
  1. Rollback key reports for every publication that reaches key capture stage.

Required final response fields

phase5c2_retry_r1_status=PASS|FAILED_ROLLED_BACK|BLOCKED_BEFORE_BEGIN|FAILED_POST_COMMIT_ROLLBACK_FAILED
r2_dieu28_status=PASS|FAILED_ROLLED_BACK|BLOCKED_BEFORE_BEGIN|SKIPPED_R1_NOT_PASS|FAILED_POST_COMMIT_ROLLBACK_FAILED
r2_dieu32_status=PASS|FAILED_ROLLED_BACK|BLOCKED_BEFORE_BEGIN|SKIPPED_R1_NOT_PASS|FAILED_POST_COMMIT_ROLLBACK_FAILED
total_publications_committed=<N>
total_iu_created_committed=<N>
total_uv_created_committed=<N>
total_birth_created_committed=<N>
tac_source_untouched=true|false|unknown
pre_existing_iu_untouched=true|false|unknown
ui_cutover_performed=false
vector_work_performed=false
bulk_migration_performed=false
v3_patched_semantics_used=true|false
cross_system_hash_equality_used_as_gate=false
rollback_reports=<list>
execution_reports=<list>
consolidated_report_path=knowledge/dev/laws/dieu44-trien-khai/reports/p3d-phase5c2-r1-r2-consolidated-status-report.md
next_recommended_action=GPT_REVIEW_R1_R2_RESULTS_THEN_START_AUTOMATED_CUTTER_DESIGN

Strategic note for report

Include a section:

Toward automated cutter: lessons learned for “Cắt luật A”

Capture what this run proves and what must become tooling:

  • source discovery;
  • segmentation/mapping;
  • vocab validation;
  • fn_iu_create batching;
  • rollback-key dual-write;
  • patched hash semantics;
  • exact source preservation;
  • edge/provenance hooks.

Stop conditions

Stop immediately if:

  • R1 retry preflight detects source drift on DIEU-35 count/address set.
  • Any publication has collision count > 0.
  • Missing vocab/owner/body > 0.
  • rollback key dual-write fails before COMMIT.
  • V-3a/V-3b/V-3c fails.
  • TAC source is mutated.
  • gateway or birth trigger is missing.

Do not improvise. Rollback exact transaction and report.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/prompts/agent-phase5c2-retry-r1-and-conditional-r2-patched-v3-2026-05-14.md