* fix(sessions): proactive prune rewrites only the changed rows, in place A prune edits the content of a few existing messages and never adds, drops or reorders one, yet every commit went through archive_and_compact, which archives all active rows and re-inserts the full list: state.db grew by the whole live transcript per commit (#124102). SessionDB.rewrite_pruned_rows resolves each changed message to its live row (_row_id, else its tool call id or tool-call ids, else the single active row with the same role and stored content), checks the row still holds the original, inserts one archived twin of the pre-prune row (active=0, compacted=1, still searchable) and updates the changed columns under the same id. The prune tries it after the stale-history watermark check and falls back to archive_and_compact when a row cannot be named exactly or an unwritten message sits before durable ones. Fixes #124102 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(sessions): in-place prune twin keeps the live row's display slot The content UPDATE fires messages_display_identity_update, which nulls display_identity/display_order on the live row and on its freshly inserted twin. The lazy backfill then gives the twin its own id as display_order, so every demoted full body renders at the END of the transcript page, after the newest user turn (3 x 24 KB bodies below "from desktop" in the probe). Re-stamp both rows with the pre-update identity and order inside the same transaction: the page projection shows one row per display_order (the live stub, in place) and the twin stays a searchable archive. This is the same stored-identity shape #117824 proposes for the archive writer (#117750), so the resume projection collapses the pair once that lands. Review-fix for #124273; one assertion added to the growth invariant (red on the PR head: 20 page rows for 17 messages). * fix(sessions): in-place prune keeps each display slot through later compaction and backfill Review follow-up (ehz0ah): the rewritten live row kept the archived body's identity, so a later archive_and_compact gave its copy a fresh slot and the full body resurfaced in the old one. The live row now takes the identity of what it stores and keeps the slot; the twin and any earlier generation's copies (all nulled by the identity trigger) are restamped with their own identity and slot, and the display backfill keeps a row's stored slot when its stored identity is still correct. The prune pre-fills slots for legacy rows so it never carries a missing one. * fix(agent): fail closed when an in-place prune finds the row rewritten rewrite_pruned_rows raised one PruneRowUnresolvedError for two different conditions: the row cannot be named (ambiguity — the full writer handles it) and the row IS named but its stored body changed. The second means another surface already rewrote this generation, which is what StaleHeldHistory exists to catch; the in-place prune preserves row ids, so _archive_watermark_for cannot see it. Returning None there handed the stale pruned_msgs to archive_and_compact, which archives rows it never compared and republished the held text over a newer durable edit. Split the stale case into a PruneRowStaleError subclass and raise StaleHeldHistory from the in-place helper so the existing handler skips the pass instead of committing. Ambiguity keeps the fallback. Replaces the prune-fallback regression with the two cases review asked for: a changed stored body skips the prune and leaves the newer body active; a genuine ambiguity over current content still commits through the full writer. * fix(agent): make an in-place prune visible to the other lease-less writer rewrite_pruned_rows keeps every row id and activity flag, so the id/activity staleness gate in held_archive_watermark cannot see that a prune already committed. A second lease-less surface (micro-compaction) holding the pre-prune transcript passes the gate and archive_and_compact republishes the pruned-away bodies over the winner, undoing the prune. stale_raises now also compares the held bodies against the stored ones via the new SessionDB.rewritten_held_row_ids, so every lease-less writer sees the in-place generation change, not just the prune caller that owns the exception. * fix(state): compare durable row versions, not raw content, in the in-place stale guard Reviewers on 631c897b converged on one root defect: the stale-write guard compared raw SQLite bytes against a loaded/normalized view, which both missed real rewrites and invented phantom ones. - rewrite_pruned_rows._resolve: the staleness check is now the repo's stored-row CAS digest (transcript_row_snapshot over a SELECT * row, covering every owned column including assistant tool_calls arguments) when the held dict carries DB_ROW_SNAPSHOT, and the loader-lens identity compare (_row_identity, the same representation _stamp_kept_live_prefix uses) otherwise. Raw storage bytes are never compared against a loaded view. - The rewritten row's replacement dict is stamped with the row's new digest, and the commit runs under _execute_transcript_write so a rollback restores the stamps. - rewritten_held_row_ids takes full held dicts and applies the same two-leg compare; only tool/assistant rows (the roles the in-place writer touches) are compared. - held_archive_watermark(stale_raises=True) checks the durable prefix BEFORE the ids[-1] is None early return, so an unstamped trailing message caps the watermark but can no longer suppress version checks for the rows the caller holds exactly. Closes the P0 stale tool_calls path, the P2 false-stale on freshly resumed whitespace-padded rows, and the unstamped-tail blind spot (#124102). * fix(state): gate the lease-less stale check on every role the fallback republishes The staleness gate behind held_archive_watermark (rewritten_held_row_ids) compared only tool/assistant held rows, but the fallback it guards — archive_and_compact, reached whenever the in-place rewrite refuses (an unwritten message before later durable rows, an unnamed row) — re-inserts held rows of every model-transcript role verbatim. A user row another view rewrote in place (set_user_message_content, the repair batch's in-place rewrite) slipped the gate: the stale pre-edit body was republished active and the newer edit left with zero active copies. Compare every model-transcript role (user/assistant/tool); the per-row comparison (DB_ROW_SNAPSHOT CAS, else the loader-lens identity) is already role-agnostic. Bookkeeping roles stay excluded — they never reach the model-facing list. Covers the prune fallback and micro-compaction, the two lease-less callers of _archive_watermark_for (#124102). Sabotage-run: reverting the role filter makes test_a_concurrent_user_edit_stays_active_when_the_prune_falls_back fail (stale body published); restored, the file is 22/22 green. * style: use builtin generics in the in-place prune commit annotations * refactor(compression): split the proactive prune and its commit into topical siblings The code-health ratchet (CI) flags this PR's own growth: hermes_state_messages.py (2190 > 2000), agent/context_compressor.py (5911 > its main value 5852) and agent/conversation_compression.py (4585 > 4564). The remedy the repo prescribes: move the PR's own code into topical siblings, keeping the facade as the seam. - agent/context_compressor_prune.py: ProactivePruneMixin (prune_tool_results_only + _rewrite_pruned_rows_in_place), bound into ContextCompressor; origin-module symbols read through _cc() at call time (the micro_compaction.py pattern) so tests patching agent.context_compressor.X stay effective; logger keeps the agent.context_compressor name for caplog parity. - agent/conversation_compression_watermark.py: held_archive_watermark; the origin module imports it and keeps _held_watermark (its only caller). - hermes_state_prune_rewrite.py: SessionPruneRewriteMixin (rewrite_pruned_rows + rewritten_held_row_ids), bound into SessionDB; every SessionDB call site and test patch seam (db.rewrite_pruned_rows / db.rewritten_held_row_ids) unchanged. Verified: scripts/check --only health --base origin/main -> 0 blocking; ruff clean; prune/compaction batches green (test_proactive_prune_restart_safety 22/22, test_micro_compaction 48/48, five proactive-prune files 25/25). The 4 failures in test_compaction_prompt_rebuild / test_compression_review reproduce identically on origin/main (pre-existing). * fix(state): re-verify held rows inside the full writer's commit transaction The lease-less stale gate (rewritten_held_row_ids) runs outside the write transaction, so a second SessionDB connection that rewrites a covered row AFTER the precheck but BEFORE archive_and_compact's BEGIN IMMEDIATE was invisible to it: the commit archived the newer row and republished the stale held text, leaving the newer edit with zero active copies (#124102, review finding on 8c6da464). - archive_and_compact takes held_for_cas: the caller's held dicts, re-verified against the live rows INSIDE the commit's transaction before any row moves; a mismatch raises HeldRowRewrittenError, which the two lease-less callers (proactive prune fallback, micro-compaction sync) treat exactly like StaleHeldHistory - nothing written, the pass is a no-op and the newer generation keeps the session. The lease path leaves it None: its lease rules out an overlapping rewrite. - held_row_mismatches(conn, ...) is the compare lifted out of rewritten_held_row_ids so both the read-path precheck and the in-transaction fence share one implementation. It now chunks the id IN query via _id_chunks (a history past SQLite's host-parameter limit failed the pass instead of reclaiming) and resolves rows that have no _row_id but do carry the persist marker + CAS snapshot by their logical message_uid (idx_messages_session_uid) - the unaddressed live-replay loader shape, whose rows were previously exempt from the fence entirely. - regression: test_a_user_edit_landing_after_the_precheck_stays_active_in_the_commit drives a second SessionDB connection's set_user_message_content at the archive_and_compact call boundary; sabotage (gate disabled) makes it fail exactly as the finding predicts. * refactor(compression): drop the duplicate _held_watermark from the module split The 8c6da464 sibling extraction left two identical _held_watermark definitions back to back in agent/conversation_compression.py; Python silently kept only the second, so editing the first had no effect (dead code, root AGENTS.md). Remove the added copy. * refactor(state): split the api_content sidecar writers into a topical sibling hermes_state_messages.py crossed the FILE_LINES ratchet (2010 > 2000) after the commit-window fence landed in it. Move set_latest_user_api_content / set_message_api_content into hermes_state_api_content.py (SessionApiContentMixin, bound into SessionDB in the same MRO slot): the two row-addressed api_content sidecar UPDATEs are one topic, no behaviour change, every call site and test patch seam unchanged. * fix(state): flatten _archive_and_compact_on_conn onto the caller's transaction after the main merge Main flattened this helper to run directly on the caller's write connection (no nested _execute_transcript_write); the merge resolution had kept the old nested shape, which opened a second BEGIN IMMEDIATE inside the live one and deadlocked every archive_and_compact. Re-flattened while keeping the #124102 held_for_cas in-transaction CAS fence. * fix(state): carry the held-row CAS fence through the worker RPC path RuntimeSessionCompressionMixin.archive_and_compact rejected held_for_cas, so a worker-side micro-compaction commit raised TypeError inside the mixin's generic except and silently skipped the whole persist (unstamped marker, durable transcript left un-compacted). Accept held_for_cas, project it onto the closed handoff fields plus the persistence marker the CAS compare reads, validate it in worker_archive, and thread it into archive_on_connection -> _archive_and_compact_on_conn (#124102). * test(micro): a stale held generation without row ids aborts the micro-compact Review follow-up (ehz0ah/greptile-apps, PR #124273): ACP restore and OpenAI session continuation load through the unaddressed live-replay projection (get_messages_as_conversation without include_row_ids=True) — the persist marker, durable message_uid and the stored-row CAS digest, but no _row_id. held_row_mismatches already resolves that shape by logical message_uid, so the lease-less fence still sees another surface's in-place prune. Pin it with the regression both reviewers asked for: after another surface prunes in place, micro-compaction from that stale no-row-id history is a true no-op — the pruned stubs stay the only live generation and the pre-prune bodies are not republished (#124102). Sabotage: disabling the by-uid branch in held_row_mismatches fails this test (the sync republishes the stale bodies); restored, 49/49 pass. Co-authored-by: ehz0ah <ehz0ah@users.noreply.github.com> * fix(sessions): the uid-resolved stale fence CASes the newest active row held_row_mismatches resolved an id-less held dict by its logical message_uid with a bare fetchone(): when a carried or concurrent compaction has cloned the row back beside its archived twin, one uid names several active physical rows, and the uid index returns them oldest-first. The fence then CASed the oldest copy — which still held the payload the held digest was stamped from — and cleared a held generation as current although another writer had rewritten the newest row; the lease-less fallback republished the stale held payload over that newer generation (#124102). Resolve with the same newest-active rule as the loader's _active_logical_message_row (ORDER BY id DESC LIMIT 1). Two regressions pin it: two active rows sharing a uid where only the newest was rewritten in place must abort the micro-compact naming the newest row id, and where only an older duplicate moved the held digest must stay current. Review follow-up to greptile-apps' 2026-10-17 P1 on PR #124273. --------- Co-authored-by: defurniture2025 <301948806+defurniture2025@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: teknium1 <127238744+teknium1@users.noreply.github.com> Co-authored-by: Austin Pickett <pickett.austin@gmail.com> Co-authored-by: Austin Pickett <austinpickett@users.noreply.github.com> Co-authored-by: ehz0ah <ehz0ah@users.noreply.github.com>
478 lines
25 KiB
Python
478 lines
25 KiB
Python
"""Closed compression operations on the worker receipt's connection.
|
|
|
|
No handler opens a connection or invokes a self-committing SessionDB method.
|
|
"""
|
|
from typing import Any, Dict, List, Optional
|
|
import json
|
|
from functools import partial
|
|
import math
|
|
import time
|
|
|
|
from hermes_state_runtime import RuntimeStoreError, _text
|
|
from hermes_state_compression import _claim_lease_row
|
|
|
|
|
|
def _fields(payload, required, optional=()):
|
|
if not isinstance(payload, dict) or set(payload) - set(required) - set(optional) or set(required) - set(payload):
|
|
raise RuntimeStoreError('invalid_params')
|
|
|
|
|
|
def _number(value):
|
|
if type(value) not in (int, float) or not math.isfinite(value):
|
|
raise RuntimeStoreError('invalid_params')
|
|
return value
|
|
|
|
|
|
def worker_compression_lock(db, conn, sid, payload, *, action):
|
|
if action == 'holder':
|
|
_fields(payload, ())
|
|
row = conn.execute('SELECT holder FROM compression_locks WHERE session_id=? AND expires_at>?',
|
|
(sid, time.time())).fetchone()
|
|
return {'value': row[0] if row else None}
|
|
_fields(payload, ('holder',), () if action == 'release' else ('ttl_seconds',))
|
|
holder = _text(payload['holder'])
|
|
now = time.time()
|
|
ttl = _number(payload.get('ttl_seconds', 300.0))
|
|
if not 0.1 <= ttl <= 3600:
|
|
raise RuntimeStoreError('invalid_params')
|
|
if action == 'acquire':
|
|
from hermes_state import _compression_lock_holder_process_is_dead
|
|
value = _claim_lease_row(conn, 'compression_locks', 'session_id', sid, holder, now, now + ttl,
|
|
lambda h, e: e < now or _compression_lock_holder_process_is_dead(h))[0]
|
|
elif action == 'renew':
|
|
value = conn.execute('UPDATE compression_locks SET expires_at=? WHERE session_id=? AND holder=?',
|
|
(now + ttl, sid, holder)).rowcount > 0
|
|
else:
|
|
conn.execute('DELETE FROM compression_locks WHERE session_id=? AND holder=?', (sid, holder))
|
|
value = None
|
|
return {'value': value}
|
|
|
|
|
|
def worker_cooldown_record(db, conn, sid, payload):
|
|
_fields(payload, ('cooldown_until', 'error'))
|
|
deadline = _number(payload['cooldown_until'])
|
|
error = payload['error']
|
|
if error is not None and not isinstance(error, str):
|
|
raise RuntimeStoreError('invalid_params')
|
|
conn.execute('UPDATE sessions SET compression_failure_cooldown_until = CASE '
|
|
'WHEN compression_failure_cooldown_until IS NOT NULL AND compression_failure_cooldown_until > ? '
|
|
'THEN compression_failure_cooldown_until ELSE ? END, compression_failure_error=? WHERE id=?',
|
|
(deadline, deadline, error, sid))
|
|
return {'value': None}
|
|
|
|
|
|
def worker_cooldown_restore(db, conn, sid, payload):
|
|
_fields(payload, ('snapshot',))
|
|
snapshot = payload['snapshot']
|
|
_fields(snapshot, ('session_exists', 'cooldown_until', 'error'))
|
|
if type(snapshot['session_exists']) is not bool:
|
|
raise RuntimeStoreError('invalid_params')
|
|
if not snapshot['session_exists']:
|
|
raise RuntimeError('cannot restore absent compression cooldown row: session now exists')
|
|
deadline, error = snapshot['cooldown_until'], snapshot['error']
|
|
if deadline is not None:
|
|
_number(deadline)
|
|
if error is not None and not isinstance(error, str):
|
|
raise RuntimeStoreError('invalid_params')
|
|
conn.execute('UPDATE sessions SET compression_failure_cooldown_until=?, compression_failure_error=? WHERE id=?',
|
|
(deadline, error, sid))
|
|
actual = conn.execute('SELECT compression_failure_cooldown_until,compression_failure_error FROM sessions WHERE id=?',
|
|
(sid,)).fetchone()
|
|
if actual is None or tuple(actual) != (deadline, error):
|
|
raise RuntimeError('compression cooldown rollback verification failed')
|
|
return {'value': None}
|
|
|
|
|
|
def worker_cooldown_clear(db, conn, sid, payload):
|
|
_fields(payload, ())
|
|
return worker_cooldown_restore(db, conn, sid, {'snapshot': {
|
|
'session_exists': True, 'cooldown_until': None, 'error': None}})
|
|
|
|
|
|
def worker_compression_counter(db, conn, sid, payload, *, column):
|
|
_fields(payload, ('value',))
|
|
value = payload['value']
|
|
if value is not None:
|
|
_number(value)
|
|
if column != 'compression_recovery_deadline' and (type(value) is not int or value < 0):
|
|
raise RuntimeStoreError('invalid_params')
|
|
conn.execute(f'UPDATE sessions SET {column}=? WHERE id=?', (value, sid))
|
|
return {'value': None}
|
|
|
|
|
|
class CompressionSnapshot:
|
|
"""Reuse production read algorithms on the receipt connection, never a second handle.
|
|
|
|
Only read primitives are provided; this object cannot start/commit a write.
|
|
Wire handlers below select concrete projections, not caller-named methods.
|
|
"""
|
|
from hermes_state_compression import SessionCompressionMixin as _C
|
|
from hermes_state_messages import SessionMessagesMixin as _M
|
|
from hermes_state_sessions import SessionSessionsMixin as _S
|
|
|
|
get_compression_chain = _C.get_compression_chain
|
|
get_compression_tip = _C.get_compression_tip
|
|
get_compression_lineage = _C.get_compression_lineage
|
|
_is_compression_child_row = _C._is_compression_child_row
|
|
_session_lineage_root_to_tip = _S._session_lineage_root_to_tip
|
|
_is_explicit_branch_session = _S._is_explicit_branch_session
|
|
declared_scope_identity = _S.declared_scope_identity
|
|
_resume_lineage_ids = _M._resume_lineage_ids
|
|
get_conversation_root = _M.get_conversation_root
|
|
resolve_resume_session_id = _M.resolve_resume_session_id
|
|
latest_conversation_boundary = _M.latest_conversation_boundary
|
|
_is_explicit_fork_child_row = _M._is_explicit_fork_child_row
|
|
|
|
def __init__(self, db, conn):
|
|
self.db, self.conn = db, conn
|
|
|
|
def _read_ctx(self):
|
|
from contextlib import nullcontext
|
|
return nullcontext(self.conn)
|
|
|
|
def _read_one(self, sql, params=()):
|
|
return self.conn.execute(sql, params).fetchone()
|
|
|
|
def _read_all(self, sql, params=()):
|
|
return self.conn.execute(sql, params).fetchall()
|
|
|
|
def get_session(self, sid):
|
|
from hermes_state_worker_context import worker_context
|
|
return worker_context(self.db, self.conn, sid, {})['session']
|
|
|
|
def authorize(self, assigned, target):
|
|
_text(target)
|
|
# Attribution ancestry is readable; unrelated siblings and foreign trees are not.
|
|
allowed = self._session_lineage_root_to_tip(assigned)
|
|
allowed += self.get_compression_chain(assigned)
|
|
if target not in allowed:
|
|
raise RuntimeStoreError('permission_denied')
|
|
|
|
|
|
def worker_lineage_context(db, conn, sid, payload):
|
|
_fields(payload, ('target',))
|
|
view = CompressionSnapshot(db, conn)
|
|
target = payload['target']
|
|
view.authorize(sid, target)
|
|
return {'session': view.get_session(target)}
|
|
|
|
|
|
def worker_lineage(db, conn, sid, payload):
|
|
_fields(payload, ('target',))
|
|
view = CompressionSnapshot(db, conn)
|
|
target = payload['target']
|
|
view.authorize(sid, target)
|
|
return {'readable_ids': view._session_lineage_root_to_tip(sid) + view.get_compression_chain(sid),
|
|
'lineage': view.get_compression_lineage(target),
|
|
'root': view.get_conversation_root(target),
|
|
'tip': view.get_compression_tip(target),
|
|
'resume': view.resolve_resume_session_id(target),
|
|
'identity': view.declared_scope_identity(target)}
|
|
|
|
|
|
def worker_boundary(db, conn, sid, payload):
|
|
_fields(payload, ('session_key', 'source'))
|
|
view = CompressionSnapshot(db, conn)
|
|
row = view.get_session(sid)
|
|
if (row['session_key'], row['source']) != (payload['session_key'], payload['source']):
|
|
raise RuntimeStoreError('permission_denied')
|
|
return {'value': view.latest_conversation_boundary(payload['session_key'], payload['source'])}
|
|
|
|
|
|
def worker_history(db, conn, sid, payload):
|
|
_fields(payload, ('target', 'include_ancestors', 'include_inactive', 'repair_alternation',
|
|
'include_row_ids', 'include_compacted'))
|
|
if any(type(v) is not bool for k, v in payload.items() if k != 'target'):
|
|
raise RuntimeStoreError('invalid_params')
|
|
view = CompressionSnapshot(db, conn)
|
|
target = payload['target']
|
|
view.authorize(sid, target)
|
|
ids = view._resume_lineage_ids(target) if payload['include_ancestors'] else [target]
|
|
active = db._active_clause(payload['include_inactive'], payload['include_compacted'])
|
|
display = payload['include_compacted'] and not payload['include_inactive']
|
|
rows = conn.execute(f'SELECT {"compacted, " if display else ""}{db._CONVERSATION_ROW_COLUMNS} FROM messages '
|
|
f'WHERE session_id IN ({",".join("?" for _ in ids)}){active} ORDER BY id', ids).fetchall()
|
|
if display: # the owner's display reads' rule: a message whose newest copy was removed stays hidden
|
|
from hermes_state_messages import SessionMessagesMixin
|
|
rows = SessionMessagesMixin._with_removed_identities(view, rows, ids)
|
|
if payload['include_compacted']:
|
|
rows = db._dedupe_display_generations(rows, drop_removed=display)
|
|
return {'messages': db._rows_to_conversation(rows, session_id=target,
|
|
include_ancestors=payload['include_ancestors'], repair_alternation=payload['repair_alternation'],
|
|
include_row_ids=payload['include_row_ids'])}
|
|
|
|
|
|
|
|
|
|
|
|
def archive_on_connection(db, conn, session_id: str, compacted_messages: list[dict[str, Any]], model_config_patch: Optional[dict[str, Any]]=None, watermark: Optional[int]=None, lock_holder: Optional[str]=None, tail_count: int=0, carried_messages: Optional[list[dict[str, Any]]]=None, covered_ids: Optional[list[int]]=None, unresolved_held: Optional[list[dict[str, Any]]]=None, held_for_cas: Optional[list[dict[str, Any]]]=None):
|
|
# The owner's in-place compaction body, on this receipt's connection: one implementation, so
|
|
# main's coverage/merge/timestamp/display-order fixes reach worker and mutation verbs too.
|
|
return db._archive_and_compact_on_conn(conn, session_id, compacted_messages,
|
|
model_config_patch=model_config_patch, watermark=watermark, lock_holder=lock_holder,
|
|
tail_count=tail_count, carried_messages=carried_messages, covered_ids=covered_ids,
|
|
unresolved_held=unresolved_held, held_for_cas=held_for_cas)
|
|
|
|
def publish_on_connection(db, conn, *, parent_session_id: str, child_session_id: str, source: str, messages: list[dict[str, Any]], model: str | None=None, model_config: dict[str, Any] | None=None, system_prompt: str | None=None, cwd: str | None=None, profile_name: str | None=None, compression_lock_holder: str | None=None, require_compression_lease: bool=True, require_lease_refresh: bool=False, lease_ttl_seconds: float=300.0, watermark: Optional[int]=None, watermark_ceiling: Optional[int]=None):
|
|
# The owner's rotation body (lease, closure, child row, timestamps, tail clone, local target,
|
|
# auto-archive lift) on this receipt's connection, so the two paths cannot drift.
|
|
db._publish_compression_child_on_conn(conn, parent_session_id=parent_session_id,
|
|
child_session_id=child_session_id, source=source, messages=messages, model=model,
|
|
model_config=model_config, system_prompt=system_prompt, cwd=cwd, profile_name=profile_name,
|
|
compression_lock_holder=compression_lock_holder, require_compression_lease=require_compression_lease,
|
|
require_lease_refresh=require_lease_refresh, lease_ttl_seconds=lease_ttl_seconds,
|
|
watermark=watermark, watermark_ceiling=watermark_ceiling)
|
|
|
|
def worker_watermark(db, conn, sid, payload):
|
|
_fields(payload, ())
|
|
return {'value': conn.execute('SELECT COALESCE(MAX(id),0) FROM messages WHERE session_id=? AND active=1',
|
|
(sid,)).fetchone()[0]}
|
|
|
|
|
|
def _handoff_messages(conn, sid, messages):
|
|
from hermes_state_runtime import HANDOFF_MESSAGE_FIELDS
|
|
if not isinstance(messages, list) or len(messages) > 1000:
|
|
raise RuntimeStoreError('invalid_params')
|
|
for msg in messages:
|
|
if not isinstance(msg, dict) or set(msg) - HANDOFF_MESSAGE_FIELDS or msg.get('role') not in ('user', 'assistant', 'system', 'tool'):
|
|
raise RuntimeStoreError('invalid_params')
|
|
if '_row_id' in msg and not conn.execute('SELECT 1 FROM messages WHERE session_id=? AND id=?',
|
|
(sid, msg['_row_id'])).fetchone():
|
|
raise RuntimeStoreError('permission_denied')
|
|
|
|
|
|
def _watermarks(payload):
|
|
for field in ('watermark', 'watermark_ceiling'):
|
|
value = payload.get(field)
|
|
if value is not None and (type(value) is not int or value < 0):
|
|
raise RuntimeStoreError('invalid_params')
|
|
|
|
|
|
def worker_archive(db, conn, sid, payload):
|
|
from hermes_state_worker_context import SIDECAR_KEYS
|
|
_fields(payload, ('messages', 'model_config_patch', 'watermark', 'lock_holder', 'tail_count'),
|
|
optional=('carried_messages', 'covered_ids', 'unresolved_held', 'held_for_cas'))
|
|
_handoff_messages(conn, sid, payload['messages'])
|
|
carried = payload.get('carried_messages') or []
|
|
if not isinstance(carried, list) or any(not isinstance(m, dict) for m in carried):
|
|
raise RuntimeStoreError('invalid_params')
|
|
covered = payload.get('covered_ids')
|
|
if covered is not None and (not isinstance(covered, list)
|
|
or any(type(i) is not int or i <= 0 for i in covered)):
|
|
raise RuntimeStoreError('invalid_params')
|
|
unresolved = payload.get('unresolved_held')
|
|
if unresolved is not None and (not isinstance(unresolved, list)
|
|
or any(not isinstance(m, dict) for m in unresolved)):
|
|
raise RuntimeStoreError('invalid_params')
|
|
# The lease-less CAS fence (#124102): dicts projected to the closed handoff fields plus the
|
|
# persistence marker the owner's compare reads; validated like the other held-row payloads.
|
|
cas = payload.get('held_for_cas')
|
|
if cas is not None and (not isinstance(cas, list)
|
|
or any(not isinstance(m, dict) for m in cas)):
|
|
raise RuntimeStoreError('invalid_params')
|
|
_watermarks(payload)
|
|
# Micro-compaction and proactive prune archive in place without a lease, like the owner path.
|
|
if payload['lock_holder'] is not None:
|
|
_text(payload['lock_holder'])
|
|
patch = payload['model_config_patch']
|
|
if patch is not None and (not isinstance(patch, dict) or set(patch) - SIDECAR_KEYS):
|
|
raise RuntimeStoreError('invalid_params')
|
|
if type(payload['tail_count']) is not int or payload['tail_count'] < 0:
|
|
raise RuntimeStoreError('invalid_params')
|
|
value = archive_on_connection(db, conn, sid, payload['messages'], model_config_patch=patch,
|
|
watermark=payload['watermark'], lock_holder=payload['lock_holder'], tail_count=payload['tail_count'],
|
|
carried_messages=carried, covered_ids=covered, unresolved_held=unresolved, held_for_cas=cas)
|
|
from hermes_state_runtime import row_annotations
|
|
return {'value': value, 'annotations': row_annotations(payload['messages'])}
|
|
|
|
|
|
def _migrate_worker_automation(db, conn, parent, child):
|
|
from hermes_cli.goals import GoalState
|
|
from hermes_cli.heartbeat import HeartbeatState
|
|
from hermes_cli.loops import LoopState
|
|
for family, state_type in (('goal', GoalState), ('heartbeat', HeartbeatState), ('loop', LoopState)):
|
|
old_key, new_key = f'{family}:{parent}', f'{family}:{child}'
|
|
row = conn.execute('SELECT value FROM state_meta WHERE key=?', (old_key,)).fetchone()
|
|
if row is None:
|
|
continue
|
|
state = state_type.from_json(row[0])
|
|
if state.status == 'cleared':
|
|
continue
|
|
prior = conn.execute('SELECT value FROM state_meta WHERE key=?', (new_key,)).fetchone()
|
|
if prior is not None and (family != 'heartbeat' or state_type.from_json(prior[0]).status != 'cleared'):
|
|
continue
|
|
db.set_meta(new_key, state.to_json(), cursor=conn)
|
|
state.status = 'cleared'
|
|
db.set_meta(old_key, state.to_json(), cursor=conn)
|
|
|
|
|
|
def worker_publish(db, conn, sid, payload):
|
|
from hermes_state_sessions import _parse_model_config
|
|
_fields(payload, ('child_session_id', 'source', 'messages', 'model', 'model_config', 'system_prompt',
|
|
'cwd', 'profile_name', 'compression_lock_holder', 'require_compression_lease',
|
|
'require_lease_refresh', 'lease_ttl_seconds', 'watermark', 'watermark_ceiling'))
|
|
child = _text(payload['child_session_id'])
|
|
_text(payload['compression_lock_holder'])
|
|
_watermarks(payload)
|
|
if payload['require_compression_lease'] is not True or type(payload['require_lease_refresh']) is not bool:
|
|
raise RuntimeStoreError('invalid_params')
|
|
if not 0.1 <= _number(payload['lease_ttl_seconds']) <= 3600:
|
|
raise RuntimeStoreError('invalid_params')
|
|
_handoff_messages(conn, sid, payload['messages'])
|
|
parent = CompressionSnapshot(db, conn).get_session(sid)
|
|
for key in ('source', 'cwd', 'profile_name'):
|
|
if payload[key] is not None and payload[key] != parent[key]:
|
|
raise RuntimeStoreError('permission_denied')
|
|
for key in ('model', 'system_prompt'):
|
|
if payload[key] is not None and not isinstance(payload[key], str):
|
|
raise RuntimeStoreError('invalid_params')
|
|
cfg = payload['model_config']
|
|
if cfg is not None and not isinstance(cfg, dict):
|
|
raise RuntimeStoreError('invalid_params')
|
|
cfg = dict(cfg or {})
|
|
prior = _parse_model_config(parent['model_config'])
|
|
for key in ('_branched_from', '_delegate_from', '_reset_from'):
|
|
if key in cfg and cfg[key] != prior.get(key):
|
|
raise RuntimeStoreError('permission_denied')
|
|
if key in prior:
|
|
cfg[key] = prior[key]
|
|
if conn.execute('SELECT 1 FROM sessions WHERE id=?', (child,)).fetchone():
|
|
raise RuntimeStoreError('permission_denied')
|
|
worker = conn.execute("SELECT * FROM worker_executions WHERE session_id=? AND status IN ('registered','running')", (sid,)).fetchone()
|
|
if worker is None:
|
|
raise RuntimeStoreError('stale_generation')
|
|
publish_on_connection(db, conn, parent_session_id=sid, **dict(payload, model_config=cfg))
|
|
_migrate_worker_automation(db, conn, sid, child)
|
|
conn.execute('UPDATE sessions SET runtime_generation=? WHERE id=?', (worker['generation'], child))
|
|
conn.execute('UPDATE worker_executions SET session_id=? WHERE execution_id=?', (child, worker['execution_id']))
|
|
# The FIFO/retry key is immutable; only physical lineage follows rotation.
|
|
conn.execute("UPDATE session_admissions SET lineage_json=json_insert(lineage_json,'$[#]',?) "
|
|
"WHERE json_extract(lineage_json,'$[#-1]')=? AND generation=? AND owner_epoch=? "
|
|
"AND status IN ('started','unknown')",
|
|
(child, sid, worker['generation'], worker['owner_epoch']))
|
|
from hermes_state_runtime import row_annotations
|
|
return {'value': None, 'worker_assignment': {'parent': sid, 'session_id': child},
|
|
'annotations': row_annotations(payload['messages'])}
|
|
|
|
|
|
def worker_receipt_assignment(conn, execution_id, session_id, generation, sequence, digest):
|
|
"""Only an identical already-committed publication can retry its old assignment."""
|
|
from hermes_state_runtime import _worker_assignment
|
|
receipt = conn.execute('SELECT payload_digest,result_json FROM worker_receipts WHERE execution_id=? AND sequence=?',
|
|
(execution_id, sequence)).fetchone()
|
|
if receipt and receipt['payload_digest'] == digest:
|
|
assignment = json.loads(receipt['result_json']).get('worker_assignment')
|
|
if assignment and assignment['parent'] == session_id:
|
|
current = conn.execute('SELECT session_id FROM worker_executions WHERE execution_id=?', (execution_id,)).fetchone()
|
|
return _worker_assignment(conn, execution_id, current['session_id'], generation)
|
|
return _worker_assignment(conn, execution_id, session_id, generation)
|
|
|
|
|
|
def worker_claim_origin(db, params):
|
|
"""Keep the producer claim bound to its original identity after physical rotation."""
|
|
with db._read_ctx() as conn:
|
|
row = conn.execute("SELECT result_json FROM worker_receipts WHERE execution_id=? "
|
|
"AND json_extract(result_json,'$.worker_assignment.parent') IS NOT NULL ORDER BY sequence LIMIT 1",
|
|
(params['execution_id'],)).fetchone()
|
|
return json.loads(row[0])['worker_assignment']['parent'] if row else params['session_id']
|
|
|
|
|
|
def worker_retry_target(db, params):
|
|
"""Transport verification accepts old targets only for exact publication replay."""
|
|
from hermes_state_keys import admission_fingerprint
|
|
if params.get('operation') != 'compression.publish':
|
|
return params['session_id']
|
|
digest = admission_fingerprint(canonical_target=params['session_id'],
|
|
payload={'operation': params['operation'], 'payload': params['payload']})
|
|
with db._read_ctx() as conn:
|
|
row = worker_receipt_assignment(conn, params['execution_id'], params['session_id'],
|
|
params['generation'], params['sequence'], digest)
|
|
return row['session_id']
|
|
|
|
|
|
from hermes_state_common import _COMPRESSION_LOCK_ROW_SQL as _LOCK_ROW_SQL, _ENDED_ROW_SQL, _ended_by_compression
|
|
|
|
def reopen_on_connection(db, conn, session_id):
|
|
if not _ended_by_compression(conn.execute(_ENDED_ROW_SQL, (session_id,)).fetchone()):
|
|
return False
|
|
child = conn.execute('\n SELECT 1\n FROM sessions\n WHERE parent_session_id = ?\n ' + db._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias='') + '\n LIMIT 1\n ', (session_id,) * 4).fetchone()
|
|
if child is not None:
|
|
return False
|
|
now = time.time()
|
|
lock_row = conn.execute(_LOCK_ROW_SQL, (session_id,)).fetchone()
|
|
if lock_row is not None:
|
|
expires_at = lock_row['expires_at']
|
|
if expires_at is None or float(expires_at) >= now:
|
|
return False
|
|
deleted = conn.execute('DELETE FROM compression_locks WHERE session_id = ? AND holder = ? AND expires_at = ?', (session_id, lock_row['holder'], expires_at))
|
|
if deleted.rowcount != 1:
|
|
return False
|
|
updated = conn.execute("UPDATE sessions SET ended_at = NULL, end_reason = NULL WHERE id = ? AND ended_at IS NOT NULL AND end_reason = 'compression'", (session_id,))
|
|
return updated.rowcount == 1
|
|
|
|
|
|
def worker_reopen(db, conn, sid, payload):
|
|
_fields(payload, ())
|
|
return {'value': reopen_on_connection(db, conn, sid)}
|
|
|
|
|
|
def worker_compression_cleanup(db, conn, sid, payload):
|
|
_fields(payload, ('target', 'holder'))
|
|
target = _text(payload['target'])
|
|
holder = _text(payload['holder'])
|
|
view = CompressionSnapshot(db, conn)
|
|
view.authorize(sid, target)
|
|
if db._session_turn_lease_key_on_conn(conn, sid) != db._session_turn_lease_key_on_conn(conn, target):
|
|
raise RuntimeStoreError('permission_denied')
|
|
conn.execute('DELETE FROM compression_locks WHERE session_id=? AND holder=?', (target, holder))
|
|
return {'value': None}
|
|
|
|
|
|
def worker_compression_append(db, conn, sid, payload):
|
|
_fields(payload, ('messages', 'compression_lock_holder', 'turn_lease_holder', 'turn_lease_ttl_seconds'))
|
|
_handoff_messages(conn, sid, payload['messages'])
|
|
for key in ('compression_lock_holder', 'turn_lease_holder'):
|
|
if payload[key] is not None:
|
|
_text(payload[key])
|
|
if not 0.1 <= _number(payload['turn_lease_ttl_seconds']) <= 3600:
|
|
raise RuntimeStoreError('invalid_params')
|
|
count = db._append_messages_in_transaction(conn, sid, **payload)
|
|
from hermes_state_runtime import row_annotations
|
|
return {'count': count, 'annotations': row_annotations(payload['messages'])}
|
|
|
|
|
|
def worker_turn_cleanup(db, conn, sid, payload):
|
|
_fields(payload, ('target', 'holder'))
|
|
target = _text(payload['target'])
|
|
holder = _text(payload['holder'])
|
|
CompressionSnapshot(db, conn).authorize(sid, target)
|
|
key = db._session_turn_lease_key_on_conn(conn, sid)
|
|
if key != db._session_turn_lease_key_on_conn(conn, target):
|
|
raise RuntimeStoreError('permission_denied')
|
|
conn.execute('DELETE FROM session_turn_leases WHERE conversation_id=? AND holder=?', (key, holder))
|
|
return {'released': True}
|
|
|
|
|
|
WORKER_COMPRESSION_HANDLERS = {
|
|
'turn.cleanup': worker_turn_cleanup,
|
|
'compression.append': worker_compression_append,
|
|
'compression.reopen': worker_reopen,
|
|
'compression.cleanup': worker_compression_cleanup,
|
|
'compression.watermark': worker_watermark,
|
|
'compression.archive': worker_archive,
|
|
'compression.publish': worker_publish,
|
|
'compression.context': worker_lineage_context,
|
|
'compression.lineage': worker_lineage,
|
|
'compression.boundary': worker_boundary,
|
|
'compression.history': worker_history,
|
|
**{'compression.lock.' + action: partial(worker_compression_lock, action=action)
|
|
for action in ('acquire', 'renew', 'release', 'holder')},
|
|
'compression.cooldown.record': worker_cooldown_record,
|
|
'compression.cooldown.restore': worker_cooldown_restore,
|
|
'compression.cooldown.clear': worker_cooldown_clear,
|
|
**{'compression.' + name: partial(worker_compression_counter, column=column)
|
|
for name, column in (('fallback', 'compression_fallback_streak'),
|
|
('ineffective', 'compression_ineffective_count'),
|
|
('recovery', 'compression_recovery_deadline'))},
|
|
}
|