KB-207A rev 2

P3D Pack 1 Phase 4 — Governance/Vocab Discovery Prompt rev2

13 min read Revision 2
p3dpack1phase4discoveryreadonlyvocabspeciesno-hardcoderev2

P3D Pack 1 Phase 4 — Read-Only Governance/Vocab Discovery Prompt rev2

Date: 2026-05-11 Status: rev2 — GPT patched for no-hardcode discipline; APPROVED_FOR_AGENT_DISPATCH_AFTER_USER_CONFIRMATION Mode: READ-ONLY DISCOVERY. NO mutation.


0. Mission

Discover live governance/vocab/species/composition/hierarchy state before any Phase 4 seed or Phase 5 migration.

This prompt must generate facts from the live system. Do not use memory counts. Do not assume fixed species/layer/table/DOT counts. All counts are snapshots only.


1. Hard boundaries

  • NO DDL.
  • NO DML.
  • NO INSERT/UPDATE/DELETE.
  • NO function/trigger patch.
  • NO migration.
  • NO Directus mutation.
  • NO Qdrant/vector mutation.
  • NO DOT-119 execution.
  • SELECT/source inspection only.

2. Shell setup

#!/usr/bin/env bash
set -uo pipefail
TS=$(date +%Y%m%d-%H%M%S)
LOG="/tmp/p3d-phase4-discovery-${TS}.log"
exec > >(tee -a "$LOG") 2>&1

CONTAINER="${PG_CONTAINER:-postgres}"
DB="${PG_DB:-directus}"
DBUSER="${PG_USER:-directus}"
PSQL=(docker exec -i "$CONTAINER" psql -U "$DBUSER" -d "$DB" -v ON_ERROR_STOP=1 -P pager=off)

echo "=== P3D PACK1 PHASE4 GOVERNANCE/VOCAB DISCOVERY rev2 $TS ==="
echo "CONTAINER=$CONTAINER DB=$DB DBUSER=$DBUSER"
"${PSQL[@]}" -c "SET default_transaction_read_only = on; SHOW default_transaction_read_only;"

If PG access is blocked, write a BLOCKED report.


3. Schema introspection first

echo "--- I1: target/source table existence ---"
"${PSQL[@]}" -c "
SELECT c.relname AS table_name, c.relkind
FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE n.nspname='public'
  AND c.relname IN (
    'tac_logical_unit','tac_unit_version','tac_publication','tac_publication_member',
    'information_unit','unit_version','dot_config','collection_registry','species_collection_map',
    'entity_species','entity_labels','birth_registry','universal_edges'
  )
ORDER BY c.relname;"

for tbl in tac_logical_unit tac_unit_version tac_publication tac_publication_member information_unit unit_version dot_config collection_registry species_collection_map entity_species entity_labels birth_registry universal_edges; do
  echo "--- I2 columns: ${tbl} ---"
  "${PSQL[@]}" -c "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema='public' AND table_name='${tbl}' ORDER BY ordinal_position;" 2>/dev/null || echo "${tbl}=NOT_FOUND"
done

4. Vocab discovery — live values and deltas only

echo "--- V1: TAC section_type actually used ---"
"${PSQL[@]}" -c "SELECT section_type, count(*) AS used_count FROM public.tac_logical_unit GROUP BY section_type ORDER BY section_type;"

echo "--- V2: TAC section_type_vocab live snapshot ---"
"${PSQL[@]}" -c "SELECT * FROM public.tac_section_type_vocab ORDER BY 1;" 2>/dev/null || echo "tac_section_type_vocab=NOT_FOUND"

echo "--- V3: TAC publication_type actually used ---"
"${PSQL[@]}" -c "SELECT publication_type, count(*) AS used_count FROM public.tac_publication GROUP BY publication_type ORDER BY publication_type;" 2>/dev/null || echo "tac_publication.publication_type=NOT_FOUND_OR_SCHEMA_DIFF"

echo "--- V4: TAC publication_type_vocab live snapshot ---"
"${PSQL[@]}" -c "SELECT * FROM public.tac_publication_type_vocab ORDER BY 1;" 2>/dev/null || echo "tac_publication_type_vocab=NOT_FOUND"

echo "--- V5: IU dot_config vocab live snapshot ---"
"${PSQL[@]}" -c "SELECT key, value FROM public.dot_config WHERE key LIKE 'vocab.%' ORDER BY key;"

