Repository navigation
fix(provider): a blank Altimate key is not a sign-in #1835
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
Changes from all commits
081695a
18874d6
d344569
b2c450b
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 |
|---|---|---|
|
|
@@ -18,10 +18,14 @@ const DEFAULT_MCP_URL = "https://mcpserver.getaltimate.com/sse" | |
| const DEFAULT_ALTIMATE_URL = "https://api.myaltimate.com" | ||
| // altimate_change end | ||
|
|
||
| // A blank field is not a sign-in. Accepted, a blank key loaded the gateway provider as connected, | ||
| // so a message went to the gateway with no Authorization header and got 403 "Not authenticated". | ||
| const CredentialField = z.string().refine((value) => value.trim() !== "", "must not be blank") | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
Collaborator
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. Minor: a blank field now makes the workspace overlay remove the user's own This was raised by Codex and reproduced live.
The test used a stdio MCP server registered as
This doesn't block. It is edge input: terminal use, plus a A follow-up could treat a file whose fields are blank as "no account" in the workspace-binding read (unbound, as on main) and keep the
Collaborator
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. Minor: for a blank file, the error text is now a raw ZodError JSON.
The overlay warning above shows the same JSON. Neither message says to sign in. This is cosmetic, and R1 covers the substance. Mapping a validation failure from |
||
|
|
||
| const AltimateCredentials = z.object({ | ||
| altimateUrl: z.string(), | ||
| altimateInstanceName: z.string(), | ||
| altimateApiKey: z.string(), | ||
| altimateUrl: CredentialField, | ||
| altimateInstanceName: CredentialField, | ||
| altimateApiKey: CredentialField, | ||
| mcpServerUrl: z.string().optional(), | ||
| }) | ||
| type AltimateCredentials = z.infer<typeof AltimateCredentials> | ||
|
|
@@ -183,20 +187,38 @@ export namespace AltimateApi { | |
| } | ||
| } | ||
|
|
||
| /** The key to match MCP entries by on sign-out, read as written. Unlike `getCredentials()` it accepts | ||
| * a file whose fields are blank or missing: such a file holds no key to protect, so sign-out can | ||
| * clear it. Undefined when there is no key; throws when one may exist but can't be read. Only the | ||
| * key is substituted, so an unset `${env:…}` in another field cannot keep a keyless file. */ | ||
| async function savedKeyForSignOut(): Promise<string | undefined> { | ||
| const scoped = currentCredentials() | ||
| if (scoped) return scoped.altimateApiKey | ||
| const raw: unknown = JSON.parse(await Filesystem.readText(credentialsPath())) | ||
| if (!raw || typeof raw !== "object" || Array.isArray(raw)) | ||
| throw new Error("the file does not hold the credentials object.") | ||
| const written = (raw as { altimateApiKey?: unknown }).altimateApiKey | ||
| if (written === undefined) return undefined | ||
| if (typeof written !== "string") throw new Error("altimateApiKey is not a string.") | ||
| const key = resolveEnvVars(written) as string | ||
| return key.trim() === "" ? undefined : key | ||
| } | ||
|
|
||
| /** Sign out of the gateway: remove MCP entries that carry the saved key, then delete the credential file. | ||
| * Standalone `datamate add` writes the key into MCP headers (`buildMcpConfig`); matching the exact key | ||
| * leaves user-authored entries alone. Searches the global config and every project config a session in | ||
| * `projectDir` loads. Fails without deleting the file when the key can't be read: the file holds the | ||
| * only copy, and without it leftover MCP entries can never be matched. */ | ||
| * only copy, and without it leftover MCP entries can never be matched. A file with no key, or a blank | ||
| * one, is simply cleared. */ | ||
| export async function signOut(projectDir: string): Promise<Array<{ name: string; path: string }>> { | ||
| if (!(await isConfigured())) { | ||
| await withdrawClaudeSkills(projectDir) | ||
| await forgetTelemetryIdentity() | ||
| return [] | ||
| } | ||
| let key: string | ||
| let key: string | undefined | ||
| try { | ||
| key = (await getCredentials()).altimateApiKey | ||
| key = await savedKeyForSignOut() | ||
| } catch (err) { | ||
| throw new Error( | ||
| `could not read the saved key from ${credentialsPath()} (${err instanceof Error ? err.message : err}); ` + | ||
|
|
@@ -208,7 +230,7 @@ export namespace AltimateApi { | |
| ...(await findGlobalConfigPaths(Global.Path.config)), | ||
| ] | ||
| const removed: Array<{ name: string; path: string }> = [] | ||
| for (const configPath of configPaths) { | ||
| for (const configPath of key ? configPaths : []) { | ||
| for (const name of await listMcpInConfig(configPath)) { | ||
| const entry = await readMcpEntryFromDisk(name, configPath) | ||
| if (entry?.type !== "remote" || entry.headers?.Authorization !== `Bearer ${key}`) continue | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** The browser sign-in from a plain shell: the Altimate LLM Gateway method of the `altimate-backend` | ||
| * provider saves the credentials every workspace command reads. `--method` skips the method picker, | ||
| * so the command also works where stdin is not a terminal (an agent's shell, Claude Code's `!`). | ||
| * Kept apart from the CLI commands so error messages outside the CLI can name it too. */ | ||
| export const SIGN_IN_COMMAND = 'altimate-code auth login -p altimate-backend --method "Altimate LLM Gateway"' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ import { iife } from "@/util/iife" | |
| import type { ProviderID } from "./schema" | ||
| // altimate_change start — translate managed Altimate Base gateway errors | ||
| import { FreeTier } from "@/altimate/free/client" | ||
| import { SIGN_IN_COMMAND } from "@/altimate/api/sign-in" | ||
|
|
||
| const ALTIMATE_GATEWAY_PROVIDER_ID = "altimate-backend" | ||
| // altimate_change end | ||
|
|
||
| export namespace ProviderError { | ||
|
|
@@ -404,6 +407,26 @@ export namespace ProviderError { | |
| } | ||
| } | ||
| // altimate_change end | ||
| // altimate_change start — the Altimate LLM Gateway refusing the key means the user must sign in | ||
| // again. It answers a missing key with 403 "Not authenticated" and a wrong one with 401; credit | ||
| // limits are 402. The gateway's own words stay in the message so error counts keep matching. | ||
| if ( | ||
| String(input.providerID) === ALTIMATE_GATEWAY_PROVIDER_ID && | ||
| (input.error.statusCode === 401 || input.error.statusCode === 403) | ||
| ) { | ||
| return { | ||
| type: "api_error", | ||
| message: | ||
| "Sign in to Altimate again: the Altimate LLM Gateway refused the request because the saved " + | ||
|
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. SUGGESTION: 403 guidance names only the API key, but this repo already maps a gateway 403 to the instance name
Contributor
Author
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. reworded in 18874d6 to say the saved sign-in is missing or no longer valid, so it doesn't point only at the key. on /agents/v1 the 403 comes from the missing bearer, but signing in again fixes either cause. |
||
| `Altimate sign-in is missing or no longer valid. In a terminal, run \`${SIGN_IN_COMMAND}\`. (${m})`, | ||
| statusCode: input.error.statusCode, | ||
| isRetryable: false, | ||
| responseHeaders: input.error.responseHeaders, | ||
| responseBody: capResponseBody(input.error.responseBody), | ||
| metadata: input.error.url ? { url: maskInternalHost(input.error.url) } : undefined, | ||
| } | ||
| } | ||
| // altimate_change end | ||
| // altimate_change start — distinguish the gateway byte cap from context overflow | ||
| // The gateway's fixed request-byte cap is not a context overflow. Retrying compaction can | ||
| // never help when system instructions and tool schemas alone exceed it. | ||
|
|
||
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.
WARNING: Blank-field refusal also breaks sign-out for exactly the file state this PR fixes
Once a blank field throws,
signOut()(lines 195-209) turns anygetCredentials()failure into a hard error. Both sign-out entry points call it — TUI/logout(packages/opencode/src/plugin/tui/altimate/provider-credentials.tsx:90) andaltimate-code auth logout -p altimate-backend(packages/opencode/src/cli/cmd/providers.ts:670) — so a user whosealtimate.jsonalready holdsaltimateApiKey: ""(the state behind #1834) cannot sign out: the file is kept, the TUI only showscould not read the saved key ... fix the file and sign out again, and the CLI aborts beforeauthSvc.remove, so the auth-store entry survives as well. On main that file parsed, sosignOutcleared it. The stated rationale for refusing ("the file holds the only copy") does not apply when the field is blank: there is no key to match MCP entries by, so clearing is still the right outcome. Suggest catchingz.ZodErrorinsignOutand continuing toclearCredentials(), keeping the hard failure only for a file that genuinely cannot be read.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.
good catch, that was a real regression. fixed in 18874d6: sign-out reads the key itself now, so a file with no key or a blank one gets cleared, and it still refuses when a key might exist but can't be read. tests cover blank, whitespace and empty files.