Skip to content

fix: call sessions end endpoint from session-stop hook#47

Merged
Alan-TheGentleman merged 1 commit intoGentleman-Programming:mainfrom
jmeiracorbal:fix/session-stop-guarantee
Mar 8, 2026
Merged

fix: call sessions end endpoint from session-stop hook#47
Alan-TheGentleman merged 1 commit intoGentleman-Programming:mainfrom
jmeiracorbal:fix/session-stop-guarantee

Conversation

@jmeiracorbal
Copy link

Summary

  • Implement session-stop.sh to call POST /sessions/{id}/end when Claude Code stops, so ended_at is always set regardless of agent cooperation

Motivation

Closes #46

session-stop.sh was a placeholder that did nothing. The server already exposed POST /sessions/{id}/end, and the Stop hook already received session_id via stdin, but the connection was never made. As a result, every session in the database has ended_at = NULL and sessions grow unbounded with no duration data.

The fix mirrors the pattern already used in session-start.sh: read session_id from stdin, call the HTTP API.

Changes

  • plugin/claude-code/scripts/session-stop.sh — replaced placeholder with a curl call to POST /sessions/{id}/end. Reads session_id from stdin (same as session-start.sh), exits cleanly if no session_id is present. Runs async (already configured in hooks.json) so it never blocks Claude's response.

Test plan

  • go test ./... passes
  • Manually tested end-to-end:
    1. Created a session via POST /sessions
    2. Confirmed ended_at = NULL in the database
    3. Simulated the hook: echo '{"session_id":"test","cwd":"/tmp"}' | bash session-stop.sh
    4. Confirmed ended_at was set correctly in the database

Copy link
Collaborator

@Alan-TheGentleman Alan-TheGentleman left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@Alan-TheGentleman Alan-TheGentleman left a comment

Choose a reason for hiding this comment

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

Fix looks good — clean, mirrors session-start.sh, does exactly what it should.

One thing worth noting: if the server happens to be down when the session ends, this fails silently (which is fine for a hook). But the agent also can't call mem_session_end as a fallback because that tool isn't in the ToolSearch select list from #43. Not a blocker for this PR — just something to address in #43.

Also flagging that this only covers Claude Code. OpenCode, Gemini, and Codex don't have session-end hooks so their sessions still get ended_at = NULL. We should open a follow-up for that — the MCP tool exists, agents just need to be told to call it.

Approve from my side, good to merge.

@Alan-TheGentleman Alan-TheGentleman merged commit daedfff into Gentleman-Programming:main Mar 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: sessions never get ended_at set — session-stop.sh is an empty placeholder

2 participants