"""Local SQLite outbox; it has no Firebird dependency or write path.""" import json import sqlite3 from pathlib import Path import uuid class AgentState: def __init__(self, path): self.connection = sqlite3.connect(Path(path)); self.connection.row_factory = sqlite3.Row self.connection.executescript("CREATE TABLE IF NOT EXISTS product_state(product_id TEXT PRIMARY KEY, source_fingerprint TEXT NOT NULL, dataset_version TEXT NOT NULL, engine_version TEXT NOT NULL, status TEXT NOT NULL, last_processed_at TEXT, last_result_hash TEXT, reason_code TEXT); CREATE TABLE IF NOT EXISTS outbox(product_id TEXT PRIMARY KEY, source_fingerprint TEXT NOT NULL, status TEXT NOT NULL, retries INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS ipi_write_log(product_id TEXT NOT NULL, source_fingerprint TEXT NOT NULL, decision_status TEXT, ipi_status TEXT, write_status TEXT NOT NULL, before_value TEXT, proposed_value TEXT, result TEXT, reason_code TEXT, timestamp TEXT NOT NULL);") self.connection.execute("CREATE TABLE IF NOT EXISTS installation_identity (id INTEGER PRIMARY KEY CHECK(id=1), installation_id TEXT NOT NULL)") # Last Hub decision per product + the source fingerprint it was computed for: what makes # SYNC_INCREMENTAL a real delta (only new/changed products are re-classified). self.connection.execute("CREATE TABLE IF NOT EXISTS decision_cache (product_id TEXT PRIMARY KEY, source_fingerprint TEXT NOT NULL, decision_json TEXT NOT NULL)") self.connection.commit() def stored_installation_id(self): row = self.connection.execute("SELECT installation_id FROM installation_identity WHERE id=1").fetchone() if row is None: return None try: return str(uuid.UUID(row["installation_id"])) except (TypeError, ValueError, AttributeError) as error: raise ValueError("stored installation_id is not a valid UUID") from error def installation_id(self, candidate=None): existing = self.stored_installation_id() if existing: if candidate is not None and str(uuid.UUID(str(candidate))) != existing: raise ValueError("configured installation_id conflicts with persisted identity") return existing if candidate is None or not str(candidate).strip(): raise ValueError("installation_id is required; refusing to generate a new identity") try: value = str(uuid.UUID(str(candidate))) except (TypeError, ValueError, AttributeError) as error: raise ValueError("installation_id must be a valid UUID") from error self.connection.execute("INSERT OR IGNORE INTO installation_identity(id, installation_id) VALUES(1, ?)", (value,)) self.connection.commit() row = self.connection.execute("SELECT installation_id FROM installation_identity WHERE id=1").fetchone() if row is None or str(uuid.UUID(row["installation_id"])) != value: raise ValueError("persisted installation_id conflicts with candidate") return value def cached_fingerprint(self, product_id): row = self.connection.execute("SELECT source_fingerprint FROM decision_cache WHERE product_id=?", (product_id,)).fetchone() return row["source_fingerprint"] if row else None def cached_decision(self, product_id): row = self.connection.execute("SELECT decision_json FROM decision_cache WHERE product_id=?", (product_id,)).fetchone() return json.loads(row["decision_json"]) if row else None def remember_decision(self, product_id, source_fingerprint, decision): self.connection.execute("INSERT OR REPLACE INTO decision_cache(product_id,source_fingerprint,decision_json) VALUES(?,?,?)", (product_id, source_fingerprint, json.dumps(decision, default=str))) self.connection.commit() def forget_missing(self, present_product_ids): """Products no longer in the ERP leave the LOCAL cache only; Hub-side history/blockers are never deleted by this.""" present = set(present_product_ids) gone = [row["product_id"] for row in self.connection.execute("SELECT product_id FROM decision_cache") if row["product_id"] not in present] for product_id in gone: self.connection.execute("DELETE FROM decision_cache WHERE product_id=?", (product_id,)) self.connection.commit() return len(gone) def enqueue(self, product_id, source_fingerprint, dataset_version, engine_version): row = self.connection.execute("SELECT * FROM product_state WHERE product_id=?", (product_id,)).fetchone() if row and tuple(row[key] for key in ("source_fingerprint", "engine_version", "status")) == (source_fingerprint, engine_version, "RESULT_RECEIVED"): return False self.connection.execute("INSERT OR REPLACE INTO product_state(product_id,source_fingerprint,dataset_version,engine_version,status) VALUES(?,?,?,?,?)", (product_id, source_fingerprint, dataset_version, engine_version, "PENDING_API")); self.connection.execute("INSERT OR REPLACE INTO outbox(product_id,source_fingerprint,status) VALUES(?,?,?)", (product_id, source_fingerprint, "PENDING_API")); self.connection.commit(); return True def pending(self): return [dict(row) for row in self.connection.execute("SELECT * FROM outbox WHERE status='PENDING_API'")] def retry(self, product_id): self.connection.execute("UPDATE outbox SET retries=retries+1,status='PENDING_API' WHERE product_id=?", (product_id,)); self.connection.commit() def result_received(self, product_id, source_fingerprint, dataset_version, engine_version): self.connection.execute("UPDATE product_state SET source_fingerprint=?,dataset_version=?,engine_version=?,status='RESULT_RECEIVED' WHERE product_id=?", (source_fingerprint, dataset_version, engine_version, product_id)); self.connection.execute("DELETE FROM outbox WHERE product_id=?", (product_id,)); self.connection.commit() def log_ipi_write(self, product_id, source_fingerprint, decision_status, ipi_status, write_status, before_value, proposed_value, result, reason_code, timestamp): self.connection.execute( "INSERT INTO ipi_write_log(product_id,source_fingerprint,decision_status,ipi_status,write_status,before_value,proposed_value,result,reason_code,timestamp) VALUES(?,?,?,?,?,?,?,?,?,?)", (product_id, source_fingerprint, decision_status, ipi_status, write_status, str(before_value) if before_value is not None else None, str(proposed_value) if proposed_value is not None else None, result, reason_code, timestamp), ) self.connection.commit() def ipi_write_log_for(self, product_id): return [dict(row) for row in self.connection.execute("SELECT * FROM ipi_write_log WHERE product_id=? ORDER BY timestamp", (product_id,))]