refactor(state): AST-neutral bracket/string-literal layout pass on the six mixin modules

This commit is contained in:
Teknium
2026-09-02 19:45:18 -07:00
parent 4de8710b74
commit 3c615c48e9
3 changed files with 23 additions and 46 deletions

View File

@@ -16,8 +16,7 @@ from typing import Any
from agent.skill_commands import SKILL_EXCERPT_JOINT, SKILL_SCAFFOLD_SQL_LIKE, describe_skill_invocation
from agent.context_compressor import (
LEGACY_SUMMARY_PREFIX, SUMMARY_PREFIX, _MERGED_PRIOR_CONTEXT_HEADER, _MERGED_SUMMARY_DELIMITER,
_SUMMARY_END_MARKER,
)
_SUMMARY_END_MARKER)
# Session preview = head of the first user message, shown wherever a session has no
@@ -141,14 +140,11 @@ _PREVIEW_RAW_SUBQUERY_SQL = (
# end_reason heuristic.
_BRANCH_CHILD_SQL = (
"json_extract(COALESCE({a}.model_config, '{{}}'), '$._branched_from') IS NOT NULL"
" OR EXISTS (SELECT 1 FROM sessions p"
" WHERE p.id = {a}.parent_session_id"
" AND p.end_reason = 'branched'"
" AND {a}.started_at >= p.ended_at)"
" OR EXISTS (SELECT 1 FROM sessions p WHERE p.id = {a}.parent_session_id"
" AND p.end_reason = 'branched' AND {a}.started_at >= p.ended_at)"
)
_COMPRESSION_CHILD_SQL = (
"EXISTS (SELECT 1 FROM sessions p"
" WHERE p.id = {a}.parent_session_id"
"EXISTS (SELECT 1 FROM sessions p WHERE p.id = {a}.parent_session_id"
" AND p.end_reason = 'compression')"
)
@@ -185,8 +181,7 @@ _RECOVERABLE_END_REASONS_SQL = ", ".join(f"'{reason}'" for reason in _RECOVERABL
# holding the lease) may clear it. Superset of the recoverable set plus the TUI
# gateway's automatic reasons.
_AUTOMATIC_END_REASONS = frozenset(_RECOVERABLE_END_REASONS) | {
"tui_shutdown", "ws_disconnect", "idle_timeout", "lru_evict",
}
"tui_shutdown", "ws_disconnect", "idle_timeout", "lru_evict"}
def is_automatic_end_reason(reason) -> bool:
@@ -261,8 +256,7 @@ def _sql_session_last_active_by_id(session_id_expr: str) -> str:
return _sql_freshest_of(
f"(SELECT last_activity_at FROM sessions _act_s WHERE _act_s.id = {session_id_expr})",
session_id_expr,
f"(SELECT started_at FROM sessions _act_s WHERE _act_s.id = {session_id_expr})",
)
f"(SELECT started_at FROM sessions _act_s WHERE _act_s.id = {session_id_expr})")
SCHEMA_VERSION = 28
@@ -308,8 +302,7 @@ def _placeholders(items) -> str:
_FTS_TRIGGERS = (
"messages_fts_insert", "messages_fts_delete", "messages_fts_update",
"messages_fts_trigram_insert", "messages_fts_trigram_delete", "messages_fts_trigram_update",
)
"messages_fts_trigram_insert", "messages_fts_trigram_delete", "messages_fts_trigram_update")
SCHEMA_SQL = """
CREATE TABLE IF NOT EXISTS schema_version (
@@ -693,8 +686,7 @@ END;
"""
_FTS_CJK_TRIGGERS = (
"messages_fts_cjk_insert", "messages_fts_cjk_delete", "messages_fts_cjk_update",
)
"messages_fts_cjk_insert", "messages_fts_cjk_delete", "messages_fts_cjk_update")
# Set when a tokenizer-less process dropped the cjk triggers to keep writes
# alive: the cjk index is missing rows and must not serve reads until
@@ -912,10 +904,8 @@ def _acquire_db_flock(lock_path, handle, timeout_seconds, poll_seconds, descript
# Not a holder and polling cannot fix it: defer NOW.
logger.warning(
"Could not acquire %s %s (%s) — deferring rather than "
"waiting out the %.0fs holder timeout on a "
"non-contention error.",
description, lock_path, exc, timeout_seconds,
)
"waiting out the %.0fs holder timeout on a non-contention error.",
description, lock_path, exc, timeout_seconds)
return None, handle
if time.monotonic() < deadline:
time.sleep(poll_seconds)
@@ -928,10 +918,8 @@ def _acquire_db_flock(lock_path, handle, timeout_seconds, poll_seconds, descript
logger.warning(
"%s %s is held by an orphaned file descriptor (recorded "
"holder pid %s is dead — a forked child inherited the lock "
"fd); breaking the stale lock and retaking it on a fresh "
"file.",
description, lock_path, (record or {}).get("pid"),
)
"fd); breaking the stale lock and retaking it on a fresh file.",
description, lock_path, (record or {}).get("pid"))
try:
os.unlink(lock_path)
handle.close()
@@ -988,10 +976,8 @@ def _acquire_msvcrt_lock(lock_path, handle, timeout):
except (BlockingIOError, OSError) as exc:
if not is_advisory_lock_contention(exc):
logger.warning(
"Could not acquire FTS rebuild lock %s (%s) — "
"deferring on a non-contention error.",
lock_path, exc,
)
"Could not acquire FTS rebuild lock %s (%s) — deferring on a non-contention error.",
lock_path, exc)
return None
if time.monotonic() >= deadline:
return False
@@ -1023,8 +1009,7 @@ def fts_rebuild_admission(db_path, *, timeout_seconds=None):
logger.warning(
"Could not open FTS rebuild lock %s (%s) — deferring this rebuild "
"rather than running it without cross-process authority.",
lock_path, exc,
)
lock_path, exc)
yield False
return
@@ -1034,8 +1019,7 @@ def fts_rebuild_admission(db_path, *, timeout_seconds=None):
acquired = _acquire_msvcrt_lock(lock_path, handle, timeout)
else:
acquired, handle = _acquire_db_flock(
lock_path, handle, timeout, _FTS_REBUILD_LOCK_POLL_SECONDS, "FTS rebuild lock",
)
lock_path, handle, timeout, _FTS_REBUILD_LOCK_POLL_SECONDS, "FTS rebuild lock")
if acquired is None:
# Already logged with the real errno; "held by another process" would be a lie.
acquired = False
@@ -1045,18 +1029,14 @@ def fts_rebuild_admission(db_path, *, timeout_seconds=None):
# Non-blocking probe from an in-process retry: keep it quiet.
logger.info(
"FTS rebuild lock %s is busy — deferring this retry "
"(the stale-FTS breadcrumb keeps it retryable). "
"Recorded holder: %s.",
lock_path, _describe_lock_holder(record),
)
"(the stale-FTS breadcrumb keeps it retryable). Recorded holder: %s.",
lock_path, _describe_lock_holder(record))
else:
logger.warning(
"FTS rebuild lock %s held by another process for more than "
"%.0fs — deferring this rebuild to avoid racing the holder "
"(the stale-FTS breadcrumb keeps it retryable). "
"Recorded holder: %s.",
lock_path, timeout, _describe_lock_holder(record),
)
"(the stale-FTS breadcrumb keeps it retryable). Recorded holder: %s.",
lock_path, timeout, _describe_lock_holder(record))
yield acquired
finally:
try:

