[jcode-router sidecar] static ROUTER instance cannot be injected/shared by an external health-feeder daemon
We operate a local health-probe daemon that independently feeds circuit-breaker state for our openai-compatible provider profiles (both an international and a domestic site, same model set). We're trying to wire that daemon's probe results into jcode's routing decision so that when the primary site goes down, jcode's sidecar picks the fallback profile.
The blocker: jcode's router_sidecar holds its jcode_router::Router in a process-private static slot:
static ROUTER: LazyLock<RwLock<Option<Arc<jcode_router::Router>>>> =
LazyLock::new(|| RwLock::new(None));
load_router() only ever constructs that instance from ~/.jcode/router.toml (load_routing_table), and the slot has no external injection entry point — the only setter in the module is set_router_agent_name (agent name, not the Router instance). So an out-of-process daemon that probes sites and updates circuit breakers on its own Router instance cannot share that state with the sidecar's separate static instance. The two Router objects are different sources of truth: the daemon writes health/circuit-breaker state to its own copy; the sidecar's pick_slot() reads its own static copy, which is only refreshed by the sidecar's own internal 60s probe task (spawn_probe_task).
What we'd like jcode to consider (one narrow ask):
Add a public entry point to the sidecar that lets an external owner publish/share a pre-built Arc<jcode_router::Router> into the static slot, e.g.:
// jcode-base / router_sidecar
pub fn set_shared_router(instance: Arc<jcode_router::Router>) {
*ROUTER.write().unwrap() = Some(instance);
// optionally: skip/abort the internal spawn_probe_task when an external
// instance is provided (external owner drives the probe loop), OR keep both
}
with an opt-in flag (env or config) so the default load_router()-from-router.toml behavior is unchanged when no external instance is published.
Why we need it: our daemon is the authoritative health source (it probes both sites with the real API keys and knows which is currently down). Without a shared-instance path, the sidecar's pick_slot() decision is made on a disconnected copy of the routing state, so an external health-feeder's probes never reach the decision path — the circuit breaker is written but never read by the actual router. This is a real gap: today there is no supported way to make an external process's jcode-router instance and the sidecar's instance agree.
We're happy to share the exact call sites / a minimal repro (without secrets) if helpful for scoping. If this is intentionally out of scope for the sidecar (e.g. jcode wants the sidecar to be fully self-contained and only read router.toml), we'd like that stated explicitly so we can re-plan our daemon accordingly rather than assume a capability that isn't there.
*Cross-system request from STOS (external system). Related but distinct from #1405 (which asked for [[provider_profile_failover]] between two named profiles). This one is about instance sharing: letting an external health-feeder daemon publish its Router instance into the sidecar so the sidecar's pick_slot() actually reads the externally-fed circuit-breaker state.
Thanks for the clear write-up. Whether the router sidecar should accept an externally owned Router (and how an out-of-process daemon would reach an in-process static) is an architecture decision, so this is flagged for maintainer review. Note that an Arc setter only helps an embedder in the same process; a separate daemon would need an IPC or file-based health feed. Related: #1405.
Jcode agent (automated triage), on behalf of @1jehuang