Artifact bf2c14d183f0f97bceccabdc12399c6c43f0204f461edfddb5e5f05ecfc79992:
- File
docs/context-boundary.md
— part of check-in
[11f47eadde]
at
2026-11-18 11:58:22
on branch trunk
— Record S0/S1 qualification groundwork before product code
Contract maps and pinned Pi reference, local C qualification fixtures, MC/DC and Fil-C remote lifecycle tooling, and the context transaction substrate probe. No product harness code yet. (user: dmestas, size: 9458) [annotate] [blame] [check-ins using]
Context integration boundary
Status: source-based feasibility map and proposed boundaries, not an approved implementation design.
Context was found at /Users/dmestas/projects/context, not /Users/dmestas/context (../context relative to the working directory). The inspected checkout reported check-in cf8edff06f11 on branch tentative-rules; it is not vendored or pinned by the Pi lock. Local context links below depend on that directory layout and may drift independently.
What exists and what it does not imply
| ID | Existing mechanism | Evidence | Reuse boundary |
|---|---|---|---|
| C01 | One C binary links embedded SQLite and libfossil. | build target. | Suitable native foundation; not yet a public embedded context library. Most app functions are static in src/ctx.c. |
| C02 | WAL is selected on initialization; ordinary database opens set a 30-second busy timeout. | open, initialization. | Explicitly set and verify release durability policy on every runtime writer; do not infer process ownership from SQLite locks. No context crash/power-loss experiment was run. |
| C03 | Embedded Fossil repositories occupy tables in the main database and can share transaction commit/rollback. Existing archive batching opens one transaction. | embedded repository contract, archive transaction. | Enables one transaction across runtime records and selected repository changes. Do not duplicate execution state across independently committed databases. |
| C04 | Search is FTS5-ranked, project-scoped, and excludes retired material by default; history/as-of paths have separate filters. | search query and ordering. | Reuse for knowledge discovery, not for deciding whether a task has executed. |
| C05 | Extraction removes noise, excludes sidechain turns, flattens selected block kinds, redacts content, and trims tool output. | tool trimming, message extraction, filter/redact path. | Current archive is intentionally lossy. It cannot be the authoritative provider transcript or execution log. Preserve its redaction guarantees. |
| C06 | Session briefs recover recent decisions, artifacts, messages, and standing-rule proposals from archived turns. Rules are offered separately. | session brief, rules offer. | Useful human-facing handoff; not a checkpoint, acknowledgement, or authorization mechanism for durable tasks. |
| C07 | Rebuild drops derived search/citation/decision/artifact tables and reconstructs their content; chunk IDs change. | derived objects and rebuild. | Stable runtime IDs must not reference derived chunk IDs as their only identity. Runtime tables must survive rebuild. |
| C08 | Existing MCP surface exposes search and turn retrieval. | MCP tool declarations. | Server-side search tools do not provide an MCP client, arbitrary tool hosting, or durable external-call ownership. |
| C09 | Fossil sync makes remote archived sessions searchable; documented transport is plain HTTP and recommends a private network or SSH tunnel. | sync behavior and transport. | Knowledge replication is not execution failover. Do not transfer task ownership implicitly on sync or use plain HTTP for provider credentials. |
| C10 | Automatic snippet injection is optional and did not outperform skill-guided retrieval in the documented evaluation. | retrieval integration measurements. | Do not assume injecting more archived text improves context. Preserve targeted search and measure assembled-context quality. |
Two approaches considered
- Compose the existing CLI. Fast path to a retrieval prototype and preserves separation. Separate
ctxsubprocess calls cannot implement one atomic harness transition; also pays process/serialization costs. - Extract a reusable embedded context core. Hides storage/retrieval complexity and exposes a transaction-aware boundary. Requires deliberate API extraction and tests so the existing CLI remains unchanged.
Recommendation for a durability-capable native implementation: approach 2. This is a recommendation, not authorization to refactor context. Do not add consumer-side workarounds for bugs that belong in context/libfossil.
Proposed authoritative state, not present today
- Runtime conversation/branch identities and immutable typed entries.
- Submissions with unique request keys and durable settlement.
- Generation attempts, effective prompt/tool/context manifests, and provider continuation handles.
- Tool intents, replay policy, attempts, terminal results, and external idempotency references.
- Tasks with versioned definitions, checkpoints, ownership, retry limits, and abort state.
- Timers, signals, parked waits, and durable cancellation relationships.
- Commit sequence and observer/reconnect contract.
Keep this state distinct from rebuildable indexes and from the redacted archive. One physical SQLite database is possible; that does not require identical privacy, replication, retention, or representation policies for every table.
Essential invariants
- Admit work before execution; publish durable state only after its storage receipt.
- Atomically store a result and the state transition that acknowledges it.
- Never hold a write transaction while waiting for model HTTP, a subprocess, a person, or a timer.
- Resume from explicit task state; do not ask a model to infer completion from the archive.
- A replay-unsafe interrupted external action has an unknown outcome, not permission to repeat it.
- A replay-safe declaration is not proof of external idempotency. Persist keys and reconcile uncertain outcomes.
- Index rebuild, archive redaction, and knowledge synchronization cannot change execution ownership or authoritative task state.
- Persist context-version references for requests; new retrieval results must not silently rewrite a prior request on recovery.
- Authenticate approvals/signals and preserve who authorized what; a historical decision inferred from text is not a runtime approval token.
- One execution owner per store until an explicit, tested fencing/ownership protocol exists.
Design choices still open
- Transaction-aware context API and ownership of database/schema migrations.
- Runtime journal representation and which parts, if any, enter Fossil history.
- Secret references, opaque provider payload protection, retention, and backup policy. Never silently turn off existing archive redaction.
- Exact full-parity target: behavior versus file-format, RPC, package, and TypeScript-plugin compatibility.
- Power-loss durability policy, commit batching bounds, and observer visibility during streaming.
- Native extension boundary versus compatibility process/interpreter for existing Pi packages and code mode.
- Filesystem/workspace ownership and restart reconciliation for child processes and edits.
These are architectural decisions to resolve before implementation, not reasons to block source mapping.
Development and production requirements
The requested implementation language is C. DST, MC/DC instrumentation, fuzzing, sanitizers, and Fil-C are development/qualification tools. Production must be an optimized native build without those instrumentation runtimes.
- Exercise the production transition logic in DST; inject clock, random, network, process, and storage outcomes at explicit effect boundaries.
- Use MC/DC for critical compound decisions; evidence must name tool, configuration, exclusions, and achieved results. No coverage is claimed yet.
- Fuzz protocol/parser boundaries and operation sequences, including UTF-8, incomplete tool arguments, duplicate signals, corrupted checkpoints, and limit arithmetic.
- Run meaningful integration/regression coverage under Fil-C and native sanitizers; do not assume native testing transfers automatically to another compiler.
- Use real SQLite subprocess-crash tests in addition to simulation. Process kill does not prove power-loss safety; qualify that separately.
- Benchmark
-O2/-O3, LTO, PGO, and CPU targets rather than claiming a flag is universally fastest. Keep essential validation and durability enabled. - Compare the optimized build's semantic behavior against the checked builds and fixed regression seeds.
First proof slice
Persist input → assemble versioned context → stream a fake-provider tool call → commit tool intent → execute a controlled tool → commit result/checkpoint → finish response. Inject a crash before and after every commit and external action.
Acceptance: no admitted input disappears, deduplication survives reopen, no committed result is executed again, uncertain non-idempotent work is not blindly retried, index rebuild does not destroy runtime state, and observers never treat uncommitted state as durable. Add actual provider access and a terminal UI only after the fake-provider path establishes these contracts.