@litfamily/lithermes 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/README.md +11 -27
  2. package/README_Ko-KR.md +11 -27
  3. package/assets/lithermes-plugin/README.md +35 -4
  4. package/assets/lithermes-plugin/__init__.py +25 -2
  5. package/assets/lithermes-plugin/auto_handoff.py +733 -0
  6. package/assets/lithermes-plugin/core.py +10 -0
  7. package/assets/lithermes-plugin/diagnostics.py +12 -2
  8. package/assets/lithermes-plugin/handoff.py +13 -2
  9. package/assets/lithermes-plugin/payload-version.json +15 -11
  10. package/assets/lithermes-plugin/plugin.yaml +1 -1
  11. package/assets/lithermes-plugin/skills/frontend-ui-ux/references/complete-contract.md +2 -2
  12. package/assets/lithermes-plugin/skills/lit-handoff/SKILL.md +7 -0
  13. package/assets/lithermes-plugin/skills/visual-qa/references/complete-contract.md +2 -2
  14. package/package.json +1 -1
  15. package/readme-assets/badge-version.svg +1 -1
  16. package/src/lib/modelRoutePolicy.js +9 -1
  17. package/readme-assets/ab-simple/s11-ui-baseline-desktop.webp +0 -0
  18. package/readme-assets/ab-simple/s11-ui-lithermes-desktop.webp +0 -0
  19. package/readme-assets/ab-simple/s3-ui-baseline-desktop.webp +0 -0
  20. package/readme-assets/ab-simple/s3-ui-lithermes-desktop.webp +0 -0
  21. package/readme-assets/ab-simple/s4-ui-baseline-desktop.webp +0 -0
  22. package/readme-assets/ab-simple/s4-ui-lithermes-desktop.webp +0 -0
  23. package/readme-assets/ab-simple/s5-office-baseline-slides.webp +0 -0
  24. package/readme-assets/ab-simple/s5-office-lithermes-slides.webp +0 -0
  25. package/readme-assets/ab-simple/s7-lithermes-diagram.webp +0 -0
  26. package/readme-assets/ab-simple/s8-office-baseline-slides.webp +0 -0
  27. package/readme-assets/ab-simple/s8-office-lithermes-slides.webp +0 -0
  28. package/readme-assets/ab-simple/s9-office-lithermes-pages.webp +0 -0
