#18883: feat: add /save command and pre-reset memory flush
size: M
Cluster:
Memory Management Enhancements
# feat: add `/save` command and pre-reset memory flush
Closes #8185
## Summary
When a user triggers `/new` or `/reset`, the session is discarded without giving the agent a chance to save important context. This PR adds:
1. **`/save` command** — Manually trigger a memory flush to disk at any time
2. **Pre-reset memory flush** — Automatically runs a memory save turn before `/new` and `/reset` when `compaction.memoryFlush` is enabled (default: `true`)
## Changes
### New file: `src/auto-reply/reply/commands-save.ts`
- `runMemorySave()` — Reusable memory save function that runs a flush turn via the embedded agent
- `runPreResetMemoryFlush()` — Reset-specific variant with tailored prompts ("Session is being reset, save important context now")
- `handleSaveCommand` — `/save` command handler with auth checks and error handling
### Modified: `src/auto-reply/reply/commands-core.ts`
- Added pre-reset memory flush before the existing reset/hook logic
- Only triggers when `memoryFlush` is configured (respects existing user settings)
- Runs before internal hooks and `before_reset` plugin hooks, so memories are saved while session history is still available
### Modified: `src/tui/commands.ts`
- Registered `/save` in the TUI command list
### New test: `src/auto-reply/reply/commands-save.test.ts`
- 7 tests covering: command routing, auth checks, missing session id, agent-running guard, reset-specific prompts
- All passing
## How it works
### `/save`
```
User: /save
Agent: (runs memory flush turn, writes to memory/YYYY-MM-DD.md)
→ 💾 Memory saved.
```
### Pre-reset flush
```
User: /new
Agent: (pre-reset flush: saves important context to memory files)
→ (session reset proceeds normally)
→ (fresh session greeting)
```
## Design decisions
- **Reuses existing `memoryFlush` infrastructure** — same prompt resolution, date formatting, and config
- **No new config options needed** — piggybacks on `compaction.memoryFlush.enabled` (default: true)
- **Non-blocking on failure** — if the flush fails, the reset still proceeds (logged as verbose warning)
- **Auth-gated** — only authorized senders can trigger `/save`
## Known limitations
- Pre-reset flush currently covers the **channel command path** (Telegram, Discord, WhatsApp, TUI, etc.) but not the **gateway API direct reset path** (`agent.ts`). The gateway path lacks the resolved model/provider context needed to run an agent turn at reset time. This could be addressed in a follow-up.
## Testing
```bash
pnpm exec vitest run src/auto-reply/reply/commands-save.test.ts
# 7 tests passing
```
Type-checked with `tsc --noEmit` — no new errors.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds a `/save` command for manual memory flushing and a pre-reset memory flush that runs automatically before `/new` and `/reset`. The implementation cleanly reuses the existing `memoryFlush` infrastructure (same prompt resolution, date formatting, config gating) and follows established command patterns.
- **`commands-save.ts`**: Well-structured new module with `runMemorySave` (shared core), `runPreResetMemoryFlush` (reset-specific wrapper), and `handleSaveCommand` (command handler). Auth-gated, agent-active guard, non-blocking on failure.
- **`commands-core.ts`**: Pre-reset flush is properly `await`ed, guarded by auth + sessionId + `memoryFlush.enabled` config, and runs before internal hooks/plugin hooks so session history is still available.
- **`commands-save.test.ts`**: 7 tests covering routing, auth, missing session, active-agent guard, and reset-specific prompt verification.
- **`commands.ts`**: `/save` added to TUI autocomplete, but **missing from `helpText()`** — users won't see it in `/help` output.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with one minor discoverability gap in the help text.
- The core logic is sound, follows existing patterns, and has good test coverage. The only issue found is that `/save` is missing from the `helpText()` function, which affects user discoverability but not functionality or safety.
- `src/tui/commands.ts` — `/save` is missing from the `helpText()` function.
<sub>Last reviewed commit: e5592a6</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#8919: Pr/memory flush improvements
by shortbus · 2026-02-04
75.2%
#8900: fix: improve default memory flush prompts with urgency and specifics
by joetomasone · 2026-02-04
74.2%
#14243: fix: fire session-memory hook on auto-resets + topic-aware memory p...
by TheDude135 · 2026-02-11
73.1%
#10644: feat: add runDuringHeartbeats option for memory flush (AI Assisted)
by tripphillips · 2026-02-06
73.0%
#14576: Fix/memory loss bugs
by ENCHIGO · 2026-02-12
72.2%
#22220: feat(bootstrap): cache session's bootstrap files so we don't invali...
by anisoptera · 2026-02-20
72.0%
#7975: feat(commands): add /reset-context command
by sergi039 · 2026-02-03
71.9%
#14018: fix: auto-remind AI to check workspace context files on session reset
by WalterSumbon · 2026-02-11
71.5%
#7091: feat: add pre-answer hooks system for automatic context injection
by dizhaky · 2026-02-02
71.4%
#20713: fix(compaction): trigger memory flush after missed compaction cycles
by zerone0x · 2026-02-19
71.3%