echo "--- V6: discovery delta candidates, no seed executed ---"
"${PSQL[@]}" <<'SQL'
WITH tac_used_section AS (
  SELECT DISTINCT section_type::text AS code FROM public.tac_logical_unit WHERE section_type IS NOT NULL
), iu_section AS (
  SELECT regexp_replace(key, '^vocab\.section_type\.', '') AS code FROM public.dot_config WHERE key LIKE 'vocab.section_type.%'
), tac_used_pub AS (
  SELECT DISTINCT publication_type::text AS code FROM public.tac_publication WHERE publication_type IS NOT NULL
), iu_pub AS (
  SELECT regexp_replace(key, '^vocab\.publication_type\.', '') AS code FROM public.dot_config WHERE key LIKE 'vocab.publication_type.%'
), needed_unit_kind AS (
  SELECT 'law_unit'::text AS code
), iu_unit_kind AS (
  SELECT regexp_replace(key, '^vocab\.unit_kind\.', '') AS code FROM public.dot_config WHERE key LIKE 'vocab.unit_kind.%'
)
SELECT 'section_type_missing_in_iu' AS delta_type, code FROM tac_used_section EXCEPT SELECT 'section_type_missing_in_iu', code FROM iu_section
UNION ALL
SELECT 'publication_type_missing_in_iu' AS delta_type, code FROM tac_used_pub EXCEPT SELECT 'publication_type_missing_in_iu', code FROM iu_pub
UNION ALL
SELECT 'unit_kind_missing_in_iu' AS delta_type, code FROM needed_unit_kind EXCEPT SELECT 'unit_kind_missing_in_iu', code FROM iu_unit_kind
ORDER BY delta_type, code;
SQL

for tbl in tac_lu_lifecycle_vocab tac_uv_lifecycle_vocab tac_pub_lifecycle_vocab tac_review_state_vocab; do
  echo "--- V7 live vocab table: ${tbl} ---"
  "${PSQL[@]}" -c "SELECT * FROM public.${tbl} ORDER BY 1;" 2>/dev/null || echo "${tbl}=NOT_FOUND_OR_SCHEMA_DIFF"
done

Note: law_unit appears here as a discovered requirement from the active migration design and Phase 3 blocker, not as a counted species/layer constant. Agent must report it as a proposed target unit_kind requiring GPT/User approval before seed.


5. Species / composition / governance discovery

echo "--- S1: entity_species live snapshot ---"
"${PSQL[@]}" -c "SELECT * FROM public.entity_species ORDER BY 1;" 2>/dev/null || echo "entity_species=NOT_FOUND_OR_SCHEMA_DIFF"

echo "--- S2: collection_registry rows for discovered target/source collections ---"
"${PSQL[@]}" -c "
SELECT *
FROM public.collection_registry
WHERE to_jsonb(collection_registry)::text ILIKE '%information_unit%'
   OR to_jsonb(collection_registry)::text ILIKE '%unit_version%'
   OR to_jsonb(collection_registry)::text ILIKE '%tac_logical_unit%'
   OR to_jsonb(collection_registry)::text ILIKE '%tac_unit_version%'
   OR to_jsonb(collection_registry)::text ILIKE '%tac_publication%'
ORDER BY 1
LIMIT 100;" 2>/dev/null || echo "collection_registry=NOT_FOUND_OR_SCHEMA_DIFF"

echo "--- S3: species_collection_map rows for target/source collections discovered by name ---"
"${PSQL[@]}" -c "
SELECT *
FROM public.species_collection_map
WHERE to_jsonb(species_collection_map)::text ILIKE '%information_unit%'
   OR to_jsonb(species_collection_map)::text ILIKE '%unit_version%'
   OR to_jsonb(species_collection_map)::text ILIKE '%tac_logical_unit%'
   OR to_jsonb(species_collection_map)::text ILIKE '%tac_unit_version%'
   OR to_jsonb(species_collection_map)::text ILIKE '%tac_publication%'
ORDER BY 1
LIMIT 100;" 2>/dev/null || echo "species_collection_map=NOT_FOUND_OR_SCHEMA_DIFF"

echo "--- S4: birth_registry evidence for target/source collections ---"
"${PSQL[@]}" -c "
SELECT collection_name, count(*) AS rows,
       count(species_code) AS rows_with_species,
       count(composition_level) AS rows_with_composition
FROM public.birth_registry
WHERE collection_name IN ('information_unit','unit_version','tac_logical_unit','tac_unit_version','tac_publication','tac_publication_member')
GROUP BY collection_name
ORDER BY collection_name;" 2>/dev/null || echo "birth_registry=NOT_FOUND_OR_SCHEMA_DIFF"

echo "--- S5: entity_labels evidence for target IU/UV ---"
"${PSQL[@]}" -c "
SELECT count(*) AS labels_for_iu_uv
FROM public.entity_labels
WHERE entity_code LIKE 'information_unit::%'
   OR entity_code LIKE 'unit_version::%';" 2>/dev/null || echo "entity_labels=NOT_FOUND_OR_SCHEMA_DIFF"

6. Parent-child hierarchy discovery

