Repository navigation
Deploy pipeline: apply three findings from the #256 review to the combined stack - #264
Merged
Merged
Conversation
…pplied to the combined stack From the Copilot review of #256, the ones the combined stack still had. Pruning a deploy directory to its record went into `.icp`, `data` and `mappings` by name, following a link: one planted there had what it pointed at emptied. Each part is now gone into only as a real directory, and a link in its place is removed like anything else that is not the record. A deploy that failed, not stopped, with a call unanswered returned the error chain alone. It now carries the warning an interrupted run already did: the call may have created a canister that nothing lists, and the caller's next move is a retry. The mapping a deploy hands back fills in every canister it created, but the record left on disk was whatever the id store had managed to write. The record is now brought up to the answer before the directory is cut down, and the log says so when that write fails. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
There was a problem hiding this comment.
🟡 Changes recommended
A critical symlink-swap race in pruning attacker-owned deploy directories can allow deletion of files outside the deploy directory with the pruning process’s permissions.
1 open issue
What changed in this PR
Applies three deploy-pipeline fixes from #256 to the combined stack in #261, improving cleanup safety and deployment recovery.
Changes:
- Adds directory-type checks when pruning deployment records.
- Warns about unanswered calls when a deployment fails.
- Reconciles saved canister IDs with the returned mapping and adds three regression tests.
| File | Description |
|---|---|
crates/imcp2-core/src/deploy/run.rs |
Updates pruning checks, failure warnings, record persistence, and regression tests. |
Review effort: Max
| } | ||
| dir.push(keep); | ||
| if !dir.is_dir() { | ||
| if !std::fs::symlink_metadata(&dir).is_ok_and(|m| m.is_dir()) { |
aterga
added a commit
that referenced
this pull request
Nov 19, 2026
…ools, remote serving, metrics (#261) * Deploy tools 1/6: a bundle host in imcp2-core — extraction, the untrusted project, and the checks before a deploy The first step of deploying an icp project bundle from the MCP servers: `imcp2_core::deploy::host`, which takes a bundle archive and makes of it something the icp CLI's project library can deploy from, treating the bundle as untrusted input. Nothing calls it yet. - **Extraction** reads the archive as a stream and writes regular files and directories only, confined to its directory, refusing the `.icp` state a deploy seeds for itself. Both sizes are held as it reads: the archive's, and what the entries occupy (whole 4 KiB blocks and 1 KiB an entry, which is what a bundle costs a server that keeps it in memory). How large is the server's to say (`BundleLimits`). - **The project** is loaded through the library's own untrusted rule (`icp_project::untrusted`): no script step, no remote source, no recipe, no path through a link; what the load retains is metered. - **The checks before a deploy** (`check_bundle`): every canister built by one pre-built module that hashes to its recorded checksum, the Candid a bundle carries held to what the parser takes, the copy of each canister's module the deploy will keep counted against the limit, and every sync plugin one the server runs (`AllowedPlugins`: any, on the user's own machine; the releases a server knows, by the hash of their module, where it deploys for others). This is where the dependency cost lands: `icp-project` and its siblings, and with `icp-sync-plugin` the wasmtime runtime, in every build of imcp2-core. The `candid` floor moves to 0.10.39 for the subtype check a deploy's upgrade compatibility check runs. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Address review: the state directory is refused by what is on disk, not only by how the archive spells it An entry under `.icp/` was refused by comparing its first component with the name. A name is the disk's to interpret: Windows takes `.icp.`, `.icp ` and `.icp::$INDEX_ALLOCATION` for `.icp`, so an archive could supply the id mappings the check is there to keep out. The spelling check stays, as the refusal that names the entry before it is written. Once the archive is out, the extraction directory is asked whether `.icp` is there, which answers for every spelling the disk honours. The Windows alias test is gated to Windows and has not been run: CI tests on Linux only. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools 2/6: an upload store that extracts a bundle as it arrives For a server the agent cannot hand a file to. A tool call carries JSON the model writes, which cannot be an archive, so the bundle arrives on an ordinary `PUT` authorised by a one-time ticket minted inside an authenticated tool call (`imcp2_core::deploy::Uploads`). - **Tickets**: 256 random bits, single use, five minutes, one pending per session, and the upload deployable by the session that minted it alone. - **Extracted as it arrives.** The body is fed to the bundle host a chunk at a time, on a thread that owns the body and cleans up after itself, so the archive is nowhere whole and the upload stops at the byte that takes the archive, or what it extracts to, past its limit. What the store holds is the extracted bundle, which its deploy takes by a rename. What is not a bundle, or carries a sync plugin the server does not run, is refused here, before any deploy is asked for. - **Bounded in the bytes it keeps**, since a server's state directory may be a RAM filesystem: one budget for the store, in extracted bytes. A bundle arriving or being deployed is charged the most one may come to, one waiting what it came to; a session has one upload at a time; an upload waits five minutes for its deploy; a body that stalls or takes too long is given up on; and what an earlier process left is removed when a store is made. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools 3/6: the pipeline — deploy a bundle as the user, and stop without losing a canister `imcp2_core::deploy::run::deploy_bundle`: extract (or take) the bundle, load it as an untrusted project, check it, and run every phase of `icp deploy` through the icp CLI's project library, reporting each phase as it completes. - **Signed as the user, for as long as it runs.** A per-app delegation lasts five minutes and a deploy can outlast it, so the agent signs through an identity that is refreshed alongside it, pinned to the account the first delegation resolved to: the principal that creates a canister is the one that goes on to install it. `Identities` says which account a delegation was resolved to, and which failure ends a session. - **Canister calls from certificates.** Whether a canister exists, what is installed on it and where it sits are read from the state tree, not from what a canister says of itself. - **A stop loses nothing.** A canister call in flight may already have created and funded a canister that only its answer names. So a stop lets that call run to its answer, which is recorded, refuses any call after it, and gives the run twenty seconds to end by itself; a deploy stopped before it began anything says so and creates nothing. - **A record outlives an interrupted deploy.** Its directory is cut down to the ids it created and kept, for a caller who never got the answer; a start cuts down what a killed process left, and old records are removed. - **The server's own failures are told from the request's**, so a caller knows whether trying again is any use. Known limit: a sync plugin holds its thread while it runs, so the twenty-second grace cannot cut one short. Its canister calls are refused once a stop is asked for and the plugin runtime has its own compute deadline, but a hard deadline belongs upstream, in icp-sync-plugin. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools 4/6: the MCP tools, and the local server serving them - **The tools** (`imcp2_core::deploy::IcDeployTools`, mounted beside the core surface by `WithDeploy`): `icp_deploy_bundle` deploys a bundle to the Internet Computer as the user, every phase of `icp deploy`, creating the canisters the given mapping does not name and upgrading those it does, and reports each phase as it completes; `icp_cycles_balance` reports the balance those deploys create canisters from, and the principal that holds it; `icp_deploy_upload` mints an upload ticket, on a server that takes its bundles that way; `icp_deploy_guide` returns the skill that walks an agent through the loop (also a resource). - **One way in per server.** A server on the project's machine reads the bundle at the path the agent names; one the agent cannot hand a file to takes an upload, and reads no path a caller gives it. Each lists the one argument it takes. - **Bounded.** One deploy per session, and so many at once across sessions in slots a process can share between its endpoints: past either a deploy is refused, not queued. A server that is stopping begins none and interrupts those running, and can wait for them to have ended. - **Observable.** What became of each deploy call — refused, and why; or how it ended, and how long it held its slot — is told to an observer, for a server to count: the call itself is one request among many to the transport that carries it. - **imcp2-local serves them**, signing as the user's default Internet Identity account at `https://cli.id.ai`, the principal `icp identity link web` links the CLI to, with each deploy's directory under the user's local data directory. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools 5/6: serve them on the remote server, with the bundle uploaded on a ticket The deploy tools on the `imcp2` server, for an embedder that turns them on (`McpServer::with_deploy`); the bundled binary always does. - **How the bundle arrives.** `icp_deploy_upload` mints a ticket inside an authenticated call, and the archive is `PUT` to `{mcp_path}/deploy/upload` on it. The route is outside the bearer gate — the ticket is its authentication — and the body is extracted as it arrives: 413 at the byte that takes the archive, or what it extracts to, past its limit, and 422 for what is not a bundle or carries a sync plugin the server does not run. The deploy tool here refuses a path: the server opens no file a caller names. - **A deploy call is answered as an event stream**, so each phase reaches the client before the result; every other request keeps the plain JSON answer. A client that goes away stops its deploy. - **One deploy state for the process** (`Deploys`): the slots, the upload store and the word that the server is stopping are shared by every endpoint. It locks its directory, so a second server over the same state directory is refused rather than clearing the first one's uploads; clears what a killed process left; and sweeps what has lapsed. - **Stopping.** `Deploys::stop` before the drain tells running deploys to end and refuses new ones, and `Deploys::stopped` waits, bounded, for them to have ended: a deploy waiting on a canister call whose client has gone holds no connection for a drain to wait on. The server's own `spawn_session_reaper` and `shutdown` cover the sweeper and the stop for an embedder that does nothing more. - **Configuration** is `DeployConfig`: deploys at once, the archive and extracted limits, the budget for every bundle held, and the sync plugins run besides the ones the server knows. The library reads no environment variable; the bundled binary maps `MCP_DEPLOY_*` onto it and refuses to start on values that contradict one another. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools 6/6: count deploys and uploads on the remote server A deploy is one `tools/call` inside a `POST`, answered 200 whether it succeeded, failed or was refused for want of a slot, so the request metrics say nothing about deploys; and the upload route's status codes fold several reasons into one. The `Metrics` handle an embedder already builds over its servers also publishes, where a server serves the deploy tools, as totals for the process: - `imcp2_deploys_running` against `imcp2_deploy_slots`: how full it is. - `imcp2_deploy_refusals_total{reason}`: busy, session_busy, stopping, no_room. A sustained rate of `busy` is a cap that is too low. - `imcp2_deploys_total{outcome}`: succeeded, failed, rejected, setup_failed (the server's own failure, not the request's), interrupted; and `imcp2_deploy_duration_seconds`. - `imcp2_deploy_uploads_total{outcome}`: accepted, bad_ticket, too_large, extracts_too_large, not_a_bundle, plugin_not_allowed, store_full, session_busy, body_error, storage_error. - `imcp2_deploy_upload_bytes` and `imcp2_deploy_bundle_extracted_bytes`, which is where the two size limits should sit is read off. - `imcp2_deploy_upload_store_bytes` against its capacity, and the uploads held and arriving: what the deploy tools keep under the state directory. The counters are fed as things happen, a deploy dropped mid-run and an upload abandoned mid-body included; the gauges by `Metrics::refresh`. Every label value is a variant named in code: nothing a client sends becomes a label. Every series starts at zero. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Deploy tools: address the Copilot review of the combined stack - Keep a deploy's record whenever it created a canister, not only when it was interrupted: the answer is sent after the deploy returns, and a client that goes away while it is sent would otherwise leave funded canisters with no record. Records still expire on the hosted server. - imcp2-local: on stdin EOF, cancel the calls still running and wait for a deploy to end (STOP_GRACE plus a margin) before the runtime exits, rather than rmcp's five-second drain. - The upload store holds the deploy directory's lock for as long as it, or a thread extracting into it, lives. - Hosted: forward `notifications/cancelled` to the running deploy call it names, by session and JSON-RPC id; the stateless transport drops it. - The upload command uses `--fail-with-body`, so a refused upload prints the server's reason. - Seed the caller's canister-id mapping in one write, off the runtime, after checking every name. - Remove bundles off the store's lock and off the async workers, keeping their room charged until their files are gone. - `Deploys::new` refuses a state directory that is not UTF-8 with `InvalidInput`, before clearing anything. - `Deploys::stop` also stops uploads: new ones are refused (503, counted as `stopping`) and one arriving is given up on at its next read. Error classification (HTTP 5xx) is unchanged: it is already identical to the icp CLI's `AgentCalls::wrap`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za * Raise candid to 0.10.41 and candid_parser to 0.4.2 candid 0.10.41 makes the subtype check linear on a chain of `opt` aliases; candid_parser 0.4.2 loads a long alias chain without overflowing the stack or taking quadratic time. candid_parser 0.4 wraps a function's argument and result types in `IDLArgType`, so the two places that document them read `.typ`. icp-project 0.1.0 requires candid_parser 0.3, so the tree now holds both: the library's own parsing still runs on 0.3.2. The comment on the candid floor in Cargo.toml is dropped. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV * Upload store: remove bundles off the state lock, and make an existing directory private Two review findings on the upload store. A bundle that lapsed or was replaced was removed (a recursive delete) under the state mutex, on whichever Tokio worker ran mint, receive or take, so every session's calls waited on it. The store now only stops holding the bundle under the lock; the tree is removed once the lock is released, on a blocking thread. Its bytes stay charged until it is gone, so the budget is still of what is on disk. An arrival or a deploy that finds a lapsed bundle counts it as room, as before, and removes it before it writes: on the extraction thread for an arrival, awaited for a deploy. A store directory that existed already was taken as it was, whatever its mode. Under one other users can write to, a bundle's directory could be renamed away and another tree put in its place before the deploy. An existing directory is now made 0700, at the store's making and at each upload, and the upload refused if it cannot be. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV (cherry picked from commit c779da6) Conflict with 271dda6 resolved by taking this commit's removal of lapsed and replaced bundles in place of 271dda6's, and keeping 271dda6's upload stop (Uploads::stop, ReceiveError::Stopping) and directory lease (Uploads::held_with) on top. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za * Deploy tools: address the second Copilot review (forwarder, signals, tickets) - Don't wait past a stop, or more than 30 seconds, for a finished deploy's last progress notifications: a client that keeps the stream open without reading it would otherwise hold the call and its slot. The outcome's log carries every line regardless. - imcp2-local: stop on SIGINT/SIGTERM as on stdin EOF: cancel the calls running and wait for a deploy to end before exiting. - Refuse an upload ticket the store never minted before sweeping: the route is outside the bearer gate, and the sweep is work under the store's lock. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za * Deploy pipeline: three findings from the review of the pipeline PR, applied to the combined stack (#264) From the Copilot review of #256, the ones the combined stack still had. Pruning a deploy directory to its record went into `.icp`, `data` and `mappings` by name, following a link: one planted there had what it pointed at emptied. Each part is now gone into only as a real directory, and a link in its place is removed like anything else that is not the record. A deploy that failed, not stopped, with a call unanswered returned the error chain alone. It now carries the warning an interrupted run already did: the call may have created a canister that nothing lists, and the caller's next move is a retry. The mapping a deploy hands back fills in every canister it created, but the record left on disk was whatever the id store had managed to write. The record is now brought up to the answer before the directory is cut down, and the log says so when that write fails. Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV Co-authored-by: Claude Fable 5.2 <[email protected]> * Deploy tools: end-to-end tests for shutdown and cancellation, and the bug they found Tests: - imcp2-local, the built binary driven over its pipes (tests/shutdown.rs): it ends cleanly when its client closes the pipe, and on SIGTERM/SIGINT with the pipe still open. - imcp2-local: as it ends it waits for a running deploy, and for no longer than a stopped deploy is given. - Hosted, over the composed router with only the network stubbed (the IC endpoint is a listener that never answers): a `notifications/cancelled` naming a deploy call in flight stops it, one naming another call does not, and the call answers on its stream and frees its slot. - Hosted: a stopping server refuses an upload with 503 and keeps nothing. - A finished deploy does not wait on progress nobody reads: not past a stop, nor longer than FORWARD_GRACE. Fix, found by the signal test: with a signal handled (6389dc1), imcp2-local no longer exited on SIGTERM while its client kept the pipe open. The transport reads stdin on a runtime thread that cannot be interrupted, and returning from `serve` waited on it. On a signal the process now stops its calls, waits for a running deploy, and exits. The handlers are installed before the MCP handshake, so a signal during it ends the process too. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za * Deploy tools: address the third Copilot review (dispatch cap, upload cleanup, guide) - The deploy dispatch buffered each request under a fixed 4 MiB cap of its own, so with the deploy tools on, a request cap raised with $MCP_MAX_REQUEST_BODY (or `with_max_request_body`) was held to 4 MiB for every call. It now reads up to the endpoint's configured cap, which the layer in front of it already enforces. - A bundle its deploy never took (the deploy was refused or stopped first) was removed by the drop of its handle, on a runtime worker. It is now handed to a blocking thread like a lapsed bundle, charged until it has gone; one the deploy did take frees its room at once, as before. - The deploy guide: signing in is said for both kinds of server (the local login tools, or reconnecting in the MCP client on a hosted one), and a cut-off upload is checked against the limits before it is taken for too large, with one retry on a new ticket when it is within both. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za * Deploy tools: document the build's uncounted scratch copies, and the fixed default account The size budget counts a bundle and the copy of each module the artifact store keeps. While a canister is built, the icp library also holds a scratch copy of its module in the deploy's directory until that build returns, and the canisters are built at once: a running deploy can pass the cap for a moment by what its modules come to, at most half the cap again. That is left uncounted on purpose and now said where the limit is described (check_bundle, BundleLimits, DeployConfig, the README), with the headroom a RAM-backed state directory needs. A deploy that started as the unnamed account asks for the default account again at each refresh. The doc on RefreshingIdentity::start now says why that is the same account for the deploy tools: the default at the origin they sign in at cannot be changed by the user. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01VP2vTFUYW9ZKb1Rjv176Za --------- Co-authored-by: Raymond Khalife <[email protected]> Co-authored-by: Claude Fable 5.2 <[email protected]> Co-authored-by: raymondk <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Targets
deploy-core-all(#261). Three findings from the Copilot review of #256 that the combined stack still had, all incrates/imcp2-core/src/deploy/run.rs.Changes
prune_to_recordwent into.icp,dataandmappingsby name, so a link in one of those places had its target emptied. Each part is now entered only if it is a real directory; a link there is removed like anything else that is not the record.Not included
The other six comments on #256:
271dda6: seed-mapping write failures classed as request faults.Failed.renameacross filesystems (uploads and deploys share a directory on the hosted server), the default account changing mid-deploy, andmainnet_agentnot inheriting the embedder's agent (deliberate per the code comments).Testing
cargo test --workspaceandcargo clippy --workspace --all-targetspass locally. Three new tests; the link test fails on the old pruning logic.🤖 Generated with Claude Code
https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV