@askalf/dario 6.18.4 → 6.19.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/README.md +2 -2
- package/dist/accounts.d.ts +26 -0
- package/dist/accounts.js +126 -14
- package/dist/proxy.js +38 -3
- package/docs/multi-instance.md +10 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
<p><strong>One local endpoint. Every AI tool you own. Your Claude seats first, your ChatGPT plan and a metered key as the rungs you chain behind them, and a ledger of who paid for what.</strong></p>
|
|
18
18
|
|
|
19
|
-
<sub><code>npm i -g @askalf/dario</code> or <code>brew install askalf/tap/dario</code> · <strong>0</strong> runtime deps · <a href="https://www.npmjs.com/package/@askalf/dario">SLSA-attested</a> every release · nothing phones home · ~
|
|
19
|
+
<sub><code>npm i -g @askalf/dario</code> or <code>brew install askalf/tap/dario</code> · <strong>0</strong> runtime deps · <a href="https://www.npmjs.com/package/@askalf/dario">SLSA-attested</a> every release · nothing phones home · ~43k lines you can read in a weekend · independent, unofficial, third-party (<a href="DISCLAIMER.md">DISCLAIMER.md</a>)</sub>
|
|
20
20
|
|
|
21
21
|
<sub><a href="#start-in-60-seconds">Start</a> · <a href="#why-people-pick-it">Why dario</a> · <a href="#what-it-does-with-a-request">Routing</a> · <a href="#will-my-account-get-suspended">Risk</a> · <a href="#reference">Reference</a> · <a href="#trust--transparency">Trust</a> · <a href="docs/returning.md">Coming back after a while?</a></sub>
|
|
22
22
|
|
|
@@ -113,7 +113,7 @@ dario runs entirely on your machine, authenticates as you with your own Claude l
|
|
|
113
113
|
|
|
114
114
|
| Signal | Status |
|
|
115
115
|
|---|---|
|
|
116
|
-
| Source | **~
|
|
116
|
+
| Source | **~43k** lines of TypeScript across **87** files, auditable in a weekend. One credential path since v5: the pool. |
|
|
117
117
|
| Dependencies | **0 runtime.** Verify: `npm ls --production` |
|
|
118
118
|
| Provenance | Every release [SLSA-attested](https://www.npmjs.com/package/@askalf/dario) via GitHub Actions + Sigstore, published with OIDC trusted publishing: no long-lived npm token exists to leak |
|
|
119
119
|
| Scanning | [CodeQL](https://github.com/askalf/dario/actions/workflows/codeql.yml) on every push to master, every PR and weekly · [ClusterFuzzLite](./.github/workflows/cflite.yml) fuzzes the SSE translator, the rejection parsers and the cch stamper weekly · the [OpenSSF Scorecard](https://scorecard.dev/viewer/?uri=github.com/askalf/dario) badge above and the [Best Practices](https://www.bestpractices.dev/projects/13638) entry are live |
|
package/dist/accounts.d.ts
CHANGED
|
@@ -38,7 +38,21 @@ export type IdentitySource = 'claude-code' | 'generated';
|
|
|
38
38
|
export declare function listAccountAliases(): Promise<string[]>;
|
|
39
39
|
export declare function loadAccount(alias: string): Promise<AccountCredentials | null>;
|
|
40
40
|
export declare function loadAllAccounts(): Promise<AccountCredentials[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Whether `candidate` holds newer credentials than `current`. A newer grant
|
|
43
|
+
* (a fresh login) wins outright, since an older grant's refreshed access
|
|
44
|
+
* token can expire later. Same grant, or a grant time missing on either
|
|
45
|
+
* side: the later expiry wins. The lock services apply the same rule.
|
|
46
|
+
*/
|
|
47
|
+
export declare function credentialsAreNewer(candidate: {
|
|
48
|
+
expiresAt: number;
|
|
49
|
+
grantedAt?: number;
|
|
50
|
+
}, current: {
|
|
51
|
+
expiresAt?: number;
|
|
52
|
+
grantedAt?: number;
|
|
53
|
+
}): boolean;
|
|
41
54
|
export declare function saveAccount(creds: AccountCredentials): Promise<void>;
|
|
55
|
+
export declare function _setAccountWriteHookForTest(hook: (() => Promise<void>) | null): void;
|
|
42
56
|
export declare function removeAccount(alias: string): Promise<boolean>;
|
|
43
57
|
/**
|
|
44
58
|
* Carry the organization the proxy observed on a seat into a record that is
|
|
@@ -118,6 +132,18 @@ export declare function chooseClientIdentity(existing: readonly AccountCredentia
|
|
|
118
132
|
export declare function regenerateClientIdentity(aliases: readonly string[]): Promise<string[]>;
|
|
119
133
|
/** Refresh an account's OAuth token using dario's auto-detected CC OAuth config. */
|
|
120
134
|
export declare function refreshAccountToken(creds: AccountCredentials): Promise<AccountCredentials>;
|
|
135
|
+
/**
|
|
136
|
+
* Adopt credentials another instance refreshed for this account, when the
|
|
137
|
+
* lock service holds a newer copy than `local` (dario#1733). Returns the
|
|
138
|
+
* newest credentials newer than `local`, saving them when the account file
|
|
139
|
+
* is older; null when nothing newer is shared, the account is gone, no lock
|
|
140
|
+
* service is set, or it cannot be reached. Never refreshes and never takes the lock. The
|
|
141
|
+
* proxy asks at startup and on every refresh tick, so a replica runs on its
|
|
142
|
+
* peer's rotation within one tick instead of carrying the old pair until its
|
|
143
|
+
* own refresh window. "Newer" is the lock service's rule: a newer grant, or
|
|
144
|
+
* the same grant with a later expiry.
|
|
145
|
+
*/
|
|
146
|
+
export declare function adoptSharedCredentials(local: Pick<AccountCredentials, 'alias' | 'expiresAt' | 'grantedAt'>): Promise<AccountCredentials | null>;
|
|
121
147
|
/**
|
|
122
148
|
* Fill in `accountId` (and the profile's other fields) on a record that predates
|
|
123
149
|
* them, from one profile GET; saves and returns the record, unchanged when
|
package/dist/accounts.js
CHANGED
|
@@ -78,10 +78,46 @@ export async function loadAllAccounts() {
|
|
|
78
78
|
const loaded = await Promise.all(aliases.map(a => loadAccount(a)));
|
|
79
79
|
return loaded.filter((a) => a !== null);
|
|
80
80
|
}
|
|
81
|
+
// Writes and removals of one account file run one at a time, in call order.
|
|
82
|
+
// A check-then-write (adoptSharedCredentials) runs inside the same chain, so
|
|
83
|
+
// a login saved or a removal made while it waited on the network lands
|
|
84
|
+
// either before its check, which then sees it, or after its write.
|
|
85
|
+
const accountWrites = new Map();
|
|
86
|
+
function serializeAccountWrite(alias, fn) {
|
|
87
|
+
const next = (accountWrites.get(alias) ?? Promise.resolve()).then(fn);
|
|
88
|
+
const tail = next.catch(() => { });
|
|
89
|
+
accountWrites.set(alias, tail);
|
|
90
|
+
void tail.then(() => { if (accountWrites.get(alias) === tail)
|
|
91
|
+
accountWrites.delete(alias); });
|
|
92
|
+
return next;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Whether `candidate` holds newer credentials than `current`. A newer grant
|
|
96
|
+
* (a fresh login) wins outright, since an older grant's refreshed access
|
|
97
|
+
* token can expire later. Same grant, or a grant time missing on either
|
|
98
|
+
* side: the later expiry wins. The lock services apply the same rule.
|
|
99
|
+
*/
|
|
100
|
+
export function credentialsAreNewer(candidate, current) {
|
|
101
|
+
const a = candidate.grantedAt, b = current.grantedAt;
|
|
102
|
+
if (typeof a === 'number' && typeof b === 'number' && Number.isFinite(a) && Number.isFinite(b) && a !== b)
|
|
103
|
+
return a > b;
|
|
104
|
+
return candidate.expiresAt > (current.expiresAt ?? 0);
|
|
105
|
+
}
|
|
81
106
|
export async function saveAccount(creds) {
|
|
82
107
|
const path = safeAliasPath(creds.alias);
|
|
83
108
|
if (!path)
|
|
84
109
|
throw new Error(`invalid account alias: ${creds.alias}`);
|
|
110
|
+
await serializeAccountWrite(creds.alias, () => writeAccountFile(path, creds));
|
|
111
|
+
}
|
|
112
|
+
// Test-only: awaited at the start of every account file write, so a test can
|
|
113
|
+
// hold a write open and queue other work behind it. Null in production.
|
|
114
|
+
let accountWriteHookForTest = null;
|
|
115
|
+
export function _setAccountWriteHookForTest(hook) {
|
|
116
|
+
accountWriteHookForTest = hook;
|
|
117
|
+
}
|
|
118
|
+
async function writeAccountFile(path, creds) {
|
|
119
|
+
if (accountWriteHookForTest)
|
|
120
|
+
await accountWriteHookForTest();
|
|
85
121
|
await ensureDir();
|
|
86
122
|
// Durable write (dario#790): fsync the temp file + parent dir so a rotated
|
|
87
123
|
// refresh token survives an abrupt container recreate. A plain rename left
|
|
@@ -94,13 +130,15 @@ export async function removeAccount(alias) {
|
|
|
94
130
|
const path = safeAliasPath(alias);
|
|
95
131
|
if (!path)
|
|
96
132
|
return false;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
133
|
+
return serializeAccountWrite(alias, async () => {
|
|
134
|
+
try {
|
|
135
|
+
await unlink(path);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
});
|
|
104
142
|
}
|
|
105
143
|
/**
|
|
106
144
|
* Carry the organization the proxy observed on a seat into a record that is
|
|
@@ -304,6 +342,9 @@ export async function refreshAccountToken(creds) {
|
|
|
304
342
|
// error, timeout) — a Cloudflare outage must not block dario's own
|
|
305
343
|
// refresh, since the lock is a resilience nice-to-have layered on top of
|
|
306
344
|
// dario's actual job, not a new dependency dario's core function needs.
|
|
345
|
+
// Open means before the token runs out: while it still has
|
|
346
|
+
// LOCK_DOWN_DEFER_MS left, the refresh waits for a later tick instead
|
|
347
|
+
// (dario#1733).
|
|
307
348
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
308
349
|
function refreshLockUrl(env = process.env) {
|
|
309
350
|
const v = env.DARIO_REFRESH_LOCK_URL;
|
|
@@ -331,6 +372,64 @@ async function lockCall(path, body, env = process.env) {
|
|
|
331
372
|
return null; // network error / CF outage — fail OPEN
|
|
332
373
|
}
|
|
333
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Adopt credentials another instance refreshed for this account, when the
|
|
377
|
+
* lock service holds a newer copy than `local` (dario#1733). Returns the
|
|
378
|
+
* newest credentials newer than `local`, saving them when the account file
|
|
379
|
+
* is older; null when nothing newer is shared, the account is gone, no lock
|
|
380
|
+
* service is set, or it cannot be reached. Never refreshes and never takes the lock. The
|
|
381
|
+
* proxy asks at startup and on every refresh tick, so a replica runs on its
|
|
382
|
+
* peer's rotation within one tick instead of carrying the old pair until its
|
|
383
|
+
* own refresh window. "Newer" is the lock service's rule: a newer grant, or
|
|
384
|
+
* the same grant with a later expiry.
|
|
385
|
+
*/
|
|
386
|
+
export async function adoptSharedCredentials(local) {
|
|
387
|
+
if (!refreshLockUrl())
|
|
388
|
+
return null;
|
|
389
|
+
const res = await lockCall(`/lock/${encodeURIComponent(local.alias)}/peek`, {
|
|
390
|
+
currentExpiresAt: local.expiresAt,
|
|
391
|
+
currentGrantedAt: local.grantedAt,
|
|
392
|
+
});
|
|
393
|
+
const shared = res?.credentials;
|
|
394
|
+
// A lock service from before /peek answers 404, which lockCall turns into
|
|
395
|
+
// null: nothing is adopted and refreshes go through /acquire as before.
|
|
396
|
+
if (!shared || shared.alias !== local.alias || typeof shared.accessToken !== 'string'
|
|
397
|
+
|| typeof shared.refreshToken !== 'string' || typeof shared.expiresAt !== 'number') {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
// The service compared against `local` as it was when the request left.
|
|
401
|
+
// Compare again against the file as it is now: a login may have landed
|
|
402
|
+
// while the request was out, or the account may have been removed, and a
|
|
403
|
+
// removed account stays removed. The file is never overwritten with older
|
|
404
|
+
// credentials, but when it already holds something newer than `local`
|
|
405
|
+
// (replicas sharing one accounts volume, where the peer saved its rotation
|
|
406
|
+
// first) that is still handed back, so the caller's seat catches up.
|
|
407
|
+
const path = safeAliasPath(shared.alias);
|
|
408
|
+
if (!path)
|
|
409
|
+
return null;
|
|
410
|
+
return serializeAccountWrite(shared.alias, async () => {
|
|
411
|
+
const current = await loadAccount(shared.alias);
|
|
412
|
+
if (!current)
|
|
413
|
+
return null;
|
|
414
|
+
if (credentialsAreNewer(shared, current)) {
|
|
415
|
+
await writeAccountFile(path, shared);
|
|
416
|
+
return shared;
|
|
417
|
+
}
|
|
418
|
+
return credentialsAreNewer(current, local) ? current : null;
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
// With the lock service unreachable, a refresh is put off while the access
|
|
422
|
+
// token has more than this left, since an uncoordinated refresh can rotate
|
|
423
|
+
// the token out from under a peer that is refreshing too. The background
|
|
424
|
+
// tick runs every 15 minutes and starts refreshing at 45 minutes left, so a
|
|
425
|
+
// token deferred at 20+ minutes is tried again with 5 to 20 minutes left,
|
|
426
|
+
// and refreshed then even if the service is still down.
|
|
427
|
+
const LOCK_DOWN_DEFER_MS = 20 * 60 * 1000;
|
|
428
|
+
// Adopted credentials with less than this left are refreshed straight away
|
|
429
|
+
// with the adopted refresh token, not handed back to be served: a peer that
|
|
430
|
+
// went quiet can leave a cached access token that is already expired, while
|
|
431
|
+
// its refresh token is still the live one.
|
|
432
|
+
const ADOPTED_MIN_LIFE_MS = 5 * 60 * 1000;
|
|
334
433
|
async function doRefreshAccountTokenDistributed(creds) {
|
|
335
434
|
if (!refreshLockUrl())
|
|
336
435
|
return doRefreshAccountToken(creds);
|
|
@@ -341,17 +440,26 @@ async function doRefreshAccountTokenDistributed(creds) {
|
|
|
341
440
|
holder,
|
|
342
441
|
ttlMs: 20_000,
|
|
343
442
|
currentExpiresAt: creds.expiresAt,
|
|
443
|
+
currentGrantedAt: creds.grantedAt,
|
|
344
444
|
});
|
|
345
445
|
if (res === null) {
|
|
346
|
-
// Lock service unreachable
|
|
347
|
-
// if DARIO_REFRESH_LOCK_URL
|
|
446
|
+
// Lock service unreachable. Hold off while the token has life left;
|
|
447
|
+
// near expiry, refresh directly exactly as if DARIO_REFRESH_LOCK_URL
|
|
448
|
+
// were unset.
|
|
449
|
+
const left = creds.expiresAt - Date.now();
|
|
450
|
+
if (left > LOCK_DOWN_DEFER_MS) {
|
|
451
|
+
throw new Error(`refresh lock service unreachable; deferring the refresh of ${creds.alias} while its access token has ${Math.round(left / 60_000)} minutes left`);
|
|
452
|
+
}
|
|
348
453
|
return doRefreshAccountToken(creds);
|
|
349
454
|
}
|
|
350
455
|
if (res.credentials) {
|
|
351
456
|
// Another instance already refreshed more recently than what we're
|
|
352
457
|
// holding — adopt it, no Anthropic call needed at all.
|
|
353
458
|
await saveAccount(res.credentials);
|
|
354
|
-
|
|
459
|
+
if (res.credentials.expiresAt - Date.now() >= ADOPTED_MIN_LIFE_MS)
|
|
460
|
+
return res.credentials;
|
|
461
|
+
creds = res.credentials;
|
|
462
|
+
continue;
|
|
355
463
|
}
|
|
356
464
|
if (res.acquired) {
|
|
357
465
|
// Echoed back on BOTH release paths below. Undefined against a
|
|
@@ -863,8 +971,10 @@ export async function resyncLoginFromCredentialsIfStale() {
|
|
|
863
971
|
// and overwriting it from credentials.json would swap a live token for one
|
|
864
972
|
// whose refresh Anthropic already rotated → invalid_grant → outage (#805).
|
|
865
973
|
// Only the strict case is skipped; equal expiresAt keeps the #235 behaviour
|
|
866
|
-
// (overwrite), since a real refresh always advances expiresAt.
|
|
867
|
-
|
|
974
|
+
// (overwrite), since a real refresh always advances expiresAt. A newer grant
|
|
975
|
+
// in login.json counts as newer even with an earlier expiry: credentials
|
|
976
|
+
// adopted from a peer instance after a re-login can expire first.
|
|
977
|
+
if (credentialsAreNewer(loginAcc, tok)) {
|
|
868
978
|
return 'creds-stale';
|
|
869
979
|
}
|
|
870
980
|
// credentials.json is the newer (or equal-age) token — the #235 case: the
|
|
@@ -920,11 +1030,13 @@ export async function mirrorLoginToCredentials(refreshed) {
|
|
|
920
1030
|
if (refreshed.alias !== MIGRATED_LOGIN_ALIAS)
|
|
921
1031
|
return 'skip-not-login';
|
|
922
1032
|
const creds = await loadCredentials();
|
|
923
|
-
const
|
|
1033
|
+
const current = creds?.claudeAiOauth;
|
|
924
1034
|
// Only mirror a strictly-newer pool token. Equal expiry means the files
|
|
925
1035
|
// already agree (or a same-second write elsewhere); newer credentials.json
|
|
926
1036
|
// is another process's fresher family we must not overwrite (#805 direction).
|
|
927
|
-
|
|
1037
|
+
// A newer grant counts as newer even with an earlier expiry: credentials
|
|
1038
|
+
// adopted from a peer instance after a re-login can expire first.
|
|
1039
|
+
if (current && !credentialsAreNewer(refreshed, current))
|
|
928
1040
|
return 'creds-newer';
|
|
929
1041
|
await saveCredentialsTokens({
|
|
930
1042
|
accessToken: refreshed.accessToken,
|
package/dist/proxy.js
CHANGED
|
@@ -29,7 +29,7 @@ import { OverageGuard, buildHaltErrorBody } from './overage-guard.js';
|
|
|
29
29
|
import { notify as osNotify } from './notify.js';
|
|
30
30
|
import { grantAge, grantThresholds, worstGrantLevel, describeGrantAge } from './refresh-grant.js';
|
|
31
31
|
import { resolveSeatPin, SEAT_PIN_HEADER, SEAT_PIN_TOKEN_HEADER } from './seat-pin.js';
|
|
32
|
-
import { loadAllAccounts, loadAccount, saveAccount, refreshAccountToken, resyncLoginFromCredentialsIfStale, ensureLoginCredentialsInPool, mirrorLoginToCredentials, withObservedOrganization } from './accounts.js';
|
|
32
|
+
import { loadAllAccounts, loadAccount, saveAccount, refreshAccountToken, adoptSharedCredentials, credentialsAreNewer, resyncLoginFromCredentialsIfStale, ensureLoginCredentialsInPool, mirrorLoginToCredentials, withObservedOrganization } from './accounts.js';
|
|
33
33
|
import { handleAdminRequest } from './admin-api.js';
|
|
34
34
|
import { createTokenBucket } from './rate-limit.js';
|
|
35
35
|
import { getOpenAIBackend, isOpenAIModel, forwardToOpenAI, isMiniMaxBackend, MINIMAX_MODELS } from './openai-backend.js';
|
|
@@ -1681,6 +1681,37 @@ export async function startProxy(opts = {}) {
|
|
|
1681
1681
|
}
|
|
1682
1682
|
}
|
|
1683
1683
|
};
|
|
1684
|
+
// Take a peer instance's newer credentials for this seat from the refresh
|
|
1685
|
+
// lock service before deciding whether to refresh it (dario#1733). Without
|
|
1686
|
+
// this, a replica kept serving and refreshing with the pair it loaded, and
|
|
1687
|
+
// its refresh token had been rotated away by the peer's last refresh. A
|
|
1688
|
+
// no-op without DARIO_REFRESH_LOCK_URL, and never fatal: the refresh
|
|
1689
|
+
// decision below still runs on what the seat already has.
|
|
1690
|
+
const adoptPeerCredentials = async (alias) => {
|
|
1691
|
+
const seat = pool.get(alias);
|
|
1692
|
+
if (!seat)
|
|
1693
|
+
return;
|
|
1694
|
+
const shared = await adoptSharedCredentials(seat).catch(() => null);
|
|
1695
|
+
if (!shared)
|
|
1696
|
+
return;
|
|
1697
|
+
// The seat can move on while the lock service answers (a login, a
|
|
1698
|
+
// refresh, a removal); replace it only if it is still there and older.
|
|
1699
|
+
const live = pool.get(alias);
|
|
1700
|
+
if (!live || !credentialsAreNewer(shared, live))
|
|
1701
|
+
return;
|
|
1702
|
+
// add(), not updateTokens(): a newer grant carries its own grantedAt, and
|
|
1703
|
+
// add() is what starts a re-granted seat's live state fresh.
|
|
1704
|
+
pool.add(alias, {
|
|
1705
|
+
accessToken: shared.accessToken, refreshToken: shared.refreshToken, expiresAt: shared.expiresAt,
|
|
1706
|
+
deviceId: shared.deviceId, accountUuid: shared.accountUuid, grantedAt: shared.grantedAt,
|
|
1707
|
+
organizationId: shared.organizationId, accountId: shared.accountId, accountEmail: shared.accountEmail,
|
|
1708
|
+
rateLimitTier: shared.rateLimitTier, seatTier: shared.seatTier,
|
|
1709
|
+
});
|
|
1710
|
+
await mirrorLoginToCredentials(shared).catch((err) => {
|
|
1711
|
+
console.error(`[dario] login→credentials.json mirror failed (peer credentials) for ${alias}: ${err instanceof Error ? err.message : err}`);
|
|
1712
|
+
});
|
|
1713
|
+
console.error(`[dario] Adopted credentials another instance refreshed for account ${alias}.`);
|
|
1714
|
+
};
|
|
1684
1715
|
// Shared pool state across instances (docs/multi-instance.md): opt-in,
|
|
1685
1716
|
// rides the refresh-lock service, fails open. Off → byte-identical to before.
|
|
1686
1717
|
const lockUrl = process.env['DARIO_REFRESH_LOCK_URL'];
|
|
@@ -1819,7 +1850,9 @@ export async function startProxy(opts = {}) {
|
|
|
1819
1850
|
// token (invalid_grant) just logs and leaves the account expired; the auth
|
|
1820
1851
|
// gate below then surfaces it. Skipped in --no-claude-auth mode.
|
|
1821
1852
|
if (!opts.noClaudeAuth) {
|
|
1822
|
-
await Promise.all(pool.all().map(async (
|
|
1853
|
+
await Promise.all(pool.all().map(async (seat) => {
|
|
1854
|
+
await adoptPeerCredentials(seat.alias);
|
|
1855
|
+
const acc = pool.get(seat.alias) ?? seat;
|
|
1823
1856
|
if (acc.expiresAt >= Date.now() + 45 * 60 * 1000)
|
|
1824
1857
|
return;
|
|
1825
1858
|
try {
|
|
@@ -1890,7 +1923,9 @@ export async function startProxy(opts = {}) {
|
|
|
1890
1923
|
if (opts.noClaudeAuth)
|
|
1891
1924
|
return; // never touch the Claude token in OpenAI-only mode
|
|
1892
1925
|
watchGrants();
|
|
1893
|
-
for (const
|
|
1926
|
+
for (const seat of pool.all()) {
|
|
1927
|
+
await adoptPeerCredentials(seat.alias);
|
|
1928
|
+
const acc = pool.get(seat.alias) ?? seat;
|
|
1894
1929
|
if (acc.expiresAt < Date.now() + 45 * 60 * 1000) {
|
|
1895
1930
|
try {
|
|
1896
1931
|
const saved = await loadAccount(acc.alias);
|
package/docs/multi-instance.md
CHANGED
|
@@ -20,6 +20,8 @@ Two instances holding the same account both notice the token is near expiry, and
|
|
|
20
20
|
|
|
21
21
|
A plain mutex does not fix this. The loser waits, acquires the lock, and then refreshes with a token that is *already* stale — it just loses more slowly. The fix is that the loser **adopts the winner's fresh credentials** instead of attempting its own. That is what dario's refresh lock does.
|
|
22
22
|
|
|
23
|
+
The handoff has to outlast the gap between replicas, not just the race. Each instance checks its tokens every 15 minutes on its own phase, so the second one can come back well after the first refreshed. The lock service keeps the latest credentials for 30 days, and every instance reads them at startup and on each tick before deciding to refresh. Until [#1733](https://github.com/askalf/dario/issues/1733) that window was five minutes, and a replica that came back later refreshed with a dead token.
|
|
24
|
+
|
|
23
25
|
### 2. Rate-limit accounting — **solved with shared state**
|
|
24
26
|
|
|
25
27
|
`pool.ts` keeps `accounts: Map<string, PoolAccount>` in process memory, and updates it only from rate-limit headers on responses *that instance* saw.
|
|
@@ -88,9 +90,13 @@ Not suitable for airgapped deployments, and it adds an internet dependency to a
|
|
|
88
90
|
|
|
89
91
|
### It fails open, deliberately
|
|
90
92
|
|
|
91
|
-
|
|
93
|
+
On any lock-service error (bad response, network failure, timeout), dario first waits if it can. While the access token still has more than 20 minutes left, the refresh is put off to a later tick, because refreshing alone is what can strand a peer. Inside 20 minutes dario refreshes on its own, exactly as if no lock were configured.
|
|
94
|
+
|
|
95
|
+
The lock is resilience layered on top of dario's job, not a new dependency dario's core function needs. An outage of your Redis must not stop your proxy serving traffic. The cost of that choice is that an outage lasting into a token's last 20 minutes puts you back to the plain race, so do not treat the lock as a hard guarantee.
|
|
96
|
+
|
|
97
|
+
### Upgrading the lock service
|
|
92
98
|
|
|
93
|
-
The
|
|
99
|
+
The 30-day handoff and the `/peek` endpoint live in the lock service, so rebuild the Redis image or redeploy the Worker along with the dario upgrade. In either order nothing breaks: a new dario against an old service gets a 404 from `/peek` and treats it as nothing to adopt, and an old dario against a new service still gets the longer handoff on `/acquire`.
|
|
94
100
|
|
|
95
101
|
---
|
|
96
102
|
|
|
@@ -136,8 +142,9 @@ Anthropic's token endpoint is the one mocked part, and only that — hammering t
|
|
|
136
142
|
|
|
137
143
|
## If you need true HA
|
|
138
144
|
|
|
139
|
-
Everything above is as far as dario goes today: shared credentials through the lock, and shared rate-limit readings and sticky bindings through `--pool-shared-state`, both eventually consistent and both failing open.
|
|
145
|
+
Everything above is as far as dario goes today: shared credentials through the lock, and shared rate-limit readings and sticky bindings through `--pool-shared-state`, both eventually consistent and both failing open. Four things are still per instance:
|
|
140
146
|
|
|
147
|
+
- **The account list.** Each instance loads `~/.dario/accounts/` from its own disk, and a login through the admin API reloads only the instance that served it. Copying the account files to every instance is safe with the refresh lock on: the copies share one refresh token, and whichever instance refreshes first hands the result to the others ([#1733](https://github.com/askalf/dario/issues/1733)). Without the lock, copies race exactly as described in section 1.
|
|
141
148
|
- **ChatGPT seats.** The Codex accounts have their own refresh flow and usage windows; neither the lock nor the shared state covers them.
|
|
142
149
|
- **The ledger.** Each instance writes its own file, so `dario usage` and `/analytics` show that instance's share of the spend.
|
|
143
150
|
- **The overage guard's halt**, which each instance tracks on its own.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@askalf/dario",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.19.0",
|
|
4
4
|
"description": "Use your Claude and ChatGPT subscriptions in Cursor, Cline, Aider, Claude Code and the Agent SDK at subscription pricing, not per-token API bills. One local Anthropic + OpenAI-compatible endpoint: either plan answers either wire shape, opt-in failover when one hits its limit, and a ledger of what each plan absorbed.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|