Skip to content

Commit 1f0337f

Browse files
rustyconoverclaude
andcommitted
function_storage: add unified state_* API alongside existing methods (Turn 1)
This is Turn 1 of a 5-turn refactor to collapse 4 RMW method families (worker_*, stream_state_*, aggregate_state_*, aggregate_window_partition_*) plus transaction_state_* into one composite-key state_* API. The new API takes (scope_id: bytes, ns: bytes, key: bytes) so callers — not the storage — pick the namespace. Turn 1 introduces the new methods alongside the old ones; nothing existing is removed, no callers are migrated. Subsequent turns migrate fixtures (Turn 2-3), extend to cloud backends (Turn 4), and delete the deprecated families (Turn 5). API surface added: - state_get_many(scope, ns, keys) -> list[bytes | None] - state_put_many(scope, ns, items) — atomic per-call - state_scan(scope, ns) — non-destructive - state_drain(scope, ns) — atomic scan-and-tombstone - state_delete(scope, ns, keys=None) — naturally idempotent - execution_clear(scope) — wipes all namespaces + future log table - BoundStorage facade wrappers (state_get/put singular convenience, state_scan/drain/delete pass-through, pack_int_key sugar) New tables: - function_state(scope_id, ns, key, value, last_attempt_id, drained_at, drained_by_attempt, created_at) - function_state_log(id AUTOINCREMENT, scope_id, ns, key, value, attempt_id, created_at) — used in Turn 6 (state_append) Idempotency: SQLite generates an internal attempt_id per call (mirrors the CfDo HTTP retry-resume pattern). Replay-detection on state_put_many checks last_attempt_id of the first item; replay-detection on state_drain checks drained_by_attempt and returns the prior tombstoned values. state_delete and execution_clear are naturally idempotent. cleanup_old_entries now sweeps function_state and function_state_log past the retention horizon (matches existing per-table behavior). Tests: 17 new unit tests covering put/get/scan/drain/delete/clear roundtrip, namespace isolation, scope isolation, non-destructive scan, empty-input edge cases, facade wrappers, pack_int_key encoding. All 31 tests pass (14 existing + 17 new). Verified the C++ integration suite (vgi/test/sql/integration/*) still passes — 152 tests green, semantic of existing fixtures unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent acee931 commit 1f0337f

2 files changed

Lines changed: 609 additions & 0 deletions

File tree

‎tests/test_function_storage.py‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,143 @@ def test_default_db_path(self) -> None:
160160
storage = FunctionStorageSqlite()
161161
assert storage.db_path.endswith("vgi_storage.db")
162162
assert "vgi" in storage.db_path
163+
164+
# ========================================================================
165+
# Unified state_* API tests
166+
# ========================================================================
167+
168+
def test_state_put_many_get_many_roundtrip(self, storage: FunctionStorageSqlite) -> None:
169+
"""Batched put then batched get returns the values in input-key order."""
170+
scope = b"exec1"
171+
ns = b"agg"
172+
storage.state_put_many(scope, ns, [(b"k1", b"v1"), (b"k2", b"v2"), (b"k3", b"v3")])
173+
result = storage.state_get_many(scope, ns, [b"k2", b"k3", b"k1", b"missing"])
174+
assert result == [b"v2", b"v3", b"v1", None]
175+
176+
def test_state_put_many_overwrites_existing(self, storage: FunctionStorageSqlite) -> None:
177+
"""Re-put on the same (scope, ns, key) replaces the value."""
178+
scope = b"exec1"
179+
ns = b"agg"
180+
storage.state_put_many(scope, ns, [(b"k1", b"old")])
181+
storage.state_put_many(scope, ns, [(b"k1", b"new")])
182+
assert storage.state_get_many(scope, ns, [b"k1"]) == [b"new"]
183+
184+
def test_state_namespaces_are_isolated(self, storage: FunctionStorageSqlite) -> None:
185+
"""Same key in different namespaces is two distinct rows."""
186+
scope = b"exec1"
187+
storage.state_put_many(scope, b"ns_a", [(b"k", b"a-val")])
188+
storage.state_put_many(scope, b"ns_b", [(b"k", b"b-val")])
189+
assert storage.state_get_many(scope, b"ns_a", [b"k"]) == [b"a-val"]
190+
assert storage.state_get_many(scope, b"ns_b", [b"k"]) == [b"b-val"]
191+
192+
def test_state_scopes_are_isolated(self, storage: FunctionStorageSqlite) -> None:
193+
"""Same (ns, key) in different scope_ids is two distinct rows."""
194+
storage.state_put_many(b"exec1", b"agg", [(b"k", b"v1")])
195+
storage.state_put_many(b"exec2", b"agg", [(b"k", b"v2")])
196+
assert storage.state_get_many(b"exec1", b"agg", [b"k"]) == [b"v1"]
197+
assert storage.state_get_many(b"exec2", b"agg", [b"k"]) == [b"v2"]
198+
199+
def test_state_scan_returns_all_in_namespace(self, storage: FunctionStorageSqlite) -> None:
200+
"""state_scan emits every (key, value) for one (scope, ns)."""
201+
scope = b"exec1"
202+
ns = b"agg"
203+
storage.state_put_many(scope, ns, [(b"k1", b"v1"), (b"k2", b"v2"), (b"k3", b"v3")])
204+
storage.state_put_many(scope, b"other_ns", [(b"k1", b"other")])
205+
rows = sorted(storage.state_scan(scope, ns))
206+
assert rows == [(b"k1", b"v1"), (b"k2", b"v2"), (b"k3", b"v3")]
207+
208+
def test_state_scan_is_non_destructive(self, storage: FunctionStorageSqlite) -> None:
209+
"""Two consecutive scans return the same rows."""
210+
storage.state_put_many(b"exec1", b"agg", [(b"k", b"v")])
211+
first = storage.state_scan(b"exec1", b"agg")
212+
second = storage.state_scan(b"exec1", b"agg")
213+
assert first == second == [(b"k", b"v")]
214+
215+
def test_state_drain_empties_and_returns_rows(self, storage: FunctionStorageSqlite) -> None:
216+
"""state_drain removes rows from subsequent reads and returns them."""
217+
scope = b"exec1"
218+
ns = b"agg"
219+
storage.state_put_many(scope, ns, [(b"k1", b"v1"), (b"k2", b"v2")])
220+
drained = sorted(storage.state_drain(scope, ns))
221+
assert drained == [(b"k1", b"v1"), (b"k2", b"v2")]
222+
assert storage.state_scan(scope, ns) == []
223+
assert storage.state_get_many(scope, ns, [b"k1", b"k2"]) == [None, None]
224+
225+
def test_state_drain_doesnt_touch_other_namespaces(self, storage: FunctionStorageSqlite) -> None:
226+
"""Draining ns A leaves ns B intact."""
227+
scope = b"exec1"
228+
storage.state_put_many(scope, b"ns_a", [(b"k", b"a-val")])
229+
storage.state_put_many(scope, b"ns_b", [(b"k", b"b-val")])
230+
storage.state_drain(scope, b"ns_a")
231+
assert storage.state_scan(scope, b"ns_b") == [(b"k", b"b-val")]
232+
233+
def test_state_delete_specific_keys(self, storage: FunctionStorageSqlite) -> None:
234+
"""state_delete with a key list removes only those keys."""
235+
scope = b"exec1"
236+
ns = b"agg"
237+
storage.state_put_many(scope, ns, [(b"k1", b"v1"), (b"k2", b"v2"), (b"k3", b"v3")])
238+
deleted = storage.state_delete(scope, ns, [b"k1", b"k3"])
239+
assert deleted == 2
240+
assert storage.state_scan(scope, ns) == [(b"k2", b"v2")]
241+
242+
def test_state_delete_namespace(self, storage: FunctionStorageSqlite) -> None:
243+
"""state_delete with keys=None wipes the namespace."""
244+
scope = b"exec1"
245+
storage.state_put_many(scope, b"ns_a", [(b"k1", b"v1"), (b"k2", b"v2")])
246+
storage.state_put_many(scope, b"ns_b", [(b"k", b"v")])
247+
deleted = storage.state_delete(scope, b"ns_a")
248+
assert deleted == 2
249+
assert storage.state_scan(scope, b"ns_a") == []
250+
assert storage.state_scan(scope, b"ns_b") == [(b"k", b"v")]
251+
252+
def test_state_delete_naturally_idempotent(self, storage: FunctionStorageSqlite) -> None:
253+
"""Deleting an already-deleted row returns 0."""
254+
scope = b"exec1"
255+
ns = b"agg"
256+
storage.state_put_many(scope, ns, [(b"k", b"v")])
257+
assert storage.state_delete(scope, ns, [b"k"]) == 1
258+
assert storage.state_delete(scope, ns, [b"k"]) == 0
259+
260+
def test_execution_clear_wipes_all_namespaces(self, storage: FunctionStorageSqlite) -> None:
261+
"""execution_clear deletes every row across every namespace for a scope."""
262+
scope = b"exec1"
263+
storage.state_put_many(scope, b"ns_a", [(b"k1", b"v1"), (b"k2", b"v2")])
264+
storage.state_put_many(scope, b"ns_b", [(b"k", b"v")])
265+
storage.state_put_many(b"exec2", b"ns_a", [(b"k", b"v")])
266+
deleted = storage.execution_clear(scope)
267+
assert deleted == 3
268+
assert storage.state_scan(scope, b"ns_a") == []
269+
assert storage.state_scan(scope, b"ns_b") == []
270+
assert storage.state_scan(b"exec2", b"ns_a") == [(b"k", b"v")]
271+
272+
def test_execution_clear_naturally_idempotent(self, storage: FunctionStorageSqlite) -> None:
273+
"""Clearing an already-clear execution returns 0."""
274+
assert storage.execution_clear(b"never-existed") == 0
275+
276+
def test_state_get_many_empty_keys(self, storage: FunctionStorageSqlite) -> None:
277+
"""Empty key list returns empty result without touching SQL."""
278+
assert storage.state_get_many(b"exec1", b"agg", []) == []
279+
280+
def test_state_put_many_empty_items(self, storage: FunctionStorageSqlite) -> None:
281+
"""Empty item list is a no-op."""
282+
storage.state_put_many(b"exec1", b"agg", [])
283+
284+
# --- Facade (BoundStorage) tests ---
285+
286+
def test_facade_state_put_get_roundtrip(self, storage: FunctionStorageSqlite) -> None:
287+
"""BoundStorage.state_put / state_get convenience wrappers work."""
288+
from vgi.function_storage import BoundStorage
289+
290+
bs = BoundStorage(storage, b"exec1", attach_opaque_data=b"a")
291+
bs.state_put(b"agg", b"k1", b"v1")
292+
assert bs.state_get(b"agg", b"k1") == b"v1"
293+
assert bs.state_get(b"agg", b"missing") is None
294+
295+
def test_facade_pack_int_key(self) -> None:
296+
"""pack_int_key is little-endian, signed, 8 bytes — round-trips to int.from_bytes."""
297+
from vgi.function_storage import BoundStorage
298+
299+
for n in [0, 1, -1, 2**62, -(2**62)]:
300+
packed = BoundStorage.pack_int_key(n)
301+
assert len(packed) == 8
302+
assert int.from_bytes(packed, "little", signed=True) == n

0 commit comments

Comments
 (0)