mirror of
https://github.com/turnstonelabs/turnstone.git
synced 2026-08-12 23:12:23 -06:00
480a1426b3
* 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
126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
"""Scripted-PostgreSQL fakes shared by the storage race-test modules.
|
|
|
|
One implementation of the scripted connection/result pair and the keyed-save
|
|
three-way dispatch, so a backend statement-sequence or signature change is
|
|
updated once. The two hand-rolled twins had already diverged before the
|
|
round-4 review folded them here: the truncation copy grew a ``SET LOCAL``
|
|
arm and ``fetchall``/``scalar`` the prune copy lacked.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from turnstone.core.storage import AttachmentWrite
|
|
|
|
|
|
def make_attachment(
|
|
attachment_id: str,
|
|
content: bytes,
|
|
*,
|
|
filename: str | None = None,
|
|
mime_type: str = "text/plain",
|
|
kind: str = "text",
|
|
) -> AttachmentWrite:
|
|
return AttachmentWrite(
|
|
attachment_id=attachment_id,
|
|
filename=filename or f"{attachment_id[0]}.txt",
|
|
mime_type=mime_type,
|
|
size_bytes=len(content),
|
|
kind=kind,
|
|
content=content,
|
|
)
|
|
|
|
|
|
def save_keyed(
|
|
backend: Any,
|
|
ws_id: str,
|
|
kind: str,
|
|
*,
|
|
content: str,
|
|
commit_key: str,
|
|
attachments: list[AttachmentWrite] | None = None,
|
|
tool_content: str | None = None,
|
|
tool_name: str = "read_file",
|
|
tool_call_id: str = "call-keyed",
|
|
) -> int:
|
|
"""Three-way plain/user/tool keyed-save dispatch.
|
|
|
|
The per-module literals (content, commit keys, attachment multiplicity)
|
|
stay at the call sites — this owns only the method dispatch, so a
|
|
signature change on the three save entry points is threaded once.
|
|
"""
|
|
if kind == "plain":
|
|
return int(backend.save_message(ws_id, "assistant", content, commit_key=commit_key))
|
|
if kind == "user":
|
|
return int(
|
|
backend.save_user_message_with_attachments(
|
|
ws_id,
|
|
content,
|
|
attachments or [],
|
|
commit_key=commit_key,
|
|
)
|
|
)
|
|
return int(
|
|
backend.save_tool_message_with_attachments(
|
|
ws_id,
|
|
tool_content if tool_content is not None else content,
|
|
tool_name,
|
|
tool_call_id,
|
|
attachments or [],
|
|
commit_key=commit_key,
|
|
)
|
|
)
|
|
|
|
|
|
class ScriptedPostgresResult:
|
|
def __init__(
|
|
self,
|
|
*,
|
|
row: Any | None = None,
|
|
rows: list[Any] | None = None,
|
|
scalar_value: Any | None = None,
|
|
) -> None:
|
|
self._row = row
|
|
self._rows = rows or []
|
|
self._scalar_value = scalar_value
|
|
|
|
def fetchone(self) -> Any | None:
|
|
return self._row
|
|
|
|
def fetchall(self) -> list[Any]:
|
|
return self._rows
|
|
|
|
def scalar(self) -> Any | None:
|
|
return self._scalar_value
|
|
|
|
def scalar_one_or_none(self) -> Any | None:
|
|
return self._scalar_value
|
|
|
|
|
|
class ScriptedPostgresConnection:
|
|
def __init__(self, results: list[ScriptedPostgresResult]) -> None:
|
|
self._results = results
|
|
self.statements: list[Any] = []
|
|
self.commits = 0
|
|
self.rollbacks = 0
|
|
|
|
def execute(self, statement: Any, *_args: Any, **_kwargs: Any) -> ScriptedPostgresResult:
|
|
self.statements.append(statement)
|
|
# Session-scoped tuning (the truncation lock_timeout bound) is not part
|
|
# of the scripted result sequence; record it and return an empty result.
|
|
if str(statement).startswith("SET LOCAL "):
|
|
return ScriptedPostgresResult()
|
|
if not self._results:
|
|
raise AssertionError("unexpected PostgreSQL statement")
|
|
return self._results.pop(0)
|
|
|
|
def commit(self) -> None:
|
|
self.commits += 1
|
|
|
|
def rollback(self) -> None:
|
|
self.rollbacks += 1
|
|
|
|
def assert_consumed(self) -> None:
|
|
assert not self._results, f"unconsumed scripted results: {len(self._results)}"
|