@inventec/dsh-copilot-auth 1.2.9 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  ## 特性
18
18
 
19
19
  - 🔐 **设备码登录**:网页 + user code,免 API Token;SSO 组织授权友好
20
- - 🛑 **诚实的授权语义(v2)**:登录进行中可「**请求撤回**」——请求发出后状态如实显示「结果待核实」,不再伪装成已取消;「退出登录/重新授权」在当前宿主无法证明安全结束前**默认禁用并说明**(绝不静默删除凭据);同一实例只有一个授权尝试,409 时恢复展示共同进度
20
+ - 🛑 **诚实的授权语义(v2)**:登录进行中可「**请求撤回**」——请求发出后状态如实显示「结果待核实」,不再伪装成已取消;**「退出登录」在静止状态可用**(确认弹窗 → 清除凭据 → 删后核实;尝试进行中/风险锁存期拒绝),「重新授权」禁用(等价退出+登录,请分步执行);同一实例只有一个授权尝试,409 时恢复展示共同进度
21
21
  - 🧩 **预置提供方路由**:安装即出现在 Models 页,无需手动配置
22
22
  - 📦 **模型目录兜底填充**:登录成功时,若 GitHub Copilot 路由还没有模型目录,自动填入账号全部可用模型;你已精简/定制过的目录**永不覆盖**(重启、重新登录均不重置);已请求撤回后晚到的授权成功**不会**再写你的配置
23
23
  - ➕➖ **补充模型 / 重建模型列表(v2 双入口)**:「补充」默认非破坏——候选模型默认全不勾选,勾选后才加入,现有对象/顺序/modelOverrides 全部保留;「重建」显式确认后按「账号可用 ∩ 目录可解析」重建纯 ID 列表并清空本路由 modelOverrides(二次确认 + 空目标单独确认)。预览快照 10 分钟有效、绑定选择;同一操作幂等可查;配置并发编辑优先受保护
@@ -33,7 +33,8 @@
33
33
 
34
34
  | 本插件 | DSH 实测版本 | authorization 服务 | 说明 |
35
35
  |---|---|---|---|
36
- | v1.2.9+ | `0.1.7-rc.2`+(`0.2.0-rc.1` web 与 `0.2.0-rc.2` desktop 已实测) | runtime 内置,插件不挂载 | **修复 v1.2.8 授权页显示回归**:授权状态机漏接 `fetch`,页面恒显「未登录」、点「授权登录」恒「失败」且请求根本不发出(2026-10-04 实机实证,nginx 全量零 `POST /copilot-auth/start`)。另加:/status 加载失败退避自愈重试(60 秒转手动+「查询状态」)、失败原因分层展示(网络/网关/响应,不再裸「失败」)、已登录时服务端拒绝替换性授权(409 `already-configured`)且客户端自动恢复已登录视图 |
36
+ | v1.2.10+ | `0.1.7-rc.2`+ | runtime 内置,插件不挂载 | **退出登录正式可用**:无进行中授权尝试且无未核实风险(风险锁存)时,经确认弹窗清除凭据并**删除后立即复核**(凭据未消失则如实报「退出未完成」);授权尝试进行中(begin 晚写窗口)与撤回/超时未核实期一律拒绝退出。`/status` 的 `authorization.capabilities.logout` 按可用性动态返回;**重新授权仍禁用**(等价退出+登录,请分步执行)。经主人 2026-10-05 裁决替代 v1.2.8 D-01「退出全禁」策略 |
37
+ | v1.2.9 | `0.1.7-rc.2`+(`0.2.0-rc.1` web 与 `0.2.0-rc.2` desktop 已实测) | runtime 内置,插件不挂载 | **修复 v1.2.8 授权页显示回归**:授权状态机漏接 `fetch`,页面恒显「未登录」、点「授权登录」恒「失败」且请求根本不发出(2026-10-04 实机实证,nginx 全量零 `POST /copilot-auth/start`)。另加:/status 加载失败退避自愈重试(60 秒转手动+「查询状态」)、失败原因分层展示(网络/网关/响应,不再裸「失败」)、已登录时服务端拒绝替换性授权(409 `already-configured`)且客户端自动恢复已登录视图 |
37
38
  | v1.2.8 | `0.1.7-rc.2`+(`0.2.0-rc.1` web 与 `0.2.0-rc.2` desktop 已实测) | runtime 内置,插件不挂载 | **协议 v2:诚实的授权语义+补充/重建双入口**。授权侧:等待中可「请求撤回」(结果如实显示待核实)、退出/重新授权在宿主可证明安全前禁用并说明、单实例 attempt、15 分钟等待上限、晚到授权成功不再写配置;模型侧:旧「刷新」拆为「补充模型」(候选默认不勾选、非破坏)与「重建模型列表」(二次确认+清 modelOverrides),预览 10 分钟绑定选择、operationId 幂等、配置并发编辑优先、崩溃可恢复(「结束旧配置意图」显式闭环),状态按 profile 隔离(旧全局状态只读提示);模型路由要求 `protocolVersion:2`,GET `/status` 零写探针;v1 函数族已从 state.mjs 移除。**已知缺陷**:授权状态机漏接 fetchImpl → 授权页恒「未登录/失败」(v1.2.9 修复) |
