Skip to content

fix(daemon): give leased artifacts and diagnostics records only to their lease id - #4945

Open
thymikee wants to merge 15 commits into
0.22from
fix/0.22-leased-output-ownership
Open

thymikee wants to merge 15 commits into
0.22from
fix/0.22-leased-output-ownership

Conversation

@thymikee

@thymikee thymikee commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Bind downloadable captures and diagnostics to their producing lease through retention (#4912). Screenshots, xctrace results, remote recordings, and remote traces use daemon-owned paths. Downloads validate the opened file descriptor. Stale cleanup preserves replacements.

Recording stop and teardown preserve the producing lease and recover a missing tenant from the session. Direct HTTP keeps the supplied recording filename. Delivered scripts use the admitted lease. Remote perf reports use the retained capture. Local xctrace reports check source ownership by path and file identity. Reports belong to the requester. Perf stop falls back to the session lease tenant. Unleased recording teardown uses the session lease tenant. Apple runner output follows the trace lifetime for active unleased caller-owned traces.

Diagnostics use daemon-generated record IDs. A current record takes precedence over a verified legacy file with the same name. Verified unleased recordings in session storage can replay. Legacy exports on reusable remote temp paths are refused.

Breaking for callers that omit the producing lease ID and for clients older than 0.21.28.

Before / after

  • GET /artifacts/<id> without the producing lease returns 401 instead of 200.
  • Reusing a screenshot destination leaves the earlier download bound to its own frame.
  • record stop refuses ambiguous remote exports.

Validation

At ce3c247f0b, 216 focused tests, 1,000 wire and eager-closure tests, wire compatibility, typecheck, lint, and Fallow passed. All 43 runnable affected checks passed, including packed-core verification.

Closes #4912

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.08 MB 6.09 MB +9.7 kB
Package (unpacked) 6.08 MB 6.09 MB +9.7 kB
Package (download) 1.86 MB 1.86 MB +3.8 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.2 ms 23.1 ms -0.1 ms
CLI --help 45.7 ms 46.6 ms +0.9 ms

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on dbcd132. The verdict and the full list are in the summary comment.

Comment on lines 166 to 175
function findTrackedArtifactId(
artifactPath: string,
tenantId: string | undefined,
owner: Pick<DownloadableArtifactRegistration, 'artifactPath' | 'tenantId' | 'leaseId'>,
): 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;
}

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 8d56500.

🟡 Minor · A file registered without a lease gets a second artifact id when later registered with one

Register path P for tenant T with no leaseId, then register P again with leaseId L: findTrackedArtifactId returns undefined and trackDownloadableArtifact mints a second id, so each id deletes the file after its own download. The second call's owner.leaseId short-circuits the tenant match (entry.tenantId === owner.tenantId && owner.leaseId === undefined), so the pre-existing unleased entry is never matched. The reverse order is also lease-blind: if (entry.leaseId !== undefined) return artifactId; returns an entry owned by lease A to a registration under lease B, so B names an artifact id it then cannot download (401 LEASE_OUTPUT_MISMATCH).

⚠️ Check this patch before you commit it: the review doubted it would be right as written.

Suggested change
function findTrackedArtifactId(
artifactPath: string,
tenantId: string | undefined,
owner: Pick<DownloadableArtifactRegistration, 'artifactPath' | 'tenantId' | 'leaseId'>,
): 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;
}
function findTrackedArtifactId(
owner: Pick<DownloadableArtifactRegistration, 'artifactPath' | 'tenantId' | 'leaseId'>,
): string | undefined {
for (const [artifactId, entry] of pendingArtifacts()) {
if (entry.cleanupRequested || entry.artifactPath !== owner.artifactPath) continue;
if (entry.tenantId !== owner.tenantId) continue;
if (entry.leaseId !== undefined || owner.leaseId !== undefined) {
if (entry.leaseId === owner.leaseId) return artifactId;
continue;
}
return artifactId;
}
return undefined;
}
Details

Also raised here

Also raised at this location by persona maintainability: findTrackedArtifactId's owner-first lookup has no reader for a leased tenant mismatch — Now that the loop takes the whole owner object, the leased branch (if (entry.leaseId !== undefined) return artifactId;) short-circuits before any tenant comparison, so a leased entry and its tenant rule coexist in one loop with no hint of which wins. A one-line comment naming that precedence — the lease outranks the tenant for an already-leased file — keeps the next change to this loop from "fixing" the missing check.

persona: maintainability, javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Comment on lines 152 to 157
const leaseId =
activeLease?.leaseId ?? assertRequestHoldsSessionLease(params.req, existingSession)?.leaseId;
if (leaseId) {
emitDiagnostic({ level: 'info', phase: REQUEST_LEASE_DIAGNOSTIC_PHASE, data: { leaseId } });
}
if (!activeLease) return params.req;

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 8d56500, with the code it pointed at unchanged: withdrawn, not fixed.

🟡 Minor · Exempt commands stamp the session lease into diagnostics but register artifacts unleased

On a leased session, a lease-admission-exempt command that presents the session lease id gets activeLease === undefined from assertRequestLeaseAdmission, so admitRequestLeaseForLockedScope never sets internal.admittedLease, yet the stamp still emits request_lease with the session lease id. Artifact registration reads only internal.admittedLease?.leaseId, so that same request's files are registered with no lease—callers carrying the tenant label list and download them while the diagnostics record refuses them.

Suggested change

Carry the lease id the request may act under (activeLease ?? session lease) on the request as one value, and use that single value both for the request_lease diagnostics stamp and for the artifact leaseId.

Details

persona: senior-developer, javascript

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

@thymikee
thymikee force-pushed the fix/0.22-leased-output-ownership branch from dbcd132 to 8d56500 Compare November 22, 2026 08:42
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 8d56500. The verdict and the full list are in the summary comment.

Comment on lines +396 to +400
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;
}

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 400ff5d: the code it pointed at changed.

🆕 🟡 Minor · Nested batch/replay step records get no owners file, so anyone can read them

A nested replay/test/batch step on a leased session writes its diagnostics record with no owners file, so GET /sessions/<tenant:session>/requests/<stepRequestId>/diagnostics answers 200 to any caller holding the shared proxy token while the outer request's own record correctly answers 401. Cause: logOwnersPublished is a single boolean on the request's ALS scope; the nested step reuses that scope, so the flag is already true and appendDiagnosticLine skips the .owners write for the step's new logPath. readRecordOwners then maps the missing file to [], making the access check vacuously true.

⚠️ Check this patch before you commit it: the review doubted it would be right as written.

Suggested change
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 (logPath === scope.logPath && scope.logOwners?.length && !scope.ownersPublishedFor?.has(logPath)) {
const owners = scope.logOwners.map((owner) => `${owner}\n`).join('');
fs.appendFileSync(diagnosticsRecordOwnersPath(logPath), owners, 'utf8');
(scope.ownersPublishedFor ??= new Set()).add(logPath);
}

