dsh-codearts 1.3.0 → 1.4.0
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/CHANGELOG.md +598 -559
- package/lib/{auth-CF9XHMX-.mjs → auth-Ch0vYSAo.mjs} +327 -48
- package/lib/bin.mjs +2 -2
- package/lib/client/index.d.cts +15 -0
- package/lib/client/index.js +454 -176
- package/lib/index.d.mts +71 -4
- package/lib/index.mjs +214 -80
- package/lib/keeper-main.mjs +20 -9
- package/lib/{version-CJTTrulR.mjs → version-BjBwEMks.mjs} +3 -3
- package/package.json +125 -125
- package/lib/{keeper-P4FkDUEd.mjs → keeper-MHeAp_eV.mjs} +1 -1
|
@@ -138,6 +138,46 @@ function buildTokenBody(cfg, params) {
|
|
|
138
138
|
...params
|
|
139
139
|
}).toString();
|
|
140
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* 华为 STS 的 token 端点错误。
|
|
143
|
+
*
|
|
144
|
+
* 必须把 HTTP 状态带出来:续期失败到底该不该把账号判死,全看这一层 —— 网络抖动 /
|
|
145
|
+
* 5xx / 429 是可以重试的,`invalid_grant`(refresh_token 真失效)才必须重新登录。
|
|
146
|
+
* 早先这里抛的是拼好的字符串,调用方只能整段 `catch {}`,于是任何一次抖动都会把
|
|
147
|
+
* 凭证永久写成 stale(issue #8)。
|
|
148
|
+
*/
|
|
149
|
+
var OAuthTokenError = class extends Error {
|
|
150
|
+
status;
|
|
151
|
+
body;
|
|
152
|
+
constructor(action, status, body) {
|
|
153
|
+
super(`${action === "refresh" ? "refresh token" : "exchange code"} failed: ${status} ${body}`);
|
|
154
|
+
this.name = "OAuthTokenError";
|
|
155
|
+
this.status = status;
|
|
156
|
+
this.body = body;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 凭证本身失效(只能重新登录),而不是这一趟请求/网络的问题。
|
|
160
|
+
*
|
|
161
|
+
* 4xx 里除了 408(超时)和 429(限流)都属于这一类:`invalid_grant` /
|
|
162
|
+
* `invalid_request` / 401 / 403 重试多少次都是同一个结果。
|
|
163
|
+
*/
|
|
164
|
+
get permanent() {
|
|
165
|
+
if (this.status === 408 || this.status === 429) return false;
|
|
166
|
+
return this.status >= 400 && this.status < 500;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* refresh_token 已经被别处用掉(一次性令牌的轮换现象)。
|
|
170
|
+
*
|
|
171
|
+
* 这**不等于**凭证失效:守护进程(keeper)和插件各有一份同一账号的刷新权时,
|
|
172
|
+
* 先刷的一方拿走了新令牌,后刷的一方必然撞上这个码。处理方式是重读磁盘拿新令牌
|
|
173
|
+
* 再试,而不是把账号判死(2026-11-13 实测:判死的同时还把 keeper 刚写回来的新
|
|
174
|
+
* refresh_token 覆盖成了旧的,只能重新登录)。
|
|
175
|
+
*/
|
|
176
|
+
get refreshTokenUsed() {
|
|
177
|
+
if (this.status !== 400) return false;
|
|
178
|
+
return this.body.includes("STS5.1806") || /has been used/i.test(this.body);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
141
181
|
/** 用授权 code 换 token。 */
|
|
142
182
|
async function exchangeCode(cfg, code, verifier, port, dpopJWK, abort) {
|
|
143
183
|
const htu = `${cfg.stsHost}/v1/oauth2/tokens`;
|
|
@@ -161,7 +201,7 @@ async function exchangeCode(cfg, code, verifier, port, dpopJWK, abort) {
|
|
|
161
201
|
});
|
|
162
202
|
if (!res.ok) {
|
|
163
203
|
const text = await res.text();
|
|
164
|
-
throw new
|
|
204
|
+
throw new OAuthTokenError("exchange", res.status, text);
|
|
165
205
|
}
|
|
166
206
|
return parseToken(await res.json(), dpopJWK, cfg.clientId);
|
|
167
207
|
}
|
|
@@ -186,7 +226,7 @@ async function refreshToken(cfg, prev, abort) {
|
|
|
186
226
|
});
|
|
187
227
|
if (!res.ok) {
|
|
188
228
|
const text = await res.text();
|
|
189
|
-
throw new
|
|
229
|
+
throw new OAuthTokenError("refresh", res.status, text);
|
|
190
230
|
}
|
|
191
231
|
return parseToken(await res.json(), prev.dpopPrivateKey, prev.clientId, prev);
|
|
192
232
|
}
|
|
@@ -410,11 +450,46 @@ async function claimIfPossible(cred) {
|
|
|
410
450
|
* (见 index.ts 的 keepAliveLoop),只要 DSH 开着就几乎不可能真的到期。
|
|
411
451
|
*/
|
|
412
452
|
const REFRESH_SKEW_MS = 18e5;
|
|
453
|
+
/**
|
|
454
|
+
* 一次续期最多试几次。
|
|
455
|
+
*
|
|
456
|
+
* 只对「可能自己好」的失败(网络、超时、5xx、429)重试;`invalid_grant` 这类
|
|
457
|
+
* 试多少次都一样,第一趟就收工。issue #8 之前是「任何失败一次就落 stale」,
|
|
458
|
+
* 一次网络抖动 = 永久失效,只能重新登录。
|
|
459
|
+
*/
|
|
460
|
+
const REFRESH_MAX_ATTEMPTS = 3;
|
|
461
|
+
/** 重试退避基数(250ms → 750ms),总等待不到 1 秒,不给请求路径添负担。 */
|
|
462
|
+
const REFRESH_RETRY_BASE_MS = 250;
|
|
463
|
+
/**
|
|
464
|
+
* 瞬时失败用尽重试后,多久内不再碰 refresh_token。
|
|
465
|
+
*
|
|
466
|
+
* 不设这个闸门的话,token 已过期时每个请求都会再刷一遍 —— 上游抖动期间会把
|
|
467
|
+
* 一次性 refresh_token 反复烧掉,把「暂时失败」放大成「真的失效」。
|
|
468
|
+
*/
|
|
469
|
+
const REFRESH_TRANSIENT_BLOCK_MS = 6e4;
|
|
470
|
+
/** 失败是不是「凭证真的没了」;只有这一类才允许写 `stale`。 */
|
|
471
|
+
function isPermanentRefreshFailure(error) {
|
|
472
|
+
return error instanceof OAuthTokenError && error.permanent;
|
|
473
|
+
}
|
|
474
|
+
/** 失败是不是「refresh_token 被别处用掉了」(一次性令牌轮换,可以重读磁盘恢复)。 */
|
|
475
|
+
function isRefreshTokenUsed(error) {
|
|
476
|
+
return error instanceof OAuthTokenError && error.refreshTokenUsed;
|
|
477
|
+
}
|
|
478
|
+
/** 日志里只放原因与状态码,别把整段响应体抄进去(可能有账号信息)。 */
|
|
479
|
+
function describeRefreshFailure(error) {
|
|
480
|
+
if (error instanceof OAuthTokenError) return `HTTP ${error.status} ${error.body.slice(0, 200)}`;
|
|
481
|
+
return error instanceof Error ? error.message : String(error);
|
|
482
|
+
}
|
|
483
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
413
484
|
var CodeArtsCredentialStore = class {
|
|
414
485
|
_current;
|
|
415
486
|
_path;
|
|
416
487
|
/** 正在进行的刷新。并发请求必须共用它,不能各刷一次。 */
|
|
417
488
|
_inflight;
|
|
489
|
+
/** 瞬时失败后的冷却:这之前不再拿 refresh_token 去撞上游。 */
|
|
490
|
+
_refreshBlockedUntil = 0;
|
|
491
|
+
/** 续期失败的落地日志(由宿主注入;测试里没有就是静默)。 */
|
|
492
|
+
log;
|
|
418
493
|
/**
|
|
419
494
|
* 凭证代际。重登/清除会推进它,让还在飞的刷新发现自己过期,
|
|
420
495
|
* 从而不把刷新结果写回去覆盖掉刚拿到的新凭证。
|
|
@@ -424,9 +499,11 @@ var CodeArtsCredentialStore = class {
|
|
|
424
499
|
invalidateInflight() {
|
|
425
500
|
this._gen += 1;
|
|
426
501
|
this._inflight = void 0;
|
|
502
|
+
this._refreshBlockedUntil = 0;
|
|
427
503
|
}
|
|
428
|
-
constructor(path) {
|
|
504
|
+
constructor(path, log) {
|
|
429
505
|
this._path = path;
|
|
506
|
+
this.log = log;
|
|
430
507
|
}
|
|
431
508
|
setPath(path) {
|
|
432
509
|
this.invalidateInflight();
|
|
@@ -491,15 +568,19 @@ var CodeArtsCredentialStore = class {
|
|
|
491
568
|
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return cred;
|
|
492
569
|
const exp = cred.expiration ? Date.parse(cred.expiration) : NaN;
|
|
493
570
|
if (!(Number.isNaN(exp) ? false : exp - Date.now() < REFRESH_SKEW_MS)) return cred;
|
|
571
|
+
if (Date.now() < this._refreshBlockedUntil) return cred;
|
|
572
|
+
return (await this.runRefresh(cred)).cred;
|
|
573
|
+
}
|
|
574
|
+
/** 真正去刷一次;失败按错误类别决定「重试 / 冷却 / 落 stale」。 */
|
|
575
|
+
async runRefresh(cred) {
|
|
494
576
|
return this._inflight ??= this.refresh(cred, {
|
|
495
|
-
refreshToken: cred.refreshToken,
|
|
577
|
+
refreshToken: cred.refreshToken ?? "",
|
|
496
578
|
dpopPrivateKey: cred.dpopPrivateKey,
|
|
497
|
-
clientId: cred.clientId
|
|
579
|
+
clientId: cred.clientId ?? ""
|
|
498
580
|
}).finally(() => {
|
|
499
581
|
this._inflight = void 0;
|
|
500
582
|
});
|
|
501
583
|
}
|
|
502
|
-
/** 真正去刷一次;失败则把凭证标成 stale 落库,让卡片提示重新登录。 */
|
|
503
584
|
async refresh(cred, mat) {
|
|
504
585
|
const gen = this._gen;
|
|
505
586
|
/**
|
|
@@ -508,7 +589,8 @@ var CodeArtsCredentialStore = class {
|
|
|
508
589
|
* 确认「当前凭证仍然是这次刷新的那一份」。
|
|
509
590
|
*/
|
|
510
591
|
const stillCurrent = () => this._gen === gen && this._current === cred;
|
|
511
|
-
|
|
592
|
+
let lastError;
|
|
593
|
+
for (let attempt = 1; attempt <= REFRESH_MAX_ATTEMPTS; attempt += 1) try {
|
|
512
594
|
const next = credentialFromOAuth(await refreshToken(defaultLoginConfig(), {
|
|
513
595
|
userId: cred.userId ?? "",
|
|
514
596
|
userName: cred.userName ?? "",
|
|
@@ -521,20 +603,47 @@ var CodeArtsCredentialStore = class {
|
|
|
521
603
|
clientId: mat.clientId,
|
|
522
604
|
dpopPrivateKey: mat.dpopPrivateKey
|
|
523
605
|
}), cred.label);
|
|
524
|
-
|
|
606
|
+
this._refreshBlockedUntil = 0;
|
|
607
|
+
if (!stillCurrent()) return {
|
|
608
|
+
ok: true,
|
|
609
|
+
cred: next
|
|
610
|
+
};
|
|
525
611
|
await saveCredential(this._path, next);
|
|
526
612
|
this._current = next;
|
|
527
613
|
await this.claimIfPossible(next);
|
|
528
|
-
return
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
614
|
+
return {
|
|
615
|
+
ok: true,
|
|
616
|
+
cred: next
|
|
617
|
+
};
|
|
618
|
+
} catch (error) {
|
|
619
|
+
lastError = error;
|
|
620
|
+
if (isPermanentRefreshFailure(error)) break;
|
|
621
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} attempt ${attempt}/${REFRESH_MAX_ATTEMPTS} failed (transient): ${describeRefreshFailure(error)}`);
|
|
622
|
+
if (attempt < REFRESH_MAX_ATTEMPTS) await sleep(REFRESH_RETRY_BASE_MS * 2 ** (attempt - 1));
|
|
537
623
|
}
|
|
624
|
+
const used = isRefreshTokenUsed(lastError);
|
|
625
|
+
const permanent = !used && isPermanentRefreshFailure(lastError);
|
|
626
|
+
const reason = describeRefreshFailure(lastError);
|
|
627
|
+
if (permanent) cred.stale = true;
|
|
628
|
+
else if (used) this._refreshBlockedUntil = Date.now() + 1e4;
|
|
629
|
+
else this._refreshBlockedUntil = Date.now() + REFRESH_TRANSIENT_BLOCK_MS;
|
|
630
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} failed (${permanent ? "permanent, marked stale" : used ? "refresh token already used elsewhere, will reload" : `transient, retry in ${Math.round(REFRESH_TRANSIENT_BLOCK_MS / 1e3)}s`}): ${reason}`);
|
|
631
|
+
if (!stillCurrent()) return {
|
|
632
|
+
ok: false,
|
|
633
|
+
cred: this._current,
|
|
634
|
+
permanent,
|
|
635
|
+
reason
|
|
636
|
+
};
|
|
637
|
+
this._current = cred;
|
|
638
|
+
if (permanent) try {
|
|
639
|
+
await saveCredential(this._path, cred);
|
|
640
|
+
} catch {}
|
|
641
|
+
return {
|
|
642
|
+
ok: false,
|
|
643
|
+
cred,
|
|
644
|
+
permanent,
|
|
645
|
+
reason
|
|
646
|
+
};
|
|
538
647
|
}
|
|
539
648
|
};
|
|
540
649
|
/**
|
|
@@ -546,6 +655,26 @@ function codeartsAccountsPath(home) {
|
|
|
546
655
|
return join(base, ".codearts-accounts.json");
|
|
547
656
|
}
|
|
548
657
|
/**
|
|
658
|
+
* 续期失败的落地日志路径。
|
|
659
|
+
*
|
|
660
|
+
* 宿主对插件 `info`/`debug` 日志没有出口,`warn` 会弹系统通知,所以「续期为什么失败」
|
|
661
|
+
* 只能写插件自己的文件。只记原因、状态码和是否落 stale,不含任何 token。
|
|
662
|
+
*/
|
|
663
|
+
function codeartsRefreshLogPath(home) {
|
|
664
|
+
const base = home ?? process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
665
|
+
return join(base, ".codearts-refresh.log");
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* 槽位取证日志路径(只有 `DSH_CODEARTS_DEBUG=1` 时才写)。
|
|
669
|
+
*
|
|
670
|
+
* 记 acquire/release 的时间点与在飞计数,用来证实或排除「本地槽位泄漏」
|
|
671
|
+
* (issue #8)。默认关闭:正常使用不需要这份噪音。
|
|
672
|
+
*/
|
|
673
|
+
function codeartsDebugLogPath(home) {
|
|
674
|
+
const base = home ?? process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
675
|
+
return join(base, ".codearts-debug.log");
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
549
678
|
* 账号的自然键,按稳定性依次退让:
|
|
550
679
|
*
|
|
551
680
|
* 1. `userId` —— 最理想,同一个人永远同一个键。
|
|
@@ -592,17 +721,37 @@ var AccountSlot = class {
|
|
|
592
721
|
_inflight;
|
|
593
722
|
_gen = 0;
|
|
594
723
|
persist;
|
|
595
|
-
|
|
724
|
+
log;
|
|
725
|
+
/** 瞬时失败后的冷却:这之前不再拿 refresh_token 去撞上游。 */
|
|
726
|
+
_refreshBlockedUntil = 0;
|
|
727
|
+
/** 最近一次续期失败的类型,供 store 判断要不要「重读磁盘再试一次」。 */
|
|
728
|
+
_lastFailure;
|
|
729
|
+
constructor(cred, persist, log) {
|
|
596
730
|
this._cred = cred;
|
|
597
731
|
this.persist = persist;
|
|
732
|
+
this.log = log;
|
|
598
733
|
}
|
|
599
734
|
get credential() {
|
|
600
735
|
return this._cred;
|
|
601
736
|
}
|
|
737
|
+
/** 最近一次续期失败的类型(只在失败后读;下一次成功会清空)。 */
|
|
738
|
+
get lastFailure() {
|
|
739
|
+
return this._lastFailure;
|
|
740
|
+
}
|
|
741
|
+
/** 把这份凭证标成 stale 并落库(store 在「令牌被别人用掉且磁盘也没有更新」时调)。 */
|
|
742
|
+
async markStale() {
|
|
743
|
+
if (this._cred.stale === true) return;
|
|
744
|
+
this._cred = {
|
|
745
|
+
...this._cred,
|
|
746
|
+
stale: true
|
|
747
|
+
};
|
|
748
|
+
await this.persist(this._cred);
|
|
749
|
+
}
|
|
602
750
|
/** 账号被替换或删除:作废在飞的刷新,别让它回头写回旧值。 */
|
|
603
751
|
invalidate() {
|
|
604
752
|
this._gen += 1;
|
|
605
753
|
this._inflight = void 0;
|
|
754
|
+
this._refreshBlockedUntil = 0;
|
|
606
755
|
}
|
|
607
756
|
async ensureFresh() {
|
|
608
757
|
const cred = this._cred;
|
|
@@ -610,7 +759,9 @@ var AccountSlot = class {
|
|
|
610
759
|
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return cred;
|
|
611
760
|
const exp = cred.expiration ? Date.parse(cred.expiration) : NaN;
|
|
612
761
|
if (!(Number.isNaN(exp) ? false : exp - Date.now() < REFRESH_SKEW_MS)) return cred;
|
|
613
|
-
|
|
762
|
+
if (Date.now() < this._refreshBlockedUntil) return cred;
|
|
763
|
+
await this.runRefresh(cred);
|
|
764
|
+
return this._cred;
|
|
614
765
|
}
|
|
615
766
|
/**
|
|
616
767
|
* 不管还剩多久,立刻续期一次。
|
|
@@ -618,15 +769,39 @@ var AccountSlot = class {
|
|
|
618
769
|
* 给上游回 `invalid_iam_token` 时用:token 可能被提前作废(另一个客户端续期、
|
|
619
770
|
* 上游轮换),而 `expiration` 还写着几小时之后,`ensureFresh` 根本不会动。
|
|
620
771
|
* 仍然走单飞,避免并发请求把一次性的 refresh_token 烧掉。
|
|
772
|
+
*
|
|
773
|
+
* **失败返回 undefined**:调用方拿它判断「要不要用同一个号重试」。以前失败也
|
|
774
|
+
* 返回凭据(已 stale 的旧 token),`!== undefined` 恒成立 ⇒ 必然拿死 token 再打
|
|
775
|
+
* 一次上游(issue #8 的第二个根因)。
|
|
621
776
|
*/
|
|
622
777
|
async forceRefresh() {
|
|
623
778
|
const cred = this._cred;
|
|
624
|
-
if (cred.stale === true) return
|
|
625
|
-
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return
|
|
779
|
+
if (cred.stale === true) return void 0;
|
|
780
|
+
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return void 0;
|
|
781
|
+
if (Date.now() < this._refreshBlockedUntil) return void 0;
|
|
782
|
+
const outcome = await this.runRefresh(cred);
|
|
783
|
+
return outcome.ok ? outcome.cred : void 0;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* 复验一个被判 stale 的账号:拿 refresh_token 再试一次。
|
|
787
|
+
*
|
|
788
|
+
* 只给后台保活循环用(20 分钟一轮)。为什么需要:1.3.0 及以前只要有**一次**续期
|
|
789
|
+
* 失败就永久落 stale,网络抖动也会 —— 那些被误判的账号不该逼用户重新登录。
|
|
790
|
+
* 真的 invalid_grant 会继续失败,一轮只多一个请求,代价可以忽略。
|
|
791
|
+
*/
|
|
792
|
+
async verifyStale() {
|
|
793
|
+
const cred = this._cred;
|
|
794
|
+
if (cred.stale !== true) return true;
|
|
795
|
+
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return false;
|
|
796
|
+
if (Date.now() < this._refreshBlockedUntil) return false;
|
|
797
|
+
return (await this.runRefresh(cred)).ok;
|
|
798
|
+
}
|
|
799
|
+
/** 单飞执行一次续期。 */
|
|
800
|
+
async runRefresh(cred) {
|
|
626
801
|
return this._inflight ??= this.refresh(cred, {
|
|
627
|
-
refreshToken: cred.refreshToken,
|
|
802
|
+
refreshToken: cred.refreshToken ?? "",
|
|
628
803
|
dpopPrivateKey: cred.dpopPrivateKey,
|
|
629
|
-
clientId: cred.clientId
|
|
804
|
+
clientId: cred.clientId ?? ""
|
|
630
805
|
}).finally(() => {
|
|
631
806
|
this._inflight = void 0;
|
|
632
807
|
});
|
|
@@ -634,7 +809,8 @@ var AccountSlot = class {
|
|
|
634
809
|
async refresh(cred, mat) {
|
|
635
810
|
const gen = this._gen;
|
|
636
811
|
const stillCurrent = () => this._gen === gen && this._cred === cred;
|
|
637
|
-
|
|
812
|
+
let lastError;
|
|
813
|
+
for (let attempt = 1; attempt <= REFRESH_MAX_ATTEMPTS; attempt += 1) try {
|
|
638
814
|
const next = credentialFromOAuth(await refreshToken(defaultLoginConfig(), {
|
|
639
815
|
userId: cred.userId ?? "",
|
|
640
816
|
userName: cred.userName ?? "",
|
|
@@ -647,20 +823,53 @@ var AccountSlot = class {
|
|
|
647
823
|
clientId: mat.clientId,
|
|
648
824
|
dpopPrivateKey: mat.dpopPrivateKey
|
|
649
825
|
}), cred.label);
|
|
650
|
-
|
|
826
|
+
this._refreshBlockedUntil = 0;
|
|
827
|
+
this._lastFailure = void 0;
|
|
828
|
+
if (!stillCurrent()) return {
|
|
829
|
+
ok: true,
|
|
830
|
+
cred: next
|
|
831
|
+
};
|
|
651
832
|
await this.persist(next);
|
|
652
833
|
this._cred = next;
|
|
653
834
|
await claimIfPossible(next);
|
|
654
|
-
return
|
|
655
|
-
|
|
835
|
+
return {
|
|
836
|
+
ok: true,
|
|
837
|
+
cred: next
|
|
838
|
+
};
|
|
839
|
+
} catch (error) {
|
|
840
|
+
lastError = error;
|
|
841
|
+
if (isPermanentRefreshFailure(error)) break;
|
|
842
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} attempt ${attempt}/${REFRESH_MAX_ATTEMPTS} failed (transient): ${describeRefreshFailure(error)}`);
|
|
843
|
+
if (attempt < REFRESH_MAX_ATTEMPTS) await sleep(REFRESH_RETRY_BASE_MS * 2 ** (attempt - 1));
|
|
844
|
+
}
|
|
845
|
+
const used = isRefreshTokenUsed(lastError);
|
|
846
|
+
const permanent = !used && isPermanentRefreshFailure(lastError);
|
|
847
|
+
const reason = describeRefreshFailure(lastError);
|
|
848
|
+
if (used) this._lastFailure = "used";
|
|
849
|
+
else if (permanent) {
|
|
850
|
+
this._lastFailure = "permanent";
|
|
656
851
|
cred.stale = true;
|
|
657
|
-
|
|
658
|
-
this.
|
|
659
|
-
|
|
660
|
-
await this.persist(cred);
|
|
661
|
-
} catch {}
|
|
662
|
-
return cred;
|
|
852
|
+
} else {
|
|
853
|
+
this._lastFailure = "transient";
|
|
854
|
+
this._refreshBlockedUntil = Date.now() + REFRESH_TRANSIENT_BLOCK_MS;
|
|
663
855
|
}
|
|
856
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} failed (${used ? "refresh token already used elsewhere, will reload from disk" : permanent ? "permanent, marked stale" : `transient, retry in ${Math.round(REFRESH_TRANSIENT_BLOCK_MS / 1e3)}s`}): ${reason}`);
|
|
857
|
+
if (!stillCurrent()) return {
|
|
858
|
+
ok: false,
|
|
859
|
+
cred: this._cred,
|
|
860
|
+
permanent,
|
|
861
|
+
reason
|
|
862
|
+
};
|
|
863
|
+
this._cred = cred;
|
|
864
|
+
if (permanent) try {
|
|
865
|
+
await this.persist(cred);
|
|
866
|
+
} catch {}
|
|
867
|
+
return {
|
|
868
|
+
ok: false,
|
|
869
|
+
cred,
|
|
870
|
+
permanent,
|
|
871
|
+
reason
|
|
872
|
+
};
|
|
664
873
|
}
|
|
665
874
|
};
|
|
666
875
|
/**
|
|
@@ -695,9 +904,12 @@ var CodeArtsAccountStore = class {
|
|
|
695
904
|
path;
|
|
696
905
|
/** 旧版单凭证文件;新文档不存在时把它收编成第一个账号。 */
|
|
697
906
|
legacyPath;
|
|
698
|
-
|
|
907
|
+
/** 续期失败的落地日志(由宿主注入;测试里没有就是静默)。 */
|
|
908
|
+
log;
|
|
909
|
+
constructor(path, legacyPath, options = {}) {
|
|
699
910
|
this.path = path;
|
|
700
911
|
this.legacyPath = legacyPath ?? join(dirname(path), ".codearts-auth.json");
|
|
912
|
+
this.log = options.log;
|
|
701
913
|
}
|
|
702
914
|
async loaded() {
|
|
703
915
|
this._loading ??= this.load();
|
|
@@ -726,7 +938,7 @@ var CodeArtsAccountStore = class {
|
|
|
726
938
|
adopt(doc) {
|
|
727
939
|
this._doc = doc;
|
|
728
940
|
this._slots.clear();
|
|
729
|
-
for (const [id, cred] of Object.entries(doc.accounts)) this._slots.set(id, new AccountSlot(cred, this.persisterFor(id)));
|
|
941
|
+
for (const [id, cred] of Object.entries(doc.accounts)) this._slots.set(id, new AccountSlot(cred, this.persisterFor(id), this.log));
|
|
730
942
|
}
|
|
731
943
|
persisterFor(id) {
|
|
732
944
|
return async (cred) => {
|
|
@@ -773,7 +985,7 @@ var CodeArtsAccountStore = class {
|
|
|
773
985
|
this._slots.get(id)?.invalidate();
|
|
774
986
|
doc.accounts[id] = cred;
|
|
775
987
|
if (!doc.order.includes(id)) doc.order.push(id);
|
|
776
|
-
this._slots.set(id, new AccountSlot(cred, this.persisterFor(id)));
|
|
988
|
+
this._slots.set(id, new AccountSlot(cred, this.persisterFor(id), this.log));
|
|
777
989
|
await this.persistDoc();
|
|
778
990
|
await claimIfPossible(cred);
|
|
779
991
|
return id;
|
|
@@ -818,7 +1030,7 @@ var CodeArtsAccountStore = class {
|
|
|
818
1030
|
...identity.domainId === "" ? {} : { domainId: identity.domainId }
|
|
819
1031
|
};
|
|
820
1032
|
this._slots.get(id)?.invalidate();
|
|
821
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id)));
|
|
1033
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
822
1034
|
await this.persistDoc();
|
|
823
1035
|
return "noop";
|
|
824
1036
|
}
|
|
@@ -836,7 +1048,7 @@ var CodeArtsAccountStore = class {
|
|
|
836
1048
|
this._slots.get(id)?.invalidate();
|
|
837
1049
|
this._slots.delete(id);
|
|
838
1050
|
this._slots.get(targetKey)?.invalidate();
|
|
839
|
-
this._slots.set(targetKey, new AccountSlot(doc.accounts[targetKey], this.persisterFor(targetKey)));
|
|
1051
|
+
this._slots.set(targetKey, new AccountSlot(doc.accounts[targetKey], this.persisterFor(targetKey), this.log));
|
|
840
1052
|
await this.persistDoc();
|
|
841
1053
|
return target === void 0 ? "moved" : "merged";
|
|
842
1054
|
}
|
|
@@ -873,7 +1085,7 @@ var CodeArtsAccountStore = class {
|
|
|
873
1085
|
paused
|
|
874
1086
|
};
|
|
875
1087
|
this._slots.get(id)?.invalidate();
|
|
876
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id)));
|
|
1088
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
877
1089
|
await this.persistDoc();
|
|
878
1090
|
return true;
|
|
879
1091
|
}
|
|
@@ -888,7 +1100,7 @@ var CodeArtsAccountStore = class {
|
|
|
888
1100
|
pinned
|
|
889
1101
|
};
|
|
890
1102
|
this._slots.get(id)?.invalidate();
|
|
891
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id)));
|
|
1103
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
892
1104
|
await this.persistDoc();
|
|
893
1105
|
return true;
|
|
894
1106
|
}
|
|
@@ -925,9 +1137,22 @@ var CodeArtsAccountStore = class {
|
|
|
925
1137
|
/** 给 shim 用:解析某账号的签名材料(内部含续期)。 */
|
|
926
1138
|
async resolve(id) {
|
|
927
1139
|
await this.loaded();
|
|
928
|
-
const
|
|
929
|
-
if (
|
|
930
|
-
|
|
1140
|
+
const first = this._slots.get(id);
|
|
1141
|
+
if (first === void 0) return void 0;
|
|
1142
|
+
let cred = await first.ensureFresh();
|
|
1143
|
+
if (first.lastFailure === "used") {
|
|
1144
|
+
if (await this.reloadIfPeerAdvanced(id)) {
|
|
1145
|
+
const retry = this._slots.get(id);
|
|
1146
|
+
if (retry !== void 0) cred = await retry.ensureFresh();
|
|
1147
|
+
if (retry?.lastFailure === "used") {
|
|
1148
|
+
await this.markStale(id);
|
|
1149
|
+
cred = this._slots.get(id)?.credential ?? cred;
|
|
1150
|
+
}
|
|
1151
|
+
} else {
|
|
1152
|
+
await this.markStale(id);
|
|
1153
|
+
cred = this._slots.get(id)?.credential ?? cred;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
931
1156
|
return {
|
|
932
1157
|
token: cred.token,
|
|
933
1158
|
accessKeyId: cred.accessKeyId,
|
|
@@ -943,10 +1168,56 @@ var CodeArtsAccountStore = class {
|
|
|
943
1168
|
*/
|
|
944
1169
|
async forceRefresh(id, seenToken) {
|
|
945
1170
|
await this.loaded();
|
|
946
|
-
|
|
1171
|
+
let slot = this._slots.get(id);
|
|
947
1172
|
if (slot === void 0) return void 0;
|
|
948
|
-
if (seenToken !== void 0 && slot.credential.token !== seenToken) return slot.credential;
|
|
949
|
-
|
|
1173
|
+
if (seenToken !== void 0 && slot.credential.token !== seenToken) return slot.credential.stale === true ? void 0 : slot.credential;
|
|
1174
|
+
let next = await slot.forceRefresh();
|
|
1175
|
+
if (next === void 0 && slot.lastFailure === "used") {
|
|
1176
|
+
if (await this.reloadIfPeerAdvanced(id)) {
|
|
1177
|
+
slot = this._slots.get(id);
|
|
1178
|
+
next = slot === void 0 ? void 0 : await slot.forceRefresh();
|
|
1179
|
+
}
|
|
1180
|
+
if (next === void 0) await this.markStale(id);
|
|
1181
|
+
}
|
|
1182
|
+
return next;
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* 磁盘上的 refresh_token 和内存里不一样了 → 说明另一个写入方(肯家是守护进程)
|
|
1186
|
+
* 已经续过:用磁盘那份重建槽位,返回 true。
|
|
1187
|
+
*
|
|
1188
|
+
* 只在续期失败后调用,重建会让在飞的刷新结果作废(代际号),这正是我们要的。
|
|
1189
|
+
*/
|
|
1190
|
+
async reloadIfPeerAdvanced(id) {
|
|
1191
|
+
const doc = await readAccountDocument(this.path);
|
|
1192
|
+
const next = doc?.accounts[id]?.refreshToken;
|
|
1193
|
+
if (doc === void 0 || next === void 0) return false;
|
|
1194
|
+
if (next === this._slots.get(id)?.credential.refreshToken) return false;
|
|
1195
|
+
for (const slot of this._slots.values()) slot.invalidate();
|
|
1196
|
+
this.adopt(doc);
|
|
1197
|
+
return true;
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* 把账号标成 stale。
|
|
1201
|
+
*
|
|
1202
|
+
* 必须先重读磁盘再写:内存里的 `_doc` 可能是几小时前的快照,直接落库会把另一个
|
|
1203
|
+
* 账号(或本账号)刚被守护进程轮换出来的 refresh_token 覆盖回旧的 —— 2026-11-13
|
|
1204
|
+
* 真踩过,代价是必须重新登录。
|
|
1205
|
+
*/
|
|
1206
|
+
async markStale(id) {
|
|
1207
|
+
const doc = await readAccountDocument(this.path);
|
|
1208
|
+
if (doc !== void 0 && doc.accounts[id] !== void 0) {
|
|
1209
|
+
if (doc.accounts[id].stale === true) return;
|
|
1210
|
+
doc.accounts[id] = {
|
|
1211
|
+
...doc.accounts[id],
|
|
1212
|
+
stale: true
|
|
1213
|
+
};
|
|
1214
|
+
this.adopt(doc);
|
|
1215
|
+
try {
|
|
1216
|
+
await this.persistDoc();
|
|
1217
|
+
} catch {}
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
await this._slots.get(id)?.markStale().catch(() => {});
|
|
950
1221
|
}
|
|
951
1222
|
/**
|
|
952
1223
|
* 从磁盘重读账号文档,丢掉内存里的槽位缓存。
|
|
@@ -975,7 +1246,10 @@ var CodeArtsAccountStore = class {
|
|
|
975
1246
|
*
|
|
976
1247
|
* 只刷「距过期不足 `REFRESH_SKEW_MS`」的那些,刚从上游拿到的号不会被白刷一次:
|
|
977
1248
|
* `refresh_token` 是一次性的,多余的刷新只会多一份失败风险。
|
|
978
|
-
*
|
|
1249
|
+
*
|
|
1250
|
+
* `stale` 的号会做一次**低成本复验**:1.3.0 及以前任何一次续期失败(含网络抖动)
|
|
1251
|
+
* 都会永久落 stale,那些被误判的账号不该逼用户重新登录。真失效的话一轮只多一个
|
|
1252
|
+
* 请求(20 分钟一次),代价可以忽略;复验成功即自动复活。
|
|
979
1253
|
*/
|
|
980
1254
|
async keepAlive() {
|
|
981
1255
|
await this.loaded();
|
|
@@ -985,6 +1259,11 @@ var CodeArtsAccountStore = class {
|
|
|
985
1259
|
for (const [id, slot] of this._slots) {
|
|
986
1260
|
const before = slot.credential;
|
|
987
1261
|
if (before.stale === true) {
|
|
1262
|
+
checked += 1;
|
|
1263
|
+
if (await slot.verifyStale().catch(() => false)) {
|
|
1264
|
+
refreshed += 1;
|
|
1265
|
+
continue;
|
|
1266
|
+
}
|
|
988
1267
|
needsLogin.push(id);
|
|
989
1268
|
continue;
|
|
990
1269
|
}
|
|
@@ -1005,4 +1284,4 @@ var CodeArtsAccountStore = class {
|
|
|
1005
1284
|
}
|
|
1006
1285
|
};
|
|
1007
1286
|
//#endregion
|
|
1008
|
-
export { codeartsAuthPath as a,
|
|
1287
|
+
export { codeartsAuthPath as a, credentialFromOAuth as c, OAuthTokenError as d, defaultLoginConfig as f, codeartsAccountsPath as i, readAccountDocument as l, startLogin as m, CodeArtsCredentialStore as n, codeartsDebugLogPath as o, refreshToken as p, MANUAL_ACCOUNT_ID as r, codeartsRefreshLogPath as s, CodeArtsAccountStore as t, writeJsonAtomic as u };
|
package/lib/bin.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { i as codeartsAccountsPath, t as CodeArtsAccountStore } from "./auth-
|
|
2
|
+
import { i as codeartsAccountsPath, t as CodeArtsAccountStore } from "./auth-Ch0vYSAo.mjs";
|
|
3
3
|
import { F as isBenefitModel, s as fetchCallerIdentity } from "./upstream-CVtspzAv.mjs";
|
|
4
|
-
import { i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-
|
|
4
|
+
import { i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-BjBwEMks.mjs";
|
|
5
5
|
import { existsSync, statSync } from "node:fs";
|
|
6
6
|
//#region src/bin.ts
|
|
7
7
|
/**
|
package/lib/client/index.d.cts
CHANGED
|
@@ -177,6 +177,8 @@ interface CodeArtsSettingsKey {
|
|
|
177
177
|
creditsDockPanelTitle: string;
|
|
178
178
|
creditsDockRefresh: string;
|
|
179
179
|
creditsDockRefreshing: string;
|
|
180
|
+
creditsDockSettings: string;
|
|
181
|
+
creditsDockSettingsFailed: string;
|
|
180
182
|
creditsDockNoTokens: string;
|
|
181
183
|
creditsDockEmpty: string;
|
|
182
184
|
creditsDockExpiring: string;
|
|
@@ -206,6 +208,9 @@ interface CodeArtsSettingsKey {
|
|
|
206
208
|
displayKeeperLog: string;
|
|
207
209
|
displayNotWritable: string;
|
|
208
210
|
displayFailed: string;
|
|
211
|
+
displaySettingsEntryTitle: string;
|
|
212
|
+
displaySettingsEntryHint: string;
|
|
213
|
+
displaySettingsEntryToggle: string;
|
|
209
214
|
displaySidebarTitle: string;
|
|
210
215
|
displaySidebarHint: string;
|
|
211
216
|
displaySidebarToggle: string;
|
|
@@ -216,6 +221,8 @@ interface CodeArtsSettingsKey {
|
|
|
216
221
|
claimPending: string;
|
|
217
222
|
claimAll: string;
|
|
218
223
|
claimHint: string;
|
|
224
|
+
/** 知道上游当前活动给多少分时用这条;{amount} 来自福利中心 delivery 的 benefitAmount。 */
|
|
225
|
+
claimHintAmount: string;
|
|
219
226
|
claimDone: string;
|
|
220
227
|
claimAlready: string;
|
|
221
228
|
claimUnavailable: string;
|
|
@@ -265,6 +272,14 @@ declare module '@deepseek-ai/dsh-client-ui-slots' {
|
|
|
265
272
|
children?: never;
|
|
266
273
|
};
|
|
267
274
|
};
|
|
275
|
+
/**
|
|
276
|
+
* 设置左侧那一列导航(宿主 ui-settings 声明)。用户找插件的第一反应就是这里
|
|
277
|
+
* (issue #7),而「内置插件 → 详情页」那条路径对第一次用的人基本等于找不到。
|
|
278
|
+
*/
|
|
279
|
+
'settings.section': {
|
|
280
|
+
kind: 'list';
|
|
281
|
+
scope: 'root';
|
|
282
|
+
};
|
|
268
283
|
}
|
|
269
284
|
}
|
|
270
285
|
declare const name = "dsh-codearts-client";
|