Skip to content

refactor(remote): save a remote script from memory with the local publisher - #4802

Merged
thymikee merged 6 commits into
mainfrom
refactor/remote-script-delivery-in-memory
Nov 20, 2026
Merged

thymikee merged 6 commits into
mainfrom
refactor/remote-script-delivery-in-memory

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

The remote client staged each script version on disk beside the destination, confirmed the directory after the download, and kept an inode anchor, a pending-commit entry, a staging list and a retired flag so an interrupted commit could be settled. That defended a directory replaced mid-download (the ownership rule already excludes it) and a crash mid-commit (the next version repairs it).

Now a version is fetched into memory and committed under the record lock by publishScriptFileSync, the call the local route makes. 29 files, no scope expansion.

Removed: staging and settlement, confirmScriptDestination, parent_replaced / script_ancestry_replaced, privateFile, repeat warnings, and receipt reconciliation against the submitted plan. The record is { destination, force, revision }.

Before After
Production lines (touched files) 4296 3803
Test lines (touched files) 6319 5272

Guarantees, each test seen failing against broken code:

  1. A response never chooses the destination: only a newer version of a recorded publication writes (endpoint, tenant, session and ID each dropped from the record key), a peer value for savedScript never reaches the caller.
  2. 8 MiB cap: a version one byte over the cap…, an in-memory fetch returns a body of exactly its cap… (cap doubled; limiter removed). The fetch holds one buffer that grows to the cap, and an error answer is dropped after 65,536 characters: …very small chunks unchanged, an error answer… is dropped once its diagnostic is full.
  3. First version never replaces without --force: the first version never replaces… (always replace).

Accepted: after a crash between the publish and the record write, the next version of a first-version, no---force recording reports "file exists" with the --force hint.

Security pass, against the three guarantees and the local path rule: the destination comes only from the locked record; the publication ID reaches the filesystem only as a hash; fetched bytes are counted as they arrive; walk and commit are one synchronous call; a receipt close can only remove a record.

Validation

Head 0c39f0a: typecheck, lint, format, layering, production-exports, fallow audit --base origin/main, check:daemon-wire-compat (protocol stays 3), eager-closure and repo guards pass. Unit suites for packages/remote, host-kit, host-swift, daemon-client, test/wire-compat and the remote-script-publication provider scenario pass. check:affected was not run; CI owns the full run. Not device-facing. Self-review was a cold re-read of the diff, with no independent reviewer.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.45 MB 6.45 MB -5.4 kB
Package (unpacked) 6.45 MB 6.45 MB -5.4 kB
Package (download) 1.97 MB 1.96 MB -2.0 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 31.0 ms 20.6 ms -10.4 ms
CLI --help 63.9 ms 42.4 ms -21.5 ms

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of dd70add

2 finding(s): 2 minor.

🟡 Minor (2)

Where Finding
packages/remote/src/script-publication-record.ts:76 Records an older client left retired parse as active again
packages/remote/src/session-script-destination.ts:98 A receipt close the submitted plan disproves now retires a still-recording publication

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

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

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

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at f11402c. The review at 1ae09ba found nothing that needs a code change, and the only change since is docs: docs/agents/security.md is back under the 40,000-byte budget, and the 65,536-character limit is now stated in ADR 0016. The kept guarantees hold, each with a test: the peer never chooses the destination, the fetch is capped at 8 MiB while it streams, the first version never replaces a file without --force, an older revision never replaces a newer one, close removes its record, and a delivery failure keeps the action outcome and is disclosed. Repo Guards, Coverage and Integration Tests exercise packages/remote, so let them finish on this head.

