Repository navigation
Read contract state as of the request's own time; refuse late state changes - #19
Merged
Merged
Conversation
…hanges trigger! handed every clause today's state and wrote the new state valid from the request's time with no end. A report arriving after later ones therefore computed from the future and overwrote every later state in valid time: with visits on Feb 2 and Feb 20 recorded, a late Jan 15 report made Jan 20 and Feb 10 both read back as three visits paid. - current-state and store/get-instance take :at; trigger! reads the state as of its own now. - A trigger whose new state would land before a state change already recorded is refused as :out-of-order (HTTP 409), checked before the write and again by an ASSERT inside the transaction. - Answer-only triggers (attestations) may still be back-dated. Replaying later executions to accept a late report is left as a design decision, since a replay can change answers already acted on. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Review of #19 found the in-transaction check only rejected state changes dated after the request, so two triggers dated at or before one another could both read the same state and one would silently drop the other's change. The transaction now asserts that no state has been written since the version the clause read, and trigger! re-runs the pure clause against the new state when it has (up to five attempts, then :state-conflict, 409). The order check stays a refusal, re-evaluated on each attempt. trigger! also normalises a non-string now (an Instant or java.util.Date from a REPL) to its ISO string, so a clause never holds a platform date and the order check no longer throws ClassCastException on a Date. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
After a concurrency retry, a concurrent copy of the same request may be the write that moved the state; its caller should hear duplicate-request with when it ran, not out-of-order. The ordinary resend case was already answered as a duplicate by trigger!'s early check; a test now pins that. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This was referenced Sep 26, 2026
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.
What
Answers the open question in AGENTS.md §6 about when
current-statereads the contract's state.The bug:
trigger!handed each clause the state as of today and wrote the new state valid from the request's own time with no end date. A late-arriving report therefore computed from later state and overwrote every later state change in valid time.Reproduced with the snow-removal contract (visits on Feb 2 and Feb 20 recorded on time, then a late Jan 15 report):
visitsPaidThe fix:
store/get-instanceandexecute/current-statetake:at;trigger!gives the clause the state as of the request's ownnow.:out-of-order, which reaches HTTP callers as 409.trigger!re-runs the pure clause against the new state; that retry also re-checks the order. It gives up after 5 attempts with:state-conflict(409). This closes a lost-update race that existed before this PR too: two triggers read the same state and the later write drops the other's change.nowgiven as anInstantorjava.util.Date(e.g. at a REPL) is normalised to its ISO string, so a clause never holds a platform date.Review fixes (second commit): the concurrency race and
Datehandling above were both found by code review of the first revision.Why refuse rather than replay
Accepting a late report properly means re-running every later execution in valid-time order. That's possible because clauses are pure. But a replay can change answers already acted on (a payout already made), so it's a decision for a person, not a side effect of a trigger. Replay is noted in AGENTS.md as a design item, next to aligning with Accord's proposed
runtime@1.0.0.AgreementStateandobligation@1.0.0.ObligationTransition(accordproject/models#200).Verification
execute_testcases:ASSERTexercised directly, so a mistake in its SQL can't hide behind the query that usually runs firstnowas ajava.util.Dateapp/trigger_test: 200 on time, 409out-of-orderfor a late report, 409duplicate-requestfor a repeat.bin/clj -M:run code-quality: 52 tests, 181 assertions, 0 failures; lint clean.dev/certification_demo.clj,dev/snow_removal_demo.cljanddev/late_delivery_demo.cljrun unchanged.409 out-of-order; AGENTS.md §6 and §8 are updated.🤖 Generated with Claude Code