Skip to content

fix(remote): download trace and perf captures only to client-chosen or generated paths - #5338

Merged
thymikee merged 8 commits into
0.22from
candidate/client-destination-022
Dec 3, 2026
Merged

thymikee merged 8 commits into
0.22from
candidate/client-destination-022

Conversation

@thymikee

@thymikee thymikee commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

The client never writes a downloaded trace or perf artifact to a path the daemon echoes in artifact.localPath. A remote daemon controls that value. Output goes to the command's own --out, else to a locally named trace-<uuid> or perf-<uuid> in cwd. A daemon-supplied name never sets the suffix: .log, .json, .tar.gz, .perfetto-trace, .perf.data, .hprof or .memgraph comes from the local command and capture kind. start --out X no longer carries over to a stop in another process. Docs: website/docs/docs/remote-proxy.md. The wire ledger digest for resolveMaterializedArtifactPath and its compatibility acknowledgement are updated (client-only change).

Before / after

Before: trace stop and perf ... stop wrote to the daemon-echoed localPath, so ../../etc/x or an absolute path worked.
After: that branch is gone. stop takes its own --out.

Validation

Head 8442c27; CI pending.
pnpm exec vitest run --project unit-core packages/remote/src/__tests__/daemon-artifacts-capture.test.ts: 27 passed. With the production file reverted to 0.22, 2 hostile-localPath tests fail. The suffix trace case fails without the suffix. wire-compat.test.ts covers the ledger.
Production delta versus 0.22: +41 / -28 (69 lines) in packages/remote/src/daemon-artifacts.ts.
No unresolved risk known.

@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 8b5a62d. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved
@blacksmith-sh

This comment has been minimized.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 5.29 MB 5.29 MB +759 B
Package (unpacked) 5.28 MB 5.29 MB +759 B
Package (download) 1.62 MB 1.62 MB +270 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 19.4 ms 18.9 ms -0.5 ms
CLI --help 32.2 ms 28.3 ms -3.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: 1 new finding on 3648ee2. The verdict and the full list are in the summary comment.

@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 7c8f377. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved
@blacksmith-sh

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Review of 4b69a38: the change holds its main invariant, but six defects in how capture intents are scoped, retired and delivered need fixes before merge. CI is green, with 24 checks and none failing at this head, and there are no conflicts.

A second independent pass found that two sessions or leases with the same local state, endpoint, credential and capture family can share a token. A pathless stop in the second session accepts the token learned by a named start in the first session, so it writes to the first capture's destination. Bind each intent to the local request scope as well as the token: tenant and lease identity for leased traffic, the caller's session identity for unleased traffic. Use the existing session and lease routing rules for aliases.

A second independent pass found that a named start followed by a stop with a different explicit destination leaves the start intent alive. The unchanged daemon echoes the explicit stop path (trace-runtime.ts:89–107, session-perf-runtime.ts:379–390), so the client delivers it and returns without retiring the saved intent. A later pathless stop in the same scope can then write to the earlier capture's original destination. After a successful explicit stop, retire the pending start for that local request scope under the record lock. Keep a newer start if its generation already replaced the stopped capture.

A second independent pass found that a daemon can turn a capture file destination into a directory. If the daemon describes the outPath artifact of a trace or native perf stop as test-artifacts, the token handler skips the intent, but the generic downloader still trusts the artifact type (artifact-download.ts:63–72 and :353–362). It then publishes an archive-named child under the selected file path. Allow directory delivery only for requests that expect a suite directory. For trace and perf outPath fields, require file delivery or reject a directory-shaped response.

A second independent pass found that the cwd fallback can fail for a valid legacy basename. A legacy client that starts a trace with a 240-byte basename on a filesystem with a 255-byte component limit gets that basename echoed back (trace-runtime.ts:107). The 37-byte suffix then makes a 277-byte component, so the pathless stop fails with ENAMETOOLONG instead of writing the documented cwd file. The fallback also keeps the first dot-delimited stem from the response, which can retain a reserved device name on Windows. Generate the fallback basename locally from a fixed stem and a UUID. Keep only a validated, bounded suffix where the format needs one.

A second independent pass found that a failed transfer renews a seven-day-old intent. A stop that claims an intent shortly before the limit rewrites the record through writeIntent, which refreshes its mtime. After the original seven days pass, readIntent (line 145) still authorizes the saved path, although remote-proxy.md:116 says it has expired. Record an immutable creation time and use it for both the expiry check and the eviction order. Store the claimed artifact ID separately.