38
39
  | v1.2.7+ | `0.1.7-rc.2`+(`0.2.0-rc.1` web 与 `0.2.0-rc.2` desktop 已实测) | runtime 内置,插件不挂载 | **重启后自动恢复**:注入后以宿主 `listModels` 复核,未端出即**净零切换**(`displayName` 改后复原;两次真内容变化 ⇒ 宿主必然重建快照,用户配置逐字节不变)触发重建;另加双路取实例 + `registryInstanceMismatch`/`registryVia` 诊断。修复 v1.2.6 的"同值写不改变配置身份 ⇒ 快照不重建" |
39
40
  | v1.2.6 | `0.1.7-rc.2`+ | runtime 内置,插件不挂载 | 首次引入宿主侧复核与 `settingsNotServed` 诊断;重启自愈用的"同值触碰"**实测不足**(同值写不改变配置对象身份),重启后模型可能仍不出现——请用 v1.2.7 |
@@ -76,7 +77,7 @@ dsh plugin --profile web add @inventec/dsh-copilot-auth
76
77
  ## 撤回、退出与卸载
77
78
 
78
79
  - **请求撤回**:授权等待期间可点「请求撤回」——插件先使本地旧意图失效,再尝试调用宿主撤销接口;无论请求是否送达,状态都如实显示「结果仍待核实」,在结果核实前不能发起新的授权尝试。
79
- - **退出登录 / 重新授权**:当前宿主桥接无法证明授权流程已安全结束(晚写不可排除),两项操作**默认禁用并说明原因**,插件也绝不调用凭据删除;待宿主提供可信安全路径后再启用。
80
+ - **退出登录(v1.2.10)**:无进行中授权尝试且无未核实风险时可退出——确认弹窗(将清除同一凭据库中所有实例共享的 Copilot 授权,不注销 GitHub 账号)→ 删除凭据 → **删除后立即复核**,凭据仍在则如实显示「退出未完成,授权凭据仍可能存在」;授权尝试进行中(设备码等待期,begin 晚写窗口)与撤回/超时未核实的风险锁存期一律拒绝退出。**重新授权**仍禁用:等价于退出+登录,请先「退出登录」再「授权登录」。
80
81
  - 卸载:`dsh plugin --profile web remove @inventec/dsh-copilot-auth` 后重启 dsh web
81
82
 
82
83
  ## 补充模型 / 重建模型列表
