The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
[codex] Ignore stale stored company selections (#4602)
Dottad95968a9f8ee
diff --git a/ui/src/context/CompanyContext.test.tsx b/ui/src/context/CompanyContext.test.tsxnew file mode 100644index 0000000000..5e68ad3d2c--- /dev/null+++ b/ui/src/context/CompanyContext.test.tsx@@ -0,0 +1,71 @@+import { describe, expect, it } from "vitest";+import { resolveBootstrapCompanySelection, shouldClearStoredCompanySelection } from "./CompanyContext";++const activeCompany = { id: "company-1" };+const secondActiveCompany = { id: "company-2" };+const archivedCompany = { id: "archived-company" };++describe("resolveBootstrapCompanySelection", () => {+ it("does not expose a stale stored company id before companies load", () => {+ expect(resolveBootstrapCompanySelection({+ companies: [],+ sidebarCompanies: [],+ selectedCompanyId: null,+ storedCompanyId: "stale-company",+ })).toBeNull();+ });++ it("replaces a stale stored company id with the first loaded company", () => {+ expect(resolveBootstrapCompanySelection({+ companies: [activeCompany],+ sidebarCompanies: [activeCompany],+ selectedCompanyId: null,+ storedCompanyId: "stale-company",+ })).toBe("company-1");+ });++ it("keeps a valid selected company ahead of stored bootstrap state", () => {+ expect(resolveBootstrapCompanySelection({+ companies: [activeCompany],+ sidebarCompanies: [activeCompany],+ selectedCompanyId: "company-1",+ storedCompanyId: "stale-company",+ })).toBe("company-1");+ });++ it("keeps a valid stored company id instead of falling back to the first company", () => {+ expect(resolveBootstrapCompanySelection({+ companies: [activeCompany, secondActiveCompany],+ sidebarCompanies: [activeCompany, secondActiveCompany],+ selectedCompanyId: null,+ storedCompanyId: "company-2",+ })).toBe("company-2");+ });++ it("uses selectable sidebar companies before archived companies", () => {+ expect(resolveBootstrapCompanySelection({+ companies: [archivedCompany, activeCompany],+ sidebarCompanies: [activeCompany],+ selectedCompanyId: null,+ storedCompanyId: "archived-company",+ })).toBe("company-1");+ });+});++describe("shouldClearStoredCompanySelection", () => {+ it("does not clear the stored company selection during an unauthorized company list response", () => {+ expect(shouldClearStoredCompanySelection({+ companies: [],+ isLoading: false,+ unauthorized: true,+ })).toBe(false);+ });++ it("clears the stored company selection when an authorized company list is empty", () => {+ expect(shouldClearStoredCompanySelection({+ companies: [],+ isLoading: false,+ unauthorized: false,+ })).toBe(true);+ });+});diff --git a/ui/src/context/CompanyContext.tsx b/ui/src/context/CompanyContext.tsxindex 86afa175f7..18a043f0c3 100644--- a/ui/src/context/CompanyContext.tsx+++ b/ui/src/context/CompanyContext.tsx@@ -14,6 +14,7 @@ import { ApiError } from "../api/client";import { queryKeys } from "../lib/queryKeys";import type { CompanySelectionSource } from "../lib/company-selection";type CompanySelectionOptions = { source?: CompanySelectionSource };+type CompanyListResult = { companies: Company[]; unauthorized: boolean };interface CompanyContextValue {companies: Company[];@@ -35,25 +36,55 @@ const STORAGE_KEY = "paperclip.selectedCompanyId";const CompanyContext = createContext<CompanyContextValue | null>(null);+export function resolveBootstrapCompanySelection(input: {+ companies: Array<Pick<Company, "id">>;+ sidebarCompanies: Array<Pick<Company, "id">>;+ selectedCompanyId: string | null;+ storedCompanyId: string | null;+}) {+ if (input.companies.length === 0) return null;++ const selectableCompanies = input.sidebarCompanies.length > 0+ ? input.sidebarCompanies+ : input.companies;+ if (input.selectedCompanyId && selectableCompanies.some((company) => company.id === input.selectedCompanyId)) {+ return input.selectedCompanyId;+ }+ if (input.storedCompanyId && selectableCompanies.some((company) => company.id === input.storedCompanyId)) {+ return input.storedCompanyId;+ }+ return selectableCompanies[0]?.id ?? null;+}++export function shouldClearStoredCompanySelection(input: {+ companies: Array<Pick<Company, "id">>;+ isLoading: boolean;+ unauthorized: boolean;+}) {+ return !input.isLoading && !input.unauthorized && input.companies.length === 0;+}+export function CompanyProvider({ children }: { children: ReactNode }) {const queryClient = useQueryClient();const [selectionSource, setSelectionSource] = useState<CompanySelectionSource>("bootstrap");- const [selectedCompanyId, setSelectedCompanyIdState] = useState<string | null>(() => localStorage.getItem(STORAGE_KEY));+ const [selectedCompanyId, setSelectedCompanyIdState] = useState<string | null>(null);- const { data: companies = [], isLoading, error } = useQuery({+ const { data: companiesResult = { companies: [], unauthorized: false }, isLoading, error } = useQuery<CompanyListResult>({queryKey: queryKeys.companies.all,queryFn: async () => {try {- return await companiesApi.list();+ return { companies: await companiesApi.list(), unauthorized: false };} catch (err) {if (err instanceof ApiError && err.status === 401) {- return [];+ return { companies: [], unauthorized: true };}throw err;}},retry: false,});+ const companies = companiesResult.companies;+ const companyListUnauthorized = companiesResult.unauthorized;const sidebarCompanies = useMemo(() => companies.filter((company) => company.status !== "archived"),[companies],@@ -61,18 +92,28 @@ export function CompanyProvider({ children }: { children: ReactNode }) {// Auto-select first company when list loadsuseEffect(() => {- if (companies.length === 0) return;-- const selectableCompanies = sidebarCompanies.length > 0 ? sidebarCompanies : companies;- const stored = localStorage.getItem(STORAGE_KEY);- if (stored && selectableCompanies.some((c) => c.id === stored)) return;- if (selectedCompanyId && selectableCompanies.some((c) => c.id === selectedCompanyId)) return;+ if (isLoading) return;+ if (companies.length === 0) {+ if (shouldClearStoredCompanySelection({ companies, isLoading: false, unauthorized: companyListUnauthorized })) {+ if (selectedCompanyId !== null) {+ setSelectedCompanyIdState(null);+ }+ localStorage.removeItem(STORAGE_KEY);+ }+ return;+ }- const next = selectableCompanies[0]!.id;+ const next = resolveBootstrapCompanySelection({+ companies,+ sidebarCompanies,+ selectedCompanyId,+ storedCompanyId: localStorage.getItem(STORAGE_KEY),+ });+ if (next === null || next === selectedCompanyId) return;setSelectedCompanyIdState(next);setSelectionSource("bootstrap");localStorage.setItem(STORAGE_KEY, next);- }, [companies, selectedCompanyId, sidebarCompanies]);+ }, [companies, companyListUnauthorized, isLoading, selectedCompanyId, sidebarCompanies]);const setSelectedCompanyId = useCallback((companyId: string, options?: CompanySelectionOptions) => {setSelectedCompanyIdState(companyId);