echo "--- P1: TAC parent hierarchy discovered depth snapshot ---"
"${PSQL[@]}" <<'SQL'
WITH RECURSIVE tree AS (
  SELECT id, parent_id, canonical_address, 0 AS depth, ARRAY[id] AS path
  FROM public.tac_logical_unit
  WHERE parent_id IS NULL
  UNION ALL
  SELECT c.id, c.parent_id, c.canonical_address, t.depth + 1, t.path || c.id
  FROM public.tac_logical_unit c
  JOIN tree t ON c.parent_id = t.id
  WHERE NOT c.id = ANY(t.path)
), all_nodes AS (
  SELECT count(*) AS all_count FROM public.tac_logical_unit
)
SELECT max(depth) AS max_depth_snapshot,
       count(*) AS reachable_nodes,
       (SELECT all_count FROM all_nodes) AS all_nodes,
       count(*) FILTER (WHERE parent_id IS NULL) AS root_count,
       count(*) FILTER (WHERE parent_id IS NOT NULL) AS child_count
FROM tree;
SQL

echo "--- P2: TAC parent orphans/cycles indicators ---"
"${PSQL[@]}" -c "
SELECT count(*) AS orphan_parent_refs
FROM public.tac_logical_unit c
LEFT JOIN public.tac_logical_unit p ON p.id=c.parent_id
WHERE c.parent_id IS NOT NULL AND p.id IS NULL;"

"${PSQL[@]}" -c "SELECT parent_id IS NULL AS is_root, count(*) FROM public.tac_logical_unit GROUP BY (parent_id IS NULL) ORDER BY is_root;"

7. Edge discovery without assuming column names

echo "--- E1: universal_edges columns ---"
"${PSQL[@]}" -c "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema='public' AND table_name='universal_edges' ORDER BY ordinal_position;" 2>/dev/null || echo "universal_edges=NOT_FOUND"

echo "--- E2: universal_edges edge type distribution if edge_type exists ---"
"${PSQL[@]}" <<'SQL'
DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='universal_edges' AND column_name='edge_type') THEN
    RAISE NOTICE 'edge_type column exists';
  ELSE
    RAISE NOTICE 'edge_type column missing';
  END IF;
END $$;
SQL
"${PSQL[@]}" -c "SELECT edge_type, count(*) FROM public.universal_edges GROUP BY edge_type ORDER BY count(*) DESC;" 2>/dev/null || echo "edge_type_distribution=SKIPPED_SCHEMA_DIFF"

echo "--- E3: IU/UV reference search via jsonb text, read-only fallback ---"
"${PSQL[@]}" -c "
SELECT count(*) AS iu_uv_edges_text_search
FROM public.universal_edges
WHERE to_jsonb(universal_edges)::text ILIKE '%information_unit%'
   OR to_jsonb(universal_edges)::text ILIKE '%unit_version%';" 2>/dev/null || echo "universal_edges_text_search=SKIPPED_SCHEMA_DIFF"

8. Hash provenance target discovery

echo "--- H1: IU/UV columns relevant to source hash/provenance ---"
"${PSQL[@]}" -c "
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema='public'
  AND table_name IN ('information_unit','unit_version')
  AND (column_name ILIKE '%profile%'
       OR column_name ILIKE '%hash%'
       OR column_name ILIKE '%provenance%'
       OR column_name ILIKE '%source%'
       OR column_name ILIKE '%metadata%')
ORDER BY table_name, column_name;"

Report whether content_profile.source_hash is a real target, a design-only idea, or unavailable.


9. Required report

Upload to:

knowledge/dev/laws/dieu44-trien-khai/reports/p3d-pack1-phase4-governance-vocab-discovery-report.md

Report must include:

phase_status=PASS|PARTIAL|BLOCKED
mode=READ_ONLY_DISCOVERY
no_mutation_performed=true
no_hardcode_compliance=true|false
pg_readonly_confirmed=true|false
vocab_delta_summary=<summary from live discovery>
species_mapping_current_state=<summary>
collection_governance_current_state=<summary>
birth_registry_current_state=<summary>
parent_child_snapshot=<summary, mark counts as snapshot>
universal_edges_schema=<summary>
iu_uv_edge_current_state=<summary>
hash_provenance_target=<real_field|needs_design|unknown>
seed_candidates_from_live_delta=<list or none>
implementation_allowed=false
next_recommended_action=<specific>
log_path=<path>

Include all query results or concise tables derived from them. Any numeric count must be labelled as a live snapshot from this run, not a constant.


10. Final response expected from Agent

phase4_discovery_status=PASS|PARTIAL|BLOCKED
report_path=knowledge/dev/laws/dieu44-trien-khai/reports/p3d-pack1-phase4-governance-vocab-discovery-report.md
vocab_delta_summary=<short>
species_mapping_current_state=<short>
collection_governance_current_state=<short>
hash_provenance_target=<short>
no_mutation_performed=true
next_recommended_action=<short>

Return only the final response plus critical blocker notes.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/prompts/p3d-pack1-phase4-readonly-governance-vocab-discovery-prompt.md