package/lib/client.js CHANGED
@@ -521,6 +521,23 @@ function createAuthFlow({ fetchImpl, clock = defaultClock(), onState = () => {
521
521
  }
522
522
  emit({ withdrawalDelivery: "failed", error: r.error?.details?.errorCode ?? r.error?.messageKey });
523
523
  }
524
+ async function logout() {
525
+ const gen = ++generation;
526
+ clearTimer();
527
+ failures = 0;
528
+ backoffIdx = 0;
529
+ failureWindowStart = null;
530
+ emit({ phase: "loading", logoutError: void 0 });
531
+ const r = await requestJson(fetchImpl, "/copilot-auth/logout", { method: "POST" });
532
+ if (stale(gen)) return;
533
+ if (!r.ok) {
534
+ const code = r.error?.details?.errorCode;
535
+ emit({ phase: "authorized", logoutError: code ?? r.error?.messageKey ?? "logout-failed" });
536
+ return;
537
+ }
538
+ emit({ logoutError: void 0 });
539
+ await initAttempt(gen);
540
+ }
524
541
  function refresh() {
525
542
  void init();
526
543
  }
@@ -528,7 +545,7 @@ function createAuthFlow({ fetchImpl, clock = defaultClock(), onState = () => {
528
545
  generation += 1;
529
546
  clearTimer();
530
547
  }
531
- return { init, start, cancel, refresh, dispose };
548
+ return { init, start, cancel, logout, refresh, dispose };
532
549
  }
533
550
 
534
551
  // src/client.jsx
@@ -568,6 +585,9 @@ var DICTS = {
568
585
  errGateway: "Gateway error: the DSH service may be restarting. Retrying automatically.",
569
586
  errBadResponse: "Unexpected response from the authorization service.",
570
587
  errHttp: "The authorization request failed. Check status and retry.",
588
+ logoutScope: "This removes Copilot authorization from this credential store and affects other instances using it. It does not delete your GitHub account.",
589
+ logoutFailed: "Sign-out did not complete; authorization credentials may remain. Please retry.",
590
+ reauthHint: "Reauthorize is unavailable. Sign out first, then sign in again.",
571
591
  // ---- Model management (protocol v2, design §5) ----
572
592
  supplement: "Add models",
573
593
  rebuild: "Rebuild model list",
@@ -650,6 +670,9 @@ var DICTS = {
650
670
  errGateway: "\u7F51\u5173\u9519\u8BEF\uFF1ADSH \u670D\u52A1\u53EF\u80FD\u6B63\u5728\u91CD\u542F\uFF0C\u6B63\u5728\u81EA\u52A8\u91CD\u8BD5\u3002",
651
671
  errBadResponse: "\u6388\u6743\u670D\u52A1\u8FD4\u56DE\u5F02\u5E38\u54CD\u5E94\u3002",
652
672
  errHttp: "\u6388\u6743\u8BF7\u6C42\u5931\u8D25\u3002\u8BF7\u67E5\u8BE2\u72B6\u6001\u540E\u91CD\u8BD5\u3002",
673
+ logoutScope: "\u5C06\u6E05\u9664\u6B64\u51ED\u636E\u5E93\u4E2D\u7684 Copilot \u6388\u6743\uFF0C\u4F7F\u7528\u540C\u4E00\u51ED\u636E\u5E93\u7684\u5176\u4ED6\u5B9E\u4F8B\u4E5F\u4F1A\u53D7\u5230\u5F71\u54CD\u3002\u6B64\u64CD\u4F5C\u4E0D\u6CE8\u9500 GitHub \u8D26\u53F7\u3002",
674
+ logoutFailed: "\u9000\u51FA\u767B\u5F55\u672A\u5B8C\u6210\uFF0C\u6388\u6743\u51ED\u636E\u4ECD\u53EF\u80FD\u5B58\u5728\u3002\u8BF7\u91CD\u8BD5\u3002",
675
+ reauthHint: "\u91CD\u65B0\u6388\u6743\u6682\u4E0D\u53EF\u7528\uFF1A\u8BF7\u5148\u9000\u51FA\u767B\u5F55\uFF0C\u518D\u91CD\u65B0\u6388\u6743\u3002",
653
676
  // ---- 模型管理(协议 v2,设计§5) ----
654
677
  supplement: "\u8865\u5145\u6A21\u578B",
655
678
  rebuild: "\u91CD\u5EFA\u6A21\u578B\u5217\u8868",
@@ -998,10 +1021,29 @@ function RetireDialog({ t, onConfirm, onCancel }) {
998
1021
  ] })
999
1022
  ] }) });
