Skip to content

feat(cli): persist Claude session mappings across restarts#51

Open
2yclaw wants to merge 1 commit intofastclaw-ai:mainfrom
2yclaw:feat/persist-cli-sessions
Open

feat(cli): persist Claude session mappings across restarts#51
2yclaw wants to merge 1 commit intofastclaw-ai:mainfrom
2yclaw:feat/persist-cli-sessions

Conversation

@2yclaw
Copy link
Copy Markdown

@2yclaw 2yclaw commented Apr 7, 2026

Summary

  • Persist CLI agent conversationID → sessionID mappings to ~/.weclaw/sessions.json
  • On startup, load saved sessions so --resume works across weclaw restarts
  • On session update or reset, write back to disk immediately

Problem

Claude CLI sessions are stored on disk by Claude Code (~/.claude/projects/), but weclaw's in-memory sessions map[string]string is lost on restart. After weclaw restart, users start a new conversation even though the Claude session still exists — losing all context.

Solution

Add loadSessions() / saveSessions() to CLIAgent with these properties:

  • File format: map[agentName]map[conversationID]sessionID — supports multiple CLI agents
  • Write strategy: write-through on every mutation (2 call sites), file is tiny (~100 bytes per session)
  • Atomic write: temp file + os.Rename to prevent corruption on crash
  • Corrupt file handling: refuses to overwrite if existing JSON is malformed
  • Graceful degradation: if persistence fails, logs warning and falls back to current behavior (new session)
  • Stale sessions: Claude handles stale --resume IDs gracefully (auto-creates new session), so no TTL needed

Only affects CLI agents that use --resume (currently Claude). ACP and HTTP agents are unchanged.

Changes

  • agent/cli_agent.go: +86 lines, 1 file changed

Test plan

  • go build ./... — compiles cleanly
  • go vet ./... — no warnings
  • Manual test: send message → verify ~/.weclaw/sessions.json created
  • Manual test: weclaw restart → send message → verify log shows resuming session with same session ID
  • Manual test: weclaw restart → verify loaded N session(s) for claude in log

Claude CLI sessions survive on disk (~/.claude/projects/), but the
conversationID→sessionID mapping in CLIAgent was only held in memory.
After a weclaw restart, users lost their conversation context even though
the Claude session still existed.

This change adds load/save logic for the sessions map:
- On startup: load from ~/.weclaw/sessions.json
- On session update or reset: write back to disk
- Keyed by agent name to support multiple CLI agents
- Atomic write (tmp + rename) to prevent corruption on crash
- Corrupt file detection: refuses to overwrite if JSON is malformed
- Graceful degradation: if persistence fails, falls back to current behavior

Only affects CLI agents that use --resume (currently Claude).
ACP and HTTP agents are unchanged.
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.

1 participant