@inventec/dsh-copilot-auth 1.2.24 → 1.2.26

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
@@ -3,7 +3,7 @@
3
3
  [![npm](https://img.shields.io/npm/v/%40inventec%2Fdsh-copilot-auth?label=npm)](https://www.npmjs.com/package/@inventec/dsh-copilot-auth)
4
4
  [![CI](https://img.shields.io/github/actions/workflow/status/iasiv5/dsh-copilot-auth/ci.yml?branch=main&label=CI)](https://github.com/iasiv5/dsh-copilot-auth/actions/workflows/ci.yml)
5
5
  [![License](https://img.shields.io/github/license/iasiv5/dsh-copilot-auth?label=License)](https://github.com/iasiv5/dsh-copilot-auth/blob/main/LICENSE)
6
- [![DSH](https://img.shields.io/badge/DSH-0.2.1--rc.5%20verified-2563eb)](#版本兼容)
6
+ [![DSH](https://img.shields.io/badge/DSH-0.2.2--rc.1%20verified-2563eb)](#版本兼容)
7
7
 
8
8
  为 DSH(DeepSeek Harness)内置的 GitHub Copilot 通道补上**网页设备码登录 / 注销**入口,预置开箱即用的 Copilot 提供方路由,并提供**模型列表管理**。用公司 GitHub 账号(带 Copilot 订阅)登录 DSH 的方式与其他 Copilot 客户端一致:**网页 + 设备码**,全程不填 API Token。
9
9
 
@@ -64,8 +64,9 @@ pi-ai 的模型目录是打包时硬编码的 JSON:上游新增模型在本机
64
64
  - **勾选即终态**:一张分组全表——**可新增**(账号可用 ∩ 目录可解析,默认不勾)、**在列**(默认勾选)、**待删除候选**(在列但账号未报告 / 目录无法解析,默认不勾=删除提案,重新勾选即**挽留**)。应用时被保留模型的对象、顺序与定制逐字保留,被移除模型的定制随模型消失。
65
65
  - **快捷动作**:「全选 / 取消全选」作用于全表;「**对齐账号与目录**」仅在漂移时出现,一键把勾选态设为账号 ∩ 可解析;「**同时清除全部模型定制**」复选框仅在存在可清除定制时出现;摘要行常驻显示「应用后共 N 个:保留 · 新增 · 移除」。
66
66
  - **证据门控按实际 diff**:纯新增可用 24 小时内同授权的缓存;**移除类变更强制实时账号证据 + 最新目录并二次确认**(缓存下勾选删除会自动升级为实时预览);空目标需单独确认,落显式空列表且不会被登录自动回填。移除所需的实时证据会在本地会话令牌过期时**自动续期**(仅内存、不落库);续期仍失败时,来源行会给出原因与出路(先用一次模型 / 重新授权 / 稍后重试)。
67
- - **生效时机**:web / 服务形态先做原子只增补丁、**重启后**生效;desktop(只读 `app.asar`)走进程内注册表注入,**当次生效、无需重启**、不碰官方产物(ADR 0003)。
68
- - **并发与恢复**:每个操作带服务端签发的 operationId(幂等可查、绝不重复执行);配置并发编辑永远优先(漂移返回 409 要求重新预览);崩溃 / 重启后可恢复,出现冲突时提供「结束旧配置意图」显式闭环。apply 撞上卡死的旧操作(423)且该操作已 `recovery-needed` 时,弹窗与刷新后的页面都会提供同一「结束旧配置意图」出口(know-how 032 缺口闭环)。
67
+ - **生效时机**:web / 服务形态先做原子只增补丁、**重启后**生效(页面明确提示「待重启生效」并附「**放弃待重启的更改**」按钮,期间新的应用会被拒绝并给出同提示);desktop(只读 `app.asar`)走进程内注册表注入,**当次生效、无需重启**、不碰官方产物(ADR 0003)。
68
+ - **并发与恢复**:每个操作带服务端签发的 operationId(幂等可查、绝不重复执行);配置并发编辑永远优先(漂移返回 409 要求重新预览);崩溃 / 重启后可恢复,出现冲突时提供「结束旧配置意图」显式闭环。apply 撞上进行中的旧操作(423)且该操作已 `recovery-needed` 或 `pending-restart` 时提供对应出口(前者「结束旧配置意图」,后者「放弃待重启的更改」),出口在弹窗与页面主视图(含刷新后)均可达——know-how 032 缺口闭环 + ADR-0006 扩展;作废 / 放弃后页面如实显示结果与原因,不再残留「待重启生效」提示(评审 Q4)。
69
+ - **授权动作即时作废**(ADR-0006):**登录 / 退出登录会立即作废进行中的模型配置更改**(历史结果中标注原因「授权状态已变化」),已落盘的目录数据保留,重新预览应用即可——无需重启也不会再出现「重启后才失效」的困惑。
69
70
 
70
71
  ## 工作原理
71
72
 
@@ -79,7 +80,7 @@ pi-ai 的模型目录是打包时硬编码的 JSON:上游新增模型在本机
79
80
 
80
81
  | 插件线 | DSH 实测版本 | 说明 |
81
82
  |---|---|---|
82
- | **v1.2.x** | `0.1.7-rc.2`、`0.2.0-rc.1/rc.2`、`0.2.1-rc.2` ~ `rc.5` | 当前线;`0.2.0` 起注意插件 peer 精确版本守卫;rc.3 Web 冒烟:`/copilot-auth/status` 200、`activated=true`、`settingsNotServed=[]`、`lastError=null`;rc.4/rc.5 为契约面等价重发布(宿主 web-app patch 连续零 diff),核验结论按等价口径纳入 |
83
+ | **v1.2.x** | `0.1.7-rc.2`、`0.2.0-rc.1/rc.2`、`0.2.1-rc.2` ~ `rc.5`、`0.2.2-rc.1` | 当前线;`0.2.0` 起注意插件 peer 精确版本守卫;rc.3 Web 冒烟:`/copilot-auth/status` 200、`activated=true`、`settingsNotServed=[]`、`lastError=null`;rc.4/rc.5 为契约面等价重发布(宿主 web-app patch 连续零 diff),核验结论按等价口径纳入;`0.2.2-rc.1` 新 minor 线首轮已实机冒烟(2026-11-04:`settingsNotServed=[]`、`lastError=null`、`registryInstanceMismatch=false`、`catalogWritable=true`) |
83
84
  | v1.1.x | `0.1.2-rc.1` ~ `0.1.5-rc.2` | 旧版线;依赖 v1.2.0 已移除的 authorization 挂载补丁,新版 DSH 上无法激活 |
84
85
 
85
86
  逐版本沿革(v1.2.0 ~ v1.2.20 完整说明与已移除能力的语义记录)见 [docs/CHANGELOG.md](./docs/CHANGELOG.md)。
@@ -120,7 +121,7 @@ base 模型(GPT-4o / 4.1 一类)不耗 premium requests;premium 模型(C
120
121
  - 自行 patch `llm-pi-ai` 整段 config 会覆盖预置路由;`settings.yaml` 的 `llm-pi-ai:` 节只能稀疏覆盖字段,无法删除预置路由本身(移除须卸载本插件)
121
122
  - 路由前缀 `/copilot-auth` 两侧硬编码,不可配置
122
123
  - 模型目录「**只增不更新**」:合并只补充本机从未见过的条目,上游对已有 id 的元数据修正不会同步(ADR 0001)
123
- - DSH rc 版本耦合:实测 `0.1.7-rc.2`、`0.2.0-rc.1/rc.2` 与 `0.2.1-rc.2/rc.3`(rc.4/rc.5 为契约面等价重发布,按等价口径纳入核验范围);rc.3 Web 冒烟见「版本兼容」,peer 仅 `@deepseek-ai/cordis@^4.0.2`
124
+ - DSH rc 版本耦合:实测 `0.1.7-rc.2`、`0.2.0-rc.1/rc.2`、`0.2.1-rc.2/rc.3`(rc.4/rc.5 为契约面等价重发布,按等价口径纳入核验范围)与 `0.2.2-rc.1`(新 minor 线首轮,2026-11-04 实机冒烟,见「版本兼容」);peer 仅 `@deepseek-ai/cordis@^4.0.2`
124
125
  - DSH 升级后的目录自愈仅在 pi-ai 基线版本不变时按已确认条目只增重放;跨基线且条目缺失时上报 `self-heal-incompatible`,不修改安装树(重新执行一次手动刷新即可在新基线上激活)
125
126
  - 并发保护为宿主单进程内 mutex,不支持多实例 / 多进程并发操作同一安装目录
126
127
  - 目录 fsync 仅在 POSIX 执行:Windows 无目录 fsync 语义(必然 `EPERM`),直接跳过,一致性由 NTFS 元数据日志保证(v1.2.3 起)
package/lib/client.js CHANGED
@@ -192,9 +192,12 @@ function reduce(state, event) {
192
192
  if (event.type === "retire-request" && state.name === "result") {
193
193
  return [{ name: "retireConfirm", result: state.result }, null];
194
194
  }
195
- if (event.type === "retire-request" && state.name === "busy" && state.active?.phaseResult?.status === "recovery-needed") {
195
+ if (event.type === "retire-request" && state.name === "busy" && ["recovery-needed", "pending-restart"].includes(state.active?.phaseResult?.status ?? "")) {
196
196
  return [{ name: "retireConfirm", active: state.active }, null];
197
197
  }
198
+ if (event.type === "retire-request" && state.name === "pendingRestart" && (state.active?.operationId || state.result?.status === "pending-restart" && state.result?.operationId)) {
199
+ return [{ name: "retireConfirm", result: state.result, active: state.active ?? null }, null];
200
+ }
198
201
  if (event.type === "dismiss" && state.name === "failed") {
199
202
  return [{ ...initial, flags: state.flags }, null];
200
203
  }
@@ -207,7 +210,7 @@ function reduce(state, event) {
207
210
  return [{ ...base, name: "busy", active: refresh.activeOperation }, null];
208
211
  }
209
212
  if (restartStates.has(refresh?.lastResult?.status ?? "") || refresh?.pendingRestart === true) {
210
- return [{ ...base, name: "pendingRestart", result: refresh.lastResult }, null];
213
+ return [{ ...base, name: "pendingRestart", result: refresh.lastResult, active: refresh.activeOperation ?? null }, null];
211
214
  }
212
215
  const st = refresh?.lastResult?.status;
213
216
  if (st && ["recovery-needed", "rollback-conflict", "conflict", "partial", "rolled-back", "intent-retired", "applied"].includes(st)) {
@@ -347,15 +350,22 @@ function reduce(state, event) {
347
350
  }
348
351
  case "retireConfirm": {
349
352
  if (event.type === "retire-confirm") {
350
- return [{ name: "retiring", operationId: state.result?.operationId ?? state.active?.operationId, result: state.result, active: state.active }, { type: "retire", operationId: state.result?.operationId ?? state.active?.operationId }];
353
+ const retireId = state.active?.operationId ?? state.result?.operationId;
354
+ return [{ name: "retiring", operationId: retireId, result: state.result, active: state.active }, { type: "retire", operationId: retireId }];
355
+ }
356
+ if (event.type === "cancel") {
357
+ if (state.active) return [{ name: "busy", active: state.active }, null];
358
+ if (state.result?.status === "pending-restart") return [{ name: "pendingRestart", result: state.result }, null];
359
+ return [{ name: "result", result: state.result }, null];
351
360
  }
352
- if (event.type === "cancel") return state.active ? [{ name: "busy", active: state.active }, null] : [{ name: "result", result: state.result }, null];
353
361
  return [state, null];
354
362
  }
355
363
  case "retiring": {
356
364
  if (event.type === "retire-ok") return [{ ...state, name: "checking" }, { type: "status" }];
357
365
  if (event.type === "retire-fail") {
358
- return state.active ? [{ name: "busy", active: state.active, retireError: event.error }, null] : [{ name: "result", result: state.result, retireError: event.error }, null];
366
+ if (state.active) return [{ name: "busy", active: state.active, retireError: event.error }, null];
367
+ if (state.result?.status === "pending-restart") return [{ name: "pendingRestart", result: state.result, retireError: event.error }, null];
368
+ return [{ name: "result", result: state.result, retireError: event.error }, null];
359
369
  }
360
370
  return [state, null];
361
371
  }
@@ -714,6 +724,11 @@ var DICTS = {
714
724
  retireBtn: "End previous configuration intent",
715
725
  retireTitle: "End previous configuration intent",
716
726
  retireConfirmText: "End the intent only; existing changes are not rolled back or undone.",
727
+ busyPendingRestart: "The previous change is saved and takes effect after restart. New changes cannot be applied until then \u2014 wait for the restart, or discard that change.",
728
+ retireBtnPending: "Discard the pending change",
729
+ retireConfirmPending: "Discarding drops this pending change; catalog data already written is kept. Preview and apply again afterwards.",
730
+ retiredByAuth: "Authorization changed (sign-in or sign-out) and the in-flight model configuration change was discarded. Preview and apply again.",
731
+ discarded: "The pending change was discarded; catalog data already written is kept. Preview and apply again afterwards.",
717
732
  legacy: "Legacy shared recovery state was found. It will not be applied automatically; preview and confirm in this profile.",
718
733
  techDetails: "Technical details",
719
734
  stateCorrupt: "State file was corrupted and quarantined. Preview and confirm again.",
@@ -805,6 +820,11 @@ var DICTS = {
805
820
  retireBtn: "\u7ED3\u675F\u65E7\u914D\u7F6E\u610F\u56FE",
806
821
  retireTitle: "\u7ED3\u675F\u65E7\u914D\u7F6E\u610F\u56FE",
807
822
  retireConfirmText: "\u53EA\u7ED3\u675F\u65E7\u610F\u56FE\uFF0C\u4E0D\u56DE\u6EDA\u6216\u64A4\u9500\u5DF2\u53D1\u751F\u7684\u66F4\u6539\u3002",
823
+ busyPendingRestart: "\u4E0A\u4E00\u6B21\u66F4\u6539\u5DF2\u4FDD\u5B58\u3001\u5F85\u91CD\u542F\u751F\u6548\uFF1B\u91CD\u542F\u524D\u65E0\u6CD5\u5E94\u7528\u65B0\u7684\u66F4\u6539\u3002\u53EF\u7B49\u5F85\u91CD\u542F\uFF0C\u6216\u653E\u5F03\u8BE5\u6B21\u66F4\u6539\u3002",
824
+ retireBtnPending: "\u653E\u5F03\u5F85\u91CD\u542F\u7684\u66F4\u6539",
825
+ retireConfirmPending: "\u653E\u5F03\u540E\uFF0C\u672C\u6B21\u5F85\u91CD\u542F\u7684\u66F4\u6539\u5C06\u88AB\u4E22\u5F03\uFF1B\u5DF2\u843D\u76D8\u7684\u76EE\u5F55\u6570\u636E\u4FDD\u7559\uFF0C\u4E4B\u540E\u53EF\u91CD\u65B0\u9884\u89C8\u5E76\u5E94\u7528\u3002",
826
+ retiredByAuth: "\u6388\u6743\u72B6\u6001\u5DF2\u53D8\u5316\uFF08\u767B\u5F55\u6216\u9000\u51FA\u767B\u5F55\uFF09\uFF0C\u8FDB\u884C\u4E2D\u7684\u6A21\u578B\u914D\u7F6E\u66F4\u6539\u5DF2\u4F5C\u5E9F\uFF1B\u8BF7\u91CD\u65B0\u9884\u89C8\u5E76\u5E94\u7528\u3002",
827
+ discarded: "\u5DF2\u653E\u5F03\u8BE5\u6B21\u5F85\u91CD\u542F\u7684\u66F4\u6539\uFF1B\u5DF2\u843D\u76D8\u7684\u76EE\u5F55\u6570\u636E\u4FDD\u7559\uFF0C\u53EF\u91CD\u65B0\u9884\u89C8\u5E76\u5E94\u7528\u3002",
808
828
  legacy: "\u68C0\u6D4B\u5230\u65E7\u7684\u5171\u4EAB\u6062\u590D\u72B6\u6001\u3002\u4E0D\u4F1A\u81EA\u52A8\u5E94\u7528\uFF0C\u8BF7\u5728\u5F53\u524D profile \u91CD\u65B0\u9884\u89C8\u786E\u8BA4\u3002",
809
829
  techDetails: "\u6280\u672F\u8BE6\u60C5",
810
830
  stateCorrupt: "\u6062\u590D\u72B6\u6001\u65E0\u6CD5\u8BFB\u53D6\uFF0C\u539F\u4EF6\u5DF2\u4FDD\u7559\u3002\u8BF7\u91CD\u65B0\u9884\u89C8\u786E\u8BA4\u3002",
@@ -1149,21 +1169,24 @@ function RefreshModal({ t, flow, onSelect, onConfirm, onCancel, onOverlay }) {
1149
1169
  ] })
1150
1170
  ] }) });
1151
1171
  }
1152
- function RetireDialog({ t, onConfirm, onCancel }) {
1172
+ function RetireDialog({ t, onConfirm, onCancel, pending = false }) {
1153
1173
  const cancelRef = (0, import_react.useRef)(null);
1154
1174
  const surface = (0, import_react.useRef)(null);
1155
1175
  if (!surface.current) surface.current = sampleThemeSurface();
1156
1176
  (0, import_react.useEffect)(() => {
1157
1177
  cancelRef.current?.focus();
1158
1178
  }, []);
1179
+ const titleKey = pending ? "retireBtnPending" : "retireTitle";
1180
+ const bodyKey = pending ? "retireConfirmPending" : "retireConfirmText";
1181
+ const btnKey = pending ? "retireBtnPending" : "retireBtn";
1159
1182
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: styles.modalMask, role: "dialog", "aria-modal": "true", onKeyDown: (e) => {
1160
1183
  if (e.key === "Escape") onCancel();
1161
1184
  }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { ...styles.modal, background: surface.current.bg, color: surface.current.fg, maxWidth: 420 }, children: [
1162
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h4", { style: styles.modalTitle, children: t("retireTitle") }),
1163
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { style: styles.modalText, children: t("retireConfirmText") }),
1185
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h4", { style: styles.modalTitle, children: t(titleKey) }),
1186
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { style: styles.modalText, children: t(bodyKey) }),
1164
1187
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: styles.modalActions, children: [
1165
1188
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { ref: cancelRef, type: "button", style: { ...styles.button, ...styles.secondary }, onClick: onCancel, children: t("cancel") }),
1166
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.primary }, onClick: onConfirm, children: t("retireBtn") })
1189
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.primary }, onClick: onConfirm, children: t(btnKey) })
1167
1190
  ] })
