Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·6672 lines (6370 loc) · 341 KB
/
Copy pathserver.js
File metadata and controls
executable file
·6672 lines (6370 loc) · 341 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* OpenAI-compatible API server wrapping DeepSeek Web API
* Supports BOTH streaming (SSE) and non-streaming modes
* Includes tool calling: injects tool definitions into system prompt,
* parses LLM text responses for TOOL_CALL patterns, returns OpenAI tool_calls format.
*
* Per-agent sessions: each resolved client key gets its own DeepSeek web session.
* Live chats reset only through explicit /new/reset, client compaction, or the
* recovery-prompt-backed rate-limit migration path; idle sessions are swept.
* Listens on 127.0.0.1:9655 by default (HOST is configurable).
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const crypto = require('crypto');
const { spawnSync } = require('child_process');
const { solvePOW } = require('./lib/pow');
// Per-DeepSeek-request network timeout. Plain fetch() has NO default timeout, so a
// stalled upstream would hang the inbound request (and pin the account) forever.
// Validated numeric env: garbage values fall back to the default with a
// one-time warning instead of silently poisoning runtime behavior (NaN
// timeouts throw on every call; NaN deadlines/cooldowns never trigger).
function numEnv(name, def, min = -Infinity, max = Infinity) {
const raw = process.env[name];
if (raw === undefined || raw === '') return def;
const value = Number(raw);
if (!Number.isFinite(value) || value < min || value > max) {
console.log(`[DS-API] Invalid ${name}=${JSON.stringify(raw)}; using default ${def}.`);
return def;
}
return value;
}
const DS_FETCH_TIMEOUT_MS = numEnv('DEEPSEEK_FETCH_TIMEOUT_MS', 60000, 1);
// Fail-fast on unexpected runtime faults: persist sessions, then exit non-zero
// so systemd restarts a clean process. Strict 0/1 range keeps a typo from
// silently disabling the crash path (falls back to 1 with a warning).
const FATAL_ON_UNHANDLED = numEnv('DEEPSEEK_FATAL_ON_UNHANDLED', 1, 0, 1);
function dsFetch(url, options = {}, timeoutMs = DS_FETCH_TIMEOUT_MS) {
const timeoutSignal = AbortSignal.timeout(timeoutMs);
let signal;
if (options.signal) {
if (typeof AbortSignal.any === 'function') {
signal = AbortSignal.any([options.signal, timeoutSignal]);
} else {
const controller = new AbortController();
const onAbort = () => controller.abort();
if (options.signal.aborted || timeoutSignal.aborted) {
controller.abort();
} else {
options.signal.addEventListener('abort', onAbort, { once: true });
timeoutSignal.addEventListener('abort', onAbort, { once: true });
}
signal = controller.signal;
}
} else {
signal = timeoutSignal;
}
return fetch(url, { ...options, signal });
}
// Log verbosity: DEEPSEEK_LOG_LEVEL=debug enables per-pick score breakdowns and
// stage timings. Default info. Anything else falls back to info.
const LOG_LEVELS = { debug: 0, info: 1 };
const LOG_THRESHOLD = LOG_LEVELS[String(process.env.DEEPSEEK_LOG_LEVEL || 'info').trim().toLowerCase()] ?? 1;
function logDebug(...args) { if (LOG_THRESHOLD <= 0) console.log(...args); }
const FORGETMEAI_WATERMARK = 't.me/forgetmeai';
// Validated like every other numeric env: PORT=garbage used to yield NaN and
// crash server.listen (F17). Range-checked 1-65535 with a loud fallback.
const PORT = numEnv('PORT', 9655, 1, 65535);
const HOST = process.env.HOST || '127.0.0.1';
function loadProxyApiKey(env = process.env) {
if (env.PROXY_API_KEY) return String(env.PROXY_API_KEY);
const secretPath = String(env.PROXY_API_KEY_FILE || '').trim();
if (!secretPath) return '';
try {
return fs.readFileSync(secretPath, 'utf8').trim();
} catch (error) {
// A missing optional secret is equivalent to an unset key. Container
// deployments set REQUIRE_PROXY_API_KEY=1 and fail closed in main().
if (error.code === 'ENOENT') return '';
throw new Error(`Could not read PROXY_API_KEY_FILE (${secretPath}): ${error.message}`);
}
}
function requireProxyApiKey(key, required) {
if (required && !key) {
throw new Error('PROXY_API_KEY is required. Set PROXY_API_KEY or mount a secret and set PROXY_API_KEY_FILE.');
}
}
// Lazy proxy key (H3): module scope must not touch disk, so
// `require('../server.js').__test` never throws on an unreadable secret.
// First use caches; callers pass through getProxyKey().
let PROXY_API_KEY = '';
let _proxyKeyLoaded = false;
function getProxyKey(env = process.env) {
// Env is read fresh (cheap, no disk); only the optional FILE read is
// cached. Semantics match the old import-time load when env is static,
// and unit tests can set/unset PROXY_API_KEY per case without a reset hook.
if (env.PROXY_API_KEY) return String(env.PROXY_API_KEY);
if (_proxyKeyLoaded) return PROXY_API_KEY;
PROXY_API_KEY = loadProxyApiKey(env);
_proxyKeyLoaded = true;
return PROXY_API_KEY;
}
const PROXY_CORS_ORIGINS = new Set(String(process.env.PROXY_CORS_ORIGINS || '')
.split(',')
.map(value => normalizeOrigin(value))
.filter(Boolean));
function formatWatermark(prefix = 'ForgetMeAI') { return `${prefix}: ${FORGETMEAI_WATERMARK}`; }
function printBanner() {
console.log(`
███████ ██████ ███████ ███████ ██████ ███████ ███████ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
█████ ██████ █████ █████ ██ ██ █████ █████ █████ █████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ███████ ███████ ██████ ███████ ███████ ███████ ██ ██
FreeDeepseekAPI — API proxy for DeepSeek Web Chat
${formatWatermark()}
`);
}
function prompt(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, ans => { rl.close(); resolve(ans); }));
}
function isTruthy(value) { return typeof value === 'string' && ['1','true','yes','on'].includes(value.trim().toLowerCase()); }
function isProxyAuthorized(authorization, expectedKey) {
const key = expectedKey === undefined ? getProxyKey() : expectedKey;
if (!key) return true;
if (typeof authorization !== 'string' || !authorization.startsWith('Bearer ')) return false;
const supplied = Buffer.from(authorization.slice('Bearer '.length), 'utf8');
const expected = Buffer.from(String(key), 'utf8');
return supplied.length === expected.length && crypto.timingSafeEqual(supplied, expected);
}
function isLoopbackHost(host) {
const normalized = String(host || '').trim().toLowerCase().replace(/^\[|\]$/g, '');
return normalized === '127.0.0.1'
|| normalized === '::1'
|| normalized === '::ffff:127.0.0.1'
|| normalized === 'localhost';
}
function normalizeOrigin(origin) {
const value = String(origin || '').trim().replace(/\/+$/, '');
if (!value) return '';
try {
const parsed = new URL(value);
return parsed.origin === 'null' ? value : parsed.origin;
} catch (e) {
return value;
}
}
function isBrowserOriginAllowed(origin, allowedOrigins = PROXY_CORS_ORIGINS) {
if (!origin) return true; // curl, SDKs, and other non-browser clients
const normalized = normalizeOrigin(origin);
if (allowedOrigins.has(normalized)) return true;
try {
const parsed = new URL(normalized);
return (parsed.protocol === 'http:' || parsed.protocol === 'https:')
&& isLoopbackHost(parsed.hostname);
} catch (e) {
return false;
}
}
const CONTEXT_COMPACTED_HEADER = 'X-FreeDeepseek-Context-Compacted';
function setCorsResponseHeaders(res) {
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-agent-session, x-api-key, anthropic-version, anthropic-beta');
res.setHeader('Access-Control-Expose-Headers', CONTEXT_COMPACTED_HEADER);
}
function markContextCompacted(res) {
// Best-effort signal only: on streams the headers are already flushed
// (start*Stream runs before the upstream read), and setHeader would
// throw ERR_HTTP_HEADERS_SENT out of the empty-retry loop (:3014).
if (!res || res.headersSent || res.writableEnded) return;
try {
res.setHeader(CONTEXT_COMPACTED_HEADER, 'true');
} catch (e) { /* headers flushed mid-call; header is expendable */ }
}
// === Per-Agent Session Store ===
const sessions = new Map(); // keyed by agent ID (from `user` field)
// At most one inbound completion turn may mutate a conversation at a time.
// A remote chat has a single parent cursor, so overlapping requests can create
// duplicate chats or commit mutually inconsistent message IDs. The map is
// request-scoped state and is intentionally never persisted.
const activeSessionTurns = new Map();
function acquireSessionTurn(agentId, owner) {
if (!agentId || activeSessionTurns.has(agentId)) return false;
activeSessionTurns.set(agentId, owner);
return true;
}
function rekeySessionTurn(from, to, owner) {
if (from === to) return activeSessionTurns.get(from) === owner;
if (activeSessionTurns.get(from) !== owner || activeSessionTurns.has(to)) return false;
activeSessionTurns.delete(from);
activeSessionTurns.set(to, owner);
return true;
}
function releaseSessionTurn(agentId, owner) {
if (agentId && activeSessionTurns.get(agentId) === owner) activeSessionTurns.delete(agentId);
}
function findAdoptableSession(baseAgentId, candidateId, turnMessages) {
if (!baseAgentId || !candidateId || sessions.has(candidateId) || !Array.isArray(turnMessages)) return null;
const matches = [];
for (const [existingId, existingSession] of sessions) {
if (!existingId.startsWith(`${baseAgentId}:`) || activeSessionTurns.has(existingId)) continue;
if (!existingSession?.id || !existingSession.deltaBoundary) continue;
const sent = Number(existingSession.deltaMsgCount) || 0;
if (sent > 0 && sent < turnMessages.length
&& hashMessageEnvelope(turnMessages[sent - 1]) === existingSession.deltaBoundary) {
matches.push({ id: existingId, session: existingSession });
}
}
return matches.length === 1 ? matches[0] : null;
}
const MAX_HISTORY_LENGTH = 15;
const MAX_HISTORY_CHARS = 10000;
const MAX_MESSAGE_DEPTH = 100; // observability threshold; no implicit chat reset
const SESSION_TTL_MS = 2 * 60 * 60 * 1000; // 2 hours
const MAX_SESSION_STORE_BYTES = 32 * 1024 * 1024;
// === Session persistence (survive restarts) ===
// A restart used to wipe the in-memory chat map, reopening every live
// conversation as a brand-new remote chat with truncated context. The map is
// snapshotted to disk on every mutation (atomic tmp+rename, mode 0600) and
// restored at startup. Stale entries past 2x TTL are dropped on load,
// mirroring sweepIdleSessions. Persistence never throws: a failed snapshot
// only logs, so the proxy keeps serving.
const SESSION_STORE_PATH = process.env.DEEPSEEK_SESSION_STORE || path.join(__dirname, '.sessions.json');
function serializeSession(session) {
const s = session && typeof session === 'object' ? session : {};
const history = [];
let remainingHistoryChars = MAX_HISTORY_CHARS;
const rawHistory = Array.isArray(s.history) ? s.history.slice(-MAX_HISTORY_LENGTH) : [];
for (let index = rawHistory.length - 1; index >= 0 && remainingHistoryChars > 0; index--) {
const entry = rawHistory[index] || {};
const userBudget = Math.min(1000, remainingHistoryChars);
const user = boundHistoryTail(entry.user, userBudget);
const assistant = boundHistoryText(entry.assistant, Math.max(0, remainingHistoryChars - user.length));
history.unshift({ user, assistant });
remainingHistoryChars -= user.length + assistant.length;
}
return {
id: typeof s.id === 'string' ? s.id : null,
parentMessageId: (typeof s.parentMessageId === 'string' || Number.isFinite(s.parentMessageId)) ? s.parentMessageId : null,
createdAt: typeof s.createdAt === 'number' ? s.createdAt : null,
messageCount: Number(s.messageCount) || 0,
accountId: typeof s.accountId === 'string' ? s.accountId : null,
// Coerce entry shapes (F22): a hand-edited or corrupted store could
// otherwise smuggle non-string fields past restore and crash readers
// (e.g. the reset-session preview's e.user.substring) with a 500.
// Live entries are always strings, so this is a no-op in the hot path.
history,
lastActivityAt: typeof s.lastActivityAt === 'number' ? s.lastActivityAt : Date.now(),
deltaMsgCount: Number(s.deltaMsgCount) || 0,
deltaBoundary: typeof s.deltaBoundary === 'string' ? s.deltaBoundary : null,
deltaPrefixHash: typeof s.deltaPrefixHash === 'string' ? s.deltaPrefixHash : null,
deltaToolNames: typeof s.deltaToolNames === 'string' ? s.deltaToolNames : null,
// Repeat-repair guard rides along so a restart between a 502 and the
// client's verbatim retry still recognizes the turn (staleness is
// enforced by classifyRepairAttempt's window, not here). The hash is
// one-way hex — no prompt content recoverable from disk.
repairHash: typeof s.repairHash === 'string' ? s.repairHash : null,
repairAt: typeof s.repairAt === 'number' ? s.repairAt : 0,
repairCount: Number(s.repairCount) || 0,
};
}
function persistSessionsNow(storePath = SESSION_STORE_PATH) {
try {
const target = String(storePath || SESSION_STORE_PATH);
const payload = JSON.stringify({
v: 1,
savedAt: Date.now(),
sessions: Array.from(sessions.entries()).map(([agentId, s]) => [agentId, serializeSession(s)]),
});
const tmp = `${target}.tmp`;
fs.writeFileSync(tmp, payload, { mode: 0o600 });
fs.renameSync(tmp, target);
try { fs.chmodSync(target, 0o600); } catch (e) { /* best effort */ }
} catch (e) {
console.log(`[DS-API] Session persist skipped: ${e && e.message ? e.message : e}`);
}
}
// Debounced persist (H2): the hot path (per-session creation/turn) only marks
// dirty and coalesces a trailing write 1s out, so bursts of new sessions cost
// O(1) disk writes instead of one full stringify+tmp+rename per mutation.
// Anything needing durability (shutdown, idle sweep) calls
// persistSessionsNow() directly. Timer is unref'd: never keeps the process alive.
const PERSIST_DEBOUNCE_MS = 1000;
let _sessionsDirty = false;
let _persistTimer = null;
function persistSessions(storePath = SESSION_STORE_PATH) {
// An explicit non-default store path is the unit-test hook: write through
// synchronously so tests don't wait out the debounce window.
if (String(storePath || SESSION_STORE_PATH) !== String(SESSION_STORE_PATH)) {
persistSessionsNow(storePath);
return;
}
_sessionsDirty = true;
if (_persistTimer) return;
_persistTimer = setTimeout(() => {
_persistTimer = null;
if (!_sessionsDirty) return;
_sessionsDirty = false;
persistSessionsNow();
}, PERSIST_DEBOUNCE_MS);
if (_persistTimer && typeof _persistTimer.unref === 'function') _persistTimer.unref();
}
function restoreSessions(now = Date.now(), storePath = SESSION_STORE_PATH) {
let data;
try {
const rawStore = fs.readFileSync(String(storePath || SESSION_STORE_PATH));
if (rawStore.length > MAX_SESSION_STORE_BYTES) {
console.warn(`[DS-API] Session store exceeds ${MAX_SESSION_STORE_BYTES} bytes; refusing to parse it.`);
return 0;
}
data = JSON.parse(rawStore.toString('utf8'));
} catch (e) {
return 0; // cold start: no store yet, or unreadable — keep serving
}
if (!data || !Array.isArray(data.sessions)) return 0;
let restored = 0;
let droppedAtCapacity = 0;
for (const entry of data.sessions) {
if (sessions.size >= MAX_SESSIONS) {
droppedAtCapacity++;
continue;
}
const agentId = entry && entry[0];
const snap = entry && entry[1];
if (typeof agentId !== 'string' || !snap || typeof snap !== 'object') continue;
const lastActive = typeof snap.lastActivityAt === 'number' ? snap.lastActivityAt : 0;
if (snap.id && (now - lastActive) > SESSION_TTL_MS * 2) continue; // stale: mirror sweepIdleSessions
const session = createSession();
Object.assign(session, serializeSession(snap), { lastActivityAt: lastActive || now });
sessions.set(agentId, session);
restored++;
}
if (droppedAtCapacity > 0) console.warn(`[DS-API] restore capacity ${MAX_SESSIONS}: dropped ${droppedAtCapacity} session entr${droppedAtCapacity === 1 ? 'y' : 'ies'}`);
if (restored > 0) console.log(`[DS-API] Restored ${restored} session(s) from ${storePath}`);
return restored;
}
// === DeepSeek Web API Config — loaded from external config file ===
const DS_CONFIG_PATH = process.env.DEEPSEEK_AUTH_PATH || path.join(__dirname, 'deepseek-auth.json');
const DEFAULT_ACCOUNT_COOLDOWN_MS = numEnv('DEEPSEEK_ACCOUNT_COOLDOWN_MS', 10 * 60 * 1000, 1000);
const MAX_ACCOUNT_COOLDOWN_MS = 30 * 60 * 1000; // upper clamp: a malicious or
// absurd Retry-After must never brick an account for years (8b).
// Hypersensitive smart-routing tuning (2026-09-16). All load-time env knobs:
// - FAILURE/TIMEOUT_WEIGHT: scorer penalty per effective failure / consecutive
// timeout (timeouts still weigh 3x plain failures, as before).
// - CONSECUTIVE_STRIKES: consecutive failures (any kind except PoW-solve, see
// F16) that sideline an account. 2 = one blip is forgiven, a repeat is not.
// - ESCALATION_COOLDOWN_MS: short sideline for strike-outs (fast failover +
// fast recovery probe). Hard auth/rate-limit faults keep the long default.
// - FAILURE_HALFLIFE_MS: failures decay exponentially (forgives old blips, so
// routing reacts to the last minutes, not ancient history). <=0 disables.
// - HOT_BONUS/HOT_WINDOW_MS: a small nudge toward the account with the most
// recent success (proven-hot wins near-ties). Small on purpose: it must not
// become a monopoly — failures still dominate within a strike or two.
const ROUTING_FAILURE_WEIGHT = numEnv('DEEPSEEK_ROUTING_FAILURE_WEIGHT', 4, 0);
const ROUTING_TIMEOUT_WEIGHT = numEnv('DEEPSEEK_ROUTING_TIMEOUT_WEIGHT', 12, 0);
const ROUTING_CONSECUTIVE_STRIKES = Math.max(1, Math.floor(numEnv('DEEPSEEK_ROUTING_CONSECUTIVE_STRIKES', 2, 1)));
const ROUTING_ESCALATION_COOLDOWN_MS = numEnv('DEEPSEEK_ROUTING_ESCALATION_COOLDOWN_MS', 60 * 1000, 1000);
const ROUTING_FAILURE_HALFLIFE_MS = numEnv('DEEPSEEK_ROUTING_FAILURE_HALFLIFE_MS', 5 * 60 * 1000, 0);
const ROUTING_HOT_BONUS = numEnv('DEEPSEEK_ROUTING_HOT_BONUS', 2, 0);
const ROUTING_HOT_WINDOW_MS = numEnv('DEEPSEEK_ROUTING_HOT_WINDOW_MS', 60 * 1000, 0);
// Hourly per-account request quota (round-3 G2, anti-mute): ~215 reqs/hour on
// one account draws a (delayed) mute upstream; default 60 keeps a wide margin
// ("add accounts, don't raise the value"). Sliding 1h window per account;
// over-quota accounts sit out exactly like cooling ones; all-spent fails fast
// 429 with Retry-After. 0 disables.
const HOURLY_QUOTA = numEnv('DEEPSEEK_HOURLY_QUOTA', 60, 0, 100000);
const QUOTA_WINDOW_MS = 3600000;
// Burst cap (pacing 4a): max turns per sliding 60s window per account.
// Default 0 = off until Sep-19 traffic measures a real number (candidate 10).
let BURST_PER_MINUTE = numEnv('DEEPSEEK_BURST_PER_MINUTE', 0, 0, 1000);
// Test hook (mirrors setExtraToolTags): swaps the burst limit for wiring
// tests. Validated like the env parse; restored by callers via t.after.
function setBurstPerMinute(n) {
const v = Math.floor(Number(n));
BURST_PER_MINUTE = Number.isFinite(v) ? Math.max(0, Math.min(1000, v)) : BURST_PER_MINUTE;
}
const BURST_WINDOW_MS = 60000;
// Ring capacity: fixed floor plus headroom scaled to both knobs, so a high
// HOURLY_QUOTA stays enforceable (H2 — a fixed 1024 silently uncapped large
// quotas). Pure part exported for tests.
function computeRingCap(quota, burst) {
return Math.max(1024, 2 * (quota || 0), 2 * (burst || 0));
}
function ringCapacity() {
return computeRingCap(HOURLY_QUOTA, BURST_PER_MINUTE);
}
function recordUpstreamTurn(account, nowMs = Date.now()) {
if (!account) return;
if (!Array.isArray(account.requestTimes)) account.requestTimes = [];
account.requestTimes.push(nowMs);
account.lastUpstreamAt = nowMs;
const cutoff = nowMs - QUOTA_WINDOW_MS;
// Filter-prune, not shift-while-old: front-only eviction assumes ascending
// order, which clock skew can break (bounded anyway by the cap below).
// Fast path skips the allocation when the head is already fresh.
if (account.requestTimes.length > 0 && account.requestTimes[0] < cutoff) {
account.requestTimes = account.requestTimes.filter(t => t >= cutoff);
}
const cap = ringCapacity();
if (account.requestTimes.length > cap) account.requestTimes.splice(0, account.requestTimes.length - cap);
}
function recordAccountRequest(account, nowMs = Date.now()) {
// Test/compat wrapper only — production stamp sites must use
// recordUpstreamTurn (C1: this gate would silently disable burst under
// DEEPSEEK_HOURLY_QUOTA=0).
if (!(HOURLY_QUOTA > 0)) return;
recordUpstreamTurn(account, nowMs);
}
function usedSince(account, windowMs, nowMs = Date.now()) {
const times = account && account.requestTimes;
if (!Array.isArray(times) || times.length === 0) return 0;
const cutoff = nowMs - windowMs;
let n = 0;
for (let i = 0; i < times.length; i++) {
if (times[i] >= cutoff) n++;
}
return n;
}
function usedThisHour(account, nowMs = Date.now()) {
return usedSince(account, QUOTA_WINDOW_MS, nowMs);
}
function burstUsedThisMinute(account, nowMs = Date.now()) {
return usedSince(account, BURST_WINDOW_MS, nowMs);
}
// Oldest in-window stamp, order-independent (arrays are chronological in
// production, but never assume it for release-time math).
function oldestInWindow(times, cutoff) {
let oldest = null;
for (let i = 0; i < (times || []).length; i++) {
const t = times[i];
if (t >= cutoff && (oldest === null || t < oldest)) oldest = t;
}
return oldest;
}
function withinQuota(account, nowMs = Date.now()) {
if (!(HOURLY_QUOTA > 0)) return true;
return usedThisHour(account, nowMs) < HOURLY_QUOTA;
}
function withinBurst(account, nowMs = Date.now(), limit = BURST_PER_MINUTE) {
if (!(limit > 0)) return true;
return burstUsedThisMinute(account, nowMs) < limit;
}
// Burst twin of the quota sticky-429: returns the 429 error (never throws)
// when a live chat sits on a burst-spent account, else null. Pure apart from
// reading its inputs, so the wiring ("fail fast, chat preserved, no mark")
// is unit-testable. Chat-less stickies get null and rotate freely below.
function stickyBurstReject(sticky, session, nowMs = Date.now(), limit = BURST_PER_MINUTE, isOverBurst = null) {
if (!sticky || !sticky.config || !sticky.config.token || !sticky.config.cookie) return null;
if (!(limit > 0)) return null;
const over = isOverBurst !== null ? isOverBurst : (burstUsedThisMinute(sticky, nowMs) >= limit);
if (!over) return null;
if (!session || !session.id) return null;
const oldest = oldestInWindow(sticky.requestTimes, nowMs - BURST_WINDOW_MS);
const waitSec = oldest !== null ? Math.max(1, Math.ceil((oldest + BURST_WINDOW_MS - nowMs) / 1000)) : 60;
const err = new Error(`Account ${sticky.id} (owner of this chat) hit the burst cap (${limit}/min). Retry in ~${waitSec}s; chat preserved.`);
err.status = 429; err.retryAfter = waitSec; err.type = 'rate_limit';
return err;
}
// Earliest per-account availability across all three limiters. Shared by the
// all-spent branch and the SSE migration-exhausted path so both quote the
// same Retry-After (H1/M2 — per-site math drifted before).
function accountReleaseMs(a, nowMs = Date.now()) {
let rel = (a && a.cooldownUntil) || 0;
if (HOURLY_QUOTA > 0) {
const oldest = oldestInWindow(a.requestTimes, nowMs - QUOTA_WINDOW_MS);
if (oldest !== null && usedThisHour(a, nowMs) >= HOURLY_QUOTA) {
rel = Math.max(rel, oldest + QUOTA_WINDOW_MS);
}
}
if (BURST_PER_MINUTE > 0) {
const oldest = oldestInWindow(a.requestTimes, nowMs - BURST_WINDOW_MS);
if (oldest !== null && burstUsedThisMinute(a, nowMs) >= BURST_PER_MINUTE) {
rel = Math.max(rel, oldest + BURST_WINDOW_MS);
}
}
return rel;
}
function earliestReleaseMs(nowMs = Date.now()) {
let best = Infinity;
for (const a of accounts) {
if (!(a && a.config && a.config.token && a.config.cookie) || a.authUnavailable) continue;
const rel = accountReleaseMs(a, nowMs);
if (rel < best) best = rel;
}
return best;
}
// Optional same-chat rate-limit retry (round-3 G7): DEEPSEEK_RETRY_RATELIMIT=1
// waits once (bounded, Retry-After honored up to a cap) and retries the turn on
// the SAME account+chat before migration runs. Default off: fail-fast 429.
const RETRY_RATELIMIT = isTruthy(process.env.DEEPSEEK_RETRY_RATELIMIT);
const RATELIMIT_RETRY_BASE_MS = 2000;
const RATELIMIT_RETRY_MAX_MS = 10000;
function rateLimitRetryDelayMs(retryAfterRaw) {
const honoredMs = parseRetryAfterMs(retryAfterRaw);
return Math.min(Math.max(RATELIMIT_RETRY_BASE_MS, honoredMs || 0), RATELIMIT_RETRY_MAX_MS);
}
// Shared fail-fast wording (graceful degradation, not a bare error): keeps the
// 429 status + Retry-After contract integrators back off on, while telling the
// caller exactly what to do. Smaller post-compact turns burn less quota, which
// is what protects the accounts — hammering helps nothing: while all accounts
// cool, retries never reach DeepSeek anyway.
function rateLimitExhaustedMessage(waitSec) {
return `[OpenCode Warning] No available DeepSeek accounts (all cooling down, in quarantine, or quota-spent). In OpenCode: wait ~${waitSec}s or run /compact to shrink context. Tip: run 'npm run auth-cli' to renew or restore accounts.`;
}
// In-place retry is only attempted when quick recovery is plausible: unknown
// Retry-After gets one optimistic probe, brief backoffs (<= cap) are worth the
// wait, long backoffs go straight to migration (waiting out minutes in-request
// burns the deadline and the client's patience).
function shouldAttemptInPlaceRetry(retryAfterRaw) {
if (retryAfterRaw === undefined || retryAfterRaw === null || retryAfterRaw === '') return true;
const retryAfterMs = parseRetryAfterMs(retryAfterRaw);
// Unknown values retain the historical optimistic probe. A valid HTTP-date
// or delta-seconds value must never be downgraded to an immediate retry.
if (retryAfterMs == null) return true;
return retryAfterMs <= RATELIMIT_RETRY_MAX_MS;
}
// Full gate for the in-place retry branch, extracted pure so the wiring ("skip
// when all cooling, skip long backoffs, default off") is unit-testable without
// fetch mocks. waitMs is computed by retryWaitMs below.
function shouldRetryInPlace(o = {}) {
if (!o.flagOn || !o.rateLimit || o.migrated || o.gone || o.deadline) return false;
if (!shouldAttemptInPlaceRetry(o.retryAfterSec)) return false;
if (o.anyReady !== undefined && !o.anyReady) return false;
if (o.accountReady !== undefined && !o.accountReady) return false;
return true;
}
// Bounded wait: never sleep past the request deadline (H-2 — an unbounded
// sleep stalls the client, then the attempt is skipped anyway).
function retryWaitMs(retryAfterSec, remainingMs) {
if (!(remainingMs > 0)) return 0;
return Math.min(rateLimitRetryDelayMs(retryAfterSec), remainingMs);
}
// Latency EWMA step (display-only): alpha 0.3 reacts within ~3 turns while
// staying readable. Pure for tests; first sample seeds directly.
function nextEwmaLatency(prevMs, sampleMs) {
const prev = Number(prevMs) || 0;
const sample = Math.max(0, Number(sampleMs) || 0);
if (!(prev > 0)) return Math.round(sample);
return Math.round(0.7 * prev + 0.3 * sample);
}
// Single readiness predicate for all four admission sites (select-fresh,
// migration peers, /readyz, retry gate). The (x || 0) cooldown form admits
// missing-field accounts as ready where the old raw form excluded them —
// deliberate: the constructor always sets a numeric cooldownUntil, so only
// hand-built objects differ, and fail-open beats TypeError-crash there.
// Exported for tests.
function isAccountReady(a, nowMs = Date.now()) {
return !!(a && a.config && a.config.token && a.config.cookie
&& !a.authUnavailable
&& !a.isProbeActive
&& (a.cooldownUntil || 0) <= nowMs && withinQuota(a, nowMs) && withinBurst(a, nowMs));
}
// Testable eligibility check for in-place rate-limit retry:
// Account must possess credentials, and must NOT be blocked by hourly quota or burst cap.
// Note: cooldownUntil is explicitly NOT checked here because inPlaceRateLimitRetry lifts cooldown once.
function isRetryAccountEligible(a, nowMs = Date.now()) {
return !!(a && a.config && a.config.token && a.config.cookie && !a.authUnavailable && withinQuota(a, nowMs) && withinBurst(a, nowMs));
}
// Readiness pre-filter shared by the retry gate: true when at least one
// account passes the shared predicate. Exported for tests.
function anyAccountReady(list, nowMs = Date.now()) {
return (list || []).some(a => isAccountReady(a, nowMs));
}
// Testable core of the in-place retry (no env, timers, or fetch): lifts the
// cooldown for exactly one attemptTurn() call on the SAME account and fully
// restores the account's limiter/scorer state (cooldown AND failure counters)
// if the probe fails. A failed probe therefore records nothing at all: our
// optimism never deepens the backoff and never inflates the failure score —
// the original failure's own mark stands alone.
// Returns { recovered, result?, error? }.
async function inPlaceRateLimitRetry(account, attemptTurn) {
if (!account || typeof attemptTurn !== 'function') return { recovered: false };
const saved = {
cooldownUntil: Number(account.cooldownUntil) || 0,
failures: Number(account.failures) || 0,
consecutiveFailures: Number(account.consecutiveFailures) || 0,
consecutiveTimeouts: Number(account.consecutiveTimeouts) || 0,
ring: Array.isArray(account.requestTimes) ? account.requestTimes.slice() : null,
lastUpstreamAt: Number(account.lastUpstreamAt) || 0,
};
account.cooldownUntil = 0;
account.isProbeActive = true;
try {
return { recovered: true, result: await attemptTurn() };
} catch (retryErr) {
// Full restore (M1): the probe's own PoW-success stamp must not spend
// quota/burst budget either — a failed probe records nothing at all.
// Slice-restore (not length-truncate): the stamp path reassigns the
// array on prune, so truncating the new array would extend it with
// holes instead of restoring contents.
account.cooldownUntil = saved.cooldownUntil;
account.failures = saved.failures;
account.consecutiveFailures = saved.consecutiveFailures;
account.consecutiveTimeouts = saved.consecutiveTimeouts;
if (saved.ring !== null && Array.isArray(account.requestTimes)) account.requestTimes = saved.ring;
account.lastUpstreamAt = saved.lastUpstreamAt;
return { recovered: false, error: retryErr };
} finally {
delete account.isProbeActive;
}
}
// Operator-configurable extra tool-call sentinels (round-3 G4):
// DEEPSEEK_TOOL_TAGS="start1|start2;end1|end2". Literal substring matchers
// (never regexes), consulted by looksLikeToolCallMarkup (detection → drives
// truncated-markup retries) and parseCustomTagToolCall (extraction). Capped so
// a bloated env value can't slow the hot path.
const MAX_TOOL_TAG_LEN = 128;
const MAX_TOOL_TAGS = 32;
function parseToolTagList(raw) {
return String(raw || '').split('|').map(s => s.trim()).filter(s => s.length > 0 && s.length <= MAX_TOOL_TAG_LEN).slice(0, MAX_TOOL_TAGS);
}
function parseToolTagEnv(value) {
const parts = String(value || '').split(';');
if (parts.length > 2) {
console.warn(`[DS-API] DEEPSEEK_TOOL_TAGS has ${parts.length} ';'-sections; using the first two, ignoring the rest.`);
}
return { starts: parseToolTagList(parts[0] || ''), ends: parseToolTagList(parts[1] || '') };
}
const TOOL_TAG_PARTS = parseToolTagEnv(process.env.DEEPSEEK_TOOL_TAGS);
let TOOL_EXTRA_STARTS = TOOL_TAG_PARTS.starts;
let TOOL_EXTRA_ENDS = TOOL_TAG_PARTS.ends;
if (TOOL_EXTRA_STARTS.length > 0 || TOOL_EXTRA_ENDS.length > 0) {
console.log(`[DS-API] custom tool tags: ${TOOL_EXTRA_STARTS.length} starts, ${TOOL_EXTRA_ENDS.length} ends`);
}
// Test hook (mirrors setInflightBodyBytes pattern): tests swap tag lists.
// Validated exactly like env input so tests cannot inject shapes prod rejects.
function setExtraToolTags(starts, ends) {
const clean = (arr) => (Array.isArray(arr) ? arr : [])
.map(s => String(s ?? '').trim())
.filter(s => s.length > 0 && s.length <= MAX_TOOL_TAG_LEN)
.slice(0, MAX_TOOL_TAGS);
const rawCount = (Array.isArray(starts) ? starts.length : 0) + (Array.isArray(ends) ? ends.length : 0);
TOOL_EXTRA_STARTS = clean(starts);
TOOL_EXTRA_ENDS = clean(ends);
if (TOOL_EXTRA_STARTS.length + TOOL_EXTRA_ENDS.length < rawCount) {
console.warn(`[DS-API] custom tool tags truncated to ${MAX_TOOL_TAGS} entries of ${MAX_TOOL_TAG_LEN} chars (input had ${rawCount})`);
}
}
let DS_CONFIG = {};
let dsHeaders = {};
const accounts = [];
let accountRoundRobin = 0;
let inFlight = 0; // concurrent in-flight completions (backpressure cap)
// Overall wall-clock budget for one inbound request (caps the retry/continuation
// loops), max concurrent completions, and the empty-response retry cap.
const REQUEST_DEADLINE_MS = numEnv('DEEPSEEK_REQUEST_DEADLINE_MS', 120000, 1000);
const MAX_CONCURRENT = Math.max(1, Math.floor(numEnv('DEEPSEEK_MAX_CONCURRENT', 24, 1)));
let MAX_PER_ACCOUNT = Math.floor(numEnv('DEEPSEEK_MAX_PER_ACCOUNT', 1, 0, 10));
function getMaxPerAccount() { return MAX_PER_ACCOUNT; }
function setMaxPerAccount(n) {
if (typeof n !== 'number' || !Number.isFinite(n) || n < 0) {
console.warn(`[DS-API] setMaxPerAccount: ignoring invalid value ${n}; keeping ${MAX_PER_ACCOUNT}`);
return;
}
MAX_PER_ACCOUNT = Math.max(0, Math.min(10, Math.floor(n)));
} // test hook
function acquireAccountLease(account) {
if (!account) return false;
account.inflight = (Number(account.inflight) || 0) + 1;
return true;
}
function releaseAccountLease(account) {
if (!account) return;
const current = Number(account.inflight) || 0;
const remaining = current - 1;
if (remaining < 0) {
console.warn(`[account:${account.id}] inflight clamp engaged (counter would go negative); floored at 0 — investigate for a leak.`);
}
account.inflight = Math.max(0, remaining);
}
// Rotation lease swap: askDeepSeekStream acquires the new account's lease
// internally when it rotates, so the caller must release the lease it holds
// and adopt the rotated one — otherwise the old lease leaks and pool
// capacity shrinks. Returns the lease the caller must hold afterwards.
function applyLeaseRotation(activeLease, rotatedAccount) {
if (rotatedAccount && rotatedAccount !== activeLease) {
if (activeLease) releaseAccountLease(activeLease);
return rotatedAccount;
}
return activeLease;
}
// Per-account capacity ceiling: admits new turns on an account only when its
// in-flight count is strictly below MAX_PER_ACCOUNT (or disabled when <= 0).
// Deliberately separate from isAccountReady so /readyz and the retry gate
// remain semantic readiness probes (not capacity gauges).
function hasCapacity(a) {
if (!a) return false;
return !(MAX_PER_ACCOUNT > 0) || (Number(a.inflight) || 0) < MAX_PER_ACCOUNT;
}
// Estimated remaining wait for a saturated account, bounded between 2s and 10s
// (and capped by REQUEST_DEADLINE_MS) based on its EWMA latency and elapsed time.
function saturatedWaitSec(account, nowMs = Date.now()) {
const elapsed = Math.max(0, nowMs - (account?.lastDispatchedAt || nowMs));
const ewma = Number(account?.ewmaLatencyMs) || 5000;
const estMs = Math.max(2000, ewma - elapsed);
const deadlineCapSec = Math.max(1, Math.ceil(REQUEST_DEADLINE_MS / 1000));
return Math.min(deadlineCapSec, Math.min(10, Math.max(2, Math.ceil(estMs / 1000))));
}
// Request-body caps (§6/§7): per-request 10MB (413) plus a global
// in-flight-body budget (64MB, 503+Retry-After) so concurrent trickled
// uploads cannot balloon memory. inflightBodyBytes is charged per chunk and
// released exactly once per request (settled flag guards end+close, which
// Node fires BOTH of on normal completion). Scope is UPLOAD-IN-FLIGHT ONLY:
// the charge is released at req `end`, before JSON.parse, so parsed bodies
// retained during upstream processing are NOT counted here; those are bounded
// instead by MAX_CONCURRENT concurrent completions × MAX_BODY_BYTES each.
const MAX_BODY_BYTES = 10 * 1024 * 1024; // chat payloads are small; cap memory before JSON.parse
const MAX_INFLIGHT_BODY_BYTES = 64 << 20;
let inflightBodyBytes = 0;
function checkBackpressure(count = inFlight) { return count >= MAX_CONCURRENT; }
function getInflightBodyBytes() { return inflightBodyBytes; }
function setInflightBodyBytes(n) { inflightBodyBytes = Math.max(0, Number(n) || 0); } // test hook
function getInFlightCount() { return inFlight; }
function setInFlightCount(n) { inFlight = Math.max(0, Math.floor(Number(n) || 0)); } // test hook
const configuredEmptyRetries = Number(process.env.DEEPSEEK_MAX_RETRIES);
const MAX_EMPTY_RETRIES = Number.isFinite(configuredEmptyRetries)
? Math.max(0, Math.min(10, Math.floor(configuredEmptyRetries)))
: 2;
const MIN_UPSTREAM_PROMPT_CHARS = 16000;
const configuredPromptChars = Number(process.env.DEEPSEEK_MAX_PROMPT_CHARS);
const MAX_UPSTREAM_PROMPT_CHARS = Number.isFinite(configuredPromptChars)
? Math.max(MIN_UPSTREAM_PROMPT_CHARS, Math.floor(configuredPromptChars))
: 80000;
function buildBaseHeaders(config = DS_CONFIG) {
return {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36",
"x-client-platform": "web",
"x-client-version": "2.0.0",
"x-client-locale": "ru",
"x-client-timezone-offset": "14400",
"x-app-version": "2.0.0",
"Authorization": `Bearer ${config.token || ''}`,
"x-hif-dliq": config.hif_dliq || '',
"x-hif-leim": config.hif_leim || '',
...(typeof config.device_id === 'string' && /^[A-Za-z0-9_.:~/-]{1,128}$/.test(config.device_id) ? { "x-device-id": config.device_id } : {}),
"Origin": "https://chat.deepseek.com",
"Referer": "https://chat.deepseek.com/",
"Cookie": config.cookie || '',
"Content-Type": "application/json",
};
}
function discoverAuthPaths() {
if (process.env.DEEPSEEK_AUTH_DIR) {
try {
return fs.readdirSync(process.env.DEEPSEEK_AUTH_DIR)
.filter(f => f.endsWith('.json') && !f.startsWith('.'))
.sort()
.map(f => path.join(process.env.DEEPSEEK_AUTH_DIR, f));
} catch (e) {
console.error(`[DS-API] Could not read DEEPSEEK_AUTH_DIR: ${e.message}`);
return [];
}
}
if (process.env.DEEPSEEK_AUTH_PATH && process.env.DEEPSEEK_AUTH_PATH.includes(',')) {
return process.env.DEEPSEEK_AUTH_PATH.split(',').map(s => s.trim()).filter(Boolean);
}
return [DS_CONFIG_PATH];
}
function loadDeepSeekConfig({ fatal = true } = {}) {
accounts.length = 0;
const paths = discoverAuthPaths();
for (const file of paths) {
try {
const raw = fs.readFileSync(file, 'utf8');
const config = JSON.parse(raw);
const fileBase = path.basename(file, '.json').replace(/[^a-zA-Z0-9_-]/g, '_');
const id = config.id || config.account_id || (paths.length === 1 && file === DS_CONFIG_PATH ? 'account_1' : (fileBase || `account_${accounts.length + 1}`));
if (!config.wasmUrl) {
// Fail-loud at load, not per-turn: without wasmUrl every PoW
// solve throws and the account 500s every request (F16).
console.error(`[DS-API] ${id} (${file}) has no wasmUrl; PoW solves will fail until it is imported.`);
}
accounts.push({ id, file, config, headers: buildBaseHeaders(config), cooldownUntil: 0, failures: 0, consecutiveTimeouts: 0, consecutiveFailures: 0, lastFailureAt: 0, lastSuccessAt: 0, lastUsedAt: 0, inflight: 0, requestTimes: [], lastUpstreamAt: 0, ewmaLatencyMs: 0, lastTelemetryAt: 0, lastTelemetryStatus: null, lastDispatchedAt: 0, multiToolBatchCount: 0, batchSizeCounts: {} });
} catch (e) {
console.error(`[DS-API] Could not load auth config ${file}: ${e.message}`);
}
}
DS_CONFIG = accounts[0]?.config || {};
dsHeaders = accounts[0]?.headers || buildBaseHeaders({});
if (accounts.length > 0) {
console.log(`[DS-API] Loaded ${accounts.length} auth account(s): ${accounts.map(a => a.id).join(', ')}`);
for (const a of accounts) {
if (a.config.device_id) logDebug(`[DS-API] ${a.id} carries device_id ${String(a.config.device_id).slice(0, 12)}...`);
}
return true;
}
if (fatal) {
console.error(`[DS-API] FATAL: Could not load any auth config. Expected ${paths.join(', ') || DS_CONFIG_PATH}`);
process.exit(1);
}
return false;
}
function hasAuthConfig() { return accounts.some(a => a.config.token && a.config.cookie); }
function accountStatus(account) {
return {
id: account.id,
ready: isAccountReady(account),
auth_unavailable: Boolean(account.authUnavailable),
cooldown: account.cooldownUntil > Date.now(),
cooldown_remaining_sec: Math.max(0, Math.ceil((account.cooldownUntil - Date.now()) / 1000)),
failures: account.failures,
consecutive_timeouts: account.consecutiveTimeouts || 0,
consecutive_failures: account.consecutiveFailures || 0,
used_this_hour: usedThisHour(account),
quota_exhausted: HOURLY_QUOTA > 0 && !withinQuota(account),
burst_used_1m: burstUsedThisMinute(account),
has_device_id: Boolean(account.config.device_id),
ewma_latency_ms: Math.round(Number(account.ewmaLatencyMs) || 0),
inflight: Number(account.inflight) || 0,
last_used_at: account.lastUsedAt || null,
last_telemetry_at: account.lastTelemetryAt || null,
last_telemetry_status: account.lastTelemetryStatus || null,
last_dispatched_at: account.lastDispatchedAt || null,
multi_tool_batches: account.multiToolBatchCount || 0,
batch_size_distribution: account.batchSizeCounts || {},
quarantined: Boolean(account.quarantined),
};
}
function quarantineAccount(account, reason = 'unknown', targetRootDir = null) {
if (!account) return null;
account.quarantined = true;
account.authUnavailable = true;
const idx = accounts.indexOf(account);
if (idx !== -1) {
accounts.splice(idx, 1);
}
if (accounts.length > 0) {
DS_CONFIG = accounts[0]?.config || {};
dsHeaders = accounts[0]?.headers || buildBaseHeaders({});
if (accountRoundRobin >= accounts.length) accountRoundRobin = 0;
} else {
DS_CONFIG = {};
dsHeaders = buildBaseHeaders({});
}
let quarantinedFilePath = null;
if (account.file && typeof account.file === 'string') {
try {
const todayStr = new Date().toISOString().slice(0, 10);
let repoRoot;
const fileDir = path.dirname(path.resolve(account.file));
if (targetRootDir) {
repoRoot = targetRootDir;
} else if (path.basename(fileDir) === 'accounts' || fileDir.endsWith('/accounts')) {
repoRoot = path.dirname(fileDir);
} else {
repoRoot = fileDir;
}
const qDir = path.join(repoRoot, `accounts-quarantined-${todayStr}`);
if (!fs.existsSync(qDir)) {
fs.mkdirSync(qDir, { recursive: true, mode: 0o700 });
}
const fileName = path.basename(account.file);
const targetFile = path.join(qDir, fileName);
if (fs.existsSync(account.file)) {
if (fs.existsSync(targetFile)) {
// Collision: never overwrite another quarantined credential
// (auth-cli.sh contract); the account stays out of the pool.
console.error(`[DS-API] Quarantine refused for ${account.id}: ${targetFile} already exists; left ${account.file} in place.`);
} else {
fs.renameSync(account.file, targetFile);
try { fs.chmodSync(targetFile, 0o600); } catch (e) {}
quarantinedFilePath = targetFile;
}
}
try {
if (fs.existsSync(fileDir)) {
const dirFiles = fs.readdirSync(fileDir);
const fileStem = path.basename(account.file, '.json');
for (const f of dirFiles) {
if (f === fileName) continue;
if (f.startsWith(`${fileName}.bak`) || f.startsWith(`${fileStem}.bak`)) {
const srcBak = path.join(fileDir, f);
const dstBak = path.join(qDir, f);
// Collision: keep the existing file, leave src in place (auth-cli.sh contract).
if (fs.existsSync(dstBak)) continue;
try {
fs.renameSync(srcBak, dstBak);
try { fs.chmodSync(dstBak, 0o600); } catch (e) {}
} catch (e) {}
}
}
}
} catch (e) {}
account.file = quarantinedFilePath || account.file;
} catch (e) {
console.error(`[DS-API] Error moving account ${account.id} to quarantine: ${e.message}`);
}
}
for (const [, s] of sessions) {
if (s && s.accountId === account.id) {
s.accountId = null;
s.id = null;
s.parentMessageId = null;
}
}
persistSessions();
console.warn(`[DS-API] Account ${account.id} QUARANTINED (${reason}). Moved to ${quarantinedFilePath || 'quarantine'}. Active accounts remaining: ${accounts.length}`);
return quarantinedFilePath;
}
function rotateAccountsOnRestart(stateFile = path.join(__dirname, '.proxy-state.json')) {
if (accounts.length <= 1) return null;
let state = { restartCount: 0, lastAccountId: null, accountOffset: 0 };
try {
if (fs.existsSync(stateFile)) {
const raw = fs.readFileSync(stateFile, 'utf8');
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === 'object') {
state.restartCount = Number(parsed.restartCount) || 0;
state.lastAccountId = parsed.lastAccountId || null;
state.accountOffset = Number(parsed.accountOffset) || 0;
}
}
} catch (e) {}
const canonical = accounts.slice().sort((a, b) => a.id.localeCompare(b.id));
let lastIdx = state.lastAccountId ? canonical.findIndex(a => a.id === state.lastAccountId) : -1;
if (lastIdx === -1) {
lastIdx = 0;
}
const nextIdx = (lastIdx + 1) % canonical.length;
const targetAccount = canonical[nextIdx];
const currentIdx = accounts.findIndex(a => a.id === targetAccount.id);
if (currentIdx > 0) {
const shifted = accounts.splice(0, currentIdx);
accounts.push(...shifted);
}
state.restartCount += 1;
state.lastAccountId = accounts[0].id;
state.accountOffset = nextIdx;
try {
const tmp = `${stateFile}.tmp`;
fs.writeFileSync(tmp, JSON.stringify(state, null, 2), { mode: 0o600 });
fs.renameSync(tmp, stateFile);
try { fs.chmodSync(stateFile, 0o600); } catch (e) {}
} catch (e) {
try { fs.writeFileSync(stateFile, JSON.stringify(state, null, 2), { mode: 0o600 }); } catch (e2) {}
}
DS_CONFIG = accounts[0]?.config || {};
dsHeaders = accounts[0]?.headers || buildBaseHeaders({});
accountRoundRobin = nextIdx;
// Rotation changes selection order for NEW chats only. Restored sessions
// retain accountId + remote IDs: moving a live chat to another credential
// is both invalid upstream and a silent context split.
console.log(`[DS-API] Proxy restart account rotation: switched starting account to ${accounts[0].id} (rotation #${state.restartCount}, account ${nextIdx + 1}/${accounts.length}); restored chats unchanged`);
return state;
}
// Dynamic model discovery (round-3 G3): hourly advisory poll of
// client/settings?scope=model. Advisory ONLY — never adds/removes aliases;
// surfaces upstream flag flips in /health for operators. Verified 2026-09-17:
// the did value is irrelevant (random UUID works); the x-client-* header group
// is the actual gate (minimal headers -> perpetual SETTINGS_NOT_FOUND).
const MODEL_DISCOVERY_ON = process.env.DEEPSEEK_MODEL_DISCOVERY !== '0';
const MODEL_DISCOVERY_MS = numEnv('DEEPSEEK_MODEL_DISCOVERY_MS', 3600000, 300000);
let discoveredModels = { types: [], fetchedAt: 0 };
function parseModelDiscovery(body) {
try {
// Envelope gate (H-1): a table-shaped biz_data inside an error