Repository navigation
Expand file tree
/
Copy pathtest-account-failover.ts
More file actions
1142 lines (1063 loc) · 39.7 KB
/
Copy pathtest-account-failover.ts
File metadata and controls
1142 lines (1063 loc) · 39.7 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
/**
* Account failover: the limit signal, the account-scoped override, the form,
* the answer, and the replay.
*
* The unit half pins the pieces a wrong answer would silently break (the
* detection's NEGATIVE cases above all: a transient error must never move
* where the billing lands). The fake-CLI half drives a real `doStream` twice
* and asserts what only the wiring can show: that the limited turn ends on a
* `question` tool-call rather than an error, and that the answer turn spawns
* the OTHER account with the `@` suffix off the model and the conversation
* replayed.
*
* Usage: npx tsx --test test-account-failover.ts
*/
import assert from "node:assert/strict"
import { after, test } from "node:test"
import {
chmodSync,
existsSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { ensureAccountRuntime } from "./src/accounts.js"
import {
ACCOUNT_FAILOVER_TOOL_CALL_PREFIX,
FAILOVER_MARKER,
ACCOUNT_BLOCK_MARKER,
_resetAccountOverrides,
accountBlockKind,
loginCommandFor,
buildFailoverContinuationPrompt,
clearAccountOverride,
consumeAccountFailoverAnswer,
createAccountFailoverQuestionCall,
failoverCandidates,
failoverUntil,
formatFailoverNote,
isAccountFailoverQuestionActive,
isAccountLimitError,
resolveAccountOverride,
resolveFailoverSpawn,
setAccountOverride,
stripAccountFailoverParts,
stripAccountSuffix,
} from "./src/account-failover.js"
import { _resetRateLimitReports, _resetSystemInitReports } from "./src/cli-events.js"
import { createClaudeCode } from "./src/index.js"
import { filterSideQuestionHistory, getClaudeUserMessage } from "./src/message-builder.js"
import { setOpencodeClient } from "./src/runtime-status.js"
import { deleteActiveProcess, sessionKey } from "./src/session-manager.js"
// Every account runtime this file builds lands under a throwaway HOME, so no
// test ever writes a wrapper or a config dir into the real one.
const HOME = mkdtempSync(join(tmpdir(), "opencode-failover-home-"))
const originalHome = process.env.HOME
const originalCache = process.env.XDG_CACHE_HOME
process.env.HOME = HOME
process.env.XDG_CACHE_HOME = join(HOME, "cache")
after(() => {
if (originalHome === undefined) delete process.env.HOME
else process.env.HOME = originalHome
if (originalCache === undefined) delete process.env.XDG_CACHE_HOME
else process.env.XDG_CACHE_HOME = originalCache
rmSync(HOME, { recursive: true, force: true })
})
// ---------------------------------------------------------------------------
// Detection
// ---------------------------------------------------------------------------
test("a rejected rate-limit event is an account limit", () => {
assert.equal(
isAccountLimitError({ rateLimit: { status: "rejected", rateLimitType: "five_hour" } }),
true,
)
assert.equal(isAccountLimitError({ rateLimit: { overageStatus: "rejected" } }), true)
})
test("both known limit error texts are recognised", () => {
assert.equal(
isAccountLimitError({
resultText:
"API Error: 400 Third-party apps now draw from your extra usage balance.",
}),
true,
)
assert.equal(
isAccountLimitError({
resultText: "You've hit your individual spend limit. Resets at 2026-09-20T18:00:00Z.",
}),
true,
)
// Curly apostrophe, which is what a copy-pasted CLI message often carries.
assert.equal(
isAccountLimitError({ resultText: "You’ve hit your individual spend limit." }),
true,
)
})
test("nothing else counts as an account limit", () => {
// The whole point of matching two exact strings: a transient failure that
// opened this form would silently move where the billing lands.
assert.equal(
isAccountLimitError({ rateLimit: { status: "allowed_warning", utilization: 0.9 } }),
false,
)
assert.equal(isAccountLimitError({ rateLimit: { status: "allowed" } }), false)
assert.equal(
isAccountLimitError({ resultText: "API Error: 400 invalid model name" }),
false,
)
assert.equal(
isAccountLimitError({ resultText: "fetch failed: ECONNRESET" }),
false,
)
assert.equal(isAccountLimitError({}), false)
assert.equal(isAccountLimitError({ resultText: "" }), false)
})
// ---------------------------------------------------------------------------
// The override store
// ---------------------------------------------------------------------------
test("an override is account-scoped, expires at the reset time, and can be cleared", () => {
_resetAccountOverrides()
const now = 1_000_000
setAccountOverride("appical", "default", now + 5_000, now)
assert.equal(resolveAccountOverride("appical", now), "default")
// Account-scoped, so every other account is untouched.
assert.equal(resolveAccountOverride("default", now), undefined)
// One second past the reset and the conversation goes back on its own.
assert.equal(resolveAccountOverride("appical", now + 5_001), undefined)
// Expiry deletes, so the next read is not a second log line.
assert.equal(resolveAccountOverride("appical", now), undefined)
setAccountOverride("appical", "default")
// No reset time from the CLI means "until opencode restarts".
assert.equal(resolveAccountOverride("appical", now + 10_000_000), "default")
clearAccountOverride("appical")
assert.equal(resolveAccountOverride("appical", now), undefined)
// An override onto itself would be a spawn loop, not a failover.
setAccountOverride("appical", "appical")
assert.equal(resolveAccountOverride("appical", now), undefined)
_resetAccountOverrides()
})
test("a reset time that is not in the future does not expire the switch at once", () => {
// Found by the fake-CLI test: with `until` behind `now` (clock skew, or a
// stale `resetsAt`), the very next read deleted the override and the turn
// spawned the limited account again and re-hit the same limit.
_resetAccountOverrides()
const now = 1_000_000
setAccountOverride("appical", "default", now - 1, now)
assert.equal(resolveAccountOverride("appical", now), "default")
assert.equal(resolveAccountOverride("appical", now + 10_000_000), "default")
_resetAccountOverrides()
})
test("failoverUntil accepts the CLI's seconds and tolerates milliseconds", () => {
assert.equal(failoverUntil(1_700_000_000), 1_700_000_000_000)
assert.equal(failoverUntil(1_700_000_000_000), 1_700_000_000_000)
assert.equal(failoverUntil(undefined), undefined)
})
// ---------------------------------------------------------------------------
// Resolving the spawn
// ---------------------------------------------------------------------------
test("the model's @account suffix comes off for a failover spawn", () => {
assert.equal(stripAccountSuffix("claude-opus-5@appical"), "claude-opus-5")
assert.equal(stripAccountSuffix("claude-opus-5"), "claude-opus-5")
})
test("resolveFailoverSpawn leaves everything alone without an override", async () => {
_resetAccountOverrides()
const spawn = await resolveFailoverSpawn({
account: "appical",
baseCliPath: "/bin/claude",
cliPath: "/cache/claude-appical",
modelId: "claude-opus-5@appical",
})
assert.deepEqual(spawn, {
cliPath: "/cache/claude-appical",
modelId: "claude-opus-5@appical",
failedOver: false,
})
})
test("a default target spawns the bare binary, a named target its wrapper", async () => {
_resetAccountOverrides()
setAccountOverride("appical", "default")
const toDefault = await resolveFailoverSpawn({
account: "appical",
baseCliPath: "/bin/claude",
cliPath: "/cache/claude-appical",
modelId: "claude-opus-5@appical",
})
// `default` has no config dir at all, so it is the base binary itself.
assert.equal(toDefault.cliPath, "/bin/claude")
assert.equal(toDefault.modelId, "claude-opus-5")
assert.equal(toDefault.target, "default")
assert.equal(toDefault.failedOver, true)
_resetAccountOverrides()
setAccountOverride("default", "work")
const toNamed = await resolveFailoverSpawn({
account: "default",
baseCliPath: "/bin/claude",
cliPath: "/bin/claude",
modelId: "claude-opus-5",
})
assert.equal(toNamed.target, "work")
assert.equal(toNamed.failedOver, true)
assert.match(toNamed.cliPath, /claude-work$/)
assert.equal(existsSync(toNamed.cliPath), true)
_resetAccountOverrides()
})
// ---------------------------------------------------------------------------
// The gate and the candidate list
// ---------------------------------------------------------------------------
test("candidates are every configured account except the limited one", () => {
assert.deepEqual(failoverCandidates(["default", "work", "appical"], "work"), [
"default",
"appical",
])
assert.deepEqual(failoverCandidates(["default"], "default"), [])
assert.deepEqual(failoverCandidates(undefined, "default"), [])
// Normalised and deduped, the same way accounts.ts normalises them.
assert.deepEqual(failoverCandidates(["My Work", "my-work"], "default"), ["my-work"])
})
test("the form is gated on more than one account, a question tool, and the transport", () => {
const base = {
configured: "ask" as const,
candidates: ["work"],
opencodeHasQuestion: true,
compactionMode: false,
interactive: false,
childSession: false,
}
assert.equal(isAccountFailoverQuestionActive(base), true)
// On by default: an unset option behaves as "ask".
assert.equal(
isAccountFailoverQuestionActive({ ...base, configured: undefined }),
true,
)
assert.equal(isAccountFailoverQuestionActive({ ...base, configured: "off" }), false)
assert.equal(isAccountFailoverQuestionActive({ ...base, candidates: [] }), false)
assert.equal(
isAccountFailoverQuestionActive({ ...base, opencodeHasQuestion: false }),
false,
)
assert.equal(isAccountFailoverQuestionActive({ ...base, compactionMode: true }), false)
assert.equal(isAccountFailoverQuestionActive({ ...base, interactive: true }), false)
// A subagent follows its parent's account for free.
assert.equal(isAccountFailoverQuestionActive({ ...base, childSession: true }), false)
})
// ---------------------------------------------------------------------------
// The form and its answer
// ---------------------------------------------------------------------------
test("the form offers the other accounts plus stop, and names the reset time", () => {
const call = createAccountFailoverQuestionCall("sk-form", {
sourceAccount: "appical",
candidates: ["default", "work"],
resetsAt: 1_700_000_000,
window: "five_hour",
})
assert.equal(call.toolName, "question")
assert.ok(call.toolCallId.startsWith(ACCOUNT_FAILOVER_TOOL_CALL_PREFIX))
const question = call.input.questions[0]
assert.equal(question.header, "Account limit")
assert.match(question.question, /"appical" is out of usage/)
assert.match(question.question, /five_hour/)
assert.match(question.question, /2023-11-14/)
assert.match(question.question, /Leaving this unanswered waits/)
assert.deepEqual(
question.options.map((option) => option.label),
["default", "work", "stop"],
)
// The source account is never one of its own options.
assert.equal(
question.options.some((option) => option.label === "appical"),
false,
)
// The two costs an operator cannot see from the label alone.
assert.match(question.options[0].description, /replayed as a fresh Claude session/)
assert.match(question.options[0].description, /MCP server configured only in "appical"/)
assert.equal(question.custom, true)
assert.equal(question.multiple, false)
})
function answer(toolCallId: string, output: unknown) {
return [
{
role: "tool",
content: [{ type: "tool-result", toolCallId, toolName: "question", output }],
},
]
}
test("picking an offered account switches to it", () => {
const call = createAccountFailoverQuestionCall("sk-a", {
sourceAccount: "appical",
candidates: ["default", "work"],
resetsAt: 1_700_000_000,
})
const result = consumeAccountFailoverAnswer(
"sk-a",
answer(call.toolCallId, { type: "text", value: "work" }) as any,
)
assert.deepEqual(result, {
kind: "switch",
target: "work",
sourceAccount: "appical",
resetsAt: 1_700_000_000,
})
})
test("custom text naming an account switches, and the answer is consumed once", () => {
const call = createAccountFailoverQuestionCall("sk-b", {
sourceAccount: "default",
candidates: ["work"],
})
const prompt = answer(call.toolCallId, {
type: "text",
// opencode wraps a picked answer in its own sentence; the unwrapper
// handles that, and the name itself is normalised the way accounts are.
value: " Work ",
}) as any
assert.deepEqual(consumeAccountFailoverAnswer("sk-b", prompt), {
kind: "switch",
target: "work",
sourceAccount: "default",
resetsAt: undefined,
})
// Consumed: a replayed prompt must not switch a second time.
assert.equal(consumeAccountFailoverAnswer("sk-b", prompt), null)
})
test("stop, a dismissal and unrecognised text all end the turn", () => {
for (const [label, output] of [
["stop", { type: "text", value: "stop" }],
["dismissal", { type: "execution-denied", reason: "The user dismissed this question" }],
["unknown text", { type: "text", value: "use my other laptop" }],
["an account that was not offered", { type: "text", value: "appical" }],
["an empty answer", { type: "text", value: " " }],
] as const) {
const call = createAccountFailoverQuestionCall(`sk-${label}`, {
sourceAccount: "default",
candidates: ["work"],
})
const result = consumeAccountFailoverAnswer(
`sk-${label}`,
answer(call.toolCallId, output) as any,
)
assert.equal(result?.kind, "stop", `${label} should stop`)
}
})
/**
* The sentence opencode's `question` tool actually returns, as read out of
* the 1.18.32 binary. Every failover pick arrived in this shape; the tests
* above feed bare labels, which is how the form shipped unable to switch.
*/
function opencodeAnswer(question: string, ...answers: string[]): string {
const value = answers.length > 0 ? answers.join(", ") : "Unanswered"
return `User has answered your questions: "${question}"="${value}". You can now continue with the user's answers in mind.`
}
test("opencode's own answer sentence switches, although the question has quotes", () => {
const call = createAccountFailoverQuestionCall("sk-real", {
sourceAccount: "appical",
candidates: ["default"],
resetsAt: 1_790_170_000,
})
const question = call.input.questions[0].question
// The failover question quotes the account, which is what defeats a naive split.
assert.match(question, /"appical"/)
assert.deepEqual(
consumeAccountFailoverAnswer(
"sk-real",
answer(call.toolCallId, { type: "text", value: opencodeAnswer(question, "default") }) as any,
),
{ kind: "switch", target: "default", sourceAccount: "appical", resetsAt: 1_790_170_000 },
)
// `stop`, a blank answer and a dismissal in their real shapes.
const stopCall = createAccountFailoverQuestionCall("sk-real-stop", {
sourceAccount: "appical",
candidates: ["default"],
})
const stopQuestion = stopCall.input.questions[0].question
assert.deepEqual(
consumeAccountFailoverAnswer(
"sk-real-stop",
answer(stopCall.toolCallId, { type: "text", value: opencodeAnswer(stopQuestion, "stop") }) as any,
),
{ kind: "stop", reason: "the operator chose to stop" },
)
const blankCall = createAccountFailoverQuestionCall("sk-real-blank", {
sourceAccount: "appical",
candidates: ["default"],
})
assert.deepEqual(
consumeAccountFailoverAnswer(
"sk-real-blank",
answer(blankCall.toolCallId, {
type: "text",
value: opencodeAnswer(blankCall.input.questions[0].question),
}) as any,
),
{ kind: "stop", reason: "no answer" },
)
const dismissCall = createAccountFailoverQuestionCall("sk-real-dismiss", {
sourceAccount: "appical",
candidates: ["default"],
})
assert.deepEqual(
consumeAccountFailoverAnswer(
"sk-real-dismiss",
answer(dismissCall.toolCallId, {
type: "error-text",
value: "The user dismissed this question",
}) as any,
),
{ kind: "stop", reason: "The user dismissed this question" },
)
})
test("an answer to a form asked before an opencode restart still switches", () => {
// No createAccountFailoverQuestionCall here: the process that asked is gone.
const toolCallId = `${ACCOUNT_FAILOVER_TOOL_CALL_PREFIX}fromlastrun`
const question =
'The Claude account "appical" is out of usage in the 5-hour window. Continue this task on another configured account? Leaving this unanswered waits, at no cost.'
const prompt = [
{ role: "user", content: [{ type: "text", text: "build the thing" }] },
{
role: "assistant",
content: [{ type: "tool-call", toolCallId, toolName: "question", input: {} }],
},
...answer(toolCallId, { type: "text", value: opencodeAnswer(question, "default") }),
] as any
const fallback = { sourceAccount: "appical", candidates: ["default"] }
assert.deepEqual(consumeAccountFailoverAnswer("sk-restarted", prompt, fallback), {
kind: "switch",
target: "default",
sourceAccount: "appical",
resetsAt: undefined,
})
// An old answer further up is history, not this turn's answer.
const later = [...prompt, { role: "user", content: [{ type: "text", text: "next" }] }] as any
assert.equal(consumeAccountFailoverAnswer("sk-restarted", later, fallback), null)
// A single-account install offers nothing to fall back to.
assert.equal(
consumeAccountFailoverAnswer("sk-restarted", prompt, { sourceAccount: "appical", candidates: [] }),
null,
)
})
test("the form never reaches Claude as a stray tool result on the next turn", () => {
const toolCallId = `${ACCOUNT_FAILOVER_TOOL_CALL_PREFIX}staleform`
const prompt = [
{ role: "user", content: [{ type: "text", text: "build the thing" }] },
{
role: "assistant",
content: [{ type: "tool-call", toolCallId, toolName: "question", input: {} }],
},
...answer(toolCallId, { type: "error-text", value: "The user dismissed this question" }),
{ role: "user", content: [{ type: "text", text: "try again please" }] },
] as any
const envelope = getClaudeUserMessage(prompt, false, { cliToolCallIds: new Set() })
assert.doesNotMatch(envelope, /opencode_tool_result|dismissed this question/)
assert.match(envelope, /try again please/)
})
test("a tool-result for another call is not a failover answer", () => {
createAccountFailoverQuestionCall("sk-c", {
sourceAccount: "default",
candidates: ["work"],
})
assert.equal(
consumeAccountFailoverAnswer(
"sk-c",
answer("toolu_something_else", { type: "text", value: "work" }) as any,
),
null,
)
})
// ---------------------------------------------------------------------------
// Transcript handling
// ---------------------------------------------------------------------------
const dialogPrompt = [
{ role: "user", content: [{ type: "text", text: "build the thing" }] },
{
role: "assistant",
content: [
{ type: "text", text: "Working on it." },
{
type: "tool-call",
toolCallId: `${ACCOUNT_FAILOVER_TOOL_CALL_PREFIX}abc123`,
toolName: "question",
input: {},
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: `${ACCOUNT_FAILOVER_TOOL_CALL_PREFIX}abc123`,
toolName: "question",
output: { type: "text", value: "work" },
},
],
},
] as any
test("the dialog is stripped from a replayed transcript, keeping real content", () => {
const stripped = stripAccountFailoverParts(dialogPrompt) as any[]
assert.equal(stripped.length, 2)
// The assistant's own words survive; only the synthetic call goes.
assert.deepEqual(stripped[1].content, [{ type: "text", text: "Working on it." }])
// The tool message held nothing but the answer, so it is dropped entirely
// rather than replayed as an empty message.
assert.equal(
stripped.some((message) => message.role === "tool"),
false,
)
})
test("filterSideQuestionHistory drops the dialog and the failover note", () => {
const withNote = [
...dialogPrompt,
{
role: "assistant",
content: [{ type: "text", text: `${FAILOVER_MARKER} moved to "work".` }],
},
] as any
const filtered = filterSideQuestionHistory(withNote) as any[]
const serialized = JSON.stringify(filtered)
assert.equal(serialized.includes(ACCOUNT_FAILOVER_TOOL_CALL_PREFIX), false)
assert.equal(serialized.includes(FAILOVER_MARKER), false)
assert.match(serialized, /build the thing/)
assert.match(serialized, /Working on it/)
})
test("the continuation prompt replaces the dialog with a carry-on instruction", () => {
const built = buildFailoverContinuationPrompt(dialogPrompt, "work") as any[]
const last = built[built.length - 1]
assert.equal(last.role, "user")
const text = last.content[0].text
assert.match(text, /"work" account/)
assert.match(text, /Continue the task from where it stopped/)
assert.match(text, /do not start over/i)
assert.match(text, /Do not mention the account switch/)
assert.equal(JSON.stringify(built).includes(ACCOUNT_FAILOVER_TOOL_CALL_PREFIX), false)
})
test("the failover note is a ▌ line naming both accounts", () => {
const note = formatFailoverNote({
sourceAccount: "appical",
target: "work",
resetsAt: 1_700_000_000,
})
assert.ok(note.trimStart().startsWith(FAILOVER_MARKER))
assert.match(note, /"appical" is out of usage/)
assert.match(note, /continues on "work"/)
assert.match(note, /2023-11-14/)
})
// ---------------------------------------------------------------------------
// The wiring, through a real doStream and a fake CLI
// ---------------------------------------------------------------------------
/**
* A fake `claude` that answers differently depending on the account it was
* reached through: the limited one (via its wrapper, so `CLAUDE_CONFIG_DIR`
* is set) rejects, the failover target (the bare binary) answers. Every run
* appends what it saw, which is how the spawn's account and `--model` are
* asserted without reaching into the plugin.
*/
function createFakeCli() {
const cwd = mkdtempSync(join(tmpdir(), "opencode-failover-"))
const cliPath = join(cwd, "fake-claude.cjs")
const record = join(cwd, "spawns.jsonl")
const source = `#!/usr/bin/env node
const fs = require("node:fs")
const readline = require("node:readline")
if (process.argv.includes("--version")) {
process.stdout.write("2.1.263\\n")
process.exit(0)
}
const limited = !!process.env.CLAUDE_CONFIG_DIR
const LIMITED_LINES = [
{ type: "system", subtype: "init", session_id: "limited-session", tools: [] },
{
type: "rate_limit_event",
session_id: "limited-session",
rate_limit_info: { status: "rejected", rateLimitType: "five_hour", resetsAt: 4102444800 },
},
{
type: "result",
subtype: "error_during_execution",
session_id: "limited-session",
is_error: true,
result: "You've hit your individual spend limit.",
duration_ms: 10,
num_turns: 1,
},
]
const FAILOVER_LINES = [
{ type: "system", subtype: "init", session_id: "failover-session", tools: [] },
{
type: "stream_event",
session_id: "failover-session",
event: { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "carried on" } },
},
{
type: "stream_event",
session_id: "failover-session",
event: { type: "message_delta", delta: { stop_reason: "end_turn" } },
},
{
type: "result",
subtype: "success",
session_id: "failover-session",
is_error: false,
result: "carried on",
duration_ms: 10,
num_turns: 1,
usage: { input_tokens: 1, output_tokens: 1 },
},
]
// The steady state of an org with extra usage disabled, measured on CLI
// 2.1.280: the request is served, and the event still says overage is
// rejected. This must never look like a limit.
const SERVED_WITH_OVERAGE_REJECTED_LINES = [
{ type: "system", subtype: "init", session_id: "limited-session", tools: [] },
{
type: "rate_limit_event",
session_id: "limited-session",
rate_limit_info: {
status: "allowed",
rateLimitType: "five_hour",
resetsAt: 4102444800,
isUsingOverage: false,
overageStatus: "rejected",
overageDisabledReason: "org_level_disabled",
},
},
{
type: "stream_event",
session_id: "limited-session",
event: { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "served anyway" } },
},
{
type: "stream_event",
session_id: "limited-session",
event: { type: "message_delta", delta: { stop_reason: "end_turn" } },
},
{
type: "result",
subtype: "success",
session_id: "limited-session",
is_error: false,
result: "served anyway",
duration_ms: 10,
num_turns: 1,
usage: { input_tokens: 1, output_tokens: 1 },
},
]
const overageOnly = process.env.FAKE_CLI_OVERAGE_ONLY === "1"
// What the CLI printed for every turn once the appical login expired on
// 2026-09-23: its own synthetic reply, tagged with the \`error\` kind, then a
// failed result. The text is the CLI's, the kind is from its 2.1.280 schema.
const AUTH_TEXT = "Failed to authenticate: OAuth session expired and could not be refreshed"
const AUTH_EXPIRED_LINES = [
{ type: "system", subtype: "init", session_id: "limited-session", tools: [] },
{
type: "assistant",
session_id: "limited-session",
parent_tool_use_id: null,
error: "authentication_failed",
message: {
role: "assistant",
model: "<synthetic>",
stop_reason: "stop_sequence",
content: [{ type: "text", text: AUTH_TEXT }],
},
},
{
type: "result",
subtype: "success",
session_id: "limited-session",
is_error: true,
result: AUTH_TEXT,
duration_ms: 40,
num_turns: 1,
},
]
const authExpired = process.env.FAKE_CLI_AUTH_EXPIRED === "1"
const rl = readline.createInterface({ input: process.stdin })
let answered = false
rl.on("line", (line) => {
if (answered) return
answered = true
fs.appendFileSync(
${JSON.stringify(record)},
JSON.stringify({
argv: process.argv.slice(2),
configDir: process.env.CLAUDE_CONFIG_DIR || null,
stdin: line,
}) + "\\n",
)
const lines = limited
? (authExpired ? AUTH_EXPIRED_LINES : overageOnly ? SERVED_WITH_OVERAGE_REJECTED_LINES : LIMITED_LINES)
: FAILOVER_LINES
for (const l of lines) {
process.stdout.write(JSON.stringify(l) + "\\n")
}
})
`
writeFileSync(cliPath, source)
chmodSync(cliPath, 0o755)
return {
cliPath,
cwd,
spawns: (): any[] =>
existsSync(record)
? readFileSync(record, "utf8")
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line))
: [],
}
}
/** opencode's registry must carry `question`, or the form is not offered. */
setOpencodeClient({
tool: {
list: async () => ({ data: [{ id: "question", description: "", parameters: {} }] }),
},
})
const MODEL_ID = "claude-test-failover@appical"
async function buildFailoverModel(
fake: ReturnType<typeof createFakeCli>,
failoverAccounts: string[] = ["default", "appical"],
) {
// The limited account is reached through its own wrapper, exactly as a real
// account provider reaches it; `default` is the failover target and has no
// wrapper at all.
const runtime = await ensureAccountRuntime("appical", fake.cliPath)
return createClaudeCode({
cliPath: runtime.cliPath,
baseCliPath: fake.cliPath,
configDir: runtime.configDir,
account: "appical",
failoverAccounts,
cwd: fake.cwd,
bridgeOpencodeMcp: false,
proxyOpencodeMcpTools: false,
proxyTools: [],
}).languageModel(MODEL_ID)
}
const TOOLS = [
{
type: "function",
name: "read",
description: "Read a file",
inputSchema: { type: "object", properties: {} },
},
]
async function drain(response: any): Promise<any[]> {
const parts: any[] = []
for await (const part of response.stream) parts.push(part)
return parts
}
function textOf(parts: any[]): string {
return parts
.filter((part) => part.type === "text-delta")
.map((part) => part.delta)
.join("")
}
function modelArg(argv: string[]): string | undefined {
const at = argv.indexOf("--model")
return at === -1 ? undefined : argv[at + 1]
}
const turnOnePrompt = [{ role: "user", content: [{ type: "text", text: "go" }] }]
test("a usage limit ends the turn on a question listing the other account", async () => {
_resetAccountOverrides()
_resetRateLimitReports()
_resetSystemInitReports()
const fake = createFakeCli()
const sk = sessionKey(
fake.cwd,
`${MODEL_ID}::tools::default::context=["claude-code",null]`,
)
try {
const model = await buildFailoverModel(fake)
const parts = await drain(
await model.doStream({ prompt: turnOnePrompt, tools: TOOLS } as any),
)
const call = parts.find((part) => part.type === "tool-call")
assert.ok(call, "the limited turn must end on a question tool-call")
assert.equal(call.toolName, "question")
assert.ok(call.toolCallId.startsWith(ACCOUNT_FAILOVER_TOOL_CALL_PREFIX))
const input = JSON.parse(call.input)
assert.deepEqual(
input.questions[0].options.map((option: any) => option.label),
["default", "stop"],
)
// `tool-calls`, not the error finish the same result produces today:
// opencode only runs the tool when the turn ends this way.
const finish = parts.find((part) => part.type === "finish")
assert.equal(finish.finishReason.unified, "tool-calls")
// The operator still sees why, from the existing rate-limit note.
assert.match(textOf(parts), /▌ \*\*rate limit:\*\*/)
// The limited account really was the one that ran.
const spawns = fake.spawns()
assert.equal(spawns.length, 1)
assert.match(String(spawns[0].configDir), /\.claude-appical$/)
} finally {
deleteActiveProcess(sk)
_resetAccountOverrides()
rmSync(fake.cwd, { recursive: true, force: true })
}
})
test("an expired login names the account and the login command, and offers the switch", async () => {
_resetAccountOverrides()
_resetRateLimitReports()
_resetSystemInitReports()
const fake = createFakeCli()
const sk = sessionKey(
fake.cwd,
`${MODEL_ID}::tools::default::context=["claude-code",null]`,
)
process.env.FAKE_CLI_AUTH_EXPIRED = "1"
try {
const model = await buildFailoverModel(fake)
const parts = await drain(
await model.doStream({ prompt: turnOnePrompt, tools: TOOLS } as any),
)
const body = textOf(parts)
assert.match(body, /▌ \*\*claude account:\*\* the Claude account "appical" is not logged in/)
assert.match(body, /CLAUDE_CONFIG_DIR=\S*\.claude-appical claude auth login/)
assert.match(body, /Or pick another account below/)
const call = parts.find((part) => part.type === "tool-call")
assert.ok(call, "another configured account is offered")
const question = JSON.parse(call.input).questions[0]
assert.match(question.question, /"appical" is not logged in/)
assert.doesNotMatch(question.question, /out of usage/)
assert.deepEqual(question.options.map((option: any) => option.label), ["default", "stop"])
assert.equal(parts.find((part) => part.type === "finish").finishReason.unified, "tool-calls")
} finally {
delete process.env.FAKE_CLI_AUTH_EXPIRED
deleteActiveProcess(sk)
_resetAccountOverrides()
rmSync(fake.cwd, { recursive: true, force: true })
}
})
test("an expired login with no other account still says what to run", async () => {
_resetAccountOverrides()
_resetRateLimitReports()
_resetSystemInitReports()
const fake = createFakeCli()
const sk = sessionKey(
fake.cwd,
`${MODEL_ID}::tools::default::context=["claude-code",null]`,
)
process.env.FAKE_CLI_AUTH_EXPIRED = "1"
try {
const model = await buildFailoverModel(fake, ["appical"])
const parts = await drain(
await model.doStream({ prompt: turnOnePrompt, tools: TOOLS } as any),
)
const body = textOf(parts)
assert.match(body, /claude auth login`, then resend your message\.\n/)
assert.doesNotMatch(body, /pick another account/)
assert.equal(parts.some((part) => part.type === "tool-call"), false)
assert.equal(parts.find((part) => part.type === "finish").finishReason.unified, "error")
} finally {
delete process.env.FAKE_CLI_AUTH_EXPIRED
deleteActiveProcess(sk)
_resetAccountOverrides()
rmSync(fake.cwd, { recursive: true, force: true })
}
})
test("account blocks come from the CLI's error kind, and the login command names the account", () => {
assert.equal(accountBlockKind({ type: "assistant", error: "authentication_failed" }), "authentication_failed")
assert.equal(accountBlockKind({ type: "assistant", error: "billing_error" }), "billing_error")
// Request-level failures would fail on any account, so they open nothing.
assert.equal(accountBlockKind({ type: "assistant", error: "server_error" }), null)
assert.equal(accountBlockKind({ type: "assistant", error: "rate_limit" }), null)
assert.equal(accountBlockKind({ type: "assistant", error: "toString" }), null)
assert.equal(accountBlockKind({ type: "result", error: "authentication_failed" }), null)
assert.equal(accountBlockKind({ type: "assistant" }), null)
assert.equal(loginCommandFor(undefined), "claude auth login")
assert.equal(
loginCommandFor("/Users/me/.claude-work", "/Users/me"),
"CLAUDE_CONFIG_DIR=~/.claude-work claude auth login",
)
assert.equal(
loginCommandFor("/srv/claude-work", "/Users/me"),
"CLAUDE_CONFIG_DIR=/srv/claude-work claude auth login",
)
// The note is stripped from rebuilt transcripts like every other `▌` note.
assert.ok(ACCOUNT_BLOCK_MARKER.startsWith("▌"))
})
test("a served turn whose limit event only rejects overage keeps its answer and asks nothing", async () => {
_resetAccountOverrides()
_resetRateLimitReports()
_resetSystemInitReports()
const fake = createFakeCli()
const sk = sessionKey(
fake.cwd,
`${MODEL_ID}::tools::default::context=["claude-code",null]`,
)
process.env.FAKE_CLI_OVERAGE_ONLY = "1"
try {
const model = await buildFailoverModel(fake)
const parts = await drain(
await model.doStream({ prompt: turnOnePrompt, tools: TOOLS } as any),
)
assert.equal(
parts.some((part) => part.type === "tool-call"),
false,
"a served turn must not end on the failover form",
)
const finish = parts.find((part) => part.type === "finish")
assert.equal(finish.finishReason.unified, "stop")
const body = textOf(parts)
assert.match(body, /served anyway/)
assert.doesNotMatch(body, /rate limit/)
} finally {
delete process.env.FAKE_CLI_OVERAGE_ONLY
deleteActiveProcess(sk)
_resetAccountOverrides()
rmSync(fake.cwd, { recursive: true, force: true })
}
})
test("answering with the other account continues the task on it, replayed", async () => {
_resetAccountOverrides()
_resetRateLimitReports()
_resetSystemInitReports()
const fake = createFakeCli()
const sk = sessionKey(
fake.cwd,
`${MODEL_ID}::tools::default::context=["claude-code",null]`,
)
try {
const model = await buildFailoverModel(fake)
const first = await drain(
await model.doStream({ prompt: turnOnePrompt, tools: TOOLS } as any),
)
const call = first.find((part) => part.type === "tool-call")
assert.ok(call)
const second = await drain(
await model.doStream({