Suggested change

Track owners publication per log path (e.g. a Set on the scope) instead of one boolean per scope, so a nested step that re-points the scope still publishes owners before its first byte. Add a regression test where a batch/replay step on a leased session reads the step record without the lease id and expects 401.

Details

persona: javascript

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 400ff5d. The verdict and the full list are in the summary comment.

Comment on lines +159 to +165
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;
}

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on ac4f9b2: the code it pointed at changed.

🆕 🟡 Minor · A diagnostics record with two lease owners becomes unreadable by every reader

A record whose owners file holds two distinct verifiers is refused to all readers because owners.every(owner => requestHoldsOutputLease(owner, presented)) can never be satisfied by the single lease a request presents. Two owners land on one file because appendDiagnosticLine guards on !scope.logOwnerPublished (per scope, not per path), and nested batch/replay steps inheriting the same meta.requestId resolve the same record path while claimRequestDiagnosticsRecord assigns their scopes different owners.

Suggested change

Key the owners-file publish by record path (a Set of paths already written) rather than by scope, and confirm no two scopes that can carry different owners resolve the same request record path; if they can, split the record per owner so a reader needs to hold only one lease.

Details

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apex-sentinel-staging note Fail-closed by design, no change. Nested steps inherit the parent lease through handDownRequestLeaseScope, so a normal batch, replay or test writes one owner value per line and the parent reads its record. A second distinct owner appears only when a step names another lease id. Admission refuses that step, and the record then serves nobody rather than serving another lease's lines. Splitting records per owner would add a path scheme for a request that is already refused.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Withdrawing this finding

Your explanation checks out against the code: handDownRequestLeaseScope only fills lease fields the child leaves unset (lease-context.ts:126-140), so a nested batch/replay/test step that inherits meta.requestId also inherits the parent's leaseId and gets the same leaseVerifier owner — and a step that names a different lease id trips assertRequestPresentsLease/assertSuppliedLeaseLabels before it can append anything, so the second owners line never lands. The remaining window (one scope appending under owner A, another under owner B for the same path) is exactly the scenario you intend to fail closed on, and refusing every reader is the safer outcome than serving another lease's lines. The line-162 every(...) over a list is fine for the single-entry case the normal path produces; no change requested.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

@thymikee

Copy link
Copy Markdown
Member Author

Commit 400ff5d has five problems with leased artifacts and diagnostics. Each one lets a record or capture reach the wrong lease, or locks out the right one. They need fixes before merge.

A second independent pass found that reusing a capture path lets older artifact IDs serve a new lease's bytes. A direct static-token daemon that accepts host output paths can register a screenshot of an unleased session, then write a leased screenshot to the same path while the first ID is pending (artifact-tracking.ts:232, screenshot adapter, request-finalization.ts:161). Registration keeps the earlier entry, so both entries point at that path. GET /artifacts and the artifacts RPC still list the tenant-only ID, and GET /artifacts/<id> opens the new leased bytes through it. If the earlier entry was leased, the same-path check returns its ID instead, so the old lease reads the new capture and the producing lease cannot. Should each downloadable capture be published to a distinct daemon-owned file before it is tracked, with an ID reused only for the same published output and owner?

A second independent pass found that diagnostics written before an upgrade stay readable by the whole tenant (request-diagnostics-http.ts:194). With the same state directory, a leased request record written by the base keeps its original path and has no owners file. The head resolves that path without a live session (daemon-runtime.ts:656). It reads the missing sidecar as an empty owner list and streams the record to a caller with the shared token and tenant but no lease ID. A base debug close that kept its lease also wrote the raw lease ID into that record. The rule: a record that is explicitly unleased and a legacy record of unknown ownership must be told apart. Legacy records should be served only after trustworthy lease provenance is recovered and credentials are redacted, and ambiguous ones should stay off the remote route.

A second independent pass found that sequential reuse of a request ID makes existing diagnostics unreadable (request-diagnostics-http.ts:162). HTTP keeps caller-supplied tracking IDs and frees them on completion (http-server.ts:532), and the record name comes from that ID and the session (request-execution-scope.ts:271). Two completed requests with different lease IDs can therefore share one record, and the second can be a request the router refused, since the router writes diagnostics before lease admission. Both digests are appended (diagnostics.ts:395), and every digest must match the same credential. The base returned the record to its tenant. At head even the first request's legitimate owner gets 401, and an earlier unleased record at that address loses tenant-only readability. The rule: each independent request gets a server-unique diagnostics locator that carries its own ownership, while the client tracking ID stays for request tracking only.

A second independent pass found that legacy recording manifests can republish a leased export without its producing lease (record-runtime.ts:504). The base manifest recorded the remote-output flag and tenant but no lease verifier. When a stop-recovery request reaches the head handler for such a manifest, the missing verifier bypasses ownership and the completed export is returned (record-runtime.ts:539). A same-tenant request for an unheld named session with no tenant isolation can reach this path without a lease (request-admission.ts:116). Finalization then registers the export with no verifier (request-finalization.ts:107). The rule: the remote stop-recovery path republishes a completed recording only when its provenance is known. Explicitly unleased manifests keep recovery, and legacy exports of unknown ownership stay out of remote delivery.

A second independent pass found that the producing caller cannot replay its retained recording through record stop after teardown (record-runtime.ts:505). ADR 0024 documents that replay. Sending the original lease ID with its scope is refused in normal lease admission before the recording handler (request-admission.ts:132, lease-registry.ts:675). A request under no lease or a replacement lease reaches recovery and is refused at the manifest. The base could return the completed export on the same named session without a live lease. The rule: a replay that only returns a completed export verifies the presented ID against the manifest owner and needs no active lease. Live recovery stays under normal admission, and the manifest owner carries into artifact registration.

Not blocking, and each can be taken or left: the lease-ownership code (leaseVerifier, requestHoldsOutputLease, leasedOutputRefusal, claimRequestDiagnosticsRecord) sits in artifact-tracking.ts although diagnostics, recording and the router import it, while lease ID canonicalization already lives beside lease-context.ts; the SHA-256 digest comparison at artifact-tracking.ts:52 could use the existing timingSafeStringEqual; the doc and PR text say the daemon log names the lease ID, but request-scoped lines written before the request record are fingerprinted, so the sentence should cover only daemon-scope events or those writes should be marked operator-only; the test "record stop after a daemon restart serves the export only under the lease that started it" retires a session in memory and never reaches the ok:true branch after a real restart, so it could be renamed to the refusal it proves; the sentence "each caller reads only its own records" in remote-device-lease-contracts.md:93 holds only for callers that sent a lease ID; and http-server-proxy-leased-output.test.ts:200 does not send the artifacts RPC with an ended lease ID, so one assertion after endLeasedSession would cover the documented CLI route.

