Skip to content

fix(tenantctx): expire cached user context by CLI version and age - #610

Merged
anirudhwarrier merged 5 commits into
mainfrom
fix-caching-DEVSVCS-6566
Oct 8, 2026
Merged

anirudhwarrier merged 5 commits into
mainfrom
fix-caching-DEVSVCS-6566

Conversation

@ejacquier

@ejacquier ejacquier commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes DEVSVCS-6566: for bearer-token users the tenants cache (~/.cre/context.yaml) was only refreshed on cre login, so new environments or server-side tenant config changes stayed hidden until a manual re-login.

Each cached environment entry is now stamped with cli_version and fetched_at. On commands that load user context, bearer users refetch when:

  • the entry was written by a different CLI version (covers every upgrade path: cre update, Homebrew, install script, manual download, Windows), or
  • the entry is older than 24h (contextTTL), or
  • the entry has no stamp (cache files written before this change — refetched once).

API-key users are unchanged (always fetch). If a bearer refetch fails (e.g. offline), the stale cached copy is used, preserving today's behaviour.

An earlier iteration of this PR deleted the cache on token refresh and after cre update. That was replaced because it raced with concurrent cre processes (one process deleting context.yaml between another's existence check and read), never ran on Windows or for non-cre update installs, and discarded a valid cache on every token refresh.

Changes

  • internal/tenantctx: EnvironmentContext gains FetchedAt / CLIVersion (omitempty); FetchAndWriteContext stamps them; EnsureContext uses isFresh instead of an existence check and falls back to the cache on fetch failure. writeContextFile uses a unique temp file per write so concurrent refreshes can't clobber each other. ClearContext() added.
  • cmd/root.go: sets tenantctx.CLIVersion = version.Version (direct import would create a cmd/version → runtime → tenantctx cycle).
  • cmd/logout: uses tenantctx.ClearContext() instead of inline removal.

Breaking Changes

  • CLI commands, flags or output changed — no
  • context.yaml format changed incompatibly — no: two optional fields added; older CLIs ignore them, and pre-existing files are refetched once
  • Behaviour change visible to users — minor: bearer users make one extra user-context fetch per 24h and after each CLI version change

Testing Verification

  • New unit tests in internal/tenantctx/tenantctx_test.go:
    • TestFetchAndWriteContext_StampsVersionAndTime
    • TestEnsureContext_BearerRefetchesStaleCache (expired / different CLI version / pre-stamp file)
    • TestEnsureContext_BearerFallsBackToStaleCacheOnFetchError
    • TestClearContext_RemovesFile, TestClearContext_MissingFile
  • go test ./... -short -skip '<make test-quick skip list>' — all packages pass.
  • golangci-lint run on changed packages — 0 issues.
  • go build ./... and go vet on changed packages — clean.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown

👋 ejacquier, thanks for creating this pull request!

To help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team.

Once you're ready, you can mark it as "Ready for review" to request feedback. Thanks!

@ejacquier ejacquier changed the title Clear cache when cre update successfully runs Clear cache on token refresh & when cre update successfully runs Oct 5, 2026
tarcisiozf
tarcisiozf previously approved these changes Oct 6, 2026
anirudhwarrier and others added 2 commits October 6, 2026 22:57
- logout uses tenantctx.ClearContext instead of inline removal
- cache tests use testutil.IsolateCLIHome so Windows runs don't touch the real ~/.cre
- replace fakeJWT with testjwt helper; tighten doc comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stamp each context.yaml entry with cli_version and fetched_at; bearer
users refetch when the version differs or the entry is older than 24h,
falling back to the cached copy if the refetch fails.

This replaces deleting the cache on token refresh and after cre update,
which raced with concurrent cre processes, never ran on Windows or for
non-`cre update` installs, and discarded valid caches on every refresh.
Temp files are now unique per write so concurrent refreshes don't clash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anirudhwarrier anirudhwarrier changed the title Clear cache on token refresh & when cre update successfully runs fix(tenantctx): expire cached user context by CLI version and age Oct 8, 2026
@anirudhwarrier
anirudhwarrier added this pull request to the merge queue Oct 8, 2026
Merged via the queue into main with commit 46a18d6 Oct 8, 2026
22 checks passed
@anirudhwarrier
anirudhwarrier deleted the fix-caching-DEVSVCS-6566 branch October 8, 2026 14:33
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.

3 participants