Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/mcpx/docs/changes/artifact-copy-semantics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sections: delivery, the-script-api-is-the-same-everywhere, what-a-remote-endpoint-is-not
Comment thread
dezren39 marked this conversation as resolved.
Resolved
kind: Changed
summary: artifact delivery prose aligned with the implemented copy semantics (#3064): the exec and remote docs no longer promise hardlink delivery or zero-copy cost where the artifacts package copies bytes; the #1660 stored-blob guarantee (in-place edits to outside files cannot mutate stored bytes) is unchanged
7 changes: 7 additions & 0 deletions pkgs/mcpx/docs/compendium.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,13 @@
"lines": 114,
"note": "the binary: what it is, how it is built and shipped"
},
{
"path": "docs/changes/artifact-copy-semantics.md",
"chapter": null,
"role": "not-compendium",
"lines": 3,
"note": "docs-history change fragment for #3064 (artifact copy semantics); history metadata consumed by the docs-history tooling, not a compendium page"
},
{
"path": "docs/cli.md",
"chapter": "designs",
Expand Down
14 changes: 8 additions & 6 deletions pkgs/mcpx/docs/exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,18 @@ What actually happens underneath differs, and only in a way nobody has to
think about. `artifact()` POSTs to `/v1/artifacts` over the transport the
generated client already has. When the runner set `MCPX_ARTIFACTS_LOCAL=1` —
which it does when a unix socket proves the daemon shares this filesystem —
a `{path}` is sent as a header and the store **hardlinks** it. Otherwise the
bytes travel once, to the daemon, and are then fetched by whoever wants them.
a `{path}` is sent as a header and the daemon reads, hashes, and copies the
file into the store. Otherwise the bytes travel once to the daemon and are
then fetched by whoever wants them.

Remoteness is declared, never inferred. A unix socket can be ssh-forwarded and
loopback TCP can be a container with its own filesystem; neither transport
tells you whether the caller can see the daemon's disk. So the *caller* says
so, with `capabilities: ["artifacts"]` and optionally `sharedFs`.

An `ssh://` endpoint is the clearest case of `sharedFs: false`: the bytes
cross a process boundary on another machine, so a `{path}` is never
hardlinked there and an artifact is uploaded instead.
cross a process boundary on another machine, so the caller uploads a `{path}`
artifact's bytes rather than giving the daemon a path it can read.

### The store

Expand Down Expand Up @@ -280,8 +281,9 @@ concurrent runs of the same script would lose.
| `inline` | base64 in the structured result, bounded by `artifacts.inlineMaxBytes` **and** the caller's `maxBytes` | a caller that cannot make a second request |
| `stream` | frames after `end` | one response, cancellable |

A `sharedFs` or `dir` beats all three: the caller can already read the bytes,
so hardlinking them into its directory costs an inode and no I/O.
A `sharedFs` or `dir` lets the caller read the destination directly, but the
store still copies the bytes into that directory; this saves a later transfer,
not filesystem I/O.

Reference is the default because of the round-trip arithmetic. Over a unix
socket a second request costs about 0.2 ms, so separate GETs are free locally
Expand Down
6 changes: 3 additions & 3 deletions pkgs/mcpx/docs/home-example/client.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ export async function readResource(server , uri )
* a tool result.
*
* This behaves identically wherever the script runs, and that is the whole
* point of it. Locally the daemon hardlinks the file it was pointed at.
* point of it. Locally the daemon reads and copies the file into its store.
* Remotely the bytes travel once, to the daemon, and the caller fetches them
* only if it wants them. The script does not ask which case it is in, because
* a script that has to ask is a script with two code paths and one of them is
Expand All @@ -1146,8 +1146,8 @@ export async function artifact(
if (declared) headers["content-type"] = declared;
let body ;
if (norm.path !== undefined && env("MCPX_ARTIFACTS_LOCAL") === "1") {
// The daemon can see this file. Naming it lets the store hardlink rather
// than read, hash into memory and write again.
// The daemon can see this file. Naming it avoids uploading the body over
// the client transport; the store still reads, hashes, and copies it.
headers["x-mcpx-path"] = norm.path;
} else if (norm.path !== undefined) {
body = await readFileBytes(norm.path) ;
Expand Down
6 changes: 3 additions & 3 deletions pkgs/mcpx/docs/home-example/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ export interface ArtifactOptions {
* a tool result.
*
* This behaves identically wherever the script runs, and that is the whole
* point of it. Locally the daemon hardlinks the file it was pointed at.
* point of it. Locally the daemon reads and copies the file into its store.
* Remotely the bytes travel once, to the daemon, and the caller fetches them
* only if it wants them. The script does not ask which case it is in, because
* a script that has to ask is a script with two code paths and one of them is
Expand All @@ -1144,8 +1144,8 @@ export async function artifact(
if (declared) headers["content-type"] = declared;
let body: BodyInit | undefined;
if (norm.path !== undefined && env("MCPX_ARTIFACTS_LOCAL") === "1") {
// The daemon can see this file. Naming it lets the store hardlink rather
// than read, hash into memory and write again.
// The daemon can see this file. Naming it avoids uploading the body over
// the client transport; the store still reads, hashes, and copies it.
headers["x-mcpx-path"] = norm.path;
} else if (norm.path !== undefined) {
body = await readFileBytes(norm.path) as unknown as BodyInit;
Expand Down
6 changes: 3 additions & 3 deletions pkgs/mcpx/docs/remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ answering — again with no port and no credential beyond ssh.
unauthenticated**; who may reach it is who may reach the host and the
socket. `ssh://` inherits exactly the ssh access you already grant.
- It is not a shared filesystem. Scripts and artifacts sent to a remote
daemon travel as bytes; a `{path}` artifact is only hardlinked when the
caller proved it shares the daemon's disk, which an ssh tunnel never
proves. See [`exec.md`](./exec.md#the-store).
daemon travel as bytes; a caller's `{path}` is uploaded, not made visible to
the daemon, because an ssh tunnel never proves shared-disk access. See
[`exec.md`](./exec.md#the-store).
- It is never started from here. Local autostart does not apply, by design:
answering from a local daemon when the remote one is unreachable would be
worse than failing.
5 changes: 3 additions & 2 deletions pkgs/mcpx/internal/api/ops_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ func execOps() []Op {
Summary: "Store a file a script produced",
Description: "What artifact() in a generated client calls. The body is the " +
"bytes; a caller on the daemon's own filesystem may instead send " +
"X-Mcpx-Path and have the file hardlinked, which is why handing back " +
"a large local file costs an inode rather than a copy. Content is " +
"X-Mcpx-Path so the daemon can read and copy the file into the store. " +
"This avoids uploading the body over the client transport, but the " +
"file is still read, hashed, and written. Content is " +
"addressed by sha256 and served by a separate random id, because a " +
"content hash is guessable by anyone who can guess the content.",
Admin: true, Mutating: true,
Expand Down
11 changes: 4 additions & 7 deletions pkgs/mcpx/internal/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,10 @@ func (s *Store) Put(r io.Reader, p PutOptions) (Meta, error) {

// PutFile registers a file that is already on this machine's disk.
//
// A hardlink where the filesystem allows one, a copy where it does not. This
// is the local case the issue singles out: a screenshot already written to
// disk should not be read, hashed into memory and written again just to be
// handed to a caller who can see the same disk.
//
// It is still hashed, because the store is content addressed and because a
// caller comparing hashes is how a transfer gets skipped.
// The file is read, hashed, and copied into the content-addressed store even
// when it is already on disk the caller can see. The blob gets its own inode
// (#1660), so in-place edits to the source cannot rewrite the stored bytes.
// The hash also lets a caller skip a later transfer when it already has them.
func (s *Store) PutFile(path string, p PutOptions) (Meta, error) {
f, err := os.Open(path)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions pkgs/mcpx/internal/artifacts/delivery_claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package artifacts_test

import (
"os"
"path/filepath"
"strings"
"testing"
)

// TestDeliveryClaimsMatchCopyImplementation keeps user-facing and generated
// artifact text aligned with the store. If artifact delivery gains an actual
// os.Link path, the wording can be reviewed alongside that implementation.
func TestDeliveryClaimsMatchCopyImplementation(t *testing.T) {
implementation, err := os.ReadFile("artifacts.go")
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(implementation), "os.Link(") || strings.Contains(string(implementation), "os.Linkname") {
return
}

files := []string{
"internal/artifacts/artifacts.go",
"internal/api/ops_exec.go",
"internal/settings/exec.go",
"internal/execsvc/execsvc.go",
"internal/daemon/routes_exec.go",
"internal/cli/exec.go",
"internal/cli/commands.go",
"internal/codegen/emit.go",
"internal/e2e/exec_test.go",
"docs/exec.md",
"docs/remote.md",
"docs/home-example/client.ts",
"docs/home-example/client.browser.js",
}
for _, name := range files {
path := filepath.Join("..", "..", filepath.FromSlash(name))
contents, err := os.ReadFile(path)
if err != nil {
t.Errorf("read %s: %v", name, err)
continue
}
text := strings.ToLower(string(contents))
text = strings.ReplaceAll(text, "hard-linked", "hardlinked")
text = strings.ReplaceAll(text, "hard-link", "hardlink")
text = strings.ReplaceAll(text, "hard link", "hardlink")
if strings.Contains(text, "hardlink") || strings.Contains(text, "costs an inode") || strings.Contains(text, "inode rather than a copy") {
t.Errorf("%s still promises hardlink delivery without an os.Link path in artifacts", name)
}
}
}
4 changes: 2 additions & 2 deletions pkgs/mcpx/internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,8 @@ func (a *App) runScript(ctx context.Context, args []string, inline bool) error {

// The artifact store is the daemon's, opened from this side. When the
// daemon is on this machine that is the same SQLite index and the same
// blobs, so a file the script registered can be hardlinked into an
// output directory rather than fetched back over a socket.
// blobs, so a file the script registered can be copied from the store into
// an output directory without being fetched back over a socket.
var store *artifacts.Store
if st, serr := a.localStore(); serr == nil {
store = st
Expand Down
8 changes: 3 additions & 5 deletions pkgs/mcpx/internal/cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,9 @@ func (e execOptions) artifactOptions(cwd string) *execsvc.ArtifactOptions {

// localStore opens the daemon's artifact store from this side.
//
// The same directory, not a copy. When the daemon is on this machine the
// store is a shared SQLite index in WAL mode and a tree of blobs, so the CLI
// reading it sees exactly what the daemon wrote -- and can hardlink a blob
// into an output directory rather than fetching its own copy of bytes that
// are already here.
// When the daemon is on this machine the store is a shared SQLite index in
// WAL mode and a tree of blobs. The CLI can read those blobs directly and
// copy them into an output directory without fetching them back over a socket.
func (a *App) localStore() (*artifacts.Store, error) {
set := a.Settings()
if !set.Bool("artifacts.enabled") {
Expand Down
6 changes: 3 additions & 3 deletions pkgs/mcpx/internal/codegen/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ export interface ArtifactOptions {
* a tool result.
*
* This behaves identically wherever the script runs, and that is the whole
* point of it. Locally the daemon hardlinks the file it was pointed at.
* point of it. Locally the daemon reads and copies the file into its store.
* Remotely the bytes travel once, to the daemon, and the caller fetches them
* only if it wants them. The script does not ask which case it is in, because
* a script that has to ask is a script with two code paths and one of them is
Expand All @@ -1542,8 +1542,8 @@ export async function artifact(
if (declared) headers["content-type"] = declared;
let body: BodyInit | undefined;
if (norm.path !== undefined && env("MCPX_ARTIFACTS_LOCAL") === "1") {
// The daemon can see this file. Naming it lets the store hardlink rather
// than read, hash into memory and write again.
// The daemon can see this file. Naming it avoids uploading the body over
// the client transport; the store still reads, hashes, and copies it.
headers["x-mcpx-path"] = norm.path;
} else if (norm.path !== undefined) {
body = await readFileBytes(norm.path) as unknown as BodyInit;
Expand Down
8 changes: 4 additions & 4 deletions pkgs/mcpx/internal/daemon/routes_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@ func (s *Server) execStream(w http.ResponseWriter, r *http.Request, svc *execsvc
// handleArtifactPut is how artifact() in a script registers a file.
//
// The body is the bytes, or -- when the script is on this machine and said so
// with X-Mcpx-Path -- nothing, and the named file is hardlinked instead.
// Reading, hashing into memory and writing again to hand somebody a file they
// can already see would be the wrong shape for the local case, which is most
// cases.
// with X-Mcpx-Path -- the daemon reads and copies the named file into the
// content-addressed store. The path hint avoids uploading the body over the
// client transport, but the source is still read and hashed so the stored blob
// is independent of the file outside the store (#1660).
func (s *Server) handleArtifactPut(w http.ResponseWriter, r *http.Request) {
store, err := s.Artifacts()
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkgs/mcpx/internal/e2e/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func names(entries []os.DirEntry) []string {
return out
}

// The local case: the daemon shares this filesystem, so handing a file back
// should cost an inode rather than a copy.
func TestALocalFileIsLinkedIntoTheStoreNotCopied(t *testing.T) {
// The local path avoids uploading the body over the client transport, but the
// store still copies it so an outside file never shares the blob's inode.
func TestALocalFileIsCopiedIntoTheStore(t *testing.T) {
e := newEnv(t, oneServer)
src := filepath.Join(e.dir, "source.bin")
if err := os.WriteFile(src, []byte("linked bytes"), 0o600); err != nil {
Expand All @@ -128,8 +128,8 @@ func TestALocalFileIsLinkedIntoTheStoreNotCopied(t *testing.T) {
if err := syscall.Stat(src, &st); err != nil {
t.Fatal(err)
}
if st.Nlink < 2 {
t.Error("the store should have linked the file, not read and rewritten it")
if st.Nlink != 1 {
t.Errorf("the source should not be linked into the store; link count = %d", st.Nlink)
}
}

Expand Down
11 changes: 6 additions & 5 deletions pkgs/mcpx/internal/execsvc/execsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ type ArtifactOptions struct {
// MaxBytes is the per-artifact ceiling the caller accepts for inline or
// streamed delivery. Anything larger stays a reference.
MaxBytes int64 `json:"maxBytes,omitempty"`
// SharedFs is a directory the caller can read. Artifacts are placed
// there -- hardlinked, not copied -- instead of being transferred.
// SharedFs is a directory the caller can read. Artifacts are copied there
// from the store so the caller can read them directly without a later
// transfer over the MCP connection.
SharedFs string `json:"sharedFs,omitempty"`
// Dir is where the CLI writes artifacts for the person who ran it. It is
// SharedFs by another name for a local caller, kept separate because one
Expand Down Expand Up @@ -1098,9 +1099,9 @@ func (s *Service) deliver(res *Result, opts Options) error {
for i := range res.Artifacts {
a := &res.Artifacts[i]
if dir != "" {
// A shared filesystem beats every transfer mode: the caller can
// already read the bytes, so hardlinking them into its directory
// costs an inode and no I/O.
// A shared filesystem lets the caller read the destination directly.
// Export still copies bytes from the store into that directory; this
// avoids a later transfer, not filesystem I/O.
p, err := s.Store.Export(a.Meta, dir)
if err != nil {
errs = append(errs, fmt.Sprintf("%s: %v", a.Name, err))
Expand Down
4 changes: 2 additions & 2 deletions pkgs/mcpx/internal/settings/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func artifactSettings() []Setting {
Name: "Artifacts directory",
Short: "write this run's artifacts here",
Long: "A directory the caller can read. When it is on the same machine " +
"as the daemon the files are hardlinked into it rather than copied, " +
"so handing back a gigabyte costs an inode. Names are sanitised and " +
"as the daemon the files are copied from the store into it; this still " +
"requires file I/O. Names are sanitised and " +
"collisions get a numeric suffix; nothing is ever overwritten.",
},
{
Expand Down
Loading