Skip to content

Read contract state as of the request's own time; refuse late state changes - #19

Merged
tbrooke merged 3 commits into
mainfrom
fix/state-as-of-request-time
Sep 24, 2026
Merged

tbrooke merged 3 commits into
mainfrom
fix/state-as-of-request-time

Conversation

@tbrooke

@tbrooke tbrooke commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Answers the open question in AGENTS.md §6 about when current-state reads 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):

date asked correct visitsPaid before this PR
Jan 20 1 3
Feb 10 2 3
Mar 1 3 3

The fix:

  • store/get-instance and execute/current-state take :at; trigger! gives the clause the state as of the request's own now.
  • A trigger whose new state would land before a state change already recorded is refused with :out-of-order, which reaches HTTP callers as 409.
  • Concurrent triggers are guarded optimistically. The transaction asserts that no state has been written for the contract since the version the clause read, at any valid time. If one has, 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.
  • now given as an Instant or java.util.Date (e.g. at a REPL) is normalised to its ISO string, so a clause never holds a platform date.
  • Triggers that only answer (attestations) can still be back-dated. They read the state as of their own time and write none, so a Clerk certifying a months-old vote still works.
  • A state change at the same instant as the latest one still lands.

Review fixes (second commit): the concurrency race and Date handling 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.AgreementState and obligation@1.0.0.ObligationTransition (accordproject/models#200).

Verification

  • New execute_test cases:
    • state changing three times and reading back correctly at each date
    • a late state change refused, with nothing written and the request id not consumed
    • a same-instant change accepted
    • a back-dated answer-only trigger seeing its own time's state
    • the in-transaction ASSERT exercised directly, so a mistake in its SQL can't hide behind the query that usually runs first
    • 8 concurrent triggers on one contract: every execution that lands is counted, and the rest are refused as out-of-order, never lost. This fails 3/3 against this PR's first revision and passes 5/5 now.
    • now as a java.util.Date
  • Against the old implementation, the scenario tests fail with 11 failures.
  • New app/trigger_test: 200 on time, 409 out-of-order for a late report, 409 duplicate-request for a repeat.
  • bin/clj -M:run code-quality: 52 tests, 181 assertions, 0 failures; lint clean.
  • dev/certification_demo.clj, dev/snow_removal_demo.clj and dev/late_delivery_demo.clj run unchanged.
  • The walkthrough's HTTP error table gains 409 out-of-order; AGENTS.md §6 and §8 are updated.

🤖 Generated with Claude Code

tbrooke and others added 3 commits September 24, 2026 11:45
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant