Files
turnstone/tests/test_sdk_sync.py
T
Patrick Buckley 480a1426b3 Fail-closed history-commit handoff (#1005)
* fix(session): fail-closed history-commit handoff (#981)

The deleted-workstream discovery is now a terminal, ws_id-keyed latch:
keyed conversation commits refuse admission once the durable parent is
gone (convergence finalizers and force-abandon are exempt), history
handoff refuses to mint a proof token so /history fails closed with a
503 instead of silently wiping the pane, and the SSE stream carries a
workstream_gone resync reason. Discarded commits leave a forensic log
of commit keys and roles, never content.

Conversation rows gain a commit_key (migration 071): keyed saves are
idempotent under retry, validated against the full commit identity, and
refused when they would cross a workstream deletion. The prune orphan
category now requires a NULL alias plus a two-hour updated grace, with
cutoffs computed at discovery time and carried into both dialects'
rechecks.

The mid-turn interjection queue is owner-partitioned with no per-site
mode flags: pops take the acting principal's and unowned rows, other
participants' rows are structurally retained, and enforcement lives at
queue admission plus the shared before_spawn gates. The retraction
ledger is bounded by open pop windows: pops open a window atomically
with the queue delete, restores close their ids atomically with the
ledger consume, every other exit closes through one helper, and misses
for unheld ids record nothing. The workstream-gone latch refuses
unattended wakes at all three gates (watcher spawn, claim, delivery
pre-pop), and the retry dispatcher regained its pre-envelope
cancel/error convergence net.

Persistence-state reporting derives through the session bound to each
UI instead of a registry lookup by id that failed open to healthy
during tombstone retention. The dashboard roster no longer re-inserts
ghost entries from trailing activity events, the history tool-outcome
scan tolerates interleaved non-turn rows, and the shared
handoff-deadline handle owns its own retirement.

Single-sourced across call sites: keyed-commit row values, attachment
save wrappers, tail-truncation and conflict-resolution bodies for both
storage dialects; worker-slot lifecycle field sets; the direct-commit
admission frame; queued-row layout accessors; the string-aware comment
stripper shared by every JS harness suite.

Refs #981 #964

* fix(session): sweep handoff fixes to their sibling surfaces

The interactive replay loop treated a system row as a tool-batch
boundary, so every tool result after an interleaved row vanished from
that pane while the coordinator rendered the same history correctly.
Only a conversational turn ends the batch window now, matching the
shared outcome index.

Accepted user turns clear the composer's attachment chips on the same
viewer policy that settles optimistic bubbles rather than on having
matched a local bubble, so a workstream created with an upload no
longer keeps a chip for an attachment the create dispatch already
consumed. The coordinator's raced-Stop arm emits the stream-end hook it
inherits alongside the idle state, leaving no unfinalized bubble or
unflushed tool output. Ending a session surfaces a failure toast when
the request never lands or answers with a non-JSON body.

The per-second persistence reconcile now probes each session without
blocking: a workstream whose generation and handoff locks are held is
skipped until the next pass instead of contending the locks every
commit needs. The one-shot repair that gates workstream creation at
capacity keeps a definite probe — it has no next pass, and the sessions
likeliest to be contended are the ones whose unresolved journals
emptied its candidate list.

Single-sourced: the attachment lane builds its conversation row through
the shared commit-identity builder; the ordinary worker exit releases
its slot through the lifecycle owner; both operator surfaces snapshot
their counters through one non-consuming helper; the replay preamble
loses its per-kind wrappers and its config hook; the browser harness
suites share one brace walker; and each in-flight history attempt is
one record carrying both its abort controller and its deadline.

Refs #981 #964
2026-08-11 04:18:36 -07:00

178 lines
5.3 KiB
Python

"""Tests for synchronous SDK wrappers (TurnstoneServer, TurnstoneConsole)."""
from __future__ import annotations
import httpx
from turnstone.sdk._sync import _SyncRunner
from turnstone.sdk.console import AsyncTurnstoneConsole, TurnstoneConsole
from turnstone.sdk.server import AsyncTurnstoneServer, TurnstoneServer
def _json_response(data: dict, status: int = 200) -> httpx.Response:
return httpx.Response(status, json=data)
# ---------------------------------------------------------------------------
# _SyncRunner
# ---------------------------------------------------------------------------
def test_sync_runner_basic():
"""_SyncRunner can execute a simple async coroutine."""
runner = _SyncRunner()
try:
import asyncio
async def _add(a: int, b: int) -> int:
await asyncio.sleep(0)
return a + b
result = runner.run(_add(1, 2))
assert result == 3
finally:
runner.close()
def test_sync_runner_iter():
"""_SyncRunner.run_iter iterates over an async generator."""
runner = _SyncRunner()
try:
async def _gen():
for i in range(3):
yield i
items = list(runner.run_iter(_gen()))
assert items == [0, 1, 2]
finally:
runner.close()
def test_sync_runner_iter_empty():
"""_SyncRunner.run_iter handles empty async generator via sentinel."""
runner = _SyncRunner()
try:
async def _empty():
return
yield # pragma: no cover # makes this an async generator
items = list(runner.run_iter(_empty()))
assert items == []
finally:
runner.close()
# ---------------------------------------------------------------------------
# TurnstoneServer (sync)
# ---------------------------------------------------------------------------
def test_sync_server_list_workstreams():
"""Sync server client delegates to async and returns correct model."""
def handler(request: httpx.Request) -> httpx.Response:
return _json_response({"workstreams": [{"ws_id": "ws1", "name": "test", "state": "idle"}]})
# We need to create the async client with a mock transport,
# then wrap it in the sync client
transport = httpx.MockTransport(handler)
hc = httpx.AsyncClient(transport=transport, base_url="http://test")
async_client = AsyncTurnstoneServer(httpx_client=hc)
server = TurnstoneServer.__new__(TurnstoneServer)
server._runner = _SyncRunner()
server._async = async_client
try:
resp = server.list_workstreams()
assert len(resp.workstreams) == 1
# Row key renamed id → ws_id in the Stage 2 list-verb lift.
assert resp.workstreams[0].ws_id == "ws1"
finally:
server.close()
def test_sync_server_get_history():
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.params["limit"] == "25"
return _json_response(
{
"ws_id": "ws1",
"messages": [],
"cursor": None,
"handoff_token": "epoch.1",
}
)
transport = httpx.MockTransport(handler)
hc = httpx.AsyncClient(transport=transport, base_url="http://test")
async_client = AsyncTurnstoneServer(httpx_client=hc)
server = TurnstoneServer.__new__(TurnstoneServer)
server._runner = _SyncRunner()
server._async = async_client
try:
history = server.get_history("ws1", limit=25)
assert history.handoff_token == "epoch.1"
finally:
server.close()
def test_sync_server_context_manager():
"""TurnstoneServer can be used as a context manager."""
def handler(request: httpx.Request) -> httpx.Response:
return _json_response(
{"status": "ok", "version": "0.3.0", "uptime_seconds": 1.0, "model": "gpt-5"}
)
transport = httpx.MockTransport(handler)
hc = httpx.AsyncClient(transport=transport, base_url="http://test")
async_client = AsyncTurnstoneServer(httpx_client=hc)
server = TurnstoneServer.__new__(TurnstoneServer)
server._runner = _SyncRunner()
server._async = async_client
with server as s:
resp = s.health()
assert resp.status == "ok"
# ---------------------------------------------------------------------------
# TurnstoneConsole (sync)
# ---------------------------------------------------------------------------
def test_sync_console_overview():
"""Sync console client delegates to async and returns correct model."""
def handler(request: httpx.Request) -> httpx.Response:
return _json_response(
{
"nodes": 1,
"workstreams": 3,
"states": {"idle": 3},
"aggregate": {"total_tokens": 100, "total_tool_calls": 0},
"version_drift": False,
"versions": ["0.3.0"],
}
)
transport = httpx.MockTransport(handler)
hc = httpx.AsyncClient(transport=transport, base_url="http://test")
async_client = AsyncTurnstoneConsole(httpx_client=hc)
console = TurnstoneConsole.__new__(TurnstoneConsole)
console._runner = _SyncRunner()
console._async = async_client
try:
resp = console.overview()
assert resp.nodes == 1
assert resp.workstreams == 3
finally:
console.close()