fix: dispatch guard and Defect-safe rollback (#32)

A readable intent that fails in handling now always gets exactly one ack:
the new static internal code, unless the handler's own ack was already
enqueued. Both transaction helpers roll back on every unwind path,
Defect included, so the shared SQLite connection never keeps an open
transaction. Test-only dispfault build covers catchable/defect/after-ack;
DESIGN 6.5 records internal as an owner decision and 6.2 names refusal
codes as additive.
This commit is contained in:
2026-11-20 21:36:44 +01:00
parent 38941da003
commit a5c0ee5d4c
9 changed files with 246 additions and 12 deletions
+8 -3
View File
@@ -289,13 +289,18 @@ template withTransaction*(db: DbConn, body: untyped) =
## One explicit transaction (DESIGN 4.2 step 6): COMMIT on success, ROLLBACK and re-raise on any
## failure. A template rather than a proc taking a closure: call sites run under the global lock
## inside `{.gcsafe.}` handlers, where a closure would need its own gcsafe proof.
## #32: the rollback is not conditional on the error's class. A `Defect` unwinds like any other
## failure, and a shared connection must never keep the BEGIN for the next caller.
db.exec(sql"BEGIN")
var open = true
try:
body
db.exec(sql"COMMIT")
except CatchableError:
try: db.exec(sql"ROLLBACK")
except CatchableError: discard # the original error is the one worth reporting
open = false
except Exception:
if open:
try: db.exec(sql"ROLLBACK")
except Exception: discard # the original error is the one worth reporting
raise
proc appliedMigrations*(db: DbConn): seq[MigrationRow] =