diff --git a/pkgs/mcpx/internal/cli/serve.go b/pkgs/mcpx/internal/cli/serve.go index 05a5bde8ad..c4cd49d7e2 100644 --- a/pkgs/mcpx/internal/cli/serve.go +++ b/pkgs/mcpx/internal/cli/serve.go @@ -522,6 +522,13 @@ func (a *App) MCPServer(ctx context.Context) (*mcpserver.Server, error) { // below reads -- so neither surface's cadence changes. if a.daemonStore != nil { srv.TaskStore = a.daemonStore + // Retention follows the same settings the daemon's taskStore() + // reads on /v1 access (#6247): without this, a tasks.ttl / + // tasks.maxAge change made through /v1/settings never reached + // the MCP surface's cached store pointer. + srv.TaskRetention = func() (time.Duration, time.Duration) { + return a.Settings().Duration("tasks.ttl"), a.Settings().Duration("tasks.maxAge") + } } else { // A standalone server (mcpx serve) keeps a private store; // wire its audit seam to the durable log or its task diff --git a/pkgs/mcpx/internal/mcpserver/server.go b/pkgs/mcpx/internal/mcpserver/server.go index 22aae109b9..b01b9a5f89 100644 --- a/pkgs/mcpx/internal/mcpserver/server.go +++ b/pkgs/mcpx/internal/mcpserver/server.go @@ -303,6 +303,16 @@ type Server struct { // means the standalone behaviour: a private in-memory store. TaskStore *taskStore + // TaskRetention, when set, supplies the current tasks.ttl / + // tasks.maxAge retention settings and is consulted on every + // task-store access, so a /v1/settings change reaches MCP task + // traffic without an intervening /v1 task request (#6247). A + // function, because the settings are hot: a value copied here + // would never follow a change (the MaxCompletions pattern). The + // daemon wires it when it injects its store; nil keeps the + // store's construction-time configuration. + TaskRetention func() (ttl, maxAge time.Duration) + // TaskAudit, when set, is the audit seam a private task store // (the TaskStore == nil path in tasks()) is wired with, so a // standalone server's task lifecycle records reach a log instead @@ -379,8 +389,16 @@ func (s *Server) WithExtras(extras []Extra) *Server { // Notify comes along. Dropping it silently turned off every push // capability the moment a single extra tool existed, and a client // cannot detect a server that declared nothing. - Notify: s.Notify, - Ask: s.Ask, + Notify: s.Notify, + Ask: s.Ask, + // The task-store wiring comes along (#6247): dropping it here + // silently gave the extras server a private store and a nil + // retention provider, so the production MCPServer() path — + // which builds extras after injecting the daemon's store — + // lost both the shared store (#1555) and retention refresh. + TaskStore: s.TaskStore, + TaskRetention: s.TaskRetention, + TaskAudit: s.TaskAudit, ExtrasOnly: s.ExtrasOnly, OwnInstructions: s.OwnInstructions, Instance: s.Instance, diff --git a/pkgs/mcpx/internal/mcpserver/tasks.go b/pkgs/mcpx/internal/mcpserver/tasks.go index d2737732cf..94274f7091 100644 --- a/pkgs/mcpx/internal/mcpserver/tasks.go +++ b/pkgs/mcpx/internal/mcpserver/tasks.go @@ -53,6 +53,15 @@ func (s *Server) tasks() *taskStore { } } } + // Retention settings are hot (#6247): refresh them on every access, + // exactly as the daemon's taskStore() accessor does for /v1, so MCP + // traffic alone keeps the shared store's retention current after a + // /v1/settings change. Configure ignores nonpositive values, so a + // provider returning zeros leaves the current configuration alone. + if s.TaskRetention != nil { + ttl, maxAge := s.TaskRetention() + s.taskStore.Configure(ttl, maxAge) + } return s.taskStore } diff --git a/pkgs/mcpx/internal/mcpserver/tasks_shared_store_test.go b/pkgs/mcpx/internal/mcpserver/tasks_shared_store_test.go index 1a22811458..9fbb58ea7b 100644 --- a/pkgs/mcpx/internal/mcpserver/tasks_shared_store_test.go +++ b/pkgs/mcpx/internal/mcpserver/tasks_shared_store_test.go @@ -2,6 +2,7 @@ package mcpserver_test import ( "context" + "encoding/json" "path/filepath" "strings" "testing" @@ -77,6 +78,77 @@ func TestInjectedStoreIsTheStoreTheMCPSurfaceUses(t *testing.T) { } } +// Issue #6247: a tasks.maxAge change must reach MCP task traffic on its +// own — no intervening /v1 task request. The MCP surface refreshes the +// shared store's retention from TaskRetention on every access, so a task +// that turns terminal after the change carries the new (tiny) retention +// deadline instead of the hour the provider returned before it. +func TestMCPOnlyTrafficPicksUpRetentionChange(t *testing.T) { + store := tasks.New() + s := sharedStoreServer(store) + + maxAge := time.Hour + s.TaskRetention = func() (time.Duration, time.Duration) { return 0, maxAge } + + // Baseline MCP task under the old retention: nothing explodes, and + // the store is the injected one. + first := startSharedTask(t, s) + if done := waitTask(t, s, first, terminal); done["status"] != "completed" { + t.Fatalf("baseline task: %v", done) + } + + // The settings change (the /v1/settings write in production). From + // here the test exercises only the MCP path. + maxAge = 2 * time.Second + + id := startSharedTask(t, s) + if done := waitTask(t, s, id, terminal); done["status"] != "completed" { + t.Fatalf("post-change task: %v", done) + } + got, ok := store.Get(id) + if !ok { + t.Fatalf("post-change task %s missing from the store", id) + } + if got.ExpiresAt == nil { + t.Fatalf("post-change task %s has no retention deadline", id) + } + if remaining := time.Until(*got.ExpiresAt); remaining > 10*time.Second { + t.Errorf("post-change task keeps the old retention: expires in %v, want the new 2s maxAge to bound it", remaining) + } +} + +// Issue #6247, extras construction path: App.MCPServer injects the +// store and the retention provider and *then* calls WithExtras, whose +// reconstruction must preserve both — a provider dropped there leaves +// the production server exactly as broken as before the fix. +func TestRetentionSurvivesWithExtras(t *testing.T) { + store := tasks.New() + s := sharedStoreServer(store) + + maxAge := time.Hour + s.TaskRetention = func() (time.Duration, time.Duration) { return 0, maxAge } + s = s.WithExtras([]mcpserver.Extra{{ + Tool: mcpserver.Tool{Name: "extra_tool", Description: "an extra"}, + Call: func(context.Context, json.RawMessage) (string, error) { return "ok", nil }, + }}) + + maxAge = 2 * time.Second + id := startSharedTask(t, s) + if done := waitTask(t, s, id, terminal); done["status"] != "completed" { + t.Fatalf("task through the extras server: %v", done) + } + got, ok := store.Get(id) + if !ok { + t.Fatalf("task %s is not in the injected store after WithExtras", id) + } + if got.ExpiresAt == nil { + t.Fatalf("task %s has no retention deadline", id) + } + if remaining := time.Until(*got.ExpiresAt); remaining > 10*time.Second { + t.Errorf("extras server kept the old retention: expires in %v, want the new 2s maxAge", remaining) + } +} + func TestInjectedStoreKeepsMCPTasksAcrossReopen(t *testing.T) { path := filepath.Join(t.TempDir(), "tasks.db") store, err := tasks.Open(path)