Add summarize delivery: prep --via claude-p + --via api (#329)#357
Merged
Conversation
`tj summarize prep --via claude-p|api` runs the rewrite for you in one shot (wrap, rewrite, check, stage) instead of the manual copy/paste or MCP in-session paths. This is the write-the-summary half of the feature and the last PR of the summarize stack. claude-p drives the user's local `claude -p` headless: no key, with a 300s timeout so a stuck call can't wedge the CLI. api calls Anthropic with the user's own TJ_ANTHROPIC_API_KEY (any global ANTHROPIC_API_KEY is ignored) and the required [summarize] api_model (no default; only frontier models preserve structure). It reports a "pays for itself" amortization: actual provider usage priced through the existing cost engine, divided by the estimated per-call saving. Both paths run through the existing check/stage gate, so structure stays a hard guarantee. The api path accepts only a clean completion (stop_reason end_turn); truncated, refused, or non-summary responses are refused before the structure gate, which matters for prose-only prompts with no markers to fail. Nothing is staged on a failure, and a rejected-but-billed call still says it was billed. Closes #329.
anilmurty
approved these changes
Jun 29, 2026
anilmurty
left a comment
Contributor
There was a problem hiding this comment.
Thanks @auspexlabs — this completes the summarize stack (scan → mechanism → apply → delivery), and it's careful exactly where it needs to be on TokenJam's first outbound call:
- Reads only
TJ_ANTHROPIC_API_KEYand ignores any ambientANTHROPIC_API_KEY; the no-key path fails before any network call, and the key never lands in a log, span, or error message. Real timeouts on both paths, nothing staged on timeout. - The "pays for itself" math is honest — labeled an estimate, real-charge vs default-rates distinguished, and a billed-but-failed call still tells the user it was billed. Below-gate candidates skip the spend entirely.
- Same
checkgate as #355/#356, staging-only (never writes the file), and the api path refuses anything but a cleanend_turncompletion before the structure gate — which correctly handles prose-only prompts that have no markers to fail.
CI green, ruff + mypy clean. Ready to merge.
anilmurty
approved these changes
Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The write-the-summary half of the feature:
tj summarize prep --via claude-p|apiruns the rewrite for you in one shot. The last PR of the summarize stack.Summary
prep --via claude-p— drives your localclaude -p(headless). No key, with a 300s timeout so a stuck call can't wedge the CLI.prep --via api— calls Anthropic with your ownTJ_ANTHROPIC_API_KEY(any globalANTHROPIC_API_KEYis ignored) and the required[summarize] api_model(no default — only frontier models preserve structure). Reports a "pays for itself" amortization. Actual provider usage priced through the existing cost engine ÷ the estimated per-call saving.check→ stage, so structure stays a hard guarantee. The api path accepts only a clean completion (stop_reason == "end_turn"); truncated / refused / non-summary responses are refused before the structure gate — important for prose-only prompts with no markers to fail. A rejected-but-billed call will still state it was billed, user gets the failure reason.What's NOT in this PR
--via api(a provider catalog) + abase_url/gateway override — deferred; v1 is Anthropic-only. This is TJ's first outbound model call (everywhere else it observes), so there's no existing outbound-endpoint home and where that config should live long-term is an open question.tj cost— deferred.Tests / Verification
tests/unit/test_summarize_delivery.py+ theprep --viaCLI round-trips cover both paths: roundtrip/stage, below-gate-skips-spend, drift-refuse, claude-p timeout + not-installed, api no-key / no-model / unknown-model-labels-estimate / failed-structure-reports-cost / missing-usage-cost-unknown / truncation / non-end_turn refusal / malformed + non-dict JSON.unit/synthetic/agents/integration): 1328 passed, 2 skipped.ruff check tokenjam/+mypy tokenjam/clean.Checklist
ruff check tokenjam/+mypy tokenjam/cleanCloses #329.