1168
1191
  ] }) });
1169
1192
  }
@@ -1337,16 +1360,20 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1337
1360
  "\u24D8 ",
1338
1361
  t("legacy")
1339
1362
  ] }),
1340
- flow.name === "pendingRestart" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.banner, children: [
1341
- "\u26A0 ",
1342
- t("pendingRestart")
1363
+ flow.name === "pendingRestart" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
1364
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.banner, children: [
1365
+ "\u26A0 ",
1366
+ t("pendingRestart"),
1367
+ flow.retireError ? `\uFF08${flow.retireError}\uFF09` : ""
1368
+ ] }),
1369
+ (flow.active?.operationId || flow.result?.status === "pending-restart" && flow.result?.operationId) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary }, onClick: () => drive({ type: "retire-request" }), children: t("retireBtnPending") }) })
1343
1370
  ] }),
1344
1371
  flow.name === "busy" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.banner, children: [
1345
1372
  "\u24D8 ",
1346
- t("busy"),
1373
+ flow.active?.phaseResult?.status === "pending-restart" ? t("busyPendingRestart") : t("busy"),
1347
1374
  flow.retireError ? `\uFF08${flow.retireError}\uFF09` : ""
1348
1375
  ] }),
1349
- flow.name === "busy" && flow.active?.phaseResult?.status === "recovery-needed" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary }, onClick: () => drive({ type: "retire-request" }), children: t("retireBtn") }) }),
1376
+ flow.name === "busy" && ["recovery-needed", "pending-restart"].includes(flow.active?.phaseResult?.status ?? "") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", style: { ...styles.button, ...styles.secondary }, onClick: () => drive({ type: "retire-request" }), children: flow.active?.phaseResult?.status === "pending-restart" ? t("retireBtnPending") : t("retireBtn") }) }),
1350
1377
  flow.name === "resultUnknown" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
