RFC: copy-on-write writable branches for speculative agent workflows
Status: proposed; owner decisions required; no implementation. This RFC changes no package source or public exports.
1. Problem and motivation
Autonomous agents frequently perform multi-step speculative work: planning a remediation across dozens of entities, running simulated tool calls, checking whether graph constraints and derivations stabilize, or exploring competing hypotheses in parallel before asking a human (or verifier agent) to approve and apply the changes.
Triplex already has two partial building blocks:
transaction preview— previews a single atomic command without committing.Derivation.Overlay(packages/core/src/derivation/Overlay.ts) — evaluates temporary assertions/retractions for one read-only derivation run by copying fixed attributes into a fresh in-memory KV store.
Neither supports multi-transaction, writable, isolated branches over a database with millions of entities:
- Copying attributes into an in-memory KV store does not scale to millions of entities.
transaction previewcannot span multiple agent turns, tool calls, or subagent handoffs.
2. Recommendation
Introduce O(1) copy-on-write writable branches forked at an exact basePosition (recorded_position), exposed as a standard Triples (and Database) layer so existing Datalog, graph constraints, commit validators, why, derivations, CLI, and MCP tools work unchanged inside a branch.
All TypeScript snippets below are proposed contract fragments (ordinary ts fences).
3. Lifecycle and API sketch
export interface BranchHandle {
readonly branchId: BranchId;
readonly basePosition: number;
readonly createdAt: number;
readonly expiresAt: number | null;
readonly actor: string;
readonly status: "open" | "merged" | "discarded" | "expired";
}
export interface BranchService {
readonly create: (options: {
readonly actor: string;
readonly basePosition?: number; // defaults to currentPosition()
readonly ttlMs?: number;
readonly label?: string;
}) => Effect.Effect<BranchHandle, WriteError>;
/** Provide a Triples layer scoped to this branch (reads basePosition + branch deltas). */
readonly layer: (branchId: BranchId) => Layer.Layer<Triples, BranchError>;
/** Compute exact net assertions, retractions, and conflicts against current head (or base). */
readonly diff: (
branchId: BranchId,
options?: { readonly against?: "base" | "head" },
) => Effect.Effect<BranchDiff, BranchError>;
/** Atomically apply net branch changes onto main with optimistic concurrency checks. */
readonly merge: (
branchId: BranchId,
options: {
readonly actor: string;
readonly commandId: string;
readonly strategy?: "fail-on-conflict" | "squash-atomic";
},
) => Effect.Effect<TransactionResult, BranchMergeError>;
readonly discard: (branchId: BranchId) => Effect.Effect<void, BranchError>;
}4. Storage model (O(1) fork, delta-only writes)
Forking never copies rows from triples. A branch stores only its metadata and delta mutations:
triplex_branches(branch_id, base_position, created_at, expires_at, actor, label, status, merged_tx_id, merged_at)triplex_branch_triples: same schema astriplesplusbranch_id TEXT NOT NULL, holding only facts asserted inside the branch (with branch-local monotonicrecorded_position > base_position).triplex_branch_retractions(branch_id, triple_id, retracted_at, retracted_position, retract_tx_id): records retractions of base facts (facts withrecorded_position <= base_positionintriples). Retracting a branch-asserted fact updates its row intriplex_branch_triplesdirectly.triplex_branch_transactions/triplex_branch_transaction_entities: branch-local journal sowhy,transactions,commandIdidempotency, andentityHistorywork inside the branch without polluting the main journal or advancing main'striplex_commit_position.
Memory and cold-tier interaction
- Because a branch pins
base_position, any cold-tier compaction watermark (cold-tier-duckdb.md) must either stay below active open branches'base_position(for fast hot-only branch reads) or allow branched reads to federate with cold Parquet when a long-lived branch falls behindW. expires_atenables automatic garbage collection of abandoned agent branches (e.g., default TTL of 24 hours for ephemeral agent runs).
5. Query and read semantics inside a branch
For a branch forked at base_position = B, the logical fact relation at branch position P is:
-- Conceptual branched view (compiled by BranchedQueryExecutor)
SELECT t.* FROM triples t
WHERE t.recorded_position <= $base_position
AND (t.retracted_position IS NULL OR t.retracted_position > $base_position)
AND NOT EXISTS (
SELECT 1 FROM triplex_branch_retractions r
WHERE r.branch_id = $branch_id
AND r.triple_id = t.id
AND r.retracted_position <= $branch_cut
)
UNION ALL
SELECT bt.* FROM triplex_branch_triples bt
WHERE bt.branch_id = $branch_id
AND bt.recorded_position <= $branch_cut
AND (bt.retracted_position IS NULL OR bt.retracted_position > $branch_cut)- No double-cost when no branch is active: Ordinary main-line queries never touch
triplex_branch_*tables. - SQL & KV parity: On SQLite/PostgreSQL/DuckDB,
BranchedQueryExecutorrewrites the compiledtriplestable reference to a CTE overtriples+ branch deltas. On KV,BranchedKvBackendoverlays a branch-local ordered map over a base snapshot cut. - Constraints and commit validators: Run identically inside the branch against the branched
before/afterviews, so agents get real constraint and policy feedback on every speculative step.
6. Diffing, conflict detection, and atomic merge
When an agent calls branch.merge(branchId, { actor, commandId }):
- Compute net operations:
- Retract every base
triple_idpresent intriplex_branch_retractions(still live in base). - Assert every still-live fact in
triplex_branch_triples(skipping facts asserted and later retracted within the same branch).
- Retract every base
- Generate automatic preconditions:
- Every base
triple_idretracted in the branch becomes aTripleLiveprecondition. - Every entity whose state was read (
tx.get) or mutated in the branch can carry anEntityStateprecondition (or attribute-level check) verifying that no concurrent main-line commit mutated those entities betweenbasePositionand current head.
- Every base
- Commit atomically to main:
- Submit the net operations, preconditions, pinned
configSnapshot,enforceconstraints, and runtime-wideCommitValidationvalidators as one atomicTriples.transacton main. - Record branch provenance in transaction metadata (e.g.,
correlationId/causationIdor a dedicated branch-merge reference) sowhycan trace merged facts back to the agent branch. - Atomically mark the branch row
status = 'merged'andmerged_tx_id = result.txId.
- Submit the net operations, preconditions, pinned
- Conflict handling:
- If main moved in a conflicting way,
mergefails with a typedBranchConflictErrordetailing the conflicting entities/triple IDs, leaving the branch open so the agent can inspectbranch.diff({ against: "head" }), rebase/repair, or discard.
- If main moved in a conflicting way,
7. MCP and CLI surface
Add first-class tools to @triplex-build/triplex/mcp and triplex CLI:
branch_create,branch_list,branch_diff,branch_merge,branch_discard- Optional
branchIdparameter onentity_get,query_run,why,transaction_preview, andtransaction_apply.
This gives coding and operational agents a clean "fork → experiment → verify → review diff → merge" loop out of the box.
8. Open questions & decision gates
- Nested branches (branch-from-branch): Defer in v1; require branches to fork from a main
recorded_positionto keep query compilation to a single delta overlay. - Merge granularity: Should v1 support only squash-atomic merge (one main transaction with net changes) or also replay merge (preserving individual branch commits in the main journal)? Recommend squash-atomic first for simplicity and atomic constraint enforcement.
- Read-set tracking for optimistic merge: Decide whether
mergechecks conflicts only on written/retracted entities (fast, lower false conflicts) or also on read entities (stricter serializability, requires recording read sets during branch execution).