Skip to content
Back to skills

Session Control

ASecurity

Run slash commands in this Claude Code session yourself through its cmux pane. Use to compact at a good moment (a CONTEXT line appears, or the user says "compact"), to move this conversation to another Claude login (an ACCOUNTS line shows switch=<name>, or "switch accounts"), or to turn an MCP server on or off mid-session. Requires cmux.

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 24, 2026
ai-agentspythongoshellbashdebugginggit

Works with

  • claude code
  • mcp

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned November 24, 2026

npx -y skills add alectivism/cross-model-agent-delegation --skill session-control --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Session Control?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Session Control
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/alectivism-session-control/badge)](https://www.skillsdirectory.com/skills/alectivism-session-control)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: session-control
description: Run slash commands in this Claude Code session yourself through its cmux pane. Use to compact at a good moment (a CONTEXT line appears, or the user says "compact"), to move this conversation to another Claude login (an ACCOUNTS line shows switch=<name>, or "switch accounts"), or to turn an MCP server on or off mid-session. Requires cmux.
---

# Session control: compact, switch logins and toggle MCP servers yourself

Claude can't run slash commands. [cmux](https://github.com/manaflow-ai/cmux) can type them into this session's pane once the turn ends. `${CLAUDE_SKILL_DIR}/scripts/session_ctl.py` does that: it waits for an idle prompt, types the command, waits for it to finish, and logs to `~/.claude/state/session-ctl.log`. `cmux send` refuses while the user has unsubmitted text in the prompt or a dialog open; the script waits and retries instead of typing over them. An unreadable pane counts as busy.

Requires cmux (`$CMUX_SURFACE_ID` set). Outside cmux, tell the user the exact commands to type instead.

The script finds this session on its own: the claude process (`$CMUX_CLAUDE_PID`, else the nearest `claude` parent), the session id (from that process's `--resume`/`--session-id`, else the newest transcript for this directory), and the context size (last main-thread turn's input, cache-read and cache-creation tokens in the transcript). `session_ctl.py ctx` prints the context in k tokens.

## Compact at a natural break

Automatic compaction fires when the window is nearly full, which is often mid-task. Add the hook so you get a signal earlier:

```json
"UserPromptSubmit": [{"hooks": [{"type": "command", "command": "python3 ~/.claude/skills/session-control/scripts/ctx-hint.py"}]}]
```

It stays silent below 250k (`CTX_HINT_K`); at 250k or more it adds `CONTEXT <n>k (>=250k): compact at the next natural break` to the turn. When that line is present, decide at the **end** of your turn:

- **Compact** when work just reached a natural break: a deliverable is done or committed, the next request is a new topic, or you are handing back for review.
- **Don't** compact mid-task: mid-debugging, while exact strings, line numbers or tool output are still needed, or while subagents or background jobs will still report back.
- Right after a compaction the context reads low and the hook goes quiet. Don't compact again until it speaks.

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/session_ctl.py --detach compact --focus "<what the summary must keep>"
```

`--focus` becomes the `/compact` instructions. Always write one: number the open threads, and name file paths, IDs, decisions and anything waiting on the user's go. In your reply, add one line saying you are compacting now, so the user expects a short wait. The script skips the compaction if context is already under `--min-k` (default 250).

## Move this conversation to another login

Pairs with the `claude-accounts` skill, which gives each extra login a `claude-<name>` launcher. Use it when the ACCOUNTS line shows `switch=<name>` or the user asks to switch.

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/session_ctl.py --detach switch personal [--work-closed] [--continue "<next step>"] [--verify "<regex>"]
```

- `--detach` is required: the Bash call returns at once so your turn can end and the prompt can go idle, and the script survives `/exit`.
- **Compact decision (`--compact auto`):**
  - Under 200k: skip it. A bare session already carries about 110k of system prompt and tool definitions, so there's little to save and the user doesn't wait.
  - 200k or more: compact **before** the switch, while the prompt cache is warm on the current login, so the new login doesn't pay to re-read the full transcript uncached.
  - `--work-closed` when the current login has no quota left: the compaction runs **after** the switch, on the new login.
  - Override with `--compact before|after|no` and `--min-k`.
- **Steps:**
  1. Optional `/compact`.
  2. `/exit`, then wait for the claude process to end.
  3. Type `claude-<name> --resume <session id> <original flags>` at the shell. Flags come from cmux's record of the launch command (`--flags` overrides), shell-quoted. The switch refuses to guess the session: if the process args carry no session id, pass `--session <id>`. `switch main` relaunches plain `claude`.
  4. Wait for the idle prompt. With `--verify`, check the screen matches a regex, such as your statusline's account label; on a mismatch it stops before any compaction or continue message. Without it, the log says to confirm with `/status`.
- `--continue "<text>"` is sent as the first message after the resume. Use it only when work is unfinished and the user expects you to keep going. Leave it out when your turn ended on a question.
- In the reply that triggers the switch, say in one line that you are moving to the other login and whether you'll compact. Then end the turn: this session is about to exit.

## Turn an MCP server on or off mid-session

Editing `~/.claude.json` does not load a server into a running session; `/mcp enable <name>` does.

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/session_ctl.py mcp enable "<server name>"
```

Run it with Bash `run_in_background` so Claude Code re-invokes you when it finishes. The log shows the `/mcp` result; then load the server's tools through tool search.

## Failure modes

Read the log after a failure.

- `timed out waiting for an idle prompt`: the session stayed busy.
- `prompt stayed busy with typed text`: the user was typing for about 10 minutes, and nothing was sent.
- `still running 60s after /exit`: the exit didn't happen, so the session is still on the old login. Say so next turn.
- `resume failed`: the launcher wasn't found or the session id didn't resolve. The pane is at a shell prompt; tell the user the resume command.
- `--verify did NOT match`: the resume ran, but the screen didn't show the expected account, so nothing else was sent. Check the pane.

Tests (offline, no cmux or network): `python3 -m unittest discover -s tests`.

Files in this skill

  • SKILL.md5.9 KB
  • scripts/ctx-hint.py1.3 KB
  • scripts/session_ctl.py12.8 KB
  • tests/test_session_ctl.py4 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.