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: cap Ollama-backed CLI providers, and trust a remote endpoint stamp (#4834)

[._.]/ Adam Eivy49ed546187aa
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index c41a90973d..88587bf427 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -505,7 +505,11 @@ async function runAgentSpawn(task) {
// alone can't answer that once the provider record is edited or deleted
// mid-run. Pre-#4834 agent records have no value here, so the counter
// falls back to resolving the id against the live provider list.
- providerEndpoint: provider.endpoint || null,
+ //
+ // `ANTHROPIC_BASE_URL` is the second source because an Ollama-backed CLI
+ // provider (shipped: claude-ollama, claude-ollama-tui) records its daemon
+ // there and leaves `endpoint` unset.
+ providerEndpoint: provider.endpoint || provider.envVars?.ANTHROPIC_BASE_URL || null,
leanMode,
// Whether THIS run's prompt told the agent to push, open, review, and merge
// its own PR. Persisted rather than re-derived at cleanup time: the two
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 81be0b00e3..133143e11c 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -1063,6 +1063,71 @@ describe('readEndpointCapacity (#4834)', () => {
});
});
+describe('localEndpointOfProvider — Ollama-backed CLI/TUI providers (#4834)', () => {
+ // Four SHIPPED providers run against the local Ollama daemon with no
+ // `endpoint` field at all. They are the archetype of this issue — PortOS
+ // launches a vendor CLI that opens its own connection — so leaving them
+ // ungated would make the cap inconsistent across providers of the same shape.
+ it('reads the daemon from ANTHROPIC_BASE_URL when endpoint is unset', () => {
+ expect(localEndpointOfProvider({
+ id: 'claude-ollama-tui', type: 'tui', ollamaBacked: true,
+ envVars: { ANTHROPIC_BASE_URL: 'http://localhost:11434' },
+ })).toBe('localhost:11434');
+ });
+
+ it('falls back to the default daemon for a bare ollamaBacked marker', () => {
+ // opencode-ollama* keep their base inside an OPENCODE_CONFIG_CONTENT blob;
+ // the marker alone means the default daemon (what the toolkit's own
+ // ollamaBaseFromProvider does).
+ expect(localEndpointOfProvider({
+ id: 'opencode-ollama-tui', type: 'tui', ollamaBacked: true,
+ envVars: { OPENCODE_CONFIG_CONTENT: '{}' },
+ })).toBe('localhost:11434');
+ });
+
+ it('shares ONE slot with the api-type `ollama` provider on the same daemon', () => {
+ const cli = localEndpointOfProvider({ id: 'claude-ollama', ollamaBacked: true, envVars: { ANTHROPIC_BASE_URL: 'http://localhost:11434' } });
+ const api = localEndpointOfProvider({ id: 'ollama', type: 'api', endpoint: 'http://localhost:11434/v1' });
+ expect(cli).toBe(api);
+ });
+
+ it('does NOT localize a REMOTE Ollama daemon', () => {
+ // The recorded base wins over the local default — an Ollama on another host
+ // is not this machine's GPU.
+ expect(localEndpointOfProvider({ id: 'remote-ollama', type: 'api', endpoint: 'http://192.0.2.10:11434' })).toBeNull();
+ expect(localEndpointOfProvider({
+ id: 'remote-claude-ollama', ollamaBacked: true,
+ envVars: { ANTHROPIC_BASE_URL: 'http://192.0.2.10:11434' },
+ })).toBeNull();
+ });
+
+ it('leaves a non-Ollama CLI provider with no endpoint ungated', () => {
+ expect(localEndpointOfProvider({ id: 'claude-cli', type: 'cli', endpoint: null })).toBeNull();
+ });
+});
+
+describe('endpointForAgent — a present stamp is authoritative both ways (#4834)', () => {
+ const slots = createLocalEndpointSlotContext({ providers: ALL_PROVIDERS, activeProvider: CLOUD_PROVIDER });
+
+ it('does NOT re-resolve when the stamp is REMOTE', () => {
+ // The agent is talking to the cloud. If its provider record is re-pointed at
+ // a local server mid-run, falling through to the id lookup would count this
+ // cloud agent against that GPU — saturating an endpoint with zero agents on
+ // it and holding every task behind it at the default limit of 1.
+ const cloudAgent = {
+ status: 'running',
+ metadata: { providerId: 'lmstudio-tui', providerEndpoint: 'https://api.orcarouter.ai/v1' },
+ };
+ expect(slots.endpointForAgent(cloudAgent)).toBeNull();
+ expect(countRunningAgentsByLocalEndpoint({ a: cloudAgent }, slots.endpointForAgent)).toEqual({});
+ });
+
+ it('still falls back to the id lookup when the stamp is ABSENT', () => {
+ expect(slots.endpointForAgent({ status: 'running', metadata: { providerId: 'lmstudio-tui' } })).toBe(LOCAL_ENDPOINT);
+ expect(slots.endpointForAgent({ status: 'running', metadata: { providerId: 'lmstudio-tui', providerEndpoint: null } })).toBe(LOCAL_ENDPOINT);
+ });
+});
+
// ─── Source-level regression guards ────────────────────────────────────────
//
// These pin two structural invariants of the production code that the
@@ -1419,7 +1484,9 @@ describe('cos.js source — priority + capacity invariants', () => {
// Counting a running agent by provider id alone breaks when the provider is
// edited or deleted while the agent still holds the GPU.
const LIFECYCLE_SRC = readFileSync(join(__dirname, 'agentLifecycle.js'), 'utf-8');
- expect(LIFECYCLE_SRC).toMatch(/providerEndpoint:\s*provider\.endpoint\s*\|\|\s*null/);
+ // Second source: an Ollama-backed CLI provider records its daemon in
+ // ANTHROPIC_BASE_URL and leaves `endpoint` unset.
+ expect(LIFECYCLE_SRC).toMatch(/providerEndpoint:\s*provider\.endpoint\s*\|\|\s*provider\.envVars\?\.ANTHROPIC_BASE_URL\s*\|\|\s*null/);
});
it('forceSpawnTask refuses synchronously when the local endpoint is full (#4834)', () => {
diff --git a/server/services/cosLocalEndpointSlots.js b/server/services/cosLocalEndpointSlots.js
index e08d097b46..12a50e5198 100644
--- a/server/services/cosLocalEndpointSlots.js
+++ b/server/services/cosLocalEndpointSlots.js
@@ -23,7 +23,7 @@
*/
import { isLocalEndpoint, LOCAL_LLM_MAX_CONCURRENCY } from '../lib/promptRunner.js';
-import { listProviders, getActiveProvider } from './providers.js';
+import { listProviders, getActiveProvider, isOllamaBackedProvider } from './providers.js';
import { isProviderAvailable, getFallbackProvider } from './providerStatus.js';
// One knob governs both paths — no new config key. A local box beefy enough to
@@ -31,13 +31,26 @@ import { isProviderAvailable, getFallbackProvider } from './providerStatus.js';
// along with N in-flight API calls.
export const LOCAL_ENDPOINT_AGENT_LIMIT = LOCAL_LLM_MAX_CONCURRENCY;
+// An Ollama-backed CLI/TUI provider carries the daemon in `envVars`, not in
+// `endpoint` — the shipped `claude-ollama` / `claude-ollama-tui` set
+// `ANTHROPIC_BASE_URL`, and `opencode-ollama` / `opencode-ollama-tui` carry only
+// the `ollamaBacked` marker (their base lives inside an OPENCODE_CONFIG_CONTENT
+// blob, which `ollamaBaseFromProvider` in the toolkit does not parse either —
+// the marker alone means the default daemon).
+const DEFAULT_OLLAMA_BASE = 'http://localhost:11434';
+
/**
* The local endpoint a provider runs against, or null when it has none.
*
- * Reads ONLY the endpoint recorded on the provider — never a model-id prefix.
- * A CLI/TUI provider with no recorded endpoint resolves to null and stays
- * ungated, which is the intended behavior: PortOS cannot know where an
- * unconfigured vendor CLI points.
+ * Reads ONLY what the provider RECORD says — the endpoint, else the Ollama base
+ * its `envVars` name, else the default daemon when (and only when) the record
+ * carries an explicit `ollamaBacked` marker. Never a model-id prefix. Everything
+ * else stays ungated: PortOS cannot know where an unconfigured vendor CLI points.
+ *
+ * The Ollama arm matters because four SHIPPED CLI/TUI providers run against the
+ * local daemon with no `endpoint` at all — precisely the "the vendor CLI opens
+ * its own connection and PortOS never sees it" case #4834 exists for. Skipping
+ * them would leave the cap inconsistent across providers of the same shape.
*
* Not gated on `provider.type === 'api'` (unlike promptRunner's request gate):
* a TUI provider pointed at a local LM Studio IS the case this exists for.
@@ -47,7 +60,12 @@ export const LOCAL_ENDPOINT_AGENT_LIMIT = LOCAL_LLM_MAX_CONCURRENCY;
* queued-no-slot log line.
*/
export function localEndpointOfProvider(provider) {
- return localSlotKey(provider?.endpoint);
+ if (!provider) return null;
+ // A recorded base is authoritative, including when it is REMOTE: an Ollama
+ // daemon on another host must resolve to null, not to the local default.
+ const recorded = provider.endpoint || provider.envVars?.ANTHROPIC_BASE_URL;
+ if (recorded) return localSlotKey(recorded);
+ return isOllamaBackedProvider(provider) ? localSlotKey(DEFAULT_OLLAMA_BASE) : null;
}
// Host+port identifies the model server; the scheme, path (`/v1`) and host
@@ -120,12 +138,15 @@ export function createLocalEndpointSlotContext({
return {
limit,
endpointForAgent: (agent) => {
- // The endpoint stamped at spawn wins: it is what this agent's inference
- // ACTUALLY landed on, and it survives the provider record being edited or
- // deleted while the agent is still holding the GPU. Falling back to the id
- // lookup keeps pre-#4834 agent records counted.
- const stamped = localEndpointOfProvider({ endpoint: agent?.metadata?.providerEndpoint });
- if (stamped) return stamped;
+ // A PRESENT stamp is authoritative in BOTH directions: it is what this
+ // agent's inference actually landed on, so a REMOTE stamp means "not on a
+ // local endpoint" — never "go re-resolve the provider". Falling through
+ // there would reintroduce the mid-run edit the stamp exists to prevent: a
+ // cloud agent would start counting against whatever endpoint its provider
+ // record now names, saturating a GPU that has no agents on it at all.
+ // Only an ABSENT stamp (a pre-#4834 record) falls back to the id lookup.
+ const stamped = agent?.metadata?.providerEndpoint;
+ if (stamped != null) return localSlotKey(stamped);
const providerId = agent?.metadata?.providerId || agent?.providerId;
return providerId ? endpointById(providerId) : null;
},