Skip to content

replaceRule is not atomic: a crash between the two writes leaves two active rules #702

Backlog

Description

@davesheffer

Priority: high

Problem

replaceRule makes two separate writes. A crash, kill, or write error between them leaves the store half-changed, and nothing repairs it.

Where

src/core/lifecycle.ts:65-68 (in the lifecycle worktree). The draft successor is saved as active at line 65. The old rule is retired at lines 66-68. Each is its own putWhereItLives call. Both run before the function returns.

Failure scenario

hunch rule replace con_old --with con_new activates con_new, then the process dies (Ctrl-C, power loss, disk full, a locked file on Windows). con_old is still active and so is con_new. Both are enforced, and the old rule carries no "Superseded by" note. A re-run fails the check at line 61 only if con_new is retired. Otherwise it succeeds, but the user is never told the first run was partial. The reverse order would be worse: it would leave no active rule. The current order is the safer one, but it is still not safe.

Suggested fix

  • Retire the old rule and activate the new one in one store transaction or one batched write, if the store can do that.
  • If it cannot, make the operation recoverable. Write a "replace pending" marker on the old rule first, and let a re-run or hunch doctor finish it.
  • At minimum, make a re-run idempotent and print a clear message when next is already active and old is not yet retired.
  • Add a test that injects a failure on the second write.

Found while reviewing #701.

🤖 Generated with Claude Code

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions