Skip to content
pi-c Context embedding API proposal
Login

Context embedding API proposal

Status: proposal only. No public API, context refactor, or production runtime is implemented. The accompanying test proves the storage substrate, not that archive_batch() is composable.

Verified current boundary

Inspected current inputs from /Users/dmestas/projects/context, not the older checkout identity in context-boundary.md. SHA-256 identities include:

Each probe run retains the complete source/schema/build manifest, copied inputs, expanded command arguments, compiler identity, binary hashes, and results under build/qualification/context/; latest-run.txt identifies the newest run. No upstream objects are borrowed.

Current contracts, verified in that source:

One host-owned SQLite connection and one host-owned outer transaction. Context would borrow that connection through an opaque, connection-scoped handle; it would neither close the connection nor own the caller's commit. This is a semantic proposal, not a set of new function names or signatures.

  1. Prepare outside transactions. The host selects the file and durability/busy-timeout policy. Context explicitly validates the connection, performs its own schema preparation, and opens the selected embedded repositories. Preparation must finish before the host starts admitting runtime writes. It must not inspect default HOME/provider configuration, import standing rules, scan transcript directories, or sync as an incidental embedding side effect.
  2. Compose one bounded write. The host begins the outer transaction, writes its authoritative records, and asks context to append/index explicitly supplied, redacted archive content on that same connection. The context operation does not issue outer BEGIN, COMMIT, or ROLLBACK; internal savepoints must not release the host transaction. It returns structured errors instead of die()/exit(). On any failed operation the host explicitly rolls back the whole unit; SQLite statement ABORT alone is insufficient. No model/network/subprocess/human wait occurs inside the write transaction.
  3. Finish at the owner. Only the host commits and publishes a durable receipt. It finalizes statements and releases context/repository handles outside transactions before closing its connection. A failed close is an error, not permission to discard a still-busy handle. The CLI remains an adapter that creates its own connection, invokes the same core, owns its transaction, and preserves existing commands, output, exit status, archive redaction, and explicit maintenance behavior.

This requires deliberate extraction in context itself. Do not copy archive_batch() into the consumer or wrap it in another BEGIN as a workaround.

Migration and rebuild ownership

Context owns migrations/version checks for its state and derived namespaces; the host owns its authoritative runtime schema and receipts. Keep those versions distinct, with an explicit startup ordering and a single maintenance owner. A borrowed-handle attach must not unexpectedly migrate or rebuild inside an application transaction. Newer unsupported versions must fail closed.

Context rebuild must retain its explicit object list and never drop host tables or treat host execution state as reconstructible knowledge. Host identities/checkpoints must not use derived chunk IDs as their sole durable reference. Rebuild, rule import, sync, and directory scanning are explicit maintenance operations, not retrieval/open side effects. Compatibility, old-schema upgrades, and host-record survival across rebuild need upstream tests before API adoption; this probe does not test them.

Retrieval and provenance

Expose bounded typed retrieval results, not CLI text parsing or raw sqlite3_stmt ownership. Preserve current project scoping, ranking, history/as-of filters, and retired-material exclusion (search_run(), src/ctx.c:1155–1218). Results should carry document kind/name, repository identity, immutable artifact/version hash when available, turn or source locator, and the exact returned text. Derived chunk IDs may be diagnostics, not durable provenance. For local mutable documents, pin the content hash and returned text rather than pretending there is a Fossil version.

The host persists the exact retrieval selection/text/hash manifest used by a request. A later reindex or search must not rewrite prior execution inputs. This archive is deliberately lossy/redacted and is not the authoritative provider transcript, execution log, or authorization source. No retrieval API or wire shape is specified by this proposal.

Lifetime and foreign-key risks

Rejected alternative

Composing existing ctx subprocess calls preserves CLI isolation, but each subprocess owns a separate connection/transaction: it cannot atomically acknowledge a host result and a Fossil archive change. Giving context the outer transaction and passing arbitrary host callbacks adds callback/error/lifetime complexity to solve a problem the host already owns. Neither is recommended for this slice.

Proof and remaining gates

Actual local tests use copied current C, schemas, and Makefile rules, including the bundled SQLite FTS5/math/thread flags and libfossil configuration. A fresh marked temporary HOME and CTX_DB, an empty inherited environment, and private Fossil/XDG configuration paths isolate each run. The probe mutates a real embedded technote, real ctx_capture state, and a host fixture table in one host transaction. It checks successful commit, explicit rollback, a duplicate-key statement failure followed by explicit rollback, and close/reopen persistence. Rollback checks include the stored Fossil artifact count, not just returned text. A deliberate COMMIT-instead-of-ROLLBACK mutant must fail the technote oracle. Temporary databases and configuration directories are removed on success and failure.

Run sh tools/test-context.sh and sh tools/test-context.sh sanitize. The latter checks ASan+UBSan; leak detection is not claimed on macOS. Neither run proves power-loss/crash recovery, multiprocess contention, cross-file atomicity, a public API, archive_batch() composition, CLI-wide regression compatibility, migration/rebuild survival, Fil-C support, or production readiness. Those are proposed follow-up gates, not tests reported as passed here.