A second independent pass found that a v0.21.46 client treats a new client's token as a literal path. Start a named remote trace with a client at this head, then stop it without a path using v0.21.46 against the same daemon. The daemon returns the token in localPath, the released resolver (daemon-artifacts.ts:766) uses it directly, and a token-named file lands in cwd. The base preserves the chosen start destination for the same pair. The compatibility fixture (http-server-capture-destination-compat.test.ts:148–176) pairs each client's start with its own stop, so it does not cover this switch. Document the version boundary in the user-facing workflow, and require a token-aware stop or an explicit stop path when switching versions mid-capture. Transparent pathless compatibility would need a carrier that legacy clients do not interpret as a path.

Not blocking, and these can be taken or left: intentPath, writeIntent, readIntent and the TTL sweep re-implement the destination-record store that script-publication-record.ts already owns, so one generalized store with a namespace parameter and optional max-count eviction would serve both; perf native-capture stop JSON now reports the token as clientOutPath (capture-destination.ts:206, via session-perf-runtime.ts:379,389) where the base reported the caller's path, so remove it or replace it with the destination; an unmatched token makes the fallback file name the token with no .log or .trace extension (daemon-artifacts.ts:775), so give download a family-based name instead; the -<uuid> suffix in remote-proxy.md:114 applies to every trace and perf output without a path, including perf memory snapshots, so the docs should say that; the Windows ':' refusal (daemon-artifacts.ts:813) and the layering parseImports refactor are unrelated to this fix and fit better in their own PRs or a named PR body item; the intent key includes the daemon bearer credential (capture-destination.ts:70), so a token refresh mid-session loses the intent unless remote credentials never rotate; and one daemon-client transport test with a remote baseUrl (start with a destination, stop without a path, assert outPath equals the destination) would cover the production wiring that all new tests bypass (packages/daemon-client/src/index.ts:199).

Not verified here: no tests were run, and CI is the evidence for this head. Regression validity was judged from the diff, and the changed daemon-artifacts-capture case would fail on base. Not verified here: that client.stateDir equals settings.paths.baseDir on every transport path, that the layering parser output is byte-identical, or whether cloud-issued credentials rotate during a session. No live remote run exists at this head. The change is client-side delivery, so live evidence was not treated as required. After the fixes, a push with matching tests for the scope binding, the explicit-stop retirement and the directory-delivery check is needed before merge.

@thymikee
thymikee force-pushed the candidate/client-destination-022 branch from 4b69a38 to 9286c42 Compare December 3, 2026 07:29
@blacksmith-sh

This comment has been minimized.

@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: 3 new findings on 9286c42. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved
Comment thread packages/remote/src/daemon-artifacts.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved

@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 finding moved to a new line on 41591ee. The verdict and the full list are in the summary comment.

