Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6175: Make unavailable price previews recoverable (#6960)

Multica Eve319f21e3cb2b
diff --git a/packages/core/billing/workspace-subscription-queries.test.ts b/packages/core/billing/workspace-subscription-queries.test.ts
new file mode 100644
index 0000000000..274ec42a3d
--- /dev/null
+++ b/packages/core/billing/workspace-subscription-queries.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+import {
+ workspaceSubscriptionPricesOptions,
+ workspaceSubscriptionPricesStaleTime,
+} from "./workspace-subscription-queries";
+
+const validPrices = {
+ month: {
+ currency: "usd",
+ unitAmount: 1_000,
+ interval: "month",
+ intervalCount: 1,
+ },
+ year: {
+ currency: "usd",
+ unitAmount: 9_600,
+ interval: "year",
+ intervalCount: 1,
+ },
+} as const;
+
+describe("workspace subscription price query", () => {
+ it("keeps missing price responses stale so the UI can recover", () => {
+ expect(workspaceSubscriptionPricesStaleTime(undefined)).toBe(0);
+ expect(workspaceSubscriptionPricesStaleTime(null)).toBe(0);
+ expect(workspaceSubscriptionPricesStaleTime(validPrices)).toBe(
+ 10 * 60 * 1_000,
+ );
+ });
+
+ it("wires the data-aware stale time into focus revalidation", () => {
+ const options = workspaceSubscriptionPricesOptions("workspace-1");
+ const staleTime = options.staleTime as (query: {
+ state: { data: typeof validPrices | null };
+ }) => number;
+
+ expect(typeof staleTime).toBe("function");
+ expect(staleTime({ state: { data: null } })).toBe(0);
+ expect(staleTime({ state: { data: validPrices } })).toBe(10 * 60 * 1_000);
+ expect(options.refetchOnWindowFocus).toBe(true);
+ expect(options.retry).toBe(false);
+ });
+});
diff --git a/packages/core/billing/workspace-subscription-queries.ts b/packages/core/billing/workspace-subscription-queries.ts
index d64920da61..4a8627af25 100644
--- a/packages/core/billing/workspace-subscription-queries.ts
+++ b/packages/core/billing/workspace-subscription-queries.ts
@@ -1,6 +1,9 @@
import { queryOptions } from "@tanstack/react-query";
+import type { WorkspaceSubscriptionPrices } from "../types";
import { api } from "../api";
+const WORKSPACE_SUBSCRIPTION_PRICES_STALE_TIME_MS = 10 * 60 * 1000;
+
/**
* Workspace subscription reads.
*
@@ -46,23 +49,37 @@ export function workspaceSubscriptionSummaryOptions(wsId: string) {
});
}
+/**
+ * A validated price snapshot is stable deployment configuration, but a null
+ * parse fallback is not a snapshot. Keeping null fresh made one transient
+ * contract/deployment failure suppress prices for ten minutes with no way to
+ * recover from the Billing tab.
+ */
+export function workspaceSubscriptionPricesStaleTime(
+ prices: WorkspaceSubscriptionPrices | null | undefined,
+): number {
+ return prices ? WORKSPACE_SUBSCRIPTION_PRICES_STALE_TIME_MS : 0;
+}
+
/**
* Prices are deployment configuration, not workspace state: they change only
- * when an operator repoints a Stripe Price. Cache them for much longer than the
- * summary and do not refetch on focus — cloud already caches them and each miss
- * costs a Stripe round trip.
+ * when an operator repoints a Stripe Price. Cache validated values for much
+ * longer than the summary. Missing/malformed values stay stale so a remount or
+ * window focus can recover after a backend rollout or transient dependency
+ * failure; cloud already caches Stripe reads, so revalidation is inexpensive.
*
* Retry is disabled because the failure mode here is a misconfigured or
- * unvalidatable Price, which retrying cannot fix; the caller shows "price
- * confirmed at Checkout" instead.
+ * unvalidatable Price, which immediate retries cannot fix. The caller keeps
+ * Checkout available and exposes an explicit price-only retry instead.
*/
export function workspaceSubscriptionPricesOptions(wsId: string) {
return queryOptions({
queryKey: workspaceSubscriptionKeys.prices(wsId),
queryFn: () => api.getWorkspaceSubscriptionPrices(),
enabled: wsId.length > 0,
- staleTime: 10 * 60 * 1000,
- refetchOnWindowFocus: false,
+ staleTime: (query) =>
+ workspaceSubscriptionPricesStaleTime(query.state.data),
+ refetchOnWindowFocus: true,
retry: false,
});
}
diff --git a/packages/views/settings/components/billing-tab.test.tsx b/packages/views/settings/components/billing-tab.test.tsx
index bb535461a9..a2c949ba45 100644
--- a/packages/views/settings/components/billing-tab.test.tsx
+++ b/packages/views/settings/components/billing-tab.test.tsx
@@ -12,6 +12,7 @@ const mocks = vi.hoisted(() => ({
portal: vi.fn(),
reconcile: vi.fn(),
refetch: vi.fn(),
+ refetchPrices: vi.fn(),
openExternal: vi.fn(),
role: "owner" as "owner" | "admin" | "member",
workspaceId: "workspace-1",
@@ -30,6 +31,7 @@ const mocks = vi.hoisted(() => ({
};
} | null,
pricesLoading: false,
+ pricesFetching: false,
pricesError: false,
entitlements: {
workspaceId: "workspace-1",
@@ -121,6 +123,7 @@ describe("BillingTab", () => {
},
};
mocks.pricesLoading = false;
+ mocks.pricesFetching = false;
mocks.pricesError = false;
Object.assign(mocks.entitlements, {
plan: "free",
@@ -138,7 +141,9 @@ describe("BillingTab", () => {
return {
data: mocks.prices,
isLoading: mocks.pricesLoading,
+ isFetching: mocks.pricesFetching,
isError: mocks.pricesError,
+ refetch: mocks.refetchPrices,
};
}
return {
@@ -202,6 +207,9 @@ describe("BillingTab", () => {
expect(
screen.queryByText("Estimated monthly total: $30.00"),
).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Retry" }),
+ ).not.toBeInTheDocument();
});
it("formats Stripe minor units for decimal, zero-decimal, and special currencies", () => {
@@ -232,6 +240,21 @@ describe("BillingTab", () => {
).toBeEnabled();
});
+ it("announces a price-only retry without blocking Checkout", () => {
+ mocks.prices = null;
+ mocks.pricesFetching = true;
+
+ renderWithI18n(<BillingTab />);
+
+ expect(screen.getByRole("button", { name: "Retry" })).toBeDisabled();
+ expect(
+ screen.getByRole("status", { name: "Loading subscription prices" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Upgrade to Pro" }),
+ ).toBeEnabled();
+ });
+
it.each([
["query error", true],
["malformed response", false],
@@ -251,6 +274,9 @@ describe("BillingTab", () => {
).toBeInTheDocument();
expect(screen.queryByText(/\$0/)).not.toBeInTheDocument();
+ await user.click(screen.getByRole("button", { name: "Retry" }));
+ expect(mocks.refetchPrices).toHaveBeenCalledOnce();
+
await user.click(screen.getByRole("button", { name: "Upgrade to Pro" }));
await user.click(
screen.getByRole("button", { name: "Continue to Stripe" }),
@@ -292,6 +318,9 @@ describe("BillingTab", () => {
expect(
screen.getByText(/Stripe Checkout shows the authoritative per-seat price/),
).toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Retry" }),
+ ).not.toBeInTheDocument();
});
it("creates Checkout with a client idempotency key and opens Stripe externally", async () => {
diff --git a/packages/views/settings/components/billing-tab.tsx b/packages/views/settings/components/billing-tab.tsx
index 328f42808b..00e16f59ec 100644
--- a/packages/views/settings/components/billing-tab.tsx
+++ b/packages/views/settings/components/billing-tab.tsx
@@ -517,6 +517,7 @@ function BillingTabContent() {
selectedPrice?.intervalCount === 1 && formattedUnitPrice !== null;
const hasDisplayableEstimatedTotal =
hasDisplayableUnitPrice && formattedEstimatedTotal !== null;
+ const canRetryPrice = !pricesQuery.isLoading && selectedPrice === null;
return (
<SettingsTab
@@ -701,6 +702,36 @@ function BillingTabContent() {
<p className="max-w-[65ch] text-caption leading-5 text-muted-foreground">
{t(($) => $.workspace.upgrade.price_at_checkout)}
</p>
+ {canRetryPrice ? (
+ <>
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ aria-busy={pricesQuery.isFetching}
+ disabled={pricesQuery.isFetching}
+ onClick={() => void pricesQuery.refetch()}
+ >
+ {pricesQuery.isFetching ? (
+ <Loader2 className="animate-spin" />
+ ) : (
+ <RefreshCw />
+ )}
+ {t(($) => $.workspace.actions.retry)}
+ </Button>
+ {pricesQuery.isFetching ? (
+ <span
+ className="sr-only"
+ role="status"
+ aria-label={t(
+ ($) => $.workspace.upgrade.price_loading,
+ )}
+ >
+ {t(($) => $.workspace.upgrade.price_loading)}
+ </span>
+ ) : null}
+ </>
+ ) : null}
</div>
{canManage ? (
<Button
diff --git a/server/internal/integrations/wecom/inbound_media_bind_db_test.go b/server/internal/integrations/wecom/inbound_media_bind_db_test.go
index 626245c2fb..0cfda32e5b 100644
--- a/server/internal/integrations/wecom/inbound_media_bind_db_test.go
+++ b/server/internal/integrations/wecom/inbound_media_bind_db_test.go
@@ -272,12 +272,12 @@ func TestInboundImageBecomesAnAttachmentOnTheChatMessage(t *testing.T) {
var attachments int
for {
if err := pool.QueryRow(ctx, `
- SELECT cm.id, cm.chat_session_id, cm.content, cm.channel_media_pending_until
+ SELECT cm.id, cm.chat_session_id, cm.content
FROM chat_message cm
JOIN chat_session cs ON cs.id = cm.chat_session_id
WHERE cs.workspace_id = $1 AND cm.role = 'user'
ORDER BY cm.created_at DESC LIMIT 1`,
- fixture.workspaceID).Scan(&chatMessageID, &sessionID, &body, &pendingUntil); err != nil {
+ fixture.workspaceID).Scan(&chatMessageID, &sessionID, &body); err != nil {
if time.Now().After(deadline) {
t.Fatalf("no durable chat_message was ever written: %v", err)
}
@@ -355,6 +355,14 @@ func TestInboundImageBecomesAnAttachmentOnTheChatMessage(t *testing.T) {
}
// 4. The pending marker is cleared, which is what releases the agent run.
+ // Read it after observing the attachment and promotion. The polling query
+ // can race with the bind transaction and otherwise leave a stale pre-bind
+ // value in pendingUntil even though the database row has been cleared.
+ if err := pool.QueryRow(ctx,
+ `SELECT channel_media_pending_until FROM chat_message WHERE id = $1`, chatMessageID).
+ Scan(&pendingUntil); err != nil {
+ t.Fatalf("load media pending marker: %v", err)
+ }
if pendingUntil.Valid {
t.Errorf("channel_media_pending_until = %v, want NULL after binding", pendingUntil.Time)
}