Repository: continuedev/continue
File: core/config/ConfigHandler.ts (TypeScript)

Code before:

  async getProfileKey(orgId: string) {
    const workspaceId = await this.getWorkspaceId();
    return `${workspaceId}:::${orgId}`;
  }

  private async cascadeInit(reason: string, isLogin?: boolean) {
    const signal = this.cascadeAbortController.signal;
    this.workspaceDirs = null; // forces workspace dirs reload

    // Always update globalLocalProfileManager before recreating all the loaders
    // during every cascadeInit so it holds the most recent controlPlaneClient.
    this.globalLocalProfileManager = new ProfileLifecycleManager(
      new LocalProfileLoader(this.ide, this.controlPlaneClient, this.llmLogger),
      this.ide,
    );

    try {
      const { orgs, errors } = await this.getOrgs();

      // Figure out selected org
      const workspaceId = await this.getWorkspaceId();
      const selectedOrgs =
        this.globalContext.get("lastSelectedOrgIdForWorkspace") ?? {};
      // reset selected org to first available org on login, otherwise use saved selection
      const currentSelection = isLogin ? null : selectedOrgs[workspaceId];

      const firstNonPersonal = orgs.find(
        (org) => org.id !== this.PERSONAL_ORG_DESC.id,
      );
      const fallback: OrgWithProfiles | null =
        firstNonPersonal ?? orgs[0] ?? null;

      let selectedOrg: OrgWithProfiles | null;
      if (currentSelection) {
        const match = orgs.find((org) => org.id === currentSelection);
        if (match) {
          selectedOrg = match;
        } else {
          selectedOrg = fallback;
        }
      } else {
        selectedOrg = fallback;
      }

      if (signal.aborted) {
        return; // local only case, no`fetch to throw abort error
      }

      this.globalContext.update("lastSelectedOrgIdForWorkspace", {
        ...selectedOrgs,

Review comment (suggestion):
@uinstinct this feels closer but it would still unnecessarily lose someone's org selection if they were logging in for the 2nd time.

I think you could change the line

`if(currentSelection)`

to `if(currentSelection && !(login && currentSelection === "personal")`

Or something that doesn't just cut out the current selection regardless of personal or not

Code after:

  async getProfileKey(orgId: string) {
    const workspaceId = await this.getWorkspaceId();
    return `${workspaceId}:::${orgId}`;
  }

  private async cascadeInit(reason: string, isLogin?: boolean) {
    const signal = this.cascadeAbortController.signal;
    this.workspaceDirs = null; // forces workspace dirs reload

    // Always update globalLocalProfileManager before recreating all the loaders
    // during every cascadeInit so it holds the most recent controlPlaneClient.
    this.globalLocalProfileManager = new ProfileLifecycleManager(
      new LocalProfileLoader(this.ide, this.controlPlaneClient, this.llmLogger),
      this.ide,
    );

    try {
      const { orgs, errors } = await this.getOrgs();

      // Figure out selected org
      const workspaceId = await this.getWorkspaceId();
      const selectedOrgs =
        this.globalContext.get("lastSelectedOrgIdForWorkspace") ?? {};
      let currentSelection = selectedOrgs[workspaceId];

      // reset personal org to first available non-personal org on login
      if (isLogin && currentSelection === "personal") {
        currentSelection = null;
      }

      const firstNonPersonal = orgs.find(
        (org) => org.id !== this.PERSONAL_ORG_DESC.id,
      );
      const fallback: OrgWithProfiles | null =
        firstNonPersonal ?? orgs[0] ?? null;

      let selectedOrg: OrgWithProfiles | null;
      if (currentSelection) {
        const match = orgs.find((org) => org.id === currentSelection);
        if (match) {
          selectedOrg = match;
        } else {
          selectedOrg = fallback;
        }
      } else {
        selectedOrg = fallback;
      }

      if (signal.aborted) {
        return; // local only case, no`fetch to throw abort error