Skip to content

Commit 81c27ca

Browse files
rustyconoverclaude
andcommitted
state_append: tighten docstring on idempotency scope (review §1)
The previous docstring said "replay returns prior ordinal" without qualifying scope, which would mislead a future caller into assuming end-to-end exactly-once. The internal attempt_id only protects *transport-layer* retries within one backend call: * CfDo's HTTP retry loop sits above attempt_id generation, so retried requests carry the same id and the server replays correctly. * Azure SQL's _execute_with_retry reruns the closure with the captured id, so DB-driver retries replay correctly. * SQLite has no retry layer above this method at all; the INSERT OR IGNORE + SELECT-by-attempt_id pattern is currently unreachable from any caller (the public signature generates a fresh UUID4 per call). Pattern is kept for symmetry with the other backends and to leave room for future caller-supplied attempt_id. Caller-level retries generate a fresh internal id and produce duplicate rows. Documented on both FunctionStorage.state_append (Protocol) and BoundStorage.state_append (facade). Updated the misleading "On conflict the row already exists" comment in the SQLite implementation to reflect that the conflict path is currently unreachable. Test: pin the duplicate-on-caller-retry behavior so any future change that exposes caller-supplied attempt_id will deliberately break it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f4ac15e commit 81c27ca

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎tests/test_function_storage.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ def test_state_append_returns_monotone_ordinals_per_key(self, storage: FunctionS
260260
ord3 = storage.state_append(scope, b"buf", b"k", b"c")
261261
assert ord1 < ord2 < ord3
262262

263+
def test_state_append_caller_retry_produces_duplicates(self, storage: FunctionStorageSqlite) -> None:
264+
"""Caller-level retry inserts a duplicate row (fresh internal attempt_id).
265+
266+
This is the limitation called out in state_append's "Idempotency
267+
scope" docstring section. Callers who need exactly-once semantics
268+
across their own retries must dedupe externally (check
269+
``state_log_scan`` first, or key the namespace on a content hash).
270+
Pinning this behavior in a test so any future change to expose
271+
caller-supplied attempt_id will deliberately break it.
272+
"""
273+
scope = b"exec-log"
274+
storage.state_append(scope, b"buf", b"k", b"payload")
275+
storage.state_append(scope, b"buf", b"k", b"payload") # caller retry
276+
assert storage.state_log_scan(scope, b"buf", b"k") == [b"payload", b"payload"]
277+
263278
def test_state_log_scan_isolates_keys(self, storage: FunctionStorageSqlite) -> None:
264279
"""Logs for distinct keys in the same namespace stay separate."""
265280
scope = b"exec-log"

‎vgi/function_storage.py‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,18 @@ def state_append(
362362
Per-key order is recovered via the ``(scope_id, ns, key, id)`` index;
363363
``state_log_scan`` yields rows in id order, which corresponds to
364364
append order. Concurrent appenders to the *same* key get distinct
365-
ordinals but interleaving is undefined.
366-
367-
Idempotent on retry: if an earlier attempt with the same
368-
backend-internal ``attempt_id`` already inserted a row, returns the
369-
prior ordinal without re-inserting.
365+
ordinals but interleaving across writers is undefined.
366+
367+
**Idempotency scope.** The internal ``attempt_id`` covers
368+
*transport-layer* retries within a single backend call: an HTTP
369+
retry on CfDo carries the same id and replays correctly; a
370+
pymssql ``OperationalError`` retry on Azure SQL replays correctly.
371+
**Caller-level retries** (re-invoking ``state_append`` for the
372+
same logical record after the call already returned) generate a
373+
fresh id and produce duplicate rows. If you need caller-level
374+
idempotency, dedupe on the caller side — e.g., check
375+
``state_log_scan`` before appending, or key your namespace on a
376+
stable content hash.
370377
"""
371378
...
372379

@@ -602,7 +609,14 @@ def execution_clear(self) -> int:
602609
)
603610

604611
def state_append(self, ns: bytes, key: bytes, item: bytes) -> int:
605-
"""Append an item to the (ns, key) log; return the assigned ordinal."""
612+
"""Append an item to the (ns, key) log; return the assigned ordinal.
613+
614+
Idempotency covers transport-layer retries only (HTTP retry on
615+
CfDo, pymssql driver-level retry on Azure SQL). Caller-level
616+
retries — re-invoking ``state_append`` for the same logical
617+
record after it returned — produce duplicate rows. See the
618+
underlying ``FunctionStorage.state_append`` for the full contract.
619+
"""
606620
return self._base.state_append(
607621
self._execution_id, ns, key, item, shard_key=self._shard_key
608622
)
@@ -1099,16 +1113,21 @@ def state_append(
10991113
*,
11001114
shard_key: str = "",
11011115
) -> int:
1102-
"""Append item; return ordinal. Replay returns prior ordinal via UNIQUE."""
1116+
"""Append item; return ordinal. Defensive against transport-layer replay only."""
11031117
del shard_key
11041118
import uuid
11051119

11061120
attempt_id = uuid.uuid4().bytes
11071121
conn = self._conn()
1108-
# INSERT OR IGNORE on the UNIQUE(scope, ns, key, attempt_id) index
1109-
# is the replay primitive. On conflict the row already exists from a
1110-
# prior call with this attempt_id; cur.lastrowid is unreliable in
1111-
# that case (SQLite returns 0), so we always SELECT back by attempt_id.
1122+
# INSERT OR IGNORE + SELECT-by-attempt_id is structurally a replay
1123+
# primitive but currently has no path to fire: SQLite has no retry
1124+
# layer above this method, and the public state_append signature
1125+
# generates a fresh UUID4 per call (no caller-supplied attempt_id).
1126+
# The pattern is here for symmetry with the Azure SQL / CfDo
1127+
# implementations and to leave the door open for future exposure
1128+
# of caller-level attempt_id. Could be simplified to
1129+
# `INSERT ... RETURNING id` (SQLite ≥3.35) if we commit to never
1130+
# exposing that contract — would save one SQL round-trip per call.
11121131
conn.execute(
11131132
"""
11141133
INSERT OR IGNORE INTO function_state_log

0 commit comments

Comments
 (0)