fix: commit audit event before mutating the in-memory record - #17
Merged
Conversation
Bug review of EauDoon/consequence-rail found: - HIGH: propose, reserveRecourse, issuePermit, and acceptRecoveryQualification all mutate the in-memory action record (or the actions Map) BEFORE the eventStore.append fires. transition() is documented as failure-atomic (it appends then mutates record.state), but the calling code overwrites that property for its own pre-state writes. A failing event append leaves the record with reservation / permit / recovery_preflight fields set, but no STATE_TRANSITION or ACTION_PROPOSED event. The action is now permanently wedged because the rail cannot retroactively insert the missing event. - HIGH: PR #15 fixed the same class of bug in authorize and the transition helper, but the four methods above were not updated. Fix: in each method, build the new field values into a local variable first, perform the eventStore.append (or transition, which appends), and only commit the field writes to the record after the append returns successfully. propose appends the ACTION_PROPOSED event before calling this.actions.set. reserveRecourse and issuePermit call transition() before writing record.reservation / record.permit. acceptRecoveryQualification appends the RECOVERY_PREFLIGHT_ACCEPTED event before writing record.recovery_preflight. Verified: node --test test/rail.test.js runs 83 tests, all pass.
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
Comment on lines
+441
to
+444
| record.recovery_preflight = deepFreeze(deepClone(bundle)); | ||
| record.recovery_preflight_verification = deepFreeze( | ||
| deepClone(verification), | ||
| ); |
There was a problem hiding this comment.
🔴 Recovery acceptance can lose its data
An accessor-backed bundle can pass verification, then deepClone(bundle) fails after acceptance is recorded. The audit log claims acceptance while permit issuance remains blocked.
Prompt for agents
In src/rail.js, acceptRecoveryQualification verifies caller-owned bundle data, appends RECOVERY_PREFLIGHT_ACCEPTED, and only then clones the bundle. verifyRecoveryPreflight permits accessor-backed fields on the top-level bundle, while deepClone rejects accessors. A bundle whose schema_version getter returns the expected value can therefore pass verification, commit the event, and fail during the later clone, leaving no recovery_preflight fields. Create immutable owned snapshots of both the bundle and verification before appending. Use those snapshots for any event values and commit them to the record only after append succeeds. Add a deterministic regression test proving that any snapshot failure occurs before the event revision changes.
Was this helpful? React with 👍 or 👎 to provide feedback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug review of EauDoon/consequence-rail found:
Fix: in each method, build the new field values into a local variable first, perform the eventStore.append (or transition, which appends), and only commit the field writes to the record after the append returns successfully. propose appends the ACTION_PROPOSED event before calling this.actions.set. reserveRecourse and issuePermit call transition() before writing record.reservation / record.permit. acceptRecoveryQualification appends the RECOVERY_PREFLIGHT_ACCEPTED event before writing record.recovery_preflight.
Verified: node --test test/rail.test.js runs 83 tests, all pass.