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: never let the endpoint cap discard an explicit on-demand Run (#4834)

[._.]/ Adam Eivy1c023aa3da71
diff --git a/server/services/cos.js b/server/services/cos.js
index 3d7882dbc1..06504ac282 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -966,7 +966,12 @@ async function spawnDequeuePriority0OnDemand(ctx) {
}
applyOnDemandConsent(task);
- if (task && capacity.canSpawn(task)) {
+ // `gateLocalEndpoint: false` — a denial HERE would discard the user's "Run":
+ // the request is already cleared and the app-review marker bound, and this
+ // branch is the only thing that persists the task. Let it through and let the
+ // chokepoint hold it (#4834) — that path leaves the task pending and releases
+ // both side effects.
+ if (task && capacity.canSpawn(task, undefined, { gateLocalEndpoint: false })) {
// Mark this as a MANUAL (on-demand) run so a completed perpetual drain
// continues in the same user-initiated lane instead of the auto-run-gated
// queue path (see perpetualRefillPlan). Stamped before addTask so the
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 14fac6246e..8dde629b6b 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -103,9 +103,13 @@ function priorityDequeue(buckets, capacity, { paused = false } = {}) {
const ceiling = capacity.availableSlots;
const drain = (bucketName) => {
+ // Priority 0 opts OUT of the local-endpoint cap, exactly as production does:
+ // a denial there discards the already-cleared on-demand request instead of
+ // deferring it, so the task is emitted and the spawner chokepoint holds it.
+ const opts = { gateLocalEndpoint: bucketName !== 'onDemand' };
for (const task of buckets[bucketName] || []) {
if (capacity.spawned >= capacity.availableSlots) return;
- if (!capacity.canSpawn(task)) continue;
+ if (!capacity.canSpawn(task, undefined, opts)) continue;
capacity.trackSpawn(task);
admitted.push({ ...task, _bucket: bucketName });
}
@@ -536,6 +540,42 @@ describe('dequeueNextTask — per-local-endpoint agent cap (#4834)', () => {
expect(capacity.spawnLocalEndpointCounts[LOCAL_ENDPOINT]).toBe(1);
});
+ it('does NOT suppress an explicit on-demand Run at a saturated endpoint', () => {
+ // Priority 0 clears the request and binds the app-review marker BEFORE
+ // canSpawn runs, and that branch is the only thing that persists the task —
+ // so a denial here destroys the user's "Run" and strands the marker rather
+ // than deferring it. The task must be admitted and held at the spawner
+ // chokepoint instead, which leaves it pending and releases both side effects.
+ const state = makeState({
+ maxConcurrentAgents: 5,
+ runningAgents: [makeRunningAgentOnProvider('lmstudio-tui')],
+ });
+ const { capacity, holds } = makeLocalSlotCapacity(state);
+
+ const spawned = priorityDequeue({
+ onDemand: [taskOnProvider('task-run-now', 'lmstudio-tui')],
+ user: [],
+ autoSystem: [],
+ mission: [],
+ idle: [],
+ }, capacity);
+
+ expect(spawned.map(t => t.id)).toEqual(['task-run-now']);
+ expect(holds).toEqual([]);
+ });
+
+ it('still gates the SAME task from a deferrable tier', () => {
+ // The opt-out is scoped to Priority 0. A user-tier task at the same endpoint
+ // stays queued, because skipping it there is a genuine defer.
+ const state = makeState({
+ maxConcurrentAgents: 5,
+ runningAgents: [makeRunningAgentOnProvider('lmstudio-tui')],
+ });
+ const { capacity } = makeLocalSlotCapacity(state);
+
+ expect(priorityDequeue(userBuckets([taskOnProvider('task-run-now', 'lmstudio-tui')]), capacity)).toEqual([]);
+ });
+
it('dispatches the held task once the endpoint frees up (next cycle)', () => {
// Same fixture, but the first agent has finished — nothing is running, so
// the previously-held task now takes the slot. This is the "dispatches when
@@ -1226,6 +1266,19 @@ describe('cos.js source — priority + capacity invariants', () => {
.toMatch(/isAvailable:\s*isProviderAvailable/);
});
+ it('Priority 0 opts out of the local-endpoint cap so a Run is never discarded (#4834)', () => {
+ // The on-demand tier clears the request and binds the app-review marker
+ // before canSpawn, and is the only path that persists the task — a denial
+ // there is destructive, not a defer. Pin the opt-out at the call site.
+ const p0 = extractFnBody(COS_SRC, COS_SRC.indexOf('async function spawnDequeuePriority0OnDemand'));
+ expect(p0).toMatch(/canSpawn\(task,\s*undefined,\s*\{\s*gateLocalEndpoint:\s*false\s*\}\)/);
+ // Every other tier keeps the cap: none of them may pass the opt-out.
+ for (const tier of ['spawnDequeuePriority1UserTasks', 'spawnDequeuePriority2AutoApproved', 'spawnDequeuePriority3Missions', 'spawnDequeuePriority4IdleReview']) {
+ const body = extractFnBody(COS_SRC, COS_SRC.indexOf(`async function ${tier}`));
+ expect(body, `${tier} must not opt out of the local-endpoint cap`).not.toMatch(/gateLocalEndpoint:\s*false/);
+ }
+ });
+
it('idle generator is fenced by spawned===0 / tasksToSpawn.length===0', () => {
// Pin the strict-idle gate. If a refactor drops either fence, idle could
// spawn alongside autoSystem/mission and double-load the agent pool.
diff --git a/server/services/cosDequeue.js b/server/services/cosDequeue.js
index 8b1f864c54..704d896472 100644
--- a/server/services/cosDequeue.js
+++ b/server/services/cosDequeue.js
@@ -45,6 +45,15 @@
* a TUI provider with no recorded endpoint) is ungated. `onLocalEndpointHold`
* fires on a denial so the scheduler can log queued-no-slot without this module
* importing the event bus.
+ *
+ * `gateLocalEndpoint: false` opts a tier out of that third cap. Priority 0 does,
+ * because a denial there is DESTRUCTIVE, not a defer: the on-demand request has
+ * already been cleared and the app-review marker bound by the time `canSpawn`
+ * runs, so a `false` discards the user's explicit "Run" and strands the marker.
+ * Emitting instead is strictly better — the authoritative cap at subAgentSpawner's
+ * `task:ready` chokepoint HOLDS the task (still `pending`, marker released, job
+ * reservation freed), which is exactly the outcome this tier cannot produce.
+ * The global/per-project caps carry the same hazard here; that is pre-existing.
*/
export function createDequeueCapacity(state, {
agentsByProject = {},
@@ -66,11 +75,11 @@ export function createDequeueCapacity(state, {
const spawnLocalEndpointCounts = { ...localEndpointCounts };
let spawned = 0;
- const canSpawn = (task, ceiling = availableSlots) => {
+ const canSpawn = (task, ceiling = availableSlots, { gateLocalEndpoint = true } = {}) => {
if (spawned >= ceiling) return false;
const project = task.metadata?.app || '_self';
if ((spawnProjectCounts[project] || 0) >= perProjectLimit) return false;
- const endpoint = resolveLocalEndpoint(task);
+ const endpoint = gateLocalEndpoint ? resolveLocalEndpoint(task) : null;
if (endpoint) {
const running = spawnLocalEndpointCounts[endpoint] || 0;
if (running >= localSlotLimit) {
diff --git a/server/services/cosTaskGenerator.test.js b/server/services/cosTaskGenerator.test.js
index 22a18fa8e1..466d87bbe3 100644
--- a/server/services/cosTaskGenerator.test.js
+++ b/server/services/cosTaskGenerator.test.js
@@ -266,7 +266,9 @@ describe('both on-demand engines apply consent before addTask', () => {
it('dequeueNextTask engine consents before canSpawn / addTask', () => {
const engine = engineBody(COS_SRC, 'async function spawnDequeuePriority0OnDemand');
expect(engine.indexOf('applyOnDemandConsent(task)')).toBeGreaterThan(-1);
- expect(engine.indexOf('applyOnDemandConsent(task)')).toBeLessThan(engine.indexOf('capacity.canSpawn(task)'));
+ // Prefix match, not the full call: Priority 0 passes the local-endpoint
+ // opt-out (#4834), so the arg list is no longer bare `(task)`.
+ expect(engine.indexOf('applyOnDemandConsent(task)')).toBeLessThan(engine.indexOf('capacity.canSpawn(task'));
});
it('idle-review steal path consents when it drains an on-demand request', () => {