fix: honest response when a transaction rollback cannot complete
When a transaction failed and its rollback ALSO failed to undo a canonical write (retries exhausted), the old path logged, continued, threw TransactionRollbackError, and set state='rolled_back' — while the record it could not undo stayed durable on disk. The caller got an error implying the write was undone; a read-back showed the record. A failed rollback had no truthful response (the post-commit response lie). Give a failed rollback a two-branch honest contract, decided by observation: both commit paths already hold byte-identical before-images, so after a failed rollback the store compares current canonical state to them and classifies each touched record as reconciled, an additive orphan (present when it should be gone), or a restorative loss (gone/wrong when it should have been restored). - Adopt-forward: a single-op write whose only damage is a durably-present orphan — the record the caller asked for — is committed forward (its generation buffered) and returns success with a loud warning that the derived index may be incomplete for that id until the next rebuild/repairIndex(). No error, no double-write; the record is durable and get-able immediately. - Fail loud + quarantine: a multi-op batch, or ANY restorative loss, throws the new StoreInconsistentError naming every unreconciled record and its disposition, and puts the brain into write-quarantine (reads keep working, writes refused via assertWritable) until repairIndex() reconciles the derived indexes against canonical and lifts it. The counter is not advanced, and Transaction.rollback now reports state 'inconsistent' instead of the 'rolled_back' lie when any undo failed. New: StoreInconsistentError + UnreconciledRecord exported from the root; repairIndex() forces a rebuild and clears the quarantine. Regression tests/integration/rollback-trapdoor.test.ts injects index-add-throws + canonical-delete-undo-fails and pins adopt-forward (durable, get-able, not quarantined), fail-loud (StoreInconsistentError + quarantine + reads work + repairIndex lifts it), and the error's record naming.
This commit is contained in:
parent
036e56c9f9
commit
711d2f046a
7 changed files with 437 additions and 35 deletions
|
|
@ -219,15 +219,22 @@ export class Transaction implements TransactionContext {
|
|||
}
|
||||
}
|
||||
|
||||
this.state = 'rolled_back'
|
||||
// Honest terminal state: only claim 'rolled_back' when EVERY undo applied.
|
||||
// If any undo failed, the store is not cleanly rolled back — mark
|
||||
// 'inconsistent' so the commit orchestration reconciles rather than trusts
|
||||
// a false 'rolled_back'. (Fixes the state half of the post-commit response
|
||||
// lie: the record whose undo failed is still durable.)
|
||||
this.state = rollbackErrors.length > 0 ? 'inconsistent' : 'rolled_back'
|
||||
this.endTime = Date.now()
|
||||
|
||||
if (this.options.logging) {
|
||||
const duration = this.endTime - (this.startTime || this.endTime)
|
||||
prodLog.info(`[Transaction] Rolled back in ${duration}ms`)
|
||||
prodLog.info(`[Transaction] ${this.state === 'inconsistent' ? 'Rollback INCOMPLETE' : 'Rolled back'} in ${duration}ms`)
|
||||
}
|
||||
|
||||
// If rollback encountered errors, wrap them with original error
|
||||
// If rollback encountered errors, wrap them with original error. The
|
||||
// orchestration keys on `instanceof TransactionRollbackError` to know the
|
||||
// undo did not fully apply and reconciliation is required.
|
||||
if (rollbackErrors.length > 0) {
|
||||
throw new TransactionRollbackError(
|
||||
`Transaction rollback encountered ${rollbackErrors.length} errors during cleanup`,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue