feat(ssh): agent-status over SSH (#1702)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-05-13 22:09:32 -07:00
committed by GitHub
co-authored by Orca
parent e7bdd18e2f
commit ba0a5b47ed
19 changed files with 1925 additions and 80 deletions
+41 -26
View File
@@ -28,7 +28,7 @@ import { join } from 'path'
import { parseAgentStatusPayload, type ParsedAgentStatusPayload } from './agent-status-types'
import { ORCA_HOOK_PROTOCOL_VERSION } from './agent-hook-types'
import type { AgentHookSource } from './agent-hook-relay'
import { REMOTE_AGENT_HOOK_ENV, type AgentHookSource } from './agent-hook-relay'
/** Maximum request body size accepted by the listener (1 MB). */
export const HOOK_REQUEST_MAX_BYTES = 1_000_000
@@ -85,6 +85,41 @@ export function clearAllListenerCaches(state: HookListenerState): void {
state.warnedEnvs.clear()
}
/** Emit warn-once diagnostics for cross-build (`version`) and dev-vs-prod
* (`env`) mismatches. Shared between the local HTTP path
* (`normalizeHookPayload`) and the relay-forwarded path
* (`AgentHookServer.ingestRemote`) so a remote-sourced event triggers the
* same diagnostic noise as a local one. The relay's "remote" marker is a
* location tag, not a build env, so it must not look like stale local hooks. */
export function warnOnHookEnvOrVersionMismatch(
state: HookListenerState,
fields: { version?: string; env?: string; expectedEnv: string }
): void {
const { version, env, expectedEnv } = fields
if (
version &&
version !== ORCA_HOOK_PROTOCOL_VERSION &&
!state.warnedVersions.has(version) &&
state.warnedVersions.size < MAX_WARNED_KEYS
) {
state.warnedVersions.add(version)
console.warn(
`[agent-hooks] received hook v${version}; server expects v${ORCA_HOOK_PROTOCOL_VERSION}. ` +
'Reinstall agent hooks from Settings to upgrade the managed script.'
)
}
if (env && env !== REMOTE_AGENT_HOOK_ENV && env !== expectedEnv) {
const key = `${env}->${expectedEnv}`
if (!state.warnedEnvs.has(key) && state.warnedEnvs.size < MAX_WARNED_KEYS) {
state.warnedEnvs.add(key)
console.warn(
`[agent-hooks] received ${env} hook on ${expectedEnv} server. ` +
'Likely a stale terminal from another Orca install.'
)
}
}
}
export type AgentHookEventPayload = {
paneKey: string
tabId?: string
@@ -1144,31 +1179,11 @@ export function normalizeHookPayload(
return null
}
const version = readStringField(record, 'version')
if (
version &&
version !== ORCA_HOOK_PROTOCOL_VERSION &&
!state.warnedVersions.has(version) &&
state.warnedVersions.size < MAX_WARNED_KEYS
) {
state.warnedVersions.add(version)
console.warn(
`[agent-hooks] received hook v${version}; server expects v${ORCA_HOOK_PROTOCOL_VERSION}. ` +
'Reinstall agent hooks from Settings to upgrade the managed script.'
)
}
const clientEnv = readStringField(record, 'env')
if (clientEnv && clientEnv !== expectedEnv) {
const key = `${clientEnv}->${expectedEnv}`
if (!state.warnedEnvs.has(key) && state.warnedEnvs.size < MAX_WARNED_KEYS) {
state.warnedEnvs.add(key)
console.warn(
`[agent-hooks] received ${clientEnv} hook on ${expectedEnv} server. ` +
'Likely a stale terminal from another Orca install.'
)
}
}
warnOnHookEnvOrVersionMismatch(state, {
version: readStringField(record, 'version'),
env: readStringField(record, 'env'),
expectedEnv
})
const tabId = readStringField(record, 'tabId')
const worktreeId = readStringField(record, 'worktreeId')
+15 -12
View File
@@ -10,16 +10,17 @@
//
// Per the design doc:
// - The relay normalizes; Orca routes. The envelope's `payload` field has
// already been through `normalizeHookPayload` on the relay side; Orca's
// ingestRemote re-runs the canonical normalizer at the trust boundary
// (defense-in-depth) before feeding the event into the same `onAgentStatus`
// fanout the local HTTP path uses.
// already been through `normalizeHookPayload` (which calls
// `parseAgentStatusPayload` → `normalizeAgentStatusObject`) on the relay
// side. Orca's `ingestRemote` re-runs the canonical payload normalizer at
// the SSH trust boundary before caching or persisting, so relay skew or a
// buggy remote process cannot poison main-process state.
// - The wire `connectionId` is **always `null`**: a `connectionId` is Orca's
// local handle on an `ssh2` connection, not a wire identity. Orca stamps the
// real value on receive from `mux` identity inside `ingestRemote`.
// - The wire `version` and `env` fields are forwarded verbatim from the agent
// CLI's POST body so Orca's existing warn-once cross-build / dev-vs-prod
// diagnostics still fire on remote-sourced events.
// - The wire `version` and `env` fields are forwarded from the agent CLI's
// POST body so Orca's warn-once protocol diagnostics still fire. The relay
// default env is `remote`, a location marker ignored by dev-vs-prod checks.
import type { ParsedAgentStatusPayload } from './agent-status-types'
@@ -31,6 +32,10 @@ import type { ParsedAgentStatusPayload } from './agent-status-types'
// that consumes it from the relay side).
export type AgentHookSource = 'claude' | 'codex' | 'gemini' | 'opencode' | 'cursor' | 'pi' | 'droid'
/** Env marker used by the remote relay. It is a transport/location marker, not
* a dev-vs-prod build tag, so main-process env mismatch diagnostics ignore it. */
export const REMOTE_AGENT_HOOK_ENV = 'remote' as const
/** Wire envelope for a single hook event flowing relay → Orca. */
export type AgentHookRelayEnvelope = {
source: AgentHookSource
@@ -39,16 +44,14 @@ export type AgentHookRelayEnvelope = {
worktreeId?: string
/** Always `null` on the wire — relay does not know Orca's local connectionId. */
connectionId: null
/** Forwarded verbatim from the agent CLI POST body (e.g. 'production',
* 'development'). Lets Orca's warn-once env-mismatch diagnostic fire on
* remote events the same as on local. */
/** Forwarded from the agent CLI POST body. The relay default is `remote`,
* which marks transport location rather than dev/prod build env. */
env?: string
/** Forwarded verbatim from the agent CLI POST body. Lets Orca's warn-once
* protocol-version diagnostic fire on remote events the same as on local. */
version?: string
/** Pre-normalized status payload from the relay's `normalizeHookPayload`.
* Orca's `ingestRemote` re-validates via `normalizeAgentStatusPayload` at
* the trust boundary as defense-in-depth. */
* Orca's `ingestRemote` validates it again at the SSH trust boundary. */
payload: ParsedAgentStatusPayload
}