Files
goodbuddy/src/main/agent/remote-runtime-channel.ts
T
mesalogo 561de29f00 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 任务在桌面端、网络或电源中断后可继续在主机上运行,并能回到原会话精确恢复;项目菜单现按主机分组并显示实时连接状态。
2026-08-31 11:04:41 +08:00

193 lines
5.9 KiB
TypeScript

import type {
AcpBindingCursors,
AcpCompletePromptRequest,
AcpCompletePromptResult,
AcpEscalateCancellationRequest,
AcpReconcilePromptRequest,
AcpReconcilePromptResult
} from '../../shared/agent-protocol'
import type {
RemoteOwnedPromptStartRequest,
RemoteOwnedPromptStartResult,
RemotePromptOperationAcceptance,
RemotePromptOperationPreparation,
remoteOwnedPromptAttachRequestSchema,
remoteSemanticTranscriptAckRequestSchema,
remoteSemanticTranscriptAckResultSchema,
remoteSemanticTranscriptPageRequestSchema,
remoteSemanticTranscriptPageResultSchema,
} from '../../shared/remote-agent-contracts'
import type { z } from 'zod'
import type {
ModelBridgePolicy
} from '../../shared/model-bridge-contracts'
export type RemoteRuntimeChannelCapabilities = {
cancellationEscalation: boolean
promptOperationReconciliation: boolean
ownedPrompt?: boolean
modelBridge?: boolean
}
export type RemoteOwnedPromptAttachRequest = z.infer<
typeof remoteOwnedPromptAttachRequestSchema
>
export type RemoteSemanticTranscriptPageRequest = z.infer<
typeof remoteSemanticTranscriptPageRequestSchema
>
export type RemoteSemanticTranscriptPageResult = z.infer<
typeof remoteSemanticTranscriptPageResultSchema
>
export type RemoteSemanticTranscriptAckRequest = z.infer<
typeof remoteSemanticTranscriptAckRequestSchema
>
export type RemoteSemanticTranscriptAckResult = z.infer<
typeof remoteSemanticTranscriptAckResultSchema
>
export type RuntimeSessionBindingCursors = AcpBindingCursors
export type RemoteCancellationEscalation = Omit<
AcpEscalateCancellationRequest,
'reason'
> & {
reason: unknown
}
export type RemotePromptOperationReconciliation =
AcpReconcilePromptRequest
export type RemotePromptOperationReconciliationResult =
AcpReconcilePromptResult
export type RemotePromptOperationCompletion = AcpCompletePromptRequest
export type RemotePromptOperationCompletionResult =
AcpCompletePromptResult
export type RemoteModelBridgeOpenRequest = {
bindingId: string
promptOperationId: string
requestId: string
policy: ModelBridgePolicy
}
export type RemoteModelBridgeCloseResult = {
clean: boolean
poisoned: boolean
}
export interface RemoteModelBridgeSession {
readonly version: 'goodbuddy-model-bridge-v1'
readonly channelId: string
readonly channelEpoch: string
readonly policy: ModelBridgePolicy
readonly closed: Promise<RemoteModelBridgeCloseResult>
close(reason?: string): Promise<RemoteModelBridgeCloseResult>
}
/**
* A process-neutral, already authenticated ACP virtual channel.
*
* Implementations own framing, journaling and transport generation checks.
* The runtime intentionally receives no SSH client, socket or command API.
*/
export interface RemoteRuntimeChannel {
readonly input: ReadableStream<Uint8Array>
readonly output: WritableStream<Uint8Array>
readonly generation: number
readonly channelEpoch: string
/**
* Signed Runtime capability identity advertised by the Agent before the
* untrusted Runtime process starts.
*/
readonly advertisedAcpCapabilitiesDigest: string
readonly capabilities: RemoteRuntimeChannelCapabilities
readonly closed: Promise<void>
isCurrentGeneration(): boolean
getBindingCursors(
bindingId: string
): Promise<RuntimeSessionBindingCursors>
/** Accepts the prompt and starts its Ask or Execute Runtime process. */
preparePrompt(
preparation: RemotePromptOperationPreparation
): Promise<RemotePromptOperationAcceptance>
/** Starts an Agent-owned ACP prompt exactly once for its stable operation. */
startOwnedPrompt?(
request: RemoteOwnedPromptStartRequest
): Promise<RemoteOwnedPromptStartResult>
/** Reattaches to an already accepted Agent-owned prompt without replaying it. */
attachOwnedPrompt?(
request: RemoteOwnedPromptAttachRequest
): Promise<RemoteOwnedPromptStartResult>
pageOwnedPromptTranscript?(
request: RemoteSemanticTranscriptPageRequest
): Promise<RemoteSemanticTranscriptPageResult>
ackOwnedPromptTranscript?(
request: RemoteSemanticTranscriptAckRequest
): Promise<RemoteSemanticTranscriptAckResult>
/**
* Bounds same-daemon stream recovery to the signed prompt lifetime and
* caller cancellation. Implementations that cannot recover may omit it.
*/
setRecoveryBoundary?(
deadlineAt: string,
signal: AbortSignal
): void
clearRecoveryBoundary?(): void
/**
* Allocates and registers a fresh Main-owned model channel for one prompt.
* Legacy channels may omit this when the runtime is configured without a
* model bridge.
*/
openModelBridge?(
request: RemoteModelBridgeOpenRequest
): Promise<RemoteModelBridgeSession>
completePromptOperation(
operation: RemotePromptOperationCompletion
): Promise<RemotePromptOperationCompletionResult>
/**
* Stops or resumes inbound ACP delivery. Pausing must take effect before
* the returned promise resolves and must not advance the Main ACK cursor.
*/
setInboundPaused(paused: boolean): Promise<void>
escalateCancellation(
escalation: RemoteCancellationEscalation
): Promise<void>
reconcilePromptOperation(
operation: RemotePromptOperationReconciliation
): Promise<RemotePromptOperationReconciliationResult>
close(): Promise<void>
}
export class StaleRemoteRuntimeGenerationError extends Error {
constructor() {
super('远端 Runtime 连接代际已失效')
this.name = 'StaleRemoteRuntimeGenerationError'
}
}
export function isDefinitiveRemoteRuntimeRequestRejection(
error: unknown,
method: string
): boolean {
if (
error === null ||
typeof error !== 'object' ||
!('remoteRequestOutcome' in error) ||
error.remoteRequestOutcome !== 'rejected' ||
!('remoteMethod' in error) ||
error.remoteMethod !== method
) {
return false
}
return true
}
export function assertCurrentRemoteRuntimeGeneration(
channel: RemoteRuntimeChannel
): void {
if (!channel.isCurrentGeneration()) {
throw new StaleRemoteRuntimeGenerationError()
}
}