Skip to content

fix(auth): keep the sign-in when a token refresh fails for a passing reason - #43

Merged
vigubikReply merged 1 commit into
mainfrom
dev/REPLY-55176-keep-sign-in
Nov 26, 2026
Merged

vigubikReply merged 1 commit into
mainfrom
dev/REPLY-55176-keep-sign-in

Conversation

@vigubikReply

Copy link
Copy Markdown
Contributor

What changed

A stored OAuth sign-in is no longer deleted when a token refresh fails for a passing reason, and reply processes
refreshing one expired sign-in together no longer sign each other out.

  • Only a definite refusal of the grant removes the record: 400 invalid_grant. Even then the record is
    removed only if the credentials file still holds the refused record, and the check is made on the same read
    that is rewritten. If another process stored a newer record in the meantime, that one is used and nothing is
    removed.

  • Every other failure keeps the record, and the code says whether to retry:

    Answer from the token endpoint Record Error Exit
    400 invalid_grant, file unchanged removed auth.refresh_failed 2
    another OAuth error at 400/401 (invalid_client, …) kept auth.refresh_failed 2
    429 kept rate_limited, with retry_after 1
    5xx, any other status, a non-OAuth body (for example an HTML maintenance page), a timeout (20 s), a network error, an unreadable answer, a lock wait that runs out kept reply.unavailable 1

    The --json error carries what the endpoint said under details.refresh: status and error, or a
    reason (timeout, network, unreadable, lock_wait). The top-level status is not set. No token is
    ever printed.

  • One refresh at a time across processes, through credentials.json.lock, created exclusively beside the
    file. It holds its holder's pid, host, pid namespace (on Linux), the time it was taken and a random id.

    • Before refreshing, a process reads the record again under the lock. If another process already
      refreshed it, that token is used and no request is sent.
    • A lock left by a process that is gone is taken over at once. A lock of any age that can't be judged by
      its pid (another host or another pid namespace) is taken over once it is older than 30 s. A live lock is
      waited on for up to 45 s.
    • A lock is only removed under a short guard file, after a re-read shows it is still the lock that was
      judged, so two processes never hold it at once. Letting go is best effort and never fails a command.
  • Every write to the credentials file is a read-modify-write under the lock: a refresh, auth login,
    auth logout, and a profile rename or delete. A rename is one locked step.

  • Writing the file: the new token is stored as soon as the answer is read. The write stays a temp file plus
    rename, the temp file now has a name of its own, and a rename that Windows refuses for a moment is retried.

  • A 2xx with a usable access_token is always stored. When expires_in or refresh_token is missing or
    of another type, the default lifetime and the previous refresh token are kept, as before. The server has
    already rotated the refresh token at that point, so dropping the answer would lose the sign-in.

  • Every token request sends the CLI's User-Agent: a refresh and a login's code exchange both send
    reply-cli/<version>, after REPLY_CLIENT when it is set, as API requests already do. Until now they went out
    as Node's default node, so the identity server's logs couldn't tell them apart.

  • Docs: CONTRIBUTING.md ("Credentials on disk", "Tests"), the README's auth section, and the reply-cli skill
    describe the new behaviour.

Why

The identity server issues this client one-time refresh tokens, and the access token lives one hour. A program
that runs several reply commands at once, typically right after it starts, finds the token expired in all of
them. Every command then sends the same refresh token. The server accepts the first and answers
invalid_grant to the rest, and each refused process deleted the stored record, including the new token the
winner had just saved. Any 429, 5xx, timeout or network error on a refresh deleted the sign-in the same way, and
the error didn't say which of these it was.

How it was verified

Tests first. Every new or changed test was run against the code before the change and failed. That
includes the two-process test, which ended with exit codes [2, 0] instead of [0, 0].

  • The two-process test (src/__tests__/auth/refresh-processes.test.ts): two built CLI processes run
    auth whoami on one expired sign-in, against a local fake identity server that rotates one-time refresh
    tokens and holds the first refresh until the second arrives.
    • Both end signed in, on one refresh, with the rotated record stored.
    • It runs in a temp config dir with every REPLY_* variable stripped, so a developer's real sign-in is never
      touched.
  • Unit tests cover each failure in the table above with the record kept byte for byte, the refusal path
    with and without a newer record, the lock (waiting, stale takeover, another host or pid namespace, a takeover
    raced by a peer, letting go when the unlink fails), and that no token string appears in any printed error.
  • The suite: npm run build && npm test gives 1108 passed, 2 skipped on Linux. On Windows 11 with Defender
    on, it gives 1105 passed, 5 skipped (the POSIX permission tests and the Linux-only filesystem tests).

Race probes against local fakes, on Linux and on Windows:

  • The lock alone: 10–16 processes taking it at one moment, on a stale lock and on none, gave no round with
    two holders.
  • The CLI: 8–12 processes on one expired sign-in, including on a stale lock and with a slow endpoint, gave
    one refresh per round, every process exiting 0, and no lock or temp file left behind. Against an endpoint that
    always answers 429 or 503, every process failed rate_limited or reply.unavailable, and the record was kept.
  • Pid namespaces (Linux): the same races with each process in its own pid namespace (bwrap --unshare-pid,
    unshare -p), with equal and with different pids, gave no round with two holders, and one refresh per round
    through the CLI.

