Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

The key-authorization dialog reports its own outcome (#532)

Yaowei Zheng9c38d2933df1
diff --git a/changelog/unreleased/2026-08-28-oauth-outcome-dialog.md b/changelog/unreleased/2026-08-28-oauth-outcome-dialog.md
new file mode 100644
index 00000000..6ad23dfb
--- /dev/null
+++ b/changelog/unreleased/2026-08-28-oauth-outcome-dialog.md
@@ -0,0 +1,34 @@
+# Authorizing a key says so where you are looking
+
+- **Date:** 2026-08-28
+- **Type:** fix
+- **Scope:** `web`, `server`
+- **PR:** [#532](https://github.com/Prism-Shadow/penguin-harness/pull/532)
+
+[中文版](2026-08-28-oauth-outcome-dialog.zh.md)
+
+Minting a provider key with **Authorize key** sends the user to the provider's own page in
+another tab. The key landing was announced with a toast fired the moment the poll saw it — in a
+window nobody was looking at, and faded by the time they switched back. The one step a user
+leaves the app for was the one step with no visible result.
+
+The dialog now stays open and reports the outcome itself: it names the provider and how many
+models the key was written to, and is dismissed deliberately.
+
+## Details
+
+- Gave the dialog a `done` phase, reached by both paths a key can land through — the redirect
+ flow's poll seeing `status: "done"`, and a manually pasted code answering `ok`. The flow's
+ controls and the manual/redirect switch leave with it, so what remains is the sentence and one
+ button, which dismisses.
+- Added `applied` to the flow-status response, so both paths report the count the server wrote
+ rather than one of them counting the table in hand. The redemption route already answered with
+ it; the status route computed it and dropped it.
+- Moved the model table's reload from the moment the key lands to the dismissal, and stopped
+ rendering the dialog behind the table's loaded-rows guard. The reload runs only when a key
+ actually landed, because it also drops the Project's speed-test measurements.
+- Removed the toast string the flow no longer uses.
+- Pinned the rule in `test/model-oauth-outcome.test.ts`: both success paths settle into `done`,
+ neither the dialog nor its caller toasts the outcome, the sentence carries the provider and the
+ count in both dictionaries, the done footer offers no cancel, and the dialog is neither gated on
+ the loaded rows nor reloaded from the success handler.
diff --git a/changelog/unreleased/2026-08-28-oauth-outcome-dialog.zh.md b/changelog/unreleased/2026-08-28-oauth-outcome-dialog.zh.md
new file mode 100644
index 00000000..cfd26c70
--- /dev/null
+++ b/changelog/unreleased/2026-08-28-oauth-outcome-dialog.zh.md
@@ -0,0 +1,20 @@
+# 授权配置密钥后,结果说在你正看着的地方
+
+- **Date:** 2026-08-28
+- **Type:** fix
+- **Scope:** `web`, `server`
+- **PR:** [#532](https://github.com/Prism-Shadow/penguin-harness/pull/532)
+
+[English](2026-08-28-oauth-outcome-dialog.md)
+
+用「授权密钥」签发厂商密钥时,用户会被带到另一个标签页去完成授权。密钥到位的提示此前是一条 toast,在轮询看到结果的那一刻弹出——弹在一个没人在看的窗口里,等用户切回来时它已经消失了。用户唯一需要离开应用去完成的那一步,恰好是唯一没有可见结果的一步。
+
+现在对话框会留在原地自己报告结果:说明是哪个厂商、密钥被配置到了多少个模型上,并由用户主动关闭。
+
+## 细节
+
+- 对话框新增 `done` 阶段,密钥到位的两条路径都会进入它——跳转流程的轮询看到 `status: "done"`,以及手工粘贴授权码得到 `ok`。流程的操作控件与「跳转/手工」切换链接随之隐去,只留下那句话和一个关闭按钮。
+- 流程状态接口新增 `applied`,使两条路径报告的都是服务端实际写入的数量,而不是其中一条去数手里的表格。兑换接口本就返回该字段,状态接口算了出来却没有带上。
+- 模型表的重新加载从「密钥到位时」改到「对话框被关闭时」,对话框本身也不再置于表格「已加载行」的渲染条件之下;且仅在确有密钥落地时才加载——因为该操作同时会清掉本 Project 的测速结果。
+- 删除了流程不再使用的那条 toast 文案。
+- 在 `test/model-oauth-outcome.test.ts` 中把规则钉住:两条成功路径都进入 `done`、对话框及其调用方都不再用 toast 报告结果、那句话在两份文案里都带上厂商与数量、完成态的底部没有取消,以及对话框既不挂在「已加载行」条件上、也不在成功回调里触发重新加载。
diff --git a/packages/server/src/api/types.ts b/packages/server/src/api/types.ts
index b361c6eb..7a7acfd3 100644
--- a/packages/server/src/api/types.ts
+++ b/packages/server/src/api/types.ts
@@ -702,6 +702,13 @@ export interface ModelOAuthStatusResponse {
status: "pending" | "done" | "error";
/** The provider group the flow mints a key for. */
provider: string;
+ /**
+ * How many models the minted key was written to — set on the `done` answer, the way
+ * {@link ModelOAuthCodeResponse} sets it on a redemption. The dialog reports this number, so
+ * it has to be the server's own: the caller's model table can outlive a rejected save and
+ * would otherwise name a count the server never wrote.
+ */
+ applied?: number;
error?: ModelOAuthErrorCode;
}
diff --git a/packages/server/src/http/routes/model-oauth.ts b/packages/server/src/http/routes/model-oauth.ts
index c1633489..0bb40034 100644
--- a/packages/server/src/http/routes/model-oauth.ts
+++ b/packages/server/src/http/routes/model-oauth.ts
@@ -276,6 +276,7 @@ export function modelOAuthRoutes(deps: AppDeps): Hono<AppEnv> {
return c.json({
status: state.status,
provider: state.provider,
+ ...(state.applied !== undefined ? { applied: state.applied } : {}),
...(state.error !== undefined ? { error: state.error } : {}),
} satisfies ModelOAuthStatusResponse);
});
diff --git a/packages/server/test/model-oauth.test.ts b/packages/server/test/model-oauth.test.ts
index f33b00cb..988130a8 100644
--- a/packages/server/test/model-oauth.test.ts
+++ b/packages/server/test/model-oauth.test.ts
@@ -458,7 +458,7 @@ describe("model-oauth routes", () => {
const status = (await (
await owner.get(`${base()}/${started.flowId}`)
).json()) as ModelOAuthStatusResponse;
- expect(status).toEqual({ status: "done", provider: "tokendance" });
+ expect(status).toEqual({ status: "done", provider: "tokendance", applied: 8 });
// The exchange spoke the documented protocol, with a verifier this side never published.
expect(exchanges).toHaveLength(1);
@@ -677,7 +677,7 @@ describe("model-oauth callback without a session", () => {
expect(await groupHasKey()).toBe(false);
// The owner's own poll is what spends the code and lands the key.
- expect(await poll(flowId)).toEqual({ status: "done", provider: "tokendance" });
+ expect(await poll(flowId)).toEqual({ status: "done", provider: "tokendance", applied: 8 });
expect(exchanges).toBe(1);
expect(await groupHasKey()).toBe(true);
});
@@ -689,7 +689,7 @@ describe("model-oauth callback without a session", () => {
const browser = await owner.get(callback(flowId, "auth-code-2"));
expect(browser.status).toBe(200);
expect(await browser.text()).toContain("Authorization received");
- expect(await poll(flowId)).toEqual({ status: "done", provider: "tokendance" });
+ expect(await poll(flowId)).toEqual({ status: "done", provider: "tokendance", applied: 8 });
expect(exchanges).toBe(1);
expect(await groupHasKey()).toBe(true);
});
diff --git a/packages/web/src/features/models/models-page.tsx b/packages/web/src/features/models/models-page.tsx
index fcb1b83a..f5ef0c95 100644
--- a/packages/web/src/features/models/models-page.tsx
+++ b/packages/web/src/features/models/models-page.tsx
@@ -602,6 +602,8 @@ export function ModelsPage() {
const [groupKeyFor, setGroupKeyFor] = useState<string | null>(null);
/** Vendor group (provider id) whose authorization dialog is open (groups that publish a key-minting flow only). */
const [oauthFor, setOauthFor] = useState<string | null>(null);
+ /** Whether the open authorization actually wrote a key — see the dialog's `onClose`. */
+ const keyLanded = useRef(false);
/** User-defined group (provider id) whose delete confirmation is open (built-in groups never offer this). */
const [deleteGroupFor, setDeleteGroupFor] = useState<string | null>(null);
/** "Add group" popup (user-defined group): create-only hands off to that group's add-model dialog, import mode fills the group from its endpoint (see AddGroupDialog). */
@@ -1239,18 +1241,33 @@ export function ModelsPage() {
/>
)}
- {rows && projectId && oauthFor !== null && (
+ {/* NOT gated on `rows`, unlike its neighbours. This dialog outlives a table reload: it
+ stays open after the key lands to report the outcome, and `load()` blanks `rows` on
+ its first line, so gating it there would unmount it mid-flow — and its mount effect
+ opens a NEW authorization, which is how a completed authorization ended up offering
+ itself again. `count` is only read while the flow is still running, so a reload's
+ momentary absence of rows reads as zero and is never seen. */}
+ {projectId && oauthFor !== null && (
<ModelOAuthDialog
projectId={projectId}
provider={MODEL_PROVIDERS.find((p) => p.id === oauthFor) ?? userProviderInfo(oauthFor)}
- count={rows.filter((r) => r.provider === oauthFor).length}
- onClose={() => setOauthFor(null)}
- onApplied={(applied) => {
+ count={rows?.filter((r) => r.provider === oauthFor).length ?? 0}
+ onClose={() => {
setOauthFor(null);
- toastSuccess(S.models.oauthApplied(applied));
- // The key was written server-side, so the table in hand is stale in exactly one
- // place: reload rather than patch, and the masked key comes back with it.
- void load();
+ // The reload waits for the dismissal rather than racing the open dialog: the key
+ // was written server-side, so the table in hand is stale in exactly one place, and
+ // reloading brings the masked key back. Gated on a key having landed, because
+ // `load` also drops this Project's speed results — measurements that cost real API
+ // quota and live only in page memory — and a cancelled flow changed nothing.
+ if (keyLanded.current) void load();
+ keyLanded.current = false;
+ }}
+ onApplied={() => {
+ // The dialog stays open and reports the outcome itself (its `done` phase), because
+ // the authorization ran in another tab and a toast would be announced to a window
+ // nobody is looking at. All this seam does is record that the dismissal has a
+ // reload to do.
+ keyLanded.current = true;
}}
/>
)}
@@ -3124,7 +3141,14 @@ function GroupKeyDialog({
// ---------------------------------------------------------------------------
/** Where the dialog is: opening a flow, holding one, waiting on an outcome, or reporting a failure. */
-type OAuthPhase = "starting" | "ready" | "waiting" | "failed";
+/**
+ * `done` exists because the authorization happens in ANOTHER TAB. A toast fired at the moment
+ * the poll sees the key would be announced to a window the user is not looking at, and by the
+ * time they switch back it has faded — the outcome of the one step they left the app for is the
+ * one thing they must not have to guess at. So the dialog stays put and says it instead, and is
+ * dismissed deliberately.
+ */
+type OAuthPhase = "starting" | "ready" | "waiting" | "failed" | "done";
/** How often the redirect flow's outcome is asked for while the user is in the other tab. */
const OAUTH_POLL_MS = 2000;
@@ -3156,6 +3180,8 @@ function ModelOAuthDialog({
const [flow, setFlow] = useState<{ flowId: string; authorizeUrl: string } | null>(null);
const [error, setError] = useState<string | null>(null);
const [code, setCode] = useState("");
+ /** How many models the key was written to — the sentence the `done` phase reports. */
+ const [applied, setApplied] = useState(0);
/** Bumped to reopen a flow after a failure; switching modes reopens one too (the URL differs). */
const [attempt, setAttempt] = useState(0);
// Latest-callback ref: the parent passes an inline arrow, and depending on its identity
@@ -3202,7 +3228,12 @@ function ModelOAuthDialog({
const res = await api.getModelOAuthStatus(projectId, flow.flowId);
if (stopped) return;
if (res.status === "done") {
- onAppliedRef.current(count);
+ // The server's own count, not the table in hand: `rows` is kept through a rejected
+ // save so the user can fix and retry, so it can name models the server never wrote.
+ const n = res.applied ?? count;
+ setApplied(n);
+ setPhase("done");
+ onAppliedRef.current(n);
return;
}
if (res.status === "error") {
@@ -3235,7 +3266,10 @@ function ModelOAuthDialog({
try {
const res = await api.submitModelOAuthCode(projectId, flow.flowId, code.trim());
if (res.ok) {
- onAppliedRef.current(res.applied ?? count);
+ const n = res.applied ?? count;
+ setApplied(n);
+ setPhase("done");
+ onAppliedRef.current(n);
return;
}
setError(res.error ? S.models.oauthErrors[res.error] : S.models.oauthTimedOut);
@@ -3271,17 +3305,29 @@ function ModelOAuthDialog({
title={S.models.oauthTitle(provider.label)}
onClose={onClose}
footer={
- <>
- <Button onClick={onClose}>{S.common.cancel}</Button>
- {primary}
- </>
+ // Done is an outcome, not a choice: a "cancel" beside it would offer to undo a key that
+ // is already written.
+ phase === "done" ? (
+ <Button onClick={onClose}>{S.common.close}</Button>
+ ) : (
+ <>
+ <Button onClick={onClose}>{S.common.cancel}</Button>
+ {primary}
+ </>
+ )
}
>
<div className="space-y-3">
- <p className="text-sm text-gray-700 dark:text-gray-300">
- {S.models.oauthIntro(provider.label, count)}
- </p>
- {manual && (
+ {phase === "done" ? (
+ <p className="text-sm text-gray-700 dark:text-gray-300">
+ {S.models.oauthAppliedBody(provider.label, applied)}
+ </p>
+ ) : (
+ <p className="text-sm text-gray-700 dark:text-gray-300">
+ {S.models.oauthIntro(provider.label, count)}
+ </p>
+ )}
+ {phase !== "done" && manual && (
<>
<Button variant="ghost" disabled={flow === null} onClick={openAuthorizePage}>
<GlyphIcon d={SIGN_IN_ICON} size={13} />
@@ -3305,13 +3351,15 @@ function ModelOAuthDialog({
</p>
)}
{error !== null && <p className="text-xs text-red-600 dark:text-red-400">{error}</p>}
- <button
- type="button"
- onClick={() => setManual((v) => !v)}
- className="text-xs text-brand-600 underline-offset-2 hover:underline dark:text-brand-300"
- >
- {manual ? S.models.oauthCallbackSwitch : S.models.oauthManualSwitch}
- </button>
+ {phase !== "done" && (
+ <button
+ type="button"
+ onClick={() => setManual((v) => !v)}
+ className="text-xs text-brand-600 underline-offset-2 hover:underline dark:text-brand-300"
+ >
+ {manual ? S.models.oauthCallbackSwitch : S.models.oauthManualSwitch}
+ </button>
+ )}
</div>
</Modal>
);
diff --git a/packages/web/src/lib/strings-en.ts b/packages/web/src/lib/strings-en.ts
index d959b088..d10de696 100644
--- a/packages/web/src/lib/strings-en.ts
+++ b/packages/web/src/lib/strings-en.ts
@@ -860,7 +860,13 @@ export const en: Strings = {
`A new API key will be created on your ${label} account and written to all ${n} models in this group, replacing the key they use now.`,
oauthAuthorize: "Open authorization page",
oauthWaiting: "Waiting for the authorization to finish in the other tab…",
- oauthApplied: (n: number): string => `New API key set for ${n} models`,
+ /**
+ * The dialog's own report once the key has landed. It names the provider as well as the
+ * count, because the user is reading it after a trip to another tab and may not remember
+ * which authorization they just finished.
+ */
+ oauthAppliedBody: (provider: string, n: number): string =>
+ `Authorized. ${provider}'s API key is set on ${n} model${n === 1 ? "" : "s"} and ready to use.`,
oauthManualSwitch: "Page can't redirect back? Enter the code by hand",
oauthCallbackSwitch: "Go back to the automatic redirect",
oauthManualHint: "Open the authorization page, then paste the one-time code it shows you here.",
diff --git a/packages/web/src/lib/strings.ts b/packages/web/src/lib/strings.ts
index 738761b6..2f394f38 100644
--- a/packages/web/src/lib/strings.ts
+++ b/packages/web/src/lib/strings.ts
@@ -838,7 +838,13 @@ export const zh = {
`将在你的 ${label} 账户下新建一个 API key,并写入该分组下全部 ${n} 个模型,覆盖它们当前的 key。`,
oauthAuthorize: "打开授权页",
oauthWaiting: "等待在新标签页中完成授权…",
- oauthApplied: (n: number): string => `已为 ${n} 个模型配置新的 API key`,
+ /**
+ * The dialog's own report once the key has landed. It says the provider as well as the
+ * count, because the user is reading it after a trip to another tab and may not remember
+ * which authorization they just finished.
+ */
+ oauthAppliedBody: (provider: string, n: number): string =>
+ `已完成授权:${provider} 的 API key 已配置到 ${n} 个模型上,可以直接使用了。`,
oauthManualSwitch: "授权页跳不回来?改为手动填写授权码",
oauthCallbackSwitch: "改回自动跳转",
oauthManualHint: "先打开授权页,再把页面上显示的一次性授权码粘贴到这里。",
diff --git a/packages/web/test/model-oauth-outcome.test.ts b/packages/web/test/model-oauth-outcome.test.ts
new file mode 100644
index 00000000..f1da9376
--- /dev/null
+++ b/packages/web/test/model-oauth-outcome.test.ts
@@ -0,0 +1,109 @@
+/**
+ * The key-authorization dialog reports its own outcome, and never through a toast.
+ *
+ * The authorization happens in ANOTHER TAB. A toast fired when the poll sees the key is
+ * announced to a window nobody is looking at, and it has faded by the time the user switches
+ * back — leaving the one step they left the app for as the one step with no visible result.
+ * So the dialog stays open, enters its `done` phase, names the provider and the count, and is
+ * dismissed deliberately.
+ *
+ * Checked rather than remembered, because the regression is a one-line `toastSuccess` added
+ * back into a success path by someone who never saw the dialog in another tab. This reads the
+ * real source rather than rendering: the dialog is module-private, and exporting it purely so a
+ * test could mount it would widen the module's surface to check a rule about its own text.
+ */
+import { describe, expect, it } from "vitest";
+import { readFileSync } from "node:fs";
+import { fileURLToPath } from "node:url";
+import { zh } from "../src/lib/strings";
+import { en } from "../src/lib/strings-en";
+
+const SOURCE = readFileSync(
+ fileURLToPath(new URL("../src/features/models/models-page.tsx", import.meta.url)),
+ "utf8",
+);
+
+/**
+ * The body of `ModelOAuthDialog`, from its phase type to the end of that component.
+ *
+ * Bounded rather than run to end of file: the component happens to be last today, so an
+ * unbounded slice would quietly sweep anything appended after it into these assertions and
+ * fail for a reason that has nothing to do with this dialog.
+ */
+function dialogSource(): string {
+ const start = SOURCE.indexOf("type OAuthPhase");
+ expect(start).toBeGreaterThan(0);
+ const body = SOURCE.slice(start);
+ const end = body.indexOf("\nfunction ", body.indexOf("function ModelOAuthDialog"));
+ return end === -1 ? body : body.slice(0, end);
+}
+
+describe("the key-authorization dialog's outcome", () => {
+ it("has a done phase, and both success paths settle into it", () => {
+ const body = dialogSource();
+ expect(body).toContain('| "done"');
+ // The redirect path (the poll seeing status "done") and the manual path (a submitted code
+ // answering ok) are the only two ways a key lands; both must report, not just the one the
+ // author happened to be testing with.
+ expect(body.match(/setPhase\("done"\)/g) ?? []).toHaveLength(2);
+ });
+
+ it("never toasts the outcome from the dialog or its caller", () => {
+ // The caller used to close the dialog and toast. Either half coming back reintroduces the
+ // bug: a toast is missed, and closing removes the surface that would have said anything.
+ // Asserted on the caller's own handler rather than on a dictionary key going unused — a
+ // string the code no longer references is exactly the thing someone deletes as dead, and
+ // that deletion would make this pass forever.
+ const call = SOURCE.slice(SOURCE.indexOf("<ModelOAuthDialog"));
+ const onApplied = call.slice(call.indexOf("onApplied="), call.indexOf("/>"));
+ expect(onApplied).not.toContain("toastSuccess");
+ expect(dialogSource()).not.toContain("toastSuccess");
+ });
+
+ it("names the provider and the count, in both dictionaries", () => {
+ // The reader arrives from another tab and may not remember which authorization they just
+ // finished, so the count alone is not enough.
+ for (const dict of [zh, en]) {
+ const body = dict.models.oauthAppliedBody("TokenDance", 7);
+ expect(body).toContain("TokenDance");
+ expect(body).toContain("7");
+ }
+ // Keys and arity are compile-enforced by `en: Strings`; the plural branch is the one thing
+ // in these two strings that runs, and it is where the dictionaries differ in shape.
+ expect(en.models.oauthAppliedBody("TokenDance", 1)).toContain("1 model ");
+ expect(en.models.oauthAppliedBody("TokenDance", 2)).toContain("2 models ");
+ });
+
+ it("survives the table reload, which is what let a finished flow offer itself again", () => {
+ // The bug this pins: `load()` blanks `rows` on its first line, and the dialog used to be
+ // rendered behind a `rows &&` guard, so reloading on success unmounted it — and its mount
+ // effect opens a NEW authorization. The user finished authorizing and was handed the
+ // authorize page again, with no way to reach the outcome.
+ //
+ // Two facts keep that shut, and both are structural rather than timing-dependent: the
+ // dialog is not gated on `rows`, and the reload waits for the dismissal.
+ const call = SOURCE.slice(SOURCE.indexOf("<ModelOAuthDialog"));
+ const guard = SOURCE.slice(0, SOURCE.indexOf("<ModelOAuthDialog"));
+ // The line that renders it must not require rows.
+ const openingGuard = guard.slice(guard.lastIndexOf("{", guard.lastIndexOf("(")));
+ expect(openingGuard).not.toContain("rows &&");
+ // onApplied must not reload; onClose must.
+ const onApplied = call.slice(call.indexOf("onApplied="), call.indexOf("/>"));
+ expect(onApplied).not.toContain("load()");
+ const onClose = call.slice(call.indexOf("onClose="), call.indexOf("onApplied="));
+ expect(onClose).toContain("load()");
+ });
+
+ it("offers one dismissal once the key is written, not a cancel beside it", () => {
+ // Done is an outcome, not a choice: a "cancel" next to it would offer to undo a key that
+ // the server has already stored.
+ const body = dialogSource();
+ // Exactly the ternary's THEN branch: slicing further would run into the else branch, which
+ // is the flow's ordinary footer and legitimately carries the cancel.
+ const start = body.indexOf('phase === "done" ? (');
+ expect(start).toBeGreaterThan(0);
+ const doneBranch = body.slice(start, body.indexOf(") : (", start));
+ expect(doneBranch).toContain("S.common.close");
+ expect(doneBranch).not.toContain("S.common.cancel");
+ });
+});