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

Reduce CoS scoped CI selection through leaf imports (#12341)

[._.]/8e5995c0c0a9
diff --git a/server/lib/importScoping.test.js b/server/lib/importScoping.test.js
index 9f00907491..c6df99ce5f 100644
--- a/server/lib/importScoping.test.js
+++ b/server/lib/importScoping.test.js
@@ -338,6 +338,18 @@ describe('deferred imports stay deferred (#6156)', () => {
// dynamically connected CoS cluster (#12285): the entry must import its target
// neither way. Re-measure with `node scripts/lib/importReach.js <source>`.
const SELECTION_CUTS = [
+ ['services/cosClaimOwnershipState.js', 'services/cosClaimOwnership.js',
+ 'owns the shared state fence without importing its reconciliation consumers'],
+ ['services/systemActivityNotify.js', 'services/systemActivityWatchers.js',
+ 'never re-exports boot observers into notification producers'],
+ ['services/memoryConfig.js', 'services/cos.js',
+ 'updates memory approval counts through the declaring agent lifecycle module'],
+ ['services/worktreeManager.js', 'services/cosClaimOwnership.js',
+ 'shares the ownership fence and trusted reader without reconciliation dependencies'],
+ ['services/promptRunner.js', 'services/compositeProviders.js',
+ 'uses the toolkit resolver already registered before runners start'],
+ ['services/systemActivityNotify.js', 'services/mediaJobQueue/index.js',
+ 'keeps boot subscriptions in systemActivityWatchers.js'],
['services/pm2.js', 'services/readinessNotify.js',
'notifies readiness subscribers through the readinessSignal.js leaf'],
['services/promptRunner.js', 'services/autoFixer.js',
diff --git a/server/services/README.md b/server/services/README.md
index e4bb59c1f3..a4dddd52a6 100644
--- a/server/services/README.md
+++ b/server/services/README.md
@@ -9,6 +9,9 @@ image-generation paths below are useful entry points when tracing render jobs.
| `branchReconcileCompletion.js` | Runs ownership-checked merged and superseded cleanup on coordinator completion, independent of perpetual scheduling. |
| `heldCheckoutReview.js` | Computes fresh forge identity, merged PR/default refs and exact code comparisons for a digest-bound operator retirement; no saved consent or policy rewrites. |
| `heldCheckoutRecovery.js` | Prepares scoped retirement reviews and preserves checkout content, empty gitlinks and private Git administration before ownership-checked removal and expected-head local ref deletion. Pending-merge receipts use `retirePendingMergeCheckout`, which independently verifies the merged head and refuses locks, supersession and initialized submodules. |
+| `cosClaimOwnershipState.js` | Shared ownership lock and fail-closed live-agent reader used by claim binding and worktree cleanup. Re-exported by `cosClaimOwnership.js` for existing callers; does not import reconciliation. |
+| `systemActivityNotify.js` | Coalesced activity invalidation signal and LLM lifecycle adapters; producers do not import media execution. |
+| `systemActivityWatchers.js` | Socket initialization arms media and CoS activity subscriptions here, sharing the notification signal without pulling boot observers into producers. |
| `branchCompletionPolicy.js` | Retains explicit registered PR completion policy through agent archives. `readBranchCompletionHold` gates reconciliation by exact repository/branch/PR identity; human handoffs and configured review stay held, explicit releases require a fresh matching forge head, and verified merged retirement is a separate action. |
| `databaseCutoverHandshake.js` | Narrow fenced-boot path for the ordinary server: at `verifying`/`verified` prove this process's own pool reaches the recorded target, publish a per-pid proof, and wait for admission release before the app graph loads. |
| `databaseMaintenanceCutover.js` | Internal cutover worker body (transfer → forward-only `.env` mode commit → server restart with target proof → release → CoS restart) and `recoverDatabaseCutover(id)` same-operation recovery for the CLI/API. |
diff --git a/server/services/cosClaimOwnership.js b/server/services/cosClaimOwnership.js
index 925f763da6..e4e4bcec73 100644
--- a/server/services/cosClaimOwnership.js
+++ b/server/services/cosClaimOwnership.js
@@ -19,35 +19,13 @@
import { resolve } from 'path';
import { bindClaimBranch, releaseClaimBranch, reserveReconcileBranch, claimCheckoutOwnerReason } from '../lib/claimContinuation.js';
+import { withClaimOwnershipLock, readClaimOwnershipAgents } from './cosClaimOwnershipState.js';
+
const normalize = (p) => (process.platform === 'win32' ? resolve(p).toLowerCase() : resolve(p));
const samePath = (a, b) => typeof a === 'string' && typeof b === 'string' && normalize(a) === normalize(b);
const branchOf = (wt) => String(wt?.branch || '').replace(/^refs\/heads\//, '');
-// One in-process tail shared by binding and branch-reconcile's retirement, so a
-// bind cannot land between cleanup's ownership re-read and its removal.
-let ownershipTail = Promise.resolve();
-export function withClaimOwnershipLock(work) {
- const run = ownershipTail.then(work, work);
- ownershipTail = run.catch(() => {});
- return run;
-}
-
-/** Destructive callers must not consume loadState's empty recovery default. */
-export async function readClaimOwnershipAgents() {
- const { readAgentsStateForSafetyCheck } = await import('./cosState.js');
- const snapshot = await readAgentsStateForSafetyCheck().catch(() => null);
- if (snapshot?.trusted !== true || !snapshot.agents || typeof snapshot.agents !== 'object' || Array.isArray(snapshot.agents)) return null;
- const entries = Object.entries(snapshot.agents);
- if (entries.some(([id, agent]) => {
- if (!agent || agent.id !== id || !['running', 'paused', 'completed'].includes(agent.status)) return true;
- if (agent.metadata != null && (typeof agent.metadata !== 'object' || Array.isArray(agent.metadata))) return true;
- return ['claimBranches', 'claimReleasedBranches'].some(key => {
- const bindings = agent.metadata?.[key] ?? agent[key];
- return bindings != null && (!Array.isArray(bindings) || bindings.some(branch => typeof branch !== 'string'));
- });
- })) return null;
- return entries.map(([, agent]) => agent);
-}
+export { withClaimOwnershipLock, readClaimOwnershipAgents } from './cosClaimOwnershipState.js';
// Match merge admission's pre-start budget, across BOTH queues. A deadline is
// cancellation only until the state callback starts; afterwards it diagnoses
diff --git a/server/services/cosClaimOwnership.test.js b/server/services/cosClaimOwnership.test.js
index 61421aafb1..9ec76065a9 100644
--- a/server/services/cosClaimOwnership.test.js
+++ b/server/services/cosClaimOwnership.test.js
@@ -18,7 +18,9 @@ vi.mock('./cosState.js', () => ({
}));
vi.mock('./cosEvents.js', () => ({ cosEvents: { emit: vi.fn() } }));
-const { updateClaimOwnership, checkReconcileOwnership, withClaimOwnershipLock, CLAIM_OWNERSHIP_QUEUE_TIMEOUT_MS } = await import('./cosClaimOwnership.js');
+const { updateClaimOwnership, checkReconcileOwnership, CLAIM_OWNERSHIP_QUEUE_TIMEOUT_MS } = await import('./cosClaimOwnership.js');
+// Hold the same leaf fence cleanup uses while exercising binding through its public service.
+const { withClaimOwnershipLock } = await import('./cosClaimOwnershipState.js');
const { claimContinuationAdmission, claimBranchAdmission } = await import('../lib/claimContinuation.js');
const { withStateLock, readAgentsStateForSafetyCheck, saveState } = await import('./cosState.js');
diff --git a/server/services/cosClaimOwnershipState.js b/server/services/cosClaimOwnershipState.js
new file mode 100644
index 0000000000..31681eeaed
--- /dev/null
+++ b/server/services/cosClaimOwnershipState.js
@@ -0,0 +1,26 @@
+/** Shared claim-ownership fence and trusted reader, independent of reconciliation. */
+// One in-process tail shared by binding and branch-reconcile's retirement, so a
+// bind cannot land between cleanup's ownership re-read and its removal.
+let ownershipTail = Promise.resolve();
+export function withClaimOwnershipLock(work) {
+ const run = ownershipTail.then(work, work);
+ ownershipTail = run.catch(() => {});
+ return run;
+}
+
+/** Destructive callers must not consume loadState's empty recovery default. */
+export async function readClaimOwnershipAgents() {
+ const { readAgentsStateForSafetyCheck } = await import('./cosState.js');
+ const snapshot = await readAgentsStateForSafetyCheck().catch(() => null);
+ if (snapshot?.trusted !== true || !snapshot.agents || typeof snapshot.agents !== 'object' || Array.isArray(snapshot.agents)) return null;
+ const entries = Object.entries(snapshot.agents);
+ if (entries.some(([id, agent]) => {
+ if (!agent || agent.id !== id || !['running', 'paused', 'completed'].includes(agent.status)) return true;
+ if (agent.metadata != null && (typeof agent.metadata !== 'object' || Array.isArray(agent.metadata))) return true;
+ return ['claimBranches', 'claimReleasedBranches'].some(key => {
+ const bindings = agent.metadata?.[key] ?? agent[key];
+ return bindings != null && (!Array.isArray(bindings) || bindings.some(branch => typeof branch !== 'string'));
+ });
+ })) return null;
+ return entries.map(([, agent]) => agent);
+}
diff --git a/server/services/heldCheckoutRecovery.test.js b/server/services/heldCheckoutRecovery.test.js
index ecb9d7029e..2b2679c2cd 100644
--- a/server/services/heldCheckoutRecovery.test.js
+++ b/server/services/heldCheckoutRecovery.test.js
@@ -302,7 +302,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
const c = await receiptCheckout('receipt', { gitlink: true });
const result = await c.retireReceipt();
expect(result).toMatchObject({ ok: true, branchDeleted: true });
- expect(await registered(c.path)).toBe(false);
+ expect(await registered(c.release.path)).toBe(false);
expect(await readFile(join(result.archiveDir, 'files/ignored.txt'), 'utf8')).toBe('ignored work\n');
const manifest = JSON.parse(await readFile(join(result.archiveDir, 'manifest.json'), 'utf8'));
expect(manifest.captured.gitlinks).toMatchObject([{ path: 'vendor', state: 'empty' }]);
@@ -362,7 +362,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
}
expect(await c.retireReceipt()).toMatchObject({ ok: false,
reason: change === 'head' ? 'receipt-pr-head-changed' : 'retirement-supersession-not-accepted' });
- expect(await registered(c.path)).toBe(true);
+ expect(await registered(c.release.path)).toBe(true);
expect(await archives()).toEqual([]);
});
@@ -379,7 +379,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
await writeFile(join(c.path, 'vendor', 'unique.txt'), 'submodule work');
expect(await c.retireReceipt()).toMatchObject({ ok: false, reason: 'retirement-submodule-not-uninitialized' });
expect(await readFile(join(c.path, 'vendor', 'unique.txt'), 'utf8')).toBe('submodule work');
- expect(await registered(c.path)).toBe(true);
+ expect(await registered(c.release.path)).toBe(true);
expect(await archives()).toEqual([]);
});
@@ -390,7 +390,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
id: 'agent-live', status: 'running', workspacePath: c.release.path,
}]) });
expect(result).toMatchObject({ ok: false, reason: 'claim-owner-active', archiveDir: expect.any(String) });
- expect(await registered(c.path)).toBe(true);
+ expect(await registered(c.release.path)).toBe(true);
expect(await readFile(join(result.archiveDir, 'files/ignored.txt'), 'utf8')).toBe('ignored work\n');
});
diff --git a/server/services/memory.test.js b/server/services/memory.test.js
index 1d87f36c43..41c8c94220 100644
--- a/server/services/memory.test.js
+++ b/server/services/memory.test.js
@@ -60,6 +60,8 @@ vi.mock('../lib/asyncMutex.js', () => ({
createMutex: () => (fn) => fn()
}));
+vi.mock('./cosAgentLifecycle.js', () => ({ getAgent: vi.fn(), updateAgent: vi.fn() }));
+
// Mock memoryConfig
vi.mock('./memoryConfig.js', () => ({
DEFAULT_MEMORY_CONFIG: {
@@ -722,8 +724,17 @@ describe('memory service', () => {
return Promise.resolve(def);
});
+ // Exercise the real approval-count side effect behind the public memory workflow.
+ const lifecycle = await import('./cosAgentLifecycle.js');
+ lifecycle.getAgent.mockResolvedValue({ memoryExtraction: { pendingApproval: 2, created: 4 } });
+ lifecycle.updateAgent.mockResolvedValue(undefined);
+ const actualConfig = await vi.importActual('./memoryConfig.js');
+ decrementAgentPendingApproval.mockImplementationOnce(actualConfig.decrementAgentPendingApproval);
const result = await approveMemory('mem-1');
+ expect(lifecycle.updateAgent).toHaveBeenCalledWith('agent-1', {
+ memoryExtraction: { pendingApproval: 1, created: 4 },
+ });
expect(result.success).toBe(true);
expect(result.memory.status).toBe('active');
expect(notifications.removeByMetadata).toHaveBeenCalledWith('memoryId', 'mem-1');
diff --git a/server/services/memoryConfig.js b/server/services/memoryConfig.js
index 550482af0b..510fe2ea49 100644
--- a/server/services/memoryConfig.js
+++ b/server/services/memoryConfig.js
@@ -40,7 +40,7 @@ export function generateSummary(content, maxLength = 150) {
export async function decrementAgentPendingApproval(sourceAgentId) {
if (!sourceAgentId) return;
- const { getAgent, updateAgent } = await import('./cos.js');
+ const { getAgent, updateAgent } = await import('./cosAgentLifecycle.js');
const agent = await getAgent(sourceAgentId).catch(() => null);
if (!agent?.memoryExtraction?.pendingApproval) return;
diff --git a/server/services/promptRunner.js b/server/services/promptRunner.js
index 1bb7027fd7..d4b4d1ba95 100644
--- a/server/services/promptRunner.js
+++ b/server/services/promptRunner.js
@@ -1185,15 +1185,18 @@ async function pickFallbackProvider(failed, requestCapabilities) {
for (const p of all.providers) providersMap[p.id] = p;
// A composite (`harness.method@service`, #7564) never sits in the stored map;
// the failed provider's own id may be one, and it must be in the map for the
- // toolkit's self-loop guard and `fallbackProvider` read to see it. Lazy: the
- // resolver's closure reaches the graph store, which this request-path module
- // must not drag in for the preset-only common case.
- if (isCompositeProviderId(failed.id)) {
- await (await import('./compositeProviders.js')).withCompositeCandidates(providersMap, [failed.id]);
+ // toolkit's self-loop guard and `fallbackProvider` read to see it. The
+ // toolkit's boot-registered resolver already materializes composite ids;
+ // reusing it keeps the host resolver graph out of request-path CI selection.
+ if (isCompositeProviderId(failed.id) && !providersMap[failed.id]) {
+ const record = await getProviderById(failed.id);
+ if (record) providersMap[failed.id] = record;
}
const chainComposites = peekFallbackChain().map((e) => e.providerId).filter(isCompositeProviderId);
- if (chainComposites.length) {
- await (await import('./compositeProviders.js')).withCompositeCandidates(providersMap, chainComposites);
+ for (const id of chainComposites) {
+ if (providersMap[id]) continue;
+ const record = await getProviderById(id);
+ if (record) providersMap[id] = record;
}
const picked = providerStatus.getFallbackProvider(failed.id, providersMap, null, null, requestCapabilities);
diff --git a/server/services/promptRunner.test.js b/server/services/promptRunner.test.js
index 95e30ee81e..6b636f27f9 100644
--- a/server/services/promptRunner.test.js
+++ b/server/services/promptRunner.test.js
@@ -1,3 +1,4 @@
+import { setFallbackChainCache } from '../lib/providerFallbackChainCache.js';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
// Usage-limit tests exercise routing, not the install's persisted usage ledger.
@@ -99,6 +100,7 @@ const tuiProvider = (extra = {}) => ({
});
beforeEach(() => {
+ setFallbackChainCache([]);
vi.clearAllMocks();
runner.createRun.mockResolvedValue({ runId: 'run-xyz' });
// vi.clearAllMocks() clears calls but NOT mockReturnValue overrides,
@@ -1097,6 +1099,30 @@ describe('promptRunner — retry-with-fallback', () => {
return { isAvailable, markUnavailable, markUsageLimit, getFallbackProvider };
}
+ it('resolves composite fallback candidates through the configured provider lookup', async () => {
+ const primary = apiProvider({ id: 'openai.api@primary', name: 'Primary composite' });
+ const fallback = apiProvider({ id: 'openai.api@fallback', name: 'Fallback composite' });
+ const unavailable = 'openai.api@disabled';
+ const status = mockToolkitWithFallback(fallback);
+ providers.getAllProviders.mockResolvedValue({ providers: [] });
+ providers.getProviderById.mockImplementation(async id => ({ [primary.id]: primary, [fallback.id]: fallback }[id] ?? null));
+ setFallbackChainCache([{ providerId: unavailable }, { providerId: fallback.id }, { providerId: fallback.id }]);
+ status.getFallbackProvider.mockImplementation((failedId, candidates) => {
+ expect(failedId).toBe(primary.id);
+ expect(candidates).toEqual({ [primary.id]: primary, [fallback.id]: fallback });
+ return { provider: candidates[fallback.id], source: 'system' };
+ });
+ runner.executeApiRun
+ .mockImplementationOnce(async ({ onComplete }) => onComplete({ success: false, error: 'Primary unavailable' }))
+ .mockImplementationOnce(async ({ onData, onComplete }) => { onData('recovered'); onComplete({ success: true }); });
+
+ const result = await runPromptThroughProvider({ provider: primary, prompt: 'p', source: 'test' });
+ expect(result.text).toBe('recovered');
+ expect(result.usedFallback).toBe(true);
+ expect(providers.getProviderById.mock.calls.filter(([id]) => id === unavailable)).toHaveLength(1);
+ expect(providers.getProviderById.mock.calls.filter(([id]) => id === fallback.id)).toHaveLength(1); // duplicate chain entries reuse the materialized candidate
+ });
+
it('refuses a stopped caller after local concurrency wait without provider fallback, benching, or escalation', async () => {
const status = mockToolkitWithFallback();
const local = apiProvider({ id: 'local-guard', endpoint: 'http://localhost:11434/v1' });
diff --git a/server/services/socket.js b/server/services/socket.js
index 81ca1aa86d..c8a32f1bb2 100644
--- a/server/services/socket.js
+++ b/server/services/socket.js
@@ -66,7 +66,8 @@ import { getBuildId } from '../lib/buildId.js';
import { authEvents, isAuthEnabled, verifyRequestSession } from './auth.js';
import { HOST_CONTROL_FORBIDDEN_MESSAGE, socketHasHostControl } from './authGate.js';
import { runEventLogEvents } from './agentRunEventLog.js';
-import { armSystemActivityWatchers, bindSystemActivityIo } from './systemActivityNotify.js';
+import { bindSystemActivityIo } from './systemActivityNotify.js';
+import { armSystemActivityWatchers } from './systemActivityWatchers.js';
import { armReadinessWatchers, registerReadinessSocket } from './readinessNotify.js';
import { registerGeneratedPreviewHandlers } from './generatedPreview/sessions.js';
diff --git a/server/services/systemActivityNotify.js b/server/services/systemActivityNotify.js
index 7b1d070027..12696dda24 100644
--- a/server/services/systemActivityNotify.js
+++ b/server/services/systemActivityNotify.js
@@ -25,9 +25,6 @@ let timer = null;
let pending = null;
const notes = [];
const captureTestNotes = isTestRunner();
-let armed = false;
-let previousMind = null;
-const unsubs = [];
export function bindSystemActivityIo(next) {
io = next || null;
@@ -70,14 +67,6 @@ export function resetSystemActivityNotifyForTests() {
notes.length = 0;
seq = 0;
io = null;
- previousMind = null;
- for (const off of unsubs.splice(0)) off();
- armed = false;
-}
-
-function listen(emitter, event, handler) {
- emitter.on(event, handler);
- unsubs.push(() => emitter.off(event, handler));
}
export function mindActivityPhases(previous, next) {
@@ -114,17 +103,6 @@ export function taskQueuePhase(change) {
return null;
}
-const MEDIA_PHASE = {
- enqueued: 'queued',
- started: 'start',
- completed: 'completion',
- failed: 'failure',
- canceled: 'cancellation',
-};
-
-const MEDIA_TERMINAL = new Set(['completion', 'failure', 'cancellation']);
-const LIVE_MEDIA = new Set(['queued', 'running']);
-
/**
* Wrap the host's runner hooks so every LLM lifecycle edge invalidates the
* snapshot. Notes first: a throwing usage/error hook must not swallow the
@@ -152,46 +130,3 @@ export function attachLlmActivityHooks(hooks = {}) {
},
};
}
-
-export async function armSystemActivityWatchers() {
- if (armed) return;
- armed = true;
- let mediaJobEvents;
- let listJobs;
- let cosEvents;
- try {
- ({ mediaJobEvents, listJobs } = await import('./mediaJobQueue/index.js'));
- ({ cosEvents } = await import('./cosEvents.js'));
- } catch (err) {
- armed = false;
- throw err;
- }
-
- for (const [event, phase] of Object.entries(MEDIA_PHASE)) {
- listen(mediaJobEvents, event, () => {
- noteSystemActivity('media', phase);
- if (!MEDIA_TERMINAL.has(phase)) return;
- let live = true;
- try {
- live = listJobs().some((job) => LIVE_MEDIA.has(job?.status));
- } catch (err) {
- console.error(`❌ media activity drain check failed: ${err.message}`);
- return;
- }
- if (!live) noteSystemActivity('media', 'drained');
- });
- }
-
- listen(cosEvents, 'agent:spawned', () => noteSystemActivity('agents', 'start'));
- listen(cosEvents, 'agent:terminate', () => noteSystemActivity('agents', 'cancellation'));
- listen(cosEvents, 'agent:completed', (agent) => noteSystemActivity('agents', agentCompletionPhase(agent)));
- listen(cosEvents, 'tasks:changed', (change) => {
- const phase = taskQueuePhase(change);
- if (phase) noteSystemActivity('agents', phase);
- });
- listen(cosEvents, 'persistent-mind:status', (status) => {
- const phases = mindActivityPhases(previousMind, status);
- previousMind = status && typeof status === 'object' ? status : previousMind;
- for (const phase of phases) noteSystemActivity('mind', phase);
- });
-}
diff --git a/server/services/systemActivityNotify.test.js b/server/services/systemActivityNotify.test.js
index 8a846b406d..7a7da58352 100644
--- a/server/services/systemActivityNotify.test.js
+++ b/server/services/systemActivityNotify.test.js
@@ -1,3 +1,4 @@
+import { armSystemActivityWatchers } from './systemActivityWatchers.js';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { claimAppOperation, endAppOperation, __resetAppOperations } from './appOperations.js';
import { cosEvents } from './cosEvents.js';
@@ -5,7 +6,6 @@ import { mediaJobEvents } from './mediaJobQueue/index.js';
import {
SYSTEM_ACTIVITY_COALESCE_MS,
agentCompletionPhase,
- armSystemActivityWatchers,
attachLlmActivityHooks,
bindSystemActivityIo,
drainActivityNotesForTests,
@@ -15,13 +15,33 @@ import {
taskQueuePhase,
} from './systemActivityNotify.js';
+let stopWatchers;
+
afterEach(() => {
+ stopWatchers?.();
+ stopWatchers = null;
resetSystemActivityNotifyForTests();
__resetAppOperations();
vi.useRealTimers();
});
describe('system activity invalidation', () => {
+ it('detaches boot watchers and can arm them again without losing the shared signal', async () => {
+ const first = armSystemActivityWatchers();
+ const repeated = armSystemActivityWatchers();
+ expect(repeated).toBe(first);
+ stopWatchers = await first;
+ const previousStop = stopWatchers;
+ stopWatchers();
+ drainActivityNotesForTests();
+ cosEvents.emit('agent:spawned', { id: 'agent-before-rearm' });
+ expect(drainActivityNotesForTests()).toEqual([]);
+ stopWatchers = await armSystemActivityWatchers();
+ previousStop(); // A stale teardown must not detach the new registration.
+ cosEvents.emit('agent:spawned', { id: 'agent-after-rearm' });
+ expect(drainActivityNotesForTests()).toEqual([{ source: 'agents', phase: 'start' }]);
+ });
+
it('coalesces a burst into one frame that carries no snapshot', () => {
vi.useFakeTimers();
const emit = vi.fn();
@@ -63,7 +83,8 @@ describe('system activity invalidation', () => {
});
it('notifies start, completion, failure, cancellation, and queue drain for every event-backed source', async () => {
- await armSystemActivityWatchers();
+ stopWatchers = await armSystemActivityWatchers();
+ expect(await armSystemActivityWatchers()).toBe(stopWatchers);
drainActivityNotesForTests();
mediaJobEvents.emit('enqueued', { id: 'job-1' });
diff --git a/server/services/systemActivityWatchers.js b/server/services/systemActivityWatchers.js
new file mode 100644
index 0000000000..fe1cb4a315
--- /dev/null
+++ b/server/services/systemActivityWatchers.js
@@ -0,0 +1,70 @@
+/** Boot-only subscriptions; notification producers need no media execution graph. */
+import { noteSystemActivity, mindActivityPhases, agentCompletionPhase, taskQueuePhase } from './systemActivityNotify.js';
+
+let registration = null;
+
+const MEDIA_PHASE = {
+ enqueued: 'queued',
+ started: 'start',
+ completed: 'completion',
+ failed: 'failure',
+ canceled: 'cancellation',
+};
+
+const MEDIA_TERMINAL = new Set(['completion', 'failure', 'cancellation']);
+const LIVE_MEDIA = new Set(['queued', 'running']);
+
+/** Repeated callers share one registration and its idempotent teardown. */
+export function armSystemActivityWatchers() {
+ registration ??= attachWatchers().catch((error) => {
+ registration = null;
+ throw error;
+ });
+ return registration;
+}
+
+async function attachWatchers() {
+ const { mediaJobEvents, listJobs } = await import('./mediaJobQueue/index.js');
+ const { cosEvents } = await import('./cosEvents.js');
+ let previousMind = null;
+ const unsubs = [];
+ const listen = (emitter, event, handler) => {
+ emitter.on(event, handler);
+ unsubs.push(() => emitter.off(event, handler));
+ };
+
+ for (const [event, phase] of Object.entries(MEDIA_PHASE)) {
+ listen(mediaJobEvents, event, () => {
+ noteSystemActivity('media', phase);
+ if (!MEDIA_TERMINAL.has(phase)) return;
+ let live = true;
+ try {
+ live = listJobs().some((job) => LIVE_MEDIA.has(job?.status));
+ } catch (err) {
+ console.error(`❌ media activity drain check failed: ${err.message}`);
+ return;
+ }
+ if (!live) noteSystemActivity('media', 'drained');
+ });
+ }
+
+ listen(cosEvents, 'agent:spawned', () => noteSystemActivity('agents', 'start'));
+ listen(cosEvents, 'agent:terminate', () => noteSystemActivity('agents', 'cancellation'));
+ listen(cosEvents, 'agent:completed', (agent) => noteSystemActivity('agents', agentCompletionPhase(agent)));
+ listen(cosEvents, 'tasks:changed', (change) => {
+ const phase = taskQueuePhase(change);
+ if (phase) noteSystemActivity('agents', phase);
+ });
+ listen(cosEvents, 'persistent-mind:status', (status) => {
+ const phases = mindActivityPhases(previousMind, status);
+ previousMind = status && typeof status === 'object' ? status : previousMind;
+ for (const phase of phases) noteSystemActivity('mind', phase);
+ });
+ let stopped = false;
+ return () => {
+ if (stopped) return;
+ stopped = true;
+ for (const off of unsubs.splice(0)) off();
+ registration = null;
+ };
+}
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 6ffae858e0..6e0571f425 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -1559,7 +1559,7 @@ function activeIdsReader(activeIds) {
* can be removed out from under it. Resolves `remove()`'s result, or null when held.
*/
async function removeIfStillOrphaned({ isOwned, branchName, holderPath, sourceWorkspace, readOwners }, remove) {
- const { withClaimOwnershipLock } = await import('./cosClaimOwnership.js');
+ const { withClaimOwnershipLock } = await import('./cosClaimOwnershipState.js');
return withClaimOwnershipLock(async () => {
if (isOwned()) return null;
const agents = await readOwners();
@@ -1582,7 +1582,7 @@ export async function cleanupOrphanedWorktrees(sourceWorkspace, getActiveIds, {
if (!existsSync(WORKTREES_DIR)) return 0;
const readActiveIds = activeIdsReader(getActiveIds);
- const { readClaimOwnershipAgents } = await import('./cosClaimOwnership.js');
+ const { readClaimOwnershipAgents } = await import('./cosClaimOwnershipState.js');
const readOwners = claimOwnerReader(claimOwners, readClaimOwnershipAgents);
const worktrees = await listWorktrees(sourceWorkspace).catch(() => []);
let cleaned = 0;
@@ -1694,7 +1694,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {
dryRun = false
} = {}) {
const { getDefaultBranch, hasBranchMergeEvidence } = await import('./git.js');
- const { withClaimOwnershipLock, readClaimOwnershipAgents } = await import('./cosClaimOwnership.js');
+ const { withClaimOwnershipLock, readClaimOwnershipAgents } = await import('./cosClaimOwnershipState.js');
const { readBranchCompletionHold } = await import('./branchCompletionPolicy.js');
// Every entry point (scheduled, reconcile, operator cleanup) needs a trusted
// registry. loadState/getAgents may default a corrupt file to empty state.