Against Reply's dev identity server, with a test account:

  • Four processes refreshing one expired sign-in together, three rounds:
    • with this change, all four exited 0 on one refresh each round, and the sign-in stayed;
    • with v0.23.0, the server took one refresh and refused three with invalid_grant each round, and the sign-in
      was deleted every time, twice after the winning process had already stored its new token.
  • A reused refresh token is answered 400 invalid_grant, which this change reports as
    "details":{"refresh":{"status":400,"error":"invalid_grant"}}. Reusing it doesn't revoke the newer token,
    which is what the re-read relies on.
  • The server's traces show the User-Agent: reply-cli/<version> on this change's token requests, and node
    on v0.23.0's.

…reason

A stored OAuth sign-in was deleted whenever a token refresh failed, whatever the reason. A 429,
a 5xx, a timeout, a network error, or two reply processes refreshing one expired one-time refresh
token together all signed the person out, and an app that starts several reply commands at once
met the last one most of all.

- The stored record is removed only when the identity server refuses the grant (400
  invalid_grant) and the credentials file still holds the refused record, decided on the same
  read the removal rewrites. If another process stored a newer one meanwhile, that one is used
  and nothing is removed.
- Another OAuth refusal (400 or 401 with an error) keeps the record and fails
  auth.refresh_failed, exit 2. A 429 keeps it and fails rate_limited with retry_after. Any other
  answer, a timeout (20 s), a network error or a 2xx without an access token keeps it and fails
  reply.unavailable, exit 1.
- One refresh at a time across processes: credentials.json.lock, created exclusively beside the
  file, holds its holder's pid, host, pid namespace and the time it was taken. A lock older than
  30 s, or whose holder is gone, is taken over; a live one is waited on for up to 45 s. The pid
  is trusted only on the same host and in the same pid namespace, since sandboxes that give each
  command a namespace of its own give them equal pids. A lock is only removed, when let go or
  taken over, under a guard file after reading it again, so no two processes hold it at once.
  Letting go never fails the command. Under the lock the record is read again, and a token
  another process already refreshed is used with no request.
- Every write to the file (a refresh, login, logout, a profile rename or delete) is a
  read-modify-write under the lock; a rename moves the credential in one locked step. The new
  token is stored as soon as the answer is read, by the same temp-file rename, now retried
  briefly when Windows refuses it for a moment.
- The error keeps what the token endpoint said under details.refresh: status and error, or a
  reason (timeout, network, unreadable, lock_wait). No token is ever printed.
- Every token request, a refresh or a login's code exchange, sends the CLI's User-Agent, as API
  requests do, instead of Node's default, so the identity server's logs can tell them apart.

A test runs two built CLI processes against a local fake identity server that rotates one-time
refresh tokens and holds the first refresh until the second arrives: both end signed in, on one
refresh.

@ArtemKosolap ArtemKosolap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving. The design holds: a record is removed only on a definite refusal of the grant and only while the file still holds the refused record, every other failure keeps it and says whether a repeat makes sense, and one refresh at a time closes the race that was deleting sign-ins. The two codes a passing failure now answers (reply.unavailable, rate_limited) are the ones a caller already retries, so nothing on a consumer's side has to move.

Read: the description, src/credentials/lock.ts whole, the changes to src/auth/resolve.ts, src/credentials/file-store.ts, src/auth/oauth-flow.ts, src/auth/token.ts and src/commands/profile.ts, and the callers of resolve_credential. Not read line by line: the tests and the docs. CI is green on both systems.

Three small notes, none blocking; each can be a follow-up.

  1. The branch with no refresh function in resolve_credential (src/auth/resolve.ts) still removes the record unconditionally, outside the re-read. Both production callers pass refresh, so it is dead in the product today, but it is the one path left that contradicts "only a definite refusal removes the record": a caller without a refresh function, started beside one that is refreshing, would wait for the lock and then delete the record the other has just stored. Keeping the record there, or passing the same only_if the other branches pass, would close it.

  2. The rename in FileCredentialStore.write (src/credentials/file-store.ts). After a successful refresh the server has already spent the old refresh token, so this rename is the one step that must not fail. The retries add up to about 0.6 s. If Windows refuses the rename for longer than that (a scanner or a backup holding the file), the new token is dropped; the next run sends the spent one, is refused with invalid_grant on an unchanged file, and removes the sign-in. A longer backoff for this write would make that window much smaller.

  3. Taking over a stale guard in try_guard (src/credentials/lock.ts) is a read, a compare and an unlink with nothing serialising them. In principle a process can unlink a fresh guard that a peer created right after another peer removed the stale one, and two processes then pass "read the lock again, then unlink" together. It needs a process that died holding the guard and three others inside the same instant, and the worst outcome is two refreshes at once, which the re-read in refresh_under_lock already absorbs. Not worth fixing while the lock guards only this; a sentence in the header comment saying so would be enough.

One thing for after the merge, outside this repository: a program that requires a minimum version of the CLI keeps running the older one until that minimum is raised. Jason's Reply package pins one, so the fix reaches those installations when the package's minimum moves to the release that carries it.

@vigubikReply
vigubikReply merged commit b8aba5e into main Nov 26, 2026
6 checks passed
@vigubikReply
vigubikReply deleted the dev/REPLY-55176-keep-sign-in branch November 26, 2026 01:58
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