1.0.1: relay the CLI's exists error and add Re-login
A node that already holds Copilot credentials gets {ok:false,error:exists} from --start. The plugin reported that as 'did not return a device code', which hid the only actionable hint (force=1). Relay the CLI's error and detail verbatim, add a Re-login (replace) button, and pin the behaviour in the mock CLI and the offline suite (119 assertions).
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-12 18:12:37
Commit6a902f70
Parentsabfd69aa
Changed files
README.md+1−1■■
release-dist.sh+18−4■■■■■
src/plugin_copilot_login.c+12−4■■■■■
src/ui.html+6−4■■■■■
src/ui_html.h+6−4■■■■■
tests/mock_login.sh+6−0■■■■■
tests/run.sh+15−1■■■■■
7 files+64−18
Diff
README.md+1−1■■
index 1ada108..ebf7e5b 100644
--- a/README.md
+++ b/README.md
@@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json
5858
5959 mkdir -p ~/.scorpiox/plugins
6060 curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \
61- https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.0/linux-x86_64-musl/plugin_copilot_login
61+ https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.1/linux-x86_64-musl/plugin_copilot_login
6262 chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login
6363 ```
6464
release-dist.sh+18−4■■■■■
index 5380b2c..1db0bff 100755
--- a/release-dist.sh
+++ b/release-dist.sh
@@ -31,7 +31,7 @@ set -euo pipefail
3131
3232 SLUG="copilot-login"
3333 ID="plugin_copilot_login"
34-VERSION="1.0.0"
34+VERSION="1.0.1"
3535 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"
3636
3737 ROOT="$(cd "$(dirname "$0")" && pwd)"
@@ -339,11 +339,25 @@ PY" || die "index.json merge failed"
339339
340340 # ---------------------------------------------------------------- 6. mirror
341341 say "6/7 plugingit mirror"
342-if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
342+# The native plugingit server needs three things a plain `git push` gets wrong:
343+# the create call must carry a body and an `Authorization: token` header, the
344+# push URL user must be the literal `token`, and HTTP/2 drops the request with
345+# PROTOCOL_ERROR. scorpiox-cli-botplugin knows all three, so prefer it.
346+if [ "$DO_MIRROR" -eq 1 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
347+ scorpiox-cli-botplugin mirror "$ROOT" \
348+ --public-name "scorpiox-bot-plugin-${SLUG}" \
349+ --plugingit-url "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}" \
350+ ${PLUGINGIT_TOKEN_FILE:+--token-file "$PLUGINGIT_TOKEN_FILE"} \
351+ || die "mirror push failed"
352+ echo " mirrored to $PLUGINGIT_REMOTE"
353+elif [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
343354 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")
344355 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|")
345- git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed"
346- git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
356+ curl -s --http1.1 -X POST -H "Authorization: token ${TOKEN}" --data '' \
357+ "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}/api?r=repos&name=scorpiox-bot-plugin-${SLUG}" \
358+ | sed 's/^/ /' || true
359+ git -c http.version=HTTP/1.1 push "$AUTH_REMOTE" HEAD:refs/heads/main 2>&1 | sed 's/^/ /' || die "mirror push failed"
360+ git -c http.version=HTTP/1.1 push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
347361 echo " mirrored to $PLUGINGIT_REMOTE"
348362 else
349363 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"
src/plugin_copilot_login.c+12−4■■■■■
index 8c68ea2..6e9aa3e 100644
--- a/src/plugin_copilot_login.c
+++ b/src/plugin_copilot_login.c
@@ -81,7 +81,7 @@
8181
8282 #define PLUGIN_SLUG "copilot-login"
8383 #define PLUGIN_ID "plugin_copilot_login"
84-#define PLUGIN_VER "1.0.0"
84+#define PLUGIN_VER "1.0.1"
8585 #define PLUGIN_NAME "copilot"
8686 #define PLUGIN_DESC "Copilot login"
8787
@@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force)
12871287 interval = json_num_field(j, end, "interval");
12881288 expires_in = json_num_field(j, end, "expires_in");
12891289 if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) {
1290+ char err[FIELD_SZ] = {0};
12901291 char detail[FIELD_SZ] = {0};
1291- json_str_field(j, end, "error", detail, sizeof(detail));
1292+ json_str_field(j, end, "error", err, sizeof(err));
1293+ json_str_field(j, end, "detail", detail, sizeof(detail));
12921294 buf_free(&out);
1293- emit_error(200, "OK", detail[0] ? detail : "start_failed",
1294- "the login CLI did not return a device code");
1295+ /* The CLI's own words beat ours: "exists" means the node already
1296+ * has a token and the caller needs force=1, and a user staring at
1297+ * "did not return a device code" would never guess that. */
1298+ emit_error(200, "OK", err[0] ? err : "start_failed",
1299+ (err[0] && strcmp(err, "exists") == 0)
1300+ ? "this node already has Copilot credentials; use Re-login to replace them"
1301+ : detail[0] ? detail
1302+ : "the login CLI did not return a device code");
12951303 return 1;
12961304 }
12971305 }
src/ui.html+6−4■■■■■
index 113d374..dfb6433 100644
--- a/src/ui.html
+++ b/src/ui.html
@@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none}
127127 <div class="step on" id="step1">
128128 <div class="row">
129129 <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button>
130+ <button class="btn ghost" id="btnRelogin" type="button">Re-login (replace)</button>
130131 </div>
131132 <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve
132133 it in the browser.</p>
@@ -326,11 +327,11 @@ function startPolling(){
326327 },delay);
327328 }
328329
329-function startLogin(){
330- var b=$('btnStart');
330+function startLogin(force){
331+ var b=force?$('btnRelogin'):$('btnStart');
331332 b.disabled=true;b.classList.add('loading');
332333 note($('startNote'),'Asking GitHub for a device code...');
333- api('start').then(function(d){
334+ api('start',force?'force=1':'').then(function(d){
334335 b.disabled=false;b.classList.remove('loading');
335336 note($('startNote'),'');
336337 if(!d||!d.ok){
@@ -397,7 +398,8 @@ function refreshToken(){
397398 });
398399 }
399400
400-$('btnStart').onclick=startLogin;
401+$('btnStart').onclick=function(){startLogin(false);};
402+$('btnRelogin').onclick=function(){startLogin(true);};
401403 $('btnCopyOpen').onclick=copyOpen;
402404 $('btnCancel').onclick=cancelLogin;
403405 $('btnRefresh').onclick=refreshToken;
src/ui_html.h+6−4■■■■■
index 97db5b6..d089dea 100644
--- a/src/ui_html.h
+++ b/src/ui_html.h
@@ -132,6 +132,7 @@ static const char *UI_HTML =
132132 " <div class=\"step on\" id=\"step1\">\n"
133133 " <div class=\"row\">\n"
134134 " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n"
135+ " <button class=\"btn ghost\" id=\"btnRelogin\" type=\"button\">Re-login (replace)</button>\n"
135136 " </div>\n"
136137 " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n"
137138 " it in the browser.</p>\n"
@@ -331,11 +332,11 @@ static const char *UI_HTML =
331332 " },delay);\n"
332333 "}\n"
333334 "\n"
334- "function startLogin(){\n"
335- " var b=$('btnStart');\n"
335+ "function startLogin(force){\n"
336+ " var b=force?$('btnRelogin'):$('btnStart');\n"
336337 " b.disabled=true;b.classList.add('loading');\n"
337338 " note($('startNote'),'Asking GitHub for a device code...');\n"
338- " api('start').then(function(d){\n"
339+ " api('start',force?'force=1':'').then(function(d){\n"
339340 " b.disabled=false;b.classList.remove('loading');\n"
340341 " note($('startNote'),'');\n"
341342 " if(!d||!d.ok){\n"
@@ -402,7 +403,8 @@ static const char *UI_HTML =
402403 " });\n"
403404 "}\n"
404405 "\n"
405- "$('btnStart').onclick=startLogin;\n"
406+ "$('btnStart').onclick=function(){startLogin(false);};\n"
407+ "$('btnRelogin').onclick=function(){startLogin(true);};\n"
406408 "$('btnCopyOpen').onclick=copyOpen;\n"
407409 "$('btnCancel').onclick=cancelLogin;\n"
408410 "$('btnRefresh').onclick=refreshToken;\n"
tests/mock_login.sh+6−0■■■■■
index dd28924..d8214ef 100755
--- a/tests/mock_login.sh
+++ b/tests/mock_login.sh
@@ -77,6 +77,12 @@ case "$MODE" in
7777 printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n'
7878 exit 1
7979 fi
80+ # A node that already holds a token refuses a second login without --force,
81+ # exactly like the real CLI.
82+ if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then
83+ printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
84+ exit 1
85+ fi
8086 p=$(pending_file "$NAME")
8187 mkdir -p "$(dirname "$p")" 2>/dev/null || true
8288 printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p"
tests/run.sh+15−1■■■■■
index fb0572f..e344ddd 100755
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME"
236236 # ---------------------------------------------------------------- start
237237 section "action=start"
238238 rm -f "$FIX/pend"/*.json "$FIX/mock"/*
239+rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json"
239240 OUT=$(api "action=start&$PWDQ")
240241 json_ok "start returns ok" "$OUT" 'd["ok"] is True'
241242 json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"'
@@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)"
247248 OUT=$(api "action=status&$PWDQ")
248249 json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True'
249250 check_not "start never returns a token" 'SECRET' "$OUT"
251+# A node that already holds credentials: the CLI answers "exists", not "no
252+# code". The plugin must relay that word, because it is the only hint that
253+# force=1 (the Re-login button) is what the user needs.
254+api "action=cancel&$PWDQ" >/dev/null
255+mkcreds "$FIX/.credentials.json"
256+OUT=$(api "action=start&$PWDQ")
257+check "start relays the CLI's exists error verbatim" '"error":"exists"' "$OUT"
258+check "the exists error tells the user to re-login" 'Re-login' "$OUT"
259+OUT=$(api "action=start&force=1&$PWDQ")
260+json_ok "start with force=1 replaces the credentials" "$OUT" 'd["ok"] is True and d["user_code"]=="1234-5678"'
261+api "action=cancel&$PWDQ" >/dev/null
250262
251263 # ---------------------------------------------------------------- poll
252264 section "action=poll"
265+rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json"
266+api "action=start&$PWDQ" >/dev/null
253267 OUT=$(api "action=poll&$PWDQ")
254268 json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True'
255269 json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"'
@@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr
263277 check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT"
264278
265279 : > "$FIX/mock/poll_denied"
266-api "action=start&$PWDQ" >/dev/null
280+api "action=start&force=1&$PWDQ" >/dev/null
267281 OUT=$(api "action=poll&$PWDQ")
268282 check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT"
269283 OUT=$(api "action=poll&$PWDQ")
270284
README.md+1−1■■
index 1ada108..ebf7e5b 100644index 1ada108..ebf7e5b 100644
--- a/README.md--- a/README.md
+++ b/README.md+++ b/README.md
@@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json@@ -58,7 +58,7 @@ curl -fsSL https://dist.scorpiox.net/bot-plugins/copilot-login/latest.json
58 58
59 mkdir -p ~/.scorpiox/plugins59 mkdir -p ~/.scorpiox/plugins
60 curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \60 curl -fsSL -o ~/.scorpiox/plugins/plugin_copilot_login \
61- https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.0/linux-x86_64-musl/plugin_copilot_login61+ https://dist.scorpiox.net/bot-plugins/copilot-login/1.0.1/linux-x86_64-musl/plugin_copilot_login
62 chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login62 chmod 0755 ~/.scorpiox/plugins/plugin_copilot_login
63 ```63 ```
64 64
release-dist.sh+18−4■■■■■
index 5380b2c..1db0bff 100755index 5380b2c..1db0bff 100755
--- a/release-dist.sh--- a/release-dist.sh
+++ b/release-dist.sh+++ b/release-dist.sh
@@ -31,7 +31,7 @@ set -euo pipefail@@ -31,7 +31,7 @@ set -euo pipefail
31 31
32 SLUG="copilot-login"32 SLUG="copilot-login"
33 ID="plugin_copilot_login"33 ID="plugin_copilot_login"
34-VERSION="1.0.0"34+VERSION="1.0.1"
35 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"35 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"
36 36
37 ROOT="$(cd "$(dirname "$0")" && pwd)"37 ROOT="$(cd "$(dirname "$0")" && pwd)"
@@ -339,11 +339,25 @@ PY" || die "index.json merge failed"@@ -339,11 +339,25 @@ PY" || die "index.json merge failed"
339 339
340 # ---------------------------------------------------------------- 6. mirror340 # ---------------------------------------------------------------- 6. mirror
341 say "6/7 plugingit mirror"341 say "6/7 plugingit mirror"
342-if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then342+# The native plugingit server needs three things a plain `git push` gets wrong:
343+# the create call must carry a body and an `Authorization: token` header, the
344+# push URL user must be the literal `token`, and HTTP/2 drops the request with
345+# PROTOCOL_ERROR. scorpiox-cli-botplugin knows all three, so prefer it.
346+if [ "$DO_MIRROR" -eq 1 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
347+ scorpiox-cli-botplugin mirror "$ROOT" \
348+ --public-name "scorpiox-bot-plugin-${SLUG}" \
349+ --plugingit-url "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}" \
350+ ${PLUGINGIT_TOKEN_FILE:+--token-file "$PLUGINGIT_TOKEN_FILE"} \
351+ || die "mirror push failed"
352+ echo " mirrored to $PLUGINGIT_REMOTE"
353+elif [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then
343 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")354 TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE")
344 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|")355 AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|")
345- git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed"356+ curl -s --http1.1 -X POST -H "Authorization: token ${TOKEN}" --data '' \
346- git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true357+ "${PLUGINGIT_URL:-https://plugingit.scorpiox.net}/api?r=repos&name=scorpiox-bot-plugin-${SLUG}" \
358+ | sed 's/^/ /' || true
359+ git -c http.version=HTTP/1.1 push "$AUTH_REMOTE" HEAD:refs/heads/main 2>&1 | sed 's/^/ /' || die "mirror push failed"
360+ git -c http.version=HTTP/1.1 push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true
347 echo " mirrored to $PLUGINGIT_REMOTE"361 echo " mirrored to $PLUGINGIT_REMOTE"
348 else362 else
349 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"363 echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)"
src/plugin_copilot_login.c+12−4■■■■■
index 8c68ea2..6e9aa3e 100644index 8c68ea2..6e9aa3e 100644
--- a/src/plugin_copilot_login.c--- a/src/plugin_copilot_login.c
+++ b/src/plugin_copilot_login.c+++ b/src/plugin_copilot_login.c
@@ -81,7 +81,7 @@@@ -81,7 +81,7 @@
81 81
82 #define PLUGIN_SLUG "copilot-login"82 #define PLUGIN_SLUG "copilot-login"
83 #define PLUGIN_ID "plugin_copilot_login"83 #define PLUGIN_ID "plugin_copilot_login"
84-#define PLUGIN_VER "1.0.0"84+#define PLUGIN_VER "1.0.1"
85 #define PLUGIN_NAME "copilot"85 #define PLUGIN_NAME "copilot"
86 #define PLUGIN_DESC "Copilot login"86 #define PLUGIN_DESC "Copilot login"
87 87
@@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force)@@ -1287,11 +1287,19 @@ static int action_start(const char *name, int force)
1287 interval = json_num_field(j, end, "interval");1287 interval = json_num_field(j, end, "interval");
1288 expires_in = json_num_field(j, end, "expires_in");1288 expires_in = json_num_field(j, end, "expires_in");
1289 if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) {1289 if (!json_bool_field(j, end, "ok", 1) || !user_code[0]) {
1290+ char err[FIELD_SZ] = {0};
1290 char detail[FIELD_SZ] = {0};1291 char detail[FIELD_SZ] = {0};
1291- json_str_field(j, end, "error", detail, sizeof(detail));1292+ json_str_field(j, end, "error", err, sizeof(err));
1293+ json_str_field(j, end, "detail", detail, sizeof(detail));
1292 buf_free(&out);1294 buf_free(&out);
1293- emit_error(200, "OK", detail[0] ? detail : "start_failed",1295+ /* The CLI's own words beat ours: "exists" means the node already
1294- "the login CLI did not return a device code");1296+ * has a token and the caller needs force=1, and a user staring at
1297+ * "did not return a device code" would never guess that. */
1298+ emit_error(200, "OK", err[0] ? err : "start_failed",
1299+ (err[0] && strcmp(err, "exists") == 0)
1300+ ? "this node already has Copilot credentials; use Re-login to replace them"
1301+ : detail[0] ? detail
1302+ : "the login CLI did not return a device code");
1295 return 1;1303 return 1;
1296 }1304 }
1297 }1305 }
src/ui.html+6−4■■■■■
index 113d374..dfb6433 100644index 113d374..dfb6433 100644
--- a/src/ui.html--- a/src/ui.html
+++ b/src/ui.html+++ b/src/ui.html
@@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none}@@ -127,6 +127,7 @@ a.btn{display:inline-flex;align-items:center;gap:6px;text-decoration:none}
127 <div class="step on" id="step1">127 <div class="step on" id="step1">
128 <div class="row">128 <div class="row">
129 <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button>129 <button class="btn" id="btnStart" type="button">Start GitHub Copilot Login</button>
130+ <button class="btn ghost" id="btnRelogin" type="button">Re-login (replace)</button>
130 </div>131 </div>
131 <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve132 <p class="hint">This asks GitHub for a one-time code. Nothing is stored until you approve
132 it in the browser.</p>133 it in the browser.</p>
@@ -326,11 +327,11 @@ function startPolling(){@@ -326,11 +327,11 @@ function startPolling(){
326 },delay);327 },delay);
327 }328 }
328 329
329-function startLogin(){330+function startLogin(force){
330- var b=$('btnStart');331+ var b=force?$('btnRelogin'):$('btnStart');
331 b.disabled=true;b.classList.add('loading');332 b.disabled=true;b.classList.add('loading');
332 note($('startNote'),'Asking GitHub for a device code...');333 note($('startNote'),'Asking GitHub for a device code...');
333- api('start').then(function(d){334+ api('start',force?'force=1':'').then(function(d){
334 b.disabled=false;b.classList.remove('loading');335 b.disabled=false;b.classList.remove('loading');
335 note($('startNote'),'');336 note($('startNote'),'');
336 if(!d||!d.ok){337 if(!d||!d.ok){
@@ -397,7 +398,8 @@ function refreshToken(){@@ -397,7 +398,8 @@ function refreshToken(){
397 });398 });
398 }399 }
399 400
400-$('btnStart').onclick=startLogin;401+$('btnStart').onclick=function(){startLogin(false);};
402+$('btnRelogin').onclick=function(){startLogin(true);};
401 $('btnCopyOpen').onclick=copyOpen;403 $('btnCopyOpen').onclick=copyOpen;
402 $('btnCancel').onclick=cancelLogin;404 $('btnCancel').onclick=cancelLogin;
403 $('btnRefresh').onclick=refreshToken;405 $('btnRefresh').onclick=refreshToken;
src/ui_html.h+6−4■■■■■
index 97db5b6..d089dea 100644index 97db5b6..d089dea 100644
--- a/src/ui_html.h--- a/src/ui_html.h
+++ b/src/ui_html.h+++ b/src/ui_html.h
@@ -132,6 +132,7 @@ static const char *UI_HTML =@@ -132,6 +132,7 @@ static const char *UI_HTML =
132 " <div class=\"step on\" id=\"step1\">\n"132 " <div class=\"step on\" id=\"step1\">\n"
133 " <div class=\"row\">\n"133 " <div class=\"row\">\n"
134 " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n"134 " <button class=\"btn\" id=\"btnStart\" type=\"button\">Start GitHub Copilot Login</button>\n"
135+ " <button class=\"btn ghost\" id=\"btnRelogin\" type=\"button\">Re-login (replace)</button>\n"
135 " </div>\n"136 " </div>\n"
136 " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n"137 " <p class=\"hint\">This asks GitHub for a one-time code. Nothing is stored until you approve\n"
137 " it in the browser.</p>\n"138 " it in the browser.</p>\n"
@@ -331,11 +332,11 @@ static const char *UI_HTML =@@ -331,11 +332,11 @@ static const char *UI_HTML =
331 " },delay);\n"332 " },delay);\n"
332 "}\n"333 "}\n"
333 "\n"334 "\n"
334- "function startLogin(){\n"335+ "function startLogin(force){\n"
335- " var b=$('btnStart');\n"336+ " var b=force?$('btnRelogin'):$('btnStart');\n"
336 " b.disabled=true;b.classList.add('loading');\n"337 " b.disabled=true;b.classList.add('loading');\n"
337 " note($('startNote'),'Asking GitHub for a device code...');\n"338 " note($('startNote'),'Asking GitHub for a device code...');\n"
338- " api('start').then(function(d){\n"339+ " api('start',force?'force=1':'').then(function(d){\n"
339 " b.disabled=false;b.classList.remove('loading');\n"340 " b.disabled=false;b.classList.remove('loading');\n"
340 " note($('startNote'),'');\n"341 " note($('startNote'),'');\n"
341 " if(!d||!d.ok){\n"342 " if(!d||!d.ok){\n"
@@ -402,7 +403,8 @@ static const char *UI_HTML =@@ -402,7 +403,8 @@ static const char *UI_HTML =
402 " });\n"403 " });\n"
403 "}\n"404 "}\n"
404 "\n"405 "\n"
405- "$('btnStart').onclick=startLogin;\n"406+ "$('btnStart').onclick=function(){startLogin(false);};\n"
407+ "$('btnRelogin').onclick=function(){startLogin(true);};\n"
406 "$('btnCopyOpen').onclick=copyOpen;\n"408 "$('btnCopyOpen').onclick=copyOpen;\n"
407 "$('btnCancel').onclick=cancelLogin;\n"409 "$('btnCancel').onclick=cancelLogin;\n"
408 "$('btnRefresh').onclick=refreshToken;\n"410 "$('btnRefresh').onclick=refreshToken;\n"
tests/mock_login.sh+6−0■■■■■
index dd28924..d8214ef 100755index dd28924..d8214ef 100755
--- a/tests/mock_login.sh--- a/tests/mock_login.sh
+++ b/tests/mock_login.sh+++ b/tests/mock_login.sh
@@ -77,6 +77,12 @@ case "$MODE" in@@ -77,6 +77,12 @@ case "$MODE" in
77 printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n'77 printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n'
78 exit 178 exit 1
79 fi79 fi
80+ # A node that already holds a token refuses a second login without --force,
81+ # exactly like the real CLI.
82+ if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then
83+ printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
84+ exit 1
85+ fi
80 p=$(pending_file "$NAME")86 p=$(pending_file "$NAME")
81 mkdir -p "$(dirname "$p")" 2>/dev/null || true87 mkdir -p "$(dirname "$p")" 2>/dev/null || true
82 printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p"88 printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p"
tests/run.sh+15−1■■■■■
index fb0572f..e344ddd 100755index fb0572f..e344ddd 100755
--- a/tests/run.sh--- a/tests/run.sh
+++ b/tests/run.sh+++ b/tests/run.sh
@@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME"@@ -236,6 +236,7 @@ check "a path-traversal account name is refused" '"error":"bad_name"' "$BADNAME"
236 # ---------------------------------------------------------------- start236 # ---------------------------------------------------------------- start
237 section "action=start"237 section "action=start"
238 rm -f "$FIX/pend"/*.json "$FIX/mock"/*238 rm -f "$FIX/pend"/*.json "$FIX/mock"/*
239+rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json"
239 OUT=$(api "action=start&$PWDQ")240 OUT=$(api "action=start&$PWDQ")
240 json_ok "start returns ok" "$OUT" 'd["ok"] is True'241 json_ok "start returns ok" "$OUT" 'd["ok"] is True'
241 json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"'242 json_ok "start returns a device_code flow" "$OUT" 'd["flow"]=="device_code"'
@@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)"@@ -247,9 +248,22 @@ check "start writes the pending file" "" "$(ls "$FIX/pend" | head -1)"
247 OUT=$(api "action=status&$PWDQ")248 OUT=$(api "action=status&$PWDQ")
248 json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True'249 json_ok "status sees the pending flow" "$OUT" 'd["pending"] is True'
249 check_not "start never returns a token" 'SECRET' "$OUT"250 check_not "start never returns a token" 'SECRET' "$OUT"
251+# A node that already holds credentials: the CLI answers "exists", not "no
252+# code". The plugin must relay that word, because it is the only hint that
253+# force=1 (the Re-login button) is what the user needs.
254+api "action=cancel&$PWDQ" >/dev/null
255+mkcreds "$FIX/.credentials.json"
256+OUT=$(api "action=start&$PWDQ")
257+check "start relays the CLI's exists error verbatim" '"error":"exists"' "$OUT"
258+check "the exists error tells the user to re-login" 'Re-login' "$OUT"
259+OUT=$(api "action=start&force=1&$PWDQ")
260+json_ok "start with force=1 replaces the credentials" "$OUT" 'd["ok"] is True and d["user_code"]=="1234-5678"'
261+api "action=cancel&$PWDQ" >/dev/null
250 262
251 # ---------------------------------------------------------------- poll263 # ---------------------------------------------------------------- poll
252 section "action=poll"264 section "action=poll"
265+rm -f "$FIX/.credentials.json" "$FIX/.copilot-token-cache.json"
266+api "action=start&$PWDQ" >/dev/null
253 OUT=$(api "action=poll&$PWDQ")267 OUT=$(api "action=poll&$PWDQ")
254 json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True'268 json_ok "poll answers ok while waiting" "$OUT" 'd["ok"] is True'
255 json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"'269 json_ok "poll reports the pending state" "$OUT" 'd["state"]=="pending"'
@@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr@@ -263,7 +277,7 @@ json_ok "poll re-reads state and reports logged in" "$OUT" 'd["logged_in"] is Tr
263 check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT"277 check_not "poll never echoes the token" 'SECRETGITHUBTOKEN' "$OUT"
264 278
265 : > "$FIX/mock/poll_denied"279 : > "$FIX/mock/poll_denied"
266-api "action=start&$PWDQ" >/dev/null280+api "action=start&force=1&$PWDQ" >/dev/null
267 OUT=$(api "action=poll&$PWDQ")281 OUT=$(api "action=poll&$PWDQ")
268 check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT"282 check "a denied authorization surfaces the CLI error" '"error":"denied"' "$OUT"
269 OUT=$(api "action=poll&$PWDQ")283 OUT=$(api "action=poll&$PWDQ")
270284