GUIDE · HOW-TO 8 min ·

Sweep pasted secrets out of coding agent historyFind every copy, rotate, then redact

A key you pasted into a coding agent lands in plaintext files under your home directory, and some of them never expire. One scanner run will not find all of them, and redaction alone leaves the key working.

TL;DR· the answer, in twenty seconds

What happened: Claude Code and Codex write prompts, tool output and edited files to plaintext stores under your home directory. Claude Code's prompt history stays until you delete it, and an infostealer Symantec described on October 15 already targets files in both agents' directories.

The minimum fix: List the stores, scan them with agentsweep and gitleaks plus a filename-only grep, rotate every key they find, then redact and delete the backups the redaction leaves.

The lesson: Rotation is the only step that covers every copy. A secret the agent never saw never needs sweeping.

Anthropic's own documentation says Claude Code stores session transcripts "locally in plaintext under ~/.claude/projects/" for 30 days by default. Its application data reference adds that ~/.claude/history.jsonl, the file behind up-arrow recall, stays until you delete it. OpenAI's Codex configuration docs say Codex saves session transcripts to ~/.codex/history.jsonl by default. If you ever pasted a key into either agent, or the agent read your .env, the value is still on disk.

Two recent reports make that worth an afternoon. On October 15, Symantec's Threat Hunter Team wrote that new versions of the Aoki infostealer target .claude.json and the Codex config.toml and .env files. Symantec lists credential and session files, not transcripts, does not report stolen history and does not name the operating systems the samples run on. The transcripts sit in the same directories. On November 6, The Register reported AE Studio tests in which agents told to keep a DEPLOY_TOKEN private posted it to a forum. Of the 43 agents that shared the token, 8 told their user without being asked. Your agent's own history is the record you can check.

This guide covers Claude Code and Codex on macOS and Linux. The order matters: list the stores, scan with more than one tool, rotate, then redact.

I ran every command below on macOS with Claude Code 2.1.325, codex-cli 0.169.0, agentsweep 0.1.9 and gitleaks 8.30.1. The scans ran against a fake home directory holding six fake keys generated at runtime, placed where each agent writes. To confirm the real layout, I listed file names in my own ~/.claude and ~/.codex and read the table schema of one Codex SQLite file, without opening any contents. I did not run claude purge, and I had no Linux machine to test on.

Where the copies live

Each agent writes more than one store, and the stores keep different things.

Claude Code, per its application data reference:

  • ~/.claude/projects/<project>/<session>.jsonl holds the conversation: your messages, tool calls and tool results. Subagent transcripts and large tool outputs sit in a folder named after the session. The retention sweep deletes these after cleanupPeriodDays, 30 by default.
  • ~/.claude/history.jsonl holds every prompt you typed, with timestamp and project path. The sweep does not remove it unless the HIPAA configuration applies to your sessions.
  • ~/.claude/file-history/<session>/ holds copies of files from before Claude edited them, for checkpoint restore. If Claude edited your .env, the old contents are here.
  • ~/.claude/paste-cache/ holds the contents of large pastes.

Codex:

  • ~/.codex/history.jsonl, which Codex writes unless you change history.persistence.
  • ~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl, one file per session, holding tool calls and their output. My machine's layout matches this.
  • SQLite files in ~/.codex. On my install of 0.169.0, thread_history_1.sqlite has a thread_items table with an item_json column, which suggests it stores the same items as the rollout files. I read the schema, not the rows, so treat that as likely rather than confirmed.

Count what you have before you scan:

