Files
ai/object/cache_bus_test.go
e5ef6b5573 fix(router): explore honors true servability + 5s org-settings TTL & Valkey invalidation bus (#112)
* fix(router): explore/heuristic honor true servability (served ∩ enabled), not just the org allowlist

The per-org policy override replaced the process's real servability predicate
(c.Known) with the org's Enabled allowlist, so the exploration floor and the
servable-first heuristic could pick an ENABLED-but-UNSERVED model (e.g. one still
allowlisted after its provider disconnected) — which 404s downstream. With
ec.Allow left unset the last-resort also relaxed to ALL models, ignoring disabled.

Intersect instead of replace: ec.Known = served ∩ enabled (explore + servable-first
heuristic only pick actually-served models); set ec.Allow = rp.Enabled as the HARD
allowlist so the last-resort may relax servability to a listed model (beats an empty
id) but never routes outside the allowlist. Adds TestExploreStaysServableUnderPolicy
+ TestExploreSamplesOnlyServedAlternatives.

* feat(router): 5s org-settings cache TTL + opt-in Valkey cross-pod invalidation bus

Reconcile the org-settings (router policy / savings↔quality dial) cache TTL from
60s to 5s so a console change converges fleet-wide within 5s (overridable via
ROUTER_SETTINGS_CACHE_TTL_SECONDS). Add an opt-in cross-pod invalidation bus:
when KV_URL is set, every org-settings write broadcasts over Valkey pub/sub so all
pods drop the stale entry sub-second, with the TTL as the fail-safe backstop; a
no-op without KV_URL. Mirrors InitDatastore/InitTelemetry; wired from Bootstrap.

---------

Co-authored-by: hanzo-dev <[email protected]>
2026-07-22 15:51:35 -07:00

62 lines
2.1 KiB
Go

package object
import (
"testing"
"time"
)
// TestApplyInvalidation_FlushesOrgSettings proves a received broadcast drops this
// pod's cached settings (the subscriber's flush path), and that an unknown kind is
// a safe no-op (an older pod tolerates a newer sender).
func TestApplyInvalidation_FlushesOrgSettings(t *testing.T) {
orgSettingsCacheMu.Lock()
orgSettingsCache["acme"] = &orgSettingsCacheEntry{settings: &OrgSettings{Owner: "acme"}, fetchedAt: time.Now()}
orgSettingsCacheMu.Unlock()
// Unknown kind must not touch the cache.
applyInvalidation("something-else")
orgSettingsCacheMu.RLock()
n := len(orgSettingsCache)
orgSettingsCacheMu.RUnlock()
if n == 0 {
t.Fatal("unknown invalidation kind flushed the cache — should be a no-op")
}
// The org_settings broadcast flushes it.
applyInvalidation("org_settings")
orgSettingsCacheMu.RLock()
n = len(orgSettingsCache)
orgSettingsCacheMu.RUnlock()
if n != 0 {
t.Fatalf("org_settings broadcast left %d cached entries, want 0 (flushed)", n)
}
}
// TestBroadcastInvalidate_NoBusIsNoop proves a write never fails when the KV_URL
// bus is disabled: broadcastInvalidate with no client is a silent no-op — the
// local flush already happened and the TTL backstops every other pod.
func TestBroadcastInvalidate_NoBusIsNoop(t *testing.T) {
cacheBusMu.Lock()
cacheBusClient = nil
cacheBusMu.Unlock()
broadcastInvalidate("org_settings") // must not panic or block
}
// TestOrgSettingsTTL_EnvOverride proves the cache TTL honors
// ROUTER_SETTINGS_CACHE_TTL_SECONDS and falls back to the 5s near-real-time
// default on unset/garbage.
func TestOrgSettingsTTL_EnvOverride(t *testing.T) {
t.Setenv("ROUTER_SETTINGS_CACHE_TTL_SECONDS", "12")
if got := orgSettingsTTL(); got != 12*time.Second {
t.Fatalf("TTL with env=12 = %v, want 12s", got)
}
t.Setenv("ROUTER_SETTINGS_CACHE_TTL_SECONDS", "")
if got := orgSettingsTTL(); got != 5*time.Second {
t.Fatalf("default TTL = %v, want 5s", got)
}
t.Setenv("ROUTER_SETTINGS_CACHE_TTL_SECONDS", "garbage")
if got := orgSettingsTTL(); got != 5*time.Second {
t.Fatalf("bad-env TTL = %v, want 5s default", got)
}
}