From ff64af20b0351eb7c74012cd0a54d5d928bcd53d Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 14:32:37 -0800 Subject: [PATCH 1/2] fix(cloud-setup): the composer follows its setup session; billing reaches the control plane for GitHub - The composer read the setup session with a bare readCloudSetupSession() call in render. The React Compiler memoizes that on the session id, so the panel kept its first (empty) read: rows never moved past 'In browser', the finished session never auto-started the workspace, and the GitHub row stayed 'Needed' after the browser said 'You're all set'. Read it through useCloudSetupSession (useSyncExternalStore with a per-poll cached snapshot) instead. - Billing asked the control plane for the GitHub install URL with a plain fetch to its workers.dev origin. Same-account Workers can't do that (Cloudflare error 1042), so every setup GitHub step showed 'Orca Cloud setup is unavailable'. Bind the control plane as a service (SANDBOX_CONTROL) in the dev config and send the request through it when bound. Found by the NP-4 end-to-end run (np4-e2e) on m4air against staging. --- cloud/apps/billing/src/cloud-setup.ts | 7 +- cloud/apps/billing/src/env.ts | 3 + cloud/apps/billing/test/cloud-setup.test.ts | 34 +++++++++- cloud/apps/billing/wrangler.dev.jsonc | 3 + .../run-on/use-cloud-start-steps.ts | 13 ++-- .../cloud-setup-session-seam.test.tsx | 68 +++++++++++++++++++ .../orca-cloud/cloud-setup-session-seam.ts | 42 +++++++++--- 7 files changed, 149 insertions(+), 21 deletions(-) create mode 100644 src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.test.tsx diff --git a/cloud/apps/billing/src/cloud-setup.ts b/cloud/apps/billing/src/cloud-setup.ts index aaf31c73cae..378c4a93846 100644 --- a/cloud/apps/billing/src/cloud-setup.ts +++ b/cloud/apps/billing/src/cloud-setup.ts @@ -634,7 +634,12 @@ export async function setupGithubInstall( url.searchParams.set('githubLogin', body.githubLogin) } const token = (env.CONTROL_PLANE_TOKEN ?? '').split(',')[0]?.trim() ?? '' - const response = await fetchImpl(url, { + // Over the service binding when there is one: a plain fetch between same-account workers.dev + // Workers fails with error 1042, which surfaced as "setup is unavailable" on the GitHub step. + const send: typeof fetch = env.SANDBOX_CONTROL + ? (input, init) => env.SANDBOX_CONTROL!.fetch(input, init) + : fetchImpl + const response = await send(url, { headers: { authorization: `Bearer ${token}`, accept: 'application/json' }, signal: AbortSignal.timeout(8000) }) diff --git a/cloud/apps/billing/src/env.ts b/cloud/apps/billing/src/env.ts index 60af142b11f..dcc33c8dcdd 100644 --- a/cloud/apps/billing/src/env.ts +++ b/cloud/apps/billing/src/env.ts @@ -67,4 +67,7 @@ export type Env = { CLOUD_SETUP_PAGES_URL?: string // Control plane origin billing asks for a repository's GitHub install URL (CONTROL_PLANE_TOKEN). SANDBOX_CONTROL_URL?: string + // Service binding to that control plane. Same-account Workers can't fetch each other over + // workers.dev (Cloudflare error 1042), so when bound the request goes through it instead. + SANDBOX_CONTROL?: Fetcher } diff --git a/cloud/apps/billing/test/cloud-setup.test.ts b/cloud/apps/billing/test/cloud-setup.test.ts index 6656ab863cf..40bad4827b2 100644 --- a/cloud/apps/billing/test/cloud-setup.test.ts +++ b/cloud/apps/billing/test/cloud-setup.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest' import type { TrialPort, SetupTrial } from '../src/cloud-setup-trial' import { normalizeRepo } from '../src/cloud-setup' +import type { Env } from '../src/env' import { checkoutCompleted } from './stripe-fake' import { CONTROL_TOKEN, createHarness } from './harness' @@ -27,8 +28,13 @@ function fakeTrials(options: { refuse?: boolean } = {}) { return { port, trials, grants } } -async function setup(options: { refuseTrial?: boolean } = {}) { - const h = await createHarness({ MEMBERSHIP_SYNC_TOKEN: SYNC, CLOUD_SETUP_PAGES_URL: 'https://auth.test', SANDBOX_CONTROL_URL: 'https://cp.test' }) +async function setup(options: { refuseTrial?: boolean; env?: Partial } = {}) { + const h = await createHarness({ + MEMBERSHIP_SYNC_TOKEN: SYNC, + CLOUD_SETUP_PAGES_URL: 'https://auth.test', + SANDBOX_CONTROL_URL: 'https://cp.test', + ...options.env + }) const trials = fakeTrials({ refuse: options.refuseTrial }) h.setTrial(trials.port) const sync = { authorization: `Bearer ${SYNC}` } @@ -57,6 +63,30 @@ describe('cloud setup sessions', () => { expect(offers.body.team.monthlyPriceLabel).toMatch(/per seat per month/) }) + it('asks the control plane for the GitHub install URL over its service binding when bound', async () => { + // Same-account workers.dev Workers can't fetch each other (Cloudflare 1042): use the binding. + const viaBinding: string[] = [] + const binding = { + fetch: async (input: RequestInfo | URL) => { + viaBinding.push(String(input)) + return Response.json({ access: 'missing', url: 'https://github.com/apps/orca-cloud/installations/new?state=s' }) + } + } as unknown as Fetcher + const { h, create, pages } = await setup({ env: { SANDBOX_CONTROL: binding } }) + const viaFetch: string[] = [] + h.setFetch(async (input) => { + viaFetch.push(String(input)) + return new Response('error code: 1042', { status: 404 }) + }) + const { session, ticket } = ticketOf((await create()).body.url) + await pages.redeem(session, ticket) + await pages.post(session, 'team', { userId: 'u1', orgId: 'o_acme', name: 'acme', created: true }) + const install = await pages.post(session, 'github', { userId: 'u1' }) + expect(install.body).toEqual({ access: 'missing', url: 'https://github.com/apps/orca-cloud/installations/new?state=s' }) + expect(viaBinding.map((url) => new URL(url).pathname)).toEqual(['/v1/internal/github/install-url']) + expect(viaFetch.filter((url) => url.includes('install-url'))).toEqual([]) + }) + it('normalizes GitHub repositories', () => { expect(normalizeRepo('https://github.com/Acme/Web.git/')).toBe('https://github.com/acme/web') expect(normalizeRepo('git@github.com:acme/web.git')).toBe('https://github.com/acme/web') diff --git a/cloud/apps/billing/wrangler.dev.jsonc b/cloud/apps/billing/wrangler.dev.jsonc index cf472671fa6..a24d45dd603 100644 --- a/cloud/apps/billing/wrangler.dev.jsonc +++ b/cloud/apps/billing/wrangler.dev.jsonc @@ -18,6 +18,9 @@ "migrations_dir": "migrations" } ], + // Same-account Workers cannot call each other over workers.dev (error 1042): the GitHub install URL + // for the setup chain goes to the control plane through this binding (SANDBOX_CONTROL_URL names the origin). + "services": [{ "binding": "SANDBOX_CONTROL", "service": "orca-sandbox-control-dev" }], // Retries Stripe meter reports that failed inline. "triggers": { "crons": ["*/10 * * * *"] }, "vars": { diff --git a/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts b/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts index afda6e82b13..f090db1a35c 100644 --- a/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts +++ b/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts @@ -1,15 +1,11 @@ // The setup panel's rows for the composer: billing access (account, trial or plan), the GitHub // repository check (D1–D3) and the agent row, or the setup session's own rows once one is open. -import { useEffect, useMemo, useRef, useSyncExternalStore } from 'react' +import { useEffect, useMemo, useRef } from 'react' import { translate } from '@/i18n/i18n' import { openAppHttpLink } from '@/lib/browser/navigation/app-http-link-open' import { orcaCloudGitHubSetupRow } from '@/lib/orca-cloud/orca-cloud-github-setup-row' import { useOrcaCloudRepoAccess } from '@/lib/orca-cloud/use-orca-cloud-repo-access' -import { - getCloudSetupSessionRevision, - readCloudSetupSession, - subscribeCloudSetupSessionRevision -} from '@/lib/orca-cloud/cloud-setup-session-seam' +import { useCloudSetupSession } from '@/lib/orca-cloud/cloud-setup-session-seam' import type { OrcaCloudAvailability } from '../../../../../shared/orca-yaml-hook-types' import type { OrcaCloudRunTargetAccess } from '../orca-cloud-run-target-access' import { deriveCloudSetupSteps, type CloudSetupStep } from './cloud-start-model' @@ -92,9 +88,8 @@ export function useCloudStartSteps({ : null, [cloudSelected, grant, loading, owner, repoName, state] ) - // Subscribing re-renders on each poll; the read itself is a cheap synchronous lookup. - useSyncExternalStore(subscribeCloudSetupSessionRevision, getCloudSetupSessionRevision) - const session = readCloudSetupSession(setupSessionId) + // Re-renders on each poll with that poll's snapshot. + const session = useCloudSetupSession(setupSessionId) const signedOut = availability === 'needs-sign-in' || access.state === 'signed-out' const accounts = useOrcaCloudAgentAccounts(cloudSelected && !signedOut) const agentRow = useMemo( diff --git a/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.test.tsx b/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.test.tsx new file mode 100644 index 00000000000..7174156d662 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.test.tsx @@ -0,0 +1,68 @@ +// @vitest-environment happy-dom + +import { act, cleanup, renderHook } from '@testing-library/react' +import { afterEach, describe, expect, it } from 'vitest' +import type { CloudSetupStep } from '@/components/new-workspace/run-on/cloud-start-model' +import { + getCloudSetupSessionSnapshot, + notifyCloudSetupSessionChanged, + setCloudSetupSessionSource, + useCloudSetupSession, + type CloudSetupSessionSnapshot +} from './cloud-setup-session-seam' + +const planActive: CloudSetupStep[] = [ + { id: 'account', state: 'done' }, + { id: 'plan', state: 'active' } +] +const planDone: CloudSetupStep[] = [ + { id: 'account', state: 'done' }, + { id: 'plan', state: 'done' } +] + +/** A poller stand-in: the source builds a new object on every read, like the real one. */ +function installPoller(): { set: (next: CloudSetupSessionSnapshot | null) => void } { + let latest: CloudSetupSessionSnapshot | null = null + setCloudSetupSessionSource((sessionId) => + sessionId === 'cs_test' && latest ? { ...latest, steps: [...latest.steps] } : null + ) + return { + set: (next) => { + latest = next + notifyCloudSetupSessionChanged() + } + } +} + +afterEach(() => { + cleanup() + setCloudSetupSessionSource(null) +}) + +describe('getCloudSetupSessionSnapshot', () => { + it('returns the same object until the next change, then the new snapshot', () => { + const poller = installPoller() + poller.set({ steps: planActive, finished: false }) + const first = getCloudSetupSessionSnapshot('cs_test') + expect(getCloudSetupSessionSnapshot('cs_test')).toBe(first) + poller.set({ steps: planDone, finished: true }) + expect(getCloudSetupSessionSnapshot('cs_test')).toEqual({ steps: planDone, finished: true }) + }) + + it('is null without a session id or a source', () => { + expect(getCloudSetupSessionSnapshot(null)).toBeNull() + expect(getCloudSetupSessionSnapshot('cs_test')).toBeNull() + }) +}) + +describe('useCloudSetupSession', () => { + it('re-renders with each poll, starting from no snapshot yet', () => { + const poller = installPoller() + const { result } = renderHook(() => useCloudSetupSession('cs_test')) + expect(result.current).toBeNull() + act(() => poller.set({ steps: planActive, finished: false })) + expect(result.current?.steps).toEqual(planActive) + act(() => poller.set({ steps: planDone, finished: true })) + expect(result.current).toEqual({ steps: planDone, finished: true }) + }) +}) diff --git a/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.ts b/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.ts index fd676601f7e..e92b0addc43 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-setup-session-seam.ts @@ -4,6 +4,7 @@ // - a starter: "Set up and start" calls it and keeps the returned session id (also on the draft); // - a source: a synchronous read of the latest polled snapshot for a session id; // and calls notifyCloudSetupSessionChanged() after each poll so the panel re-renders. +import { useCallback, useSyncExternalStore } from 'react' import type { CloudSetupStep } from '@/components/new-workspace/run-on/cloud-start-model' /** "All set" holds this long before the composer submits itself (O4; 0 with reduced motion). */ @@ -67,8 +68,17 @@ export function readCloudSetupSession(sessionId: string | null): CloudSetupSessi return sessionId && source ? source(sessionId) : null } +// Bumped on every change, so a read is cached per (session, revision): useSyncExternalStore needs +// the same object back until something changed. +let revision = 0 +const cachedReads = new Map< + string, + { revision: number; value: CloudSetupSessionSnapshot | null } +>() + /** Call after each poll so open composers re-read their session. */ export function notifyCloudSetupSessionChanged(): void { + revision += 1 for (const listener of listeners) { listener() } @@ -79,14 +89,28 @@ export function subscribeCloudSetupSession(listener: () => void): () => void { return () => listeners.delete(listener) } -let revision = 0 -/** useSyncExternalStore pair: each poll bumps the revision so composers re-read their session. */ -export function subscribeCloudSetupSessionRevision(listener: () => void): () => void { - return subscribeCloudSetupSession(() => { - revision += 1 - listener() - }) +/** The latest snapshot for a session: the same object until the next poll or source change. */ +export function getCloudSetupSessionSnapshot( + sessionId: string | null +): CloudSetupSessionSnapshot | null { + if (!sessionId) { + return null + } + const cached = cachedReads.get(sessionId) + if (cached?.revision === revision) { + return cached.value + } + const value = readCloudSetupSession(sessionId) + cachedReads.set(sessionId, { revision, value }) + return value } -export function getCloudSetupSessionRevision(): number { - return revision + +/** + * A setup session's snapshot that re-renders on every poll. Read it through this hook, never by + * calling readCloudSetupSession in a render: the React Compiler memoizes a bare call on its + * arguments, so the panel would keep the first (empty) read and never see the session move on. + */ +export function useCloudSetupSession(sessionId: string | null): CloudSetupSessionSnapshot | null { + const getSnapshot = useCallback(() => getCloudSetupSessionSnapshot(sessionId), [sessionId]) + return useSyncExternalStore(subscribeCloudSetupSession, getSnapshot) } From 06463860a48691b475093357d02f6d39273a6e8b Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 14:39:19 -0800 Subject: [PATCH 2/2] fix(cloud-setup): a signed-out start asks for GitHub; a reachable repo finishes a waiting session's GitHub row - Signed out, the composer can't check GitHub yet, so 'Set up and start' asked the session for the plan only. The browser then ended on 'You're all set' while the composer still needed GitHub. A GitHub project now always asks a signed-out session for GitHub; the chain marks it done on its own when access is already there. - With the composer following its session, the session's GitHub row is the only source of truth. The control plane's notice to billing is best effort, so also take the desktop's own repo-access poll: once it reaches the repository, the GitHub row is done and the start goes ahead. --- .../run-on/cloud-start-model.test.ts | 25 ++++++++++++++++++- .../new-workspace/run-on/cloud-start-model.ts | 17 +++++++++++++ .../run-on/use-cloud-start-steps.ts | 9 ++++--- .../install-cloud-setup-session.test.ts | 11 +++++++- .../orca-cloud/install-cloud-setup-session.ts | 18 ++++++++++--- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/components/new-workspace/run-on/cloud-start-model.test.ts b/src/renderer/src/components/new-workspace/run-on/cloud-start-model.test.ts index 0be7e1912f6..fb27f03f3be 100644 --- a/src/renderer/src/components/new-workspace/run-on/cloud-start-model.test.ts +++ b/src/renderer/src/components/new-workspace/run-on/cloud-start-model.test.ts @@ -5,7 +5,8 @@ import { getOrcaCloudSegmentState, getRunOnSegment, isCloudSetupPanelVisible, - resolveDefaultRunOn + resolveDefaultRunOn, + withReachableGitHub } from './cloud-start-model' describe('Run on segment', () => { @@ -250,3 +251,25 @@ describe('remembered Run on default', () => { ).toEqual({ target: 'local', cloudFellBack: false }) }) }) + +describe('withReachableGitHub', () => { + const session = [ + { id: 'account', state: 'done' }, + { id: 'plan', state: 'done', detail: 'subscription' }, + { id: 'github', state: 'active' } + ] as const + + it("marks the session's GitHub row done once this desktop reaches the repository", () => { + expect(withReachableGitHub([...session], true)).toEqual([ + { id: 'account', state: 'done' }, + { id: 'plan', state: 'done', detail: 'subscription' }, + { id: 'github', state: 'done' } + ]) + expect(getCloudPrimaryAction([...withReachableGitHub([...session], true)])).toBe('start') + }) + + it('leaves the rows alone while the repository is still unreachable', () => { + const steps = [...session] + expect(withReachableGitHub(steps, false)).toBe(steps) + }) +}) diff --git a/src/renderer/src/components/new-workspace/run-on/cloud-start-model.ts b/src/renderer/src/components/new-workspace/run-on/cloud-start-model.ts index 9af5c2924bc..a257c9ebebd 100644 --- a/src/renderer/src/components/new-workspace/run-on/cloud-start-model.ts +++ b/src/renderer/src/components/new-workspace/run-on/cloud-start-model.ts @@ -260,3 +260,20 @@ export function resolveDefaultRunOn({ } return { target: wanted, cloudFellBack: false } } + +/** + * The session's rows, with its GitHub row done once this desktop's own check reaches the + * repository: the control plane's notice to the session is best effort, and the desktop's + * repo-access poll is the fallback that keeps a waiting start from hanging. + */ +export function withReachableGitHub( + steps: readonly CloudSetupStep[], + reachable: boolean +): readonly CloudSetupStep[] { + if (!reachable || !steps.some((step) => step.id === 'github' && step.state !== 'done')) { + return steps + } + return steps.map( + (step): CloudSetupStep => (step.id === 'github' ? { id: 'github', state: 'done' } : step) + ) +} diff --git a/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts b/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts index f090db1a35c..d023a9876b6 100644 --- a/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts +++ b/src/renderer/src/components/new-workspace/run-on/use-cloud-start-steps.ts @@ -8,7 +8,7 @@ import { useOrcaCloudRepoAccess } from '@/lib/orca-cloud/use-orca-cloud-repo-acc import { useCloudSetupSession } from '@/lib/orca-cloud/cloud-setup-session-seam' import type { OrcaCloudAvailability } from '../../../../../shared/orca-yaml-hook-types' import type { OrcaCloudRunTargetAccess } from '../orca-cloud-run-target-access' -import { deriveCloudSetupSteps, type CloudSetupStep } from './cloud-start-model' +import { deriveCloudSetupSteps, withReachableGitHub, type CloudSetupStep } from './cloud-start-model' import { cloudStartAgentStep } from './cloud-start-agent-step' import { orcaCloudAgentSetupRow, @@ -109,11 +109,12 @@ export function useCloudStartSteps({ const steps = useMemo( () => decorateCloudSetupSteps( - session?.steps ?? - deriveCloudSetupSteps({ availability, access, github: githubOverride ?? github }), + session + ? withReachableGitHub(session.steps, Boolean(githubOverride) || state === 'ok') + : deriveCloudSetupSteps({ availability, access, github: githubOverride ?? github }), agentRow ), - [access, agentRow, availability, github, githubOverride, session?.steps] + [access, agentRow, availability, github, githubOverride, session, state] ) return { steps, session, openInstall } } diff --git a/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.test.ts b/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.test.ts index c176d5339e7..de8a7d28850 100644 --- a/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.test.ts +++ b/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest' vi.mock('@/store', () => ({ useAppStore: { getState: () => ({ repos: [] }) } })) -import { githubRepoUrlFor, setupNeedsFrom } from './install-cloud-setup-session' +import { githubRepoUrlFor, sessionNeedsFor, setupNeedsFrom } from './install-cloud-setup-session' describe('cloud setup seam wiring', () => { it('maps a project remote to the setup session repository', () => { @@ -19,4 +19,13 @@ describe('cloud setup seam wiring', () => { 'github' ]) }) + + it('asks a signed-out start for GitHub access, which it could not check yet', () => { + const repo = 'https://github.com/acme/web' + expect(sessionNeedsFor(['account', 'plan', 'agent'], repo)).toEqual(['plan', 'github']) + expect(sessionNeedsFor(['account', 'plan'], undefined)).toEqual(['plan']) + // Signed in, the composer's own GitHub check already decided. + expect(sessionNeedsFor(['plan'], repo)).toEqual(['plan']) + expect(sessionNeedsFor(['plan', 'github'], undefined)).toEqual(['plan']) + }) }) diff --git a/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.ts b/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.ts index 021e5054881..0d9db7fa14f 100644 --- a/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.ts +++ b/src/renderer/src/lib/orca-cloud/install-cloud-setup-session.ts @@ -34,6 +34,20 @@ export function setupNeedsFrom(needs: readonly string[]): CloudSetupNeed[] { ) } +/** + * What the session is asked to run. Signed out, Orca can't check GitHub access yet, so a GitHub + * project always asks for it: the chain marks it done on its own when access is already there. + * Without it the browser said "You're all set" while the composer still needed GitHub. + */ +export function sessionNeedsFor(needs: readonly string[], repoUrl: string | undefined): CloudSetupNeed[] { + const steps = setupNeedsFrom(needs) + if (!repoUrl) { + // Without a GitHub remote there's nothing to install; D3 covers that case in the composer. + return steps.filter((need) => need !== 'github') + } + return needs.includes('account') && !steps.includes('github') ? [...steps, 'github'] : steps +} + export function installCloudSetupSession(): () => void { setCloudSetupSessionSource((sessionId) => { const snapshot = getCloudSetupSnapshot(sessionId) @@ -57,11 +71,9 @@ export function installCloudSetupSession(): () => void { setCloudSetupStarter(async (request) => { const repo = getIndexedRepoMap(useAppStore.getState().repos).get(request.repoId) const repoUrl = githubRepoUrlFor(repo?.gitRemoteIdentity?.remoteUrl) - const needs = setupNeedsFrom(request.needs) return startCloudSetupSession({ ...(repoUrl ? { repo: repoUrl } : {}), - // Without a GitHub remote there's nothing to install; D3 covers that case in the composer. - needs: repoUrl ? needs : needs.filter((need) => need !== 'github') + needs: sessionNeedsFor(request.needs, repoUrl) }) }) const unsubscribe = subscribeCloudSetupSessions(() => notifyCloudSetupSessionChanged())