KB-5443

GPT Review — 23-P3C1 Execution Result and P3C2 Directive

11 min read Revision 1
gpt-reviewpack-23p3c1-passp3c2-directivesafe-functionsapply-wrapper

GPT Review — 23-P3C1 Execution Result and P3C2 Directive

Date: 2026-05-07
Reviewer: GPT-5.5 Thinking / Incomex Hội đồng AI
Reviewed: knowledge/dev/laws/dieu44-trien-khai/reports/23-p3c1-iu-edit-draft-safe-functions-report.md rev1
Related: knowledge/dev/laws/dieu44-trien-khai/reviews/opus-review-23-p3c1-execution-result-2026-05-07.md

Verdict

23-P3C1 PASS. No supplemental Agent action needed.

Agent executed correctly. Opus review is accurate.

P3C1 is closed. Proceed to P3C2 apply/wrapper prompt design, not immediate execution.

Evidence accepted

  • phase_status=PASS
  • p3c2_readiness=READY
  • 21/21 tests PASS
  • 4 functions created:
    • fn_iu_edit_plan
    • fn_iu_create_edit_draft
    • fn_iu_comment_edit_draft
    • fn_iu_comment
  • SECURITY DEFINER + search_path=pg_catalog,public on all 4
  • PUBLIC EXECUTE absent
  • role directus can EXECUTE all 4
  • IU/UV counts unchanged: 6 → 6 / 6 → 6
  • Draft/comment counts changed as expected: 0 → 2 / 0 → 4
  • T11b proves multiple_open_drafts does not silently insert a comment
  • T15b proves address mismatch does not insert a comment
  • T21 proves P3C1 functions have no IU/UV write path and no app.canonical_writer
  • UX evidence matches the “simple but safe” principle
  • UX notes hooks recorded:
    • timestamp OK
    • actor-card GUC deferred
    • per-actor watermark deferred

Runtime state after P3C1

Existing and retained:

  • fn_iu_edit_plan
  • fn_iu_create_edit_draft
  • fn_iu_comment_edit_draft
  • fn_iu_comment

Still absent:

  • fn_iu_apply_edit_draft
  • fn_iu_edit

Pilot drafts retained for P3C2:

  • DRAFT_A = ea7fc2eb-d026-4e0e-bfe0-e12b0e0bc9f4
  • DRAFT_B = f403cb0b-68f2-4e94-a972-6a889a7014fe
  • both on ADDR_A = pilot.iu0.test-001
  • ADDR_B = pilot.p2.20260506-045033.e0ae7ec5 has no open draft in the P3C1 test context

Important P3C2 implementation caution

The P3C1 report found:

psql :'var' substitution under -c does not work reliably on psql 16.13.

P3C2 prompt must avoid this pattern.

Use one of these instead:

  1. heredoc/stdin psql with -v variables; or
  2. carefully quoted shell interpolation for trusted dynamic values; or
  3. SQL set_config(...)/GUC inside same psql session.

Do not use psql -c "... :'var' ..." in P3C2.

Directive to Opus — P3C2 prompt design

Create prompt, do not dispatch:

knowledge/dev/laws/dieu44-trien-khai/prompts/23-p3c2-iu-apply-edit-functions-prompt.md

Report path:

knowledge/dev/laws/dieu44-trien-khai/reports/23-p3c2-iu-apply-edit-functions-report.md

P3C2 objective

Create the official-version write path:

  1. fn_iu_apply_edit_draft(...)
  2. fn_iu_edit(...) wrapper

These are the dangerous functions because they write unit_version / information_unit. They must use the P3A gateway marker and Pack 22 invariant verification.

Hard boundaries

  • No table DDL.
  • No schema changes.
  • No trigger changes.
  • No gateway changes.
  • No vector mutation.
  • No cleanup / no pilot deletion.
  • No direct IU/UV writes outside fn_iu_apply_edit_draft.
  • No Pack 2C.
  • Do not alter P3C1 safe functions unless explicitly justified and reviewed.

