Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import http from 'node:http';
import { test } from 'vitest';
import { DAEMON_RPC_PROTOCOL_VERSION } from '@agent-device/contracts/daemon-http';
import { readVersion } from '@agent-device/host-kit/version';
import { readRemoteDaemonHealth } from '../daemon-client-transport.ts';
import {
closeLoopbackServer,
Expand Down Expand Up @@ -87,6 +88,38 @@ test('a proxy whose daemon speaks another protocol is refused, naming the daemon
});
});

test.each([
[
'an older daemon',
{ ...DAEMON_LINK, rpcProtocolVersion: DAEMON_RPC_PROTOCOL_VERSION - 1 },
'remote',
],
[
'a newer daemon',
{ ...DAEMON_LINK, rpcProtocolVersion: DAEMON_RPC_PROTOCOL_VERSION + 1 },
'client',
],
['an older daemon behind a proxy', proxyHealth(DAEMON_RPC_PROTOCOL_VERSION - 1), 'remote'],
[
'a newer daemon behind a proxy',
proxyHealth(DAEMON_RPC_PROTOCOL_VERSION + 1),
'client-and-proxy',
],
] as const)('a refusal of %s names the side to upgrade', async (_label, payload, upgradeSide) => {
await withHealthServer(payload, async (baseUrl) => {
await assert.rejects(
readRemoteDaemonHealth({ baseUrl, token: 't', pid: 0 }),
(error: unknown) => {
const details = (error as { details?: Record<string, unknown> }).details;
assert.equal(details?.upgradeSide, upgradeSide);
assert.equal(details?.clientVersion, readVersion());
assert.equal(details?.remoteVersion, '98.0.0');
return true;
},
);
});
});

test('a daemon health payload without an upstream link or host arch parses as before', async (t) => {
if (await skipWhenLoopbackUnavailable(t)) return;

Expand Down
56 changes: 55 additions & 1 deletion packages/daemon-client/src/daemon-client-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
} from '@agent-device/remote/daemon-artifacts';
import { localizeRemoteDaemonError } from '@agent-device/remote/remote-request-diagnostics';
import type { DaemonInfo } from './daemon-client-metadata.ts';
import { DAEMON_REQUEST_MAX_BYTES } from '@agent-device/contracts/daemon-http';
import {
DAEMON_REQUEST_MAX_BYTES,
DAEMON_RPC_PROTOCOL_VERSION,
} from '@agent-device/contracts/daemon-http';
import { readVersion } from '@agent-device/host-kit/version';
import { DAEMON_TRANSPORT_REASONS } from './daemon-client-transport-reasons.ts';
import { buildDaemonRpcRequest, leaseScopeFromRequest } from '@agent-device/contracts/lease-scope';

Expand Down Expand Up @@ -242,3 +246,53 @@ export function refuseOversizedDaemonRequest(payload: string, requestId: string
hint: 'Split the replay suite or flow into smaller runs.',
});
}

/** Who must upgrade so every link speaks the client's protocol. Client-local: never serialized. */
type ProtocolUpgradeSide = 'remote' | 'client' | 'client-and-proxy';

/** The `/health` link whose advertised RPC protocol differs from the client's. */
type SkewedHealthLink = Readonly<{
service?: string;
version?: string;
rpcProtocolVersion: number;
}>;

/**
* The refusal for a remote link on another RPC protocol. It names both versions and the side to
* upgrade. `proxy` names the proxy when the skewed link is the daemon behind it.
*/
export function incompatibleRemoteProtocolError(
daemonBaseUrl: string,
link: SkewedHealthLink,
proxy: string | undefined,
): AppError {
const clientVersion = readVersion();
const upgradeSide: ProtocolUpgradeSide =
link.rpcProtocolVersion < DAEMON_RPC_PROTOCOL_VERSION
? 'remote'
: proxy
? 'client-and-proxy'
: 'client';
const service = link.service ?? 'daemon';
const remote = proxy ? `the ${service} behind ${proxy}` : `the remote ${service}`;
const upgradeTarget = {
remote: proxy ? remote : 'the remote host',
client: 'this client',
'client-and-proxy': `this client and on ${proxy}`,
}[upgradeSide];
const remoteVersion = link.version ? `runs ${link.version}` : 'does not report its version';
return new AppError(
'COMMAND_FAILED',
`Remote daemon RPC protocol is incompatible: this client runs agent-device ${clientVersion} (RPC protocol ${DAEMON_RPC_PROTOCOL_VERSION}); ${remote} ${remoteVersion} (RPC protocol ${link.rpcProtocolVersion}). Upgrade agent-device on ${upgradeTarget}.`,
{
daemonBaseUrl,
clientVersion,
remoteVersion: link.version,
remoteService: link.service,
supportedRpcProtocolVersion: DAEMON_RPC_PROTOCOL_VERSION,
remoteRpcProtocolVersion: link.rpcProtocolVersion,
upgradeSide,
hint: 'Retry after the upgrade. This check runs before any command is sent.',
},
);
}
19 changes: 5 additions & 14 deletions packages/daemon-client/src/daemon-client-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import { DAEMON_TRANSPORT_REASONS } from './daemon-client-transport-reasons.ts';
import {
buildHttpRpcPayload,
handleDaemonHttpResponseBody,
incompatibleRemoteProtocolError,
refuseOversizedDaemonRequest,
} from './daemon-client-rpc.ts';
import { handleRequestTimeout } from './daemon-client-timeout.ts';
import { isRemoteDaemon, type DaemonInfo } from './daemon-client-metadata.ts';
import type { HostEnvironment } from '@agent-device/kernel/source-value';
import { readVersion } from '@agent-device/host-kit/version';