1000
1023
  }
1024
+ function ConfirmDialog({ title, body, confirmText, cancelText, onConfirm, onCancel }) {
1025
+ const cancelRef = (0, import_react.useRef)(null);
1026
+ const surface = (0, import_react.useRef)(null);
1027
+ if (!surface.current) surface.current = sampleThemeSurface();
1028
+ (0, import_react.useEffect)(() => {
1029
+ cancelRef.current?.focus();
1030
+ }, []);
1031
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: styles.modalMask, role: "dialog", "aria-modal": "true", onKeyDown: (e) => {
1032
+ if (e.key === "Escape") onCancel();
1033
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { ...styles.modal, background: surface.current.bg, color: surface.current.fg, maxWidth: 460 }, children: [
1034
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h4", { style: styles.modalTitle, children: title }),
1035
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { style: styles.modalText, children: body }),
1036
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: styles.modalActions, children: [
1037
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { ref: cancelRef, type: "button", style: { ...styles.button, ...styles.secondary }, onClick: onCancel, children: cancelText }),
1038
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.primary }, onClick: onConfirm, children: confirmText })
1039
+ ] })
1040
+ ] }) });
1041
+ }
1001
1042
  function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1002
1043
  const [auth, setAuth] = (0, import_react.useState)({ phase: "loading", connectivity: "online", shared: false });
1003
1044
  const [copied, setCopied] = (0, import_react.useState)(false);
1004
1045
  const [copyFail, setCopyFail] = (0, import_react.useState)(false);
1046
+ const [logoutConfirm, setLogoutConfirm] = (0, import_react.useState)(false);
1005
1047
  const [flow, setFlow] = (0, import_react.useState)(initial);
1006
1048
  const flowRef = (0, import_react.useRef)(flow);
1007
1049
  const authFlowRef = (0, import_react.useRef)(null);
@@ -1031,6 +1073,10 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1031
1073
  const checkStatus = () => {
1032
1074
  void authFlowRef.current?.refresh();
1033
1075
  };
1076
+ const doLogout = () => {
1077
+ setLogoutConfirm(false);
1078
+ void authFlowRef.current?.logout();
1079
+ };
1034
1080
  const copyCode = async (code) => {
1035
1081
  try {
1036
1082
  await navigator.clipboard.writeText(code);
@@ -1104,8 +1150,21 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1104
1150
  ] }),
1105
1151
  phase === "authorized" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: styles.card, children: [
1106
1152
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { display: "flex", gap: 10, alignItems: "center", flexWrap: "wrap" }, children: [
1107
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary, opacity: 0.5, cursor: "not-allowed" }, disabled: true, title: t("authUnsafe"), children: t("logout") }),
1108
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary, opacity: 0.5, cursor: "not-allowed" }, disabled: true, title: t("authUnsafe"), children: t("reauthorize") }),
1153
+ (() => {
1154
+ const logoutAvailable = auth.status?.authorization?.capabilities?.logout === true;
1155
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1156
+ "button",
1157
+ {
1158
+ type: "button",
1159
+ style: { ...styles.button, ...styles.secondary, ...logoutAvailable ? null : { opacity: 0.5, cursor: "not-allowed" } },
1160
+ disabled: !logoutAvailable,
1161
+ title: logoutAvailable ? t("logoutScope") : t("authUnsafe"),
1162
+ onClick: () => setLogoutConfirm(true),
1163
+ children: t("logout")
1164
+ }
1165
+ );
1166
+ })(),
1167
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary, opacity: 0.5, cursor: "not-allowed" }, disabled: true, title: t("reauthHint"), children: t("reauthorize") }),
1109
1168
  ["previewing", "applying", "checking", "retiring"].includes(flow.name) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.primary, opacity: 0.5 }, disabled: true, children: flow.name === "applying" ? t("applying") : t("refreshing") }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
