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
+29 -1
View File
@@ -2,7 +2,7 @@
## The hook is compiled in only with `-d:leakfault`, and every fault checks
## `LEAK_FAULT=<name>` from the environment: a normal build cannot fault, it contains no `LEAK_FAULT`
## string, and tools/fault.test.mjs asserts that the shipped binary's bytes lack it.
when defined(leakfault):
when defined(leakfault) or defined(dispfault):
import std/os
proc faultActive*(name: string): bool =
@@ -18,3 +18,31 @@ proc crashNow*() =
## transactions (the `markerCrash` hook, TESTING 10.3, #60). A shipped build has no death path.
when defined(leakfault):
quit(70)
when defined(dispfault):
var txnFaultFired = false
var ackFaultFired = false
proc injectTxn*() =
## #32 only: raise from inside a mutation that holds the database transaction, once per process.
## The build define and the environment variable are both required, so a shipped binary has no
## raise site at all.
when defined(dispfault):
if txnFaultFired: return
case getEnv("DISP_FAULT")
of "txn-catchable":
txnFaultFired = true
raise newException(ValueError, "injected fault")
of "txn-defect":
txnFaultFired = true
raise newException(Defect, "injected fault")
else: discard
proc injectAfterAck*() =
## #32 only: fail after the handler has already enqueued its ack, once per process, to prove the
## guard never doubles an existing ack. A shipped binary has no raise site.
when defined(dispfault):
if ackFaultFired: return
if getEnv("DISP_FAULT") == "after-ack":
ackFaultFired = true
raise newException(Defect, "injected fault")