Repository navigation
feat(mcpx): enforce cacheScope at the disk boundary #1705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package daemon | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dezren39/mcpx/internal/config" | ||
| "github.com/dezren39/mcpx/internal/mcpclient" | ||
| ) | ||
|
|
||
| // #1603: cacheScope at the disk boundary. SaveCache never persists a | ||
| // private catalogue and writes a public one with its absolute deadline | ||
| // and scope; loadCache never seeds a private entry, even from a file | ||
| // this daemon did not write, and seeds a public entry's deadline so a | ||
| // past-deadline seed starts expired. | ||
|
|
||
| func cacheScopeRegistry(t *testing.T, dir string) (*Registry, *config.Config) { | ||
| t.Helper() | ||
| cfg := &config.Config{MCPServers: map[string]*config.Server{ | ||
| "pub": {Name: "pub", Command: filepath.Join(dir, "never-run")}, | ||
| "priv": {Name: "priv", Command: filepath.Join(dir, "never-run-either")}, | ||
| }} | ||
| r, err := NewRegistry(cfg, Paths{State: dir, Cache: dir}, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return r, cfg | ||
| } | ||
|
|
||
| func TestSaveCacheSkipsPrivatePools(t *testing.T) { | ||
| dir := t.TempDir() | ||
| r, _ := cacheScopeRegistry(t, dir) | ||
| fetched := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC) | ||
| deadline := fetched.Add(2 * time.Hour) | ||
|
|
||
| pub, _ := r.Pool("pub") | ||
| pub.SetSchemas([]mcpclient.Tool{{Name: "pub-tool"}}, nil, nil, "", fetched, deadline, false) | ||
| priv, _ := r.Pool("priv") | ||
| priv.SetSchemas([]mcpclient.Tool{{Name: "priv-tool"}}, nil, nil, "", fetched, deadline, true) | ||
|
|
||
| if err := r.SaveCache(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| b, err := os.ReadFile(r.paths.SchemaCachePath(r.hash)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var cf cacheFile | ||
| if err := json.Unmarshal(b, &cf); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if cf.Version != cacheVersion { | ||
| t.Fatalf("cache file version = %d, want %d", cf.Version, cacheVersion) | ||
| } | ||
| if _, ok := cf.Servers["priv"]; ok { | ||
| t.Fatal("a private catalogue was persisted to the schema cache") | ||
| } | ||
| entry, ok := cf.Servers["pub"] | ||
| if !ok { | ||
| t.Fatal("the public catalogue is missing from the schema cache") | ||
| } | ||
| if entry.Scope != "public" { | ||
| t.Fatalf("public entry scope = %q, want %q", entry.Scope, "public") | ||
| } | ||
| if !entry.Deadline.Equal(deadline) { | ||
| t.Fatalf("public entry deadline = %v, want the absolute %v", entry.Deadline, deadline) | ||
| } | ||
| if len(entry.Tools) != 1 || entry.Tools[0].Name != "pub-tool" { | ||
| t.Fatalf("public entry tools = %v, want the seeded catalogue", entry.Tools) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadCacheNeverSeedsPrivateEntry(t *testing.T) { | ||
| dir := t.TempDir() | ||
| r, cfg := cacheScopeRegistry(t, dir) | ||
| fetched := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC) | ||
| past := fetched.Add(-time.Hour) | ||
|
|
||
| // A file this daemon's SaveCache would never write: a private | ||
| // entry for pub, and a public entry for priv whose deadline has | ||
| // already passed. | ||
| cf := cacheFile{ | ||
| Version: cacheVersion, ConfigHash: r.hash, SavedAt: fetched, | ||
| Servers: map[string]*cachedEntry{ | ||
| "pub": { | ||
| Tools: []mcpclient.Tool{{Name: "sneaky-tool"}}, | ||
| FetchedAt: fetched, Deadline: fetched.Add(2 * time.Hour), | ||
| Scope: "private", | ||
| }, | ||
| "priv": { | ||
| Tools: []mcpclient.Tool{{Name: "seeded-tool"}}, | ||
| FetchedAt: fetched, Deadline: past, | ||
| Scope: "public", | ||
| }, | ||
| }, | ||
| } | ||
| b, err := json.Marshal(cf) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(r.paths.SchemaCachePath(r.hash), b, 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| next, err := NewRegistry(cfg, Paths{State: dir, Cache: dir}, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| pub, _ := next.Pool("pub") | ||
| if _, _, _, at, _, _ := pub.CachedAll(); !at.IsZero() { | ||
| t.Fatal("a private cache entry was seeded from disk") | ||
| } | ||
| privPool, _ := next.Pool("priv") | ||
| tools, _, _, _, deadline, private := privPool.CachedAll() | ||
| if len(tools) != 1 || tools[0].Name != "seeded-tool" { | ||
| t.Fatalf("public entry seeded tools = %v, want the file's catalogue", tools) | ||
| } | ||
| if private { | ||
| t.Fatal("a public entry was seeded as private") | ||
| } | ||
| if !deadline.Equal(past) { | ||
| t.Fatalf("seeded deadline = %v, want the file's past deadline %v (the seed starts expired)", deadline, past) | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package pool_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dezren39/mcpx/internal/config" | ||
| "github.com/dezren39/mcpx/internal/mcpclient" | ||
| "github.com/dezren39/mcpx/internal/pool" | ||
| ) | ||
|
|
||
| // #1603: the disk boundary for cacheScope. A seed carries the absolute | ||
| // deadline and scope it was saved with; a past deadline starts expired, | ||
| // and a failed re-read of an expired cache serves stale instead of | ||
| // erroring — while a failed first fetch keeps erroring. | ||
|
|
||
| func badPool(t *testing.T) *pool.Pool { | ||
| t.Helper() | ||
| cfg := &config.Config{MCPServers: map[string]*config.Server{ | ||
| "bad": {Name: "bad", Command: filepath.Join(t.TempDir(), "never-run")}, | ||
| }} | ||
| r, err := cfg.Resolve("bad") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| p := pool.New(r) | ||
| t.Cleanup(func() { p.Close() }) | ||
| return p | ||
| } | ||
|
|
||
| func TestPoolCacheSeededPastDeadlineRefetches(t *testing.T) { | ||
| p, s := ttlPool(t, "modern", map[string]string{"ERAMCP_TTL_MS": "60000"}) | ||
| now := time.Now() | ||
| p.SetSchemas( | ||
| []mcpclient.Tool{{Name: "seeded-marker"}}, nil, nil, "", | ||
| now, now.Add(-time.Minute), false, | ||
| ) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
| defer cancel() | ||
| tools, _, err := p.Schemas(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if n := listCalls(t, s); n != 1 { | ||
| t.Fatalf("seeded past its deadline: tools/list called %d times, want 1 (seed starts expired)", n) | ||
| } | ||
| for _, tool := range tools { | ||
| if tool.Name == "seeded-marker" { | ||
| t.Fatal("the expired seed was served as fresh instead of being re-read") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestPoolCacheSeededPrivateScopeStored(t *testing.T) { | ||
| p, _ := ttlPool(t, "modern", nil) | ||
| now := time.Now() | ||
| p.SetSchemas(nil, nil, nil, "", now, now.Add(time.Hour), true) | ||
| if !p.SchemaPrivate() { | ||
| t.Fatal("a seed's private scope was not carried onto the pool cache") | ||
| } | ||
| _, _, _, _, deadline, private := p.CachedAll() | ||
| if !private || !deadline.Equal(now.Add(time.Hour)) { | ||
| t.Fatalf("CachedAll = deadline %v, private %v; want the seed's deadline and scope", deadline, private) | ||
| } | ||
| } | ||
|
|
||
| func TestPoolCacheStaleServedOnRefetchError(t *testing.T) { | ||
| p := badPool(t) | ||
| at := time.Now().Add(-2 * time.Hour) | ||
| p.SetSchemas( | ||
| []mcpclient.Tool{{Name: "stale-tool"}}, nil, nil, "", | ||
| at, time.Now().Add(-time.Hour), false, | ||
| ) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
| defer cancel() | ||
| for i := 0; i < 2; i++ { | ||
| tools, _, err := p.Schemas(ctx) | ||
| if err != nil { | ||
| t.Fatalf("call %d: expired cache + failing upstream must serve stale, got error %v", i, err) | ||
| } | ||
| if len(tools) != 1 || tools[0].Name != "stale-tool" { | ||
| t.Fatalf("call %d: served %v, want the stale catalogue", i, tools) | ||
| } | ||
| } | ||
| if p.Status().LastError == "" { | ||
| t.Fatal("the failed refresh was not recorded in the pool status") | ||
| } | ||
| _, _, gotAt := p.CachedSchemas() | ||
| if !gotAt.Equal(at) { | ||
| t.Fatalf("cache timestamp moved to %v; a stale serve must leave it at %v so the next read retries", gotAt, at) | ||
| } | ||
| } | ||
|
|
||
| func TestPoolCacheFirstFetchFailureStillErrors(t *testing.T) { | ||
| p := badPool(t) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
| defer cancel() | ||
| if _, _, err := p.Schemas(ctx); err == nil { | ||
| t.Fatal("a failed first fetch must return its error") | ||
| } | ||
| _, _, at := p.CachedSchemas() | ||
| if !at.IsZero() { | ||
| t.Fatalf("a failed first fetch recorded timestamp %v; it must stay unset", at) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a server previously persisted a public catalogue and a later refresh returns a private catalogue, skipping this entry can leave
cf.Serversempty. The existing empty-map guard then returns without replacing or removing the previous cache file. On restart,loadCacherestores that superseded public catalogue and, if its original deadline is still in the future, serves it as fresh instead of fetching the current catalogue. Persist an empty cache or remove the existing file when no entries remain eligible.Useful? React with 👍 / 👎.