-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli,db): 2.C — provider/key commands (OS keychain via @napi-rs/keyring) #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
223fd66
docs(roadmap): mark 2.H Done (PR #44) across the status surfaces — ne…
cemililik 5aac708
feat(cli,db): 2.C — provider/key commands (OS keychain via @napi-rs/k…
cemililik 6caf969
fix(cli): 2.C reviewer follow-ups (set-key base-url, stdin TTY guard,…
cemililik 8ae794c
fix(db,cli): 2.C second review round — defaultHeaders double-encode, …
cemililik 9f6a0e2
fix(cli): 2.C third review round — idempotent db close + commands.md …
cemililik 5a6e883
fix(cli,db): 2.C multi-dimensional review — HTTPS-only base-url, type…
cemililik d3d1d53
fix(cli): 2.C review follow-ups — stdin chunk type-guard + @napi-rs/k…
cemililik 14fbe76
chore: narrow over-broad Read(**/secrets/**) deny so TS source module…
cemililik 8fff501
fix(cli,db): 2.C review round — close db handle on setup failure + te…
cemililik d1eef7b
chore: drop machine-specific absolute path from settings allow-list
cemililik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| import { createClient, createProviderStore, runMigrations, type DbClient } from '@relavium/db'; | ||
| import type { LlmProvider } from '@relavium/llm'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { | ||
| createProviderResolver, | ||
| keyHint, | ||
| providerKeyEnvVar, | ||
| type ProviderResolver, | ||
| } from '../engine/providers.js'; | ||
| import { KeychainUnavailableError, type KeychainStore } from '../secrets/keychain.js'; | ||
| import { readSecretFromStdin } from '../secrets/read-secret.js'; | ||
| import { captureIo } from '../test-support.js'; | ||
| import { runProviderCommand, type ProviderCommandDeps } from './provider.js'; | ||
|
|
||
| const TS_MS = new Date('2026-06-23T12:00:00.000Z').getTime(); | ||
| // A fake key built from fragments so no contiguous secret literal exists (Leakwatch convention). | ||
| const RAW_KEY = ['sk', 'ant', 'FAKEKEY', '9999'].join('-'); | ||
|
|
||
| /** A mutable in-memory keychain — the production `@napi-rs/keyring` accessor is never loaded by these tests. */ | ||
| function memKeychain( | ||
| seed: Record<string, string> = {}, | ||
| ): KeychainStore & { map: Map<string, string> } { | ||
| const map = new Map(Object.entries(seed)); | ||
| return { | ||
| map, | ||
| get: (account) => map.get(account) ?? null, | ||
| set: (account, secret) => { | ||
| map.set(account, secret); | ||
| }, | ||
| delete: (account) => map.delete(account), | ||
| }; | ||
| } | ||
|
|
||
| /** A stub resolver for `provider test` — `generate` resolves or rejects; `keyFor` returns a fixed key. */ | ||
| function stubResolver(generate: LlmProvider['generate']): ProviderResolver { | ||
| const provider: LlmProvider = { | ||
| id: 'anthropic', | ||
| generate, | ||
| stream: () => { | ||
| throw new Error('stream not used in provider test'); | ||
| }, | ||
| // `provider test` only calls `generate`; `supports` is never read here. A real CapabilityFlags fixture | ||
| // would have to satisfy the schema's `vision === media.input.image` refine (+ the nested media shape) and | ||
| // would couple this test to that evolving seam schema for zero assertion value — hence a bare stub cast. | ||
| supports: {} as LlmProvider['supports'], | ||
| }; | ||
| return { resolveProvider: () => provider, keyFor: () => RAW_KEY }; | ||
| } | ||
|
|
||
| describe('relavium provider commands (2.C)', () => { | ||
| let client: DbClient; | ||
| let deps: (over: Partial<ProviderCommandDeps>) => ProviderCommandDeps; | ||
| let io: ReturnType<typeof captureIo>; | ||
|
|
||
| beforeEach(() => { | ||
| client = createClient(':memory:'); | ||
| runMigrations(client.db); | ||
| io = captureIo(); | ||
| let next = 0; | ||
| const store = createProviderStore(client.db, { | ||
| uuid: () => `00000000-0000-4000-8000-${String(++next).padStart(12, '0')}`, | ||
| now: () => TS_MS, | ||
| }); | ||
| deps = (over) => ({ | ||
| io: io.io, | ||
| store, | ||
| keychain: memKeychain(), | ||
| resolver: stubResolver(() => | ||
| Promise.resolve({} as Awaited<ReturnType<LlmProvider['generate']>>), | ||
| ), | ||
| readSecret: () => Promise.resolve(RAW_KEY), | ||
| ...over, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| client.sqlite.close(); | ||
| }); | ||
|
|
||
| it('set-key stores the key in the keychain + only a ref in the db — never the key value', async () => { | ||
| const keychain = memKeychain(); | ||
| const d = deps({ keychain }); | ||
| const code = await runProviderCommand({ action: 'set-key', name: 'anthropic' }, d); | ||
| expect(code).toBe(0); | ||
| expect(keychain.get('anthropic:default')).toBe(RAW_KEY); // raw key in the keychain only | ||
| const rec = d.store.get('anthropic'); | ||
| expect(rec?.apiKeyKeychainRef).toBe('anthropic:default'); // db row holds the ref | ||
| expect(JSON.stringify(rec)).not.toContain(RAW_KEY); // ...never the key | ||
| expect(io.out()).toContain('••••9999'); // a hint, never the full key | ||
| expect(io.out()).not.toContain(RAW_KEY); | ||
| }); | ||
|
|
||
| it('list shows registered providers and whether a key is set (no key echoed)', async () => { | ||
| const keychain = memKeychain(); | ||
| const d = deps({ keychain }); | ||
| await runProviderCommand({ action: 'set-key', name: 'anthropic' }, d); | ||
| await runProviderCommand({ action: 'add', name: 'openai' }, d); | ||
| io.out(); // (accumulates; assert on the final list call) | ||
| await runProviderCommand({ action: 'list' }, d); | ||
| const text = io.out(); | ||
| expect(text).toContain('anthropic'); | ||
| expect(text).toContain('[key set]'); | ||
| expect(text).toContain('openai'); | ||
| expect(text).toContain('[no key]'); | ||
| expect(text).not.toContain(RAW_KEY); | ||
| }); | ||
|
|
||
| it('remove-key deletes the keychain entry and clears the db ref', async () => { | ||
| const keychain = memKeychain(); | ||
| const d = deps({ keychain }); | ||
| await runProviderCommand({ action: 'set-key', name: 'anthropic' }, d); | ||
| const code = await runProviderCommand({ action: 'remove-key', name: 'anthropic' }, d); | ||
| expect(code).toBe(0); | ||
| expect(keychain.get('anthropic:default')).toBeNull(); | ||
| expect(d.store.get('anthropic')?.apiKeyKeychainRef).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('test reports success when the provider generate resolves', async () => { | ||
| const d = deps({ | ||
| resolver: stubResolver(() => | ||
| Promise.resolve({} as Awaited<ReturnType<LlmProvider['generate']>>), | ||
| ), | ||
| }); | ||
| const code = await runProviderCommand({ action: 'test', name: 'anthropic' }, d); | ||
| expect(code).toBe(0); | ||
| expect(io.out()).toContain('key works'); | ||
| }); | ||
|
|
||
| it('test fails cleanly (exit 2) and redacts the key even if the provider error contains it', async () => { | ||
| // The provider error embeds the raw key — the CLI must redact it (the key is in scope at the catch). | ||
| const d = deps({ | ||
| resolver: stubResolver(() => | ||
| Promise.reject(new Error(`401 unauthorized: ${RAW_KEY} rejected`)), | ||
| ), | ||
| }); | ||
| let caught: unknown; | ||
| try { | ||
| await runProviderCommand({ action: 'test', name: 'anthropic' }, d); | ||
| } catch (err) { | ||
| caught = err; | ||
| } | ||
| expect(caught).toMatchObject({ exitCode: 2 }); | ||
| expect(caught).toBeInstanceOf(Error); | ||
| const message = caught instanceof Error ? caught.message : String(caught); | ||
| expect(message).not.toContain(RAW_KEY); // redacted... | ||
| expect(message).toContain('••••9999'); // ...to the hint | ||
| }); | ||
|
|
||
| it('rejects a non-HTTPS or malformed --base-url (exit 2)', async () => { | ||
| // HTTPS-only at store time — a plaintext `http:` endpoint must never be persisted, and a | ||
| // non-`http(s)` scheme is rejected outright (matches the routing-time `assertHttpsBaseUrl` gate). | ||
| for (const baseUrl of ['http://proxy.example/v1', 'file:///etc/passwd', 'not-a-url']) { | ||
| await expect( | ||
| runProviderCommand({ action: 'add', name: 'openai', baseUrl }, deps({})), | ||
| ).rejects.toMatchObject({ exitCode: 2 }); | ||
| } | ||
| }); | ||
|
|
||
| it('set-key preserves a base URL set by a prior `add` (never clobbers it)', async () => { | ||
| const d = deps({}); | ||
| await runProviderCommand( | ||
| { action: 'add', name: 'openai', baseUrl: 'https://proxy.example/v1' }, | ||
| d, | ||
| ); | ||
| await runProviderCommand({ action: 'set-key', name: 'openai' }, d); | ||
| expect(d.store.get('openai')?.baseUrl).toBe('https://proxy.example/v1'); // not the SDK default | ||
| }); | ||
|
|
||
| it('rejects an unknown provider name (exit 2)', async () => { | ||
| await expect( | ||
| runProviderCommand({ action: 'add', name: 'bogus' }, deps({})), | ||
| ).rejects.toMatchObject({ exitCode: 2 }); | ||
| }); | ||
|
|
||
| it('maps a keychain-unavailable backend to a clean exit-2 message', async () => { | ||
| const keychain: KeychainStore = { | ||
| get: () => null, | ||
| set: () => { | ||
| throw new KeychainUnavailableError('no Secret Service'); | ||
| }, | ||
| delete: () => false, | ||
| }; | ||
| await expect( | ||
| runProviderCommand({ action: 'set-key', name: 'anthropic' }, deps({ keychain })), | ||
| ).rejects.toMatchObject({ exitCode: 2 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createProviderResolver keyFor — keychain → env var → error (2.C)', () => { | ||
| it('prefers the OS keychain', () => { | ||
| const keychain = memKeychain({ 'anthropic:default': 'from-keychain' }); | ||
| const resolver = createProviderResolver( | ||
| { [providerKeyEnvVar('anthropic')]: 'from-env' }, | ||
| keychain, | ||
| ); | ||
| expect(resolver.keyFor('anthropic')).toBe('from-keychain'); | ||
| }); | ||
|
|
||
| it('falls back to the env var when the keychain has no entry', () => { | ||
| const resolver = createProviderResolver( | ||
| { [providerKeyEnvVar('openai')]: 'from-env' }, | ||
| memKeychain(), | ||
| ); | ||
| expect(resolver.keyFor('openai')).toBe('from-env'); | ||
| }); | ||
|
|
||
| it('falls through to the env var when the keychain backend is unavailable', () => { | ||
| const keychain: KeychainStore = { | ||
| get: () => { | ||
| throw new KeychainUnavailableError('locked'); | ||
| }, | ||
| set: () => undefined, | ||
| delete: () => false, | ||
| }; | ||
| const resolver = createProviderResolver( | ||
| { [providerKeyEnvVar('gemini')]: 'from-env' }, | ||
| keychain, | ||
| ); | ||
| expect(resolver.keyFor('gemini')).toBe('from-env'); | ||
| }); | ||
|
|
||
| it('throws a clean invocation error (exit 2) when no source has a key', () => { | ||
| const resolver = createProviderResolver({}, memKeychain()); | ||
| expect(() => resolver.keyFor('deepseek')).toThrowError(/no API key for provider 'deepseek'/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('keyHint', () => { | ||
| it('shows only the last 4 chars, never the full key', () => { | ||
| expect(keyHint(RAW_KEY)).toBe('••••9999'); | ||
| expect(keyHint(RAW_KEY)).not.toContain('sk-ant'); | ||
| }); | ||
| it('fully masks a too-short value', () => { | ||
| expect(keyHint('abc')).toBe('••••'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('readSecretFromStdin', () => { | ||
| it('refuses to read a typed key from an interactive TTY (errors with a pipe hint, exit 2)', async () => { | ||
| // Capture the FULL original descriptor so the restore reinstates it exactly (a value-only restore would | ||
| // leave a synthetic data property where there may have been an accessor / a different configurability). | ||
| const original = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY'); | ||
| // A typed secret on an echoing terminal is the failure mode the stdin guard prevents — even if stdout | ||
| // is redirected (the guard keys on stdin, not stdout). | ||
| Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true }); | ||
| try { | ||
| await expect(readSecretFromStdin()).rejects.toMatchObject({ exitCode: 2 }); | ||
| } finally { | ||
| if (original === undefined) { | ||
| delete (process.stdin as { isTTY?: boolean }).isTTY; | ||
| } else { | ||
| Object.defineProperty(process.stdin, 'isTTY', original); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
📐 Maintainability & Code Quality | 🟠 Major
🧩 Analysis chain
🏁 Script executed:
Repository: HodeTech/Relavium
Length of output: 1596
🏁 Script executed:
Repository: HodeTech/Relavium
Length of output: 4531
🏁 Script executed:
Repository: HodeTech/Relavium
Length of output: 457
🏁 Script executed:
Repository: HodeTech/Relavium
Length of output: 1111
🏁 Script executed:
Repository: HodeTech/Relavium
Length of output: 2993
Remove unsafe
ascasts from test doubles and error assertions.Lines 43, 67, 119, and 141-142 bypass strict typing with
ascasts:supports: {} as LlmProvider['supports']— empty object cannot satisfyCapabilityFlags, which requirestools,streaming,parallelToolCalls,vision,promptCache,reasoningbooleans and a complex nestedmediaobject with a refine constraint.{} as Awaited<ReturnType<LlmProvider['generate']>>— empty objects bypassLlmResultstructure requirements.(caught as Error).message— unsafe type assertion on a value declared asunknown. Use a type guard (instanceof Error) or property check instead.Per coding guidelines,
**/*.{ts,tsx}must use strict TypeScript with no unsafeascasts. Create properly typed test fixtures (e.g.,Partial<CapabilityFlags>,Partial<LlmResult>) or use type guards for runtime checks.🧰 Tools
🪛 ESLint
[error] 43-43: Unsafe assignment of an error typed value.
(
@typescript-eslint/no-unsafe-assignment)🤖 Prompt for AI Agents
Sources: Coding guidelines, Linters/SAST tools