cozempic icon indicating copy to clipboard operation
cozempic copied to clipboard

Guard daemon: add --session flag for explicit session targeting

Open aitonydev opened this issue 9 months ago • 1 comments

Problem

When multiple Claude Code instances share overlapping CWD paths, find_current_session() cannot reliably identify the correct session. The guard daemon then monitors the wrong session, making its threshold checks useless.

Reproduction

  1. Run 5+ Claude Code sessions across overlapping directories (e.g., ~/, ~/business-os-cloud/, ~/businesses/repositories/dharma-yatra/)
  2. Start cozempic guard --daemon from ~/
  3. The guard picks up a dharma-yatra session (0.3MB) instead of the actual home-dir session (1.5MB at 140K tokens)

Root cause

find_current_session() uses PID → CWD → recency fallback, but with multiple Claude processes, the CWD matching is ambiguous. The PID approach requires knowing which Claude process belongs to the current terminal, which isn't available in a daemon context.

Suggested fix

Add --session <session_id> flag to cozempic guard:

cozempic guard --daemon --session 0dbe9cca-5d17-4e34-a882-62c41b2c3cf6 --threshold-tokens 160000

This bypasses find_current_session() entirely and directly monitors the specified session path at ~/.claude/projects/<project>/<session_id>.jsonl.

The SessionStart hook could then pass $SESSION_ID (if Claude Code exposes it as an environment variable to hooks) for exact targeting.

Impact

Without explicit session targeting, the guard daemon is unreliable in multi-session environments — which is exactly when it's most needed (heavy users running multiple Claude instances).

Environment

  • cozempic 0.7.2
  • Claude Code 2.1.50
  • macOS Darwin 25.2.0
  • 7 concurrent Claude processes

aitonydev avatar Feb 23 '26 01:02 aitonydev

Fixed in v0.7.4.

Changes:

  • Added --session <session_id> flag to cozempic guard — bypasses find_current_session() entirely
  • SessionStart hook now extracts session_id from hook input and passes it to the guard daemon automatically
  • Guard prints a tip to use --session when auto-detection fails

After upgrading, run cozempic init to update the SessionStart hook:

pip install --upgrade cozempic
cozempic init

The guard daemon will now receive the exact session ID from Claude Code on startup — no more wrong-session issues in multi-instance environments.

For manual use:

cozempic guard --daemon --session 0dbe9cca-5d17-4e34-a882-62c41b2c3cf6 --threshold-tokens 160000

junaidtitan avatar Feb 24 '26 07:02 junaidtitan