Metadata-Version: 2.4
Name: backfill
Version: 0.3.0
Summary: Keeps every Claude Code and Codex session of a project in the project itself, automatically.
Author-email: Dausuva Labs <support@focsd.com>
License: MIT
Project-URL: Homepage, https://github.com/focsd/backfill
Project-URL: Issues, https://github.com/focsd/backfill/issues
Project-URL: Source, https://github.com/focsd/backfill
Keywords: claude,claude-code,codex,logging,ai-agents,transcripts
Classifier: Environment :: Console
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# backfill

Keeps every **Claude Code** and **Codex** session of every project in the project itself, automatically.
Install it once per computer; from then on each session you start, in any repo, is saved to that repo's
`.claude-logs/` or `.codex-logs/` folder: readable history, every tool call, raw transcript, per-turn summaries.
Nothing leaves your machine, the folders ignore themselves in git, and secrets are stripped from the logs by default.

## TL;DR

```bash
pipx install git+https://github.com/focsd/backfill   # pre-release; later: pipx install backfill  |  brew install focsd/tap/backfill
backfill install      # once per computer: hooks into Claude Code and Codex
backfill doctor       # everything green? then you are done
```

Restart open Claude Code / Codex sessions. From now on every session in any repo is saved to that repo's `.claude-logs/` / `.codex-logs/` (git-ignored, secrets removed).

| You want to | Run |
|---|---|
| see what is logged here | `backfill status` |
| read or search old sessions | `backfill search <text>`, `backfill show` |
| continue yesterday's work | `backfill digest` (paste into a new session) or `backfill recap` |
| tune this project | `backfill init --preset minimal\|standard\|everything [--layout monthly]` |
| free disk space | `backfill clean` (dry run), then `--apply` |
| stop logging here / everywhere | `backfill off` / `backfill uninstall` |

### Details

```bash
pipx install backfill        # or: uv tool install backfill   |   brew install focsd/tap/backfill
backfill install             # hooks into Claude Code and Codex (whichever are on this machine)
backfill status              # what is installed, what is logged for the project you are in
```

> **Status: pre-release.** backfill is not on PyPI or Homebrew yet. Until the first release, install straight from GitHub:
> `pipx install git+https://github.com/focsd/backfill`. Then run `backfill install`.

**Upgrading:** `pipx upgrade backfill` (or `brew upgrade backfill`), then run `backfill install` once more so the hooks' copy in `~/.backfill/` is refreshed.
**Removing:** `backfill uninstall` removes backfill's hooks from your agent settings (your own entries and the logs stay); `backfill uninstall --purge` also deletes `~/.backfill/`.

macOS and Windows are supported; Linux is expected to work and is built in CI, but is not yet a declared target. Needs Python 3.11+ (the Homebrew formula brings it along; on Windows install Python from python.org or the Microsoft Store and tick "Add to PATH"). No third-party Python dependencies.

## What `backfill install` does

| Agent | Change (always backed up as `*.bak-<time>`, undone by `backfill uninstall`) |
|---|---|
| Claude Code | 8 hooks in `~/.claude/settings.json` (your own hooks stay), instruction block in `~/.claude/CLAUDE.md` |
| Codex | `notify` in `~/.codex/config.toml` (an existing notify command is **kept and chained**, not replaced), lifecycle hooks in `hooks.json`, instruction block in `AGENTS.md`; JetBrains Codex homes are found too |
| Both | a copy of the runtime in `~/.backfill/` (`%LOCALAPPDATA%\backfill` on Windows) so hooks keep working when a package manager moves or upgrades its own copy |

