diff --git a/pkgs/mcpx/TODO.md b/pkgs/mcpx/TODO.md index dc91171e03..8c35ad660b 100644 --- a/pkgs/mcpx/TODO.md +++ b/pkgs/mcpx/TODO.md @@ -3,4 +3,3 @@ - Replace the takeover handoff with a supervisor that owns each stdio child's pipes for the child's whole life. The current handoff is a stopgap: `mcpx upgrade` (run from the new binary) and `mcpx daemon --takeover` have the old daemon start the successor as its own child, so it stays in the service's cgroup, and pass its children's pipes to it over a unix socket. Every handoff depends on the framing, the ack sequence and the restore path working. A supervisor that outlives the daemon would make a daemon upgrade a plain restart of the front end. - Streamable HTTP upstream sessions survive a takeover; SSE upstream sessions do not, because their standalone GET stream cannot be moved between processes. Those are closed and re-initialised on the successor's first call. - A cold restart does not resume upstream sessions. The lease table is restored from `sessions.json`, but the upstream `Mcp-Session-Id` is kept only in memory and in the takeover handoff, so after a restart each HTTP upstream is initialised again. -- A stdio server's identity is the bytes of its executable (`ExecIdentity`, `internal/config/execid.go`). That is wrong for a program that finds its own files relative to its real location: a node script beside its `node_modules`, re-pointed at a byte-identical copy in another directory, is kept by the automatic check and keeps running with the old directory's dependencies. Only a restart (`mcpx restart `) moves it. Above `limits.execHashMax` the identity is the inode, so such a copy is restarted. Decide whether the identity should include the program's directory for scripts, or whether a re-pointed link should always restart; the test `TestReloadFollowsAForRePointedCommandByWhatItRuns` pins the current choice. diff --git a/pkgs/mcpx/internal/daemon/exec_watch_test.go b/pkgs/mcpx/internal/daemon/exec_watch_test.go index 04be36069f..c4eb206cc3 100644 --- a/pkgs/mcpx/internal/daemon/exec_watch_test.go +++ b/pkgs/mcpx/internal/daemon/exec_watch_test.go @@ -278,6 +278,72 @@ func TestExecWatchFollowsARepointedLinkByWhatItRuns(t *testing.T) { waitGone(t, pid) } +// The cross-directory counterpart of the re-pointed-link test above +// (#1531, Phase 2 proof): a live pool whose link is re-pointed at a +// byte-identical script in a different directory restarts within the watch +// ticks, because a script's identity includes its resolved directory. The +// restart is logged exactly once, and the neighbour pool is untouched. +func TestExecWatchRestartsAScriptRePointedAtAnIdenticalCopyInAnotherDirectory(t *testing.T) { + setExecWatch(t, execTick) + fake := testsupport.FakeMCPBinary(t) + state := t.TempDir() + dirA := t.TempDir() + dirB := t.TempDir() + scriptA := filepath.Join(dirA, "mcp-server") + scriptB := filepath.Join(dirB, "mcp-server") + writeWrapper(t, fake, scriptA, "same program") + writeWrapper(t, fake, scriptB, "same program") + if a, b := mustRead(t, scriptA), mustRead(t, scriptB); !bytes.Equal(a, b) { + t.Fatal("the cross-directory copy is not byte-identical to the original script") + } + link := filepath.Join(state, "mcp-server") + if err := os.Symlink(scriptA, link); err != nil { + t.Fatal(err) + } + log := recordLifecycle(t) + + cfg := &config.Config{MCPServers: map[string]*config.Server{ + "a": {Name: "a", Command: fake}, + "b": {Name: "b", Command: link}, + }} + r, err := NewRegistry(cfg, Paths{State: state, Cache: state}, nil) + if err != nil { + t.Fatal(err) + } + defer r.Close() + pa, _ := r.Pool("a") + pb, _ := r.Pool("b") + pidA, pidB := childPID(t, pa), childPID(t, pb) + + if err := os.Remove(link); err != nil { + t.Fatal(err) + } + if err := os.Symlink(scriptB, link); err != nil { + t.Fatal(err) + } + var pidB2 int + waitFor(t, 2*time.Second, "a script re-pointed at a byte-identical copy in another directory kept its child", func() bool { + pidB2 = livePID(pb) + return pidB2 != 0 && pidB2 != pidB + }) + waitGone(t, pidB) + + // Let several more checks run: the restart must stay a single event. + time.Sleep(10 * execTick) + if n := log.count("server.restart", "b", map[string]string{"reason": "executable changed"}); n != 1 { + t.Fatalf("pool b logged %d restarts for executable changed, want exactly 1", n) + } + if n := log.count("server.restart", "a", nil); n != 0 { + t.Fatalf("the neighbour pool logged %d restarts", n) + } + if got := livePID(pa); got != pidA { + t.Fatalf("the neighbour pool was restarted: pid %d -> %d", pidA, got) + } + if got := servedPID(t, pb); got != pidB2 { + t.Fatalf("the call was served by pid %d, not the restarted child %d", got, pidB2) + } +} + // A child whose executable is deleted keeps serving, with one warning however // many checks see the gap. When the file returns, the child is kept if the // bytes are the same and replaced if they are not. diff --git a/pkgs/mcpx/internal/daemon/reload_identity_test.go b/pkgs/mcpx/internal/daemon/reload_identity_test.go index 839966e1fd..90bb795ec3 100644 --- a/pkgs/mcpx/internal/daemon/reload_identity_test.go +++ b/pkgs/mcpx/internal/daemon/reload_identity_test.go @@ -151,13 +151,14 @@ func TestReloadRestartsAPoolWhoseBinaryChangedAtTheSamePath(t *testing.T) { // file, not by the path it was reached by. A link re-pointed at a file with // different bytes is a different program and gets a new pool. // -// Caveat: identity is the bytes of the file and nothing else. A program that -// finds its own files relative to its real location, such as a node script -// beside its node_modules, is kept when the link points at an identical copy -// in another directory, and its child keeps the dependencies of the old -// location. An operator who wants the new location in use restarts the server -// (mcpx restart). Above limits.execHashMax the identity is the inode instead, -// so an oversized copy is restarted; see +// The keep above is same-directory only: a script (a hashed file beginning +// with #!) is identified by its content plus the resolved directory of the +// real file, because a script finds its own files relative to its real +// location. A link re-pointed at a byte-identical script in a DIFFERENT +// directory is a different program and replaces the pool; see +// TestReloadRestartsAScriptRePointedAtAnIdenticalCopyInAnotherDirectory. +// Above limits.execHashMax the identity is the inode instead, so an +// oversized copy is restarted either way; see // TestReloadRestartsAnOversizedProgramRePointedAtACopy. func TestReloadFollowsAForRePointedCommandByWhatItRuns(t *testing.T) { fake := testsupport.FakeMCPBinary(t) @@ -214,6 +215,64 @@ func TestReloadFollowsAForRePointedCommandByWhatItRuns(t *testing.T) { waitGone(t, pid) } +// The cross-directory counterpart of the re-pointed-command test above +// (#1531, Phase 2 proof): the two scripts are byte-identical and both are +// scripts, so the only identity difference is the resolved directory the +// script rule folds in. A link re-pointed at the copy in the other directory +// must replace the pool: new pool, new child, old child gone. +func TestReloadRestartsAScriptRePointedAtAnIdenticalCopyInAnotherDirectory(t *testing.T) { + fake := testsupport.FakeMCPBinary(t) + state := t.TempDir() + dirA := t.TempDir() + dirB := t.TempDir() + scriptA := filepath.Join(dirA, "mcp-server") + scriptB := filepath.Join(dirB, "mcp-server") + writeWrapper(t, fake, scriptA, "same program") + writeWrapper(t, fake, scriptB, "same program") + if a, b := mustRead(t, scriptA), mustRead(t, scriptB); !bytes.Equal(a, b) { + t.Fatal("the cross-directory copy is not byte-identical to the original script") + } + link := filepath.Join(state, "mcp-server") + if err := os.Symlink(scriptA, link); err != nil { + t.Fatal(err) + } + + cfg := func() *config.Config { + return &config.Config{MCPServers: map[string]*config.Server{ + "b": {Name: "b", Command: link}, + }} + } + r, err := NewRegistry(cfg(), Paths{State: state, Cache: state}, nil) + if err != nil { + t.Fatal(err) + } + defer r.Close() + pb, _ := r.Pool("b") + pid := childPID(t, pb) + identityBefore := pb.ExecIdentity() + + if err := os.Remove(link); err != nil { + t.Fatal(err) + } + if err := os.Symlink(scriptB, link); err != nil { + t.Fatal(err) + } + if _, _, err := r.Reload(cfg()); err != nil { + t.Fatal(err) + } + pb2, _ := r.Pool("b") + if pb2 == pb { + t.Fatal("a script re-pointed at a byte-identical copy in another directory kept its pool") + } + if pb2.ExecIdentity() == identityBefore { + t.Fatal("the new pool records the old directory's identity") + } + if got := childPID(t, pb2); got == pid { + t.Fatalf("the cross-directory copy's child pid %d was kept", pid) + } + waitGone(t, pid) +} + // The inverse boundary. A full-size program is identified by its inode, so a // link re-pointed at a byte-identical copy of it is a different program and // the pool is replaced. This is the behaviour the re-pointed-command test diff --git a/planning/roadmaps/mcpx.md b/planning/roadmaps/mcpx.md index 8300557b83..26bb979834 100644 --- a/planning/roadmaps/mcpx.md +++ b/planning/roadmaps/mcpx.md @@ -14,15 +14,15 @@ This repo is the only source for `mcpx`. The macOS flake in `dezren39/nix` delet ## Next -| Title | Status | Intent | Links | -| ----------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | -| A supervisor that owns each stdio child's pipes | idea | Today's takeover handoff is a stopgap that passes pipes over a unix socket. A supervisor that outlives the daemon makes a daemon upgrade a plain restart of the front end. | #1384, [TODO](../../pkgs/mcpx/TODO.md) | -| Resume upstream sessions after a cold restart | idea | The upstream `Mcp-Session-Id` is kept only in memory and in the takeover handoff, so every HTTP upstream is initialised again after a restart. | #1414, [TODO](../../pkgs/mcpx/TODO.md) | -| Decide what a stdio server's identity is | proposed | `ExecIdentity` is the bytes of the executable, which is wrong for a script that finds files relative to its location. Include the directory, or always restart a re-pointed link. | [TODO](../../pkgs/mcpx/TODO.md) | -| JSON Schema validation of tool input and output | proposed | Seven conformance rows depend on it. | nix#254 | -| Legacy client robustness | proposed | Session 404, GET stream, SSE retry, string ids, stream timeout and the version header. | nix#203, nix#200, nix#201 | -| Tasks, which are advertised and not implemented | proposed | The 2026 tasks extension is advertised, and core tasks live in two stores. | nix#209 | -| Script runtime follow-ups | proposed | Capability probe and clearer parse-failure diagnostics for `--runtime node`, and recipes with `@param` holes. | nix#108, nix#367 | +| Title | Status | Intent | Links | +| ----------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | +| A supervisor that owns each stdio child's pipes | idea | Today's takeover handoff is a stopgap that passes pipes over a unix socket. A supervisor that outlives the daemon makes a daemon upgrade a plain restart of the front end. | #1384, [TODO](../../pkgs/mcpx/TODO.md) | +| Resume upstream sessions after a cold restart | idea | The upstream `Mcp-Session-Id` is kept only in memory and in the takeover handoff, so every HTTP upstream is initialised again after a restart. | #1414, [TODO](../../pkgs/mcpx/TODO.md) | +| Decide what a stdio server's identity is | decided | Decided in #1531, built in #1532 (PR #1571): a hashed script is identified by content plus its resolved directory, so a byte-identical script re-pointed into another directory restarts while a same-directory re-point keeps its child; non-scripts stay bytes-only. Proven end to end by #1533. | #1531, #1532 | +| JSON Schema validation of tool input and output | proposed | Seven conformance rows depend on it. | nix#254 | +| Legacy client robustness | proposed | Session 404, GET stream, SSE retry, string ids, stream timeout and the version header. | nix#203, nix#200, nix#201 | +| Tasks, which are advertised and not implemented | proposed | The 2026 tasks extension is advertised, and core tasks live in two stores. | nix#209 | +| Script runtime follow-ups | proposed | Capability probe and clearer parse-failure diagnostics for `--runtime node`, and recipes with `@param` holes. | nix#108, nix#367 | ## Later