1351
1378
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: styles.error, children: [
1352
1379
  "\u26A0 ",
@@ -1357,7 +1384,9 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1357
1384
  flow.name === "result" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
1358
1385
  (() => {
1359
1386
  const st = flow.result?.status;
1360
- const key = {
1387
+ const retiredByAuth = st === "intent-retired" && typeof flow.result?.error === "string" && flow.result.error.startsWith("superseded-by-auth-action:");
1388
+ const selfDiscarded = st === "intent-retired" && !retiredByAuth && flow.result?.error == null;
1389
+ const key = retiredByAuth ? "retiredByAuth" : selfDiscarded ? "discarded" : {
1361
1390
  applied: "applied",
1362
1391
  conflict: "conflict",
1363
1392
  partial: "partial",
@@ -1366,8 +1395,8 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1366
1395
  "recovery-needed": "recoveryNeeded",
1367
1396
  "intent-retired": "recoveryNeeded"
1368
1397
  }[st];
1369
- return key ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: st === "applied" ? styles.banner : styles.error, children: [
1370
- st === "applied" ? "\u2713" : "\u26A0",
1398
+ return key ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { style: st === "applied" || key === "discarded" ? styles.banner : styles.error, children: [
1399
+ st === "applied" ? "\u2713" : key === "discarded" ? "\u24D8" : "\u26A0",
1371
1400
  " ",
1372
1401
  t(key)
1373
1402
  ] }) : null;
@@ -1395,6 +1424,7 @@ function CopilotSection({ t = (key) => DICTS.en[key] ?? key }) {
1395
1424
  RetireDialog,
1396
1425
  {
1397
1426
  t,
1427
+ pending: flow.active?.phaseResult?.status === "pending-restart",
1398
1428
  onConfirm: () => drive({ type: "retire-confirm" }),
1399
1429
  onCancel: () => drive({ type: "cancel" })
1400
1430
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inventec/dsh-copilot-auth",
3
- "version": "1.2.24",
3
+ "version": "1.2.26",
4
4
  "description": "为内置 Copilot 提供方补设备码登录设置页,预置开箱路由;凭据入内置凭据库,模型目录兜底不覆盖定制。v1.2.19:管理模型列表 v3 目标状态编辑(分组全表/挽留/对齐/清空语义告知)+单次确认+移除请求撤回+退出残影修复。v1.2.18:授权失败附网络失败码+人性化提示。适配 DSH 0.1.7+(≤0.1.5 请用 1.1.x)。v1.2.21:移除前实时证据自动续期(ephemeral 不落库+负缓存 5min)+缓存证据原因分型文案+busy 卡死 retire 出口与水合判序修正(032)。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
package/src/auth-host.mjs CHANGED
@@ -39,7 +39,7 @@ function authError(code, message, extra = {}) {
39
39
  return err;
40
40
  }
41
41
 
42
- export function createAuthorizationController(ctx, { scope, intentIO, clock = { now: () => Date.now() }, onAuthorized } = {}) {
42
+ export function createAuthorizationController(ctx, { scope, intentIO, clock = { now: () => Date.now() }, onAuthorized, onIntentBump } = {}) {
43
43
  let attempt = null;
44
44
  let riskLatch = null; // { reason, at } — 进程级风险锁存,本进程生命周期内不解除
45
45
 
@@ -69,6 +69,10 @@ export function createAuthorizationController(ctx, { scope, intentIO, clock = {
69
69
  }
70
70
  // 先持久意图版本,失败不发 begin(安全门拒绝动作不产生副作用)
71
71
  const originIntentVersion = intentIO.bump();
72
+ // ADR-0006:栅栏抬升的同一时刻即时作废进行中的模型配置操作(fire-and-forget,不阻断授权主路径)
73
+ if (typeof onIntentBump === "function") {
74
+ try { onIntentBump("auth-start"); } catch { /* 回调异常不阻断授权 */ }
75
+ }
72
76
  const a = {
73
77
  attemptId: randomUUID(),
74
78
  status: "starting",
@@ -143,6 +147,10 @@ export function createAuthorizationController(ctx, { scope, intentIO, clock = {
143
147
  if (riskLatch) throw authError("LOGOUT_UNSAFE", "logout-unsafe");
144
148
  // 本 profile 显式操作:旧预览/模型配置意图先行失效(失败则不动凭据)
145
149
  intentIO.bump();
150
+ // ADR-0006:作废即时化——不等到 boot 漂移检查才暴露(fire-and-forget,不阻断退出主路径)
151
+ if (typeof onIntentBump === "function") {
152
+ try { onIntentBump("logout"); } catch { /* 回调异常不阻断退出 */ }
153
+ }
146
154
  try {
147
155
  const deleted = await ctx.credentials.deleteRecord(CREDENTIAL_KEY);
148
156
  if (deleted === false) throw authError("LOGOUT_FAILED", "logout-failed");
package/src/host.mjs CHANGED
@@ -113,7 +113,43 @@ async function syncAvailableModels(ctx, opts = {}) {
113
113
  ], desc?.revision);
114
114
  }
115
115
 
116
+ function isLoopbackHostname(hostname) {
117
+ if (hostname === "localhost" || hostname === "[::1]") return true;
118
+ const parts = hostname.split(".");
119
+ return parts.length === 4 && parts[0] === "127" && parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) <= 255);
120
+ }
121
+
122
+ // know-how 035 兼容层:webStartup (0.2.1+) → webRuntime (≤0.2.0) → [];非抛式查询。
123
+ function trustedHostsOf(ctx) {
124
+ let startup, runtime;
125
+ try { startup = ctx?.get?.("webStartup"); } catch { /* 未注入即缺席 */ }
126
+ try { runtime = ctx?.get?.("webRuntime"); } catch { /* 同上 */ }
127
+ return startup?.trustedHosts ?? runtime?.trustedHosts ?? [];
128
+ }
129
+
130
+ /** Host 必须是 loopback 或宿主信任名单成员——DNS rebinding 下 Host=攻击者域,必被拒。 */
131
+ function isTrustedHostRequest(req, trustedHosts) {
132
+ const host = req.headers?.host;
133
+ if (typeof host !== "string" || host === "") return false;
134
+ let hostname;
135
+ try {
136
+ hostname = new URL(`http://${host}`).hostname;
137
+ } catch {
138
+ return false;
139
+ }
140
+ if (isLoopbackHostname(hostname)) return true;
141
+ return (trustedHosts ?? []).some((entry) => {
142
+ try {
143
+ const parsed = new URL(`http://${entry}`);
144
+ return parsed.port !== "" ? `${parsed.hostname}:${parsed.port}` === host : parsed.hostname === hostname;
145
+ } catch {
146
+ return false;
147
+ }
148
+ });
149
+ }
150
+
116
151
  function sameOrigin(req) {
152
+ if (req.headers?.["sec-fetch-site"] === "cross-site") return false;
117
153
  const origin = req.headers?.origin;
118
154
  if (origin === undefined || origin === null || origin === "") return true;
119
155
  try {
@@ -123,23 +159,52 @@ function sameOrigin(req) {
123
159
  }
124
160
  }
125
161
 
162
+ /** 官方信任检查的守卫式委派(dsh-m 0.9.0 同款):服务在位 → 官方判定;服务查不到(含未注入名 get 抛错,兼容 0.1.x 无 connection 服务)→ undefined 回落本地栅栏;围栏执行抛错 → fail-closed 403。 */
163
+ function officialRejection(ctx, req) {
164
+ let connection, reject;
165
+ try {
166
+ connection = ctx?.get?.("connection");
167
+ reject = connection?.requestRejection;
168
+ } catch {
169
+ return undefined;
170
+ }
171
+ if (typeof reject !== "function") return undefined;
172
+ try {
173
+ return reject.call(connection, { headers: req.headers }) ?? undefined;
174
+ } catch {
175
+ return 403;
176
+ }
177
+ }
178
+
179
+ /**
180
+ * 路由信任栅栏(2026-11-12 PR 评审同款强化):原 guard 仅 Origin===Host,
181
+ * DNS rebinding(Origin 与 Host 均为攻击者域)可穿透驱动授权/退出/模型目录变更。
182
+ * 现官方 Connection 指纹围栏优先;官方服务缺席回落「Host ∈ loopback∪宿主名单 + 同源」。
183
+ */
184
+ export function createGuard(ctx) {
185
+ return (req, res, method) => {
186
+ const rejection = officialRejection(ctx, req);
187
+ if (rejection !== undefined) {
188
+ json(res, rejection, { ok: false, error: "forbidden origin" });
189
+ return false;
190
+ }
191
+ if (!isTrustedHostRequest(req, trustedHostsOf(ctx)) || !sameOrigin(req)) {
192
+ json(res, 403, { ok: false, error: "forbidden origin" });
193
+ return false;
194
+ }
195
+ if (req.method !== method) {
196
+ json(res, 405, { ok: false, error: "method not allowed" });
197
+ return false;
198
+ }
199
+ return true;
200
+ };
201
+ }
202
+
126
203
  function json(res, code, payload) {
127
204
  res.writeHead(code, { "content-type": "application/json" });
128
205
  res.end(JSON.stringify(payload));
129
206
  }
130
207
 
131
- function guard(req, res, method) {
132
- if (!sameOrigin(req)) {
133
- json(res, 403, { ok: false, error: "forbidden origin" });
134
- return false;
135
- }
136
- if (req.method !== method) {
137
- json(res, 405, { ok: false, error: "method not allowed" });
138
- return false;
139
- }
140
- return true;
141
- }
142
-
143
208
  function readBody(req) {
144
209
  if (req.body !== undefined) return Promise.resolve(req.body);
145
210
  return new Promise((resolve, reject) => {
@@ -364,6 +429,7 @@ function serviceError(res, err) {
364
429
 
365
430
  export function apply(ctx, opts = {}) {
366
431
  const r = routes();
432
+ const guard = createGuard(ctx); // 全路由信任检查:官方指纹围栏优先,缺席回落 Host 栅栏 + 同源
367
433
  const scope = resolveRuntimeScope(ctx);
368
434
  const intentIO = createIntentIO(scope);
369
435
  const legacyPath = opts.stateFile ?? defaultStateFile();
@@ -389,11 +455,24 @@ export function apply(ctx, opts = {}) {
389
455
  ctx.logger?.warn?.("copilot-auth: model sync failed: %s", lastSyncError);
390
456
  });
391
457
 
458
+ // ADR-0006:授权动作即时作废进行中的模型配置操作。service 晚于 controller 创建(L479),
459
+ // 用晚绑定 ref;fire-and-forget,失败只记日志,绝不影响授权动作本身。
460
+ let refreshServiceRef = null;
461
+ const onIntentBump = (source) => {
462
+ const svc = refreshServiceRef;
463
+ const invalidate = svc?.invalidateForAuthAction;
464
+ if (typeof invalidate !== "function") return;
465
+ Promise.resolve(invalidate.call(svc, source)).catch((err) => {
466
+ ctx.logger?.warn?.("copilot-auth: invalidate for %s failed: %s", source, String(err?.message ?? err));
467
+ });
468
+ };
469
+
392
470
  // 授权控制器(T3):软撤回+风险锁存;退出/重新授权在当前宿主下统一禁用。
393
471
  const controller = createAuthorizationController(ctx, {
394
472
  scope,
395
473
  intentIO,
396
474
  onAuthorized: (handoff) => { void runSync(handoff); },
475
+ onIntentBump,
397
476
  });
398
477
 
399
478
  // ---- 初始化期能力探测(Q23:GET status 不执行写探针,通道分类来自此处缓存) ----
@@ -488,6 +567,7 @@ export function apply(ctx, opts = {}) {
488
567
  transaction,
489
568
  fetchImpl,
490
569
  });
570
+ refreshServiceRef = refreshService; // ADR-0006:晚绑定回填(见 onIntentBump)
491
571
 
492
572
  // 启动序列(boot 恢复+保守激活/自愈,T9 bootRecover);绝不阻断挂载。
493
573
  void refreshService.bootReady()
@@ -588,7 +668,12 @@ export function apply(ctx, opts = {}) {
588
668
  activated: st?.activated === true,
589
669
  activeOperation: st?.activeOperation ?? null,
590
670
  lastResult: st?.lastResult ?? null,
591
- pendingRestart: !!st?.restartState || st?.activeOperation?.phaseResult?.status === "pending-restart",
671
+ // 评审 Q4(ADR-0006):作废(intent-retired)后内核 restartState 残留不得再宣称
672
+ // 「待重启生效」——activeOperation 已清、无任何待提交配置,重启不再完成任何更改;
673
+ // 正常 pending-restart(lastResult 非 intent-retired)行为不变,水合判序零改动
674
+ pendingRestart: (st?.activeOperation == null && st?.lastResult?.status === "intent-retired")
675
+ ? false
676
+ : (!!st?.restartState || st?.activeOperation?.phaseResult?.status === "pending-restart"),
592
677
  legacyStateDetected: loaded.flags.legacyStateDetected === true,
593
678
  stateUnknownVersion: loaded.flags.unknownVersion === true,
594
679
  piAiVersion: installInfo?.version ?? null,
@@ -3,8 +3,9 @@
3
3
  // 状态:idle → previewing → confirming(勾选→materialize 循环;缓存证据+删除提案时
4
4
  // 自动 live 升级:闩锁、硬失败回退 materialize、软降级置 staleNotice)→ applying →
5
5
  // result / pendingRestart / checking / busy / resultUnknown;recovery-needed 经
6
- // retireConfirm → retiring → 重新预览闭环;busy 态在 active.phaseResult.status ===
7
- // "recovery-needed" 时同样提供 retire 出口(032),水合时该判据优先于旧 lastResult
6
+ // retireConfirm → retiring → 重新预览闭环;busy 态在 active.phaseResult.status 为
7
+ // "recovery-needed"(032)或 "pending-restart"(ADR-0006/v1.2.25:放弃等待重启)时
8
+ // 同样提供 retire 出口,水合时 recovery-needed 判据优先于旧 lastResult
8
9
  // (防旧 "applied" 遮蔽卡死操作),retire 失败回 busy 可重试。
9
10
  // 关键规则:
10
11
  // - 勾选集合即终态;选择变化经 basePreviewId 从原快照 materialize(不重新取数)
@@ -110,10 +111,18 @@ export function reduce(state, event) {
110
111
  if (event.type === "retire-request" && state.name === "result") {
111
112
  return [{ name: "retireConfirm", result: state.result }, null];
112
113
  }
113
- // 032:busy 态的 retire 出口——仅当卡死操作已 recovery-needed(静止核实后清,绝不写配置)
114
- if (event.type === "retire-request" && state.name === "busy" && state.active?.phaseResult?.status === "recovery-needed") {
114
+ // 032 + ADR-0006:busy 态的 retire 出口——卡死操作(recovery-needed)与挂起重启的
115
+ // 操作(pending-restart)都可放弃(静止核实后清,绝不写配置);文案由渲染层按态区分
116
+ if (event.type === "retire-request" && state.name === "busy" && ["recovery-needed", "pending-restart"].includes(state.active?.phaseResult?.status ?? "")) {
115
117
  return [{ name: "retireConfirm", active: state.active }, null];
116
118
  }
119
+ // ADR-0006/v1.2.25(Q5 + Q6):pendingRestart 态主视图的放弃出口——优先取 active
120
+ // (真实占用操作,跨会话水合落点),result 兜底仅认 pending-restart 终态(排除旧
121
+ // lastResult 残影与终态残留的误导出口)
122
+ if (event.type === "retire-request" && state.name === "pendingRestart"
123
+ && (state.active?.operationId || (state.result?.status === "pending-restart" && state.result?.operationId))) {
124
+ return [{ name: "retireConfirm", result: state.result, active: state.active ?? null }, null];
125
+ }
117
126
  if (event.type === "dismiss" && state.name === "failed") {
118
127
  return [{ ...initial, flags: state.flags }, null];
119
128
  }
@@ -127,7 +136,9 @@ export function reduce(state, event) {
127
136
  return [{ ...base, name: "busy", active: refresh.activeOperation }, null];
128
137
  }
129
138
  if (restartStates.has(refresh?.lastResult?.status ?? "") || refresh?.pendingRestart === true) {
130
- return [{ ...base, name: "pendingRestart", result: refresh.lastResult }, null];
139
+ // 评审 Q6:水合落点必须携带 active(真实占用操作的 operationId)——lastResult 可能是
140
+ // 旧操作残影(如旧 applied),放弃出口若取旧 id 会 operation-mismatch 永远失败
141
+ return [{ ...base, name: "pendingRestart", result: refresh.lastResult, active: refresh.activeOperation ?? null }, null];
131
142
  }
132
143
  const st = refresh?.lastResult?.status;
133
144
  if (st && ["recovery-needed", "rollback-conflict", "conflict", "partial", "rolled-back", "intent-retired", "applied"].includes(st)) {
@@ -281,19 +292,26 @@ export function reduce(state, event) {
281
292
  }
282
293
  case "retireConfirm": {
283
294
  if (event.type === "retire-confirm") {
284
- // 双入口:result 入口携 result;busy 入口(032)携 active——operationId 二选一
285
- return [{ name: "retiring", operationId: state.result?.operationId ?? state.active?.operationId, result: state.result, active: state.active }, { type: "retire", operationId: state.result?.operationId ?? state.active?.operationId }];
295
+ // 双入口:busy/pendingRestart 入口携 active(真实占用操作——Q6 起 active 优先,
296
+ // 防 lastResult 残影 id 造成 operation-mismatch);result 入口携 result
297
+ const retireId = state.active?.operationId ?? state.result?.operationId;
298
+ return [{ name: "retiring", operationId: retireId, result: state.result, active: state.active }, { type: "retire", operationId: retireId }];
299
+ }
300
+ if (event.type === "cancel") {
301
+ // Q5:pendingRestart 入口取消回 pendingRestart(携待重启横幅),不落进无提示的 result 空态
302
+ if (state.active) return [{ name: "busy", active: state.active }, null];
303
+ if (state.result?.status === "pending-restart") return [{ name: "pendingRestart", result: state.result }, null];
304
+ return [{ name: "result", result: state.result }, null];
286
305
  }
287
- if (event.type === "cancel") return state.active ? [{ name: "busy", active: state.active }, null] : [{ name: "result", result: state.result }, null];
288
306
  return [state, null];
289
307
  }
290
308
  case "retiring": {
291
309
  if (event.type === "retire-ok") return [{ ...state, name: "checking" }, { type: "status" }];
292
310
  if (event.type === "retire-fail") {
293
- // 032:busy 入口失败回 busy 可重试;result 入口维持既有落点
294
- return state.active
295
- ? [{ name: "busy", active: state.active, retireError: event.error }, null]
296
- : [{ name: "result", result: state.result, retireError: event.error }, null];
311
+ // 032:busy 入口失败回 busy 可重试;Q5:pendingRestart 入口失败回 pendingRestart 可重试;result 入口维持既有落点
312
+ if (state.active) return [{ name: "busy", active: state.active, retireError: event.error }, null];
313
+ if (state.result?.status === "pending-restart") return [{ name: "pendingRestart", result: state.result, retireError: event.error }, null];
314
+ return [{ name: "result", result: state.result, retireError: event.error }, null];
297
315
  }
298
316
  return [state, null];
299
317
  }
@@ -314,6 +314,24 @@ export function createRefreshService(ctx, {
314
314
  return queue(() => transaction.retire({ operationId: body?.operationId }));
315
315
  }
316
316
 
317
+ // ADR-0006:授权动作(start/logout)bump 意图栅栏的同一时刻,即时终结进行中的 refresh 操作。
318
+ // 幂等:无 activeOperation 时 {ok:true, invalidated:false};operationId 恒 undefined
319
+ // (transaction.retire 语义=终结当前 active,无需 service 层读状态或校验归属)。
320
+ async function invalidateForAuthAction(source) {
321
+ requireScope();
322
+ if (source !== "auth-start" && source !== "logout") {
323
+ throw flowError("INVALID_SOURCE", "invalid invalidate source");
324
+ }
325
+ if (!transaction) throw flowError("DEPS_MISSING", "transaction kernel not wired");
326
+ const res = await queue(() => transaction.retire({ operationId: undefined, reason: `superseded-by-auth-action:${source}` }));
327
+ // 评审 Q3:真内核在无 active 且 lastResult 为空时会误入 idempotent 分支
328
+ // (lastResult.operationId 与 undefined 严格相等误命中),该情形下没有任何操作被
329
+ // 终结 → 映射 invalidated:false,不虚报契约语义;内核 retire 语义零改动(全局约束 1)。
330
+ return res?.ok && res?.idempotent !== true
331
+ ? { ok: true, invalidated: true, result: res.result }
332
+ : { ok: true, invalidated: false };
333
+ }
334
+
317
335
  async function bootReady() {
318
336
  requireScope();
319
337
  if (!transaction) return { ok: true, skipped: true };
@@ -332,5 +350,5 @@ export function createRefreshService(ctx, {
332
350
  return snapshots.size;
333
351
  }
334
352
 
335
- return { preview, apply, status, retire, bootReady, dispose, snapshotCount };
353
+ return { preview, apply, status, retire, invalidateForAuthAction, bootReady, dispose, snapshotCount };
336
354
  }
@@ -390,7 +390,9 @@ export function createTransaction(ctx, {
390
390
  const active = st?.activeOperation;
391
391
  if (!active) return { ok: true, active: false };
392
392
 
393
- // intentVersion 漂移:不消费旧 target,标记 recovery-needed(用户经 retire 显式结束后重新预览)
393
+ // intentVersion 漂移:不消费旧 target,标记 recovery-needed(用户经 retire 显式结束后重新预览)。
394
+ // 兜底定位(ADR-0006,v1.2.25 起):正常路径的授权失效已由 auth 动作即时作废
395
+ // (invalidateForAuthAction),此处只应被崩溃窗口/外部改写触达。
394
396
  if (active.intentVersion !== readIntentVersion()) {
395
397
  const next = stateIO.load();
396
398
  next.activeOperation = { ...active, phaseResult: { status: "recovery-needed", error: "intent-version-drift" } };
@@ -544,7 +546,9 @@ export function createTransaction(ctx, {
544
546
  }
545
547
 
546
548
  // ---- retire:静止核实后清 activeOperation(服务队列内串行即静止;绝不写配置) ----
547
- async function retire({ operationId } = {}) {
549
+ // reason(ADR-0006):授权动作即时作废时携带受控原因串(superseded-by-auth-action:<source>),
550
+ // 透传为 lastResult.error;未携带时维持既有 drift 语义。
551
+ async function retire({ operationId, reason } = {}) {
548
552
  const state = stateIO.load();
549
553
  const active = state?.activeOperation;
550
554
  if (!active) {
@@ -555,13 +559,14 @@ export function createTransaction(ctx, {
555
559
  return { ok: false, error: "operation-mismatch" };
556
560
  }
557
561
  const drift = active.intentVersion !== readIntentVersion();
562
+ const reasonText = typeof reason === "string" && reason.length > 0 && reason.length <= 120 ? reason : null;
558
563
  const result = {
559
564
  operationId: active.operationId,
560
565
  status: "intent-retired",
561
566
  phase: active.phase,
562
567
  drift,
563
568
  changes: null,
564
- error: drift ? "intent-version-drift: old target not consumed" : null,
569
+ error: reasonText ?? (drift ? "intent-version-drift: old target not consumed" : null),
565
570
  };
566
571
  const next = { ...state, activeOperation: null, lastResult: result };
567
572
  next.intentVersion = readIntentVersion();