Skip to content

Action journal, backups, and undo core (codeburn act) #603

Closed

Description

@iamtoruk

Action journal, backups, and undo core (codeburn act)

Part of the acting-layer epic. This is the foundation: nothing in the epic may modify a file except through this module.

What to build

A small, dependency-free action framework in src/act/:

src/act/types.ts      # ActionRecord, ActionKind, FileChange types
src/act/journal.ts    # append/read/update the JSONL journal
src/act/backup.ts     # snapshot files before modification
src/act/apply.ts      # runAction(): backup -> mutate -> journal, atomically
src/act/undo.ts       # restore backups, mark record undone
src/act/cli.ts        # `codeburn act list|undo|report` wiring (report lands in a later issue)

Register the act command in src/main.ts following the existing commander patterns (see how optimize is registered around line 1325).

Storage layout

Everything under the existing CodeBurn home directory (reuse the same resolution used by src/config.ts, do not invent a new location):

~/.codeburn/actions/journal.jsonl
~/.codeburn/actions/backups/<action-id>/<n>.bak       # numbered, mapping stored in the record

Data model

export type ActionKind =
  | 'mcp-remove' | 'mcp-project-scope'
  | 'archive-skill' | 'archive-agent' | 'archive-command'
  | 'claude-md-rule' | 'shell-config'
  | 'guard-install' | 'guard-uninstall'
  | 'model-default'

export type FileChange = {
  path: string            // absolute path modified
  backup: string | null   // backups/<id>/<n>.bak, null if the file did not exist before
  op: 'edit' | 'create' | 'move'
  movedTo?: string        // for op: 'move' (archives)
}

export type ActionRecord = {
  id: string              // crypto.randomUUID()
  at: string              // ISO timestamp
  kind: ActionKind
  findingId: string | null  // stable finding id (added in the --apply issue)
  description: string     // one human sentence, shown in `act list`
  changes: FileChange[]
  status: 'applied' | 'undone'
  undoneAt?: string
  baseline?: Record<string, number>  // metric snapshot, used by the measurement issue; write null-safe, read-optional
}

Journal is append-only JSONL; status flips are written as a full replacement line for the same id (last line wins on read). Keep the reader tolerant: skip unparseable lines, never crash on a corrupt journal.

Behaviors

runAction(plan): Promise<ActionRecord> in apply.ts is the only mutation path. Order: create backup dir, snapshot every file in the plan, perform mutations, append journal record. If any mutation throws midway, restore the already-snapshotted files before rethrowing, and do not journal a partial apply.

codeburn act list: table of records, newest first: short id (first 8 chars), date, description, status. --json for the full records. Empty state prints a one-line hint.

codeburn act undo <id> (accepts the 8-char prefix) and codeburn act undo --last:

  • Refuse if the record is already undone.
  • For each change, compare the current file hash against the post-apply hash stored in the record (add afterHash to FileChange, sha256). If a file was modified since apply, refuse with a clear message listing the drifted files; --force overrides per the user's choice.
  • edit: restore backup bytes. create: delete the created file. move: move back.
  • Flip status to undone, journal it.

Concurrency: take a simple lockfile (~/.codeburn/actions/.lock, pid + timestamp, stale after 60s) around apply and undo.

Out of scope here

No actual optimize actions, no hooks, no report. This issue is complete when the framework and the two CLI verbs work end to end with tests.

Tests (vitest, tests/act-journal.test.ts, tests/act-undo.test.ts)

Use a temp dir via fs.mkdtemp and point the module at it (make the actions root injectable for tests, defaulting to the real home path).

  • apply journals a record with backups and afterHash for edit, create, and move ops
  • undo restores byte-identical content for each op type, flips status, and a second undo refuses
  • undo refuses when the target drifted after apply; --force proceeds
  • mid-apply failure (make the second mutation throw) leaves the first file restored and no journal record
  • corrupt journal line is skipped, remaining records still load
  • act list --json round-trips the record shape

Acceptance

  • npm run build clean, all tests pass, zero new dependencies.
  • codeburn act list and codeburn act undo work against a manually crafted record.
  • No behavior change to any existing command.

Part of epic #602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions