feat: keep managed SSH prompts running on Hosts

Managed SSH prompts previously depended on Desktop-owned provider and transcript state, so accepted work could stop or become uncertain when the Desktop, network, or power disappeared. The Agent now owns accepted OpenCode prompt execution, model and tool rounds, cancellation, and the durable semantic transcript, while Desktop remains authoritative for permanent projects, conversations, tasks, and messages.

Recovered work attaches to the original conversation without replay. The project switcher also groups managed SSH projects by saved Host and shows live Agent connectivity beside each group without opening a connection merely to read status. Remote project subtitles now show only the path, and the Agent lock advances to 0.11.14 with Runtime ACP v4.

Release note: 托管 SSH 任务在桌面端、网络或电源中断后可继续在主机上运行,并能回到原会话精确恢复;项目菜单现按主机分组并显示实时连接状态。
This commit is contained in:
mesalogo
2026-08-31 11:04:41 +08:00
parent f6f8d5e053
commit 561de29f00
75 changed files with 13439 additions and 1327 deletions
@@ -1,21 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import {
runtimeSessionBindingSchema,
type RuntimeSessionBinding
} from '../../shared/remote-agent-contracts'
import type { RemoteModelGateway } from '../agent/remote-model-gateway'
import { RemoteModelGatewayError } from '../agent/remote-model-gateway'
import { ModelCallCapacityError } from '../agent/model-call-operation-store'
import { MemoryRuntimeSessionBindingStore } from '../agent/runtime-session-binding-store'
import { describe, expect, it } from 'vitest'
import type { ResolvedModelProfile } from '../runtime-settings-store'
import {
createManagedModelBridge,
reconcileStartupModelCalls
} from './managed-model-bridge'
import { MainModelBridgeDispatchError } from './main-model-bridge-dispatcher'
const digest = (character: string): string =>
`sha256:${character.repeat(64)}`
import { createManagedModelBridge } from './managed-model-bridge'
const profile: ResolvedModelProfile = {
id: '00000000-0000-4000-8000-000000000001',
@@ -24,307 +9,65 @@ const profile: ResolvedModelProfile = {
modelName: 'private-model',
protocol: 'openai-responses',
authentication: 'api-key',
supportsImageInput: false,
apiKey: 'main-only-secret'
supportsImageInput: true,
maximumOutputTokens: 8_192,
apiKey: 'prompt-scoped-secret'
}
const response = {
status: 200,
headers: { 'content-type': 'application/json' },
bodyBase64: Buffer.from('{}').toString('base64')
}
describe('managed Agent prompt model setup', () => {
it('validates and clones a bounded Agent prompt profile', () => {
const setup = createManagedModelBridge({ profile })
function bridgeFixture(options?: {
bindingStore?: MemoryRuntimeSessionBindingStore
}) {
const bindingStore =
options?.bindingStore ??
new MemoryRuntimeSessionBindingStore()
const dispatch = vi.fn(async () => response)
const markResponseDelivered = vi.fn()
const finalizePrompt = vi.fn()
const gateway = {
dispatch,
markResponseDelivered,
finalizePrompt
} as unknown as RemoteModelGateway
const bridge = createManagedModelBridge({
profile,
gateway,
bindingStore
})
return {
bindingStore,
dispatch,
markResponseDelivered,
finalizePrompt,
bridge
}
}
function binding(
policy: ReturnType<typeof bridgeFixture>['bridge']['policy'],
state: RuntimeSessionBinding['state'] = 'prompt-running'
): RuntimeSessionBinding {
return runtimeSessionBindingSchema.parse({
bindingId: 'binding-1',
controllerId: 'controller-1',
controllerGeneration: 2,
conversationId: 'conversation-1',
hostId: 'host-1',
hostRevision: 1,
hostKeyGeneration: 1,
workspaceIdentity: 'workspace-1',
agentInstallationId: 'agent-1',
daemonBootIdAtOpen: 'boot-1',
runtimeId: 'opencode',
runtimeBundleDigest: digest('a'),
runtimeAdapterDigest: digest('b'),
modelBridgeVersion: 'goodbuddy-model-bridge-v1',
modelBridgePolicy: policy,
acpSessionId: 'session-1',
acpCapabilitiesDigest: digest('c'),
state,
activePromptOperationId:
state === 'prompt-running' || state === 'outcome-unknown'
? 'prompt-1'
: undefined,
channelEpoch: '1',
lastOutboundJournaledSequence: '0',
lastOutboundDeliveredSequence: '0',
lastInboundJournaledSequence: '0',
lastMainAckSequence: '0'
})
}
function dispatchInput(
policy: ReturnType<typeof bridgeFixture>['bridge']['policy']
) {
return {
identity: {
bindingId: 'binding-1',
promptOperationId: 'prompt-1',
requestId: 'local-model-request-1',
roundIndex: 0,
modelProfileDigest: policy.modelProfileDigest,
messageId: 'message-1'
},
policy,
request: {
method: 'POST' as const,
path: '/v1/responses' as const,
headers: { 'content-type': 'application/json' },
bodyBase64: Buffer.from(
JSON.stringify({ model: 'private-model' })
).toString('base64')
},
requestDigest: digest('e')
}
}
describe('managed model bridge composition', () => {
it('does not impose call-count or token quotas', () => {
const fixture = bridgeFixture()
expect(fixture.bridge.policy).toEqual({
protocol: 'openai-responses',
model: 'private-model',
expect(setup.profile).toEqual({
profileId: profile.id,
modelProfileDigest: expect.stringMatching(
/^sha256:[a-f0-9]{64}$/u
),
supportsImageInput: false
provider: 'openai',
baseUrl: 'https://provider.example/v1',
model: 'private-model',
protocol: 'openai-responses',
authentication: 'api-key',
apiKey: 'prompt-scoped-secret',
capabilities: { imageInput: true },
limits: {
maximumOutputTokens: 8_192,
maximumModelCalls: 100,
maximumTotalOutputTokens: 819_200,
requestTimeoutMilliseconds: 60_000
}
})
expect(setup.policy).toEqual({
protocol: 'openai-responses',
model: 'private-model',
modelProfileDigest: setup.profile.modelProfileDigest,
supportsImageInput: true
})
expect(JSON.stringify(setup.policy)).not.toContain(
'prompt-scoped-secret'
)
profile.apiKey = 'changed-after-creation'
expect(setup.profile.apiKey).toBe('prompt-scoped-secret')
})
it('keeps credentials in Main and maps exact dispatch plus delivery identity', async () => {
const fixture = bridgeFixture()
await fixture.bindingStore.put(binding(fixture.bridge.policy))
const input = dispatchInput(fixture.bridge.policy)
await expect(
fixture.bridge.channel.dispatch(
input,
new AbortController().signal
)
).resolves.toEqual(response)
expect(fixture.dispatch).toHaveBeenCalledWith(
expect.objectContaining({
requestId: 'local-model-request-1',
bindingId: 'binding-1',
promptOperationId: 'prompt-1',
promptSequence: 0,
roundIndex: 0,
modelProfile: expect.objectContaining({
apiKey: 'main-only-secret'
})
}),
input.request,
expect.any(AbortSignal)
)
expect(JSON.stringify(fixture.bridge.policy)).not.toContain(
'main-only-secret'
)
expect(JSON.stringify(fixture.bridge.policy)).not.toContain(
'provider.example'
)
await fixture.bridge.channel.onDelivered(
input.identity,
input.requestDigest
)
expect(fixture.markResponseDelivered).toHaveBeenCalledWith({
bindingId: 'binding-1',
promptOperationId: 'prompt-1',
roundIndex: 0
})
await fixture.bridge.channel.finalizePrompt(input.identity)
expect(fixture.finalizePrompt).toHaveBeenCalledWith({
bindingId: 'binding-1',
promptOperationId: 'prompt-1',
promptSequence: 0
})
})
it('classifies only known pre-dispatch gateway failures as definitive', async () => {
const fixture = bridgeFixture()
await fixture.bindingStore.put(binding(fixture.bridge.policy))
vi.mocked(fixture.dispatch).mockRejectedValueOnce(
new RemoteModelGatewayError(
'request-policy-mismatch',
'safe failure'
)
)
await expect(
fixture.bridge.channel.dispatch(
dispatchInput(fixture.bridge.policy),
new AbortController().signal
)
).rejects.toMatchObject({
code: 'request-policy-mismatch',
outcomeUnknown: false,
postDispatch: false
})
vi.mocked(fixture.dispatch).mockRejectedValueOnce(
new ModelCallCapacityError()
)
await expect(
fixture.bridge.channel.dispatch(
dispatchInput(fixture.bridge.policy),
new AbortController().signal
)
).rejects.toMatchObject({
code: 'model-ledger-capacity',
outcomeUnknown: false,
postDispatch: false
})
vi.mocked(fixture.dispatch).mockRejectedValueOnce(
new RemoteModelGatewayError(
'network-error',
'must not escape'
)
)
await expect(
fixture.bridge.channel.dispatch(
dispatchInput(fixture.bridge.policy),
new AbortController().signal
)
).rejects.toBeInstanceOf(MainModelBridgeDispatchError)
await expect(
fixture.bridge.channel.dispatch(
{
...dispatchInput(fixture.bridge.policy),
policy: {
...fixture.bridge.policy,
model: 'different-model'
}
},
new AbortController().signal
)
).rejects.toMatchObject({
code: 'model-policy-mismatch',
outcomeUnknown: false
})
})
it('durably poisons the exact active binding', async () => {
const fixture = bridgeFixture()
await fixture.bindingStore.put(binding(fixture.bridge.policy))
await fixture.bridge.channel.poison(
'binding-1',
'prompt-1',
'delivery-unknown'
)
await expect(
fixture.bindingStore.getById('binding-1')
).resolves.toMatchObject({ state: 'outcome-unknown' })
})
it('paginates startup uncertainty and poisons only matching live prompts', async () => {
const fixture = bridgeFixture()
await fixture.bindingStore.put(binding(fixture.bridge.policy))
const firstCursor = {
updatedAt: 10,
callOperationId: 'call-1'
}
const listStartupRecords = vi
.fn()
.mockReturnValueOnce({
records: [
{
status: 'completed',
identity: {
bindingId: 'binding-1',
promptOperationId: 'prompt-1'
}
}
],
nextCursor: firstCursor
it('rejects unusable profiles before opening a remote binding', () => {
expect(() =>
createManagedModelBridge({
profile: {
...profile,
authentication: 'api-key',
apiKey: undefined
}
})
.mockReturnValueOnce({ records: [] })
await reconcileStartupModelCalls({
gatewayStore: { listStartupRecords },
bindingStore: fixture.bindingStore
})
expect(listStartupRecords).toHaveBeenNthCalledWith(2, {
cursor: firstCursor,
limit: 100
})
await expect(
fixture.bindingStore.getById('binding-1')
).resolves.toMatchObject({ state: 'outcome-unknown' })
})
it('marks a prepared but undispatched startup call as interrupted', async () => {
const fixture = bridgeFixture()
await fixture.bindingStore.put(binding(fixture.bridge.policy))
await reconcileStartupModelCalls({
gatewayStore: {
listStartupRecords: () => ({
records: [
{
status: 'prepared',
identity: {
bindingId: 'binding-1',
promptOperationId: 'prompt-1'
}
}
]
})
},
bindingStore: fixture.bindingStore
})
await expect(
fixture.bindingStore.getById('binding-1')
).resolves.toMatchObject({
state: 'interrupted',
activePromptOperationId: undefined
})
).toThrow(/usable text model profile/iu)
expect(() =>
createManagedModelBridge({
profile: {
...profile,
protocol: 'openai-images-generations'
}
})
).toThrow(/usable text model profile/iu)
})
})