Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: keep a remote base on the agent stamp, and drop the slot-context cache (#4834)

[._.]/ Adam Eivy85457f1c924d
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 0b52965aa8..daee0a05c6 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -40,6 +40,7 @@ import { createDequeueCapacity, countRunningAgentsByLocalEndpoint, isMissionTier
import {
createLocalEndpointSlotContext,
localEndpointOfProvider,
+ providerBaseUrl,
reserveLocalEndpointSpawn,
pendingLocalEndpointSpawns,
__resetLocalEndpointSpawnReservations,
@@ -1084,6 +1085,34 @@ describe('localEndpointOfProvider — Ollama-backed CLI/TUI providers (#4834)',
});
});
+describe('providerBaseUrl — the stamp source (#4834)', () => {
+ it('returns the REMOTE base of an Ollama-backed CLI that has no endpoint', () => {
+ // `localRuntimeForProvider` answers null for it (correctly — it is not this
+ // box), and it records its daemon ONLY in envVars. Returning null here would
+ // stamp an absent endpoint, and `endpointForAgent` reads absent as
+ // "re-resolve by provider id" — so re-pointing that provider at a local URL
+ // mid-run would start counting a remote agent against the local GPU.
+ const remote = { id: 'remote-ollama-cli', ollamaBacked: true, envVars: { ANTHROPIC_BASE_URL: 'http://192.0.2.10:11434' } };
+ expect(providerBaseUrl(remote)).toBe('http://192.0.2.10:11434');
+ expect(localEndpointOfProvider(remote)).toBeNull();
+ });
+
+ it('prefers the resolved local runtime endpoint over the raw record', () => {
+ const local = { id: 'claude-ollama', ollamaBacked: true, envVars: { ANTHROPIC_BASE_URL: 'http://localhost:11434' } };
+ expect(localEndpointOfProvider(local)).toBe('localhost:11434');
+ });
+
+ it('is null only when the record names no base at all', () => {
+ expect(providerBaseUrl({ id: 'claude-cli', type: 'cli', endpoint: null })).toBeNull();
+ expect(providerBaseUrl(null)).toBeNull();
+ });
+
+ it('keeps a cloud base so its agent is never re-resolved', () => {
+ expect(providerBaseUrl(CLOUD_PROVIDER)).toBe('https://api.anthropic.com/v1');
+ expect(localEndpointOfProvider(CLOUD_PROVIDER)).toBeNull();
+ });
+});
+
// ─── Source-level regression guards ────────────────────────────────────────
//
// These pin two structural invariants of the production code that the
diff --git a/server/services/cosLocalEndpointSlots.js b/server/services/cosLocalEndpointSlots.js
index 48949c1e9c..1cf4a1ddae 100644
--- a/server/services/cosLocalEndpointSlots.js
+++ b/server/services/cosLocalEndpointSlots.js
@@ -45,7 +45,16 @@ import { isProviderAvailable, getFallbackProvider } from './providerStatus.js';
* `localEndpointPort` below, never by the provider's name or model id.
*/
export function providerBaseUrl(provider) {
- return localRuntimeForProvider(provider)?.endpoint || provider?.endpoint || null;
+ return localRuntimeForProvider(provider)?.endpoint
+ || provider?.endpoint
+ // Last resort, and REMOTE by construction — `localRuntimeForProvider` already
+ // took this value when it pointed at this box. It still has to be returned:
+ // an Ollama-backed CLI aimed at another host records its base ONLY here, and
+ // `endpointForAgent` treats an absent stamp as "re-resolve by id". Answering
+ // null would let that cloud agent start counting against a local endpoint the
+ // moment its provider record is re-pointed — the hole the stamp exists to close.
+ || provider?.envVars?.ANTHROPIC_BASE_URL
+ || null;
}
/**
@@ -164,33 +173,12 @@ export function createLocalEndpointSlotContext({
* an uninitialized toolkit yields a null active provider — which resolves every
* task to null (ungated) rather than stalling the queue.
*/
-// One dispatch consults this 2-3 times — the dequeue cycle, then the chokepoint
-// per emitted task (and "Run now" checks before emitting too). Each build costs
-// two provider reads plus a Map and an object over the whole catalog, so cache
-// the SNAPSHOT for a beat. Deliberately the same TTL the toolkit's own provider
-// cache uses: this adds no staleness the callers didn't already have.
-//
-// Only the provider LIST is cached. `isAvailable` and `resolveFallback` are read
-// live inside the returned closures, so a provider going unavailable still
-// changes the answer immediately — which is what the gate turns on.
-const SNAPSHOT_TTL_MS = 1000;
-let snapshot = null;
-let snapshotAt = 0;
-
-/** Test hook — drop the cached provider snapshot. */
-export function __resetLocalEndpointSlotCache() {
- snapshot = null;
- snapshotAt = 0;
-}
-
+// Deliberately NOT memoized. One dispatch builds this 2-3 times, but the
+// toolkit already caches the underlying provider read, so a local snapshot would
+// only save a Map and an object allocation — while adding a staleness window the
+// toolkit does not have: it invalidates on a provider edit, and nothing here can
+// observe that, so an edited endpoint would keep gating on its old value.
export async function buildLocalEndpointSlotContext() {
- if (snapshot && Date.now() - snapshotAt < SNAPSHOT_TTL_MS) return snapshot;
- snapshot = await buildSlotContextUncached();
- snapshotAt = Date.now();
- return snapshot;
-}
-
-async function buildSlotContextUncached() {
const providers = await listProviders();
const activeProvider = await getActiveProvider().catch(() => null);
return createLocalEndpointSlotContext({