Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xsline OpenChatCut Atomic Edit Sessions

From Leeroopedia
Knowledge Sources
Domains Concurrency, Version_Control, Editing
Last Updated 2026-10-22 14:00 GMT

Overview

Concurrency-control pattern that lets external agents edit a shared document through draft, review, and atomic commit phases, ensuring no partial writes survive a failure and every committed change is revision-checked and undoable.

Description

Atomic Edit Sessions apply the principles of optimistic concurrency control to document editing. An external agent (CLI tool, MCP client, or automated script) works against a private draft fork of the project document. The draft is invisible to other editors until the agent explicitly requests a review. At review time, the runtime checks that the project's base revision has not changed since the session began; if it has, the commit is rejected rather than silently overwriting the concurrent change.

This pattern solves three problems:

  • Partial-write safety: A failed command leaves the project byte-identical — no half-applied timeline edits.
  • Concurrent-edit safety: A revision check (compare-and-swap) prevents one editor from clobbering another's work.
  • Undoability: The commit path snapshots a pre-edit version, so every applied change can be rolled back from version history.

An ownership claim is held while the session lives, so an open browser editor blocks the write instead of racing it.

Usage

Use this principle when:

  • Designing systems where external agents modify a shared document without holding a long-lived lock.
  • Building CLI or automation tools that need the same editing contract as interactive UI.
  • Ensuring that concurrent edits (browser + agent) are detected and rejected rather than silently lost.
  • Providing undoable, revision-checked commits for offline or headless editing workflows.

Theoretical Basis

The mechanism is a form of Optimistic Concurrency Control (OCC) combined with copy-on-write fork semantics:

  1. Fork: At session begin, a draft fork of the document is created from the current revision.
  2. Edit: Tools operate on the fork; the original is untouched.
  3. Validate: At review, compare the session's base revision against the store's current revision.
  4. Commit: If revisions match, atomically swap the fork in and record the old version for undo.
  5. Reject: If revisions differ, reject the commit and surface the conflict.

Pseudo-code:

# Abstract algorithm (NOT real implementation)
def begin_session(project, base_revision):
    draft = fork(project.document)
    return Session(draft, base_revision)

def run_tool(session, tool, args):
    # Operates on session.draft only; original untouched
    apply(tool, args, session.draft)

def review_and_commit(session, store, summary):
    if store.current_revision != session.base_revision:
        reject("project changed during edit")
    old = store.snapshot_current()
    store.swap(session.draft)
    store.record_undo(old)
    store.bump_revision()

The ownership claim acts as a soft lock: it does not block reads, but it prevents another writer from claiming the project until the session ends (commit or discard).

Practical Guide

When implementing an atomic edit session system:

  • Always fork before editing. Never mutate the shared document in place during a session.
  • Check revision at commit, not at begin. The conflict window is the full session duration, but the check cost is paid once at review.
  • Release ownership in a finally block.' A leaked ownership claim blocks all future writes to that project.
  • Handle the "no changes" case.' An idempotent re-run that stages nothing should discard cleanly, not fail on an empty commit.
  • Persist checkpoints for long sessions.' If the process crashes mid-session, a checkpoint lets the session resume rather than lose all draft work.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment