223 lines
13 KiB
Python
223 lines
13 KiB
Python
"""Attach reviewed ECCV proceedings evidence to an existing programme attempt."""
|
|
import argparse
|
|
from contextlib import closing
|
|
from hashlib import sha256
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
|
|
from paper_atlas.audits.source_identity import entity_packet
|
|
from paper_atlas.collectors.eccv2026 import SOURCE as PROGRAMME
|
|
from paper_atlas.collectors.eccv2026_publications import SOURCE as PUBLISHER
|
|
from paper_atlas.db.backend import database_arguments, open_store
|
|
from paper_atlas.db.edition_scopes import KEY
|
|
from paper_atlas.db.submissions import identities_for_row, timestamp
|
|
from paper_atlas.ingestion.edition_tracks import curation
|
|
from paper_atlas.ingestion.records import source_record_count
|
|
|
|
MARKER = 'eccv_publication_reconciliation_v1:'
|
|
PUBLISHER_COUNT = 2835
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise ValueError(message)
|
|
|
|
|
|
def packet_digest(packet):
|
|
return sha256(json.dumps(packet, sort_keys=True, ensure_ascii=False).encode()).hexdigest()
|
|
|
|
|
|
def validate_plan(plan):
|
|
require(plan.get('status') == 'readonly_same_submission_reconciliation_plan'
|
|
and plan.get('database_written') is False, 'A reviewed read-only plan is required')
|
|
rows = plan['plans']
|
|
require(0 < len(rows) <= 1000 and len(rows) == plan['guarded_same_submission_plans'],
|
|
'Unexpected approved plan size')
|
|
for key in ('doi', 'retained_paper_id', 'retained_submission_id', 'retained_programme_record_id'):
|
|
require(len({entry[key] for entry in rows}) == len(rows), 'Repeated reviewed ' + key)
|
|
for entry in rows:
|
|
doi = entry['doi']
|
|
row = entry['source_row']
|
|
url = 'https://link.springer.com/chapter/' + doi
|
|
evidence = row['metadata']['publisher_chapter_evidence']
|
|
require(re.fullmatch(r'10\.1007/978-3-032-\d{5}-[0-9Xx]_[1-9]\d*', doi)
|
|
and (row['source'], row['external_id'], row['url'], row['evidence_url']) ==
|
|
(PUBLISHER, doi, url, url)
|
|
and (row['venue'], row['year'], row['track'], row['status']) ==
|
|
('ECCV', 2026, 'main', 'accepted')
|
|
and row['identifiers'] == {'doi': doi}
|
|
and identities_for_row(row) == [('publication_doi', doi)]
|
|
and isinstance(row['abstract'], str) and row['abstract'].strip()
|
|
and re.fullmatch(r'[0-9a-f]{64}', evidence['sha256'])
|
|
and re.fullmatch(r'[0-9a-f]{64}', entry['before_packet_sha256']),
|
|
'Invalid reviewed publisher observation')
|
|
return rows
|
|
|
|
|
|
def existing_coverage(conn, edition, source):
|
|
rows = conn.execute('''SELECT list_url,status,expected_count,imported_count,cursor,last_success_at
|
|
FROM edition_sources WHERE edition_id=? AND source_id=?''', (edition, source)).fetchall()
|
|
require(len(rows) <= 1, 'Source coverage is ambiguous')
|
|
actual = source_record_count(conn, source, edition)
|
|
if not rows:
|
|
require(actual == 0, 'Linked records have no coverage listing')
|
|
return None
|
|
row = rows[0]
|
|
require(row[1] == 'partial' and row[3] == actual, 'Coverage differs from linked records')
|
|
return tuple(row)
|
|
|
|
|
|
def reconcile_attempt(store, entry, original, target, venue_id, event_key, venue_names, publisher, signature):
|
|
conn = store.conn
|
|
doi = entry['doi']
|
|
canonical_doi = doi.lower()
|
|
paper = entry['retained_paper_id']
|
|
submission = entry['retained_submission_id']
|
|
programme_record = entry['retained_programme_record_id']
|
|
row = entry['source_row']
|
|
before = entity_packet(conn, paper)
|
|
require(packet_digest(before) == entry['before_packet_sha256'], 'Reviewed paper history changed: ' + doi)
|
|
require(not before['source_records_truncated'] and not before['attempts_truncated']
|
|
and all(not attempt['decisions_truncated'] and not attempt['sources_truncated']
|
|
and all(not decision['sources_truncated'] for decision in attempt['decisions'])
|
|
for attempt in before['attempts']), 'Reviewed paper history exceeds verification limits')
|
|
same_event = [a for a in before['attempts'] if a['venue'].casefold() == 'eccv' and a['year'] == 2026]
|
|
require(len(same_event) == 1, 'Paper has multiple or missing ECCV 2026 attempts')
|
|
attempt = same_event[0]
|
|
require((attempt['id'], attempt['edition_id'], attempt['source_record_id']) ==
|
|
(submission, original, programme_record)
|
|
and [(d['id'], d['status']) for d in attempt['decisions']] ==
|
|
[(entry['retained_programme_decision_id'], 'accepted')]
|
|
and [(s['source_record_id'], s['source']) for s in attempt['sources']] ==
|
|
[(programme_record, PROGRAMME)], 'Original programme attempt differs')
|
|
programme = next(record for record in before['source_records'] if record['id'] == programme_record)
|
|
metadata = json.loads(programme['metadata_json'])
|
|
require(KEY not in metadata and attempt['source_submission_id'] == programme['external_id'],
|
|
'Programme observation already curated or ambiguous')
|
|
scope = curation({
|
|
'external_id': programme['external_id'], 'venue_names': venue_names, 'from_edition_id': original,
|
|
'year': 2026, 'from_track': 'program_unclassified', 'event_key': event_key,
|
|
'venue_id': venue_id, 'track': 'main', 'section': 'published ECCV refereed proceedings',
|
|
'evidence_url': row['evidence_url'], 'evidence_sha256': row['metadata']['publisher_chapter_evidence']['sha256'],
|
|
}, target, signature)
|
|
require(not conn.execute('SELECT 1 FROM submission_identifiers WHERE submission_id=?', (submission,)).fetchone()
|
|
and not conn.execute("SELECT 1 FROM submission_identifiers WHERE edition_id=? AND scheme='publication_doi' AND value=?",
|
|
(target, doi)).fetchone()
|
|
and not conn.execute('SELECT 1 FROM source_records WHERE source_id=? AND external_id=?',
|
|
(publisher, doi)).fetchone(), 'Publication observation or submission identity already exists')
|
|
require(not conn.execute('SELECT 1 FROM identifiers WHERE scheme=? AND value=?',
|
|
(PUBLISHER, doi)).fetchone(), 'Publisher identity already exists')
|
|
owner = conn.execute("SELECT paper_id FROM identifiers WHERE scheme='doi' AND value=?", (canonical_doi,)).fetchone()
|
|
require(owner is None or owner[0] == paper, 'Publication DOI belongs to another paper')
|
|
if owner is None:
|
|
conn.execute("INSERT INTO identifiers(scheme,value,paper_id) VALUES('doi',?,?)", (canonical_doi, paper))
|
|
before['identifiers'].append({'scheme': 'doi', 'value': canonical_doi, 'paper_id': paper})
|
|
before['identifiers'].append({'scheme': PUBLISHER, 'value': doi, 'paper_id': paper})
|
|
before['identifiers'].sort(key=lambda identifier: (identifier['scheme'], identifier['value']))
|
|
require(conn.execute('UPDATE submissions SET edition_id=? WHERE id=? AND edition_id=?',
|
|
(target, submission, original)).rowcount == 1, 'Programme attempt changed')
|
|
conn.execute("INSERT INTO submission_identifiers VALUES(?,'publication_doi',?,?)", (target, doi, submission))
|
|
metadata[KEY] = [scope]
|
|
programme['metadata_json'] = json.dumps(metadata, ensure_ascii=False)
|
|
conn.execute('UPDATE source_records SET metadata_json=? WHERE id=?',
|
|
(programme['metadata_json'], programme_record))
|
|
actual_paper, record = store.record(PUBLISHER, doi, row['url'], row['title'], row['abstract'],
|
|
row['authors'], row['identifiers'], row['metadata'])
|
|
require(actual_paper == paper, 'Unexpected canonical paper')
|
|
store.record_tags(paper, record, PUBLISHER, row['tags'])
|
|
actual_edition = store.decision(paper, record, 'ECCV', 2026, 'main', doi, row['evidence_url'],
|
|
'accepted', row['raw_decision'], identities=identities_for_row(row),
|
|
observed_at=row['observed_at'], event_key=event_key)
|
|
require(actual_edition == target, 'Unexpected target edition')
|
|
|
|
after = entity_packet(conn, paper)
|
|
published = [r for r in after['source_records'] if r['id'] == record]
|
|
require(len(published) == 1 and published[0]['external_id'] == doi
|
|
and published[0]['abstract'] == row['abstract'], 'Publication source is missing or changed')
|
|
after['source_records'].remove(published[0])
|
|
moved = next(a for a in after['attempts'] if a['id'] == submission)
|
|
attached = [s for s in moved['sources'] if s['source_record_id'] == record]
|
|
observed = [d for d in moved['decisions'] if any(s['source_record_id'] == record for s in d['sources'])]
|
|
require(len(attached) == len(observed) == 1 and attached[0]['source_submission_id'] == doi
|
|
and observed[0]['status'] == 'accepted' and observed[0]['evidence_url'] == row['evidence_url']
|
|
and len(observed[0]['sources']) == 1, 'Publisher evidence did not attach to the existing submission')
|
|
moved['sources'].remove(attached[0])
|
|
moved['decisions'].remove(observed[0])
|
|
attempt.update(edition_id=target, track='main')
|
|
require(before == after, 'Existing paper or submission history changed')
|
|
|
|
|
|
def apply(store, plan, signature, previous_count):
|
|
rows = validate_plan(plan)
|
|
conn = store.conn
|
|
marker = MARKER + signature
|
|
require(not conn.execute('SELECT 1 FROM schema_migrations WHERE name=?', (marker,)).fetchone(),
|
|
'Reconciliation already applied')
|
|
edition_rows = conn.execute('''SELECT e.track,e.id,e.venue_id,e.event_key
|
|
FROM editions e JOIN venues v ON v.id=e.venue_id WHERE v.acronym='ECCV' AND e.year=2026
|
|
AND e.track IN ('main','program_unclassified')''').fetchall()
|
|
require(len(edition_rows) == 2 and {r[0] for r in edition_rows} == {'main', 'program_unclassified'},
|
|
'Expected two unambiguous reviewed editions')
|
|
editions = {r[0]: (r[1], r[2], r[3]) for r in edition_rows}
|
|
original, venue, event = editions['program_unclassified']
|
|
target, target_venue, target_event = editions['main']
|
|
require(venue == target_venue and event == target_event
|
|
and all((r['move_from_edition_id'], r['move_to_edition_id']) == (original, target) for r in rows),
|
|
'Reviewed edition identity differs')
|
|
names = ['ECCV', *(r[0] for r in conn.execute('SELECT alias FROM venue_aliases WHERE venue_id=?', (venue,)))]
|
|
sources = {r[0]: r[1] for r in conn.execute('SELECT name,id FROM sources WHERE name IN (?,?)', (PROGRAMME, PUBLISHER))}
|
|
programme, publisher = sources[PROGRAMME], sources[PUBLISHER]
|
|
old = existing_coverage(conn, original, programme)
|
|
main = existing_coverage(conn, target, programme)
|
|
published = existing_coverage(conn, target, publisher)
|
|
require(old is not None and old[3] >= len(rows) and (main is None or main[0] == old[0])
|
|
and published is not None and published[2:4] == (PUBLISHER_COUNT, previous_count)
|
|
and previous_count + len(rows) <= PUBLISHER_COUNT, 'Reviewed coverage parent differs')
|
|
for entry in rows:
|
|
reconcile_attempt(store, entry, original, target, venue, event, names, publisher, signature)
|
|
|
|
remaining = old[3] - len(rows)
|
|
imported = previous_count + len(rows)
|
|
main_count = (main[3] if main else 0) + len(rows)
|
|
for edition, source, expected in ((original, programme, remaining),
|
|
(target, programme, main_count), (target, publisher, imported)):
|
|
require(source_record_count(conn, source, edition) == expected, 'Reconciled coverage differs from linked records')
|
|
conn.execute('UPDATE edition_sources SET imported_count=? WHERE edition_id=? AND source_id=? AND list_url=?',
|
|
(remaining, original, programme, old[0]))
|
|
conn.execute('''INSERT INTO edition_sources
|
|
(edition_id,source_id,list_url,status,imported_count,cursor,last_success_at) VALUES(?,?,?,'partial',?,?,?)
|
|
ON CONFLICT(edition_id,source_id,list_url) DO UPDATE SET imported_count=excluded.imported_count''',
|
|
(target, programme, old[0], main_count, old[4], old[5]))
|
|
conn.execute('''UPDATE edition_sources SET imported_count=?,last_success_at=?
|
|
WHERE edition_id=? AND source_id=? AND list_url=?''', (imported, timestamp(), target, publisher, published[0]))
|
|
conn.execute('INSERT INTO schema_migrations(name) VALUES(?)', (marker,))
|
|
return {'status': 'applied', 'plan_sha256': signature, 'programme_main': main_count,
|
|
'programme_unclassified': remaining, 'springer_main': imported, 'new_papers': 0, 'new_submissions': 0}
|
|
|
|
|
|
def main(argv=None, *, prog=None):
|
|
parser = argparse.ArgumentParser(prog=prog)
|
|
database_arguments(parser)
|
|
parser.add_argument('--plan', type=Path, required=True)
|
|
parser.add_argument('--plan-sha256', required=True)
|
|
parser.add_argument('--springer-previous-count', type=int, required=True)
|
|
args = parser.parse_args(argv)
|
|
raw = args.plan.read_bytes()
|
|
signature = sha256(raw).hexdigest()
|
|
require(signature == args.plan_sha256, 'Plan hash differs')
|
|
plan = json.loads(raw)
|
|
validate_plan(plan)
|
|
store = open_store(args.db, pg_service=args.pg_service, pg_schema=args.pg_schema)
|
|
with closing(store.conn) as conn:
|
|
try:
|
|
if not getattr(conn, 'postgres', False):
|
|
conn.commit()
|
|
conn.execute('BEGIN IMMEDIATE')
|
|
result = apply(store, plan, signature, args.springer_previous_count)
|
|
conn.commit()
|
|
except BaseException:
|
|
conn.rollback()
|
|
raise
|
|
print(json.dumps(result), flush=True)
|