GPT Review — 23-P3C1 Execution Result and P3C2 Directive
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.mdrev1
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=PASSp3c2_readiness=READY- 21/21 tests PASS
- 4 functions created:
fn_iu_edit_planfn_iu_create_edit_draftfn_iu_comment_edit_draftfn_iu_comment
- SECURITY DEFINER +
search_path=pg_catalog,publicon all 4 - PUBLIC EXECUTE absent
- role
directuscan 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_draftsdoes 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_planfn_iu_create_edit_draftfn_iu_comment_edit_draftfn_iu_comment
Still absent:
fn_iu_apply_edit_draftfn_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.e0ae7ec5has no open draft in the P3C1 test context
Important P3C2 implementation caution
The P3C1 report found:
psql
:'var'substitution under-cdoes not work reliably on psql 16.13.
P3C2 prompt must avoid this pattern.
Use one of these instead:
- heredoc/stdin psql with
-vvariables; or - carefully quoted shell interpolation for trusted dynamic values; or
- 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:
fn_iu_apply_edit_draft(...)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_draftandfn_iu_editdo 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_createsource/metadata still exists for lifecycle convention comparison.unit_versionschema 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_statusdistinct 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:
- Inspect existing
unit_version.lifecycle_statusvalues. - If all current UV rows share one value, use that exact value for new official versions.
- If mixed values exist, inspect
fn_iu_createsource and report the convention. - 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_refis still IU head; if not:- update this draft to
stale_base, setstale_at=now(), reason if useful; - insert system/comment note if safe;
- return
stale_basewith guidance, not raw exception.
- update this draft to
- recompute draft hash and compare to
draft_content_hash; if mismatch, return/raise safe status. Recommendation: returndraft_hash_mismatchbefore 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_versionofficial row using draft body/title/profile as designed. - update
information_unitanchors/current content metadata as Pack 22 convention requires. - mark applied draft:
draft_status='applied'applied_by=p_actorapplied_at=now()applied_version_ref=<new_uv_id>
- mark other open drafts for same IU/base as
stale_base, withstale_at=now(). - insert apply/system review comment:
- if
p_review_notepresent, use it; - else default message.
- if
- call
fn_iu_verify_invariantsand require all pass. - if invariant verification fails after writes, RAISE EXCEPTION so transaction rolls back.
- return JSONB status
appliedwith 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.
- call
- If
require_review:- call
fn_iu_create_edit_draft(...)only; - return
draft_created_review_required.
- call
- 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:
statusguidancenext_action
Required statuses include at least:
invalid_inputdraft_not_founddraft_not_openstale_basedraft_hash_mismatchinvariant_failedapplieddraft_created_review_requiredno_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:
- applying retained DRAFT_A or a newly created pilot draft;
- apply creates exactly +1
unit_versionand no newinformation_unitrow; - IU head/anchor moves to the new version;
- applied draft status fields set correctly;
- apply review/system comment inserted;
- other open draft on same IU/base becomes
stale_base; - applying stale/non-open draft returns safe status and does not create another UV;
- direct IU/UV wrong-door still blocked;
fn_iu_editunderauto_applycreates draft + applies it;fn_iu_editno_change returns no_change and no writes;- invariant verification all_pass after apply;
- PUBLIC EXECUTE absent and grantees can execute;
- source check confirms only
fn_iu_apply_edit_draftcontains IU/UV write paths and gateway marker; - P3C1 functions remain unchanged if possible;
- 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
- P3C2 is riskier than P3C1. Prefer near-complete SQL bodies and concrete tests.
- Do not overbuild diff/merge/rebase in P3C2.
stale_baseis enough for Phase 1. Rebase can be future work.- Actor-card and per-actor watermark are future hooks; do not implement DDL now.
- 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.