KB-18DF

P3D Pack 1 Phase 4 — Implementation DRAFT v6 Patch Report

9 min read Revision 1
p3dpack1phase4patch-reportdraft-v6g3-g7-fix

P3D Pack 1 Phase 4 — Implementation DRAFT v6 Patch Report

Date: 2026-05-11 Author: Opus 4.7 (patch executor) Directive: gpt-directive-opus-p3d-pack1-phase4-draft-v6-g3-g7-planner-contract-2026-05-11.md Action: Patch DRAFT v5 → DRAFT v6 (G3 + G7 planner contract only) Execution: None. No agent dispatched. No seed run. No migration.


1. Status flags

phase4_draft_v6_status=PATCHED
patched_scope=G3_G7_SECTION_C_ONLY
v5_run_status=ROLLED_BACK_ACCEPTED
g3_contract_gate_updated=true
g7_jsonb_planner_probe_updated=true
section_c_planner_call_updated=true
no_delta_logic_changed=true
no_insert_logic_changed=true
no_species_execution_added=true
no_migration_added=true
requires_GPT_User_review_before_dispatch=true

2. Paths

Artifact Path
DRAFT v6 prompt (patched in place) knowledge/dev/laws/dieu44-trien-khai/prompts/p3d-pack1-phase4-governance-vocab-species-prep-implementation-prompt-DRAFT.md (revision 8)
This patch report knowledge/dev/laws/dieu44-trien-khai/reports/p3d-pack1-phase4-implementation-draft-v6-patch-report.md
Source directive knowledge/dev/laws/dieu44-trien-khai/directives/gpt-directive-opus-p3d-pack1-phase4-draft-v6-g3-g7-planner-contract-2026-05-11.md
v5 run report knowledge/dev/laws/dieu44-trien-khai/reports/p3d-pack1-phase4-vocab-species-seed-implementation-report.md

3. Top 5 exact changes from v5

Change 1 — G3 now verifies fn_iu_create_plan FULL CONTRACT via pg_proc

v5 G3: Only checked pg_proc.proname = 'fn_iu_create_plan' existed. Did not verify return type or argument names. A signature mismatch surfaced only at G7 (after delta computation and 14 INSERTs that then rolled back).

v6 G3: Uses pg_proc introspection to verify three things before any delta/insert:

  • prorettype resolves to jsonb (not a row type).
  • proargnames contains all 9 required arg names: p_canonical_address, p_title, p_body, p_actor, p_unit_kind, p_section_type, p_owner_ref, p_publication_type, p_parent_ref.
  • If any check fails → RAISE EXCEPTION 'G3_FAIL ...' with clear diagnostic (missing args listed, actual return type shown).

Rationale: pg_proc introspection is a compile-time check — verifies contract without executing the function. This catches signature mismatches at G3 (cheap, before any write) instead of G7 (expensive, after 14 INSERTs). Chosen over GPT's fallback suggestion of "probe call before insert" because a pre-insert probe would fail with unresolved_vocab (expected but confusing — can't distinguish signature failure from vocab failure).

Change 2 — G7 calls production 9-arg jsonb signature

v5 G7: Called fn_iu_create_plan(p_canonical_address, p_unit_kind, p_title, p_body) expecting a row with columns (status, resolved_unit_kind, would_create). This signature does not exist in production.

v6 G7: Calls all 9 named args:

  • p_canonical_address := probe address with run_marker
  • p_title := probe title
  • p_body := probe body
  • p_actor := current_setting('p3d.run_marker') (run-scoped, not a real user)
  • p_unit_kind := 'law_unit' (Pack 1 EVOLVE contract)
  • p_section_type := live from dot_config WHERE key LIKE 'vocab.section_type.%' LIMIT 1 (no hardcode)
  • p_owner_ref := NULL
  • p_publication_type := live from dot_config WHERE key LIKE 'vocab.publication_type.%' LIMIT 1 (no hardcode)
  • p_parent_ref := NULL::uuid

Result stored as v_plan jsonb.

Change 3 — G7 defensive jsonb parsing

v5 G7: Assumed SELECT status, resolved_unit_kind, would_create INTO ... from a row-returning function. Would fail at resolution time (PG cannot find the 4-arg signature).

