Skip to content

fix: refresh an expired DoiT session before requesting AI access - #214

Open
apgiorgi wants to merge 3 commits into
mainfrom
apgiorgi/ai-vend-refresh
Open

apgiorgi wants to merge 3 commits into
mainfrom
apgiorgi/ai-vend-refresh

Conversation

@apgiorgi

@apgiorgi apgiorgi commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With DoiT-provided access, dci ai asks the DoiT API for a temporary Anthropic token. That request read the cached DoiT access token straight from the cache and never refreshed it, unlike every other request, which goes through restish's auth handler. Once the token expired (after an hour), dci ai failed with "your DoiT session has expired — run dci login", although the stored refresh token was still valid. Running any ordinary command refreshed it and made dci ai work again.

Fix

aiDCIBearer (ai_credentials.go) returns the bearer for that request, renewing an expired one first:

  • DCI_API_KEY is returned as-is, with no refresh.
  • Session state comes from disk only. cache.json is loaded into a private viper, and the refreshed token is written back through it. cli.Cache is never touched, so a /login, account switch or logout done by a child process always wins, and the TUI picks the new token up on its next reload. Restish's oauth.TokenHandler is deliberately not used: it stores into viper's override layer, which ReadInConfig never clears, and it would write from the session goroutine while the TUI reads cli.Cache.
  • The refresh is a single request, the same form restish's RefreshTokenSource sends, bounded by the turn's context (Esc cancels it) and a 15s timeout. It never falls back to the browser login.
  • Errors: a 4xx from the token endpoint (other than 408 and 429) shows the existing "run dci login" message. A network failure, timeout, 408, 429 or 5xx says the sign-in service could not be reached, and a cache write failure is reported as its own error.
  • OAuth params (token_url, client_id, scopes, extra params) are read from the dci profile in apis.json, like restish's handler. The writeConfig values, now shared constants in main.go, are the fallback.
  • A token with no recorded expiry is sent unchanged, as before.

Test plan

  • Unit tests against a stand-in token endpoint (TestAIDCIBearerRefresh, 15 cases):
    • an expired token refreshes, the new bearer reaches the AI access request and is written to the cache;
    • a rejected refresh asks for dci login;
    • no refresh token asks for dci login without dialing;
    • a valid token is reused;
    • a token with no recorded expiry is sent unchanged;
    • a token another process renewed on disk is picked up;
    • after a refresh, a later login and logout on disk still win;
    • a stalled endpoint returns on cancel, and on timeout;
    • a 502, 429 or 408 is not reported as an expired session;
    • a response without a refresh token keeps the old one and other cache keys;
    • apis.json params are honored;
    • DCI_API_KEY is never refreshed.
  • go vet ., go test ., go test -race on the AI tests
  • Live, on the final commit: with the real cached session expired, dci ai --quiet with this build answered. The cached expiry moved forward one hour, cache.json stayed 0600, and a following dci list-budgets used the renewed token without refreshing again.

The DoiT-provided AI access request read the cached DoiT token directly
and never ran restish's auth handler, so once the access token expired it
got a 401 and told the user to run dci login, even though the stored
refresh token was still valid. Refresh the token first, the same way other
requests do, without ever falling back to the browser login.

@taltultc taltultc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test covers the refreshed bearer reaching the AI access request. Please address the two inline findings before merge.

Comment thread ai_credentials.go Outdated
if cli.Cache == nil {
return "", nil
}
reloadCredentialCache()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Credential cache

After the parent refreshes once, Viper keeps returning its bearer even when a child updates the cache. This also leaves the old bearer in use after logout or an account switch. Please honor the disk change and add a regression test for that sequence.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e94f226. The refresh no longer uses restish's TokenHandler and never touches cli.Cache. It loads cache.json into a private viper and writes the renewed token back through it, so nothing lands in the override layer and a later login or logout on disk always wins. Regression test: "after a refresh, a login or logout on disk still wins" (refresh, then a child login, then a child logout).

Comment thread ai_credentials.go Outdated
if err != nil {
return "", err
}
if err := oauth.TokenHandler(source, key, probe); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Refresh cancellation

A stalled token endpoint keeps the turn waiting even after cancellation. Please pass the turn context to the refresh HTTP request and give it a timeout.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e94f226. The refresh is now our own request, built with NewRequestWithContext on the turn's ctx plus a 15s timeout (aiDCIRefreshTimeout). Tests cover a stalled endpoint returning on cancel and on timeout.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the inline findings, I also checked whether hard-coding dci:default in aiDCIBearer breaks users on a non-default restish profile. It doesn't: main.go pins rsh-profile to "default" (line 785) and rejects --profile/--rsh-profile arguments (line 935), so dci:default is the only cache key a session can have.

Extended reasoning...

One candidate was ruled out: the hard-coded dci:default key can't miss a profile's token, because the CLI forces the restish profile to "default" and rejects the profile flags.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread ai_credentials.go Resolved
Comment thread ai_credentials.go Resolved
Comment thread ai_credentials.go Resolved
Comment thread ai_credentials.go Resolved
Comment thread ai_credentials.go Resolved
Comment thread ai_credentials.go Resolved
Address review on #214: the refresh no longer writes into cli.Cache's
viper override layer (which shadowed later logins and logouts on disk),
is bounded by the turn context and a 15s timeout, honors the OAuth params
in apis.json, short-circuits DCI_API_KEY, and reports token-endpoint
outages separately from an expired session.
@apgiorgi
apgiorgi requested a review from taltultc November 26, 2026 10:04

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the new commit, which replaces restish's TokenHandler with a direct refresh, and found no bugs. It is OAuth session-refresh code and another reviewer's change request is still open, so a human should look at it too.

What was reviewed:

  • aiDCIBearer: the DCI_API_KEY check now runs first, the cache is read from and written to a scratch viper, and cli.Cache's override layer is never touched.
  • aiRefreshDCIToken: limited by ctx plus a 15s timeout, 4xx and other failures map to separate errors, and the form fields match restish v0.21.2's refresh request.
  • aiDCIOAuthParams reads token_url, client_id, scopes and extra params from apis.json and falls back to the main.go constants.
Extended reasoning...

The change touches ai_credentials.go, its tests, and the OAuth constants in main.go. It adds a refresh_token POST to the DoiT token endpoint and writes the renewed tokens back to restish's cache.json, which makes it authentication and credential-storage code. The latest commit rewrites the refresh path and appears to address the earlier inline concerns about the override layer, the missing timeout, apis.json params, error messages and the API-key path. A CHANGES_REQUESTED review from another reviewer predates that commit, and only that reviewer can confirm it is resolved, so approval is not appropriate.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new blocking issues. 1 optional suggestion (a nit or a note on pre-existing code) was found and not posted. Nothing in this review needs a push before merging.

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.

2 participants