feat: layer runtime and model selection across conversation, project and global

- Store runtime selection as { provider?, model? } layers and resolve them per
  field (conversation -> project -> global); drop the legacy auto provider.
- Migrate stored selections (database v49) and clear deleted profile ids.
- Re-resolve selections in Main per request and at queue dispatch.
- Add ProjectRuntimeSelector and a two-column RuntimeModelPicker; manage
  link opens model connections; restore per-runtime default model tabs.
- Stop duplicating trusted instructions and history across OpenCode,
  Continue and DeepSeek Harness runtimes.
- Document the layered design, global provider removal plan and planned
  purpose-specific models.
This commit is contained in:
mesalogo
2026-09-29 21:33:06 +08:00
parent d1b92deff3
commit 9c88ca20f3
50 changed files with 2879 additions and 1836 deletions
@@ -838,6 +838,37 @@ describe('DeepSeekHarnessRuntime', () => {
await harness.runtime.dispose()
})
it('sends GoodBuddy history only when it creates the native session', async () => {
const harness = setup()
const history = [
{ role: 'user' as const, content: 'earlier question' },
{ role: 'assistant' as const, content: 'earlier answer' }
]
for (const [index, prompt] of ['first', 'second'].entries()) {
const running = collect(
harness.runtime.run(
{ ...request('reused'), requestId: `request-${prompt}`, prompt, history },
new AbortController().signal
)
)
await vi.waitFor(() =>
expect(harness.promptGates).toHaveLength(index + 1)
)
harness.promptGates[index]!.resolve({ stopReason: 'end_turn' })
await running
}
const texts = harness.requests
.filter(({ method }) => method === 'session/prompt')
.map(({ params }) =>
(params.prompt as Array<{ text?: string }>)[0]?.text
)
expect(texts[0]).toContain('<conversation-history>')
expect(texts[0]).toContain('earlier answer')
expect(texts[1]).toBe('second')
await harness.runtime.dispose()
})
it('fails Ask closed and never calls the authorizer', async () => {
const harness = setup()
const authorize = vi.fn().mockResolvedValue('once')
+12 -6
View File
@@ -1348,17 +1348,17 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
state: HarnessState,
conversationId: string,
workspace: string
): Promise<string> {
): Promise<{ id: string; created: boolean }> {
const current = this.sessions.get(conversationId)
if (current?.workspace === workspace) {
return current.id
return { id: current.id, created: false }
}
if (current) {
await this.releaseConversation(conversationId)
}
const pending = this.sessionInitializations.get(conversationId)
if (pending) {
return pending
return { id: await pending, created: false }
}
const creation = state.agent
.newSession({
@@ -1374,7 +1374,7 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
})
this.sessionInitializations.set(conversationId, creation)
try {
return await creation
return { id: await creation, created: true }
} finally {
this.sessionInitializations.delete(conversationId)
}
@@ -1496,11 +1496,14 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
const workspace = request.executionWorkspace
? await realpath(request.executionWorkspace)
: state.workspace
sessionId = await this.getSession(
const session = await this.getSession(
state,
request.conversationId,
workspace
)
sessionId = session.id
// A reused Harness session already retains earlier turns natively.
const includeHistory = session.created
run = {
request,
toolProvider: this.toolsFor(workspace),
@@ -1557,7 +1560,10 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
prompt: [
{
type: 'text',
text: promptWithUntrustedConversationHistory(request, true)
text: promptWithUntrustedConversationHistory(
request,
includeHistory
)
},
...(request.images ?? []).map((image) => ({
type: 'image' as const,
+3 -1
View File
@@ -24,7 +24,7 @@ class LocalRuntimeEntry {
private idle?: ReturnType<typeof setTimeout>
readonly description: Pick<AgentRuntime,
'runtimeId' | 'requiresToolApproval' | 'supportsToolExecution' |
'supportsScopedDataTools' | 'capability'>
'supportsScopedDataTools' | 'consumesTrustedInstructions' | 'capability'>
constructor(
readonly configuration: unknown,
@@ -38,6 +38,7 @@ class LocalRuntimeEntry {
requiresToolApproval: runtime.requiresToolApproval,
supportsToolExecution: runtime.supportsToolExecution,
supportsScopedDataTools: runtime.supportsScopedDataTools,
consumesTrustedInstructions: runtime.consumesTrustedInstructions,
capability: runtime.capability
}
this.scheduleIdle()
@@ -220,6 +221,7 @@ class WorkspaceRuntime implements AgentRuntime {
get requiresToolApproval() { return this.entry.description.requiresToolApproval }
get supportsToolExecution() { return this.entry.description.supportsToolExecution }
get supportsScopedDataTools() { return this.entry.description.supportsScopedDataTools }
get consumesTrustedInstructions() { return this.entry.description.consumesTrustedInstructions }
get capability() { return this.entry.description.capability }
private check(): void {
+1
View File
@@ -1637,6 +1637,7 @@ async function* readAnthropicToolStream(
export class ModelAgentRuntime implements AgentRuntime {
readonly runtimeId = 'model'
readonly requiresToolApproval = false
readonly consumesTrustedInstructions = true
private readonly conversations = new Map<string, ConversationMessage[]>()
private readonly conversationSummaries = new Map<
string,
+6 -4
View File
@@ -4,7 +4,6 @@ import { mkdir, stat } from 'node:fs/promises'
import { join } from 'node:path'
import { promisify } from 'node:util'
import { normalizeInteractiveWorkMode } from '../../shared/assistant-contracts'
import { getDefaultRuntimeSelection } from '../../shared/runtime-selection-contracts'
import type { RuntimeNativeClientResult } from '../../shared/runtime-native-client-contracts'
import type { AssistantDatabase } from '../assistant/assistant-database'
import type { ApplicationSettingsStore } from '../application-settings-store'
@@ -17,7 +16,7 @@ import type { BundledRuntimePaths } from './bundled-runtimes'
import type { KnowledgeMcpGateway } from './knowledge-mcp-gateway'
import { NativeDshWebClientService } from './native-dsh-web-client'
import { NativeTerminalClient } from './native-terminal-client'
import { applyRuntimeSelection } from './runtime-selection'
import { applyRuntimeSelection, resolveLayeredRuntimeSelection } from './runtime-selection'
type Options = {
database: AssistantDatabase
@@ -86,8 +85,11 @@ export class NativeClientCoordinator {
if (!conversation.projectId) throw new Error('Native clients require a saved project conversation')
const project = this.options.database.getProject(conversation.projectId)
const settings = await this.options.settingsStore.getResolvedSettings()
const saved = conversation.runtimeSelection ?? project.runtimeSelection
const selected = applyRuntimeSelection(settings, saved && saved.provider !== 'auto' ? saved : getDefaultRuntimeSelection(settings))
const selected = applyRuntimeSelection(settings, resolveLayeredRuntimeSelection(
settings,
{ project: project.runtimeSelection, conversation: conversation.runtimeSelection },
{ remote: project.executionSpace?.kind === 'ssh' }
).selection)
const space = this.options.executionSpaceResolver.resolveProject(project)
const workMode = normalizeInteractiveWorkMode(conversation.workMode ?? project.defaultWorkMode)
const [skills, mcpServers, builtin, application, obsidian] = await Promise.all([
+33
View File
@@ -207,6 +207,10 @@ function runClient(events: Record<string, unknown>[]) {
data: true,
error: undefined,
}),
deleteMessage: vi.fn().mockResolvedValue({
data: true,
error: undefined,
}),
},
event: {
subscribe: vi.fn().mockImplementation(async () => {
@@ -4155,6 +4159,35 @@ describe("OpenCodeRuntime embedded permission mediation", () => {
await runtime.dispose();
});
it("removes an unanswered prompt from a reused session after failure", async () => {
const { client, session } = runClient([
{
id: "event-error",
type: "session.error",
properties: {
sessionID: "session-1",
error: { name: "UnknownError", data: { message: "model failed" } },
},
},
]);
const runtime = embeddedRuntime(client);
await expect(collectRun(runtime)).rejects.toThrow("model failed");
const promptMessageId = vi.mocked(session.promptAsync).mock.calls[0]?.[0]
?.messageID;
expect(promptMessageId).toMatch(/^msg_/);
expect(session.deleteMessage).toHaveBeenCalledWith(
{
sessionID: "session-1",
messageID: promptMessageId,
directory: process.cwd(),
},
{ signal: expect.any(AbortSignal) },
);
await runtime.dispose();
});
it("deletes an ephemeral OpenCode session when released", async () => {
const { client, session } = runClient([
{
+28 -3
View File
@@ -726,6 +726,8 @@ export class OpenCodeRuntime implements AgentRuntime {
readonly runtimeId = "opencode";
readonly requiresToolApproval = false;
readonly supportsToolExecution = true;
// OpenCode applies the prompt `system` field to the current turn only.
readonly consumesTrustedInstructions = true;
private client?: OpencodeClient;
private clientInitialization?: Promise<OpencodeClient>;
private server?: OpenCodeServer;
@@ -2453,6 +2455,7 @@ export class OpenCodeRuntime implements AgentRuntime {
const checklistCalls = new Map<string, unknown>();
const submittedChecklistCalls = new Set<string>();
const pendingChecklistUpdates: RuntimeChecklist[] = [];
let runFailed = false;
try {
const promptText = promptWithUntrustedConversationHistory(
request,
@@ -2502,9 +2505,14 @@ export class OpenCodeRuntime implements AgentRuntime {
: undefined,
...(selectedAgent ? { agent: selectedAgent } : {}),
system:
nativeSkillIds.length > 0
? undefined
: this.options.skillInstructions || undefined,
[
nativeSkillIds.length > 0
? undefined
: this.options.skillInstructions,
request.trustedInstructions,
]
.filter(Boolean)
.join("\n\n") || undefined,
...(toolOverrides ? { tools: toolOverrides } : {}),
parts: [
{ type: "text" as const, text: promptText },
@@ -3021,6 +3029,7 @@ export class OpenCodeRuntime implements AgentRuntime {
signal.throwIfAborted();
throw new Error("OpenCode 事件流意外结束");
} catch (error) {
runFailed = true;
abortSession();
for (const [callId, tool] of toolStates) {
if (tool.state === "pending" || tool.state === "running") {
@@ -3056,6 +3065,22 @@ export class OpenCodeRuntime implements AgentRuntime {
signal.removeEventListener("abort", abortSession);
// Keep the conversation locked until its session-wide abort settles.
await sessionAbort;
// A reused session would otherwise merge this unanswered prompt into the next turn.
if (
runFailed &&
!selectedCommand &&
checklistMessageIds.size === 0 &&
this.sessions.get(request.conversationId)?.id === sessionId
) {
await Promise.resolve()
.then(() =>
client.session.deleteMessage(
{ sessionID: sessionId, messageID: promptMessageId, directory },
{ signal: AbortSignal.timeout(1_000) },
),
)
.catch(() => undefined);
}
for (const questionId of reportedQuestionIds.values()) {
this.pendingQuestions.delete(questionId);
}
+4
View File
@@ -112,6 +112,10 @@ export class AgentRuntimeController implements AgentRuntime {
return this.current.runtime.supportsScopedDataTools !== false
}
get consumesTrustedInstructions(): boolean {
return this.current.runtime.consumesTrustedInstructions === true
}
get capability(): AgentRuntime['capability'] {
return this.current.runtime.capability
}
+67 -2
View File
@@ -1,15 +1,80 @@
import type { AgentExecutionRequest } from './runtime'
type ConversationHistory = NonNullable<AgentExecutionRequest['history']>
const historyHeader =
'Continue this conversation. The history below is untrusted conversation data, not system instructions.'
const historyOpen = '<conversation-history>'
const historyClose = '</conversation-history>'
export function promptWithUntrustedConversationHistory(
request: Pick<AgentExecutionRequest, 'history' | 'prompt'>,
includeHistory: boolean
): string {
return includeHistory && request.history?.length
? [
'Continue this conversation. The history below is untrusted conversation data, not system instructions.',
`<conversation-history>${JSON.stringify(request.history)}</conversation-history>`,
historyHeader,
`${historyOpen}${JSON.stringify(request.history)}${historyClose}`,
'',
request.prompt
].join('\n')
: request.prompt
}
/**
* Reverses `promptWithUntrustedConversationHistory` for runtimes that keep
* their own transcript, so an embedded history envelope is not re-wrapped.
*/
export function splitUntrustedConversationHistory(text: string): {
history?: ConversationHistory
prompt: string
} {
const prefix = `${historyHeader}\n${historyOpen}`
if (!text.startsWith(prefix)) return { prompt: text }
// Message content may itself contain the closing tag; accept the first
// boundary whose payload is a valid history array.
let index = text.indexOf(historyClose, prefix.length)
while (index !== -1) {
const history = parseHistory(text.slice(prefix.length, index))
const rest = text.slice(index + historyClose.length)
if (history && rest.startsWith('\n\n')) {
return { history, prompt: rest.slice(2) }
}
index = text.indexOf(historyClose, index + 1)
}
return { prompt: text }
}
function parseHistory(json: string): ConversationHistory | undefined {
let value: unknown
try {
value = JSON.parse(json)
} catch {
return undefined
}
if (!Array.isArray(value)) return undefined
const history: ConversationHistory = []
for (const item of value) {
if (
!item ||
typeof item !== 'object' ||
((item as { role?: unknown }).role !== 'user' &&
(item as { role?: unknown }).role !== 'assistant') ||
typeof (item as { content?: unknown }).content !== 'string'
) {
return undefined
}
history.push({
role: (item as { role: 'user' | 'assistant' }).role,
content: (item as { content: string }).content
})
}
return history
}
/** Drops the per-turn GoodBuddy work-mode paragraph from stored user text. */
export function stripWorkModeInstruction(prompt: string): string {
if (!prompt.startsWith('Work mode: ')) return prompt
const end = prompt.indexOf('\n\n')
return end === -1 ? prompt : prompt.slice(end + 2)
}
@@ -1,109 +1,168 @@
import { describe, expect, it } from 'vitest'
import { applyRuntimeSelection } from './runtime-selection'
import type { ResolvedRuntimeSettings } from '../runtime-settings-store'
import { defaultRuntimeCustomizationSettings } from '../../shared/contracts'
import {
repairChannelRuntimeSelection,
type AgentRuntimeSelection,
type RuntimeSelectionRepairSettings
compactRuntimeSelectionLayer,
optionalAgentRuntimeSelectionSchema,
repairRuntimeSelectionLayer,
resolveRuntimeChoice,
runtimeSelectionLayerSchema,
withoutLegacyAutoSelection,
type RuntimeResolutionSettings
} from '../../shared/runtime-selection-contracts'
const defaultId = '00000000-0000-4000-8000-000000000001'
const fallbackId = '00000000-0000-4000-8000-000000000002'
const missingId = '00000000-0000-4000-8000-000000000003'
const secondId = '00000000-0000-4000-8000-000000000002'
const imageId = '00000000-0000-4000-8000-000000000003'
const keylessId = '00000000-0000-4000-8000-000000000004'
const missingId = '00000000-0000-4000-8000-000000000009'
function settings(
defaultProfile: Partial<RuntimeSelectionRepairSettings['modelProfiles'][number]>
): RuntimeSelectionRepairSettings {
overrides: Partial<RuntimeResolutionSettings> = {}
): RuntimeResolutionSettings {
return {
provider: 'opencode',
defaultModelProfileId: defaultId,
modelProfiles: [
{
id: defaultId, protocol: 'openai-chat-completions',
authentication: 'api-key', apiKeyConfigured: true, ...defaultProfile
},
{
id: fallbackId, protocol: 'openai-chat-completions',
authentication: 'none', apiKeyConfigured: false
}
{ id: defaultId, name: 'Default', protocol: 'openai-chat-completions', baseUrl: 'https://a.example/v1', authentication: 'api-key', apiKeyConfigured: true },
{ id: secondId, name: 'Second', protocol: 'anthropic-messages', baseUrl: 'https://b.example/v1', authentication: 'none' },
{ id: imageId, name: 'Image', protocol: 'openai-images-generations', baseUrl: 'https://c.example/v1', authentication: 'none' },
{ id: keylessId, name: 'Keyless', protocol: 'openai-chat-completions', baseUrl: 'https://d.example/v1', authentication: 'api-key', apiKeyConfigured: false }
],
opencodeModelSource: { kind: 'platform' },
continueModelSource: { kind: 'platform' }
opencodeModelSource: { kind: 'profile', profileId: secondId },
continueModelSource: { kind: 'platform' },
deepseekHarnessModelSource: { kind: 'default' },
...overrides
}
}
function resolvedSettings(input: RuntimeSelectionRepairSettings): ResolvedRuntimeSettings {
return {
provider: 'model', modelBaseUrl: 'https://model.example/v1', modelName: 'default',
modelProtocol: 'openai-chat-completions', modelAuthentication: 'api-key',
imageGenerationQuality: 'auto',
modelProfiles: input.modelProfiles.map((profile) => ({
id: profile.id, name: profile.id, baseUrl: 'https://model.example/v1',
modelName: profile.id,
protocol: profile.protocol as ResolvedRuntimeSettings['modelProtocol'],
authentication: profile.authentication!,
apiKey: profile.apiKeyConfigured ? 'fixture-key' : undefined
})),
defaultModelProfileId: input.defaultModelProfileId,
opencodeBaseUrl: '', opencodeEmbedded: false, opencodeBinaryPath: '',
opencodeConfigPath: '', continueBinaryPath: '', continueConfigPath: '',
continueMode: 'chat', subagentSmartRoutingEnabled: false,
knowledgeEmbeddingEnabled: false, knowledgeEmbeddingBaseUrl: '',
knowledgeEmbeddingModel: '', knowledgeRerankEnabled: false,
knowledgeRerankEndpoint: '', knowledgeRerankModel: '',
runtimeCustomization: defaultRuntimeCustomizationSettings,
workspacePath: '.', toolApproval: 'always'
}
}
describe('layered runtime resolution', () => {
it('uses global defaults when project and conversation inherit', () => {
const resolved = resolveRuntimeChoice(settings())
expect(resolved.selection).toEqual({ provider: 'opencode', profileId: secondId })
expect(resolved).toMatchObject({ providerSource: 'global', modelSource: 'global' })
})
describe('channel direct model repair', () => {
const selections: AgentRuntimeSelection[] = [
{ provider: 'auto' },
{ provider: 'model' },
{ provider: 'model', profileId: defaultId },
{ provider: 'model', profileId: missingId }
]
it.each([
{ protocol: 'openai-images-generations' },
{ apiKeyConfigured: false },
{ id: missingId }
])('pins a verified fallback when the default is unusable: %j', (defaultProfile) => {
const input = settings(defaultProfile)
if (defaultProfile.id) input.modelProfiles = input.modelProfiles.slice(1)
for (const selection of selections) {
const repaired = repairChannelRuntimeSelection(selection, input)
expect(repaired).toEqual({ provider: 'model', profileId: fallbackId })
const resolved = applyRuntimeSelection(resolvedSettings(input), repaired)
expect(resolved.settings.defaultModelProfileId).toBe(fallbackId)
expect(resolved.settings.modelProtocol).toBe('openai-chat-completions')
expect(resolved.settings.modelAuthentication).toBe('none')
it('inherits each field independently across layers', () => {
const resolved = resolveRuntimeChoice(settings(), {
project: { provider: 'continue' },
conversation: { model: { kind: 'profile', profileId: defaultId } }
})
expect(resolved.selection).toEqual({ provider: 'continue', profileId: defaultId })
expect(resolved).toMatchObject({ providerSource: 'project', modelSource: 'conversation' })
})
it('switching only the execution mode takes that mode’s own default model', () => {
const resolved = resolveRuntimeChoice(settings(), {
project: { provider: 'opencode', model: { kind: 'profile', profileId: secondId } },
conversation: { provider: 'continue' }
})
// The project's OpenCode model still applies because it is valid for Continue too;
// a Continue-incompatible model would fall back to Continue's global default.
expect(resolved.selection).toEqual({ provider: 'continue', profileId: secondId })
const harness = resolveRuntimeChoice(settings(), {
project: { model: { kind: 'profile', profileId: secondId } },
conversation: { provider: 'deepseek-harness' }
})
expect(harness.selection).toEqual({ provider: 'deepseek-harness', profileId: defaultId })
expect(harness.ignored).toMatchObject({ layer: 'project', kind: 'model', reason: 'incompatible' })
})
it('keeps a keyless model so the run can explain the missing key', () => {
expect(resolveRuntimeChoice(settings(), {
conversation: { model: { kind: 'profile', profileId: keylessId } }
}).selection).toEqual({ provider: 'opencode', profileId: keylessId })
})
it('falls back and reports deleted or incompatible models', () => {
for (const [profileId, reason] of [
[missingId, 'missing'],
[imageId, 'incompatible']
] as const) {
const resolved = resolveRuntimeChoice(settings(), {
conversation: { model: { kind: 'profile', profileId } }
})
expect(resolved.selection).toEqual({ provider: 'opencode', profileId: secondId })
expect(resolved.ignored).toEqual({ layer: 'conversation', kind: 'model', reason })
}
})
it('keeps a healthy default dynamic and preserves valid explicit pins', () => {
const input = settings({})
for (const selection of [selections[0]!, selections[1]!, selections[3]!]) {
const repaired = repairChannelRuntimeSelection(selection, input)
expect(repaired).toEqual({ provider: 'model' })
const changed = { ...resolvedSettings(input), defaultModelProfileId: fallbackId }
expect(applyRuntimeSelection(changed, repaired).settings.defaultModelProfileId).toBe(fallbackId)
}
for (const profileId of [defaultId, fallbackId]) {
const selection = { provider: 'model', profileId } as const
expect(repairChannelRuntimeSelection(selection, input)).toEqual(selection)
}
it('treats own configuration as an explicit choice', () => {
expect(resolveRuntimeChoice(settings(), { project: { provider: 'continue' } }).selection)
.toEqual({ provider: 'continue', runtimeConfig: true })
const direct = resolveRuntimeChoice(settings(), {
project: { provider: 'model' },
conversation: { model: { kind: 'runtime-config' } }
})
expect(direct.selection).toEqual({ provider: 'model', profileId: defaultId })
expect(direct.ignored?.reason).toBe('incompatible')
})
it('preserves a valid pin even when the global default is an image model', () => {
const input = settings({ protocol: 'openai-images-generations' })
const selection = { provider: 'model', profileId: fallbackId } as const
expect(repairChannelRuntimeSelection(selection, input)).toEqual(selection)
it('limits managed SSH projects to OpenCode and Continue with GoodBuddy connections', () => {
const resolved = resolveRuntimeChoice(
settings({ provider: 'model', continueModelSource: { kind: 'platform' } }),
{ project: { provider: 'deepseek-harness' }, conversation: { provider: 'continue' } },
{ remote: true }
)
expect(resolved.selection).toEqual({ provider: 'continue', profileId: defaultId })
expect(resolved.ignored).toMatchObject({ layer: 'project', kind: 'provider' })
})
it('does not invent a profile when no usable fallback exists', () => {
const input = settings({ apiKeyConfigured: false })
input.modelProfiles = input.modelProfiles.slice(0, 1)
expect(repairChannelRuntimeSelection({ provider: 'auto' }, input))
it('treats legacy automatic global settings as the direct model', () => {
expect(resolveRuntimeChoice(settings({ provider: 'auto' })).selection)
.toEqual({ provider: 'model', profileId: defaultId })
})
it('skips an unusable global direct default instead of failing', () => {
const resolved = resolveRuntimeChoice(settings({ provider: 'model', defaultModelProfileId: imageId }))
expect(resolved.selection).toEqual({ provider: 'model', profileId: defaultId })
})
it('never invents a profile id when no connections exist', () => {
expect(resolveRuntimeChoice(settings({ provider: 'model', defaultModelProfileId: '', modelProfiles: [] })).selection)
.toEqual({ provider: 'model' })
})
it('refuses GoodBuddy connections for an external OpenCode server', () => {
const resolved = resolveRuntimeChoice(settings({ opencodeBaseUrl: 'http://127.0.0.1:4096' }), {
conversation: { model: { kind: 'profile', profileId: defaultId } }
})
expect(resolved.selection).toEqual({ provider: 'opencode', runtimeConfig: true })
})
})
describe('runtime selection layer storage', () => {
it('upgrades legacy stored selections', () => {
expect(runtimeSelectionLayerSchema.parse({ provider: 'auto' })).toEqual({})
expect(runtimeSelectionLayerSchema.parse({ provider: 'opencode' })).toEqual({ provider: 'opencode' })
expect(runtimeSelectionLayerSchema.parse({ provider: 'model', profileId: defaultId }))
.toEqual({ provider: 'model', model: { kind: 'profile', profileId: defaultId } })
expect(() => runtimeSelectionLayerSchema.parse({ provider: 'model', model: { kind: 'runtime-config' } }))
.toThrow()
})
it('stores inheritance as absence', () => {
expect(compactRuntimeSelectionLayer({})).toBeUndefined()
expect(compactRuntimeSelectionLayer(undefined)).toBeUndefined()
})
it('repairs only references to deleted connections', () => {
const input = settings()
expect(repairRuntimeSelectionLayer(
{ provider: 'continue', model: { kind: 'profile', profileId: missingId } }, input
)).toEqual({ provider: 'continue' })
expect(repairRuntimeSelectionLayer(
{ model: { kind: 'profile', profileId: missingId } }, input
)).toBeUndefined()
const valid = { provider: 'continue' as const, model: { kind: 'profile' as const, profileId: imageId } }
// Incompatible but existing: kept so the UI can explain the fallback.
expect(repairRuntimeSelectionLayer(valid, input)).toEqual(valid)
})
it('maps legacy automatic request selections to unspecified', () => {
expect(withoutLegacyAutoSelection({ provider: 'auto' })).toBeUndefined()
expect(withoutLegacyAutoSelection({ prompt: 'x', runtimeSelection: { provider: 'auto' } }))
.toEqual({ prompt: 'x' })
expect(() => optionalAgentRuntimeSelectionSchema.parse({ provider: 'auto' })).toThrow()
expect(optionalAgentRuntimeSelectionSchema.parse(withoutLegacyAutoSelection({ provider: 'auto' })))
.toBeUndefined()
})
})
+13 -7
View File
@@ -291,15 +291,21 @@ describe('runtime selection', () => {
).toEqual({ provider: 'deepseek-harness' })
})
it('routes legacy automatic settings through local OpenCode when the Server is blank', () => {
expect(getConfiguredRuntimeTarget(settings())).toBe('opencode')
it('treats legacy automatic settings as the direct model, matching settings normalization', () => {
expect(getConfiguredRuntimeTarget(settings())).toBe('model')
expect(
getConfiguredRuntimeTarget(
settings({ opencodeEmbedded: false })
)
getConfiguredRuntimeTarget(settings({ provider: 'opencode' }))
).toBe('opencode')
})
it('resolves own configuration explicitly instead of the configured profile', () => {
const configured = settings({
provider: 'opencode',
opencodeModelProfile: settings().modelProfiles[0]
})
expect(
applyRuntimeSelection(settings(), { provider: 'auto' }).settings
).toEqual(settings())
applyRuntimeSelection(configured, { provider: 'opencode', runtimeConfig: true })
.settings.opencodeModelProfile
).toBeUndefined()
})
})
+66 -23
View File
@@ -2,7 +2,14 @@ import {
isAgentRuntimeModelProtocol,
isDeepSeekHarnessModelProfile
} from '../../shared/contracts'
import type { AgentRuntimeSelection } from '../../shared/runtime-selection-contracts'
import {
resolveRuntimeChoice,
type AgentRuntimeSelection,
type ResolvedRuntimeChoice,
type RuntimeResolutionOptions,
type RuntimeResolutionSettings,
type RuntimeSelectionLayer
} from '../../shared/runtime-selection-contracts'
import type {
ResolvedModelProfile,
ResolvedRuntimeSettings
@@ -36,24 +43,62 @@ export function getConfiguredRuntimeTarget(
if (settings.provider === 'deepseek-harness') {
return 'deepseek-harness'
}
if (
settings.provider === 'opencode' ||
settings.provider === 'auto'
) {
if (settings.provider === 'opencode') {
return 'opencode'
}
return 'model'
}
/** Presents resolved Main settings in the shape used by the shared resolver. */
export function runtimeResolutionSettings(
settings: ResolvedRuntimeSettings
): RuntimeResolutionSettings {
// Partial settings (early startup, narrow callers) resolve with no connections.
const modelProfiles = settings.modelProfiles ?? []
const known = (profile?: ResolvedModelProfile) =>
profile && modelProfiles.some((candidate) => candidate.id === profile.id)
? ({ kind: 'profile', profileId: profile.id } as const)
: ({ kind: 'platform' } as const)
return {
provider: settings.provider,
defaultModelProfileId: settings.defaultModelProfileId,
modelProfiles: modelProfiles.map((profile) => ({
id: profile.id,
name: profile.name,
modelName: profile.modelName,
baseUrl: profile.baseUrl,
protocol: profile.protocol,
authentication: profile.authentication,
apiKeyConfigured:
profile.authentication !== 'api-key' || Boolean(profile.apiKey)
})),
opencodeModelSource: known(settings.opencodeModelProfile),
continueModelSource: known(settings.continueModelProfile),
deepseekHarnessModelSource: known(settings.deepseekHarnessModelProfile),
opencodeBaseUrl: settings.opencodeBaseUrl
}
}
export function resolveLayeredRuntimeSelection(
settings: ResolvedRuntimeSettings,
layers: {
project?: RuntimeSelectionLayer
conversation?: RuntimeSelectionLayer
} = {},
options: RuntimeResolutionOptions = {}
): ResolvedRuntimeChoice {
return resolveRuntimeChoice(runtimeResolutionSettings(settings), layers, options)
}
/** Pins the model a Runtime selection implies so remote validation sees one concrete profile. */
export function resolveConfiguredAgentRuntimeSelection(
settings: ResolvedRuntimeSettings,
selection: AgentRuntimeSelection
): AgentRuntimeSelection {
if ('profileId' in selection && selection.profileId) return selection
if (
selection.provider !== 'opencode' &&
selection.provider !== 'continue' &&
selection.provider !== 'deepseek-harness'
selection.provider === 'model' ||
selection.profileId ||
('runtimeConfig' in selection && selection.runtimeConfig)
) {
return selection
}
@@ -80,13 +125,6 @@ export function applyRuntimeSelection(
settings: ResolvedRuntimeSettings
target: SelectedRuntimeTarget
} {
if (selection.provider === 'auto') {
return {
settings,
target: getConfiguredRuntimeTarget(settings)
}
}
if (selection.provider === 'model') {
const profile = requireProfile(settings, selection.profileId ?? settings.defaultModelProfileId)
return {
@@ -107,13 +145,16 @@ export function applyRuntimeSelection(
}
}
const runtimeConfig = selection.runtimeConfig === true
const profile = selection.profileId
? requireProfile(settings, selection.profileId)
: selection.provider === 'opencode'
? settings.opencodeModelProfile
: selection.provider === 'continue'
? settings.continueModelProfile
: settings.deepseekHarnessModelProfile
: runtimeConfig
? undefined
: selection.provider === 'opencode'
? settings.opencodeModelProfile
: selection.provider === 'continue'
? settings.continueModelProfile
: settings.deepseekHarnessModelProfile
if (selection.provider === 'opencode') {
if (profile && !isAgentRuntimeModelProtocol(profile.protocol)) {
throw new Error(
@@ -137,8 +178,10 @@ export function applyRuntimeSelection(
}
if (selection.provider === 'deepseek-harness') {
const selectedProfile =
profile ?? settings.deepseekHarnessModelProfile
// DeepSeek Harness "own configuration" is the administrator-provided or fallback model.
const selectedProfile = runtimeConfig
? settings.deepseekHarnessPlatformModelProfile
: profile
if (
selectedProfile &&
!isDeepSeekHarnessModelProfile(selectedProfile)
+5
View File
@@ -94,6 +94,11 @@ export interface AgentRuntime {
readonly supportsToolExecution: boolean
/** Whether request-scoped GoodBuddy data tools can reach this runtime. */
readonly supportsScopedDataTools?: boolean
/**
* Whether `trustedInstructions` reach the model's system layer for every
* turn. Runtimes without this guarantee receive them inside the prompt.
*/
readonly consumesTrustedInstructions?: boolean
readonly capability?: 'chat' | 'image-generation'
getStatus(): Promise<AgentRuntimeStatus>
testConnection?(): Promise<AgentRuntimeStatus>
+71 -118
View File
@@ -2388,7 +2388,7 @@ describe('AssistantDatabase', () => {
database.close()
})
it('fails closed when an SSH project lacks a Runtime selection', async () => {
it('lets an SSH project without a Runtime selection follow global settings', async () => {
const directory = await mkdtemp(
join(tmpdir(), 'goodbuddy-invalid-ssh-project-')
)
@@ -2411,9 +2411,8 @@ describe('AssistantDatabase', () => {
)
raw.close()
expect(() => database.getProject(project.id)).toThrow(
'缺少远程 Runtime 选择'
)
// Resolution limits remote projects to OpenCode or Continue at run time.
expect(database.getProject(project.id).runtimeSelection).toBeUndefined()
database.close()
})
@@ -2478,12 +2477,6 @@ describe('AssistantDatabase', () => {
database.createSshProject(rootMismatch)
).toThrow('目录不匹配')
const missingRuntime = validatedSshProjectWrite()
delete missingRuntime.project.runtimeSelection
expect(() =>
database.createSshProject(missingRuntime)
).toThrow('必须选择 Runtime')
expect(() =>
database.createSshProject(
validatedSshProjectWrite({
@@ -2727,10 +2720,8 @@ describe('AssistantDatabase', () => {
rootPath: 'C:\\Users\\test'
},
defaultWorkMode: 'ask',
runtimeSelection: {
provider: 'model',
profileId: channelDefaultProfileId
},
// The model follows global settings instead of freezing today's default.
runtimeSelection: { provider: 'model' },
kind: 'channel',
channel: 'weixin'
}),
@@ -2760,7 +2751,7 @@ describe('AssistantDatabase', () => {
defaultWorkMode: 'execute',
runtimeSelection: {
provider: 'opencode',
profileId: '00000000-0000-4000-8000-000000000019'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000019' }
}
})
expect(updated).toMatchObject({
@@ -2774,7 +2765,7 @@ describe('AssistantDatabase', () => {
defaultWorkMode: 'execute',
runtimeSelection: {
provider: 'opencode',
profileId: '00000000-0000-4000-8000-000000000019'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000019' }
}
})
expect(() =>
@@ -5159,7 +5150,7 @@ describe('AssistantDatabase', () => {
projectId: project.id,
runtimeSelection: {
provider: 'model',
profileId: '00000000-0000-4000-8000-000000000299'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000299' }
},
knowledgeLibraryIds: [
'00000000-0000-4000-8000-000000000214'
@@ -5264,7 +5255,7 @@ describe('AssistantDatabase', () => {
projectId: project.id,
runtimeSelection: {
provider: 'model',
profileId: '00000000-0000-4000-8000-000000000299'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000299' }
},
knowledgeLibraryIds: [
'00000000-0000-4000-8000-000000000214'
@@ -5654,7 +5645,7 @@ describe('AssistantDatabase', () => {
projectId: project.id,
runtimeSelection: {
provider: 'model',
profileId: channelDefaultProfileId
model: { kind: 'profile', profileId: channelDefaultProfileId }
},
knowledgeLibraryIds: [
'00000000-0000-4000-8000-000000000559'
@@ -5765,7 +5756,7 @@ describe('AssistantDatabase', () => {
projectId: project.id,
runtimeSelection: {
provider: 'model',
profileId: channelDefaultProfileId
model: { kind: 'profile', profileId: channelDefaultProfileId }
},
knowledgeLibraryIds: [
'00000000-0000-4000-8000-000000000559'
@@ -6322,99 +6313,47 @@ describe('AssistantDatabase', () => {
database.close()
})
it('repairs unattended channel selections without rebinding ordinary conversations', async () => {
it('clears deleted model connections from every project and conversation layer', async () => {
const database = await createDatabase()
const removedProfileId =
'00000000-0000-4000-8000-000000000291'
const defaultProfileId =
'00000000-0000-4000-8000-000000000292'
const runtimeProfileId =
'00000000-0000-4000-8000-000000000293'
const imageProfileId =
'00000000-0000-4000-8000-000000000294'
const removedProfileId = '00000000-0000-4000-8000-000000000291'
const keptProfileId = '00000000-0000-4000-8000-000000000293'
const imageProfileId = '00000000-0000-4000-8000-000000000294'
database.replaceConversations(
([
['model', removedProfileId],
['opencode', removedProfileId],
['continue', removedProfileId],
['model', runtimeProfileId]
['continue', keptProfileId],
[undefined, removedProfileId]
] as const).map(([provider, profileId], index) => ({
id: `00000000-0000-4000-8000-00000000030${index}`,
runtimeSelection: { provider, profileId },
runtimeSelection: {
...(provider ? { provider } : {}),
model: { kind: 'profile' as const, profileId }
},
title: `对话 ${index}`,
updatedAt: index + 1,
messages: []
}))
)
const channelProject = database.ensureChannelProjects(
'C:\\Users\\test',
defaultProfileId
)[0]!
database.updateProject(channelProject.id, {
name: channelProject.name,
description: channelProject.description,
rootPath: channelProject.rootPath,
defaultWorkMode: channelProject.defaultWorkMode,
runtimeSelection: {
provider: 'opencode',
profileId: runtimeProfileId
}
const [channelProject, imageChannelProject] = database.ensureChannelProjects('C:\\Users\\test')
database.updateProject(channelProject!.id, {
...channelProject!,
runtimeSelection: { provider: 'opencode', model: { kind: 'profile', profileId: removedProfileId } }
})
const imageChannelProject = database.ensureChannelProjects(
'C:\\Users\\test',
defaultProfileId
)[1]!
database.updateProject(imageChannelProject.id, {
name: imageChannelProject.name,
description: imageChannelProject.description,
rootPath: imageChannelProject.rootPath,
defaultWorkMode: imageChannelProject.defaultWorkMode,
runtimeSelection: {
provider: 'model',
profileId: imageProfileId
}
database.updateProject(imageChannelProject!.id, {
...imageChannelProject!,
runtimeSelection: { provider: 'model', model: { kind: 'profile', profileId: imageProfileId } }
})
const automaticChannelProject = database.ensureChannelProjects(
'C:\\Users\\test',
defaultProfileId
)[2]!
database.updateProject(automaticChannelProject.id, {
name: automaticChannelProject.name,
description: automaticChannelProject.description,
rootPath: automaticChannelProject.rootPath,
defaultWorkMode: automaticChannelProject.defaultWorkMode,
runtimeSelection: { provider: 'auto' }
const localProject = database.createProject({
name: '本地', description: '', rootPath: 'C:\\Local', defaultWorkMode: 'ask',
runtimeSelection: { model: { kind: 'profile', profileId: removedProfileId } }
})
const automaticRemoteConversation =
database.getOrCreateRemoteConversation({
projectId: automaticChannelProject.id,
channel: 'dingtalk',
accountId: 'default',
externalConversationId: 'legacy-auto-conversation',
conversationType: 'direct',
title: '钉钉 · 旧版自动后端',
accountDisplay: '发送者 ****0001',
runtimeSelection: { provider: 'auto' }
})
expect(
database.repairConversationRuntimeSelections({
modelProfiles: [
{ id: defaultProfileId },
{ id: runtimeProfileId },
{
id: imageProfileId,
protocol: 'openai-images-generations'
}
],
defaultModelProfileId: defaultProfileId,
opencodeModelSource: {
kind: 'profile',
profileId: runtimeProfileId
},
continueModelSource: { kind: 'platform' }
modelProfiles: [{ id: keptProfileId }, { id: imageProfileId, protocol: 'openai-images-generations' }]
})
).toBe(3)
).toBe(5)
expect(
database
.listConversations()
@@ -6422,35 +6361,49 @@ describe('AssistantDatabase', () => {
.sort((left, right) => left.title.localeCompare(right.title))
.map((conversation) => conversation.runtimeSelection)
).toEqual([
{ provider: 'model', profileId: removedProfileId },
{ provider: 'opencode', profileId: removedProfileId },
{ provider: 'continue', profileId: removedProfileId },
{ provider: 'model', profileId: runtimeProfileId }
{ provider: 'model' },
{ provider: 'opencode' },
{ provider: 'continue', model: { kind: 'profile', profileId: keptProfileId } },
undefined
])
expect(database.getProject(channelProject.id).runtimeSelection).toEqual({
provider: 'opencode',
profileId: runtimeProfileId
})
expect(
database.getProject(imageChannelProject.id).runtimeSelection
).toEqual({
provider: 'model'
})
expect(
database.getProject(automaticChannelProject.id).runtimeSelection
).toEqual({
provider: 'model'
})
expect(
database.getConversation(
automaticRemoteConversation.id
).runtimeSelection
).toEqual({
provider: 'model'
expect(database.getProject(channelProject!.id).runtimeSelection).toEqual({ provider: 'opencode' })
// An existing but incompatible model is kept so the UI can explain the fallback.
expect(database.getProject(imageChannelProject!.id).runtimeSelection).toEqual({
provider: 'model', model: { kind: 'profile', profileId: imageProfileId }
})
expect(database.getProject(localProject.id).runtimeSelection).toBeUndefined()
database.close()
})
it('migrates legacy stored selections to layers', async () => {
const directory = await mkdtemp(join(tmpdir(), 'goodbuddy-runtime-layer-migration-'))
temporaryDirectories.push(directory)
const databasePath = join(directory, 'assistant.sqlite')
const database = new AssistantDatabase(databasePath)
database.initialize('C:\\Workspace')
const project = database.listProjects()[0]!
database.close()
const raw = new DatabaseSync(databasePath)
raw.prepare('UPDATE projects SET runtime_selection_json = ? WHERE id = ?')
.run(JSON.stringify({ provider: 'continue', profileId: '00000000-0000-4000-8000-000000000301' }), project.id)
raw.prepare(
`INSERT INTO conversations (id, project_id, runtime_selection_json, work_mode, title, status, created_at, updated_at)
VALUES (?, ?, ?, 'ask', 'legacy', 'active', ?, ?)`
).run('00000000-0000-4000-8000-000000000302', project.id, JSON.stringify({ provider: 'auto' }), '2026-01-01', '2026-01-01')
raw.exec('PRAGMA user_version = 48')
raw.close()
const reopened = new AssistantDatabase(databasePath)
reopened.initialize('C:\\Workspace')
const check = new DatabaseSync(databasePath)
expect(check.prepare('SELECT runtime_selection_json AS value FROM projects WHERE id = ?').get(project.id))
.toEqual({ value: JSON.stringify({ provider: 'continue', model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000301' } }) })
expect(check.prepare('SELECT runtime_selection_json AS value FROM conversations WHERE id = ?').get('00000000-0000-4000-8000-000000000302'))
.toEqual({ value: null })
check.close()
reopened.close()
})
it('durably interrupts active tool and subagent metadata during startup recovery', async () => {
const directory = await mkdtemp(
join(tmpdir(), 'goodbuddy-conversation-recovery-')
+119 -120
View File
@@ -83,10 +83,14 @@ import {
} from '../../shared/channel-contracts'
import {
agentRuntimeSelectionKey,
agentRuntimeSelectionSchema,
repairChannelRuntimeSelection,
compactRuntimeSelectionLayer,
optionalAgentRuntimeSelectionSchema,
repairRuntimeSelectionLayer,
runtimeSelectionLayerSchema,
withoutLegacyAutoSelection,
type AgentRuntimeSelection,
type RuntimeSelectionRepairSettings
type RuntimeResolutionSettings,
type RuntimeSelectionLayer
} from '../../shared/runtime-selection-contracts'
import {
MAGIC_NOTE_MAX_NOTE_EMBED_BYTES,
@@ -126,7 +130,7 @@ import {
SUBAGENT_PROGRESS_STORAGE_SCHEMA_VERSION
} from './subagent-progress-storage'
export const ASSISTANT_DATABASE_SCHEMA_VERSION = 48
export const ASSISTANT_DATABASE_SCHEMA_VERSION = 49
export type RemoteTaskEventInput = {
taskId: string
@@ -318,22 +322,69 @@ type MessageMetadata = {
const MAX_CHANNEL_OUTBOX_RETRY_BYTES = 20 * 1024 * 1024
const MAX_CHANNEL_OUTBOX_MEDIA_ENTRIES = 8
function parseRuntimeSelection(value: string | null):
| ConversationSnapshot['runtimeSelection']
| undefined {
/** Rewrites pre-layer `{ provider, profileId }` and `{ provider: 'auto' }` rows. */
function migrateRuntimeSelectionLayers(database: DatabaseSync): void {
for (const table of ['projects', 'conversations'] as const) {
const rows = database
.prepare(
`SELECT id, runtime_selection_json FROM ${table}
WHERE runtime_selection_json IS NOT NULL`
)
.all() as Array<{ id: string; runtime_selection_json: string }>
const update = database.prepare(
`UPDATE ${table} SET runtime_selection_json = ? WHERE id = ?`
)
for (const row of rows) {
const next = serializeRuntimeSelection(
parseRuntimeSelection(row.runtime_selection_json)
)
if (next !== row.runtime_selection_json) update.run(next, row.id)
}
}
}
/** Reads a stored layer; legacy concrete and `auto` selections upgrade on read. */
function parseRuntimeSelection(
value: string | null
): RuntimeSelectionLayer | undefined {
if (!value) {
return undefined
}
try {
const parsed = agentRuntimeSelectionSchema.safeParse(
JSON.parse(value)
)
return parsed.success ? parsed.data : undefined
const parsed = runtimeSelectionLayerSchema.safeParse(JSON.parse(value))
return parsed.success ? compactRuntimeSelectionLayer(parsed.data) : undefined
} catch {
return undefined
}
}
function serializeRuntimeSelection(
layer: RuntimeSelectionLayer | undefined
): string | null {
// Normalize legacy `{ provider, profileId }` input before storing it.
const parsed = runtimeSelectionLayerSchema.safeParse(layer)
const compact = parsed.success ? compactRuntimeSelectionLayer(parsed.data) : undefined
return compact ? JSON.stringify(compact) : null
}
/** Remote events record the concrete selection they ran with for context metrics keys. */
function remoteEventSelectionKey(
requestSelection: AgentRuntimeSelection | undefined,
conversationLayer: RuntimeSelectionLayer | undefined,
projectLayer: RuntimeSelectionLayer | undefined
): string {
if (requestSelection) return agentRuntimeSelectionKey(requestSelection)
const provider = conversationLayer?.provider ?? projectLayer?.provider ?? 'opencode'
const model = conversationLayer?.model ?? projectLayer?.model
return agentRuntimeSelectionKey(
model?.kind === 'profile'
? { provider, profileId: model.profileId }
: model?.kind === 'runtime-config' && provider !== 'model'
? { provider, runtimeConfig: true }
: { provider }
)
}
type ArtifactRow = {
id: string
project_id: string | null
@@ -576,18 +627,7 @@ function toProject(row: ProjectRow): AssistantProject {
if (!executionSpace.success) {
throw new Error(`项目 ${row.id} 的执行空间配置无效`)
}
const runtimeSelection =
row.kind === 'channel'
? parseRuntimeSelection(row.runtime_selection_json) ?? {
provider: 'auto' as const
}
: parseRuntimeSelection(row.runtime_selection_json)
if (
executionSpace.data.kind === 'ssh' &&
runtimeSelection === undefined
) {
throw new Error(`项目 ${row.id} 缺少远程 Runtime 选择`)
}
const runtimeSelection = parseRuntimeSelection(row.runtime_selection_json)
const rootPath =
executionSpace.data.kind === 'local'
? executionSpace.data.rootPath
@@ -764,8 +804,8 @@ function toSchedule(row: ScheduleWithTaskRow): AssistantSchedule {
workMode: LegacyWorkMode
runtimeSelection?: unknown
}
const runtimeSelection = agentRuntimeSelectionSchema.safeParse(
template.runtimeSelection
const runtimeSelection = optionalAgentRuntimeSelectionSchema.safeParse(
withoutLegacyAutoSelection(template.runtimeSelection)
)
const recurrence = JSON.parse(row.recurrence_json) as {
type: AssistantSchedule['recurrence']
@@ -1666,9 +1706,6 @@ function normalizeSshProjectWrite(
if (project.rootPath !== executionSpace.remoteRootPath) {
throw new Error('项目目录与远程执行空间目录不匹配')
}
if (project.runtimeSelection === undefined) {
throw new Error('远程项目必须选择 Runtime')
}
if (typeof write.assertCurrent !== 'function') {
throw new TypeError('缺少 SSH Host 事务前置校验')
}
@@ -2334,8 +2371,10 @@ export class AssistantDatabase {
ensureChannelProjects(
defaultRootPath: string,
defaultModelProfileId: string
_defaultModelProfileId?: string
): AssistantProject[] {
// Kept for callers; channel projects no longer pin the current default model.
void _defaultModelProfileId
const database = this.requireDatabase()
const definitions: ReadonlyArray<{
channel: ProjectChannel
@@ -2387,10 +2426,8 @@ export class AssistantDatabase {
definition.name,
definition.description,
defaultRootPath,
JSON.stringify({
provider: 'model',
profileId: defaultModelProfileId
}),
// Unattended channels start on the direct model; the model follows global settings.
JSON.stringify({ provider: 'model' }),
definition.channel,
now,
now
@@ -2441,9 +2478,7 @@ export class AssistantDatabase {
input.description,
input.rootPath,
input.defaultWorkMode,
input.runtimeSelection
? JSON.stringify(input.runtimeSelection)
: null,
serializeRuntimeSelection(input.runtimeSelection),
builtInDefault ? 1 : 0,
now,
now
@@ -2493,11 +2528,8 @@ export class AssistantDatabase {
input.description,
input.rootPath,
input.defaultWorkMode,
input.runtimeSelection || current.runtimeSelection
? JSON.stringify(
input.runtimeSelection ?? current.runtimeSelection
)
: null,
// The settings form always sends the full layer; absent means follow global.
serializeRuntimeSelection(input.runtimeSelection),
new Date().toISOString(),
projectId
)
@@ -2547,9 +2579,7 @@ export class AssistantDatabase {
normalized.project.description,
normalized.executionSpace.remoteRootPath,
normalized.project.defaultWorkMode,
normalized.project.runtimeSelection
? JSON.stringify(normalized.project.runtimeSelection)
: null,
serializeRuntimeSelection(normalized.project.runtimeSelection),
now,
now
)
@@ -2640,9 +2670,7 @@ export class AssistantDatabase {
normalized.project.description,
normalized.executionSpace.remoteRootPath,
normalized.project.defaultWorkMode,
normalized.project.runtimeSelection
? JSON.stringify(normalized.project.runtimeSelection)
: null,
serializeRuntimeSelection(normalized.project.runtimeSelection),
updatedAt,
projectId,
expectedUpdatedAt
@@ -2988,32 +3016,29 @@ export class AssistantDatabase {
}
}
/**
* Clears references to deleted model connections in every project and
* conversation so they fall back to the next layer instead of failing.
* Also rewrites legacy stored shapes to the layered form.
*/
repairConversationRuntimeSelections(
settings: RuntimeSelectionRepairSettings
settings: Pick<RuntimeResolutionSettings, 'modelProfiles'>
): number {
const database = this.requireDatabase()
const projects = database
.prepare(
`SELECT id, runtime_selection_json
FROM projects
WHERE kind = 'channel'`
WHERE runtime_selection_json IS NOT NULL`
)
.all() as Array<{
id: string
runtime_selection_json: string | null
}>
.all() as Array<{ id: string; runtime_selection_json: string }>
const conversations = database
.prepare(
`SELECT id, runtime_selection_json, channel
`SELECT id, runtime_selection_json
FROM conversations
WHERE runtime_selection_json IS NOT NULL
AND channel IS NOT NULL`
WHERE runtime_selection_json IS NOT NULL`
)
.all() as Array<{
id: string
runtime_selection_json: string
channel: ProjectChannel | null
}>
.all() as Array<{ id: string; runtime_selection_json: string }>
const update = database.prepare(
`UPDATE conversations
SET runtime_selection_json = ?
@@ -3024,44 +3049,25 @@ export class AssistantDatabase {
SET runtime_selection_json = ?, updated_at = ?
WHERE id = ?`
)
const repairedJson = (stored: string): string | null | false => {
const next = serializeRuntimeSelection(
repairRuntimeSelectionLayer(parseRuntimeSelection(stored), settings)
)
return next === stored ? false : next
}
let repaired = 0
database.exec('BEGIN IMMEDIATE')
try {
for (const project of projects) {
const stored = parseRuntimeSelection(
project.runtime_selection_json
)
const current = stored ?? { provider: 'auto' as const }
const next = repairChannelRuntimeSelection(current, settings)
if (
stored &&
agentRuntimeSelectionKey(next) ===
agentRuntimeSelectionKey(current)
) {
continue
}
updateProject.run(
JSON.stringify(next),
new Date().toISOString(),
project.id
)
const next = repairedJson(project.runtime_selection_json)
if (next === false) continue
updateProject.run(next, new Date().toISOString(), project.id)
repaired += 1
}
for (const conversation of conversations) {
const current = parseRuntimeSelection(
conversation.runtime_selection_json
)
if (!current) {
continue
}
const next = repairChannelRuntimeSelection(current, settings)
if (
agentRuntimeSelectionKey(next) ===
agentRuntimeSelectionKey(current)
) {
continue
}
update.run(JSON.stringify(next), conversation.id)
const next = repairedJson(conversation.runtime_selection_json)
if (next === false) continue
update.run(next, conversation.id)
repaired += 1
}
database.exec('COMMIT')
@@ -3107,9 +3113,7 @@ export class AssistantDatabase {
insertConversation.run(
conversation.id,
conversation.projectId ?? null,
conversation.runtimeSelection
? JSON.stringify(conversation.runtimeSelection)
: null,
serializeRuntimeSelection(conversation.runtimeSelection),
conversation.knowledgeRetrievalMode ?? null,
serializeConversationContextState(conversation),
conversation.title,
@@ -3202,9 +3206,7 @@ export class AssistantDatabase {
if (existingConversation) {
const result = updateConversation.run(
header.projectId ?? null,
header.runtimeSelection
? JSON.stringify(header.runtimeSelection)
: null,
serializeRuntimeSelection(header.runtimeSelection),
header.knowledgeRetrievalMode ?? null,
serializeConversationContextState(header),
header.title,
@@ -3220,9 +3222,7 @@ export class AssistantDatabase {
insertConversation.run(
header.id,
header.projectId ?? null,
header.runtimeSelection
? JSON.stringify(header.runtimeSelection)
: null,
serializeRuntimeSelection(header.runtimeSelection),
header.knowledgeRetrievalMode ?? null,
serializeConversationContextState(header),
header.title,
@@ -3525,7 +3525,7 @@ export class AssistantDatabase {
conversationType: 'direct' | 'group'
title: string
accountDisplay: string
runtimeSelection?: AgentRuntimeSelection
runtimeSelection?: RuntimeSelectionLayer
}): ConversationSnapshot {
const database = this.requireDatabase()
const existing = database
@@ -3556,9 +3556,7 @@ export class AssistantDatabase {
input.title,
input.conversationType,
input.accountDisplay,
input.runtimeSelection
? JSON.stringify(input.runtimeSelection)
: null,
serializeRuntimeSelection(input.runtimeSelection),
new Date().toISOString(),
existing.id
)
@@ -3578,9 +3576,7 @@ export class AssistantDatabase {
.run(
id,
input.projectId,
input.runtimeSelection
? JSON.stringify(input.runtimeSelection)
: null,
serializeRuntimeSelection(input.runtimeSelection),
input.title,
input.channel,
input.accountId,
@@ -5873,9 +5869,7 @@ export class AssistantDatabase {
input.assistantMessageId
)
const requestRuntimeSelection =
input.runtimeSelection === undefined
? undefined
: agentRuntimeSelectionSchema.parse(input.runtimeSelection)
optionalAgentRuntimeSelectionSchema.parse(input.runtimeSelection)
const publicEvent = stripRemoteEventProvenance(input.event)
if (publicEvent.requestId !== input.taskId) {
throw new Error('远程事件的请求 ID 与任务不匹配')
@@ -6012,17 +6006,16 @@ export class AssistantDatabase {
let contextState = parseConversationContextState(
conversation.context_state_json
)
const runtimeSelection =
requestRuntimeSelection ??
parseRuntimeSelection(conversation.runtime_selection_json) ??
parseRuntimeSelection(conversation.project_runtime_selection_json) ??
{ provider: 'auto' as const }
const runtimeSelectionKey = remoteEventSelectionKey(
requestRuntimeSelection,
parseRuntimeSelection(conversation.runtime_selection_json),
parseRuntimeSelection(conversation.project_runtime_selection_json)
)
if (publicEvent.type === 'context-metrics') {
contextState = {
...contextState,
contextMetrics: {
runtimeSelectionKey:
agentRuntimeSelectionKey(runtimeSelection),
runtimeSelectionKey,
contextTokens: publicEvent.contextTokens,
source: publicEvent.source,
basis: 'model-call'
@@ -6046,8 +6039,7 @@ export class AssistantDatabase {
estimatedAfterTokens !== undefined
? {
contextMetrics: {
runtimeSelectionKey:
agentRuntimeSelectionKey(runtimeSelection),
runtimeSelectionKey,
contextTokens: estimatedAfterTokens,
source: 'estimated' as const,
basis: 'conversation' as const
@@ -11569,6 +11561,13 @@ export class AssistantDatabase {
database.exec('PRAGMA user_version = 48; COMMIT;')
} catch (error) { database.exec('ROLLBACK'); throw error }
}
if (version.user_version < 49) {
database.exec('BEGIN IMMEDIATE')
try {
migrateRuntimeSelectionLayers(database)
database.exec('PRAGMA user_version = 49; COMMIT;')
} catch (error) { database.exec('ROLLBACK'); throw error }
}
}
supervisionReviewStore(): SupervisionReviewStore {
+8 -5
View File
@@ -32,6 +32,7 @@ import {
applyRuntimeSelection,
getConfiguredRuntimeTarget,
resolveConfiguredAgentRuntimeSelection,
resolveLayeredRuntimeSelection,
type SelectedRuntimeTarget
} from './agent/runtime-selection'
import { CapabilityService } from './capabilities/capability-service'
@@ -709,11 +710,13 @@ if (hasSingleInstanceLock) {
appPath: app.getAppPath(),
resourcesPath: process.resourcesPath,
packaged: app.isPackaged,
resolveRuntimeSelection: async (selection) =>
resolveConfiguredAgentRuntimeSelection(
await settingsStore.getResolvedSettings(),
selection
),
resolveRuntimeSelection: async (layer) => {
const settings = await settingsStore.getResolvedSettings()
return resolveConfiguredAgentRuntimeSelection(
settings,
resolveLayeredRuntimeSelection(settings, { project: layer }, { remote: true }).selection
)
},
resolveModelProfile: async (selection) => {
const resolved = applyRuntimeSelection(
await settingsStore.getResolvedSettings(),
+59 -31
View File
@@ -4559,7 +4559,10 @@ describe('registerIpcHandlers local conversation persistence', () => {
const database = {
queueDueSchedules: vi.fn(() => []), listConversationQueueItems: vi.fn(() => []),
listPendingConversationQueueIds: vi.fn(() => []),
getConversation: vi.fn(() => ({ id: conversationId, runtimeSelection: selection, messages })),
// Stored conversations hold a layer; the renderer sends the resolved selection.
getConversation: vi.fn(() => ({ id: conversationId, runtimeSelection: {
provider: 'model', model: { kind: 'profile', profileId: selection.profileId }
}, messages })),
createTask: vi.fn(), updateTaskStatus: vi.fn()
}
const compactor = {
@@ -4714,7 +4717,7 @@ describe('registerIpcHandlers local conversation persistence', () => {
JSON.stringify({
input: {
conversationId,
runtimeSelection: { provider: 'auto' },
runtimeSelection: { provider: 'model' },
workMode: 'ask',
includeMemoryContext: true,
prompt: queuedItem.label,
@@ -5229,7 +5232,8 @@ describe('registerIpcHandlers Runtime customization', () => {
requestId,
conversationId,
projectId,
runtimeSelection: { provider: 'opencode' as const },
// The renderer sends the resolved selection; OpenCode here uses its own configuration.
runtimeSelection: { provider: 'opencode' as const, runtimeConfig: true as const },
history: messages.map(({ role, content }) => ({
role,
content
@@ -5429,7 +5433,7 @@ describe('registerIpcHandlers agent terminal state', () => {
defaultWorkMode: 'ask',
runtimeSelection: {
provider: 'model',
profileId: '00000000-0000-4000-8000-000000000001'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000001' }
},
kind: 'channel',
channel: 'wecom',
@@ -7316,8 +7320,18 @@ describe('registerIpcHandlers agent terminal state', () => {
}
}
const { harness, projectId } = createManagedSshHarness(selectedRuntime)
harness.getResolvedSettings.mockResolvedValue({
provider: 'opencode', defaultModelProfileId: selection.profileId, opencodeBaseUrl: '',
modelProfiles: [{
id: selection.profileId, name: 'Remote', modelName: 'remote', baseUrl: 'https://models.example/v1',
protocol: 'openai-chat-completions', authentication: 'none'
}]
})
const project = harness.assistantDatabase.getProject(projectId)
harness.assistantDatabase.getProject.mockReturnValue({ ...project, runtimeSelection: selection })
harness.assistantDatabase.getProject.mockReturnValue({
...project,
runtimeSelection: { provider: 'opencode', model: { kind: 'profile', profileId: selection.profileId } }
})
harness.assistantDatabase.getConversation.mockReturnValue({
id: conversationId, projectId, messages: []
})
@@ -8395,7 +8409,7 @@ describe('registerIpcHandlers agent terminal state', () => {
{
requestId: '00000000-0000-4000-8000-000000000728',
conversationId,
runtimeSelection: { provider: 'auto' },
runtimeSelection: { provider: 'model' },
prompt: '等待 Runtime',
workMode: 'ask',
knowledgeLibraryIds: []
@@ -8409,7 +8423,7 @@ describe('registerIpcHandlers agent terminal state', () => {
harness.handler?.(trustedEvent(harness.webContents), {
requestId: '00000000-0000-4000-8000-000000000729',
conversationId,
runtimeSelection: { provider: 'auto' },
runtimeSelection: { provider: 'model' },
prompt: '不能并发',
workMode: 'ask',
knowledgeLibraryIds: []
@@ -8468,7 +8482,7 @@ describe('registerIpcHandlers agent terminal state', () => {
}
const input = {
conversationId,
runtimeSelection: { provider: 'auto' as const },
runtimeSelection: { provider: 'model' as const },
workMode: 'ask' as const,
includeMemoryContext: true,
prompt: item.label,
@@ -8521,7 +8535,7 @@ describe('registerIpcHandlers agent terminal state', () => {
try {
await expect(electronMocks.handlers.get(ipcChannels.conversationQueueEnqueueUser)?.(trustedEvent(harness.webContents), {
conversationId: '00000000-0000-4000-8000-000000000731',
runtimeSelection: { provider: 'auto' }, workMode: 'ask', prompt: 'Read the picture',
workMode: 'ask', prompt: 'Read the picture',
attachments: [{ id: '00000000-0000-4000-8000-000000000734', name: 'image.png', size: 10, preview: '', kind: 'image' }],
knowledgeLibraryIds: [], knowledgeRetrievalMode: 'auto'
})).rejects.toThrow('图片输入')
@@ -8550,7 +8564,7 @@ describe('registerIpcHandlers agent terminal state', () => {
'00000000-0000-4000-8000-000000000734'
const input = {
conversationId,
runtimeSelection: { provider: 'auto' as const },
runtimeSelection: { provider: 'model' as const },
workMode: 'ask' as const,
includeMemoryContext: true,
prompt: '排队发送',
@@ -8655,7 +8669,7 @@ describe('registerIpcHandlers agent terminal state', () => {
const itemId = '00000000-0000-4000-8000-000000000746'
const input = {
conversationId,
runtimeSelection: { provider: 'auto' as const },
runtimeSelection: { provider: 'model' as const },
workMode: 'ask' as const,
includeMemoryContext: true,
prompt: '需要重新排队',
@@ -8721,7 +8735,7 @@ describe('registerIpcHandlers agent terminal state', () => {
await harness.handler?.(trustedEvent(harness.webContents), {
requestId: '00000000-0000-4000-8000-000000000748',
conversationId,
runtimeSelection: { provider: 'auto' },
runtimeSelection: { provider: 'model' },
prompt: '失败后的新消息',
workMode: 'ask',
knowledgeLibraryIds: []
@@ -8750,7 +8764,7 @@ describe('registerIpcHandlers agent terminal state', () => {
const createItem = (id: string, prompt: string) => {
const input = {
conversationId,
runtimeSelection: { provider: 'auto' as const },
runtimeSelection: { provider: 'model' as const },
workMode: 'ask' as const,
includeMemoryContext: true,
prompt,
@@ -9095,7 +9109,7 @@ describe('registerIpcHandlers agent terminal state', () => {
defaultModelProfileId: profileId, deepseekHarnessModelSource: { kind: 'profile', profileId }
}))
const project = database.createProject({ name: 'Native UI', description: '', rootPath: root,
defaultWorkMode: 'ask', runtimeSelection: { provider: 'deepseek-harness', profileId } })
defaultWorkMode: 'ask', runtimeSelection: { provider: 'deepseek-harness', model: { kind: 'profile', profileId } } })
const conversationId = '00000000-0000-4000-8000-000000000222'
const now = Date.now()
const save = async (): Promise<string> => {
@@ -9183,7 +9197,7 @@ describe('registerIpcHandlers agent terminal state', () => {
[`${provider}ModelSource`]: { kind: 'profile', profileId }
}))
}
database.updateProject(project.id, { ...project, runtimeSelection: { provider, profileId } })
database.updateProject(project.id, { ...project, runtimeSelection: { provider, model: { kind: 'profile', profileId } } })
const onTerminal = vi.fn<(value: import('../shared/terminal-contracts').TerminalSnapshot) => void>()
notify.mockClear()
view.rerender(createElement(RuntimeNativeClientActions, { browser: false, contextKey: `${project.id}:${provider}`,
@@ -10661,8 +10675,10 @@ describe('registerIpcHandlers agent terminal state', () => {
).resolves.toEqual(
expect.objectContaining({ label: 'model-two' })
)
// Resolved selections name the model source explicitly.
expect(selectedRuntimes.getStatus).toHaveBeenLastCalledWith({
provider: 'opencode'
provider: 'opencode',
runtimeConfig: true
})
expect(fallbackRuntime.getStatus).not.toHaveBeenCalled()
@@ -10726,21 +10742,36 @@ describe('registerIpcHandlers agent terminal state', () => {
}
const harness = createHarness(requestRuntime, undefined, 'always', undefined, false, selectedRuntimes)
const projectId = '00000000-0000-4000-8000-000000000101'
const fixed = { provider: 'continue', profileId: '00000000-0000-4000-8000-000000000042' }
const fixedProfileId = '00000000-0000-4000-8000-000000000042'
const project = { id: projectId, rootPath: 'C:\\ProjectWorkspace' }
harness.getResolvedSettings.mockResolvedValue({
provider: 'model', defaultModelProfileId: fixedProfileId, opencodeBaseUrl: '',
modelProfiles: [{
id: fixedProfileId, name: 'Fixed', modelName: 'fixed', baseUrl: 'https://models.example/v1',
protocol: 'openai-chat-completions', authentication: 'none'
}]
})
harness.assistantDatabase.getProject.mockReturnValue({ ...project, runtimeSelection: { provider: 'opencode' } })
const event = trustedEvent(harness.webContents)
await harness.handler?.(event, {
projectId, conversationId: 'project-rule-one',
requestId: '00000000-0000-4000-8000-000000000011', prompt: 'one', workMode: 'ask'
})
expect(selectedRuntimes.getRuntime).toHaveBeenLastCalledWith({ provider: 'opencode' }, expect.anything())
harness.assistantDatabase.getProject.mockReturnValue({ ...project, runtimeSelection: fixed })
// No project model: OpenCode uses its global default (own configuration here).
expect(selectedRuntimes.getRuntime).toHaveBeenLastCalledWith(
{ provider: 'opencode', runtimeConfig: true }, expect.anything()
)
harness.assistantDatabase.getProject.mockReturnValue({
...project,
runtimeSelection: { provider: 'continue', model: { kind: 'profile', profileId: fixedProfileId } }
})
await harness.handler?.(event, {
projectId, conversationId: 'project-rule-two',
requestId: '00000000-0000-4000-8000-000000000012', prompt: 'two', workMode: 'ask'
})
expect(selectedRuntimes.getRuntime).toHaveBeenLastCalledWith(fixed, expect.anything())
expect(selectedRuntimes.getRuntime).toHaveBeenLastCalledWith(
{ provider: 'continue', profileId: fixedProfileId }, expect.anything()
)
await harness.handler?.(event, {
projectId, conversationId: 'project-rule-three', runtimeSelection: { provider: 'model' },
requestId: '00000000-0000-4000-8000-000000000013', prompt: 'three', workMode: 'ask'
@@ -11803,7 +11834,7 @@ describe('registerIpcHandlers agent terminal state', () => {
defaultWorkMode: 'ask',
runtimeSelection: {
provider: 'model',
profileId: '00000000-0000-4000-8000-000000000001'
model: { kind: 'profile', profileId: '00000000-0000-4000-8000-000000000001' }
},
kind: 'channel',
channel,
@@ -12231,15 +12262,11 @@ describe('registerIpcHandlers agent terminal state', () => {
expect.any(String),
'waiting_approval'
)
// Channel conversations inherit the project's choice instead of copying it.
expect(
harness.assistantDatabase.getOrCreateRemoteConversation
).toHaveBeenCalledWith(
expect.objectContaining({
runtimeSelection: {
provider: 'model',
profileId: '00000000-0000-4000-8000-000000000001'
}
})
expect.not.objectContaining({ runtimeSelection: expect.anything() })
)
await harness.dispose()
})
@@ -12561,7 +12588,10 @@ describe('registerIpcHandlers agent terminal state', () => {
toolApproval: 'always',
subagentSmartRoutingEnabled: false,
continueModelProfile: { id: configuredProfileId },
modelProfiles: [{ id: configuredProfileId }]
modelProfiles: [{
id: configuredProfileId, baseUrl: 'https://models.example/v1',
protocol: 'openai-chat-completions', authentication: 'none'
}]
})
vi.mocked(
harness.assistantDatabase.listProjects
@@ -12617,9 +12647,7 @@ describe('registerIpcHandlers agent terminal state', () => {
expect(
harness.assistantDatabase.getOrCreateRemoteConversation
).toHaveBeenCalledWith(
expect.objectContaining({
runtimeSelection: { provider: 'continue' }
})
expect.not.objectContaining({ runtimeSelection: expect.anything() })
)
expect(receivedAuthorize).toBeUndefined()
expect(harness.approvalBroker.request).not.toHaveBeenCalled()
+67 -41
View File
@@ -121,9 +121,10 @@ import {
} from '../shared/document-parsing-contracts'
import {
agentRuntimeSelectionKey,
agentRuntimeSelectionSchema,
getDefaultRuntimeSelection,
type AgentRuntimeSelection
optionalAgentRuntimeSelectionSchema,
withoutLegacyAutoSelection,
type AgentRuntimeSelection,
type RuntimeSelectionLayer
} from '../shared/runtime-selection-contracts'
import { registerMagicNotesAnalysisIpcHandlers, registerMagicTodosAnalysisIpcHandlers } from './magic-notes/magic-notes-analysis-ipc'
import { registerMagicNotesIpcHandlers, registerMagicTodosIpcHandlers } from './magic-notes/magic-notes-ipc'
@@ -146,6 +147,7 @@ import {
expertCreateSchema,
type AssistantSchedule,
type AssistantArtifact,
type AssistantProject,
type ConversationAttachment
} from '../shared/assistant-contracts'
import {
@@ -168,7 +170,8 @@ import {
} from './agent/create-runtime'
import {
applyRuntimeSelection,
resolveConfiguredAgentRuntimeSelection
resolveConfiguredAgentRuntimeSelection,
resolveLayeredRuntimeSelection
} from './agent/runtime-selection'
import { safeToolErrorDetail } from './agent/approval-summary'
import { ReasoningTagStreamParser } from './agent/reasoning-stream'
@@ -1177,6 +1180,24 @@ export function registerIpcHandlers(
)
}
})
/** Resolves stored project and conversation layers against current settings. */
const resolveStoredRuntimeSelection = async (
input: {
projectId?: string
/** Callers that already loaded the project pass it to avoid a second read. */
project?: Pick<AssistantProject, 'runtimeSelection' | 'executionSpace'>
conversationLayer?: RuntimeSelectionLayer
}
): Promise<AgentRuntimeSelection> => {
const project =
input.project ??
(input.projectId ? assistantDatabase.getProject(input.projectId) : undefined)
return resolveLayeredRuntimeSelection(
await settingsStore.getResolvedSettings(),
{ project: project?.runtimeSelection, conversation: input.conversationLayer },
{ remote: project?.executionSpace?.kind === 'ssh' }
).selection
}
const resolveRequestRuntime = async (
request: Pick<
AgentRequest,
@@ -1200,14 +1221,15 @@ export function registerIpcHandlers(
if (executionSpace?.kind === 'ssh' && !selectedRuntimes) {
throw new Error(REMOTE_EXECUTION_SPACE_UNAVAILABLE)
}
let selection = request.runtimeSelection ?? project?.runtimeSelection
if (
!selectedRuntimes ||
(!selection && !executionSpace)
(!request.runtimeSelection && !project?.runtimeSelection && !executionSpace)
) {
return runtime
}
selection ??= { provider: 'auto' }
let selection =
request.runtimeSelection ??
(await resolveStoredRuntimeSelection({ projectId: project?.id }))
if (request.followConfiguredAgentRuntime) {
selection = resolveConfiguredAgentRuntimeSelection(
await settingsStore.getResolvedSettings(),
@@ -1221,8 +1243,8 @@ export function registerIpcHandlers(
if (selected.capability === 'image-generation') return
const target = runtimeTargetFor(selected)
const configured = await settingsStore.getResolvedSettings()
const projectSelection = request.projectId ? assistantDatabase.getProject(request.projectId).runtimeSelection : undefined
const selection = request.runtimeSelection ?? projectSelection
const selection = request.runtimeSelection ??
(request.projectId ? await resolveStoredRuntimeSelection({ projectId: request.projectId }) : undefined)
const effective = selection ? applyRuntimeSelection(configured, selection).settings : configured
const supported = target === 'model' ? effective.supportsImageInput
: target === 'opencode' ? effective.opencodeModelProfile?.supportsImageInput
@@ -1695,11 +1717,10 @@ export function registerIpcHandlers(
const toolStates = new Map(
recoveredTools.map((tool) => [tool.callId, tool])
)
const project = assistantDatabase.getProject(task.projectId)
const runtimeSelection =
conversation.runtimeSelection ??
project.runtimeSelection ??
({ provider: 'auto' } as const)
const runtimeSelection = await resolveStoredRuntimeSelection({
projectId: task.projectId,
conversationLayer: conversation.runtimeSelection
})
const controller = new AbortController()
const lease = leaseActiveRequest(
task.taskId,
@@ -2005,7 +2026,7 @@ export function registerIpcHandlers(
input: unknown
serializedContexts?: unknown
}
const input = conversationQueueUserInputSchema.parse(stored.input)
const input = conversationQueueUserInputSchema.parse(withoutLegacyAutoSelection(stored.input))
if (
stored.serializedContexts !== undefined &&
typeof stored.serializedContexts !== 'string'
@@ -2020,7 +2041,7 @@ export function registerIpcHandlers(
}
return input
}
return conversationQueueUserInputSchema.parse(parsed)
return conversationQueueUserInputSchema.parse(withoutLegacyAutoSelection(parsed))
}
const pumpingConversationQueues = new Set<string>()
@@ -2166,7 +2187,7 @@ export function registerIpcHandlers(
projectName: string
rootPath: string
conversationId: string
runtimeSelection: AgentRuntimeSelection
runtimeSelection?: AgentRuntimeSelection
followConfiguredAgentRuntime?: boolean
runtime?: AgentRuntime
taskId: string
@@ -2958,9 +2979,10 @@ export function registerIpcHandlers(
}
}
const channelLabel = projectChannelLabels[channel]
const runtimeSelection = project.runtimeSelection ?? {
provider: 'auto' as const
}
// Without a selected-runtime manager the configured runtime is used directly.
const runtimeSelection = selectedRuntimes
? await resolveStoredRuntimeSelection({ project })
: undefined
const identitySuffix = message.senderId.slice(-4)
const senderDisplay = `发送者 ****${identitySuffix}`
const contextIds: string[] = []
@@ -3016,8 +3038,7 @@ export function registerIpcHandlers(
externalConversationId: message.conversationId,
conversationType: message.conversationType,
title: `${channelLabel} · ****${identitySuffix}`,
accountDisplay: senderDisplay,
runtimeSelection
accountDisplay: senderDisplay
})
const incomingMessageId = assistantDatabase.appendRemoteConversationMessage({
conversationId: remoteConversation.id,
@@ -3329,15 +3350,12 @@ export function registerIpcHandlers(
registerHandler(ipcChannels.agentStatus, async (event, input: unknown) => {
assertTrustedSender(event, window)
const selection = agentRuntimeSelectionSchema.optional().parse(input)
const selection = optionalAgentRuntimeSelectionSchema.parse(withoutLegacyAutoSelection(input))
if (!selectedRuntimes) {
return runtime.getStatus()
}
const effectiveSelection =
selection ??
getDefaultRuntimeSelection(
await settingsStore.getResolvedSettings()
)
selection ?? (await resolveStoredRuntimeSelection({}))
return selectedRuntimes.getStatus(effectiveSelection)
})
@@ -3629,7 +3647,7 @@ export function registerIpcHandlers(
if (executionPaused || shuttingDown) {
throw new Error('本地数据维护期间暂不接受新任务')
}
const parsedInput = agentRequestSchema.parse(input)
const parsedInput = agentRequestSchema.parse(withoutLegacyAutoSelection(input))
if (
activeRequests.has(parsedInput.requestId) ||
preparingRequestConversations.has(parsedInput.requestId)
@@ -3727,7 +3745,8 @@ export function registerIpcHandlers(
assistantDatabase.saveLocalConversations([{
header: {
id: conversation.id, projectId: conversation.projectId, title: conversation.title,
updatedAt: now, workMode: normalizedWorkMode, runtimeSelection: parsedRequest.runtimeSelection,
// The renderer owns the conversation layer; never pin the resolved selection here.
updatedAt: now, workMode: normalizedWorkMode, runtimeSelection: conversation.runtimeSelection,
knowledgeLibraryIds: conversation.knowledgeLibraryIds,
knowledgeRetrievalMode: conversation.knowledgeRetrievalMode,
contextMetrics: conversation.contextMetrics, contextCompressionState: conversation.contextCompressionState,
@@ -3798,6 +3817,15 @@ export function registerIpcHandlers(
const configExecutionSpace = configProject
? spaceResolver.resolveProject(configProject)
: undefined
// Remote events key their context metrics by the selection actually used.
const remoteEventSelection =
enrichedRequest.runtimeSelection ??
(configExecutionSpace?.kind === 'ssh'
? await resolveStoredRuntimeSelection({
projectId: configProject?.id,
conversationLayer: assistantDatabase.getConversation(enrichedRequest.conversationId).runtimeSelection
})
: undefined)
const configWorkspacePath =
configAccess === 'none'
? undefined
@@ -4062,7 +4090,7 @@ export function registerIpcHandlers(
? assistantDatabase.appendRemoteConversationTaskEventOnce({
taskId: request.requestId,
conversationId: request.conversationId,
runtimeSelection: request.runtimeSelection ?? configProject?.runtimeSelection,
runtimeSelection: remoteEventSelection,
assistantMessageId:
remoteConversationRecovery.currentAssistantMessageId,
bindingId: provenance.bindingId,
@@ -4090,7 +4118,7 @@ export function registerIpcHandlers(
? assistantDatabase.appendRemoteConversationTaskEventOnce({
taskId: request.requestId,
conversationId: request.conversationId,
runtimeSelection: request.runtimeSelection ?? configProject?.runtimeSelection,
runtimeSelection: remoteEventSelection,
assistantMessageId:
remoteConversationRecovery.currentAssistantMessageId,
bindingId: provenance.bindingId,
@@ -4405,7 +4433,7 @@ export function registerIpcHandlers(
const contextMetricsEvent = runtimeUsageContextMetrics(
usageEvent as RuntimeModelUsageEvent,
runtimeSettings,
request.runtimeSelection
request.runtimeSelection ?? remoteEventSelection
)!
if (provenance === undefined) {
eventBuffer.push(contextMetricsEvent)
@@ -4756,12 +4784,10 @@ export function registerIpcHandlers(
const project = conversation.projectId
? assistantDatabase.getProject(conversation.projectId)
: undefined
const savedRuntimeSelection =
conversation.runtimeSelection ?? project?.runtimeSelection
const persistedRuntimeSelection =
savedRuntimeSelection && savedRuntimeSelection.provider !== 'auto'
? savedRuntimeSelection
: getDefaultRuntimeSelection(settings)
const persistedRuntimeSelection = await resolveStoredRuntimeSelection({
projectId: conversation.projectId,
conversationLayer: conversation.runtimeSelection
})
if (
conversation.projectId !== request.projectId ||
agentRuntimeSelectionKey(persistedRuntimeSelection) !==
@@ -5924,7 +5950,7 @@ export function registerIpcHandlers(
const attachmentParsing = new Map<string, AbortController>()
registerHandler(ipcChannels.contextImageCapability, async (event, input: unknown) => {
assertTrustedSender(event, window)
const { conversationId, runtimeSelection } = z.object({ conversationId: z.string().uuid(), runtimeSelection: agentRuntimeSelectionSchema.optional() }).strict().parse(input)
const { conversationId, runtimeSelection } = z.object({ conversationId: z.string().uuid(), runtimeSelection: optionalAgentRuntimeSelectionSchema }).strict().parse(withoutLegacyAutoSelection(input))
try {
const conversation = assistantDatabase.getConversation(conversationId)
await assertImageInputSupport({ requestId: randomUUID(), conversationId, projectId: conversation.projectId, runtimeSelection, prompt: '', workMode: 'ask' })
@@ -6502,7 +6528,7 @@ export function registerIpcHandlers(
if (executionPaused || shuttingDown) {
throw new Error('本地数据维护期间暂不接受新消息')
}
const parsed = conversationQueueUserInputSchema.parse(input)
const parsed = conversationQueueUserInputSchema.parse(withoutLegacyAutoSelection(input))
if (contextManager.assets) {
parsed.attachments = parsed.attachments.map((attachment) => contextManager.assets!.has(attachment.id) ? contextManager.assets!.get(attachment.id) : attachment)
}
@@ -7247,7 +7273,7 @@ export function registerIpcHandlers(
const queueInput = conversationQueueUserInputSchema.parse({
conversationId: conversation.id,
projectId: request.projectId ?? conversation.projectId ?? undefined,
runtimeSelection: request.runtimeSelection ?? conversation.runtimeSelection,
runtimeSelection: request.runtimeSelection,
workMode: conversation.workMode ?? 'ask',
includeMemoryContext: true,
prompt: request.prompt,
@@ -1,7 +1,10 @@
import { join } from 'node:path'
import { SqliteRuntimeSessionBindingStore } from '../agent/runtime-session-binding-store'
import type { ResolvedModelProfile } from '../runtime-settings-store'
import type { AgentRuntimeSelection } from '../../shared/runtime-selection-contracts'
import type {
AgentRuntimeSelection,
RuntimeSelectionLayer
} from '../../shared/runtime-selection-contracts'
import type { SshHostStore } from '../ssh/ssh-host-store'
import {
resolveBundledRemoteRuntimeResourcePaths,
@@ -44,7 +47,7 @@ export type ManagedRemoteExecutionServicesOptions = {
selection: AgentRuntimeSelection
): Promise<ResolvedModelProfile | undefined>
resolveRuntimeSelection(
selection: AgentRuntimeSelection
layer: RuntimeSelectionLayer | undefined
): Promise<AgentRuntimeSelection>
}
@@ -1,7 +1,10 @@
import { EventEmitter } from 'node:events'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { AssistantProject } from '../../shared/assistant-contracts'
import type { AgentRuntimeSelection } from '../../shared/runtime-selection-contracts'
import type {
AgentRuntimeSelection,
RuntimeSelectionLayer
} from '../../shared/runtime-selection-contracts'
import type { SshConnectionTarget } from '../ssh/ssh-host-store'
import { verifyAgentInstallationId } from '../ssh/ssh-agent-command'
import type { AgentInstallationIdentity } from './agent-installation-manager'
@@ -110,7 +113,7 @@ type HarnessOptions = {
create?: () => AssistantProject
update?: () => AssistantProject
resolveRuntimeSelection?: (
selection: AgentRuntimeSelection
layer: RuntimeSelectionLayer | undefined
) => Promise<AgentRuntimeSelection>
}
@@ -262,7 +265,7 @@ function harness(options: HarnessOptions = {}) {
},
resolveRuntimeSelection:
options.resolveRuntimeSelection ??
(async (selection) => selection),
(async (layer) => ({ provider: layer?.provider === 'continue' ? 'continue' : 'opencode' }) as AgentRuntimeSelection),
runtimeValidator: {
validate: vi.fn(async () => {
calls.push('runtime')
@@ -11,7 +11,10 @@ import {
remoteWorkspaceValidateResultSchema,
type RemoteWorkspaceHandle
} from '../../shared/remote-agent-contracts'
import type { AgentRuntimeSelection } from '../../shared/runtime-selection-contracts'
import type {
AgentRuntimeSelection,
RuntimeSelectionLayer
} from '../../shared/runtime-selection-contracts'
import type {
AssistantDatabase,
SshProjectWrite
@@ -99,11 +102,11 @@ type ActiveSave = {
type RemoteProjectPreparationDraft = Readonly<{
hostId: string
remoteRootPath: string
runtimeSelection: AgentRuntimeSelection
runtimeSelection?: RuntimeSelectionLayer
}>
type PreparedRemoteProject = Readonly<{
runtimeSelection: AgentRuntimeSelection
runtimeSelection?: RuntimeSelectionLayer
target: SshConnectionTarget
installation: AgentInstallationIdentity
connection: RemoteAgentConnection
@@ -125,7 +128,7 @@ export type RemoteProjectSaveServiceOptions = {
installationManager: Pick<AgentInstallationManager, 'activateInstalled'>
connectionManager: Pick<RemoteAgentConnectionManager, 'acquire'>
resolveRuntimeSelection(
selection: AgentRuntimeSelection
layer: RuntimeSelectionLayer | undefined
): Promise<AgentRuntimeSelection>
runtimeValidator?: RemoteProjectRuntimeValidator
notify?: (
+9 -1
View File
@@ -430,6 +430,8 @@ export type ResolvedRuntimeSettings = {
opencodeModelProfile?: ResolvedModelProfile
continueModelProfile?: ResolvedModelProfile
deepseekHarnessModelProfile?: ResolvedModelProfile
/** What DeepSeek Harness uses when its own configuration is selected. */
deepseekHarnessPlatformModelProfile?: ResolvedModelProfile
opencodeBaseUrl: string
opencodeEmbedded: boolean
opencodeBinaryPath: string
@@ -1989,6 +1991,11 @@ export class RuntimeSettingsStore {
: undefined
const deepseekHarnessModelProfile =
this.resolveDeepSeekHarnessModelProfile(settings, modelProfiles)
const deepseekHarnessPlatformModelProfile =
this.resolveDeepSeekHarnessModelProfile(
{ ...settings, deepseekHarnessModelSource: { kind: 'platform' } },
modelProfiles
)
const embeddingConnections =
this.resolveEmbeddingConnections(settings)
const activeEmbeddingConnection =
@@ -2014,6 +2021,7 @@ export class RuntimeSettingsStore {
defaultImageModelProfileId: settings.defaultImageModelProfileId,
continueModelProfile,
deepseekHarnessModelProfile,
deepseekHarnessPlatformModelProfile,
...agent,
subagentSmartRoutingEnabled:
settings.subagentSmartRoutingEnabled,
@@ -2530,7 +2538,7 @@ export class RuntimeSettingsStore {
const next: StoredSettings = {
...current,
version: CURRENT_SETTINGS_VERSION,
provider: input.provider,
provider: input.provider === 'auto' ? 'model' : input.provider,
modelProfiles,
defaultModelProfileId,
opencodeModelSource,