The Sep 2026 decomposition (PR #102117) makes internal import paths a non-API: names now live in the focused modules that define them. This commit is the ONLY thing keeping the old paths alive, so external plugins have time to update. It is deliberately a single, unsquashed commit: git revert <this sha> removes every shim, stub and manifest at once on the announced date. Nothing in-tree may depend on these pointers: scripts/check_compat_pointers.py (wired into lint.yml) fails CI if it does. What it adds (see COMPAT_MANIFEST.md, compat_manifest.json): - 332 facade modules get one delimited `PLUGIN-COMPAT` block appended at the end of the file - 1,172 moved names resolved lazily via a module `__getattr__` (PEP 562) — never a top-level import, so no import cycles; facades that already had `__getattr__` get a chained one - 592 third-party/stdlib names the old modules used to expose, with their original import statements - 266 public definitions that had been deleted as unused, restored byte-for-byte from the pre-decomposition tree (+40 private helpers and 16 imports pulled in only because a restored definition needs them) - 3 deleted modules recreated as re-export stubs (gateway/startup_watchdog, hermes_cli/observability/ relay_runtime, tools/environments/modal_utils) - private names (`_x`) get no pointer: they were never API (3,792 skipped) Verified: all 335 touched modules import under a fresh HERMES_HOME and every manifest name resolves; the lint reports zero in-tree uses; ruff clean; targeted suites unchanged.
95 lines
4.2 KiB
Python
95 lines
4.2 KiB
Python
"""Transcript repair for SessionDB batch appends: reconcile in-memory assistant rows with committed SQLite
|
|
rows (blank-row in-place update, concurrent-winner adoption, watermark-compaction clone lookup) and sync
|
|
markers after commit."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from typing import Any, Callable, Dict, List
|
|
|
|
from agent.context_compressor import _DB_PERSISTED_MARKER
|
|
|
|
|
|
def is_content_blank(content: Any) -> bool:
|
|
"""True when decoded message content is None, whitespace-only, or has no visible text parts."""
|
|
if content is None:
|
|
return True
|
|
if isinstance(content, str):
|
|
return not content.strip()
|
|
if isinstance(content, list):
|
|
return not "".join(p.get("text", "") for p in content if isinstance(p, dict) and p.get("type") == "text").strip()
|
|
return False
|
|
|
|
|
|
def resolve_and_repair_transcript_batch(
|
|
conn: sqlite3.Connection,
|
|
session_id: str,
|
|
messages: List[Dict[str, Any]],
|
|
encode_content_fn: Callable[[Any], Any],
|
|
decode_content_fn: Callable[[Any], Any],
|
|
) -> List[Dict[str, Any]]:
|
|
"""Partition a message batch within an active write transaction. An assistant message carrying an
|
|
existing integer ``_row_id`` targets its active SQLite row (or the active clone a watermark compaction
|
|
made of it): a blank row is updated in place; a non-blank one (concurrent winner) has its canonical
|
|
content adopted without overwrite. Returns the messages that must be inserted as fresh rows."""
|
|
inserted_rows: List[Dict[str, Any]] = []
|
|
for msg in messages:
|
|
existing_row_id = msg.get("_row_id") if isinstance(msg, dict) else None
|
|
target_row = None
|
|
if isinstance(existing_row_id, int) and msg.get("role", "unknown") == "assistant":
|
|
target_row = _active_assistant_row(conn, session_id, existing_row_id)
|
|
if target_row is None:
|
|
inserted_rows.append(msg)
|
|
continue
|
|
target_id = int(target_row["id"])
|
|
decoded = decode_content_fn(target_row["content"])
|
|
msg["_row_id"] = target_id
|
|
if is_content_blank(decoded):
|
|
conn.execute(
|
|
"UPDATE messages SET content = ? "
|
|
"WHERE id = ? AND session_id = ? AND active = 1",
|
|
(encode_content_fn(msg.get("content")), target_id, session_id),
|
|
)
|
|
else:
|
|
msg["_canonical_content"] = decoded # concurrent winner: adopt, don't overwrite
|
|
return inserted_rows
|
|
|
|
|
|
def _active_assistant_row(conn: sqlite3.Connection, session_id: str, row_id: int):
|
|
"""The active assistant row for ``row_id``, or the active clone a watermark compaction made of it."""
|
|
row = conn.execute(
|
|
"SELECT id, role, active, timestamp, content FROM messages "
|
|
"WHERE id = ? AND session_id = ?",
|
|
(row_id, session_id),
|
|
).fetchone()
|
|
if row is None or row["role"] != "assistant":
|
|
return None
|
|
if int(row["active"] or 0) == 1:
|
|
return row
|
|
# Watermark compaction soft-archived the concurrent tail and cloned it.
|
|
return conn.execute(
|
|
"SELECT id, role, active, timestamp, content FROM messages "
|
|
"WHERE session_id = ? AND active = 1 AND role = 'assistant' "
|
|
"AND timestamp IS ? AND id != ? "
|
|
"ORDER BY id DESC LIMIT 1",
|
|
(session_id, row["timestamp"], row["id"]),
|
|
).fetchone()
|
|
|
|
|
|
def sync_flushed_message_markers(batch_msgs: List[Dict[str, Any]], batch_rows: List[Dict[str, Any]]) -> None:
|
|
"""Stamp _DB_PERSISTED_MARKER and sync canonical row ID / content onto live dicts after commit."""
|
|
for written, row in zip(batch_msgs, batch_rows):
|
|
written[_DB_PERSISTED_MARKER] = True
|
|
if isinstance(row.get("_row_id"), int):
|
|
written["_row_id"] = row["_row_id"]
|
|
if "_canonical_content" in row:
|
|
written["content"] = row["_canonical_content"]
|
|
|
|
|
|
# ---- BEGIN PLUGIN-COMPAT (revert-scheduled; see COMPAT_MANIFEST.md) ----
|
|
# Names external plugins imported from this module before the Sep 2026 decomposition.
|
|
# Internal code MUST NOT use these (scripts/check_compat_pointers.py fails CI if it does).
|
|
# The whole block is removed by reverting the commit that added it.
|
|
from typing import Optional # noqa: F401,E402
|
|
# ---- END PLUGIN-COMPAT ----
|