Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsCommunityBlog
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

Back to skills

Recover Session

ASecurity

Recover sessions that hit context limits without a handoff.

85 stars
0 votes
0 copies
1 views
Added 9/19/2026
ai-agentspythongobashgitapi

Works with

claude codeapi

Security Analysis

A100/100

Scanned 9/19/2026

Install to Claude Code

$npx -y skills add indigoai-us/hq-core --skill recover-session --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Recover Session?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Recover Session
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/indigoai-us-recover-session/badge)](https://www.skillsdirectory.com/skills/indigoai-us-recover-session)

More formats (shields.io, HTML) on the badges page.

Download Zip
Files
SKILL.md
---
name: recover-session
description: Recover sessions that hit context limits without a handoff.
allowed-tools: Read, Write, Bash, Glob, Grep, AskUserQuestion
---

# /recover-session - Dead Session Recovery

Find and recover Claude Code sessions that died from context limits ("Prompt is too long") without running `/handoff`. Reconstructs thread JSON from JSONL session data so work is tracked in HQ.

**User's input:** $ARGUMENTS

## Arguments

- No args: scan last 7 days, interactive selection
- `--days N`: scan last N days (default 7)
- `--session UUID`: recover specific session by ID
- `--dry-run`: show what would be recovered, don't write anything

## Process

### 1. Scan for Dead Sessions

Parse arguments. Set `DAYS` from `--days` or default to 7.

**Death signals** (from real data — the session ends with these patterns):
- Assistant content `"Prompt is too long"` (the API rejects the request)
- `/compact` fails with `"Error during compaction: Error: Conversation too long"`
- No productive content follows the error (session is dead)

**Step 1a: Find candidates** — search for error strings in JSONL files. Exclude subagent files (in `subagents/` dirs):

```bash
SESSIONS_ROOT="$HOME/.claude/projects"
find "$SESSIONS_ROOT" -name "*.jsonl" -not -path "*/subagents/*" -mtime -${DAYS} -size +10k 2>/dev/null | while read f; do
  if grep -q "Prompt is too long\|Conversation too long\|Error during compaction" "$f" 2>/dev/null; then
    echo "$f"
  fi
done
```

If `--session UUID` was provided, only check files matching `*/${UUID}.jsonl`.

**Step 1b: Verify death** — a session is truly dead only if the error appears near the end. Sessions that compacted successfully and continued are NOT dead. Check the last 20 lines:

```bash
tail -20 "$JSONL_FILE" | grep -q "Prompt is too long\|Conversation too long\|Error during compaction"
```

Only keep sessions where the error is in the tail (nothing productive follows).

**Step 1c: Extract metadata** — the slug may not be in the first line (first line is often `queue-operation`). Scan up to 30 lines:

```bash
head -30 "$JSONL_FILE" | python3 -c "
import json, sys
for line in sys.stdin:
    try:
        d = json.loads(line)
        if d.get('slug'):
            print(json.dumps({
                'session_id': d.get('sessionId',''),
                'slug': d.get('slug',''),
                'cwd': d.get('cwd',''),
                'git_branch': d.get('gitBranch',''),
                'started': d.get('timestamp','')
            }))
            break
    except: continue
"
```

**Step 1d: Cross-reference** — check `workspace/threads/` for existing threads. Skip sessions that already have a `recovered` thread:

```bash
grep -rl "original_session_id.*SESSION_ID" workspace/threads/ 2>/dev/null
```

Build candidate list. If empty, report "No unrecovered dead sessions found" and stop.

### 2. Present Candidates

If `--session UUID` was provided, skip to step 3 with that session.

Otherwise display a table:

```
Dead Sessions Found (last {DAYS} days):

  # | Died       | Slug                     | Project | Size
  1 | 2026-02-12 | linked-watching-lovelace | HQ      | 3.4MB
  2 | 2026-02-11 | merry-nibbling-coral     | {company}  | 858KB
```

Use `AskUserQuestion` to let user pick which to recover (number, or "all").

### 3. Extract State from JSONL

**CRITICAL: Never read the full JSONL. Always stream via Python.**

For each selected session, run this extraction script via Bash. It streams line-by-line and outputs a compact ~3KB summary JSON:

```bash
python3 << 'PYEOF'
import json, sys

JSONL_PATH = "SESSION_PATH_HERE"

out = {
    "session_id": None, "slug": None, "cwd": None, "git_branch": None,
    "started_at": None, "died_at": None, "first_user_message": None,
    "files_edited": [], "files_written": [], "git_commands": [],
    "last_5_summaries": [], "death_count": 0, "total_lines": 0,
    "project_dir_encoded": None
}

files_edited = set()
files_written = set()
git_cmds = []
summaries = []
first_user = False

with open(JSONL_PATH) as f:
    for line in f:
        out["total_lines"] += 1
        try:
            d = json.loads(line)
        except:
            continue

        # Session metadata from first slug-bearing line
        if not out["slug"] and d.get("slug"):
            out["session_id"] = d.get("sessionId")
            out["slug"] = d["slug"]
            out["cwd"] = d.get("cwd")
            out["git_branch"] = d.get("gitBranch")
            out["started_at"] = d.get("timestamp")

        # Track last timestamp as death time
        if d.get("timestamp"):
            out["died_at"] = d["timestamp"]

        # First user message (truncated). Skip command XML wrappers and
        # compact summaries — find the first real user message
        if not first_user and d.get("type") == "user":
            if d.get("isCompactSummary"):
                continue
            msg = d.get("message", {})
            content = msg.get("content", "")
            text = ""
            if isinstance(content, str) and content:
                text = content
            elif isinstance(content, list):
                for c in content:
                    if isinstance(c, dict) and c.get("type") == "text":
                        text = c["text"]
                        break
            if text:
                # Extract command name if wrapped in XML
                import re
                cmd_match = re.search(r"<command-name>(/\S+)</command-name>", text)
                args_match = re.search(r"<command-args>(.*?)</command-args>", text, re.DOTALL)
                if cmd_match:
                    out["first_user_message"] = cmd_match.group(1)
                    if args_match:
                        out["first_user_message"] += " " + args_match.group(1).strip()[:400]
                else:
                    out["first_user_message"] = text[:500]
                first_user = True

        # Tool use: find file edits and git commands
        if d.get("type") == "assistant":
            msg = d.get("message", {})
            content = msg.get("content", [])
            if isinstance(content, list):
                for c in content:
                    if not isinstance(c, dict):
                        continue
                    if c.get("type") == "tool_use":
                        name = c.get("name", "")
                        inp = c.get("input", {})
                        if name == "Edit" and inp.get("file_path"):
                            files_edited.add(inp["file_path"])
                        elif name == "Write" and inp.get("file_path"):
                            files_written.add(inp["file_path"])
                        elif name == "Bash":
                            cmd = inp.get("command", "")
                            if "git commit" in cmd or "git push" in cmd:
                                git_cmds.append(cmd[:150])
                    elif c.get("type") == "text":
                        text = c["text"]
                        if text == "Prompt is too long":
                            out["death_count"] += 1
                        elif len(text) > 30:
                            summaries.append(text[:300])

        # Check for subagent (skip these)
        if d.get("subagentId"):
            print(json.dumps({"skip": True, "reason": "subagent"}))
            sys.exit(0)

out["files_edited"] = sorted(files_edited)
out["files_written"] = sorted(files_written)
out["git_commands"] = git_cmds[-10:]
out["last_5_summaries"] = summaries[-5:]

# Detect project from path
parts = JSONL_PATH.split("/")
for i, p in enumerate(parts):
    if p == "projects" and i + 1 < len(parts):
        out["project_dir_encoded"] = parts[i + 1]
        break

print(json.dumps(out, indent=2))
PYEOF
```

Read the output JSON. If `{"skip": true}`, skip this session (subagent).

### 4. Detect Company

Decode `project_dir_encoded` and inspect `cwd` + `files_edited`/`files_written`:

- `cwd` contains `repos/private/{company}` → {company}
- `cwd` contains `repos/private/{company}-` → {company}
- `cwd` contains `repos/private/{company}-` → {company}
- `files_touched` include `companies/{co}/` → that company
- Fallback: "personal"

Decode project dir for display:
- `-Users-{your-username}-Documents-HQ` → "HQ"
- `-Users-{your-username}-Documents-HQ-repos-private-{company}` → "{company}"
- etc. (replace leading path + `-` with readable name)

### 5. Generate Thread JSON

Using extracted data, generate a summary and next_steps from:
- `first_user_message` — what the session set out to do
- `last_5_summaries` — what it was doing when it died
- `files_edited` + `files_written` — scope of work

Build thread in HQ format:

```json
{
  "id": "T-{YYYYMMDD}-{HHMMSS}-recovered-{slug-fragment}",
  "created_at": "{died_at}",
  "summary": "{1-2 sentence AI-generated summary}",
  "status": "interrupted",
  "category": "recovered",
  "company": "{detected company}",
  "git": {
    "branch": "{git_branch}",
    "repos": {}
  },
  "files_touched": ["{relativized file paths}"],
  "next_actions": ["{inferred from death context}"],
  "recovery": {
    "original_session_id": "{session_id}",
    "original_slug": "{slug}",
    "death_timestamp": "{died_at}",
    "death_count": 0,
    "total_lines": 0,
    "project_dir": "{decoded project name}"
  }
}
```

Use the death timestamp (not current time) for the thread ID — represents when work happened.

Relativize file paths: strip `~/HQ/` prefix.

If `--dry-run`, display what would be written and stop here. Do NOT write any files.

Write to `workspace/threads/{thread_id}.json`.

### 6. Update Handoff Artifacts

**Update `workspace/threads/recent.md`:**
Regenerate table of last 15 threads (sorted by date descending):
```markdown
| Thread | Created | Summary | Status |
|--------|---------|---------|--------|
| T-...-recovered-... | 2026-02-12 | ... | interrupted |
```

**Update `workspace/threads/INDEX.md`:**
Add recovered thread to the full threads table.

**Write `workspace/threads/handoff.json`:**
```json
{
  "created_at": "{now ISO8601}",
  "message": "Recovered dead session: {slug}",
  "last_thread": "{thread_id}",
  "thread_path": "workspace/threads/{thread_id}.json",
  "context_notes": "{summary}. Died from context limits. {next_actions[0] if any}"
}
```

**Skip** (unsafe in recovery context):
- Auto-committing repos (can't know what was dirty at death vs now)
- Knowledge repo commits
- qmd reindex (user can run `qmd update`)

### 7. Report

```
Session Recovery Complete
═════════════════════════

Thread:   {thread_id}
Died:     {death timestamp}
Session:  {slug} ({session_id})
Company:  {company}
Summary:  {conversation_summary}
Files:    {N} files touched
Commits:  {N} git commands found in session

Thread saved: workspace/threads/{thread_id}.json

Next:
  - Review: Read workspace/threads/{thread_id}.json
  - Continue: /nexttask (recovered thread will appear)
  - Reindex: qmd update (if needed)
```

If multiple sessions recovered, show summary table then per-session details.

## Rules

- NEVER cat/read entire JSONL files — always stream via python3 or use grep/tail
- Keep total context from extraction under 5KB per session
- Cross-reference existing threads BEFORE running extraction (cheap check first)
- Recovery threads use death timestamp, not current time, for thread_id
- Never auto-commit repos during recovery — user must verify what's dirty now vs then
- Skip subagent JSONL files (they have `subagentId` in their lines)
- Relativize all file paths to HQ root in thread JSON
- `--dry-run` must not write any files

Attribution

indigoai-usindigoai-us
View sourceMore from indigoai-us →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

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

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Caveman

Ultra-compressed communication mode that cuts output tokens while keeping technical accuracy. Levels: lite, full, ultra and the wenyan variants. Use for /caveman, "caveman mode", "talk like caveman", "be brief" or "less tokens".

1066601 votes

Hyperplan

Adversarial multi-agent planning skill. Self-orchestrates 5 hostile category members (unspecified-low, unspecified-high, deep, ultrabrain, artistry) via team-mode for ruthless cross-critique debate, distills only the defensible insights, then MANDATORILY hands the distilled insight bundle to the `plan` agent for executable plan formalization. Use when planning needs maximum rigor and surfacing of weak assumptions, blind spots, and over-engineering. Triggers: 'hyperplan', 'hpp', '/hyperplan', ...

686011 votes

Mcp Code Execution

Routes multi-tool workflows through MCP servers for large datasets and pipelines. Use when Bash tool overhead is limiting throughput on data-heavy tasks.

3351 votes

catchup

Recovers the conversation and failed tool calls of a previous Codex, Claude Code, Antigravity, Cline, Copilot CLI, Cursor, DeepSeek Harness, Kimi, OpenCode, Pi Agent, or ZCode session. Use when the user says "catch up", "what did the last session do", "get me up to speed", "I switched agents", asks to recover/summarize a previous session before continuing, or asks to diagnose or report a catchup failure. Do NOT use for the current conversation, git history, or any non-agent log.

651 votes

math-skill

A comprehensive mathematical reasoning skill for AI assistants — handles arithmetic to research-level problems with rigorous step-by-step reasoning, systematic verification, and transparent uncertainty handling

381 votes
View all in ai-agents →