From 51541be36fec418f1a17027e70bd9a2f767b3d21 Mon Sep 17 00:00:00 2001 From: Nicky Leach Date: Tue, 1 Dec 2026 17:45:32 +0000 Subject: [PATCH] fix(onboarding): start an agent-targeted sign-in only on a settled, prepared draft (#23383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Onboarding signs the first agent in to Claude or Codex through a plugin-managed execution environment that is prepared for the agent (#23312) > - Each change to the setup draft, such as switching between Claude and OpenAI, makes Core prepare the environment again before it accepts a sign-in > - The wizard remembered that the environment had been prepared once, so it started the new sign-in during that preparation > - Core refused it with `login_agent_unavailable`, and the sign-in card showed only "Sign-in didn't finish" > - This pull request starts a sign-in only when the draft has settled on the current choice and is prepared, and shows Core's error on the card > - The benefit is that switching the sign-in source works, without an added wait ## Linked Issues or Issue Description Refs #23312 and #23315. **What happened?** In onboarding with a plugin-managed execution environment as the default, a user picked Claude, then switched to OpenAI. Every sign-in start after the switch returned `400 login_agent_unavailable`: Core refused the agent as the sign-in target. The card showed "Sign-in didn't finish. Try signing in again." **What did you expect to happen?** The OpenAI sign-in starts as soon as the environment is prepared for the new choice. **Steps to reproduce** 1. Make a plugin-managed execution environment that prepares an environment per agent the instance default. 2. Start onboarding, name the agent, and choose Claude with a subscription. 3. When the Claude sign-in shows, choose OpenAI. 4. See the sign-in fail with the message above. ## What Changed - The sign-in readiness hook takes `settled` and `holdKey`. A sign-in starts only when the agent has settled on the current choice and the environment is prepared for it. Readiness is held per agent, environment and sign-in source, so a source switch waits for the real preparation, and a sign-in already on screen stays on screen. - Onboarding treats the setup draft as settled when its saved harness (and model, when one is set) matches the selection. Each draft save puts the saved agent in the query cache at once, and readiness polls every 500 ms instead of every second. - The new-agent flow treats its draft as settled when no environment save is in flight, and polls a preparing draft every 500 ms. - Both sign-in panels show Core's start error for any sign-in that targets an agent, not only for one that Core selects automatically. ## Verification - `pnpm typecheck` in `ui/`: no errors. - The sign-in readiness helper and hook suites, the `AgentProviderConnection` suites and the `AgentConfigForm` render suite in `ui/`: 23 tests pass. A new hook test covers a switch from Claude to OpenAI and back while the environment is prepared again. - `PAPERCLIP_PLAYWRIGHT_CHANNEL=chrome npx playwright test --config tests/e2e/playwright.config.ts tests/e2e/onboarding-early-setup.spec.ts tests/e2e/onboarding.spec.ts -g "naming step creates|create-company path"`: 2 tests pass. - I reproduced the failure on a staging instance from the network log. I did not repeat the sign-in on a staging instance with this change. ## Risks - A change to the draft after a sign-in started can still make Core refuse the login lease when it acquires it. The card then shows Core's error and the usual retry. - Readiness polling is now twice as frequent while an environment is prepared. It stops when the environment is ready. - No migration and no API change. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), with extended thinking and tool use (code search, shell, tests, browser network inspection) in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- ui/src/components/AgentConfigForm.tsx | 6 +++--- ui/src/components/OnboardingWizard.tsx | 16 +++++++++++++--- ui/src/components/new-agent/NewAgentSetup.tsx | 4 +++- ui/src/lib/agent-computer-login.hook.test.tsx | 18 ++++++++++++++++-- ui/src/lib/agent-computer-login.ts | 12 +++++++----- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/ui/src/components/AgentConfigForm.tsx b/ui/src/components/AgentConfigForm.tsx index f88bc6b2e08..10f75b0109f 100644 --- a/ui/src/components/AgentConfigForm.tsx +++ b/ui/src/components/AgentConfigForm.tsx @@ -3296,7 +3296,7 @@ function DisplayedCodeLoginPanel({ ) => + agentHarnessType(agent.adapterType, agent.adapterConfig) === adapterType + && (!model.trim() || agent.adapterConfig?.model === model.trim()); const loginAgentId = createdAgentId ?? setupAgentId; const loginAgentQuery = useQuery({ queryKey: queryKeys.agents.detail(loginAgentId ?? "none"), @@ -1176,16 +1179,20 @@ function OnboardingWizardInner({ const agent = query.state.data; if (query.state.status === "error") return false; const instanceDefault = instanceSettingsForLogin?.defaultEnvironmentId ?? null; - return agent && (agent.lifecycleError || agentComputerHostsLogin(agent, resolvedLoginEnvironmentId, instanceDefault) - || agentComputerAwaitsHire(agent, resolvedLoginEnvironmentId, instanceDefault)) ? false : 1000; + return agent && (agent.lifecycleError || (agentSettledOnChoice(agent) + && (agentComputerHostsLogin(agent, resolvedLoginEnvironmentId, instanceDefault) + || agentComputerAwaitsHire(agent, resolvedLoginEnvironmentId, instanceDefault)))) ? false : 500; }, }); const loginAgent = loginAgentQuery.data?.id === loginAgentId ? loginAgentQuery.data : null; + const loginAgentSettled = Boolean(loginAgent && agentSettledOnChoice(loginAgent)); const loginComputer = useAgentComputerLogin({ required: loginRequiresAgent, agent: loginAgent, environmentId: resolvedLoginEnvironmentId, instanceDefaultEnvironmentId: instanceSettingsForLogin?.defaultEnvironmentId ?? null, + settled: loginAgentSettled, + holdKey: adapterType, }); const loginComputerReady = loginComputer.ready; const loginOnOwnComputer = loginRequiresAgent && loginComputer.ownComputer; @@ -2352,7 +2359,10 @@ function OnboardingWizardInner({ const save = () => { setupSave.current = setupSave.current.catch(() => {}).then(async () => { if (!stillTheSameCompany(companyId) || hiringAgentRef.current !== null) return; - await agentsApi.update(id, JSON.parse(setupSnapshot), companyId); + const saved = await agentsApi.update(id, JSON.parse(setupSnapshot), companyId); + if (!stillTheSameCompany(companyId)) return; + await queryClient.cancelQueries({ queryKey: queryKeys.agents.detail(id) }); + queryClient.setQueryData(queryKeys.agents.detail(id), saved); }); void setupSave.current.catch(cause => { if (stillTheSameCompany(companyId)) setError(cause instanceof Error ? cause.message : "Could not save agent setup."); diff --git a/ui/src/components/new-agent/NewAgentSetup.tsx b/ui/src/components/new-agent/NewAgentSetup.tsx index a271ed5b737..1922b39ea84 100644 --- a/ui/src/components/new-agent/NewAgentSetup.tsx +++ b/ui/src/components/new-agent/NewAgentSetup.tsx @@ -214,7 +214,7 @@ function Setup({ const agent = query.state.data; if (!agent || agent.lifecycleError) return false; return (agent.pauseReason === "setup" && ["preparing", "pausing"].includes(agent.lifecycleState ?? "")) - || (screen === "saved" && !isDot && ["preparing", "verifying", "resuming", "pausing"].includes(agent.lifecycleState ?? "")) ? 1000 : false; + || (screen === "saved" && !isDot && ["preparing", "verifying", "resuming", "pausing"].includes(agent.lifecycleState ?? "")) ? 500 : false; }, }); const created = @@ -378,6 +378,8 @@ function Setup({ agent: created, environmentId, instanceDefaultEnvironmentId: settings.data?.defaultEnvironmentId ?? null, + settled: !environmentSaving, + holdKey: connectionAdapter ?? adapterType, }); const envKey = isKimi ? "KIMI_API_KEY" : SETUP_CREDENTIAL_KEYS[adapterType] ?? providerKeys[provider] ?? "API_KEY"; diff --git a/ui/src/lib/agent-computer-login.hook.test.tsx b/ui/src/lib/agent-computer-login.hook.test.tsx index 8f5ff4857a9..777f2d965e6 100644 --- a/ui/src/lib/agent-computer-login.hook.test.tsx +++ b/ui/src/lib/agent-computer-login.hook.test.tsx @@ -23,8 +23,9 @@ function Probe(props: Parameters[0]) { return null; } -function render(agent: Agent) { - flushSync(() => root.render()); +function render(agent: Agent, { settled = true, holdKey = "claude_local" } = {}) { + flushSync(() => root.render()); } beforeEach(() => { @@ -51,6 +52,19 @@ describe("useAgentComputerLogin", () => { expect(login).toEqual({ ready: false, ownComputer: false }); }); + it("waits for the draft to settle on a newly chosen source before starting another sign-in", () => { + render(draft("verifying")); + expect(login).toEqual({ ready: true, ownComputer: true }); + render(draft("verifying"), { settled: false, holdKey: "codex_local" }); + expect(login).toEqual({ ready: false, ownComputer: false }); + render(draft("preparing"), { holdKey: "codex_local" }); + expect(login).toEqual({ ready: false, ownComputer: false }); + render(draft("preparing"), { holdKey: "claude_local" }); + expect(login).toEqual({ ready: false, ownComputer: false }); + render(draft("verifying"), { holdKey: "claude_local" }); + expect(login).toEqual({ ready: true, ownComputer: true }); + }); + it("does not wait for a draft held until its hire is approved", () => { render(draft("paused")); expect(login).toEqual({ ready: true, ownComputer: false }); diff --git a/ui/src/lib/agent-computer-login.ts b/ui/src/lib/agent-computer-login.ts index 67bed4a8951..d24dfd0880c 100644 --- a/ui/src/lib/agent-computer-login.ts +++ b/ui/src/lib/agent-computer-login.ts @@ -36,16 +36,18 @@ export function useAgentComputerLogin(input: { agent: LoginComputerAgent | null | undefined; environmentId: string | null; instanceDefaultEnvironmentId: string | null; + settled: boolean; + holdKey: string; }): { ready: boolean; ownComputer: boolean } { - const { required, agent, environmentId, instanceDefaultEnvironmentId } = input; - const hosts = Boolean(required && agent && agentComputerHostsLogin(agent, environmentId, instanceDefaultEnvironmentId)); - const key = required && agent ? JSON.stringify([agent.id, environmentId]) : null; + const { required, agent, environmentId, instanceDefaultEnvironmentId, settled, holdKey } = input; + const hosts = Boolean(required && agent && settled && agentComputerHostsLogin(agent, environmentId, instanceDefaultEnvironmentId)); + const key = required && agent ? JSON.stringify([agent.id, environmentId, holdKey]) : null; const [prepared, setPrepared] = useState(null); useEffect(() => { - if (hosts && key) setPrepared(key); + setPrepared(current => current === key ? current : hosts && key ? key : null); }, [hosts, key]); if (!required) return { ready: true, ownComputer: false }; if (hosts || (key !== null && prepared === key)) return { ready: true, ownComputer: true }; - if (agent && agentComputerAwaitsHire(agent, environmentId, instanceDefaultEnvironmentId)) return { ready: true, ownComputer: false }; + if (agent && settled && agentComputerAwaitsHire(agent, environmentId, instanceDefaultEnvironmentId)) return { ready: true, ownComputer: false }; return { ready: false, ownComputer: false }; }