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

run a continuation that arrived mid-cycle instead of dropping it on the re-entrancy guard

[._.]/ Adam Eivy096d9ecbcd45
diff --git a/server/services/quotaBurnRunner.js b/server/services/quotaBurnRunner.js
index 87389dcca1..ba23ce9442 100644
--- a/server/services/quotaBurnRunner.js
+++ b/server/services/quotaBurnRunner.js
@@ -49,6 +49,9 @@ const TICK_MS = 60_000;
let tickTimer = null;
let running = false;
let lastRunAt = null;
+// Family ids whose completion continuation arrived while a cycle was running —
+// drained by `drainDeferredContinuations` when that cycle releases the guard.
+const deferredContinuations = new Set();
/**
* The jobs a cycle will consider, in plan order.
@@ -143,7 +146,17 @@ async function dispatchFromCandidate(candidate, { jobId = null, force = false, a
* gate — the reset window, the reserve, and the per-window dispatch cap.
*/
export async function runQuotaBurnCycle(options = {}) {
- if (running) return { skipped: 'already-running' };
+ if (running) {
+ // A continuation that lands mid-cycle is REMEMBERED, not dropped. Two burn
+ // agents from different families finishing within the same cycle is ordinary
+ // — and a dropped continuation stalls that family's plan until the next
+ // interval tick, which at the 12-hour default is most of a day of the window
+ // this feature exists to spend. Keyed by family, so several completions from
+ // one family while it is mid-cycle collapse to the single re-evaluation they
+ // amount to.
+ if (options.trigger === 'continuation' && options.familyId) deferredContinuations.add(options.familyId);
+ return { skipped: 'already-running' };
+ }
running = true;
// try/finally, not try/catch: this runs from a timer and from a route, and a
// throw anywhere below (an ENOSPC on the ledger write, say) would otherwise
@@ -154,6 +167,26 @@ export async function runQuotaBurnCycle(options = {}) {
return await evaluate(options);
} finally {
running = false;
+ await drainDeferredContinuations();
+ }
+}
+
+/**
+ * Run the continuations that arrived while a cycle was in flight, one at a time.
+ *
+ * Bounded by the same ledger every other dispatch charges: a drained family gets
+ * one cycle, which the gate ladder can decline. A completion that lands DURING
+ * the drain re-enters the set legitimately — it is a different agent finishing —
+ * and is likewise bounded by `maxDispatchesPerWindow`. Errors are swallowed here
+ * rather than allowed to replace the outer cycle's result: this runs from the
+ * caller's `finally`, so a throw would mask what that cycle actually decided.
+ */
+async function drainDeferredContinuations() {
+ while (deferredContinuations.size) {
+ const [familyId] = deferredContinuations;
+ deferredContinuations.delete(familyId);
+ await runQuotaBurnCycle({ trigger: 'continuation', familyId })
+ .catch((err) => console.error(`❌ Quota-burn deferred continuation for ${familyId} failed: ${err.message}`));
}
}
@@ -453,6 +486,7 @@ export function __resetQuotaBurnRunner() {
if (tickTimer) clearInterval(tickTimer);
tickTimer = null;
cosEvents.off('agent:completed', onBurnAgentCompleted);
+ deferredContinuations.clear();
running = false;
lastRunAt = null;
}
diff --git a/server/services/quotaBurnRunner.test.js b/server/services/quotaBurnRunner.test.js
index 27429841b2..79a6ce33fe 100644
--- a/server/services/quotaBurnRunner.test.js
+++ b/server/services/quotaBurnRunner.test.js
@@ -438,6 +438,32 @@ describe('completion continuation', () => {
expect(getProviderQuotas).toHaveBeenCalledWith(expect.objectContaining({ family: 'grok' }));
});
+ it('runs a continuation that arrived mid-cycle instead of dropping it', async () => {
+ // Two burn agents finishing inside one cycle is ordinary. Dropping the
+ // second on the re-entrancy guard stalls that family until the next interval
+ // tick — most of a day at the 12-hour default.
+ state.config = normalizeQuotaBurnConfig({
+ enabled: true,
+ families: {
+ claude: { enabled: true, resetWithinHours: 24, jobs: [{ id: 'c1', enabled: true, jobType: 'agent-prompt' }] },
+ agy: { enabled: true, resetWithinHours: 24, jobs: [{ id: 'a1', enabled: true, jobType: 'agent-prompt' }] },
+ },
+ });
+ state.quotas = [card('claude'), card('agy')];
+ state.pending = { c1: { count: 1 }, a1: { count: 1 } };
+ // agy's agent finishes while claude's cycle is still mid-dispatch.
+ const { runBurnJob } = await import('./quotaBurnJobs/index.js');
+ runBurnJob.mockImplementationOnce(async ({ job, family, candidate }) => {
+ state.ran.push({ jobId: job.id, familyId: family.id, charge: candidate.charge });
+ await expect(__onBurnAgentCompleted({ metadata: { taskQuotaBurnFamily: 'agy' } }))
+ .resolves.toEqual({ skipped: 'already-running' });
+ return { dispatched: true, summary: `ran ${job.id}` };
+ });
+
+ await runQuotaBurnCycle({ trigger: 'manual', familyId: 'claude' });
+ expect(state.ran.map((entry) => entry.familyId)).toEqual(['claude', 'agy']);
+ });
+
it('ignores an agent that was not a quota burn', async () => {
state.pending = { first: { count: 1 } };
await __onBurnAgentCompleted({ metadata: { taskType: 'user' } });