Skip to content

fix(backend): Settlement and TransactionStatus update the existing transaction - #13

Merged
alpha912 merged 2 commits into
mainfrom
alpha/trusting-darwin-kdwngg
Oct 23, 2026
Merged

alpha912 merged 2 commits into
mainfrom
alpha/trusting-darwin-kdwngg

Conversation

@alpha912

Copy link
Copy Markdown
Contributor

Summary

The bug. POST /api/v1/transaction inserted a new row for every @type, using the id the payload names. So a Settlement or TransactionStatus for an existing transaction always answered 409 Transaction with this id already exists, and its status never changed. The code comment said "a recorded settlement marks the referenced transaction as settled", but that couldn't happen. For an unknown id, a Settlement instead created an orphan "completed" transaction that never had a MaterialTransaction.

The fix (apps/backend/src/db/loop.ts, src/routes/transactions.ts):

  • MaterialTransaction creates the transaction, as before. A duplicate id is still a 409.
  • Settlement and TransactionStatus lock the existing row (SELECT … FOR UPDATE) and update its status. The stored payload stays the original MaterialTransaction. The Settlement or TransactionStatus body is recorded in a transaction.settled / transaction.status_updated loop event and a status-updated evidence entry, in the same database transaction.
  • Unknown transaction_id: 404 NOT_FOUND, with details.searched_id. Nothing is created.
  • Rules from SPECIFICATION.md §3.6 (terminal states are completed, cancelled and disputed):
    • Moving from a terminal state back to a non-terminal one returns 409 CONFLICT. The spec says MUST NOT, and that nodes SHOULD answer 409.
    • Settling a transaction that is already terminal returns 409 CONFLICT.
    • Moving between two terminal states is allowed, for example disputing a completed transaction. The spec forbids only a return to a non-terminal state.
    • The spec leaves ordering between non-terminal states to implementations, so it is not enforced.
  • protocol/openapi.json now documents the update semantics and the 400/404/409 responses. Only 201 was listed before. The site mirror is re-synced.

Also in this PR (separate commit): a gate fix. The mirror drift check I added in MycelAI/LocalLOOP#8 failed scripts/ci-local.sh on any uncommitted mirror change. A correctly re-mirrored protocol edit, like the openapi.json change here, therefore couldn't pass the local gate before it was committed. The check now compares the mirror's file contents before and after aggregate-docs.sh. It still fails on new, edited or deleted protocol files that were never re-mirrored; I tested both drift cases. conformance.yml keeps its clean-checkout git status check.

Tree(s) touched

  • apps/backend
  • apps/site (mirror re-sync only)
  • protocol (openapi.json, CHANGELOG.md)
  • ops/org-profile
  • ops/ledger, scripts/, .github/ or root files (scripts/ci-local.sh, CHANGELOG.md)

How was this tested?

  • bash scripts/ci-local.sh (all targets): pass.
  • Backend bun test against a scratch localloop_test database: 378 pass, 0 fail.
  • Three new DB-backed tests in tests/transactions.routes.test.ts. All three fail on the old code:
    • pending → Settlement gives 201 and status completed, with one loop event and one evidence entry;
    • Settlement for an unknown id gives 404 and no row;
    • pending → confirmed → completed, then in_transit gives 409, a second Settlement gives 409, and disputed gives 201.
  • Mirror check probes: an unmirrored new example and an unmirrored edit both fail the gate. A re-mirrored but uncommitted change passes.

Checklist

  • bash scripts/ci-local.sh passes locally.
  • Tests added or updated where behaviour changed. Backend tests ran against a scratch database, never the default .env.
  • protocol/ changed: the site mirror was re-synced, check:conformance passes, and git status --short shows no deletions.
  • CHANGELOG.md Unreleased updated in backend, protocol and root.
  • No secrets, .env files, dumps or apps/backend/data/ content included.
  • Outward-facing wording stays "lab demo only".
  • Commit messages follow Conventional Commits and contain no tool attribution or co-author trailers for tools.
  • Contributed under the licence of the area touched.

Generated by Claude Code

…itted changes

ci-local's conformance target failed on any git status output under the
site mirror, so a correctly re-mirrored protocol edit could not pass the
local gate before it was committed. Compare the mirror's file contents
before and after aggregate-docs.sh instead: new, edited and deleted
protocol files that were never re-mirrored still fail.
…ansaction

POST /api/v1/transaction INSERTed every @type under its id, so a
Settlement or TransactionStatus for an existing transaction always
answered 409 and never changed its status, while one for an unknown id
created an orphan completed transaction.

They now lock and update the existing row, recording a
transaction.settled / transaction.status_updated loop event and a
status-updated evidence entry: 404 for an unknown transaction, 409 for
leaving a terminal state or settling a terminal transaction again
(SPECIFICATION.md §3.6). openapi.json documents the update semantics and
the 400/404/409 responses; the site mirror is re-synced.
@alpha912
alpha912 merged commit c054df2 into main Oct 23, 2026
5 checks passed
@alpha912 alpha912 mentioned this pull request Oct 23, 2026
9 of 13 tasks
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.

2 participants