Required preflight gates

STOP unless all are true:

  • P3A gateway allow-list includes fn_iu_apply_edit_draft.
  • P3B schema exists and matches expected shape.
  • P3C1 functions exist with expected signatures and grants.
  • fn_iu_apply_edit_draft and fn_iu_edit do not already exist, unless prompt supports idempotent already-created state.
  • fn_content_hash(text) exists.
  • fn_iu_verify_invariants(...) exists and exact callable signature is known.
  • fn_iu_create source/metadata still exists for lifecycle convention comparison.
  • unit_version schema and unique (unit_id, version_seq) constraint still match runtime inspection.
  • direct write gateway is still enforced.
  • current owner/grantee pattern is known.

Capture:

  • IU/UV/draft/comment counts before;
  • current UV lifecycle_status distinct values;
  • full source or relevant excerpts of Pack 22 functions;
  • gateway allow-list value;
  • P3C1 function metadata and grants;
  • existing pilot draft IDs if using retained DRAFT_A/DRAFT_B.

Lifecycle convention rule

P3C2 must decide official unit_version.lifecycle_status deterministically.

Preferred rule:

  1. Inspect existing unit_version.lifecycle_status values.
  2. If all current UV rows share one value, use that exact value for new official versions.
  3. If mixed values exist, inspect fn_iu_create source and report the convention.
  4. If still unclear, STOP for GPT/User decision.

Current P3C1 report says distinct value = draft. If this remains true, use draft unless runtime evidence changes.

Function requirements

1. fn_iu_apply_edit_draft(...)

Recommended signature:

fn_iu_apply_edit_draft(
  p_draft_id uuid,
  p_actor text,
  p_review_note text DEFAULT NULL
) RETURNS jsonb

Behavior:

  • SECURITY DEFINER.
  • SET search_path=pg_catalog,public.
  • validate inputs.
  • resolve actor; P3C2 may still require explicit actor but should record actor-card GUC as future hook.
  • load draft and lock target IU row FOR UPDATE.
  • require draft_status='open'.
  • verify draft base_version_ref is still IU head; if not:
    • update this draft to stale_base, set stale_at=now(), reason if useful;
    • insert system/comment note if safe;
    • return stale_base with guidance, not raw exception.
  • recompute draft hash and compare to draft_content_hash; if mismatch, return/raise safe status. Recommendation: return draft_hash_mismatch before official writes.
  • compute next version sequence under IU lock: max(version_seq)+1.
  • set transaction-local gateway marker:
    • set_config('app.canonical_writer','fn_iu_apply_edit_draft', true)
  • insert one new unit_version official row using draft body/title/profile as designed.
  • update information_unit anchors/current content metadata as Pack 22 convention requires.
  • mark applied draft:
    • draft_status='applied'
    • applied_by=p_actor
    • applied_at=now()
    • applied_version_ref=<new_uv_id>
  • mark other open drafts for same IU/base as stale_base, with stale_at=now().
  • insert apply/system review comment:
    • if p_review_note present, use it;
    • else default message.
  • call fn_iu_verify_invariants and require all pass.
  • if invariant verification fails after writes, RAISE EXCEPTION so transaction rolls back.
  • return JSONB status applied with new version id/seq, applied draft id, stale draft count, guidance.

Important: the only IU/UV write function in Pack 23 should be fn_iu_apply_edit_draft. P3C1 functions must remain safe.

2. fn_iu_edit(...) wrapper

Recommended signature:

fn_iu_edit(
  p_address text,
  p_body text,
  p_actor text,
  p_reason text DEFAULT NULL,
  p_title text DEFAULT NULL,
  p_review_note text DEFAULT NULL
) RETURNS jsonb

