Skip to content

feat: persistent Claude sessions, /new, /model, /effort - #12

Merged
emanuilo merged 5 commits into
mainfrom
feat/claude-persistent-sessions
Oct 18, 2026
Merged

emanuilo merged 5 commits into
mainfrom
feat/claude-persistent-sessions

Conversation

@emanuilo

@emanuilo emanuilo commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Summary

Each chat now keeps one Claude Code session, reused across messages and resumed after a restart, instead of a fresh CLI per message with a rolling history window.

 handle(message, chat)
-  system prompt = AGENTS.md + MEMORY.md + time + memories + last N messages   # changes every turn, never cached
-  spawn Claude CLI (loads ~/.claude settings, plugins, MCP servers)
-  query → close CLI
-  consolidate before replying
+  context = <context> time, relevant memories, delivered reminders,
+            AGENTS.md / MEMORY.md if changed since the session saw them </context>
+  client = live client for chat
+        ?? connect(resume = sessions.json[chat].session_id)   # fingerprint mismatch / failed resume → fresh session
+  client.query(context + message)                            # system prompt static per session → prompt-cached
+  sessions.json[chat] = session_id + hashes of AGENTS.md / MEMORY.md seen
+  consolidate in the background, after the reply

Locked-down CLI: setting_sources=[], strict_mcp_config, tools=[] (Memclaw tools only), permission_mode="dontAsk" (was bypassPermissions). claude-agent-sdk → 0.2.164.

Telegram:

+ /new                    fresh conversation (also a "/new" message in Slack, WhatsApp, terminal)
+ /model, /effort         inline button menus; saved to ~/.memclaw/.env, conversation kept
+ "Working… step N: …"    silent status message following tool calls, deleted when the reply arrives

Code structure:

ClaudeAgentBackend
  _options(model, effort, …)   # the lockdown above, in one place, for sessions and one-shots
  _session_options(…)          # + cwd, MCP server, allowed tools, resume
  run_turn → _turn_cost(…)     # running total → this turn's cost, or a token estimate
MemclawAgent.handle            # read/hash files → context → run_turn → _log_turn,
                               #   _ack_notes, _save_session

Defaults: claude-sonnet-5-5 at medium. CONVERSATION_HISTORY_* config removed. Per-turn cost is the turn's, not the session's running total.

Evidence

  • Before: pytest → 252 passed
    After: pytest → 317 passed, ruff clean (also clears 11 findings that were already on main). New coverage includes:
    first turn starts a session, saved to sessions.json
    next turn and a restart resume that session
    fingerprint change → old session not resumed
    AGENTS.md / MEMORY.md change → sent once in <context>; own writes not echoed
    /new drops the session; a turn running during /new doesn't re-save it,
      and a message sent while /new is still disconnecting starts fresh
    /model refuses to switch when the model list can't be loaded
    /model, /effort menus and button callbacks
    
  • Latency (live, subscription): follow-up turns ~1.6–3 s, vs a cold CLI start of ~4–8 s per message before.
  • Deployed to the Mac mini (Telegram). Restart resume and /new were verified end to end there.

Merge Danger

Door: two-way

Revert restores per-message CLIs. The new sessions.json is ignored by the old code, and memories are untouched.

Blast Radius: conversations

  • On first run, every chat starts a fresh conversation, since there's no saved session yet.
  • The CLI no longer loads the user's ~/.claude settings, plugins or MCP servers. That's intended, but anyone relying on them inside Memclaw loses them.
  • Cursor keeps an interim in-memory transcript here; native Cursor conversations land in feat: native Cursor conversations, /model and /effort for Cursor #13.

🤖 Generated with Claude Code

- Reuse one Claude CLI client per chat instead of spawning one per turn
- Resume conversations after restart (sessions.json), fresh session on failure
- Replace the rolling history window with native sessions; /new resets
- Static system prompt; time and memory context move into the user message
- Load no ~/.claude settings/plugins/MCP servers, no built-in tools, dontAsk
- Telegram /model and /effort, persisted to ~/.memclaw/.env
- Background consolidation after the reply
- Default to claude-sonnet-5-5 at medium effort; bump claude-agent-sdk
- /new forgets the session before awaiting the backend reset, so a
  message sent right after it can't resume the old session
- /model refuses to switch when the model list can't be loaded, instead
  of saving an unchecked (possibly mistyped) id that breaks every turn
- the fallback cost estimate includes cache-write tokens
- long-id model buttons carry a hash of the id instead of a list
  position, so they can't pick a different model if the list changed
- one builder for the locked-down CLI options, shared by chat sessions and
  one-shots, so the lockdown can't drift between them
- turn cost computed in _turn_cost, out of run_turn's connection handling
- agent.handle's logging, pending-note acknowledgement and session saving
  pulled into named helpers
- clear the repo's ruff findings (unused imports, ambiguous names)
@emanuilo
emanuilo merged commit f77e012 into main Oct 18, 2026
4 checks passed
@emanuilo
emanuilo deleted the feat/claude-persistent-sessions branch October 18, 2026 17:21
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