mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-29 23:41:35 +08:00
Compare commits
7 Commits
fix/plugin
...
bb/p2-mru-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4bc87bfab | ||
|
|
efd217be54 | ||
|
|
7c10c3be98 | ||
|
|
c3a2d70656 | ||
|
|
8f3e9f80cc | ||
|
|
690a0e35e1 | ||
|
|
2528684b3a |
@@ -44,6 +44,7 @@ Usage:
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
@@ -595,17 +596,22 @@ def _session_browse_picker(sessions: list) -> Optional[str]:
|
||||
|
||||
|
||||
def _resolve_last_session(source: str = "cli") -> Optional[str]:
|
||||
"""Look up the most recent session ID for a source."""
|
||||
"""Look up the most recently-used session ID for a source."""
|
||||
db = None
|
||||
try:
|
||||
from hermes_state import SessionDB
|
||||
|
||||
db = SessionDB()
|
||||
sessions = db.search_sessions(source=source, limit=1)
|
||||
db.close()
|
||||
if sessions:
|
||||
return sessions[0]["id"]
|
||||
return sessions[0]["id"] if sessions else None
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
if db is not None:
|
||||
try:
|
||||
db.close()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
@@ -760,9 +766,20 @@ def _resolve_session_by_name_or_id(name_or_id: str) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
def _print_tui_exit_summary(session_id: Optional[str]) -> None:
|
||||
def _read_tui_active_session_file(path: Optional[str]) -> Optional[str]:
|
||||
if not path:
|
||||
return None
|
||||
try:
|
||||
data = json.loads(Path(path).read_text(encoding="utf-8"))
|
||||
sid = str(data.get("session_id") or "").strip()
|
||||
return sid or None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _print_tui_exit_summary(session_id: Optional[str], active_session_file: Optional[str] = None) -> None:
|
||||
"""Print a shell-visible epilogue after TUI exits."""
|
||||
target = session_id or _resolve_last_session(source="tui")
|
||||
target = _read_tui_active_session_file(active_session_file) or session_id or _resolve_last_session(source="tui")
|
||||
if not target:
|
||||
return
|
||||
|
||||
@@ -1037,7 +1054,14 @@ def _launch_tui(
|
||||
"""Replace current process with the TUI."""
|
||||
tui_dir = PROJECT_ROOT / "ui-tui"
|
||||
|
||||
import tempfile
|
||||
|
||||
env = os.environ.copy()
|
||||
active_session_fd, active_session_file = tempfile.mkstemp(
|
||||
prefix="hermes-tui-active-session-", suffix=".json"
|
||||
)
|
||||
os.close(active_session_fd)
|
||||
env["HERMES_TUI_ACTIVE_SESSION_FILE"] = active_session_file
|
||||
env["HERMES_PYTHON_SRC_ROOT"] = os.environ.get(
|
||||
"HERMES_PYTHON_SRC_ROOT", str(PROJECT_ROOT)
|
||||
)
|
||||
@@ -1065,13 +1089,20 @@ def _launch_tui(
|
||||
env["HERMES_TUI_RESUME"] = resume_session_id
|
||||
|
||||
argv, cwd = _make_tui_argv(tui_dir, tui_dev)
|
||||
code: Optional[int] = None
|
||||
try:
|
||||
code = subprocess.call(argv, cwd=str(cwd), env=env)
|
||||
except KeyboardInterrupt:
|
||||
code = 130
|
||||
try:
|
||||
code = subprocess.call(argv, cwd=str(cwd), env=env)
|
||||
except KeyboardInterrupt:
|
||||
code = 130
|
||||
|
||||
if code in (0, 130):
|
||||
_print_tui_exit_summary(resume_session_id)
|
||||
if code in (0, 130):
|
||||
_print_tui_exit_summary(resume_session_id, active_session_file)
|
||||
finally:
|
||||
try:
|
||||
os.unlink(active_session_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
sys.exit(code)
|
||||
|
||||
|
||||
@@ -1481,16 +1481,32 @@ class SessionDB:
|
||||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""List sessions, optionally filtered by source."""
|
||||
"""List sessions, optionally filtered by source.
|
||||
|
||||
Returns rows enriched with a computed ``last_active`` column (latest
|
||||
message timestamp for the session, falling back to ``started_at``),
|
||||
ordered by most-recently-used first.
|
||||
"""
|
||||
select_with_last_active = (
|
||||
"SELECT s.*, COALESCE(m.last_active, s.started_at) AS last_active "
|
||||
"FROM sessions s "
|
||||
"LEFT JOIN ("
|
||||
"SELECT session_id, MAX(timestamp) AS last_active "
|
||||
"FROM messages GROUP BY session_id"
|
||||
") m ON m.session_id = s.id "
|
||||
)
|
||||
with self._lock:
|
||||
if source:
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM sessions WHERE source = ? ORDER BY started_at DESC LIMIT ? OFFSET ?",
|
||||
f"{select_with_last_active}"
|
||||
"WHERE s.source = ? "
|
||||
"ORDER BY last_active DESC, s.started_at DESC, s.id DESC LIMIT ? OFFSET ?",
|
||||
(source, limit, offset),
|
||||
)
|
||||
else:
|
||||
cursor = self._conn.execute(
|
||||
"SELECT * FROM sessions ORDER BY started_at DESC LIMIT ? OFFSET ?",
|
||||
f"{select_with_last_active}"
|
||||
"ORDER BY last_active DESC, s.started_at DESC, s.id DESC LIMIT ? OFFSET ?",
|
||||
(limit, offset),
|
||||
)
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
157
tests/hermes_cli/test_resolve_last_session.py
Normal file
157
tests/hermes_cli/test_resolve_last_session.py
Normal file
@@ -0,0 +1,157 @@
|
||||
"""Verify `hermes -c` picks the session the user most recently used."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from hermes_cli.main import _resolve_last_session
|
||||
|
||||
|
||||
class _FakeDB:
|
||||
def __init__(self, rows):
|
||||
self._rows = rows
|
||||
self.closed = False
|
||||
|
||||
def search_sessions(self, source=None, limit=20, **_kw):
|
||||
rows = [r for r in self._rows if r.get("source") == source] if source else list(self._rows)
|
||||
rows.sort(
|
||||
key=lambda r: float(r.get("last_active") or r.get("started_at") or 0),
|
||||
reverse=True,
|
||||
)
|
||||
return rows[:limit]
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
|
||||
def test_resolve_last_session_prefers_last_active_over_started_at(monkeypatch):
|
||||
# `search_sessions` should return in MRU order, so -c can trust row 0.
|
||||
rows = [
|
||||
{
|
||||
"id": "new_started_old_active",
|
||||
"source": "cli",
|
||||
"started_at": 1000.0,
|
||||
"last_active": 100.0,
|
||||
},
|
||||
{
|
||||
"id": "old_started_recently_active",
|
||||
"source": "cli",
|
||||
"started_at": 500.0,
|
||||
"last_active": 999.0,
|
||||
},
|
||||
]
|
||||
|
||||
fake_db = _FakeDB(rows)
|
||||
monkeypatch.setattr("hermes_state.SessionDB", lambda: fake_db)
|
||||
|
||||
assert _resolve_last_session("cli") == "old_started_recently_active"
|
||||
assert fake_db.closed
|
||||
|
||||
|
||||
def test_search_sessions_exposes_last_active_column(tmp_path, monkeypatch):
|
||||
# End-to-end: SessionDB must surface last_active and order by MRU.
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
||||
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
||||
|
||||
import hermes_state
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
db = hermes_state.SessionDB(db_path=Path(tmp_path / "state.db"))
|
||||
try:
|
||||
db.create_session("s_started_later", source="cli")
|
||||
db.create_session("s_active_later", source="cli")
|
||||
# Force started_at ordering so the test is deterministic regardless
|
||||
# of how quickly the two inserts land.
|
||||
with db._lock:
|
||||
db._conn.execute("UPDATE sessions SET started_at=? WHERE id=?", (2000.0, "s_started_later"))
|
||||
db._conn.execute("UPDATE sessions SET started_at=? WHERE id=?", (1000.0, "s_active_later"))
|
||||
db._conn.commit()
|
||||
|
||||
db.append_message("s_active_later", role="user", content="hi")
|
||||
with db._lock:
|
||||
db._conn.execute(
|
||||
"UPDATE messages SET timestamp=? WHERE session_id=?",
|
||||
(3000.0, "s_active_later"),
|
||||
)
|
||||
db._conn.commit()
|
||||
|
||||
rows = db.search_sessions(source="cli", limit=5)
|
||||
ids = {r["id"]: r.get("last_active") for r in rows}
|
||||
|
||||
assert ids["s_started_later"] == 2000.0
|
||||
assert ids["s_active_later"] == 3000.0
|
||||
assert rows[0]["id"] == "s_active_later"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_resolve_last_session_returns_none_when_empty(monkeypatch):
|
||||
monkeypatch.setattr("hermes_state.SessionDB", lambda: _FakeDB([]))
|
||||
assert _resolve_last_session("cli") is None
|
||||
|
||||
|
||||
def test_resolve_last_session_closes_db_on_search_error(monkeypatch):
|
||||
class _FailingDB:
|
||||
def __init__(self):
|
||||
self.closed = False
|
||||
|
||||
def search_sessions(self, source=None, limit=20, **_kw):
|
||||
raise RuntimeError("boom")
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
db = _FailingDB()
|
||||
monkeypatch.setattr("hermes_state.SessionDB", lambda: db)
|
||||
|
||||
assert _resolve_last_session("cli") is None
|
||||
assert db.closed is True
|
||||
|
||||
|
||||
def test_resolve_last_session_falls_back_to_started_at(monkeypatch):
|
||||
# When last_active is missing entirely (legacy row), fall back to
|
||||
# started_at so the helper still picks the newest session.
|
||||
rows = [
|
||||
{"id": "older", "source": "cli", "started_at": 10.0},
|
||||
{"id": "newer", "source": "cli", "started_at": 20.0},
|
||||
]
|
||||
monkeypatch.setattr("hermes_state.SessionDB", lambda: _FakeDB(rows))
|
||||
assert _resolve_last_session("cli") == "newer"
|
||||
|
||||
|
||||
def test_resolve_last_session_not_limited_to_newest_started_20(tmp_path, monkeypatch):
|
||||
# Regression: when sampling by started_at, -c could miss the true MRU if
|
||||
# it was older than the newest 20 started sessions.
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
||||
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
||||
|
||||
import hermes_state
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
state_db = Path(tmp_path / "state.db")
|
||||
real_session_db = hermes_state.SessionDB
|
||||
db = real_session_db(db_path=state_db)
|
||||
try:
|
||||
for i in range(25):
|
||||
sid = f"s_{i:02d}"
|
||||
db.create_session(sid, source="cli")
|
||||
with db._lock:
|
||||
db._conn.execute(
|
||||
"UPDATE sessions SET started_at=? WHERE id=?",
|
||||
(10_000.0 - i, sid),
|
||||
)
|
||||
db._conn.commit()
|
||||
|
||||
target = "s_24"
|
||||
db.append_message(target, role="user", content="latest activity")
|
||||
with db._lock:
|
||||
db._conn.execute(
|
||||
"UPDATE messages SET timestamp=? WHERE session_id=?",
|
||||
(20_000.0, target),
|
||||
)
|
||||
db._conn.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
monkeypatch.setattr("hermes_state.SessionDB", lambda: real_session_db(db_path=state_db))
|
||||
assert _resolve_last_session("cli") == target
|
||||
@@ -84,7 +84,9 @@ def test_cmd_chat_tui_resume_resolves_title_before_launch(monkeypatch, main_mod)
|
||||
captured["resume"] = resume_session_id
|
||||
raise SystemExit(0)
|
||||
|
||||
monkeypatch.setattr(main_mod, "_resolve_session_by_name_or_id", lambda val: "20260409_000000_aa11bb")
|
||||
monkeypatch.setattr(
|
||||
main_mod, "_resolve_session_by_name_or_id", lambda val: "20260409_000000_aa11bb"
|
||||
)
|
||||
monkeypatch.setattr(main_mod, "_launch_tui", fake_launch)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
@@ -124,6 +126,7 @@ def test_cmd_chat_tui_passes_model_and_provider(monkeypatch, main_mod):
|
||||
|
||||
def test_launch_tui_exports_model_and_provider(monkeypatch, main_mod):
|
||||
captured = {}
|
||||
active_path_during_call = None
|
||||
|
||||
monkeypatch.setattr(
|
||||
main_mod,
|
||||
@@ -132,7 +135,10 @@ def test_launch_tui_exports_model_and_provider(monkeypatch, main_mod):
|
||||
)
|
||||
|
||||
def fake_call(argv, cwd=None, env=None):
|
||||
nonlocal active_path_during_call
|
||||
captured.update({"argv": argv, "cwd": cwd, "env": env})
|
||||
active_path_during_call = Path(env["HERMES_TUI_ACTIVE_SESSION_FILE"])
|
||||
assert active_path_during_call.exists()
|
||||
return 1
|
||||
|
||||
monkeypatch.setattr(main_mod.subprocess, "call", fake_call)
|
||||
@@ -145,6 +151,11 @@ def test_launch_tui_exports_model_and_provider(monkeypatch, main_mod):
|
||||
assert env["HERMES_INFERENCE_MODEL"] == "nous/hermes-test"
|
||||
assert env["HERMES_TUI_PROVIDER"] == "nous"
|
||||
assert env["HERMES_INFERENCE_PROVIDER"] == "nous"
|
||||
active_path = Path(env["HERMES_TUI_ACTIVE_SESSION_FILE"])
|
||||
assert active_path.name.startswith("hermes-tui-active-session-")
|
||||
assert active_path.suffix == ".json"
|
||||
assert active_path_during_call == active_path
|
||||
assert not active_path.exists()
|
||||
assert env["NODE_ENV"] == "production"
|
||||
|
||||
|
||||
@@ -169,7 +180,9 @@ def test_print_tui_exit_summary_includes_resume_and_token_totals(monkeypatch, ca
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
monkeypatch.setitem(sys.modules, "hermes_state", types.SimpleNamespace(SessionDB=lambda: _FakeDB()))
|
||||
monkeypatch.setitem(
|
||||
sys.modules, "hermes_state", types.SimpleNamespace(SessionDB=lambda: _FakeDB())
|
||||
)
|
||||
|
||||
main_mod._print_tui_exit_summary("20260409_000001_abc123")
|
||||
out = capsys.readouterr().out
|
||||
@@ -178,3 +191,42 @@ def test_print_tui_exit_summary_includes_resume_and_token_totals(monkeypatch, ca
|
||||
assert "hermes --tui --resume 20260409_000001_abc123" in out
|
||||
assert 'hermes --tui -c "demo title"' in out
|
||||
assert "Tokens: 21 (in 10, out 6, cache 4, reasoning 1)" in out
|
||||
|
||||
|
||||
def test_print_tui_exit_summary_prefers_actual_active_session_file(
|
||||
monkeypatch, capsys, tmp_path
|
||||
):
|
||||
import hermes_cli.main as main_mod
|
||||
|
||||
seen = []
|
||||
|
||||
class _FakeDB:
|
||||
def get_session(self, session_id):
|
||||
seen.append(session_id)
|
||||
return {
|
||||
"message_count": 1,
|
||||
"input_tokens": 0,
|
||||
"output_tokens": 0,
|
||||
"cache_read_tokens": 0,
|
||||
"cache_write_tokens": 0,
|
||||
"reasoning_tokens": 0,
|
||||
}
|
||||
|
||||
def get_session_title(self, _session_id):
|
||||
return "actual"
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
active = tmp_path / "active.json"
|
||||
active.write_text('{"session_id":"actual_session"}', encoding="utf-8")
|
||||
monkeypatch.setitem(
|
||||
sys.modules, "hermes_state", types.SimpleNamespace(SessionDB=lambda: _FakeDB())
|
||||
)
|
||||
|
||||
main_mod._print_tui_exit_summary("startup_resume", str(active))
|
||||
out = capsys.readouterr().out
|
||||
|
||||
assert seen == ["actual_session"]
|
||||
assert "hermes --tui --resume actual_session" in out
|
||||
assert "startup_resume" not in out
|
||||
|
||||
27
ui-tui/src/__tests__/useSessionLifecycle.test.ts
Normal file
27
ui-tui/src/__tests__/useSessionLifecycle.test.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
|
||||
import { writeActiveSessionFile } from '../app/useSessionLifecycle.js'
|
||||
|
||||
describe('writeActiveSessionFile', () => {
|
||||
let dir = ''
|
||||
|
||||
afterEach(() => {
|
||||
if (dir) {
|
||||
rmSync(dir, { force: true, recursive: true })
|
||||
dir = ''
|
||||
}
|
||||
})
|
||||
|
||||
it('writes the actual resumed session id for the shell exit summary', () => {
|
||||
dir = mkdtempSync(join(tmpdir(), 'hermes-tui-active-'))
|
||||
const path = join(dir, 'active.json')
|
||||
|
||||
writeActiveSessionFile('actual_session', path)
|
||||
|
||||
expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual({ session_id: 'actual_session' })
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,5 @@
|
||||
import { writeFileSync } from 'node:fs'
|
||||
|
||||
import type { ScrollBoxHandle } from '@hermes/ink'
|
||||
import { evictInkCaches } from '@hermes/ink'
|
||||
import { type RefObject, useCallback } from 'react'
|
||||
@@ -23,6 +25,18 @@ import { getUiState, patchUiState } from './uiStore.js'
|
||||
|
||||
const usageFrom = (info: null | SessionInfo): Usage => (info?.usage ? { ...ZERO, ...info.usage } : ZERO)
|
||||
|
||||
export const writeActiveSessionFile = (sessionId: null | string, file = process.env.HERMES_TUI_ACTIVE_SESSION_FILE) => {
|
||||
if (!file || !sessionId) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
writeFileSync(file, JSON.stringify({ session_id: sessionId }), { mode: 0o600 })
|
||||
} catch {
|
||||
// Best-effort shell epilogue hint only; never break live session changes.
|
||||
}
|
||||
}
|
||||
|
||||
const trimTail = (items: Msg[]) => {
|
||||
const q = [...items]
|
||||
|
||||
@@ -131,6 +145,7 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
|
||||
resetSession()
|
||||
setSessionStartedAt(Date.now())
|
||||
|
||||
writeActiveSessionFile(r.session_id)
|
||||
patchUiState({
|
||||
info,
|
||||
sid: r.session_id,
|
||||
@@ -188,6 +203,7 @@ export function useSessionLifecycle(opts: UseSessionLifecycleOptions) {
|
||||
const resumed = toTranscriptMessages(r.messages)
|
||||
|
||||
setHistoryItems(r.info ? [introMsg(r.info), ...resumed] : resumed)
|
||||
writeActiveSessionFile(r.resumed ?? r.session_id)
|
||||
patchUiState({
|
||||
info: r.info ?? null,
|
||||
sid: r.session_id,
|
||||
|
||||
Reference in New Issue
Block a user