Could the ownership code be simpler and better owned with the same behavior? One lease-output module next to lease-context.ts could export the verifier (via canonicalLeaseId), a constant-time holds(), the refusal and the diagnostics claim. The HTTP authorizer could return {tenantId, leaseId} so both REST routes share one reader. The router could set one request-lease value read for artifacts, manifests and diagnostics owner. TenantOwnedEntry could take an optional leaseVerifier, so requireTenantOwnedEntry enforces both rules for artifacts. Production size (+266/-56) is under the 700-line threshold, and no deletion was found that would shrink it.

The three open inline threads from earlier review do not apply, so they can be resolved. Shared path cleanup: two IDs for one path share one refcounted cleanup, and leased-entry precedence is the documented rule. Lease-exempt commands: none of the lease-exempt commands returns downloadable artifacts. In-scope steps: they keep the outer request ID and append to the record the outer request already owns, and other steps get a fresh scope and claim. No lower-priority threads still apply.

CI is still running: Smoke Tests (android, ios, macos) and one other check are in progress, and the smoke jobs run every request through the new diagnostics claim, so the diff overlaps their route. Body rules has two cancelled runs superseded by a rerun in progress, with no code overlap. No conflicts are known. Not verified here: the test suite, the seeded mutations and check:affected were not run, and regression validity was judged by reading the base code path. Not verified here: the 0.21.28 client cut-off against release history, whether local macos-app leased sessions use the artifacts command or GET /artifacts without sending the lease ID (their listing would then be empty), and the daemon side of an artifacts RPC with an ended lease ID, which is traced in code only.

Before merge, the five problems above need fixes, and the checks that are still running need to finish green.

@thymikee
thymikee force-pushed the fix/0.22-leased-output-ownership branch from 400ff5d to ac4f9b2 Compare November 22, 2026 09:48
@thymikee

Copy link
Copy Markdown
Member Author

Reply to #4945 (comment). Head is now ac4f9b2, rebased on 0.22.

The five problems

  1. Capture path reuse. An artifact record is bound to the output it registered (device, inode, size, mtime). An id is reused only for the same output and the same owner. A later capture at that path gets its own id, and the earlier id expires on list and on download. I did not copy each capture to a daemon-owned file: the tracker runs synchronously in response finalization, so recordings and directory artifacts would be copied on the request path, and record stop replay needs the original path. One gap stays: the output is checked at stat, not on the open descriptor, so a rewrite between the check and the open is not caught.
  2. Request id reuse. The daemon names each diagnostics record with a random id that carries its owner (<32 hex>-<SHA-256 of the lease id> or -unleased). The caller's request id names no record. A record has one owner or none, so the owners file and "match every line" are gone.
  3. Legacy records. A record name that the daemon did not choose has no known owner. The route answers 404 for it.
  4. Legacy manifests. The manifest names its lease, or that it has none. record stop does not return a remote export whose manifest names neither.
  5. Replay after teardown. A completed export replays to a request that sends the manifest's lease id. record stop with no session passes admission when the lease it names has ended. A recording that still runs needs an admitted lease. The manifest owner goes into artifact registration.

Taken from the optional list: timingSafeStringEqual for the digest compare; the daemon-log sentence now covers only daemon-scope events; the contract doc sentence is limited to callers that sent a lease id; the recovery test is renamed and now reaches the ok: true branch; the proxy test sends the artifacts RPC with the ended lease id.

Not taken: a separate lease-output module. A new module in the daemon's eager import closure fails the closure budget, so the code stays in artifact-tracking.ts.

Side effect: files under requests/ are no longer named by the request id. A failed command prints the path, and each line carries the request id. Help and docs are updated.

Tests: seven seeded mutations each fail a test. pnpm check:affected --base origin/0.22 --run passes.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on ac4f9b2. The verdict and the full list are in the summary comment.

Comment on lines +75 to +86
export function claimRequestDiagnosticsRecord(req: Pick<DaemonRequest, 'flags' | 'meta'>): 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);
registerDiagnosticSensitiveValue(leaseId.trim());
}
if (getDiagnosticsMeta().recordId) return;
const owner = leaseId === undefined ? UNLEASED_RECORD : leaseVerifier(leaseId);
updateDiagnosticsScope({ recordId: `${crypto.randomBytes(16).toString('hex')}-${owner}` });
}

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 639973d: marked fixed in 93c51bb by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · A non-hex lease id is registered as no sensitive value, so it stays in the record

A lease id that is not 16–128 hex digits — or whose text leaks into a field key the fingerprinter does not recognise, such as a releaseId-style key or free-form message text — is written to the diagnostics record verbatim because claimRequestDiagnosticsRecord only calls registerDiagnosticSensitiveValue when normalizeLeaseId accepts the id. The owner still comes from the raw id, so the record exists and is served with the sensitive text intact.

Suggested change

Register the raw and trimmed lease id as a sensitive value whenever leaseId !== undefined, independently of whether normalizeLeaseId accepts it, so a non-canonical lease id is still redacted from the record it owns.

Details

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 93c51bb: the record now registers the trimmed lease id as sensitive whenever one is sent, and the canonical form too when it is valid. No test: a malformed id never reaches a record field on the paths a test can drive, so a test passes with or without the fix.

Comment on lines +143 to +153
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);

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 639973d: marked fixed in 93c51bb and ac4f9b2 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · A record an earlier release wrote is answered 401, not 404, when any lease id is sent

A legacy locator (e.g. /sessions/s/requests/abc123/diagnostics) sent with the x-agent-device-lease-id header returns 401 LEASE_OUTPUT_MISMATCH instead of 404. requestHoldsOutputLease returns true for an undefined ownerVerifier, so the owner !== null guard that would yield 404 is never reached when a lease id is present.

Suggested change

Check owner === null before the lease verification so that every record whose owner the daemon cannot name returns 404 regardless of whether a lease id is sent.

Details

Also raised here

Also raised at this location by persona maintainability: Unknown diagnostics owner decides its 404 in two places — A diagnostics record id the daemon did not choose is refused in two steps: diagnosticsRecordOwner returning null suppresses the stat in a null-as-sentinel ternary, and the shared !stats branch then produces the not-found error. The lease guard above already treats null specially, so the next reader has to check both places to learn what an unknown name does. Fold the null case into the guard above and keep the existing not-found error it sends, so one branch decides that outcome.