Behavior:

  • Read iu_edit.policy.default_mode.
  • If auto_apply:
    • call fn_iu_create_edit_draft(...);
    • if draft_created, call fn_iu_apply_edit_draft(...);
    • return combined result with both draft/apply info.
  • If require_review:
    • call fn_iu_create_edit_draft(...) only;
    • return draft_created_review_required.
  • If no_change, propagate no_change.
  • Do not write IU/UV directly; delegate official writes only through fn_iu_apply_edit_draft.
  • It does not need its own gateway marker.

Status / guidance requirements

Every non-success must include:

  • status
  • guidance
  • next_action

Required statuses include at least:

  • invalid_input
  • draft_not_found
  • draft_not_open
  • stale_base
  • draft_hash_mismatch
  • invariant_failed
  • applied
  • draft_created_review_required
  • no_change

Security/grants

  • Both functions SECURITY DEFINER + SET search_path=pg_catalog,public.
  • REVOKE PUBLIC.
  • GRANT to the same discovered non-PUBLIC EXECUTE grantee set as Pack 22/P3C1, normally directus.
  • Verify intended grantees can execute.

Testing requirements

P3C2 prompt must include tests for:

  1. applying retained DRAFT_A or a newly created pilot draft;
  2. apply creates exactly +1 unit_version and no new information_unit row;
  3. IU head/anchor moves to the new version;
  4. applied draft status fields set correctly;
  5. apply review/system comment inserted;
  6. other open draft on same IU/base becomes stale_base;
  7. applying stale/non-open draft returns safe status and does not create another UV;
  8. direct IU/UV wrong-door still blocked;
  9. fn_iu_edit under auto_apply creates draft + applies it;
  10. fn_iu_edit no_change returns no_change and no writes;
  11. invariant verification all_pass after apply;
  12. PUBLIC EXECUTE absent and grantees can execute;
  13. source check confirms only fn_iu_apply_edit_draft contains IU/UV write paths and gateway marker;
  14. P3C1 functions remain unchanged if possible;
  15. no vector mutation.

Use dynamic pilot data. Retain pilots; no cleanup.

psql execution requirement

Because of the P3C1 psql 16.13 finding, P3C2 prompt must not use -c with :'var' interpolation.

Use heredocs or single psql sessions with set_config, e.g.:

"${PSQL[@]}" -v draft_id="$DRAFT_ID" <<'SQL'
SELECT public.fn_iu_apply_edit_draft(:'draft_id'::uuid, 'agent:p3c2', 'review note');
SQL

or safe shell interpolation only for validated UUID/address values.

Report requirements

Report must include:

  • phase_status PASS/FAIL/CRITICAL;
  • p3d_readiness READY/BLOCKED;
  • function signatures/security/grants;
  • lifecycle convention decision;
  • pilot draft IDs and resulting UV ID/seq;
  • IU/UV/draft/comment counts before/after;
  • stale draft behavior;
  • invariant verification output;
  • gateway marker evidence;
  • UX/self-guiding JSON samples;
  • psql interpolation caveat handling;
  • boundaries honored.

Design cautions for Opus

  1. P3C2 is riskier than P3C1. Prefer near-complete SQL bodies and concrete tests.
  2. Do not overbuild diff/merge/rebase in P3C2.
  3. stale_base is enough for Phase 1. Rebase can be future work.
  4. Actor-card and per-actor watermark are future hooks; do not implement DDL now.
  5. Keep Agent interface simple:
    • fn_iu_edit(address, body, actor) for simple edit;
    • fn_iu_comment(address, actor, comment) for comment;
    • fn_iu_apply_edit_draft(draft_id, actor) for reviewer/apply path.

Summary

P3C1 is complete and proves the safe editorial layer works: create drafts, comment naturally, avoid silent wrong attachment, and preserve IU/UV. P3C2 should now add the official write path carefully: apply a draft into a new version through the existing gateway and invariant system, then expose a simple wrapper for AI/Agent editing.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/reviews/gpt-review-23-p3c1-execution-result-and-p3c2-directive-2026-05-07.md