-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: cache hub assistants for offline fallback #12016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| describe("ConfigHandler hub assistant cache fallback", () => { | ||
| let tempDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "continue-hub-cache-")); | ||
| process.env.CONTINUE_GLOBAL_DIR = path.join(tempDir, ".continue-global"); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete process.env.CONTINUE_GLOBAL_DIR; | ||
| fs.rmSync(tempDir, { force: true, recursive: true }); | ||
| }); | ||
|
|
||
| test("uses cached hub assistants when the hub becomes unavailable", async () => { | ||
| const [{ ConfigHandler }, { LLMLogger }, { default: FileSystemIde }] = | ||
| await Promise.all([ | ||
| import("./ConfigHandler"), | ||
| import("../llm/logger"), | ||
| import("../util/filesystem"), | ||
| ]); | ||
|
|
||
| class TestIde extends FileSystemIde { | ||
| override async fileExists(fileUri: string): Promise<boolean> { | ||
| const filepath = fileUri.startsWith("file://") | ||
| ? fileURLToPath(fileUri) | ||
| : fileUri; | ||
| return fs.existsSync(filepath); | ||
| } | ||
| } | ||
|
|
||
| const ide = new TestIde(tempDir); | ||
| const handler = new ConfigHandler( | ||
| ide, | ||
| new LLMLogger(), | ||
| Promise.resolve(undefined), | ||
| ); | ||
| await handler.isInitialized; | ||
|
|
||
| const personalAssistant = { | ||
| configResult: { | ||
| config: { | ||
| name: "Local LlamaCpp", | ||
| version: "1.0.0", | ||
| }, | ||
| configLoadInterrupted: false, | ||
| errors: [], | ||
| }, | ||
| iconUrl: "https://example.com/personal.png", | ||
| ownerSlug: "lemonade", | ||
| packageSlug: "llamacpp", | ||
| rawYaml: "name: Local LlamaCpp", | ||
| }; | ||
| const orgAssistant = { | ||
| configResult: { | ||
| config: { | ||
| name: "Org Assistant", | ||
| version: "2.0.0", | ||
| }, | ||
| configLoadInterrupted: false, | ||
| errors: [], | ||
| }, | ||
| iconUrl: "https://example.com/org.png", | ||
| ownerSlug: "continuedev", | ||
| packageSlug: "team-assistant", | ||
| rawYaml: "name: Org Assistant", | ||
| }; | ||
|
|
||
| let outage = false; | ||
| (handler as any).controlPlaneClient = { | ||
| getPolicy: vi.fn().mockResolvedValue(null), | ||
| isSignedIn: vi.fn().mockResolvedValue(true), | ||
| listOrganizations: vi.fn().mockImplementation(async () => { | ||
| if (outage) { | ||
| return null; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| iconUrl: "https://example.com/org.png", | ||
| id: "org-1", | ||
| name: "Org One", | ||
| slug: "org-one", | ||
| }, | ||
| ]; | ||
| }), | ||
| listAssistants: vi | ||
| .fn() | ||
| .mockImplementation(async (orgId: string | null) => { | ||
| if (outage) { | ||
| return null; | ||
| } | ||
|
|
||
| return orgId === null ? [personalAssistant] : [orgAssistant]; | ||
| }), | ||
| }; | ||
|
|
||
| const warm = await (handler as any).getOrgs(); | ||
| expect(warm.errors ?? []).toHaveLength(0); | ||
|
|
||
| outage = true; | ||
|
|
||
| const fallback = await (handler as any).getOrgs(); | ||
| expect(fallback.errors?.map((error: any) => error.message)).toContain( | ||
| "Continue Hub is unavailable. Using cached Hub assistants until the service recovers.", | ||
| ); | ||
|
|
||
| const personalOrg = fallback.orgs.find((org: any) => org.id === "personal"); | ||
| expect( | ||
| personalOrg?.profiles.some( | ||
| (profile: any) => | ||
| profile.profileDescription.id === "lemonade/llamacpp" && | ||
| profile.profileDescription.title === "Local LlamaCpp", | ||
| ), | ||
| ).toBe(true); | ||
|
|
||
| const org = fallback.orgs.find((entry: any) => entry.id === "org-1"); | ||
| expect( | ||
| org?.profiles.some( | ||
| (profile: any) => | ||
| profile.profileDescription.id === "continuedev/team-assistant" && | ||
| profile.profileDescription.title === "Org Assistant", | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,11 @@ import { | |
| IdeSettings, | ||
| ILLMLogger, | ||
| } from "../index.js"; | ||
| import { GlobalContext } from "../util/GlobalContext.js"; | ||
| import { | ||
| CachedHubAssistant, | ||
| CachedHubOrganization, | ||
| GlobalContext, | ||
| } from "../util/GlobalContext.js"; | ||
| import { getConfigYamlPath } from "../util/paths.js"; | ||
|
|
||
| import EventEmitter from "node:events"; | ||
|
|
@@ -176,30 +180,71 @@ export class ConfigHandler { | |
| const isSignedIn = await this.controlPlaneClient.isSignedIn(); | ||
| if (isSignedIn) { | ||
| try { | ||
| let usedCachedHubData = false; | ||
|
|
||
| // TODO use policy returned with org, not policy endpoint | ||
| const policyResponse = await this.controlPlaneClient.getPolicy(); | ||
| PolicySingleton.getInstance().policy = policyResponse; | ||
|
|
||
| const cachedHubOrganizations = this.getCachedHubOrganizations(); | ||
| const orgDescriptions = | ||
| await this.controlPlaneClient.listOrganizations(); | ||
| const orgsWithPolicy = orgDescriptions.map((d) => ({ | ||
| ...d, | ||
| policy: policyResponse?.policy, | ||
| })); | ||
| const orgsWithPolicy = (orgDescriptions ?? cachedHubOrganizations) | ||
| .filter((org) => org.id !== this.PERSONAL_ORG_DESC.id) | ||
|
Comment on lines
+189
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| .map((d) => ({ | ||
| iconUrl: d.iconUrl, | ||
| id: d.id, | ||
| name: d.name, | ||
| slug: d.slug, | ||
| policy: policyResponse?.policy, | ||
| })); | ||
|
|
||
| if (orgDescriptions === null && cachedHubOrganizations.length > 0) { | ||
| usedCachedHubData = true; | ||
| } | ||
|
|
||
| if (policyResponse?.policy?.allowOtherOrgs === false) { | ||
| if (orgsWithPolicy.length === 0) { | ||
| return { orgs: [] }; | ||
| } else { | ||
| const firstOrg = await this.getNonPersonalHubOrg(orgsWithPolicy[0]); | ||
| return { orgs: [firstOrg] }; | ||
| if (firstOrg.usedCache) { | ||
| usedCachedHubData = true; | ||
| } | ||
| if (usedCachedHubData) { | ||
| errors.push({ | ||
| fatal: false, | ||
| message: | ||
| "Continue Hub is unavailable. Using cached Hub assistants until the service recovers.", | ||
| }); | ||
| } | ||
| return { orgs: [firstOrg.org], errors }; | ||
| } | ||
| } | ||
| const orgs = await Promise.all([ | ||
| this.getPersonalHubOrg(), | ||
| ...orgsWithPolicy.map((org) => this.getNonPersonalHubOrg(org)), | ||
| ]); | ||
|
|
||
| const personalOrg = await this.getPersonalHubOrg(); | ||
| const nonPersonalOrgs = await Promise.all( | ||
| orgsWithPolicy.map((org) => this.getNonPersonalHubOrg(org)), | ||
| ); | ||
| if ( | ||
| personalOrg.usedCache || | ||
| nonPersonalOrgs.some((org) => org.usedCache) | ||
| ) { | ||
| usedCachedHubData = true; | ||
| } | ||
| if (usedCachedHubData) { | ||
| errors.push({ | ||
| fatal: false, | ||
| message: | ||
| "Continue Hub is unavailable. Using cached Hub assistants until the service recovers.", | ||
| }); | ||
| } | ||
| const orgs = [ | ||
| personalOrg.org, | ||
| ...nonPersonalOrgs.map((org) => org.org), | ||
| ]; | ||
| // TODO make try/catch more granular here, to catch specific org errors | ||
| return { orgs }; | ||
| return { orgs, errors }; | ||
| } catch (e) { | ||
| errors.push({ | ||
| fatal: false, | ||
|
|
@@ -236,9 +281,21 @@ export class ConfigHandler { | |
| })); | ||
| } | ||
|
|
||
| private async getHubProfiles(orgScopeId: string | null) { | ||
| const assistants = await this.controlPlaneClient.listAssistants(orgScopeId); | ||
| private getCachedHubOrganizations(): CachedHubOrganization[] { | ||
| return this.globalContext.get("cachedHubOrganizations") ?? []; | ||
| } | ||
|
|
||
| private updateCachedHubOrganization(org: CachedHubOrganization) { | ||
| const cached = this.getCachedHubOrganizations().filter( | ||
| (existing) => existing.id !== org.id, | ||
| ); | ||
| this.globalContext.update("cachedHubOrganizations", [...cached, org]); | ||
| } | ||
|
|
||
| private async createPlatformProfiles( | ||
| assistants: CachedHubAssistant[], | ||
| orgScopeId: string | null, | ||
| ) { | ||
| return await Promise.all( | ||
| assistants.map(async (assistant) => { | ||
| const profileLoader = await PlatformProfileLoader.create({ | ||
|
|
@@ -254,23 +311,58 @@ export class ConfigHandler { | |
| ide: this.ide, | ||
| llmLogger: this.llmLogger, | ||
| rawYaml: assistant.rawYaml, | ||
| orgScopeId: orgScopeId, | ||
| orgScopeId, | ||
| }); | ||
|
|
||
| return new ProfileLifecycleManager(profileLoader, this.ide); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| private async loadHubProfiles( | ||
| orgScopeId: string | null, | ||
| org: OrganizationDescription, | ||
| ): Promise<{ profiles: ProfileLifecycleManager[]; usedCache: boolean }> { | ||
| const assistants = await this.controlPlaneClient.listAssistants(orgScopeId); | ||
| if (assistants !== null) { | ||
| this.updateCachedHubOrganization({ | ||
| id: org.id, | ||
| iconUrl: org.iconUrl, | ||
| name: org.name, | ||
| slug: org.slug, | ||
| profiles: assistants, | ||
| }); | ||
| return { | ||
| profiles: await this.createPlatformProfiles(assistants, orgScopeId), | ||
| usedCache: false, | ||
| }; | ||
| } | ||
|
|
||
| const cachedProfiles = | ||
| this.getCachedHubOrganizations().find((entry) => entry.id === org.id) | ||
| ?.profiles ?? []; | ||
| return { | ||
| profiles: await this.createPlatformProfiles(cachedProfiles, orgScopeId), | ||
| usedCache: cachedProfiles.length > 0, | ||
| }; | ||
| } | ||
|
|
||
| private async getNonPersonalHubOrg( | ||
| org: OrganizationDescription, | ||
| ): Promise<OrgWithProfiles> { | ||
| ): Promise<{ org: OrgWithProfiles; usedCache: boolean }> { | ||
| const localProfiles = await this.getLocalProfiles({ | ||
| includeGlobal: false, | ||
| includeWorkspace: true, | ||
| }); | ||
| const profiles = [...(await this.getHubProfiles(org.id)), ...localProfiles]; | ||
| return this.rectifyProfilesForOrg(org, profiles); | ||
| const { profiles: hubProfiles, usedCache } = await this.loadHubProfiles( | ||
| org.id, | ||
| org, | ||
| ); | ||
| const profiles = [...hubProfiles, ...localProfiles]; | ||
| return { | ||
| org: await this.rectifyProfilesForOrg(org, profiles), | ||
| usedCache, | ||
| }; | ||
| } | ||
|
|
||
| private PERSONAL_ORG_DESC: OrganizationDescription = { | ||
|
|
@@ -279,14 +371,23 @@ export class ConfigHandler { | |
| name: "Personal", | ||
| slug: undefined, | ||
| }; | ||
| private async getPersonalHubOrg() { | ||
| private async getPersonalHubOrg(): Promise<{ | ||
| org: OrgWithProfiles; | ||
| usedCache: boolean; | ||
| }> { | ||
| const localProfiles = await this.getLocalProfiles({ | ||
| includeGlobal: true, | ||
| includeWorkspace: true, | ||
| }); | ||
| const hubProfiles = await this.getHubProfiles(null); | ||
| const { profiles: hubProfiles, usedCache } = await this.loadHubProfiles( | ||
| null, | ||
| this.PERSONAL_ORG_DESC, | ||
| ); | ||
| const profiles = [...hubProfiles, ...localProfiles]; | ||
| return this.rectifyProfilesForOrg(this.PERSONAL_ORG_DESC, profiles); | ||
| return { | ||
| org: await this.rectifyProfilesForOrg(this.PERSONAL_ORG_DESC, profiles), | ||
| usedCache, | ||
| }; | ||
| } | ||
|
|
||
| private async getLocalOrg() { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Hub fallback cache is not scoped or cleared per account, so unavailable Hub responses can expose stale orgs/assistants after account switch.
Prompt for AI agents