Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Routing summary: match the server's turn order in its edge cases

Lin (kwant bot)1a9c310b406e
diff --git a/frontend/src/components/EnginesPanel.tsx b/frontend/src/components/EnginesPanel.tsx
index 56aa4a90..29387c32 100644
--- a/frontend/src/components/EnginesPanel.tsx
+++ b/frontend/src/components/EnginesPanel.tsx
@@ -163,13 +163,14 @@ export const RouteSummary: React.FC<{ rows: RouteRow[]; pending: boolean }> = ({
<span className="w-14 shrink-0 font-semibold text-zinc-100">{r.label}</span>
<span className="px-1.5 py-0.5 rounded-md bg-zinc-800/80 text-zinc-300">{r.engine}</span>
<ArrowRight size={11} className="text-zinc-500" aria-label="on" />
- <span className={`px-1.5 py-0.5 rounded-md font-medium ${r.account.otherProvider ? 'bg-amber-500/15 text-amber-100' : 'bg-emerald-500/15 text-emerald-100'}`}>
+ <span className={`px-1.5 py-0.5 rounded-md font-medium ${r.account.otherProvider || r.note ? 'bg-amber-500/15 text-amber-100' : 'bg-emerald-500/15 text-emerald-100'}`}>
{r.account.name}
</span>
{r.account.detail && <span className={r.account.otherProvider ? 'text-amber-200' : 'text-zinc-400'}>{r.account.detail}</span>}
</div>
<p className="mt-1 pl-0 sm:pl-[3.875rem] text-zinc-500">
- {r.then.length > 0 ? `If it’s out of usage or signed out: ${r.then.join(', then ')}.` : 'No backup account is ready.'}
+ {r.note && <span className="text-amber-200">{r.note} </span>}
+ {r.then.length > 0 ? `If it’s out of usage or signed out: ${r.then.join(', then ')}.` : r.note ? '' : 'No backup account is ready.'}
{r.defaultModel && <> Kwants on Auto get {r.defaultModel}.</>}
</p>
{r.fallback && <p className="pl-0 sm:pl-[3.875rem] text-zinc-500">{r.fallback}</p>}
diff --git a/frontend/src/profiles.ts b/frontend/src/profiles.ts
index 99953963..50383abe 100644
--- a/frontend/src/profiles.ts
+++ b/frontend/src/profiles.ts
@@ -233,6 +233,12 @@ export function accountsForCard(profiles: Profile[], card: string): Profile[] {
);
}
+/** What turn routing asks of an account (Availability::usable on the server): not out of usage, not an
+ * API-key account without its key, and not signed out on its last check. Not checked yet still counts,
+ * so the server tries it; a refused turn's "auth" error doesn't matter until a check says signed out. */
+export const accountRoutable = (p: Profile, now = Date.now()) =>
+ !activeLimit(p.limited_until, now) && !(p.kind === 'api_key' && !p.has_key) && p.signed_in !== false;
+
/** Could run a turn as last checked: on, signed in (or keyed), not out of usage. */
export const accountUsable = (p: Profile, now = Date.now()) =>
p.enabled && !activeLimit(p.limited_until, now) && p.last_error !== 'auth' && (p.kind === 'api_key' ? p.has_key : p.signed_in === true);
diff --git a/frontend/src/routing.ts b/frontend/src/routing.ts
index 498ab678..736c6d99 100644
--- a/frontend/src/routing.ts
+++ b/frontend/src/routing.ts
@@ -1,9 +1,10 @@
// "What runs your kwants": for each model family that can run, the engine and account a turn starts
// on and the accounts it falls back to, in the server's order (src/profiles/mod.rs failover_order):
-// the family's switched-on accounts, the default first, then by priority; at most 3 tries.
+// the family's switched-on accounts, the default first, then by priority; at most 3 tries. Accounts
+// on an engine that's off drop out last, as in the server's plan (src/server/profile_turn.rs).
import { EngineInfo, FAMILIES, FAMILY_OF, Family, isUsable } from './engines';
-import { Profile, accountUsable, cardOf, isThirdPartyLabel } from './profiles';
+import { Profile, accountRoutable, activeLimit, cardOf, isThirdPartyLabel } from './profiles';
/** Turns try at most this many accounts (MAX_ATTEMPTS on the server). */
const MAX_TRIES = 3;
@@ -24,6 +25,8 @@ export interface RouteRow {
engine: string;
/** The account a turn starts on. */
account: RouteAccount;
+ /** Why that account may not run, when it's tried anyway (the only one left, or not checked yet). */
+ note: string | null;
/** Then these, if the first is out of usage or signed out. */
then: string[];
/** The model a kwant on "Auto" for this family gets, when the engine says. */
@@ -42,18 +45,28 @@ function describe(p: Profile): RouteAccount {
return { name: accountName(p), detail, otherProvider };
}
-/** The family's accounts in the order a turn tries them; empty when none is switched on. */
+/** The family's accounts in the order a turn tries them; empty when none is switched on.
+ * Equal priorities keep the API's order, which is the server's tie-break (created_at, then id). */
export function tryOrder(profiles: Profile[], family: Family, now = Date.now()): Profile[] {
const on = profiles
.filter((p) => p.engine === family && p.enabled)
- .sort((a, b) => Number(b.is_default) - Number(a.is_default) || a.priority - b.priority || a.name.localeCompare(b.name));
+ .sort((a, b) => Number(b.is_default) - Number(a.is_default) || a.priority - b.priority);
const [first, ...rest] = on;
if (!first) return [];
- const usableRest = rest.filter((p) => accountUsable(p, now));
- const order = accountUsable(first, now) || usableRest.length === 0 ? [first, ...usableRest] : usableRest;
+ const usableRest = rest.filter((p) => accountRoutable(p, now));
+ const order = accountRoutable(first, now) || usableRest.length === 0 ? [first, ...usableRest] : usableRest;
return order.slice(0, MAX_TRIES);
}
+/** Why a turn's first account may not run, or null when it was ready on its last check. */
+function startNote(p: Profile, now: number): string | null {
+ if (activeLimit(p.limited_until, now)) return 'It’s out of usage and nothing else is ready, so kwants will stop and say so.';
+ if (p.kind === 'api_key') return p.has_key ? null : 'It has no key and nothing else is ready, so kwants will stop and say so.';
+ if (p.signed_in === false) return 'It needs sign-in and nothing else is ready, so kwants will stop and say so.';
+ if (p.signed_in === null && p.kind !== 'system') return 'Its sign-in hasn’t been checked yet. Kwants try it anyway.';
+ return null;
+}
+
/** One row per family that has a ready engine; [] when nothing can run. */
export function routeSummary(engines: EngineInfo[], profiles: Profile[] | null, now = Date.now()): RouteRow[] {
return FAMILIES.flatMap(({ id: family }) => {
@@ -61,19 +74,19 @@ export function routeSummary(engines: EngineInfo[], profiles: Profile[] | null,
if (ready.length === 0) return [];
const mine = profiles?.filter((p) => p.engine === family) ?? [];
const order = profiles ? tryOrder(profiles, family, now) : [];
- // Accounts on an engine that's off can't run, so skip them like the server's plan does.
- const onEngine = (p: Profile) => ready.some((e) => e.id === cardOf(p));
- const usable = order.filter((p) => accountUsable(p, now) && onEngine(p));
- const start = usable[0];
+ // The server drops accounts whose engine is off from the plan; the rest are tried in order.
+ const onEngine = (p: Profile) => p.kind === 'system' || ready.some((e) => e.id === cardOf(p));
+ const route = order.filter(onEngine);
+ const start = route[0];
const cli = ready.find((e) => e.kind === 'cli');
const host = mine.find((p) => p.kind === 'system');
let account: RouteAccount;
let engine: EngineInfo;
if (start) {
account = describe(start);
- engine = ready.find((e) => e.id === cardOf(start)) ?? ready[0];
- } else if (profiles && cli && order.length === 0) {
- // No account switched on: the turn runs on the CLI's own login.
+ engine = start.kind === 'system' ? cli ?? ready[0] : ready.find((e) => e.id === cardOf(start)) ?? ready[0];
+ } else if (profiles && cli) {
+ // No account to try: the turn runs on the CLI's own login.
account = host ? describe(host) : { name: hostName, detail: null, otherProvider: false };
engine = cli;
} else {
@@ -81,12 +94,13 @@ export function routeSummary(engines: EngineInfo[], profiles: Profile[] | null,
engine = ready[0];
account = { name: engine.uses?.label ?? (engine.kind === 'local' ? 'This computer' : engine.kind === 'api' ? 'API key in Settings' : hostName), detail: null, otherProvider: false };
}
- const then = usable.slice(1).map(accountName);
+ const note = start ? startNote(start, now) : null;
+ const then = route.slice(1).map(accountName);
const hostOff = !!host && !host.enabled;
const fallback = hostOff && engine.kind === 'cli' && mine.some((p) => p.kind !== 'system')
? `If you switch off every ${familyName(family)} account, kwants use This machine.`
: null;
const defaultModel = engine.models.find((m) => m.default)?.label ?? null;
- return [{ family, label: familyName(family), engine: engine.name, account, then, defaultModel, fallback }];
+ return [{ family, label: familyName(family), engine: engine.name, account, note, then, defaultModel, fallback }];
});
}
diff --git a/frontend/tests/enginesAccounts.test.mjs b/frontend/tests/enginesAccounts.test.mjs
index 0aabfdf5..646da28d 100644
--- a/frontend/tests/enginesAccounts.test.mjs
+++ b/frontend/tests/enginesAccounts.test.mjs
@@ -36,7 +36,8 @@ const profile = (over) => ({
id: 'prf_x', engine: 'claude', name: 'Work', kind: 'cli_home', enabled: true, is_default: false, priority: 100,
home_dir: null, has_key: false, signed_in: true, account_label: null, limited_until: null, last_error: null, ...over,
});
-const machine = profile({ id: 'prf_system_claude', name: 'This machine', kind: 'system', signed_in: null, priority: 0 });
+// The host CLI is signed out, so This machine's own check says signed out too (src/profiles/status.rs probes it).
+const machine = profile({ id: 'prf_system_claude', name: 'This machine', kind: 'system', signed_in: false, priority: 0 });
const personal = profile({ id: 'prf_personal', name: 'Personal', is_default: true, priority: 10 });
const api = (engines) => ({ engines, error: null, rechecking: false, recheck: async () => {}, reload: async () => {}, setEnabled: async () => {}, apply: () => {} });
diff --git a/frontend/tests/routing.test.mjs b/frontend/tests/routing.test.mjs
index 0f0b92ad..5d30cb32 100644
--- a/frontend/tests/routing.test.mjs
+++ b/frontend/tests/routing.test.mjs
@@ -72,3 +72,44 @@ test('nothing ready means no rows; an API key from Settings shows as such', () =
assert.equal(row.engine, 'Anthropic API key');
assert.equal(row.account.name, 'API key in Settings');
});
+
+// Rigel's edge run: the only switched-on accounts are a signed-out Work and an unchecked one.
+test('when no account is ready, the summary names the one the server still tries, with why', () => {
+ const out = { ...work, signed_in: false, is_default: true };
+ const [row] = m.routeSummary([engine('claude', { uses: { profile_id: 'prf_personal', label: 'Personal' } })], [machine, out, { ...personal, enabled: false }], NOW);
+ assert.equal(row.account.name, 'Work');
+ assert.match(row.note, /needs sign-in/);
+ assert.deepEqual(row.then, []);
+ const limited = { ...personal, limited_until: '2026-10-17T21:00:00Z' };
+ const [r2] = m.routeSummary([engine('claude')], [machine, limited], NOW);
+ assert.equal(r2.account.name, 'Personal');
+ assert.match(r2.note, /out of usage/);
+ // A ready account carries no note.
+ assert.equal(m.routeSummary([engine('claude')], [machine, personal], NOW)[0].note, null);
+});
+
+test('an account whose sign-in is not checked yet is tried, as on the server', () => {
+ const unchecked = profile({ id: 'prf_new', name: 'New', priority: 10, signed_in: null });
+ const out = { ...work, signed_in: false };
+ assert.deepEqual(m.tryOrder([machine, personal, unchecked, out], 'claude', NOW).map((p) => p.name), ['Personal', 'New']);
+ const [row] = m.routeSummary([engine('claude')], [machine, { ...unchecked, is_default: true }, out], NOW);
+ assert.equal(row.account.name, 'New');
+ assert.match(row.note, /hasn’t been checked/);
+ // A refused turn's "auth" error alone doesn't take it out; only a signed-out check does.
+ assert.deepEqual(m.tryOrder([{ ...personal, last_error: 'auth' }, work], 'claude', NOW).map((p) => p.name), ['Personal', 'Work']);
+});
+
+test('equal priorities keep the API order (created_at, then id), not the name', () => {
+ // The API lists Zed before Alpha: Zed was added first.
+ const zed = profile({ id: 'prf_a1', name: 'Zed', priority: 100 });
+ const alpha = profile({ id: 'prf_b2', name: 'Alpha', priority: 100 });
+ assert.deepEqual(m.tryOrder([machine, zed, alpha], 'claude', NOW).map((p) => p.name), ['Zed', 'Alpha']);
+ assert.deepEqual(m.tryOrder([machine, alpha, zed], 'claude', NOW).map((p) => p.name), ['Alpha', 'Zed']);
+});
+
+test('an API-key account on an engine that is off drops out of the route', () => {
+ const key = profile({ id: 'prf_key', name: 'Key', kind: 'api_key', has_key: true, priority: 10 });
+ const [row] = m.routeSummary([engine('claude')], [machine, key, personal], NOW);
+ assert.equal(row.account.name, 'Personal');
+ assert.deepEqual(row.then, []);
+});