View File

@@ -13,8 +13,7 @@ from typing import Any, Dict, List, Optional, Tuple
from hermes_state_common import (
_COMPRESSION_LOCK_ROW_SQL as _LOCK_ROW_SQL, _ENDED_ROW_SQL, _ended_by_compression, _sql_session_last_active,
is_automatic_end_reason,
)
is_automatic_end_reason)
# Log-record parity with the origin module (caplog tests pin "hermes_state").
logger = logging.getLogger("hermes_state")

View File

@@ -17,8 +17,7 @@ from agent.memory_manager import sanitize_context
from agent.message_sanitization import _sanitize_surrogates
from hermes_state_common import (
_COMPRESSION_LOCK_ROW_SQL, _ENDED_ROW_SQL, _RESET_END_REASONS, _RESET_END_REASONS_SQL, _ended_by_compression,
_legacy_reset_child_sql, _placeholders,
)
_legacy_reset_child_sql, _placeholders)
# Log-record parity with the origin module (caplog tests pin "hermes_state").
logger = logging.getLogger("hermes_state")
@@ -258,8 +257,7 @@ class SessionMessagesMixin:
msg.get("platform_message_id") or msg.get("message_id"),
1 if msg.get("observed") else 0, 1 if msg.get("_compressed_summary") else 0, 1,
_str_or_none(msg.get("api_content")), _str_or_none(msg.get("display_kind")),
self._encode_display_metadata(msg.get("display_metadata")),
)
self._encode_display_metadata(msg.get("display_metadata")))
@staticmethod
def _bump_session_counters(conn, session_id: str, inserted: int, tool_calls: int, *, unit: bool) -> None: