Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
perf(agents): raise the model catalog serve window to 24h (MUL-5444) (#6115)
Multica Eve5f820a1d2253
diff --git a/server/internal/handler/runtime_model_catalog.go b/server/internal/handler/runtime_model_catalog.goindex 621b39ecae..7962df05a9 100644--- a/server/internal/handler/runtime_model_catalog.go+++ b/server/internal/handler/runtime_model_catalog.go@@ -22,12 +22,22 @@ import (// stale-while-revalidate candidate: answer from the last known good snapshot// immediately, and refresh in the background so the NEXT open is also warm.//-// Windows are deliberately conservative:-// - modelCatalogServeWindow bounds how stale an answer the API will hand a-// client without waiting for the daemon.-// - modelCatalogRevalidateAfter bounds how long a snapshot can be served-// without triggering a background refresh, so a CLI upgrade converges-// within one open instead of lingering for the whole serve window.+// The two windows do different jobs, and only one of them governs freshness:+// - modelCatalogRevalidateAfter is the freshness knob. Serving a snapshot+// older than this also queues a background refresh, so a CLI upgrade+// converges after one open no matter how long the serve window is.+// - modelCatalogServeWindow only bounds how long an UNUSED snapshot survives,+// and how stale the answer is for someone who opens the picker exactly once+// and never returns. It is deliberately day-scale: nothing keeps an entry+// warm in the background, the browser's own react-query cache dies with the+// tab, and agent CLIs are upgraded on a scale of days — so a minutes-scale+// window made every first-open-of-the-day a cold miss (the exact multi-second+// wait this cache exists to remove) while buying no real freshness.+//+// The window is not unbounded because a *failed* report deliberately leaves the+// snapshot in place (a transient discovery failure must not empty the picker).+// For a runtime whose discovery keeps failing — CLI uninstalled, logged out —+// expiry is the only thing that eventually retires the stale catalog.//// Only successful, non-empty, `supported` catalogs are cached. An empty list is// almost always a transient discovery failure (CLI not logged in, timeout) —@@ -36,10 +46,15 @@ import (const (// modelCatalogServeWindow is how long a cached catalog may answer a- // list-models request without waiting for the daemon.- modelCatalogServeWindow = 15 * time.Minute+ // list-models request without waiting for the daemon. Day-scale on purpose+ // (MUL-5444): see the freshness discussion above — every served snapshot+ // past modelCatalogRevalidateAfter queues its own refresh, so this bounds+ // unused-entry lifetime and the open-once worst case, not staleness for an+ // active user.+ modelCatalogServeWindow = 24 * time.Hour// modelCatalogRevalidateAfter is the age past which serving from cache also- // enqueues a background refresh.+ // enqueues a background refresh. This is the knob that actually keeps the+ // catalog honest; keep it short.modelCatalogRevalidateAfter = 60 * time.Second)diff --git a/server/internal/handler/runtime_model_catalog_test.go b/server/internal/handler/runtime_model_catalog_test.goindex 09cf4a8bf9..ea99b0e057 100644--- a/server/internal/handler/runtime_model_catalog_test.go+++ b/server/internal/handler/runtime_model_catalog_test.go@@ -194,6 +194,86 @@ func TestInMemoryModelCatalogCache_ExpiresAndInvalidates(t *testing.T) {}}+// TestModelCatalogServeWindow_ServesDayOldSnapshotAndRevalidates pins the+// day-scale serve window (MUL-5444). Nothing keeps a snapshot warm in the+// background and the browser's own react-query cache dies with the tab, so a+// minutes-scale window turned every first-open-of-the-day into a cold daemon+// round trip — the multi-second wait this cache exists to remove. Freshness is+// not traded away for it: a snapshot past modelCatalogRevalidateAfter is still+// served, but serving it queues the refresh that makes the next open correct.+func TestModelCatalogServeWindow_ServesDayOldSnapshotAndRevalidates(t *testing.T) {+ ctx := context.Background()++ if modelCatalogRevalidateAfter >= modelCatalogServeWindow {+ t.Fatalf("revalidate threshold %s must stay well below the serve window %s, otherwise nothing refreshes",+ modelCatalogRevalidateAfter, modelCatalogServeWindow)+ }+ if modelCatalogServeWindow < 12*time.Hour {+ t.Fatalf("serve window %s is not day-scale; agent CLIs are upgraded on a scale of days, so a shorter window only makes the first open of each day slow",+ modelCatalogServeWindow)+ }++ cache := NewInMemoryModelCatalogCache()+ store := NewInMemoryModelListStore()+ rec := &pendingWorkRecorder{}+ h := &Handler{ModelCatalogCache: cache, ModelListStore: store, DaemonPendingWork: rec}++ seed := func(runtimeID string, age time.Duration) {+ cache.mu.Lock()+ defer cache.mu.Unlock()+ cache.entries[runtimeID] = ModelCatalogSnapshot{+ RuntimeID: runtimeID,+ Models: sampleCatalog(),+ Supported: true,+ StoredAt: time.Now().Add(-age),+ }+ }++ for _, tc := range []struct {+ name string+ age time.Duration+ served bool+ }{+ {name: "hours old is still served", age: 2 * time.Hour, served: true},+ {name: "just inside the window is served", age: modelCatalogServeWindow - time.Minute, served: true},+ {name: "past the window is a miss", age: modelCatalogServeWindow + time.Minute, served: false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ runtimeID := "rt-" + tc.name+ seed(runtimeID, tc.age)+ got := h.cachedModelCatalog(ctx, runtimeID)+ if tc.served && got == nil {+ t.Fatalf("snapshot aged %s should still answer without waiting for the daemon", tc.age)+ }+ if !tc.served && got != nil {+ t.Fatalf("snapshot aged %s is past the serve window and must not be served: %+v", tc.age, got)+ }+ })+ }++ // Serving an aged snapshot must still queue the refresh — the whole reason a+ // long window is safe.+ seed("rt-revalidate", 2*time.Hour)+ served := h.cachedModelCatalog(ctx, "rt-revalidate")+ if served == nil {+ t.Fatal("expected the aged snapshot to be served")+ }+ if age := served.Age(time.Now()); age < modelCatalogRevalidateAfter {+ t.Fatalf("test fixture is younger than the revalidate threshold (%s < %s)", age, modelCatalogRevalidateAfter)+ }+ h.revalidateModelCatalog(ctx, "rt-revalidate")+ pending, err := store.HasPending(ctx, "rt-revalidate")+ if err != nil {+ t.Fatalf("has pending: %v", err)+ }+ if !pending {+ t.Fatal("serving an aged snapshot must enqueue a background refresh")+ }+ if rec.count() != 1 {+ t.Fatalf("expected exactly 1 pending-work hint, got %d", rec.count())+ }+}+// failingModelCatalogCache reports a backend error on every read.type failingModelCatalogCache struct{}