fix(store-and-forward): immediate delivery never overtakes buffered rows of its lane (SF-1 follow-up, decision B)

SF-1 kept the retry sweep per-(category,target) FIFO, but a NEW message could
still be delivered inline while older rows of the same lane were buffered and
backing off: EnqueueAsync's immediate attempt, and the first attempt that
ExternalSystem.CachedCall / Database.CachedWrite make themselves before handing
a transient failure to S&F. A recovered target then received B before A, and
A's later retry overwrote B.

Now each of those paths first probes the lane (StoreAndForwardService.
LaneHasPendingAsync: SELECT EXISTS seek on the covering idx_sf_messages_lane,
pinned by an EXPLAIN QUERY PLAN test; fails open to the old behaviour). An
occupied lane gets no inline attempt: the message is buffered due-immediately
(LastAttemptAt null, deferToSweep) behind the older rows and the sweep delivers
in order. Empty lanes (or parked-only) are unchanged. Notify.Send already
deferred to the sweep. Return contract unchanged: handle/TrackedOperationId at
once, WasBuffered=true, tracking reads Pending.

Regression tests (fail before, pass after): service, CachedCall, CachedWrite;
plus lane-probe + query-plan storage tests. Component-StoreAndForward.md and
Component-ExternalSystemGateway.md updated.
This commit is contained in:
Joseph Doherty
2026-12-03 12:22:31 -05:00
parent 3f98d40fa4
commit 8267ab8ffc
10 changed files with 484 additions and 74 deletions
@@ -69,6 +69,7 @@ Each database connection definition includes:
- Script calls `Database.CachedWrite("name", "sql", parameters)`. This is **deferred delivery**: the call returns a `TrackedOperationId` tracking handle immediately rather than the write result.
- Payload includes: connection name, SQL statement, serialized parameter values.
- The write is attempted immediately. On immediate success it is recorded as a terminal `Delivered` tracking record. On **transient failure** (database unavailable) it is buffered (`Pending`/`Retrying`) and retried per the connection's retry settings by the Store-and-Forward Engine.
- **Per-connection order (2026-12-02 review, decision B):** when writes to the same connection are already buffered, the new write is **not** attempted immediately — it is buffered behind them (`Pending`) and the Store-and-Forward sweep applies them in enqueue order, so a recovered database never sees a newer write before an older one. See [Component-StoreAndForward.md](Component-StoreAndForward.md) → Message Lifecycle.
- On **permanent failure** (e.g. a SQL syntax or constraint error — a request that will never succeed), the error is returned **synchronously** to the calling script and the write is **not** buffered. The call is also recorded as a terminal `Failed` tracking record capturing the error.
- Cached-write status is observable to scripts via `Tracking.Status(id)` (answered site-locally and authoritatively) and centrally via the Site Call Audit component.
- **Audit emission**: each lifecycle transition (`CachedEnqueued`, `CachedAttempt`, `CachedTerminal`) emits an audit row via the combined cached-operation telemetry packet — one packet carries both the audit row and the SiteCalls upsert (see [Component-AuditLog.md](Component-AuditLog.md), Cached Operations — Combined Telemetry, and [Component-SiteCallAudit.md](Component-SiteCallAudit.md)). Audit-write failure never aborts the script.
@@ -96,6 +97,7 @@ Scripts choose between two call modes per invocation, mirroring the dual-mode da
### Cached (Store-and-Forward)
- Script calls `ExternalSystem.CachedCall("systemName", "methodName", params)`. This is **deferred delivery**: the call returns a `TrackedOperationId` tracking handle immediately rather than the response body.
- The call is attempted immediately. If it succeeds, the response is discarded and the call is recorded as a terminal `Delivered` tracking record.
- **Per-system order (2026-12-02 review, decision B):** when calls to the same external system are already buffered, the new call makes **no** immediate HTTP request — it is buffered behind them (`Pending`) and delivered by the Store-and-Forward sweep in enqueue order, so a recovered system never receives a newer call before an older one. See [Component-StoreAndForward.md](Component-StoreAndForward.md) → Message Lifecycle.
- On **transient failure** (connection refused, timeout, HTTP 5xx), the call is routed to the Store-and-Forward Engine for retry per the system's retry settings. The script does **not** block — the call is buffered (`Pending`/`Retrying`) and the script continues.
- On **permanent failure** (HTTP 4xx), the error is returned **synchronously** to the calling script. No retry — the request itself is wrong. The call is also recorded as a terminal `Failed` tracking record capturing the error.
- Cached-call status is observable to scripts via `Tracking.Status(id)` (answered site-locally and authoritatively) and centrally via the Site Call Audit component.