v6 G7: Validates jsonb step by step:

  1. v_plan IS NOT NULL
  2. jsonb_typeof(v_plan) = 'object'
  3. v_plan ? 'status' (key exists)
  4. v_plan ? 'resolved_unit_kind' (key exists)
  5. (v_plan ->> 'status') NOT ILIKE '%unresolved_vocab%'
  6. (v_plan ->> 'resolved_unit_kind') = 'law_unit'

Any check failure → RAISE EXCEPTION 'G7_FAIL ...' with full v_plan in the error message for diagnostics.

Change 4 — G7 BEGIN/EXCEPTION wrapper for clear error labelling

v5 G7: No exception handler — a planner-side error would surface as a raw PG error without G7 attribution.

v6 G7: The fn_iu_create_plan(...) call is wrapped in:

BEGIN
  v_plan := public.fn_iu_create_plan(...);
EXCEPTION WHEN OTHERS THEN
  RAISE EXCEPTION 'G7_FAIL planner call exception: % [SQLSTATE %]', SQLERRM, SQLSTATE;
END;

This ensures any planner-side error (signature, internal, null-arg) gets a G7-attributed message. Agent report and VPS log will always show G7_FAIL prefix for planner failures.

Change 5 — Section C planner probe updated to 9-arg jsonb

v5 Section C: Called the same non-existent 4-arg signature as G7. Would fail after a successful commit, producing an incomplete post-commit report.

v6 Section C: Uses the production 9-arg call with p_section_type and p_publication_type derived from live dot_config via inline subqueries:

SELECT public.fn_iu_create_plan(
  ...,
  p_section_type := (SELECT value FROM public.dot_config WHERE key LIKE 'vocab.section_type.%' ORDER BY key LIMIT 1),
  p_publication_type := (SELECT value FROM public.dot_config WHERE key LIKE 'vocab.publication_type.%' ORDER BY key LIMIT 1),
  ...
) AS planner_result;

Returns one row with one jsonb column. Agent includes the jsonb output in the report.

4. What was NOT changed (attestation)

Component Changed? Notes
G1 (schema gate) No Identical to v5
G2 (unique key gate) No Identical to v5
G4 (source columns gate) No Identical to v5
G5 (value conflict gate) No Identical to v5
G6 (run marker gate) No Identical to v5
Delta computation logic No Same 3 temp tables, same authoritative sources
INSERT/RETURNING logic No Same _inserted_keys pattern
ON COMMIT DROP temp tables No All temp tables still transaction-scoped
Section A (read-only preflight) No Identical to v5
Section D (species read-only) No Identical to v5
Section E (hash provenance doc-only) No Identical to v5
Section F (rollback exact keys) No Identical to v5
Hard boundaries (§1) No Identical to v5
Preflight bash setup (§2) No Only echo string changed v5→v6
No-hardcode philosophy No Reinforced: G7 probe args derived live
Single transaction model No Same one-psql, one-BEGIN..COMMIT

5. Opus deviation from GPT directive

One methodological choice that differs from GPT's fallback suggestion:

GPT directive §4 offered a fallback: "If exact verification is too difficult, use a safe equivalent gate: call a read-only planner probe inside the transaction with all nine named args before insert."

Opus chose pg_proc introspection (primary approach) instead of the fallback probe-before-insert. Reasoning:

  • pg_proc introspection is NOT "too difficult" — three fields (proargnames, prorettype, existence) are straightforward in PG 16.
  • A pre-insert probe would fail with unresolved_vocab (vocab not yet seeded), which is expected but creates diagnostic confusion: is the failure from a signature mismatch or from missing vocab? pg_proc introspection eliminates this ambiguity entirely.
  • This aligns with GPT's stated preference ("implementation can use pg_proc...") — the fallback was offered only if introspection was too difficult.

Net result: Strictly better than the fallback. If GPT disagrees with this choice, the pg_proc approach can be replaced with a probe call in a future patch without structural impact.

6. Final response to chat

phase4_draft_v5=ROLLED_BACK_ACCEPTED
phase4_draft_v6=PATCHED_G3_G7_SECTION_C
patched_scope=G3_G7_SECTION_C_ONLY
delta_logic_changed=false
insert_logic_changed=false
species_execution_added=false
migration_added=false
agent_dispatch_allowed=false
requires_GPT_User_review_before_dispatch=true
next_action=GPT_REVIEW_DRAFT_V6
Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/reports/p3d-pack1-phase4-implementation-draft-v6-patch-report.md