The two resolved threads on the retired:true record (#4802 (comment)) and on closesSubmittedPlan (#4802 (comment)) do not apply any more. The maintainer accepted both as scope decisions, so the author can resolve them.

Not blocking, and fine to take or leave: drop prepareScriptDestination and type ScriptDestination from the export list in packages/host-kit/src/file.ts (no package imports them now); add a test where the v1 fetch fails, a file appears at the destination, and v2 arrives, expecting script_target_exists with the file unchanged, so the first saved version (record.revision === 0) stays exclusive whatever its artifact revision; reword the test comments that still describe staging directories and the ledger rationale that still describes draining error answers (the client now stops reading once 65,536 characters are held); assert details.reason === "artifact_too_large" in the bare rejects at packages/remote/src/tests/artifact-download.test.ts:285; and split settle at packages/remote/src/artifact-download.ts:153 into resolve and fail so the result as T cast goes away.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
@thymikee
thymikee force-pushed the refactor/remote-script-delivery-in-memory branch from f11402c to 4aa5e92 Compare November 20, 2026 11:40
thymikee and others added 6 commits November 20, 2026 12:40
…lisher

The remote client fetched each script version into a staging directory
beside the destination, confirmed the directory after the download, and
kept an inode anchor and a pending-commit entry in its record so an
interrupted commit could be settled later. That defended a directory
replaced during a download, which the ownership rule already excludes,
and a crash between the rename and the record write, which the next
version repairs.

A version is now fetched into memory, capped at 8 MiB as the bytes
arrive, and committed under the record lock with publishScriptFileSync,
the call the local route makes. The record is { destination, force,
revision }; extra fields an older client wrote are ignored. A close
removes its record. A version that is not newer is answered from the
record with no warning. A batch receipt that names a close retires the
record without reconciling it against the submitted plan.

host-kit loses confirmScriptDestination and the parent_replaced
exposure, so script_ancestry_replaced is no longer emitted.

Accepted: after a crash between the publish and the record write, the
next version of a first-version recording without --force reports that
the file exists, with the --force hint.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The artifact request moved into requestRemoteArtifact and gained an
in-memory consumer, so the wire surface lists the new declarations, the
tenant-header mutation case targets the function that now builds the
request, and the ledger carries the new digests with a rationale for
each changed declaration. No request, header, route or response framing
changed, so the protocol version stays at 3.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The in-memory fetch kept every chunk and then concatenated them, so a
body at the cap held twice the cap and a body in tiny chunks kept one
buffer per chunk. It now collects into one buffer that doubles up to
the cap.

A non-200 answer was read to its end or to the 90-second timeout while
only the first 65,536 characters were kept. The request now fails with
the HTTP status as soon as that much is read and drops the response.
The file and directory downloads share the handler and report the same
error shape.

Also removes a settlement field and an export nothing reads, and
corrects ADR 0016, the website, the CLI help and the security notes
where they said more than the code does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The artifact sink is local I/O plumbing with no wire shape, so the
request function spells it inline and the surface no longer lists it.
The ledger carries the digests of the bounded fetch and of the
unexported publish outcome, and the materialize rationale says the wire
fields are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The two sentences on script publishing are shorter than the ones they
replace, and the exact error-answer length moves to ADR 0016.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
host-kit stops exporting the ScriptDestination type, which has no
importer left; prepareScriptDestination stays for the Swift cache. The
artifact request settles through separate succeed and fail paths, so
the result cast is gone. A new test pins that the first version saved
stays exclusive when an earlier fetch failed. Test comments and one
ledger rationale no longer describe staging or reading an error answer
to its end.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the refactor/remote-script-delivery-in-memory branch from 4aa5e92 to 0c39f0a Compare November 20, 2026 11:41
@thymikee

Copy link
Copy Markdown
Member Author

Thanks. All optional notes are applied at 0c39f0a, on top of a rebase onto current main (no behavior change):

  1. Exports. type ScriptDestination is no longer exported from packages/host-kit/src/file.ts. prepareScriptDestination stays: packages/host-swift/src/swift-cache.ts imports it.
  2. Test. Added a later version stays exclusive while no version was saved yet: the version 1 fetch fails, a file appears, version 2 arrives without --force, and the result is script_target_exists with the file unchanged. It fails when exclusivity is keyed on the artifact revision instead of the record's.
  3. Wording. The test comments no longer describe a staging directory, and the ledger rationale says the client stops reading an error answer once 65,536 characters are held.
  4. Assertion. The one-byte-over case asserts details.reason === 'artifact_too_large'.
  5. Cast. settle is split into succeed and fail; the as T cast is gone.
  6. Threads. Both scope-decision threads are resolved.

The rebase picked up #4799: its group rule, closedToOwnGroup, callerOwnsAncestor and its ADR and website wording are kept, together with this PR's removal of confirmScriptDestination and parent_replaced.

@thymikee

Copy link
Copy Markdown
Member Author

Still ready at 0c39f0a. The rebase over #4799 keeps its group rule and callerOwnsAncestor hint, and the removed confirming walk stays gone. The removed "directory changed while it downloaded" table depended only on that walk and the staged file. Since f11402c the only production change besides the rebase is the settle split in artifact-download.ts. succeed and fail share one settle-once guard, fail still runs the cleanup before rejecting, and every former error path now calls fail, so behavior is unchanged without the as T cast. ScriptDestination is no longer exported. The new test pins that a later version stays exclusive while no version has been saved yet. The label stays.

@thymikee
thymikee merged commit 7de4587 into main Nov 20, 2026
20 of 21 checks passed
@thymikee
thymikee deleted the refactor/remote-script-delivery-in-memory branch November 20, 2026 12:00
thymikee added a commit to pcbeingused333/agent-device that referenced this pull request Nov 20, 2026
* origin/main:
  test: remove five timing races from CI (callstack#4800)
  refactor(remote): save a remote script from memory with the local publisher (callstack#4802)
thymikee added a commit that referenced this pull request Nov 20, 2026
* origin/main:
  feat(gesture): hold before the move with gesture pan --hold-ms (#4767)
  docs: clarify device verification and security proposals (#4805)
  fix(daemon-client): say that a hung daemon is stopped before ensureDaemon starts a new one (#4803)
  test: remove five timing races from CI (#4800)
  refactor(remote): save a remote script from memory with the local publisher (#4802)
  refactor(apple-runner): one RUNNER_ERROR_CODES row per runner error code with parity/docs gates (#4774)
  fix(android): require acknowledgements for test IME typing (#4722)
  feat(scroll): add scroll refresh for pull-to-refresh (#4770)
  refactor(host-kit): add request Scope and use it for the wait capture deadline (#4794)
  fix(android): let permission all change nothing on an app that declares no permissions (#4796)
  fix(cli): replace the CLI session and generated remote config instead of rewriting them in place (#4797)
  fix(host-kit): accept a group-writable script directory the user owns (#4799)
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