/** * `/model`'s subagent authorization editor, opened with `ctrl-k`. * * The Host entry `subagent-model-selection-settings` — the profile row this * bundle inserts for * `@deepseek-ai/dsh-tool-subagent/model-selection-settings`, and therefore the * namespace name under the adopted generation's entry-id rule — is the only * durable authorization authority, and the generic `ctx.settings` document is * the only way this module reads or writes it. Nothing here reads * `ctx.subagentModelSelection`, records a Session event, or routes a child: * Harness samples the setting for the next composed top-level Session and its * delegation executor enforces the recorded policy. * * The editor is a staged draft against the revision it opened on. `esc` writes * nothing; only Save performs the one mutation, which sets both fields * together under that revision so a concurrent change is refused rather than * merged. * @module dshline/subagent-model-selection */ import type { Context } from '@deepseek-ai/cordis' import type {} from '@deepseek-ai/dsh-settings' import type { SettingsPathOp } from '@deepseek-ai/dsh-settings' import { paint } from '@dshline/renderer' import { modelRouteKey, readModelCatalog } from '../model-catalog.ts' import { SUBAGENT_MODEL_SELECTION_NAMESPACE, authorizedRoutes, draftFrom, joinEntries, saveRefusal, selectionValueFrom, withEnabled, withToggled, } from './model.ts' import type { SubagentModelDraft, SubagentModelEntry, SubagentModelReading, SubagentModelRoute, } from './model.ts' import { createSubagentModelSelectionOverlay } from './overlay.ts' /** The acknowledgement a landed save commits, once. */ const SAVED_MESSAGE = 'subagent model authorization updated · applies to new sessions; current session unchanged' /** What opening the editor needs from the window it opens over. */ export interface SubagentModelSelectionSpec { /** Context carrying the generic settings document and the llm registry. */ readonly ctx: Context /** Write the acknowledgement into the terminal's scrollback. */ readonly commit: (lines: readonly string[]) => void /** * Whether the gesture that opened the editor still has authority to mount UI * or write window state. * * The editor is opened from inside the `/model` picker, so its lifetime is the * attachment epoch that began THAT gesture — not the editor's own. Without * this it outlives its parent: withdrawing the model picker removes only that * picker's registration, and the child keeps its overlay, its catalog read, * and its settings write. */ readonly mayAct?: () => boolean /** Withdraw the editor when the initiating attachment retires. */ readonly signal?: AbortSignal } /** * Why a write was refused, worded for the reader. * * A revision conflict is not a generic failure: the draft is still valid, and * the reader has to decide whether to re-apply it. The `SETTINGS_CONFLICT` * code is read structurally rather than by importing the error class, so this * frontend keeps its type-only relationship with the settings package. * @param error - whatever `settings.mutate` rejected with. * @returns a sentence to show in the editor. */ function refusalMessage(error: unknown): string { if (typeof error === 'object' && error !== null && (error as { code?: unknown }).code === 'SETTINGS_CONFLICT') { return 'this setting changed elsewhere; your draft is kept — save again to write it, or esc to discard' } return error instanceof Error ? error.message : String(error) } /** * Show the subagent authorization editor and stay until the reader closes it. * * Pushed on top of the `/model` picker, so closing it returns to the picker. * @param spec - the context and where the acknowledgement goes. * @returns when the editor is closed. */ export async function openSubagentModelSelection(spec: SubagentModelSelectionSpec): Promise { const { ctx, commit } = spec const settings = ctx.get('settings') let loading = true let failure: string | undefined let entries: readonly SubagentModelEntry[] = [] let failedProviders: readonly string[] = [] let catalogError: string | undefined let draft: SubagentModelDraft = { enabled: false, selected: new Map() } let refusal: string | undefined let saving = false // The routes the setting held when the editor opened, kept for stable write // order. Refresh never updates this, nor the opening revision. let saved: readonly SubagentModelRoute[] = [] let revision: number | undefined let busy = false let closed = false let dismiss = (): void => {} let resolveOpen = (): void => {} /** * Whether this editor has stopped owning the screen. * * Two ways that happens, and both matter. The parent `/model` picker can * retire, and the editor is its child, so that lifetime is this one too. Or * the editor closes on its own — Escape, or a landed Save — while the * attachment stays perfectly healthy, and a catalog read or a save already in * flight finishes into a surface that is gone. Checking only the first let a * held `listModels()` call `tuiSlots.invalidate()` after Escape. */ const retired = (): boolean => closed || spec.mayAct?.() === false const invalidate = (): void => { // A retired epoch owns no screen: the overlay has been withdrawn, so a // repaint would touch a frame that now belongs to whatever replaced it. if (retired()) return ctx.tuiSlots.invalidate() } /** Withdraw this editor because the gesture that opened it is over. */ const withdraw = (): void => { finish(true) } /** * Complete the editor exactly once, from either way it can end. * * A normal close asks this to remove its own registration; an external * `TuiSlots` teardown has ALREADY removed it before calling the overlay's * `dispose`, so that path must not call the disposer again. Both paths * resolve the promise `openSubagentModelSelection` awaits, which is the * lifecycle contract: once the editor is gone — by escape, by a landed Save, * or by teardown — the caller finishes too. * @param dismissSelf - whether this completion should also remove the * overlay; false from external disposal, which has already done so. */ const finish = (dismissSelf: boolean): void => { if (closed) return closed = true spec.signal?.removeEventListener('abort', withdraw) if (dismissSelf) dismiss() resolveOpen() } const settle = (): void => { finish(true) } const readingOf = (): SubagentModelReading => { if (loading) return { kind: 'loading' } if (failure !== undefined) return { kind: 'unavailable', message: failure } return { kind: 'ready', entries, failedProviders, catalogError, draft, refusal, saving } } /** * The routes the merged rows must keep even if the live catalog drops them. * * This is saved authorization plus anything checked in the draft, so a * refresh can never delete a staged choice — the catalog is advisory, and a * route it stops advertising stays visible and removable. * @returns the routes to keep in the merged list. */ const keepRoutes = (): SubagentModelRoute[] => { const keep = saved.map(route => ({ provider: route.provider, model: route.model })) const present = new Set(keep.map(route => modelRouteKey(route.provider, route.model))) for (const route of draft.selected.values()) { const key = modelRouteKey(route.provider, route.model) if (present.has(key)) continue present.add(key) keep.push({ provider: route.provider, model: route.model }) } return keep } const readCatalog = async (): Promise => { try { const catalog = await readModelCatalog(ctx) entries = joinEntries(catalog.routes, keepRoutes()) failedProviders = catalog.failedProviders catalogError = undefined } catch (error: unknown) { // `listProviders()` itself failed, so no live route could be read. // Saved authorization must still be manageable, so the merged list is // built from the retained routes alone; the `unavailable` kind is NOT // used here because it carries no entries at all. entries = joinEntries([], keepRoutes()) failedProviders = [] catalogError = error instanceof Error ? error.message : String(error) } } /** * Re-read the setting and the catalog from scratch. This is the open and the * retry after an unavailable read; it is the only path that moves `revision`. */ const loadAll = async (): Promise => { if (busy || saving || retired()) return busy = true loading = true failure = undefined refusal = undefined invalidate() try { const descriptors = settings?.describe({ redactSecrets: true }) ?? [] const descriptor = descriptors.find(candidate => candidate.ns === SUBAGENT_MODEL_SELECTION_NAMESPACE) const value = descriptor === undefined ? undefined : selectionValueFrom(descriptor.value) if (settings === undefined) { failure = 'this profile mounts no settings provider, so subagent model authorization cannot be read or changed' } else if (descriptor === undefined) { failure = `the ${SUBAGENT_MODEL_SELECTION_NAMESPACE} setting is not registered in this profile` } else if (value === undefined) { failure = `the ${SUBAGENT_MODEL_SELECTION_NAMESPACE} setting could not be read` } else { revision = descriptor.revision saved = value.allowedModels.map(route => ({ provider: route.provider, model: route.model })) draft = draftFrom(value) await readCatalog() } } finally { busy = false loading = false invalidate() } } /** Re-read only the live catalog; the draft and the opening revision stay. */ const refreshCatalog = async (): Promise => { if (busy || saving || retired()) return busy = true try { await readCatalog() } finally { busy = false invalidate() } } /** * Refresh the revision a later Save fences against, after a conflict. * * The draft and its baseline route order are deliberately untouched: the * reader was told the document moved, and a second explicit Save is their * decision. This is not a silent retry — nothing is written here. */ const refreshRevision = (): void => { const descriptors = settings?.describe({ redactSecrets: true }) ?? [] const descriptor = descriptors.find(candidate => candidate.ns === SUBAGENT_MODEL_SELECTION_NAMESPACE) if (descriptor !== undefined && selectionValueFrom(descriptor.value) !== undefined) { revision = descriptor.revision } } const save = (): void => { // A retired epoch admits no new mutation: the editor is gone, and a settings // write begun now would belong to a gesture nobody is still making. if (saving || retired()) return if (busy) { // A catalog refresh is in flight. Saying so beats a key that silently // does nothing while the footer still advertises it. refusal = 'the live catalog is still being read; save again in a moment' invalidate() return } const blocked = saveRefusal(draft) if (blocked !== undefined) { refusal = blocked invalidate() return } if (settings === undefined || revision === undefined) { refusal = 'subagent model authorization cannot be written in this profile' invalidate() return } // Both fields in ONE revision-fenced mutation. Written separately, the // first could persist an enabled setting with no routes, which Harness // rejects — and the second could apply over a document that moved. const ops: SettingsPathOp[] = [ { op: 'set', path: ['enabled'], value: draft.enabled }, { op: 'set', path: ['allowedModels'], value: authorizedRoutes(saved, draft.selected, entries) .map(route => ({ provider: route.provider, model: route.model })), }, ] saving = true refusal = undefined invalidate() void settings.mutate(SUBAGENT_MODEL_SELECTION_NAMESPACE, ops, revision) .then(() => { // A window or session teardown can dispose the overlay while the write // is in flight; the acknowledgement then has no surface to land on. The // write itself is already committed and is deliberately NOT rolled back: // the reader asked for it while this epoch still owned the screen. if (closed || retired()) return commit([paint(`· ${SAVED_MESSAGE}`, 'muted')]) settle() }) .catch((error: unknown) => { // A teardown can dispose the surface while the write is in flight; // there is no reader left to report a refusal to. if (closed || retired()) return saving = false // The draft stays open with the reader's edit intact: a conflict is // theirs to resolve, and a Harness refusal is the authority speaking. // A conflict also refreshes the revision so a second explicit Save is // fenced against the document as it now stands. if (typeof error === 'object' && error !== null && (error as { code?: unknown }).code === 'SETTINGS_CONFLICT') refreshRevision() refusal = refusalMessage(error) invalidate() }) } await new Promise(resolve => { resolveOpen = resolve const overlay = createSubagentModelSelectionOverlay({ reading: readingOf, toggle: entry => { if (saving || retired()) return draft = withToggled(draft, entry.route) refusal = undefined invalidate() }, flipEnabled: () => { if (saving || retired()) return draft = withEnabled(draft, !draft.enabled) refusal = undefined invalidate() }, save, refresh: () => { if (failure !== undefined || revision === undefined) void loadAll() else void refreshCatalog() }, close: settle, invalidate, }) // Registration is already gone when `TuiSlots` calls this, so it completes // without dismissing again — but it still resolves the caller's promise. overlay.dispose = () => { finish(false) } dismiss = ctx.tuiSlots.pushOverlay(overlay) // Withdraw with the parent, and detach when the editor ends on its own: the // signal belongs to the attachment epoch and outlives every editor opened // against it, so an anonymous listener would accumulate the way the // picker's did. if (spec.signal?.aborted === true) withdraw() else spec.signal?.addEventListener('abort', withdraw, { once: true }) void loadAll() }) }