persona: javascript, maintainability

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 93c51bb. On ac4f9b2 a legacy locator already answered 404: diagnosticsRecordOwner returns null for it, which skipped the lease check. The maintainability point holds, so an unknown owner now returns the not-found error in one early branch. The legacy-record test now sends the request with and without a lease header.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Several findings from the earlier review (#4945 (comment)) are fixed at 639973d: the lease-blind artifact lookup, the owners file, the owners.every() check and the sensitive lease id registration are gone. CI is green, with 16 checks and none failing. There are no conflicts. The code still has the problems below.

Two user-doc sentences now name the diagnostics file by the request id, but the daemon names it by the record id (request-execution-scope.ts#L276, session-artifact-paths.ts#L50-L57). sessions.md#L49 tells the reader to look in requests/<request-id>.ndjson, and debugging-profiling.md#L149 names the same file for the remote download. The next line, #L150, says the "Log: unavailable" line carries the request id, but remote-request-diagnostics.ts#L84 prints the record id. A reader who follows these steps looks for a file that does not exist. Every user-doc and help mention of a request record file or its locator should name the record id. Searching request-id>.ndjson and "request id" near "Log: unavailable" over website/, skills/ and src/commands/schema lists the sites; docs/evidence is exempt.

A second independent pass found that an earlier screenshot ID can serve a later lease's bytes (artifact-tracking.ts#L339). A direct static-token daemon without an auth hook accepts explicit screenshot destinations. An unleased screenshot registered through clientArtifactPaths passes the identity check at #L432-L440. If a leased request publishes a same-size replacement to that path while the GET moves from stat to stream open, the unleased ID returns the leased bytes. Two captures can also overlap before registration (screenshot-runtime.ts#L218-L234). The rule: each downloadable result is bound at capture publication to a distinct daemon-owned file that stays stable through download and cleanup.

A second independent pass found the same problem for xctrace directories (artifact-tracking.ts#L285). A stop under another lease can target an existing published directory (session-perf-runtime.ts#L296-L299). The rename falls back to a recursive copy into it (perf-xctrace.ts#L171-L175), so the inode stays the same and the earlier ID still passes the check. Its GET then packages the new contents. The rule: an ID is bound at publication to a distinct daemon-owned directory or a sealed archive, so later writes to the caller-visible directory cannot change what the ID serves.

A second independent pass found that looking up a stale artifact can delete a newer caller-owned screenshot (artifact-tracking.ts#L396). Register an unleased screenshot at an explicit path, then write a replacement to the same path without clientArtifactPaths. The replacement is not registered (request-finalization.ts#L158-L181). A GET /artifacts, the artifacts RPC, or a GET of the stale ID detects the mismatch and cleans up the only entry, which unlinks the current file (#L615-L617). The base left that file in place. Retiring a stale entry should never unlink a replacement; cleanup should touch only the output the retired entry owned.

A second independent pass found that an old leased diagnostics record can pass as a new unleased one (request-diagnostics-http.ts#L143). The base accepted caller-chosen tracking IDs as filenames, so a legacy leased record can have a name that matches the new unleased format. After an upgrade with the same state directory, the head treats that name as explicitly unleased (artifact-tracking.ts#L100-L103) and streams the record to an authorized reader without its lease ID. The rule: new records resolve only in a daemon-owned namespace separate from legacy files, or carry persisted provenance that the legacy writer could not have supplied.

A second independent pass found the opposite upgrade gap in the same file (request-diagnostics-http.ts#L145). An unleased failure record written before the upgrade was fetchable through its locator on the base. At the head the same request gets 404, because the filename has no owner suffix. Legacy records with trusted evidence of unleased production should stay readable, through migration or a provenance-aware read. Records with ambiguous ownership should stay closed.

A second independent pass found that a routed batch step can append leased diagnostics to an unleased record (artifact-tracking.ts#L84). Send a batch without a lease to a fresh named session, and give its open step an active lease. Step flags are kept (batch.ts#L268-L278), and the step inherits the batch record ID (diagnostics.ts#L88-L102). The claim returns early, so the step writes into the batch's unleased file. The rule: a child reuses its parent's record only when their effective lease owners match; otherwise it claims its own record.

A second independent pass found that completed direct-HTTP recordings from the base can be republished without their lease (record-runtime.ts#L515). On the base, a leased recording with a client destination and no daemon path used the downloadable remote temp path, and its manifest had neither remoteCaptureOutput nor a lease verifier. With the export still present and no live session, a same-tenant request without a lease reaches record stop recovery. The guard allows the unknown owner, the temp-path check in record-runtime-response.ts#L12-L20 marks the export downloadable, and finalization registers it with no verifier. The unknown-owner refusal should cover both forms of daemon-owned remote recording, and provenance should be verified before either completed export is returned or registered.

A second independent pass found that an unleased recording can take the session's later lease at publication (request-finalization.ts#L102). Start an unleased remote recording in a named session, then open the same session and device under an active lease. Open keeps the recording and attaches the lease (session-open-state.ts#L52-L62). On record stop, the explicit unleased marker becomes undefined (record-runtime.ts#L511-L512), so finalization uses the stop request's lease. Close and release do the same through leased-recording-retention.ts#L28-L41. The explicit-unleased owner should survive response finalization, teardown should register with the recording's stored owner, and the request's lease should apply only when the producer made no ownership decision.

A second independent pass found a replay gap for recordings that finished before the upgrade (record-runtime.ts#L515). The base manifest stored remoteCaptureOutput and no lease verifier, and a repeated record stop replayed the export. At the head the same request gets LEASE_OUTPUT_MISMATCH, although no lease produced the file. Replay should work for legacy manifests with trustworthy evidence that the recording was unleased, and unknown remote ownership should stay closed.

Not blocking, and these can be taken or left: when an identity mismatch cleans up a registered output, the entry should be dropped without unlinking the path, or the source removed only if it still matches entry.output (artifact-tracking.ts#L396, which the base expiry timer already does by path); and a nested routed step should reuse the parent's record ID only when the step's sent lease matches the parent's owner (artifact-tracking.ts#L84). Both fit a follow-up issue.

None of the open inline threads from other reviewers still applies. These can be resolved: the lease-blind reverse lookup is gone because the lookup now matches tenant, verifier and output (#4945 (comment)); the record owner now comes from the lease id the request sent (#4945 (comment)); the owners file was deleted (#4945 (comment)); owners.every() was deleted (#4945 (comment)); the sent lease id is now registered as sensitive in 93c51bb (#4945 (comment)); and the unknown-owner case now answers 404 before the lease check (#4945 (comment)).

Not verified here: tests, seeded mutations and check:affected were not run. Regression validity was judged by reading the earlier test commit against the new tests. A batch step carrying a lease id different from its parent's was judged from the fill-unset rule only. The timing window between a capture write and its registration was judged from code, with no live attempt. Parts outside this delta were not re-traced.

Before merge, fix the request-id wording in sessions.md:49 and debugging-profiling.md:149-150 so every record file mention names the record id. Then address the diagnostics, artifact and recording findings above, and re-run CI.

@thymikee

Copy link
Copy Markdown
Member Author

Addressed the six findings in 40f030f3d8.

  • Docs and help use the record ID. Screenshot and xctrace downloads serve their own daemon-owned paths. Stale artifact cleanup checks the registered output before unlinking.
  • New diagnostics use a separate directory. A routed child with a different lease gets its own record. The host can restore a legacy unleased record after verifying its provenance and checking for credentials.
  • Direct-HTTP recording replay refuses ambiguous remote exports. Stop and teardown preserve the recorded owner, including explicit unleased ownership. The lease contract documents how to restore a verified legacy unleased export.

The final head passed 280 focused tests, 950 eager-closure tests, 52 wire tests, the released wire compatibility check, typecheck, lint, and Fallow. The full affected rerun was queued behind the shared validation lock at push.

Review findings

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The smoke-test failures from the earlier review are fixed, but 9a83b71 still lets unleased output leak or be misowned in three places, and a second independent pass found six more. There are no conflicts. The only check not passing is Body rules, which was cancelled rather than failed and looks unrelated to the diff. Before merge, the three findings below need fixes, and the later ones need an answer.

Every remote screenshot against a direct static-token daemon leaves a PNG in the host's /tmp. The client sends a generated remote-temp outPath, and that path is kept when there is no auth hook or network marker. screenshot-runtime.ts:256 writes the capture to a private file, which is the only registered artifact, and then publishes a second full copy to outPath. Download or expiry removes only the private file, so long-running daemons grow without bound. On base the outPath file was the registered artifact and the download deleted it. The rule: a file the daemon writes for a downloadable result is either the registered artifact or is not written. Skip the publish when isRemoteTempArtifactPath(outPath, 'screenshot', '.png') is true, as macos-app-lease.ts:205 does, keep the caller-visible copy only for a real daemon-side destination, and assert that the remote-temp outPath does not exist after the download. The second pass reached the same result. Not verified here: that nothing else sweeps these files from /tmp.

The recording producer marker now owns the whole response. In request-finalization.ts:117, producer === null registers every artifact in the response with no verifier. Take an unleased recording in a session with remote script publication armed and stop it under a valid lease. The stop records an action, and remoteScriptVersionDelivery appends a savedScript revision to the same response. That revision is registered with no verifier, so a same-tenant caller without the lease can list and download it. The rule: a producer marker governs only what that producer made, here the recording export, its chunks and its telemetry. A daemon-delivered artifact takes the verifier of the admitted lease of the request that wrote it. Use the admitted-lease verifier when daemonDeliveredArtifact(artifact) is set, or carry the owner per artifact. Not verified here: the re-open path that leads to this state was not traced end to end.

Unleased recording exports lose the base tenant fallback. At request-finalization.ts:105, an unleased producer with no stored tenant registers the export with an undefined tenant, and artifact-tracking.ts:577 serves such an entry to every reader. On base, the stop request's tenant was used. A recording that a tenant-labelled stop finalizes is now downloadable for every tenant and for callers with none. leased-recording-retention.ts:34 dropped its fallback to lease.tenantId the same way. The rule: the explicit unleased producer decides only the lease verifier, and tenant resolution stays as on base. Use producerTenant ?? req.meta?.tenantId in every case, and fall back to lease.tenantId in retention.

A second independent pass found that request-diagnostics-http.ts:146 cannot serve verified legacy records with reserved-format names. Base accepts caller request IDs that match the new generated format. An operator who moves a verified record to requests-unleased under its original name, as the lease-contracts doc says, cannot restore access when the ID ends in the unleased marker or a 64-hex verifier. The first looks only in request-records and returns 404, and the second can return a lease refusal before any file is checked. Honor the verifier of an existing current record, and otherwise look for the exact name in the host-verified legacy directory regardless of its format.

A second independent pass found that record-runtime.ts:464 can register replacement bytes under the original owner. Two sessions on different devices can share an explicit remote-temp recording destination over direct static-token HTTP. Complete an unleased recording, then a leased one to the same destination. Android copies the new export over the path, and a repeat stop of the unleased session validates only the original manifest and that the path exists. It then registers the replacement under an unleased ID. Give direct-HTTP downloadable recordings a daemon-owned per-capture export path before the video, chunks and telemetry are written, and require verified byte provenance for older exports on reusable paths.

A second independent pass found that artifact-tracking.ts:344 validates a pathname before it opens the file. The identity check at lines 435-446 can pass while the original export is present, then another session's leased recording can replace the output before the stream opens. The handler can send the replacement's bytes under the original artifact ID. For replaceable producer paths, serve a daemon-owned per-capture copy. For file downloads, validate the identity of the opened file and stream from that same handle.

A second independent pass found that trace-runtime.ts:85 can publish leased diagnostics to an unleased reader. On a static-token daemon, an unleased named session can run a public-network command under a valid scoped lease without attaching it to the session. A trace started under that lease collects leased events, and a same-tenant stop without a lease registers the trace with no verifier. Starting the trace unleased also lets a later leased request append before an unleased stop. Keep the trace's producer owner from start through registration, refuse a stop under the wrong lease, and attach diagnostics only when the request and trace owners match.

A second independent pass found that session-perf-runtime.ts:331 assigns a perf capture to whoever stops it. Start a native perf capture under a valid scoped lease in an unleased named session. Start persists the path and tenant with no producer verifier, so a same-tenant request can stop it without the lease, and the artifact takes that stop request's owner in request-finalization.ts:100-117. The route is shared by xctrace, Perfetto and simpleperf, and the inverse case registers an unleased capture under a later stop lease. Persist the producer verifier or explicit unleased owner with the capture, check it before stopping, and pass it to artifact registration.

A second independent pass found that session-perf-runtime.ts:174 lets a report read a leased profile without the lease. Start and stop a CPU profile under the same scoped lease in an unleased named session. The profile stays in session.lastPerfProfile without its owner, and a same-tenant report request with no lease reads it and registers the report with no verifier. Carry the capture's owner into the retained profile, require the producing lease for the report source, and preserve that owner on the resulting artifact.

Would it be simpler to let trackDownloadableArtifact take ownership of the bytes at registration? It could hard-link the produced file or directory into an artifact-store directory under the daemon state dir, or copy it when linking is impossible, and serve and delete only that copy. Producers would keep writing to their normal outPath. That removes the screenshot private path and publish copy, publishXctraceDownload and the readOutputIdentity checks on download and cleanup, so the screenshot leak cannot occur. The parallel string|null|undefined helpers could become one OutputOwner value, {lease: verifier} | 'unleased', where absent means unknown and closed. Stamped per artifact at production and read by finalization, recording retention and the diagnostics route, it would stop a response-wide marker from reassigning a delivered artifact or dropping the tenant fallback. Does that fit what this PR needs?

Not blocking: the plugin-host managed-process test fix in plugin-host.test.ts:69 (try/finally and expect.poll for the hilog line) is not in the commit message, which describes only the smoke probes, so it can be mentioned there or split out, or left as is.

Of the other open threads, none are high-priority and all six no longer apply, so they can be resolved. Same-path registration: the tracker matches tenant, verifier and output, so a leased registration of the same path gets its own id by design. Record owner: it comes from the lease id the request sent, not the session lease (artifact-tracking.ts:76). Owners file: the file is gone, and nested steps reuse the parent record only on an owner match. owners.every(): it is deleted, and a record has one owner encoded in its id. Sensitive lease id: the trimmed canonical id is registered as a sensitive value. Unminted ids: an id the daemon did not mint is read only from requests-unleased, and an absent file returns 404.

Not verified here: no tests, seeded mutations or check:affected were run. Every finding comes from reading the code against base and the earlier head.

@thymikee

Copy link
Copy Markdown
Member Author

Addressed the findings in 34b6b2c1d5.

  • A remote screenshot writes only its registered file. A delivered script keeps the admitted lease alongside an unleased recording. Response finalization and teardown keep the tenant fallback.
  • Verified legacy diagnostics keep their original locator, including reserved-format names. An existing current record takes precedence and enforces its verifier.
  • Downloadable direct-HTTP recordings get a daemon-owned path at start. Replay refuses legacy exports on reusable temp paths. Verified unleased exports in session storage can still replay. File downloads check the opened descriptor and stream from that descriptor.
  • Trace stop and request diagnostics attachment check the trace producer. Downloadable traces use request-scoped diagnostics. Persistent runner streams stay on caller-owned trace paths.
  • Perf stop and report carry the producer through the retained profile and resulting artifact. A request that lacks the producing lease cannot stop or report on a leased capture.

I kept publication in the producers because a registration-time copy runs after the producer can lose control of a reusable path. A hard link also shares the source inode. Finalization selects the admitted owner for delivered artifacts and the stored producer for capture output.

Unit-core, focused tests, eager closure, wire compatibility, typecheck, lint, Fallow, and package verification passed. The affected run was still queued at push after 15 minutes of cumulative lock wait.

Review findings

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on 34b6b2c. The verdict and the full list are in the summary comment.

Comment on lines 76 to +81
): 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 });

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on f307a48: marked fixed in f307a48 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · trace stop compares verifiers exactly, so an unleased trace can never be stopped under a lease

A trace started with no lease stores no verifier, so once the session holds a lease every stop request presents one and the exact comparison refuses it with LEASE_OUTPUT_MISMATCH. The throw happens before session.trace is cleared, so the session keeps a trace that blocks every later trace start. The strict check contradicts the rule artifact-tracking documents and perf runtime applies, namely that output with no lease has no lease rule.

Suggested change
): 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 owner = session.trace.leaseVerifier;
if (owner && owner !== admittedOutputLeaseVerifier(req)) throw leasedOutputRefusal('Trace');
Details

Also raised here

Also raised at this location by persona senior-developer: trace stop on pre-upgrade session state refuses a leased reader with LEASE_OUTPUT_MISMATCH — A session that already holds a trace written by an earlier release has no leaseVerifier field, so stopTrace compares null (the absent-owner side of the new check) against the verifier of the stop request's admitted lease and throws LEASE_OUTPUT_MISMATCH: an upgraded daemon refuses trace stop for exactly the leased caller that started the trace before the upgrade, and the trace file stays open with no way to close it under that lease. The record-based paths in this PR (recording, perf) treat an unknown stored owner as "no lease rule" or fall back; here an absent field is read as an unleased producer. Consider treating leaseVerifier === undefined as unknown ownership that only an unleased or matching-lease request may stop, or documenting the upgrade requirement in remote-device-lease-contracts.md.

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f307a48ef7. The producing lease is required when the trace was leased. A session caller can stop a trace that began unleased, and the download keeps its unleased owner. The loopback regression failed at stop before the fix and now passes.

@@ -197,7 +205,8 @@ async function executeAdmittedPerfPlan(
data: withCaptureOutputOwnership(

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on f307a48: marked fixed in f307a48 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · perf report's download is registered under the retained capture's tenant, not the requester's

When a second tenant runs perf cpu profile report over another tenant's stopped capture, the report registers under the capture's retained?.tenantId, so the caller who produced it cannot download its own artifactId while the other tenant can. Passing retained?.tenantId alone drops the request-tenant fallback that request-finalization relied on. Captures that recorded no tenant are unaffected: the value stays undefined and finalization still falls back to the request tenant.

Suggested change

        data: withCaptureOutputOwnership(
          { perf: 'reported', ...data },
          params.req.internal?.publicNetworkOnly ? 'daemon' : 'caller',
          retained?.tenantId ?? params.req.meta?.tenantId,
          retained?.leaseVerifier,
        ),
Details

Also raised here

Also raised at this location by persona senior-developer: profile-report no longer carries the requesting tenant for an unleased capture — profile-report now passes retained?.tenantId (the tenant stored at stop time) to withCaptureOutputOwnership, where the predecessor used session.lease?.tenantId ?? params.req.meta?.tenantId. For a profile stopped before this change, or one stopped with no tenant in metadata, the report artifact is registered with no tenant at all and becomes readable by any tenant that can reach the download route rather than by the tenant that ran the request. Keeping the request-tenant fallback when retained?.tenantId is undefined preserves the old scoping.

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f307a48ef7. The retained profile keeps its lease check for source access. The new report is registered for the requester’s tenant and admitted lease. The loopback regression now downloads successfully with that lease and refuses the same tenant without it.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At f307a48, the two earlier trace-stop and perf-report findings from the 9a83b71 review are fixed, but four defects remain. All 17 checks pass at this commit. Not verified here: any behavior by running it, because no tests, mutations or check:affected were run, and every result below comes from reading f307a48 against 9a83b71, 34b6b2c and base e8adbb8.

A second independent pass found that an ID-only stop loses the session tenant for an unleased recording (record-runtime.ts:498). On a static-token daemon, start a downloadable recording in an unleased named session with no tenant label, then reopen that session under tenant A's lease. The reopen keeps the recording (session-open-state.ts:52-62). A stop with only the lease id now stores no tenant, and request-finalization.ts:103 falls back to the absent request tenant. The base used session.lease.tenantId. Callers with no tenant, or another tenant, now see the video, chunks and telemetry in inventory and can fetch them by ID. When the manifest has no tenant, the stop should take it from the stored tenant, then the session lease tenant, then the request, and the recording should keep its unleased verifier. This assumes admission does not copy the session lease tenant into req.meta.tenantId for a request that sends only the lease id. Not verified here: every writer of that field.

A second independent pass found that direct HTTP recordings change an explicitly extensionless client filename (record-runtime.ts:386). Against a static-token direct HTTP daemon without a network marker, start an unleased Android recording with the client destination clip. The client sends a .mp4 host placeholder but keeps the client destination without an extension (daemon-artifacts.ts:372-384,426-432). The base kept that destination as supplied. The head appends .mp4, so the start reply echoes clip.mp4, and a stop without a new destination writes clip.mp4 (record-runtime-response.ts:41,140-146). The unique daemon export path and its platform extension should stay. The supplied client destination should stay as supplied on the direct HTTP explicit-placeholder branch, and extension completion should remain on the branches that applied it on base.

A second independent pass found that unleased downloadable traces lose replay timing events and Apple runner output (trace-runtime.ts:58). In an unleased session reached through a public-network HTTP request, start a downloadable trace and run an .ad replay against that session. The base set session.trace.outPath, so replay appended replay_action_start and replay_action_stop with result timing. The head sets only remoteCaptureOutputPath, so the fallback at session-replay-runtime-plan.ts:153 is undefined and session-replay-trace.ts:9 drops these events. Apple runner launch and build chunks that start during the trace are also lost (request-generic-dispatch.ts:102, runner-launched-session.ts:114-117). The owner-checked trace destination should reach replay's event writer, and runner output attributable to a matching request should reach the trace. Runner writes should stay bound to the trace lifetime and the producing request, so the published file stays stable and output from other owners stays out.

A second independent pass found that a retained leased profile blocks an xctrace report on an unrelated explicit source (session-perf-runtime.ts:178). In a named Apple session without a bound lease, start and stop xctrace under a scoped lease so lastPerfProfile belongs to that lease. A local SDK caller then asks for a report with an explicit tracePath that names its own unleased capture. The base uses the explicit source. The head refuses with LEASE_OUTPUT_MISMATCH before it selects the source. The SDK exposes this input (perf/index.ts:46,133-143), and the Apple backend reads it without consuming the handoff (platform-runtime-perf-host.ts:40-48). The report source should be selected first, and ownership should be checked only where the retained handoff or its path is used. A distinct caller-owned xctrace source should work without consuming the retained profile or its template.

Not blocking, and these can be taken or left: a remote trace now has no outPath, so runner and backend streams no longer reach it, and the PR body could say it carries only request diagnostics; record stop and perf stop (record-runtime.ts:500, session-perf-runtime.ts:308) could resolve a missing manifest tenant from the stored tenant, then the session lease tenant, then the request, as leased-recording-retention.ts:34 does; and ADR 0024 line 290 should say a daemon-owned export (remoteCaptureOutput) is registered and legacy remote-temp exports are refused, to match remote-device-lease-contracts.md.

Could one assertRequestMayUseOutput(req, owner: string | null | undefined) live in artifact-tracking.ts next to requestHoldsOutputLease? Trace stop, perf stop and report (replacing assertPerfOutputOwner), and the running-recording branch of assertRequestMayStopRecording could all use it, and the perf manifest owner could be read through the same reader as recordings, so remoteCaptureLease has one encoding. That would remove the duplicate predicate and the second encoding with no behavior change.

No high-priority threads from other reviewers still apply, and no lower-priority threads still apply. These eight threads do not apply and can be resolved. The tracker reuses an id only for the same tenant, verifier and output identity, so a leased registration gets its own id by design (#4945 (comment)). The record owner comes from the lease id the request sent, not from the session lease (#4945 (comment)). The owners file is gone, and the owner is encoded in the record id (#4945 (comment)). owners.every() is deleted, and a record has one owner (#4945 (comment)). The trimmed and the canonical lease id are both registered as sensitive values (#4945 (comment)). An unminted name is read only from the unleased requests, and a missing file returns 404 (#4945 (comment)). Trace stop under a lease is fixed at f307a48 (#4945 (comment)). The perf report now uses the requester's lease and tenant, fixed at f307a48 (#4945 (comment)).

Before merge, the four findings above need fixes, and the checks need to pass again on the new head. Live validation should reach each route named above and show the tenant, filename, trace events and report source it describes.

@thymikee

Copy link
Copy Markdown
Member Author

Addressed the four findings in the review at f307a48 in a141e047f2.

  • Recording stop uses the manifest tenant, then the session lease tenant, then the request tenant. The recording keeps its explicit unleased owner.
  • Direct HTTP keeps the supplied client filename, including an extensionless name, and exports to a unique daemon path with the platform extension.
  • Replay timing and Apple runner output use the owner-checked request trace. Runner output stops when the trace or its producing request ends.
  • A distinct explicit xctrace source reports without consuming the retained profile or its template. The retained source and aliases still require the producing lease.

All six regression cases failed with the relevant fixes disabled. The amended head passed 322 focused tests, wire compatibility, 1,002 wire and eager-import tests, typecheck, lint, and Fallow. The packed-core checks passed before the final test-only amendment, and clean-install snapshot-bridge verification passed. The final affected run is queued behind the shared validation lock.

@apex-sentinel-staging

This comment has been minimized.

…eir 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
…dings 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.
…iagnostics 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.
- 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.
@thymikee
thymikee force-pushed the fix/0.22-leased-output-ownership branch from a141e04 to b2e982f Compare November 22, 2026 16:37
@thymikee

Copy link
Copy Markdown
Member Author

Rebased on 0.22 at bdf5096f77, including #4942 and #4953, and pushed b2e982f02a. The rebase keeps upstream lease admission and session routing, uses the contracts package’s lease ID normalizer, and updates the fixtures for the typed session lease. All 439 focused tests, 1,000 wire and eager-closure tests, the wire compatibility gate, typecheck, lint, and Fallow passed. The full affected run and hosted CI are pending.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on b2e982f. The verdict and the full list are in the summary comment.

Comment on lines 177 to 193
case 'profile-report': {
const runtime = await bindDevice(device, plan.use);
const last = session.lastPerfProfile;
const tracePath = plan.request.tracePath ?? last?.outPath;
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 last = retained?.profile;
if (!tracePath) {
throw new AppError(
'INVALID_ARGS',

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on ce3c247: marked fixed in ce3c247 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · A caller-chosen tracePath skips every lease check in perf report and reads any host path

A request that sends no lease id (or another lease's id) with an explicit tracePath on a session whose retained profile is owned by lease A gets the report: retained becomes undefined because the explicit path differs, so assertPerfOutputOwner(req, undefined) never refuses and the handler reads any directory. When the retained outPath equals the explicit path or they symlink each other (accepted by samePerfSource via dev/ino), retained is kept, making only that capture's lease owner the gate on a caller-chosen path. samePerfSource throws because statSync(path, { throwIfNoEntry: false }) still rejects ENOTDIR when a parent segment is a file and ENOENT for a dangling symlink, escaping as COMMAND_FAILED instead of INVALID_ARGS.

Suggested change

Gate the report on the source: when the explicit tracePath is not the retained capture, check the request against the lease that produced that path (or refuse an explicit path outside the session's own artifacts) before invoking the runtime. Pass a null-ish entry to statSync or wrap samePerfSource so an unreadable path yields the existing INVALID_ARGS.

Details

Also raised here

Also raised at this location by persona maintainability: samePerfSource lets a stat failure abort perf report with a raw fs error — perf report cpu <path> out.json where <path> cannot be stat'ed for a reason other than a missing entry — permission denied on a parent directory, or a symlink cycle — now fails with the raw EACCES/ELOOP filesystem error instead of the command's typed error. The new samePerfSource calls statSync(explicitPath, { throwIfNoEntry: false }), and that option only swallows a missing entry; every other stat failure propagates out of executeAdmittedPerfPlan. The comparison is a heuristic for "is this the retained capture", so a stat failure should fall back to "not the same source" rather than abort the report; wrap both stats (or the whole helper) so only a successful pair compares.

persona: javascript, maintainability

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ce3c247f0b. Public-network and tenant-scoped requests that supply tracePath receive REMOTE_HOST_INPUT_DENIED before runtime admission. Local reports check retained xctrace sources by canonical path and file identity, including older sources whose session handoff has since changed. The loopback regressions cover requests with no lease and with a different lease.

@@ -197,7 +215,7 @@ async function executeAdmittedPerfPlan(
data: withCaptureOutputOwnership(

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on ce3c247: marked fixed in ce3c247 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · Perf report of a retained capture registered with no tenant, so only a tenant-less reader can get it

A capture taken and stopped under no lease, then reported under no lease, is registered with no tenantId because withCaptureOutputOwnership reads params.req.internal?.admittedLease?.tenantId (undefined), so any tenant can download the report. The reverse case fails closed: an unleased capture stopped under tenant-a's lease is registered with tenant-a, blocking a tenant-less report request with 401.

Suggested change

Register the report under the producer's tenant (req.meta?.tenantId) as other capture paths do, and use the lease verifier of the retained profile.

Details

persona: javascript

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ce3c247f0b. The report registers under the requester’s tenant and admitted lease. For a request without a lease, the daemon also reads the tenant from the request’s lease scope. The regression verifies that the requester can download the report and that a different tenant or a tenant-less reader receives 401.

@thymikee

Copy link
Copy Markdown
Member Author

The four earlier findings from the f307a48 review are fixed at b2e982f, but a second independent pass found six remaining defects, so this needs another change before merge.

17 of 18 checks pass. The one other check is still running and covers no test route, so no failure here comes from this diff.

A second independent pass found that a scoped lease produces a daemon-owned xctrace CPU capture, and stop keeps its source for later reports (session-perf-runtime.ts:180). On direct HTTP with the static credential, a report from a different unleased Apple session can name that source through tracePath, and the verifier comes only from the current session's latest handoff. The Apple backend then returns the leased trace's sample summary without the producing lease. The rule: every retained daemon-owned source records the verifier that produced it, and an explicit xctrace report checks the resolved source and its file identity against that verifier, whatever the session's latest handoff is.

A second independent pass found that perf stop drops the base tenant fallback for an unleased capture (session-perf-runtime.ts:318). Start a downloadable native perf capture with no tenant on a named unleased session, reopen that session under tenant A's lease (which keeps perfCapture), then stop with only the lease id. Base bdf5096 used the session lease tenant. At this head the capture registers with no tenant and no lease verifier, so readers outside tenant A can list and download it. The rule: finalization falls back from the stored capture tenant to the session lease tenant before it applies the request tenant, and keeps the explicit unleased verifier.

A second independent pass found that closing a session changes the tenant of a retained unleased recording (leased-recording-retention.ts:34). Tenant A starts an unleased downloadable recording on a named session, tenant B reopens it on the same device under B's lease, and B closes the session before record stop. The base teardown registered the export for B. The head registers it for A, so B's inventory after close loses a recording it received before. The rule: an explicitly unleased producer keeps the session lease tenant that the base teardown route used, and leased recordings use the producing verifier and stored tenant.

A second independent pass found that unleased caller-owned traces lose Apple runner output while the trace is still active (diagnostics.ts:107). The runner writer now captures the launch request's scope, and that scope closes when the interaction returns. Later runner output reaches runner.log but not the open trace, where the base kept appending until the runner ended. The rule: a caller-owned unleased trace keeps its runner writer for the life of the same active trace and stops it when the trace or session ownership changes. The request-scoped limit stays for downloadable traces.

A second independent pass found that an unleased explicit xctrace report loses the template field (session-perf-runtime.ts:181). Stop an unleased CPU capture, then report on another explicit trace in the same session without a template. The base returned the saved capture's template. The head discards the unleased handoff and passes no template, so the returned and written report omits it. The rule: an unleased saved xctrace handoff keeps the base template fallback, and only a leased handoff and its template are discarded when an independent source is chosen.

A second independent pass found that a non-string RPC lease claim escapes the typed lease refusal (artifact-tracking.ts:102). The RPC schema only checks that meta is an object, so a numeric meta.leaseId reaches the new diagnostics code, which calls trim before admission and outside the router's error handler. HTTP then returns a generic exception where the base returned UNAUTHORIZED with LEASE_SESSION_MISMATCH. The rule: only string lease claims name and redact the diagnostics record, and the existing admission check handles the original malformed value.

No open inline thread from other reviewers still applies. These eight do not apply and can be resolved: #4945 (comment) (an id is reused only for the same tenant, verifier and output, so a leased registration gets its own id by design), #4945 (comment) (the lifecycle stamp is gone at head), #4945 (comment) (the owners file is gone and the owner is in the record id), #4945 (comment) (owners.every() is deleted and a record has one owner), #4945 (comment) (both the trimmed and canonical lease id are registered as sensitive), #4945 (comment) (an unminted name is read only from unleased requests and a missing file returns 404), #4945 (comment) (trace stop under a lease was fixed at f307a48), #4945 (comment) (the perf report uses the requester's lease and tenant).

Not verified here: any test, mutation or check:affected run, and whether each new test fails at f307a48. Every finding above comes from reading b2e982f against f307a48 and base bdf5096. Before merge, fix the six findings and wait for the one other check to finish.

@thymikee

Copy link
Copy Markdown
Member Author

Addressed the four requested findings from the maintainer review in ce3c247f0b.

  • Each retained daemon-owned xctrace source records its producing verifier. Explicit reports check the resolved path and file identity even after a newer capture replaces the session handoff.
  • Perf stop resolves the tenant from the stored capture, then the session lease, then the request. It preserves an explicitly unleased producer.
  • Teardown registers an explicitly unleased recording under the session lease tenant.
  • An unleased caller-owned trace keeps its Apple runner writer until the trace stops or the session changes ownership. Downloadable traces keep their request lifetime.

The regressions failed against the earlier behavior. All 216 focused tests, 1,000 wire and eager-closure tests, wire compatibility, typecheck, lint, and Fallow passed. The earlier affected run passed with packaging. The final affected run is queued behind the shared validation lock.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of ce3c247

Since the review of b2e982f: 0 new · 0 still open · 2 no longer raised (2 marked fixed)

No open findings.

Not in the reviewers' prompt (1 file)
  • test/wire-compat/ledger.json: left out as an asset or for its size; reviewers could still search it

Reviewed with this repository's own Sentinel configuration (e10ad0cc2fb8).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant