From 15262cbc1af82d602380bf8208075dd28142445a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 08:24:56 +0100 Subject: [PATCH 01/15] fix(daemon): give leased artifacts and diagnostics records only to their lease id Output of a request that acted under a lease keeps that lease as its owner. Artifact inventory and download (HTTP and the artifacts RPC) and the request diagnostics route give it only to a request that sends the lease id, for as long as the daemon keeps the output, also after the lease ends. The proxy forwards the lease header, REST errors carry details.field, and the CLI sends the ended proxy lease id on artifacts. Closes #4912 --- docs/adr/0007-remote-device-leases.md | 1 + .../0024-screen-recording-provable-signal.md | 3 +- .../docs/remote-device-lease-contracts.md | 13 +- .../src/resources/artifact-tracking.test.ts | 34 +-- .../daemon/src/resources/artifact-tracking.ts | 48 ++-- .../src/resources/lease-registry-scope.ts | 27 ++ .../__tests__/http-server-artifacts.test.ts | 12 +- .../src/server/downloadable-artifact-http.ts | 14 +- packages/daemon/src/server/http-errors.ts | 3 +- .../http/http-server-capture-tenant.test.ts | 14 +- .../http-server-proxy-leased-output.test.ts | 252 ++++++++++++++++++ .../http/http-server-remote-output.test.ts | 20 +- .../src/server/request-diagnostics-http.ts | 45 +++- .../daemon/src/server/request-finalization.ts | 1 + .../leased-recording-retention.test.ts | 61 +++-- .../daemon/src/sessions/handlers/lease.ts | 6 +- .../sessions/leased-recording-retention.ts | 5 +- packages/proxy/src/daemon-proxy.ts | 2 + .../remote/src/remote-connection-state.ts | 3 + src/cli/commands/connection-runtime.ts | 2 +- src/cli/connection/ended-lease.test.ts | 69 +++-- src/cli/connection/ended-lease.ts | 15 +- src/commands/schema/cli-help.ts | 2 +- test/wire-compat/ledger.json | 24 +- website/docs/docs/commands.md | 2 +- website/docs/docs/error-reasons.md | 1 + website/docs/docs/remote-proxy.md | 8 +- 27 files changed, 563 insertions(+), 124 deletions(-) create mode 100644 packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts diff --git a/docs/adr/0007-remote-device-leases.md b/docs/adr/0007-remote-device-leases.md index b33b76280c..bf9e45c22a 100644 --- a/docs/adr/0007-remote-device-leases.md +++ b/docs/adr/0007-remote-device-leases.md @@ -21,6 +21,7 @@ Runner and process locks remain separate backend guards. The proxy token authenticates the host, not the client: every client of one proxy can hold the same token. The lease id is the credential that proves ownership of a leased session. A request on a session that holds a lease must send that lease's id. The daemon refuses a request that omits it with `LEASE_SESSION_MISMATCH` and never fills it from the session. +Output that a leased request produced keeps that lease as its owner: a downloadable artifact, and a request diagnostics record. A request reads such output only when it sends the lease id, for as long as the daemon keeps the output, also after the lease ends. The daemon refuses another reader with `LEASE_OUTPUT_MISMATCH`. Ownership stays with the output, not with the session, because the output can outlive its session and its lease. Tenant, run id, lease backend, client id, device key, and lease provider are scope labels. The daemon checks a label when the request sends it. A label never grants access. A lease id that a client chooses on allocate needs at least 128 random bits. The daemon returns a lease id only to the caller that allocated the lease. A daemon that enforces this rule reports the `session-lease-id` capability. diff --git a/docs/adr/0024-screen-recording-provable-signal.md b/docs/adr/0024-screen-recording-provable-signal.md index b220be489f..7211d84905 100644 --- a/docs/adr/0024-screen-recording-provable-signal.md +++ b/docs/adr/0024-screen-recording-provable-signal.md @@ -287,7 +287,8 @@ Artifact cleanup is a separate, explicit decision (rule 6). Session teardown and failed-start rollback dispose through the backend under the fence. They do this only after the manifest records that no recoverable export is lost. -A leased session's teardown that commits an export at a remote temp path registers it, and its telemetry, as downloadable artifacts of the lease's tenant (rule 1, #2281). +A leased session's teardown that commits an export at a remote temp path registers it, and its telemetry, as downloadable artifacts of that lease (rule 1, #2281). +Only a request that sends the id of the ended lease lists or downloads them (ADR 0007). The inventory names the session, and the usual delete-after-download and 15-minute expiry apply. A later `record stop` replay of that manifest serves the same artifact ids. If registration fails, teardown deletes the files. diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 1172abe50a..098ad81826 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -17,7 +17,7 @@ Idle reap then shuts the daemon down within one more idle window. A session cannot outlive the lease under which it was opened. When the lease is released, expires, or its provider reports the instance ended, the daemon tears the session down under its execution locks and clears its device claim. -Any teardown of a leased session, including `close`, finishes an active recording. A finished export at a remote temp path then belongs to the lease's tenant as a downloadable artifact, like the artifact of any `record stop` (ADR 0024 2.5). +Any teardown of a leased session, including `close`, finishes an active recording. A finished export at a remote temp path then stays a downloadable artifact of that lease, like the artifact of any `record stop` (ADR 0024 2.5). This applies even when the device no longer answers. A release is a tenant `lease_release` or a host `DELETE /admin/leases/`. The daemon sweeps for expired leases every second. @@ -55,6 +55,7 @@ Nested requests record on the ledger of the outermost request, so a step reply n If a close finds its lease already inactive, it reports that lease as ended. A client never sends a release after such a request. If a client stores the lease id, it forgets that id when the reply names it. +The CLI keeps the id of an ended proxy lease apart, and sends it only on `artifacts` while the connection holds no lease. A gateway that relays the reply reads the lease end from it. Close and lease-end teardown take the same execution locks. @@ -85,6 +86,16 @@ An allocate that repeats the labels of a live lease without that lease's id is r A caller-chosen lease id on allocate must have at least 32 hex digits (128 bits). A shorter id fails with reason `LEASE_ID_TOO_SHORT`. Commands that ignore the session lease, such as `devices`, `session_list`, and the lease RPCs, keep their own scope. +Output of a request that acted under a lease belongs to that lease. +This applies to a downloadable artifact and to a request diagnostics record. +The daemon gives such output only to a request that sends the lease id. +The `artifacts` RPC reads the lease id from the request. `GET /artifacts`, `GET /artifacts/`, and the diagnostics route read the `x-agent-device-lease-id` header, which the proxy forwards. +This rule applies for as long as the daemon keeps the output, also after the lease ends. The lifetime of the output does not change. +An inventory omits an artifact of another lease. +A download or a diagnostics read without the lease id fails with `UNAUTHORIZED`, reason `LEASE_OUTPUT_MISMATCH`, and `field: 'leaseId'`. +If a later request registers a file that a lease already registered, the file stays with that lease. +Output of a request that acted under no lease keeps the tenant rule. + ## Admitted request work A lease renews when a request is admitted, and never again while that request runs. diff --git a/packages/daemon/src/resources/artifact-tracking.test.ts b/packages/daemon/src/resources/artifact-tracking.test.ts index e318c92593..638ae53b03 100644 --- a/packages/daemon/src/resources/artifact-tracking.test.ts +++ b/packages/daemon/src/resources/artifact-tracking.test.ts @@ -57,7 +57,7 @@ test.each(['cleanup', 'expiry'] as const)( } finally { timers.mockRestore(); } - listing = runtime.run(() => listDownloadableArtifacts(undefined, process.env)); + listing = runtime.run(() => listDownloadableArtifacts({}, process.env)); await started; expect(fs.existsSync(archivePath)).toBe(true); if (trigger === 'expiry') { @@ -99,7 +99,7 @@ test('a missing cached directory archive is rebuilt from the live source', async const id = runtime.run(() => trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }), ); - const first = await runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const first = await runtime.run(() => openDownloadableArtifact(id, {}, process.env)); downloads.push(first); expect(gunzipSync(await buffer(first.stream)).includes(expected)).toBe(true); await closed(first.stream); @@ -107,7 +107,7 @@ test('a missing cached directory archive is rebuilt from the live source', async const oldArchive = String(first.stream.path); fs.unlinkSync(oldArchive); expect(fs.existsSync(artifactPath)).toBe(true); - const second = await runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const second = await runtime.run(() => openDownloadableArtifact(id, {}, process.env)); downloads.push(second); expect(gunzipSync(await buffer(second.stream)).includes(expected)).toBe(true); await closed(second.stream); @@ -155,12 +155,12 @@ test('cancelling the archive builder preserves a second download waiting for its trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }), ); const timerIndex = timers.mock.calls.length; - const first = runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const first = runtime.run(() => openDownloadableArtifact(id, {}, process.env)); openings.push(first); void first.catch(() => {}); const cancelFirst = timers.mock.calls[timerIndex]![0]; await started; - const second = runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const second = runtime.run(() => openDownloadableArtifact(id, {}, process.env)); openings.push(second); void second.catch(() => {}); runtime.run(cancelFirst); @@ -242,7 +242,7 @@ test.each(['deadline-aborted', 'late-aborted'] as const)( trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }), ); const timers = vi.spyOn(globalThis, 'setTimeout'); - const first = runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const first = runtime.run(() => openDownloadableArtifact(id, {}, process.env)); openings.push(first); void first.catch(() => {}); const deadline = timers.mock.calls.find(([, delay]) => delay === 15 * 60 * 1000)?.[0]; @@ -253,7 +253,7 @@ test.each(['deadline-aborted', 'late-aborted'] as const)( expect(deadline).toBeTypeOf('function'); runtime.run(() => deadline!()); await cancelled; - const second = runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const second = runtime.run(() => openDownloadableArtifact(id, {}, process.env)); openings.push(second); void second.catch(() => {}); finishAborted(); @@ -313,9 +313,7 @@ test.each(['before opening', 'while archiving'] as const)( trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }), ); if (phase === 'before opening') caller.abort(reason); - opening = runtime.run(() => - openDownloadableArtifact(id, undefined, process.env, caller.signal), - ); + opening = runtime.run(() => openDownloadableArtifact(id, {}, process.env, caller.signal)); void opening.catch(() => {}); if (phase === 'while archiving') caller.abort(reason); const error = await opening.catch((error: unknown) => error); @@ -369,9 +367,9 @@ test('disposing every archive waiter aborts the shared build and removes its tem const id = runtime.run(() => trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' }), ); - openings.push(runtime.run(() => openDownloadableArtifact(id, undefined, process.env))); + openings.push(runtime.run(() => openDownloadableArtifact(id, {}, process.env))); await started; - openings.push(runtime.run(() => openDownloadableArtifact(id, undefined, process.env))); + openings.push(runtime.run(() => openDownloadableArtifact(id, {}, process.env))); for (const opening of openings) void opening.catch(() => {}); runtime.dispose(); await aborted(archiveSignal); @@ -477,15 +475,11 @@ test.each(['old-first', 'new-first'] as const)( runtime.run(() => trackDownloadableArtifact({ artifactPath, artifactType: 'trace-log' })); try { const firstId = track(); - downloads.push( - await runtime.run(() => openDownloadableArtifact(firstId, undefined, process.env)), - ); + downloads.push(await runtime.run(() => openDownloadableArtifact(firstId, {}, process.env))); runtime.run(() => cleanupDownloadableArtifact(firstId)); const secondId = track(); expect(secondId).not.toBe(firstId); - downloads.push( - await runtime.run(() => openDownloadableArtifact(secondId, undefined, process.env)), - ); + downloads.push(await runtime.run(() => openDownloadableArtifact(secondId, {}, process.env))); runtime.run(() => cleanupDownloadableArtifact(secondId)); for (const download of downloads) download.stream.resume(); await reads.untilPending(2); @@ -544,7 +538,7 @@ test.each(['expiry-first', 'deadline-first'] as const)( const expire = timers.mock.calls.at(-1)![0]; const expiryTimer = timers.mock.results.at(-1)!.value; const timerIndex = timers.mock.calls.length; - const opening = runtime.run(() => openDownloadableArtifact(id, undefined, process.env)); + const opening = runtime.run(() => openDownloadableArtifact(id, {}, process.env)); const deadline = timers.mock.calls[timerIndex]![0]; download = await opening; const archiveDir = path.dirname(String(download.stream.path)); @@ -659,7 +653,7 @@ test.each( ); const open = async (id: string) => { const artifact: OpenedDownloadableArtifact = await runtime.run(() => - openDownloadableArtifact(id, 'tenant-a', process.env), + openDownloadableArtifact(id, { tenantId: 'tenant-a' }, process.env), ); downloads.push(artifact); return artifact; diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 2418b802a2..8365e759ff 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -13,6 +13,7 @@ import { requireTenantOwnedEntry, type TenantOwnedResourceKind, } from '../core/tenant-owned-entry.ts'; +import { leasedOutputRefusal, requestHoldsOutputLease } from './lease-registry-scope.ts'; // --- Downloadable artifact tracking --- @@ -53,6 +54,7 @@ type ArtifactDownload = { type ArtifactEntry = { artifactPath: string; tenantId?: string; + leaseId?: string; artifactType: DaemonArtifactType | undefined; fileName?: string; displayRotation?: DeviceRotation; @@ -94,6 +96,8 @@ const pendingUploads = () => artifactTracking().uploads; export type DownloadableArtifactRegistration = { artifactPath: string; tenantId?: string; + /** The lease that produced the artifact. Only a request that sends this id lists or downloads it. */ + leaseId?: string; artifactType: DaemonArtifactType | undefined; fileName?: string; displayRotation?: DeviceRotation; @@ -104,6 +108,9 @@ export type DownloadableArtifactRegistration = { export type TrackDownloadableArtifact = (registration: DownloadableArtifactRegistration) => string; +/** What a request presents to list or download artifacts. */ +export type DownloadableArtifactReader = { tenantId?: string; leaseId?: string }; + type PreparedDownloadableArtifact = { artifactPath: string; fileName?: string; @@ -121,10 +128,11 @@ export type OpenedDownloadableArtifact = PreparedDownloadableArtifact & { /** * Registers one file for download and returns its id. A file the same tenant can already download * returns its existing entry unchanged: two ids for one file would each delete it after their own - * download. + * download. A file that a lease registered keeps that entry for every later registration, so a + * request without that lease cannot register the file again and read it. */ export function trackDownloadableArtifact(params: DownloadableArtifactRegistration): string { - const existingId = findTrackedArtifactId(params.artifactPath, params.tenantId); + const existingId = findTrackedArtifactId(params); if (existingId) return existingId; const artifactId = crypto.randomUUID(); const createdAt = Date.now(); @@ -141,6 +149,7 @@ export function trackDownloadableArtifact(params: DownloadableArtifactRegistrati pendingArtifacts().set(artifactId, { artifactPath: params.artifactPath, tenantId: params.tenantId, + ...(params.leaseId !== undefined ? { leaseId: params.leaseId } : {}), artifactType: params.artifactType, fileName: params.fileName, ...(params.displayRotation !== undefined ? { displayRotation: params.displayRotation } : {}), @@ -155,16 +164,12 @@ export function trackDownloadableArtifact(params: DownloadableArtifactRegistrati } function findTrackedArtifactId( - artifactPath: string, - tenantId: string | undefined, + owner: Pick, ): string | undefined { for (const [artifactId, entry] of pendingArtifacts()) { - if ( - !entry.cleanupRequested && - entry.artifactPath === artifactPath && - entry.tenantId === tenantId - ) - return artifactId; + if (entry.cleanupRequested || entry.artifactPath !== owner.artifactPath) continue; + if (entry.leaseId !== undefined) return artifactId; + if (entry.tenantId === owner.tenantId && owner.leaseId === undefined) return artifactId; } return undefined; } @@ -178,13 +183,13 @@ function findArtifactSourceCleanup(artifactPath: string): ArtifactSourceCleanup export async function openDownloadableArtifact( artifactId: string, - tenantId: string | undefined, + reader: DownloadableArtifactReader, hostEnv: NodeJS.ProcessEnv, signal?: AbortSignal, ): Promise { if (signal?.aborted) throw createRequestCanceledError(); const artifacts = pendingArtifacts(); - const { entry, stat } = readDownloadableArtifactEntry(artifactId, tenantId); + const { entry, stat } = readDownloadableArtifactEntry(artifactId, reader); const download: ArtifactDownload = { controller: new AbortController(), release: () => { @@ -243,12 +248,12 @@ function cancelArtifactDownload( } export async function listDownloadableArtifacts( - tenantId: string | undefined, + reader: DownloadableArtifactReader, hostEnv: NodeJS.ProcessEnv, ): Promise { const artifacts: DaemonArtifactInventoryEntry[] = []; for (const [id, entry] of pendingArtifacts()) { - if (!canReadArtifact(entry, tenantId)) continue; + if (!canReadArtifact(entry, reader)) continue; let stat: fs.Stats; try { stat = fs.statSync(entry.artifactPath); @@ -289,14 +294,17 @@ async function prepareArtifactPayloadForInventory( function readDownloadableArtifactEntry( artifactId: string, - tenantId: string | undefined, + reader: DownloadableArtifactReader, ): { entry: ArtifactEntry; stat: fs.Stats } { const entry = requireTenantOwnedEntry( pendingArtifacts(), artifactId, - tenantId, + reader.tenantId, DOWNLOADABLE_ARTIFACT_RESOURCE, ); + if (!requestHoldsOutputLease(entry.leaseId, reader.leaseId)) { + throw leasedOutputRefusal(DOWNLOADABLE_ARTIFACT_RESOURCE.label); + } if (entry.cleanupRequested) { throw expiredTenantOwnedEntryError(DOWNLOADABLE_ARTIFACT_RESOURCE, artifactId); } @@ -435,8 +443,12 @@ function archiveFilename(fileName: string): string { return `${fileName}.tar.gz`; } -function canReadArtifact(entry: ArtifactEntry, tenantId: string | undefined): boolean { - return !entry.cleanupRequested && (!entry.tenantId || entry.tenantId === tenantId); +function canReadArtifact(entry: ArtifactEntry, reader: DownloadableArtifactReader): boolean { + return ( + !entry.cleanupRequested && + (!entry.tenantId || entry.tenantId === reader.tenantId) && + requestHoldsOutputLease(entry.leaseId, reader.leaseId) + ); } function safeArtifactFilename(fileName: string | undefined, fallback: string): string { diff --git a/packages/daemon/src/resources/lease-registry-scope.ts b/packages/daemon/src/resources/lease-registry-scope.ts index 11221baff1..0fdc029a24 100644 --- a/packages/daemon/src/resources/lease-registry-scope.ts +++ b/packages/daemon/src/resources/lease-registry-scope.ts @@ -418,3 +418,30 @@ export function leaseReleaseRequestFor(lease: SessionLease): ReleaseLeaseRequest clientId: lease.clientId, }); } + +/** The `error.details.reason` values a leased-output refusal publishes. */ +const LEASED_OUTPUT_REASONS = { + // The request does not send the id of the lease that produced the artifact or diagnostics record. + leaseMismatch: { reason: 'LEASE_OUTPUT_MISMATCH' }, +} as const satisfies ReasonTable<'leases'>; + +/** + * Whether a request may read output that `ownerLeaseId` produced. Output of a leased request + * belongs to that lease for as long as the output is kept, also after the lease ends: only a + * request that sends the lease id reads it (ADR 0007). Output with no lease has no lease rule. + */ +export function requestHoldsOutputLease( + ownerLeaseId: string | undefined, + presentedLeaseId: string | undefined, +): boolean { + if (ownerLeaseId === undefined) return true; + const owner = normalizeLeaseId(ownerLeaseId); + return owner !== undefined && owner === normalizeLeaseId(presentedLeaseId); +} + +export function leasedOutputRefusal(label: string): AppError { + return new AppError('UNAUTHORIZED', `${label} belongs to a device lease. Send its lease id.`, { + reason: LEASED_OUTPUT_REASONS.leaseMismatch.reason, + field: 'leaseId', + }); +} diff --git a/packages/daemon/src/server/__tests__/http-server-artifacts.test.ts b/packages/daemon/src/server/__tests__/http-server-artifacts.test.ts index f713c1bacf..b6a1f47b0b 100644 --- a/packages/daemon/src/server/__tests__/http-server-artifacts.test.ts +++ b/packages/daemon/src/server/__tests__/http-server-artifacts.test.ts @@ -60,19 +60,17 @@ test('downloadable artifact inventory is filtered by tenant', async () => { try { assert.deepEqual( - (await listDownloadableArtifacts(undefined, process.env)).map( - (artifact) => artifact.filename, - ), + (await listDownloadableArtifacts({}, process.env)).map((artifact) => artifact.filename), ['public.txt'], ); assert.deepEqual( - (await listDownloadableArtifacts('tenant-a', process.env)).map( + (await listDownloadableArtifacts({ tenantId: 'tenant-a' }, process.env)).map( (artifact) => artifact.filename, ), ['public.txt', 'tenant-a.txt'], ); assert.deepEqual( - (await listDownloadableArtifacts('tenant-b', process.env)).map( + (await listDownloadableArtifacts({ tenantId: 'tenant-b' }, process.env)).map( (artifact) => artifact.filename, ), ['public.txt', 'tenant-b.txt'], @@ -111,7 +109,7 @@ test('downloadable artifact inventory skips directory artifacts that fail to arc if (cmd !== 'tar') return undefined; throw new Error('tar unavailable'); }, - async () => await listDownloadableArtifacts(undefined, process.env), + async () => await listDownloadableArtifacts({}, process.env), ); assert.deepEqual( artifacts.map((artifact) => artifact.filename), @@ -430,7 +428,7 @@ test('downloadable artifact inventory reports a screenshot display rotation only }); try { - const inventory = await listDownloadableArtifacts(undefined, process.env); + const inventory = await listDownloadableArtifacts({}, process.env); const rotated = inventory.find((entry) => entry.id === rotatedId); const plain = inventory.find((entry) => entry.id === plainId); assert.equal(rotated?.displayRotation, 'landscape-right'); diff --git a/packages/daemon/src/server/downloadable-artifact-http.ts b/packages/daemon/src/server/downloadable-artifact-http.ts index 59fce82a46..6b9a14068d 100644 --- a/packages/daemon/src/server/downloadable-artifact-http.ts +++ b/packages/daemon/src/server/downloadable-artifact-http.ts @@ -3,11 +3,13 @@ import { once } from 'node:events'; import { Transform } from 'node:stream'; import { finished, pipeline } from 'node:stream/promises'; import { AppError, normalizeError } from '@agent-device/kernel/errors'; +import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { cleanupDownloadableArtifact, listDownloadableArtifacts, openDownloadableArtifact, + type DownloadableArtifactReader, } from '../resources/artifact-tracking.ts'; import { failStreamedHttpResponse, sendRestJsonError } from './http-errors.ts'; import { decodeUriSegment } from './http-request-target.ts'; @@ -98,7 +100,7 @@ async function handleArtifactDownload( const artifact = await openDownloadableArtifact( artifactId, - auth.tenantId, + artifactReader(req, auth), options.hostEnv, signal, ); @@ -173,7 +175,7 @@ async function handleArtifactInventory( res.setHeader('content-type', 'application/json'); res.end( JSON.stringify({ - artifacts: await listDownloadableArtifacts(auth.tenantId, options.hostEnv), + artifacts: await listDownloadableArtifacts(artifactReader(req, auth), options.hostEnv), }), ); } catch (error) { @@ -181,6 +183,14 @@ async function handleArtifactInventory( } } +function artifactReader( + req: http.IncomingMessage, + auth: { tenantId?: string }, +): DownloadableArtifactReader { + const leaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; + return { tenantId: auth.tenantId, ...(typeof leaseId === 'string' ? { leaseId } : {}) }; +} + function readArtifactId(pathname: string): string { const encoded = pathname.slice('/artifacts/'.length); if (!encoded || encoded.includes('/')) return ''; diff --git a/packages/daemon/src/server/http-errors.ts b/packages/daemon/src/server/http-errors.ts index ea1726178f..72837fd875 100644 --- a/packages/daemon/src/server/http-errors.ts +++ b/packages/daemon/src/server/http-errors.ts @@ -32,13 +32,14 @@ export function statusCodeForNormalizedError(error: { export function sendRestJsonError(res: http.ServerResponse, normalized: NormalizedHttpError): void { res.statusCode = statusCodeForNormalizedError(normalized); res.setHeader('content-type', 'application/json'); - const reason = normalized.details?.reason; + const { reason, field } = normalized.details ?? {}; res.end( JSON.stringify({ ok: false, error: normalized.message, code: normalized.code, ...(typeof reason === 'string' ? { reason } : {}), + ...(typeof field === 'string' ? { field } : {}), ...(normalized.hint !== undefined ? { hint: normalized.hint } : {}), }), ); diff --git a/packages/daemon/src/server/http/http-server-capture-tenant.test.ts b/packages/daemon/src/server/http/http-server-capture-tenant.test.ts index a8dc335bdd..86bc361f61 100644 --- a/packages/daemon/src/server/http/http-server-capture-tenant.test.ts +++ b/packages/daemon/src/server/http/http-server-capture-tenant.test.ts @@ -9,6 +9,7 @@ import { mkdtempForTestSync, } from '@agent-device/test-kit'; import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; +import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { defineUse } from '@agent-device/contracts/platform-runtime-operations'; import type { DeviceInfo } from '@agent-device/kernel/device'; import { createDaemonHttpServer } from './http-server.ts'; @@ -122,10 +123,13 @@ test.each([false, true])( if (!stopped.ok) return; const artifactId = stopped.data?.artifacts?.[0]?.artifactId; expect(artifactId).toBeTypeOf('string'); - expect(await listDownloadableArtifacts('tenant-b', {})).toEqual([]); + expect(await listDownloadableArtifacts({ tenantId: 'tenant-b' }, {})).toEqual([]); expect( - (await listDownloadableArtifacts(tenantId, {})).some(({ id }) => id === artifactId), + (await listDownloadableArtifacts({ tenantId, leaseId: lease.leaseId }, {})).some( + ({ id }) => id === artifactId, + ), ).toBe(true); + expect(await listDownloadableArtifacts({ tenantId }, {})).toEqual([]); const recovered = await stop(); expect(recovered.ok, JSON.stringify(recovered)).toBe(true); @@ -136,7 +140,11 @@ test.each([false, true])( }); expect(otherTenant.status).toBe(401); const ownTenant = await fetch(`${base}/artifacts/${artifactId}`, { - headers: { authorization: 'Bearer capture-token', 'x-tenant': tenantId }, + headers: { + authorization: 'Bearer capture-token', + 'x-tenant': tenantId, + [DAEMON_HTTP_LEASE_HEADER]: lease.leaseId, + }, }); expect(ownTenant.status).toBe(200); expect(await ownTenant.text()).toBe('private-recording'); diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts new file mode 100644 index 0000000000..20a6d4be9f --- /dev/null +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -0,0 +1,252 @@ +import path from 'node:path'; +import { afterEach, beforeEach, expect, test } from 'vitest'; +import { createDaemonProxyServer } from '@agent-device/proxy'; +import { + closeLoopbackServer, + createTestHostEnvironment, + listenOnLoopback, + mkdtempForTestSync, + skipWhenLoopbackUnavailable, +} from '@agent-device/test-kit'; +import { + DAEMON_HTTP_LEASE_HEADER, + DAEMON_HTTP_TENANT_HEADER, +} from '@agent-device/contracts/daemon-http'; +import { resolveSessionRequestLogPath } from '@agent-device/daemon-contracts/session-artifact-paths'; +import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; +import type { DiagnosticsRecordRef } from '@agent-device/kernel/errors'; +import { createDaemonHttpServer } from './http-server.ts'; +import { createRequestHandler } from '../../__tests__/support/test-device-runtime-gateway.ts'; +import { makeAndroidSession } from '../../__tests__/support/session-factories.ts'; +import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; +import { screenshotRuntimeFixture } from '../../__tests__/support/screenshot-runtime-fixture.ts'; +import { screenshotCommandSurface } from '../../__tests__/support/screenshot-command-surface-fake.ts'; +import { tenantScopedSessionName } from '../../core/session-tenant-scope.ts'; +import { LeaseRegistry } from '../../resources/lease-registry.ts'; +import { + cleanupDownloadableArtifact, + listDownloadableArtifacts, + trackDownloadableArtifact, +} from '../../resources/artifact-tracking.ts'; + +// One proxy token and one tenant label for every client, as a host with many sandboxes has. +const PROXY_TOKEN = 'shared-proxy-token'; +const TENANT = 'e2b'; +const SESSION = 'agentA'; +const OWNER_SCOPE = { tenantId: TENANT, runId: 'run-a' } as const; +const OTHER_LEASE_ID = 'f'.repeat(32); +const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); + +type RpcBody = { + result?: { ok?: boolean; data?: Record }; + error?: { + data?: { + code?: string; + details?: { reason?: string; field?: string }; + diagnosticsRecord?: DiagnosticsRecordRef; + }; + }; +}; + +let world: Awaited> | undefined; + +beforeEach(async (context) => { + if (await skipWhenLoopbackUnavailable(context)) return; + world = await startProxiedDaemon(); +}); + +afterEach(async () => { + await world?.close(); + world = undefined; +}); + +async function startProxiedDaemon() { + const sessionStore = makeSessionStore('agent-device-proxy-leased-output-'); + const leaseRegistry = new LeaseRegistry(); + const lease = leaseRegistry.allocateLease({ ...OWNER_SCOPE, leaseBackend: 'android-instance' }); + const sessionName = tenantScopedSessionName(TENANT, SESSION); + sessionStore.publish( + sessionName, + makeAndroidSession(sessionName, { + sessionScope: { kind: 'tenant', id: TENANT }, + lease: { leaseId: lease.leaseId, ...OWNER_SCOPE, leaseBackend: lease.backend }, + }), + ); + const handleRequest = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(mkdtempForTestSync('agent-device-proxy-leased-output-log-'), 'daemon.log'), + token: 'daemon-token', + sessionStore, + leaseRegistry, + deviceInventoryGateways: createTestDeviceInventoryGateways(), + deviceRuntimeGateway: screenshotRuntimeFixture().gateway, + trackDownloadableArtifact, + }); + const daemon = await createDaemonHttpServer({ + token: 'daemon-token', + handleRequest, + leaseRegistry, + resolveRequestDiagnosticsPath: (ref) => + resolveSessionRequestLogPath(sessionStore.resolveSessionDir(ref.session), ref.requestId), + env: {}, + cwd: process.cwd(), + }); + const daemonPort = await listenOnLoopback(daemon); + const proxy = createDaemonProxyServer({ + upstreamBaseUrl: `http://127.0.0.1:${daemonPort}`, + upstreamToken: 'daemon-token', + clientToken: PROXY_TOKEN, + }); + const base = `http://127.0.0.1:${await listenOnLoopback(proxy)}/agent-device`; + const headers = (leaseId: string | undefined) => ({ + authorization: `Bearer ${PROXY_TOKEN}`, + [DAEMON_HTTP_TENANT_HEADER]: TENANT, + ...(leaseId ? { [DAEMON_HTTP_LEASE_HEADER]: leaseId } : {}), + }); + const send = async ( + command: string, + session: string, + meta: Record, + ): Promise => { + const response = await fetch(`${base}/rpc`, { + method: 'POST', + headers: { authorization: `Bearer ${PROXY_TOKEN}`, 'content-type': 'application/json' }, + body: JSON.stringify({ + jsonrpc: '2.0', + id: command, + method: 'agent_device.command', + params: { + session, + command, + positionals: [], + flags: {}, + meta: { tenantId: TENANT, sessionIsolation: 'tenant', sessionExplicit: true, ...meta }, + }, + }), + }); + return (await response.json()) as RpcBody; + }; + return { + owner: { ...OWNER_SCOPE, leaseId: lease.leaseId }, + leaseId: lease.leaseId, + sessionName, + sessionStore, + leaseRegistry, + send, + get: async (route: string, leaseId?: string) => + await fetch(`${base}${route}`, { headers: headers(leaseId) }), + close: async () => { + for (const reader of [{ tenantId: TENANT, leaseId: lease.leaseId }, { tenantId: TENANT }]) { + for (const { id } of await listDownloadableArtifacts(reader, {})) { + cleanupDownloadableArtifact(id); + } + } + await closeLoopbackServer(proxy); + await closeLoopbackServer(daemon); + }, + }; +} + +async function listedIds(response: Response): Promise { + expect(response.status).toBe(200); + const body = (await response.json()) as { artifacts: Array<{ id: string }> }; + return body.artifacts.map((artifact) => artifact.id); +} + +async function rpcListedIds(body: RpcBody): Promise { + expect(body.result?.ok, JSON.stringify(body)).toBe(true); + return (body.result!.data!.artifacts as Array<{ id: string }>).map((artifact) => artifact.id); +} + +async function expectLeasedOutputRefusal(response: Response): Promise { + expect(response.status).toBe(401); + expect(await response.json()).toMatchObject({ + ok: false, + code: 'UNAUTHORIZED', + reason: 'LEASE_OUTPUT_MISMATCH', + field: 'leaseId', + }); +} + +/** Ends the session and its lease, as a close of the leased session does. */ +function endLeasedSession(daemon: NonNullable): void { + daemon.sessionStore.retire(daemon.sessionStore.lookup(daemon.sessionName)!); + daemon.leaseRegistry.releaseLease({ ...OWNER_SCOPE, leaseId: daemon.leaseId }); + expect(daemon.leaseRegistry.listActiveLeases()).toEqual([]); +} + +async function takeLeasedScreenshot(daemon: NonNullable): Promise { + const shot = await daemon.send('screenshot', SESSION, daemon.owner); + expect(shot.result?.ok, JSON.stringify(shot)).toBe(true); + const [artifact] = shot.result!.data!.artifacts as Array<{ artifactId: string }>; + return artifact!.artifactId; +} + +test('a client with the same token and tenant cannot list or download a leased screenshot', async () => { + if (!world) return; + const artifactId = await takeLeasedScreenshot(world); + + expect(await listedIds(await world.get('/artifacts'))).toEqual([]); + expect(await listedIds(await world.get('/artifacts', OTHER_LEASE_ID))).toEqual([]); + expect(await rpcListedIds(await world.send('artifacts', 'agentB', {}))).toEqual([]); + await expectLeasedOutputRefusal(await world.get(`/artifacts/${artifactId}`)); + await expectLeasedOutputRefusal(await world.get(`/artifacts/${artifactId}`, OTHER_LEASE_ID)); + + expect(await listedIds(await world.get('/artifacts', world.leaseId))).toEqual([artifactId]); + expect(await rpcListedIds(await world.send('artifacts', SESSION, world.owner))).toEqual([ + artifactId, + ]); +}); + +test('the owner downloads a leased screenshot after its session and lease ended', async () => { + if (!world) return; + const artifactId = await takeLeasedScreenshot(world); + endLeasedSession(world); + + expect(await listedIds(await world.get('/artifacts'))).toEqual([]); + await expectLeasedOutputRefusal(await world.get(`/artifacts/${artifactId}`)); + expect(await rpcListedIds(await world.send('artifacts', SESSION, {}))).toEqual([]); + + expect(await listedIds(await world.get('/artifacts', world.leaseId.toUpperCase()))).toEqual([ + artifactId, + ]); + const download = await world.get(`/artifacts/${artifactId}`, world.leaseId); + expect(download.status).toBe(200); + const bytes = Buffer.from(await download.arrayBuffer()); + expect(bytes.subarray(0, PNG_SIGNATURE.length)).toEqual(PNG_SIGNATURE); +}); + +test('only the owner reads the diagnostics record of a request on its leased session', async () => { + if (!world) return; + const failed = await world.send('press', SESSION, world.owner); + const record = failed.error?.data?.diagnosticsRecord; + expect(record, JSON.stringify(failed)).toMatchObject({ session: world.sessionName }); + const route = `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`; + + await expectLeasedOutputRefusal(await world.get(route)); + await expectLeasedOutputRefusal(await world.get(route, OTHER_LEASE_ID)); + const owned = await world.get(route, world.leaseId); + expect(owned.status).toBe(200); + expect(await owned.text()).toContain('"phase":"request_failed"'); + + endLeasedSession(world); + + await expectLeasedOutputRefusal(await world.get(route)); + expect((await world.get(route, world.leaseId)).status).toBe(200); +}); + +test('a refused request on a leased session leaves a record with no lease', async () => { + if (!world) return; + const refused = await world.send('press', SESSION, {}); + expect(refused.error?.data?.details).toMatchObject({ reason: 'LEASE_SESSION_MISMATCH' }); + const record = refused.error?.data?.diagnosticsRecord; + expect(record, JSON.stringify(refused)).toBeDefined(); + + const own = await world.get( + `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`, + ); + + expect(own.status).toBe(200); + expect(await own.text()).not.toContain('"phase":"request_lease"'); +}); diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index 3762f2e7d1..c2cd6e3d93 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -18,6 +18,7 @@ import type { } from '@agent-device/kernel/contracts'; import type { DaemonRequest } from '../../core/daemon-request.ts'; import { + DAEMON_HTTP_LEASE_HEADER, DAEMON_HTTP_NETWORK_ACCESS_HEADER, DAEMON_HTTP_PUBLIC_NETWORK_ACCESS, } from '@agent-device/contracts/daemon-http'; @@ -287,7 +288,10 @@ test.each(['marker', 'auth-hook', 'batch'] as const)( const artifact = data?.artifacts?.[0]; expect(artifact?.artifactId).toBeTypeOf('string'); const download = await fetch(`${base}/artifacts/${artifact?.artifactId}`, { - headers: { authorization: 'Bearer test-token' }, + headers: { + authorization: 'Bearer test-token', + ...(lease ? { [DAEMON_HTTP_LEASE_HEADER]: lease.leaseId } : {}), + }, }); expect(download.status).toBe(200); const bytes = Buffer.from(await download.arrayBuffer()); @@ -400,7 +404,7 @@ test.each(['local', 'local-client-destination', 'remote'] as const)( cleanupDownloadableArtifact(artifactId!); } else { expect(artifactId).toBeUndefined(); - const inventory = await listDownloadableArtifacts(undefined, {}); + const inventory = await listDownloadableArtifacts({}, {}); expect( inventory.some( ({ filename }) => filename === 'caller.mp4' || filename === 'copied.mp4', @@ -925,7 +929,11 @@ test('remote suite artifacts belong to fresh tenant/session invocations despite path.join(sessionStore.resolveSessionDir('tenant-b:capture'), 'test-artifacts'), ); const download = await fetch(`${baseUrl}/artifacts/${second.artifactId}`, { - headers: { authorization: 'Bearer test-token', 'x-tenant': 'tenant-b' }, + headers: { + authorization: 'Bearer test-token', + 'x-tenant': 'tenant-b', + [DAEMON_HTTP_LEASE_HEADER]: leases.get('tenant-b')!.leaseId, + }, }); expect(download.status).toBe(200); const archive = gunzipSync(Buffer.from(await download.arrayBuffer())); @@ -942,7 +950,11 @@ test('remote suite artifacts belong to fresh tenant/session invocations despite expect(forbidden.status).toBe(401); for (const artifact of [first, third]) { const own = await fetch(`${baseUrl}/artifacts/${artifact.artifactId}`, { - headers: { authorization: 'Bearer test-token', 'x-tenant': 'tenant-a' }, + headers: { + authorization: 'Bearer test-token', + 'x-tenant': 'tenant-a', + [DAEMON_HTTP_LEASE_HEADER]: leases.get('tenant-a')!.leaseId, + }, }); expect(own.status).toBe(200); await own.arrayBuffer(); diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index 84540ad98d..e962421524 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -16,15 +16,19 @@ import fs from 'node:fs'; import http from 'node:http'; +import readline from 'node:readline'; import { AppError, normalizeError, type DiagnosticsRecordRef } from '@agent-device/kernel/errors'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { isSafeSessionSegment } from '@agent-device/host-kit/session-paths'; +import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { decodeUriSegment } from './http-request-target.ts'; import { isTenantAddressableSessionName, type TenantSessionNamespace, } from '../core/session-tenant-scope.ts'; import { failStreamedHttpResponse, sendRestJsonError } from './http-errors.ts'; +import { leasedOutputRefusal, requestHoldsOutputLease } from '../resources/lease-registry-scope.ts'; +import { REQUEST_LEASE_DIAGNOSTIC_PHASE } from '../sessions/lease-lifecycle.ts'; const REQUEST_DIAGNOSTICS_CONTENT_TYPE = 'application/x-ndjson'; @@ -152,7 +156,16 @@ async function handleRequestDiagnostics( return; } - const stream = fs.createReadStream(recordPath); + // The bytes that the lease check read are the bytes served, so a later stamp cannot be skipped. + const leaseIds = await readRequestDiagnosticsLeases(recordPath, stats.size); + const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; + const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; + if (!leaseIds.every((leaseId) => requestHoldsOutputLease(leaseId, presented))) { + sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); + return; + } + + const stream = fs.createReadStream(recordPath, { end: stats.size - 1 }); res.statusCode = 200; res.setHeader('content-type', REQUEST_DIAGNOSTICS_CONTENT_TYPE); res.setHeader('content-length', String(stats.size)); @@ -168,8 +181,36 @@ async function handleRequestDiagnostics( async function statRecord(recordPath: string): Promise { try { const stats = await fs.promises.stat(recordPath); - return stats.isFile() ? stats : null; + return stats.isFile() && stats.size > 0 ? stats : null; } catch { return null; } } + +/** The leases stamped in the first `byteLength` bytes of a request diagnostics record. */ +async function readRequestDiagnosticsLeases( + recordPath: string, + byteLength: number, +): Promise { + const leaseIds: string[] = []; + const lines = readline.createInterface({ + input: fs.createReadStream(recordPath, { end: byteLength - 1 }), + }); + for await (const line of lines) { + if (!line.includes(REQUEST_LEASE_DIAGNOSTIC_PHASE)) continue; + const leaseId = readStampedLease(line); + if (leaseId !== undefined) leaseIds.push(leaseId); + } + return leaseIds; +} + +function readStampedLease(line: string): string | undefined { + try { + const event = JSON.parse(line) as { phase?: unknown; data?: { leaseId?: unknown } }; + if (event.phase !== REQUEST_LEASE_DIAGNOSTIC_PHASE) return undefined; + // A stamp that cannot be read still marks the record as leased. + return typeof event.data?.leaseId === 'string' ? event.data.leaseId : ''; + } catch { + return undefined; + } +} diff --git a/packages/daemon/src/server/request-finalization.ts b/packages/daemon/src/server/request-finalization.ts index 06b515b4a8..9479a43cf9 100644 --- a/packages/daemon/src/server/request-finalization.ts +++ b/packages/daemon/src/server/request-finalization.ts @@ -109,6 +109,7 @@ function registerDownloadableArtifacts( tenantId: delivered ? delivered.tenantId : (captureOutputTenantId(data) ?? req.meta?.tenantId), + leaseId: req.internal?.admittedLease?.leaseId, artifactType: artifact.artifactType, fileName: artifact.fileName, ...(artifact.deleteAfterDownload === false ? { deleteAfterDownload: false } : {}), diff --git a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts index 60c3f0f6de..5a385a2c6b 100644 --- a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts +++ b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts @@ -12,7 +12,10 @@ import { import { createDurableResourceEnvelope } from '@agent-device/capture-kit'; import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store'; import { resolveScreenRecordingStopRecovery } from '@agent-device/capture-kit/screen-recording-stop-recovery'; -import { buildRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http'; +import { + DAEMON_HTTP_LEASE_HEADER, + buildRemoteTempArtifactPath, +} from '@agent-device/contracts/daemon-http'; import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime'; import type { ScreenRecordingCompletion, @@ -51,11 +54,13 @@ vi.mock('../../resources/artifact-tracking.ts', async (importOriginal) => { }); const TENANT = 'tenant-a'; +const LEASE_ID = 'a1b2c3d4e5f60718293a4b5c6d7e8f90'; +const OWNER = { tenantId: TENANT, leaseId: LEASE_ID }; const ADDRESS = `${TENANT}:checkout-flow`; const createdPaths: string[] = []; afterEach(async () => { - for (const { id } of await listDownloadableArtifacts(TENANT, process.env)) { + for (const { id } of await listDownloadableArtifacts(OWNER, process.env)) { cleanupDownloadableArtifact(id); } for (const filePath of createdPaths.splice(0)) fs.rmSync(filePath, { force: true }); @@ -91,7 +96,7 @@ function publishRecordingSession( ...(options.leased === false ? {} : { - lease: { leaseId: 'lease-1', tenantId: TENANT, runId: 'run-1', backend: 'ios-simulator' }, + lease: { leaseId: LEASE_ID, tenantId: TENANT, runId: 'run-1', backend: 'ios-simulator' }, }), }); const completion: ScreenRecordingCompletion = { @@ -156,14 +161,14 @@ async function teardown(sessionStore: SessionStore) { }); } -test('a leased session teardown keeps the remote recording export downloadable by its tenant', async () => { +test('a leased session teardown keeps the remote recording export downloadable by its lease', async () => { const sessionStore = makeSessionStore('agent-device-leased-recording-'); const recordingExport = remoteRecordingExport(); publishRecordingSession(sessionStore, recordingExport); await teardown(sessionStore); - const artifacts = await listDownloadableArtifacts(TENANT, process.env); + const artifacts = await listDownloadableArtifacts(OWNER, process.env); expect(artifacts).toEqual([ expect.objectContaining({ artifactType: 'screen-recording', @@ -176,16 +181,17 @@ test('a leased session teardown keeps the remote recording export downloadable b sessionName: 'checkout-flow', }), ]); - expect(await listDownloadableArtifacts('tenant-b', process.env)).toEqual([]); + expect(await listDownloadableArtifacts({ tenantId: 'tenant-b' }, process.env)).toEqual([]); + expect(await listDownloadableArtifacts({ tenantId: TENANT }, process.env)).toEqual([]); }); -test('a retained recording is deleted once its tenant downloads it', async (t) => { +test('a retained recording is deleted once its lease holder downloads it', async (t) => { if (await skipWhenLoopbackUnavailable(t)) return; const sessionStore = makeSessionStore('agent-device-leased-recording-download-'); const recordingExport = remoteRecordingExport(); publishRecordingSession(sessionStore, recordingExport); await teardown(sessionStore); - const [video] = await listDownloadableArtifacts(TENANT, process.env); + const [video] = await listDownloadableArtifacts(OWNER, process.env); const server = http.createServer((req, res) => { tryHandleDownloadableArtifactHttpRoute({ req, @@ -196,12 +202,21 @@ test('a retained recording is deleted once its tenant downloads it', async (t) = }); const port = await listenOnLoopback(server); try { - const response = await fetch(`http://127.0.0.1:${port}/artifacts/${video!.id}`); + const url = `http://127.0.0.1:${port}/artifacts/${video!.id}`; + const refused = await fetch(url); + expect(refused.status).toBe(401); + expect(await refused.json()).toMatchObject({ + code: 'UNAUTHORIZED', + reason: 'LEASE_OUTPUT_MISMATCH', + field: 'leaseId', + }); + + const response = await fetch(url, { headers: { [DAEMON_HTTP_LEASE_HEADER]: LEASE_ID } }); expect(await response.text()).toBe('video'); await vi.waitFor(() => expect(fs.existsSync(recordingExport.outPath)).toBe(false)); expect( - (await listDownloadableArtifacts(TENANT, process.env)).map((a) => a.artifactType), + (await listDownloadableArtifacts(OWNER, process.env)).map((a) => a.artifactType), ).toEqual(['screen-recording-telemetry']); } finally { await closeLoopbackServer(server); @@ -214,11 +229,11 @@ test('a retained recording nobody downloads is deleted when it expires', async ( const recordingExport = remoteRecordingExport(); publishRecordingSession(sessionStore, recordingExport); await teardown(sessionStore); - expect(await listDownloadableArtifacts(TENANT, process.env)).toHaveLength(2); + expect(await listDownloadableArtifacts(OWNER, process.env)).toHaveLength(2); await vi.advanceTimersByTimeAsync(15 * 60 * 1000); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([]); + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([]); expect(fs.existsSync(recordingExport.outPath)).toBe(false); expect(fs.existsSync(recordingExport.telemetryPath)).toBe(false); }); @@ -232,7 +247,7 @@ test('a failed teardown finish runs forced cleanup and keeps nothing for downloa await expect(teardown(sessionStore)).rejects.toThrow(/recording: .*failed to stop recording/); expect(forceCleanup).toHaveBeenCalledOnce(); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([]); + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([]); }); test('a leased teardown that cannot register the export deletes it instead of leaking it', async () => { @@ -243,7 +258,7 @@ test('a leased teardown that cannot register the export deletes it instead of le await teardown(sessionStore); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([]); + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([]); expect(fs.existsSync(recordingExport.outPath)).toBe(false); expect(fs.existsSync(recordingExport.telemetryPath)).toBe(false); }); @@ -252,7 +267,7 @@ test('a record stop replay after a leased teardown serves the artifacts teardown const sessionStore = makeSessionStore('agent-device-leased-recording-replay-'); publishRecordingSession(sessionStore, remoteRecordingExport()); await teardown(sessionStore); - const retained = await listDownloadableArtifacts(TENANT, process.env); + const retained = await listDownloadableArtifacts(OWNER, process.env); const recovery = resolveScreenRecordingStopRecovery({ sessionName: ADDRESS, @@ -276,10 +291,12 @@ test('a record stop replay after a leased teardown serves the artifacts teardown expect(replayed.ok && replayed.data?.artifacts?.map((a) => a.artifactId)).toEqual( retained.map((a) => a.id), ); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([ + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([ expect.objectContaining({ id: retained[0]!.id, sessionName: 'checkout-flow' }), expect.objectContaining({ id: retained[1]!.id, sessionName: 'checkout-flow' }), ]); + // The replay names no lease, so it registers no second way to the files. + expect(await listDownloadableArtifacts({ tenantId: TENANT }, process.env)).toEqual([]); }); test.each([undefined, '/client/local-copy.mp4'])( @@ -291,7 +308,7 @@ test.each([undefined, '/client/local-copy.mp4'])( await teardown(sessionStore); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([]); + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([]); expect(fs.readFileSync(recordingExport.outPath, 'utf8')).toBe('video'); }, ); @@ -303,8 +320,8 @@ test('an unleased session teardown registers nothing for a remote export', async await teardown(sessionStore); - expect(await listDownloadableArtifacts(TENANT, process.env)).toEqual([]); - expect(await listDownloadableArtifacts(undefined, process.env)).toEqual([]); + expect(await listDownloadableArtifacts(OWNER, process.env)).toEqual([]); + expect(await listDownloadableArtifacts({}, process.env)).toEqual([]); expect(fs.existsSync(recordingExport.outPath)).toBe(true); }); @@ -316,7 +333,7 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( }); harness.session.lease = { backend: 'ios-simulator', - leaseId: 'lease-1', + leaseId: LEASE_ID, tenantId: TENANT, runId: 'run-1', }; @@ -337,7 +354,7 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( await teardown(harness.sessionStore); - const artifacts = await listDownloadableArtifacts(TENANT, process.env); + const artifacts = await listDownloadableArtifacts(OWNER, process.env); expect(artifacts).toEqual([ expect.objectContaining({ artifactType: 'screen-recording', @@ -345,7 +362,7 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( sessionName: 'checkout-flow', }), ]); - expect(await listDownloadableArtifacts('tenant-b', process.env)).toEqual([]); + expect(await listDownloadableArtifacts({ tenantId: 'tenant-b' }, process.env)).toEqual([]); expect(fs.readFileSync(output, 'utf8')).toBe('session-owned-video'); expect( screenRecordingResourceStore.read( diff --git a/packages/daemon/src/sessions/handlers/lease.ts b/packages/daemon/src/sessions/handlers/lease.ts index 64bdc55937..0da20dad03 100644 --- a/packages/daemon/src/sessions/handlers/lease.ts +++ b/packages/daemon/src/sessions/handlers/lease.ts @@ -502,7 +502,7 @@ async function listArtifactsForRequest( ): Promise { const providerSessionId = readFlagString(req.flags, 'providerSessionId'); if (shouldListDaemonArtifacts(leaseScope, providerSessionId)) { - return await listDaemonArtifacts(leaseScope.tenantId, hostEnv); + return await listDaemonArtifacts(leaseScope, hostEnv); } return await listCloudArtifactsForRequest( @@ -522,10 +522,10 @@ function shouldListDaemonArtifacts( } async function listDaemonArtifacts( - tenantId: string | undefined, + reader: Pick, hostEnv: NodeJS.ProcessEnv, ): Promise { - const artifacts = await listDownloadableArtifacts(tenantId, hostEnv); + const artifacts = await listDownloadableArtifacts(reader, hostEnv); return { source: 'daemon', status: 'ready', diff --git a/packages/daemon/src/sessions/leased-recording-retention.ts b/packages/daemon/src/sessions/leased-recording-retention.ts index e42524f7a4..216b08c969 100644 --- a/packages/daemon/src/sessions/leased-recording-retention.ts +++ b/packages/daemon/src/sessions/leased-recording-retention.ts @@ -14,8 +14,8 @@ import { } from './handlers/record-runtime-response.ts'; /** - * Keeps the export of a recording that a leased session's teardown finished, so the tenant can - * still download it (ADR 0024 rule 1). Remote admission marks session-owned output in the durable + * Keeps the export of a recording that a leased session's teardown finished, so a request that sends + * the ended lease's id can still download it (ADR 0024 rule 1). Remote admission marks session-owned output in the durable * envelope. Remote client exports served by local HTTP use the generated daemon temp path. * A local caller's export already sits at the path that the caller named. */ @@ -37,6 +37,7 @@ export function retainLeasedSessionRecording( trackDownloadableArtifact({ artifactPath: artifact.path!, tenantId: lease.tenantId, + leaseId: lease.leaseId, artifactType: artifact.artifactType, fileName: artifact.fileName, sessionName, diff --git a/packages/proxy/src/daemon-proxy.ts b/packages/proxy/src/daemon-proxy.ts index 8857d44a12..feb3c0d7f6 100644 --- a/packages/proxy/src/daemon-proxy.ts +++ b/packages/proxy/src/daemon-proxy.ts @@ -8,6 +8,7 @@ import { DAEMON_HTTP_BASE_PATH, DAEMON_HTTP_INSTANCE_HEADER, DAEMON_HTTP_INSTANCE_MISMATCH_HEADER, + DAEMON_HTTP_LEASE_HEADER, DAEMON_HTTP_UPSTREAM_INSTANCE_HEADER, DAEMON_HTTP_NETWORK_ACCESS_HEADER, DAEMON_HTTP_PUBLIC_NETWORK_ACCESS, @@ -130,6 +131,7 @@ const FORWARDED_REQUEST_HEADERS = [ 'x-artifact-hash', 'x-artifact-hash-algorithm', DAEMON_HTTP_TENANT_HEADER, + DAEMON_HTTP_LEASE_HEADER, ]; const FORWARDED_RESPONSE_HEADERS = [ 'content-type', diff --git a/packages/remote/src/remote-connection-state.ts b/packages/remote/src/remote-connection-state.ts index be0875dbf8..d24c16d5fb 100644 --- a/packages/remote/src/remote-connection-state.ts +++ b/packages/remote/src/remote-connection-state.ts @@ -46,6 +46,8 @@ export type RemoteConnectionState = { tenant: string; runId: string; leaseId?: string; + /** The lease that a close on this connection ended. `artifacts` sends it while no lease is held. */ + endedLeaseId?: string; leaseBackend?: LeaseBackend; leaseProvider?: string; deviceKey?: string; @@ -619,6 +621,7 @@ function isRemoteConnectionState(value: unknown): value is RemoteConnectionState ]) && hasOptionalStringFields(record, [ 'leaseId', + 'endedLeaseId', 'leaseBackend', 'leaseProvider', 'deviceKey', diff --git a/src/cli/commands/connection-runtime.ts b/src/cli/commands/connection-runtime.ts index 4285f74e2c..65283aa4fc 100644 --- a/src/cli/commands/connection-runtime.ts +++ b/src/cli/commands/connection-runtime.ts @@ -145,7 +145,7 @@ export async function materializeRemoteConnectionForCommand(options: { flags: { ...nextFlags, session: nextState.session, - leaseId: nextState.leaseId, + leaseId: nextState.leaseId ?? (command === 'artifacts' ? nextState.endedLeaseId : undefined), leaseBackend: nextState.leaseBackend, platform: nextState.platform ?? nextFlags.platform, target: nextState.target ?? nextFlags.target, diff --git a/src/cli/connection/ended-lease.test.ts b/src/cli/connection/ended-lease.test.ts index 59330ea328..d4a8877714 100644 --- a/src/cli/connection/ended-lease.test.ts +++ b/src/cli/connection/ended-lease.test.ts @@ -9,6 +9,8 @@ import { type StoredConnectionSeed, } from '../../__tests__/remote-connection.fixtures.ts'; import { readRemoteConnectionState } from '@agent-device/remote/remote-connection-state'; +import { createAgentDeviceClient } from '@agent-device/client'; +import { materializeRemoteConnectionForCommand } from '../commands/connection-runtime.ts'; import { forgetEndedConnectionLease } from './ended-lease.ts'; bindTestCommandHost(); @@ -16,10 +18,10 @@ bindTestCommandHost(); const LEASE_ID = 'abc123abc123abc1'; const ENDED = [{ leaseId: LEASE_ID, outcome: 'released' }] as const; -function storedLeaseIdAfter( +function endLeaseOnConnection( response: DaemonResponse, overrides: Partial = {}, -): string | undefined { +) { const { stateDir, remoteConfigPath } = connectionWorkspace('agent-device-ended-lease-'); fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); seedConnectionState({ @@ -36,19 +38,27 @@ function storedLeaseIdAfter( ...overrides, }, }); - forgetEndedConnectionLease( - { - json: true, - help: false, - version: false, - stateDir, - remoteConfig: remoteConfigPath, - session: 'adc-proxy', - }, - response, - createTestHostEnvironment(), - ); - return readRemoteConnectionState({ stateDir, session: 'adc-proxy' })?.leaseId; + const flags = { + json: true, + help: false, + version: false, + stateDir, + remoteConfig: remoteConfigPath, + session: 'adc-proxy', + }; + forgetEndedConnectionLease(flags, response, createTestHostEnvironment()); + return { flags, state: readRemoteConnectionState({ stateDir, session: 'adc-proxy' }) }; +} + +function storedStateAfter(response: DaemonResponse, overrides: Partial = {}) { + return endLeaseOnConnection(response, overrides).state; +} + +function storedLeaseIdAfter( + response: DaemonResponse, + overrides: Partial = {}, +): string | undefined { + return storedStateAfter(response, overrides)?.leaseId; } test('an ended lease is forgotten from a reply or its error details', () => { @@ -59,7 +69,11 @@ test('an ended lease is forgotten from a reply or its error details', () => { error: { code: 'COMMAND_FAILED', message: 'failed', details: { endedLeases: [...ENDED] } }, }, ]; - for (const reply of replies) assert.equal(storedLeaseIdAfter(reply), undefined); + for (const reply of replies) { + const stored = storedStateAfter(reply); + assert.equal(stored?.leaseId, undefined); + assert.equal(stored?.endedLeaseId, LEASE_ID); + } }); test('the stored lease stays unless the reply ends that lease at response level', () => { @@ -80,8 +94,29 @@ test('an ended host-allocated lease stays stored', () => { storedLeaseIdAfter(reply, { leaseBackend: 'macos-app', platform: 'macos' }), LEASE_ID, ); - assert.equal(storedLeaseIdAfter(reply, { leaseProvider: 'limrun' }), undefined); + const limrun = storedStateAfter(reply, { leaseProvider: 'limrun' }); + assert.equal(limrun?.leaseId, undefined); + assert.equal(limrun?.endedLeaseId, undefined); for (const leaseProvider of [undefined, 'revyl', 'browserstack', 'custom-provider']) { assert.equal(storedLeaseIdAfter(reply, { leaseProvider }), LEASE_ID); } }); + +test('artifacts still sends the lease that a close ended', async () => { + const { flags } = endLeaseOnConnection({ ok: true, data: { endedLeases: [...ENDED] } }); + const host = createTestHostEnvironment(); + const client = createAgentDeviceClient( + {}, + { + host, + transport: async (request) => { + throw new Error(`unexpected daemon request: ${request.command}`); + }, + }, + ); + const leaseIdFor = async (command: string) => + (await materializeRemoteConnectionForCommand({ host, command, client, flags })).flags.leaseId; + + assert.equal(await leaseIdFor('artifacts'), LEASE_ID); + assert.equal(await leaseIdFor('session'), undefined); +}); diff --git a/src/cli/connection/ended-lease.ts b/src/cli/connection/ended-lease.ts index 87455b4f14..8450ef8ccd 100644 --- a/src/cli/connection/ended-lease.ts +++ b/src/cli/connection/ended-lease.ts @@ -12,7 +12,9 @@ import { isHostAllocatedLease } from '../commands/connection-lease-transaction.t /** * Forgets the stored connection lease once a daemon reply says its request ended that lease, so the next - * `open` allocates instead of heartbeating a lease that no longer exists. + * `open` allocates instead of heartbeating a lease that no longer exists. A proxy connection keeps + * the id as `endedLeaseId`: the daemon lists the output that it kept for an ended lease only to a + * request that sends that lease's id. */ export function forgetEndedConnectionLease( flags: CliFlags, @@ -29,10 +31,15 @@ export function forgetEndedConnectionLease( const state = readRemoteConnectionState({ stateDir, session: flags.session ?? 'default' }); if (!state?.leaseId || !endedLeaseIds.includes(state.leaseId)) return; if (isHostAllocatedLease(state)) return; - if (!connectionProviderCapabilities(state.leaseProvider, host.env).forgetsEndedConnectionLease) - return; + const capabilities = connectionProviderCapabilities(state.leaseProvider, host.env); + if (!capabilities.forgetsEndedConnectionLease) return; writeRemoteConnectionState({ stateDir, - state: { ...state, leaseId: undefined, updatedAt: new Date().toISOString() }, + state: { + ...state, + leaseId: undefined, + ...(capabilities.leaseKind === 'proxy' ? { endedLeaseId: state.leaseId } : {}), + updatedAt: new Date().toISOString(), + }, }); } diff --git a/src/commands/schema/cli-help.ts b/src/commands/schema/cli-help.ts index 0e2ef66a8a..a3471aebdd 100644 --- a/src/commands/schema/cli-help.ts +++ b/src/commands/schema/cli-help.ts @@ -204,7 +204,7 @@ Batch: agent-device test ./e2e/maestro --maestro --device udid1,emulator-5554 --shard-all 2 Recording: - record start/stop. Default scope is app (needs an active open session); use --scope device/system for whole-screen capture spanning multiple apps/home/settings. --quality medium|high on Android and Apple targets. stop burns touch overlays into the video by default; --hide-touches skips that for the fastest raw recording, and is recommended for gesture-heavy iOS simulator proof videos since overlay timing depends on a stable runner session. Android adb screenrecord has a 180s limit, so long Android recordings return as multiple MP4 chunks while the daemon stays alive; after a daemon restart, record stop recovers only manifest-owned chunks. record stop is safe to repeat: if its request window ended while the daemon was still exporting, running it again in that session returns the completed recording instead of starting a second one. If a leased remote session ends while a recording runs (lease expiry, release, or close), the daemon finishes it and keeps the video and telemetry downloadable by the tenant for 15 minutes; GET /artifacts lists them with the sessionName. + record start/stop. Default scope is app (needs an active open session); use --scope device/system for whole-screen capture spanning multiple apps/home/settings. --quality medium|high on Android and Apple targets. stop burns touch overlays into the video by default; --hide-touches skips that for the fastest raw recording, and is recommended for gesture-heavy iOS simulator proof videos since overlay timing depends on a stable runner session. Android adb screenrecord has a 180s limit, so long Android recordings return as multiple MP4 chunks while the daemon stays alive; after a daemon restart, record stop recovers only manifest-owned chunks. record stop is safe to repeat: if its request window ended while the daemon was still exporting, running it again in that session returns the completed recording instead of starting a second one. If a leased remote session ends while a recording runs (lease expiry, release, or close), the daemon finishes it and keeps the video and telemetry downloadable for 15 minutes by a request that sends the ended lease id (x-agent-device-lease-id); GET /artifacts lists them with the sessionName. record contact-sheet [--out ] reads an exported MP4 back and writes one PNG holding the frames where the screen visibly changed, each labeled with its elapsed time. It is how you read a recording you cannot play, and it needs no session or device; the frames come out of that file, so the sheet cannot describe a screen the video does not contain. The sample grid is bounded and spread over the whole clip, so it reports coverage, not a review: a flash that opens and closes between two sample times is not in it. Needs a macOS host and an MP4 (a WebM recording is refused). Tracing: trace start ./trace.log, trace stop ./trace.log (path is positional, not --path).`, }, diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index 8a567c04ce..105198ac19 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -73,20 +73,20 @@ "packages/daemon-client/src/daemon-client-transport.ts#readRemoteDaemonHealth": "sha256:df174d1ccf73851ca58bd75533fb4660424003b98bd9e41fe942cf3ab3698e5f", "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:fbb1ffa587ffb8b2975936fd35113c1970da16c58a713014df2c6ba885eecdd5", - "packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactInventory": "sha256:651806eabbd78d51d4bb21dab0b8a28847fb6087941f73dee735c24d9c2a79fa", + "packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactDownload": "sha256:67e01e9a3951728880cd864f483fe895e61b86412251e1589417455feeabafa6", + "packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactInventory": "sha256:38c9db160852bea71de8250e4c15f8d34b6fb3de2a5f59e69a91ae8885784c58", "packages/daemon/src/server/downloadable-artifact-http.ts#readArtifactId": "sha256:3472ee52d951fdf7ef00ee500298602c68f0e1e3b8c44ff51b09981efabf348c", "packages/daemon/src/server/downloadable-artifact-http.ts#readRequestPathname": "sha256:6b3a49c72365cf7da4552abef59862c073461f746025e28d409048815dd586f2", "packages/daemon/src/server/downloadable-artifact-http.ts#resolveDownloadableArtifactHttpRoute": "sha256:9fcdc9619e8d378e8f04ef7b3584863af446e855702ddce83a16ec194a6d98af", "packages/daemon/src/server/http-errors.ts#NormalizedHttpError": "sha256:2c2111802fe5f193acbeacc454de9cfab82bc8e4274b896f485e19a14f5518cf", "packages/daemon/src/server/http-errors.ts#failStreamedHttpResponse": "sha256:3e6b642cb41b88ab1e6f3269ebd14918f912cbecf09f40e9b591fdd28a2ba489", - "packages/daemon/src/server/http-errors.ts#sendRestJsonError": "sha256:aef588e77ea73eb3cf6e94ec2f2c98a434fcaaeb96de6973a43c442b704694ad", + "packages/daemon/src/server/http-errors.ts#sendRestJsonError": "sha256:226399f1ecfc9024c44198568d39970c7b9fb7ef2f6ab58afe57ed8b0d8edd27", "packages/daemon/src/server/http-errors.ts#statusCodeForNormalizedError": "sha256:39aa61eed8459527d7077e31e23be4fe0275021ed99c70ad692538091db91125", "packages/daemon/src/server/http-request-target.ts#decodeUriSegment": "sha256:b37622771ea4e61d1a9820169dbdc5809b5c38889bfad79277d7b22f3c3f1ef1", "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:cd2c0dda77d2bc434857aea07206a5491ce5f0b82cb86628569f607d6c2cebcf", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:08d3fd203cd7e47814cf8c50f710e86bee28ac2818dbf93910ae49725b97468a", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a0f21f50003b9702a6defc350066d7bf8111af5ecc55e1b5106ab2517a29a4e7", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", @@ -275,13 +275,13 @@ }, { "declaration": "packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactDownload", - "digest": "sha256:fbb1ffa587ffb8b2975936fd35113c1970da16c58a713014df2c6ba885eecdd5", - "rationale": "Implementation-only daemon changes under ADR 0006: #3753 passes the runtime host environment into archiving; #4165 uses a bounded, abortable Node pipeline and validates the declared byte length, with idempotent reader release after completion or failed setup. The handler waits for file readiness before setting headers and starting the pipeline, so open errors use the existing JSON error response. Setup errors clear artifact-only headers first. Successful status codes, headers and artifact bytes retain their existing contracts. Failed or incomplete streaming reads close the HTTP transfer instead of hanging; released clients already treat an interrupted download as failure. Routes, authorization and error JSON shapes are unchanged, so no peer must parse a new payload or send a new field. Implementation-only follow-up to #4165 under ADR 0006: response close cancels artifact preparation and streaming through an internal AbortSignal. Shared builds continue for other downloads. The existing streamed failure helper now owns the same pre-header JSON error and post-header connection destruction. Successful status, headers, bytes, routes and authorization retain their existing contracts; disconnected clients receive no new payload. A response that has finished writing is not cancelled by its normal close event, so successful pipeline completion keeps its existing success semantics. Caller cancellation is normalized to the existing request_canceled error instead of forwarding caller-supplied reasons. Artifact expiry and download deadlines keep their existing reasons. The shared HTTP disconnect helper preserves typed cancellation on aborted requests and unfinished response closes. Directory preparation counts inventory and download callers together, so one disconnected download cannot abort an inventory caller's build; successful response fields and authorization stay unchanged." + "digest": "sha256:67e01e9a3951728880cd864f483fe895e61b86412251e1589417455feeabafa6", + "rationale": "Implementation-only daemon changes under ADR 0006: #3753 passes the runtime host environment into archiving; #4165 uses a bounded, abortable Node pipeline and validates the declared byte length, with idempotent reader release after completion or failed setup. The handler waits for file readiness before setting headers and starting the pipeline, so open errors use the existing JSON error response. Setup errors clear artifact-only headers first. Successful status codes, headers and artifact bytes retain their existing contracts. Failed or incomplete streaming reads close the HTTP transfer instead of hanging; released clients already treat an interrupted download as failure. Routes, authorization and error JSON shapes are unchanged, so no peer must parse a new payload or send a new field. Implementation-only follow-up to #4165 under ADR 0006: response close cancels artifact preparation and streaming through an internal AbortSignal. Shared builds continue for other downloads. The existing streamed failure helper now owns the same pre-header JSON error and post-header connection destruction. Successful status, headers, bytes, routes and authorization retain their existing contracts; disconnected clients receive no new payload. A response that has finished writing is not cancelled by its normal close event, so successful pipeline completion keeps its existing success semantics. Caller cancellation is normalized to the existing request_canceled error instead of forwarding caller-supplied reasons. Artifact expiry and download deadlines keep their existing reasons. The shared HTTP disconnect helper preserves typed cancellation on aborted requests and unfinished response closes. Directory preparation counts inventory and download callers together, so one disconnected download cannot abort an inventory caller's build; successful response fields and authorization stay unchanged. #4912 reads the existing x-agent-device-lease-id request header. A download of an artifact that a lease produced needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients since 0.21.28 send the header from the request that produced the artifact. A caller that omits it fails clearly and misreads no payload. Unleased artifacts keep their tenant rule." }, { "declaration": "packages/daemon/src/server/downloadable-artifact-http.ts#handleArtifactInventory", - "digest": "sha256:651806eabbd78d51d4bb21dab0b8a28847fb6087941f73dee735c24d9c2a79fa", - "rationale": "#3753 threads the daemon runtime's host environment into the same archive spawn from the inventory route. The JSON body it writes and the error path are unchanged; no peer-visible field moves." + "digest": "sha256:38c9db160852bea71de8250e4c15f8d34b6fb3de2a5f59e69a91ae8885784c58", + "rationale": "#3753 threads the daemon runtime's host environment into the same archive spawn from the inventory route. The JSON body it writes and the error path are unchanged; no peer-visible field moves. #4912 reads the existing x-agent-device-lease-id request header. An artifact that a lease produced is listed only to a request that sends that lease id; other entries and the JSON body shape are unchanged. A caller that omits the header sees a shorter list and parses it as before." }, { "declaration": "packages/kernel/src/errors.ts#normalizeError", @@ -315,8 +315,8 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:08d3fd203cd7e47814cf8c50f710e86bee28ac2818dbf93910ae49725b97468a", - "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes." + "digest": "sha256:a0f21f50003b9702a6defc350066d7bf8111af5ecc55e1b5106ab2517a29a4e7", + "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that acted under a lease needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." }, { "declaration": "packages/kernel/src/contracts.ts#LEASE_BACKENDS", @@ -470,8 +470,8 @@ }, { "declaration": "packages/daemon/src/server/http-errors.ts#sendRestJsonError", - "digest": "sha256:aef588e77ea73eb3cf6e94ec2f2c98a434fcaaeb96de6973a43c442b704694ad", - "rationale": "The REST error body gains two optional fields, reason (details.reason when it is a string) and hint, beside the unchanged ok, error and code; full details are never serialized. ADR 0006 lists a new optional response field an older client can ignore as additive, and released clients read only ok, error and code. The status now comes from the details-aware mapper: A gone resource now answers 404 instead of 500: COMMAND_FAILED with details.reason RESOURCE_EXPIRED (an artifact, upload ticket, uploaded artifact or materialized path id the TTL registry no longer holds) maps to 404. The mapper also takes over the DEVICE_IN_USE human_control_active 423 rule, which statusCodeForDaemonError applied only to a failed response a /rpc handler returned. The rule now also reaches the REST routes and thrown /rpc failures, which answered 500: a PUT /admin/leases/ refused during a human-control hold, or a non-streamed /rpc call whose handler throws human_control_active, now answers 423. /admin/* is host-only on the loopback port and the proxy never forwards it, and the host already reads 423 for the same refusal on a returned /rpc failure. No released client branches on 404 versus 500 for these routes: downloadRemoteArtifactFromUrl treats any status >= 400 alike, the upload client fails any non-2xx and decides retries from a typed stream detail rather than the status, the /rpc client parses the JSON-RPC error envelope whatever the status and only special-cases 409, and the proxy passes the status through. The error code, message and JSON-RPC error data a peer parses are unchanged, so a protocol 2 peer reports the same failure it did before." + "digest": "sha256:226399f1ecfc9024c44198568d39970c7b9fb7ef2f6ab58afe57ed8b0d8edd27", + "rationale": "The REST error body gains two optional fields, reason (details.reason when it is a string) and hint, beside the unchanged ok, error and code; full details are never serialized. ADR 0006 lists a new optional response field an older client can ignore as additive, and released clients read only ok, error and code. The status now comes from the details-aware mapper: A gone resource now answers 404 instead of 500: COMMAND_FAILED with details.reason RESOURCE_EXPIRED (an artifact, upload ticket, uploaded artifact or materialized path id the TTL registry no longer holds) maps to 404. The mapper also takes over the DEVICE_IN_USE human_control_active 423 rule, which statusCodeForDaemonError applied only to a failed response a /rpc handler returned. The rule now also reaches the REST routes and thrown /rpc failures, which answered 500: a PUT /admin/leases/ refused during a human-control hold, or a non-streamed /rpc call whose handler throws human_control_active, now answers 423. /admin/* is host-only on the loopback port and the proxy never forwards it, and the host already reads 423 for the same refusal on a returned /rpc failure. No released client branches on 404 versus 500 for these routes: downloadRemoteArtifactFromUrl treats any status >= 400 alike, the upload client fails any non-2xx and decides retries from a typed stream detail rather than the status, the /rpc client parses the JSON-RPC error envelope whatever the status and only special-cases 409, and the proxy passes the status through. The error code, message and JSON-RPC error data a peer parses are unchanged, so a protocol 2 peer reports the same failure it did before. #4912 adds the optional field string beside reason, copied from the error details when the refusal names one. A released client ignores the unknown key." }, { "declaration": "packages/daemon/src/server/http-errors.ts#failStreamedHttpResponse", diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 24f802546f..fb41a7997b 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -2500,7 +2500,7 @@ Exact behavior: `agent-device help record`. - MP4 verification setup runs in the background from `record start`; capture does not wait for it. If setup exceeds the stop budget, stop returns retriable `details.reason: video_validator_compile_timeout`. Run `record stop` again; setup continues and your recording is retained. Hosts without xcrun keep container-only MP4 completion. Other compilation failures, or an unavailable validator executable or AVFoundation during verification, return `details.reason: video_validator_unavailable`. Repair the reported failure and retry `record stop`; a failed compile is retried on the next start or stop. - `record stop` is safe to repeat. If the request window ends during export, a second `record stop` in the same session returns the finished recording. A recording whose video file is gone reports `no active recording`. - `record start --scope device` on a session that does not exist yet creates one, and `record stop` ends that session. If anything else takes over the session meanwhile — an `open` or an app event that publishes an app, or an audio probe, perf capture, trace, app log, device claim, or lease it holds — `record stop` ends only the recording; the session stays and `close` releases it. -- On a remote daemon, if a leased session ends while a recording runs (lease expiry, release, or `close`), the daemon finishes the recording. It keeps the video and telemetry downloadable by the lease's tenant: `GET /artifacts` lists them with `sessionName`, and `GET /artifacts/` downloads them. Each file is deleted after its download, or after 15 minutes. +- On a remote daemon, if a leased session ends while a recording runs (lease expiry, release, or `close`), the daemon finishes the recording. It keeps the video and telemetry downloadable with the ended lease's id: `GET /artifacts` lists them with `sessionName`, and `GET /artifacts/` downloads them, when the request sends the `x-agent-device-lease-id` header. After `close`, `agent-device artifacts` on the same connection sends that id for you. Each file is deleted after its download, or after 15 minutes. - `record stop` may also report `recorder` (`confirmed` or `lost`) and `nativePathDisposition` (`retirable` or `retired`). See `agent-device help record` and [ADR 0024](https://github.com/callstack/agent-device/blob/main/docs/adr/0024-screen-recording-provable-signal.md). - `record contact-sheet [--out ]` turns an exported MP4 into one PNG grid of the frames where the screen visibly changed, each labeled with its elapsed time. It needs no session or device. The default output is `.contact-sheet.png`, and `--out` cannot point at the recording itself. `--json` adds per-cell times and change shares plus `sampledFrameCount`, `decodedFrameCount`, and `skippedSampleCount`. - A contact sheet samples at most 48 times across the clip (every 250ms for short clips) and prints at most 24 cells, so a transient between samples can be missing; `warning` says when cells were thinned or samples skipped. It runs only on macOS hosts (`details.reason: contact_sheet_unsupported_host` elsewhere) and refuses WebM with `contact_sheet_container_unsupported`. diff --git a/website/docs/docs/error-reasons.md b/website/docs/docs/error-reasons.md index 48eed42345..550225cc85 100644 --- a/website/docs/docs/error-reasons.md +++ b/website/docs/docs/error-reasons.md @@ -199,6 +199,7 @@ These reasons can come from more than one command. | `LEASE_SCOPE_REQUIRED` | The lease was allocated with an owner scope that the request does not repeat. | | `LEASE_SESSION_MISMATCH` | The lease scope of the request differs from the lease of its session; `details.field` names the field. | | `LEASE_ID_TOO_SHORT` | A lease id that the caller chose on allocate has fewer than 32 hex digits (128 bits). | +| `LEASE_OUTPUT_MISMATCH` | The request does not send the id of the lease that produced the artifact or diagnostics record. | | `LEASE_ID_IN_USE` | The requested lease id already names a lease that this daemon issued. | | `LEASE_CAPACITY_EXCEEDED` | Every iOS simulator lease slot is taken; retry after another lease is released. | | `LEASE_LEDGER_OVERWRITE` | The CLI already tracks an allocated lease and must release or transfer it before it allocates another. | diff --git a/website/docs/docs/remote-proxy.md b/website/docs/docs/remote-proxy.md index f9531e1556..4ccf1a8e89 100644 --- a/website/docs/docs/remote-proxy.md +++ b/website/docs/docs/remote-proxy.md @@ -52,7 +52,7 @@ The daemon enforces the same rule for direct RPC callers. An `agent_device.comma `--daemon-auth-token ` works instead of the environment variable, but it authenticates only the command you pass it to. Later commands need the token again through the environment variable, a `daemonAuthToken` entry in your remote config profile, or the flag on each command. -`connect proxy` stores the proxy profile and client identity. `open` leases the device for you, and the lease expires after five minutes without commands. A lease allocated directly over the RPC without `ttlMs` gets the daemon's one-minute inactivity default instead. Either window starts when allocation completes, not when it was requested. `close` releases the active session and its device lease, unless the lease was allocated with `retainOnClose`; only `leases.release`, expiry, or daemon shutdown ends that one. The reply to any request that closed a session lists the leases it ended in `endedLeases`, as `{ leaseId, outcome: 'released' }`. This covers `close`, a `batch` with a close step, and a `replay` or `test` script that closes, as well as the session cleanup `test` runs after each script. A successful reply has the field in `data`, and a failed one has it in `error.details`. A gateway in front of the daemon reads the lease end from that reply. When the reply names the stored lease, the CLI forgets it, so the next `open` leases a device again. `disconnect` clears local connection state. +`connect proxy` stores the proxy profile and client identity. `open` leases the device for you, and the lease expires after five minutes without commands. A lease allocated directly over the RPC without `ttlMs` gets the daemon's one-minute inactivity default instead. Either window starts when allocation completes, not when it was requested. `close` releases the active session and its device lease, unless the lease was allocated with `retainOnClose`; only `leases.release`, expiry, or daemon shutdown ends that one. The reply to any request that closed a session lists the leases it ended in `endedLeases`, as `{ leaseId, outcome: 'released' }`. This covers `close`, a `batch` with a close step, and a `replay` or `test` script that closes, as well as the session cleanup `test` runs after each script. A successful reply has the field in `data`, and a failed one has it in `error.details`. A gateway in front of the daemon reads the lease end from that reply. When the reply names the stored lease, the CLI forgets it, so the next `open` leases a device again. It still sends the ended lease id on `artifacts`, so files the daemon kept for that lease stay listed. `disconnect` clears local connection state. Without `--udid` or `--serial`, `open` does not pick the device on the agent machine: it sends `--platform`, `--target`, and `--device` with the lease request, the daemon behind the proxy picks a matching device, and later commands use the device the lease names. The device therefore does not need to exist before `open`, which lets a service in front of the daemon create or choose it at that point. While that lease is active, an `open` that names a different device is refused instead of running on the leased one; close the session first, or run `connect --force` to start over. An existing lease that names no device, such as one supplied through `leaseId`, is refused with reason `LEASE_SCOPE_MISMATCH` and left active. @@ -99,7 +99,11 @@ the daemon. When you specify an output path through the CLI, SDK, or MCP, it sel destination on your machine. A recording is different: it downloads to the path named by the `record stop` request, or otherwise to the working directory with a warning. The directory of a `record start` path does not carry over; the working-directory fallback keeps only its file name. Direct RPC callers receive artifact IDs and download each result with -`GET /artifacts/` using the same authentication. +`GET /artifacts/` using the same authentication. For an artifact from a leased session, also +send the lease id in the `x-agent-device-lease-id` header: without it, `GET /artifacts` does not +list the artifact and the download fails with `UNAUTHORIZED` and reason `LEASE_OUTPUT_MISMATCH`. +The same header is required to read the diagnostics record of a request on a leased session. +This holds after the session closes, for as long as the daemon keeps the file. By default, artifacts leave the inventory after download or after 15 minutes. `AGENT_DEVICE_RETAIN_ARTIFACTS` keeps downloaded artifacts in inventory until expiry. Downloads already in progress From d8ae03aab7c1d263e3ebb8c4df890012f52a8685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 09:13:34 +0100 Subject: [PATCH 02/15] fix(daemon): keep lease ids out of diagnostics records and bind recordings to their lease A request diagnostics record gets its owners before its first byte: the lease id that the request sends and the lease of the session at its address. They are kept beside the record as SHA-256 verifiers and never served. Diagnostics name a lease by a fingerprint, except in the host-only daemon log. A recording manifest keeps the verifier of the lease that started it, and record stop checks it. The request_lease line and its parser are gone. --- docs/adr/0007-remote-device-leases.md | 2 +- .../src/daemon-diagnostics-scope.ts | 17 ++-- .../docs/remote-device-lease-contracts.md | 7 ++ .../daemon/src/resources/artifact-tracking.ts | 83 +++++++++++++++++-- .../src/resources/lease-registry-scope.ts | 27 ------ .../http-server-proxy-leased-output.test.ts | 22 ++++- .../src/server/request-diagnostics-http.ts | 41 +++------ .../src/server/request-execution-scope.ts | 6 +- .../record-runtime-stop-recovery.test.ts | 44 ++++++++++ .../src/sessions/handlers/record-runtime.ts | 54 +++++++++--- packages/host-kit/src/diagnostics.ts | 1 + .../host-kit/src/internal/diagnostics.test.ts | 40 +++++++++ packages/host-kit/src/internal/diagnostics.ts | 53 +++++++++++- test/wire-compat/ledger.json | 6 +- website/docs/docs/error-reasons.md | 2 +- 15 files changed, 312 insertions(+), 93 deletions(-) diff --git a/docs/adr/0007-remote-device-leases.md b/docs/adr/0007-remote-device-leases.md index bf9e45c22a..a4f17fce97 100644 --- a/docs/adr/0007-remote-device-leases.md +++ b/docs/adr/0007-remote-device-leases.md @@ -21,7 +21,7 @@ Runner and process locks remain separate backend guards. The proxy token authenticates the host, not the client: every client of one proxy can hold the same token. The lease id is the credential that proves ownership of a leased session. A request on a session that holds a lease must send that lease's id. The daemon refuses a request that omits it with `LEASE_SESSION_MISMATCH` and never fills it from the session. -Output that a leased request produced keeps that lease as its owner: a downloadable artifact, and a request diagnostics record. A request reads such output only when it sends the lease id, for as long as the daemon keeps the output, also after the lease ends. The daemon refuses another reader with `LEASE_OUTPUT_MISMATCH`. Ownership stays with the output, not with the session, because the output can outlive its session and its lease. +Output that a leased request produced keeps that lease as its owner: a downloadable artifact, and a request diagnostics record. A request reads such output only when it sends the lease id, for as long as the daemon keeps the output, also after the lease ends. The daemon refuses another reader with `LEASE_OUTPUT_MISMATCH`. Ownership stays with the output, not with the session, because the output can outlive its session and its lease. Stored output keeps a SHA-256 of the lease id, and diagnostics name a lease by a fingerprint of it, because a served or archived record must not hold a credential. Tenant, run id, lease backend, client id, device key, and lease provider are scope labels. The daemon checks a label when the request sends it. A label never grants access. A lease id that a client chooses on allocate needs at least 128 random bits. The daemon returns a lease id only to the caller that allocated the lease. A daemon that enforces this rule reports the `session-lease-id` capability. diff --git a/packages/daemon-contracts/src/daemon-diagnostics-scope.ts b/packages/daemon-contracts/src/daemon-diagnostics-scope.ts index 8af5976315..d6ec188d77 100644 --- a/packages/daemon-contracts/src/daemon-diagnostics-scope.ts +++ b/packages/daemon-contracts/src/daemon-diagnostics-scope.ts @@ -18,11 +18,14 @@ export async function withDaemonDiagnosticsScope( body: () => Promise | T, ): Promise { const { logPath, command = 'daemon', debug = true } = options; - return await withDiagnosticsScope({ command, session: 'daemon', logPath, debug }, async () => { - try { - return await body(); - } finally { - flushDiagnosticsToSessionFile({ force: true }); - } - }); + return await withDiagnosticsScope( + { command, session: 'daemon', logPath, debug, operatorLog: true }, + async () => { + try { + return await body(); + } finally { + flushDiagnosticsToSessionFile({ force: true }); + } + }, + ); } diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 098ad81826..9a686ad9cc 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -94,6 +94,13 @@ This rule applies for as long as the daemon keeps the output, also after the lea An inventory omits an artifact of another lease. A download or a diagnostics read without the lease id fails with `UNAUTHORIZED`, reason `LEASE_OUTPUT_MISMATCH`, and `field: 'leaseId'`. If a later request registers a file that a lease already registered, the file stays with that lease. +The daemon sets the owners of a request diagnostics record before it writes the first event of that record. +The owners are the lease id that the request sends and the lease of the session at the address of the request. +A reader must send a lease id that matches each owner. Thus a request that a leased session refuses cannot read its own record. +A recording manifest keeps the lease that started the recording. `record stop` under another lease, or under no lease, fails with the same refusal. +The daemon keeps an owner as the SHA-256 of the lease id, never as the id, and never serves it. +A request diagnostics record names a lease by the first 8 hex digits of that SHA-256, never by the lease id. +The daemon log, which only the host operator reads, names the lease id, because the operator needs it to end a rented device. Output of a request that acted under no lease keeps the tenant rule. ## Admitted request work diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 8365e759ff..a01687aed7 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -3,7 +3,11 @@ import crypto from 'node:crypto'; import fs from 'node:fs'; import path from 'node:path'; import { finished } from 'node:stream/promises'; -import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors'; +import { + AppError, + createRequestCanceledError, + type ReasonTable, +} from '@agent-device/kernel/errors'; import type { DaemonArtifactType } from '@agent-device/kernel/contracts'; import type { DeviceRotation } from '@agent-device/contracts/device'; import type { DaemonArtifactInventoryEntry } from '@agent-device/contracts/observability'; @@ -13,7 +17,72 @@ import { requireTenantOwnedEntry, type TenantOwnedResourceKind, } from '../core/tenant-owned-entry.ts'; -import { leasedOutputRefusal, requestHoldsOutputLease } from './lease-registry-scope.ts'; +import { + registerDiagnosticSensitiveValue, + updateDiagnosticsScope, +} from '@agent-device/host-kit/diagnostics'; +import type { DaemonRequest } from '../core/daemon-request.ts'; +import { resolveLeaseScope } from '../core/lease-context.ts'; +import { normalizeLeaseId } from './lease-registry-scope.ts'; + +// --- Leased output ownership --- + +/** The `error.details.reason` values a leased-output refusal publishes. */ +const LEASED_OUTPUT_REASONS = { + // The request does not send the id of the lease that produced the artifact or diagnostics record. + leaseMismatch: { reason: 'LEASE_OUTPUT_MISMATCH' }, +} as const satisfies ReasonTable<'leases'>; + +/** What a record keeps to check a reader against its lease: the SHA-256 of the lease id, never the id. */ +export function leaseVerifier(leaseId: string): string { + const canonical = normalizeLeaseId(leaseId) ?? leaseId.trim(); + return crypto.createHash('sha256').update(canonical).digest('hex'); +} + +/** + * Whether a request may read output that a lease produced. Output of a leased request belongs to + * that lease for as long as the output is kept, also after the lease ends: only a request that + * sends the lease id reads it (ADR 0007). Output with no lease has no lease rule. + */ +export function requestHoldsOutputLease( + ownerVerifier: string | undefined, + presentedLeaseId: string | undefined, +): boolean { + if (ownerVerifier === undefined) return true; + return presentedLeaseId !== undefined && leaseVerifier(presentedLeaseId) === ownerVerifier; +} + +export function leasedOutputRefusal(label: string): AppError { + return new AppError('UNAUTHORIZED', `${label} belongs to a device lease. Send its lease id.`, { + reason: LEASED_OUTPUT_REASONS.leaseMismatch.reason, + field: 'leaseId', + }); +} + +/** + * Fixes who may read a request's diagnostics record before the record gets its first event: the + * lease id that the request sends, and the lease of the session at the request's address. A reader + * must send a lease id that matches each of them, so a request that a leased session refuses + * cannot read what the daemon logged while it refused. A record with no owner keeps the tenant + * rule. + */ +export function claimRequestDiagnosticsRecord( + req: Pick, + session: Readonly<{ lease?: Readonly<{ leaseId: string }> | null }> | undefined, +): void { + const leaseIds = [resolveLeaseScope(req).leaseId, session?.lease?.leaseId].filter( + (leaseId): leaseId is string => leaseId !== undefined, + ); + if (leaseIds.length === 0) return; + // A lease id that reaches a message, and not a lease field, is removed from the record too. + for (const leaseId of leaseIds) { + const canonical = normalizeLeaseId(leaseId); + if (!canonical) continue; + registerDiagnosticSensitiveValue(canonical); + registerDiagnosticSensitiveValue(leaseId.trim()); + } + updateDiagnosticsScope({ logOwners: [...new Set(leaseIds.map(leaseVerifier))] }); +} // --- Downloadable artifact tracking --- @@ -54,7 +123,7 @@ type ArtifactDownload = { type ArtifactEntry = { artifactPath: string; tenantId?: string; - leaseId?: string; + leaseVerifier?: string; artifactType: DaemonArtifactType | undefined; fileName?: string; displayRotation?: DeviceRotation; @@ -149,7 +218,7 @@ export function trackDownloadableArtifact(params: DownloadableArtifactRegistrati pendingArtifacts().set(artifactId, { artifactPath: params.artifactPath, tenantId: params.tenantId, - ...(params.leaseId !== undefined ? { leaseId: params.leaseId } : {}), + ...(params.leaseId !== undefined ? { leaseVerifier: leaseVerifier(params.leaseId) } : {}), artifactType: params.artifactType, fileName: params.fileName, ...(params.displayRotation !== undefined ? { displayRotation: params.displayRotation } : {}), @@ -168,7 +237,7 @@ function findTrackedArtifactId( ): string | undefined { for (const [artifactId, entry] of pendingArtifacts()) { if (entry.cleanupRequested || entry.artifactPath !== owner.artifactPath) continue; - if (entry.leaseId !== undefined) return artifactId; + if (entry.leaseVerifier !== undefined) return artifactId; if (entry.tenantId === owner.tenantId && owner.leaseId === undefined) return artifactId; } return undefined; @@ -302,7 +371,7 @@ function readDownloadableArtifactEntry( reader.tenantId, DOWNLOADABLE_ARTIFACT_RESOURCE, ); - if (!requestHoldsOutputLease(entry.leaseId, reader.leaseId)) { + if (!requestHoldsOutputLease(entry.leaseVerifier, reader.leaseId)) { throw leasedOutputRefusal(DOWNLOADABLE_ARTIFACT_RESOURCE.label); } if (entry.cleanupRequested) { @@ -447,7 +516,7 @@ function canReadArtifact(entry: ArtifactEntry, reader: DownloadableArtifactReade return ( !entry.cleanupRequested && (!entry.tenantId || entry.tenantId === reader.tenantId) && - requestHoldsOutputLease(entry.leaseId, reader.leaseId) + requestHoldsOutputLease(entry.leaseVerifier, reader.leaseId) ); } diff --git a/packages/daemon/src/resources/lease-registry-scope.ts b/packages/daemon/src/resources/lease-registry-scope.ts index 0fdc029a24..11221baff1 100644 --- a/packages/daemon/src/resources/lease-registry-scope.ts +++ b/packages/daemon/src/resources/lease-registry-scope.ts @@ -418,30 +418,3 @@ export function leaseReleaseRequestFor(lease: SessionLease): ReleaseLeaseRequest clientId: lease.clientId, }); } - -/** The `error.details.reason` values a leased-output refusal publishes. */ -const LEASED_OUTPUT_REASONS = { - // The request does not send the id of the lease that produced the artifact or diagnostics record. - leaseMismatch: { reason: 'LEASE_OUTPUT_MISMATCH' }, -} as const satisfies ReasonTable<'leases'>; - -/** - * Whether a request may read output that `ownerLeaseId` produced. Output of a leased request - * belongs to that lease for as long as the output is kept, also after the lease ends: only a - * request that sends the lease id reads it (ADR 0007). Output with no lease has no lease rule. - */ -export function requestHoldsOutputLease( - ownerLeaseId: string | undefined, - presentedLeaseId: string | undefined, -): boolean { - if (ownerLeaseId === undefined) return true; - const owner = normalizeLeaseId(ownerLeaseId); - return owner !== undefined && owner === normalizeLeaseId(presentedLeaseId); -} - -export function leasedOutputRefusal(label: string): AppError { - return new AppError('UNAUTHORIZED', `${label} belongs to a device lease. Send its lease id.`, { - reason: LEASED_OUTPUT_REASONS.leaseMismatch.reason, - field: 'leaseId', - }); -} diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts index 20a6d4be9f..11b39ee664 100644 --- a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -228,7 +228,10 @@ test('only the owner reads the diagnostics record of a request on its leased ses await expectLeasedOutputRefusal(await world.get(route, OTHER_LEASE_ID)); const owned = await world.get(route, world.leaseId); expect(owned.status).toBe(200); - expect(await owned.text()).toContain('"phase":"request_failed"'); + const served = await owned.text(); + expect(served).toContain('"phase":"request_failed"'); + // The record that a client downloads holds no credential to replay. + expect(served).not.toContain(world.leaseId); endLeasedSession(world); @@ -236,17 +239,30 @@ test('only the owner reads the diagnostics record of a request on its leased ses expect((await world.get(route, world.leaseId)).status).toBe(200); }); -test('a refused request on a leased session leaves a record with no lease', async () => { +test('the record of a request that a leased session refused belongs to the session lease', async () => { if (!world) return; const refused = await world.send('press', SESSION, {}); expect(refused.error?.data?.details).toMatchObject({ reason: 'LEASE_SESSION_MISMATCH' }); const record = refused.error?.data?.diagnosticsRecord; expect(record, JSON.stringify(refused)).toBeDefined(); + const route = `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`; + + // The refused client is not served what the daemon logged about the session it does not own. + await expectLeasedOutputRefusal(await world.get(route)); + await expectLeasedOutputRefusal(await world.get(route, OTHER_LEASE_ID)); + expect((await world.get(route, world.leaseId)).status).toBe(200); + expect((await world.get(`${route}.owners`)).status).toBe(404); +}); + +test('a request with no lease on a session with no lease keeps the tenant rule', async () => { + if (!world) return; + const failed = await world.send('press', 'agentB', {}); + const record = failed.error?.data?.diagnosticsRecord; + expect(record, JSON.stringify(failed)).toBeDefined(); const own = await world.get( `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`, ); expect(own.status).toBe(200); - expect(await own.text()).not.toContain('"phase":"request_lease"'); }); diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index e962421524..2e3553f73d 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -16,10 +16,10 @@ import fs from 'node:fs'; import http from 'node:http'; -import readline from 'node:readline'; import { AppError, normalizeError, type DiagnosticsRecordRef } from '@agent-device/kernel/errors'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { isSafeSessionSegment } from '@agent-device/host-kit/session-paths'; +import { diagnosticsRecordOwnersPath } from '@agent-device/host-kit/diagnostics'; import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { decodeUriSegment } from './http-request-target.ts'; import { @@ -27,8 +27,7 @@ import { type TenantSessionNamespace, } from '../core/session-tenant-scope.ts'; import { failStreamedHttpResponse, sendRestJsonError } from './http-errors.ts'; -import { leasedOutputRefusal, requestHoldsOutputLease } from '../resources/lease-registry-scope.ts'; -import { REQUEST_LEASE_DIAGNOSTIC_PHASE } from '../sessions/lease-lifecycle.ts'; +import { leasedOutputRefusal, requestHoldsOutputLease } from '../resources/artifact-tracking.ts'; const REQUEST_DIAGNOSTICS_CONTENT_TYPE = 'application/x-ndjson'; @@ -156,11 +155,11 @@ async function handleRequestDiagnostics( return; } - // The bytes that the lease check read are the bytes served, so a later stamp cannot be skipped. - const leaseIds = await readRequestDiagnosticsLeases(recordPath, stats.size); + // The owners are on disk before the first byte of the record, so they are read after the size. + const owners = await readRecordOwners(recordPath); const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; - if (!leaseIds.every((leaseId) => requestHoldsOutputLease(leaseId, presented))) { + if (!owners.every((owner) => requestHoldsOutputLease(owner, presented))) { sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); return; } @@ -187,30 +186,12 @@ async function statRecord(recordPath: string): Promise { } } -/** The leases stamped in the first `byteLength` bytes of a request diagnostics record. */ -async function readRequestDiagnosticsLeases( - recordPath: string, - byteLength: number, -): Promise { - const leaseIds: string[] = []; - const lines = readline.createInterface({ - input: fs.createReadStream(recordPath, { end: byteLength - 1 }), - }); - for await (const line of lines) { - if (!line.includes(REQUEST_LEASE_DIAGNOSTIC_PHASE)) continue; - const leaseId = readStampedLease(line); - if (leaseId !== undefined) leaseIds.push(leaseId); - } - return leaseIds; -} - -function readStampedLease(line: string): string | undefined { +async function readRecordOwners(recordPath: string): Promise { try { - const event = JSON.parse(line) as { phase?: unknown; data?: { leaseId?: unknown } }; - if (event.phase !== REQUEST_LEASE_DIAGNOSTIC_PHASE) return undefined; - // A stamp that cannot be read still marks the record as leased. - return typeof event.data?.leaseId === 'string' ? event.data.leaseId : ''; - } catch { - return undefined; + const owners = await fs.promises.readFile(diagnosticsRecordOwnersPath(recordPath), 'utf8'); + return owners.split('\n').filter((owner) => owner.length > 0); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []; + throw error; } } diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index 35e96bda36..028f297474 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -12,7 +12,10 @@ import { import type { DaemonResponse } from '@agent-device/kernel/contracts'; import type { CommandFlags } from '@agent-device/contracts/command'; import type { LeaseLifecycleProvider, ProviderAppCatalog } from '@agent-device/contracts/device'; -import type { TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; +import { + claimRequestDiagnosticsRecord, + type TrackDownloadableArtifact, +} from '../resources/artifact-tracking.ts'; import { emitDiagnostic, getDiagnosticsMeta, @@ -273,6 +276,7 @@ export async function createRequestExecutionScope(params: { logPath: requestLog.path, logRecord: requestLog.ref, }); + claimRequestDiagnosticsRecord(scopedReq, sessionStore.get(sessionName)); emitDiagnostic({ level: 'info', phase: 'request_start', diff --git a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts index 4958c2e120..c2a46a69a9 100644 --- a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts +++ b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts @@ -15,6 +15,7 @@ import { withRequestDeadline, } from '@agent-device/host-kit/request'; import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store'; +import { LeaseRegistry } from '../../../resources/lease-registry.ts'; import { expectDecodedCompletedRecording, makeRecordRuntimeHarness, @@ -105,6 +106,49 @@ test('record stop after a daemon restart replays the completed export for the na ); }); +test('record stop after a daemon restart serves the export only under the lease that started it', async () => { + const registry = new LeaseRegistry(); + const [owner, other] = ['run-a', 'run-b'].map((runId) => ({ + admittedLease: registry.allocateLease({ + tenantId: 'tenant-a', + runId, + leaseBackend: 'android-instance', + }), + })); + const harness = makeRecordRuntimeHarness('record-runtime-restart-stop-lease-'); + const outPath = path.join( + mkdtempForTestSync('record-runtime-restart-stop-lease-output-'), + 'capture.mp4', + ); + fs.writeFileSync(outPath, 'mp4'); + await harness.run(['start', outPath], undefined, undefined, owner); + await harness.run(['stop'], undefined, undefined, owner); + const ref = harness.sessionStore.lookup(harness.sessionName); + if (!ref) throw new Error('Expected the stopped recording session'); + harness.sessionStore.retire(ref); + const manifest = fs.readFileSync( + recordingResourcePath(harness.sessionStore, harness.sessionName), + 'utf8', + ); + expect(manifest).not.toContain(owner!.admittedLease.leaseId); + + for (const internal of [other, undefined]) { + expect( + await harness.run(['stop'], { sessionExplicit: true }, undefined, internal), + ).toMatchObject({ + ok: false, + error: { + code: 'UNAUTHORIZED', + details: { reason: 'LEASE_OUTPUT_MISMATCH', field: 'leaseId' }, + }, + }); + } + expect(await harness.run(['stop'], { sessionExplicit: true }, undefined, owner)).toMatchObject({ + ok: true, + data: { recording: 'stopped', outPath }, + }); +}); + test('record stop recovery after a daemon restart keeps the request host-scoping selectors', async () => { // --android-device-allowlist says WHERE to look, not which device. A recovery that dropped it // would resolve (or fail) outside the allowlist the recording was started under. diff --git a/packages/daemon/src/sessions/handlers/record-runtime.ts b/packages/daemon/src/sessions/handlers/record-runtime.ts index a839a4f2ab..dfdf181a7b 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime.ts @@ -53,6 +53,11 @@ import { requestCwd } from '../../core/context.ts'; import type { SessionRef, SessionState } from '../../core/session-state.ts'; import { sessionHasOwnersBeyondRecording } from '../../core/session-state.ts'; import { withCaptureOutputOwnership } from '../../core/capture-output-ownership.ts'; +import { + leasedOutputRefusal, + leaseVerifier, + requestHoldsOutputLease, +} from '../../resources/artifact-tracking.ts'; import { recordSessionAction } from '../../core/session-action-recorder.ts'; import { missingAppSessionResponse, @@ -276,7 +281,6 @@ async function startRecording( const started = await runtime.operations.screenRecordingStart( screenRecordingStartInput(params, session, prepared, fence, outputPaths), ); - const tenantId = session.lease?.tenantId ?? params.req.meta?.tenantId; await adoptStartedScreenRecording({ admissionLedger: params.admissionLedger, binding, @@ -284,17 +288,7 @@ async function startRecording( owner: runtime.owner, fence, ...started, - envelope: - params.req.internal?.publicNetworkOnly || tenantId - ? { - ...started.envelope, - metadata: { - ...started.envelope.metadata, - ...(params.req.internal?.publicNetworkOnly ? { remoteCaptureOutput: true } : {}), - ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), - }, - } - : started.envelope, + envelope: withCaptureOwner(started.envelope, params.req, session), throwIfCanceled: params.throwIfCanceled, }); const adoptedRef = ref ?? draft!.requireRef(); @@ -315,6 +309,26 @@ async function startRecording( ); } +/** Names in the manifest who gets the export: a remote caller, its tenant, and the lease that started it. */ +function withCaptureOwner( + envelope: DurableResourceEnvelope<'screen-recording'>, + req: DaemonRequest, + session: SessionState, +): DurableResourceEnvelope<'screen-recording'> { + const tenantId = session.lease?.tenantId ?? req.meta?.tenantId; + const leaseId = req.internal?.admittedLease?.leaseId; + if (!req.internal?.publicNetworkOnly && !tenantId && !leaseId) return envelope; + return { + ...envelope, + metadata: { + ...envelope.metadata, + ...(req.internal?.publicNetworkOnly ? { remoteCaptureOutput: true } : {}), + ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), + ...(leaseId ? { remoteCaptureLeaseVerifier: leaseVerifier(leaseId) } : {}), + }, + }; +} + function prepareRecordingStart(params: RecordRuntimeHandlerParams, session: SessionState) { const resourcePath = screenRecordingDurableResource.resourcePath( params.sessionStore, @@ -418,6 +432,7 @@ async function stopRecording( kind: 'stop-live' | 'stop-recovery', needsReadiness: boolean, ): Promise { + assertRequestHoldsRecordingLease(params); let stopped: ScreenRecordingStop; try { stopped = @@ -476,6 +491,21 @@ async function stopRecording( : response; } +/** + * The manifest names the lease that started the recording. Only a request under that lease stops + * the recording or gets its export, also when the manifest outlived its session or its daemon. + */ +function assertRequestHoldsRecordingLease(params: RecordRuntimeHandlerParams): void { + const record = screenRecordingDurableResource.store.read( + screenRecordingDurableResource.resourcePath(params.sessionStore, params.sessionName), + ); + const owner = + record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureLeaseVerifier : undefined; + if (typeof owner !== 'string') return; + if (requestHoldsOutputLease(owner, params.req.internal?.admittedLease?.leaseId)) return; + throw leasedOutputRefusal('Recording'); +} + function deleteTerminalRecordOnlySession( params: Pick, ref: SessionRef | undefined, diff --git a/packages/host-kit/src/diagnostics.ts b/packages/host-kit/src/diagnostics.ts index 4da4b7136f..4a55965fdb 100644 --- a/packages/host-kit/src/diagnostics.ts +++ b/packages/host-kit/src/diagnostics.ts @@ -2,6 +2,7 @@ export { countDiagnosticEventsByPhase, createRequestId, DIAGNOSTIC_STDERR_PREFIX, + diagnosticsRecordOwnersPath, type DiagnosticEventInput, type DiagnosticLevel, emitDiagnostic, diff --git a/packages/host-kit/src/internal/diagnostics.test.ts b/packages/host-kit/src/internal/diagnostics.test.ts index e2379e89a8..e548efd760 100644 --- a/packages/host-kit/src/internal/diagnostics.test.ts +++ b/packages/host-kit/src/internal/diagnostics.test.ts @@ -9,6 +9,7 @@ import { withDiagnosticsScope, withDiagnosticTimer, DIAGNOSTIC_STDERR_PREFIX, + diagnosticsRecordOwnersPath, } from './diagnostics.ts'; import { escapeTerminalText } from '@agent-device/kernel/redaction'; import { mkdtempForTestSync } from './tmp-dir.fixtures.ts'; @@ -155,6 +156,45 @@ test('appendDiagnosticLine ensures the log directory once across appended lines' } }); +test('a record names a lease by its fingerprint and publishes its owners before its first byte', async () => { + const logPath = path.join( + mkdtempForTestSync('agent-device-diag-owners-'), + 'requests', + 'r1.ndjson', + ); + const leaseId = 'A1B2C3D4E5F60718293A4B5C6D7E8F90'; + const ownersBeforeRecord = await withDiagnosticsScope( + { session: 'leased', requestId: 'r1', debug: true, logPath, logOwners: ['owner-a', 'owner-b'] }, + async () => { + const write = vi.spyOn(fs, 'appendFileSync'); + emitDiagnostic({ + phase: 'leased_session_ended', + data: { + leaseId, + endedLeases: [{ leaseId }], + releasedLeaseIds: [leaseId], + releaseId: 'r-1', + }, + }); + emitDiagnostic({ phase: 'request_failed' }); + const written = write.mock.calls.map(([file]) => String(file)); + write.mockRestore(); + return written; + }, + ); + + assert.deepEqual(ownersBeforeRecord, [`${logPath}.owners`, logPath, logPath]); + assert.equal(fs.readFileSync(diagnosticsRecordOwnersPath(logPath), 'utf8'), 'owner-a\nowner-b\n'); + const record = fs.readFileSync(logPath, 'utf8'); + assert.equal(record.toLowerCase().includes(leaseId.toLowerCase()), false); + assert.deepEqual(JSON.parse(record.split('\n')[0]!).data, { + leaseId: 'ff25d238', + endedLeases: [{ leaseId: 'ff25d238' }], + releasedLeaseIds: ['ff25d238'], + releaseId: 'r-1', + }); +}); + test('a later diagnostics scope recreates a removed log directory', async () => { const rootDir = mkdtempForTestSync('agent-device-diag-recreate-'); const logDir = path.join(rootDir, 'nested'); diff --git a/packages/host-kit/src/internal/diagnostics.ts b/packages/host-kit/src/internal/diagnostics.ts index a5ae2537a2..bef6e6ae53 100644 --- a/packages/host-kit/src/internal/diagnostics.ts +++ b/packages/host-kit/src/internal/diagnostics.ts @@ -40,6 +40,18 @@ type DiagnosticsScopeOptions = { * the locator cannot name different records (#1801). */ logRecord?: DiagnosticsRecordRef; + /** + * Set together with `logPath` when only some readers may be served that record. Each entry is + * one opaque owner. They are written to {@link diagnosticsRecordOwnersPath}, one per line, + * before the first byte of the record. + */ + logOwners?: readonly string[]; + /** + * Set for a log that only the host operator reads, such as the daemon log. It names a lease by + * its id, which the operator needs to end a rented device. Every other scope names a lease by + * a fingerprint. + */ + operatorLog?: boolean; traceLogPath?: string; }; @@ -48,6 +60,7 @@ type DiagnosticsScope = DiagnosticsScopeOptions & { flushedLogPath?: string; events: DiagnosticEvent[]; liveWrittenEventCount: number; + logOwnersPublished?: true; // Running per-phase emit tally. Unlike `events`, this is NOT cleared by // `flushDiagnosticsToSessionFile`, so consumers (e.g. the agent-cost graft) // can count phase occurrences for the whole request even in debug mode where @@ -313,7 +326,11 @@ function redactScopeData( ): Record { const values = sortedScopeSensitiveValues(scope); try { - return replaceSensitiveValues(redactDiagnosticData(input), values) as Record; + const redacted = redactDiagnosticData(input); + return replaceSensitiveValues( + scope.operatorLog ? redacted : fingerprintLeaseIds(redacted), + values, + ) as Record; } catch { return replaceSensitiveValues({ reason: 'diagnostic-data-unserializable' }, values) as Record< string, @@ -342,6 +359,30 @@ function replaceSensitiveValues(value: unknown, sensitiveValues: readonly string ); } +// `leaseId`, `releasedLeaseIds`, `lease_id`; never `releaseId`. +const LEASE_ID_KEY_RE = /(?:^l|L|[_-]l)ease[_-]?[iI]ds?$/; + +/** + * A lease id is the credential for its session and its output, and a request record can reach + * another reader. Thus diagnostics name a lease by a fingerprint: the first 8 hex digits of the + * SHA-256 of the id. + */ +function fingerprintLeaseIds(value: unknown, namesLease = false): unknown { + if (typeof value === 'string') { + if (!namesLease) return value; + const digest = crypto.createHash('sha256').update(value.trim().toLowerCase()).digest('hex'); + return digest.slice(0, 8); + } + if (Array.isArray(value)) return value.map((entry) => fingerprintLeaseIds(entry, namesLease)); + if (!value || typeof value !== 'object') return value; + return Object.fromEntries( + Object.entries(value).map(([key, entry]) => [ + key, + fingerprintLeaseIds(entry, LEASE_ID_KEY_RE.test(key)), + ]), + ); +} + function sanitizePathPart(value: string): string { return value.replaceAll(/[^a-zA-Z0-9._-]/g, '_'); } @@ -352,5 +393,15 @@ function appendDiagnosticLine(scope: DiagnosticsScope, logPath: string, line: st fs.mkdirSync(dir, { recursive: true }); scope.ensuredDirectories.add(dir); } + if (logPath === scope.logPath && scope.logOwners?.length && !scope.logOwnersPublished) { + const owners = scope.logOwners.map((owner) => `${owner}\n`).join(''); + fs.appendFileSync(diagnosticsRecordOwnersPath(logPath), owners, 'utf8'); + scope.logOwnersPublished = true; + } fs.appendFileSync(logPath, line, 'utf8'); } + +/** Where the owners of the record at `logPath` are kept. No reader is served this file. */ +export function diagnosticsRecordOwnersPath(logPath: string): string { + return `${logPath}.owners`; +} diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index 105198ac19..eaed40dde9 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -86,7 +86,7 @@ "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:cd2c0dda77d2bc434857aea07206a5491ce5f0b82cb86628569f607d6c2cebcf", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a0f21f50003b9702a6defc350066d7bf8111af5ecc55e1b5106ab2517a29a4e7", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a6c3bebf32340694f774872bbaea1becbd9fa8294a6895b9f5fa36e25ac368b4", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", @@ -315,8 +315,8 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:a0f21f50003b9702a6defc350066d7bf8111af5ecc55e1b5106ab2517a29a4e7", - "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that acted under a lease needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." + "digest": "sha256:a6c3bebf32340694f774872bbaea1becbd9fa8294a6895b9f5fa36e25ac368b4", + "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that sent a lease id, or that ran at the address of a leased session, needs each of those lease ids, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. The owners are read from a file beside the record that no route serves. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." }, { "declaration": "packages/kernel/src/contracts.ts#LEASE_BACKENDS", diff --git a/website/docs/docs/error-reasons.md b/website/docs/docs/error-reasons.md index 550225cc85..116f0472af 100644 --- a/website/docs/docs/error-reasons.md +++ b/website/docs/docs/error-reasons.md @@ -198,8 +198,8 @@ These reasons can come from more than one command. | `LEASE_SCOPE_MISMATCH` | The lease does not match the tenant, run, backend, provider, device, or client of the request. | | `LEASE_SCOPE_REQUIRED` | The lease was allocated with an owner scope that the request does not repeat. | | `LEASE_SESSION_MISMATCH` | The lease scope of the request differs from the lease of its session; `details.field` names the field. | -| `LEASE_ID_TOO_SHORT` | A lease id that the caller chose on allocate has fewer than 32 hex digits (128 bits). | | `LEASE_OUTPUT_MISMATCH` | The request does not send the id of the lease that produced the artifact or diagnostics record. | +| `LEASE_ID_TOO_SHORT` | A lease id that the caller chose on allocate has fewer than 32 hex digits (128 bits). | | `LEASE_ID_IN_USE` | The requested lease id already names a lease that this daemon issued. | | `LEASE_CAPACITY_EXCEEDED` | Every iOS simulator lease slot is taken; retry after another lease is released. | | `LEASE_LEDGER_OVERWRITE` | The CLI already tracks an allocated lease and must release or transfer it before it allocates another. | From e59edb46a277e6924eaa8b4f072109e9591fa941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 09:44:16 +0100 Subject: [PATCH 03/15] fix(daemon): make the lease id a request sent the only owner of its diagnostics record The lease of the session at the request's address is no longer an owner. A request without the session lease id never acts on a leased session, and a record never holds a raw lease id, so each caller reads only its own records. The record claim moves to the request router; request-execution-scope.ts is unchanged from the base. --- .../docs/remote-device-lease-contracts.md | 6 ++--- .../daemon/src/resources/artifact-tracking.ts | 26 +++++++------------ .../http-server-proxy-leased-output.test.ts | 16 +++++++----- .../src/server/request-execution-scope.ts | 6 +---- packages/daemon/src/server/request-router.ts | 6 ++++- .../host-kit/src/internal/diagnostics.test.ts | 13 +++++++--- packages/host-kit/src/internal/diagnostics.ts | 21 ++++++++------- test/wire-compat/ledger.json | 2 +- 8 files changed, 50 insertions(+), 46 deletions(-) diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 9a686ad9cc..22178a1f6c 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -94,9 +94,9 @@ This rule applies for as long as the daemon keeps the output, also after the lea An inventory omits an artifact of another lease. A download or a diagnostics read without the lease id fails with `UNAUTHORIZED`, reason `LEASE_OUTPUT_MISMATCH`, and `field: 'leaseId'`. If a later request registers a file that a lease already registered, the file stays with that lease. -The daemon sets the owners of a request diagnostics record before it writes the first event of that record. -The owners are the lease id that the request sends and the lease of the session at the address of the request. -A reader must send a lease id that matches each owner. Thus a request that a leased session refuses cannot read its own record. +The owner of a request diagnostics record is the lease id that the request itself sent. A request that sent no lease id leaves a record with no owner. +The daemon sets the owner before it writes the first event of that record. +Thus each caller reads only its own records, also the record of a request that a leased session refused. A recording manifest keeps the lease that started the recording. `record stop` under another lease, or under no lease, fails with the same refusal. The daemon keeps an owner as the SHA-256 of the lease id, never as the id, and never serves it. A request diagnostics record names a lease by the first 8 hex digits of that SHA-256, never by the lease id. diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index a01687aed7..483ebec2e6 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -60,28 +60,20 @@ export function leasedOutputRefusal(label: string): AppError { } /** - * Fixes who may read a request's diagnostics record before the record gets its first event: the - * lease id that the request sends, and the lease of the session at the request's address. A reader - * must send a lease id that matches each of them, so a request that a leased session refuses - * cannot read what the daemon logged while it refused. A record with no owner keeps the tenant - * rule. + * Makes the lease id that a request sends the owner of that request's diagnostics record. Only a + * reader that sends the same lease id is served the record. A request that sends no lease id + * leaves a record with no owner, which keeps the tenant rule. */ -export function claimRequestDiagnosticsRecord( - req: Pick, - session: Readonly<{ lease?: Readonly<{ leaseId: string }> | null }> | undefined, -): void { - const leaseIds = [resolveLeaseScope(req).leaseId, session?.lease?.leaseId].filter( - (leaseId): leaseId is string => leaseId !== undefined, - ); - if (leaseIds.length === 0) return; +export function claimRequestDiagnosticsRecord(req: Pick): void { + const leaseId = resolveLeaseScope(req).leaseId; + if (leaseId === undefined) return; // A lease id that reaches a message, and not a lease field, is removed from the record too. - for (const leaseId of leaseIds) { - const canonical = normalizeLeaseId(leaseId); - if (!canonical) continue; + const canonical = normalizeLeaseId(leaseId); + if (canonical) { registerDiagnosticSensitiveValue(canonical); registerDiagnosticSensitiveValue(leaseId.trim()); } - updateDiagnosticsScope({ logOwners: [...new Set(leaseIds.map(leaseVerifier))] }); + updateDiagnosticsScope({ logOwner: leaseVerifier(leaseId) }); } // --- Downloadable artifact tracking --- diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts index 11b39ee664..1cd775fa33 100644 --- a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -239,19 +239,23 @@ test('only the owner reads the diagnostics record of a request on its leased ses expect((await world.get(route, world.leaseId)).status).toBe(200); }); -test('the record of a request that a leased session refused belongs to the session lease', async () => { +test('a request that a leased session refused reads its own record, which names no lease id', async () => { if (!world) return; - const refused = await world.send('press', SESSION, {}); + const refused = await world.send('press', SESSION, { leaseId: OTHER_LEASE_ID }); expect(refused.error?.data?.details).toMatchObject({ reason: 'LEASE_SESSION_MISMATCH' }); const record = refused.error?.data?.diagnosticsRecord; expect(record, JSON.stringify(refused)).toBeDefined(); const route = `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`; - // The refused client is not served what the daemon logged about the session it does not own. + // The owner of a record is the lease id that its request sent, not the lease of the session. await expectLeasedOutputRefusal(await world.get(route)); - await expectLeasedOutputRefusal(await world.get(route, OTHER_LEASE_ID)); - expect((await world.get(route, world.leaseId)).status).toBe(200); - expect((await world.get(`${route}.owners`)).status).toBe(404); + await expectLeasedOutputRefusal(await world.get(route, world.leaseId)); + const own = await world.get(route, OTHER_LEASE_ID); + expect(own.status).toBe(200); + const served = await own.text(); + expect(served).not.toContain(world.leaseId); + expect(served).not.toContain(OTHER_LEASE_ID); + expect((await world.get(`${route}.owners`, OTHER_LEASE_ID)).status).toBe(404); }); test('a request with no lease on a session with no lease keeps the tenant rule', async () => { diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index 028f297474..35e96bda36 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -12,10 +12,7 @@ import { import type { DaemonResponse } from '@agent-device/kernel/contracts'; import type { CommandFlags } from '@agent-device/contracts/command'; import type { LeaseLifecycleProvider, ProviderAppCatalog } from '@agent-device/contracts/device'; -import { - claimRequestDiagnosticsRecord, - type TrackDownloadableArtifact, -} from '../resources/artifact-tracking.ts'; +import type { TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; import { emitDiagnostic, getDiagnosticsMeta, @@ -276,7 +273,6 @@ export async function createRequestExecutionScope(params: { logPath: requestLog.path, logRecord: requestLog.ref, }); - claimRequestDiagnosticsRecord(scopedReq, sessionStore.get(sessionName)); emitDiagnostic({ level: 'info', phase: 'request_start', diff --git a/packages/daemon/src/server/request-router.ts b/packages/daemon/src/server/request-router.ts index 31e9566d09..100b99b3fe 100644 --- a/packages/daemon/src/server/request-router.ts +++ b/packages/daemon/src/server/request-router.ts @@ -28,7 +28,10 @@ import { } from '@agent-device/kernel/contracts'; import { namesSessionExplicitly } from '@agent-device/kernel/record'; import type { CloudArtifactProvider } from '@agent-device/contracts/observability'; -import type { TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; +import { + claimRequestDiagnosticsRecord, + type TrackDownloadableArtifact, +} from '../resources/artifact-tracking.ts'; import type { RequestPlatformProviderScope, RequestPlatformProviders, @@ -233,6 +236,7 @@ export function createRequestHandler(deps: RequestRouterDeps): DaemonInvokeFn { logPath, }, async () => { + claimRequestDiagnosticsRecord(req); const run = async () => await withCommandHost( { host, requestEnv: requestCommandEnv(req) }, diff --git a/packages/host-kit/src/internal/diagnostics.test.ts b/packages/host-kit/src/internal/diagnostics.test.ts index e548efd760..cff152e49d 100644 --- a/packages/host-kit/src/internal/diagnostics.test.ts +++ b/packages/host-kit/src/internal/diagnostics.test.ts @@ -156,7 +156,7 @@ test('appendDiagnosticLine ensures the log directory once across appended lines' } }); -test('a record names a lease by its fingerprint and publishes its owners before its first byte', async () => { +test('a record names a lease by its fingerprint and publishes its owner before its first byte', async () => { const logPath = path.join( mkdtempForTestSync('agent-device-diag-owners-'), 'requests', @@ -164,7 +164,14 @@ test('a record names a lease by its fingerprint and publishes its owners before ); const leaseId = 'A1B2C3D4E5F60718293A4B5C6D7E8F90'; const ownersBeforeRecord = await withDiagnosticsScope( - { session: 'leased', requestId: 'r1', debug: true, logPath, logOwners: ['owner-a', 'owner-b'] }, + { + session: 'leased', + requestId: 'r1', + debug: true, + logPath, + logRecord: { session: 'leased', requestId: 'r1' }, + logOwner: 'owner-a', + }, async () => { const write = vi.spyOn(fs, 'appendFileSync'); emitDiagnostic({ @@ -184,7 +191,7 @@ test('a record names a lease by its fingerprint and publishes its owners before ); assert.deepEqual(ownersBeforeRecord, [`${logPath}.owners`, logPath, logPath]); - assert.equal(fs.readFileSync(diagnosticsRecordOwnersPath(logPath), 'utf8'), 'owner-a\nowner-b\n'); + assert.equal(fs.readFileSync(diagnosticsRecordOwnersPath(logPath), 'utf8'), 'owner-a\n'); const record = fs.readFileSync(logPath, 'utf8'); assert.equal(record.toLowerCase().includes(leaseId.toLowerCase()), false); assert.deepEqual(JSON.parse(record.split('\n')[0]!).data, { diff --git a/packages/host-kit/src/internal/diagnostics.ts b/packages/host-kit/src/internal/diagnostics.ts index bef6e6ae53..6cb6004f2d 100644 --- a/packages/host-kit/src/internal/diagnostics.ts +++ b/packages/host-kit/src/internal/diagnostics.ts @@ -41,11 +41,10 @@ type DiagnosticsScopeOptions = { */ logRecord?: DiagnosticsRecordRef; /** - * Set together with `logPath` when only some readers may be served that record. Each entry is - * one opaque owner. They are written to {@link diagnosticsRecordOwnersPath}, one per line, - * before the first byte of the record. + * The one opaque owner of what this scope writes to its `logRecord`. It is written as a line of + * {@link diagnosticsRecordOwnersPath} before the first byte that this scope adds to the record. */ - logOwners?: readonly string[]; + logOwner?: string; /** * Set for a log that only the host operator reads, such as the daemon log. It names a lease by * its id, which the operator needs to end a rented device. Every other scope names a lease by @@ -60,7 +59,7 @@ type DiagnosticsScope = DiagnosticsScopeOptions & { flushedLogPath?: string; events: DiagnosticEvent[]; liveWrittenEventCount: number; - logOwnersPublished?: true; + logOwnerPublished?: true; // Running per-phase emit tally. Unlike `events`, this is NOT cleared by // `flushDiagnosticsToSessionFile`, so consumers (e.g. the agent-cost graft) // can count phase occurrences for the whole request even in debug mode where @@ -393,15 +392,17 @@ function appendDiagnosticLine(scope: DiagnosticsScope, logPath: string, line: st fs.mkdirSync(dir, { recursive: true }); scope.ensuredDirectories.add(dir); } - if (logPath === scope.logPath && scope.logOwners?.length && !scope.logOwnersPublished) { - const owners = scope.logOwners.map((owner) => `${owner}\n`).join(''); - fs.appendFileSync(diagnosticsRecordOwnersPath(logPath), owners, 'utf8'); - scope.logOwnersPublished = true; + if (scope.logRecord && logPath === scope.logPath && scope.logOwner && !scope.logOwnerPublished) { + fs.appendFileSync(diagnosticsRecordOwnersPath(logPath), `${scope.logOwner}\n`, 'utf8'); + scope.logOwnerPublished = true; } fs.appendFileSync(logPath, line, 'utf8'); } -/** Where the owners of the record at `logPath` are kept. No reader is served this file. */ +/** + * Where the owners of the record at `logPath` are kept, one line for each scope that wrote to the + * record with an owner. No reader is served this file. + */ export function diagnosticsRecordOwnersPath(logPath: string): string { return `${logPath}.owners`; } diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index eaed40dde9..0f2754f29d 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -316,7 +316,7 @@ { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", "digest": "sha256:a6c3bebf32340694f774872bbaea1becbd9fa8294a6895b9f5fa36e25ac368b4", - "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that sent a lease id, or that ran at the address of a leased session, needs each of those lease ids, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. The owners are read from a file beside the record that no route serves. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." + "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that sent a lease id needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. The owner is read from a file beside the record that no route serves. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." }, { "declaration": "packages/kernel/src/contracts.ts#LEASE_BACKENDS", From d64fd5f7c3fa845a67f4669551e7027e9498cc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 10:23:12 +0100 Subject: [PATCH 04/15] fix(daemon): bind each output to its own record and its lease - An artifact id serves only the output that it registered. Another capture written to the same path gets its own id, and the earlier id expires. - The daemon names each request diagnostics record. The name carries the owner, so a record has one owner or none, and the owners file is gone. The caller's request id names no record. A record that an earlier release named is not served. - A recording manifest names its lease, or that it has none. A completed export replays to a request that sends that lease id, with no active lease, and stays with that lease when it is registered. A remote export of unknown ownership is not replayed. --- .../src/daemon-command-descriptor.ts | 5 +- .../src/descriptors/device-operations.ts | 4 + .../docs/remote-device-lease-contracts.md | 18 +-- .../src/core/capture-output-ownership.ts | 10 +- .../src/resources/artifact-tracking.test.ts | 41 +++++++ .../daemon/src/resources/artifact-tracking.ts | 113 ++++++++++++++---- .../http-server-tenant-trust.test.ts | 30 ++--- .../request-diagnostics-http.test.ts | 58 +++++---- ...router-android-foreground-evidence.test.ts | 4 +- .../request-router-error-redaction.test.ts | 2 +- .../request-router-fill-text-stdin.test.ts | 4 +- .../__tests__/request-router-open.test.ts | 7 +- .../http-server-proxy-leased-output.test.ts | 23 +++- .../src/server/request-diagnostics-http.ts | 41 +++---- .../src/server/request-execution-scope.ts | 8 +- .../daemon/src/server/request-finalization.ts | 8 +- .../leased-recording-retention.test.ts | 12 +- .../__tests__/request-admission.test.ts | 22 ++++ .../record-runtime-stop-recovery.test.ts | 85 ++++++++----- .../src/sessions/handlers/record-runtime.ts | 41 +++++-- .../sessions/leased-recording-retention.ts | 3 +- .../daemon/src/sessions/request-admission.ts | 14 ++- .../internal/session-open-execution.ts | 5 +- packages/host-kit/src/diagnostics.ts | 1 - .../host-kit/src/internal/diagnostics.test.ts | 34 ++---- packages/host-kit/src/internal/diagnostics.ts | 26 ++-- src/__tests__/daemon-client-abort.test.ts | 16 ++- .../schema/cli-help-workflows.test.ts | 2 +- src/commands/schema/cli-help-workflows.ts | 2 +- .../active-session-script-publication.test.ts | 11 +- .../remote-proxy-request-diagnostics.test.ts | 10 +- test/wire-compat/ledger.json | 6 +- website/docs/docs/debugging-profiling.md | 2 +- website/docs/docs/sessions.md | 2 +- 34 files changed, 448 insertions(+), 222 deletions(-) diff --git a/packages/command-registry/src/daemon-command-descriptor.ts b/packages/command-registry/src/daemon-command-descriptor.ts index 7d0fee3768..7f8e5a2d33 100644 --- a/packages/command-registry/src/daemon-command-descriptor.ts +++ b/packages/command-registry/src/daemon-command-descriptor.ts @@ -25,7 +25,10 @@ export type DaemonRefFrameEffect = export type SessionlessLeaseAdmissionExemption = | Readonly<{ kind: 'unconditional' }> - | Readonly<{ kind: 'provider-app-catalog'; provider: string }>; + | Readonly<{ kind: 'provider-app-catalog'; provider: string }> + // The request names a lease that ended, to read output that the lease left. Its handler checks + // that lease id against the owner of the output. + | Readonly<{ kind: 'ended-lease-output' }>; /** * Daemon route + request-policy traits for one command. Generic over the request diff --git a/packages/command-registry/src/descriptors/device-operations.ts b/packages/command-registry/src/descriptors/device-operations.ts index 6c0a9d7425..9856867206 100644 --- a/packages/command-registry/src/descriptors/device-operations.ts +++ b/packages/command-registry/src/descriptors/device-operations.ts @@ -182,6 +182,10 @@ export const DEVICE_OPERATION_COMMAND_DESCRIPTORS = { refFrameEffect: 'preserve', allowInvalidRecording: true, allowSessionlessDefaultDevice: isRecordingStartRequest, + sessionlessLeaseAdmissionExemption: (req) => + req.positionals?.[0]?.toLowerCase() === 'stop' + ? { kind: 'ended-lease-output' } + : undefined, }, platformExecution: { kind: 'device-runtime', uses: screenRecordingRuntimePlanUses }, // A `record stop` export can outlast the request envelope. Resetting the daemon mid-export diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 22178a1f6c..7f625594a1 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -93,14 +93,18 @@ The `artifacts` RPC reads the lease id from the request. `GET /artifacts`, `GET This rule applies for as long as the daemon keeps the output, also after the lease ends. The lifetime of the output does not change. An inventory omits an artifact of another lease. A download or a diagnostics read without the lease id fails with `UNAUTHORIZED`, reason `LEASE_OUTPUT_MISMATCH`, and `field: 'leaseId'`. -If a later request registers a file that a lease already registered, the file stays with that lease. -The owner of a request diagnostics record is the lease id that the request itself sent. A request that sent no lease id leaves a record with no owner. -The daemon sets the owner before it writes the first event of that record. -Thus each caller reads only its own records, also the record of a request that a leased session refused. -A recording manifest keeps the lease that started the recording. `record stop` under another lease, or under no lease, fails with the same refusal. -The daemon keeps an owner as the SHA-256 of the lease id, never as the id, and never serves it. +An artifact id serves only the output that it registered. If another capture is written to the same path, that capture gets its own id, and the earlier id expires. +The daemon names each request diagnostics record. The name carries the owner of the record: the lease id that the request sent, or that it sent none. +The request id of the caller names no record. Thus two requests do not share a record, and a record has one owner or none. +A caller that sent a lease id reads only its own records, also the record of a request that a leased session refused. +The diagnostics route does not serve a record that an earlier release named, because the owner of that record is not known. +A recording manifest keeps the lease that started the recording, or that no lease started it. +`record stop` returns a completed export to a request that sends that lease id, also after the lease ended. Such a request needs no active lease. +A recording that still runs needs a request that is admitted under that lease. +`record stop` does not return a completed remote export whose manifest names neither. +The daemon keeps an owner as the SHA-256 of the lease id, never as the id. A request diagnostics record names a lease by the first 8 hex digits of that SHA-256, never by the lease id. -The daemon log, which only the host operator reads, names the lease id, because the operator needs it to end a rented device. +An event that the daemon writes outside a request goes to the daemon log, which only the host operator reads. It names the lease id, because the operator needs it to end a rented device. Output of a request that acted under no lease keeps the tenant rule. ## Admitted request work diff --git a/packages/daemon/src/core/capture-output-ownership.ts b/packages/daemon/src/core/capture-output-ownership.ts index 7f51ff9071..7b03ab970b 100644 --- a/packages/daemon/src/core/capture-output-ownership.ts +++ b/packages/daemon/src/core/capture-output-ownership.ts @@ -5,6 +5,8 @@ type CaptureOutputOwnership = 'daemon' | 'caller'; type CaptureOutputOwner = Readonly<{ ownership: CaptureOutputOwnership; tenantId?: string; + /** The verifier of the lease that produced the capture, when it is not the lease of this request. */ + leaseVerifier?: string; }>; type CaptureOutputData = DaemonResponseData & { [CAPTURE_OUTPUT_OWNERSHIP]?: CaptureOutputOwner; @@ -15,8 +17,9 @@ export function withCaptureOutputOwnership( data: DaemonResponseData, ownership: CaptureOutputOwnership, tenantId?: string, + leaseVerifier?: string, ): DaemonResponseData { - return { ...data, [CAPTURE_OUTPUT_OWNERSHIP]: { ownership, tenantId } }; + return { ...data, [CAPTURE_OUTPUT_OWNERSHIP]: { ownership, tenantId, leaseVerifier } }; } export function captureOutputOwnership( @@ -31,6 +34,11 @@ export function captureOutputTenantId(data: DaemonResponseData): string | undefi return capture[CAPTURE_OUTPUT_OWNERSHIP]?.tenantId; } +export function captureOutputLeaseVerifier(data: DaemonResponseData): string | undefined { + const capture: CaptureOutputData = data; + return capture[CAPTURE_OUTPUT_OWNERSHIP]?.leaseVerifier; +} + const DAEMON_DELIVERED_ARTIFACT = Symbol('daemonDeliveredArtifact'); type DeliveredArtifact = DaemonArtifact & { [DAEMON_DELIVERED_ARTIFACT]?: Readonly<{ tenantId: string | undefined }>; diff --git a/packages/daemon/src/resources/artifact-tracking.test.ts b/packages/daemon/src/resources/artifact-tracking.test.ts index 638ae53b03..f941a64a16 100644 --- a/packages/daemon/src/resources/artifact-tracking.test.ts +++ b/packages/daemon/src/resources/artifact-tracking.test.ts @@ -10,6 +10,7 @@ import { createRuntimeStateHandle } from '@agent-device/host-kit/runtime-slots'; import * as command from '@agent-device/host-kit/command'; import { cleanupDownloadableArtifact, + leaseVerifier, listDownloadableArtifacts, openDownloadableArtifact, trackDownloadableArtifact, @@ -628,6 +629,46 @@ test('a file tracked twice for one tenant keeps one id, and another tenant gets expect(otherTenant).not.toBe(first); }); +test.each([ + { first: undefined, second: 'a'.repeat(32) }, + { first: 'a'.repeat(32), second: 'b'.repeat(32) }, +])( + 'an id does not serve a later capture written to its path (first lease $first)', + async ({ first, second }) => { + const artifactPath = path.join(mkdtempForTestSync('agent-device-artifact-path-'), 'shot.png'); + const runtime = createRuntimeStateHandle(); + const capture = (bytes: string, leaseId: string | undefined) => { + fs.writeFileSync(artifactPath, bytes); + return runtime.run(() => + trackDownloadableArtifact({ + artifactPath, + tenantId: 'tenant-a', + artifactType: 'screenshot', + ...(leaseId ? { leaseVerifier: leaseVerifier(leaseId) } : {}), + }), + ); + }; + const reader = (leaseId: string | undefined) => ({ tenantId: 'tenant-a', leaseId }); + const firstId = capture('first capture', first); + const secondId = capture('the second capture', second); + + expect(secondId).not.toBe(firstId); + // The first id named the first capture, which is gone. It never opens the second capture. + await expect( + runtime.run(() => openDownloadableArtifact(firstId, reader(first), process.env)), + ).rejects.toMatchObject({ details: { reason: 'RESOURCE_EXPIRED' } }); + expect(await runtime.run(() => listDownloadableArtifacts(reader(first), process.env))).toEqual( + [], + ); + const download = await runtime.run(() => + openDownloadableArtifact(secondId, reader(second), process.env), + ); + expect((await buffer(download.stream)).toString()).toBe('the second capture'); + download.release(); + runtime.dispose(); + }, +); + test.each( (['old-first', 'new-first'] as const).flatMap((order) => [true, false].map((deleteAfterDownload) => ({ order, deleteAfterDownload })), diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 483ebec2e6..8654bbf77e 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -18,9 +18,11 @@ import { type TenantOwnedResourceKind, } from '../core/tenant-owned-entry.ts'; import { + getDiagnosticsMeta, registerDiagnosticSensitiveValue, updateDiagnosticsScope, } from '@agent-device/host-kit/diagnostics'; +import { timingSafeStringEqual } from '@agent-device/host-kit/transport'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { resolveLeaseScope } from '../core/lease-context.ts'; import { normalizeLeaseId } from './lease-registry-scope.ts'; @@ -49,7 +51,10 @@ export function requestHoldsOutputLease( presentedLeaseId: string | undefined, ): boolean { if (ownerVerifier === undefined) return true; - return presentedLeaseId !== undefined && leaseVerifier(presentedLeaseId) === ownerVerifier; + return ( + presentedLeaseId !== undefined && + timingSafeStringEqual(leaseVerifier(presentedLeaseId), ownerVerifier) + ); } export function leasedOutputRefusal(label: string): AppError { @@ -59,21 +64,42 @@ export function leasedOutputRefusal(label: string): AppError { }); } +const UNLEASED_RECORD = 'unleased'; +const DIAGNOSTICS_RECORD_ID = new RegExp(`^[a-f0-9]{32}-(${UNLEASED_RECORD}|[a-f0-9]{64})$`); + /** - * Makes the lease id that a request sends the owner of that request's diagnostics record. Only a - * reader that sends the same lease id is served the record. A request that sends no lease id - * leaves a record with no owner, which keeps the tenant rule. + * Names the diagnostics record of a request that no other request opened: a server-chosen id that + * carries its owner, the lease id that the request sent, or that it sent none. The caller's request + * id names no record, so two requests never share one, and a record has one owner or none. */ export function claimRequestDiagnosticsRecord(req: Pick): void { const leaseId = resolveLeaseScope(req).leaseId; - if (leaseId === undefined) return; // A lease id that reaches a message, and not a lease field, is removed from the record too. const canonical = normalizeLeaseId(leaseId); - if (canonical) { + if (leaseId !== undefined && canonical) { registerDiagnosticSensitiveValue(canonical); registerDiagnosticSensitiveValue(leaseId.trim()); } - updateDiagnosticsScope({ logOwner: leaseVerifier(leaseId) }); + if (getDiagnosticsMeta().recordId) return; + const owner = leaseId === undefined ? UNLEASED_RECORD : leaseVerifier(leaseId); + updateDiagnosticsScope({ recordId: `${crypto.randomBytes(16).toString('hex')}-${owner}` }); +} + +/** The name of the diagnostics record that the running request writes. */ +export function requestDiagnosticsRecordId(req: Pick): string | undefined { + const scope = getDiagnosticsMeta(); + return scope.recordId ?? req.meta?.requestId ?? scope.requestId; +} + +/** + * The lease that owns the diagnostics record `recordId` names: `undefined` for a record of a + * request that sent no lease id. Returns `null` for a name that this daemon did not choose, such as + * a record from a release that named records by the caller's request id. Its owner is not known. + */ +export function diagnosticsRecordOwner(recordId: string): string | undefined | null { + const owner = DIAGNOSTICS_RECORD_ID.exec(recordId)?.[1]; + if (owner === undefined) return null; + return owner === UNLEASED_RECORD ? undefined : owner; } // --- Downloadable artifact tracking --- @@ -116,6 +142,8 @@ type ArtifactEntry = { artifactPath: string; tenantId?: string; leaseVerifier?: string; + /** The output that was registered. Another output written to `artifactPath` is not this artifact. */ + output: string | undefined; artifactType: DaemonArtifactType | undefined; fileName?: string; displayRotation?: DeviceRotation; @@ -157,8 +185,8 @@ const pendingUploads = () => artifactTracking().uploads; export type DownloadableArtifactRegistration = { artifactPath: string; tenantId?: string; - /** The lease that produced the artifact. Only a request that sends this id lists or downloads it. */ - leaseId?: string; + /** {@link leaseVerifier} of the lease that produced the artifact. Only a request that sends that lease id lists or downloads it. */ + leaseVerifier?: string; artifactType: DaemonArtifactType | undefined; fileName?: string; displayRotation?: DeviceRotation; @@ -187,13 +215,14 @@ export type OpenedDownloadableArtifact = PreparedDownloadableArtifact & { }; /** - * Registers one file for download and returns its id. A file the same tenant can already download - * returns its existing entry unchanged: two ids for one file would each delete it after their own - * download. A file that a lease registered keeps that entry for every later registration, so a - * request without that lease cannot register the file again and read it. + * Registers one output for download and returns its id. The same output registered again for the + * same tenant and lease returns its existing entry unchanged: two ids for one file would each + * delete it after their own download. An id serves only the output that it registered: another + * output written to the same path gets its own id, and the earlier id expires. */ export function trackDownloadableArtifact(params: DownloadableArtifactRegistration): string { - const existingId = findTrackedArtifactId(params); + const output = readOutputIdentity(params.artifactPath); + const existingId = findTrackedArtifactId(params, output); if (existingId) return existingId; const artifactId = crypto.randomUUID(); const createdAt = Date.now(); @@ -210,7 +239,8 @@ export function trackDownloadableArtifact(params: DownloadableArtifactRegistrati pendingArtifacts().set(artifactId, { artifactPath: params.artifactPath, tenantId: params.tenantId, - ...(params.leaseId !== undefined ? { leaseVerifier: leaseVerifier(params.leaseId) } : {}), + ...(params.leaseVerifier !== undefined ? { leaseVerifier: params.leaseVerifier } : {}), + output, artifactType: params.artifactType, fileName: params.fileName, ...(params.displayRotation !== undefined ? { displayRotation: params.displayRotation } : {}), @@ -225,12 +255,18 @@ export function trackDownloadableArtifact(params: DownloadableArtifactRegistrati } function findTrackedArtifactId( - owner: Pick, + owner: Pick, + output: string | undefined, ): string | undefined { for (const [artifactId, entry] of pendingArtifacts()) { - if (entry.cleanupRequested || entry.artifactPath !== owner.artifactPath) continue; - if (entry.leaseVerifier !== undefined) return artifactId; - if (entry.tenantId === owner.tenantId && owner.leaseId === undefined) return artifactId; + if ( + !entry.cleanupRequested && + entry.artifactPath === owner.artifactPath && + entry.output === output && + entry.tenantId === owner.tenantId && + entry.leaseVerifier === owner.leaseVerifier + ) + return artifactId; } return undefined; } @@ -242,6 +278,20 @@ function findArtifactSourceCleanup(artifactPath: string): ArtifactSourceCleanup return undefined; } +/** What tells one output at a path from the next one written there. A directory keeps its identity while its entries change. */ +function outputIdentity(stat: fs.Stats): string { + const inode = `${stat.dev}:${stat.ino}`; + return stat.isDirectory() ? inode : `${inode}:${stat.size}:${stat.mtimeMs}`; +} + +function readOutputIdentity(artifactPath: string): string | undefined { + try { + return outputIdentity(fs.statSync(artifactPath)); + } catch { + return undefined; + } +} + export async function openDownloadableArtifact( artifactId: string, reader: DownloadableArtifactReader, @@ -315,13 +365,8 @@ export async function listDownloadableArtifacts( const artifacts: DaemonArtifactInventoryEntry[] = []; for (const [id, entry] of pendingArtifacts()) { if (!canReadArtifact(entry, reader)) continue; - let stat: fs.Stats; - try { - stat = fs.statSync(entry.artifactPath); - } catch { - cleanupDownloadableArtifact(id); - continue; - } + const stat = statRegisteredOutput(id, entry); + if (!stat) continue; const payload = await prepareArtifactPayloadForInventory(id, entry, stat, hostEnv); if (!payload || entry.cleanupRequested) continue; artifacts.push({ @@ -339,6 +384,18 @@ export async function listDownloadableArtifacts( return artifacts; } +/** The output that `entry` registered. Cleans the entry up when the output is gone or replaced. */ +function statRegisteredOutput(artifactId: string, entry: ArtifactEntry): fs.Stats | undefined { + try { + const stat = fs.statSync(entry.artifactPath); + if (outputIdentity(stat) === entry.output) return stat; + } catch { + // A missing output is cleaned up like a replaced one. + } + cleanupDownloadableArtifact(artifactId); + return undefined; +} + async function prepareArtifactPayloadForInventory( artifactId: string, entry: ArtifactEntry, @@ -376,6 +433,10 @@ function readDownloadableArtifactEntry( cleanupDownloadableArtifact(artifactId); throw new AppError('COMMAND_FAILED', `Artifact file is missing: ${entry.artifactPath}`); } + if (outputIdentity(stat) !== entry.output) { + cleanupDownloadableArtifact(artifactId); + throw expiredTenantOwnedEntryError(DOWNLOADABLE_ARTIFACT_RESOURCE, artifactId); + } return { entry, stat }; } diff --git a/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts b/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts index d8aab1339d..9da09239fd 100644 --- a/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts +++ b/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts @@ -109,6 +109,8 @@ async function withDiagnosticsHookServer( } } +const RECORD_ID = `${'a'.repeat(32)}-unleased`; + function writeDiagnosticsRecord(sessionsDir: string, session: string, requestId: string): void { const recordPath = resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(session)), @@ -363,8 +365,8 @@ test('aux route: a hook configured but silent on tenant refuses a client-declare try { const hookPath = writeSilentAuthHook(root); await withDiagnosticsHookServer(hookPath, async ({ baseUrl, sessionsDir }) => { - writeDiagnosticsRecord(sessionsDir, 'victim-tenant:default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'victim-tenant:default', 'abc123'), { + writeDiagnosticsRecord(sessionsDir, 'victim-tenant:default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'victim-tenant:default', RECORD_ID), { headers: { authorization: `Bearer ${DAEMON_TOKEN}`, [DAEMON_HTTP_TENANT_HEADER]: 'victim-tenant', @@ -384,8 +386,8 @@ test('aux route: a hook configured but silent on tenant refuses an omitted heade try { const hookPath = writeSilentAuthHook(root); await withDiagnosticsHookServer(hookPath, async ({ baseUrl, sessionsDir }) => { - writeDiagnosticsRecord(sessionsDir, 'victim-tenant:default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'victim-tenant:default', 'abc123'), { + writeDiagnosticsRecord(sessionsDir, 'victim-tenant:default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'victim-tenant:default', RECORD_ID), { headers: { authorization: `Bearer ${DAEMON_TOKEN}` }, }); assert.equal(response.status, 401); @@ -406,9 +408,9 @@ test('aux route: a hook that attests a tenant wins over a mismatched client-decl try { const hookPath = writeAttestingAuthHook(root); await withDiagnosticsHookServer(hookPath, async ({ baseUrl, sessionsDir }) => { - writeDiagnosticsRecord(sessionsDir, `${ATTESTED_TENANT_ID}:default`, 'abc123'); + writeDiagnosticsRecord(sessionsDir, `${ATTESTED_TENANT_ID}:default`, RECORD_ID); const owner = await fetch( - diagnosticsUrl(baseUrl, `${ATTESTED_TENANT_ID}:default`, 'abc123'), + diagnosticsUrl(baseUrl, `${ATTESTED_TENANT_ID}:default`, RECORD_ID), { headers: { authorization: `Bearer ${DAEMON_TOKEN}`, @@ -427,8 +429,8 @@ test('aux route: a hook that attests a tenant wins over a mismatched client-decl test('aux route: no hook configured keeps the header-declared tenant unchanged (regression)', async (t) => { if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsHookServer(undefined, async ({ baseUrl, sessionsDir }) => { - writeDiagnosticsRecord(sessionsDir, 'tenant-a:default', 'abc123'); - const owner = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', 'abc123'), { + writeDiagnosticsRecord(sessionsDir, 'tenant-a:default', RECORD_ID); + const owner = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', RECORD_ID), { headers: { authorization: `Bearer ${DAEMON_TOKEN}`, [DAEMON_HTTP_TENANT_HEADER]: 'tenant-a', @@ -439,7 +441,7 @@ test('aux route: no hook configured keeps the header-declared tenant unchanged ( // session namespace and the `:` prefix carries no ownership to check // — the two RPC regressions above show the same caller may already run any // command in `tenant-a:default`. The token is the gate in this mode. - const otherTenant = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', 'abc123'), { + const otherTenant = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', RECORD_ID), { headers: { authorization: `Bearer ${DAEMON_TOKEN}`, [DAEMON_HTTP_TENANT_HEADER]: 'tenant-b', @@ -458,18 +460,18 @@ test('aux route: an attested tenant is still refused a record outside its partit // The hook attests `tenant-real`, so every session that caller can reach is // named `tenant-real:...`. Another tenant's record, and the plain sessions // outside the partition, both stay outside what it may address. - writeDiagnosticsRecord(sessionsDir, 'tenant-other:default', 'abc123'); - writeDiagnosticsRecord(sessionsDir, 'cwd:9f1:default', 'abc123'); + writeDiagnosticsRecord(sessionsDir, 'tenant-other:default', RECORD_ID); + writeDiagnosticsRecord(sessionsDir, 'cwd:9f1:default', RECORD_ID); const auth = { authorization: `Bearer ${DAEMON_TOKEN}` }; for (const session of ['tenant-other:default', 'cwd:9f1:default']) { - const refused = await fetch(diagnosticsUrl(baseUrl, session, 'abc123'), { headers: auth }); + const refused = await fetch(diagnosticsUrl(baseUrl, session, RECORD_ID), { headers: auth }); assert.equal(refused.status, 401, `expected 401 for session ${session}`); const body = (await refused.json()) as { error?: string; code?: string }; assert.equal(body.code, 'UNAUTHORIZED'); assert.equal(body.error, 'Session is outside this tenant'); } - writeDiagnosticsRecord(sessionsDir, `${ATTESTED_TENANT_ID}:default`, 'abc123'); - const own = await fetch(diagnosticsUrl(baseUrl, `${ATTESTED_TENANT_ID}:default`, 'abc123'), { + writeDiagnosticsRecord(sessionsDir, `${ATTESTED_TENANT_ID}:default`, RECORD_ID); + const own = await fetch(diagnosticsUrl(baseUrl, `${ATTESTED_TENANT_ID}:default`, RECORD_ID), { headers: auth, }); assert.equal(own.status, 200); diff --git a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts index e546f319dc..fa55c8d68e 100644 --- a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts +++ b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts @@ -17,6 +17,9 @@ import { const RECORD = '{"phase":"request_start"}\n{"phase":"request_failed"}\n'; +// A record id as the daemon chooses it for a request that sent no lease id. +const RECORD_ID = `${'a'.repeat(32)}-unleased`; + function writeRecord(sessionsDir: string, session: string, requestId: string): string { const recordPath = resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(session)), @@ -83,8 +86,8 @@ test('request diagnostics route serves one record as ndjson to an authorized cal if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'cwd:9f1:default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'cwd:9f1:default', 'abc123'), { + writeRecord(sessionsDir, 'cwd:9f1:default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'cwd:9f1:default', RECORD_ID), { headers: { authorization: 'Bearer daemon-secret' }, }); assert.equal(response.status, 200); @@ -98,7 +101,7 @@ test('request diagnostics returns complete JSON when its stream fails before hea if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'default', 'abc123'); + writeRecord(sessionsDir, 'default', RECORD_ID); const createReadStream = fs.createReadStream; const streams = vi.spyOn(fs, 'createReadStream').mockImplementation((file, options) => createReadStream(file, { @@ -111,7 +114,7 @@ test('request diagnostics returns complete JSON when its stream fails before hea ); let response: Response | undefined; try { - response = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + response = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers: { authorization: 'Bearer daemon-secret' }, }); expect(response.status).toBe(500); @@ -129,8 +132,8 @@ test('request diagnostics route refuses an unauthenticated caller', async (t) => if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123')); + writeRecord(sessionsDir, 'default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID)); assert.equal(response.status, 401); assert.equal((await response.text()).includes('request_start'), false); }); @@ -140,31 +143,44 @@ test('request diagnostics route answers 404 for a record that was never written' if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'default', 'abc123'); + writeRecord(sessionsDir, 'default', RECORD_ID); const auth = { authorization: 'Bearer daemon-secret' }; const unknownRequest = await fetch(diagnosticsUrl(baseUrl, 'default', 'nope'), { headers: auth, }); assert.equal(unknownRequest.status, 404); - const unknownSession = await fetch(diagnosticsUrl(baseUrl, 'other', 'abc123'), { + const unknownSession = await fetch(diagnosticsUrl(baseUrl, 'other', RECORD_ID), { headers: auth, }); assert.equal(unknownSession.status, 404); }); }); -test('request diagnostics route rejects ids that do not name one record', async (t) => { +test('request diagnostics route does not serve a record that an earlier release named', async (t) => { if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { + // Its name carries no owner, so nobody is known to own it. writeRecord(sessionsDir, 'default', 'abc123'); + const legacy = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + headers: { authorization: 'Bearer daemon-secret' }, + }); + assert.equal(legacy.status, 404); + }); +}); + +test('request diagnostics route rejects ids that do not name one record', async (t) => { + if (await skipWhenLoopbackUnavailable(t)) return; + + await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { + writeRecord(sessionsDir, 'default', RECORD_ID); // Sent over a raw request rather than `fetch`: URL parsing resolves `..` // away client-side, so only a hand-built request target reaches the daemon // with the segment a traversal attempt would actually carry. for (const [session, requestId] of [ - ['..', 'abc123'], + ['..', RECORD_ID], ['default', '..'], - ['', 'abc123'], + ['', RECORD_ID], ['default', ''], ] as const) { const status = await requestRawStatus( @@ -188,13 +204,13 @@ test('request diagnostics route reads an unattested tenant as no session partiti if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'tenant-a:default', 'abc123'); + writeRecord(sessionsDir, 'tenant-a:default', RECORD_ID); const auth = { authorization: 'Bearer daemon-secret' }; - const owner = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', 'abc123'), { + const owner = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', RECORD_ID), { headers: { ...auth, 'x-agent-device-tenant': 'tenant-a' }, }); assert.equal(owner.status, 200); - const otherTenant = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', 'abc123'), { + const otherTenant = await fetch(diagnosticsUrl(baseUrl, 'tenant-a:default', RECORD_ID), { headers: { ...auth, 'x-agent-device-tenant': 'tenant-b' }, }); assert.equal(otherTenant.status, 200); @@ -205,8 +221,8 @@ test('request diagnostics route serves a plain session to the tenant that ran th if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + writeRecord(sessionsDir, 'default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers: { authorization: 'Bearer daemon-secret', 'x-agent-device-tenant': 'tenant-a', @@ -221,8 +237,8 @@ test('request diagnostics route serves the cwd-scoped session its owner ran in', if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'cwd:9f1:default', 'abc123'); - const response = await fetch(diagnosticsUrl(baseUrl, 'cwd:9f1:default', 'abc123'), { + writeRecord(sessionsDir, 'cwd:9f1:default', RECORD_ID); + const response = await fetch(diagnosticsUrl(baseUrl, 'cwd:9f1:default', RECORD_ID), { headers: { authorization: 'Bearer daemon-secret', 'x-agent-device-tenant': 'tenant-a', @@ -256,14 +272,14 @@ test('request diagnostics route does not serve a tenant that held a macos-app le deviceKey: 'com.example.app', }); await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { - writeRecord(sessionsDir, 'default', 'abc123'); + writeRecord(sessionsDir, 'default', RECORD_ID); const auth = { authorization: 'Bearer daemon-secret' }; - const leased = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + const leased = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers: { ...auth, 'x-agent-device-tenant': 'tenant-a' }, }); assert.equal(leased.status, 401, await leased.clone().text()); assert.equal((await leased.text()).includes('request_start'), false); - const other = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + const other = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers: { ...auth, 'x-agent-device-tenant': 'tenant-b' }, }); assert.equal(other.status, 200); diff --git a/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts b/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts index 5702f5ded7..f6606412e8 100644 --- a/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts @@ -73,9 +73,11 @@ test('open relaunch retains Android foreground probe failure in the serialized r 'sessions', 'fence-session', 'requests', - 'fence-request.ndjson', + `${response.error.diagnosticsRecord?.requestId}.ndjson`, ), ); + // The daemon names the record. The caller's request id names no file. + expect(response.error.diagnosticsRecord?.requestId).toMatch(/^[a-f0-9]{32}-unleased$/); const events = fs .readFileSync(response.error.logPath!, 'utf8') .trim() diff --git a/packages/daemon/src/server/__tests__/request-router-error-redaction.test.ts b/packages/daemon/src/server/__tests__/request-router-error-redaction.test.ts index 6d29ec8724..fac6db1c6c 100644 --- a/packages/daemon/src/server/__tests__/request-router-error-redaction.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-error-redaction.test.ts @@ -86,6 +86,6 @@ test('a Maestro replay error comes back unchanged after its inputText registered expect(result.ok).toBe(false); if (result.ok) return; expect(JSON.stringify(result.error)).not.toContain('[REDACTED]'); - expect(result.error.logPath).toContain('req-maestro-input-1'); + expect(result.error.logPath).toMatch(/[a-f0-9]{32}-unleased\.ndjson$/); expect(fs.existsSync(result.error.logPath!)).toBe(true); }); diff --git a/packages/daemon/src/server/__tests__/request-router-fill-text-stdin.test.ts b/packages/daemon/src/server/__tests__/request-router-fill-text-stdin.test.ts index 752c42c4a0..82335b5ba5 100644 --- a/packages/daemon/src/server/__tests__/request-router-fill-text-stdin.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-fill-text-stdin.test.ts @@ -226,7 +226,6 @@ test.each([ ); test('a short --text-stdin value is redacted from the error text but not from its log path', async () => { - // `stdin` also appears in this request's id, which names its diagnostics file. const { response } = fillWithStdinText( makeSession('web', { device: WEB_DESKTOP_DEVICE }), {}, @@ -238,9 +237,8 @@ test('a short --text-stdin value is redacted from the error text but not from it expect(result.ok).toBe(false); if (result.ok) return; expect(result.error.message).toBe('could not type "[REDACTED]" into the field'); - expect(result.error.logPath).toContain('req-fill-text-stdin'); expect(fs.existsSync(result.error.logPath!)).toBe(true); - expect(result.error.diagnosticsRecord?.requestId).toBe('req-fill-text-stdin'); + expect(result.error.diagnosticsRecord?.requestId).toMatch(/^[a-f0-9]{32}-unleased$/); }); test('a batch step carrying --text-stdin is refused before typing and returns no part of the value', async () => { diff --git a/packages/daemon/src/server/__tests__/request-router-open.test.ts b/packages/daemon/src/server/__tests__/request-router-open.test.ts index 2b7ab8b4c2..b71c32380b 100644 --- a/packages/daemon/src/server/__tests__/request-router-open.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-open.test.ts @@ -146,8 +146,11 @@ test('open returns and creates the session state directory', async () => { expect(response.data?.runnerLogPath).toEqual( path.join(String(response.data?.sessionStateDir), 'runner.log'), ); - expect(response.data?.requestLogPath).toEqual( - path.join(String(response.data?.sessionStateDir), 'requests', 'req-open-state.ndjson'), + expect(path.dirname(String(response.data?.requestLogPath))).toEqual( + path.join(String(response.data?.sessionStateDir), 'requests'), + ); + expect(path.basename(String(response.data?.requestLogPath))).toMatch( + /^[a-f0-9]{32}-unleased\.ndjson$/, ); expect(response.data?.eventLogPath).toEqual( path.join(String(response.data?.sessionStateDir), 'events.ndjson'), diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts index 1cd775fa33..4b69c94c7a 100644 --- a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -211,6 +211,10 @@ test('the owner downloads a leased screenshot after its session and lease ended' expect(await listedIds(await world.get('/artifacts', world.leaseId.toUpperCase()))).toEqual([ artifactId, ]); + // The CLI sends the ended lease id on `artifacts` after close. + expect(await rpcListedIds(await world.send('artifacts', SESSION, world.owner))).toEqual([ + artifactId, + ]); const download = await world.get(`/artifacts/${artifactId}`, world.leaseId); expect(download.status).toBe(200); const bytes = Buffer.from(await download.arrayBuffer()); @@ -255,7 +259,24 @@ test('a request that a leased session refused reads its own record, which names const served = await own.text(); expect(served).not.toContain(world.leaseId); expect(served).not.toContain(OTHER_LEASE_ID); - expect((await world.get(`${route}.owners`, OTHER_LEASE_ID)).status).toBe(404); +}); + +test('two requests that reuse one request id get one record each', async () => { + if (!world) return; + const requestId = 'reused-request-id'; + const owned = await world.send('press', SESSION, { ...world.owner, requestId }); + const refused = await world.send('press', SESSION, { requestId }); + const records = [owned, refused].map((body) => body.error?.data?.diagnosticsRecord); + expect(records[0]?.requestId).not.toBe(records[1]?.requestId); + const route = (record: DiagnosticsRecordRef | undefined) => + `/sessions/${encodeURIComponent(record!.session)}/requests/${record!.requestId}/diagnostics`; + + // Each caller still reads its own record: neither request changed the owner of the other. + expect((await world.get(route(records[0]), world.leaseId)).status).toBe(200); + await expectLeasedOutputRefusal(await world.get(route(records[0]))); + expect((await world.get(route(records[1]))).status).toBe(200); + // The request id of the caller names no record. + expect((await world.get(route({ ...records[0]!, requestId }), world.leaseId)).status).toBe(404); }); test('a request with no lease on a session with no lease keeps the tenant rule', async () => { diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index 2e3553f73d..19dc7ff4d6 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -19,7 +19,6 @@ import http from 'node:http'; import { AppError, normalizeError, type DiagnosticsRecordRef } from '@agent-device/kernel/errors'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { isSafeSessionSegment } from '@agent-device/host-kit/session-paths'; -import { diagnosticsRecordOwnersPath } from '@agent-device/host-kit/diagnostics'; import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { decodeUriSegment } from './http-request-target.ts'; import { @@ -27,7 +26,11 @@ import { type TenantSessionNamespace, } from '../core/session-tenant-scope.ts'; import { failStreamedHttpResponse, sendRestJsonError } from './http-errors.ts'; -import { leasedOutputRefusal, requestHoldsOutputLease } from '../resources/artifact-tracking.ts'; +import { + diagnosticsRecordOwner, + leasedOutputRefusal, + requestHoldsOutputLease, +} from '../resources/artifact-tracking.ts'; const REQUEST_DIAGNOSTICS_CONTENT_TYPE = 'application/x-ndjson'; @@ -137,8 +140,17 @@ async function handleRequestDiagnostics( return; } + const owner = diagnosticsRecordOwner(ref.requestId); + const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; + const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; + if (owner !== null && !requestHoldsOutputLease(owner, presented)) { + sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); + return; + } + const recordPath = resolveRecordPath(ref); - const stats = await statRecord(recordPath); + // A record of unknown ownership, such as one that an earlier release wrote, is not served. + const stats = owner === null ? null : await statRecord(recordPath); if (!stats) { sendRestJsonError( res, @@ -155,16 +167,7 @@ async function handleRequestDiagnostics( return; } - // The owners are on disk before the first byte of the record, so they are read after the size. - const owners = await readRecordOwners(recordPath); - const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; - const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; - if (!owners.every((owner) => requestHoldsOutputLease(owner, presented))) { - sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); - return; - } - - const stream = fs.createReadStream(recordPath, { end: stats.size - 1 }); + const stream = fs.createReadStream(recordPath); res.statusCode = 200; res.setHeader('content-type', REQUEST_DIAGNOSTICS_CONTENT_TYPE); res.setHeader('content-length', String(stats.size)); @@ -180,18 +183,8 @@ async function handleRequestDiagnostics( async function statRecord(recordPath: string): Promise { try { const stats = await fs.promises.stat(recordPath); - return stats.isFile() && stats.size > 0 ? stats : null; + return stats.isFile() ? stats : null; } catch { return null; } } - -async function readRecordOwners(recordPath: string): Promise { - try { - const owners = await fs.promises.readFile(diagnosticsRecordOwnersPath(recordPath), 'utf8'); - return owners.split('\n').filter((owner) => owner.length > 0); - } catch (error) { - if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []; - throw error; - } -} diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index 35e96bda36..3a89162eca 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -12,7 +12,10 @@ import { import type { DaemonResponse } from '@agent-device/kernel/contracts'; import type { CommandFlags } from '@agent-device/contracts/command'; import type { LeaseLifecycleProvider, ProviderAppCatalog } from '@agent-device/contracts/device'; -import type { TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; +import { + requestDiagnosticsRecordId, + type TrackDownloadableArtifact, +} from '../resources/artifact-tracking.ts'; import { emitDiagnostic, getDiagnosticsMeta, @@ -259,12 +262,11 @@ export async function createRequestExecutionScope(params: { // trait under which an activity stamp can be written while holding that lock. const attachesToSession = shouldLockSessionExecution(command); const sessionName = resolveRequestSessionName({ scopedReq, unscopedReq, sessionStore }); - const diagnosticsMeta = getDiagnosticsMeta(); const sessionDir = sessionStore.resolveSessionDir(sessionName); const requestLog = resolveSessionRequestLog({ sessionDir, session: sessionName, - requestId: scopedReq.meta?.requestId ?? diagnosticsMeta.requestId, + requestId: requestDiagnosticsRecordId(scopedReq), }); const requestLogPath = requestLog.path; const runnerLogPath = resolveSessionRunnerLogPath(sessionDir); diff --git a/packages/daemon/src/server/request-finalization.ts b/packages/daemon/src/server/request-finalization.ts index 9479a43cf9..5b277b5432 100644 --- a/packages/daemon/src/server/request-finalization.ts +++ b/packages/daemon/src/server/request-finalization.ts @@ -13,8 +13,9 @@ import type { DaemonResponseData, } from '@agent-device/kernel/contracts'; import { isDeviceRotation, type DeviceRotation } from '@agent-device/contracts/device'; -import type { TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; +import { leaseVerifier, type TrackDownloadableArtifact } from '../resources/artifact-tracking.ts'; import { + captureOutputLeaseVerifier, captureOutputOwnership, captureOutputTenantId, daemonDeliveredArtifact, @@ -96,6 +97,9 @@ function registerDownloadableArtifacts( trackArtifact: TrackDownloadableArtifact, ): DaemonResponseData | undefined { if (!data) return data; + const admittedLeaseId = req.internal?.admittedLease?.leaseId; + const owner = + captureOutputLeaseVerifier(data) ?? (admittedLeaseId && leaseVerifier(admittedLeaseId)); const register = (artifact: PendingArtifact): DaemonArtifact => { const artifactPath = artifact.path as string; const delivered = daemonDeliveredArtifact(artifact); @@ -109,7 +113,7 @@ function registerDownloadableArtifacts( tenantId: delivered ? delivered.tenantId : (captureOutputTenantId(data) ?? req.meta?.tenantId), - leaseId: req.internal?.admittedLease?.leaseId, + ...(owner ? { leaseVerifier: owner } : {}), artifactType: artifact.artifactType, fileName: artifact.fileName, ...(artifact.deleteAfterDownload === false ? { deleteAfterDownload: false } : {}), diff --git a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts index 5a385a2c6b..86ed281c72 100644 --- a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts +++ b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts @@ -29,9 +29,11 @@ import type { SessionStore } from '../../core/session-store.ts'; import type { SessionState } from '../../core/session-state.ts'; import { cleanupDownloadableArtifact, + leaseVerifier, listDownloadableArtifacts, trackDownloadableArtifact, } from '../../resources/artifact-tracking.ts'; +import { withCaptureOutputOwnership } from '../../core/capture-output-ownership.ts'; import { tryHandleDownloadableArtifactHttpRoute } from '../../server/downloadable-artifact-http.ts'; import { finalizeDaemonResponse } from '../../server/request-finalization.ts'; import { buildRecordingStopResponse } from '../handlers/record-runtime-response.ts'; @@ -275,6 +277,9 @@ test('a record stop replay after a leased teardown serves the artifacts teardown device: IOS_SIMULATOR, }); if (recovery.kind !== 'completed') throw new Error(`expected a replay, got ${recovery.kind}`); + const stopped = buildRecordingStopResponse(recovery.completion); + if (!stopped.ok || !stopped.data) throw new Error('expected a stop response'); + // The stop handler names the lease of the manifest as the owner of a replayed export. const replayed = finalizeDaemonResponse( { token: 't', @@ -284,7 +289,10 @@ test('a record stop replay after a leased teardown serves the artifacts teardown flags: {}, meta: { tenantId: TENANT }, }, - buildRecordingStopResponse(recovery.completion), + { + ok: true, + data: withCaptureOutputOwnership(stopped.data, 'daemon', TENANT, leaseVerifier(LEASE_ID)), + }, trackDownloadableArtifact, ); @@ -295,7 +303,7 @@ test('a record stop replay after a leased teardown serves the artifacts teardown expect.objectContaining({ id: retained[0]!.id, sessionName: 'checkout-flow' }), expect.objectContaining({ id: retained[1]!.id, sessionName: 'checkout-flow' }), ]); - // The replay names no lease, so it registers no second way to the files. + // The replay registers under the lease of the manifest, so it opens no second way to the files. expect(await listDownloadableArtifacts({ tenantId: TENANT }, process.env)).toEqual([]); }); diff --git a/packages/daemon/src/sessions/__tests__/request-admission.test.ts b/packages/daemon/src/sessions/__tests__/request-admission.test.ts index 0f3165cbb3..353592739e 100644 --- a/packages/daemon/src/sessions/__tests__/request-admission.test.ts +++ b/packages/daemon/src/sessions/__tests__/request-admission.test.ts @@ -138,6 +138,28 @@ test('close with an explicit lease id but no session lease still requires an act ); }); +test('record stop with no session runs without a lease only when the lease it names ended', () => { + const registry = new LeaseRegistry(); + const scope = { tenantId: 'tenant-a', runId: 'run-1', leaseBackend: 'ios-instance' } as const; + const live = registry.allocateLease(scope); + const admit = (positionals: string[], leaseId: string) => + assertRequestLeaseAdmission( + makeRequest({ + command: 'record', + positionals, + meta: { ...scope, leaseId, sessionIsolation: 'tenant' }, + }), + registry, + undefined, + { env: process.env }, + ); + + // The handler checks the ended lease id against the owner of the export. + assert.equal(admit(['stop'], 'a'.repeat(32)), undefined); + assert.equal(admit(['stop'], live.leaseId)?.leaseId, live.leaseId); + assert.throws(() => admit(['start'], 'a'.repeat(32)), /Lease is not active/); +}); + test('non-close commands on a tenant-isolated session still require a lease id', () => { const registry = new LeaseRegistry(); diff --git a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts index c2a46a69a9..7f9d8b20c9 100644 --- a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts +++ b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts @@ -16,6 +16,8 @@ import { } from '@agent-device/host-kit/request'; import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store'; import { LeaseRegistry } from '../../../resources/lease-registry.ts'; +import { leaseVerifier } from '../../../resources/artifact-tracking.ts'; +import { captureOutputLeaseVerifier } from '../../../core/capture-output-ownership.ts'; import { expectDecodedCompletedRecording, makeRecordRuntimeHarness, @@ -106,47 +108,66 @@ test('record stop after a daemon restart replays the completed export for the na ); }); -test('record stop after a daemon restart serves the export only under the lease that started it', async () => { - const registry = new LeaseRegistry(); - const [owner, other] = ['run-a', 'run-b'].map((runId) => ({ - admittedLease: registry.allocateLease({ - tenantId: 'tenant-a', - runId, - leaseBackend: 'android-instance', - }), - })); - const harness = makeRecordRuntimeHarness('record-runtime-restart-stop-lease-'); - const outPath = path.join( - mkdtempForTestSync('record-runtime-restart-stop-lease-output-'), - 'capture.mp4', - ); +async function completeLeasedRecording(prefix: string) { + const lease = new LeaseRegistry().allocateLease({ + tenantId: 'tenant-a', + runId: 'run-a', + leaseBackend: 'android-instance', + }); + const harness = makeRecordRuntimeHarness(prefix); + const outPath = path.join(mkdtempForTestSync(`${prefix}output-`), 'capture.mp4'); fs.writeFileSync(outPath, 'mp4'); - await harness.run(['start', outPath], undefined, undefined, owner); - await harness.run(['stop'], undefined, undefined, owner); + await harness.run(['start', outPath], undefined, undefined, { admittedLease: lease }); + await harness.run(['stop'], undefined, undefined, { admittedLease: lease }); const ref = harness.sessionStore.lookup(harness.sessionName); if (!ref) throw new Error('Expected the stopped recording session'); harness.sessionStore.retire(ref); + const replay = async (leaseId?: string) => + await harness.run(['stop'], { sessionExplicit: true, ...(leaseId ? { leaseId } : {}) }); + return { harness, leaseId: lease.leaseId, outPath, replay }; +} + +const LEASE_OUTPUT_REFUSAL = { + ok: false, + error: { code: 'UNAUTHORIZED', details: { reason: 'LEASE_OUTPUT_MISMATCH', field: 'leaseId' } }, +}; + +test('a completed leased export replays to the lease id that started it, with no active lease', async () => { + const { harness, leaseId, outPath, replay } = await completeLeasedRecording( + 'record-runtime-replay-lease-', + ); const manifest = fs.readFileSync( recordingResourcePath(harness.sessionStore, harness.sessionName), 'utf8', ); - expect(manifest).not.toContain(owner!.admittedLease.leaseId); - - for (const internal of [other, undefined]) { - expect( - await harness.run(['stop'], { sessionExplicit: true }, undefined, internal), - ).toMatchObject({ - ok: false, - error: { - code: 'UNAUTHORIZED', - details: { reason: 'LEASE_OUTPUT_MISMATCH', field: 'leaseId' }, - }, - }); - } - expect(await harness.run(['stop'], { sessionExplicit: true }, undefined, owner)).toMatchObject({ - ok: true, - data: { recording: 'stopped', outPath }, + expect(manifest).not.toContain(leaseId); + + expect(await replay('f'.repeat(32))).toMatchObject(LEASE_OUTPUT_REFUSAL); + expect(await replay()).toMatchObject(LEASE_OUTPUT_REFUSAL); + + // The lease ended with its session. The replay names it, and no request is admitted under it. + const replayed = await replay(leaseId.toUpperCase()); + expect(replayed).toMatchObject({ ok: true, data: { recording: 'stopped', outPath } }); + // The export stays with the lease of the manifest when the response is registered. + expect(replayed.ok && captureOutputLeaseVerifier(replayed.data!)).toBe(leaseVerifier(leaseId)); +}); + +test('a completed remote export whose manifest names no owner is not replayed', async () => { + const { harness, leaseId, replay } = await completeLeasedRecording( + 'record-runtime-replay-legacy-', + ); + const resourcePath = recordingResourcePath(harness.sessionStore, harness.sessionName); + const record = screenRecordingResourceStore.read(resourcePath); + if (record.status !== 'decoded') throw new Error('Expected decoded recording manifest'); + // A manifest as an earlier release wrote it: a remote export, and no lease or its absence. + const { remoteCaptureLease: _owner, ...metadata } = record.envelope.metadata ?? {}; + screenRecordingResourceStore.write(resourcePath, { + ...record.envelope, + metadata: { ...metadata, remoteCaptureOutput: true }, }); + + expect(await replay(leaseId)).toMatchObject(LEASE_OUTPUT_REFUSAL); + expect(await replay()).toMatchObject(LEASE_OUTPUT_REFUSAL); }); test('record stop recovery after a daemon restart keeps the request host-scoping selectors', async () => { diff --git a/packages/daemon/src/sessions/handlers/record-runtime.ts b/packages/daemon/src/sessions/handlers/record-runtime.ts index dfdf181a7b..02a250eada 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime.ts @@ -53,6 +53,7 @@ import { requestCwd } from '../../core/context.ts'; import type { SessionRef, SessionState } from '../../core/session-state.ts'; import { sessionHasOwnersBeyondRecording } from '../../core/session-state.ts'; import { withCaptureOutputOwnership } from '../../core/capture-output-ownership.ts'; +import { resolveLeaseScope } from '../../core/lease-context.ts'; import { leasedOutputRefusal, leaseVerifier, @@ -309,7 +310,12 @@ async function startRecording( ); } -/** Names in the manifest who gets the export: a remote caller, its tenant, and the lease that started it. */ +const UNLEASED_CAPTURE = 'unleased'; + +/** + * Names in the manifest who gets the export: a remote caller, its tenant, and the lease that started + * the recording, or that no lease did. A manifest that names neither is of unknown ownership. + */ function withCaptureOwner( envelope: DurableResourceEnvelope<'screen-recording'>, req: DaemonRequest, @@ -317,14 +323,13 @@ function withCaptureOwner( ): DurableResourceEnvelope<'screen-recording'> { const tenantId = session.lease?.tenantId ?? req.meta?.tenantId; const leaseId = req.internal?.admittedLease?.leaseId; - if (!req.internal?.publicNetworkOnly && !tenantId && !leaseId) return envelope; return { ...envelope, metadata: { ...envelope.metadata, ...(req.internal?.publicNetworkOnly ? { remoteCaptureOutput: true } : {}), ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), - ...(leaseId ? { remoteCaptureLeaseVerifier: leaseVerifier(leaseId) } : {}), + remoteCaptureLease: leaseId ? leaseVerifier(leaseId) : UNLEASED_CAPTURE, }, }; } @@ -432,7 +437,7 @@ async function stopRecording( kind: 'stop-live' | 'stop-recovery', needsReadiness: boolean, ): Promise { - assertRequestHoldsRecordingLease(params); + const leaseOwner = assertRequestMayStopRecording(params); let stopped: ScreenRecordingStop; try { stopped = @@ -486,24 +491,36 @@ async function stopRecording( response.data, daemonOwned ? 'daemon' : 'caller', tenantId, + leaseOwner, ), } : response; } /** - * The manifest names the lease that started the recording. Only a request under that lease stops - * the recording or gets its export, also when the manifest outlived its session or its daemon. + * Refuses a stop that the owner of the recording did not send, and returns the verifier of the + * lease that owns the export. A completed export needs only the lease id of its owner, also after + * that lease ended. A recording that still runs needs the admitted lease. */ -function assertRequestHoldsRecordingLease(params: RecordRuntimeHandlerParams): void { +function assertRequestMayStopRecording(params: RecordRuntimeHandlerParams): string | undefined { const record = screenRecordingDurableResource.store.read( screenRecordingDurableResource.resourcePath(params.sessionStore, params.sessionName), ); - const owner = - record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureLeaseVerifier : undefined; - if (typeof owner !== 'string') return; - if (requestHoldsOutputLease(owner, params.req.internal?.admittedLease?.leaseId)) return; - throw leasedOutputRefusal('Recording'); + if (record.status !== 'decoded') return undefined; + const { metadata, lifecycle } = record.envelope; + const owner = metadata?.remoteCaptureLease; + if (owner === UNLEASED_CAPTURE) return undefined; + if (typeof owner !== 'string') { + // A manifest of an earlier release names no lease, so nobody is known to own its remote export. + if (metadata?.remoteCaptureOutput === true) throw leasedOutputRefusal('Recording'); + return undefined; + } + const presented = + lifecycle === 'completed' + ? resolveLeaseScope(params.req).leaseId + : params.req.internal?.admittedLease?.leaseId; + if (!requestHoldsOutputLease(owner, presented)) throw leasedOutputRefusal('Recording'); + return owner; } function deleteTerminalRecordOnlySession( diff --git a/packages/daemon/src/sessions/leased-recording-retention.ts b/packages/daemon/src/sessions/leased-recording-retention.ts index 216b08c969..4a0b444bbd 100644 --- a/packages/daemon/src/sessions/leased-recording-retention.ts +++ b/packages/daemon/src/sessions/leased-recording-retention.ts @@ -6,6 +6,7 @@ import type { SessionRef } from '../core/session-state.ts'; import { tenantPublicSessionName } from '../core/session-tenant-scope.ts'; import { cleanupDownloadableArtifact, + leaseVerifier, trackDownloadableArtifact, } from '../resources/artifact-tracking.ts'; import { @@ -37,7 +38,7 @@ export function retainLeasedSessionRecording( trackDownloadableArtifact({ artifactPath: artifact.path!, tenantId: lease.tenantId, - leaseId: lease.leaseId, + leaseVerifier: leaseVerifier(lease.leaseId), artifactType: artifact.artifactType, fileName: artifact.fileName, sessionName, diff --git a/packages/daemon/src/sessions/request-admission.ts b/packages/daemon/src/sessions/request-admission.ts index a43f138fbf..2be557bab1 100644 --- a/packages/daemon/src/sessions/request-admission.ts +++ b/packages/daemon/src/sessions/request-admission.ts @@ -158,8 +158,12 @@ export function assertRequestLeaseAdmission( } if ( session === undefined && - !requestLeaseScope.leaseId && - hasSessionlessLeaseAdmissionExemption(req, options.providerAppCatalog) + hasSessionlessLeaseAdmissionExemption( + req, + requestLeaseScope.leaseId, + leaseRegistry, + options.providerAppCatalog, + ) ) { return undefined; } @@ -235,9 +239,15 @@ export function isConfinedToAppLease( function hasSessionlessLeaseAdmissionExemption( req: DaemonRequest, + leaseId: string | undefined, + leaseRegistry: LeaseRegistry, providerAppCatalog: ProviderAppCatalog | undefined, ): boolean { const exemption = resolveSessionlessLeaseAdmissionExemption(req); + if (exemption?.kind === 'ended-lease-output') { + return leaseId !== undefined && leaseRegistry.inspectActiveLease(leaseId) === undefined; + } + if (leaseId) return false; if (exemption?.kind === 'unconditional') return true; return ( exemption?.kind === 'provider-app-catalog' && diff --git a/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts b/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts index 20f41a8107..36fe38686e 100644 --- a/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts +++ b/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts @@ -54,7 +54,8 @@ import { markDeferredInteractionOutcome, markPostOpenStabilization, } from '../../../core/deferred-interaction-outcome.ts'; -import { emitDiagnostic, getDiagnosticsMeta } from '@agent-device/host-kit/diagnostics'; +import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; +import { requestDiagnosticsRecordId } from '../../../resources/artifact-tracking.ts'; import { prepareOpenCommandDetails, type ResolvedOpenRuntimeHintPlan, @@ -374,7 +375,7 @@ export async function completeOpenCommand(params: { const sessionStateDir = sessionStore.ensureSessionDir(sessionName); const requestLogPath = resolveSessionRequestLogPath( sessionStateDir, - req.meta?.requestId ?? getDiagnosticsMeta().requestId, + requestDiagnosticsRecordId(req), ); timing.totalDurationMs = Math.max(0, Date.now() - openCommandStartedAtMs); emitDiagnostic({ diff --git a/packages/host-kit/src/diagnostics.ts b/packages/host-kit/src/diagnostics.ts index 4a55965fdb..4da4b7136f 100644 --- a/packages/host-kit/src/diagnostics.ts +++ b/packages/host-kit/src/diagnostics.ts @@ -2,7 +2,6 @@ export { countDiagnosticEventsByPhase, createRequestId, DIAGNOSTIC_STDERR_PREFIX, - diagnosticsRecordOwnersPath, type DiagnosticEventInput, type DiagnosticLevel, emitDiagnostic, diff --git a/packages/host-kit/src/internal/diagnostics.test.ts b/packages/host-kit/src/internal/diagnostics.test.ts index cff152e49d..e1337056b0 100644 --- a/packages/host-kit/src/internal/diagnostics.test.ts +++ b/packages/host-kit/src/internal/diagnostics.test.ts @@ -9,7 +9,7 @@ import { withDiagnosticsScope, withDiagnosticTimer, DIAGNOSTIC_STDERR_PREFIX, - diagnosticsRecordOwnersPath, + getDiagnosticsMeta, } from './diagnostics.ts'; import { escapeTerminalText } from '@agent-device/kernel/redaction'; import { mkdtempForTestSync } from './tmp-dir.fixtures.ts'; @@ -156,24 +156,12 @@ test('appendDiagnosticLine ensures the log directory once across appended lines' } }); -test('a record names a lease by its fingerprint and publishes its owner before its first byte', async () => { - const logPath = path.join( - mkdtempForTestSync('agent-device-diag-owners-'), - 'requests', - 'r1.ndjson', - ); +test('a record names a lease by its fingerprint, and a nested scope keeps the record of its parent', async () => { + const logPath = path.join(mkdtempForTestSync('agent-device-diag-lease-'), 'r1.ndjson'); const leaseId = 'A1B2C3D4E5F60718293A4B5C6D7E8F90'; - const ownersBeforeRecord = await withDiagnosticsScope( - { - session: 'leased', - requestId: 'r1', - debug: true, - logPath, - logRecord: { session: 'leased', requestId: 'r1' }, - logOwner: 'owner-a', - }, + const recordIds = await withDiagnosticsScope( + { session: 'leased', requestId: 'r1', debug: true, logPath, recordId: 'record-1' }, async () => { - const write = vi.spyOn(fs, 'appendFileSync'); emitDiagnostic({ phase: 'leased_session_ended', data: { @@ -183,15 +171,15 @@ test('a record names a lease by its fingerprint and publishes its owner before i releaseId: 'r-1', }, }); - emitDiagnostic({ phase: 'request_failed' }); - const written = write.mock.calls.map(([file]) => String(file)); - write.mockRestore(); - return written; + const nested = await withDiagnosticsScope( + { requestId: 'r1' }, + () => getDiagnosticsMeta().recordId, + ); + return [getDiagnosticsMeta().recordId, nested]; }, ); - assert.deepEqual(ownersBeforeRecord, [`${logPath}.owners`, logPath, logPath]); - assert.equal(fs.readFileSync(diagnosticsRecordOwnersPath(logPath), 'utf8'), 'owner-a\n'); + assert.deepEqual(recordIds, ['record-1', 'record-1']); const record = fs.readFileSync(logPath, 'utf8'); assert.equal(record.toLowerCase().includes(leaseId.toLowerCase()), false); assert.deepEqual(JSON.parse(record.split('\n')[0]!).data, { diff --git a/packages/host-kit/src/internal/diagnostics.ts b/packages/host-kit/src/internal/diagnostics.ts index 6cb6004f2d..de44b23932 100644 --- a/packages/host-kit/src/internal/diagnostics.ts +++ b/packages/host-kit/src/internal/diagnostics.ts @@ -41,10 +41,11 @@ type DiagnosticsScopeOptions = { */ logRecord?: DiagnosticsRecordRef; /** - * The one opaque owner of what this scope writes to its `logRecord`. It is written as a line of - * {@link diagnosticsRecordOwnersPath} before the first byte that this scope adds to the record. + * The server-chosen name of the record that this scope writes. A scope that opens inside another + * scope writes to the record of that scope, so it keeps its `recordId`. `requestId` is the + * caller's tracking id and names no record. */ - logOwner?: string; + recordId?: string; /** * Set for a log that only the host operator reads, such as the daemon log. It names a lease by * its id, which the operator needs to end a rented device. Every other scope names a lease by @@ -59,7 +60,6 @@ type DiagnosticsScope = DiagnosticsScopeOptions & { flushedLogPath?: string; events: DiagnosticEvent[]; liveWrittenEventCount: number; - logOwnerPublished?: true; // Running per-phase emit tally. Unlike `events`, this is NOT cleared by // `flushDiagnosticsToSessionFile`, so consumers (e.g. the agent-cost graft) // can count phase occurrences for the whole request even in debug mode where @@ -90,6 +90,7 @@ export async function withDiagnosticsScope( fn: () => Promise | T, ): Promise { const scope: DiagnosticsScope = { + recordId: diagnosticsStorage.getStore()?.recordId, ...options, diagnosticId: createDiagnosticId(), events: [], @@ -110,6 +111,7 @@ export function updateDiagnosticsScope(options: DiagnosticsScopeOptions): void { export function getDiagnosticsMeta(): { diagnosticId?: string; requestId?: string; + recordId?: string; session?: string; command?: string; debug?: boolean; @@ -117,8 +119,8 @@ export function getDiagnosticsMeta(): { } { const scope = diagnosticsStorage.getStore(); if (!scope) return {}; - const { diagnosticId, requestId, session, command, debug, flushOnSuccess } = scope; - return { diagnosticId, requestId, session, command, debug, flushOnSuccess }; + const { diagnosticId, requestId, recordId, session, command, debug, flushOnSuccess } = scope; + return { diagnosticId, requestId, recordId, session, command, debug, flushOnSuccess }; } /** Register a caller-declared literal that must not reach this request's diagnostics. */ @@ -392,17 +394,5 @@ function appendDiagnosticLine(scope: DiagnosticsScope, logPath: string, line: st fs.mkdirSync(dir, { recursive: true }); scope.ensuredDirectories.add(dir); } - if (scope.logRecord && logPath === scope.logPath && scope.logOwner && !scope.logOwnerPublished) { - fs.appendFileSync(diagnosticsRecordOwnersPath(logPath), `${scope.logOwner}\n`, 'utf8'); - scope.logOwnerPublished = true; - } fs.appendFileSync(logPath, line, 'utf8'); } - -/** - * Where the owners of the record at `logPath` are kept, one line for each scope that wrote to the - * record with an owner. No reader is served this file. - */ -export function diagnosticsRecordOwnersPath(logPath: string): string { - return `${logPath}.owners`; -} diff --git a/src/__tests__/daemon-client-abort.test.ts b/src/__tests__/daemon-client-abort.test.ts index fb1279c99e..812346f495 100644 --- a/src/__tests__/daemon-client-abort.test.ts +++ b/src/__tests__/daemon-client-abort.test.ts @@ -5,11 +5,9 @@ import { createAgentDeviceClient } from '@agent-device/client'; import { startPublicDaemonRuntime } from './test-utils/public-daemon-runtime.ts'; import { getRequestSignal } from '@agent-device/host-kit/request'; import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution'; -import { - resolveSessionDir, - resolveSessionRequestLogPath, -} from '@agent-device/daemon-contracts/session-artifact-paths'; +import { resolveSessionDir } from '@agent-device/daemon-contracts/session-artifact-paths'; import fs from 'node:fs'; +import path from 'node:path'; import { skipWhenLoopbackUnavailable, type SkippableTestContext } from '@agent-device/test-kit'; async function waitFor(condition: () => boolean, what: string): Promise { @@ -63,8 +61,14 @@ async function runAlreadyAborted( resolveDaemonPaths(daemon.stateDir).sessionsDir, 'default', ); - assert.equal(fs.existsSync(resolveSessionRequestLogPath(sessionDir, followUpRequestId)), true); - assert.equal(fs.existsSync(resolveSessionRequestLogPath(sessionDir, requestId)), false); + // The daemon names each record. The request id is in each line of it. + const requestsDir = path.join(sessionDir, 'requests'); + const records = fs + .readdirSync(requestsDir) + .map((name) => fs.readFileSync(path.join(requestsDir, name), 'utf8')) + .join(''); + assert.equal(records.includes(`"requestId":"${followUpRequestId}"`), true); + assert.equal(records.includes(`"requestId":"${requestId}"`), false); } finally { await daemon.close(); } diff --git a/src/commands/schema/cli-help-workflows.test.ts b/src/commands/schema/cli-help-workflows.test.ts index 4ff294a20d..da04275a6c 100644 --- a/src/commands/schema/cli-help-workflows.test.ts +++ b/src/commands/schema/cli-help-workflows.test.ts @@ -28,7 +28,7 @@ test('usageForCommand resolves debugging help topic', async () => { ); assert.match(help, /open --debug --json/); assert.match(help, /open_timing event/); - assert.match(help, /requests\/\.ndjson holds daemon request diagnostics/); + assert.match(help, /requests\/\.ndjson holds daemon request diagnostics/); assert.match(help, /daemon\.log is global daemon lifecycle evidence/); assert.match(help, /agent-device perf memory sample --json/); assert.match(help, /agent-device audio probe start 10 1000 --platform web/); diff --git a/src/commands/schema/cli-help-workflows.ts b/src/commands/schema/cli-help-workflows.ts index 113d2c5276..66e7dc17f4 100644 --- a/src/commands/schema/cli-help-workflows.ts +++ b/src/commands/schema/cli-help-workflows.ts @@ -141,7 +141,7 @@ Diagnostics and traces: Use AGENT_DEVICE_EXEC_TRACE=1 when you need host-tool spawn timing without full debug streaming; it flushes the full request diagnostics file on successful requests that spawn host tools. Open output includes Session state; JSON also includes runnerLogPath and requestLogPath. For open timing under --debug, run open --debug --json and inspect requestLogPath for the open_timing event. - Session requests/.ndjson holds daemon request diagnostics; session runner.log holds Apple runner/xcodebuild output. + Session requests/.ndjson holds daemon request diagnostics, one file per request, named by the daemon (a failed command prints its path; each line carries the request id); session runner.log holds Apple runner/xcodebuild output. Against a remote daemon the Log path is always local: the failing request's record is fetched to /remote-diagnostics/, and if it cannot be fetched the line reads "Log: unavailable" with the reason instead of a daemon-host path. daemon.log is global daemon lifecycle evidence, not the primary per-run log. Use trace for low-level session diagnostics around one repro: diff --git a/test/integration/provider-scenarios/active-session-script-publication.test.ts b/test/integration/provider-scenarios/active-session-script-publication.test.ts index eb7b8b1001..f1fe2a5f92 100644 --- a/test/integration/provider-scenarios/active-session-script-publication.test.ts +++ b/test/integration/provider-scenarios/active-session-script-publication.test.ts @@ -358,10 +358,13 @@ test('parameterized fill publishes only ${VAR} and replay resolves it immediatel assertRpcOk(replay); assert.equal(world.textInjectionCalls.at(-1)?.text, secret); assert.ok(opened.sessionStateDir); - const requestLog = fs.readFileSync( - path.join(opened.sessionStateDir, 'requests', 'parameterized-replay-diagnostics.ndjson'), - 'utf8', - ); + // The daemon names the record. The request id is in each line of it. + const requestsDir = path.join(opened.sessionStateDir, 'requests'); + const requestLog = fs + .readdirSync(requestsDir) + .map((name) => fs.readFileSync(path.join(requestsDir, name), 'utf8')) + .filter((log) => log.includes('"requestId":"parameterized-replay-diagnostics"')) + .join(''); assert.match(requestLog, /provider_text_echo_regression/); assert.match(requestLog, /\[REDACTED\]/); assert.equal(requestLog.includes(secret), false); diff --git a/test/integration/provider-scenarios/remote-proxy-request-diagnostics.test.ts b/test/integration/provider-scenarios/remote-proxy-request-diagnostics.test.ts index 51eb05e329..139747ff4c 100644 --- a/test/integration/provider-scenarios/remote-proxy-request-diagnostics.test.ts +++ b/test/integration/provider-scenarios/remote-proxy-request-diagnostics.test.ts @@ -35,10 +35,10 @@ test('Provider-backed integration local proxy serves the daemon diagnostics reco assert.equal(failed.statusCode, 404, JSON.stringify(failed.json)); const error = failed.json.error?.data as DaemonError | undefined; assert.equal(error?.code, 'SESSION_NOT_FOUND', JSON.stringify(failed.json)); - assert.deepEqual(error?.diagnosticsRecord, { - session: PLAIN_SESSION, - requestId: REQUEST_ID, - }); + // The daemon names the record. The request sent no lease id, so the record has no owner. + const recordId = error?.diagnosticsRecord?.requestId ?? ''; + assert.equal(error?.diagnosticsRecord?.session, PLAIN_SESSION); + assert.match(recordId, /^[a-f0-9]{32}-unleased$/); const throughProxy = await localizeRemoteDaemonError(error!, { endpoint: { baseUrl: proxy.baseUrl, token: PROXY_TOKEN }, @@ -50,7 +50,7 @@ test('Provider-backed integration local proxy serves the daemon diagnostics reco assert.match(proxyRecord, /"phase":"request_start"/); assert.match(proxyRecord, /"phase":"request_failed"/); - const expectedPath = `/sessions/${encodeURIComponent(PLAIN_SESSION)}/requests/${REQUEST_ID}/diagnostics`; + const expectedPath = `/sessions/${encodeURIComponent(PLAIN_SESSION)}/requests/${recordId}/diagnostics`; const forwardedRequests = proxy.forwarded.map((entry) => [ entry.method, entry.route, diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index 0f2754f29d..c8219950f5 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -86,7 +86,7 @@ "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:cd2c0dda77d2bc434857aea07206a5491ce5f0b82cb86628569f607d6c2cebcf", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a6c3bebf32340694f774872bbaea1becbd9fa8294a6895b9f5fa36e25ac368b4", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:d885657c040b91b4ae883cee71b0cec6bb8dce11dab918ab4ccf1ef5802b394a", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", @@ -315,8 +315,8 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:a6c3bebf32340694f774872bbaea1becbd9fa8294a6895b9f5fa36e25ac368b4", - "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. A record of a request that sent a lease id needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. The owner is read from a file beside the record that no route serves. Released clients send the header and treat any error status as an unavailable record. The 200 response serves the bytes that the lease check read; an empty record answers 404." + "digest": "sha256:d885657c040b91b4ae883cee71b0cec6bb8dce11dab918ab4ccf1ef5802b394a", + "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. The daemon now chooses the requestId segment of the locator it returns in diagnosticsRecord, and that id carries the owner: the lease id the request sent, or that it sent none. A released client uses the locator as an opaque value for the URL and for its local file name. A record of a request that sent a lease id needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients send the header and treat any error status as an unavailable record. A locator that the daemon did not choose, such as one from an earlier release, answers the existing 404." }, { "declaration": "packages/kernel/src/contracts.ts#LEASE_BACKENDS", diff --git a/website/docs/docs/debugging-profiling.md b/website/docs/docs/debugging-profiling.md index 7dcf8071db..470cd093c7 100644 --- a/website/docs/docs/debugging-profiling.md +++ b/website/docs/docs/debugging-profiling.md @@ -136,7 +136,7 @@ When a command fails, stderr starts and ends with the same summary line, so the ```text Error (COMMAND_FAILED): tap @e12: runner did not answer within 10s. May have reached the device. Next: Run snapshot to check the screen before you retry. Read the diagnostics log for the failing step. -Log: /Users/me/.agent-device/sessions//requests/.ndjson +Log: /Users/me/.agent-device/sessions//requests/.ndjson Error (COMMAND_FAILED): tap @e12: runner did not answer within 10s. May have reached the device. Next: Run snapshot to check the screen before you retry. Read the diagnostics log for the failing step. ``` diff --git a/website/docs/docs/sessions.md b/website/docs/docs/sessions.md index 33f4c212e1..06aa75ef74 100644 --- a/website/docs/docs/sessions.md +++ b/website/docs/docs/sessions.md @@ -28,7 +28,7 @@ JSON output also includes `runnerLogPath` and `requestLogPath` when available. Each session artifact directory contains: -- `requests/.ndjson` - daemon request diagnostics for this session. +- `requests/.ndjson` - daemon request diagnostics for this session, one file per request. The daemon names the file, a failed command prints its path, and each line carries the request id. - `events.ndjson` - session event timeline for requests and recorded actions; rotates to `events.ndjson.1` before an entry would exceed 5 MiB (`AGENT_DEVICE_EVENT_LOG_MAX_BYTES`, whole bytes), with `events.ndjson.window.json` tracking the retained files so `events` cursors within the retained window stay valid across rotation. Older cursors expire. - `runner.log` - Apple runner and `xcodebuild` build/start output for this session. - `app.log` - app/device logs when `logs start` or `logs clear --restart` is active. From 35b4594e9af3c2b5e295c50f06aca650a824a1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 10:58:17 +0100 Subject: [PATCH 05/15] fix(daemon): redact any sent lease id from its record and answer unknown records 404 in one place --- .../daemon/src/resources/artifact-tracking.ts | 7 ++-- .../request-diagnostics-http.test.ts | 10 +++--- .../src/server/request-diagnostics-http.ts | 33 ++++++++++--------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 8654bbf77e..de02f3ff96 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -75,10 +75,11 @@ const DIAGNOSTICS_RECORD_ID = new RegExp(`^[a-f0-9]{32}-(${UNLEASED_RECORD}|[a-f export function claimRequestDiagnosticsRecord(req: Pick): void { const leaseId = resolveLeaseScope(req).leaseId; // A lease id that reaches a message, and not a lease field, is removed from the record too. - const canonical = normalizeLeaseId(leaseId); - if (leaseId !== undefined && canonical) { - registerDiagnosticSensitiveValue(canonical); + if (leaseId !== undefined) { + // A malformed id is no credential, but the record still never echoes what the caller sent. registerDiagnosticSensitiveValue(leaseId.trim()); + const canonical = normalizeLeaseId(leaseId); + if (canonical) registerDiagnosticSensitiveValue(canonical); } if (getDiagnosticsMeta().recordId) return; const owner = leaseId === undefined ? UNLEASED_RECORD : leaseVerifier(leaseId); diff --git a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts index fa55c8d68e..7742dc6e8b 100644 --- a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts +++ b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts @@ -162,10 +162,12 @@ test('request diagnostics route does not serve a record that an earlier release await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { // Its name carries no owner, so nobody is known to own it. writeRecord(sessionsDir, 'default', 'abc123'); - const legacy = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { - headers: { authorization: 'Bearer daemon-secret' }, - }); - assert.equal(legacy.status, 404); + for (const leaseHeader of [{}, { 'x-agent-device-lease-id': 'b'.repeat(32) }]) { + const legacy = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { + headers: { authorization: 'Bearer daemon-secret', ...leaseHeader }, + }); + assert.equal(legacy.status, 404); + } }); }); diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index 19dc7ff4d6..da7b7e8d67 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -141,29 +141,22 @@ async function handleRequestDiagnostics( } const owner = diagnosticsRecordOwner(ref.requestId); + // A record of unknown ownership, such as one that an earlier release wrote, is not served. + if (owner === null) { + sendRestJsonError(res, normalizeError(missingRecordError(ref))); + return; + } const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; - if (owner !== null && !requestHoldsOutputLease(owner, presented)) { + if (!requestHoldsOutputLease(owner, presented)) { sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); return; } const recordPath = resolveRecordPath(ref); - // A record of unknown ownership, such as one that an earlier release wrote, is not served. - const stats = owner === null ? null : await statRecord(recordPath); + const stats = await statRecord(recordPath); if (!stats) { - sendRestJsonError( - res, - normalizeError( - new AppError( - 'SESSION_NOT_FOUND', - `No diagnostics record for request ${ref.requestId} in session ${ref.session}`, - { - hint: 'The daemon keeps a request record only for the session it ran in; re-run the command against this daemon to produce a fresh one.', - }, - ), - ), - ); + sendRestJsonError(res, normalizeError(missingRecordError(ref))); return; } @@ -188,3 +181,13 @@ async function statRecord(recordPath: string): Promise { return null; } } + +function missingRecordError(ref: { session: string; requestId: string }): AppError { + return new AppError( + 'SESSION_NOT_FOUND', + `No diagnostics record for request ${ref.requestId} in session ${ref.session}`, + { + hint: 'The daemon keeps a request record only for the session it ran in; re-run the command against this daemon to produce a fresh one.', + }, + ); +} From d50046d2f9cfdcdfd2486a6e933de8a942e77972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 11:02:07 +0100 Subject: [PATCH 06/15] fix(daemon): type the legacy diagnostics headers and refresh the route's wire acknowledgment --- .../src/server/__tests__/request-diagnostics-http.test.ts | 5 ++++- test/wire-compat/ledger.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts index 7742dc6e8b..d85fdc96c1 100644 --- a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts +++ b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts @@ -162,7 +162,10 @@ test('request diagnostics route does not serve a record that an earlier release await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { // Its name carries no owner, so nobody is known to own it. writeRecord(sessionsDir, 'default', 'abc123'); - for (const leaseHeader of [{}, { 'x-agent-device-lease-id': 'b'.repeat(32) }]) { + for (const leaseHeader of [{}, { 'x-agent-device-lease-id': 'b'.repeat(32) }] as Record< + string, + string + >[]) { const legacy = await fetch(diagnosticsUrl(baseUrl, 'default', 'abc123'), { headers: { authorization: 'Bearer daemon-secret', ...leaseHeader }, }); diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index c8219950f5..671f434727 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -315,7 +315,7 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:d885657c040b91b4ae883cee71b0cec6bb8dce11dab918ab4ccf1ef5802b394a", + "digest": "sha256:3d3223a0f05c50b07ae4988cccbd9638aea421753b2c5f4c550a60b8c0100e8f", "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. The daemon now chooses the requestId segment of the locator it returns in diagnosticsRecord, and that id carries the owner: the lease id the request sent, or that it sent none. A released client uses the locator as an opaque value for the URL and for its local file name. A record of a request that sent a lease id needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients send the header and treat any error status as an unavailable record. A locator that the daemon did not choose, such as one from an earlier release, answers the existing 404." }, { From d10cdbd4470622711a6873307b7df592fd4b8a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 11:07:15 +0100 Subject: [PATCH 07/15] test(wire-compat): record the diagnostics route digest in the declaration map --- test/wire-compat/ledger.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index 671f434727..86d305abcd 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -86,7 +86,7 @@ "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:cd2c0dda77d2bc434857aea07206a5491ce5f0b82cb86628569f607d6c2cebcf", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:d885657c040b91b4ae883cee71b0cec6bb8dce11dab918ab4ccf1ef5802b394a", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:3d3223a0f05c50b07ae4988cccbd9638aea421753b2c5f4c550a60b8c0100e8f", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", From 2193a50cfecef59ca3b4cd411f413adabe3ecc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 12:27:27 +0100 Subject: [PATCH 08/15] fix(daemon): isolate published captures and preserve producer ownership --- .../src/session-artifact-paths.ts | 6 +- .../docs/remote-device-lease-contracts.md | 9 +- .../src/core/capture-output-ownership.ts | 8 +- .../execution/screenshot-artifact-adapter.ts | 30 +++- .../src/execution/screenshot-runtime.ts | 94 ++++++++--- .../daemon/src/resources/artifact-tracking.ts | 22 ++- .../__tests__/http-server-artifacts.test.ts | 21 ++- .../http-server-tenant-trust.test.ts | 4 +- .../request-diagnostics-http.test.ts | 27 ++- .../__tests__/request-execution-scope.test.ts | 3 +- ...router-android-foreground-evidence.test.ts | 2 +- .../__tests__/request-router-open.test.ts | 2 +- .../daemon/src/server/http/daemon-runtime.ts | 3 +- .../src/server/http/http-output.fixtures.ts | 68 ++++++++ .../http-server-capture-publication.test.ts | 157 ++++++++++++++++++ .../http-server-proxy-leased-output.test.ts | 52 +++++- .../http/http-server-remote-output.test.ts | 89 ++-------- .../daemon/src/server/http/http-server.ts | 5 +- .../src/server/request-diagnostics-http.ts | 18 +- .../src/server/request-execution-scope.ts | 1 + .../daemon/src/server/request-finalization.ts | 9 +- .../leased-recording-retention.test.ts | 36 +++- .../handlers/record-runtime-response.ts | 13 ++ .../src/sessions/handlers/record-runtime.ts | 35 ++-- .../sessions/leased-recording-retention.ts | 16 +- .../internal/session-open-execution.ts | 1 + .../internal/session-perf-runtime.ts | 54 +++++- .../daemon/src/sessions/session-teardown.ts | 3 +- src/__tests__/cli-remote-diagnostics.test.ts | 2 +- src/__tests__/daemon-client-abort.test.ts | 2 +- src/commands/schema/cli-help-workflows.ts | 2 +- .../active-session-script-publication.test.ts | 2 +- .../remote-proxy-parity.test.ts | 4 +- .../support/daemon-leak-model.test.ts | 14 +- test/integration/support/daemon-leak-model.ts | 2 +- test/wire-compat/ledger.json | 13 +- website/docs/docs/debugging-profiling.md | 6 +- website/docs/docs/sessions.md | 4 +- 38 files changed, 643 insertions(+), 196 deletions(-) create mode 100644 packages/daemon/src/server/http/http-output.fixtures.ts create mode 100644 packages/daemon/src/server/http/http-server-capture-publication.test.ts diff --git a/packages/daemon-contracts/src/session-artifact-paths.ts b/packages/daemon-contracts/src/session-artifact-paths.ts index 26ee8f41b0..fdc06363e8 100644 --- a/packages/daemon-contracts/src/session-artifact-paths.ts +++ b/packages/daemon-contracts/src/session-artifact-paths.ts @@ -18,9 +18,10 @@ export function resolveSessionTestArtifactsRoot(sessionDir: string): string { export function resolveSessionRequestLogPath( sessionDir: string, requestId: string | undefined, + directory: 'requests' | 'request-records' | 'requests-unleased' = 'requests', ): string { const safeRequestId = safeSessionName(requestId && requestId.length > 0 ? requestId : 'unknown'); - return path.join(sessionDir, 'requests', `${safeRequestId}.ndjson`); + return path.join(sessionDir, directory, `${safeRequestId}.ndjson`); } /** @@ -32,9 +33,10 @@ export function resolveSessionRequestLog(params: { sessionDir: string; session: string; requestId: string | undefined; + directory?: 'request-records'; }): { path: string; ref: DiagnosticsRecordRef } { return { - path: resolveSessionRequestLogPath(params.sessionDir, params.requestId), + path: resolveSessionRequestLogPath(params.sessionDir, params.requestId, params.directory), ref: { session: params.session, requestId: params.requestId && params.requestId.length > 0 ? params.requestId : 'unknown', diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 7f625594a1..956f725e79 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -93,11 +93,11 @@ The `artifacts` RPC reads the lease id from the request. `GET /artifacts`, `GET This rule applies for as long as the daemon keeps the output, also after the lease ends. The lifetime of the output does not change. An inventory omits an artifact of another lease. A download or a diagnostics read without the lease id fails with `UNAUTHORIZED`, reason `LEASE_OUTPUT_MISMATCH`, and `field: 'leaseId'`. -An artifact id serves only the output that it registered. If another capture is written to the same path, that capture gets its own id, and the earlier id expires. +A downloadable screenshot or xctrace result has its own daemon-owned path. A write to the caller's destination does not change an earlier download. The daemon names each request diagnostics record. The name carries the owner of the record: the lease id that the request sent, or that it sent none. -The request id of the caller names no record. Thus two requests do not share a record, and a record has one owner or none. +The request id of the caller names no record. A nested request uses the parent's record only when both requests send the same lease id or both send no lease id. A caller that sent a lease id reads only its own records, also the record of a request that a leased session refused. -The diagnostics route does not serve a record that an earlier release named, because the owner of that record is not known. +New records are stored in the session's `request-records` directory. The diagnostics route serves legacy records only from the host-verified directory. A recording manifest keeps the lease that started the recording, or that no lease started it. `record stop` returns a completed export to a request that sends that lease id, also after the lease ended. Such a request needs no active lease. A recording that still runs needs a request that is admitted under that lease. @@ -107,6 +107,9 @@ A request diagnostics record names a lease by the first 8 hex digits of that SHA An event that the daemon writes outside a request goes to the daemon log, which only the host operator reads. It names the lease id, because the operator needs it to end a rented device. Output of a request that acted under no lease keeps the tenant rule. +Before you restore access to a legacy diagnostics record, verify its full contents came from unleased requests and contain no lease credentials. Move the file from the session's `requests` directory to its `requests-unleased` directory. Keep its filename. +Before you restore access to a legacy recording, verify that the producing request was unleased. Set `metadata.remoteCaptureLease` to `"unleased"` in its recording manifest. + ## Admitted request work A lease renews when a request is admitted, and never again while that request runs. diff --git a/packages/daemon/src/core/capture-output-ownership.ts b/packages/daemon/src/core/capture-output-ownership.ts index 7b03ab970b..bc7cdc6a9b 100644 --- a/packages/daemon/src/core/capture-output-ownership.ts +++ b/packages/daemon/src/core/capture-output-ownership.ts @@ -5,8 +5,8 @@ type CaptureOutputOwnership = 'daemon' | 'caller'; type CaptureOutputOwner = Readonly<{ ownership: CaptureOutputOwnership; tenantId?: string; - /** The verifier of the lease that produced the capture, when it is not the lease of this request. */ - leaseVerifier?: string; + /** Null records an unleased producer. Undefined leaves ownership to the request. */ + leaseVerifier?: string | null; }>; type CaptureOutputData = DaemonResponseData & { [CAPTURE_OUTPUT_OWNERSHIP]?: CaptureOutputOwner; @@ -17,7 +17,7 @@ export function withCaptureOutputOwnership( data: DaemonResponseData, ownership: CaptureOutputOwnership, tenantId?: string, - leaseVerifier?: string, + leaseVerifier?: string | null, ): DaemonResponseData { return { ...data, [CAPTURE_OUTPUT_OWNERSHIP]: { ownership, tenantId, leaseVerifier } }; } @@ -34,7 +34,7 @@ export function captureOutputTenantId(data: DaemonResponseData): string | undefi return capture[CAPTURE_OUTPUT_OWNERSHIP]?.tenantId; } -export function captureOutputLeaseVerifier(data: DaemonResponseData): string | undefined { +export function captureOutputLeaseVerifier(data: DaemonResponseData): string | null | undefined { const capture: CaptureOutputData = data; return capture[CAPTURE_OUTPUT_OWNERSHIP]?.leaseVerifier; } diff --git a/packages/daemon/src/execution/screenshot-artifact-adapter.ts b/packages/daemon/src/execution/screenshot-artifact-adapter.ts index 43086e8d61..dc3d19c79a 100644 --- a/packages/daemon/src/execution/screenshot-artifact-adapter.ts +++ b/packages/daemon/src/execution/screenshot-artifact-adapter.ts @@ -95,9 +95,7 @@ async function reserveCroppedOutput( visibility: 'client-visible', publish: async () => { try { - const contents = await fs.readFile(stagingPath); - signal?.throwIfAborted(); - publishFileSync({ destination, contents, preserveExistingMode: true }); + await writeScreenshotFile(stagingPath, destination, signal); } catch (error) { if (signal?.aborted) throw signal.reason; throw screenshotPublicationError(error, outputPath); @@ -109,6 +107,32 @@ async function reserveCroppedOutput( }; } +export async function publishScreenshotFile( + source: string, + outputPath: string, + signal: AbortSignal | undefined, +): Promise { + try { + const destination = await resolveScreenshotDestination(outputPath); + await fs.mkdir(path.dirname(destination), { recursive: true }); + await writeScreenshotFile(source, destination, signal); + } catch (error) { + if (signal?.aborted) throw signal.reason; + if (error instanceof AppError) throw error; + throw screenshotPublicationError(error, outputPath); + } +} + +async function writeScreenshotFile( + source: string, + destination: string, + signal: AbortSignal | undefined, +): Promise { + const contents = await fs.readFile(source); + signal?.throwIfAborted(); + publishFileSync({ destination, contents, preserveExistingMode: true }); +} + async function resolveScreenshotDestination(outputPath: string): Promise { let destination: string; try { diff --git a/packages/daemon/src/execution/screenshot-runtime.ts b/packages/daemon/src/execution/screenshot-runtime.ts index ab094426c2..f1fb73a4ce 100644 --- a/packages/daemon/src/execution/screenshot-runtime.ts +++ b/packages/daemon/src/execution/screenshot-runtime.ts @@ -1,4 +1,11 @@ -import { createDaemonScreenshotArtifactAdapter } from './screenshot-artifact-adapter.ts'; +import { randomUUID } from 'node:crypto'; +import { rm } from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import { + createDaemonScreenshotArtifactAdapter, + publishScreenshotFile, +} from './screenshot-artifact-adapter.ts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import type { CommandFlags } from '@agent-device/contracts/command'; import { @@ -103,6 +110,10 @@ export async function resolveScreenshotGenericExecution( dispatchContext: execution.dispatchContext, flags: execution.request.flags, outPath: request.outPath, + clientPath: req.meta?.clientArtifactPaths?.path, + downloadable: Boolean( + req.internal?.publicNetworkOnly || req.meta?.clientArtifactPaths?.path, + ), runtime, cropOn, cropRun, @@ -193,6 +204,8 @@ async function executeScreenshot( dispatchContext: DaemonCommandContext; flags: CommandFlags | undefined; outPath: string | undefined; + clientPath: string | undefined; + downloadable: boolean; runtime: BoundScreenshotRuntime; cropOn?: string; cropRun?: ScreenshotCropRun; @@ -205,34 +218,61 @@ async function executeScreenshot( cropRun: params.cropRun, logPath: params.logPath, }); - const captured = await captureScreenshotArtifact({ - createCommandSurface: params.createCommandSurface, - session, - sessionName: params.sessionName, - outPath: params.outPath, - dispatchContext: params.dispatchContext, - captureScreenshot: runtime.captureScreenshot, - ...(crop ? { crop } : {}), - }); - const warnings = [...(captured.warnings ?? []), ...(crop?.run?.warnings ?? [])]; - return { - ...captured, - ...(warnings.length > 0 ? { warnings } : {}), - ...(await screenshotOverlayRefsField({ + const publishedPath = params.outPath; + const privatePath = + params.downloadable && publishedPath + ? path.join(os.tmpdir(), `agent-device-screenshot-${randomUUID()}.png`) + : undefined; + try { + const captured = await captureScreenshotArtifact({ + createCommandSurface: params.createCommandSurface, session, - cropOn: params.cropOn, - captureSnapshot: runtime.captureSnapshot, - screenshotPath: captured.path, - logPath: params.logPath, + sessionName: params.sessionName, + outPath: privatePath ?? publishedPath, dispatchContext: params.dispatchContext, - })), - ...(await readScreenshotResultMetadata({ - device: session.device, - path: captured.path, - requestedPixelDensity: flags?.screenshotPixelDensity, - scale: flags?.screenshotScale, - })), - }; + captureScreenshot: runtime.captureScreenshot, + ...(crop ? { crop } : {}), + }); + const warnings = [...(captured.warnings ?? []), ...(crop?.run?.warnings ?? [])]; + const result = { + ...captured, + ...(warnings.length > 0 ? { warnings } : {}), + ...(await screenshotOverlayRefsField({ + session, + cropOn: params.cropOn, + captureSnapshot: runtime.captureSnapshot, + screenshotPath: captured.path, + logPath: params.logPath, + dispatchContext: params.dispatchContext, + })), + ...(await readScreenshotResultMetadata({ + device: session.device, + path: captured.path, + requestedPixelDensity: flags?.screenshotPixelDensity, + scale: flags?.screenshotScale, + })), + }; + if (!privatePath || !publishedPath) return result; + await publishScreenshotFile(privatePath, publishedPath, params.dispatchContext.signal); + return { + ...result, + path: publishedPath, + ...(result.message === undefined ? {} : { message: `Saved screenshot: ${publishedPath}` }), + artifacts: [ + { + field: 'path', + artifactType: 'screenshot', + path: privatePath, + localPath: params.clientPath, + fileName: path.basename(params.clientPath ?? publishedPath), + displayRotation: result.displayRotation, + }, + ], + }; + } catch (error) { + if (privatePath) await rm(privatePath, { force: true }); + throw error; + } } /** Present exactly when the admitted plan carries a crop and the snapshot binding to serve it. */ diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index de02f3ff96..a17d0ec112 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -81,8 +81,10 @@ export function claimRequestDiagnosticsRecord(req: Pick { + const root = mkdtempForTestSync('stale-artifact-'); + const artifactPath = path.join(root, 'artifact.png'); + fs.writeFileSync(artifactPath, 'old'); + const id = trackDownloadableArtifact({ artifactPath, artifactType: 'screenshot' }); + const next = path.join(root, 'replacement.png'); + fs.writeFileSync(next, 'replacement'); + fs.renameSync(next, artifactPath); + try { + expect(await listDownloadableArtifacts({}, {})).not.toContainEqual( + expect.objectContaining({ id }), + ); + expect(fs.readFileSync(artifactPath, 'utf8')).toBe('replacement'); + } finally { + cleanupDownloadableArtifact(id); + fs.rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts b/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts index 9da09239fd..31b7c6efc3 100644 --- a/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts +++ b/packages/daemon/src/server/__tests__/http-server-tenant-trust.test.ts @@ -91,10 +91,11 @@ async function withDiagnosticsHookServer( const server = await createDaemonHttpServer({ token: DAEMON_TOKEN, handleRequest: async (): Promise => ({ ok: true, data: {} }), - resolveRequestDiagnosticsPath: (ref) => + resolveRequestDiagnosticsPath: (ref, directory) => resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(ref.session)), ref.requestId, + directory, ), env: process.env, cwd: process.cwd(), @@ -115,6 +116,7 @@ function writeDiagnosticsRecord(sessionsDir: string, session: string, requestId: const recordPath = resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(session)), requestId, + 'request-records', ); fs.mkdirSync(path.dirname(recordPath), { recursive: true }); fs.writeFileSync(recordPath, DIAGNOSTICS_RECORD); diff --git a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts index d85fdc96c1..672a2984cc 100644 --- a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts +++ b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts @@ -20,10 +20,16 @@ const RECORD = '{"phase":"request_start"}\n{"phase":"request_failed"}\n'; // A record id as the daemon chooses it for a request that sent no lease id. const RECORD_ID = `${'a'.repeat(32)}-unleased`; -function writeRecord(sessionsDir: string, session: string, requestId: string): string { +function writeRecord( + sessionsDir: string, + session: string, + requestId: string, + directory: Parameters[2] = 'request-records', +): string { const recordPath = resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(session)), requestId, + directory, ); fs.mkdirSync(path.dirname(recordPath), { recursive: true }); fs.writeFileSync(recordPath, RECORD); @@ -40,10 +46,11 @@ async function withDiagnosticsServer( token: 'daemon-secret', leaseRegistry, handleRequest: async (): Promise => ({ ok: true, data: {} }), - resolveRequestDiagnosticsPath: (ref) => + resolveRequestDiagnosticsPath: (ref, directory) => resolveSessionRequestLogPath( path.join(sessionsDir, safeSessionName(ref.session)), ref.requestId, + directory, ), env: process.env, cwd: process.cwd(), @@ -290,3 +297,19 @@ test('request diagnostics route does not serve a tenant that held a macos-app le assert.equal(other.status, 200); }, leaseRegistry); }); + +test('legacy diagnostics are served only from the host-verified namespace', async (t) => { + if (await skipWhenLoopbackUnavailable(t)) return; + await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { + writeRecord(sessionsDir, 'default', RECORD_ID, 'requests'); + writeRecord(sessionsDir, 'default', 'verified-legacy', 'requests-unleased'); + const headers = { authorization: 'Bearer daemon-secret' }; + const unknown = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers }); + expect(unknown.status).toBe(404); + const verified = await fetch(diagnosticsUrl(baseUrl, 'default', 'verified-legacy'), { + headers, + }); + expect(verified.status).toBe(200); + expect(await verified.text()).toBe(RECORD); + }); +}); diff --git a/packages/daemon/src/server/__tests__/request-execution-scope.test.ts b/packages/daemon/src/server/__tests__/request-execution-scope.test.ts index 44c9655453..b67a0e52ec 100644 --- a/packages/daemon/src/server/__tests__/request-execution-scope.test.ts +++ b/packages/daemon/src/server/__tests__/request-execution-scope.test.ts @@ -123,7 +123,7 @@ test('createRequestExecutionScope resolves session-scoped request and runner log expect(scope.sessionName).toMatch(/^cwd:[a-f0-9]{16}:default$/); expect(scope.requestLogPath).toMatch( - /cwd_[a-f0-9]{16}_default\/requests\/request-logs-1\.ndjson$/, + /cwd_[a-f0-9]{16}_default\/request-records\/request-logs-1\.ndjson$/, ); expect(scope.runnerLogPath).toMatch(/cwd_[a-f0-9]{16}_default\/runner\.log$/); }); @@ -640,6 +640,7 @@ test('tenant lease rejection flushes diagnostics into the effective session requ const expectedPath = resolveSessionRequestLogPath( sessionStore.resolveSessionDir('tenant-a:default'), requestId, + 'request-records', ); expect(flushedPath).toBe(expectedPath); expect(fs.readFileSync(expectedPath, 'utf8')).toContain('"phase":"request_start"'); diff --git a/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts b/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts index f6606412e8..2af777853d 100644 --- a/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-android-foreground-evidence.test.ts @@ -72,7 +72,7 @@ test('open relaunch retains Android foreground probe failure in the serialized r sessionStore.resolveDaemonStateDir(), 'sessions', 'fence-session', - 'requests', + 'request-records', `${response.error.diagnosticsRecord?.requestId}.ndjson`, ), ); diff --git a/packages/daemon/src/server/__tests__/request-router-open.test.ts b/packages/daemon/src/server/__tests__/request-router-open.test.ts index b71c32380b..a287a19694 100644 --- a/packages/daemon/src/server/__tests__/request-router-open.test.ts +++ b/packages/daemon/src/server/__tests__/request-router-open.test.ts @@ -147,7 +147,7 @@ test('open returns and creates the session state directory', async () => { path.join(String(response.data?.sessionStateDir), 'runner.log'), ); expect(path.dirname(String(response.data?.requestLogPath))).toEqual( - path.join(String(response.data?.sessionStateDir), 'requests'), + path.join(String(response.data?.sessionStateDir), 'request-records'), ); expect(path.basename(String(response.data?.requestLogPath))).toMatch( /^[a-f0-9]{32}-unleased\.ndjson$/, diff --git a/packages/daemon/src/server/http/daemon-runtime.ts b/packages/daemon/src/server/http/daemon-runtime.ts index 2726450ec2..7ddff64e43 100644 --- a/packages/daemon/src/server/http/daemon-runtime.ts +++ b/packages/daemon/src/server/http/daemon-runtime.ts @@ -655,10 +655,11 @@ async function startDaemonRuntimeOnHost( // locator so a remote caller can fetch what it cannot read by path. resolveRequestDiagnosticsPath: daemonPolicy?.requiredLeaseBackend ? undefined - : (ref) => + : (ref, directory) => resolveSessionRequestLogPath( sessionStore.resolveSessionDir(ref.session), ref.requestId, + directory, ), }, rollback, diff --git a/packages/daemon/src/server/http/http-output.fixtures.ts b/packages/daemon/src/server/http/http-output.fixtures.ts new file mode 100644 index 0000000000..7c329dc664 --- /dev/null +++ b/packages/daemon/src/server/http/http-output.fixtures.ts @@ -0,0 +1,68 @@ +import { expect } from 'vitest'; +import type { DaemonResponse, DaemonResponseData } from '@agent-device/kernel/contracts'; +import { + DAEMON_HTTP_NETWORK_ACCESS_HEADER, + DAEMON_HTTP_PUBLIC_NETWORK_ACCESS, +} from '@agent-device/contracts/daemon-http'; +import { closeLoopbackServer, listenOnLoopback } from '@agent-device/test-kit'; +import type { DaemonRequest } from '../../core/daemon-request.ts'; +import { createDaemonHttpServer } from './http-server.ts'; + +type RpcBody = { + result?: Extract; + error?: { data?: { code?: string; details?: { reason?: string; field?: string } } }; +}; + +export function successfulData(body: RpcBody): DaemonResponseData { + expect(body.result?.ok, JSON.stringify(body)).toBe(true); + expect(body.result!.data).toBeDefined(); + return body.result!.data!; +} + +export function localEnv(): NodeJS.ProcessEnv { + const env = { ...process.env }; + delete env.AGENT_DEVICE_HTTP_AUTH_HOOK; + delete env.AGENT_DEVICE_HTTP_AUTH_EXPORT; + return env; +} + +export async function withHttp( + handleRequest: (request: DaemonRequest) => Promise, + run: ( + post: (params: Record, remote?: boolean, tenant?: string) => Promise, + base: string, + ) => Promise, + env = localEnv(), +): Promise { + const server = await createDaemonHttpServer({ + token: 'test-token', + handleRequest, + env, + cwd: process.cwd(), + }); + try { + const base = `http://127.0.0.1:${await listenOnLoopback(server)}`; + await run(async (params, remote = true, tenant) => { + const response = await fetch(`${base}/rpc`, { + method: 'POST', + headers: { + 'content-type': 'application/json', + authorization: 'Bearer test-token', + ...(tenant ? { 'x-tenant': tenant } : {}), + ...(remote + ? { [DAEMON_HTTP_NETWORK_ACCESS_HEADER]: DAEMON_HTTP_PUBLIC_NETWORK_ACCESS } + : {}), + }, + body: JSON.stringify({ + jsonrpc: '2.0', + id: 'output', + method: 'agent-device.command', + params, + }), + }); + return (await response.json()) as RpcBody; + }, base); + } finally { + await closeLoopbackServer(server); + } +} diff --git a/packages/daemon/src/server/http/http-server-capture-publication.test.ts b/packages/daemon/src/server/http/http-server-capture-publication.test.ts new file mode 100644 index 0000000000..6a10c2976c --- /dev/null +++ b/packages/daemon/src/server/http/http-server-capture-publication.test.ts @@ -0,0 +1,157 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { expect, test } from 'vitest'; +import { PNG } from '@agent-device/capture-kit/png'; +import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store'; +import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; +import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; +import { createRequestHandler } from '../../__tests__/support/test-device-runtime-gateway.ts'; +import { makeAndroidSession } from '../../__tests__/support/session-factories.ts'; +import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; +import { screenshotRuntimeFixture } from '../../__tests__/support/screenshot-runtime-fixture.ts'; +import { screenshotCommandSurface } from '../../__tests__/support/screenshot-command-surface-fake.ts'; +import { LeaseRegistry } from '../../resources/lease-registry.ts'; +import { trackDownloadableArtifact } from '../../resources/artifact-tracking.ts'; +import { finalizeDaemonResponse } from '../request-finalization.ts'; +import { makeRecordRuntimeHarness } from '../../sessions/handlers/__tests__/record-runtime.fixtures.ts'; +import { successfulData, withHttp } from './http-output.fixtures.ts'; + +test('downloadable screenshots keep the published frame when the caller output is reused', async () => { + const root = mkdtempForTestSync('immutable-screenshot-'); + const destination = path.join(root, 'capture.png'); + const sessionStore = makeSessionStore('immutable-screenshot-session-'); + sessionStore.publish('capture', makeAndroidSession('capture')); + const png = (red: number) => { + const frame = new PNG({ width: 1, height: 1 }); + frame.data.set([red, 0, 0, 255]); + return PNG.sync.write(frame); + }; + const original = png(42); + const replacement = png(84); + const frames = [original, replacement]; + const runtime = screenshotRuntimeFixture({ + onCapture: ({ outPath }) => { + fs.writeFileSync(outPath, frames.shift()!); + }, + }); + const handler = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(root, 'daemon.log'), + token: 'test-token', + sessionStore, + leaseRegistry: new LeaseRegistry(), + deviceInventoryGateways: createTestDeviceInventoryGateways(), + deviceRuntimeGateway: runtime.gateway, + trackDownloadableArtifact, + }); + await withHttp(handler, async (post, base) => { + const capture = { command: 'screenshot', session: 'capture', positionals: [destination] }; + const first = successfulData( + await post( + { + ...capture, + meta: { clientArtifactPaths: { path: '/client/first.png' } }, + }, + false, + ), + ); + const second = successfulData(await post(capture, false)); + expect(second.path).toBe(destination); + const artifactId = first.artifacts?.[0]?.artifactId; + expect(artifactId).toBeTypeOf('string'); + const download = await fetch(`${base}/artifacts/${artifactId}`, { + headers: { authorization: 'Bearer test-token' }, + }); + expect(download.status).toBe(200); + expect(Buffer.from(await download.arrayBuffer())).toEqual(original); + expect(fs.readFileSync(destination)).toEqual(replacement); + }); +}); + +test('direct HTTP recording replay requires known remote output provenance', async () => { + const harness = makeRecordRuntimeHarness('legacy-remote-recording-'); + const start = harness.runtime.start.getMockImplementation()!; + harness.runtime.start.mockImplementation(async (input) => { + fs.writeFileSync(input.outputPath, 'recorded-video'); + return await start(input); + }); + const started = await harness.run(['start'], { + clientArtifactPaths: { outPath: '/client/recording.mp4' }, + }); + expect(started.ok).toBe(true); + expect((await harness.run(['stop'])).ok).toBe(true); + const resourcePath = screenRecordingResourceStore.resolvePath( + harness.sessionStore.resolveSessionDir(harness.sessionName), + ); + const record = screenRecordingResourceStore.read(resourcePath); + if (record.status !== 'decoded') throw new Error('Expected completed recording'); + const { remoteCaptureLease: _owner, ...metadata } = record.envelope.metadata ?? {}; + screenRecordingResourceStore.write(resourcePath, { ...record.envelope, metadata }); + await withHttp( + async (request) => + finalizeDaemonResponse( + request, + await harness.run(request.positionals, request.meta, request.flags, request.internal), + trackDownloadableArtifact, + ), + async (post, base) => { + const replay = async () => await post({ command: 'record', positionals: ['stop'] }, false); + expect((await replay()).error?.data?.details).toMatchObject({ + reason: 'LEASE_OUTPUT_MISMATCH', + }); + screenRecordingResourceStore.write(resourcePath, { + ...record.envelope, + metadata: { ...metadata, remoteCaptureLease: 'unleased' }, + }); + const data = successfulData(await replay()); + const id = data.artifacts?.[0]?.artifactId; + const download = await fetch(`${base}/artifacts/${id}`, { + headers: { authorization: 'Bearer test-token' }, + }); + expect(download.status).toBe(200); + expect(await download.text()).toBe('recorded-video'); + }, + ); +}); + +test('record stop keeps the unleased producer when the stop runs under a lease', async () => { + const harness = makeRecordRuntimeHarness('remote-recording-later-lease-'); + const start = harness.runtime.start.getMockImplementation()!; + harness.runtime.start.mockImplementation(async (input) => { + fs.writeFileSync(input.outputPath, 'unleased-video'); + return await start(input); + }); + const lease = new LeaseRegistry().allocateLease({ + tenantId: 'tenant-a', + runId: 'run-a', + leaseBackend: 'android-instance', + }); + await withHttp( + async (request) => { + const admitted = + request.positionals?.[0] === 'stop' + ? { ...request, internal: { ...request.internal, admittedLease: lease } } + : request; + return finalizeDaemonResponse( + admitted, + await harness.run(admitted.positionals, admitted.meta, admitted.flags, admitted.internal), + trackDownloadableArtifact, + ); + }, + async (post, base) => { + successfulData(await post({ command: 'record', positionals: ['start'] })); + harness.sessionStore.update(harness.sessionStore.lookup(harness.sessionName)!, { + lease: { leaseId: lease.leaseId, tenantId: lease.tenantId, runId: lease.runId }, + }); + const output = successfulData(await post({ command: 'record', positionals: ['stop'] })); + const id = output.artifacts?.[0]?.artifactId; + expect(id).toBeTypeOf('string'); + const download = await fetch(`${base}/artifacts/${id}`, { + headers: { authorization: 'Bearer test-token' }, + }); + expect(download.status).toBe(200); + expect(await download.text()).toBe('unleased-video'); + }, + ); +}); diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts index 4b69c94c7a..afef438e5a 100644 --- a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -1,3 +1,4 @@ +import fs from 'node:fs'; import path from 'node:path'; import { afterEach, beforeEach, expect, test } from 'vitest'; import { createDaemonProxyServer } from '@agent-device/proxy'; @@ -87,8 +88,12 @@ async function startProxiedDaemon() { token: 'daemon-token', handleRequest, leaseRegistry, - resolveRequestDiagnosticsPath: (ref) => - resolveSessionRequestLogPath(sessionStore.resolveSessionDir(ref.session), ref.requestId), + resolveRequestDiagnosticsPath: (ref, directory) => + resolveSessionRequestLogPath( + sessionStore.resolveSessionDir(ref.session), + ref.requestId, + directory, + ), env: {}, cwd: process.cwd(), }); @@ -108,6 +113,7 @@ async function startProxiedDaemon() { command: string, session: string, meta: Record, + flags: Record = {}, ): Promise => { const response = await fetch(`${base}/rpc`, { method: 'POST', @@ -120,7 +126,7 @@ async function startProxiedDaemon() { session, command, positionals: [], - flags: {}, + flags, meta: { tenantId: TENANT, sessionIsolation: 'tenant', sessionExplicit: true, ...meta }, }, }), @@ -291,3 +297,43 @@ test('a request with no lease on a session with no lease keeps the tenant rule', expect(own.status).toBe(200); }); + +test('a routed batch step writes a record under its own lease', async () => { + if (!world) return; + const session = 'nested'; + const batch = await world.send( + 'batch', + session, + { + sessionIsolation: undefined, + debug: true, + }, + { + batchSteps: [ + { + command: 'open', + flags: { + leaseId: world.leaseId, + runId: OWNER_SCOPE.runId, + leaseBackend: 'android-instance', + }, + positionals: ['com.example.app'], + }, + ], + }, + ); + expect(batch.error?.data?.diagnosticsRecord, JSON.stringify(batch)).toBeDefined(); + const parentRecord = batch.error!.data!.diagnosticsRecord!; + const recordDir = path.join(world.sessionStore.resolveSessionDir(session), 'request-records'); + const records = fs.readdirSync(recordDir); + const child = records.find((file) => file !== `${parentRecord.requestId}.ndjson`); + expect(child, JSON.stringify(records)).toBeDefined(); + const route = (recordId: string) => `/sessions/${session}/requests/${recordId}/diagnostics`; + await expectLeasedOutputRefusal(await world.get(route(path.basename(child!, '.ndjson')))); + const own = await world.get(route(path.basename(child!, '.ndjson')), world.leaseId); + expect(own.status).toBe(200); + expect(await own.text()).toContain('"command":"open"'); + const parent = await world.get(route(parentRecord.requestId)); + expect(parent.status).toBe(200); + expect(await parent.text()).not.toContain('"command":"open"'); +}); diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index c2cd6e3d93..9862dc09ad 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -11,26 +11,12 @@ import type { PerfNativeCaptureLiveHandle, PerfNativeCaptureSnapshot, } from '@agent-device/contracts/perf-runtime'; -import type { - DaemonArtifact, - DaemonResponse, - DaemonResponseData, -} from '@agent-device/kernel/contracts'; +import type { DaemonArtifact } from '@agent-device/kernel/contracts'; import type { DaemonRequest } from '../../core/daemon-request.ts'; -import { - DAEMON_HTTP_LEASE_HEADER, - DAEMON_HTTP_NETWORK_ACCESS_HEADER, - DAEMON_HTTP_PUBLIC_NETWORK_ACCESS, -} from '@agent-device/contracts/daemon-http'; +import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { parsePerfRuntimeRequest } from '@agent-device/capture-kit/perf-runtime-plan'; import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; -import { - closeLoopbackServer, - createTestHostEnvironment, - listenOnLoopback, - mkdtempForTestSync, -} from '@agent-device/test-kit'; -import { createDaemonHttpServer } from './http-server.ts'; +import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; import { createRequestHandler } from '../../__tests__/support/test-device-runtime-gateway.ts'; import { makeAndroidSession, @@ -45,69 +31,11 @@ import { listDownloadableArtifacts, trackDownloadableArtifact, } from '../../resources/artifact-tracking.ts'; +import { localEnv, successfulData, withHttp } from './http-output.fixtures.ts'; import { finalizeDaemonResponse } from '../request-finalization.ts'; import { runBatchCommands } from '../../sessions/handlers/session-batch.ts'; import { makeRecordRuntimeHarness } from '../../sessions/handlers/__tests__/record-runtime.fixtures.ts'; -type RpcBody = { - result?: Extract; - error?: { data?: { code?: string; details?: { reason?: string; field?: string } } }; -}; - -function successfulData(body: RpcBody): DaemonResponseData { - expect(body.result?.ok, JSON.stringify(body)).toBe(true); - expect(body.result!.data).toBeDefined(); - return body.result!.data!; -} - -function localEnv(): NodeJS.ProcessEnv { - const env = { ...process.env }; - delete env.AGENT_DEVICE_HTTP_AUTH_HOOK; - delete env.AGENT_DEVICE_HTTP_AUTH_EXPORT; - return env; -} - -async function withHttp( - handleRequest: (request: DaemonRequest) => Promise, - run: ( - post: (params: Record, remote?: boolean, tenant?: string) => Promise, - base: string, - ) => Promise, - env = localEnv(), -): Promise { - const server = await createDaemonHttpServer({ - token: 'test-token', - handleRequest, - env, - cwd: process.cwd(), - }); - try { - const base = `http://127.0.0.1:${await listenOnLoopback(server)}`; - await run(async (params, remote = true, tenant) => { - const response = await fetch(`${base}/rpc`, { - method: 'POST', - headers: { - 'content-type': 'application/json', - authorization: 'Bearer test-token', - ...(tenant ? { 'x-tenant': tenant } : {}), - ...(remote - ? { [DAEMON_HTTP_NETWORK_ACCESS_HEADER]: DAEMON_HTTP_PUBLIC_NETWORK_ACCESS } - : {}), - }, - body: JSON.stringify({ - jsonrpc: '2.0', - id: 'output', - method: 'agent-device.command', - params, - }), - }); - return (await response.json()) as RpcBody; - }, base); - } finally { - await closeLoopbackServer(server); - } -} - const outputCases = [ { command: 'screenshot', positionals: ['/host/screenshot.png'] }, { command: 'screenshot', positionals: [], flags: { out: '../screenshot.png' } }, @@ -666,6 +594,7 @@ test.each( } expect(artifact?.artifactId).toBeTypeOf('string'); const output = stoppedData.outPath as string; + const restoreProfile = replaceXctraceProfile(kind, area, output); const download = await fetch(`${baseUrl}/artifacts/${artifact?.artifactId}`, { headers: { authorization: 'Bearer test-token' }, }); @@ -676,6 +605,7 @@ test.each( ? gunzipSync(payload).includes(Buffer.from('generated-capture')) : payload.toString() === 'generated-capture', ).toBe(true); + restoreProfile(); if (area === 'cpu') { expect( fs.readFileSync(kind === 'xctrace' ? path.join(output, 'capture.bin') : output, 'utf8'), @@ -970,3 +900,10 @@ test('remote suite artifacts belong to fresh tenant/session invocations despite cwd.mockRestore(); } }); + +function replaceXctraceProfile(kind: string, area: string, output: string): () => void { + if (kind !== 'xctrace' || area !== 'cpu') return () => {}; + const profile = path.join(output, 'capture.bin'); + fs.writeFileSync(profile, 'later-profile'); + return () => fs.writeFileSync(profile, 'generated-capture'); +} diff --git a/packages/daemon/src/server/http/http-server.ts b/packages/daemon/src/server/http/http-server.ts index 2eae0ce110..ca856f1254 100644 --- a/packages/daemon/src/server/http/http-server.ts +++ b/packages/daemon/src/server/http/http-server.ts @@ -110,7 +110,10 @@ export async function createDaemonHttpServer(options: { * then does not exist and a remote caller is told the record is unavailable * rather than handed a daemon-host path. */ - resolveRequestDiagnosticsPath?: (ref: DiagnosticsRecordRef) => string; + resolveRequestDiagnosticsPath?: ( + ref: DiagnosticsRecordRef, + directory: 'request-records' | 'requests-unleased', + ) => string; /** * Hears about a `/rpc` flow rejection this server answered itself, because no part of the flow * reached the answer. The daemon-level sinks live in the runtime, outside any request, so the diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index da7b7e8d67..39643be8fb 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -65,7 +65,10 @@ export type RequestDiagnosticsHttpOptions = { * owns the session store, so this module never builds a session artifact path * of its own (AGENTS.md, "Session artifact paths come from session-store"). */ - resolveRecordPath: (ref: DiagnosticsRecordRef) => string; + resolveRecordPath: ( + ref: DiagnosticsRecordRef, + directory: 'request-records' | 'requests-unleased', + ) => string; }; export function tryHandleRequestDiagnosticsHttpRoute( @@ -141,19 +144,18 @@ async function handleRequestDiagnostics( } const owner = diagnosticsRecordOwner(ref.requestId); - // A record of unknown ownership, such as one that an earlier release wrote, is not served. - if (owner === null) { - sendRestJsonError(res, normalizeError(missingRecordError(ref))); - return; - } const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; - if (!requestHoldsOutputLease(owner, presented)) { + if (owner !== null && !requestHoldsOutputLease(owner, presented)) { sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); return; } - const recordPath = resolveRecordPath(ref); + // Only the host can attest a legacy record by moving it out of the old request-id namespace. + const recordPath = resolveRecordPath( + ref, + owner === null ? 'requests-unleased' : 'request-records', + ); const stats = await statRecord(recordPath); if (!stats) { sendRestJsonError(res, normalizeError(missingRecordError(ref))); diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index 3a89162eca..f06f55ed72 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -267,6 +267,7 @@ export async function createRequestExecutionScope(params: { sessionDir, session: sessionName, requestId: requestDiagnosticsRecordId(scopedReq), + directory: 'request-records', }); const requestLogPath = requestLog.path; const runnerLogPath = resolveSessionRunnerLogPath(sessionDir); diff --git a/packages/daemon/src/server/request-finalization.ts b/packages/daemon/src/server/request-finalization.ts index 5b277b5432..7e13dfaef2 100644 --- a/packages/daemon/src/server/request-finalization.ts +++ b/packages/daemon/src/server/request-finalization.ts @@ -98,8 +98,11 @@ function registerDownloadableArtifacts( ): DaemonResponseData | undefined { if (!data) return data; const admittedLeaseId = req.internal?.admittedLease?.leaseId; + const producer = captureOutputLeaseVerifier(data); const owner = - captureOutputLeaseVerifier(data) ?? (admittedLeaseId && leaseVerifier(admittedLeaseId)); + producer === undefined ? admittedLeaseId && leaseVerifier(admittedLeaseId) : producer; + const producerTenant = captureOutputTenantId(data); + const tenantId = producer === undefined ? (producerTenant ?? req.meta?.tenantId) : producerTenant; const register = (artifact: PendingArtifact): DaemonArtifact => { const artifactPath = artifact.path as string; const delivered = daemonDeliveredArtifact(artifact); @@ -110,9 +113,7 @@ function registerDownloadableArtifacts( ...(artifact.artifactType !== undefined ? { artifactType: artifact.artifactType } : {}), artifactId: trackArtifact({ artifactPath, - tenantId: delivered - ? delivered.tenantId - : (captureOutputTenantId(data) ?? req.meta?.tenantId), + tenantId: delivered ? delivered.tenantId : tenantId, ...(owner ? { leaseVerifier: owner } : {}), artifactType: artifact.artifactType, fileName: artifact.fileName, diff --git a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts index 86ed281c72..3901d03d2a 100644 --- a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts +++ b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts @@ -91,7 +91,7 @@ function localRecordingExport(): RecordingExport { function publishRecordingSession( sessionStore: SessionStore, recordingExport: RecordingExport, - options: { leased?: boolean; finishFails?: boolean } = {}, + options: { leased?: boolean; finishFails?: boolean; unleasedProducer?: boolean } = {}, ) { const session: SessionState = makeIosSession(ADDRESS, { appBundleId: 'com.example.app', @@ -144,7 +144,12 @@ function publishRecordingSession( fence: { token: 'recording-fence', generation: 1 }, lifecycle: 'open', descriptor: { version: 1, body: { recordingId: 'checkout-flow' } }, - metadata: { phase: 'active' }, + metadata: { + phase: 'active', + remoteCaptureTenantId: TENANT, + remoteCaptureLease: + options.leased === false || options.unleasedProducer ? 'unleased' : leaseVerifier(LEASE_ID), + }, }); session.screenRecording = { handle, envelope }; screenRecordingResourceStore.write( @@ -354,7 +359,18 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( ['start'], clientOutPath ? { clientArtifactPaths: { outPath: clientOutPath } } : undefined, undefined, - { publicNetworkOnly: true }, + { + publicNetworkOnly: true, + admittedLease: { + leaseId: LEASE_ID, + tenantId: TENANT, + runId: 'run-1', + backend: 'android-instance', + createdAt: 0, + heartbeatAt: 0, + expiresAt: 0, + }, + }, ); expect(started.ok).toBe(true); const output = harness.runtime.start.mock.calls[0]![0].outputPath; @@ -384,3 +400,17 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( expect(fs.existsSync(output)).toBe(false); }, ); + +test('teardown keeps the recording unleased when the session later acquired a lease', async () => { + const sessionStore = makeSessionStore('agent-device-unleased-recording-'); + publishRecordingSession(sessionStore, remoteRecordingExport(), { unleasedProducer: true }); + + await teardown(sessionStore); + + const artifacts = await listDownloadableArtifacts({ tenantId: TENANT }, process.env); + expect(artifacts).toEqual( + expect.arrayContaining([ + expect.objectContaining({ artifactType: 'screen-recording', filename: 'checkout.mp4' }), + ]), + ); +}); diff --git a/packages/daemon/src/sessions/handlers/record-runtime-response.ts b/packages/daemon/src/sessions/handlers/record-runtime-response.ts index 2c863863af..82ffd934ef 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime-response.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime-response.ts @@ -1,5 +1,6 @@ import type { DaemonArtifact, DaemonResponse } from '@agent-device/kernel/contracts'; import path from 'node:path'; +import type { DurableResourceEnvelope } from '@agent-device/contracts/durable-resource-envelope'; import type { RecordingCommandResult } from '@agent-device/contracts/recording'; import type { ScreenRecordingCompletion, @@ -9,6 +10,18 @@ import type { RuntimeOperationUnavailability } from '@agent-device/contracts/pla import { deriveRecordingTelemetryPath } from '@agent-device/capture-kit/recording-telemetry'; import { isRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http'; +export const UNLEASED_CAPTURE = 'unleased'; + +/** Null identifies an unleased producer. Undefined means that ownership is unknown. */ +export function recordingOutputLeaseVerifier( + metadata: DurableResourceEnvelope['metadata'], +): string | null | undefined { + const owner = metadata?.remoteCaptureLease; + if (owner === UNLEASED_CAPTURE) return null; + if (typeof owner === 'string' && /^[a-f0-9]{64}$/.test(owner)) return owner; + return undefined; +} + export function isDaemonOwnedRecordingOutput( completion: ScreenRecordingCompletion, remoteCaptureOutput: boolean, diff --git a/packages/daemon/src/sessions/handlers/record-runtime.ts b/packages/daemon/src/sessions/handlers/record-runtime.ts index 02a250eada..2a1c59afdc 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime.ts @@ -79,6 +79,8 @@ import { buildRecordingStopResponse, buildRecordingUnsupportedResponse, isDaemonOwnedRecordingOutput, + recordingOutputLeaseVerifier, + UNLEASED_CAPTURE, } from './record-runtime-response.ts'; import { resolveRequestTargetDevice } from '../../core/request-device-resolution.ts'; @@ -289,7 +291,7 @@ async function startRecording( owner: runtime.owner, fence, ...started, - envelope: withCaptureOwner(started.envelope, params.req, session), + envelope: withCaptureOwner(started.envelope, params.req), throwIfCanceled: params.throwIfCanceled, }); const adoptedRef = ref ?? draft!.requireRef(); @@ -310,8 +312,6 @@ async function startRecording( ); } -const UNLEASED_CAPTURE = 'unleased'; - /** * Names in the manifest who gets the export: a remote caller, its tenant, and the lease that started * the recording, or that no lease did. A manifest that names neither is of unknown ownership. @@ -319,10 +319,10 @@ const UNLEASED_CAPTURE = 'unleased'; function withCaptureOwner( envelope: DurableResourceEnvelope<'screen-recording'>, req: DaemonRequest, - session: SessionState, ): DurableResourceEnvelope<'screen-recording'> { - const tenantId = session.lease?.tenantId ?? req.meta?.tenantId; - const leaseId = req.internal?.admittedLease?.leaseId; + const lease = req.internal?.admittedLease; + const tenantId = lease?.tenantId ?? req.meta?.tenantId; + const leaseId = lease?.leaseId; return { ...envelope, metadata: { @@ -465,11 +465,10 @@ async function stopRecording( completion, record.status === 'decoded' && record.envelope.metadata?.remoteCaptureOutput === true, ); - const tenantId = - record.status === 'decoded' && - typeof record.envelope.metadata?.remoteCaptureTenantId === 'string' - ? record.envelope.metadata.remoteCaptureTenantId - : session.lease?.tenantId; + if (daemonOwned && leaseOwner === undefined) throw leasedOutputRefusal('Recording'); + const storedTenant = + record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureTenantId : undefined; + const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; if (stopped.recordsSessionAction) { recordSessionAction(params.sessionStore, ref, params.req, params.req.command, { action: 'stop', @@ -502,19 +501,19 @@ async function stopRecording( * lease that owns the export. A completed export needs only the lease id of its owner, also after * that lease ended. A recording that still runs needs the admitted lease. */ -function assertRequestMayStopRecording(params: RecordRuntimeHandlerParams): string | undefined { +function assertRequestMayStopRecording( + params: RecordRuntimeHandlerParams, +): string | null | undefined { const record = screenRecordingDurableResource.store.read( screenRecordingDurableResource.resourcePath(params.sessionStore, params.sessionName), ); if (record.status !== 'decoded') return undefined; const { metadata, lifecycle } = record.envelope; - const owner = metadata?.remoteCaptureLease; - if (owner === UNLEASED_CAPTURE) return undefined; - if (typeof owner !== 'string') { - // A manifest of an earlier release names no lease, so nobody is known to own its remote export. - if (metadata?.remoteCaptureOutput === true) throw leasedOutputRefusal('Recording'); - return undefined; + const owner = recordingOutputLeaseVerifier(metadata); + if (owner === undefined && metadata?.remoteCaptureOutput === true) { + throw leasedOutputRefusal('Recording'); } + if (typeof owner !== 'string') return owner; const presented = lifecycle === 'completed' ? resolveLeaseScope(params.req).leaseId diff --git a/packages/daemon/src/sessions/leased-recording-retention.ts b/packages/daemon/src/sessions/leased-recording-retention.ts index 4a0b444bbd..a90bec43ee 100644 --- a/packages/daemon/src/sessions/leased-recording-retention.ts +++ b/packages/daemon/src/sessions/leased-recording-retention.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import type { DurableResourceEnvelope } from '@agent-device/contracts/durable-resource-envelope'; import type { ScreenRecordingCompletion } from '@agent-device/contracts/screen-recording-runtime'; import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; import { errorMessage } from '@agent-device/kernel/errors'; @@ -6,12 +7,12 @@ import type { SessionRef } from '../core/session-state.ts'; import { tenantPublicSessionName } from '../core/session-tenant-scope.ts'; import { cleanupDownloadableArtifact, - leaseVerifier, trackDownloadableArtifact, } from '../resources/artifact-tracking.ts'; import { isDaemonOwnedRecordingOutput, recordingStopArtifacts, + recordingOutputLeaseVerifier, } from './handlers/record-runtime-response.ts'; /** @@ -23,10 +24,15 @@ import { export function retainLeasedSessionRecording( ref: SessionRef, completion: ScreenRecordingCompletion, - remoteCaptureOutput: boolean, + metadata: DurableResourceEnvelope['metadata'], ): void { const lease = ref.session.lease; - if (!lease || !isDaemonOwnedRecordingOutput(completion, remoteCaptureOutput)) return; + if (!lease || !isDaemonOwnedRecordingOutput(completion, metadata?.remoteCaptureOutput === true)) + return; + const owner = recordingOutputLeaseVerifier(metadata); + if (owner === undefined) return; + const storedTenant = metadata?.remoteCaptureTenantId; + const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; const sessionName = tenantPublicSessionName(lease.tenantId, ref.address); const artifacts = recordingStopArtifacts(completion).filter( (artifact) => typeof artifact.path === 'string', @@ -37,8 +43,8 @@ export function retainLeasedSessionRecording( artifactIds.push( trackDownloadableArtifact({ artifactPath: artifact.path!, - tenantId: lease.tenantId, - leaseVerifier: leaseVerifier(lease.leaseId), + tenantId, + ...(owner === null ? {} : { leaseVerifier: owner }), artifactType: artifact.artifactType, fileName: artifact.fileName, sessionName, diff --git a/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts b/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts index 36fe38686e..b8895aa9a0 100644 --- a/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts +++ b/packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution.ts @@ -376,6 +376,7 @@ export async function completeOpenCommand(params: { const requestLogPath = resolveSessionRequestLogPath( sessionStateDir, requestDiagnosticsRecordId(req), + 'request-records', ); timing.totalDurationMs = Math.max(0, Date.now() - openCommandStartedAtMs); emitDiagnostic({ diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index ff50235ab6..b55d68541c 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -1,7 +1,9 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value'; -import type { DaemonResponse } from '@agent-device/kernel/contracts'; +import type { DaemonArtifact, DaemonResponse } from '@agent-device/kernel/contracts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import path from 'node:path'; +import { randomUUID } from 'node:crypto'; +import { cp, rename, rm } from 'node:fs/promises'; import { type PerfCaptureAdmissionLedger } from '@agent-device/capture-kit/perf-capture-admission-ledger'; import { adoptStartedPerfCapture, @@ -227,7 +229,13 @@ async function startPerfCapture( : request.kind === 'perfetto' ? 'app.perfetto-trace' : 'cpu.perf.data'; - const outPath = resolveNativeOutPath(params, request.outPath, fallback); + const outPath = + request.kind === 'xctrace' && params.req.internal?.publicNetworkOnly + ? path.join( + params.sessionStore.ensureSessionDir(params.sessionName), + `${randomUUID()}-${fallback}`, + ) + : resolveNativeOutPath(params, request.outPath, fallback); const resourcePath = perfCaptureDurableResource.store.resolvePath( params.sessionStore.resolveSessionDir(params.sessionName), ); @@ -311,18 +319,52 @@ async function stopPerfCapture( typeof capture.envelope.metadata?.remoteCaptureTenantId === 'string' ? capture.envelope.metadata.remoteCaptureTenantId : session.lease?.tenantId; + const outPath = readString(completion.outPath); + const daemonOwned = + outPath !== undefined && capture.envelope.metadata?.remoteCaptureOutputPath === outPath; + const artifacts = + daemonOwned && completion.kind === 'xctrace' + ? [await publishXctraceDownload(outPath, clientOutPath, request.area === 'cpu')] + : undefined; return { ok: true, data: withCaptureOutputOwnership( - { ...completion, ...(clientOutPath ? { clientOutPath } : {}) }, - capture.envelope.metadata?.remoteCaptureOutputPath === completion.outPath - ? 'daemon' - : 'caller', + { + ...completion, + ...(clientOutPath ? { clientOutPath } : {}), + ...(artifacts ? { artifacts } : {}), + }, + daemonOwned ? 'daemon' : 'caller', tenantId, ), }; } +async function publishXctraceDownload( + outPath: string, + clientOutPath: string | undefined, + keepForReport: boolean, +): Promise { + const publishedPath = path.join(path.dirname(outPath), `${randomUUID()}.trace`); + try { + if (keepForReport) { + await cp(outPath, publishedPath, { recursive: true, errorOnExist: true, force: false }); + } else { + await rename(outPath, publishedPath); + } + } catch (error) { + await rm(publishedPath, { recursive: true, force: true }); + throw error; + } + return { + field: 'outPath', + artifactType: 'performance', + path: publishedPath, + fileName: path.basename(clientOutPath ?? outPath), + localPath: clientOutPath, + }; +} + function readProfileHandoff(snapshot: PerfNativeCaptureSnapshot): PerfProfileHandoff { if (isAppleProfileHandoff(snapshot) || isAndroidProfileHandoff(snapshot)) return snapshot; throw new AppError('COMMAND_FAILED', 'Stopped CPU profile metadata is malformed.'); diff --git a/packages/daemon/src/sessions/session-teardown.ts b/packages/daemon/src/sessions/session-teardown.ts index 912cd24e91..8ade9fed9a 100644 --- a/packages/daemon/src/sessions/session-teardown.ts +++ b/packages/daemon/src/sessions/session-teardown.ts @@ -217,11 +217,10 @@ async function finishSessionScreenRecording(params: { const binding = bindSessionScreenRecording(params.sessionStore, params.ref); const recording = binding.read(); if (!recording) return; - const remoteCaptureOutput = recording.envelope.metadata?.remoteCaptureOutput === true; const completion = await finishLiveScreenRecording({ binding, intent: 'disposal' }); if (!params.ref.session.lease) return; const { retainLeasedSessionRecording } = await import('./leased-recording-retention.ts'); - retainLeasedSessionRecording(params.ref, completion, remoteCaptureOutput); + retainLeasedSessionRecording(params.ref, completion, recording.envelope.metadata); } async function finishSessionAudioProbe(params: { diff --git a/src/__tests__/cli-remote-diagnostics.test.ts b/src/__tests__/cli-remote-diagnostics.test.ts index 898e967535..e53af6043d 100644 --- a/src/__tests__/cli-remote-diagnostics.test.ts +++ b/src/__tests__/cli-remote-diagnostics.test.ts @@ -114,7 +114,7 @@ function findRequestLog(stateDir: string): string { .readdirSync(sessionsDir, { withFileTypes: true }) .filter((entry) => entry.isDirectory()); const requestLogs = sessionDirs.flatMap((session) => { - const requestsDir = path.join(sessionsDir, session.name, 'requests'); + const requestsDir = path.join(sessionsDir, session.name, 'request-records'); if (!fs.existsSync(requestsDir)) return []; return fs.readdirSync(requestsDir).map((name) => path.join(requestsDir, name)); }); diff --git a/src/__tests__/daemon-client-abort.test.ts b/src/__tests__/daemon-client-abort.test.ts index 812346f495..42f3744e19 100644 --- a/src/__tests__/daemon-client-abort.test.ts +++ b/src/__tests__/daemon-client-abort.test.ts @@ -62,7 +62,7 @@ async function runAlreadyAborted( 'default', ); // The daemon names each record. The request id is in each line of it. - const requestsDir = path.join(sessionDir, 'requests'); + const requestsDir = path.join(sessionDir, 'request-records'); const records = fs .readdirSync(requestsDir) .map((name) => fs.readFileSync(path.join(requestsDir, name), 'utf8')) diff --git a/src/commands/schema/cli-help-workflows.ts b/src/commands/schema/cli-help-workflows.ts index 66e7dc17f4..80a32da4f3 100644 --- a/src/commands/schema/cli-help-workflows.ts +++ b/src/commands/schema/cli-help-workflows.ts @@ -141,7 +141,7 @@ Diagnostics and traces: Use AGENT_DEVICE_EXEC_TRACE=1 when you need host-tool spawn timing without full debug streaming; it flushes the full request diagnostics file on successful requests that spawn host tools. Open output includes Session state; JSON also includes runnerLogPath and requestLogPath. For open timing under --debug, run open --debug --json and inspect requestLogPath for the open_timing event. - Session requests/.ndjson holds daemon request diagnostics, one file per request, named by the daemon (a failed command prints its path; each line carries the request id); session runner.log holds Apple runner/xcodebuild output. + Session request-records/.ndjson holds daemon request diagnostics, one file per request, named by the daemon (a failed command prints its path; each line carries the request id); session runner.log holds Apple runner/xcodebuild output. Against a remote daemon the Log path is always local: the failing request's record is fetched to /remote-diagnostics/, and if it cannot be fetched the line reads "Log: unavailable" with the reason instead of a daemon-host path. daemon.log is global daemon lifecycle evidence, not the primary per-run log. Use trace for low-level session diagnostics around one repro: diff --git a/test/integration/provider-scenarios/active-session-script-publication.test.ts b/test/integration/provider-scenarios/active-session-script-publication.test.ts index f1fe2a5f92..f6d8ef946a 100644 --- a/test/integration/provider-scenarios/active-session-script-publication.test.ts +++ b/test/integration/provider-scenarios/active-session-script-publication.test.ts @@ -359,7 +359,7 @@ test('parameterized fill publishes only ${VAR} and replay resolves it immediatel assert.equal(world.textInjectionCalls.at(-1)?.text, secret); assert.ok(opened.sessionStateDir); // The daemon names the record. The request id is in each line of it. - const requestsDir = path.join(opened.sessionStateDir, 'requests'); + const requestsDir = path.join(opened.sessionStateDir, 'request-records'); const requestLog = fs .readdirSync(requestsDir) .map((name) => fs.readFileSync(path.join(requestsDir, name), 'utf8')) diff --git a/test/integration/provider-scenarios/remote-proxy-parity.test.ts b/test/integration/provider-scenarios/remote-proxy-parity.test.ts index d55dab3563..551548308e 100644 --- a/test/integration/provider-scenarios/remote-proxy-parity.test.ts +++ b/test/integration/provider-scenarios/remote-proxy-parity.test.ts @@ -163,7 +163,7 @@ function parityScript(): readonly ScenarioRequest[] { /** * The published form of a response with transport identity removed: JSON drops `undefined` * exactly like the wire does, every path under the world's session root becomes the same - * token, and per-request log files lose the request id in their name. + * token, and request diagnostics files lose the record id in their name. */ function stripVolatile(value: unknown, sessionRoot: string): unknown { return stripVolatileKeys(JSON.parse(JSON.stringify(value)), sessionRoot); @@ -175,7 +175,7 @@ function stripVolatileKeys(value: unknown, sessionRoot: string): unknown { return value .split(sessionRoot) .join('') - .replace(/\/requests\/[^/]+\.ndjson$/, '/requests/.ndjson'); + .replace(/\/request-records\/[^/]+\.ndjson$/, '/request-records/.ndjson'); } if (!value || typeof value !== 'object') return value; return Object.fromEntries( diff --git a/test/integration/support/daemon-leak-model.test.ts b/test/integration/support/daemon-leak-model.test.ts index f4948970c3..6c1462ce56 100644 --- a/test/integration/support/daemon-leak-model.test.ts +++ b/test/integration/support/daemon-leak-model.test.ts @@ -200,7 +200,19 @@ describe('state-dir residue rules', () => { test.each<[string, StateEntry, DaemonLeakPhaseSelection, 'expected' | 'stray']>([ ['session event log', file('sessions/default/events.ndjson'), AFTER_SHUTDOWN, 'expected'], - ['request diagnostics', file('sessions/d/requests/abc.ndjson'), AFTER_SHUTDOWN, 'expected'], + [ + 'request diagnostics', + file('sessions/d/request-records/abc.ndjson'), + AFTER_SHUTDOWN, + 'expected', + ], + ['legacy diagnostics', file('sessions/d/requests/abc.ndjson'), AFTER_SHUTDOWN, 'expected'], + [ + 'verified legacy diagnostics', + file('sessions/d/requests-unleased/abc.ndjson'), + AFTER_SHUTDOWN, + 'expected', + ], ['shutdown report', file('daemon-shutdown.json'), AFTER_SHUTDOWN, 'expected'], ['torn publish temporary', file('device-claims/a.json.55.tmp'), AFTER_SHUTDOWN, 'stray'], [ diff --git a/test/integration/support/daemon-leak-model.ts b/test/integration/support/daemon-leak-model.ts index 0a47f1c80a..7b6f53fe89 100644 --- a/test/integration/support/daemon-leak-model.ts +++ b/test/integration/support/daemon-leak-model.ts @@ -102,7 +102,7 @@ const EXPECTED_STATE_DIR_ENTRIES: readonly RegExp[] = [ /^daemon\.log$/, /^daemon-shutdown\.json$/, /^sessions\/[^/]+\/events\.ndjson$/, - /^sessions\/[^/]+\/requests\/[^/]+\.ndjson$/, + /^sessions\/[^/]+\/(?:request-records|requests(?:-unleased)?)\/[^/]+\.ndjson$/, /^sessions\/[^/]+\/(?:app|runner)\.log$/, /^sessions\/[^/]+\/repair-tombstone\.json$/, /^sessions\/[^/]+\/artifacts\/.+$/, diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index 86d305abcd..a332467a0b 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -85,8 +85,8 @@ "packages/daemon/src/server/http-request-target.ts#decodeUriSegment": "sha256:b37622771ea4e61d1a9820169dbdc5809b5c38889bfad79277d7b22f3c3f1ef1", "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", - "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:cd2c0dda77d2bc434857aea07206a5491ce5f0b82cb86628569f607d6c2cebcf", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:3d3223a0f05c50b07ae4988cccbd9638aea421753b2c5f4c550a60b8c0100e8f", + "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:58fc1eb61b082b64eb9714ae76ef6f3ff44102a72736db52ce0b9214dca7f509", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a374824d0af99b2a40f95a4a4f9981f4446a5dd7df427f3fdc9852350f4b73b8", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", @@ -315,8 +315,8 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:3d3223a0f05c50b07ae4988cccbd9638aea421753b2c5f4c550a60b8c0100e8f", - "rationale": "#2198 context: asks isTenantAddressableSessionName instead of applying the prefix rule locally. Same UNAUTHORIZED code, same 'Session is outside this tenant' message and same 401 body for the attested-tenant case the rule still refuses; the only difference a peer can observe is that an unattested tenant now gets the 200 + ndjson record it already knows how to read instead of a 401 it could not act on. Implementation-only HTTP error repair under ADR 0006: setup failures use the shared streamed failure helper. Before headers, it emits the existing REST JSON error shape with an error status; after headers, it destroys the incomplete transfer. Successful diagnostics remain 200 with the same NDJSON bytes. Released clients reject error statuses before reading diagnostic records, so no successful response parser or request requirement changes. #4912 reads the existing x-agent-device-lease-id request header. The daemon now chooses the requestId segment of the locator it returns in diagnosticsRecord, and that id carries the owner: the lease id the request sent, or that it sent none. A released client uses the locator as an opaque value for the URL and for its local file name. A record of a request that sent a lease id needs that lease id, else it fails with the existing 401 REST error shape and reason LEASE_OUTPUT_MISMATCH. Released clients send the header and treat any error status as an unavailable record. A locator that the daemon did not choose, such as one from an earlier release, answers the existing 404." + "digest": "sha256:a374824d0af99b2a40f95a4a4f9981f4446a5dd7df427f3fdc9852350f4b73b8", + "rationale": "The existing locator and NDJSON response keep their shape. Only the server storage lookup and lease authorization change." }, { "declaration": "packages/kernel/src/contracts.ts#LEASE_BACKENDS", @@ -667,6 +667,11 @@ "declaration": "packages/daemon-contracts/src/daemon-resolution.ts#DAEMON_CAPABILITIES", "digest": "sha256:1ba3b95deeedfc4f47504e9566f9d934a5dd69da4ffdc8a9c39ffc485cd4dbd4", "rationale": "Adds the session-lease-id capability: the daemon refuses a request on a leased session that does not send that lease's id, with UNAUTHORIZED and reason LEASE_SESSION_MISMATCH (field leaseId). readDaemonCapabilities filters to known values, so a released client ignores the new string. A released client that relied on the daemon filling the lease id from the session gets that normal command error and misreads no payload. The current CLI already sends the saved connection's lease id. No request or response field changes shape." + }, + { + "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions", + "digest": "sha256:58fc1eb61b082b64eb9714ae76ef6f3ff44102a72736db52ce0b9214dca7f509", + "rationale": "The server-internal path resolver takes a storage namespace. No HTTP request or response field changes." } ] } diff --git a/website/docs/docs/debugging-profiling.md b/website/docs/docs/debugging-profiling.md index 470cd093c7..3b6595ca3e 100644 --- a/website/docs/docs/debugging-profiling.md +++ b/website/docs/docs/debugging-profiling.md @@ -136,7 +136,7 @@ When a command fails, stderr starts and ends with the same summary line, so the ```text Error (COMMAND_FAILED): tap @e12: runner did not answer within 10s. May have reached the device. Next: Run snapshot to check the screen before you retry. Read the diagnostics log for the failing step. -Log: /Users/me/.agent-device/sessions//requests/.ndjson +Log: /Users/me/.agent-device/sessions//request-records/.ndjson Error (COMMAND_FAILED): tap @e12: runner did not answer within 10s. May have reached the device. Next: Run snapshot to check the screen before you retry. Read the diagnostics log for the failing step. ``` @@ -146,9 +146,9 @@ Use `--json` to inspect `error.details.stderr` when it is present. Diagnostic st When a command fails against a remote daemon, the `Log:` path is always on your machine: agent-device downloads the failing request's record over the same base URL and token into -`/remote-diagnostics//requests/.ndjson`, so a CI job can keep it as a build +`/remote-diagnostics//requests/.ndjson`, so a CI job can keep it as a build artifact. If the download fails, the line reads `Log: unavailable` with the remote daemon, the -request id, and the reason — never a path on the daemon host. +record id, and the reason. On iOS simulators, `logs` scope by bundle id and the resolved app executable. For launch-time stdout/stderr, capture the direct app launch console instead of starting raw `simctl` streams: diff --git a/website/docs/docs/sessions.md b/website/docs/docs/sessions.md index 06aa75ef74..0608f78f27 100644 --- a/website/docs/docs/sessions.md +++ b/website/docs/docs/sessions.md @@ -28,7 +28,7 @@ JSON output also includes `runnerLogPath` and `requestLogPath` when available. Each session artifact directory contains: -- `requests/.ndjson` - daemon request diagnostics for this session, one file per request. The daemon names the file, a failed command prints its path, and each line carries the request id. +- `request-records/.ndjson` - daemon request diagnostics for this session, one file per request. The daemon names the file, a failed command prints its path, and each line carries the request id. - `events.ndjson` - session event timeline for requests and recorded actions; rotates to `events.ndjson.1` before an entry would exceed 5 MiB (`AGENT_DEVICE_EVENT_LOG_MAX_BYTES`, whole bytes), with `events.ndjson.window.json` tracking the retained files so `events` cursors within the retained window stay valid across rotation. Older cursors expire. - `runner.log` - Apple runner and `xcodebuild` build/start output for this session. - `app.log` - app/device logs when `logs start` or `logs clear --restart` is active. @@ -46,7 +46,7 @@ The per-session limit is 128 events and 16 MiB of event data, including the writ Further events are dropped until capacity is available. These drops do not fail the command, and failed entries are not replayed. To capture write or rotation failure diagnostics while reproducing missing entries, run the command with `--debug`, then look for `session_event_log_write_failed` in -`requests/.ndjson` inside the session artifact directory. An active `trace` also records +`request-records/.ndjson` inside the session artifact directory. An active `trace` also records these diagnostics in its trace file. To debug a specific run, start with the session artifact directory. The top-level daemon log only From 86a9ead6a521b7b53a10eb651cb5742380756a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 12:57:11 +0100 Subject: [PATCH 09/15] test: follow the daemon-reported diagnostics path in packed smoke probes --- packages/platform-vega/test/package-smoke.mjs | 37 +++++++++--------- packages/platform-web/test/package-smoke.mjs | 38 +++++++++---------- .../schema/cli-help-workflows.test.ts | 2 +- src/platform-runtime/plugin-host.test.ts | 18 +++++---- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/packages/platform-vega/test/package-smoke.mjs b/packages/platform-vega/test/package-smoke.mjs index 2e3cb27abf..213e2bacd9 100644 --- a/packages/platform-vega/test/package-smoke.mjs +++ b/packages/platform-vega/test/package-smoke.mjs @@ -35,22 +35,18 @@ function vegaStubSource(logFile) { } /** - * The exec records a `--debug` request wrote, from the request records the daemon keeps under its - * session dirs. Records are matched by the CLI command they belong to, so an empty answer means the - * request traced no spawn at all rather than that its file was not found. + * Read the open record by its CLI-reported path. Use its directory to find follow-up records, + * which the daemon names independently and the CLI does not report on success. */ -function execRecordsOf(storeRoot, command) { - const sessionsDir = path.join(storeRoot, 'sessions'); - if (!fs.existsSync(sessionsDir)) return []; - const requestFiles = fs - .readdirSync(sessionsDir) - .flatMap((session) => { - const requestsDir = path.join(sessionsDir, session, 'requests'); - return fs.existsSync(requestsDir) - ? fs.readdirSync(requestsDir).map((request) => path.join(requestsDir, request)) - : []; - }) - .filter((file) => file.endsWith('.ndjson')); +function execRecordsOf(openLogPath, command) { + const recordDir = path.dirname(openLogPath); + const requestFiles = + command === 'open' + ? [openLogPath] + : fs + .readdirSync(recordDir) + .filter((file) => file.endsWith('.ndjson')) + .map((file) => path.join(recordDir, file)); return requestFiles .flatMap((file) => fs.readFileSync(file, 'utf8').split('\n')) .filter((line) => line.includes('"phase":"exec_command"')) @@ -103,7 +99,7 @@ test('packed core discovers vega only after the managed platform is installed', // The daemon environment is captured when the CLI starts it, so the stub directory has to be on // the PATH of the process that launches it. const stubEnv = () => ({ ...cliEnv, PATH: [stubBin, cliEnv.PATH].join(path.delimiter) }); - // A `--debug` request writes its own exec records to the daemon log: the route proof below reads + // A `--debug` request writes its own exec records to its diagnostics record: the route proof reads // them back, which is what a spawn through the bundle's own host-kit copy cannot produce. const debugCli = (...args) => execFileSync( @@ -111,7 +107,6 @@ test('packed core discovers vega only after the managed platform is installed', ['node_modules/agent-device/bin/agent-device.mjs', ...args, '--json', '--debug'], { cwd: scratch, env: stubEnv(), encoding: 'utf8', timeout: 120_000 }, ); - const storeRoot = env.AGENT_DEVICE_STATE_DIR; try { const [packed] = JSON.parse( exec('npm', ['pack', '--ignore-scripts', '--json', '--pack-destination', scratch], repo), @@ -170,7 +165,11 @@ test('packed core discovers vega only after the managed platform is installed', found.data.devices.map(({ id, platform }) => ({ id, platform })), [{ id: 'VirtualDevice', platform: 'vega' }], ); - debugCli('open', 'com.example.app.main', '--platform', 'vega', '--target', 'tv'); + const opened = JSON.parse( + debugCli('open', 'com.example.app.main', '--platform', 'vega', '--target', 'tv'), + ); + const openLogPath = opened.data.requestLogPath; + assert.equal(typeof openLogPath, 'string', 'open must report its diagnostics record path'); debugCli( 'tv-remote', 'press', @@ -232,7 +231,7 @@ test('packed core discovers vega only after the managed platform is installed', ['tv-remote', 10_120, 'button_press'], ['close', 30_000, 'device terminate-app'], ]) { - const records = execRecordsOf(storeRoot, command); + const records = execRecordsOf(openLogPath, command); const spawns = records.filter( (record) => record.data?.event === 'spawn' && record.data.timeoutMs === timeoutMs, ); diff --git a/packages/platform-web/test/package-smoke.mjs b/packages/platform-web/test/package-smoke.mjs index 70b0efcb9a..4ed764a71a 100644 --- a/packages/platform-web/test/package-smoke.mjs +++ b/packages/platform-web/test/package-smoke.mjs @@ -78,22 +78,18 @@ function debugRecordsOf(stderr) { const DEBUG_RECORD_PREFIX = '[agent-device][diag] '; /** - * The records one `--debug` request wrote, read back from the request records the daemon keeps under - * its session dirs. Matched by the CLI command they belong to, so an empty answer means the request - * traced nothing rather than that a file was missed. + * Read the open record by its CLI-reported path. Use its directory to find follow-up records, + * which the daemon names independently and the CLI does not report on success. */ -function requestRecordsOf(storeRoot, phase, command) { - const sessionsDir = path.join(storeRoot, 'sessions'); - if (!fs.existsSync(sessionsDir)) return []; - const requestFiles = fs - .readdirSync(sessionsDir) - .flatMap((session) => { - const requestsDir = path.join(sessionsDir, session, 'requests'); - return fs.existsSync(requestsDir) - ? fs.readdirSync(requestsDir).map((request) => path.join(requestsDir, request)) - : []; - }) - .filter((file) => file.endsWith('.ndjson')); +function requestRecordsOf(openLogPath, phase, command) { + const recordDir = path.dirname(openLogPath); + const requestFiles = + command === 'open' + ? [openLogPath] + : fs + .readdirSync(recordDir) + .filter((file) => file.endsWith('.ndjson')) + .map((file) => path.join(recordDir, file)); return requestFiles .flatMap((file) => fs.readFileSync(file, 'utf8').split('\n')) .filter((line) => line.includes(`"phase":"${phase}"`)) @@ -256,7 +252,11 @@ test('packed core discovers web only after the managed platform is installed', ( // One named session for the whole interaction: the backend is told which session each call // belongs to, so the recorded argv names it too. const session = 'web-pack'; - debugCli('open', 'https://example.test', '--platform', 'web', '--session', session); + const opened = JSON.parse( + debugCli('open', 'https://example.test', '--platform', 'web', '--session', session), + ); + const openLogPath = opened.data.requestLogPath; + assert.equal(typeof openLogPath, 'string', 'open must report its diagnostics record path'); debugCli('press', '100', '200', '--platform', 'web', '--session', session); const shot = path.join(scratch, 'screen.png'); debugCli('screenshot', shot, '--platform', 'web', '--session', session); @@ -306,7 +306,7 @@ test('packed core discovers web only after the managed platform is installed', ( // reads, and the executable path of each candidate — on macOS one `osascript`, on Windows one CIM // query. Read through a bundled host-kit copy instead, each read still succeeds — and the request // that owed it traces nothing, which is what the two assertions below refuse to accept. - const openRecords = requestRecordsOf(storeRoot, 'exec_command', 'open'); + const openRecords = requestRecordsOf(openLogPath, 'exec_command', 'open'); assert.ok( openRecords.some((record) => record.data?.event === 'spawn' && record.data.command === 'ps'), `the open request traced no process-table read out of ${openRecords.length} exec records: ${JSON.stringify( @@ -328,7 +328,7 @@ test('packed core discovers web only after the managed platform is installed', ( ['screenshot', 'a screenshot call'], ['close', 'close'], ]) { - const records = requestRecordsOf(storeRoot, 'exec_command', command); + const records = requestRecordsOf(openLogPath, 'exec_command', command); const spawns = records.filter((record) => record.data?.event === 'spawn'); const exits = records.filter((record) => record.data?.event === 'exit'); assert.ok( @@ -347,7 +347,7 @@ test('packed core discovers web only after the managed platform is installed', ( // The interactor's snapshot timer reports through the host too, so one capture timing is // attributed to the request that asked for the capture. assert.ok( - requestRecordsOf(storeRoot, 'snapshot_capture', 'snapshot').some( + requestRecordsOf(openLogPath, 'snapshot_capture', 'snapshot').some( (record) => record.data?.backend === 'web' && record.durationMs > 0, ), 'the snapshot request carried no web capture timing', diff --git a/src/commands/schema/cli-help-workflows.test.ts b/src/commands/schema/cli-help-workflows.test.ts index da04275a6c..9faf6d7600 100644 --- a/src/commands/schema/cli-help-workflows.test.ts +++ b/src/commands/schema/cli-help-workflows.test.ts @@ -28,7 +28,7 @@ test('usageForCommand resolves debugging help topic', async () => { ); assert.match(help, /open --debug --json/); assert.match(help, /open_timing event/); - assert.match(help, /requests\/\.ndjson holds daemon request diagnostics/); + assert.match(help, /request-records\/\.ndjson holds daemon request diagnostics/); assert.match(help, /daemon\.log is global daemon lifecycle evidence/); assert.match(help, /agent-device perf memory sample --json/); assert.match(help, /agent-device audio probe start 10 1000 --platform web/); diff --git a/src/platform-runtime/plugin-host.test.ts b/src/platform-runtime/plugin-host.test.ts index 72b838e7c5..86074482ea 100644 --- a/src/platform-runtime/plugin-host.test.ts +++ b/src/platform-runtime/plugin-host.test.ts @@ -66,15 +66,17 @@ test('the platform plugin host keeps a background command as a managed process', new AbortController().signal, ); - expect(process.marker?.pid).toBeGreaterThan(0); - // The marker is the core's own record, so its recovery path reads it. - expect(JSON.parse(fs.readFileSync(markerPath, 'utf8'))).toMatchObject({ - pid: process.marker?.pid, - }); - - await process.terminate(); + try { + expect(process.marker?.pid).toBeGreaterThan(0); + // The marker is the core's own record, so its recovery path reads it. + expect(JSON.parse(fs.readFileSync(markerPath, 'utf8'))).toMatchObject({ + pid: process.marker?.pid, + }); + await expect.poll(() => written.join('')).toContain('hilog line'); + } finally { + await process.terminate(); + } expect((await process.wait).exitCode).not.toBe(0); - expect(written.join('')).toContain('hilog line'); expect(fs.existsSync(markerPath)).toBe(false); }); From 03829de7ffe3ddf5dada295438d99f05e9664422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 14:20:41 +0100 Subject: [PATCH 10/15] fix(daemon): retain capture provenance through artifact delivery --- packages/contracts/src/replay-backend.ts | 2 +- .../docs/remote-device-lease-contracts.md | 4 +- packages/daemon/src/core/session-state.ts | 15 +- .../src/execution/screenshot-runtime.ts | 2 + .../src/resources/artifact-tracking.test.ts | 24 ++ .../daemon/src/resources/artifact-tracking.ts | 34 ++- .../request-diagnostics-http.test.ts | 8 +- .../__tests__/request-finalization.test.ts | 51 +++- .../http-server-capture-publication.test.ts | 223 +++++++++++++----- .../http/http-server-remote-output.test.ts | 45 +++- .../src/server/request-diagnostics-http.ts | 24 +- .../src/server/request-execution-scope.ts | 3 +- .../daemon/src/server/request-finalization.ts | 7 +- .../leased-recording-retention.test.ts | 19 +- .../__tests__/record-runtime-start.test.ts | 10 +- .../handlers/record-runtime-response.ts | 10 +- .../src/sessions/handlers/record-runtime.ts | 69 +++--- .../src/sessions/handlers/trace-runtime.ts | 29 ++- .../sessions/leased-recording-retention.ts | 11 +- .../internal/session-perf-runtime.ts | 66 +++--- test/wire-compat/ledger.json | 4 +- 21 files changed, 477 insertions(+), 183 deletions(-) diff --git a/packages/contracts/src/replay-backend.ts b/packages/contracts/src/replay-backend.ts index 07f6fc383c..0b4f9d913a 100644 --- a/packages/contracts/src/replay-backend.ts +++ b/packages/contracts/src/replay-backend.ts @@ -44,7 +44,7 @@ export type ReplaySessionState = Readonly<{ device: DeviceInfo; appBundleId?: string; actions: SessionAction[]; - trace?: Readonly<{ outPath: string }>; + trace?: Readonly<{ outPath?: string }>; /** Read for presence only: an active recording means replay video must not start another. */ screenRecording?: object; snapshotDiagnostics?: Pick; diff --git a/packages/daemon/docs/remote-device-lease-contracts.md b/packages/daemon/docs/remote-device-lease-contracts.md index 956f725e79..fc884620ee 100644 --- a/packages/daemon/docs/remote-device-lease-contracts.md +++ b/packages/daemon/docs/remote-device-lease-contracts.md @@ -17,7 +17,7 @@ Idle reap then shuts the daemon down within one more idle window. A session cannot outlive the lease under which it was opened. When the lease is released, expires, or its provider reports the instance ended, the daemon tears the session down under its execution locks and clears its device claim. -Any teardown of a leased session, including `close`, finishes an active recording. A finished export at a remote temp path then stays a downloadable artifact of that lease, like the artifact of any `record stop` (ADR 0024 2.5). +Any teardown of a leased session, including `close`, finishes an active recording. A daemon-owned export stays downloadable under its producing owner, as with `record stop` (ADR 0024 2.5). This applies even when the device no longer answers. A release is a tenant `lease_release` or a host `DELETE /admin/leases/`. The daemon sweeps for expired leases every second. @@ -108,7 +108,7 @@ An event that the daemon writes outside a request goes to the daemon log, which Output of a request that acted under no lease keeps the tenant rule. Before you restore access to a legacy diagnostics record, verify its full contents came from unleased requests and contain no lease credentials. Move the file from the session's `requests` directory to its `requests-unleased` directory. Keep its filename. -Before you restore access to a legacy recording, verify that the producing request was unleased. Set `metadata.remoteCaptureLease` to `"unleased"` in its recording manifest. +Before you restore access to a legacy recording in the daemon session directory, verify that the producing request was unleased. The daemon refuses replay from legacy remote temp paths. Set `metadata.remoteCaptureLease` to `"unleased"` in its recording manifest. ## Admitted request work diff --git a/packages/daemon/src/core/session-state.ts b/packages/daemon/src/core/session-state.ts index 2b1a000191..55a367e11c 100644 --- a/packages/daemon/src/core/session-state.ts +++ b/packages/daemon/src/core/session-state.ts @@ -240,13 +240,16 @@ export type SessionState = { */ deferredSurface?: DeferredSurface; snapshotDiagnostics?: SnapshotDiagnosticsState; + /** Remote traces accept request-scoped diagnostics instead of persistent runner streams. */ trace?: { - outPath: string; clientOutPath?: string; - remoteCaptureOutputPath?: string; remoteCaptureTenantId?: string; + leaseVerifier?: string | null; startedAt: number; - }; + } & ( + | { outPath: string; remoteCaptureOutputPath?: never } + | { outPath?: never; remoteCaptureOutputPath: string } + ); /** Native profiling mechanics stay behind one exact-owner durable handle. */ perfCapture?: { clientOutPath?: string; @@ -254,7 +257,11 @@ export type SessionState = { envelope: DurableResourceEnvelope<'perf-capture'>; }; /** Last stopped CPU-profile coordinates retained for report in the same live session. */ - lastPerfProfile?: PerfProfileHandoff; + lastPerfProfile?: { + profile: PerfProfileHandoff; + leaseVerifier: string | null; + tenantId?: string; + }; /** Native sampler mechanics stay behind the adopted runtime handle. */ audioProbe?: { handle: AudioProbeLiveHandle; diff --git a/packages/daemon/src/execution/screenshot-runtime.ts b/packages/daemon/src/execution/screenshot-runtime.ts index f1fb73a4ce..8569cf2ca2 100644 --- a/packages/daemon/src/execution/screenshot-runtime.ts +++ b/packages/daemon/src/execution/screenshot-runtime.ts @@ -1,4 +1,5 @@ import { randomUUID } from 'node:crypto'; +import { isRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http'; import { rm } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; @@ -253,6 +254,7 @@ async function executeScreenshot( })), }; if (!privatePath || !publishedPath) return result; + if (isRemoteTempArtifactPath(publishedPath, 'screenshot', '.png')) return result; await publishScreenshotFile(privatePath, publishedPath, params.dispatchContext.signal); return { ...result, diff --git a/packages/daemon/src/resources/artifact-tracking.test.ts b/packages/daemon/src/resources/artifact-tracking.test.ts index f941a64a16..6ecd7aa593 100644 --- a/packages/daemon/src/resources/artifact-tracking.test.ts +++ b/packages/daemon/src/resources/artifact-tracking.test.ts @@ -741,3 +741,27 @@ test.each( } }, ); + +test('a file download streams from the descriptor whose identity was checked', async () => { + const root = mkdtempForTestSync('artifact-descriptor-'); + const source = path.join(root, 'capture.mp4'); + fs.writeFileSync(source, 'recorded-video'); + const id = trackDownloadableArtifact({ artifactPath: source, artifactType: 'screen-recording' }); + const checked = vi.spyOn(fs, 'fstatSync'); + const streams = vi.spyOn(fs, 'createReadStream'); + try { + const opened = await openDownloadableArtifact(id, {}, process.env); + try { + expect(checked.mock.calls[0]?.[0]).toBeTypeOf('number'); + expect(streams).toHaveBeenCalledWith(source, { fd: checked.mock.calls[0]?.[0] }); + expect((await buffer(opened.stream)).toString()).toBe('recorded-video'); + } finally { + opened.stream.destroy(); + opened.release(); + } + } finally { + streams.mockRestore(); + checked.mockRestore(); + cleanupDownloadableArtifact(id); + } +}); diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index a17d0ec112..8718ed0e23 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -24,6 +24,7 @@ import { } from '@agent-device/host-kit/diagnostics'; import { timingSafeStringEqual } from '@agent-device/host-kit/transport'; import type { DaemonRequest } from '../core/daemon-request.ts'; +import type { SessionState } from '../core/session-state.ts'; import { resolveLeaseScope } from '../core/lease-context.ts'; import { normalizeLeaseId } from './lease-registry-scope.ts'; @@ -41,6 +42,20 @@ export function leaseVerifier(leaseId: string): string { return crypto.createHash('sha256').update(canonical).digest('hex'); } +export function admittedOutputLeaseVerifier(req: Pick): string | null { + const leaseId = req.internal?.admittedLease?.leaseId; + return leaseId === undefined ? null : leaseVerifier(leaseId); +} + +export function requestTraceLogPath( + req: DaemonRequest, + trace: SessionState['trace'], +): string | undefined { + if (!trace || (trace.leaseVerifier ?? null) !== admittedOutputLeaseVerifier(req)) + return undefined; + return trace.remoteCaptureOutputPath ?? trace.outPath; +} + /** * Whether a request may read output that a lease produced. Output of a leased request belongs to * that lease for as long as the output is kept, also after the lease ends: only a request that @@ -341,7 +356,9 @@ export async function openDownloadableArtifact( `Artifact path is not a regular file or directory: ${entry.artifactPath}`, ); } - const stream = fs.createReadStream(payload.artifactPath); + const stream = stat.isFile() + ? openRegisteredFile(artifactId, entry) + : fs.createReadStream(payload.artifactPath); download.stream = stream; return { ...payload, stream, signal: download.controller.signal, release: download.release }; } catch (error) { @@ -350,6 +367,21 @@ export async function openDownloadableArtifact( } } +function openRegisteredFile(artifactId: string, entry: ArtifactEntry): fs.ReadStream { + const fd = fs.openSync(entry.artifactPath, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK); + try { + const stat = fs.fstatSync(fd); + if (!stat.isFile() || outputIdentity(stat) !== entry.output) { + cleanupDownloadableArtifact(artifactId); + throw expiredTenantOwnedEntryError(DOWNLOADABLE_ARTIFACT_RESOURCE, artifactId); + } + return fs.createReadStream(entry.artifactPath, { fd }); + } catch (error) { + fs.closeSync(fd); + throw error; + } +} + function cancelArtifactDownload( artifactId: string, entry: ArtifactEntry, diff --git a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts index 672a2984cc..5dee254645 100644 --- a/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts +++ b/packages/daemon/src/server/__tests__/request-diagnostics-http.test.ts @@ -302,14 +302,18 @@ test('legacy diagnostics are served only from the host-verified namespace', asyn if (await skipWhenLoopbackUnavailable(t)) return; await withDiagnosticsServer(async ({ baseUrl, sessionsDir }) => { writeRecord(sessionsDir, 'default', RECORD_ID, 'requests'); - writeRecord(sessionsDir, 'default', 'verified-legacy', 'requests-unleased'); + const reservedId = `${'b'.repeat(32)}-${'c'.repeat(64)}`; + writeRecord(sessionsDir, 'default', reservedId, 'requests-unleased'); const headers = { authorization: 'Bearer daemon-secret' }; const unknown = await fetch(diagnosticsUrl(baseUrl, 'default', RECORD_ID), { headers }); expect(unknown.status).toBe(404); - const verified = await fetch(diagnosticsUrl(baseUrl, 'default', 'verified-legacy'), { + const verified = await fetch(diagnosticsUrl(baseUrl, 'default', reservedId), { headers, }); expect(verified.status).toBe(200); expect(await verified.text()).toBe(RECORD); + writeRecord(sessionsDir, 'default', reservedId); + const current = await fetch(diagnosticsUrl(baseUrl, 'default', reservedId), { headers }); + expect(current.status).toBe(401); }); }); diff --git a/packages/daemon/src/server/__tests__/request-finalization.test.ts b/packages/daemon/src/server/__tests__/request-finalization.test.ts index 94fa4dcc58..8b9ff3e336 100644 --- a/packages/daemon/src/server/__tests__/request-finalization.test.ts +++ b/packages/daemon/src/server/__tests__/request-finalization.test.ts @@ -4,7 +4,12 @@ import { finalizeDaemonResponse } from '../request-finalization.ts'; import type { DaemonRequest } from '../../core/daemon-request.ts'; import type { DaemonArtifactType, DaemonResponse } from '@agent-device/kernel/contracts'; import type { DownloadableArtifactRegistration } from '../../resources/artifact-tracking.ts'; -import { asDaemonDeliveredArtifact } from '../../core/capture-output-ownership.ts'; +import { LeaseRegistry } from '../../resources/lease-registry.ts'; +import { leaseVerifier } from '../../resources/artifact-tracking.ts'; +import { + asDaemonDeliveredArtifact, + withCaptureOutputOwnership, +} from '../../core/capture-output-ownership.ts'; test('finalizeDaemonResponse preserves handler error hints from details', () => { const req: DaemonRequest = { @@ -413,3 +418,47 @@ test.each([ expect(registrations.map(({ tenantId }) => tenantId)).toEqual(['tenant-a']); }, ); + +test('a delivered script keeps the admitted lease alongside an unleased recording', () => { + const lease = new LeaseRegistry().allocateLease({ tenantId: 'tenant', runId: 'record' }); + const tracked: DownloadableArtifactRegistration[] = []; + finalizeDaemonResponse( + { + token: 'token', + session: 'default', + command: 'record', + positionals: ['stop'], + internal: { admittedLease: lease }, + meta: { tenantId: 'tenant' }, + }, + { + ok: true, + data: withCaptureOutputOwnership( + { + artifacts: [ + { field: 'outPath', artifactType: 'screen-recording', path: '/daemon/video.mp4' }, + asDaemonDeliveredArtifact( + { + field: 'savedScript', + artifactType: 'session-script', + path: '/daemon/session.ad', + }, + 'tenant', + ), + ], + }, + 'daemon', + undefined, + null, + ), + }, + (entry) => { + tracked.push(entry); + return 'artifact'; + }, + ); + expect(tracked.map((artifact) => [artifact.tenantId, artifact.leaseVerifier])).toEqual([ + ['tenant', undefined], + ['tenant', leaseVerifier(lease.leaseId)], + ]); +}); diff --git a/packages/daemon/src/server/http/http-server-capture-publication.test.ts b/packages/daemon/src/server/http/http-server-capture-publication.test.ts index 6a10c2976c..913a196613 100644 --- a/packages/daemon/src/server/http/http-server-capture-publication.test.ts +++ b/packages/daemon/src/server/http/http-server-capture-publication.test.ts @@ -1,3 +1,8 @@ +import { + buildRemoteTempArtifactPath, + DAEMON_HTTP_LEASE_HEADER, + DAEMON_HTTP_TENANT_HEADER, +} from '@agent-device/contracts/daemon-http'; import fs from 'node:fs'; import path from 'node:path'; import { expect, test } from 'vitest'; @@ -16,58 +21,67 @@ import { finalizeDaemonResponse } from '../request-finalization.ts'; import { makeRecordRuntimeHarness } from '../../sessions/handlers/__tests__/record-runtime.fixtures.ts'; import { successfulData, withHttp } from './http-output.fixtures.ts'; -test('downloadable screenshots keep the published frame when the caller output is reused', async () => { - const root = mkdtempForTestSync('immutable-screenshot-'); - const destination = path.join(root, 'capture.png'); - const sessionStore = makeSessionStore('immutable-screenshot-session-'); - sessionStore.publish('capture', makeAndroidSession('capture')); - const png = (red: number) => { - const frame = new PNG({ width: 1, height: 1 }); - frame.data.set([red, 0, 0, 255]); - return PNG.sync.write(frame); - }; - const original = png(42); - const replacement = png(84); - const frames = [original, replacement]; - const runtime = screenshotRuntimeFixture({ - onCapture: ({ outPath }) => { - fs.writeFileSync(outPath, frames.shift()!); - }, - }); - const handler = createRequestHandler({ - host: createTestHostEnvironment(), - createCommandSurface: screenshotCommandSurface, - logPath: path.join(root, 'daemon.log'), - token: 'test-token', - sessionStore, - leaseRegistry: new LeaseRegistry(), - deviceInventoryGateways: createTestDeviceInventoryGateways(), - deviceRuntimeGateway: runtime.gateway, - trackDownloadableArtifact, - }); - await withHttp(handler, async (post, base) => { - const capture = { command: 'screenshot', session: 'capture', positionals: [destination] }; - const first = successfulData( - await post( - { - ...capture, - meta: { clientArtifactPaths: { path: '/client/first.png' } }, - }, - false, - ), - ); - const second = successfulData(await post(capture, false)); - expect(second.path).toBe(destination); - const artifactId = first.artifacts?.[0]?.artifactId; - expect(artifactId).toBeTypeOf('string'); - const download = await fetch(`${base}/artifacts/${artifactId}`, { - headers: { authorization: 'Bearer test-token' }, +test.each(['caller destination', 'remote placeholder'])( + 'downloadable screenshots keep the published frame with a %s', + async (destinationKind) => { + const root = mkdtempForTestSync('immutable-screenshot-'); + const placeholder = destinationKind === 'remote placeholder'; + const destination = placeholder + ? buildRemoteTempArtifactPath('screenshot', '.png') + : path.join(root, 'capture.png'); + const sessionStore = makeSessionStore('immutable-screenshot-session-'); + sessionStore.publish('capture', makeAndroidSession('capture')); + const png = (red: number) => { + const frame = new PNG({ width: 1, height: 1 }); + frame.data.set([red, 0, 0, 255]); + return PNG.sync.write(frame); + }; + const original = png(42); + const replacement = png(84); + const frames = [original, replacement]; + const runtime = screenshotRuntimeFixture({ + onCapture: ({ outPath }) => { + fs.writeFileSync(outPath, frames.shift()!); + }, }); - expect(download.status).toBe(200); - expect(Buffer.from(await download.arrayBuffer())).toEqual(original); - expect(fs.readFileSync(destination)).toEqual(replacement); - }); -}); + const handler = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(root, 'daemon.log'), + token: 'test-token', + sessionStore, + leaseRegistry: new LeaseRegistry(), + deviceInventoryGateways: createTestDeviceInventoryGateways(), + deviceRuntimeGateway: runtime.gateway, + trackDownloadableArtifact, + }); + await withHttp(handler, async (post, base) => { + const capture = { command: 'screenshot', session: 'capture', positionals: [destination] }; + const first = successfulData( + await post( + { + ...capture, + meta: { clientArtifactPaths: { path: '/client/first.png' } }, + }, + false, + ), + ); + if (!placeholder) { + const second = successfulData(await post(capture, false)); + expect(second.path).toBe(destination); + } + const artifactId = first.artifacts?.[0]?.artifactId; + expect(artifactId).toBeTypeOf('string'); + const download = await fetch(`${base}/artifacts/${artifactId}`, { + headers: { authorization: 'Bearer test-token' }, + }); + expect(download.status).toBe(200); + expect(Buffer.from(await download.arrayBuffer())).toEqual(original); + if (placeholder) expect(fs.existsSync(destination)).toBe(false); + else expect(fs.readFileSync(destination)).toEqual(replacement); + }); + }, +); test('direct HTTP recording replay requires known remote output provenance', async () => { const harness = makeRecordRuntimeHarness('legacy-remote-recording-'); @@ -76,10 +90,16 @@ test('direct HTTP recording replay requires known remote output provenance', asy fs.writeFileSync(input.outputPath, 'recorded-video'); return await start(input); }); - const started = await harness.run(['start'], { + const placeholder = buildRemoteTempArtifactPath('recording', '.mp4'); + const started = await harness.run(['start', placeholder], { clientArtifactPaths: { outPath: '/client/recording.mp4' }, }); expect(started.ok).toBe(true); + const capturePath = harness.runtime.start.mock.calls[0]![0].outputPath; + expect(path.dirname(capturePath)).toBe( + harness.sessionStore.resolveSessionDir(harness.sessionName), + ); + expect(fs.existsSync(placeholder)).toBe(false); expect((await harness.run(['stop'])).ok).toBe(true); const resourcePath = screenRecordingResourceStore.resolvePath( harness.sessionStore.resolveSessionDir(harness.sessionName), @@ -100,6 +120,30 @@ test('direct HTTP recording replay requires known remote output provenance', asy expect((await replay()).error?.data?.details).toMatchObject({ reason: 'LEASE_OUTPUT_MISMATCH', }); + const savedCompletion = metadata.completion; + if ( + !savedCompletion || + typeof savedCompletion !== 'object' || + Array.isArray(savedCompletion) + ) { + throw new Error('Expected stored recording completion'); + } + fs.writeFileSync(placeholder, 'legacy-recording'); + try { + screenRecordingResourceStore.write(resourcePath, { + ...record.envelope, + metadata: { + ...metadata, + remoteCaptureLease: 'unleased', + completion: { ...savedCompletion, outPath: placeholder }, + }, + }); + expect((await replay()).error?.data?.details).toMatchObject({ + reason: 'LEASE_OUTPUT_MISMATCH', + }); + } finally { + fs.rmSync(placeholder, { force: true }); + } screenRecordingResourceStore.write(resourcePath, { ...record.envelope, metadata: { ...metadata, remoteCaptureLease: 'unleased' }, @@ -144,14 +188,83 @@ test('record stop keeps the unleased producer when the stop runs under a lease', harness.sessionStore.update(harness.sessionStore.lookup(harness.sessionName)!, { lease: { leaseId: lease.leaseId, tenantId: lease.tenantId, runId: lease.runId }, }); - const output = successfulData(await post({ command: 'record', positionals: ['stop'] })); + const output = successfulData( + await post({ + command: 'record', + positionals: ['stop'], + meta: { tenantId: lease.tenantId }, + }), + ); const id = output.artifacts?.[0]?.artifactId; expect(id).toBeTypeOf('string'); - const download = await fetch(`${base}/artifacts/${id}`, { - headers: { authorization: 'Bearer test-token' }, + const url = `${base}/artifacts/${id}`; + const other = await fetch(url, { + headers: { authorization: 'Bearer test-token', [DAEMON_HTTP_TENANT_HEADER]: 'tenant-b' }, + }); + expect(other.status).toBe(401); + const download = await fetch(url, { + headers: { + authorization: 'Bearer test-token', + [DAEMON_HTTP_TENANT_HEADER]: lease.tenantId, + }, }); expect(download.status).toBe(200); expect(await download.text()).toBe('unleased-video'); }, ); }); + +test('a downloadable trace accepts events and stop only from its producing lease', async () => { + const sessionStore = makeSessionStore('leased-trace-session-'); + sessionStore.publish('capture', makeAndroidSession('capture')); + const leaseRegistry = new LeaseRegistry(); + const scope = { tenantId: 'tenant', runId: 'trace', leaseBackend: 'android-instance' } as const; + const { leaseId } = leaseRegistry.allocateLease(scope); + const owner = { ...scope, leaseId }; + const handler = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(sessionStore.resolveDaemonStateDir(), 'daemon.log'), + token: 'test-token', + sessionStore, + leaseRegistry, + deviceInventoryGateways: createTestDeviceInventoryGateways(), + trackDownloadableArtifact, + }); + await withHttp(handler, async (post, base) => { + const trace = (action: string, meta = {}) => + post({ + command: 'trace', + session: 'capture', + positionals: [action], + meta, + }); + successfulData(await trace('start', owner)); + const wait = (requestId: string, meta = {}) => + post({ + command: 'wait', + session: 'capture', + positionals: ['1'], + meta: { ...meta, requestId }, + }); + successfulData(await wait('trace-other-request', { tenantId: scope.tenantId })); + successfulData(await wait('trace-owner-request', owner)); + expect((await trace('stop', { tenantId: scope.tenantId })).error?.data?.details).toMatchObject({ + reason: 'LEASE_OUTPUT_MISMATCH', + }); + const id = successfulData(await trace('stop', owner)).artifacts?.[0]?.artifactId; + const url = `${base}/artifacts/${id}`; + const headers = { + authorization: 'Bearer test-token', + [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId, + }; + expect((await fetch(url, { headers })).status).toBe(401); + const served = await fetch(url, { + headers: { ...headers, [DAEMON_HTTP_LEASE_HEADER]: leaseId }, + }); + expect(served.status).toBe(200); + const log = await served.text(); + expect(log).toContain('trace-owner-request'); + expect(log).not.toContain('trace-other-request'); + }); +}); diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index 9862dc09ad..d8ff296a83 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -13,7 +13,10 @@ import type { } from '@agent-device/contracts/perf-runtime'; import type { DaemonArtifact } from '@agent-device/kernel/contracts'; import type { DaemonRequest } from '../../core/daemon-request.ts'; -import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; +import { + DAEMON_HTTP_LEASE_HEADER, + DAEMON_HTTP_TENANT_HEADER, +} from '@agent-device/contracts/daemon-http'; import { parsePerfRuntimeRequest } from '@agent-device/capture-kit/perf-runtime-plan'; import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; @@ -432,7 +435,7 @@ test.each( { kind: 'xctrace', area: 'trace' }, ] as const ).flatMap((capture) => - (['remote', 'local', 'remote-local-stop', 'relocated'] as const).map((origin) => ({ + (['remote', 'local', 'remote-local-stop', 'relocated', 'leased'] as const).map((origin) => ({ ...capture, origin, })), @@ -460,13 +463,15 @@ test.each( }, }; }; + const leaseRegistry = new LeaseRegistry(); + const ownership = nativePerfOwnership(leaseRegistry, origin === 'leased'); const handler = createRequestHandler({ host: createTestHostEnvironment(), createCommandSurface: screenshotCommandSurface, logPath: path.join(root, 'daemon.log'), token: 'test-token', sessionStore, - leaseRegistry: new LeaseRegistry(), + leaseRegistry, deviceInventoryGateways: createTestDeviceInventoryGateways(), deviceRuntimeGateway: { ...base, @@ -563,18 +568,22 @@ test.each( command: 'perf', session: 'capture', positionals: positionals('start'), + meta: ownership.meta, ...(origin === 'local' ? { flags: { out: callerOutput } } : {}), }, origin !== 'local', ); expect(successfulData(started).artifacts).toBeUndefined(); + await ownership.refuseUnowned(() => + post({ command: 'perf', session: 'capture', positionals: positionals('stop') }), + ); const stopped = await post( { command: 'perf', session: 'capture', positionals: positionals('stop'), ...(origin === 'relocated' ? { flags: { out: callerOutput } } : {}), - meta: { clientArtifactPaths: { outPath: '/client/capture' } }, + meta: { ...ownership.meta, clientArtifactPaths: { outPath: '/client/capture' } }, }, origin !== 'remote-local-stop' && origin !== 'relocated', ); @@ -596,7 +605,7 @@ test.each( const output = stoppedData.outPath as string; const restoreProfile = replaceXctraceProfile(kind, area, output); const download = await fetch(`${baseUrl}/artifacts/${artifact?.artifactId}`, { - headers: { authorization: 'Bearer test-token' }, + headers: ownership.headers, }); expect(download.status).toBe(200); const payload = Buffer.from(await download.arrayBuffer()); @@ -610,15 +619,19 @@ test.each( expect( fs.readFileSync(kind === 'xctrace' ? path.join(output, 'capture.bin') : output, 'utf8'), ).toBe('generated-capture'); + await ownership.refuseUnowned(() => + post({ command: 'perf', session: 'capture', positionals: positionals('report') }), + ); const report = await post({ command: 'perf', session: 'capture', positionals: positionals('report'), + meta: ownership.meta, }); const reportData = successfulData(report); const reportDownload = await fetch( `${baseUrl}/artifacts/${reportData.artifacts?.[0]?.artifactId}`, - { headers: { authorization: 'Bearer test-token' } }, + { headers: ownership.headers }, ); expect(reportDownload.status).toBe(200); expect(await reportDownload.text()).toBe('generated-report'); @@ -907,3 +920,23 @@ function replaceXctraceProfile(kind: string, area: string, output: string): () = fs.writeFileSync(profile, 'later-profile'); return () => fs.writeFileSync(profile, 'generated-capture'); } + +function nativePerfOwnership(registry: LeaseRegistry, leased: boolean) { + const scope = { tenantId: 'tenant', runId: 'perf', leaseBackend: 'android-instance' } as const; + const lease = leased ? registry.allocateLease(scope) : undefined; + return { + meta: lease ? { ...scope, leaseId: lease.leaseId } : {}, + headers: { + authorization: 'Bearer test-token', + ...(lease + ? { [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId, [DAEMON_HTTP_LEASE_HEADER]: lease.leaseId } + : {}), + }, + refuseUnowned: async (run: () => Promise<{ error?: { data?: unknown } }>) => { + if (lease) + expect((await run()).error?.data).toMatchObject({ + details: { reason: 'LEASE_OUTPUT_MISMATCH' }, + }); + }, + }; +} diff --git a/packages/daemon/src/server/request-diagnostics-http.ts b/packages/daemon/src/server/request-diagnostics-http.ts index 39643be8fb..2a1176eb62 100644 --- a/packages/daemon/src/server/request-diagnostics-http.ts +++ b/packages/daemon/src/server/request-diagnostics-http.ts @@ -144,19 +144,19 @@ async function handleRequestDiagnostics( } const owner = diagnosticsRecordOwner(ref.requestId); - const presentedLeaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; - const presented = typeof presentedLeaseId === 'string' ? presentedLeaseId : undefined; - if (owner !== null && !requestHoldsOutputLease(owner, presented)) { - sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); - return; + let recordPath = resolveRecordPath(ref, 'request-records'); + let stats = owner === null ? null : await statRecord(recordPath); + if (stats && owner !== null) { + const leaseId = req.headers[DAEMON_HTTP_LEASE_HEADER]; + if (!requestHoldsOutputLease(owner, typeof leaseId === 'string' ? leaseId : undefined)) { + sendRestJsonError(res, normalizeError(leasedOutputRefusal('Diagnostics record'))); + return; + } + } else { + // Only the host can attest a legacy record by moving it out of the old request-id namespace. + recordPath = resolveRecordPath(ref, 'requests-unleased'); + stats = await statRecord(recordPath); } - - // Only the host can attest a legacy record by moving it out of the old request-id namespace. - const recordPath = resolveRecordPath( - ref, - owner === null ? 'requests-unleased' : 'request-records', - ); - const stats = await statRecord(recordPath); if (!stats) { sendRestJsonError(res, normalizeError(missingRecordError(ref))); return; diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index f06f55ed72..a3bbfb060f 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -13,6 +13,7 @@ import type { DaemonResponse } from '@agent-device/kernel/contracts'; import type { CommandFlags } from '@agent-device/contracts/command'; import type { LeaseLifecycleProvider, ProviderAppCatalog } from '@agent-device/contracts/device'; import { + requestTraceLogPath, requestDiagnosticsRecordId, type TrackDownloadableArtifact, } from '../resources/artifact-tracking.ts'; @@ -741,7 +742,7 @@ export async function prepareLockedRequestScope(params: { const existingRef = binding.existingRef; const existingSession = existingRef?.session; const deliverRemoteScript = remoteScriptVersionDelivery(sessionStore, scope.sessionName); - updateDiagnosticsScope({ traceLogPath: existingSession?.trace?.outPath }); + updateDiagnosticsScope({ traceLogPath: requestTraceLogPath(lockedReq, existingSession?.trace) }); const finalize = (handled: DaemonResponse): DaemonResponse => { const response = deliverRemoteScript(handled); const finalized = finalizeDaemonResponse(lockedReq, response, trackDownloadableArtifact); diff --git a/packages/daemon/src/server/request-finalization.ts b/packages/daemon/src/server/request-finalization.ts index 7e13dfaef2..2ddbfe0fc2 100644 --- a/packages/daemon/src/server/request-finalization.ts +++ b/packages/daemon/src/server/request-finalization.ts @@ -99,13 +99,12 @@ function registerDownloadableArtifacts( if (!data) return data; const admittedLeaseId = req.internal?.admittedLease?.leaseId; const producer = captureOutputLeaseVerifier(data); - const owner = - producer === undefined ? admittedLeaseId && leaseVerifier(admittedLeaseId) : producer; - const producerTenant = captureOutputTenantId(data); - const tenantId = producer === undefined ? (producerTenant ?? req.meta?.tenantId) : producerTenant; + const admittedOwner = admittedLeaseId && leaseVerifier(admittedLeaseId); + const tenantId = captureOutputTenantId(data) ?? req.meta?.tenantId; const register = (artifact: PendingArtifact): DaemonArtifact => { const artifactPath = artifact.path as string; const delivered = daemonDeliveredArtifact(artifact); + const owner = delivered || producer === undefined ? admittedOwner : producer; return { field: artifact.field, // Omitted (not null/undefined-valued) when untyped, matching the diff --git a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts index 3901d03d2a..a760e10c13 100644 --- a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts +++ b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts @@ -12,10 +12,7 @@ import { import { createDurableResourceEnvelope } from '@agent-device/capture-kit'; import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store'; import { resolveScreenRecordingStopRecovery } from '@agent-device/capture-kit/screen-recording-stop-recovery'; -import { - DAEMON_HTTP_LEASE_HEADER, - buildRemoteTempArtifactPath, -} from '@agent-device/contracts/daemon-http'; +import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http'; import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime'; import type { ScreenRecordingCompletion, @@ -71,13 +68,19 @@ afterEach(async () => { vi.useRealTimers(); }); -type RecordingExport = { outPath: string; clientOutPath?: string; telemetryPath: string }; +type RecordingExport = { + outPath: string; + clientOutPath?: string; + telemetryPath: string; + daemonOwned?: true; +}; function remoteRecordingExport(): RecordingExport { - const outPath = buildRemoteTempArtifactPath('recording', 'mp4'); + const outPath = path.join(mkdtempForTestSync('daemon-recording-export-'), 'checkout.mp4'); return { outPath, clientOutPath: '/Users/client/checkout.mp4', + daemonOwned: true, telemetryPath: deriveRecordingTelemetryPath(outPath), }; } @@ -146,7 +149,8 @@ function publishRecordingSession( descriptor: { version: 1, body: { recordingId: 'checkout-flow' } }, metadata: { phase: 'active', - remoteCaptureTenantId: TENANT, + ...(recordingExport.daemonOwned ? { remoteCaptureOutput: true } : {}), + ...(options.unleasedProducer ? {} : { remoteCaptureTenantId: TENANT }), remoteCaptureLease: options.leased === false || options.unleasedProducer ? 'unleased' : leaseVerifier(LEASE_ID), }, @@ -407,6 +411,7 @@ test('teardown keeps the recording unleased when the session later acquired a le await teardown(sessionStore); + expect(await listDownloadableArtifacts({ tenantId: 'tenant-b' }, process.env)).toEqual([]); const artifacts = await listDownloadableArtifacts({ tenantId: TENANT }, process.env); expect(artifacts).toEqual( expect.arrayContaining([ diff --git a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-start.test.ts b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-start.test.ts index 39f87b5de6..32a9b4b027 100644 --- a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-start.test.ts +++ b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-start.test.ts @@ -50,12 +50,12 @@ test('record start without a path records a remote caller default on the daemon ok: true, data: { outPath: '/client/project/recording-1.mp4' }, }); - expect(harness.runtime.start).toHaveBeenCalledWith( - expect.objectContaining({ - outputPath: expect.stringMatching(/^\/tmp\/agent-device-recording-\d+-[a-z0-9]+\.mp4$/), - clientOutputPath: '/client/project/recording-1.mp4', - }), + const [input] = harness.runtime.start.mock.calls[0]!; + expect(path.dirname(input.outputPath)).toBe( + harness.sessionStore.resolveSessionDir(harness.sessionName), ); + expect(path.basename(input.outputPath)).toMatch(/^recording-[a-f0-9-]{36}\.mp4$/); + expect(input.clientOutputPath).toBe('/client/project/recording-1.mp4'); }); test.each(['linux', 'vega'] as const)( diff --git a/packages/daemon/src/sessions/handlers/record-runtime-response.ts b/packages/daemon/src/sessions/handlers/record-runtime-response.ts index 82ffd934ef..7dd7caac40 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime-response.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime-response.ts @@ -22,14 +22,10 @@ export function recordingOutputLeaseVerifier( return undefined; } -export function isDaemonOwnedRecordingOutput( - completion: ScreenRecordingCompletion, - remoteCaptureOutput: boolean, -): boolean { +export function isLegacyRemoteRecordingOutput(completion: ScreenRecordingCompletion): boolean { return ( - remoteCaptureOutput || - (completion.clientOutPath !== undefined && - isRemoteTempArtifactPath(completion.outPath, 'recording', path.extname(completion.outPath))) + completion.clientOutPath !== undefined && + isRemoteTempArtifactPath(completion.outPath, 'recording', path.extname(completion.outPath)) ); } diff --git a/packages/daemon/src/sessions/handlers/record-runtime.ts b/packages/daemon/src/sessions/handlers/record-runtime.ts index 2a1c59afdc..e0957c5c10 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime.ts @@ -71,14 +71,14 @@ import { recordingExtensionForPlatform, resolveRecordingOutputPaths, } from '@agent-device/capture-kit/recording-output-path'; -import { buildRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http'; +import { isRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http'; import { randomUUID } from 'node:crypto'; import { buildRecordingStartResponse, buildRecordingStartedAction, buildRecordingStopResponse, buildRecordingUnsupportedResponse, - isDaemonOwnedRecordingOutput, + isLegacyRemoteRecordingOutput, recordingOutputLeaseVerifier, UNLEASED_CAPTURE, } from './record-runtime-response.ts'; @@ -291,7 +291,7 @@ async function startRecording( owner: runtime.owner, fence, ...started, - envelope: withCaptureOwner(started.envelope, params.req), + envelope: withCaptureOwner(started.envelope, params.req, outputPaths.daemonOwned === true), throwIfCanceled: params.throwIfCanceled, }); const adoptedRef = ref ?? draft!.requireRef(); @@ -319,6 +319,7 @@ async function startRecording( function withCaptureOwner( envelope: DurableResourceEnvelope<'screen-recording'>, req: DaemonRequest, + daemonOwned: boolean, ): DurableResourceEnvelope<'screen-recording'> { const lease = req.internal?.admittedLease; const tenantId = lease?.tenantId ?? req.meta?.tenantId; @@ -327,7 +328,7 @@ function withCaptureOwner( ...envelope, metadata: { ...envelope.metadata, - ...(req.internal?.publicNetworkOnly ? { remoteCaptureOutput: true } : {}), + ...(daemonOwned ? { remoteCaptureOutput: true } : {}), ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), remoteCaptureLease: leaseId ? leaseVerifier(leaseId) : UNLEASED_CAPTURE, }, @@ -354,7 +355,8 @@ function prepareRecordingStart(params: RecordRuntimeHandlerParams, session: Sess }; } -type RecordingStartPaths = RecordingOutputPaths & Readonly<{ clientOutputPath?: string }>; +type RecordingStartPaths = RecordingOutputPaths & + Readonly<{ clientOutputPath?: string; daemonOwned?: true }>; function resolveRecordingStartPaths( req: DaemonRequest, @@ -364,7 +366,13 @@ function resolveRecordingStartPaths( ): RecordingStartPaths { const requestedPath = req.positionals?.[1]; const clientOutputPath = req.meta?.clientArtifactPaths?.outPath; - if (req.internal?.publicNetworkOnly) { + const remotePlaceholder = + requestedPath !== undefined && + isRemoteTempArtifactPath(requestedPath, 'recording', path.extname(requestedPath)); + if ( + req.internal?.publicNetworkOnly || + (clientOutputPath !== undefined && (requestedPath === undefined || remotePlaceholder)) + ) { const extension = recordingExtensionForPlatform(platform); return { ...resolveRecordingOutputPaths({ @@ -372,24 +380,12 @@ function resolveRecordingStartPaths( platform, cwd: hostCwd, }), + daemonOwned: true, ...(clientOutputPath === undefined ? {} : { clientOutputPath: appendRecordingExtensionWhenMissing(clientOutputPath, extension) }), }; } - if (requestedPath === undefined && clientOutputPath !== undefined) { - // A remote caller that names no path cannot know the session platform's extension, so it sends - // its local destination without one; its cwd does not exist on this host. - const extension = recordingExtensionForPlatform(platform); - return { - ...resolveRecordingOutputPaths({ - requestedPath: buildRemoteTempArtifactPath('recording', extension), - platform, - cwd: hostCwd, - }), - clientOutputPath: appendRecordingExtensionWhenMissing(clientOutputPath, extension), - }; - } return { ...resolveRecordingOutputPaths({ requestedPath, @@ -456,19 +452,7 @@ async function stopRecording( } const completion = stopped.completion; const response = buildRecordingStopResponse(completion); - const resourcePath = screenRecordingDurableResource.resourcePath( - params.sessionStore, - params.sessionName, - ); - const record = screenRecordingDurableResource.store.read(resourcePath); - const daemonOwned = isDaemonOwnedRecordingOutput( - completion, - record.status === 'decoded' && record.envelope.metadata?.remoteCaptureOutput === true, - ); - if (daemonOwned && leaseOwner === undefined) throw leasedOutputRefusal('Recording'); - const storedTenant = - record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureTenantId : undefined; - const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; + const { daemonOwned, tenantId } = readRecordingOutput(params, completion, leaseOwner); if (stopped.recordsSessionAction) { recordSessionAction(params.sessionStore, ref, params.req, params.req.command, { action: 'stop', @@ -496,6 +480,27 @@ async function stopRecording( : response; } +function readRecordingOutput( + params: RecordRuntimeHandlerParams, + completion: ScreenRecordingCompletion, + leaseOwner: string | null | undefined, +): { daemonOwned: boolean; tenantId?: string } { + const resourcePath = screenRecordingDurableResource.resourcePath( + params.sessionStore, + params.sessionName, + ); + const record = screenRecordingDurableResource.store.read(resourcePath); + const daemonOwned = + record.status === 'decoded' && record.envelope.metadata?.remoteCaptureOutput === true; + if (isLegacyRemoteRecordingOutput(completion) || (daemonOwned && leaseOwner === undefined)) { + throw leasedOutputRefusal('Recording'); + } + const storedTenant = + record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureTenantId : undefined; + const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; + return { daemonOwned, tenantId }; +} + /** * Refuses a stop that the owner of the recording did not send, and returns the verifier of the * lease that owns the export. A completed export needs only the lease id of its owner, also after diff --git a/packages/daemon/src/sessions/handlers/trace-runtime.ts b/packages/daemon/src/sessions/handlers/trace-runtime.ts index 556240ea0b..12d942f53a 100644 --- a/packages/daemon/src/sessions/handlers/trace-runtime.ts +++ b/packages/daemon/src/sessions/handlers/trace-runtime.ts @@ -2,6 +2,12 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value'; import type { DaemonResponse } from '@agent-device/kernel/contracts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import fs from 'node:fs'; +import { randomUUID } from 'node:crypto'; +import { updateDiagnosticsScope } from '@agent-device/host-kit/diagnostics'; +import { + admittedOutputLeaseVerifier, + leasedOutputRefusal, +} from '../../resources/artifact-tracking.ts'; import path from 'node:path'; import { parseTraceAction, @@ -39,18 +45,21 @@ function startTrace( ): DaemonResponse { const session = sessionStore.requireCurrent(ref); if (session.trace) return errorResponse('INVALID_ARGS', 'trace already in progress'); - const outPath = expandSessionPath( - req.positionals?.[1] ?? sessionStore.defaultTracePath(session), - requestCwd(req, host), - ); + const daemonOwned = req.internal?.publicNetworkOnly === true; + const outPath = daemonOwned + ? path.join(sessionStore.ensureSessionDir(ref.address), `trace-${randomUUID()}.log`) + : expandSessionPath( + req.positionals?.[1] ?? sessionStore.defaultTracePath(session), + requestCwd(req, host), + ); fs.mkdirSync(path.dirname(outPath), { recursive: true }); fs.appendFileSync(outPath, ''); session.trace = { - outPath, + ...(daemonOwned ? { remoteCaptureOutputPath: outPath } : { outPath }), startedAt: Date.now(), clientOutPath: req.meta?.clientArtifactPaths?.outPath, - ...(req.internal?.publicNetworkOnly ? { remoteCaptureOutputPath: outPath } : {}), - remoteCaptureTenantId: session.lease?.tenantId ?? req.meta?.tenantId, + remoteCaptureTenantId: req.internal?.admittedLease?.tenantId ?? req.meta?.tenantId, + leaseVerifier: admittedOutputLeaseVerifier(req), }; recordSessionAction(sessionStore, ref, req, req.command, { action: 'start', outPath }); return { @@ -67,9 +76,12 @@ function stopTrace( ): DaemonResponse { const session = sessionStore.requireCurrent(ref); if (!session.trace) return errorResponse('INVALID_ARGS', 'no active trace'); + const owner = session.trace.leaseVerifier ?? null; + if (owner !== admittedOutputLeaseVerifier(req)) throw leasedOutputRefusal('Trace'); + updateDiagnosticsScope({ traceLogPath: undefined }); const outPath = relocateTraceOutput( requestCwd(req, host), - session.trace.outPath, + session.trace.remoteCaptureOutputPath ?? session.trace.outPath, req.positionals?.[1], ); const daemonOwned = session.trace.remoteCaptureOutputPath === outPath; @@ -98,6 +110,7 @@ function stopTrace( } satisfies TraceCommandResult, daemonOwned ? 'daemon' : 'caller', tenantId, + owner, ), }; } diff --git a/packages/daemon/src/sessions/leased-recording-retention.ts b/packages/daemon/src/sessions/leased-recording-retention.ts index a90bec43ee..2352121722 100644 --- a/packages/daemon/src/sessions/leased-recording-retention.ts +++ b/packages/daemon/src/sessions/leased-recording-retention.ts @@ -10,16 +10,15 @@ import { trackDownloadableArtifact, } from '../resources/artifact-tracking.ts'; import { - isDaemonOwnedRecordingOutput, + isLegacyRemoteRecordingOutput, recordingStopArtifacts, recordingOutputLeaseVerifier, } from './handlers/record-runtime-response.ts'; /** * Keeps the export of a recording that a leased session's teardown finished, so a request that sends - * the ended lease's id can still download it (ADR 0024 rule 1). Remote admission marks session-owned output in the durable - * envelope. Remote client exports served by local HTTP use the generated daemon temp path. - * A local caller's export already sits at the path that the caller named. + * the producing lease's id can still download it (ADR 0024 rule 1). The durable envelope marks + * daemon-owned exports. A caller-owned export stays at the path the caller named. */ export function retainLeasedSessionRecording( ref: SessionRef, @@ -27,12 +26,12 @@ export function retainLeasedSessionRecording( metadata: DurableResourceEnvelope['metadata'], ): void { const lease = ref.session.lease; - if (!lease || !isDaemonOwnedRecordingOutput(completion, metadata?.remoteCaptureOutput === true)) + if (!lease || metadata?.remoteCaptureOutput !== true || isLegacyRemoteRecordingOutput(completion)) return; const owner = recordingOutputLeaseVerifier(metadata); if (owner === undefined) return; const storedTenant = metadata?.remoteCaptureTenantId; - const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; + const tenantId = typeof storedTenant === 'string' ? storedTenant : lease.tenantId; const sessionName = tenantPublicSessionName(lease.tenantId, ref.address); const artifacts = recordingStopArtifacts(completion).filter( (artifact) => typeof artifact.path === 'string', diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index b55d68541c..291dcf37ec 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -40,6 +40,10 @@ import { requestCwd } from '../../../core/context.ts'; import type { SessionRef, SessionState } from '../../../core/session-state.ts'; import { bindSessionPerfCapture } from '../../../core/session-capture-binding.ts'; import { withCaptureOutputOwnership } from '../../../core/capture-output-ownership.ts'; +import { + admittedOutputLeaseVerifier, + leasedOutputRefusal, +} from '../../../resources/artifact-tracking.ts'; import { recordSessionAction } from '../../../core/session-action-recorder.ts'; import { admitRuntimePlan, @@ -170,8 +174,10 @@ async function executeAdmittedPerfPlan( return await startPerfCapture(params, session, runtime, plan.request); } case 'profile-report': { + const retained = session.lastPerfProfile; + assertPerfOutputOwner(params.req, retained?.leaseVerifier); const runtime = await bindDevice(device, plan.use); - const last = session.lastPerfProfile; + const last = retained?.profile; const tracePath = plan.request.tracePath ?? last?.outPath; if (!tracePath) { throw new AppError( @@ -199,7 +205,8 @@ async function executeAdmittedPerfPlan( data: withCaptureOutputOwnership( { perf: 'reported', ...data }, params.req.internal?.publicNetworkOnly ? 'daemon' : 'caller', - session.lease?.tenantId ?? params.req.meta?.tenantId, + retained?.tenantId, + retained?.leaseVerifier, ), }; } @@ -229,13 +236,12 @@ async function startPerfCapture( : request.kind === 'perfetto' ? 'app.perfetto-trace' : 'cpu.perf.data'; - const outPath = - request.kind === 'xctrace' && params.req.internal?.publicNetworkOnly - ? path.join( - params.sessionStore.ensureSessionDir(params.sessionName), - `${randomUUID()}-${fallback}`, - ) - : resolveNativeOutPath(params, request.outPath, fallback); + const outPath = params.req.internal?.publicNetworkOnly + ? path.join( + params.sessionStore.ensureSessionDir(params.sessionName), + `${randomUUID()}-${fallback}`, + ) + : resolveNativeOutPath(params, request.outPath, fallback); const resourcePath = perfCaptureDurableResource.store.resolvePath( params.sessionStore.resolveSessionDir(params.sessionName), ); @@ -259,7 +265,7 @@ async function startPerfCapture( outPath, fence, }); - const tenantId = session.lease?.tenantId ?? params.req.meta?.tenantId; + const tenantId = params.req.internal?.admittedLease?.tenantId ?? params.req.meta?.tenantId; await adoptStartedPerfCapture({ admissionLedger: requirePerfCaptureAdmissionLedger(params), binding, @@ -267,16 +273,15 @@ async function startPerfCapture( owner: runtime.owner, fence, pendingHandle: started.pendingHandle, - envelope: params.req.internal?.publicNetworkOnly - ? { - ...started.envelope, - metadata: { - ...started.envelope.metadata, - remoteCaptureOutputPath: outPath, - ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), - }, - } - : started.envelope, + envelope: { + ...started.envelope, + metadata: { + ...started.envelope.metadata, + ...(params.req.internal?.publicNetworkOnly ? { remoteCaptureOutputPath: outPath } : {}), + ...(tenantId ? { remoteCaptureTenantId: tenantId } : {}), + remoteCaptureLease: admittedOutputLeaseVerifier(params.req), + }, + }, throwIfCanceled: params.throwIfCanceled, }); return { ok: true, data: started.response }; @@ -298,6 +303,10 @@ async function stopPerfCapture( ): Promise { const capture = session.perfCapture; if (!capture) return errorResponse('INVALID_ARGS', 'no active native perf capture'); + const metadata = capture.envelope.metadata; + const owner = readString(metadata?.remoteCaptureLease) ?? null; + assertPerfOutputOwner(params.req, owner); + const tenantId = readString(metadata?.remoteCaptureTenantId); const snapshot = capture.handle.inspect(); const mismatchMessage = perfCaptureStopMismatch(snapshot, request); if (mismatchMessage) return errorResponse('INVALID_ARGS', mismatchMessage); @@ -312,16 +321,13 @@ async function stopPerfCapture( }); if (request.area === 'cpu') { const profile = readProfileHandoff(completion); - params.sessionStore.update(params.ref, { lastPerfProfile: profile }); + params.sessionStore.update(params.ref, { + lastPerfProfile: { profile, leaseVerifier: owner, tenantId }, + }); } const clientOutPath = params.req.meta?.clientArtifactPaths?.outPath ?? capture.clientOutPath; - const tenantId = - typeof capture.envelope.metadata?.remoteCaptureTenantId === 'string' - ? capture.envelope.metadata.remoteCaptureTenantId - : session.lease?.tenantId; const outPath = readString(completion.outPath); - const daemonOwned = - outPath !== undefined && capture.envelope.metadata?.remoteCaptureOutputPath === outPath; + const daemonOwned = outPath !== undefined && metadata?.remoteCaptureOutputPath === outPath; const artifacts = daemonOwned && completion.kind === 'xctrace' ? [await publishXctraceDownload(outPath, clientOutPath, request.area === 'cpu')] @@ -336,10 +342,16 @@ async function stopPerfCapture( }, daemonOwned ? 'daemon' : 'caller', tenantId, + owner, ), }; } +function assertPerfOutputOwner(req: DaemonRequest, owner: string | null | undefined): void { + if (owner && owner !== admittedOutputLeaseVerifier(req)) + throw leasedOutputRefusal('Performance capture'); +} + async function publishXctraceDownload( outPath: string, clientOutPath: string | undefined, diff --git a/test/wire-compat/ledger.json b/test/wire-compat/ledger.json index a332467a0b..8f2b9aa846 100644 --- a/test/wire-compat/ledger.json +++ b/test/wire-compat/ledger.json @@ -86,7 +86,7 @@ "packages/daemon/src/server/request-diagnostics-http.ts#REQUEST_DIAGNOSTICS_CONTENT_TYPE": "sha256:16bbd9ae7eaeaaa8c8cbd6e65c4ce786a6c5230c5351288228bbf1a6f11bdcdc", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpAuthorizer": "sha256:24b0fa6b1d965e5cf6503a4665148dc6360c56bf5a0ae710eeaa76a73a62c52a", "packages/daemon/src/server/request-diagnostics-http.ts#RequestDiagnosticsHttpOptions": "sha256:58fc1eb61b082b64eb9714ae76ef6f3ff44102a72736db52ce0b9214dca7f509", - "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:a374824d0af99b2a40f95a4a4f9981f4446a5dd7df427f3fdc9852350f4b73b8", + "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics": "sha256:c5cdf275ff5f00b4edbd7c1d456465da340505dc07199ece624ce0fd645d2a78", "packages/daemon/src/server/request-diagnostics-http.ts#resolveRequestDiagnosticsHttpRoute": "sha256:4ff07623b27a6f7461e1addb87aabd149904da63f7462149ae4114d14336cde0", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonProgressEnvelope": "sha256:16162d01cfc43fc6a0198dbba3358981c1a278a70f7494ebe70d051f517cfb22", "packages/daemon-contracts/src/request-progress-protocol.ts#DaemonResponseEnvelope": "sha256:202ae64836af890549a1d95963903f9a506294b80bd0862c1b708e72d7f286d0", @@ -315,7 +315,7 @@ }, { "declaration": "packages/daemon/src/server/request-diagnostics-http.ts#handleRequestDiagnostics", - "digest": "sha256:a374824d0af99b2a40f95a4a4f9981f4446a5dd7df427f3fdc9852350f4b73b8", + "digest": "sha256:c5cdf275ff5f00b4edbd7c1d456465da340505dc07199ece624ce0fd645d2a78", "rationale": "The existing locator and NDJSON response keep their shape. Only the server storage lookup and lease authorization change." }, { From 3a78c03b46896df885b27b04b7da1e35121e203a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 14:48:27 +0100 Subject: [PATCH 11/15] fix(daemon): preserve trace stop and assign perf reports to requesters --- packages/daemon/src/core/session-state.ts | 1 - .../http-server-capture-publication.test.ts | 117 ++++++++++-------- .../http/http-server-remote-output.test.ts | 53 +++++--- .../src/sessions/handlers/trace-runtime.ts | 2 +- .../internal/session-perf-runtime.ts | 5 +- 5 files changed, 104 insertions(+), 74 deletions(-) diff --git a/packages/daemon/src/core/session-state.ts b/packages/daemon/src/core/session-state.ts index 55a367e11c..1766b77680 100644 --- a/packages/daemon/src/core/session-state.ts +++ b/packages/daemon/src/core/session-state.ts @@ -260,7 +260,6 @@ export type SessionState = { lastPerfProfile?: { profile: PerfProfileHandoff; leaseVerifier: string | null; - tenantId?: string; }; /** Native sampler mechanics stay behind the adopted runtime handle. */ audioProbe?: { diff --git a/packages/daemon/src/server/http/http-server-capture-publication.test.ts b/packages/daemon/src/server/http/http-server-capture-publication.test.ts index 913a196613..a74745b7ff 100644 --- a/packages/daemon/src/server/http/http-server-capture-publication.test.ts +++ b/packages/daemon/src/server/http/http-server-capture-publication.test.ts @@ -214,57 +214,70 @@ test('record stop keeps the unleased producer when the stop runs under a lease', ); }); -test('a downloadable trace accepts events and stop only from its producing lease', async () => { - const sessionStore = makeSessionStore('leased-trace-session-'); - sessionStore.publish('capture', makeAndroidSession('capture')); - const leaseRegistry = new LeaseRegistry(); - const scope = { tenantId: 'tenant', runId: 'trace', leaseBackend: 'android-instance' } as const; - const { leaseId } = leaseRegistry.allocateLease(scope); - const owner = { ...scope, leaseId }; - const handler = createRequestHandler({ - host: createTestHostEnvironment(), - createCommandSurface: screenshotCommandSurface, - logPath: path.join(sessionStore.resolveDaemonStateDir(), 'daemon.log'), - token: 'test-token', - sessionStore, - leaseRegistry, - deviceInventoryGateways: createTestDeviceInventoryGateways(), - trackDownloadableArtifact, - }); - await withHttp(handler, async (post, base) => { - const trace = (action: string, meta = {}) => - post({ - command: 'trace', - session: 'capture', - positionals: [action], - meta, - }); - successfulData(await trace('start', owner)); - const wait = (requestId: string, meta = {}) => - post({ - command: 'wait', - session: 'capture', - positionals: ['1'], - meta: { ...meta, requestId }, - }); - successfulData(await wait('trace-other-request', { tenantId: scope.tenantId })); - successfulData(await wait('trace-owner-request', owner)); - expect((await trace('stop', { tenantId: scope.tenantId })).error?.data?.details).toMatchObject({ - reason: 'LEASE_OUTPUT_MISMATCH', +test.each(['leased at start', 'leased after start'])( + 'a downloadable trace preserves producer ownership when the session is %s', + async (scenario) => { + const sessionStore = makeSessionStore('leased-trace-session-'); + sessionStore.publish('capture', makeAndroidSession('capture')); + const leaseRegistry = new LeaseRegistry(); + const scope = { tenantId: 'tenant', runId: 'trace', leaseBackend: 'android-instance' } as const; + const { leaseId } = leaseRegistry.allocateLease(scope); + const owner = { ...scope, leaseId }; + const handler = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(sessionStore.resolveDaemonStateDir(), 'daemon.log'), + token: 'test-token', + sessionStore, + leaseRegistry, + deviceInventoryGateways: createTestDeviceInventoryGateways(), + trackDownloadableArtifact, }); - const id = successfulData(await trace('stop', owner)).artifacts?.[0]?.artifactId; - const url = `${base}/artifacts/${id}`; - const headers = { - authorization: 'Bearer test-token', - [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId, - }; - expect((await fetch(url, { headers })).status).toBe(401); - const served = await fetch(url, { - headers: { ...headers, [DAEMON_HTTP_LEASE_HEADER]: leaseId }, + await withHttp(handler, async (post, base) => { + const trace = (action: string, meta = {}) => + post({ + command: 'trace', + session: 'capture', + positionals: [action], + meta, + }); + const leasedAtStart = scenario === 'leased at start'; + const unleased = { tenantId: scope.tenantId }; + successfulData(await trace('start', leasedAtStart ? owner : unleased)); + const wait = (requestId: string, meta = {}) => + post({ + command: 'wait', + session: 'capture', + positionals: ['1'], + meta: { ...meta, requestId }, + }); + if (leasedAtStart) { + successfulData(await wait('trace-other-request', unleased)); + successfulData(await wait('trace-owner-request', owner)); + expect((await trace('stop', unleased)).error?.data?.details).toMatchObject({ + reason: 'LEASE_OUTPUT_MISMATCH', + }); + } else { + successfulData(await wait('trace-owner-request', unleased)); + sessionStore.update(sessionStore.lookup('capture')!, { + lease: { ...owner, leaseBackend: scope.leaseBackend }, + }); + successfulData(await wait('trace-other-request', owner)); + } + const id = successfulData(await trace('stop', owner)).artifacts?.[0]?.artifactId; + const url = `${base}/artifacts/${id}`; + const headers = { + authorization: 'Bearer test-token', + [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId, + }; + if (leasedAtStart) expect((await fetch(url, { headers })).status).toBe(401); + const served = await fetch(url, { + headers: leasedAtStart ? { ...headers, [DAEMON_HTTP_LEASE_HEADER]: leaseId } : headers, + }); + expect(served.status).toBe(200); + const log = await served.text(); + expect(log).toContain('trace-owner-request'); + expect(log).not.toContain('trace-other-request'); }); - expect(served.status).toBe(200); - const log = await served.text(); - expect(log).toContain('trace-owner-request'); - expect(log).not.toContain('trace-other-request'); - }); -}); + }, +); diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index d8ff296a83..f03686401d 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -426,8 +426,8 @@ test('nested remote dispatch normalizes host outputs again below HTTP', async () if (response.ok) expect(response.data?.artifacts?.[0]?.artifactId).toBeTypeOf('string'); }); -test.each( - ( +test.each([ + ...( [ { kind: 'simpleperf', area: 'cpu' }, { kind: 'xctrace', area: 'cpu' }, @@ -440,7 +440,8 @@ test.each( origin, })), ), -)( + { kind: 'simpleperf', area: 'cpu', origin: 'report-requester' } as const, +])( '$origin $kind $area capture keeps producer output ownership through stop', async ({ kind, area, origin }) => { const root = mkdtempForTestSync('remote-native-perf-'); @@ -464,7 +465,7 @@ test.each( }; }; const leaseRegistry = new LeaseRegistry(); - const ownership = nativePerfOwnership(leaseRegistry, origin === 'leased'); + const { ownership, reporter } = nativePerfOwners(leaseRegistry, origin); const handler = createRequestHandler({ host: createTestHostEnvironment(), createCommandSurface: screenshotCommandSurface, @@ -626,15 +627,12 @@ test.each( command: 'perf', session: 'capture', positionals: positionals('report'), - meta: ownership.meta, + meta: reporter.meta, }); const reportData = successfulData(report); - const reportDownload = await fetch( + await reporter.expectReport( `${baseUrl}/artifacts/${reportData.artifacts?.[0]?.artifactId}`, - { headers: ownership.headers }, ); - expect(reportDownload.status).toBe(200); - expect(await reportDownload.text()).toBe('generated-report'); } else await vi.waitFor(() => expect(fs.existsSync(output)).toBe(false)); }); }, @@ -921,16 +919,37 @@ function replaceXctraceProfile(kind: string, area: string, output: string): () = return () => fs.writeFileSync(profile, 'generated-capture'); } -function nativePerfOwnership(registry: LeaseRegistry, leased: boolean) { - const scope = { tenantId: 'tenant', runId: 'perf', leaseBackend: 'android-instance' } as const; +function nativePerfOwners(registry: LeaseRegistry, origin: string) { + if (origin === 'report-requester') { + return { + ownership: nativePerfOwnership(registry, false, 'capture-tenant'), + reporter: nativePerfOwnership(registry, true, 'report-tenant'), + }; + } + const ownership = nativePerfOwnership(registry, origin === 'leased'); + return { ownership, reporter: ownership }; +} + +function nativePerfOwnership(registry: LeaseRegistry, leased: boolean, tenantId?: string) { + const scope = { + tenantId: tenantId ?? 'tenant', + runId: 'perf', + leaseBackend: 'android-instance', + } as const; const lease = leased ? registry.allocateLease(scope) : undefined; + const headers = { + authorization: 'Bearer test-token', + ...(lease || tenantId ? { [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId } : {}), + }; + const leasedHeaders = lease ? { ...headers, [DAEMON_HTTP_LEASE_HEADER]: lease.leaseId } : headers; return { - meta: lease ? { ...scope, leaseId: lease.leaseId } : {}, - headers: { - authorization: 'Bearer test-token', - ...(lease - ? { [DAEMON_HTTP_TENANT_HEADER]: scope.tenantId, [DAEMON_HTTP_LEASE_HEADER]: lease.leaseId } - : {}), + meta: lease ? { ...scope, leaseId: lease.leaseId } : { tenantId }, + headers: leasedHeaders, + expectReport: async (url: string) => { + if (lease) expect((await fetch(url, { headers })).status).toBe(401); + const download = await fetch(url, { headers: leasedHeaders }); + expect(download.status).toBe(200); + expect(await download.text()).toBe('generated-report'); }, refuseUnowned: async (run: () => Promise<{ error?: { data?: unknown } }>) => { if (lease) diff --git a/packages/daemon/src/sessions/handlers/trace-runtime.ts b/packages/daemon/src/sessions/handlers/trace-runtime.ts index 12d942f53a..c1e6abee38 100644 --- a/packages/daemon/src/sessions/handlers/trace-runtime.ts +++ b/packages/daemon/src/sessions/handlers/trace-runtime.ts @@ -77,7 +77,7 @@ function stopTrace( const session = sessionStore.requireCurrent(ref); if (!session.trace) return errorResponse('INVALID_ARGS', 'no active trace'); const owner = session.trace.leaseVerifier ?? null; - if (owner !== admittedOutputLeaseVerifier(req)) throw leasedOutputRefusal('Trace'); + if (owner && owner !== admittedOutputLeaseVerifier(req)) throw leasedOutputRefusal('Trace'); updateDiagnosticsScope({ traceLogPath: undefined }); const outPath = relocateTraceOutput( requestCwd(req, host), diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index 291dcf37ec..0dce29eeb1 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -205,8 +205,7 @@ async function executeAdmittedPerfPlan( data: withCaptureOutputOwnership( { perf: 'reported', ...data }, params.req.internal?.publicNetworkOnly ? 'daemon' : 'caller', - retained?.tenantId, - retained?.leaseVerifier, + params.req.internal?.admittedLease?.tenantId, ), }; } @@ -322,7 +321,7 @@ async function stopPerfCapture( if (request.area === 'cpu') { const profile = readProfileHandoff(completion); params.sessionStore.update(params.ref, { - lastPerfProfile: { profile, leaseVerifier: owner, tenantId }, + lastPerfProfile: { profile, leaseVerifier: owner }, }); } const clientOutPath = params.req.meta?.clientArtifactPaths?.outPath ?? capture.clientOutPath; From 99f5e205c359f6b7992dc3dee9d9ade15bf7daec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 16:24:04 +0100 Subject: [PATCH 12/15] fix(daemon): preserve capture destinations and scope trace writers --- .../__tests__/runner-process-launch.test.ts | 38 ++++++++++++- packages/apple-runner/src/host.ts | 6 +- packages/apple-runner/src/runner-artifact.ts | 7 ++- packages/apple-runner/src/runner-io.ts | 19 ++++++- .../src/runner-launched-session.ts | 10 +++- .../apple-runner/src/runner-process-launch.ts | 6 +- .../daemon/src/resources/artifact-tracking.ts | 21 ++++--- .../http-server-capture-publication.test.ts | 36 +++++++++++- .../http/http-server-remote-output.test.ts | 29 +++++++++- .../src/server/request-execution-scope.ts | 4 +- packages/daemon/src/server/request-router.ts | 1 + ...ay-runtime-maestro-shell-artifacts.test.ts | 46 ++++++++------- .../src/sessions/handlers/record-runtime.ts | 16 +++++- .../internal/session-perf-runtime.ts | 25 +++++++-- packages/host-kit/src/diagnostics.ts | 1 + packages/host-kit/src/internal/diagnostics.ts | 56 ++++++++++++++++--- .../platform-apple/src/core/runner-host.ts | 7 ++- .../session-replay-runtime-plan.ts | 2 +- .../src/daemon-port/session-replay-trace.ts | 9 ++- .../test-kit/src/host-kit-capabilities.ts | 2 + 20 files changed, 274 insertions(+), 67 deletions(-) diff --git a/packages/apple-runner/src/__tests__/runner-process-launch.test.ts b/packages/apple-runner/src/__tests__/runner-process-launch.test.ts index 8d7d9b3efb..174836578c 100644 --- a/packages/apple-runner/src/__tests__/runner-process-launch.test.ts +++ b/packages/apple-runner/src/__tests__/runner-process-launch.test.ts @@ -1,7 +1,8 @@ import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; -import { beforeEach, test, vi } from 'vitest'; +import { beforeEach, onTestFinished, test, vi } from 'vitest'; +import { loadRealHostKitCapabilities } from '@agent-device/test-kit'; import { IOS_SIMULATOR } from './device-fixtures.ts'; import { makeBackgroundRunner } from './runner-session-fixtures.ts'; import { mkdtempForTestSync } from './tmp-dir.ts'; @@ -124,6 +125,41 @@ test('the listener-ready marker is read back from the runner log file', async () await waitFor(() => assert.equal(launched.startupRetryWake.aborted, true)); }); +test('runner trace output is scoped to the trace and the request that started the runner', async () => { + const { withDiagnosticsScope } = await loadRealHostKitCapabilities(); + const logPath = runnerLogPath(); + const traceLogPath = path.join(path.dirname(logPath), 'trace.log'); + let finish = () => {}; + const wait = new Promise<{ stdout: string; stderr: string; exitCode: number }>((resolve) => { + finish = () => resolve({ stdout: '', stderr: '', exitCode: 0 }); + }); + onTestFinished(finish); + mockRunCmdBackground.mockReturnValue({ ...makeBackgroundRunner(4242), wait }); + let traceActive = true; + const launched = await withDiagnosticsScope( + { traceLogPath, traceLogWritable: () => traceActive }, + async () => { + const runner = launchRunnerProcess({ + device: IOS_SIMULATOR, + xctestrunPath: '/tmp/runner.xctestrun', + derivedPath: '/tmp/runner-derived', + logPath, + }); + fs.appendFileSync(logPath, 'runner output\n'); + await waitFor(() => assert.equal(fs.readFileSync(traceLogPath, 'utf8'), 'runner output\n')); + traceActive = false; + fs.appendFileSync(logPath, 'AGENT_DEVICE_RUNNER_LISTENER_READY\n'); + await waitFor(() => assert.equal(runner.startupRetryWake.aborted, true)); + return runner; + }, + ); + traceActive = true; + fs.appendFileSync(logPath, 'after producer request\n'); + finish(); + await launched.output; + assert.equal(fs.readFileSync(traceLogPath, 'utf8'), 'runner output\n'); +}); + test('output an older runner generation left in the same log file does not wake startup', async () => { const logPath = runnerLogPath(); fs.writeFileSync(logPath, 'AGENT_DEVICE_RUNNER_LISTENER_READY\n'); diff --git a/packages/apple-runner/src/host.ts b/packages/apple-runner/src/host.ts index 8ce159ac86..fa6bc74e5c 100644 --- a/packages/apple-runner/src/host.ts +++ b/packages/apple-runner/src/host.ts @@ -97,7 +97,10 @@ export type AppleRunnerHost = Pick< | 'isCommandTimeoutError' | 'commandDeveloperDir' > & - Pick & + Pick< + typeof HostDiagnostics, + 'captureDiagnosticTraceWriter' | 'emitDiagnostic' | 'withDiagnosticTimer' + > & Pick & Pick< typeof HostProcess, @@ -196,6 +199,7 @@ export const isCommandTimeoutError = delegate('isCommandTimeoutError'); export const commandDeveloperDir = delegate('commandDeveloperDir'); export const hostEnvironment = delegate('hostEnvironment'); export const shellQuote = delegate('shellQuote'); +export const captureDiagnosticTraceWriter = delegate('captureDiagnosticTraceWriter'); export const emitDiagnostic = delegate('emitDiagnostic'); export const withDiagnosticTimer = delegate('withDiagnosticTimer'); export const retryWithPolicy = delegate('retryWithPolicy'); diff --git a/packages/apple-runner/src/runner-artifact.ts b/packages/apple-runner/src/runner-artifact.ts index b8f2bce018..c7866db963 100644 --- a/packages/apple-runner/src/runner-artifact.ts +++ b/packages/apple-runner/src/runner-artifact.ts @@ -21,7 +21,7 @@ import { import type { ExecBackgroundResult } from '@agent-device/host-kit/command'; import type { DeviceInfo } from '@agent-device/kernel/device'; import { classifyRunnerStartupFailure } from './runner-error-classification.ts'; -import { logChunk } from './runner-io.ts'; +import { bindRunnerTraceOutput, logChunk } from './runner-io.ts'; import { runnerLaunchLogClassificationDetails, runnerSimulatorSetFailureDetails, @@ -900,6 +900,7 @@ async function buildRunnerXctestrun( const performanceBuildSettings = resolveRunnerPerformanceBuildSettings(); const archBuildSettings = resolveRunnerArchBuildSettings(env); const sandboxBuildArgs = resolveRunnerSandboxBuildArgs(env); + const writeTrace = bindRunnerTraceOutput(options.traceLogPath); try { await runCmdStreaming( 'xcodebuild', @@ -932,10 +933,10 @@ async function buildRunnerXctestrun( registerRunnerPrepProcess(device.id, child, options.startAdmission); }, onStdoutChunk: (chunk) => { - logChunk(chunk, options.logPath, options.traceLogPath, options.verbose); + logChunk(chunk, options.logPath, writeTrace, options.verbose); }, onStderrChunk: (chunk) => { - logChunk(chunk, options.logPath, options.traceLogPath, options.verbose); + logChunk(chunk, options.logPath, writeTrace, options.verbose); }, }, ); diff --git a/packages/apple-runner/src/runner-io.ts b/packages/apple-runner/src/runner-io.ts index dd447dcb61..622079644a 100644 --- a/packages/apple-runner/src/runner-io.ts +++ b/packages/apple-runner/src/runner-io.ts @@ -1,3 +1,4 @@ +import { captureDiagnosticTraceWriter } from './host.ts'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; @@ -20,14 +21,28 @@ export async function getFreePort(): Promise { }); } +export function bindRunnerTraceOutput( + path: string | undefined, +): ((chunk: string) => void) | undefined { + const write = captureDiagnosticTraceWriter(); + if (!write) return path ? (chunk) => appendLogChunk(path, chunk) : undefined; + return (chunk) => { + try { + write(chunk); + } catch { + // A trace write must not interrupt runner startup or output. + } + }; +} + export function logChunk( chunk: string, logPath?: string, - traceLogPath?: string, + writeTrace?: (chunk: string) => void, verbose?: boolean, ): void { if (logPath) appendLogChunk(logPath, chunk); - if (traceLogPath) appendLogChunk(traceLogPath, chunk); + writeTrace?.(chunk); if (verbose) { process.stderr.write(chunk); } diff --git a/packages/apple-runner/src/runner-launched-session.ts b/packages/apple-runner/src/runner-launched-session.ts index fbd1094444..c3dabea0a6 100644 --- a/packages/apple-runner/src/runner-launched-session.ts +++ b/packages/apple-runner/src/runner-launched-session.ts @@ -4,7 +4,12 @@ import type { DeviceInfo } from '@agent-device/kernel/device'; import { Deadline, emitDiagnostic } from './host.ts'; import { disposeRunnerSession, waitForRunnerLaunchExit } from './runner-disposal.ts'; import { mintRunnerToken } from './runner-contract.ts'; -import { logChunk, resolveRunnerLaunchLogPath, flushRunnerLogAppends } from './runner-io.ts'; +import { + bindRunnerTraceOutput, + logChunk, + resolveRunnerLaunchLogPath, + flushRunnerLogAppends, +} from './runner-io.ts'; import type { RunnerLauncher, RunnerLaunchObservation } from './runner-launcher.ts'; import type { AppleRunnerLifecycleOptions } from './runner-provider.ts'; import { @@ -111,10 +116,11 @@ export async function launchRunnerSession( session.retention?.cancel(); }; void launch.exited.then(() => settled(), settled); + const writeTrace = bindRunnerTraceOutput(options.traceLogPath); observation.output = (async () => { for await (const text of launch.output) { output = (output + text).slice(-16_384); - logChunk(text, logPath, options.traceLogPath, options.verbose); + logChunk(text, logPath, writeTrace, options.verbose); } })().catch(async (error: unknown) => { observation.failure = error; diff --git a/packages/apple-runner/src/runner-process-launch.ts b/packages/apple-runner/src/runner-process-launch.ts index 6413cb74fd..08ed54db5a 100644 --- a/packages/apple-runner/src/runner-process-launch.ts +++ b/packages/apple-runner/src/runner-process-launch.ts @@ -10,6 +10,7 @@ import { resolveRunnerMaxConcurrentDestinationsFlag } from './runner-cache-metad import { xcodebuildDestinationArgs } from './runner-device-set.ts'; import { createRunnerLogFile, + bindRunnerTraceOutput, logChunk, readRunnerLogTail, tailRunnerLogFile, @@ -97,12 +98,13 @@ export function launchRunnerProcess(input: LaunchRunnerProcessInput): LaunchedRu captureOutput: false, }, ); + const writeTrace = bindRunnerTraceOutput(input.traceLogPath); const logTail = tailRunnerLogFile({ file: logFile, onOutput: (chunk) => { listenerReady.observe(chunk); - if (input.traceLogPath || input.verbose) { - logChunk(chunk, undefined, input.traceLogPath, input.verbose); + if (writeTrace || input.verbose) { + logChunk(chunk, undefined, writeTrace, input.verbose); } }, }); diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 8718ed0e23..a7a70cbed6 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -24,7 +24,8 @@ import { } from '@agent-device/host-kit/diagnostics'; import { timingSafeStringEqual } from '@agent-device/host-kit/transport'; import type { DaemonRequest } from '../core/daemon-request.ts'; -import type { SessionState } from '../core/session-state.ts'; +import type { SessionRef } from '../core/session-state.ts'; +import type { SessionStore } from '../core/session-store.ts'; import { resolveLeaseScope } from '../core/lease-context.ts'; import { normalizeLeaseId } from './lease-registry-scope.ts'; @@ -47,13 +48,19 @@ export function admittedOutputLeaseVerifier(req: Pick return leaseId === undefined ? null : leaseVerifier(leaseId); } -export function requestTraceLogPath( +export function requestTraceDiagnostics( req: DaemonRequest, - trace: SessionState['trace'], -): string | undefined { - if (!trace || (trace.leaseVerifier ?? null) !== admittedOutputLeaseVerifier(req)) - return undefined; - return trace.remoteCaptureOutputPath ?? trace.outPath; + ref: SessionRef | undefined, + store: Pick, +) { + const trace = ref?.session.trace; + if (!ref || !trace || (trace.leaseVerifier ?? null) !== admittedOutputLeaseVerifier(req)) { + return { traceLogPath: undefined, traceLogWritable: undefined }; + } + return { + traceLogPath: trace.remoteCaptureOutputPath ?? trace.outPath, + traceLogWritable: () => store.resolveCurrent(ref)?.trace === trace, + }; } /** diff --git a/packages/daemon/src/server/http/http-server-capture-publication.test.ts b/packages/daemon/src/server/http/http-server-capture-publication.test.ts index a74745b7ff..29e6e13bec 100644 --- a/packages/daemon/src/server/http/http-server-capture-publication.test.ts +++ b/packages/daemon/src/server/http/http-server-capture-publication.test.ts @@ -91,11 +91,15 @@ test('direct HTTP recording replay requires known remote output provenance', asy return await start(input); }); const placeholder = buildRemoteTempArtifactPath('recording', '.mp4'); + const clientPath = '/client/recording'; const started = await harness.run(['start', placeholder], { - clientArtifactPaths: { outPath: '/client/recording.mp4' }, + clientArtifactPaths: { outPath: clientPath }, }); expect(started.ok).toBe(true); - const capturePath = harness.runtime.start.mock.calls[0]![0].outputPath; + const capture = harness.runtime.start.mock.calls[0]![0]; + expect(capture.clientOutputPath).toBe(clientPath); + const capturePath = capture.outputPath; + expect(path.extname(capturePath)).toBe('.mp4'); expect(path.dirname(capturePath)).toBe( harness.sessionStore.resolveSessionDir(harness.sessionName), ); @@ -149,6 +153,7 @@ test('direct HTTP recording replay requires known remote output provenance', asy metadata: { ...metadata, remoteCaptureLease: 'unleased' }, }); const data = successfulData(await replay()); + expect(data.artifacts?.[0]?.localPath).toBe(clientPath); const id = data.artifacts?.[0]?.artifactId; const download = await fetch(`${base}/artifacts/${id}`, { headers: { authorization: 'Bearer test-token' }, @@ -192,7 +197,6 @@ test('record stop keeps the unleased producer when the stop runs under a lease', await post({ command: 'record', positionals: ['stop'], - meta: { tenantId: lease.tenantId }, }), ); const id = output.artifacts?.[0]?.artifactId; @@ -244,6 +248,22 @@ test.each(['leased at start', 'leased after start'])( const leasedAtStart = scenario === 'leased at start'; const unleased = { tenantId: scope.tenantId }; successfulData(await trace('start', leasedAtStart ? owner : unleased)); + const script = 'flow.ad'; + successfulData( + await post({ + command: 'replay', + session: 'capture', + positionals: [script], + flags: { + replayKeepSession: true, + replayScriptSource: { + entry: script, + files: { [script]: 'context platform=android\nwait 0\n' }, + }, + }, + meta: { ...(leasedAtStart ? owner : unleased), sessionExplicit: true }, + }), + ); const wait = (requestId: string, meta = {}) => post({ command: 'wait', @@ -276,6 +296,16 @@ test.each(['leased at start', 'leased after start'])( }); expect(served.status).toBe(200); const log = await served.text(); + const events = log + .trim() + .split('\n') + .map((line) => JSON.parse(line)); + expect(events).toEqual( + expect.arrayContaining([ + expect.objectContaining({ type: 'replay_action_start' }), + expect.objectContaining({ type: 'replay_action_stop' }), + ]), + ); expect(log).toContain('trace-owner-request'); expect(log).not.toContain('trace-other-request'); }); diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index f03686401d..c0c2f1b8bd 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -464,6 +464,7 @@ test.each([ }, }; }; + const explicitTrace = path.join(root, 'explicit.trace'); const leaseRegistry = new LeaseRegistry(); const { ownership, reporter } = nativePerfOwners(leaseRegistry, origin); const handler = createRequestHandler({ @@ -544,13 +545,18 @@ test.each([ response: snapshot(false), }; }, - perfProfileReport: async ({ tracePath, outPath }) => { + perfProfileReport: async ({ tracePath, outPath, profile, template }) => { + const explicit = tracePath === explicitTrace; + if (explicit) { + expect(profile).toBeUndefined(); + expect(template).toBeUndefined(); + } expect( fs.readFileSync( kind === 'xctrace' ? path.join(tracePath, 'capture.bin') : tracePath, 'utf8', ), - ).toBe('generated-capture'); + ).toBe(explicit ? 'independent-capture' : 'generated-capture'); fs.writeFileSync(outPath, 'generated-report'); return { outPath }; }, @@ -623,6 +629,25 @@ test.each([ await ownership.refuseUnowned(() => post({ command: 'perf', session: 'capture', positionals: positionals('report') }), ); + if (kind === 'xctrace' && origin === 'leased') { + fs.mkdirSync(explicitTrace); + fs.writeFileSync(path.join(explicitTrace, 'capture.bin'), 'independent-capture'); + const reportPath = path.join(root, 'explicit-report.json'); + const explicitReport = (source: string) => + post( + { + command: 'perf', + session: 'capture', + positionals: [...positionals('report'), '', reportPath, source], + }, + false, + ); + successfulData(await explicitReport(explicitTrace)); + expect(fs.readFileSync(reportPath, 'utf8')).toBe('generated-report'); + const alias = path.join(root, 'retained-alias.trace'); + fs.symlinkSync(output, alias, 'dir'); + await ownership.refuseUnowned(() => explicitReport(alias)); + } const report = await post({ command: 'perf', session: 'capture', diff --git a/packages/daemon/src/server/request-execution-scope.ts b/packages/daemon/src/server/request-execution-scope.ts index a3bbfb060f..d0b52f27bf 100644 --- a/packages/daemon/src/server/request-execution-scope.ts +++ b/packages/daemon/src/server/request-execution-scope.ts @@ -13,7 +13,7 @@ import type { DaemonResponse } from '@agent-device/kernel/contracts'; import type { CommandFlags } from '@agent-device/contracts/command'; import type { LeaseLifecycleProvider, ProviderAppCatalog } from '@agent-device/contracts/device'; import { - requestTraceLogPath, + requestTraceDiagnostics, requestDiagnosticsRecordId, type TrackDownloadableArtifact, } from '../resources/artifact-tracking.ts'; @@ -742,7 +742,7 @@ export async function prepareLockedRequestScope(params: { const existingRef = binding.existingRef; const existingSession = existingRef?.session; const deliverRemoteScript = remoteScriptVersionDelivery(sessionStore, scope.sessionName); - updateDiagnosticsScope({ traceLogPath: requestTraceLogPath(lockedReq, existingSession?.trace) }); + updateDiagnosticsScope(requestTraceDiagnostics(lockedReq, existingRef, sessionStore)); const finalize = (handled: DaemonResponse): DaemonResponse => { const response = deliverRemoteScript(handled); const finalized = finalizeDaemonResponse(lockedReq, response, trackDownloadableArtifact); diff --git a/packages/daemon/src/server/request-router.ts b/packages/daemon/src/server/request-router.ts index 100b99b3fe..0ddbb134f0 100644 --- a/packages/daemon/src/server/request-router.ts +++ b/packages/daemon/src/server/request-router.ts @@ -234,6 +234,7 @@ export function createRequestHandler(deps: RequestRouterDeps): DaemonInvokeFn { command: req.command, debug, logPath, + traceLogPath: undefined, }, async () => { claimRequestDiagnosticsRecord(req); diff --git a/packages/daemon/src/sessions/__tests__/replay-maestro/session-replay-runtime-maestro-shell-artifacts.test.ts b/packages/daemon/src/sessions/__tests__/replay-maestro/session-replay-runtime-maestro-shell-artifacts.test.ts index adc7949fc1..2f9104dc53 100644 --- a/packages/daemon/src/sessions/__tests__/replay-maestro/session-replay-runtime-maestro-shell-artifacts.test.ts +++ b/packages/daemon/src/sessions/__tests__/replay-maestro/session-replay-runtime-maestro-shell-artifacts.test.ts @@ -3,6 +3,7 @@ import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { test } from 'vitest'; +import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics'; import { createLiveTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; import { SessionStore } from '../../../core/session-store.ts'; import { replayScriptSourceBundleFor } from '../../../__tests__/support/replay-script-source-fixture.ts'; @@ -56,30 +57,33 @@ test('runReplayCommand writes per-action timing events to active trace', async ( booted: true, }, createdAt: Date.now(), - trace: { outPath: tracePath, startedAt: Date.now() }, actions: [], }); - const response = await runReplayForTest({ - req: { - token: 't', - session: 's', - command: 'replay', - positionals: [scriptPath], - flags: { replayScriptSource: replayScriptSourceBundleFor(scriptPath) }, - meta: { cwd: root }, - }, - sessionName: 's', - logPath: path.join(root, 'log'), - sessionStore, - invoke: async (req) => ({ - ok: true, - data: - req.command === 'click' - ? { timing: { totalDurationMs: 12, internal: { ignored: true } } } - : {}, - }), - }); + const response = await withDiagnosticsScope( + { traceLogPath: tracePath }, + async () => + await runReplayForTest({ + req: { + token: 't', + session: 's', + command: 'replay', + positionals: [scriptPath], + flags: { replayScriptSource: replayScriptSourceBundleFor(scriptPath) }, + meta: { cwd: root }, + }, + sessionName: 's', + logPath: path.join(root, 'log'), + sessionStore, + invoke: async (req) => ({ + ok: true, + data: + req.command === 'click' + ? { timing: { totalDurationMs: 12, internal: { ignored: true } } } + : {}, + }), + }), + ); assert.equal(response.ok, true); const events = fs diff --git a/packages/daemon/src/sessions/handlers/record-runtime.ts b/packages/daemon/src/sessions/handlers/record-runtime.ts index e0957c5c10..4806ceb7d9 100644 --- a/packages/daemon/src/sessions/handlers/record-runtime.ts +++ b/packages/daemon/src/sessions/handlers/record-runtime.ts @@ -383,7 +383,12 @@ function resolveRecordingStartPaths( daemonOwned: true, ...(clientOutputPath === undefined ? {} - : { clientOutputPath: appendRecordingExtensionWhenMissing(clientOutputPath, extension) }), + : { + clientOutputPath: + remotePlaceholder && !req.internal?.publicNetworkOnly + ? clientOutputPath + : appendRecordingExtensionWhenMissing(clientOutputPath, extension), + }), }; } return { @@ -452,7 +457,7 @@ async function stopRecording( } const completion = stopped.completion; const response = buildRecordingStopResponse(completion); - const { daemonOwned, tenantId } = readRecordingOutput(params, completion, leaseOwner); + const { daemonOwned, tenantId } = readRecordingOutput(params, completion, leaseOwner, ref); if (stopped.recordsSessionAction) { recordSessionAction(params.sessionStore, ref, params.req, params.req.command, { action: 'stop', @@ -484,6 +489,7 @@ function readRecordingOutput( params: RecordRuntimeHandlerParams, completion: ScreenRecordingCompletion, leaseOwner: string | null | undefined, + ref: SessionRef | undefined, ): { daemonOwned: boolean; tenantId?: string } { const resourcePath = screenRecordingDurableResource.resourcePath( params.sessionStore, @@ -497,7 +503,11 @@ function readRecordingOutput( } const storedTenant = record.status === 'decoded' ? record.envelope.metadata?.remoteCaptureTenantId : undefined; - const tenantId = typeof storedTenant === 'string' ? storedTenant : undefined; + const tenantId = + typeof storedTenant === 'string' + ? storedTenant + : ((ref && params.sessionStore.resolveCurrent(ref)?.lease?.tenantId) ?? + params.req.meta?.tenantId); return { daemonOwned, tenantId }; } diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index 0dce29eeb1..50146947c7 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -2,6 +2,7 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value'; import type { DaemonArtifact, DaemonResponse } from '@agent-device/kernel/contracts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import path from 'node:path'; +import { statSync } from 'node:fs'; import { randomUUID } from 'node:crypto'; import { cp, rename, rm } from 'node:fs/promises'; import { type PerfCaptureAdmissionLedger } from '@agent-device/capture-kit/perf-capture-admission-ledger'; @@ -174,11 +175,19 @@ async function executeAdmittedPerfPlan( return await startPerfCapture(params, session, runtime, plan.request); } case 'profile-report': { - const retained = session.lastPerfProfile; + const cwd = requestCwd(params.req, params.host); + const explicitPath = plan.request.tracePath && expandSessionPath(plan.request.tracePath, cwd); + const saved = session.lastPerfProfile; + const retained = + plan.request.kind === 'xctrace' && + explicitPath && + saved && + !samePerfSource(explicitPath, saved.profile.outPath) + ? undefined + : saved; + const tracePath = explicitPath || retained?.profile.outPath; assertPerfOutputOwner(params.req, retained?.leaseVerifier); - const runtime = await bindDevice(device, plan.use); const last = retained?.profile; - const tracePath = plan.request.tracePath ?? last?.outPath; if (!tracePath) { throw new AppError( 'INVALID_ARGS', @@ -191,11 +200,12 @@ async function executeAdmittedPerfPlan( 'perf cpu profile report requires a stopped profile trace; stop the active capture first.', ); } + const runtime = await bindDevice(device, plan.use); const outPath = resolveNativeOutPath(params, plan.request.outPath, 'cpu-report.json'); const data = await runtime.operations.perfProfileReport({ appId: session.appBundleId, kind: plan.request.kind, - tracePath: expandSessionPath(tracePath, requestCwd(params.req, params.host)), + tracePath, outPath, template: plan.request.template ?? (last?.kind === 'xctrace' ? last.template : undefined), profile: last, @@ -346,6 +356,13 @@ async function stopPerfCapture( }; } +function samePerfSource(explicitPath: string, retainedPath: string): boolean { + if (explicitPath === retainedPath) return true; + const explicit = statSync(explicitPath, { throwIfNoEntry: false }); + const retained = statSync(retainedPath, { throwIfNoEntry: false }); + return !!explicit && !!retained && explicit.dev === retained.dev && explicit.ino === retained.ino; +} + function assertPerfOutputOwner(req: DaemonRequest, owner: string | null | undefined): void { if (owner && owner !== admittedOutputLeaseVerifier(req)) throw leasedOutputRefusal('Performance capture'); diff --git a/packages/host-kit/src/diagnostics.ts b/packages/host-kit/src/diagnostics.ts index 4da4b7136f..cc397411cf 100644 --- a/packages/host-kit/src/diagnostics.ts +++ b/packages/host-kit/src/diagnostics.ts @@ -7,6 +7,7 @@ export { emitDiagnostic, flushDiagnosticsToSessionFile, getDiagnosticsMeta, + captureDiagnosticTraceWriter, registerDiagnosticSensitiveValue, type ResourceDiagnostic, toDiagnosticJson, diff --git a/packages/host-kit/src/internal/diagnostics.ts b/packages/host-kit/src/internal/diagnostics.ts index de44b23932..4c71dde79a 100644 --- a/packages/host-kit/src/internal/diagnostics.ts +++ b/packages/host-kit/src/internal/diagnostics.ts @@ -53,6 +53,7 @@ type DiagnosticsScopeOptions = { */ operatorLog?: boolean; traceLogPath?: string; + traceLogWritable?: () => boolean; }; type DiagnosticsScope = DiagnosticsScopeOptions & { @@ -67,6 +68,7 @@ type DiagnosticsScope = DiagnosticsScopeOptions & { phaseCounts: Map; sensitiveValues: Set; ensuredDirectories: Set; + traceLogClosed?: true; // Sorted-longest-first view of `sensitiveValues`, recomputed lazily after a // registration invalidates it so replacement order stays deterministic. sortedSensitiveValues?: string[]; @@ -99,7 +101,13 @@ export async function withDiagnosticsScope( sensitiveValues: new Set(), ensuredDirectories: new Set(), }; - return await diagnosticsStorage.run(scope, fn); + try { + return await diagnosticsStorage.run(scope, fn); + } finally { + scope.traceLogClosed = true; + scope.traceLogPath = undefined; + scope.traceLogWritable = undefined; + } } export function updateDiagnosticsScope(options: DiagnosticsScopeOptions): void { @@ -108,6 +116,31 @@ export function updateDiagnosticsScope(options: DiagnosticsScopeOptions): void { Object.assign(scope, options); } +/** + * Binds a raw trace writer to the current request and trace. An inactive binding still suppresses + * fallback to an unverified session path. + */ +export function captureDiagnosticTraceWriter(): ((line: string) => void) | undefined { + const scope = diagnosticsStorage.getStore(); + if (!scope || !('traceLogPath' in scope)) return undefined; + const { traceLogPath } = scope; + return (line) => { + if (traceLogPath && activeTracePath(scope) === traceLogPath) { + appendDiagnosticLine( + scope, + traceLogPath, + redactSensitiveString(line, sortedScopeSensitiveValues(scope)), + ); + } + }; +} + +function activeTracePath(scope: DiagnosticsScope): string | undefined { + if (!scope.traceLogClosed && scope.traceLogPath && scope.traceLogWritable?.() !== false) + return scope.traceLogPath; + return undefined; +} + export function getDiagnosticsMeta(): { diagnosticId?: string; requestId?: string; @@ -203,16 +236,17 @@ export function emitDiagnostic(event: DiagnosticEventInput): void { }; scope.events.push(payload); scope.phaseCounts.set(event.phase, (scope.phaseCounts.get(event.phase) ?? 0) + 1); - if (!scope.debug && !scope.traceLogPath) return; + const tracePath = activeTracePath(scope); + if (!scope.debug && !tracePath) return; const fileLine = `${stringifyDiagnosticEvent(payload, sortedScopeSensitiveValues(scope))}\n`; if (scope.debug && scope.logPath) { appendDiagnosticLine(scope, scope.logPath, fileLine); scope.liveWrittenEventCount = scope.events.length; } - if (scope.traceLogPath) { - appendDiagnosticLine(scope, scope.traceLogPath, fileLine); + if (tracePath) { + appendDiagnosticLine(scope, tracePath, fileLine); } - if (scope.debug && !scope.logPath && !scope.traceLogPath) { + if (scope.debug && !scope.logPath && !tracePath) { // The file form stays exact; the terminal copy is escaped, and its `\u00NN` form keeps the JSON parseable. process.stderr.write(`${DIAGNOSTIC_STDERR_PREFIX}${escapeTerminalText(fileLine)}`); } @@ -340,13 +374,17 @@ function redactScopeData( } } +function redactSensitiveString(value: string, sensitiveValues: readonly string[]): string { + return sensitiveValues.reduce( + (output, sensitive) => output.replaceAll(sensitive, '[REDACTED]'), + value, + ); +} + function replaceSensitiveValues(value: unknown, sensitiveValues: readonly string[]): unknown { if (typeof value === 'bigint') value = value.toString(); if (typeof value === 'string') { - return sensitiveValues.reduce( - (output, sensitive) => output.replaceAll(sensitive, '[REDACTED]'), - value, - ); + return redactSensitiveString(value, sensitiveValues); } if (Array.isArray(value)) { return value.map((entry) => replaceSensitiveValues(entry, sensitiveValues)); diff --git a/packages/platform-apple/src/core/runner-host.ts b/packages/platform-apple/src/core/runner-host.ts index 72efa662ff..705631b1c7 100644 --- a/packages/platform-apple/src/core/runner-host.ts +++ b/packages/platform-apple/src/core/runner-host.ts @@ -10,7 +10,11 @@ import { } from '@agent-device/host-kit/command'; import { shellQuote } from '@agent-device/kernel/device-shell'; import { AppError, type ReasonTable } from '@agent-device/kernel/errors'; -import { emitDiagnostic, withDiagnosticTimer } from '@agent-device/host-kit/diagnostics'; +import { + captureDiagnosticTraceWriter, + emitDiagnostic, + withDiagnosticTimer, +} from '@agent-device/host-kit/diagnostics'; import { isProcessAlive, isProcessGroupAlive, @@ -87,6 +91,7 @@ export const appleRunnerHost: AppleRunnerHost = { return host; }, shellQuote, + captureDiagnosticTraceWriter, emitDiagnostic, withDiagnosticTimer, retryWithPolicy, diff --git a/packages/replay-port/src/daemon-port/session-replay-runtime-plan.ts b/packages/replay-port/src/daemon-port/session-replay-runtime-plan.ts index 24986b1b82..ec59e4b65e 100644 --- a/packages/replay-port/src/daemon-port/session-replay-runtime-plan.ts +++ b/packages/replay-port/src/daemon-port/session-replay-runtime-plan.ts @@ -150,7 +150,7 @@ export function prepareReplayPlan(params: { resolved, metadata, }), - actionTracePath: tracePath ?? preEntrySession?.trace?.outPath, + actionTracePath: tracePath, }, }; } diff --git a/packages/replay-port/src/daemon-port/session-replay-trace.ts b/packages/replay-port/src/daemon-port/session-replay-trace.ts index 2f314bfaee..c1bf29f379 100644 --- a/packages/replay-port/src/daemon-port/session-replay-trace.ts +++ b/packages/replay-port/src/daemon-port/session-replay-trace.ts @@ -1,14 +1,17 @@ import fs from 'node:fs'; import { redactDiagnosticData } from '@agent-device/kernel/redaction'; -import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; +import { captureDiagnosticTraceWriter, emitDiagnostic } from '@agent-device/host-kit/diagnostics'; export function appendReplayTraceEvent( tracePath: string | undefined, event: Record, ): void { - if (!tracePath) return; + const scopedWrite = tracePath ? undefined : captureDiagnosticTraceWriter(); + if (!tracePath && !scopedWrite) return; try { - fs.appendFileSync(tracePath, `${JSON.stringify(redactDiagnosticData(event))}\n`); + const line = `${JSON.stringify(redactDiagnosticData(event))}\n`; + if (tracePath) fs.appendFileSync(tracePath, line); + else scopedWrite?.(line); } catch (error) { emitDiagnostic({ level: 'warn', diff --git a/packages/test-kit/src/host-kit-capabilities.ts b/packages/test-kit/src/host-kit-capabilities.ts index a2f82efce8..b9b2f4200e 100644 --- a/packages/test-kit/src/host-kit-capabilities.ts +++ b/packages/test-kit/src/host-kit-capabilities.ts @@ -25,7 +25,9 @@ export async function loadRealHostKitCapabilities() { requireExecSuccess: command.requireExecSuccess, isCommandTimeoutError: command.isCommandTimeoutError, commandDeveloperDir: command.commandDeveloperDir, + captureDiagnosticTraceWriter: diagnostics.captureDiagnosticTraceWriter, emitDiagnostic: diagnostics.emitDiagnostic, + withDiagnosticsScope: diagnostics.withDiagnosticsScope, withDiagnosticTimer: diagnostics.withDiagnosticTimer, retryWithPolicy: retry.retryWithPolicy, isEnvTruthy: retry.isEnvTruthy, From b2e982f02ab1f1822d18a195b32278d2e0a10fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 17:34:56 +0100 Subject: [PATCH 13/15] fix(daemon): use canonical lease types for retained outputs --- packages/daemon/src/resources/artifact-tracking.ts | 2 +- .../server/http/http-server-capture-publication.test.ts | 8 +++++--- .../server/http/http-server-proxy-leased-output.test.ts | 3 ++- .../__tests__/record-runtime-stop-recovery.test.ts | 2 +- src/cli/connection/ended-lease.test.ts | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index a7a70cbed6..701eac1c0d 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -10,6 +10,7 @@ import { } from '@agent-device/kernel/errors'; import type { DaemonArtifactType } from '@agent-device/kernel/contracts'; import type { DeviceRotation } from '@agent-device/contracts/device'; +import { normalizeLeaseId } from '@agent-device/contracts/lease-scope'; import type { DaemonArtifactInventoryEntry } from '@agent-device/contracts/observability'; import type { PackedGzipTar } from '@agent-device/host-kit/gzip-tar'; import { @@ -27,7 +28,6 @@ import type { DaemonRequest } from '../core/daemon-request.ts'; import type { SessionRef } from '../core/session-state.ts'; import type { SessionStore } from '../core/session-store.ts'; import { resolveLeaseScope } from '../core/lease-context.ts'; -import { normalizeLeaseId } from './lease-registry-scope.ts'; // --- Leased output ownership --- diff --git a/packages/daemon/src/server/http/http-server-capture-publication.test.ts b/packages/daemon/src/server/http/http-server-capture-publication.test.ts index 29e6e13bec..080da0c020 100644 --- a/packages/daemon/src/server/http/http-server-capture-publication.test.ts +++ b/packages/daemon/src/server/http/http-server-capture-publication.test.ts @@ -15,6 +15,7 @@ import { makeAndroidSession } from '../../__tests__/support/session-factories.ts import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; import { screenshotRuntimeFixture } from '../../__tests__/support/screenshot-runtime-fixture.ts'; import { screenshotCommandSurface } from '../../__tests__/support/screenshot-command-surface-fake.ts'; +import { sessionLeaseOf } from '../../core/lease-context.ts'; import { LeaseRegistry } from '../../resources/lease-registry.ts'; import { trackDownloadableArtifact } from '../../resources/artifact-tracking.ts'; import { finalizeDaemonResponse } from '../request-finalization.ts'; @@ -191,7 +192,7 @@ test('record stop keeps the unleased producer when the stop runs under a lease', async (post, base) => { successfulData(await post({ command: 'record', positionals: ['start'] })); harness.sessionStore.update(harness.sessionStore.lookup(harness.sessionName)!, { - lease: { leaseId: lease.leaseId, tenantId: lease.tenantId, runId: lease.runId }, + lease: sessionLeaseOf(lease), }); const output = successfulData( await post({ @@ -225,7 +226,8 @@ test.each(['leased at start', 'leased after start'])( sessionStore.publish('capture', makeAndroidSession('capture')); const leaseRegistry = new LeaseRegistry(); const scope = { tenantId: 'tenant', runId: 'trace', leaseBackend: 'android-instance' } as const; - const { leaseId } = leaseRegistry.allocateLease(scope); + const lease = leaseRegistry.allocateLease(scope); + const { leaseId } = lease; const owner = { ...scope, leaseId }; const handler = createRequestHandler({ host: createTestHostEnvironment(), @@ -280,7 +282,7 @@ test.each(['leased at start', 'leased after start'])( } else { successfulData(await wait('trace-owner-request', unleased)); sessionStore.update(sessionStore.lookup('capture')!, { - lease: { ...owner, leaseBackend: scope.leaseBackend }, + lease: sessionLeaseOf(lease), }); successfulData(await wait('trace-other-request', owner)); } diff --git a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts index afef438e5a..f0271e4691 100644 --- a/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts +++ b/packages/daemon/src/server/http/http-server-proxy-leased-output.test.ts @@ -23,6 +23,7 @@ import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; import { screenshotRuntimeFixture } from '../../__tests__/support/screenshot-runtime-fixture.ts'; import { screenshotCommandSurface } from '../../__tests__/support/screenshot-command-surface-fake.ts'; import { tenantScopedSessionName } from '../../core/session-tenant-scope.ts'; +import { sessionLeaseOf } from '../../core/lease-context.ts'; import { LeaseRegistry } from '../../resources/lease-registry.ts'; import { cleanupDownloadableArtifact, @@ -70,7 +71,7 @@ async function startProxiedDaemon() { sessionName, makeAndroidSession(sessionName, { sessionScope: { kind: 'tenant', id: TENANT }, - lease: { leaseId: lease.leaseId, ...OWNER_SCOPE, leaseBackend: lease.backend }, + lease: sessionLeaseOf(lease), }), ); const handleRequest = createRequestHandler({ diff --git a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts index 7f9d8b20c9..783e0b1671 100644 --- a/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts +++ b/packages/daemon/src/sessions/handlers/__tests__/record-runtime-stop-recovery.test.ts @@ -260,7 +260,7 @@ test('record stop recovery carries the request lease and discovery context', asy ); fs.writeFileSync(outPath, 'mp4'); const leaseFlags = { - leaseId: 'lease-1', + leaseId: 'a'.repeat(32), leaseProvider: 'proxy', deviceKey: 'android:mobile:emulator-5554', clientId: 'client-1', diff --git a/src/cli/connection/ended-lease.test.ts b/src/cli/connection/ended-lease.test.ts index d4a8877714..292e001918 100644 --- a/src/cli/connection/ended-lease.test.ts +++ b/src/cli/connection/ended-lease.test.ts @@ -79,7 +79,7 @@ test('an ended lease is forgotten from a reply or its error details', () => { test('the stored lease stays unless the reply ends that lease at response level', () => { const nestedStep = { step: 1, command: 'close', ok: true, data: { endedLeases: [...ENDED] } }; const replies: DaemonResponse[] = [ - { ok: true, data: { endedLeases: [{ leaseId: 'other-lease', outcome: 'released' }] } }, + { ok: true, data: { endedLeases: [{ leaseId: 'f'.repeat(32), outcome: 'released' }] } }, { ok: true, data: { endedLeases: [{ leaseId: LEASE_ID, outcome: 'retained' }] } }, { ok: true, data: { endedLeases: { leaseId: LEASE_ID, outcome: 'released' } } }, { ok: true, data: { lease: { leaseId: LEASE_ID, tenantId: 'proxy', runId: 'run' } } }, From 14b374324cf9695f6e9825f6c51527ea07006e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 18:16:03 +0100 Subject: [PATCH 14/15] fix(daemon): authorize perf report sources and downloads --- packages/command-surface/src/perf/index.ts | 4 +- .../daemon/src/resources/artifact-tracking.ts | 15 ++ .../http/http-server-perf-report.test.ts | 229 ++++++++++++++++++ .../src/server/remote-host-input-policy.ts | 23 ++ .../internal/session-perf-runtime.ts | 75 +++++- website/docs/docs/debugging-profiling.md | 1 + 6 files changed, 334 insertions(+), 13 deletions(-) create mode 100644 packages/daemon/src/server/http/http-server-perf-report.test.ts diff --git a/packages/command-surface/src/perf/index.ts b/packages/command-surface/src/perf/index.ts index ed40176248..e73c4f1321 100644 --- a/packages/command-surface/src/perf/index.ts +++ b/packages/command-surface/src/perf/index.ts @@ -43,7 +43,9 @@ export const perfCommandMetadata = defineFieldCommandMetadata( kind: enumField(PERF_KIND_VALUES), template: stringField('xctrace template name, for example Time Profiler.'), out: stringField('Output artifact path.'), - tracePath: stringField('Existing .trace path to report, defaults to the latest session trace.'), + tracePath: stringField( + 'Existing local .trace path to report. For a remote report, omit it to use the session capture.', + ), }, ); diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 701eac1c0d..7cb0172d93 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -320,6 +320,21 @@ function readOutputIdentity(artifactPath: string): string | undefined { } } +/** Checks source identity because a local report can reach the same file through another path. */ +export function assertPerformanceArtifactLease(source: fs.Stats, caller: string | null): void { + const identity = outputIdentity(source); + for (const entry of pendingArtifacts().values()) { + if ( + entry.artifactType === 'performance' && + entry.leaseVerifier && + entry.leaseVerifier !== caller && + entry.output === identity + ) { + throw leasedOutputRefusal('Performance capture'); + } + } +} + export async function openDownloadableArtifact( artifactId: string, reader: DownloadableArtifactReader, diff --git a/packages/daemon/src/server/http/http-server-perf-report.test.ts b/packages/daemon/src/server/http/http-server-perf-report.test.ts new file mode 100644 index 0000000000..17f5e5e951 --- /dev/null +++ b/packages/daemon/src/server/http/http-server-perf-report.test.ts @@ -0,0 +1,229 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { expect, onTestFinished, test, vi } from 'vitest'; +import { DAEMON_HTTP_TENANT_HEADER } from '@agent-device/contracts/daemon-http'; +import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; +import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; +import { makeIosAppSession } from '../../__tests__/support/session-factories.ts'; +import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; +import { screenshotRuntimeFixture } from '../../__tests__/support/screenshot-runtime-fixture.ts'; +import { screenshotCommandSurface } from '../../__tests__/support/screenshot-command-surface-fake.ts'; +import { createRequestHandler } from '../../__tests__/support/test-device-runtime-gateway.ts'; +import { + cleanupDownloadableArtifact, + leaseVerifier, + trackDownloadableArtifact, +} from '../../resources/artifact-tracking.ts'; +import { LeaseRegistry } from '../../resources/lease-registry.ts'; +import { successfulData, withHttp } from './http-output.fixtures.ts'; + +const tenantId = 'report-tenant'; +const positionals = ['cpu', 'profile', 'report', 'xctrace']; + +function reportFixture() { + const root = mkdtempForTestSync('daemon-perf-report-'); + const sessionStore = makeSessionStore('daemon-perf-report-session-'); + const leaseRegistry = new LeaseRegistry(); + const gateway = screenshotRuntimeFixture().gateway; + const inspectFacts = vi.fn(async (...args) => { + const facts = await gateway.inspectFacts(...args); + return { + ...facts, + operations: { ...facts.operations, perfProfileReport: { available: true } }, + }; + }); + const perfProfileReport = vi.fn(async ({ outPath }: { outPath: string }) => { + fs.writeFileSync(outPath, 'generated-report'); + return { outPath }; + }); + const handler = createRequestHandler({ + host: createTestHostEnvironment(), + createCommandSurface: screenshotCommandSurface, + logPath: path.join(root, 'daemon.log'), + token: 'test-token', + sessionStore, + leaseRegistry, + deviceInventoryGateways: createTestDeviceInventoryGateways(), + deviceRuntimeGateway: { + ...gateway, + inspectFacts, + bind: async (request) => { + const binding = await gateway.bind(request); + return { + ...binding, + facts: await inspectFacts(request.device), + operations: { ...binding.operations, perfProfileReport }, + }; + }, + }, + trackDownloadableArtifact, + }); + const profilePath = path.join(root, 'retained.trace'); + fs.mkdirSync(profilePath); + const retainProfile = (name: string, leaseId?: string) => { + sessionStore.publish( + name, + makeIosAppSession(name, { + ...(name.startsWith(tenantId + ':') + ? { sessionScope: { kind: 'tenant', id: tenantId } } + : {}), + lastPerfProfile: { + profile: { + kind: 'xctrace', + mode: 'cpu-profile', + outPath: profilePath, + template: 'Time Profiler', + }, + leaseVerifier: leaseId ? leaseVerifier(leaseId) : null, + }, + }), + ); + }; + return { + handler, + sessionStore, + leaseRegistry, + profilePath, + retainProfile, + perfProfileReport, + inspectFacts, + }; +} + +test.each(['public without a lease', 'tenant-scoped under another lease'])( + 'perf report refuses an explicit host trace path from a %s request before runtime admission', + async (origin) => { + const fixture = reportFixture(); + const isPublic = origin === 'public without a lease'; + const scope = { tenantId, runId: 'perf', leaseBackend: 'android-instance' } as const; + const producer = fixture.leaseRegistry.allocateLease({ ...scope, runId: 'producer' }); + fixture.retainProfile(isPublic ? 'capture' : `${tenantId}:capture`, producer.leaseId); + const explicitPath = fixture.profilePath + '-external'; + fs.mkdirSync(explicitPath); + const meta = isPublic + ? {} + : { + ...scope, + leaseId: fixture.leaseRegistry.allocateLease(scope).leaseId, + sessionIsolation: 'tenant', + }; + await withHttp(fixture.handler, async (post) => { + const body = await post( + { + command: 'perf', + session: 'capture', + positionals: [...positionals, '', '', explicitPath], + meta, + }, + isPublic, + ); + expect(body.error?.data).toMatchObject({ + code: 'INVALID_ARGS', + details: { reason: 'REMOTE_HOST_INPUT_DENIED', field: 'tracePath' }, + }); + expect(fixture.inspectFacts).not.toHaveBeenCalled(); + expect(fixture.perfProfileReport).not.toHaveBeenCalled(); + }); + }, +); + +test.each(['HTTP request', 'direct daemon request'])( + 'an unleased %s perf report is downloadable by the requester tenant', + async (origin) => { + const fixture = reportFixture(); + fixture.retainProfile('capture'); + await withHttp(fixture.handler, async (post, base) => { + const request = { command: 'perf', session: 'capture', positionals }; + const report = successfulData( + origin === 'HTTP request' + ? await post({ ...request, meta: { tenantId } }) + : { result: await reportWithoutHttpTenantStamp() }, + ); + async function reportWithoutHttpTenantStamp() { + const response = await fixture.handler({ + ...request, + token: 'test-token', + flags: { tenant: tenantId }, + internal: { publicNetworkOnly: true }, + }); + if (!response.ok) throw new Error(JSON.stringify(response.error)); + return response; + } + const id = report.artifacts?.[0]?.artifactId; + expect(id).toBeTypeOf('string'); + const fetchReport = (tenant?: string) => + fetch(`${base}/artifacts/${id}`, { + headers: { + authorization: 'Bearer test-token', + ...(tenant ? { [DAEMON_HTTP_TENANT_HEADER]: tenant } : {}), + }, + }); + expect((await fetchReport('other-tenant')).status).toBe(401); + expect((await fetchReport()).status).toBe(401); + const download = await fetchReport(tenantId); + expect(download.status).toBe(200); + expect(await download.text()).toBe('generated-report'); + }); + }, +); + +test.each(['retained in another session', 'registered as an artifact', 'also retained locally'])( + 'trusted local perf report refuses a lease-owned source %s', + async (owner) => { + const fixture = reportFixture(); + const lease = fixture.leaseRegistry.allocateLease({ + tenantId, + runId: 'owner', + leaseBackend: 'android-instance', + }); + if (owner === 'also retained locally') fixture.retainProfile('capture'); + else fixture.sessionStore.publish('capture', makeIosAppSession('capture')); + if (owner === 'registered as an artifact') { + const id = trackDownloadableArtifact({ + artifactPath: fixture.profilePath, + artifactType: 'performance', + leaseVerifier: leaseVerifier(lease.leaseId), + }); + onTestFinished(() => cleanupDownloadableArtifact(id)); + } else { + fixture.retainProfile('leased', lease.leaseId); + } + const alias = fixture.profilePath + '-alias'; + fs.symlinkSync(fixture.profilePath, alias, 'dir'); + await withHttp(fixture.handler, async (post) => { + const body = await post( + { + command: 'perf', + session: 'capture', + positionals: + owner === 'also retained locally' ? positionals : [...positionals, '', '', alias], + }, + false, + ); + expect(body.error?.data?.details).toMatchObject({ reason: 'LEASE_OUTPUT_MISMATCH' }); + expect(fixture.perfProfileReport).not.toHaveBeenCalled(); + }); + }, +); + +test('trusted local perf report returns INVALID_ARGS for an unreadable explicit source', async () => { + const fixture = reportFixture(); + fixture.retainProfile('capture'); + const file = path.join(fixture.profilePath, 'file'); + fs.writeFileSync(file, ''); + await withHttp(fixture.handler, async (post) => { + const body = await post( + { + command: 'perf', + session: 'capture', + positionals: [...positionals, '', '', path.join(file, 'unreadable.trace')], + }, + false, + ); + expect(body.error?.data).toMatchObject({ + code: 'INVALID_ARGS', + details: { field: 'tracePath' }, + }); + expect(fixture.perfProfileReport).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/daemon/src/server/remote-host-input-policy.ts b/packages/daemon/src/server/remote-host-input-policy.ts index 3b866d5108..d16bfeb2bb 100644 --- a/packages/daemon/src/server/remote-host-input-policy.ts +++ b/packages/daemon/src/server/remote-host-input-policy.ts @@ -2,6 +2,7 @@ import { AppError } from '@agent-device/kernel/errors'; import { INTERNAL_COMMANDS } from '@agent-device/command-registry/catalog'; import { validateAndNormalizeBatchSteps } from '@agent-device/command-registry/batch'; import { DEFAULT_BATCH_MAX_STEPS } from '@agent-device/contracts/command'; +import { resolveSessionIsolationMode } from '../core/config.ts'; import type { DaemonRequest } from '../core/daemon-request.ts'; import { remoteScriptOpen } from '../core/session-script-publication-capability.ts'; @@ -21,6 +22,7 @@ const HOST_PATH_FIELDS = [ /** Remote callers choose client destinations; only the daemon chooses host output paths. */ export function restrictRemoteHostInputs(request: DaemonRequest): DaemonRequest { + assertPerfReportHostInput(request); if (!request.internal?.publicNetworkOnly) return request; const { cwd: _cwd, ...meta } = request.meta ?? {}; const command = request.command; @@ -49,6 +51,27 @@ export function restrictRemoteHostInputs(request: DaemonRequest): DaemonRequest }); } +function assertPerfReportHostInput(request: DaemonRequest): void { + if (!hasExplicitPerfReportSource(request)) return; + const isolation = resolveSessionIsolationMode( + request.meta?.sessionIsolation ?? request.flags?.sessionIsolation, + ); + if (request.internal?.publicNetworkOnly || isolation === 'tenant') { + throw remoteHostInputDenied('tracePath'); + } +} + +function hasExplicitPerfReportSource(request: DaemonRequest): boolean { + const args = request.positionals; + return ( + request.command === 'perf' && + args?.[0]?.toLowerCase() === 'cpu' && + args[1]?.toLowerCase() === 'profile' && + args[2]?.toLowerCase() === 'report' && + Boolean(args[6]) + ); +} + function assertRemoteHostToolingInputs( request: DaemonRequest, fields: Array, diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index 50146947c7..cb70e2c0e7 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -2,7 +2,7 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value'; import type { DaemonArtifact, DaemonResponse } from '@agent-device/kernel/contracts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import path from 'node:path'; -import { statSync } from 'node:fs'; +import { statSync, type Stats } from 'node:fs'; import { randomUUID } from 'node:crypto'; import { cp, rename, rm } from 'node:fs/promises'; import { type PerfCaptureAdmissionLedger } from '@agent-device/capture-kit/perf-capture-admission-ledger'; @@ -38,11 +38,13 @@ import type { import type { SessionStore } from '../../../core/session-store.ts'; import type { DaemonRequest } from '../../../core/daemon-request.ts'; import { requestCwd } from '../../../core/context.ts'; +import { LEASE_FIELD_NORMALIZERS, resolveLeaseScope } from '../../../core/lease-context.ts'; import type { SessionRef, SessionState } from '../../../core/session-state.ts'; import { bindSessionPerfCapture } from '../../../core/session-capture-binding.ts'; import { withCaptureOutputOwnership } from '../../../core/capture-output-ownership.ts'; import { admittedOutputLeaseVerifier, + assertPerformanceArtifactLease, leasedOutputRefusal, } from '../../../resources/artifact-tracking.ts'; import { recordSessionAction } from '../../../core/session-action-recorder.ts'; @@ -175,17 +177,18 @@ async function executeAdmittedPerfPlan( return await startPerfCapture(params, session, runtime, plan.request); } case 'profile-report': { - const cwd = requestCwd(params.req, params.host); - const explicitPath = plan.request.tracePath && expandSessionPath(plan.request.tracePath, cwd); + const explicit = plan.request.tracePath + ? resolveExplicitPerfSource(params, plan.request.tracePath) + : undefined; const saved = session.lastPerfProfile; const retained = plan.request.kind === 'xctrace' && - explicitPath && + explicit && saved && - !samePerfSource(explicitPath, saved.profile.outPath) + !samePerfSource(explicit.path, explicit.stat, saved.profile.outPath) ? undefined : saved; - const tracePath = explicitPath || retained?.profile.outPath; + const tracePath = explicit?.path ?? retained?.profile.outPath; assertPerfOutputOwner(params.req, retained?.leaseVerifier); const last = retained?.profile; if (!tracePath) { @@ -200,6 +203,8 @@ async function executeAdmittedPerfPlan( 'perf cpu profile report requires a stopped profile trace; stop the active capture first.', ); } + const source = explicit?.stat ?? readPerfSourceStat(tracePath); + if (source) assertPerfSourceLease(params, tracePath, source); const runtime = await bindDevice(device, plan.use); const outPath = resolveNativeOutPath(params, plan.request.outPath, 'cpu-report.json'); const data = await runtime.operations.perfProfileReport({ @@ -215,7 +220,8 @@ async function executeAdmittedPerfPlan( data: withCaptureOutputOwnership( { perf: 'reported', ...data }, params.req.internal?.publicNetworkOnly ? 'daemon' : 'caller', - params.req.internal?.admittedLease?.tenantId, + params.req.internal?.admittedLease?.tenantId ?? + LEASE_FIELD_NORMALIZERS.tenantId(resolveLeaseScope(params.req).tenantId), ), }; } @@ -356,11 +362,56 @@ async function stopPerfCapture( }; } -function samePerfSource(explicitPath: string, retainedPath: string): boolean { - if (explicitPath === retainedPath) return true; - const explicit = statSync(explicitPath, { throwIfNoEntry: false }); - const retained = statSync(retainedPath, { throwIfNoEntry: false }); - return !!explicit && !!retained && explicit.dev === retained.dev && explicit.ino === retained.ino; +function resolveExplicitPerfSource(params: PerfRuntimeHandlerParams, requestedPath: string) { + const tracePath = expandSessionPath(requestedPath, requestCwd(params.req, params.host)); + const source = readPerfSourceStat(tracePath); + if (!source) { + throw new AppError('INVALID_ARGS', 'tracePath must name a readable stopped profile trace.', { + field: 'tracePath', + }); + } + return { path: tracePath, stat: source }; +} + +function samePerfSource(tracePath: string, source: Stats, otherPath: string): boolean { + if (tracePath === otherPath) return true; + const other = readPerfSourceStat(otherPath); + return other?.dev === source.dev && other.ino === source.ino; +} + +function assertPerfSourceLease( + params: PerfRuntimeHandlerParams, + tracePath: string, + source: Stats, +): void { + const owner = admittedOutputLeaseVerifier(params.req); + for (const session of params.sessionStore.values()) { + const retained = session.lastPerfProfile; + if ( + retained?.leaseVerifier && + retained.leaseVerifier !== owner && + samePerfSource(tracePath, source, retained.profile.outPath) + ) { + throw leasedOutputRefusal('Performance capture'); + } + const capture = session.perfCapture; + const captureOwner = readString(capture?.envelope.metadata?.remoteCaptureLease); + if (capture && captureOwner && captureOwner !== owner) { + const capturePath = readString(capture.handle.inspect().outPath); + if (capturePath && samePerfSource(tracePath, source, capturePath)) { + throw leasedOutputRefusal('Performance capture'); + } + } + } + assertPerformanceArtifactLease(source, owner); +} + +function readPerfSourceStat(tracePath: string) { + try { + return statSync(tracePath); + } catch { + return undefined; + } } function assertPerfOutputOwner(req: DaemonRequest, owner: string | null | undefined): void { diff --git a/website/docs/docs/debugging-profiling.md b/website/docs/docs/debugging-profiling.md index 3b6595ca3e..0820082ee5 100644 --- a/website/docs/docs/debugging-profiling.md +++ b/website/docs/docs/debugging-profiling.md @@ -274,6 +274,7 @@ agent-device perf trace stop --kind perfetto --out app.perfetto-trace - `perf memory snapshot` escalates to file artifacts. Android supports Java HPROF capture for active app processes when the build/device allows heap dumping. iOS simulator and macOS app sessions support memgraph capture. On physical iOS devices, memgraph capture reports unavailable with a hint. - Heap and memgraph artifacts are returned as paths plus compact metadata. Example default output: `Memory artifact (android-hprof): /tmp/app.hprof (42MB)`. They are not printed or embedded in JSON by default. Native allocation tracing (heapprofd) is not supported. - `perf cpu profile ... --kind xctrace` collects an Apple native `.trace`; `report` aggregates every run, returns at most ten weighted top functions in JSON, and prints five. `perf trace ... --kind xctrace` keeps trace data as an artifact. +- For a remote CPU profile report, use the session’s stopped capture. Supply an existing host trace path only when you run the report locally. - On iOS simulators and macOS, process sampling and captures target the resolved app executable. Other running copies with the same executable name are excluded, including copies installed on another simulator. - Android native profiling uses `perf cpu profile ... --kind simpleperf`; its report likewise returns at most ten top functions and prints five. Android native trace capture uses `perf trace ... --kind perfetto`. These commands require an active Android app session and return artifact paths/summaries instead of dumping profile or trace contents. - Use the compact result as evidence. For example, a successful Perfetto stop may return `state: "stopped"`, `outPath: "/tmp/app.perfetto-trace"`, `sizeBytes: 5392410`, and `method: "adb-shell-perfetto"` while the 5.3 MB raw trace remains on disk as the artifact. From ce3c247f0b2b4edbe9ed1ffc7fe087819949889c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 22 Nov 2026 18:29:23 +0100 Subject: [PATCH 15/15] fix(daemon): preserve retained capture ownership and trace lifetime --- .../__tests__/runner-process-launch.test.ts | 30 ++++ packages/apple-runner/src/runner-io.ts | 2 +- .../src/resources/artifact-tracking.test.ts | 45 ++++++ .../daemon/src/resources/artifact-tracking.ts | 59 +++++++- .../http/http-server-perf-report.test.ts | 131 +++++++++++++++++- .../http/http-server-remote-output.test.ts | 3 +- .../leased-recording-retention.test.ts | 43 ++++-- .../sessions/leased-recording-retention.ts | 3 +- .../internal/session-perf-runtime.ts | 60 ++++---- packages/host-kit/src/internal/diagnostics.ts | 15 +- 10 files changed, 336 insertions(+), 55 deletions(-) diff --git a/packages/apple-runner/src/__tests__/runner-process-launch.test.ts b/packages/apple-runner/src/__tests__/runner-process-launch.test.ts index 174836578c..854633b12f 100644 --- a/packages/apple-runner/src/__tests__/runner-process-launch.test.ts +++ b/packages/apple-runner/src/__tests__/runner-process-launch.test.ts @@ -160,6 +160,36 @@ test('runner trace output is scoped to the trace and the request that started th assert.equal(fs.readFileSync(traceLogPath, 'utf8'), 'runner output\n'); }); +test('runner output stays bound to an active caller-owned trace after the request ends', async () => { + const { withDiagnosticsScope } = await loadRealHostKitCapabilities(); + const logPath = runnerLogPath(); + const traceLogPath = path.join(path.dirname(logPath), 'caller.trace'); + let finish = () => {}; + const wait = new Promise<{ stdout: string; stderr: string; exitCode: number }>((resolve) => { + finish = () => resolve({ stdout: '', stderr: '', exitCode: 0 }); + }); + onTestFinished(finish); + mockRunCmdBackground.mockReturnValue({ ...makeBackgroundRunner(4242), wait }); + let traceActive = true; + const launched = await withDiagnosticsScope( + { traceLogPath, runnerTraceLogWritable: () => traceActive }, + () => + launchRunnerProcess({ + device: IOS_SIMULATOR, + xctestrunPath: '/tmp/runner.xctestrun', + derivedPath: '/tmp/runner-derived', + logPath, + }), + ); + fs.appendFileSync(logPath, 'after the request\n'); + await waitFor(() => assert.equal(fs.readFileSync(traceLogPath, 'utf8'), 'after the request\n')); + traceActive = false; + fs.appendFileSync(logPath, 'after trace stop\n'); + finish(); + await launched.output; + assert.equal(fs.readFileSync(traceLogPath, 'utf8'), 'after the request\n'); +}); + test('output an older runner generation left in the same log file does not wake startup', async () => { const logPath = runnerLogPath(); fs.writeFileSync(logPath, 'AGENT_DEVICE_RUNNER_LISTENER_READY\n'); diff --git a/packages/apple-runner/src/runner-io.ts b/packages/apple-runner/src/runner-io.ts index 622079644a..873c27a02b 100644 --- a/packages/apple-runner/src/runner-io.ts +++ b/packages/apple-runner/src/runner-io.ts @@ -24,7 +24,7 @@ export async function getFreePort(): Promise { export function bindRunnerTraceOutput( path: string | undefined, ): ((chunk: string) => void) | undefined { - const write = captureDiagnosticTraceWriter(); + const write = captureDiagnosticTraceWriter('runner'); if (!write) return path ? (chunk) => appendLogChunk(path, chunk) : undefined; return (chunk) => { try { diff --git a/packages/daemon/src/resources/artifact-tracking.test.ts b/packages/daemon/src/resources/artifact-tracking.test.ts index 6ecd7aa593..e9827af2ae 100644 --- a/packages/daemon/src/resources/artifact-tracking.test.ts +++ b/packages/daemon/src/resources/artifact-tracking.test.ts @@ -8,16 +8,61 @@ import { mkdtempForTestSync } from '@agent-device/test-kit'; import { AppError, isRequestCanceledError, normalizeError } from '@agent-device/kernel/errors'; import { createRuntimeStateHandle } from '@agent-device/host-kit/runtime-slots'; import * as command from '@agent-device/host-kit/command'; +import { + captureDiagnosticTraceWriter, + withDiagnosticsScope, +} from '@agent-device/host-kit/diagnostics'; +import { makeIosSession } from '../__tests__/support/session-factories.ts'; +import { makeSessionStore } from '../__tests__/support/store-factory.ts'; import { cleanupDownloadableArtifact, leaseVerifier, listDownloadableArtifacts, openDownloadableArtifact, + requestTraceDiagnostics, trackDownloadableArtifact, trackUploadedArtifact, type OpenedDownloadableArtifact, } from './artifact-tracking.ts'; +test.each(['trace stopped', 'session gained a lease'])( + 'an unleased caller-owned trace accepts runner output after the request until %s', + async (ended) => { + const store = makeSessionStore('agent-device-runner-trace-scope-'); + const tracePath = path.join(store.ensureSessionDir('default'), 'active-runner.trace'); + const ref = store.publish( + 'default', + makeIosSession('default', { + trace: { outPath: tracePath, startedAt: Date.now() }, + }), + ); + const req = { token: 'test-token', session: 'default', command: 'snapshot', positionals: [] }; + const write = await withDiagnosticsScope(requestTraceDiagnostics(req, ref, store), () => { + const writer = captureDiagnosticTraceWriter('runner'); + writer?.('from the producing request\n'); + return writer; + }); + write?.('while the trace is still active\n'); + if (ended === 'trace stopped') { + store.update(ref, { trace: undefined }); + } else { + store.update(ref, { + lease: { + leaseId: 'a'.repeat(32), + tenantId: 'tenant-a', + runId: 'run-a', + backend: 'ios-simulator', + }, + }); + } + write?.('after the trace or owner changed\n'); + + expect(fs.readFileSync(tracePath, 'utf8')).toBe( + 'from the producing request\nwhile the trace is still active\n', + ); + }, +); + test.each(['cleanup', 'expiry'] as const)( '%s aborts an inventory-only archive and removes its source after tar settles', async (trigger) => { diff --git a/packages/daemon/src/resources/artifact-tracking.ts b/packages/daemon/src/resources/artifact-tracking.ts index 7cb0172d93..cb72f3b9be 100644 --- a/packages/daemon/src/resources/artifact-tracking.ts +++ b/packages/daemon/src/resources/artifact-tracking.ts @@ -54,12 +54,23 @@ export function requestTraceDiagnostics( store: Pick, ) { const trace = ref?.session.trace; - if (!ref || !trace || (trace.leaseVerifier ?? null) !== admittedOutputLeaseVerifier(req)) { - return { traceLogPath: undefined, traceLogWritable: undefined }; + const owner = trace?.leaseVerifier ?? null; + if (!ref || !trace || owner !== admittedOutputLeaseVerifier(req)) { + return { + traceLogPath: undefined, + traceLogWritable: undefined, + runnerTraceLogWritable: undefined, + }; } + const unleasedCallerTrace = trace.outPath !== undefined && owner === null && !ref.session.lease; + const traceLogWritable = () => { + const current = store.resolveCurrent(ref); + return current?.trace === trace && (!unleasedCallerTrace || current.lease === undefined); + }; return { traceLogPath: trace.remoteCaptureOutputPath ?? trace.outPath, - traceLogWritable: () => store.resolveCurrent(ref)?.trace === trace, + traceLogWritable, + runnerTraceLogWritable: unleasedCallerTrace ? traceLogWritable : undefined, }; } @@ -185,13 +196,21 @@ type ArtifactEntry = { type ArtifactTrackingState = { artifacts: Map; uploads: Map; + retainedPerformanceSources: Map< + string, + { output: string; leaseVerifier: string | null; birthtimeMs: number } + >; }; const ARTIFACT_TRACKING_SLOT = Symbol('artifactTracking'); const artifactTracking = (): ArtifactTrackingState => runtimeSlot( ARTIFACT_TRACKING_SLOT, - (): ArtifactTrackingState => ({ artifacts: new Map(), uploads: new Map() }), + (): ArtifactTrackingState => ({ + artifacts: new Map(), + uploads: new Map(), + retainedPerformanceSources: new Map(), + }), (state) => { for (const [id, entry] of state.artifacts) { requestArtifactCleanup(state.artifacts, id, entry); @@ -320,10 +339,36 @@ function readOutputIdentity(artifactPath: string): string | undefined { } } -/** Checks source identity because a local report can reach the same file through another path. */ -export function assertPerformanceArtifactLease(source: fs.Stats, caller: string | null): void { +/** Keeps the report source owner independently of the downloadable copy and latest handoff. */ +export function retainXctraceReportSource(source: string, leaseVerifier: string | null): void { + const canonical = fs.realpathSync(source); + const stat = fs.statSync(canonical); + artifactTracking().retainedPerformanceSources.set(canonical, { + output: outputIdentity(stat), + birthtimeMs: stat.birthtimeMs, + leaseVerifier, + }); +} + +/** Checks both resolved paths and source identities, including renamed or aliased sources. */ +export function assertTrackedPerformanceSourceLease( + canonical: string, + source: fs.Stats, + caller: string | null, +): void { const identity = outputIdentity(source); - for (const entry of pendingArtifacts().values()) { + const state = artifactTracking(); + for (const [retainedPath, entry] of state.retainedPerformanceSources) { + if ( + entry.leaseVerifier && + entry.leaseVerifier !== caller && + (canonical === retainedPath || + (identity === entry.output && source.birthtimeMs === entry.birthtimeMs)) + ) { + throw leasedOutputRefusal('Performance capture'); + } + } + for (const entry of state.artifacts.values()) { if ( entry.artifactType === 'performance' && entry.leaseVerifier && diff --git a/packages/daemon/src/server/http/http-server-perf-report.test.ts b/packages/daemon/src/server/http/http-server-perf-report.test.ts index 17f5e5e951..cbbba8cc81 100644 --- a/packages/daemon/src/server/http/http-server-perf-report.test.ts +++ b/packages/daemon/src/server/http/http-server-perf-report.test.ts @@ -1,6 +1,14 @@ import fs from 'node:fs'; import path from 'node:path'; import { expect, onTestFinished, test, vi } from 'vitest'; +import { createDurableResourceEnvelope } from '@agent-device/capture-kit'; +import { PendingTransferGuard } from '@agent-device/contracts/async-lifecycle'; +import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime'; +import type { + PerfRuntimeOperations, + PerfNativeCaptureLiveHandle, +} from '@agent-device/contracts/perf-runtime'; +import { deviceIdentity, type DeviceInfo } from '@agent-device/kernel/device'; import { DAEMON_HTTP_TENANT_HEADER } from '@agent-device/contracts/daemon-http'; import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit'; @@ -14,6 +22,7 @@ import { leaseVerifier, trackDownloadableArtifact, } from '../../resources/artifact-tracking.ts'; +import { sessionLeaseOf } from '../../core/lease-context.ts'; import { LeaseRegistry } from '../../resources/lease-registry.ts'; import { successfulData, withHttp } from './http-output.fixtures.ts'; @@ -29,7 +38,11 @@ function reportFixture() { const facts = await gateway.inspectFacts(...args); return { ...facts, - operations: { ...facts.operations, perfProfileReport: { available: true } }, + operations: { + ...facts.operations, + perfProfileReport: { available: true }, + perfNativeCaptureStart: { available: true }, + }, }; }); const perfProfileReport = vi.fn(async ({ outPath }: { outPath: string }) => { @@ -52,7 +65,11 @@ function reportFixture() { return { ...binding, facts: await inspectFacts(request.device), - operations: { ...binding.operations, perfProfileReport }, + operations: { + ...binding.operations, + perfProfileReport, + perfNativeCaptureStart: fixtureCapture(request.device), + }, }; }, }, @@ -90,6 +107,116 @@ function reportFixture() { }; } +function fixtureCapture(device: DeviceInfo): PerfRuntimeOperations['perfNativeCaptureStart'] { + return async (input) => { + let outPath = input.outPath; + const snapshot = () => ({ + kind: 'xctrace' as const, + mode: 'cpu-profile' as const, + appBundleId: input.appId, + template: 'Time Profiler', + outPath, + }); + const handle: PerfNativeCaptureLiveHandle = { + inspect: snapshot, + setOutputPath: (value) => { + outPath = value; + }, + finish: async () => { + fs.mkdirSync(outPath, { recursive: true }); + fs.writeFileSync(path.join(outPath, 'sample'), 'fixture'); + return { status: 'completed', result: snapshot() }; + }, + forceCleanup: async () => ({ status: 'cleaned' }), + [Symbol.asyncDispose]: async () => {}, + }; + return { + pendingHandle: new PendingTransferGuard(handle), + envelope: createDurableResourceEnvelope({ + resourceKind: 'perf-capture', + sessionId: input.sessionId, + device: deviceIdentity(device), + owner: localRuntimeOwner(device.platform), + fence: input.fence, + lifecycle: 'open', + descriptor: { version: 1, body: { kind: 'fixture' } }, + }), + response: snapshot(), + }; + }; +} + +function captureRequest(action: 'start' | 'stop', meta = {}) { + return { + command: 'perf', + session: 'capture', + positionals: ['cpu', 'profile', action, 'xctrace'], + meta, + }; +} + +test('explicit perf reports check the source and identity of an older daemon-owned xctrace capture', async () => { + const fixture = reportFixture(); + fixture.sessionStore.publish('capture', makeIosAppSession('capture')); + fixture.sessionStore.publish('reporter', makeIosAppSession('reporter')); + const scope = { tenantId, runId: 'producer', leaseBackend: 'android-instance' } as const; + const meta = { ...scope, leaseId: fixture.leaseRegistry.allocateLease(scope).leaseId }; + await withHttp(fixture.handler, async (post) => { + const complete = async () => { + successfulData(await post(captureRequest('start', meta))); + const result = successfulData(await post(captureRequest('stop', meta))); + return result.outPath as string; + }; + const older = await complete(); + expect(await complete()).not.toBe(older); + const moved = older + '-moved'; + fs.renameSync(older, moved); + fs.mkdirSync(older); + const report = (source: string, owner = {}) => + post( + { + command: 'perf', + session: 'reporter', + positionals: [...positionals, '', '', source], + meta: owner, + }, + false, + ); + const refusals = [await report(older), await report(moved)]; + expect(refusals.map((body) => body.error?.data?.details?.reason)).toEqual([ + 'LEASE_OUTPUT_MISMATCH', + 'LEASE_OUTPUT_MISMATCH', + ]); + expect(fixture.perfProfileReport).not.toHaveBeenCalled(); + successfulData(await report(moved, meta)); + }); +}); + +test('perf stop retains the session lease tenant for an unleased producer', async () => { + const fixture = reportFixture(); + const session = fixture.sessionStore.publish('capture', makeIosAppSession('capture')); + const lease = fixture.leaseRegistry.allocateLease({ + tenantId, + runId: 'session-owner', + leaseBackend: 'android-instance', + }); + await withHttp(fixture.handler, async (post, base) => { + successfulData(await post(captureRequest('start'))); + fixture.sessionStore.update(session, { lease: sessionLeaseOf(lease) }); + const result = successfulData(await post(captureRequest('stop', { leaseId: lease.leaseId }))); + const id = result.artifacts?.[0]?.artifactId; + expect(id).toBeTypeOf('string'); + const fetchCapture = (tenant: string) => + fetch(`${base}/artifacts/${id}`, { + headers: { authorization: 'Bearer test-token', [DAEMON_HTTP_TENANT_HEADER]: tenant }, + }); + expect((await fetchCapture('other-tenant')).status).toBe(401); + const download = await fetchCapture(tenantId); + expect(download.status).toBe(200); + await download.arrayBuffer(); + }); +}); + test.each(['public without a lease', 'tenant-scoped under another lease'])( 'perf report refuses an explicit host trace path from a %s request before runtime admission', async (origin) => { diff --git a/packages/daemon/src/server/http/http-server-remote-output.test.ts b/packages/daemon/src/server/http/http-server-remote-output.test.ts index c0c2f1b8bd..c9769f3d32 100644 --- a/packages/daemon/src/server/http/http-server-remote-output.test.ts +++ b/packages/daemon/src/server/http/http-server-remote-output.test.ts @@ -546,7 +546,8 @@ test.each([ }; }, perfProfileReport: async ({ tracePath, outPath, profile, template }) => { - const explicit = tracePath === explicitTrace; + const explicit = + fs.existsSync(explicitTrace) && tracePath === fs.realpathSync(explicitTrace); if (explicit) { expect(profile).toBeUndefined(); expect(template).toBeUndefined(); diff --git a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts index a760e10c13..7eaa525a1c 100644 --- a/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts +++ b/packages/daemon/src/sessions/__tests__/leased-recording-retention.test.ts @@ -94,7 +94,12 @@ function localRecordingExport(): RecordingExport { function publishRecordingSession( sessionStore: SessionStore, recordingExport: RecordingExport, - options: { leased?: boolean; finishFails?: boolean; unleasedProducer?: boolean } = {}, + options: { + leased?: boolean; + finishFails?: boolean; + unleasedProducer?: boolean; + producerTenant?: string; + } = {}, ) { const session: SessionState = makeIosSession(ADDRESS, { appBundleId: 'com.example.app', @@ -150,7 +155,9 @@ function publishRecordingSession( metadata: { phase: 'active', ...(recordingExport.daemonOwned ? { remoteCaptureOutput: true } : {}), - ...(options.unleasedProducer ? {} : { remoteCaptureTenantId: TENANT }), + ...(options.unleasedProducer && !options.producerTenant + ? {} + : { remoteCaptureTenantId: options.producerTenant ?? TENANT }), remoteCaptureLease: options.leased === false || options.unleasedProducer ? 'unleased' : leaseVerifier(LEASE_ID), }, @@ -405,17 +412,25 @@ test.each([undefined, '/client/downloads/checkout.mp4'])( }, ); -test('teardown keeps the recording unleased when the session later acquired a lease', async () => { - const sessionStore = makeSessionStore('agent-device-unleased-recording-'); - publishRecordingSession(sessionStore, remoteRecordingExport(), { unleasedProducer: true }); +test.each([undefined, 'former-tenant'])( + 'teardown assigns an unleased recording with producer tenant %s to the session lease tenant', + async (producerTenant) => { + const sessionStore = makeSessionStore('agent-device-unleased-recording-'); + publishRecordingSession(sessionStore, remoteRecordingExport(), { + unleasedProducer: true, + producerTenant, + }); - await teardown(sessionStore); + await teardown(sessionStore); - expect(await listDownloadableArtifacts({ tenantId: 'tenant-b' }, process.env)).toEqual([]); - const artifacts = await listDownloadableArtifacts({ tenantId: TENANT }, process.env); - expect(artifacts).toEqual( - expect.arrayContaining([ - expect.objectContaining({ artifactType: 'screen-recording', filename: 'checkout.mp4' }), - ]), - ); -}); + expect( + await listDownloadableArtifacts({ tenantId: producerTenant ?? 'tenant-b' }, process.env), + ).toEqual([]); + const artifacts = await listDownloadableArtifacts({ tenantId: TENANT }, process.env); + expect(artifacts).toEqual( + expect.arrayContaining([ + expect.objectContaining({ artifactType: 'screen-recording', filename: 'checkout.mp4' }), + ]), + ); + }, +); diff --git a/packages/daemon/src/sessions/leased-recording-retention.ts b/packages/daemon/src/sessions/leased-recording-retention.ts index 2352121722..2e2c91a87c 100644 --- a/packages/daemon/src/sessions/leased-recording-retention.ts +++ b/packages/daemon/src/sessions/leased-recording-retention.ts @@ -31,7 +31,8 @@ export function retainLeasedSessionRecording( const owner = recordingOutputLeaseVerifier(metadata); if (owner === undefined) return; const storedTenant = metadata?.remoteCaptureTenantId; - const tenantId = typeof storedTenant === 'string' ? storedTenant : lease.tenantId; + const tenantId = + owner !== null && typeof storedTenant === 'string' ? storedTenant : lease.tenantId; const sessionName = tenantPublicSessionName(lease.tenantId, ref.address); const artifacts = recordingStopArtifacts(completion).filter( (artifact) => typeof artifact.path === 'string', diff --git a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts index cb70e2c0e7..a80b0c5be8 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-perf-runtime.ts @@ -2,7 +2,7 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value'; import type { DaemonArtifact, DaemonResponse } from '@agent-device/kernel/contracts'; import { expandSessionPath } from '@agent-device/host-kit/session-paths'; import path from 'node:path'; -import { statSync, type Stats } from 'node:fs'; +import { realpathSync, statSync, type Stats } from 'node:fs'; import { randomUUID } from 'node:crypto'; import { cp, rename, rm } from 'node:fs/promises'; import { type PerfCaptureAdmissionLedger } from '@agent-device/capture-kit/perf-capture-admission-ledger'; @@ -44,8 +44,9 @@ import { bindSessionPerfCapture } from '../../../core/session-capture-binding.ts import { withCaptureOutputOwnership } from '../../../core/capture-output-ownership.ts'; import { admittedOutputLeaseVerifier, - assertPerformanceArtifactLease, + assertTrackedPerformanceSourceLease, leasedOutputRefusal, + retainXctraceReportSource, } from '../../../resources/artifact-tracking.ts'; import { recordSessionAction } from '../../../core/session-action-recorder.ts'; import { @@ -185,7 +186,7 @@ async function executeAdmittedPerfPlan( plan.request.kind === 'xctrace' && explicit && saved && - !samePerfSource(explicit.path, explicit.stat, saved.profile.outPath) + !samePerfSource(explicit, saved.profile.outPath) ? undefined : saved; const tracePath = explicit?.path ?? retained?.profile.outPath; @@ -203,14 +204,14 @@ async function executeAdmittedPerfPlan( 'perf cpu profile report requires a stopped profile trace; stop the active capture first.', ); } - const source = explicit?.stat ?? readPerfSourceStat(tracePath); - if (source) assertPerfSourceLease(params, tracePath, source); + const source = explicit ?? readPerfSource(tracePath); + if (source) assertPerfSourceLease(params, source); const runtime = await bindDevice(device, plan.use); const outPath = resolveNativeOutPath(params, plan.request.outPath, 'cpu-report.json'); const data = await runtime.operations.perfProfileReport({ appId: session.appBundleId, kind: plan.request.kind, - tracePath, + tracePath: source?.path ?? tracePath, outPath, template: plan.request.template ?? (last?.kind === 'xctrace' ? last.template : undefined), profile: last, @@ -321,7 +322,11 @@ async function stopPerfCapture( const metadata = capture.envelope.metadata; const owner = readString(metadata?.remoteCaptureLease) ?? null; assertPerfOutputOwner(params.req, owner); - const tenantId = readString(metadata?.remoteCaptureTenantId); + const tenantId = resolveStoppedCaptureTenant( + params.req, + session, + metadata?.remoteCaptureTenantId, + ); const snapshot = capture.handle.inspect(); const mismatchMessage = perfCaptureStopMismatch(snapshot, request); if (mismatchMessage) return errorResponse('INVALID_ARGS', mismatchMessage); @@ -334,15 +339,17 @@ async function stopPerfCapture( intent: 'capture', binding: bindSessionPerfCapture(params.sessionStore, params.ref), }); + const outPath = readString(completion.outPath); + const daemonOwned = outPath !== undefined && metadata?.remoteCaptureOutputPath === outPath; if (request.area === 'cpu') { const profile = readProfileHandoff(completion); + if (daemonOwned && profile.kind === 'xctrace') + retainXctraceReportSource(profile.outPath, owner); params.sessionStore.update(params.ref, { lastPerfProfile: { profile, leaseVerifier: owner }, }); } const clientOutPath = params.req.meta?.clientArtifactPaths?.outPath ?? capture.clientOutPath; - const outPath = readString(completion.outPath); - const daemonOwned = outPath !== undefined && metadata?.remoteCaptureOutputPath === outPath; const artifacts = daemonOwned && completion.kind === 'xctrace' ? [await publishXctraceDownload(outPath, clientOutPath, request.area === 'cpu')] @@ -362,35 +369,37 @@ async function stopPerfCapture( }; } +function resolveStoppedCaptureTenant(req: DaemonRequest, session: SessionState, stored: unknown) { + return readString(stored) ?? session.lease?.tenantId ?? req.meta?.tenantId; +} + function resolveExplicitPerfSource(params: PerfRuntimeHandlerParams, requestedPath: string) { const tracePath = expandSessionPath(requestedPath, requestCwd(params.req, params.host)); - const source = readPerfSourceStat(tracePath); + const source = readPerfSource(tracePath); if (!source) { throw new AppError('INVALID_ARGS', 'tracePath must name a readable stopped profile trace.', { field: 'tracePath', }); } - return { path: tracePath, stat: source }; + return source; } -function samePerfSource(tracePath: string, source: Stats, otherPath: string): boolean { - if (tracePath === otherPath) return true; - const other = readPerfSourceStat(otherPath); - return other?.dev === source.dev && other.ino === source.ino; +type PerfReportSource = { path: string; stat: Stats }; + +function samePerfSource(source: PerfReportSource, otherPath: string): boolean { + if (source.path === otherPath) return true; + const other = readPerfSource(otherPath); + return other?.stat.dev === source.stat.dev && other.stat.ino === source.stat.ino; } -function assertPerfSourceLease( - params: PerfRuntimeHandlerParams, - tracePath: string, - source: Stats, -): void { +function assertPerfSourceLease(params: PerfRuntimeHandlerParams, source: PerfReportSource): void { const owner = admittedOutputLeaseVerifier(params.req); for (const session of params.sessionStore.values()) { const retained = session.lastPerfProfile; if ( retained?.leaseVerifier && retained.leaseVerifier !== owner && - samePerfSource(tracePath, source, retained.profile.outPath) + samePerfSource(source, retained.profile.outPath) ) { throw leasedOutputRefusal('Performance capture'); } @@ -398,17 +407,18 @@ function assertPerfSourceLease( const captureOwner = readString(capture?.envelope.metadata?.remoteCaptureLease); if (capture && captureOwner && captureOwner !== owner) { const capturePath = readString(capture.handle.inspect().outPath); - if (capturePath && samePerfSource(tracePath, source, capturePath)) { + if (capturePath && samePerfSource(source, capturePath)) { throw leasedOutputRefusal('Performance capture'); } } } - assertPerformanceArtifactLease(source, owner); + assertTrackedPerformanceSourceLease(source.path, source.stat, owner); } -function readPerfSourceStat(tracePath: string) { +function readPerfSource(tracePath: string): PerfReportSource | undefined { try { - return statSync(tracePath); + const resolved = realpathSync(tracePath); + return { path: resolved, stat: statSync(resolved) }; } catch { return undefined; } diff --git a/packages/host-kit/src/internal/diagnostics.ts b/packages/host-kit/src/internal/diagnostics.ts index 4c71dde79a..2b05065213 100644 --- a/packages/host-kit/src/internal/diagnostics.ts +++ b/packages/host-kit/src/internal/diagnostics.ts @@ -54,6 +54,7 @@ type DiagnosticsScopeOptions = { operatorLog?: boolean; traceLogPath?: string; traceLogWritable?: () => boolean; + runnerTraceLogWritable?: () => boolean; }; type DiagnosticsScope = DiagnosticsScopeOptions & { @@ -107,6 +108,7 @@ export async function withDiagnosticsScope( scope.traceLogClosed = true; scope.traceLogPath = undefined; scope.traceLogWritable = undefined; + scope.runnerTraceLogWritable = undefined; } } @@ -117,15 +119,20 @@ export function updateDiagnosticsScope(options: DiagnosticsScopeOptions): void { } /** - * Binds a raw trace writer to the current request and trace. An inactive binding still suppresses - * fallback to an unverified session path. + * Binds a raw trace writer to the current request and trace. A runner can continue through an + * active caller-owned trace when it has a separate lifetime guard. An inactive binding still + * suppresses fallback to an unverified session path. */ -export function captureDiagnosticTraceWriter(): ((line: string) => void) | undefined { +export function captureDiagnosticTraceWriter( + source: 'request' | 'runner' = 'request', +): ((line: string) => void) | undefined { const scope = diagnosticsStorage.getStore(); if (!scope || !('traceLogPath' in scope)) return undefined; const { traceLogPath } = scope; + const runnerTraceLogWritable = source === 'runner' ? scope.runnerTraceLogWritable : undefined; return (line) => { - if (traceLogPath && activeTracePath(scope) === traceLogPath) { + const writable = runnerTraceLogWritable?.() ?? activeTracePath(scope) === traceLogPath; + if (traceLogPath && writable) { appendDiagnosticLine( scope, traceLogPath,