Skip to content

fix: delete leftover files from the cache dir on refresh - #150

Merged
leet-c1 merged 7 commits into
mainfrom
fix/prune-leftover-cache
Oct 9, 2026
Merged

leet-c1 merged 7 commits into
mainfrom
fix/prune-leftover-cache

Conversation

@leet-c1

@leet-c1 leet-c1 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Why

A release that renames or drops a cache file leaves the old one in ~/.c1i/cache/ forever. The rename in #148 (openapi.yaml → api-openapi.yaml) would strand ~0.9 MB per machine. This replaces a cleanup rule per file with one general rule.

What

  • cmd/cache.go holds an allowlist, cacheFiles, of every file the cache dir is meant to contain. Today that is just cacheFileName.
  • After a successful OpenAPI cache write, pruneCacheDir deletes every other file in that dir. The write happens at most once per 24h.
  • Because the allowlist reads the constant, fix: use the current public OpenAPI source for API discovery #148's rename removes the old file with no extra code, whichever PR merges first.
  • The sweep is best-effort and never fails the command. It does not run on a cache read or a failed fetch.
  • It skips:
    • subdirectories;
    • a cache dir that is a symlink, whose target may hold files c1i doesn't own;
    • a relative cache path, which is what an unset HOME produces and which would sweep the working directory.
  • README and cmd/agents.md now say not to keep your own files in ~/.c1i/cache/.

What c1i writes, and what this touches

Path Kind Swept
~/.c1i/cache/* OpenAPI spec cache yes, the only dir swept
~/.c1i.yaml config no
<UserConfigDir>/c1i/credentials/ credential file fallback no
<UserConfigDir>/c1i/tokens/ access-token cache, keyed by a hash of the credentials no
~/.sigstore/root/ Sigstore TUF trust-root cache, written by upgrade through sigstore-go's default path and shared with other Sigstore tools no
.c1i-upgrade-* beside the binary upgrade staging file, removed by upgrade itself no

I chose not to add a cache clear command. Users would have to know to run it, and the automatic sweep already covers leftovers.

Known trade-off: two different c1i versions on one machine that use different cache names delete each other's file. The cost is one refetch per refresh and no offline fallback for the other version.

Verification

  • TestCacheWritePrunesLeftoverFiles drives docs openapi end to end with a stubbed transport. TestCachePruneSkipsSymlinkedCacheDir and TestCachePruneSkipsRelativeCacheDir cover the two guards.
  • Each test failed before its fix. The symlink test also fails when Lstat is swapped for Stat.
  • Live: ran a built binary against the real spec endpoint with a temporary HOME containing a stray file. The stray file was deleted and the fresh spec was kept.
  • Passing: go build, go vet, go test -count=1 -shuffle=on, golangci-lint, gosec, go mod tidy diff, gitleaks, and govulncheck (all three GOOS) after merging main with Build with Go 1.27.2 and bump x/net to fix new advisories #149's Go 1.27.2 / x/net bumps.

The CHANGELOG entry will conflict trivially with #148.

🤖 Generated with Claude Code

leet-c1 and others added 7 commits October 9, 2026 00:44
A release that renames or drops a cache file left the old file in
~/.c1i/cache forever. Instead of a cleanup rule per file, list the files
the cache is supposed to hold and delete anything else there whenever the
OpenAPI cache is rewritten (at most once per 24h). Subdirectories are
skipped, and so is a cache dir that is a symlink, whose target may hold
files c1i doesn't own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With HOME unset the cache path resolves against the working directory, so
the sweep would delete files there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n Windows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep both CHANGELOG entries. The sweep test now plants the real pre-rename
openapi.yaml as the leftover, since the cache file is api-openapi.yaml.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…siblings

Plant a symlink entry, a file inside a subdirectory, and state beside the
cache dir. The sweep must remove only the link. Also restore the docs
openapi command's output and context after each test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leet-c1
leet-c1 merged commit 0f21c0a into main Oct 9, 2026
2 checks passed
@leet-c1
leet-c1 deleted the fix/prune-leftover-cache branch October 9, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant