From 1e5237fe19160eba31ec02fea017429ddc31e79c Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Thu, 26 Nov 2026 01:40:22 +0800 Subject: [PATCH] Preserve match candidate evidence when papers merge --- paper_atlas/db/store.py | 41 ++++++++++++++++++++++++++++++-- tests/test_index_core.py | 34 ++++++++++++++++++++++++++ tests/test_postgres_ingestion.py | 31 ++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/paper_atlas/db/store.py b/paper_atlas/db/store.py index 545c8bb..717bf07 100644 --- a/paper_atlas/db/store.py +++ b/paper_atlas/db/store.py @@ -28,6 +28,23 @@ def author_keys(authors): return result +def _candidate_observations(score, evidence, status): + try: + payload = json.loads(evidence) + except ValueError: + payload = None + if isinstance(payload, dict) and set(payload) == {'reattached_observations'}: + rows = payload['reattached_observations'] + if isinstance(rows, list) and rows and all( + isinstance(row, list) and len(row) == 3 + and (row[0] is None or isinstance(row[0], (int, float))) + and isinstance(row[1], str) and isinstance(row[2], str) + for row in rows + ): + return [tuple(row) for row in rows] + return [(score, evidence, status)] + + class Store: def __init__(self,path): require_sqlite_writable(path) @@ -69,8 +86,28 @@ class Store: for left,right,method,score,evidence,status in candidates: left = target if left == other else left right = target if right == other else right - if left != right: - c.execute('INSERT INTO match_candidates VALUES(?,?,?,?,?,?) ON CONFLICT DO NOTHING',(min(left,right),max(left,right),method,score,evidence,status)) + if left == right: + continue + key = (min(left, right), max(left, right), method) + inserted = c.execute( + 'INSERT INTO match_candidates VALUES(?,?,?,?,?,?) ON CONFLICT DO NOTHING', + (*key, score, evidence, status), + ) + if inserted.rowcount: + continue + current = c.execute( + 'SELECT score,evidence_json,status FROM match_candidates ' + 'WHERE left_paper_id=? AND right_paper_id=? AND method=?', + key, + ).fetchone() + existing = _candidate_observations(*current) + combined = list(dict.fromkeys(existing + _candidate_observations(score, evidence, status))) + if combined != existing: + c.execute( + 'UPDATE match_candidates SET evidence_json=? ' + 'WHERE left_paper_id=? AND right_paper_id=? AND method=?', + (json.dumps({'reattached_observations': combined}, ensure_ascii=False), *key), + ) c.execute('DELETE FROM papers WHERE id=?',(other,)) def record(self,source,external_id,url,title,abstract,authors,identifiers=None,metadata=None,published=None): diff --git a/tests/test_index_core.py b/tests/test_index_core.py index dc9e2cf..b293a91 100644 --- a/tests/test_index_core.py +++ b/tests/test_index_core.py @@ -97,6 +97,40 @@ class StoreTests(unittest.TestCase): self.assertIsNotNone(self.store.conn.execute('SELECT 1 FROM papers WHERE id=?',(unrelated,)).fetchone()) self.assertEqual(self.store.conn.execute('SELECT count(*) FROM paper_merges').fetchone()[0],1) + def test_merge_preserves_colliding_candidate_observations(self): + first, _ = self.record('arxiv', '2601.00001', authors=['First Author']) + second, _ = self.record('pmlr', 'v1/second', authors=['Second Author']) + third, _ = self.record('acl', '2026.acl.1', authors=['Third Author']) + related, _ = self.record('arxiv', '2601.00002', authors=['Related Author']) + db = self.store.conn + method = 'exact_title_unconfirmed' + original = (None, '{"title":"Conference title"}', 'pending') + incoming = (0.98, '{"title":"Preprint title"}', 'pending') + for paper, observation in ((first, original), (second, incoming), (third, incoming)): + left, right = sorted((paper, related)) + db.execute( + 'INSERT INTO match_candidates VALUES(?,?,?,?,?,?) ' + 'ON CONFLICT(left_paper_id,right_paper_id,method) DO UPDATE SET ' + 'score=excluded.score,evidence_json=excluded.evidence_json,status=excluded.status', + (left, right, method, *observation), + ) + + self.store.merge(first, second, 'confirmed publication version') + self.store.merge(first, third, 'confirmed additional source') + + rows = db.execute( + 'SELECT left_paper_id,right_paper_id,score,evidence_json,status ' + 'FROM match_candidates WHERE method=?', + (method,), + ).fetchall() + [(left, right, score, evidence, status)] = rows + self.assertEqual((left, right, score, status), (first, related, None, 'pending')) + self.assertEqual( + json.loads(evidence)['reattached_observations'], + [list(original), list(incoming)], + ) + self.assertEqual(db.execute('PRAGMA foreign_key_check').fetchall(), []) + def test_manifest_count_mismatch_rolls_back_records(self): path=Path(self.tmp.name)/'source.jsonl' path.write_text(json.dumps({'source':'pmlr','external_id':'v1/paper','url':'https://example.org/paper','title':'Paper','abstract':'Abstract','authors':[],'venue':'ICML','year':2023,'track':'main','status':'accepted'})+'\n') diff --git a/tests/test_postgres_ingestion.py b/tests/test_postgres_ingestion.py index 706e737..8d0f402 100644 --- a/tests/test_postgres_ingestion.py +++ b/tests/test_postgres_ingestion.py @@ -66,6 +66,37 @@ class PostgreSQLIngestionTests(unittest.TestCase): def store(self): return open_store(pg_service=self.service, pg_schema=self.schema) + def test_merge_preserves_colliding_candidate_evidence(self): + store = self.store() + with closing(store.conn) as db, db: + ids = [ + store.record('fixture', key, 'https://example.org/' + key, title, None, ['Author'])[0] + for key, title in ( + ('preprint', 'Preprint'), + ('proceedings', 'Proceedings version'), + ('related', 'Related work'), + ) + ] + first, second, related = ids + expected = [ + (None, '{"title":"Preprint"}', 'pending'), + (0.75, '{"title":"Proceedings version"}', 'pending'), + ] + for paper, observation in zip((first, second), expected): + db.execute( + 'INSERT INTO match_candidates VALUES(?,?,?,?,?,?)', + (paper, related, 'exact_title_unconfirmed', *observation), + ) + store.merge(first, second, 'confirmed publication version') + row = db.execute( + 'SELECT left_paper_id,right_paper_id,evidence_json FROM match_candidates', + ).fetchone() + self.assertEqual(row[:2], (first, related)) + self.assertEqual( + json.loads(row[2])['reattached_observations'], + [list(observation) for observation in expected], + ) + def row(self, source='openreview_snapshot', external='forumNew1', status='rejected', year=2025, venue='ICLR'): return {'source': source, 'external_id': external, 'url': 'https://example.org/' + external, 'venue': venue, 'year': year, 'track': 'main', 'status': status,