Skip to content

Commit 0bd157d

Browse files
committed
feat(remote): name the held lease on upload, artifact download and diagnostics requests
1 parent f70224a commit 0bd157d

12 files changed

Lines changed: 536 additions & 48 deletions

‎packages/contracts/src/daemon-http.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
buildDaemonHealthPayload,
55
buildDaemonHttpAuthHeaders,
66
buildDaemonHttpBaseUrl,
7+
buildDaemonHttpLeaseHeaders,
78
buildDaemonHttpTenantHeaders,
89
buildDaemonHttpUrl,
910
buildRemoteTempArtifactDirPath,
@@ -49,6 +50,14 @@ test('buildDaemonHttpTenantHeaders omits blank tenant identities', () => {
4950
assert.deepEqual(buildDaemonHttpTenantHeaders(''), {});
5051
});
5152

53+
test('buildDaemonHttpLeaseHeaders names a held lease and omits a blank one', () => {
54+
assert.deepEqual(buildDaemonHttpLeaseHeaders(' lease-1 '), {
55+
'x-agent-device-lease-id': 'lease-1',
56+
});
57+
assert.deepEqual(buildDaemonHttpLeaseHeaders(''), {});
58+
assert.deepEqual(buildDaemonHttpLeaseHeaders(undefined), {});
59+
});
60+
5261
test('buildDaemonHealthPayload takes the version from its caller and keeps the payload shape', () => {
5362
assert.deepEqual(buildDaemonHealthPayload('agent-device-daemon', '0.20.9'), {
5463
ok: true,

‎packages/contracts/src/daemon-http.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import path from 'node:path';
22

33
// The daemon HTTP wire vocabulary shared by the daemon server, the remote
4-
// proxy, and every client that talks to them: the base path, the tenant and
5-
// network-access header names, the URL/auth/tenant header builders, the
4+
// proxy, and every client that talks to them: the base path, the tenant, lease
5+
// and network-access header names, the URL/auth/tenant/lease header builders, the
66
// /health payload, and the temp artifact paths a remote client names on the
77
// daemon host. Client and server must agree on all of it, so neither side
88
// owns it (ADR 0006).
99
export const DAEMON_HTTP_BASE_PATH = '/agent-device';
1010
export const DAEMON_HTTP_TENANT_HEADER = 'x-agent-device-tenant';
11+
// Names the lease a non-RPC request (upload, artifact download, request diagnostics) belongs to,
12+
// as RPC calls name it in their body. Neither the daemon nor the proxy reads it; it lets a gateway
13+
// in front of many daemons route those requests to the one holding the lease.
14+
const DAEMON_HTTP_LEASE_HEADER = 'x-agent-device-lease-id';
1115
export const DAEMON_HTTP_NETWORK_ACCESS_HEADER = 'x-agent-device-network-access';
1216
export const DAEMON_HTTP_PUBLIC_NETWORK_ACCESS = 'public-only';
1317

@@ -35,6 +39,12 @@ export function buildDaemonHttpTenantHeaders(tenantId: string | undefined): Reco
3539
return { [DAEMON_HTTP_TENANT_HEADER]: normalizedTenantId };
3640
}
3741

42+
export function buildDaemonHttpLeaseHeaders(leaseId: string | undefined): Record<string, string> {
43+
const normalizedLeaseId = leaseId?.trim();
44+
if (!normalizedLeaseId) return {};
45+
return { [DAEMON_HTTP_LEASE_HEADER]: normalizedLeaseId };
46+
}
47+
3848
// See docs/adr/0006-daemon-rpc-protocol-version.md before changing this value.
3949
// Enforced, not just documented: `test/wire-compat/` digests the declarations
4050
// that cross this boundary and fails when one changes shape without a bump or

‎src/__tests__/upload-client.test.ts‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,152 @@ test('uploadArtifact uploads APK, AAB, and IPA files without wrapping them', asy
918918
}
919919
});
920920

921+
/**
922+
* A daemon fake that answers every upload route and records the lease header each request carried,
923+
* keyed by `METHOD path`. The direct ticket points at `ticketOrigin` (the daemon's own by default).
924+
*/
925+
async function startLeaseRecordingUploadServer(options: {
926+
preflight: 'direct' | 'unsupported';
927+
ticketOrigin?: (baseUrl: string) => string;
928+
}): Promise<{
929+
baseUrl: string;
930+
close: () => Promise<void>;
931+
leaseHeaders: Map<string, unknown>;
932+
}> {
933+
const leaseHeaders = new Map<string, unknown>();
934+
const server = await startServer(async (req, res) => {
935+
leaseHeaders.set(`${req.method} ${req.url}`, req.headers['x-agent-device-lease-id']);
936+
await readRequestBody(req);
937+
if (req.method === 'POST' && req.url === '/upload/preflight') {
938+
if (options.preflight === 'unsupported') {
939+
res.statusCode = 404;
940+
res.end('not found');
941+
return;
942+
}
943+
const origin = (options.ticketOrigin ?? ((baseUrl) => baseUrl))(server.baseUrl);
944+
sendJson(res, {
945+
ok: true,
946+
cacheHit: false,
947+
uploadId: 'ticket-1',
948+
upload: { url: `${origin}/upload/direct/ticket-1`, headers: {} },
949+
});
950+
return;
951+
}
952+
if (req.method === 'PUT' && req.url === '/upload/direct/ticket-1') {
953+
res.statusCode = 200;
954+
res.end('ok');
955+
return;
956+
}
957+
if (req.method === 'POST' && (req.url === '/upload/finalize' || req.url === '/upload')) {
958+
sendJson(res, { ok: true, uploadId: 'upload-done' });
959+
return;
960+
}
961+
res.statusCode = 404;
962+
res.end('not found');
963+
});
964+
return { ...server, leaseHeaders };
965+
}
966+
967+
test('uploadArtifact names the held lease on preflight, direct upload and finalize', async () => {
968+
const artifactPath = createTempFile('app.apk', 'leased-direct-payload');
969+
const server = await startLeaseRecordingUploadServer({ preflight: 'direct' });
970+
try {
971+
const uploadId = await uploadArtifact({
972+
localPath: artifactPath,
973+
baseUrl: server.baseUrl,
974+
token: TEST_TOKEN,
975+
leaseId: 'lease-1',
976+
});
977+
assert.equal(uploadId, 'upload-done');
978+
assert.deepEqual(Object.fromEntries(server.leaseHeaders), {
979+
'POST /upload/preflight': 'lease-1',
980+
'PUT /upload/direct/ticket-1': 'lease-1',
981+
'POST /upload/finalize': 'lease-1',
982+
});
983+
} finally {
984+
await server.close();
985+
}
986+
});
987+
988+
test('uploadArtifact names the held lease on the legacy upload', async () => {
989+
const artifactPath = createTempFile('app.apk', 'leased-legacy-payload');
990+
const server = await startLeaseRecordingUploadServer({
991+
preflight: 'unsupported',
992+
});
993+
try {
994+
await uploadArtifact({
995+
localPath: artifactPath,
996+
baseUrl: server.baseUrl,
997+
token: TEST_TOKEN,
998+
leaseId: 'lease-1',
999+
});
1000+
assert.deepEqual(Object.fromEntries(server.leaseHeaders), {
1001+
'POST /upload/preflight': 'lease-1',
1002+
'POST /upload': 'lease-1',
1003+
});
1004+
} finally {
1005+
await server.close();
1006+
}
1007+
});
1008+
1009+
test('uploadArtifact sends no lease header when no lease is held', async () => {
1010+
const directPath = createTempFile('app.apk', 'unleased-direct-payload');
1011+
const legacyPath = createTempFile('app.apk', 'unleased-legacy-payload');
1012+
const direct = await startLeaseRecordingUploadServer({ preflight: 'direct' });
1013+
const legacy = await startLeaseRecordingUploadServer({
1014+
preflight: 'unsupported',
1015+
});
1016+
try {
1017+
await uploadArtifact({
1018+
localPath: directPath,
1019+
baseUrl: direct.baseUrl,
1020+
token: TEST_TOKEN,
1021+
});
1022+
await uploadArtifact({
1023+
localPath: legacyPath,
1024+
baseUrl: legacy.baseUrl,
1025+
token: TEST_TOKEN,
1026+
});
1027+
assert.deepEqual(Object.fromEntries(direct.leaseHeaders), {
1028+
'POST /upload/preflight': undefined,
1029+
'PUT /upload/direct/ticket-1': undefined,
1030+
'POST /upload/finalize': undefined,
1031+
});
1032+
assert.deepEqual(Object.fromEntries(legacy.leaseHeaders), {
1033+
'POST /upload/preflight': undefined,
1034+
'POST /upload': undefined,
1035+
});
1036+
} finally {
1037+
await direct.close();
1038+
await legacy.close();
1039+
}
1040+
});
1041+
1042+
test('uploadArtifact keeps the lease off a direct upload ticket on another origin', async () => {
1043+
const artifactPath = createTempFile('app.apk', 'foreign-ticket-payload');
1044+
// `localhost` and `127.0.0.1` reach the same fake but are different origins, as a signed
1045+
// storage URL would be.
1046+
const server = await startLeaseRecordingUploadServer({
1047+
preflight: 'direct',
1048+
ticketOrigin: (baseUrl) => baseUrl.replace('127.0.0.1', 'localhost'),
1049+
});
1050+
try {
1051+
await uploadArtifact({
1052+
localPath: artifactPath,
1053+
baseUrl: server.baseUrl,
1054+
token: TEST_TOKEN,
1055+
leaseId: 'lease-1',
1056+
});
1057+
assert.deepEqual(Object.fromEntries(server.leaseHeaders), {
1058+
'POST /upload/preflight': 'lease-1',
1059+
'PUT /upload/direct/ticket-1': undefined,
1060+
'POST /upload/finalize': 'lease-1',
1061+
});
1062+
} finally {
1063+
await server.close();
1064+
}
1065+
});
1066+
9211067
function createTempDir(): string {
9221068
const dir = mkdtempForTestSync(`agent-device-upload-client-${randomUUID()}-`);
9231069
tempDirs.push(dir);

0 commit comments

Comments
 (0)