It also replaces the older `claude-logger` / `codex_repo_logger` hooks (any generation, including `codex_notify_fanout.py` / `codex_notify_router.py`), and imports the existing history
of the project you ran it in. If your Codex `notify` is a chain (Codex's own Computer Use plugin wraps it with `--previous-notify`), only backfill's link is replaced and the wrapper stays outermost. Hooks never block or fail a session: they always exit 0 and report problems to `~/.backfill/hook-errors.log`.

Codex may ask you to review the new lifecycle hooks once: type `/hooks` in Codex. Turn logging works through `notify` even before that.

## Commands

| Command | |
|---|---|
| `install [--agent claude\|codex] [--codex-profile minimal\|standard\|full] [--retain-days N] [--dry-run]` | hook the agents |
| `uninstall [--purge]` | remove the hooks (logs are never touched) |
| `status [--all] [--json]` | installation + what is logged for this project (sessions, turns, size, history waiting to be imported) |
| `doctor [--json]` | checks that logging will really happen, with a fix hint per problem; exit 1 on failure |
| `init [--preset minimal\|standard\|everything] [--layout flat\|monthly]` | create `.agents/backfill.toml` for the project |
| `on` / `off` | switch logging on/off for the project |
| `sync [--all]` | import history the agents already have, plus logs of the older loggers (`_codex_logs`, `_claude_logs`, `.claude/logs`); safe to repeat, never touches the old folders |
| `summary` | sessions, turns, tool calls, tokens, models, active days |
| `search <text>` / `show [session]` | find and read past sessions |
| `digest` | markdown hand-off of the latest sessions: paste it into a fresh session to continue |
| `recap [--since 1d] [--all]` | what happened since yesterday / `7d` / a date, per project and session |
| `clean [--older-than 30] [--apply]` | map what the agents keep on this machine (transcripts, caches, scratchpads, databases, log folders) and prune it safely |
| `audit [--fix]` | find (and redact) secrets in existing logs |
| `retain --days N [--apply]` | delete old journals (dry run by default) |
| `export --output logs.zip`, `list`, `config` | archive, list projects, show effective settings |

## Per-project config: `.agents/backfill.toml`

Commit it to share settings with a team. First existing file wins: `.agents/backfill.toml`, `.agents/backfill/config.toml`,
`.backfill.toml`, `.backfill/config.toml`. A config file also marks the project root (handy in monorepos). `backfill init` writes a commented template:

```toml
enabled = true                   # false = log nothing for this project
agents = ["claude", "codex"]
[logs]
claude_dir = ".claude-logs"      # one folder name inside the project; paths outside it are rejected
codex_dir  = ".codex-logs"
layout = "flat"                  # flat | monthly (YYYY-MM/ folders for journals, histories, transcripts, artifacts)
[capture]
preset = "standard"              # minimal | standard | everything; keys below override the preset
transcripts = "raw"              # raw | redacted | off
scratchpad = "manifest"          # off | manifest | copy: Claude scratchpad files, Codex generated images
exclude_tools = ["Read"]         # leave these tools' output out of the history and journal
session_max_mb = 0               # cap on one session's readable history (0 = none); the raw transcript is never capped
[redact]
enabled = true
extra_patterns = ["ACME-[0-9]{8}"]
[retention]
days = 0                         # used by `backfill retain`
[context]
announce = true                  # tell the agent where earlier sessions live
resume = false                   # also hand it a digest of the previous session
notes = ""
```

More keys under `[capture]` (all optional): `journal_events = true`, `copy_artifacts = true`, `codex_profile = "standard"` (`minimal|standard|full`, which Codex hooks `backfill install` registers),
`scratchpad_max_mb = 5`, and the size limits `event_value_limit = 20000`, `markdown_text_limit = 20000`, `markdown_tool_input_limit = 2000`, `markdown_tool_output_limit = 4000` (characters; longer values are cut in the history and journal, never in the raw transcript).
Unknown keys and wrong types are ignored, so a typo never stops logging; `backfill doctor` lists unknown keys and invalid choices. A project config comes with the repository, so it can only change how *that project* is logged: it cannot point logs outside the project or touch your install.

Machine-wide settings live in `~/.backfill/config.toml`; `mode = "opt-in"` there logs only projects that have a config file.
Project root detection: `BACKFILL_ROOT`, then the nearest config file / `.backfill-root`, `.git`, `.idea`, a manifest
(`package.json`, `pyproject.toml`, ...), then the working directory. The home directory and the agents' own folders are never logged.
`BACKFILL_DISABLE=1` stops logging for one process; `~/.backfill/denylist.txt` / `allowlist.txt` list folders, one per line.

## Capture presets

| Preset | What it does |
|---|---|
| `minimal` | transcript copy + readable history (shorter tool output); no event journal, no artifacts, no scratchpads |
| `standard` | the default: transcript, history, journal, per-turn summaries, edit backups/todos/subagents, scratchpad manifest |
| `everything` | standard plus scratchpad and generated-image copies, Codex `full` hook profile, long history limits |

Keys you write next to `preset` always win over the preset. `scratchpad = "manifest"` lists the files of a session's scratchpad
(names, sizes, SHA-256) in `artifacts/<session>/scratchpad-manifest.json`; `"copy"` also keeps files up to 5 MB (`scratchpad_max_mb`), text with secrets removed.
`exclude_tools` and `session_max_mb` shape the readable history and the journal; the transcript copy is separate (`transcripts = "redacted"` or `"off"` for that).
`backfill doctor` prints what is and is not covered in the current project (`covers:` lines).

## What is kept

```text
.claude-logs/  (or .codex-logs/)
  INDEX.md                      one row per session
  history/<session>.md          readable conversation, tool calls and (capped) results
  transcripts/<session>.jsonl   the agent's raw transcript, refreshed every turn
  events-DAY.jsonl  turns-DAY.jsonl  sessions-DAY.jsonl
  artifacts/<session>/          edit backups, todos, subagents, scratchpad manifest/copies (Claude); generated images (Codex)
  session_state/  latest-*.json  .gitignore ("*")  README.md
```

With `layout = "monthly"`, everything except `INDEX.md`, `session_state/`, `latest-*.json` and the housekeeping files sits inside `YYYY-MM/` folders
(`.claude-logs/2026-12/history/...`). A session lives in the month it started; changing the layout later keeps finding earlier files.

## Environment variables

| Variable | Effect |
|---|---|
| `BACKFILL_DISABLE=1` | log nothing in this process |
| `BACKFILL_ROOT` | use this folder as the project root |
| `BACKFILL_HOME`, `CLAUDE_CONFIG_DIR`, `CODEX_HOME` | move backfill's data folder / Claude's / Codex's home |
| `BACKFILL_ALLOWLIST`, `BACKFILL_DENYLIST` | folders (separated like `PATH`) to log only / never log |
| `BACKFILL_TAGS`, `BACKFILL_IDE`, `BACKFILL_HOST`, `BACKFILL_WORKSPACE_LABEL` | extra fields in the session records |
| `BACKFILL_TMP_DIRS` | where to look for Claude Code scratchpads (default: the system temp folders) |

## Troubleshooting

- **Nothing is logged:** run `backfill doctor`; every failing check has a fix hint. Restart running Claude Code / Codex sessions after `backfill install`.
- **Codex logs only some events:** type `/hooks` in Codex once and approve the lifecycle hooks (turn logging works without it).
- **A session is missing:** `backfill sync` imports what the agents still have; `backfill status` shows sessions waiting to be imported.
- **Errors inside hooks:** they never break a session; they are written to `~/.backfill/hook-errors.log` (`%LOCALAPPDATA%\backfill\` on Windows).
- **Logging should stop for one project:** `backfill off` in it (or `enabled = false` in its config); for one run set `BACKFILL_DISABLE=1`.

## Cleaning up (`backfill clean`)

Agents keep a lot: Codex rollouts and throw-away folders, Claude edit backups and shell snapshots, scratchpads in the temp folder, databases.
`backfill clean` lists them with sizes and marks what is safe to prune, always a dry run first:

- an agent's own transcript is a candidate **only** when backfill holds a complete copy of that session in a project (otherwise it says `run backfill sync --all`);
- only items older than `--older-than` days (default 30); running sessions are never that old;
- `--apply` moves them to `<backfill data>/trash/<time>/` (`--restore <batch>` puts them back, `--empty-trash` frees the space); `--delete` removes for good;
- databases, credentials, config, memory, skills and plugins are never candidates; project log folders are reported with the `backfill retain` command that prunes them;
- `--scan ~/dev` also finds log folders of projects backfill has not seen yet.

## Built for real machines

Lessons from the earlier Claude and Codex loggers that are built in:

- **Overlapping hooks are safe.** Codex runs most hooks asynchronously; a per-session file lock (`flock` / `msvcrt`) means every transcript line is processed exactly once.
  On Windows, replacing a file another process has open is retried instead of failing.
- **Nothing is overwritten silently.** If an agent rewrites a transcript so that it shrinks, the previous copy is kept as `<session>.prev-<time>.jsonl`.
- **Read-only folders are skipped quietly** (a session started in `C:\Program Files\...` is not an error).
- **Where it ran:** session records carry workspace, IDE (`TERM_PROGRAM`, `BACKFILL_IDE`), host, user and `BACKFILL_TAGS`, which helps when several IDEs share a repo.
- **Env lists:** `BACKFILL_ALLOWLIST` / `BACKFILL_DENYLIST` (paths separated like `PATH`; the old `CODEX_REPO_LOG_*` variables still work).

## Privacy

Logs contain full conversations. Redaction removes well-known credential formats (API keys, tokens, private keys, JWTs, URL
passwords, `password=...`) from journals and histories. The raw transcript copy is byte-exact unless you choose
`transcripts = "redacted"`. Redaction is best effort: treat the folders like source credentials. `backfill audit` shows what is
in them. Claude Code deletes its own transcripts after 30 days by default; the copies here are not affected (`--retain-days` also keeps Claude's own).

## Development

```bash
python -m pip install pytest ruff && python -m pytest -q && ruff check src tests
python scripts/check_versions.py     # the version in the code must match the tag
```

Layout: `src/backfill/` (core, `agents/claude.py`, `agents/codex.py`), `tests/`. Needs no third-party packages.

## Support and contributing

Questions and problems: open an issue, or write to support@focsd.com. Security reports: see `SECURITY.md`. Contributions: see `CONTRIBUTING.md`. Changes per version: `CHANGELOG.md`.

## Releasing (maintainers)

1. Bump `__version__` in `src/backfill/__init__.py`; `python scripts/check_versions.py`.
2. Tag `vX.Y.Z` and push: the workflow publishes to PyPI (trusted publisher, environment `pypi`).
3. Update `url` and `sha256` in the formula of the `homebrew-tap` repository.

## License

MIT, see `LICENSE`.
