-
Notifications
You must be signed in to change notification settings - Fork 1
fix(lineage): preserve grouping provenance and fail closed weights #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e4e5bf3
4b6062d
6751623
b8d9ce4
9d27e59
a2ae3c9
2aaac30
d242e08
3e28196
df8663e
65de9cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,15 +10,22 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
| import re | ||
| from collections.abc import Mapping | ||
| from datetime import datetime | ||
| from typing import Any, Mapping | ||
| from typing import Any | ||
|
|
||
| import asyncpg | ||
|
|
||
| from backend.app.post_eligibility import SOURCE_POST_ELIGIBILITY_SQL | ||
| from lineageweave.lineage_persistence import lineage_edge_specs | ||
| from lineageweave.models import Edge, Record | ||
|
|
||
| # ADR 0145 rejected the unanchored estimator. A future accepted ADR must add | ||
| # its independently validated method code here before persisted weights can run. | ||
| _SUPPORTED_ANCHOR_METHOD_CODES: frozenset[str] = frozenset() | ||
|
|
||
|
|
||
| def _occurred_at(value: datetime) -> datetime: | ||
| """Reconstruct expects naive datetimes; asyncpg returns timestamptz.""" | ||
|
|
@@ -72,14 +79,94 @@ async def persist_lineage_edges(conn: asyncpg.Connection, edges: list[Edge]) -> | |
| ) | ||
|
|
||
|
|
||
| async def load_estimated_channel_weights( | ||
| conn: asyncpg.Connection, active_channels: set[str] | ||
| ) -> dict[str, float] | None: | ||
| """Load only a complete vector from an independently anchored method. | ||
|
|
||
| ADR 0145 currently authorizes no anchor method. A partial or invalid vector | ||
| returns ``None`` rather than being repaired. A database that has not applied | ||
| migration 0135 is likewise an unavailable state, detected without issuing a | ||
| statement that would abort the caller's outer PostgreSQL transaction. | ||
| """ | ||
| table_exists = await conn.fetchval( | ||
| "select to_regclass('public.lineage_channel_weight') is not null" | ||
| ) | ||
| if not table_exists: | ||
| return None | ||
| rows = await conn.fetch( | ||
| "select channel_code, weight_value, estimation_run_id, " | ||
| "estimation_method_code, estimator_version, anchor_method_code, " | ||
| "source_snapshot_sha256, sample_pair_count, knowledge_cutoff " | ||
| "from lineage_channel_weight" | ||
| ) | ||
| persisted = {row["channel_code"]: float(row["weight_value"]) for row in rows} | ||
| if not persisted or set(persisted) != active_channels: | ||
| return None | ||
| if any( | ||
| not math.isfinite(weight) or weight <= 0 or weight > 1 | ||
| for weight in persisted.values() | ||
| ): | ||
| return None | ||
| if not math.isclose(sum(persisted.values()), 1.0, rel_tol=0.0, abs_tol=1e-9): | ||
| return None | ||
|
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Weight-sum tolerance stricter than schema allows Loading rejects a vector unless its sum is within Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| provenance = { | ||
| ( | ||
| row["estimation_run_id"], | ||
| row["estimation_method_code"], | ||
| row["estimator_version"], | ||
| row["anchor_method_code"], | ||
| row["source_snapshot_sha256"], | ||
| row["sample_pair_count"], | ||
| row["knowledge_cutoff"], | ||
| ) | ||
| for row in rows | ||
| } | ||
| if len(provenance) != 1: | ||
| return None | ||
| run = next(iter(provenance)) | ||
| ( | ||
| run_id, | ||
| estimation_method, | ||
| estimator_version, | ||
| anchor_method, | ||
| snapshot_digest, | ||
| sample_pair_count, | ||
| knowledge_cutoff, | ||
| ) = run | ||
| if ( | ||
| run_id is None | ||
| or not isinstance(estimation_method, str) | ||
| or not estimation_method.strip() | ||
| or not isinstance(estimator_version, str) | ||
| or not estimator_version.strip() | ||
| or not isinstance(anchor_method, str) | ||
| or anchor_method not in _SUPPORTED_ANCHOR_METHOD_CODES | ||
| or not isinstance(snapshot_digest, str) | ||
| or re.fullmatch(r"[0-9a-f]{64}", snapshot_digest) is None | ||
| or not isinstance(sample_pair_count, int) | ||
| or isinstance(sample_pair_count, bool) | ||
| or sample_pair_count < 200 | ||
| or not isinstance(knowledge_cutoff, datetime) | ||
| ): | ||
| return None | ||
| return persisted | ||
|
seonghobae marked this conversation as resolved.
seonghobae marked this conversation as resolved.
|
||
|
|
||
|
|
||
| async def rebuild_lineage(conn: asyncpg.Connection) -> list[Edge]: | ||
| """Reconstruct lineage for every ``source_post`` and persist the edges.""" | ||
| rows = await conn.fetch( | ||
| "select post_id, post_title, voc_type_code, created_at, corporate_entity_id, " | ||
| "process_unit_id, thread_group_key, secondary_grouping_key " | ||
| f"from source_post where {SOURCE_POST_ELIGIBILITY_SQL.format(alias='source_post')}" | ||
| ) | ||
| edges = lineage_edge_specs(records_from_source_posts(rows)) | ||
| # No adjudication client is wired on this path, so the active channel | ||
| # set is the three deterministic channels (reconstruct drops llm when | ||
| # unavailable rather than faking it). | ||
| weights = await load_estimated_channel_weights( | ||
| conn, {"temporal", "secondary_key", "text"} | ||
| ) | ||
|
seonghobae marked this conversation as resolved.
|
||
| edges = lineage_edge_specs(records_from_source_posts(rows), weights=weights) | ||
| await persist_lineage_edges(conn, edges) | ||
| return edges | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # ADR 0145 — Channel-weight estimation remains unavailable without an independent anchor | ||
|
|
||
| **Decision status:** Rejected proposal | ||
| **Date:** 2026-08-23 | ||
| **Reconciles with:** [ADR 0003](0003-fast-mlsirm-report-integration.md) | ||
|
|
||
| ## Context | ||
|
|
||
| `lineageweave.reconstruct` currently uses hand-picked convex weights for its | ||
| temporal, secondary-key, text-similarity, and optional LLM channels. Those | ||
| constants are an explicitly ungrounded historical fallback; a citation does | ||
| not turn them into calibrated measurement. | ||
|
|
||
| The rejected proposal treated channels as 2PL items, candidate pairs as | ||
| respondents, and normalized item discriminations as fusion weights. That does | ||
| not establish the product construct. An unanchored IRT fit can describe common | ||
| response structure, but it provides no independent evidence that its latent | ||
| factor is “these posts are genuinely related.” Birnbaum's 2PL item information | ||
| is also conditional on trait location and item difficulty, | ||
| `I_j(theta) = a_j^2 P_j(theta) (1 - P_j(theta))`; it is not a global constant | ||
| proportional only to `a_j`. Normalizing discriminations therefore is not an | ||
| information-optimal convex fusion rule. | ||
|
|
||
| The official pinned `fast-mlsirm` contract makes two further boundaries | ||
| explicit: `factor_id` assigns items to latent dimensions, while `cluster_id` | ||
| represents respondent nesting; and a `FitResult` exposes | ||
| `convergence_status` plus package diagnostics that callers must inspect. Those | ||
| contracts can validate a fit's execution, but cannot supply the missing | ||
| criterion validity. | ||
|
|
||
| Accepted ADR 0003 assigns temporal/event measurement to TEPP and limits this | ||
| repository's fast-mlsirm integration to the approved LLM-judge/report path. | ||
| This proposed lineage-weight path cannot silently expand that boundary. | ||
|
|
||
| ## Decision | ||
|
|
||
| 1. **No unanchored estimate.** LineageWeave does not run the proposed | ||
| candidate-pair IRT fit and does not persist or activate weights from it. | ||
| Estimation reports unavailable until an independent lineage anchor and its | ||
| upstream contract exist. | ||
| 2. **No scientific claim for fallback constants.** Existing constants remain | ||
| unchanged for compatibility, but are neither calibrated nor | ||
| paper-grounded. This ADR does not promote them to measurement evidence. | ||
| 3. **A future proposal must be ADR-first.** It must amend ADR 0003, identify an | ||
| independent outcome/anchor (for example, an accepted TEPP contract rather | ||
| than a local proxy), use official fast-mlsirm diagnostics, reject every | ||
| non-converged fit, and prove criterion validity before product activation. | ||
| 4. **Future persisted vectors must be self-consistent and reproducible.** The | ||
| schema requires a known channel vocabulary, finite positive weights, an | ||
| exact sum of one at runtime, one estimation run identity, estimator and | ||
| anchor method versions, sample size, immutable source-snapshot digest, and | ||
| knowledge cutoff. Current code has no supported anchor method and therefore | ||
| loads no vector. | ||
| 5. **Grouping repair is independent of measurement.** Source row identifiers | ||
| that were mapped as grouping values are normalized only in derived | ||
| reconstruction fields. Their caller-mapped raw values remain preserved in | ||
| separate source-provenance columns across backfill and re-import. | ||
|
|
||
| ## Consequences | ||
|
|
||
| - `scripts/estimate_channel_weights.py` exits without writing because no | ||
| scientifically authorized anchor exists. | ||
| - Missing migration 0135 remains a normal rollout state and is detected via a | ||
| non-error PostgreSQL catalog probe, so an outer rebuild transaction is not | ||
| aborted. | ||
| - The persistence contract is fail-closed: malformed, mixed-provenance, or | ||
| unsupported-anchor rows are ignored rather than renormalized or repaired. | ||
| - Parameter-recovery tests cannot substitute for criterion validity; they may | ||
| return only with a future accepted anchored estimator. | ||
|
|
||
| ## References | ||
|
|
||
| Birnbaum, A. (1968). Some latent trait models and their use in inferring an | ||
| examinee's ability. In F. M. Lord & M. R. Novick, *Statistical theories of | ||
| mental test scores* (pp. 397–479). Addison-Wesley. | ||
|
|
||
| Fox, J.-P., & Glas, C. A. W. (2001). Bayesian estimation of a multilevel IRT | ||
| model using Gibbs sampling. *Psychometrika, 66*(2), 271–288. | ||
| https://doi.org/10.1007/BF02294839 | ||
|
|
||
| McNeish, D., & Wolf, M. G. (2020). Thinking twice about sum scores. | ||
| *Behavior Research Methods, 52*(6), 2287–2305. | ||
| https://doi.org/10.3758/s13428-020-01398-0 | ||
|
|
||
| ContextualWisdomLab. (2026). *fast-mlsirm*, pinned LineageWeave dependency | ||
| contract at commit `5006c38286a4fa1d81bcf57eeed5ce27ae743f50`. | ||
| https://github.com/ContextualWisdomLab/fast-mlsirm/tree/5006c38286a4fa1d81bcf57eeed5ce27ae743f50 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Fail-closed lineage channel-weight boundary (ADR 0145). | ||
|
|
||
| Channel-score covariance is not an independent lineage anchor. Until an | ||
| accepted upstream anchor contract exists, this module produces no estimate. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def estimate_channel_weights( | ||
| pair_channel_scores: list[dict[str, float]], | ||
| group_ids: list[int], | ||
| ) -> None: | ||
| """Report unavailable for unanchored channel scores. | ||
|
|
||
| Args: | ||
| pair_channel_scores: unanchored candidate-pair channel scores. | ||
| group_ids: corresponding reconstruction-group indexes. | ||
|
|
||
| Returns: | ||
| Always ``None`` until ADR 0145's independent-anchor requirement is met. | ||
| """ | ||
| if len(pair_channel_scores) != len(group_ids): | ||
| raise ValueError("pair_channel_scores and group_ids must align") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| -- ADR 0145: reserve an integrity- and provenance-bearing persistence contract. | ||
| -- No anchor method is currently authorized, so application code activates no | ||
| -- stored vector. | ||
|
|
||
| alter table source_post | ||
| add column if not exists source_thread_group_key text, | ||
| add column if not exists source_secondary_grouping_key text; | ||
|
|
||
| comment on column source_post.source_thread_group_key is | ||
| 'Raw caller-mapped source thread field; preserved separately from derived reconstruction grouping.'; | ||
| comment on column source_post.source_secondary_grouping_key is | ||
| 'Raw caller-mapped source secondary-group field; preserved separately from derived reconstruction evidence.'; | ||
|
|
||
| create table if not exists lineage_channel_weight ( | ||
| channel_code text primary key, | ||
| weight_value double precision not null, | ||
| estimation_run_id uuid not null, | ||
| estimation_method_code text not null, | ||
| estimator_version text not null, | ||
| anchor_method_code text not null, | ||
| source_snapshot_sha256 text not null, | ||
| sample_pair_count bigint not null, | ||
| knowledge_cutoff timestamptz not null, | ||
| estimated_at timestamptz not null default now(), | ||
| constraint lineage_channel_code_check | ||
| check (channel_code in ('temporal', 'secondary_key', 'text', 'llm')), | ||
| constraint lineage_weight_value_check | ||
| check (weight_value > 0 and weight_value <= 1), | ||
| constraint lineage_estimation_method_check | ||
| check (btrim(estimation_method_code) <> ''), | ||
| constraint lineage_estimator_version_check | ||
| check (btrim(estimator_version) <> ''), | ||
| constraint lineage_anchor_method_check | ||
| check (btrim(anchor_method_code) <> ''), | ||
| constraint lineage_sample_pair_count_check | ||
| check (sample_pair_count >= 200), | ||
| constraint lineage_source_snapshot_check | ||
| check (source_snapshot_sha256 ~ '^[0-9a-f]{64}$'), | ||
| constraint lineage_knowledge_cutoff_check | ||
| check (knowledge_cutoff <= estimated_at) | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| -- Rollback for 0135: remove the unavailable channel-weight persistence | ||
| -- contract and the raw grouping provenance columns introduced with it. | ||
|
|
||
| drop table if exists lineage_channel_weight; | ||
|
|
||
| alter table source_post | ||
| drop column if exists source_secondary_grouping_key, | ||
| drop column if exists source_thread_group_key; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Catalog probe correctly avoids transaction abort
The
to_regclassprobe returns None before anyselect ... from lineage_channel_weight, so a database missing migration 0135 does not raise undefined_table and abort the surrounding rebuild transaction. The fetch runs only after the table is confirmed present.Was this helpful? React with 👍 or 👎 to provide feedback.