# Slash commands and context compaction

## Slash commands

When input starts with `/` and has no space, the menu opens upward from the `/` as the same glass popover as the `@` menu, one row per command with an icon, its arguments and description; a built-in has its own icon, a skill, plugin or user command its source's; a space closes it at once.

Enter runs a registered first word, otherwise sends chat. A menu that is closing does not intercept Enter. Skills, plugins and user commands come from `/api/commands`; built-ins win dispatch.

| Command | Selection or Enter |
| --- | --- |
| `/compact [keep_recent_tokens]` | Compacts at once |
| `/rewind` | Web: opens a picker of earlier messages and takes no arguments. The default TUI has no such command. Rich fallback only: lists points; `/rewind N` previews one; `/rewind N confirm <plan_hash> <idempotency_key>`, as printed by the preview, rewinds |
| `/branch [name]` | Runs at once |
| `/context` | Opens the ring panel |
| `/clear`, `/new` | New conversation |
| `/task`, `/skill`, `/merge` | Fill the prefix and wait for required arguments |
| `/help` | Sets input to `/`, showing the menu again; no separate page |
| `/sandbox` | Toggles the system sandbox |
| `/memory` | Opens the memory page |
| `/doctor` | Runs health checks and shows the report locally, never to the model |

Optional arguments (only `[…]`) run at once. Without a session, session commands keep the input and warn "Start a conversation first".

A command the server runs (`type: "local"` in `/api/commands`: `/goal`, `/rename` …) leaves as the composer's own message, through `sendChatMessage`:

- From a New chat draft it carries the draft's `local_*` id, permission mode, Agent, project and folders: it runs in the draft, and the tab becomes that conversation.
- It draws no bubble of its own. One that goes on as a turn (`/goal <objective>`) gets its message from `chat_ack`, before the reply starts; one that only answers (`/goal status`, `/goal pause`) leaves just its reply line.
- During a run or a waiting request it goes at once beside the turn, with no bubble.

**Known small issues**

- A registry item also named compact shows twice in the menu; dispatch still picks the built-in.
- Jump to latest may overlap the menu position.

## What compaction sends

One rolling summary. Further compaction combines the old summary and more turns into a new one.

```
[System prompt] [Current summary] [Recent uncompacted turns] [New user message]
```

## Triggers

| Trigger | Condition | Path |
| --- | --- | --- |
| Manual `/compact` | From the composer, optionally keeping the latest N tokens. The selected branch's foreground operation (`op_kind: compact`): every client shows "Compacting context…" in the run indicator with Stop, a reload too, and Stop sends `operation.cancel` with its `op_id`. Refused, naming what runs, while that branch is busy; other branches keep running. The ending frame goes out after the slot is free, and its release sends `running_task_clear`. The terminal UI shows the progress and the result line. | `action: compact`, `handle_compact` |
| Automatic, before a turn | Budget at or above `context.auto_compact_percent` (default 80). Compact the chain the turn runs on, on any branch; the head stays. Skipped for a background turn. | `loop_runner.py` |
| Automatic, within a turn | A round of tool results passes the same threshold: once per turn (`mid_turn`), then the turn goes on. A branch moving to a model with a smaller window is compacted before its next turn by the larger model (`model_downshift`) | `loop_runner.py` |
| Overflow recovery | Model returns 413 or `prompt_too_long`: inside the agent loop, compact and send the same request again; once per request, twice per turn, and the turn never reruns from its start | `reactive.py` |

All of them use `compaction_started`, `compaction_finished` and `compaction_failed`. The start shows in the run indicator. A `/compact` adds a system message when it ends; a stopped one reads “Compaction stopped; context unchanged.” A turn's own compaction that fails says nothing: the history is as it was and a later request tries again.

## Steps

1. **Read** rendered history from SessionDB: the summary and remaining turns. Text already covered is not reread.
2. **Threshold.** Fewer than four messages: stop; the manual path explains. Already summary plus tail with no valid cut: nothing to compact, no model call.
3. **Summarize** with a separate model call. Default tail: 10% of the window, clamped to 8k–40k tokens, at least four non-empty messages. `/compact 2000` overrides.
4. **Persist** a `summary_*` node (`role=llm`, `name=context/summary`, sent as a user message that says it is a summary); `covers_ids` marks the range. Old turns stay and HEAD does not move.
5. **Graph.** The DAG shows a capsule. A chain has one summary in effect, the one covering furthest; an older one is superseded and stops rendering ([Compaction](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/compaction.html)).

## Ring

Lower right: occupancy over window size, from the compacted view. Green below 50%, yellow 50–80%, red at 80% and above. Measurement, running state, nothing-to-compact and Compact now: [occupancy and compaction](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/occupancy-status.html).

**Key files**

| Responsibility | Path |
| --- | --- |
| Registry, menu | `apps/web/components/chat/composer/slash/slash-commands.ts`, `use-slash-menu.ts` |
| Selection | `apps/web/components/chat/composer/index.tsx` (`selectSlashCommand`) |
| Keyboard | `composer/input/use-composer-keydown.ts` |
| Enter | `composer/submit/use-chat-submit.ts` |
| Manual over WS | `openprogram/interfaces/server/ws_actions/chat/commands.py` |
| Pipeline | `openprogram/runtime/context/engine.py`, `compaction/summarize.py` |
| Automatic | `openprogram/runtime/conversation/turn_runtime/loop_runner.py` |
| Overflow | `openprogram/runtime/context/compaction/reactive.py` |
| Frontend events | `apps/web/lib/runtime-bridge/session/chat-handlers.ts` |
| Ring, panel | `apps/web/components/chat/badges/context-badge.tsx`, `context-breakdown-panel.tsx` |
