pythia-knowledge 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pythia/__init__.py +3 -0
- pythia/cli.py +246 -0
- pythia/connectors/__init__.py +0 -0
- pythia/connectors/claude_code.py +467 -0
- pythia/connectors/common.py +99 -0
- pythia/connectors/confluence.py +803 -0
- pythia/connectors/github.py +444 -0
- pythia/connectors/incident_io.py +365 -0
- pythia/connectors/slack.py +674 -0
- pythia/consumer.py +268 -0
- pythia/daemon.py +650 -0
- pythia/db.py +80 -0
- pythia/docker-compose.yml +15 -0
- pythia/embed.py +39 -0
- pythia/events.py +339 -0
- pythia/evolution.py +392 -0
- pythia/ids.py +12 -0
- pythia/jobs.py +103 -0
- pythia/knowledge.py +494 -0
- pythia/llm/__init__.py +0 -0
- pythia/llm/client.py +328 -0
- pythia/mcp_server.py +73 -0
- pythia/migrations/001_schema.sql +536 -0
- pythia/migrations/__init__.py +0 -0
- pythia/pipeline/__init__.py +0 -0
- pythia/pipeline/adjudicate.py +297 -0
- pythia/pipeline/batch.py +254 -0
- pythia/pipeline/correct.py +227 -0
- pythia/pipeline/derive.py +317 -0
- pythia/pipeline/live.py +149 -0
- pythia/pipeline/process.py +472 -0
- pythia/pipeline/prompts.py +260 -0
- pythia/pipeline/reconcile.py +168 -0
- pythia/pipeline/schemas.py +154 -0
- pythia/pipeline/sweep.py +155 -0
- pythia/quality.py +359 -0
- pythia/reexplain.py +248 -0
- pythia/retrieval.py +137 -0
- pythia/services.py +41 -0
- pythia/settings.py +194 -0
- pythia/usage.py +135 -0
- pythia/value.py +265 -0
- pythia/vault.py +31 -0
- pythia/web/__init__.py +0 -0
- pythia/web/app.py +779 -0
- pythia/web/graph.py +167 -0
- pythia/web/processing.py +131 -0
- pythia/web/static/explore.js +242 -0
- pythia/web/static/style.css +120 -0
- pythia/web/templates/_now.html +25 -0
- pythia/web/templates/base.html +30 -0
- pythia/web/templates/config.html +231 -0
- pythia/web/templates/costs.html +30 -0
- pythia/web/templates/evolution.html +153 -0
- pythia/web/templates/explore.html +36 -0
- pythia/web/templates/label.html +22 -0
- pythia/web/templates/quality.html +141 -0
- pythia/web/templates/sources.html +249 -0
- pythia/web/templates/usage.html +33 -0
- pythia_knowledge-1.0.0.dist-info/METADATA +197 -0
- pythia_knowledge-1.0.0.dist-info/RECORD +64 -0
- pythia_knowledge-1.0.0.dist-info/WHEEL +4 -0
- pythia_knowledge-1.0.0.dist-info/entry_points.txt +2 -0
- pythia_knowledge-1.0.0.dist-info/licenses/LICENSE +21 -0
pythia/db.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Postgres connection and migrations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
from importlib import resources
|
|
8
|
+
from typing import Any, Iterator
|
|
9
|
+
|
|
10
|
+
import psycopg
|
|
11
|
+
from pgvector.psycopg import register_vector
|
|
12
|
+
from psycopg.rows import dict_row
|
|
13
|
+
from psycopg_pool import ConnectionPool
|
|
14
|
+
|
|
15
|
+
DEFAULT_URL = "postgresql://pythia:pythia@127.0.0.1:54329/pythia"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def database_url() -> str:
|
|
19
|
+
return os.environ.get("PYTHIA_DATABASE_URL", DEFAULT_URL)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _configure(conn: psycopg.Connection) -> None:
|
|
23
|
+
register_vector(conn)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Database:
|
|
27
|
+
def __init__(self, url: str | None = None, min_size: int = 1, max_size: int = 10):
|
|
28
|
+
self.url = url or database_url()
|
|
29
|
+
self.pool = ConnectionPool(
|
|
30
|
+
self.url,
|
|
31
|
+
min_size=min_size,
|
|
32
|
+
max_size=max_size,
|
|
33
|
+
kwargs={"row_factory": dict_row, "autocommit": False},
|
|
34
|
+
configure=_configure,
|
|
35
|
+
open=False,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def open(self) -> None:
|
|
39
|
+
# The vector extension must exist before register_vector.
|
|
40
|
+
with psycopg.connect(self.url, autocommit=True) as conn:
|
|
41
|
+
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
|
|
42
|
+
self.pool.open(wait=True)
|
|
43
|
+
self.migrate()
|
|
44
|
+
|
|
45
|
+
def close(self) -> None:
|
|
46
|
+
self.pool.close()
|
|
47
|
+
|
|
48
|
+
@contextmanager
|
|
49
|
+
def tx(self) -> Iterator[psycopg.Connection]:
|
|
50
|
+
"""Connection in a transaction; commits on a clean exit."""
|
|
51
|
+
with self.pool.connection() as conn:
|
|
52
|
+
with conn.transaction():
|
|
53
|
+
yield conn
|
|
54
|
+
|
|
55
|
+
def query(self, sql: str, params: Any = None) -> list[dict]:
|
|
56
|
+
with self.tx() as conn:
|
|
57
|
+
return conn.execute(sql, params).fetchall()
|
|
58
|
+
|
|
59
|
+
def one(self, sql: str, params: Any = None) -> dict | None:
|
|
60
|
+
with self.tx() as conn:
|
|
61
|
+
return conn.execute(sql, params).fetchone()
|
|
62
|
+
|
|
63
|
+
def execute(self, sql: str, params: Any = None) -> None:
|
|
64
|
+
with self.tx() as conn:
|
|
65
|
+
conn.execute(sql, params)
|
|
66
|
+
|
|
67
|
+
def migrate(self) -> None:
|
|
68
|
+
files = sorted(
|
|
69
|
+
f for f in resources.files("pythia.migrations").iterdir() if f.name.endswith(".sql")
|
|
70
|
+
)
|
|
71
|
+
with self.tx() as conn:
|
|
72
|
+
conn.execute(
|
|
73
|
+
"CREATE TABLE IF NOT EXISTS schema_migrations (name text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())"
|
|
74
|
+
)
|
|
75
|
+
done = {r["name"] for r in conn.execute("SELECT name FROM schema_migrations").fetchall()}
|
|
76
|
+
for f in files:
|
|
77
|
+
if f.name in done:
|
|
78
|
+
continue
|
|
79
|
+
conn.execute(f.read_text())
|
|
80
|
+
conn.execute("INSERT INTO schema_migrations (name) VALUES (%s)", (f.name,))
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
services:
|
|
2
|
+
db:
|
|
3
|
+
image: pgvector/pgvector:pg17
|
|
4
|
+
container_name: pythia-db
|
|
5
|
+
environment:
|
|
6
|
+
POSTGRES_USER: pythia
|
|
7
|
+
POSTGRES_PASSWORD: pythia
|
|
8
|
+
POSTGRES_DB: pythia
|
|
9
|
+
ports:
|
|
10
|
+
- "127.0.0.1:54329:5432"
|
|
11
|
+
volumes:
|
|
12
|
+
- pythia-db:/var/lib/postgresql/data
|
|
13
|
+
restart: unless-stopped
|
|
14
|
+
volumes:
|
|
15
|
+
pythia-db:
|
pythia/embed.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Local embeddings for search (plumbing: decides what the LLM sees, never what it means)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
from typing import Protocol
|
|
7
|
+
|
|
8
|
+
DIM = 384
|
|
9
|
+
MODEL = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Embedder(Protocol):
|
|
13
|
+
def embed(self, texts: list[str]) -> list[list[float]]: ...
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class LocalEmbedder:
|
|
17
|
+
"""Small multilingual model (ONNX), loaded on demand and reused."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, model: str = MODEL):
|
|
20
|
+
self.model_name = model
|
|
21
|
+
self._model = None
|
|
22
|
+
self._lock = threading.Lock()
|
|
23
|
+
|
|
24
|
+
def _load(self):
|
|
25
|
+
with self._lock:
|
|
26
|
+
if self._model is None:
|
|
27
|
+
from fastembed import TextEmbedding
|
|
28
|
+
|
|
29
|
+
self._model = TextEmbedding(self.model_name)
|
|
30
|
+
return self._model
|
|
31
|
+
|
|
32
|
+
def embed(self, texts: list[str]) -> list[list[float]]:
|
|
33
|
+
if not texts:
|
|
34
|
+
return []
|
|
35
|
+
model = self._load()
|
|
36
|
+
return [list(map(float, v)) for v in model.embed([t[:2000] for t in texts])]
|
|
37
|
+
|
|
38
|
+
def embed_one(self, text: str) -> list[float]:
|
|
39
|
+
return self.embed([text])[0]
|
pythia/events.py
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"""Event sourcing: append-only log + projections applied in the same transaction.
|
|
2
|
+
|
|
3
|
+
All knowledge state (entities, mentions, claims, tensions, verdicts) is a
|
|
4
|
+
projection of the `events` log. Reverting means writing a compensating event.
|
|
5
|
+
`rebuild` rebuilds the projections from scratch out of the log.
|
|
6
|
+
|
|
7
|
+
Evidence content does NOT go into the log: it stays in the `evidence` table, so
|
|
8
|
+
that an edit/deletion at the source really erases the content. Text *derived*
|
|
9
|
+
from a piece of evidence (mention context, statement of a claim that only it
|
|
10
|
+
supported) is redacted in the log itself by `redact` — the only exception to
|
|
11
|
+
append-only, because erasing at the source must erase here too. `rebuild` keeps
|
|
12
|
+
reproducing the same state, since the projection reads the redacted payload.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from contextvars import ContextVar
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
from typing import Any, Callable
|
|
20
|
+
|
|
21
|
+
import psycopg
|
|
22
|
+
from psycopg.types.json import Jsonb
|
|
23
|
+
|
|
24
|
+
REDACTED = "[removed at the source]"
|
|
25
|
+
|
|
26
|
+
PROJECTION_TABLES = [
|
|
27
|
+
"entities",
|
|
28
|
+
"mentions",
|
|
29
|
+
"mention_link_history",
|
|
30
|
+
"claims",
|
|
31
|
+
"claim_status_history",
|
|
32
|
+
"tensions",
|
|
33
|
+
"verdicts",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
_APPLY: dict[str, Callable[[psycopg.Connection, dict, datetime, int], None]] = {}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _handler(event_type: str):
|
|
40
|
+
def register(fn):
|
|
41
|
+
_APPLY[event_type] = fn
|
|
42
|
+
return fn
|
|
43
|
+
|
|
44
|
+
return register
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# What caused the changes in progress (unit:<id>, tension:<id>, correction:<id>…), for Evolution.
|
|
48
|
+
# Set by whoever runs the work (the job); it is event metadata, not part of the payload.
|
|
49
|
+
CAUSE: ContextVar[str | None] = ContextVar("pythia_event_cause", default=None)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def append(conn: psycopg.Connection, event_type: str, actor: str, payload: dict[str, Any]) -> int:
|
|
53
|
+
"""Writes the event and applies the projection. Returns the event id."""
|
|
54
|
+
if event_type not in _APPLY:
|
|
55
|
+
raise ValueError(f"unknown event type: {event_type}")
|
|
56
|
+
row = conn.execute(
|
|
57
|
+
"INSERT INTO events (type, actor, payload, cause) VALUES (%s, %s, %s, %s) RETURNING id, ts",
|
|
58
|
+
(event_type, actor, Jsonb(payload), CAUSE.get()),
|
|
59
|
+
).fetchone()
|
|
60
|
+
_APPLY[event_type](conn, payload, row["ts"], row["id"])
|
|
61
|
+
return row["id"]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def redact(conn: psycopg.Connection, event_type: str, match: dict[str, str], fields: list[str]) -> int:
|
|
65
|
+
"""Replaces text fields in the payloads matching `match` (e.g. {"id": "cl_x"})."""
|
|
66
|
+
where = " AND ".join(f"payload->>'{k}' = %s" for k in match)
|
|
67
|
+
sets = "payload"
|
|
68
|
+
params: list = []
|
|
69
|
+
for f in fields:
|
|
70
|
+
sets = f"jsonb_set({sets}, '{{{f}}}', to_jsonb(%s::text))"
|
|
71
|
+
params.append(REDACTED)
|
|
72
|
+
cur = conn.execute(f"UPDATE events SET payload = {sets} WHERE type = %s AND {where}",
|
|
73
|
+
(*params, event_type, *match.values()))
|
|
74
|
+
return cur.rowcount
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def rebuild(conn: psycopg.Connection) -> int:
|
|
78
|
+
"""Wipes the projections and replays the whole log. Returns how many events were applied."""
|
|
79
|
+
for table in PROJECTION_TABLES:
|
|
80
|
+
conn.execute(f"TRUNCATE {table}")
|
|
81
|
+
count = 0
|
|
82
|
+
cur = conn.execute("SELECT id, ts, type, payload FROM events ORDER BY id")
|
|
83
|
+
for ev in cur.fetchall():
|
|
84
|
+
_APPLY[ev["type"]](conn, ev["payload"], ev["ts"], ev["id"])
|
|
85
|
+
count += 1
|
|
86
|
+
return count
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------- entities
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@_handler("entity_created")
|
|
93
|
+
def _entity_created(conn, p, ts, eid):
|
|
94
|
+
conn.execute(
|
|
95
|
+
"""INSERT INTO entities (id, type, name, aliases, external_ids, description, origin, is_owner, created_at, updated_at)
|
|
96
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
|
|
97
|
+
(
|
|
98
|
+
p["id"],
|
|
99
|
+
p["type"],
|
|
100
|
+
p["name"],
|
|
101
|
+
p.get("aliases", []),
|
|
102
|
+
Jsonb(p.get("external_ids", {})),
|
|
103
|
+
p.get("description"),
|
|
104
|
+
p["origin"],
|
|
105
|
+
p.get("is_owner", False),
|
|
106
|
+
ts,
|
|
107
|
+
ts,
|
|
108
|
+
),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@_handler("entity_updated")
|
|
113
|
+
def _entity_updated(conn, p, ts, eid):
|
|
114
|
+
fields = {k: p[k] for k in ("type", "name", "aliases", "description") if k in p}
|
|
115
|
+
if "external_ids" in p:
|
|
116
|
+
fields["external_ids"] = Jsonb(p["external_ids"])
|
|
117
|
+
if not fields:
|
|
118
|
+
return
|
|
119
|
+
sets = ", ".join(f"{k} = %s" for k in fields)
|
|
120
|
+
conn.execute(
|
|
121
|
+
f"UPDATE entities SET {sets}, updated_at = %s WHERE id = %s",
|
|
122
|
+
(*fields.values(), ts, p["id"]),
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@_handler("entity_merged")
|
|
127
|
+
def _entity_merged(conn, p, ts, eid):
|
|
128
|
+
conn.execute(
|
|
129
|
+
"UPDATE entities SET status = 'merged', merged_into = %s, updated_at = %s WHERE id = %s",
|
|
130
|
+
(p["into"], ts, p["id"]),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@_handler("entity_unmerged")
|
|
135
|
+
def _entity_unmerged(conn, p, ts, eid):
|
|
136
|
+
conn.execute(
|
|
137
|
+
"UPDATE entities SET status = 'active', merged_into = NULL, updated_at = %s WHERE id = %s",
|
|
138
|
+
(ts, p["id"]),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# ---------------------------------------------------------------- mentions
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@_handler("mention_recorded")
|
|
146
|
+
def _mention_recorded(conn, p, ts, eid):
|
|
147
|
+
conn.execute(
|
|
148
|
+
"""INSERT INTO mentions (id, evidence_id, unit_id, surface, context, type_hint, entity_id,
|
|
149
|
+
confidence, resolved_by, candidates, created_at)
|
|
150
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
|
|
151
|
+
(
|
|
152
|
+
p["id"],
|
|
153
|
+
p.get("evidence_id"),
|
|
154
|
+
p.get("unit_id"),
|
|
155
|
+
p["surface"],
|
|
156
|
+
p.get("context"),
|
|
157
|
+
p.get("type_hint"),
|
|
158
|
+
p.get("entity_id"),
|
|
159
|
+
p.get("confidence"),
|
|
160
|
+
p.get("resolved_by"),
|
|
161
|
+
Jsonb(p.get("candidates", [])),
|
|
162
|
+
ts,
|
|
163
|
+
),
|
|
164
|
+
)
|
|
165
|
+
conn.execute(
|
|
166
|
+
"INSERT INTO mention_link_history (mention_id, entity_id, from_ts, event_id) VALUES (%s, %s, %s, %s)",
|
|
167
|
+
(p["id"], p.get("entity_id"), ts, eid),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@_handler("mention_relinked")
|
|
172
|
+
def _mention_relinked(conn, p, ts, eid):
|
|
173
|
+
mid, to = p["id"], p.get("to")
|
|
174
|
+
conn.execute(
|
|
175
|
+
"UPDATE mentions SET entity_id = %s, resolved_by = %s, confidence = %s WHERE id = %s",
|
|
176
|
+
(to, p.get("by"), p.get("confidence"), mid),
|
|
177
|
+
)
|
|
178
|
+
conn.execute(
|
|
179
|
+
"UPDATE mention_link_history SET to_ts = %s WHERE mention_id = %s AND to_ts IS NULL",
|
|
180
|
+
(ts, mid),
|
|
181
|
+
)
|
|
182
|
+
conn.execute(
|
|
183
|
+
"INSERT INTO mention_link_history (mention_id, entity_id, from_ts, event_id) VALUES (%s, %s, %s, %s)",
|
|
184
|
+
(mid, to, ts, eid),
|
|
185
|
+
)
|
|
186
|
+
conn.execute("UPDATE claims SET subject_entity_id = %s WHERE subject_mention_id = %s", (to, mid))
|
|
187
|
+
conn.execute("UPDATE claims SET object_entity_id = %s WHERE object_mention_id = %s", (to, mid))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# ---------------------------------------------------------------- claims
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _set_claim_status(conn, claim_id: str, status: str, ts, eid: int, verdict_id: str | None) -> None:
|
|
194
|
+
conn.execute("UPDATE claims SET status = %s WHERE id = %s", (status, claim_id))
|
|
195
|
+
conn.execute(
|
|
196
|
+
"UPDATE claim_status_history SET to_ts = %s WHERE claim_id = %s AND to_ts IS NULL",
|
|
197
|
+
(ts, claim_id),
|
|
198
|
+
)
|
|
199
|
+
conn.execute(
|
|
200
|
+
"""INSERT INTO claim_status_history (claim_id, status, from_ts, event_id, verdict_id)
|
|
201
|
+
VALUES (%s, %s, %s, %s, %s)""",
|
|
202
|
+
(claim_id, status, ts, eid, verdict_id),
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@_handler("claim_asserted")
|
|
207
|
+
def _claim_asserted(conn, p, ts, eid):
|
|
208
|
+
conn.execute(
|
|
209
|
+
"""INSERT INTO claims (id, kind, subject_mention_id, subject_entity_id, predicate, object_value,
|
|
210
|
+
object_mention_id, object_entity_id, statement, valid_from, valid_to,
|
|
211
|
+
recorded_at, origin, source_id, source_label, status, confidence,
|
|
212
|
+
extractor_version, evidence_ids, supporting_claim_ids)
|
|
213
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
|
|
214
|
+
(
|
|
215
|
+
p["id"],
|
|
216
|
+
p["kind"],
|
|
217
|
+
p.get("subject_mention_id"),
|
|
218
|
+
p.get("subject_entity_id"),
|
|
219
|
+
p["predicate"],
|
|
220
|
+
p.get("object_value"),
|
|
221
|
+
p.get("object_mention_id"),
|
|
222
|
+
p.get("object_entity_id"),
|
|
223
|
+
p["statement"],
|
|
224
|
+
p.get("valid_from"),
|
|
225
|
+
p.get("valid_to"),
|
|
226
|
+
ts,
|
|
227
|
+
p["origin"],
|
|
228
|
+
p.get("source_id"),
|
|
229
|
+
p.get("source_label"),
|
|
230
|
+
p.get("status", "current"),
|
|
231
|
+
p.get("confidence"),
|
|
232
|
+
p.get("extractor_version"),
|
|
233
|
+
p.get("evidence_ids", []),
|
|
234
|
+
p.get("supporting_claim_ids", []),
|
|
235
|
+
),
|
|
236
|
+
)
|
|
237
|
+
conn.execute(
|
|
238
|
+
"""INSERT INTO claim_status_history (claim_id, status, from_ts, event_id)
|
|
239
|
+
VALUES (%s, %s, %s, %s)""",
|
|
240
|
+
(p["id"], p.get("status", "current"), ts, eid),
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@_handler("claim_corroborated")
|
|
245
|
+
def _claim_corroborated(conn, p, ts, eid):
|
|
246
|
+
conn.execute(
|
|
247
|
+
"""UPDATE claims SET evidence_ids = ARRAY(SELECT DISTINCT unnest(evidence_ids || %s::text[]))
|
|
248
|
+
WHERE id = %s""",
|
|
249
|
+
(p["evidence_ids"], p["id"]),
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
@_handler("claim_evidence_removed")
|
|
254
|
+
def _claim_evidence_removed(conn, p, ts, eid):
|
|
255
|
+
conn.execute(
|
|
256
|
+
"UPDATE claims SET evidence_ids = array_remove(evidence_ids, %s) WHERE id = %s",
|
|
257
|
+
(p["evidence_id"], p["id"]),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@_handler("claim_validity_set")
|
|
262
|
+
def _claim_validity_set(conn, p, ts, eid):
|
|
263
|
+
"""a claim that evolved in a Confluence edit was valid until the edit date."""
|
|
264
|
+
conn.execute("UPDATE claims SET valid_to = %s WHERE id = %s", (p.get("valid_to"), p["id"]))
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
@_handler("claim_reassigned")
|
|
268
|
+
def _claim_reassigned(conn, p, ts, eid):
|
|
269
|
+
column = "subject_entity_id" if p["field"] == "subject" else "object_entity_id"
|
|
270
|
+
conn.execute(f"UPDATE claims SET {column} = %s WHERE id = %s", (p["to"], p["id"]))
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
@_handler("claim_status_changed")
|
|
274
|
+
def _claim_status_changed(conn, p, ts, eid):
|
|
275
|
+
_set_claim_status(conn, p["id"], p["to"], ts, eid, p.get("verdict_id"))
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
# ---------------------------------------------------------------- tensions and verdicts
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@_handler("tension_opened")
|
|
282
|
+
def _tension_opened(conn, p, ts, eid):
|
|
283
|
+
conn.execute(
|
|
284
|
+
"""INSERT INTO tensions (id, claim_ids, reason, opened_by, created_at, dossier)
|
|
285
|
+
VALUES (%s, %s, %s, %s, %s, %s)""",
|
|
286
|
+
(p["id"], p["claim_ids"], p["reason"], p["opened_by"], ts,
|
|
287
|
+
Jsonb(p["dossier"]) if isinstance(p.get("dossier"), dict) else None), # redacted = no dossier
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
@_handler("verdict_applied")
|
|
292
|
+
def _verdict_applied(conn, p, ts, eid):
|
|
293
|
+
conn.execute(
|
|
294
|
+
"""INSERT INTO verdicts (id, tension_id, kind, decided_by, claim_ids, changes, summary, rationale,
|
|
295
|
+
cited_evidence, confidence, model, prompt_version, dossier_hash, impact, exploration,
|
|
296
|
+
created_at)
|
|
297
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
|
|
298
|
+
(
|
|
299
|
+
p["id"],
|
|
300
|
+
p.get("tension_id"),
|
|
301
|
+
p["kind"],
|
|
302
|
+
p["decided_by"],
|
|
303
|
+
p["claim_ids"],
|
|
304
|
+
Jsonb(p["changes"]),
|
|
305
|
+
p["summary"],
|
|
306
|
+
p["rationale"],
|
|
307
|
+
p.get("cited_evidence", []),
|
|
308
|
+
p.get("confidence"),
|
|
309
|
+
p.get("model"),
|
|
310
|
+
p.get("prompt_version"),
|
|
311
|
+
p.get("dossier_hash"),
|
|
312
|
+
p.get("impact", 0),
|
|
313
|
+
Jsonb(p.get("exploration", [])),
|
|
314
|
+
ts,
|
|
315
|
+
),
|
|
316
|
+
)
|
|
317
|
+
for ch in p["changes"]:
|
|
318
|
+
if ch["from"] != ch["to"]:
|
|
319
|
+
_set_claim_status(conn, ch["claim_id"], ch["to"], ts, eid, p["id"])
|
|
320
|
+
if p.get("tension_id"):
|
|
321
|
+
conn.execute(
|
|
322
|
+
"UPDATE tensions SET status = 'resolved', verdict_id = %s WHERE id = %s",
|
|
323
|
+
(p["id"], p["tension_id"]),
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@_handler("verdict_reverted")
|
|
328
|
+
def _verdict_reverted(conn, p, ts, eid):
|
|
329
|
+
conn.execute(
|
|
330
|
+
"UPDATE verdicts SET status = %s, reverted_at = %s WHERE id = %s",
|
|
331
|
+
(p.get("status", "reverted"), ts, p["id"]),
|
|
332
|
+
)
|
|
333
|
+
for ch in p["changes"]:
|
|
334
|
+
_set_claim_status(conn, ch["claim_id"], ch["to"], ts, eid, p["id"])
|
|
335
|
+
if p.get("reopen_tension_id"):
|
|
336
|
+
conn.execute(
|
|
337
|
+
"UPDATE tensions SET status = 'open', verdict_id = NULL WHERE id = %s",
|
|
338
|
+
(p["reopen_tension_id"],),
|
|
339
|
+
)
|