fix(container): persist npm cache for per-task MCP servers

Ephemeral task containers re-resolve and re-download npx-launched MCP
server packages from the npm registry on every task, since each task
gets a fresh container with no persistent cache -- a worse amplifier
of a registry outage than the daemon-restart case d4bb914b already
fixed.

Extract the daemon's npm-cache env logic into mcp.NpmCacheEnv, shared
by both the daemon's own long-lived npx MCP servers and archie-agent's
per-task ones. Mount a new archie-cache-mcp-npm Docker volume at
/data/cache/mcp-npm on every task container regardless of ecosystem
(MCP servers are daemon-wide config, not tied to the task repo's
language), and point npx-launched per-task stdio transports at it.

Closes archie-core-qk8m.
This commit is contained in:
sam committed 2026-08-22 21:13:33 +10:00
1 parent 943da54bdd
commit 99738845b6
7 files changed
+246 -37

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
package mcp
import "path/filepath"
// NpmCacheEnv returns environment variables that make an npx-launched MCP
// server reuse a persistent package cache at cacheDir, instead of npm
// re-resolving and re-downloading the package from the registry on every
// launch. Empty for any command other than npx, or an empty cacheDir --
// npx has a registry dependency other commands don't, and an empty cacheDir
// means the caller has no persistent location to point npm at.
//
// This covers the npm package itself, not a package's own separate
// download path -- e.g. Playwright/Puppeteer-based servers fetch browser
// binaries from a CDN via a postinstall script, gated by their own env
// vars, which this does not touch.
//
// Matched by base name, not exact string: a command may be pinned via an
// absolute path (an operator's daemon config, or a container image's PATH),
// and an exact "npx" match would skip the fix silently, with no error
// logged, leaving the underlying registry re-fetch in place unnoticed.
func NpmCacheEnv(command, cacheDir string) []string {
if filepath.Base(command) != "npx" || cacheDir == "" {
return nil
}
return []string{
"NPM_CONFIG_CACHE=" + cacheDir,
"NPM_CONFIG_PREFER_OFFLINE=true",
}
}
+60
View File
@@ -0,0 +1,60 @@
package mcp
import (
"slices"
"testing"
)
func TestNpmCacheEnv(t *testing.T) {
cases := []struct {
name string
command string
cacheDir string
want []string
}{
{
name: "npx gets a persistent cache",
command: "npx",
cacheDir: "/data/cache/mcp-npm",
want: []string{
"NPM_CONFIG_CACHE=/data/cache/mcp-npm",
"NPM_CONFIG_PREFER_OFFLINE=true",
},
},
{
name: "an absolute path to npx still gets the persistent cache",
command: "/usr/local/bin/npx",
cacheDir: "/data/cache/mcp-npm",
want: []string{
"NPM_CONFIG_CACHE=/data/cache/mcp-npm",
"NPM_CONFIG_PREFER_OFFLINE=true",
},
},
{
name: "a non-npx command has no npm registry dependency to fix",
command: "/usr/local/bin/my-mcp-server",
cacheDir: "/data/cache/mcp-npm",
want: nil,
},
{
name: "empty command is left alone",
command: "",
cacheDir: "/data/cache/mcp-npm",
want: nil,
},
{
name: "empty cacheDir is refused rather than caching into the process CWD",
command: "npx",
cacheDir: "",
want: nil,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := NpmCacheEnv(c.command, c.cacheDir)
if !slices.Equal(got, c.want) {
t.Errorf("NpmCacheEnv(%q, %q) = %v, want %v", c.command, c.cacheDir, got, c.want)
}
})
}
}