Commit d6158fc
feat(tunnel): auto-relogin on session_expired during first connect
When the initial tunnel connection fails with session_expired (dead or
invalid refresh token), the CLI previously printed "please run
`ankimcp --login`" and exited, forcing a manual re-auth. Now, in an
interactive tunnel session, it runs the existing device-flow login
automatically and retries the connection once.
Behavior:
- On a first-connect session_expired, if stdout is a TTY and no relogin
has been attempted yet, run performLogin (prints URL + code, opens
browser, polls, saves creds) and retry connect() once with the fresh
credentials.
- Single-shot: a second session_expired, a failed login, a non-TTY run,
or any other error falls back to the previous error-and-exit. No loop.
- Scope is first-connect only; the mid-session error-event path is
unchanged. TunnelClient is untouched — this lives entirely in the
command layer.
Also extract the duplicated device-flow error reporting into a shared
reportLoginError(cli, error) helper, now used by handleLogin,
ensureCredentials, and the relogin path.
Tests: fix the now-stale ../perform-login module mocks to export
reportLoginError; add coverage for relogin success, single-shot bound,
login-failure, non-TTY gate, non-session errors, and reportLoginError.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent f675baa commit d6158fc
6 files changed
Lines changed: 448 additions & 64 deletions
File tree
- src/tunnel/commands
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
6 | | - | |
7 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
8 | 36 | | |
9 | 37 | | |
10 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
2 | 6 | | |
3 | 7 | | |
4 | 8 | | |
| |||
73 | 77 | | |
74 | 78 | | |
75 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
76 | 132 | | |
77 | 133 | | |
78 | 134 | | |
| |||
0 commit comments