diff options
| author | Adam Malczewski <[email protected]> | 2026-06-28 15:27:43 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-28 15:27:43 +0900 |
| commit | 3899ecef48325014a3bb5d6333a7a3e1fc34296a (patch) | |
| tree | 4068e66677dd97fd0f02208dac297b91c21b5755 /src/app | |
| parent | 7f2d66d5c132b45f7d01b0d9107f341094b9c904 (diff) | |
| parent | 0f1b04bd7976ea416f11d83e4b7b78cf537bfdf3 (diff) | |
| download | dispatch-web-3899ecef48325014a3bb5d6333a7a3e1fc34296a.tar.gz dispatch-web-3899ecef48325014a3bb5d6333a7a3e1fc34296a.zip | |
Merge branch 'feature/concurrency-fixes' into predev
# Conflicts:
# backend-handoff.md
Diffstat (limited to 'src/app')
| -rw-r--r-- | src/app/App.svelte | 4 | ||||
| -rw-r--r-- | src/app/store.svelte.ts | 80 | ||||
| -rw-r--r-- | src/app/store.test.ts | 112 |
3 files changed, 195 insertions, 1 deletions
diff --git a/src/app/App.svelte b/src/app/App.svelte index e508203..b2cfd2d 100644 --- a/src/app/App.svelte +++ b/src/app/App.svelte @@ -76,6 +76,7 @@ type DeleteConcurrencyLimit, type LoadConcurrencyLimits, type LoadConcurrencyStatus, + type SaveConcurrencyCooldown, type SaveConcurrencyLimit, } from "../features/concurrency"; import type { ChatStore } from "../features/chat"; @@ -491,6 +492,8 @@ const deleteConcurrencyLimit: DeleteConcurrencyLimit = (providerId) => store.deleteConcurrencyLimit(providerId); const loadConcurrencyStatus: LoadConcurrencyStatus = () => store.concurrencyStatus(); + const saveConcurrencyCooldown: SaveConcurrencyCooldown = (providerId, cooldownMs) => + store.setConcurrencyCooldown(providerId, cooldownMs); </script> <main class="relative flex h-screen overflow-hidden"> @@ -841,6 +844,7 @@ saveLimit={saveConcurrencyLimit} deleteLimit={deleteConcurrencyLimit} loadStatus={loadConcurrencyStatus} + saveCooldown={saveConcurrencyCooldown} /> {/if} {/snippet} diff --git a/src/app/store.svelte.ts b/src/app/store.svelte.ts index 82d755d..22b0a25 100644 --- a/src/app/store.svelte.ts +++ b/src/app/store.svelte.ts @@ -55,12 +55,14 @@ import { import type { ChatStore, HistorySync, MetricsSync } from "../features/chat"; import { createChatStore } from "../features/chat"; import type { + ConcurrencyCooldownResult, ConcurrencyDeleteResult, ConcurrencyLimitResult, ConcurrencyLimitsResult, ConcurrencyStatusResult, } from "../features/concurrency"; import { + normalizeConcurrencyCooldown, normalizeConcurrencyLimit, normalizeConcurrencyLimits, normalizeConcurrencyStatus, @@ -452,11 +454,29 @@ export interface AppStore { /** * Fetch live concurrency status for every provider with a configured limit * (`GET /concurrency/status`): in-flight slots held, agents queued, and a paused - * state with a `pausedUntil` epoch-ms when a 429 backoff is in effect. Returns + * state with a `pausedUntil` epoch-ms when a 429 backoff is in effect. Each + * entry also carries the per-slot release `cooldownMs` + an `autoReduced` flag + * (true when a 429 auto-reduced the limit by 1; the FE renders a banner). Returns * an empty list when the extension isn't loaded (`{ providers: [] }`). */ concurrencyStatus(): Promise<ConcurrencyStatusResult>; /** + * Fetch the per-slot release cooldown (ms) for one provider + * (`GET /concurrency/cooldown/:providerId`). `404` (no concurrency config at + * all) and `503` (extension not loaded) both surface as `ok: false`. + */ + getConcurrencyCooldown(providerId: string): Promise<ConcurrencyCooldownResult>; + /** + * Set the per-slot release cooldown (ms) for one provider + * (`PUT /concurrency/cooldown/:providerId`, body `{ cooldownMs }`). `cooldownMs` + * must be a non-negative integer (0 = no cooldown / instant re-admission); an + * invalid body is `400`. Persists + applies to subsequently recycled slots. + */ + setConcurrencyCooldown( + providerId: string, + cooldownMs: number, + ): Promise<ConcurrencyCooldownResult>; + /** * A critical error that blocks normal operation (e.g. the cross-device tab * restore fetch failed). When non-null, a full-screen modal is shown with the * error details. Cleared by `clearFatalError` (the modal's dismiss button). @@ -1989,6 +2009,64 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { } }, + async getConcurrencyCooldown(providerId: string): Promise<ConcurrencyCooldownResult> { + try { + const res = await fetchImpl( + `${httpBase}/concurrency/cooldown/${encodeURIComponent(providerId)}`, + ); + if (!res.ok) { + const errBody = (await res.json().catch(() => null)) as { error?: string } | null; + return { + ok: false, + error: errBody?.error ?? `Get concurrency cooldown failed (HTTP ${res.status})`, + }; + } + const data = normalizeConcurrencyCooldown(await res.json()); + if (data === null) { + return { ok: false, error: "Malformed concurrency cooldown response" }; + } + return { ok: true, providerId: data.providerId, cooldownMs: data.cooldownMs }; + } catch (err) { + return { + ok: false, + error: err instanceof Error ? err.message : "Get concurrency cooldown request failed", + }; + } + }, + + async setConcurrencyCooldown( + providerId: string, + cooldownMs: number, + ): Promise<ConcurrencyCooldownResult> { + try { + const res = await fetchImpl( + `${httpBase}/concurrency/cooldown/${encodeURIComponent(providerId)}`, + { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ cooldownMs }), + }, + ); + if (!res.ok) { + const errBody = (await res.json().catch(() => null)) as { error?: string } | null; + return { + ok: false, + error: errBody?.error ?? `Set concurrency cooldown failed (HTTP ${res.status})`, + }; + } + const data = normalizeConcurrencyCooldown(await res.json()); + if (data === null) { + return { ok: false, error: "Malformed concurrency cooldown response" }; + } + return { ok: true, providerId: data.providerId, cooldownMs: data.cooldownMs }; + } catch (err) { + return { + ok: false, + error: err instanceof Error ? err.message : "Set concurrency cooldown request failed", + }; + } + }, + async loadSystemPrompt(): Promise<SystemPromptLoadResult> { try { const res = await fetchImpl(`${httpBase}/system-prompt`); diff --git a/src/app/store.test.ts b/src/app/store.test.ts index 1048d87..a945ea7 100644 --- a/src/app/store.test.ts +++ b/src/app/store.test.ts @@ -1462,6 +1462,20 @@ describe("createAppStore", () => { if (url.includes("/concurrency/limits/") && method === "DELETE") { return new Response(JSON.stringify({ ok: true, providerId: "umans" }), { status: 200 }); } + if (url.includes("/concurrency/cooldown/") && method === "GET") { + return new Response(JSON.stringify({ providerId: "umans", cooldownMs: 350 }), { + status: 200, + }); + } + if (url.includes("/concurrency/cooldown/") && method === "PUT") { + const seg = url.slice( + url.lastIndexOf("/concurrency/cooldown/") + "/concurrency/cooldown/".length, + ); + const body = init?.body ? JSON.parse(init.body as string) : {}; + return new Response(JSON.stringify({ providerId: seg, cooldownMs: body.cooldownMs ?? 0 }), { + status: 200, + }); + } return base(input, init); }; } @@ -1645,11 +1659,15 @@ describe("createAppStore", () => { inFlight: 2, queued: 1, paused: false, + cooldownMs: 350, + autoReduced: false, }); expect(result.providers[1]).toMatchObject({ providerId: "openai-compat", paused: true, pausedUntil: 1_719_408_000_000, + cooldownMs: 350, + autoReduced: false, }); store.dispose(); }); @@ -1667,6 +1685,100 @@ describe("createAppStore", () => { store.dispose(); }); + it("getConcurrencyCooldown loads + coerces the cooldown", async () => { + const store = createAppStore({ + socketFactory: () => fakeSocket(), + fetchImpl: concurrencyFetchImpl(), + localStorage: createFakeStorage(), + }); + const result = await store.getConcurrencyCooldown("umans"); + expect(result.ok).toBe(true); + if (!result.ok) throw new Error("unreachable"); + expect(result.providerId).toBe("umans"); + expect(result.cooldownMs).toBe(350); + store.dispose(); + }); + + it("getConcurrencyCooldown surfaces a 404 (no concurrency config) as ok:false", async () => { + const store = createAppStore({ + socketFactory: () => fakeSocket(), + fetchImpl: async (input) => { + const url = + typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + if (url.includes("/concurrency/cooldown/")) { + return new Response( + JSON.stringify({ error: "No concurrency configuration for this provider" }), + { + status: 404, + }, + ); + } + return fakeFetchImpl()(input); + }, + localStorage: createFakeStorage(), + }); + const result = await store.getConcurrencyCooldown("ghost"); + expect(result.ok).toBe(false); + if (result.ok) throw new Error("unreachable"); + expect(result.error).toContain("No concurrency configuration"); + store.dispose(); + }); + + it("setConcurrencyCooldown PUTs { cooldownMs } + returns the echoed value", async () => { + const calls: { url: string; method: string; body: unknown }[] = []; + const store = createAppStore({ + socketFactory: () => fakeSocket(), + fetchImpl: async (input, init) => { + const url = + typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + const method = init?.method ?? "GET"; + calls.push({ url, method, body: init?.body ? JSON.parse(init.body as string) : null }); + if (url.includes("/concurrency/cooldown/") && method === "PUT") { + const body = init?.body ? JSON.parse(init.body as string) : {}; + return new Response( + JSON.stringify({ providerId: "umans", cooldownMs: body.cooldownMs }), + { status: 200 }, + ); + } + return fakeFetchImpl()(input, init); + }, + localStorage: createFakeStorage(), + }); + const result = await store.setConcurrencyCooldown("umans", 500); + expect(result.ok).toBe(true); + if (!result.ok) throw new Error("unreachable"); + expect(result.cooldownMs).toBe(500); + const cooldownCall = calls.find( + (c) => c.url.includes("/concurrency/cooldown/") && c.method === "PUT", + ); + expect(cooldownCall?.url).toContain("/concurrency/cooldown/umans"); + expect(cooldownCall?.body).toEqual({ cooldownMs: 500 }); + store.dispose(); + }); + + it("setConcurrencyCooldown surfaces a 400 (invalid body) as ok:false", async () => { + const store = createAppStore({ + socketFactory: () => fakeSocket(), + fetchImpl: async (input, init) => { + const url = + typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + if (url.includes("/concurrency/cooldown/") && init?.method === "PUT") { + return new Response( + JSON.stringify({ error: "Body must be { cooldownMs: <non-negative integer> }" }), + { status: 400 }, + ); + } + return fakeFetchImpl()(input, init); + }, + localStorage: createFakeStorage(), + }); + const result = await store.setConcurrencyCooldown("umans", -1); + expect(result.ok).toBe(false); + if (result.ok) throw new Error("unreachable"); + expect(result.error).toContain("non-negative integer"); + store.dispose(); + }); + // ── Conversation status: `queued` (CR-13 — waiting for a concurrency slot) ──── it("conversation.statusChanged 'queued' sets the status (tab spinner) without opening a duplicate tab", () => { |