1110
1169
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1111
1170
  "button",
@@ -1131,7 +1190,11 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1131
1190
  ] }),
1132
1191
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.banner, children: [
1133
1192
  "\u24D8 ",
1134
- t("authUnsafe")
1193
+ t("reauthHint")
1194
+ ] }),
1195
+ auth.logoutError && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.error, children: [
1196
+ "\u26A0 ",
1197
+ auth.logoutError === "logout-unsafe" ? t("authUnsafe") : t("logoutFailed")
1135
1198
  ] }),
1136
1199
  refreshBlocked(flow.flags) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.banner, children: [
1137
1200
  "\u26A0 ",
@@ -1204,6 +1267,17 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1204
1267
  onCancel: () => drive({ type: "cancel" })
1205
1268
  }
1206
1269
  ),
1270
+ logoutConfirm && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1271
+ ConfirmDialog,
1272
+ {
1273
+ title: t("logout"),
1274
+ body: t("logoutScope"),
1275
+ confirmText: t("logout"),
1276
+ cancelText: t("cancel"),
1277
+ onCancel: () => setLogoutConfirm(false),
1278
+ onConfirm: doLogout
1279
+ }
1280
+ ),
1207
1281
  (phase === "idle" || phase === "failed") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.primary }, onClick: login, children: t("login") }) })
1208
1282
  ] });