@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 finding moved to a new line on d5737ba. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved
@thymikee
thymikee marked this pull request as ready for review December 3, 2026 10:11

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 4 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread website/docs/docs/remote-proxy.md Resolved
@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 6a9ff24. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/daemon-artifacts.ts Resolved
@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Three problems remain at 6a9ff24, and the earlier findings (#5338 (comment)) are otherwise addressed: the ledger digests, the outPath guard, the kind-based suffix and the docs note are fixed. The code needs these changes before merge. Not verified here: the end-to-end remote CLI to daemon route with a real daemon, and no tests were run.

A second independent pass found that the earlier directory-delivery problem still holds at daemon-artifacts.ts:619. For a trace or perf outPath, the resolver picks a file at line 747 or 751. If the daemon describes that artifact as test-artifacts, artifact-download.ts:63 creates a directory there and publishes a child inside it. The caller then gets a child path, not the capture file. Directory delivery should apply only when the local side expects a suite directory. Trace and perf outPath artifacts should require file delivery, or a directory-shaped response should be rejected before any destination is prepared.

A second independent pass found that remote-proxy.md:103 tells CLI users to run trace stop --out capture.log. The trace command takes a positional path (index.ts:90, read at :106). The strict parser rejects the flag (args.ts:199), so the trace is never stopped. The docs should show trace stop <path> and perf ... stop --out <path> as separate forms.

A second independent pass found that the Android profiling examples at cli-help-workflows.ts:165 set --out on start and omit it on stop. On base, stop used the start destination. At this head, daemon-artifacts.ts:747 writes a generated file in the working directory, so /tmp/app.perfetto-trace and /tmp/cpu.perf.data are never created. Each Android stop command in the example should pass the same --out path it displays.

Not blocking, and fine to take or leave: after a remote perf trace start --kind perfetto --out /tmp/app.perfetto-trace and a pathless stop, the stop data still carries clientOutPath set to that start path while the file is written to a generated path (daemon-artifacts.ts:750), so dropping or rewriting that field would help, and the PR title still describes the deleted intent-record design.

None of the open inline threads from other reviewers still apply. These can be resolved: the ledger digests are updated at head (#5338 (comment)); the generated-name branch is limited to outPath with a kind-based suffix (#5338 (comment), #5338 (comment)); --kind is now mapped, with tests (#5338 (comment)); the docs now describe the suffix rule (#5338 (comment)); and the generated path uses only the command, a local UUID and the kind table, so no daemon string reaches the filesystem (#5338 (comment)).

The only check not passing is Body rules, which was cancelled. It validates PR body text and does not exercise the changed routes, so it is likely unrelated; it needs a re-run to show green. No conflicts are known. Before merge, the three changes above are needed and the cancelled check must pass.

@thymikee
thymikee force-pushed the candidate/client-destination-022 branch from 6a9ff24 to 8442c27 Compare December 3, 2026 11:26
@thymikee thymikee changed the title fix(remote): keep capture destinations in client intent records fix(remote): download trace and perf captures only to client-chosen or generated paths Dec 3, 2026
@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Fixed in 8442c27 (rebased onto current 0.22). A trace or perf outPath artifact is now always delivered as one file, even when the daemon calls it test-artifacts. An xctrace .trace bundle already arrives as a single .tar.gz file, so that case is unchanged, and only the suite directory path keeps directory delivery. The trace stop <path> and perf ... stop --out <path> docs forms are split, and both Android stop examples pass their displayed --out. I left the daemon-side clientOutPath field alone.

@blacksmith-sh

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

The three problems from the earlier review (#5338 (comment)) are fixed at 17408d5, but a second independent pass found one new problem: a failed trace or perf download can delete an existing local output file. CI is green at this head: 19 checks, none failing, and the Body rules check that was cancelled at 6a9ff24 has passed. No conflicts.

When a trace or perf stop names an existing local output file, the remote daemon describes the outPath artifact as test-artifacts, and the download returns an error before writing any bytes. The base takes the directory branch, fails while preparing the existing file as a directory, and leaves the file alone. The new code sends that same response down the ordinary file branch. Its HTTP failure handler then calls rm on the caller's existing file, although the transfer never opened it (https://github.com/callstack/agent-device/blob/17408d5/packages/remote/src/artifact-download.ts#L63, L79, L184, L217; entry point https://github.com/callstack/agent-device/blob/17408d5/packages/remote/src/daemon-artifacts.ts#L623). The caller gets a download error and loses the previous file. The rule to satisfy: a test-artifacts response for a trace or perf outPath is rejected before the destination is prepared, so no failure path can remove a file the transfer did not create. Keep the existing suite directory delivery. A test for an existing output file plus a test-artifacts response should show the file is still there after the error.

The four earlier inline threads on the ledger digest and the generated-name, suffix and --kind handling are fixed at this head and can be resolved (#5338 (comment), #5338 (comment), #5338 (comment), #5338 (comment)). The docs thread is also fixed (#5338 (comment)). The thread on generatedCapturePath ignoring the artifact does not apply, because that is the invariant itself: no daemon-supplied string reaches the local filename (#5338 (comment)).

Not verified here: the changes were read, not run, so the new directory-shaped test case was judged valid from the harness and the old isDirectory expression. Not verified here: the full remote CLI to HTTP daemon to download route was traced in code but not run against a live daemon. After the fix above and green checks, nothing else stops the merge.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Fixed at 458b9d5. A trace or perf outPath answered as test-artifacts is now refused before any local path is prepared, with the new reason remote_capture_output_directory_shaped. No request is made and no failure handler runs, so an existing file at the outPath stays byte-for-byte; a new test covers it. The earlier single-file delivery for that case is gone, and suite directory delivery is unchanged. The four ledger threads and the docs thread are fixed at this head, and the generatedCapturePath thread is the invariant itself.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 458b9d5

Since the review of 17408d5: 0 new · 0 still open · 0 no longer raised

No open findings.

1 finding below minor hidden (user_settings.min_severity).

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

💬 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.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

The code at 458b9d5 looks good. The earlier problem is fixed: a failed directory-shaped trace or perf download no longer deletes an existing output file. CI is green, with 19 checks and none failing. No conflicts are known, and nothing else blocks merge. The docs thread on perf download suffixes (#5338 (comment)) is fixed at this head, so it can be resolved. Not verified here: the new test was read, not run, and the full remote CLI to daemon download route was traced in code, not run against a live remote daemon.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 3, 2026
@thymikee
thymikee merged commit ad62567 into 0.22 Dec 3, 2026
19 checks passed
@thymikee
thymikee deleted the candidate/client-destination-022 branch December 3, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant