A Claude Code plugin that stops developers from starting new tasks inside old sessions.
When a prompt clearly targets a different task than the ongoing conversation, the plugin blocks it and instructs the developer to run /clear. The blocked prompt is saved and automatically resubmitted as the first message of the fresh session — nothing is retyped.
Claude Code sends the whole session history with every turn. Developers who keep working in one session across unrelated tasks waste context, trigger compaction, and — worse — get degraded answers because the model trusts stale file reads and dead assumptions from the previous task. /clear fixes it, but only if people actually run it. This plugin makes the lazy path impossible.
/plugin marketplace add zdamir/task-switch-gate
/plugin install task-switch-gate@task-switch-gate
Requirements: python3 and the claude CLI on PATH (both already present for any Claude Code user). No API key — classification runs through claude -p using your existing auth.
- On every prompt, a
UserPromptSubmithook reads the session transcript. - Young sessions (<3 user turns), slash commands, and trivial prompts ("continue", "yes") pass instantly — no model call, no latency.
- Otherwise a Haiku classifier compares the new prompt against the last 6 user prompts.
- It blocks only on high-confidence task switches. Ambiguity → allowed. Classifier error, timeout, or garbage output → allowed (fail-open). It can never lock you out.
- On block, the prompt is written to
.claude/.pending-task-promptin your project. ASessionStarthook (matcher:clear) picks it up and resubmits it in the fresh session. Pending prompts expire after 10 minutes.
Add .claude/.pending-task-prompt to your project .gitignore.
Knobs at the top of hooks/task_switch_gate.py:
| Setting | Default | Meaning |
|---|---|---|
MIN_USER_TURNS |
3 | Skip classification for young sessions |
MAX_HISTORY_PROMPTS |
6 | Recent prompts shown to the classifier |
CLASSIFIER_MODEL |
claude-haiku-4-5 |
Model for the yes/no call |
CLASSIFIER_TIMEOUT |
25s | Hard cap on the classifier call |
The classifier prompt is deliberately biased toward "not a new task". If you see false positives, tighten the rules text in call_classifier before touching thresholds.
If your Claude Code version ignores initialUserMessage from SessionStart hooks, set USE_INITIAL_USER_MESSAGE = False in hooks/inject_handoff.py to fall back to context injection.
Test changes without installing:
claude --plugin-dir /path/to/task-switch-gate
Validate structure:
claude plugin validate .
MIT