@@ -0,0 +1,733 @@
1
+ """Opt-in automatic handoff for Hermes sessions.
2
+
3
+ Off by default. The user turns it on with ``/lit-handoff auto on <percent>`` or
4
+ with ``LITHERMES_AUTO_HANDOFF=1`` plus ``LITHERMES_AUTO_HANDOFF_PERCENT``. The
5
+ percent is always the user's own: there is no built-in value, and a value that
6
+ is not a whole number from 1 to 99 leaves the feature off.
7
+
8
+ How it works on this host, step by step:
9
+
10
+ 1. ``post_api_request`` reports the prompt size of every model call. The plugin
11
+ divides it by the model's context window and keeps the latest reading per
12
+ session. Crossing the user's percent marks one pending directive.
13
+ 2. ``pre_llm_call`` is the only hook whose return value reaches the model, so
14
+ the directive rides on the next user turn. It asks the model to write the
15
+ handoff with the bundled lit-handoff procedure, to put a one-time id line in
16
+ the file, and to tell the user to run the compact command.
17
+ 3. A plugin cannot start compaction on Hermes. The user runs it, or Hermes
18
+ compacts on its own threshold. The next ``pre_llm_call`` notices the new
19
+ compaction summary in the history and loads a bounded digest of the handoff,
20
+ but only if the file carries this session's id and was written after the
21
+ directive. Anything else is refused.
22
+ 4. Hermes moves a compacted conversation to a new session id. When a turn
23
+ arrives under an id this process has no waiting state for, carries a new
24
+ compaction summary, and exactly one session with the same platform and
25
+ profile is waiting for its reload, that state moves to the new id (a
26
+ ``parent_session_id`` that names a waiting session settles it directly).
27
+ With two or more waiting sessions and no named parent, nothing is adopted.
28
+
29
+ The feature fires once per crossing of the percent, never inside a tool call,
30
+ and never for delegate children. State is kept in memory per session; only the
31
+ user's switch and last percent persist, in Hermes home ``lithermes/``.
32
+ """
33
+
34
+ from __future__ import annotations
35
+
36
+ import contextlib
37
+ import hashlib
38
+ import html
39
+ import importlib
40
+ import json
41
+ import os
42
+ import re
43
+ import secrets
44
+ import stat
45
+ import threading
46
+ import time
47
+ from collections.abc import Mapping
48
+ from dataclasses import dataclass
49
+ from pathlib import Path
50
+ from typing import Any, Callable
51
+
52
+ try:
53
+ from .core_runtime import assert_within_isolation, get_hermes_home, utc_now
54
+ from .provider_metrics import _nonnegative_int, _value
55
+ from .redaction import redact_text
56
+ from .rules import constants as _RULE_CONSTANTS
57
+ from .session_context import is_delegate_child_platform
58
+ except (ImportError, ModuleNotFoundError):
59
+ from core_runtime import assert_within_isolation, get_hermes_home, utc_now # type: ignore
60
+ from provider_metrics import _nonnegative_int, _value # type: ignore
61
+ from redaction import redact_text # type: ignore
62
+ from rules import constants as _RULE_CONSTANTS # type: ignore
63
+ from session_context import is_delegate_child_platform # type: ignore
64
+
65
+
66
+ FLAG = "LITHERMES_AUTO_HANDOFF"
67
+ PERCENT_ENV = "LITHERMES_AUTO_HANDOFF_PERCENT"
68
+ SESSION_ENV = "HERMES_SESSION_ID"
69
+ SETTINGS_FILE = "auto-handoff.json"
70
+ ROUTE = "/lit-handoff auto"
71
+ SOURCE_ROOT = Path(__file__).resolve().parent / "vendor" / "handoff"
72
+ SOURCE_SKILL = SOURCE_ROOT / "SKILL.md"
73
+ SOURCE_TEMPLATE = SOURCE_ROOT / "templates" / "HANDOFF.md"
74
+ MAX_TRACKED_SESSIONS = 256
75
+ MAX_SETTINGS_BYTES = 4096
76
+ MAX_HANDOFF_READ_BYTES = 64 * 1024
77
+ MAX_HANDOFF_FILE_BYTES = 2 * 1024 * 1024
78
+ MAX_DIGEST_BYTES = 1400
79
+ MAX_DIGEST_LINES = 10
80
+ MAX_DIGEST_LINE_CHARS = 200
81
+ HOST_LOOKUP_SECONDS = 3.0
82
+ FAILED_LOOKUP_RETRY_SECONDS = 60.0
83
+ CLOCK_SLACK_SECONDS = 2.0
84
+ HANDOFF_CANDIDATES = ("HANDOFF.md", ".handoff/HANDOFF.md")
85
+ PERCENT_RULE = "a whole number from 1 to 99"
86
+ _NOFOLLOW = getattr(os, "O_NOFOLLOW", 0)
87
+ _PERCENT = re.compile(r"[0-9]{1,2}")
88
+ _BARE_HANDOFF = re.compile(r"^\s*handoff\s*$", re.IGNORECASE)
89
+ _HEADING = re.compile(r"^#{1,4}\s*(.+?)\s*$")
90
+ _WANTED_SECTIONS = (("current state", "Current State"), ("next steps", "Next Steps"))
91
+ _LOCK = threading.RLock()
92
+ _SESSIONS: dict[str, dict[str, Any]] = {}
93
+ _WINDOWS: dict[tuple[str, str, str], tuple[int | None, float]] = {}
94
+ _LAST_SESSION: list[str] = [""]
95
+
96
+
97
+ @dataclass(frozen=True)
98
+ class Setting:
99
+ on: bool
100
+ percent: int | None
101
+ flag_source: str
102
+ percent_source: str
103
+ warnings: tuple[str, ...]
104
+
105
+
106
+ def _reset_for_tests() -> None:
107
+ with _LOCK:
108
+ _SESSIONS.clear()
109
+ _WINDOWS.clear()
110
+ _LAST_SESSION[0] = ""
111
+
112
+
113
+ def parse_percent(text: Any) -> int | None:
114
+ """Return the percent for a whole number from 1 to 99, else None."""
115
+ value = str(text or "").strip()
116
+ if value.endswith("%"):
117
+ value = value[:-1]
118
+ if not _PERCENT.fullmatch(value):
119
+ return None
120
+ number = int(value)
121
+ return number if 1 <= number <= 99 else None
122
+
123
+
124
+ # ---- the user's switch -----------------------------------------------------
125
+
126
+ def settings_path() -> Path:
127
+ return get_hermes_home() / "lithermes" / SETTINGS_FILE
128
+
129
+
130
+ def _read_saved() -> tuple[dict[str, Any], str]:
131
+ """Return the saved switch and, when the file is damaged, one warning."""
132
+ blank: dict[str, Any] = {"enabled": False, "percent": None, "exists": False}
133
+ try:
134
+ fd = os.open(settings_path(), os.O_RDONLY | _NOFOLLOW)
135
+ except OSError:
136
+ return blank, ""
137
+ try:
138
+ with os.fdopen(fd, "rb") as handle:
139
+ raw = handle.read(MAX_SETTINGS_BYTES + 1)
140
+ data = json.loads(raw[:MAX_SETTINGS_BYTES].decode("utf-8"))
141
+ except (OSError, ValueError, UnicodeDecodeError):
142
+ return blank, "the saved setting file could not be read, so automatic handoff stays off"
143
+ if not isinstance(data, dict):
144
+ return blank, "the saved setting file could not be read, so automatic handoff stays off"
145
+ enabled = data.get("enabled") is True
146
+ percent = data.get("percent")
147
+ if percent is None:
148
+ return {"enabled": enabled, "percent": None, "exists": True}, ""
149
+ if isinstance(percent, bool) or not isinstance(percent, int) or not 1 <= percent <= 99:
150
+ return {"enabled": enabled, "percent": None, "exists": True}, (
151
+ f"the saved percent is not {PERCENT_RULE}, so automatic handoff stays off"
152
+ )
153
+ return {"enabled": enabled, "percent": percent, "exists": True}, ""
154
+
155
+
156
+ def _write_saved(enabled: bool, percent: int | None) -> None:
157
+ path = settings_path()
158
+ assert_within_isolation(path)
159
+ path.parent.mkdir(parents=True, exist_ok=True)
160
+ temporary = path.with_name(f".{path.name}.{os.getpid()}.{threading.get_ident()}.tmp")
161
+ payload = {"enabled": enabled, "percent": percent, "updated": utc_now().isoformat()}
162
+ try:
163
+ with contextlib.suppress(OSError):
164
+ temporary.unlink()
165
+ fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | _NOFOLLOW, 0o600)
166
+ with os.fdopen(fd, "w", encoding="utf-8") as handle:
167
+ json.dump(payload, handle, sort_keys=True)
168
+ # os.replace swaps a symlink planted at the final path instead of writing through it.
169
+ os.replace(temporary, path)
170
+ except OSError:
171
+ with contextlib.suppress(OSError):
172
+ temporary.unlink()
173
+ raise
174
+
175
+
176
+ def resolve(env: Mapping[str, str] | None = None) -> Setting:
177
+ """Combine the environment and the saved switch into the effective setting.
178
+
179
+ The environment wins when it names a value, so a shell profile can pin the
180
+ feature; the saved value fills in otherwise. No percent is ever invented.
181
+ """
182
+ values = os.environ if env is None else env
183
+ saved, saved_warning = _read_saved()
184
+ warnings: list[str] = []
185
+
186
+ raw_flag = (values.get(FLAG) or "").strip()
187
+ if raw_flag:
188
+ enabled, flag_source = raw_flag == "1", "environment"
189
+ else:
190
+ enabled, flag_source = bool(saved["enabled"]), ("saved" if saved["exists"] else "default")
191
+
192
+ raw_percent = (values.get(PERCENT_ENV) or "").strip()
193
+ if raw_percent:
194
+ percent, percent_source = parse_percent(raw_percent), "environment"
195
+ if percent is None:
196
+ warnings.append(f"{PERCENT_ENV} is {raw_percent[:20]!r}, which is not {PERCENT_RULE}, so automatic handoff stays off")
197
+ else:
198
+ percent = saved["percent"]
199
+ percent_source = "saved" if percent is not None else "none"
200
+ if saved_warning:
201
+ warnings.append(saved_warning)
202
+
203
+ if enabled and percent is None and not warnings:
204
+ warnings.append(f"it is switched on but no percent is chosen; run {ROUTE} on <percent>")
205
+ return Setting(enabled and percent is not None, percent, flag_source, percent_source, tuple(warnings))
206
+
207
+
208
+ def parse_route(raw_args: str) -> str | None:
209
+ """Return the text after ``auto`` when the route's first word is exactly ``auto``."""
210
+ parts = str(raw_args or "").strip().split(None, 1)
211
+ if not parts or parts[0].lower() != "auto":
212
+ return None
213
+ return parts[1].strip() if len(parts) > 1 else ""
214
+
215
+
216
+ def _shown(value: Any) -> str:
217
+ return redact_text(str(value))[:20]
218
+
219
+
220
+ def run_command(rest: str, env: Mapping[str, str] | None = None, session_id: str = "") -> str:
221
+ """Handle ``/lit-handoff auto on <percent> | on | off | status`` as plain text."""
222
+ values = os.environ if env is None else env
223
+ tokens = str(rest or "").split()
224
+ sub = tokens[0].lower() if tokens else "status"
225
+ if sub == "status" and len(tokens) <= 1:
226
+ return _status_text(values, session_id)
227
+ if sub == "off" and len(tokens) == 1:
228
+ return _command_off(values)
229
+ if sub == "on" and len(tokens) <= 2:
230
+ return _command_on(values, tokens[1] if len(tokens) == 2 else "")
231
+ return f"Usage: {ROUTE} on <percent> | off | status"
232
+
233
+
234
+ def _command_off(values: Mapping[str, str]) -> str:
235
+ saved, _ = _read_saved()
236
+ try:
237
+ _write_saved(False, saved["percent"])
238
+ except (OSError, RuntimeError) as exc:
239
+ return f"Could not save the setting ({type(exc).__name__}); nothing changed."
240
+ lines = ["Automatic handoff is off."]
241
+ if saved["percent"] is not None:
242
+ lines.append(f"Your last percent, {saved['percent']}%, is remembered for the next time you turn it on.")
243
+ if (values.get(FLAG) or "").strip() == "1":
244
+ lines.append(f"{FLAG}=1 is set in your environment, so it stays on until you unset that variable.")
245
+ return " ".join(lines)
246
+
247
+
248
+ def _command_on(values: Mapping[str, str], argument: str) -> str:
249
+ saved, _ = _read_saved()
250
+ if argument:
251
+ percent = parse_percent(argument)
252
+ if percent is None:
253
+ return f"{_shown(argument)!r} is not {PERCENT_RULE}. Nothing changed."
254
+ elif saved["percent"] is not None:
255
+ percent = saved["percent"]
256
+ else:
257
+ return (
258
+ f"Which percent should trigger it? Run {ROUTE} on <percent> with {PERCENT_RULE}. "
259
+ "LitHermes has no built-in value and will not pick one for you."
260
+ )
261
+ try:
262
+ _write_saved(True, percent)
263
+ except (OSError, RuntimeError) as exc:
264
+ return f"Could not save the setting ({type(exc).__name__}); nothing changed."
265
+ lines = [
266
+ f"Automatic handoff is ON at {percent}%. Once a model call passes {percent}% of the context window, "
267
+ f"your next message carries a request to write a handoff, and you then run {_compact_command()}. "
268
+ "It starts with your next message."
269
+ ]
270
+ flag = (values.get(FLAG) or "").strip()
271
+ if flag and flag != "1":
272
+ lines.append(f"{FLAG}={_shown(flag)} is set in your environment, which keeps it off until you unset that variable.")
273
+ environment_percent = (values.get(PERCENT_ENV) or "").strip()
274
+ if environment_percent:
275
+ lines.append(f"{PERCENT_ENV}={_shown(environment_percent)} is set in your environment and wins over the saved {percent}%; unset it to use {percent}%.")
276
+ lines.append(_host_comparison(resolve(values))[0])
277
+ return "\n".join(lines)
278
+
279
+
280
+ def _status_text(values: Mapping[str, str], session_id: str) -> str:
281
+ setting = resolve(values)
282
+ if setting.on:
283
+ head = f"Automatic handoff: ON at {setting.percent}% (percent from your {setting.percent_source} setting)"
284
+ elif setting.warnings:
285
+ head = f"Automatic handoff: off ({'; '.join(setting.warnings)})"
286
+ else:
287
+ head = "Automatic handoff: off"
288
+ lines = [head]
289
+ if setting.on:
290
+ lines.append(_reading_line(session_id or (values.get(SESSION_ENV) or "").strip() or _LAST_SESSION[0]))
291
+ lines.append(_host_comparison(setting)[0])
292
+ return "\n".join(lines)
293
+
294
+
295
+ def _reading_line(session_id: str) -> str:
296
+ with _LOCK:
297
+ state = _SESSIONS.get(session_id)
298
+ reading = dict(state) if state else {}
299
+ if not reading or reading.get("used") is None:
300
+ return "Last reading in this session: no reading yet"
301
+ if reading.get("percent") is None:
302
+ return "Last reading in this session: unavailable (the model's context window is unknown)"
303
+ return (
304
+ f"Last reading in this session: {reading['percent']:.0f}% "
305
+ f"({reading['used']:,} of {reading['window']:,} tokens)"
306
+ )
307
+
308
+
309
+ def status_line(env: Mapping[str, str] | None = None) -> str:
310
+ setting = resolve(env)
311
+ if setting.on:
312
+ return f"Automatic handoff: on at {setting.percent}%"
313
+ if setting.warnings:
314
+ return f"Automatic handoff: off ({'; '.join(setting.warnings)})"
315
+ return "Automatic handoff: off"
316
+
317
+
318
+ def doctor_line(env: Mapping[str, str] | None = None) -> tuple[str, str]:
319
+ """Return (tag, text) for doctor: NOTE when off, WARN when it cannot work as chosen."""
320
+ setting = resolve(env)
321
+ if setting.warnings and not setting.on:
322
+ return "WARN", f"Automatic handoff: off ({'; '.join(setting.warnings)})"
323
+ if not setting.on:
324
+ return "NOTE", "Automatic handoff: off"
325
+ point, reason = host_estimate()
326
+ if point is None:
327
+ return "OK", f"Automatic handoff: on at {setting.percent}% ({reason}; no comparison with Hermes' own compaction point)"
328
+ if setting.percent >= round(point):
329
+ return "WARN", (
330
+ f"Automatic handoff: on at {setting.percent}%, but Hermes compacts at about {point:.0f}% of the window, "
331
+ "so Hermes compacts first; choose a lower percent"
332
+ )
333
+ return "OK", f"Automatic handoff: on at {setting.percent}% (Hermes compacts at about {point:.0f}% of the window)"
334
+
335
+
336
+ def _host_comparison(setting: Setting) -> tuple[str, bool]:
337
+ """Say where Hermes' own compaction sits next to the user's percent; True when it wins."""
338
+ point, reason = host_estimate()
339
+ if point is None:
340
+ return f"Hermes' own compaction point: unavailable ({reason})", False
341
+ if setting.percent >= round(point):
342
+ return (
343
+ f"Heads up: Hermes compacts at about {point:.0f}% of the window, so it compacts first and "
344
+ "no handoff is written. Choose a lower percent.",
345
+ True,
346
+ )
347
+ return f"Hermes' own compaction point is about {point:.0f}% of the window, above yours.", False
348
+
349
+
350
+ # ---- the host's own compaction point ---------------------------------------
351
+
352
+ def _host_config() -> dict[str, Any]:
353
+ try:
354
+ path = get_hermes_home() / "config.yaml"
355
+ if path.stat().st_size > 1024 * 1024:
356
+ return {}
357
+ yaml = importlib.import_module("yaml")
358
+ data = yaml.safe_load(path.read_text(encoding="utf-8"))
359
+ except Exception: # noqa: BLE001 - a config we cannot read only removes the comparison
360
+ return {}
361
+ return data if isinstance(data, dict) else {}
362
+
363
+
364
+ def _host_facts() -> dict[str, Any]:
365
+ """Read the running host's own compaction defaults; empty outside Hermes."""
366
+ facts: dict[str, Any] = {}
367
+ with contextlib.suppress(Exception):
368
+ module = importlib.import_module("agent.context_compressor")
369
+ facts["floor"] = (int(module._SMALL_CTX_WINDOW_LIMIT), float(module._SMALL_CTX_THRESHOLD_PERCENT))
370
+ with contextlib.suppress(Exception):
371
+ defaults = importlib.import_module("hermes_cli.config_defaults").DEFAULT_CONFIG["compression"]
372
+ if isinstance(defaults, dict):
373
+ facts["compression"] = dict(defaults)
374
+ return facts
375
+
376
+
377
+ def _ratio(value: Any) -> float | None:
378
+ if isinstance(value, bool) or not isinstance(value, (int, float)):
379
+ return None
380
+ return float(value) if 0 < value <= 1 else None
381
+
382
+
383
+ def host_trigger_percent(
384
+ window: int,
385
+ compression: Mapping[str, Any],
386
+ model: str,
387
+ floor: tuple[int, float] | None = None,
388
+ ) -> float | None:
389
+ """Estimate where Hermes' own compaction starts, as a percent of the window.
390
+
391
+ Follows the documented compression settings: the ratio (with a per-model
392
+ override and the small-window floor when the host has one) and the absolute
393
+ token cap. Returns None when compression is switched off.
394
+ """
395
+ if compression.get("enabled") is False:
396
+ return None
397
+ ratio = _ratio(compression.get("threshold")) or 0.50
398
+ overrides = compression.get("model_thresholds")
399
+ if isinstance(overrides, Mapping) and model:
400
+ keys = [key for key in overrides if isinstance(key, str) and key and key.lower() in model.lower()]
401
+ if keys:
402
+ best = max(keys, key=len)
403
+ ratio = _ratio(overrides[best]) or ratio
404
+ if floor is not None and window < floor[0]:
405
+ ratio = max(ratio, floor[1])
406
+ trigger = int(window * ratio)
407
+ cap = compression.get("threshold_tokens")
408
+ if isinstance(cap, int) and not isinstance(cap, bool) and cap > 0:
409
+ trigger = min(trigger, cap, window)
410
+ return trigger * 100 / window
411
+
412
+
413
+ def _bounded(function: Callable[[], Any], seconds: float = HOST_LOOKUP_SECONDS) -> Any:
414
+ """Run a host lookup that may touch the network, giving up after a few seconds."""
415
+ box: list[Any] = [None]
416
+
417
+ def work() -> None:
418
+ with contextlib.suppress(Exception):
419
+ box[0] = function()
420
+
421
+ thread = threading.Thread(target=work, daemon=True)
422
+ thread.start()
423
+ thread.join(seconds)
424
+ return box[0]
425
+
426
+
427
+ def _host_context_length(model: str, base_url: str, provider: str) -> int | None:
428
+ """Ask the running Hermes for the model's context window; None outside a host."""
429
+ function = importlib.import_module("agent.model_metadata").get_model_context_length
430
+ value = function(model, base_url or "", provider=provider or "")
431
+ return value if isinstance(value, int) and not isinstance(value, bool) and value > 0 else None
432
+
433
+
434
+ def _context_window(model: str, base_url: str, provider: str, config: Mapping[str, Any] | None = None) -> int | None:
435
+ key = (model, base_url, provider)
436
+ now = time.monotonic()
437
+ with _LOCK:
438
+ cached = _WINDOWS.get(key)
439
+ if cached and (cached[0] is not None or cached[1] > now):
440
+ return cached[0]
441
+ section = (config if config is not None else _host_config()).get("model")
442
+ section = section if isinstance(section, Mapping) else {}
443
+ configured = section.get("context_length")
444
+ window: int | None = None
445
+ if isinstance(configured, int) and not isinstance(configured, bool) and configured > 0:
446
+ if not section.get("default") or section.get("default") == model:
447
+ window = configured
448
+ if window is None and model:
449
+ found = _bounded(lambda: _host_context_length(model, base_url, provider))
450
+ window = found if isinstance(found, int) and found > 0 else None
451
+ with _LOCK:
452
+ while len(_WINDOWS) >= MAX_TRACKED_SESSIONS:
453
+ _WINDOWS.pop(next(iter(_WINDOWS)), None)
454
+ _WINDOWS[key] = (window, now + FAILED_LOOKUP_RETRY_SECONDS)
455
+ return window
456
+
457
+
458
+ def host_estimate() -> tuple[float | None, str]:
459
+ """Estimate Hermes' compaction point for the configured model, or say why not."""
460
+ config = _host_config()
461
+ section = config.get("model") if isinstance(config.get("model"), Mapping) else {}
462
+ model = str(section.get("default") or "")
463
+ window = _context_window(model, str(section.get("base_url") or ""), str(section.get("provider") or ""), config)
464
+ if window is None:
465
+ return None, "the model's context window is unknown (window unknown)"
466
+ facts = _host_facts()
467
+ compression = dict(facts.get("compression") or {})
468
+ user = config.get("compression")
469
+ if isinstance(user, Mapping):
470
+ compression.update(user)
471
+ point = host_trigger_percent(window, compression, model, facts.get("floor"))
472
+ if point is None:
473
+ return None, "Hermes compression is switched off"
474
+ return point, ""
475
+
476
+
477
+ def _compact_command() -> str:
478
+ """The slash command that compacts here; only Hermes 0.19 and later know /compact."""
479
+ try:
480
+ resolve_command = importlib.import_module("hermes_cli.commands").resolve_command
481
+ return "/compact" if resolve_command("compact") else "/compress"
482
+ except Exception: # noqa: BLE001 - outside a host the plain wording is the right default
483
+ return "/compact"
484
+
485
+
486
+ # ---- reading the model's context use ----------------------------------------
487
+
488
+ def _prompt_tokens(usage: Any) -> int | None:
489
+ if usage is None:
490
+ return None
491
+ prompt = _nonnegative_int(_value(usage, "prompt_tokens"))
492
+ if prompt is not None:
493
+ return prompt
494
+ parts = [_nonnegative_int(_value(usage, name)) for name in ("input_tokens", "cache_read_tokens", "cache_write_tokens")]
495
+ return sum(parts) if all(part is not None for part in parts) else None
496
+
497
+
498
+ def _state(session_id: str) -> dict[str, Any]:
499
+ state = _SESSIONS.get(session_id)
500
+ if state is None:
501
+ while len(_SESSIONS) >= MAX_TRACKED_SESSIONS:
502
+ _SESSIONS.pop(next(iter(_SESSIONS)), None)
503
+ state = _SESSIONS[session_id] = {
504
+ "percent": None, "used": None, "window": None,
505
+ "above": False, "pending": False, "awaiting": False, "dropped": False,
506
+ "nonce": "", "fired_at": 0.0, "cwd": "", "summary": "", "platform": "", "home": "",
507
+ }
508
+ return state
509
+
510
+
511
+ def begin_session(session_id: Any) -> None:
512
+ with _LOCK:
513
+ _SESSIONS.pop(str(session_id or ""), None)
514
+
515
+
516
+ def release_session(session_id: Any) -> None:
517
+ begin_session(session_id)
518
+
519
+
520
+ def post_api_request(**kwargs: Any) -> None:
521
+ """Keep the latest context reading and mark a crossing of the user's percent."""
522
+ setting = resolve()
523
+ if not setting.on:
524
+ return None
525
+ session_id = str(kwargs.get("session_id") or "")
526
+ if not session_id or is_delegate_child_platform(str(kwargs.get("platform") or "")):
527
+ return None
528
+ used = _prompt_tokens(kwargs.get("usage"))
529
+ if used is None:
530
+ return None
531
+ window = _context_window(str(kwargs.get("model") or ""), str(kwargs.get("base_url") or ""), str(kwargs.get("provider") or ""))
532
+ with _LOCK:
533
+ state = _state(session_id)
534
+ _LAST_SESSION[0] = session_id
535
+ state["used"], state["window"] = used, window
536
+ if window is None:
537
+ state["percent"] = None
538
+ return None
539
+ percent = used * 100 / window
540
+ state["percent"] = percent
541
+ if percent >= setting.percent:
542
+ if not state["above"]:
543
+ state["above"] = True
544
+ if not state["awaiting"]:
545
+ state["pending"] = True
546
+ else:
547
+ state["above"] = False
548
+ state["pending"] = False
549
+ if state["awaiting"]:
550
+ state["dropped"] = True
551
+ return None
552
+
553
+
554
+ # ---- the directive and the reload -------------------------------------------
555
+
556
+ def _summary_fingerprint(history: Any) -> str:
557
+ if not isinstance(history, (list, tuple)):
558
+ return ""
559
+ parts: list[str] = []
560
+ for message in history:
561
+ if not isinstance(message, dict):
562
+ continue
563
+ content = str(message.get("content") or "")
564
+ lowered = content.lower()
565
+ if message.get(_RULE_CONSTANTS.COMPACTION_METADATA_KEY) or any(
566
+ marker in lowered for marker in _RULE_CONSTANTS.COMPACTION_TEXT_MARKERS
567
+ ):
568
+ parts.append(content[:4000])
569
+ return hashlib.sha256("\x00".join(parts).encode("utf-8", "replace")).hexdigest()[:16] if parts else ""
570
+
571
+
572
+ def _explicit_handoff_turn(message: str) -> bool:
573
+ return bool(_BARE_HANDOFF.fullmatch(message)) or "<lithermes-handoff-route" in message
574
+
575
+
576
+ def pre_llm_call(**kwargs: Any) -> str:
577
+ """Return this turn's directive or reload block, or an empty string."""
578
+ setting = resolve()
579
+ if not setting.on:
580
+ return ""
581
+ session_id = str(kwargs.get("session_id") or "")
582
+ if not session_id or is_delegate_child_platform(str(kwargs.get("platform") or "")):
583
+ return ""
584
+ history = kwargs.get("conversation_history")
585
+ platform = str(kwargs.get("platform") or "")
586
+ with _LOCK:
587
+ state = _SESSIONS.get(session_id)
588
+ if state is None or _is_fresh(state):
589
+ adopted = _adopt_rotated(session_id, str(kwargs.get("parent_session_id") or ""), platform, history)
590
+ state = adopted or state
591
+ if state is None:
592
+ return ""
593
+ _LAST_SESSION[0] = session_id
594
+ if state["awaiting"]:
595
+ fingerprint = _summary_fingerprint(history)
596
+ if fingerprint and fingerprint != state["summary"]:
597
+ state["awaiting"], state["dropped"] = False, False
598
+ return _reload_block(state)
599
+ if state["dropped"]:
600
+ state["awaiting"], state["dropped"] = False, False
601
+ return ""
602
+ if not state["pending"] or _explicit_handoff_turn(str(kwargs.get("user_message") or "")):
603
+ return ""
604
+ if state["percent"] is None:
605
+ return ""
606
+ nonce = secrets.token_hex(6)
607
+ block = _directive_block({**state, "nonce": nonce}, setting.percent)
608
+ state["pending"], state["awaiting"], state["dropped"] = False, True, False
609
+ state["nonce"] = nonce
610
+ state["fired_at"] = time.time()
611
+ state["cwd"] = os.getcwd()
612
+ state["summary"] = _summary_fingerprint(history)
613
+ state["platform"], state["home"] = platform, str(get_hermes_home())
614
+ return block
615
+
616
+
617
+ def _is_fresh(state: Mapping[str, Any]) -> bool:
618
+ """A state that has only read usage: never crossed, never fired."""
619
+ return not (state["awaiting"] or state["pending"] or state["nonce"])
620
+
621
+
622
+ def _adopt_rotated(
623
+ session_id: str, parent_session_id: str, platform: str, history: Any
624
+ ) -> dict[str, Any] | None:
625
+ """Move a waiting state to the new id Hermes gave the conversation when it compacted.
626
+
627
+ Returns the moved state, or None when nothing is adopted. Caller holds the lock.
628
+ """
629
+ if not any(value["awaiting"] for value in _SESSIONS.values()):
630
+ return None
631
+ summary = _summary_fingerprint(history)
632
+ if not summary:
633
+ return None
634
+ home = str(get_hermes_home())
635
+ waiting = {
636
+ key: value for key, value in _SESSIONS.items()
637
+ if key != session_id and value["awaiting"] and value["platform"] == platform
638
+ and value["home"] == home and summary != value["summary"]
639
+ }
640
+ if parent_session_id in waiting:
641
+ old_id = parent_session_id
642
+ elif len(waiting) == 1:
643
+ old_id = next(iter(waiting))
644
+ else:
645
+ return None
646
+ state = waiting[old_id]
647
+ fresh = _SESSIONS.pop(session_id, None)
648
+ if fresh is not None:
649
+ for name in ("percent", "used", "window", "above"):
650
+ state[name] = fresh[name]
651
+ del _SESSIONS[old_id]
652
+ _SESSIONS[session_id] = state
653
+ return state
654
+
655
+
656
+ def _directive_block(state: Mapping[str, Any], percent: int) -> str:
657
+ return "\n".join([
658
+ f'<lithermes-auto-handoff id="{state["nonce"]}" used="{state["percent"]:.0f}%" threshold="{percent}%">',
659
+ f"This conversation has used {state['percent']:.0f}% of the model's context window, past the {percent}% "
660
+ "the user chose for an automatic handoff. Write the handoff now, before anything else:",
661
+ f"1. Read {SOURCE_SKILL} and follow that lit-handoff procedure. The template is {SOURCE_TEMPLATE}.",
662
+ f"2. Save the handoff where the procedure says, and put this exact line on its own line near the top: auto-handoff-id: {state['nonce']}",
663
+ f"3. Read the file back, then tell the user exactly this one line: Handoff saved. Run {_compact_command()} now.",
664
+ "After that, carry on with the user's request. The user set this up in their own settings.",
665
+ "</lithermes-auto-handoff>",
666
+ ])
667
+
668
+
669
+ def _find_handoff(state: Mapping[str, Any]) -> tuple[str, float, str] | None:
670
+ found: tuple[str, float, str] | None = None
671
+ for relative in HANDOFF_CANDIDATES:
672
+ path = Path(state["cwd"]) / relative
673
+ try:
674
+ info = os.lstat(path)
675
+ if not stat.S_ISREG(info.st_mode) or info.st_size > MAX_HANDOFF_FILE_BYTES:
676
+ continue
677
+ if info.st_mtime < state["fired_at"] - CLOCK_SLACK_SECONDS:
678
+ continue
679
+ fd = os.open(path, os.O_RDONLY | _NOFOLLOW)
680
+ with os.fdopen(fd, "rb") as handle:
681
+ text = handle.read(MAX_HANDOFF_READ_BYTES).decode("utf-8", "replace")
682
+ except OSError:
683
+ continue
684
+ if re.search(rf"auto-handoff-id:[ \t]*{re.escape(state['nonce'])}(?![0-9a-f])", text) is None:
685
+ continue
686
+ if found is None or info.st_mtime > found[1]:
687
+ found = (relative, info.st_mtime, text)
688
+ return found
689
+
690
+
691
+ def _digest(text: str, nonce: str) -> str:
692
+ sections: dict[str, list[str]] = {}
693
+ current = ""
694
+ body: list[str] = []
695
+ for line in text.splitlines():
696
+ heading = _HEADING.match(line)
697
+ if heading:
698
+ title = heading.group(1).lower()
699
+ current = next((label for key, label in _WANTED_SECTIONS if key in title), "")
700
+ continue
701
+ if nonce in line or not line.strip():
702
+ continue
703
+ if len(body) < MAX_DIGEST_LINES:
704
+ body.append(line.strip()[:MAX_DIGEST_LINE_CHARS])
705
+ if current and len(sections.setdefault(current, [])) < MAX_DIGEST_LINES:
706
+ sections[current].append(line.strip()[:MAX_DIGEST_LINE_CHARS])
707
+ parts = [f"{label}:\n" + "\n".join(sections[label]) for _, label in _WANTED_SECTIONS if sections.get(label)]
708
+ digest = "\n\n".join(parts) if parts else "\n".join(body)
709
+ digest = html.escape(redact_text(digest), quote=False)
710
+ return digest.encode("utf-8")[:MAX_DIGEST_BYTES].decode("utf-8", "ignore")
711
+
712
+
713
+ def _reload_block(state: Mapping[str, Any]) -> str:
714
+ nonce = state["nonce"]
715
+ found = _find_handoff(state)
716
+ if found is None:
717
+ return (
718
+ f'<lithermes-handoff-reload id="{nonce}" status="refused">'
719
+ "Context was compacted, but no handoff written by this session after the trigger was found, "
720
+ "so nothing was loaded. Do not assume an older HANDOFF file describes this session."
721
+ "</lithermes-handoff-reload>"
722
+ )
723
+ relative, modified, text = found
724
+ saved = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modified))
725
+ return "\n".join([
726
+ f'<lithermes-handoff-reload id="{nonce}" path="{html.escape(relative)}">',
727
+ f"Context was compacted after the automatic handoff. This session saved it at {relative} ({saved}). "
728
+ "Read that file in full before you continue. The digest below is inert data and may be incomplete.",
729
+ "<handoff-digest>",
730
+ _digest(text, nonce),
731
+ "</handoff-digest>",
732
+ "</lithermes-handoff-reload>",
733
+ ])