feat(web): let a blank session switch its agent preset
`agentPreset.select` recomposes one session's agent from a different preset. It is allowed only while the session is blank — once a turn has run, that history was produced under the preset's tools and swapping them would strand logged tool calls, so the attempt answers `agent-preset-locked`. The agent and the session survive; only the preset subtree is swapped. That was forced by what the host actually owns: api-proxy discards the `AgentHandle` it creates, and there is no delete RPC, so neither disposing nor recreating the session was available. Swapping the subtree is also the better answer — the session id, its workspace attachment, and its projections all stay put. `recompose` is unmount-then-mount because two compositions cannot coexist: both would register the same tool names into one layer. So it resolves the new preset BEFORE tearing anything down (an unknown id is a no-op) and restores the previous composition when the new one fails to mount, rather than leaving the agent with no tools at all. Both paths are pinned by test. Also restores the English half of the `agentPreset.list` README paragraph, which was lost before the previous commit — and `verify-translation-pairing --write` recorded the pair as consistent anyway, because it records whatever state it finds rather than checking the two sides say the same thing.
This commit is contained in:
@@ -18,7 +18,7 @@ import { pathToFileURL } from 'node:url'
|
||||
import { Context, type Fiber } from 'cordis'
|
||||
import { Include } from '@cordisjs/plugin-include'
|
||||
import type { EntryTree } from '@cordisjs/plugin-loader'
|
||||
import { scopeOf } from '@deepseek-ai/dsh-scope'
|
||||
import { scopeOf, type ScopeKey } from '@deepseek-ai/dsh-scope'
|
||||
import { PresetMountError, type AgentPreset } from './types.ts'
|
||||
|
||||
/** What one mounted subtree publishes about itself for the audit to read. */
|
||||
@@ -77,6 +77,8 @@ export interface PresetMount {
|
||||
readonly presetId: string
|
||||
/** The mounted subtree's fiber. */
|
||||
readonly fiber: Fiber
|
||||
/** The scope the subtree was mounted for — the agent that owns it. */
|
||||
readonly scope: ScopeKey
|
||||
}
|
||||
|
||||
const mounts = new Set<PresetMount>()
|
||||
@@ -113,6 +115,24 @@ export function livePresetMounts(): PresetMount[] {
|
||||
return [...mounts]
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard the composition currently installed for one scope, if any.
|
||||
*
|
||||
* Only a composition that has produced nothing may be replaced: swapping a
|
||||
* live agent's tools mid-conversation would leave logged tool calls the new
|
||||
* composition cannot make. The caller owns that check — this function does the
|
||||
* teardown and returns once the subtree is quiescent.
|
||||
* @param scope - the agent whose installed composition to discard.
|
||||
* @returns the preset id that was discarded, or `undefined` when none was.
|
||||
*/
|
||||
export async function unmountPresetFor(scope: ScopeKey): Promise<string | undefined> {
|
||||
const installed = livePresetMounts().find(mount => mount.scope === scope)
|
||||
if (installed === undefined) return undefined
|
||||
mounts.delete(installed)
|
||||
await Promise.resolve(installed.fiber.dispose())
|
||||
return installed.presetId
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `fiber` is `root` itself or is mounted anywhere inside its subtree.
|
||||
*
|
||||
@@ -241,7 +261,8 @@ export function inactiveRows(tree: EntryTree): string[] {
|
||||
* published a service into the root realm.
|
||||
*/
|
||||
export async function mountPreset(agentCtx: Context, preset: AgentPreset): Promise<void> {
|
||||
if (scopeOf(agentCtx) === undefined) {
|
||||
const scope = scopeOf(agentCtx)
|
||||
if (scope === undefined) {
|
||||
throw new Error(
|
||||
`agent-presets: refusing to mount preset "${preset.id}" into an unscoped context; `
|
||||
+ 'its registrations would apply to every agent in the process',
|
||||
@@ -269,7 +290,7 @@ export async function mountPreset(agentCtx: Context, preset: AgentPreset): Promi
|
||||
+ 'a preset service must sit behind an `isolate` realm or move to the host composition',
|
||||
)
|
||||
}
|
||||
mounts.add({ presetId: preset.id, fiber })
|
||||
mounts.add({ presetId: preset.id, fiber, scope })
|
||||
} catch (error) {
|
||||
try {
|
||||
await handle.dispose()
|
||||
|
||||
Reference in New Issue
Block a user