T-151: M2.1i — findings + provider_caveats append-only PostgreSQL migration (research track, 37-research-findings.sql) with fresh-database schema tests (no adapters, no new deps)

This commit is contained in:
Studio 2026-12-02 00:12:13 -05:00
parent 34fdfd6bc4
commit c16d6b3f60
7 changed files with 672 additions and 11 deletions

View file

@ -194,6 +194,65 @@ The M1 exit gate evaluates **gate-eligible US/CA common equities & ETFs on EODHD
2. `uncovered_venue` — listings on exchanges absent from the EODHD bar master (`XCNQ` Canadian Securities Exchange across `qm`, `ceo`, and `ib:AEQLIT`, plus `ceo:NEOE` where EODHD carries only 19 of 587 listings).
3. `non_equity_instrument` — non-common-equity instruments & test tickers (warrants `.WT`/`.WS`, convertible debentures `.DB`, subscription receipts/rights `.R`/`.RT`, SPAC/REIT units `.U`/`.UN`, preferred shares `.PR*`/`-P*`, TSXV NEX inactive shells `.H`, TSXV CPCs `.P`, and exchange test symbols).
### Research Knowledge & Findings Schema (PostgreSQL, Append-Only)
Applied via `@stock-bot/postgres` (`research` migration track, `database/postgres/init/37-research-findings.sql`, immediately after `36-trial-ledger.sql`). Owned by Stock Bot research alongside `trial_ledger` and `vault_evaluations` (see [`design/VALIDATION-AND-RIGOR.md`](VALIDATION-AND-RIGOR.md) §2A and §3 for those tables).
- **Research-visible boundary:** `findings` and `provider_caveats` store research-visible empirical evidence only. Callers and MCP adapters (`file_finding`, `read_knowledge`) are responsible for verifying that every `trial_ref` belongs to the research-visible `trial_ledger` and for excluding Vault holdout observations or Vault-only evidence from stored text and opaque `evidence_refs`.
- **Append-only enforcement:** Both tables reuse `reject_research_evidence_mutation()` from `17-research-retention.sql` via row-level `BEFORE UPDATE OR DELETE` (`findings_retained`, `provider_caveats_retained`) and statement-level `BEFORE TRUNCATE` (`findings_no_truncate`, `provider_caveats_no_truncate`) triggers, raising SQLSTATE `23514` on any mutation attempt.
- **Character-length convention:** SQL `CHECK` constraints enforce PostgreSQL `char_length(...)` (Unicode code points) matching the MCP tool caps (`title` trimmed `1..256`, `summary` trimmed `1..4096`, each ref `<= 256`, `idempotency_key` `1..128`). JavaScript/Zod validation counts UTF-16 code units, which can reach the same numeric limit sooner for supplementary Unicode characters.
```sql
CREATE TABLE IF NOT EXISTS findings (
id TEXT PRIMARY KEY
CHECK (id ~ '\S'),
kind TEXT NOT NULL
CHECK (kind IN ('finding', 'dead_end')),
title TEXT NOT NULL
CHECK (char_length(regexp_replace(title, '^\s+|\s+$', '', 'g')) BETWEEN 1 AND 256),
summary TEXT NOT NULL
CHECK (char_length(regexp_replace(summary, '^\s+|\s+$', '', 'g')) BETWEEN 1 AND 4096),
status TEXT NOT NULL DEFAULT 'pending_review'
CHECK (status ~ '\S'),
created_at TIMESTAMPTZ NOT NULL,
trial_refs TEXT[] NOT NULL
CHECK (cardinality(trial_refs) <= 32)
CHECK (COALESCE(array_ndims(trial_refs), 1) = 1)
CHECK (research_finding_refs_valid(trial_refs, 256)),
evidence_refs TEXT[] NOT NULL
CHECK (cardinality(evidence_refs) <= 32)
CHECK (COALESCE(array_ndims(evidence_refs), 1) = 1)
CHECK (research_finding_refs_valid(evidence_refs, 256)),
idempotency_key TEXT
CHECK (idempotency_key IS NULL OR (idempotency_key ~ '\S' AND char_length(idempotency_key) BETWEEN 1 AND 128))
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_findings_idempotency_key
ON findings (idempotency_key)
WHERE idempotency_key IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_findings_created_at
ON findings (created_at);
CREATE TABLE IF NOT EXISTS provider_caveats (
id TEXT PRIMARY KEY
CHECK (id ~ '\S'),
provider TEXT NOT NULL
CHECK (provider ~ '\S'),
dataset TEXT NOT NULL
CHECK (dataset ~ '\S'),
caveat TEXT NOT NULL
CHECK (caveat ~ '\S'),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_provider_caveats_lower_provider
ON provider_caveats (lower(provider));
```
- **Reference arrays (`trial_refs`, `evidence_refs`):** Stored as opaque 1-dimensional `TEXT[]` arrays (empty arrays permitted via `COALESCE(array_ndims(...), 1) = 1`, max 32 entries each, no `NULL` elements, max 256 chars per entry enforced via `research_finding_refs_valid`). Reference normalization, deduplication, minimum `trial_refs` count, and research-visibility checks remain in the MCP tool/adapter layer.
- **Indexes:** `idx_findings_idempotency_key` (partial unique `WHERE idempotency_key IS NOT NULL`) supports atomic compare-and-append idempotency while allowing multiple `NULL` keys; `idx_findings_created_at` supports chronological listing; `idx_provider_caveats_lower_provider` supports case-insensitive provider filtering in `read_knowledge`.
---
## 3. Curated Storage Schemas