Skip to main content
Back to Community & Earn Hub

CRDTs & Conflict-Free Collaborative Agent Workspaces: Letting Multiple Coding Agents Edit the Same Codebase Simultaneously

@aditya_verma4 days ago·2 min read 1959 views
CRDTs & Conflict-Free Collaborative Agent Workspaces: Letting Multiple Coding Agents Edit the Same Codebase Simultaneously

The Multi-Agent File Clobber Problem

Imagine a human developer editing lines 10–25 of auth.ts in VS Code while a background Type-Fixer Agent patches a missing import on line 2 and a Test-Generator Agent updates line 80.

With traditional string-replacement tools (replace_file_content(old_str, new_str)):

  1. Line numbers shift beneath the agent's feet mid-generation.
  2. Saving the buffer overwrites the human's unsaved keystrokes—or triggers messy <<<<<<< HEAD git merge conflicts.

Architecture: CRDT Shared Document State (Yjs) + AST Anchors

Instead of passing whole file strings back and forth, collaborative agent workspaces back every open file with a Conflict-free Replicated Data Type (Y.Text) synced over WebSockets:

text
[Human IDE (Monaco / VS Code)] ──┐
                                 ├──► [Yjs CRDT Sync Server] ──► Deterministic Merged AST
[Type-Fixer Agent (Y.Doc)] ──────┤
[Test-Writer Agent (Y.Doc)] ─────┘

Because Yjs tracks character-level causal order via Lamport timestamps and relative positions (Y.RelativePosition), an agent's edit anchored to function verifyJwt automatically tracks its shifted line offset even if the human types 15 new lines above it!


TypeScript Agent CRDT Edit Adapter

typescript
import * as Y from "yjs";

export class CollaborativeAgentBuffer {
  private doc: Y.Doc;
  private ytext: Y.Text;

  constructor(initialContent: string) {
    this.doc = new Y.Doc();
    this.ytext = this.doc.getText("source");
    this.ytext.insert(0, initialContent);
  }

  // Anchor a relative position BEFORE the LLM spends 3 seconds thinking
  createAstAnchor(charOffset: number): Uint8Array {
    const relPos = Y.createRelativePositionFromTypeIndex(this.ytext, charOffset);
    return Y.encodeRelativePosition(relPos);
  }

  // Apply the agent's patch cleanly even if concurrent edits shifted offsets!
  applyAnchoredPatch(encodedAnchor: Uint8Array, deleteLen: number, insertText: string) {
    const relPos = Y.decodeRelativePosition(encodedAnchor);
    const absPos = Y.createAbsolutePositionFromRelativePosition(relPos, this.doc);
    if (!absPos) throw new Error("Anchor invalidated by deleted parent block");

    this.doc.transact(() => {
      if (deleteLen > 0) this.ytext.delete(absPos.index, deleteLen);
      this.ytext.insert(absPos.index, insertText);
    }, "agent-worker");
  }
}

Why Relative Positions Beat Line-Number Edits

  • Zero Line-Drift Bugs: Even if 40 lines are inserted above the target function while the LLM is streaming tokens, Y.createAbsolutePositionFromRelativePosition resolves the exact updated index in O(1).
  • Instant Human Undo: Wrapping each agent patch in a tagged Y.UndoManager scope lets the developer press Cmd+Z to undo only the agent's edits without losing their own typing.
2 Comments

Discussion

@aarav_sharma4 days ago

Y.RelativePosition anchored before the LLM starts streaming tokens solves one of the most annoying bugs in IDE agents: line-number drift while the user keeps typing.

@siddharth_rao4 days ago

Scoped Y.UndoManager per agent origin ("agent-worker") is awesome UX too—letting the developer Cmd+Z only the bot's diff without touching their own edits.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.