type ResolvedDaemonTransport = 'socket' | 'http';
type SendRequestOptions = {
Expand Down Expand Up @@ -250,22 +250,13 @@ export async function readRemoteDaemonHealth(
// Every link a command RPC crosses has to speak the client's protocol: a proxy that reports a
// skewed daemon behind it fails here, before the RPC, exactly like a skewed proxy does.
const incompatible = [health, health.upstream].find(
(link) =>
(link): link is RemoteDaemonHealthLink & { rpcProtocolVersion: number } =>
typeof link?.rpcProtocolVersion === 'number' &&
link.rpcProtocolVersion !== DAEMON_RPC_PROTOCOL_VERSION,
);
if (incompatible) {
throw new AppError('COMMAND_FAILED', 'Remote daemon RPC protocol is incompatible', {
daemonBaseUrl: info.baseUrl,
clientVersion: readVersion(),
remoteVersion: incompatible.version,
remoteService: incompatible.service,
supportedRpcProtocolVersion: DAEMON_RPC_PROTOCOL_VERSION,
remoteRpcProtocolVersion: incompatible.rpcProtocolVersion,
hint: 'Upgrade agent-device on the client or remote host so both support the same daemon RPC protocol.',
});
}
return health;
if (!incompatible) return health;
const proxy = incompatible === health.upstream ? (health.service ?? 'the proxy') : undefined;
throw incompatibleRemoteProtocolError(info.baseUrl, incompatible, proxy);
}

function daemonHealthEndpoint(info: DaemonInfo): string | null {
Expand Down
6 changes: 3 additions & 3 deletions test/wire-compat/ledger.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"packages/daemon-client/src/daemon-client-transport.ts#readDaemonHttpHealth": "sha256:6ce1f9be1e6a977130d0875003ac22da2de5e90b514a3b22b30b9347e63d81c7",
"packages/daemon-client/src/daemon-client-transport.ts#readHealthLink": "sha256:b709f88901784e767a542acc27d90ba080e7b016e85d6053a59df3495454ae3b",
"packages/daemon-client/src/daemon-client-transport.ts#readHealthPayload": "sha256:4e85ffc3e35e02379c393e9312344757e003cf1f0ad9eb8d1f77d90c81c861f1",
"packages/daemon-client/src/daemon-client-transport.ts#readRemoteDaemonHealth": "sha256:df174d1ccf73851ca58bd75533fb4660424003b98bd9e41fe942cf3ab3698e5f",
"packages/daemon-client/src/daemon-client-transport.ts#readRemoteDaemonHealth": "sha256:7da1eb056fd2a73b298c1b076bf1704da30b8fe56aa6a52317bdbaf894b1ee05",
"packages/daemon/src/server/downloadable-artifact-http.ts#DownloadableArtifactHttpAuthorizer": "sha256:1b2702a929ca9170db2ca97c08e3ab67e17edb3ee75325a576c4c1b9cdbebb44",
"packages/daemon/src/server/downloadable-artifact-http.ts#DownloadableArtifactHttpRoute": "sha256:e63c4581ccde668913914149c9092d16ecf8a5cbd7ab33c6eb8617e77fc1015e",
"packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactDownload": "sha256:9f72097ae602ee8040c7a42104d682e4ea6b4c5999aa653a72b3cf47d4f5b81d",
Expand Down Expand Up @@ -299,8 +299,8 @@
},
{
"declaration": "packages/daemon-client/src/daemon-client-transport.ts#readRemoteDaemonHealth",
"digest": "sha256:df174d1ccf73851ca58bd75533fb4660424003b98bd9e41fe942cf3ab3698e5f",
"rationale": "#2198 checks every link a command RPC crosses for protocol skew. #2650 caps a retry health probe at the request's remaining deadline. Health payload parsing and protocol mismatch refusal are unchanged; released peers still receive the same RPCs. #3178 threads the caller's optional AbortSignal through so a canceled call cuts a probe already in flight instead of waiting it out: the signal is client-local, never serialized, and the GET /health request and payload are unchanged."
"digest": "sha256:7da1eb056fd2a73b298c1b076bf1704da30b8fe56aa6a52317bdbaf894b1ee05",
"rationale": "#2198 checks every link a command RPC crosses for protocol skew. #2650 caps a retry health probe at the request's remaining deadline. Health payload parsing and protocol mismatch refusal are unchanged; released peers still receive the same RPCs. #3178 threads the caller's optional AbortSignal through so a canceled call cuts a probe already in flight instead of waiting it out: the signal is client-local, never serialized, and the GET /health request and payload are unchanged. The mismatch refusal now names both versions and the side to upgrade in its message, with a new hint and a client-local upgradeSide detail; the comparison it makes, the /health request, and the refusal's code and existing detail fields are unchanged, and nothing new is serialized."
},
{
"declaration": "packages/daemon/src/server/http/http-authorization.ts#authorizeAuxiliaryHttpRequest",
Expand Down
Loading