Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 4.41 KB

store.md

File metadata and controls

105 lines (74 loc) · 4.41 KB

store.py

Revision: 1 Last modified: 2026-12-04T19:00:00Z

Overview

store.py is the workable-items database access layer for the Issues mirror. The database is the source of truth (anchor §11.4.93), and this module is the only place the mirror reads it.

Its access discipline is narrow on purpose:

  • Read-only on items. The mirror never modifies a workable item.
  • Writes only issue_sync_map — the mirror's own sync-state table — created on demand (CREATE TABLE IF NOT EXISTS, lossless).
  • The consumer's own store may bump schema_version separately; this module does not interfere.

An item collapses by atm_id

The database can hold both an open (Issues) row and a Fixed row for the same item id. When it does, the open row wins. Without that rule the mirror would alternate between two representations of one item depending on row order, producing phantom drift on every run.

The sync map

CREATE TABLE IF NOT EXISTS issue_sync_map (
  atm_id TEXT NOT NULL, tracker TEXT NOT NULL, host TEXT NOT NULL, repo TEXT NOT NULL,
  issue_number INTEGER, issue_url TEXT,
  content_hash TEXT, last_synced_at TEXT, state TEXT, last_error TEXT,
  PRIMARY KEY (atm_id, tracker, host, repo))

The primary key is the quadruple (atm_id, tracker, host, repo), which is what lets one item be mirrored to several hosts independently — each with its own recorded hash, state and last error.

Prerequisites

  • python3 with the standard-library sqlite3 module.
  • A readable DB at the configured path (default docs/workable_items.db).
  • Write access only for the issue_sync_map table.

Usage examples

from issue_sync import store

con = store.open_db(db_path)          # read access to items, write to issue_sync_map
rows = con.execute(f"SELECT {store.FIELDS} FROM ...")

UNCONFIRMED: the precise helper names (open_db, and any reader/writer wrappers) beyond MAP_DDL and FIELDS were not read while writing this guide. Confirm before depending on one: python3 -c "import issue_sync.store as s; print([n for n in dir(s) if not n.startswith('_')])".

Exported surface

Name Kind Contract
MAP_DDL string The CREATE TABLE IF NOT EXISTS issue_sync_map statement.
FIELDS string atm_id, type, status, title, severity, description, current_location, forensic_anchor, body_md — the columns the mirror reads.

Note that FIELDS is the columns read, not the columns sent: payload.py's allowlist decides what leaves the process. The two are intentionally different.

Exit codes

A library, so no exit codes. Failures surface through the caller as the package's 2 (cannot determine) when the DB is unreadable — never as "in sync".

Edge cases

  • Both an open row and a Fixed row for one atm_id. The open row wins; this is the documented collapse rule.
  • A missing issue_sync_map table. Created on demand, losslessly.
  • A DB that cannot be read. Cannot determine (2) — an unreadable DB is never an empty mirror.
  • A pre-existing issue_sync_map with a narrower schema. IF NOT EXISTS will not alter it; a genuine schema change is a migration, not a silent rewrite.
  • A consumer that bumps schema_version. Out of this module's scope; it does not manage the consumer's schema.
  • Several legs for one item. Independent rows keyed by (atm_id, tracker, host, repo).
  • last_error populated. The leg is recorded as failing rather than silently retried forever.

Internal behaviour

  1. Opens the SQLite DB.
  2. Ensures issue_sync_map exists (MAP_DDL).
  3. Reads items with FIELDS, collapsing duplicate atm_id rows in favour of the open one.
  4. Records per-leg sync state (hash, issue number/URL, state, last error) into issue_sync_map.
  5. Never writes to an item row.

Related scripts

Used by engine.py, inbound.py, cli.py, pipeline.py and offline_status.py (which compares current content hashes against issue_sync_map).

Related: payload.py (what may be sent), inbound.py (read-only inbound), offline_status.py (offline pending report).

See also docs/scripts/engine.md, docs/scripts/offline_status.md and docs/scripts/payload.md.

Last verified

2026-12-04 — python3 -c "import ast;ast.parse(open('scripts/issue_sync/store.py').read())" → exit 0. python3 -c "import issue_sync.store as s; print('issue_sync_map' in s.MAP_DDL)" prints True.