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

Implementation:3641397194 wq Gpt6 Astra SeatTransactions

From Leeroopedia
Knowledge Sources
Domains File_Systems, Reliability, Security
Last Updated 2026-10-26 12:00 GMT

Overview

Concrete tool for crash-safe file transactions with journaled snapshots, content-addressed backups, and atomic rollback, provided by the ColdBrew Zero desktop library.

Description

The SeatTransactions class implements a full ACID-like transaction lifecycle for multi-file configuration writes. It provides four main operations — `preview`, `deploy`, `verify`, and `restore` — plus supporting methods for history, snapshot validation, and backup blob management.

The transaction is crash-safe: a journal at `.coldcoffee-history/<uuid>.json` records the snapshot with before/after hashes and status progression (`preparing` → `applied` → `restoring` → `restored`). Before-content is stored as content-addressed blobs at `.coldcoffee-history/blobs/<sha256>`, deduplicated by hash. On failure, all written files are rolled back to their before-state; if rollback itself fails (file was modified by another process), the snapshot is marked `partial` and the backup is preserved.

The class also provides the guardPath function (exported) that rejects symlinks, junctions, and hardlinks — the trust boundary used across the entire codebase.

Usage

  • One-click install: `preview` → `deploy` → `verify` sequence, orchestrated by `installOne`.
  • Uninstall: `history` → `restore` loop, orchestrated by `uninstallOne`.
  • Multi-root install: `runtime.deploy` creates multiple SeatTransactions instances (one per root) and previews all before writing any.
  • Verification UI: `verify` re-runs preview and checks `changes === 0`.
  • Path safety: `guardPath` and `within` are used by many other modules to validate paths before file operations.

Code Reference

Source Location

  • Repository: ColdBrew Zero
  • File: desktop/src/lib/seat-transactions.js

Signature

// Constructor
class SeatTransactions {
  constructor({ plan = runtime.plan, now = Date.now, write = atomic } = {})

  // Root selection
  select(root, { layout = 'default' } = {}) -> { root }
  requireRoot() -> string

  // Transaction lifecycle
  preview(seat) -> {
    id, seat, root, pack, packHash, changes, upstream,
    files: [{ path, kind, kinds, sources, beforeHash, afterHash,
              beforeMode, afterMode, changed, exists, bytes }],
    modelStatus
  }
  deploy(id) -> { ok, id, changed, root, upstream, backup, message }
  verify(seat) -> { ok, root, seat, upstream, checks, modelStatus }
  restore(id) -> { ok, restored, message }

  // History & snapshots
  history() -> Array<{ id, seat, at, status, files }>
  snapshot(id) -> snapshot object
  getPlan(id) -> plan object
  file(id, index) -> { path, kind, kinds, sources, beforeHash, afterHash, before, after }

  // Backup management
  retain(buffer, digest) -> 'blob:<digest>'
  content(value, digest) -> Buffer | null
  blob(digest) -> string
  journal(id) -> string
  save(snapshot) -> void

  // Validation
  validateCurrent(files, expected) -> void
  restoreState(file, { allowBefore }) -> { file, isBefore, isAfter }
  validateRestore(id) -> Array
}

// Module-level exports
function guardPath(file) -> void  // throws on symlink/junction/hardlink
function within(root, relative) -> string  // resolves within root, guards path
function atomic(file, buffer, mode = 0o600) -> void
function nextText(item, previous, spec) -> string
function nextBuffer(item, previous, spec) -> Buffer

Import

const { SeatTransactions, guardPath, within, atomic } = require('./seat-transactions');

I/O Contract

Inputs

Name Type Required Description
seat string Yes One of PACK_IDS (for preview/verify)
id string Yes Plan id from preview (for deploy) or snapshot uuid (for restore)
root string Yes Absolute config directory (for select)
layout string No 'deepseek-harness' | 'zcode'
options.plan function No Injectable plan function (default: runtime.plan)
options.now function No Injectable clock (default: Date.now)
options.write function No Injectable atomic writer (default: atomic)

Outputs

Name Type Description
preview() returns object Plan with id, files (before/after hashes), changes count, packHash
deploy() returns object { ok, id, changed, root, backup (journal path), message }
verify() returns object { ok (changes===0), checks: [{path, ok, exists}], modelStatus }
restore() returns object { ok, restored (count), message }
history() returns Array Snapshots sorted by date descending

Usage Examples

Full Install Lifecycle

const { SeatTransactions } = require('./seat-transactions');
const { detectDirectory } = require('./detect-directory');

// 1. Select target directory
const found = detectDirectory('claude');
const tx = new SeatTransactions();
tx.select(found.root, { layout: found.layout });

// 2. Preview — compute before/after state (no writes)
const plan = tx.preview('claude');
console.log(`Plan ${plan.id}: ${plan.changes} files to change`);
for (const file of plan.files) {
  console.log(`  ${file.path}: ${file.changed ? 'CHANGED' : 'unchanged'} (${file.bytes} bytes)`);
}

// 3. Deploy — atomic write with backup
const deployed = tx.deploy(plan.id);
if (deployed.ok) {
  console.log(`Deployed ${deployed.changed} files, backup: ${deployed.backup}`);
}

// 4. Verify — re-check all files match
const verified = tx.verify('claude');
console.log(`Verified: ${verified.ok}`);

Uninstall via Restore

const { SeatTransactions } = require('./seat-transactions');

const tx = new SeatTransactions();
tx.select('/home/user/.claude');

// Find all applied snapshots for this seat
const history = tx.history();
const applied = history.filter(h => h.seat === 'claude' &&
  ['applied', 'preparing', 'restoring', 'partial'].includes(h.status));

if (applied.length > 0) {
  const result = tx.restore(applied[0].id);
  console.log(`Restored ${result.restored} files: ${result.message}`);
}

Use guardPath for Path Safety

const { guardPath, within } = require('./seat-transactions');

// Rejects if path contains symlinks or hardlinks
guardPath('/home/user/.claude/CLAUDE.md');

// Resolves a relative path within a root, guarding the result
const file = within('/home/user/.claude', 'rules/cha-breakopen.md');
// file: /home/user/.claude/rules/cha-breakopen.md

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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