1209
1283
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inventec/dsh-copilot-auth",
3
- "version": "1.2.9",
4
- "description": "为内置 Copilot 提供方补设备码登录设置页,预置开箱路由;凭据入内置凭据库,模型目录兜底不覆盖定制。v1.2.9:修复授权页恒未登录/失败(漏传 fetchImpl)、失败原因分层展示+自愈重试、已登录拒绝替换性授权(协议 v2)。适配 DSH 0.1.7+(≤0.1.5 请用 1.1.x)。",
3
+ "version": "1.2.10",
4
+ "description": "为内置 Copilot 提供方补设备码登录设置页,预置开箱路由;凭据入内置凭据库,模型目录兜底不覆盖定制。v1.2.10:退出登录正式可用(静止态删凭据+删后核实,尝试期/风险锁存期拒绝),失败原因分层展示(协议 v2)。适配 DSH 0.1.7+(≤0.1.5 请用 1.1.x)。",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": "iasi",
package/src/auth-flow.mjs CHANGED
@@ -199,6 +199,27 @@ export function createAuthFlow({ fetchImpl, clock = defaultClock(), onState = ()
199
199
  emit({ withdrawalDelivery: "failed", error: r.error?.details?.errorCode ?? r.error?.messageKey });
200
200
  }
201
201
 
202
+ async function logout() {
203
+ // 退出登录(写操作,单次调用):pending 态 → 成功后完整重跑 init 恢复真实
204
+ // 状态(若晚到写把凭据写了回来,init 会如实显示 authorized,绝不伪报已退出);
205
+ // 失败回到 authorized 并保留 logoutError 供 UI 分层展示。
206
+ const gen = ++generation;
207
+ clearTimer();
208
+ failures = 0;
209
+ backoffIdx = 0;
210
+ failureWindowStart = null;
211
+ emit({ phase: "loading", logoutError: undefined });
212
+ const r = await requestJson(fetchImpl, "/copilot-auth/logout", { method: "POST" });
213
+ if (stale(gen)) return;
214
+ if (!r.ok) {
215
+ const code = r.error?.details?.errorCode;
216
+ emit({ phase: "authorized", logoutError: code ?? r.error?.messageKey ?? "logout-failed" });
217
+ return;
218
+ }
219
+ emit({ logoutError: undefined });
220
+ await initAttempt(gen);
221
+ }
222
+
202
223
  function refresh() {
203
224
  // 手动「查询状态」:完整重跑 init(status→state),可从过期的未登录/失败视图恢复
204
225
  void init();
@@ -209,5 +230,5 @@ export function createAuthFlow({ fetchImpl, clock = defaultClock(), onState = ()
209
230
  clearTimer();
210
231
  }
211
232
 
212
- return { init, start, cancel, refresh, dispose };
233
+ return { init, start, cancel, logout, refresh, dispose };
213
234
  }
package/src/auth-host.mjs CHANGED
@@ -10,7 +10,8 @@ import { randomUUID } from "node:crypto";
10
10
  import { CREDENTIAL_KEY } from "./shared.mjs";
11
11
 
12
12
  export const WAIT_LIMIT_MS = 15 * 60 * 1000;
13
- const LIVE_STATUSES = ["starting", "waiting", "finishing"];
13
+ // 进行中的授权尝试状态:begin 的晚写窗口,期间退出/撤回类操作一律拒绝
14
+ export const LIVE_STATUSES = ["starting", "waiting", "finishing"];
14
15
 
15
16
  // 授权错误脱敏:仅保留安全字符集(字母/数字/空格/点/冒号/逗号/连字符),
16
17
  // 阻断上游异常文本或疑似 token(含 = ; / + _ 等的串)进入状态面。
@@ -147,9 +148,33 @@ export function createAuthorizationController(ctx, { scope, intentIO, clock = {
147
148
  return { withdrawalDelivery };
148
149
  }
149
150
 
150
- function logout() {
151
- // 当前宿主无法证明安全退出(D-01):统一拒绝,不调用 deleteRecord
152
- throw authError("LOGOUT_UNAVAILABLE", "logout-safety-unavailable");
151
+ async function logout() {
152
+ // 退出安全条件(v1.2.10 起,经主人裁决替代 D-01 全禁策略):
153
+ // ① 无进行中的授权尝试(begin 晚写窗口)② 无风险锁存(撤回/超时未核实)。
154
+ // 满足后凭据库视为静止:deleteRecord+删除后 describe 复核;晚到写无法绝对
155
+ // 排除,以复核与状态页如实展示兜底——退出未核实成功绝不伪报成功。
156
+ requireScope();
157
+ if (attempt && LIVE_STATUSES.includes(attempt.status)) {
158
+ throw authError("ATTEMPT_RUNNING", "already running", { attemptId: attempt.attemptId });
159
+ }
160
+ if (riskLatch) throw authError("LOGOUT_UNSAFE", "logout-unsafe");
161
+ // 本 profile 显式操作:旧预览/模型配置意图先行失效(失败则不动凭据)
162
+ intentIO.bump();
163
+ try {
164
+ const deleted = await ctx.credentials.deleteRecord(CREDENTIAL_KEY);
165
+ if (deleted === false) throw authError("LOGOUT_FAILED", "logout-failed");
166
+ } catch (err) {
167
+ if (err?.code === "LOGOUT_FAILED") throw err;
168
+ throw authError("LOGOUT_FAILED", "logout-failed");
169
+ }
170
+ try {
171
+ const info = await ctx.credentials.describeRecord(CREDENTIAL_KEY);
172
+ if (info?.configured === true) throw authError("LOGOUT_FAILED", "logout-failed");
173
+ } catch (err) {
174
+ if (err?.code === "LOGOUT_FAILED") throw err;
175
+ throw authError("LOGOUT_FAILED", "logout-failed");
176
+ }
177
+ return { ok: true };
153
178
  }
154
179
 
155
180
  function snapshot() {
package/src/host.mjs CHANGED
@@ -9,7 +9,7 @@ import { homedir } from "node:os";
9
9
  import { fileURLToPath } from "node:url";
10
10
  import { CREDENTIAL_KEY, PROTOCOL_VERSION, routes } from "./shared.mjs";
11
11
  import { readJson, writeJsonAtomic, createMutex } from "./atomic-json.mjs";
12
- import { createAuthorizationController, sanitizeAuthError } from "./auth-host.mjs";
12
+ import { createAuthorizationController, sanitizeAuthError, LIVE_STATUSES } from "./auth-host.mjs";
13
13
  import { resolveRuntimeScope } from "./runtime-scope.mjs";
14
14
  import { mergeCatalog, digest, findPiAiInstallation, classifyCatalogTarget, normalizeRemoteCatalog, catalogShapeKeys } from "./catalog.mjs";
15
15
  import { fetchLatestCatalog } from "./catalog-fetch.mjs";
@@ -569,6 +569,12 @@ export function apply(ctx, opts = {}) {
569
569
  credential = "read-error";
570
570
  }
571
571
  const authSnapshot = controller.snapshot();
572
+ // 退出可用性(v1.2.10):无进行中尝试且无风险锁存才可安全删除凭据;
573
+ // 重新授权恒不可用(等价于退出+登录,请分步执行)
574
+ const logoutAvailable = !authSnapshot.riskLatch
575
+ && !LIVE_STATUSES.includes(authSnapshot.status)
576
+ && authSnapshot.status !== "withdrawal-pending-unverified"
577
+ && authSnapshot.status !== "timed-out-unverified";
572
578
  // refresh 块:只读聚合(通道分类来自启动期缓存;GET 不执行写探针,Q23)
573
579
  const loaded = loadRefreshState(scope, { legacyPath });
574
580
  const st = loaded.flags.unknownVersion ? null : loaded.state;
@@ -623,7 +629,7 @@ export function apply(ctx, opts = {}) {
623
629
  credential,
624
630
  attempt: authSnapshot,
625
631
  riskLatch: authSnapshot.riskLatch,
626
- capabilities: { logout: false, reauthorize: false },
632
+ capabilities: { logout: logoutAvailable, reauthorize: false },
627
633
  },
628
634
  refresh,
629
635
  ...(operation ? { operation } : {}),
@@ -631,13 +637,35 @@ export function apply(ctx, opts = {}) {
631
637
  },
632
638
  });
633
639
 
634
- // 退出登录:当前宿主桥接无法证明安全退出(D-01),统一拒绝且绝不 deleteRecord。
640
+ // 退出登录(v1.2.10):无进行中尝试且无风险锁存时执行凭据删除+删后核实;
641
+ // 进行中尝试(begin 晚写窗口)与风险锁存期(撤回/超时未核实)一律拒绝。
635
642
  ctx.webServer.register({
636
643
  kind: "exact",
637
644
  path: r.logout,
638
- handler: (req, res) => {
645
+ handler: async (req, res) => {
639
646
  if (!guard(req, res, "POST")) return;
640
- json(res, 403, { ok: false, error: "logout-safety-unavailable" });
647
+ if (!scope.known) {
648
+ json(res, 503, { ok: false, error: "scope-unavailable" });
649
+ return;
650
+ }
651
+ try {
652
+ await controller.logout();
653
+ json(res, 200, { ok: true });
654
+ } catch (err) {
655
+ if (err?.code === "ATTEMPT_RUNNING") {
656
+ json(res, 409, { ok: false, error: "already running", attemptId: err.attemptId });
657
+ return;
658
+ }
659
+ if (err?.code === "LOGOUT_UNSAFE") {
660
+ json(res, 409, { ok: false, error: "logout-unsafe" });
661
+ return;
662
+ }
663
+ if (err?.code === "LOGOUT_FAILED") {
664
+ json(res, 409, { ok: false, error: "logout-failed" });
665
+ return;
666
+ }
667
+ json(res, 500, { ok: false, error: sanitizeAuthError(err) });
668
+ }
641
669
  },
642
670
  });
643
671