Review findings on #2509, all confirmed: - Every writer of .credentials.yaml now waits out the record-mutation lock (DOCUMENT_LOCK_WAIT_MS): refs and records share one file and one lock, so a reference write or record delete contending with an OAuth refresh must not fail at the 2s file-work default. - api-key records are admitted before they are rendered: an empty key, a non-POSIX env name, or an empty env value is refused at the write instead of persisting a document the next boot rejects wholesale. - llm-pi-ai no longer lets the credential-key grammar reject legal route ids: reads answer "nothing stored" via isCredentialKeySegment (new dsh-credentials export), deletes have nothing to remove, and only a write refuses, as LlmError UNSTORABLE_PROVIDER_ID; flow registration skips a future catalog id outside the grammar instead of failing the mount. - authorization/settled fans out with contained listener failures on the credentials seam's terms (INVARIANT still rethrows), so a broken watcher can never turn a finished attempt into a failure. - notify() is fire-and-forget at the seam: a surface that cannot render a notice loses the notice, never the attempt. - A declined prompt is an outcome: interactions reject with the new AuthorizationDeclinedError and the attempt settles cancelled instead of failed. - NOT_COMMITTED now confirms a commit observed during the attempt (credentials/record-updated for the flow's key), so a re-auth cannot pass a stale record off as fresh; a flow that deletes its record is refused on the same code. READMEs, the subsystem/event/config catalogs, and the Agent Note follow the shipped behavior; memory.ts carries the dedup TODO.
208 lines
8.7 KiB
TypeScript
208 lines
8.7 KiB
TypeScript
/**
|
|
* The three adapters between pi-ai's auth model and the harness credential
|
|
* plane. Every pi-ai-specific concept stays on this side of them: the harness
|
|
* seams they consume — `ctx.credentials` records and `ctx.authorization` flows —
|
|
* name nothing from this library, so another adapter family can arrive with a
|
|
* different auth model and share the same two seams.
|
|
*
|
|
* @module dsh-llm-pi-ai/auth
|
|
*/
|
|
|
|
import { homedir } from 'node:os'
|
|
import { access } from 'node:fs/promises'
|
|
import { resolve as resolvePath } from 'node:path'
|
|
import type { AuthContext, Credential, CredentialInfo, CredentialStore } from '@earendil-works/pi-ai'
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
import {
|
|
credentialKey, credentialKeyId, credentialKeyScope, credentialRef, isCredentialKeySegment, isCredentialRefName,
|
|
} from '@deepseek-ai/dsh-credentials'
|
|
import type { CredentialKey, CredentialProvider, CredentialRecord } from '@deepseek-ai/dsh-credentials'
|
|
import { launchEnvironmentOf } from '@deepseek-ai/dsh-launch-environment'
|
|
import { LlmError } from '@deepseek-ai/dsh-llm'
|
|
|
|
/**
|
|
* The record scope every credential this adapter family stores is written
|
|
* under. It is the plugin's registered name, which is what tells a later
|
|
* reader — a configuration UI, or a second adapter family serving the same
|
|
* provider name — that this plugin owns the format inside the record.
|
|
*/
|
|
export const RECORD_SCOPE = 'llm-pi-ai'
|
|
|
|
/**
|
|
* The record address for one pi-ai provider id.
|
|
* @param providerId - pi-ai's own provider id, which is also the harness route key.
|
|
* @returns the scoped credential key this adapter family reads and writes.
|
|
*/
|
|
export function recordKeyFor(providerId: string): CredentialKey {
|
|
return credentialKey(RECORD_SCOPE, providerId)
|
|
}
|
|
|
|
/**
|
|
* Translate a stored record into the credential pi-ai expects.
|
|
*
|
|
* An `api-key` record is structural on both sides, so it is rebuilt field by
|
|
* field. A `grant` payload is pi-ai's own OAuth credential, stored verbatim:
|
|
* the seam treats it as opaque JSON precisely so a library that owns a token
|
|
* format keeps owning it, refresh fields and all.
|
|
* @param record - the stored record, or undefined when nothing is stored.
|
|
* @returns the pi-ai credential, or undefined for an absent record.
|
|
*/
|
|
function toPiCredential(record: CredentialRecord | undefined): Credential | undefined {
|
|
if (record === undefined) return undefined
|
|
if (record.kind === 'api-key') {
|
|
return {
|
|
type: 'api_key',
|
|
...record.key === undefined ? {} : { key: record.key },
|
|
...record.env === undefined ? {} : { env: { ...record.env } },
|
|
}
|
|
}
|
|
return record.payload as Credential
|
|
}
|
|
|
|
/**
|
|
* Translate a pi-ai credential into the record to store.
|
|
* @param credential - what a login or refresh produced.
|
|
* @returns the record to commit, in the union the credential seam stores.
|
|
*/
|
|
function toRecord(credential: Credential): CredentialRecord {
|
|
if (credential.type === 'api_key') {
|
|
return {
|
|
kind: 'api-key',
|
|
...credential.key === undefined ? {} : { key: credential.key },
|
|
...credential.env === undefined ? {} : { env: { ...credential.env } },
|
|
}
|
|
}
|
|
return { kind: 'grant', payload: credential }
|
|
}
|
|
|
|
/**
|
|
* The credential service, or the failure that names what is missing. Reads
|
|
* answer "nothing stored" without a service, because a composition with no
|
|
* credential plane genuinely holds no credential; writes refuse, because a
|
|
* login whose grant silently evaporated would report success and then fail
|
|
* every request.
|
|
* @param ctx - the plugin context.
|
|
* @returns the live service.
|
|
* @throws {LlmError} code `NO_CREDENTIAL_STORE` when none is mounted.
|
|
*/
|
|
function writableStore(ctx: Context): CredentialProvider {
|
|
const credentials = ctx.get('credentials')
|
|
if (credentials === undefined) {
|
|
throw new LlmError(
|
|
'llm-pi-ai: this composition mounts no credentials service, so there is nowhere to store the'
|
|
+ ' credential a sign-in produces; mount one (dsh-credentials-local) to sign in',
|
|
'NO_CREDENTIAL_STORE',
|
|
)
|
|
}
|
|
return credentials
|
|
}
|
|
|
|
/**
|
|
* A pi-ai `CredentialStore` over the harness credential records.
|
|
*
|
|
* pi-ai runs OAuth refresh *inside* `modify()`, so this store's exclusion has
|
|
* to cover a network round trip rather than a file rename — which is why the
|
|
* record write path takes a wait limit of its own rather than the short one a
|
|
* local write would need.
|
|
*
|
|
* pi-ai asks this store about every provider in the collection, hand-declared
|
|
* routes included, and a route key is an arbitrary settings dict key while a
|
|
* record id is not. An id outside the record grammar can never have stored a
|
|
* record, so reads answer "nothing stored" and a delete has nothing to remove;
|
|
* only `modify` refuses it, because a write that cannot land must not report
|
|
* that it did.
|
|
* @param ctx - the plugin context carrying the optional `ctx.credentials`.
|
|
* @returns the store to hand `createModels()`.
|
|
*/
|
|
export function credentialStoreFrom(ctx: Context): CredentialStore {
|
|
return {
|
|
async read(providerId) {
|
|
const credentials = ctx.get('credentials')
|
|
if (credentials === undefined) return undefined
|
|
if (!isCredentialKeySegment(providerId)) return undefined
|
|
return toPiCredential(await credentials.readRecord(recordKeyFor(providerId)))
|
|
},
|
|
async list(): Promise<readonly CredentialInfo[]> {
|
|
const stored = await ctx.get('credentials')?.listRecords() ?? []
|
|
const mine: CredentialInfo[] = []
|
|
for (const entry of stored) {
|
|
// Records another plugin owns are not this collection's to report:
|
|
// their payloads are written in a format pi-ai never agreed to.
|
|
if (credentialKeyScope(entry.key) !== RECORD_SCOPE) continue
|
|
mine.push({
|
|
providerId: credentialKeyId(entry.key),
|
|
type: entry.kind === 'api-key' ? 'api_key' : 'oauth',
|
|
})
|
|
}
|
|
return mine
|
|
},
|
|
async modify(providerId, mutate) {
|
|
if (!isCredentialKeySegment(providerId)) {
|
|
throw new LlmError(
|
|
`llm-pi-ai: provider id "${providerId}" cannot address a stored credential record (a record id is a`
|
|
+ ' lowercase hyphenated identifier); authenticate this route through apiKeyEnv instead of a stored'
|
|
+ ' credential',
|
|
'UNSTORABLE_PROVIDER_ID',
|
|
)
|
|
}
|
|
const stored = await writableStore(ctx).modifyRecord(recordKeyFor(providerId), async (current) => {
|
|
const next = await mutate(toPiCredential(current))
|
|
return next === undefined ? undefined : toRecord(next)
|
|
})
|
|
return toPiCredential(stored)
|
|
},
|
|
// `async` so a missing service reaches the caller as a rejection: pi-ai's
|
|
// store contract is promise-returning, and a synchronous throw would
|
|
// escape the `ModelsError` wrapper every other storage failure gets.
|
|
async delete(providerId) {
|
|
if (!isCredentialKeySegment(providerId)) return
|
|
await writableStore(ctx).deleteRecord(recordKeyFor(providerId))
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A pi-ai `AuthContext` over the harness credential plane and the host
|
|
* filesystem.
|
|
*
|
|
* `env()` answers from the credential seam first, so a value a deployment
|
|
* stored through the harness is found by a provider's own ambient discovery —
|
|
* without this, that discovery reads only the process environment and a stored
|
|
* `AWS_ACCESS_KEY_ID` is invisible to it. `fileExists()` answers about the host
|
|
* process's own filesystem rather than the workspace `ctx.fs` seam, because the
|
|
* paths it is asked about (`~/.aws/credentials`, application-default
|
|
* credentials) are facts about where this process runs, not about the project
|
|
* under edit.
|
|
* @param ctx - the plugin context carrying the optional `ctx.credentials`.
|
|
* @returns the auth context to hand `createModels()`.
|
|
*/
|
|
export function authContextFrom(ctx: Context): AuthContext {
|
|
return {
|
|
async env(name) {
|
|
// pi-ai asks about arbitrary provider-declared names; one that is not a
|
|
// POSIX identifier can never have been stored as a reference, and asking
|
|
// the seam would throw instead of answering "not set".
|
|
if (isCredentialRefName(name)) {
|
|
const credentials = ctx.get('credentials')
|
|
const hit = await credentials?.resolve(credentialRef(name))
|
|
if (hit !== undefined) return hit.value
|
|
}
|
|
return launchEnvironmentOf(ctx).get(name)?.value
|
|
},
|
|
async fileExists(path) {
|
|
const expanded = path.startsWith('~/') || path === '~'
|
|
? resolvePath(homedir(), path.slice(1).replace(/^\//, ''))
|
|
: path
|
|
try {
|
|
await access(expanded)
|
|
return true
|
|
} catch {
|
|
// Absent, unreadable, or a broken symlink — every one of which means
|
|
// this ambient credential source cannot be used, which is the only
|
|
// distinction the caller makes.
|
|
return false
|
|
}
|
|
},
|
|
}
|
|
}
|