fix(db): split inbound truth-chain hot lookup
Some checks failed
CD Pipeline / workflow-shape (push) Has been cancelled
CD Pipeline / cancel-stale-cd (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled

This commit is contained in:
Your Name
2026-07-01 08:45:40 +08:00
parent a6dc806d38
commit abc512a7b3
7 changed files with 245 additions and 65 deletions

View File

@@ -3,6 +3,8 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MIGRATION = "awooop_conversation_event_hot_path_indexes_2026-07-01.sql"
ROLLBACK = "awooop_conversation_event_hot_path_indexes_2026-07-01_down.sql"
CONTENT_PREVIEW_MIGRATION = "awooop_conversation_event_content_preview_trgm_2026-07-01.sql"
CONTENT_PREVIEW_ROLLBACK = "awooop_conversation_event_content_preview_trgm_2026-07-01_down.sql"
def _read(path: str) -> str:
@@ -17,6 +19,14 @@ def _rollback_sql() -> str:
return _read(f"migrations/{ROLLBACK}")
def _content_preview_migration_sql() -> str:
return _read(f"migrations/{CONTENT_PREVIEW_MIGRATION}")
def _content_preview_rollback_sql() -> str:
return _read(f"migrations/{CONTENT_PREVIEW_ROLLBACK}")
def test_hot_path_indexes_are_concurrent_and_transactionless() -> None:
sql = _migration_sql()
@@ -93,3 +103,25 @@ def test_rollback_only_drops_indexes_owned_by_hot_path_migration() -> None:
"idx_awooop_conv_event_source_refs_fingerprints_gin",
):
assert f"DROP INDEX CONCURRENTLY IF EXISTS {index_name};" in rollback
def test_content_preview_trgm_index_bounds_truth_chain_preview_fallback() -> None:
sql = _content_preview_migration_sql()
rollback = _content_preview_rollback_sql()
truth_chain_source = _read("src/services/awooop_truth_chain_service.py")
assert "BEGIN;" not in sql
assert "COMMIT;" not in sql
assert "CREATE EXTENSION IF NOT EXISTS pg_trgm" in sql
assert "CREATE EXTENSION IF NOT EXISTS btree_gin" in sql
assert "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_awooop_conv_event_content_preview_trgm" in sql
assert "USING gin (content_preview gin_trgm_ops)" in sql
assert "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_awooop_conv_event_project_content_preview_trgm" in sql
assert "USING gin (project_id, content_preview gin_trgm_ops)" in sql
assert "WHERE content_preview IS NOT NULL" in sql
assert "content_preview ILIKE :source_needle" in truth_chain_source
assert "content_preview ILIKE :fingerprint_needle" in truth_chain_source
assert "DROP INDEX CONCURRENTLY IF EXISTS idx_awooop_conv_event_content_preview_trgm;" in rollback
assert "DROP INDEX CONCURRENTLY IF EXISTS idx_awooop_conv_event_project_content_preview_trgm;" in rollback
assert "DROP EXTENSION" not in rollback

View File

@@ -38,6 +38,7 @@ from src.services.awooop_truth_chain_service import (
_automation_quality_score_bucket,
_clean_row,
_execution_backend_summary,
_fetch_inbound_conversation_event_rows,
_incident_fingerprints,
_summarize_gateway_mcp,
_truth_status,
@@ -131,13 +132,29 @@ def test_ansible_transport_cooldown_uses_asyncpg_safe_interval_parameter() -> No
def test_fetch_truth_chain_returns_inbound_redacted_envelope_fields() -> None:
source = inspect.getsource(fetch_truth_chain)
helper_source = inspect.getsource(_fetch_inbound_conversation_event_rows)
assert "content_redacted" in source
assert "source_envelope" in source
assert "source_refs,event_ids" in source
assert "source_refs,incident_ids" in source
assert "source_refs,sentry_issue_ids" in source
assert "source_refs,signoz_alerts" in source
assert "source_envelope" in helper_source
assert "source_refs,event_ids" in helper_source
assert "source_refs,incident_ids" in helper_source
assert "source_refs,sentry_issue_ids" in helper_source
assert "source_refs,signoz_alerts" in helper_source
def test_fetch_truth_chain_splits_inbound_lookup_into_index_friendly_branches() -> None:
source = inspect.getsource(fetch_truth_chain)
helper_source = inspect.getsource(_fetch_inbound_conversation_event_rows)
assert "await _fetch_inbound_conversation_event_rows(" in source
assert "run_id::text = :source_id" in helper_source
assert "provider_event_id = :source_id" in helper_source
assert "source_refs,event_ids" in helper_source
assert "source_refs,incident_ids" in helper_source
assert "content_preview ILIKE :source_needle" in helper_source
assert "len(rows_by_id) < branch_limit" in helper_source
assert "OR content_preview ILIKE :source_needle" not in helper_source
assert "OR coalesce(source_envelope #>" not in helper_source
def test_truth_status_marks_no_action_approval_as_manual_required() -> None: