SQLite generation job store: persist a single-machine collector
·
A single-machine automation does not need to keep its entire generation history in memory. SQLite can hold a small application ledger containing operation identity, accepted job IDs, latest snapshots, and delivery progress. This is your client-side database; Genace’s own job service uses PostgreSQL and is not changed by this design.
Store recoverable milestones
Create a database on persistent local storage rather than a temporary container filesystem. Keep request intent separate from remote rendering and local delivery phases. A successful render whose file copy failed should remain a succeeded render with incomplete delivery.
import sqlite3
connection = sqlite3.connect("generation-ledger.sqlite", timeout=5)
connection.execute("""
CREATE TABLE IF NOT EXISTS operations (
operation_id TEXT PRIMARY KEY NOT NULL,
phase TEXT NOT NULL,
remote_job_id TEXT,
snapshot_json TEXT,
delivery_state TEXT NOT NULL DEFAULT 'not_started'
)
""")
connection.commit()
def save_acceptance(operation_id: str, job_id: str):
if not isinstance(job_id, str) or not job_id.strip():
raise ValueError("Expected a nonblank job ID")
with connection:
cursor = connection.execute("""
UPDATE operations SET phase = 'accepted', remote_job_id = ?
WHERE operation_id = ? AND phase = 'submitting'
AND remote_job_id IS NULL
""", (job_id, operation_id))
if cursor.rowcount != 1:
raise RuntimeError("Operation could not accept this transition")
Create the operation row before calling this helper. The code illustrates a conditional milestone update and does not submit or observe a generation. Parameterized values avoid turning identifiers or prompt content into SQL syntax.
In an ordinary SQLite rowid table, a text primary key does not by itself reject
NULL. The explicit NOT NULL protects new records from that identity gap.
CREATE TABLE IF NOT EXISTS does not add constraints to an existing table:
inspect and reconcile old rows, then apply a reviewed migration separately.
Do not run a tutorial migration against production or silently delete records.
The helper rejects non-string and blank job IDs before writing, and refuses to
replace any saved remote reference, even on a row still marked submitting.
These are local safeguards, not proof that a job exists or belongs to the caller.
Validate the 202 response before calling this accepted-only helper. A known
submission 502 can instead carry error.job_id; preserve it in a separate
reconciliation path without marking an uncertain submission accepted or
automatically submitting again.
Keep network waits outside transactions
Use short transactions for state changes. Do not hold a SQLite write transaction open while a long poll waits for thirty seconds. SQLite supports concurrent readers but only one simultaneous writer, so unnecessary write locks can block unrelated local progress.
Use a dedicated connection with no unrelated pending transaction when calling this helper. Python’s connection context commits or rolls back the current transaction; it is not a nested transaction and does not close the connection. Close the connection during orderly shutdown. A failed local write must retain the known ID in the caller’s recovery path, rather than repeat the remote POST.
After a GET response arrives, parse and validate it, then update the saved snapshot in a short transaction. The remote snapshot uses id and can contain null outputs. Store the original state and error fields needed for recovery without copying Authorization headers or unrestricted sensitive logs into the ledger.
Resume from the ledger at startup
On restart, select accepted operations with known remote IDs and schedule observation. Select succeeded operations with incomplete delivery for download recovery. Do not submit again merely because the previous process ended before it printed a final URL.
An operation left submitting without an ID needs a separate uncertainty branch. SQLite durability cannot prove whether an external POST was accepted before the process stopped. A new remote generation remains an explicit decision after reconciliation because the creation route has no published idempotency contract.
Operate within the single-host scope
Choose a backup strategy and test restoration. Avoid treating a copied database file during active writes as automatically consistent; use SQLite-supported backup mechanisms for your chosen configuration. Restrict filesystem permissions because the ledger can contain prompts and asset references even if it contains no API key.
Verification checklist
Stop the process after saving acceptance and confirm startup resumes GET observation. Fail a snapshot update and verify no new POST occurs. Test a completed render with a missing download and confirm only delivery resumes. Finally, contend two local writers and verify lock failures produce bounded retries rather than silent lost state.
Also test NULL operation identity, blank and non-string acceptance IDs, and a submitting row that already contains a job reference. Each must be rejected without replacing recoverable state. Inject a SQLite update failure and check that the transaction rolls back; after releasing a competing writer’s lock, retry only the local save of the same known job. A second connection can verify the committed milestone, but this alone is not a power-loss durability test.
See durable acceptance, output storage, and PostgreSQL storage when the deployment grows beyond one machine.
Continue with production operations
Where these facts come from
- Editorial keyword hypothesis; search volume not measured
- https://www.sqlite.org/lang_transaction.html
- https://www.sqlite.org/lang_createtable.html#the_primary_key
- https://docs.python.org/3/library/sqlite3.html#how-to-use-the-connection-context-manager
- codebase: src/ai/jobs/http.ts