Skip to content

Fire the Hermes session hook on on_session_finalize, not per turn - #347

Merged
eugeniughelbur merged 3 commits into
eugeniughelbur:mainfrom
drakeo338:claude/345-fix
Oct 24, 2026
Merged

eugeniughelbur merged 3 commits into
eugeniughelbur:mainfrom
drakeo338:claude/345-fix

Conversation

@drakeo338

Copy link
Copy Markdown
Contributor

What this PR does

Fixes #345. Hermes fires on_session_end after every turn, not once per session, and the hermes -z consolidation the hook starts fires it again. An armed hook would start a consolidation per message, each starting another. The hook, example config and Hermes adapter now use on_session_finalize. The script also exits when OBSIDIAN_HERMES_CONSOLIDATING is set (exported to the child) and takes a per-user lock so consolidations never overlap.

Type of change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 📜 New slash command
  • 📚 Documentation update
  • 🧹 Refactor / cleanup
  • ⚠️ Breaking change (fix or feature that changes existing behavior)

Related issues

Closes #345

How I tested it

New tests/test_hermes_session_hook.py and updated tests/test_smoke.py: 61 pass on the committed HEAD, against 57 pass and 6 fail with the new tests on the base commit. 2 other failures remain, outside the Hermes hook tests.

AI-first rule compliance (required for any vault-writing command)

Unticked: no vault-writing command changed.

  • My changes follow references/ai-first-rules.md
  • If I added or changed a command that writes to the vault, the produced note has a ## For future Claude preamble
  • Frontmatter includes ai-first: true, type:, date:, tags:
  • Wikilinks ([[...]]) used for every person, project, idea, decision referenced
  • External claims have recency markers ((as of YYYY-MM, source.com))
  • Sources preserved verbatim (URLs inline, not paraphrased)

Checklist

  • I searched existing issues and PRs before opening this one
  • My commit messages are descriptive (focus on WHY, not just WHAT)
  • I updated the README or docs if user-facing behavior changed
  • I updated CHANGELOG.md (under "Unreleased") if applicable
  • If I added a new command, I added an entry in SKILL.md and the README's command table

Screenshots / output (if visual or producing notes)

n/a


…iughelbur#345)

Hermes fires on_session_end after every turn, and the hermes -z run
the hook starts fires it again, so each turn started a consolidation
and each consolidation started another. Register the hook on
on_session_finalize instead.

The script now also refuses to re-enter: the consolidation child gets
OBSIDIAN_HERMES_CONSOLIDATING=1, and a per-user mode-0600 lock file
allows one consolidation at a time (a lock whose PID is gone is
replaced). The background run no longer inherits the hook's stdout, so
the hook returns {} at once instead of when the consolidation ends.
@eugeniughelbur eugeniughelbur added bug Something isn't working area: adapters Platform builds and the adapter layer labels Oct 20, 2026 — with Cursor

@eugeniughelbur eugeniughelbur left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drakeo338 thanks. Moving the hook to on_session_finalize is the right fix, and the tests check it. One thing before merge.

The loop guard has 2 parts, and #345 says part 1 may not work. @konsorsiumai found the hook runs inside the gateway (payload cwd is ~/.hermes). Then the OBSIDIAN_HERMES_CONSOLIDATING=1 marker on the hermes -z child never reaches the hook. The lock is what remains, and it can miss this order:

  1. The hook starts a consolidation and takes the lock.
  2. The consolidation's hermes -z exits, and the lock is released.
  3. Hermes finalizes that child session and fires the hook.
  4. The hook finds a free lock and starts the next consolidation.

I can't run a real Hermes gateway, so I'm not claiming step 3 lands after step 2. This PR has no guard for it if it does.

A minimum interval, the issue's second suggestion, holds in any order:

  • A per-user stamp file next to the lock, mode 0600, with the start time of the last consolidation.
  • The hook exits with {} when that start is newer than OBSIDIAN_HERMES_MIN_INTERVAL seconds, default 6 hours.
  • A test: two hook runs back to back, marker unset and lock free, start 1 consolidation, not 2.

@konsorsiumai, you have the gateway and the probe script. Once this is in, could you run the probe on on_session_finalize? It shows where the child's event lands against the lock.

Last, the PR body says "2 other failures remain, outside the Hermes hook tests." Which 2 tests, and do they fail on main too?

If the hook runs inside the Hermes gateway, the
OBSIDIAN_HERMES_CONSOLIDATING marker on the hermes -z child never
reaches it, and the lock alone misses a child whose finalize event
lands after its lock is released: the hook then finds a free lock and
starts the next consolidation.

The hook now keeps the start time of the last consolidation in a
per-user mode-0600 stamp file next to the lock, and exits with {}
while that start is newer than OBSIDIAN_HERMES_MIN_INTERVAL seconds
(default 21600, 6 hours; 0 turns it off). The check runs under the
lock, and the stamp is written through a temp file and a rename so a
symlink at its path is replaced, not followed.
@drakeo338

Copy link
Copy Markdown
Contributor Author

Pushed f4fcc33: the Hermes session hook now skips a consolidation while a per-user 0600 stamp is newer than OBSIDIAN_HERMES_MIN_INTERVAL seconds (default 6 h, 0 = off), with a back-to-back test, and main is merged in.

@eugeniughelbur
eugeniughelbur merged commit 8fd3a71 into eugeniughelbur:main Oct 24, 2026
2 checks passed
@eugeniughelbur

Copy link
Copy Markdown
Owner

Merged through #364, together with 4 other PRs. The new interval test fails on your previous commit and passes now. @konsorsiumai, a probe run with a real gateway on on_session_finalize would still be welcome, since none of us could test that part.

@konsorsiumai

Copy link
Copy Markdown

Tested on_session_finalize with a logging probe (no vault touched, the real script was not used). The hook only wrote the event name and session id to a file.

Hermes 5fc490e2b341, Linux, one machine.

Run on_session_end on_session_finalize
hermes -z, turn 1 1 event none
hermes chat -q (one query, then exit) 1 event 1 event, at exit
hermes -z --resume, turn 2 of the same session 1 more event (2 for that session) none

What this shows:

Not tested: a real gateway (Telegram etc.). There on_session_finalize should fire on session reset or expiry, but that needs a real incoming message and I could not send one. I only ran the CLI. So the gateway path is still unverified.

wilsonwu-ai pushed a commit to wilsonwu-ai/obsidian-second-brain that referenced this pull request Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: adapters Platform builds and the adapter layer bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hermes hook runs after every turn, and a hermes -z started from it fires it again (on_session_end is turn-scoped)

3 participants