compact-ledger 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- compact_ledger-0.1.0/.claude-plugin/marketplace.json +14 -0
- compact_ledger-0.1.0/.claude-plugin/plugin.json +12 -0
- compact_ledger-0.1.0/.github/workflows/ci.yml +29 -0
- compact_ledger-0.1.0/.gitignore +18 -0
- compact_ledger-0.1.0/AGENTS.md +38 -0
- compact_ledger-0.1.0/LICENSE +21 -0
- compact_ledger-0.1.0/PKG-INFO +318 -0
- compact_ledger-0.1.0/PLAN.md +49 -0
- compact_ledger-0.1.0/README.md +295 -0
- compact_ledger-0.1.0/bin/compact-ledger +28 -0
- compact_ledger-0.1.0/evals/README.md +30 -0
- compact_ledger-0.1.0/evals/run_evals.py +45 -0
- compact_ledger-0.1.0/hooks/hooks.json +26 -0
- compact_ledger-0.1.0/pyproject.toml +58 -0
- compact_ledger-0.1.0/skills/compact-ledger/SKILL.md +64 -0
- compact_ledger-0.1.0/src/compact_ledger/__init__.py +21 -0
- compact_ledger-0.1.0/src/compact_ledger/__main__.py +8 -0
- compact_ledger-0.1.0/src/compact_ledger/cli.py +905 -0
- compact_ledger-0.1.0/src/compact_ledger/doctor.py +1038 -0
- compact_ledger-0.1.0/src/compact_ledger/evals.py +1857 -0
- compact_ledger-0.1.0/src/compact_ledger/hooks.py +807 -0
- compact_ledger-0.1.0/src/compact_ledger/ledger.py +1419 -0
- compact_ledger-0.1.0/src/compact_ledger/models.py +580 -0
- compact_ledger-0.1.0/src/compact_ledger/parsers/__init__.py +47 -0
- compact_ledger-0.1.0/src/compact_ledger/parsers/runners.py +2191 -0
- compact_ledger-0.1.0/src/compact_ledger/parsers/transcript.py +2222 -0
- compact_ledger-0.1.0/src/compact_ledger/pins.py +2497 -0
- compact_ledger-0.1.0/src/compact_ledger/renderer.py +888 -0
- compact_ledger-0.1.0/src/compact_ledger/store.py +494 -0
- compact_ledger-0.1.0/tests/test_adoption_polish.py +1523 -0
- compact_ledger-0.1.0/tests/test_cli.py +669 -0
- compact_ledger-0.1.0/tests/test_evals.py +127 -0
- compact_ledger-0.1.0/tests/test_hooks.py +972 -0
- compact_ledger-0.1.0/tests/test_ledger.py +936 -0
- compact_ledger-0.1.0/tests/test_models.py +255 -0
- compact_ledger-0.1.0/tests/test_pins.py +871 -0
- compact_ledger-0.1.0/tests/test_renderer.py +712 -0
- compact_ledger-0.1.0/tests/test_runners.py +704 -0
- compact_ledger-0.1.0/tests/test_store.py +283 -0
- compact_ledger-0.1.0/tests/test_transcript_parser.py +788 -0
- compact_ledger-0.1.0/tests/test_usability.py +634 -0
- compact_ledger-0.1.0/uv.lock +108 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "compact-ledger",
|
|
3
|
+
"description": "compact-ledger: compaction memory for Claude Code that keeps user rules and failed attempts.",
|
|
4
|
+
"owner": {
|
|
5
|
+
"name": "Solomon Wakhungu"
|
|
6
|
+
},
|
|
7
|
+
"plugins": [
|
|
8
|
+
{
|
|
9
|
+
"name": "compact-ledger",
|
|
10
|
+
"source": "./",
|
|
11
|
+
"description": "Keeps user rules, corrections and failed test attempts alive across context compaction."
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "compact-ledger",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Keeps user rules, corrections and failed test attempts alive across context compaction. Deterministic, local, no LLM calls.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Solomon Wakhungu"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/1solomonwakhungu/compact-ledger",
|
|
9
|
+
"repository": "https://github.com/1solomonwakhungu/compact-ledger",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"keywords": ["compaction", "memory", "hooks", "context", "constraints"]
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.11", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: astral-sh/setup-uv@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- name: Run checks
|
|
21
|
+
run: |
|
|
22
|
+
if [ ! -f pyproject.toml ]; then
|
|
23
|
+
echo "Bootstrap commit (no pyproject.toml yet); skipping."
|
|
24
|
+
exit 0
|
|
25
|
+
fi
|
|
26
|
+
uv sync --dev
|
|
27
|
+
uv run ruff check .
|
|
28
|
+
uv run ruff format --check .
|
|
29
|
+
uv run pytest -v
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# AGENTS.md — compact-ledger
|
|
2
|
+
|
|
3
|
+
## Mission
|
|
4
|
+
Build **compact-ledger**, a deterministic, zero-LLM `PreCompact` / `SessionStart` memory injector that preserves verbatim user constraints (including superseding corrections) and a structured test/debug attempt ledger across AI coding agent context compactions.
|
|
5
|
+
|
|
6
|
+
## Tech Stack & Constraints
|
|
7
|
+
- **Language:** Python `>=3.11` managed with `uv`.
|
|
8
|
+
- **Runtime dependencies:** **Zero** third-party runtime dependencies (Python standard library only) so hooks run in <50ms with zero install friction across Python 3.11–3.15.
|
|
9
|
+
- **Dev dependencies:** `pytest`, `ruff`.
|
|
10
|
+
- **Package layout:** `src/compact_ledger/` with console entry point `compact-ledger = "compact_ledger.cli:main"`.
|
|
11
|
+
- **No LLM calls anywhere.** Everything is deterministic, local, and fail-open on hook execution.
|
|
12
|
+
|
|
13
|
+
## Quality Bar
|
|
14
|
+
- `uv run pytest` passes 100% with zero skipped tests.
|
|
15
|
+
- `uv run ruff check .` and `uv run ruff format --check .` pass cleanly.
|
|
16
|
+
- Strict token/character budget enforcement: user pins ALWAYS take priority over attempt history, and output never blows up the post-compaction context window.
|
|
17
|
+
- Hook commands always fail-open (exit 0 on unexpected internal error) so a bug never breaks a user's live agent session.
|
|
18
|
+
|
|
19
|
+
## Mandatory Worker PR, Review & Merge Workflow
|
|
20
|
+
Every implementation worker thread owns its PR end-to-end (the orchestrator does NOT review or merge PRs):
|
|
21
|
+
1. Work in your assigned git worktree branched from `origin/main`.
|
|
22
|
+
2. Implement your milestone cleanly with thorough unit + integration tests.
|
|
23
|
+
3. Run `uv run pytest && uv run ruff check . && uv run ruff format --check .`.
|
|
24
|
+
4. Commit with clear commit messages, push to `origin`, open a PR targeting `main` via `gh pr create`, and call `link_pull_request` with the full PR URL.
|
|
25
|
+
5. **Mandatory Fable 5.2 Review:**
|
|
26
|
+
- Call `orchestrator_capabilities` once in your thread before your first `delegate_task`.
|
|
27
|
+
- Start a review delegate using `delegate_task` with:
|
|
28
|
+
- `role: "review"`
|
|
29
|
+
- `mode: "wait"` (or `"async"` and wait for wakeup)
|
|
30
|
+
- `target: { "providerInstanceId": "claudeAgent_claude_proxy", "model": "claude-fable-5-2", "options": { "effort": "high" } }`
|
|
31
|
+
- Pass the PR URL/number, `git diff origin/main...HEAD` (or instructions to inspect it), and test output. Ask Fable 5.2 for an adversarial review covering edge cases, false positives in override detection, parser robustness, budget enforcement, and hook safety.
|
|
32
|
+
- Fix all valid findings, re-run tests & ruff, and push updates. If changes were non-trivial, run a fresh `delegate_task` re-check with Fable 5.2 (`claudeAgent_claude_proxy` / `claude-fable-5-2`) until approved with no blocking issues.
|
|
33
|
+
6. **CI & Self-Merge:**
|
|
34
|
+
- Check PR checks (`gh pr view <num> --json statusCheckRollup`). If GitHub Actions CI is still running, call `watch_pull_request` and end your turn to wake on completion (do NOT use `sleep` or `gh --watch`).
|
|
35
|
+
- If `origin/main` moved while you worked and there are conflicts, `git fetch origin && git rebase origin/main`, resolve conflicts, re-run tests, and push.
|
|
36
|
+
- Once Fable 5.2 approves and CI is green, merge your PR yourself:
|
|
37
|
+
`gh pr merge <num> --squash --delete-branch`
|
|
38
|
+
- Report the merged PR URL, Fable 5.2 review summary, and final test results in your final message.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Solomon Wakhungu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: compact-ledger
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministic, zero-LLM PreCompact/SessionStart memory injector for AI coding agents.
|
|
5
|
+
Project-URL: Homepage, https://github.com/1solomonwakhungu/compact-ledger
|
|
6
|
+
Project-URL: Repository, https://github.com/1solomonwakhungu/compact-ledger
|
|
7
|
+
Project-URL: Issues, https://github.com/1solomonwakhungu/compact-ledger/issues
|
|
8
|
+
Author: Solomon Wakhungu
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-agents,claude-code,codex,compaction,context,hooks,memory
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# compact-ledger
|
|
25
|
+
|
|
26
|
+
**Deterministic `PreCompact` + `SessionStart` memory injector for AI coding agents.**
|
|
27
|
+
|
|
28
|
+
Long coding sessions hit context compaction right when precision matters most. Lossy summaries routinely drop or blur verbatim user rules, forget mid-session corrections (*"actually use July 31, not June 30"*), and erase the exact assertion values observed across failed test runs — causing the post-compaction agent to break constraints or repeat dead-end edits.
|
|
29
|
+
|
|
30
|
+
`compact-ledger` parses the session transcript right before compaction and re-injects a short, strictly budgeted ledger on resume:
|
|
31
|
+
|
|
32
|
+
1. **Manual pins & superseding corrections (Tier 1)**: verbatim user overrides linked to what they superseded, so the agent sees both the active rule and the stale one.
|
|
33
|
+
2. **Active verbatim user constraints (Tier 2)**: positive and negative (`[DO NOT]`) directives, one pin per rule even when they arrive in one long brief, with chatter and harness-written messages filtered out.
|
|
34
|
+
3. **Structured attempt & edit-thrash ledger (Tier 3)**: ordered failure trajectories across `pytest`, `vitest`, `jest`, `go test`, and generic commands (`PROGRESSING`, `OSCILLATING`, `STALLED`, `RESOLVED`) plus `[THRASH/REVERT]` warnings when edits loop or revert.
|
|
35
|
+
4. **Modified files summary (Tier 4)**: deduplicated list of files touched in the session.
|
|
36
|
+
|
|
37
|
+
- **Zero LLM calls** — 100% deterministic, local, reproducible output.
|
|
38
|
+
- **Zero third-party runtime dependencies** — Python `>=3.11` standard library only.
|
|
39
|
+
- **Linear-time & lightweight** — O(N) scanning with atomic JSON storage (runs all 24 seeded eval cases in ~30 ms and scales linearly with transcript size).
|
|
40
|
+
- **Fail-open hooks** — hook commands exit `0` without polluting stdout on malformed input, bad flags, broken pipes, or unexpected errors (unless `--strict` is passed) so a hook issue never disrupts a live agent session.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Quick start
|
|
45
|
+
|
|
46
|
+
### Claude Code plugin (no Python setup)
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
/plugin install compact-ledger --marketplace 1solomonwakhungu/compact-ledger
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The plugin bundles the hooks, a `compact-ledger` command for the Bash tool, and a skill that
|
|
53
|
+
teaches the agent when to pin a rule. It runs from the plugin's own source with any Python
|
|
54
|
+
3.11+ already on the machine and installs nothing.
|
|
55
|
+
|
|
56
|
+
### CLI + hooks
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
uv tool install git+https://github.com/1solomonwakhungu/compact-ledger.git # or: pip install git+…
|
|
60
|
+
cd your-project
|
|
61
|
+
compact-ledger init --write # merges PreCompact + SessionStart hooks into .claude/settings.json
|
|
62
|
+
compact-ledger doctor # verifies PATH, Python >=3.11, hooks, plugin & store
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Restart Claude Code, run `/compact` once, then `compact-ledger doctor` or `compact-ledger status`
|
|
66
|
+
to confirm a snapshot was written. `init` warns if `compact-ledger` is not on `PATH`, because a
|
|
67
|
+
hook that cannot find its command fails silently.
|
|
68
|
+
|
|
69
|
+
Try it without installing: `uvx --from git+https://github.com/1solomonwakhungu/compact-ledger.git compact-ledger eval`.
|
|
70
|
+
|
|
71
|
+
Local development:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
git clone https://github.com/1solomonwakhungu/compact-ledger.git
|
|
75
|
+
cd compact-ledger
|
|
76
|
+
uv sync --dev
|
|
77
|
+
uv run pytest
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## What the agent sees after compaction
|
|
83
|
+
|
|
84
|
+
A messy session: a long opening brief with four rules, a date correction, a tool choice that
|
|
85
|
+
gets reversed, chatter, a hook complaint, and two tests that keep failing.
|
|
86
|
+
|
|
87
|
+
- Turn 1 (brief): *"…Use June 30 as the billing cutoff… Never touch migrations under
|
|
88
|
+
db/migrations/… Don't add any new third-party dependencies… All money math must use
|
|
89
|
+
`Decimal`, never floats."*
|
|
90
|
+
- *"ok sounds good"*, *"thanks!"*, *"wait, actually can you show me the diff so far?"* (chatter and questions, dropped)
|
|
91
|
+
- *"Actually use July 31 instead of June 30 for the billing cutoff. Finance moved the fiscal year."*
|
|
92
|
+
- *"For the frontend bits use pnpm, not npm. And always run the tests with -x…"* then
|
|
93
|
+
*"hmm scratch that, the CI image doesn't have pnpm. Stick with npm."*
|
|
94
|
+
- `Stop hook feedback: …` (written by the harness, dropped)
|
|
95
|
+
- `test_proration` fails with `400 -> 200 -> 150`; `test_tax_cap` fails with `400 -> 200 -> 400`
|
|
96
|
+
while `src/billing/tax.py` is edited and reverted
|
|
97
|
+
|
|
98
|
+
This is the exact block injected on resume:
|
|
99
|
+
|
|
100
|
+
```markdown
|
|
101
|
+
## Compact Ledger (Session: billing-refactor)
|
|
102
|
+
Recorded verbatim before compaction. Pins, overrides and constraints are the user's standing instructions: where the compaction summary disagrees, follow these. Failure lines list values already observed, oldest first; do not retry an approach that produced them.
|
|
103
|
+
|
|
104
|
+
### Manual Pins & Active Overrides
|
|
105
|
+
- [MANUAL] [tags: arch] Zero third-party runtime dependencies.
|
|
106
|
+
- [OVERRIDE] hmm scratch that, the CI image doesn't have pnpm. Stick with npm. (supersedes: For the frontend bits use pnpm, not npm.)
|
|
107
|
+
- [OVERRIDE] Actually use July 31 instead of June 30 for the billing cutoff. Finance moved the fiscal year. (supersedes: Use June 30 as the billing cutoff date for the fiscal-year rollover.)
|
|
108
|
+
|
|
109
|
+
### User Constraints
|
|
110
|
+
- [DO NOT] [turn 9] Makes sense. One more thing — I pasted the staging DB URL earlier in Slack, do not commit it or anything under .env* to the repo.
|
|
111
|
+
- [CONSTRAINT] [turn 6] And always run the tests with -x so we stop at the first failure.
|
|
112
|
+
- [CONSTRAINT] [turn 1] Refactor `compute_proration()` so it's a pure function.
|
|
113
|
+
- [DO NOT] [turn 1] Never touch migrations under db/migrations/ — the DBA team owns those.
|
|
114
|
+
- [DO NOT] [turn 1] Don't add any new third-party dependencies.
|
|
115
|
+
- [CONSTRAINT] [turn 1] Keep the public signature of `compute_proration(account, cutoff)` stable, the mobile team imports it.
|
|
116
|
+
- [DO NOT] [turn 1] All money math must use `Decimal`, never floats.
|
|
117
|
+
|
|
118
|
+
### Active Test Failures & Thrash Warnings
|
|
119
|
+
- [OSCILLATING] tests/test_billing.py::test_tax_cap (pytest) [400 -> 200 -> 400; expected 100] @ tests/test_billing.py:88
|
|
120
|
+
- [PROGRESSING] tests/test_billing.py::test_proration (pytest) [400 -> 200 -> 150; expected 100] @ tests/test_billing.py:42
|
|
121
|
+
- [SINGLE_FAILURE] npm run lint (generic) [exit 1: ✖ 2 problems (2 errors, 0 warnings)]
|
|
122
|
+
- [SINGLE_FAILURE] src/ui/Invoice.test.ts > Invoice > shows prorated amount (vitest) ['$4.00'; expected '$1.00'] @ src/ui/Invoice.test.ts:31
|
|
123
|
+
- [THRASH/REVERT] src/billing/tax.py (2 edits, 5 failures): Reverted prior edit from turn 7 while failures persisted
|
|
124
|
+
|
|
125
|
+
### Modified Files
|
|
126
|
+
- src/billing/proration.py
|
|
127
|
+
- src/billing/tax.py
|
|
128
|
+
- services/ledger/rollover.go
|
|
129
|
+
- CHANGELOG.md
|
|
130
|
+
|
|
131
|
+
### Resolved Tests
|
|
132
|
+
- [RESOLVED] github.com/acme/billing/services/ledger::TestRollover (go) [2026-06-30 -> PASS; expected 2026-07-31] @ rollover_test.go:27
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
How to read it:
|
|
136
|
+
|
|
137
|
+
- A long message becomes **one pin per rule**. The July 31 correction retires only the
|
|
138
|
+
June 30 line; the other rules from the same brief stay active.
|
|
139
|
+
- `(supersedes: …)` shows the dead instruction next to the live one.
|
|
140
|
+
- Failure lines list observed values oldest first. `400 -> 200 -> 400` on `test_tax_cap` plus
|
|
141
|
+
the `[THRASH/REVERT]` line says the last change to `tax.py` was already tried and undone.
|
|
142
|
+
- Paths are relative to the project. The preamble is only added when it fits in spare budget.
|
|
143
|
+
|
|
144
|
+
Output is strictly bounded (`len(output) <= --max-chars`, default 6000). Under a tight
|
|
145
|
+
budget, pins and overrides come first, then constraints, then failures, then files.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## For agents
|
|
150
|
+
|
|
151
|
+
Inside Claude Code every shell command gets `CLAUDE_CODE_SESSION_ID`, so `--session-id` is
|
|
152
|
+
optional:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
compact-ledger pin "Never push to main" --negative --tag git # survives every compaction
|
|
156
|
+
compact-ledger inject # what will be injected
|
|
157
|
+
compact-ledger status --json # sessions, store dir, current session
|
|
158
|
+
compact-ledger status --session-id "$CLAUDE_CODE_SESSION_ID" # pin IDs and text, for unpin
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Rule detection is pattern-based. Pin anything the user would be upset to see forgotten.
|
|
162
|
+
[skills/compact-ledger/SKILL.md](skills/compact-ledger/SKILL.md) is a ready-made skill with
|
|
163
|
+
the same guidance and a table for reading each ledger tag; the plugin installs it, or copy it
|
|
164
|
+
into `.claude/skills/compact-ledger/`.
|
|
165
|
+
|
|
166
|
+
Exit codes: `0` ok, `1` not found or runtime error, `2` usage error. Hook commands always
|
|
167
|
+
exit `0` unless `--strict` is passed.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Claude Code hooks
|
|
172
|
+
|
|
173
|
+
`compact-ledger init --write` adds these entries to `.claude/settings.json`. It preserves
|
|
174
|
+
existing hooks and settings, writes atomically, and changes nothing on a second run. Without
|
|
175
|
+
`--write` it prints the merged file.
|
|
176
|
+
|
|
177
|
+
```json
|
|
178
|
+
{
|
|
179
|
+
"hooks": {
|
|
180
|
+
"PreCompact": [
|
|
181
|
+
{
|
|
182
|
+
"matcher": "",
|
|
183
|
+
"hooks": [
|
|
184
|
+
{
|
|
185
|
+
"type": "command",
|
|
186
|
+
"command": "compact-ledger hook pre-compact"
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
],
|
|
191
|
+
"SessionStart": [
|
|
192
|
+
{
|
|
193
|
+
"matcher": "compact|resume",
|
|
194
|
+
"hooks": [
|
|
195
|
+
{
|
|
196
|
+
"type": "command",
|
|
197
|
+
"command": "compact-ledger hook session-start"
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
}
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
- **`PreCompact`** reads Claude Code's stdin JSON (`session_id`, `transcript_path`, `cwd`, `trigger`), parses the transcript, and atomically writes `.compact-ledger/sessions/<session_id>.json`.
|
|
207
|
+
- **`SessionStart`** reads stdin JSON (`session_id`, `source`, `cwd`) and, only when `source` is `"compact"` or `"resume"` (or `--always` is passed), emits `hookSpecificOutput.additionalContext` with the bounded Markdown ledger.
|
|
208
|
+
|
|
209
|
+
Use `--command` to change the hook command prefix, for example `--command "uvx compact-ledger"` once the package is on PyPI.
|
|
210
|
+
|
|
211
|
+
### Where snapshots live
|
|
212
|
+
|
|
213
|
+
One store per project, at `<project root>/.compact-ledger/`. The project root is the nearest
|
|
214
|
+
ancestor that already has a `.compact-ledger` directory, else the nearest ancestor with
|
|
215
|
+
`.git`, else the starting directory. Hooks start from `CLAUDE_PROJECT_DIR` (or the payload
|
|
216
|
+
`cwd`), CLI commands from the current directory, so a hook and an agent running
|
|
217
|
+
`compact-ledger pin` from a subdirectory reach the same store.
|
|
218
|
+
|
|
219
|
+
Override with `--store-dir` or `COMPACT_LEDGER_DIR`. The store writes its own `.gitignore`,
|
|
220
|
+
because snapshots contain user messages verbatim.
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## Codex CLI & Generic Transcripts
|
|
225
|
+
|
|
226
|
+
`compact-ledger` parses real Codex CLI rollouts end-to-end — including modern code-mode `exec` tool calls (`tools.exec_command`, `tools.shell_command`, `tools.apply_patch`), `event_msg` `item_completed` (`CommandExecution`, `FileChange`, `UserMessage`) records, legacy `function_call` / `local_shell_call` / `apply_patch` events, and generic JSONL transcripts — extracting user rules, test trajectories (`pytest`, `vitest`, `jest`, `go test`, generic), edit-thrash warnings, and modified files.
|
|
227
|
+
|
|
228
|
+
Hook integration (`PreCompact` + `SessionStart`) is currently **Claude Code native**; for Codex CLI sessions, use the CLI commands directly:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
compact-ledger snapshot --transcript ~/.codex/sessions/.../rollout-*.jsonl
|
|
232
|
+
compact-ledger inject --session-id <session-id>
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Known limits
|
|
238
|
+
|
|
239
|
+
- **Automatic compaction hooks are Claude Code native.** Codex CLI has no compaction hook to attach to yet; Codex rollouts are supported via `compact-ledger snapshot` / `inject` / `pin`.
|
|
240
|
+
- **Rule detection is heuristic.** Casual phrasings can be missed and some task lines in a
|
|
241
|
+
long brief are labelled `[CONSTRAINT]`. Manual pins (`compact-ledger pin`) are the reliable path.
|
|
242
|
+
- **Not on PyPI yet.** Install from GitHub or as a Claude Code plugin.
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## CLI Reference
|
|
247
|
+
|
|
248
|
+
All session commands accept `--store-dir <dir>` (or `COMPACT_LEDGER_DIR`). `--session-id`
|
|
249
|
+
defaults to `COMPACT_LEDGER_SESSION_ID`, then `CLAUDE_CODE_SESSION_ID`.
|
|
250
|
+
|
|
251
|
+
| Command | Description |
|
|
252
|
+
| --- | --- |
|
|
253
|
+
| `compact-ledger init [--write] [--settings-path <path>] [--command <cmd>]` | Print or atomically merge `PreCompact` & `SessionStart` hooks into `.claude/settings.json` (idempotent; refuses invalid JSON without corruption; warns when the hook command is not on `PATH`). |
|
|
254
|
+
| `compact-ledger doctor [--settings-path <path>] [--store-dir <dir>] [--json]` | Verify `compact-ledger` on `PATH`, Python `>=3.11`, `.claude/settings.json` `PreCompact` & `SessionStart` hooks, `.claude-plugin/` health (if present), store writability & `.gitignore`, and hook snapshot activity — with exact copy-pasteable fix commands. |
|
|
255
|
+
| `compact-ledger pin "<text>" [--session-id <id>] [--tag <tag>...] [--negative] [--json]` | Add a high-priority manual pin that survives across snapshots and tight budgets. |
|
|
256
|
+
| `compact-ledger unpin "<pin-id-or-text>" [--session-id <id>]` | Remove a manual pin by pin ID or verbatim text. |
|
|
257
|
+
| `compact-ledger snapshot --transcript <path> [--session-id <id>] [--format auto\|claude-code\|codex\|generic] [--json]` | Parse a JSONL transcript, preserve manual pins, compute overrides/trajectories/thrash, and save an atomic snapshot. |
|
|
258
|
+
| `compact-ledger inject [--session-id <id>] [--max-chars 6000] [--format markdown\|json]` | Render the stored snapshot strictly within `--max-chars`. |
|
|
259
|
+
| `compact-ledger status [--session-id <id>] [--json]` | List stored sessions (the current one is marked `*`), or show one session's counts and manual pins. |
|
|
260
|
+
| `compact-ledger clear [--session-id <id>]` | Delete the stored snapshot for a session. |
|
|
261
|
+
| `compact-ledger hook pre-compact [--store-dir <dir>] [--strict]` | Claude Code `PreCompact` stdin hook handler (fail-open unless `--strict`). |
|
|
262
|
+
| `compact-ledger hook session-start [--store-dir <dir>] [--max-chars 6000] [--always] [--strict]` | Claude Code `SessionStart` stdin hook handler (injects on `compact`/`resume` or `--always`; fail-open unless `--strict`). |
|
|
263
|
+
| `compact-ledger eval [--format text\|json] [--verbose]` | Run the packaged 24-case deterministic evaluation suite from any working directory. |
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## Evaluation Suite (24 Seeded Cases)
|
|
268
|
+
|
|
269
|
+
`compact-ledger` ships with 24 realistic multi-turn evaluation cases covering Claude Code, Codex (including code-mode `exec` / `apply_patch` rollouts), and generic JSONL transcripts, long multi-rule briefs, contradictory negative-constraint overrides, override chains, harness-feedback & false-positive guards, `pytest`/`vitest`/`jest`/`go test`/generic trajectories, edit-thrash detection, strict budget truncation, and full hook round-trips.
|
|
270
|
+
|
|
271
|
+
Run it anywhere with `compact-ledger eval` (or `uv run compact-ledger eval`):
|
|
272
|
+
|
|
273
|
+
```text
|
|
274
|
+
compact-ledger eval (24 seeded cases)
|
|
275
|
+
------------------------------------------------------------------------
|
|
276
|
+
[PASS] 01_negative_constraints_claude_code (constraints ) 1.08 ms
|
|
277
|
+
[PASS] 02_codex_transcript_constraints_and_edits (constraints ) 0.77 ms
|
|
278
|
+
[PASS] 03_manual_pin_priority_over_auto_and_ledger (constraints ) 1.14 ms
|
|
279
|
+
[PASS] 04_superseding_date_correction (overrides ) 0.76 ms
|
|
280
|
+
[PASS] 05_ignore_earlier_and_switch_from_x_to_y (overrides ) 0.72 ms
|
|
281
|
+
[PASS] 06_three_step_override_chain_a_b_c (overrides ) 0.86 ms
|
|
282
|
+
[PASS] 07_false_positive_guard_casual_actually (overrides ) 0.68 ms
|
|
283
|
+
[PASS] 08_unrelated_topic_isolation (overrides ) 0.76 ms
|
|
284
|
+
[PASS] 09_pytest_progressing_trajectory (trajectories) 1.04 ms
|
|
285
|
+
[PASS] 10_pytest_oscillating_trajectory (trajectories) 0.78 ms
|
|
286
|
+
[PASS] 11_pytest_stalled_and_raises_parametrized (trajectories) 0.75 ms
|
|
287
|
+
[PASS] 12_pytest_scoped_vs_unscoped_resolution (trajectories) 0.94 ms
|
|
288
|
+
[PASS] 13_vitest_and_jest_ansi_trajectories (trajectories) 0.97 ms
|
|
289
|
+
[PASS] 14_go_test_and_generic_fallback_trajectories (trajectories) 1.07 ms
|
|
290
|
+
[PASS] 15_edit_thrash_hunk_revert_and_stalled_loop (thrash ) 0.84 ms
|
|
291
|
+
[PASS] 16_clean_edit_then_pass_no_false_thrash (thrash ) 1.08 ms
|
|
292
|
+
[PASS] 17_tight_budget_pins_survive_400_1200_6000 (budget ) 3.29 ms
|
|
293
|
+
[PASS] 18_long_override_does_not_starve_negative_constraints (budget ) 1.90 ms
|
|
294
|
+
[PASS] 19_hook_pre_compact_to_session_start_roundtrip (hooks ) 1.47 ms
|
|
295
|
+
[PASS] 20_hook_session_start_gating_and_fail_open (hooks ) 0.44 ms
|
|
296
|
+
[PASS] 21_long_brief_single_rule_override (overrides ) 0.92 ms
|
|
297
|
+
[PASS] 22_contradictory_negative_constraint_override (overrides ) 0.80 ms
|
|
298
|
+
[PASS] 23_harness_feedback_filtering (constraints ) 0.85 ms
|
|
299
|
+
[PASS] 24_codex_code_mode_exec_trajectories_and_edits (trajectories) 1.15 ms
|
|
300
|
+
------------------------------------------------------------------------
|
|
301
|
+
Category breakdown:
|
|
302
|
+
- constraints 4/4 passed
|
|
303
|
+
- overrides 7/7 passed
|
|
304
|
+
- trajectories 7/7 passed
|
|
305
|
+
- thrash 2/2 passed
|
|
306
|
+
- budget 2/2 passed
|
|
307
|
+
- hooks 2/2 passed
|
|
308
|
+
------------------------------------------------------------------------
|
|
309
|
+
Total: 24/24 passed (100.0%) in 30.2 ms
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
See [evals/README.md](evals/README.md) for category details or run `uv run compact-ledger eval --format json` for machine-readable output.
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## License
|
|
317
|
+
|
|
318
|
+
MIT
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# MVP Plan — compact-ledger
|
|
2
|
+
|
|
3
|
+
Build a production-quality, user-ready MVP across 4 sequenced/parallel PRs.
|
|
4
|
+
|
|
5
|
+
## PR 1 (Foundation — land first on `main`)
|
|
6
|
+
**Branch:** `feat/foundation-store`
|
|
7
|
+
- Create `pyproject.toml` (`compact-ledger`, Python `>=3.11`, pure stdlib runtime, `pytest` + `ruff` dev deps, `src/` layout).
|
|
8
|
+
- Define core models in `src/compact_ledger/models.py`:
|
|
9
|
+
- `UserTurn`, `PinEntry` (with `supersedes` link, `source`, `timestamp`), `TestAttempt` (`test_id`, `runner`, `assertion_loc`, `expected`, `actual`, `outcome`, `turn_index`), `EditThrashEntry`, `CompactionSnapshot`.
|
|
10
|
+
- Implement durable local session storage in `src/compact_ledger/store.py` (`.compact-ledger/sessions/<session_id>.json` with atomic temp-file writes and manual `pin` persistence).
|
|
11
|
+
- Implement transcript readers in `src/compact_ledger/parsers/transcript.py` for Claude Code JSONL, Codex JSONL, and generic JSONL (extracting ordered user turns, bash/tool calls + outputs, and file edits).
|
|
12
|
+
- Implement CLI skeleton (`src/compact_ledger/cli.py`): `snapshot`, `inject`, `pin`, `status`, `clear`, `--version`.
|
|
13
|
+
- Unit tests for storage atomicity, transcript parsing, and CLI commands.
|
|
14
|
+
|
|
15
|
+
## PR 2 (User Pin & Superseding Correction Engine — parallel after PR 1)
|
|
16
|
+
**Branch:** `feat/pins-and-overrides`
|
|
17
|
+
- Implement `src/compact_ledger/pins.py`:
|
|
18
|
+
- Extracts verbatim user directives, constraints, negative constraints (*"don't touch X"*, *"never use Y"*, *"must keep Z"*), and explicit manual pins.
|
|
19
|
+
- Detects **superseding corrections** (*"actually use July 31 instead of June 30"*, *"ignore what I said earlier about..."*, *"switch from X to Y"*, *"correction:"*) and links newer pins to earlier superseded ones so the agent knows BOTH the active rule AND what is now stale.
|
|
20
|
+
- Deduplicates repetitive chatter/acknowledgements (*"ok"*, *"thanks"*, *"continue"*) so the budget stays dense and high-signal.
|
|
21
|
+
- Implement token/character budget allocator in `src/compact_ledger/renderer.py`:
|
|
22
|
+
- Default budget (e.g., `--max-chars 6000` / ~1500 tokens, configurable).
|
|
23
|
+
- Strict priority order: (1) Manual pins & active superseding corrections, (2) Remaining verbatim user constraints (newest first), (3) Active failing test attempts & thrash warnings, (4) Modified files summary.
|
|
24
|
+
- Thorough unit tests for override chains, negative constraints, chatter filtering, and exact budget truncation.
|
|
25
|
+
|
|
26
|
+
## PR 3 (Test Attempt Ledger & Thrash Detector — parallel after PR 1)
|
|
27
|
+
**Branch:** `feat/attempt-ledger`
|
|
28
|
+
- Implement runner output parsers in `src/compact_ledger/parsers/runners.py`:
|
|
29
|
+
- **pytest**: extracts failing node IDs, `file:line`, assertion expression, and `expected` vs `actual` (from `assert X == Y`, `E assert 400 == 100`, `AssertionError`, `pytest.raises`).
|
|
30
|
+
- **vitest / jest**: extracts test names, `Expected:` / `Received:` pairs, file:line.
|
|
31
|
+
- **go test**: extracts `--- FAIL: TestName`, file:line, got/want lines.
|
|
32
|
+
- **generic fallback**: command + non-zero exit code + normalized last error line.
|
|
33
|
+
- Implement `src/compact_ledger/ledger.py`:
|
|
34
|
+
- Tracks ordered attempts per unresolved test ID across the session.
|
|
35
|
+
- Classifies trajectories: `PROGRESSING` (e.g., `400 -> 200 -> 100` moving or distinct values), `OSCILLATING` (`400 -> 200 -> 400`), `STALLED` (same assertion + same observed value repeated), `RESOLVED` (later passed).
|
|
36
|
+
- Detects edit-revert thrashing (editing the same file repeatedly between identical failures, or reverting a prior hunk).
|
|
37
|
+
- Emits concise, high-signal ledger lines (last N distinct states per unresolved test) so post-compaction agents immediately see what was already tried.
|
|
38
|
+
- Thorough unit tests with real pytest, vitest/jest, and `go test` failure traces.
|
|
39
|
+
|
|
40
|
+
## PR 4 (Claude Code Hooks, Seeded 20-Case Eval Suite & Docs — final PR after PR 2 & PR 3)
|
|
41
|
+
**Branch:** `feat/hooks-evals-docs`
|
|
42
|
+
- Wire pins + attempt ledger + renderer into `snapshot` and `inject` end-to-end.
|
|
43
|
+
- Implement `src/compact_ledger/hooks.py`:
|
|
44
|
+
- `compact-ledger hook pre-compact`: reads Claude Code `PreCompact` stdin JSON (`session_id`, `transcript_path`, `cwd`), builds and saves the snapshot.
|
|
45
|
+
- `compact-ledger hook session-start`: reads Claude Code `SessionStart` stdin JSON (`session_id`, `source`), and when `source` is `"compact"` or `"resume"` (or `--always`), emits structured hook JSON (`hookSpecificOutput.additionalContext`) injecting the bounded ledger.
|
|
46
|
+
- Fail-open error handling (never exits non-zero on malformed stdin unless `--strict`).
|
|
47
|
+
- Implement `compact-ledger init --claude-code [--write]` to print or safely merge both hooks into `.claude/settings.json`.
|
|
48
|
+
- Build `evals/` with 20 realistic multi-turn transcripts verifying: verbatim constraint survival, superseding correction priority, pytest/jest/go progress vs oscillation classification, edit-thrash warnings, and strict budget compliance. Add `compact-ledger eval` CLI command.
|
|
49
|
+
- Finalize `README.md` with install (`uvx`/`pip`), Claude Code setup, before/after compaction examples, and eval benchmark output.
|