feat: add managed SSH execution spaces

Add managed SSH Hosts and projects with signed side-by-side GoodBuddy Agent and OpenCode Runtime installation, Ask and Execute workspace access, a Main-owned model bridge, detached Agent lifecycle, durable ACP output recovery, and project activation UI.

Use protocol v2 with bounded FIFO frames over SSH instead of custom credit, priority, batching, or model-message chunking. Mismatched Host Agents now upgrade automatically, an exactly verified Node runtime is reused on-host, and local Portable builds can embed strictly verified Agent artifacts.

Release note: 新增托管 SSH 远程项目,可自动安装或升级签名 Agent/OpenCode Runtime,并在 SSH 断线后继续 Agent 工作和同步未确认输出。
This commit is contained in:
mesalogo
2026-08-24 23:02:18 +08:00
parent 3829a871f5
commit 607bb33249
258 changed files with 99449 additions and 2427 deletions
@@ -0,0 +1,232 @@
import { isDeepStrictEqual } from 'node:util'
import { projectRuntimeValidationSchema } from '../../shared/assistant-contracts'
import { agentRuntimeSelectionKey } from '../../shared/runtime-selection-contracts'
import type { ResolvedModelProfile } from '../runtime-settings-store'
import type { RemoteAgentConnection } from './remote-agent-connection-manager'
import type {
RemoteProjectRuntimeValidationEvidence,
RemoteProjectRuntimeValidationInput,
RemoteProjectRuntimeValidationLease,
RemoteProjectRuntimeValidator
} from './remote-project-save-service'
import type {
RemoteRuntimeInstallationIdentity,
RemoteRuntimeInstallationManager
} from './remote-runtime-installation-manager'
const RUNTIME_ACP_CAPABILITY = 'runtime/acp'
const RUNTIME_ACP_CAPABILITY_VERSION = 3
const RUNTIME_MODEL_BRIDGE_CAPABILITY = 'runtime/model-bridge'
const RUNTIME_MODEL_BRIDGE_CAPABILITY_VERSION = 1
export class ManagedRemoteProjectRuntimeValidator
implements RemoteProjectRuntimeValidator
{
readonly #installationManager: Pick<
RemoteRuntimeInstallationManager,
'ensureInstalled'
>
readonly #now: () => Date
readonly #resolveModelProfile: (
selection: RemoteProjectRuntimeValidationInput['selection']
) => Promise<ResolvedModelProfile | undefined>
constructor(options: {
installationManager: Pick<
RemoteRuntimeInstallationManager,
'ensureInstalled'
>
resolveModelProfile(
selection: RemoteProjectRuntimeValidationInput['selection']
): Promise<ResolvedModelProfile | undefined>
now?: () => Date
}) {
this.#installationManager = options.installationManager
this.#resolveModelProfile = options.resolveModelProfile
this.#now = options.now ?? (() => new Date())
}
async validate(
input: RemoteProjectRuntimeValidationInput
): Promise<RemoteProjectRuntimeValidationLease> {
input.signal.throwIfAborted()
if (
input.selection.provider !== 'opencode' ||
input.runtimeSelectionKey !==
agentRuntimeSelectionKey(input.selection)
) {
throw new Error(
'Managed remote projects require the OpenCode Runtime'
)
}
assertInputIdentity(input)
const modelProfile = await this.#resolveModelProfile(
input.selection
)
input.signal.throwIfAborted()
if (
modelProfile === undefined ||
modelProfile.protocol === 'openai-images-generations' ||
(modelProfile.authentication === 'api-key' &&
!modelProfile.apiKey)
) {
throw new Error(
'Managed remote OpenCode requires a usable text model profile'
)
}
const installation =
await this.#installationManager.ensureInstalled(
input.host.hostId,
{ signal: input.signal }
)
input.signal.throwIfAborted()
assertInstallationIdentity(input, installation)
const refreshed = await input.connection.refreshCapabilities(
input.signal
)
input.signal.throwIfAborted()
if (
input.connection.state !== 'ready' ||
!isDeepStrictEqual(input.connection.capabilities, refreshed)
) {
throw new Error(
'Remote Agent capabilities changed during Runtime validation'
)
}
assertRuntimeCapability(refreshed, installation)
const validated = projectRuntimeValidationSchema.parse({
runtimeSelectionKey: input.runtimeSelectionKey,
runtimeBundleDigest: installation.bundleDigest,
runtimeAdapterDigest: installation.runtimeAdapterDigest,
agentInstallationIdAtValidation: input.agent.installationId,
validatedAt: this.#now().toISOString(),
workMode: input.workMode
})
const evidence: RemoteProjectRuntimeValidationEvidence = validated
const capabilitySnapshot = structuredClone(refreshed)
let released = false
return {
evidence,
assertCurrent: (): void => {
if (released) {
throw new Error(
'Remote Runtime validation lease is released'
)
}
if (input.connection.state !== 'ready') {
throw new Error(
'Remote Runtime validation connection is not ready'
)
}
let current: RemoteAgentConnection['capabilities']
try {
current = input.connection.capabilities
} catch (error) {
throw new Error(
'Remote Runtime validation connection is not ready',
{ cause: error }
)
}
if (!isDeepStrictEqual(current, capabilitySnapshot)) {
throw new Error(
'Remote Runtime capability validation is stale'
)
}
},
release: (): void => {
released = true
}
}
}
}
function assertInputIdentity(
input: RemoteProjectRuntimeValidationInput
): void {
const connection = input.connection
if (
connection.state !== 'ready' ||
connection.identity.hostId !== input.host.hostId ||
connection.identity.hostRevision !== input.host.hostRevision ||
connection.identity.hostKeyGeneration !==
input.host.hostKeyGeneration ||
connection.identity.remoteUsername !==
input.host.remoteUsername ||
connection.identity.installationId !==
input.agent.installationId ||
connection.identity.binaryDigest !== input.agent.binaryDigest ||
connection.identity.protocolMajor !== input.agent.protocolMajor ||
connection.status.state !== 'ready' ||
connection.status.draining ||
connection.status.installationId !==
input.agent.installationId ||
connection.status.binaryDigest !== input.agent.binaryDigest ||
connection.status.agentVersion !== input.agent.version ||
connection.status.architecture !== input.agent.architecture ||
connection.status.protocol.major !== input.agent.protocolMajor
) {
throw new Error(
'Remote Agent identity does not match Runtime validation'
)
}
}
function assertInstallationIdentity(
input: RemoteProjectRuntimeValidationInput,
installation: RemoteRuntimeInstallationIdentity
): void {
if (
installation.runtimeId !== 'opencode' ||
installation.platform !== 'linux' ||
installation.architecture !== input.agent.architecture
) {
throw new Error(
'Installed OpenCode Runtime architecture is incompatible'
)
}
}
function assertRuntimeCapability(
capabilities: RemoteAgentConnection['capabilities'],
installation: RemoteRuntimeInstallationIdentity
): void {
const acp = capabilities.capabilities.find(
(capability) => capability.name === RUNTIME_ACP_CAPABILITY
)
const modelBridge = capabilities.capabilities.find(
(capability) =>
capability.name === RUNTIME_MODEL_BRIDGE_CAPABILITY
)
const runtimes = capabilities.runtimes.filter(
(runtime) => runtime.runtimeId === 'opencode'
)
if (
acp === undefined ||
acp.version !== RUNTIME_ACP_CAPABILITY_VERSION ||
!acp.critical ||
modelBridge === undefined ||
modelBridge.version !==
RUNTIME_MODEL_BRIDGE_CAPABILITY_VERSION ||
!modelBridge.critical ||
runtimes.length !== 1
) {
throw new Error(
'Remote Agent does not advertise the required Runtime ACP capability'
)
}
const runtime = runtimes[0]!
if (
runtime.version !== installation.runtimeVersion ||
runtime.bundleDigest !== installation.bundleDigest ||
runtime.acpCapabilitiesDigest !==
installation.acpCapabilitiesDigest ||
!runtime.sessionLoad ||
!runtime.sessionResume
) {
throw new Error(
'Advertised OpenCode Runtime does not match the verified installation'
)
}
}