find ~/.claude/projects -name '*.jsonl' | wc -l
find ~/.claude/file-history -type f | wc -l
wc -l < ~/.claude/history.jsonl
find ~/.codex/sessions -name 'rollout-*.jsonl' | wc -l
ls ~/.codex/*.sqlite ~/.codex/history.jsonl 2>/dev/null

If you also run Cursor, Gemini CLI, Cline or another agent, it keeps its own stores. agentsweep's --source flag lists 29 of them, and its README marks some as experimental. I tested Claude Code and Codex and no others.

What three scanners found in a planted home

agentsweep is an MIT-licensed Python tool built for this job. It knows each agent's default history path, works offline and ships 191 rules ported from gitleaks. Its scan command is read-only and exits 1 when it finds something. Gitleaks is a general secret scanner with a dir mode for plain folders. I planted one key in each of six places and ran both, plus a plain grep:

Planted where agentsweep, default root agentsweep, --root ~/.claude gitleaks dir grep -a
Claude transcript, AWS key found found found found
Claude transcript, Postgres URL with password found found missed found
~/.claude/history.jsonl, GitHub token missed found found found
~/.claude/file-history snapshot, AWS key missed missed found found
Codex rollout file, Stripe key found not scanned found found
SQLite file in ~/.codex, GitHub token missed not scanned missed found

Three findings decide how you run the sweep.

agentsweep's Claude Code source reads ~/.claude/projects and nothing else. Pointing it at the whole directory with --root ~/.claude added the prompt history. It still skipped the snapshot, because the tool reads .jsonl files and nothing else. When I copied the same Postgres line into a .txt file inside projects/, it missed that too. Its db-url-with-password rule caught the same line inside a transcript.

Gitleaks reads any text file, so it caught the snapshot. It has no rule for a password inside a database URL, so it missed the Postgres transcript. It also skipped the SQLite file, which is binary.

Neither tool found the key in the SQLite file. A grep -a treats binary files as text and found every planted key, at the cost of false positives on anything shaped like user:pass@host.

Run all three, and keep output to masked values or file names so the sweep does not print your keys to the terminal and into a new transcript:

# Read-only. agentsweep findings hold masked values only.
uvx agentsweep@0.1.9 scan --source claude-code --root ~/.claude -o claude-agentsweep.json
uvx agentsweep@0.1.9 scan --source codex -o codex-agentsweep.json

# --redact replaces the Secret and Match fields with REDACTED in the report.
gitleaks dir ~/.claude --redact -r claude-gitleaks.json -f json
gitleaks dir ~/.codex --redact -r codex-gitleaks.json -f json

# Backstop for SQLite and other binary stores. File names only.
grep -rlaE 'AKIA[A-Z2-7]{16}|gh[pousr]_[A-Za-z0-9]{36}|sk_live_[A-Za-z0-9]{24,}|sk-(proj|ant)-[A-Za-z0-9_-]{20,}|://[^:/@[:space:]]+:[^@[:space:]]+@' ~/.claude ~/.codex 2>/dev/null

Run these from your own terminal, not by asking the agent, so no transcript records the output. Extend the grep pattern with the prefixes of the providers you use. If your organization runs a different scanner, such as TruffleHog, add it. The point of the table is that every tool had a blind spot.

I did not plant a key in paste-cache/. It sits outside projects/, so agentsweep's default root skips it, and its contents may not be .jsonl. Gitleaks and grep read every file under ~/.claude, so they cover it either way.

The report files list paths, rule names and masked values, which is enough to tell someone where to look. Keep them out of synced folders and delete them once rotation is done.

Do not stop at a clean agentsweep result. After a redaction, which I cover below, a rescan of ~/.codex exited 0 while the .bak files beside the rewritten files still held both keys in full. The grep found them.

Rotate first, then redact

Redaction changes files on one disk. It does nothing to a copy an infostealer already sent out, a copy in a backup or sync tool, or the copy your model provider received when you sent the prompt. agentsweep's README says the same: it "does not affect what your AI provider already received." Anthropic's data usage page gives retention on its side as 30 days for commercial accounts and for consumer accounts that opt out of model training, and 5 years for consumer accounts that allow it.

So rotate first. Each finding names a rule and a file, which tells you which key and which project. For each one:

  1. Revoke or roll the key in the provider's console. agentsweep's fix output prints the revoke page for the rules it knows. Mine listed GitHub's token settings and Stripe's API keys page.
  2. Update wherever the real copy lives: a vault, CI variables, a deploy target.
  3. Read the provider's access logs for the period since the paste, if it has them.

A rotated key in a transcript is a dead string. Rotation is the only step that covers the copies you cannot reach, including the Codex SQLite rows that I found no documented way to scrub one value from.

Redact the local copies and their backups

Close every session of the agent you are cleaning. agentsweep checks for this. Pointed at the fake home, fix --source codex refused to rewrite anything until I passed --force, because a real Codex process was running on the same machine. It also refuses the default root without --allow-production, and its refusal message suggests copying the history elsewhere and passing --root to the copy as a rehearsal.

# Rewrites matches as [REDACTED:<rule>] after you type REDACT.
uvx agentsweep@0.1.9 fix --source codex --allow-production
uvx agentsweep@0.1.9 fix --source claude-code --root ~/.claude --allow-production

# Each rewritten file gets a .bak holding the original, secret included.
# undo restores from .bak (no prompt in my non-interactive run). purge deletes the backups.
uvx agentsweep@0.1.9 purge --source codex --yes
uvx agentsweep@0.1.9 purge --source claude-code --root ~/.claude --yes

In my test the rewrite kept the JSON valid and replaced the value while leaving the rest of the line: STRIPE_SECRET_KEY=[REDACTED:stripe-live]. Without --yes, purge printed a warning, deleted nothing and exited 2, so a script that ignores exit codes will leave every backup in place. After the purge, the grep backstop found no planted key in ~/.codex. The same run against ~/.claude cleaned the transcripts and the prompt history and left the file-history snapshot untouched.

agentsweep does not touch the stores it does not scan. For Claude Code there is a blunter tool. claude purge <project-path> deletes one project's transcripts and auto memory, its file-history and debug entries, its prompt lines in history.jsonl and its entry in ~/.claude.json. Per the docs it prints the plan and asks first, and --dry-run shows the plan without deleting. Before v2.1.288 the command was claude project purge. It does not remove pasted images or a session's scratchpad, which live under the temp directory. I read this from the docs and did not run it.

For a single snapshot in file-history that you would rather not lose the whole project over, delete that file. The cost is a checkpoint you can no longer restore. For the Codex SQLite stores, I would not edit rows by hand while the tool's schema is undocumented. The rotated key is the fix there.

Then shorten how long the next paste survives. In ~/.claude/settings.json, cleanupPeriodDays sets the sweep age, with a minimum of 1. It does not apply to history.jsonl, so add that file to your periodic scan. In ~/.codex/config.toml:

[history]
persistence = "none"

OpenAI's docs describe that key as controlling history.jsonl. I did not test whether it also stops the rollout files under sessions/, so check that folder after a session before you rely on it.

What this means for your stack

The minimum action fits in one sitting. Count the stores, run agentsweep with --root ~/.claude, gitleaks and the filename grep, rotate every key they name, then run fix and purge --yes. Add history.jsonl and file-history to whatever scan you already run, because the retention sweep never deletes the first and agentsweep never reads the second. If keys ended up in a repo instead of your home directory, the settings.local.json guide covers the Apiiro findings and that cleanup.

The pattern underneath is that anything the agent sees, the agent writes down. A transcript records your prompts, the commands it ran and what they printed. The durable fix keeps the value out of all three. The agent asks for a capability by name, a broker injects the real value into the child process at exec time, and the transcript records the name.

hasp is one working implementation. hasp run --project-root . --env STRIPE_SECRET_KEY=@STRIPE_SECRET_KEY --grant-secret window --grant-window 15m -- npm test passes the key to the test process while the agent's command line carries the reference name and never the value. Source-available (FCL-1.0), local-first, macOS and Linux, no account.

Whatever you use, run the sweep once now and again after any session where a key went into the prompt. The first sweep shows you how many keys you have pasted. The ones that matter are the ones still live.

Sources· cited above, in one place

NEXT STEP~90 seconds

Stop handing the agent your real keys.

hasp keeps secrets in one local encrypted vault, brokers them into the child process at exec, and never lets the agent read the value.

  • Local, encrypted vault — no account, no cloud, no telemetry by default.
  • Brokered run — agent gets a reference, the child process gets the value.
  • Pre-commit + pre-push hooks catch managed values before they ship.
  • Append-only HMAC audit log answers "did the agent touch the prod token?" in seconds.
→ okvault unlocked · binding ./api
→ okgrant once · pid 88421
→ okagent never read

macOS & Linux. Source-available (FCL-1.0, converts to Apache 2.0). No account.

Browse all clusters· eight threads, one index