#!/usr/bin/env python3
"""Print the prompts a user typed in past agent sessions for one project.
Three sources, read with --source (default: all):
claude Claude Code stores each session as JSONL under ~/.claude/projects/<slug>/,
where <slug> is the project's absolute path with every non-alphanumeric
character replaced by "-" (C:\\Users\\me\\proj becomes C--Users-me-proj).
Those transcripts are pruned after 30 days by default, so older sessions
come from ~/.claude/history.jsonl, the prompt log behind the up-arrow, which
keeps the typed text, project and session id. A session is read from its
transcript when one survives, else from that log, so no prompt is counted
twice.
codex Codex stores each session as JSONL under $CODEX_HOME/sessions/YYYY/MM/DD/
(CODEX_HOME defaults to ~/.codex). The first line, session_meta, records
the cwd the session ran in; that is how a session is matched to a project.
cursor Cursor keeps chats in an undocumented SQLite store, state.vscdb under its
globalStorage folder, in the table cursorDiskKV. Each composerData:<id> row is
one conversation, matched to a project by workspaceIdentifier or, failing that,
trackedGitRepos. Its turns are bubbleId:<id>:<bubble> rows; type 1 is the user.
Timestamps are epoch milliseconds, and older bubbles carry none, so the
conversation's own createdAt stands in.
Prints one typed prompt per line, oldest first, as "YYYY-MM-DD<TAB>source<TAB>prompt".
Skipped: tool results, subagent turns (Claude sidechains, Codex subagent threads),
non-interactive runs (codex exec, and Codex sessions driven by Claude Code, whose
prompts the agent wrote, not the user), meta turns, injected context and system
wrappers, anything longer than --max-len (pasted documents, not asks), and anything
shorter than --min-words ("ok", "yes" and "push" carry no intent to cluster).
Secrets pasted into a prompt (keys, tokens, passwords, credentials in URLs) are replaced
with [REDACTED] before printing, because the list is handed to a model. --no-redact keeps
them.
Usage:
extract_asks.py [project_dir] [--source all|claude|codex|cursor] [--since YYYY-MM-DD]
[--max-len 400] [--min-words 1] [--format tsv|jsonl] [--no-redact]
"""
import argparse
import json
import os
import re
import sqlite3
import sys
from datetime import date, datetime, timezone
from pathlib import Path
# Codex session sources a person types into. "exec" is a scripted run, and a dict
# source ({"subagent": ...}) is a thread another agent spawned.
CODEX_INTERACTIVE = {"cli", "vscode"}
# Another agent driving Codex (e.g. a Claude Code plugin): its prompts are not the user's.
CODEX_AGENT_ORIGINATORS = {"Claude Code"}
def slug(path: str) -> str:
return re.sub(r"[^A-Za-z0-9]", "-", os.path.abspath(path))
def norm_path(path: str) -> str:
"""Fold the ways a tool may record the same folder into one string. On Windows that
means drive-letter case, / vs \\, and the \\\\?\\ prefix a canonicalized path carries."""
if path.startswith("\\\\?\\"):
path = path[4:]
return os.path.normcase(os.path.abspath(path))
def spellings(project_dir):
"""The folder as given and with symlinks resolved. A tool records whichever path its
working directory had, usually the resolved one, so a symlink must match either."""
return list(dict.fromkeys([project_dir, os.path.realpath(project_dir)]))
def as_dict(value):
return value if isinstance(value, dict) else {}
def as_text(value):
return value if isinstance(value, str) else ""
def prompt_text(content):
if isinstance(content, str):
return content
if isinstance(content, list):
return " ".join(
as_text(part.get("text"))
for part in content
if isinstance(part, dict) and part.get("type") in ("text", "input_text")
)
return ""
def is_wrapper(text: str) -> bool:
return text.startswith(("<", "[Request", "# AGENTS.md instructions"))
# history.jsonl records slash commands and ! shell input as typed; transcripts wrap them in
# tags that is_wrapper() already drops. A path such as /Users/me/cv.pdf is still an ask.
SLASH_OR_SHELL = re.compile(r"^(/[A-Za-z0-9:_-]+(\s|$)|!)")
PASTE_ONLY = re.compile(r"^\[Pasted text #\d+[^\]]*\]$")
# Secrets people paste into a prompt: vendor key prefixes, credentials in URLs and
# webhooks, then a catch-all for bare high-entropy tokens. Ordinary prose never matches.
SECRETS = [
re.compile(p)
for p in (
r"(?<=://)[^\s:/@]+:[^\s@/]+(?=@)", # user:password@ in a URL
r"\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}", # Anthropic, OpenAI
r"\b(?:gh[pousr]_|github_pat_)[A-Za-z0-9_]{20,}", # GitHub
r"\bglpat-[A-Za-z0-9_-]{20,}", # GitLab
r"\bxox[abprs]-[A-Za-z0-9-]{10,}", # Slack
r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b", # AWS access key id
r"\bAIza[0-9A-Za-z_-]{35}", # Google API key
r"\bhf_[A-Za-z0-9]{30,}", # Hugging Face
r"\b(?:re|rk|sk|pk)_(?:live_|test_)?(?=[A-Za-z0-9_]*\d)[A-Za-z0-9_]{20,}", # Resend, Stripe
r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}", # JWT
r"(?i)(?<=bearer )[A-Za-z0-9._~+/=-]{16,}",
r"(?<=/api/webhooks/)\d+/[A-Za-z0-9_-]+", # Discord webhook
r"(?<=hooks\.slack\.com/services/)[A-Za-z0-9/]+", # Slack webhook
r"\b[MNO][A-Za-z0-9_-]{23,27}\.[A-Za-z0-9_-]{6}\.[A-Za-z0-9_-]{27,}", # Discord bot
r"\bsb_(?:secret|publishable)_[A-Za-z0-9_-]{20,}", # Supabase
r"\bKGAT_[A-Za-z0-9]{20,}", # Kaggle
# Any other bare token: 30+ characters mixing upper, lower and digits, standing alone
# (a path, file name or URL has / . or \ in it and is left alone).
r"(?<![^\s'\"=:])(?=[A-Za-z0-9_-]*[0-9])(?=[A-Za-z0-9_-]*[a-z])(?=[A-Za-z0-9_-]*[A-Z])"
r"[A-Za-z0-9_-]{30,}(?![^\s'\",;])",
)
]
# A labelled value keeps its label: "password=hunter2" becomes "password=[REDACTED]".
LABELLED_SECRET = re.compile(
r"(?i)\b((?:password|passwd|pwd|secret|token|api[_-]?key)\s*[=:]\s*)[^\s,;\[]+"
)
def redact(text):
"""The prompt with each pasted secret replaced, and how many were replaced."""
count = 0
for pattern in SECRETS:
text, n = pattern.subn("[REDACTED]", text)
count += n
text, n = LABELLED_SECRET.subn(r"\1[REDACTED]", text)
return text, count + n
def read_jsonl(path):
with open(path, encoding="utf-8", errors="ignore") as fh:
for line in fh:
try:
value = json.loads(line)
except json.JSONDecodeError:
continue
if isinstance(value, dict):
yield value
def claude_asks(project_dir):
home = Path.home() / ".claude"
folders = [home / "projects" / slug(p) for p in spellings(project_dir)]
found = [d for d in folders if d.is_dir()]
sessions = found[0] if found else folders[-1]
history = home / "history.jsonl"
if not found and not history.is_file():
return None, sessions
asks, seen = [], set()
for f in (f for d in found for f in d.glob("*.jsonl")):
seen.add(f.stem)
for turn in read_jsonl(f):
if turn.get("type") != "user" or turn.get("isMeta") or turn.get("isSidechain"):
continue
seen.add(as_text(turn.get("sessionId")))
text = prompt_text(as_dict(turn.get("message")).get("content")).strip()
asks.append((cursor_day(turn.get("timestamp")), text))
projects = {norm_path(p) for p in spellings(project_dir)}
for entry in read_jsonl(history) if history.is_file() else []:
where = as_text(entry.get("project"))
if not where or norm_path(where) not in projects or as_text(entry.get("sessionId")) in seen:
continue
text = as_text(entry.get("display")).strip()
if SLASH_OR_SHELL.match(text) or PASTE_ONLY.match(text):
continue
day = cursor_day(entry.get("timestamp"))
asks.append((day, text))
if not asks and not found:
return None, sessions
return asks, f"{' + '.join(map(str, found or [sessions]))} + {history}"
def codex_asks(project_dir):
root = Path(os.environ.get("CODEX_HOME", Path.home() / ".codex")) / "sessions"
if not root.is_dir():
return None, root
projects = {norm_path(p) for p in spellings(project_dir)}
asks, matched = [], 0
for f in root.rglob("*.jsonl"):
turns = read_jsonl(f)
meta = next(turns, {})
info = as_dict(meta.get("payload"))
source = info.get("source")
if (
meta.get("type") != "session_meta"
or not as_text(info.get("cwd"))
or norm_path(info["cwd"]) not in projects
or not isinstance(source, str)
or source not in CODEX_INTERACTIVE
or as_text(info.get("originator")) in CODEX_AGENT_ORIGINATORS
):
continue
matched += 1
for turn in turns:
item = as_dict(turn.get("payload"))
if turn.get("type") != "response_item" or item.get("role") != "user":
continue
text = prompt_text(item.get("content")).strip()
asks.append((cursor_day(turn.get("timestamp")), text))
if not matched:
return None, root
return asks, root
def cursor_db_path():
if sys.platform == "darwin":
return (
Path.home()
/ "Library"
/ "Application Support"
/ "Cursor"
/ "User"
/ "globalStorage"
/ "state.vscdb"
)
if sys.platform == "win32":
appdata = os.environ.get("APPDATA")
if appdata:
return Path(appdata) / "Cursor" / "User" / "globalStorage" / "state.vscdb"
return (
Path.home()
/ "AppData"
/ "Roaming"
/ "Cursor"
/ "User"
/ "globalStorage"
/ "state.vscdb"
)
config = os.environ.get("XDG_CONFIG_HOME")
base = Path(config) if config else Path.home() / ".config"
return base / "Cursor" / "User" / "globalStorage" / "state.vscdb"
def cursor_day(ts):
"""A Cursor timestamp as YYYY-MM-DD: epoch milliseconds, or ISO text in older data."""
if isinstance(ts, (int, float)) and not isinstance(ts, bool):
try:
return datetime.fromtimestamp(ts / 1000, timezone.utc).date().isoformat()
except (OverflowError, OSError, ValueError):
return ""
if isinstance(ts, str):
try:
return date.fromisoformat(ts[:10]).isoformat()
except ValueError:
pass
return ""
def cursor_project(data):
"""The folders a Cursor conversation ran in: its workspace, else its tracked repos."""
uri = as_dict(as_dict(data.get("workspaceIdentifier")).get("uri"))
if as_text(uri.get("fsPath")):
return [uri["fsPath"]]
repos = data.get("trackedGitRepos") or []
if not isinstance(repos, list):
return []
return [r["repoPath"] for r in repos if isinstance(r, dict) and as_text(r.get("repoPath"))]
def cursor_asks(project_dir):
db = cursor_db_path()
if not db.is_file():
return None, db
projects = {norm_path(p) for p in spellings(project_dir)}
try:
# Read-only, so a running Cursor holding the database is no obstacle.
conn = sqlite3.connect(f"{db.as_uri()}?mode=ro", uri=True)
except sqlite3.Error:
return None, db
try:
started = {}
for key, value in conn.execute(
"SELECT key, value FROM cursorDiskKV WHERE key LIKE 'composerData:%'"
):
try:
data = as_dict(json.loads(value))
except (TypeError, json.JSONDecodeError):
continue
if any(norm_path(p) in projects for p in cursor_project(data)):
started[key.split(":", 1)[1]] = cursor_day(data.get("createdAt"))
if not started:
return None, db
asks = []
for composer_id, day in started.items():
for (value,) in conn.execute(
"SELECT value FROM cursorDiskKV WHERE key LIKE ?", (f"bubbleId:{composer_id}:%",)
):
try:
bubble = as_dict(json.loads(value))
except (TypeError, json.JSONDecodeError):
continue
if bubble.get("type") != 1:
continue
text = as_text(bubble.get("text")).strip()
asks.append((cursor_day(bubble.get("createdAt")) or day, text))
except sqlite3.Error:
return None, db
finally:
conn.close()
return asks, db
SOURCES = {"claude": claude_asks, "codex": codex_asks, "cursor": cursor_asks}
# What a first-time user sees when there is nothing, or too little, to find repeats in.
NO_HISTORY_HINT = (
"\nNo agent history for {project} yet. History is stored per folder: run this from the "
"folder you opened in Claude Code, Codex or Cursor, or come back after a few sessions there."
)
FEW_PROMPTS = 20
FEW_PROMPTS_HINT = (
"Only {count} {noun} so far, which is too few to show what you repeat. Shortcuts come "
"from asks typed 5+ times; check the folder, drop --since or --min-words, or come back later."
)
def since_date(value):
try:
parsed = date.fromisoformat(value)
if parsed.isoformat() == value:
return value
except ValueError:
pass
raise argparse.ArgumentTypeError("expected a valid date in YYYY-MM-DD format")
def positive_int(value):
try:
number = int(value)
if number > 0:
return number
except ValueError:
pass
raise argparse.ArgumentTypeError("expected a positive integer")
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n", 1)[0])
ap.add_argument("project_dir", nargs="?", default=os.getcwd(), help="project folder (default: current directory)")
ap.add_argument("--source", choices=["all", *SOURCES], default="all", help="history source (default: all)")
ap.add_argument("--since", type=since_date, help="include prompts on or after YYYY-MM-DD (UTC)")
ap.add_argument("--max-len", type=positive_int, default=400, help="maximum prompt length in characters (default: 400)")
ap.add_argument("--min-words", type=positive_int, default=1, help="skip prompts with fewer words (default: 1)")
ap.add_argument("--no-redact", action="store_true", help="print pasted secrets as typed instead of [REDACTED]")
ap.add_argument("--format", choices=["tsv", "jsonl"], default="tsv", help="output format (default: tsv)")
args = ap.parse_args()
args.since = args.since or ""
args.project_dir = os.path.abspath(os.path.expanduser(args.project_dir))
if not Path(args.project_dir).is_dir():
ap.error("project_dir must be an existing directory")
if sys.platform == "win32":
# A pipe on Windows defaults to the ANSI code page, which can't print most prompts.
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
names = list(SOURCES) if args.source == "all" else [args.source]
rows, missing, redacted = [], [], 0
for name in names:
try:
asks, where = SOURCES[name](args.project_dir)
except OSError as exc:
missing.append(f"{name}: could not read session history ({exc.strerror or type(exc).__name__})")
continue
if asks is None:
missing.append(f"{name}: checked, nothing for this folder ({where})")
continue
kept = 0
for day, text in asks:
words = text.split()
if (
not text
or is_wrapper(text)
or len(text) > args.max_len
or len(words) < args.min_words
or day < args.since
):
continue
text = " ".join(words)
if not args.no_redact:
text, n = redact(text)
redacted += n
rows.append((day, name, text))
kept += 1
print(f"{kept} prompts from {name} ({where})", file=sys.stderr)
if len(missing) == len(names):
missing.append(NO_HISTORY_HINT.format(project=args.project_dir))
sys.exit("\n".join(missing))
for note in missing:
print(note, file=sys.stderr)
if redacted:
print(f"{redacted} secrets replaced with [REDACTED] (--no-redact keeps them)", file=sys.stderr)
if len(rows) < FEW_PROMPTS:
print(FEW_PROMPTS_HINT.format(count=len(rows), noun="prompt" if len(rows) == 1 else "prompts"), file=sys.stderr)
rows.sort()
for day, name, text in rows:
if args.format == "jsonl":
print(json.dumps({"date": day, "source": name, "prompt": text}, ensure_ascii=False))
else:
print(f"{day}\t{name}\t{text}")
if __name__ == "__main__":
main()