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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
ci: report dynamic-aware test reach and cut two CoS-cluster import edges (#12285)
[._.]/ Adam Eivy1990a5a10fc6
diff --git a/scripts/ci-test-plan.js b/scripts/ci-test-plan.jsindex 68abb4fe96..0490334123 100644--- a/scripts/ci-test-plan.js+++ b/scripts/ci-test-plan.js@@ -10,6 +10,7 @@ import { writeStepOutput } from './lib/githubOutput.js';import { resolveDbTestFiles } from './lib/dbTestFiles.js';import { BROWSER_SUITES } from './lib/browserSuites.js';import { ATTESTABLE_STAGES, selectAttestation } from './lib/pregateAttestation.js';+import { formatReachSummary, measureRepoImportReach } from './lib/importReach.js';import { declaresPromptHashes, isNumberedMigrationFile } from './migrations/shippedPromptHistory.js';const TEST_FILE_RE = /\.(?:test|spec)\.[cm]?[jt]sx?$/i;@@ -958,9 +959,13 @@ const suiteReasonsFor = (plan, { appRouteOnly = false } = {}) => ({*/export function buildCiTestPlan(changedFiles, options = {}) {const plan = buildSuitePlan(changedFiles, options);- const { forceFull = false, promptHistoryInputs = null, linuxContainmentInputs = null } = options;+ const { forceFull = false, promptHistoryInputs = null, linuxContainmentInputs = null, serverReach = null } = options;+ // Informational: names the changed source behind a large related selection+ // (#12285). It never changes what runs.+ const reachNote = !plan.full && plan.server.mode === 'related' ? formatReachSummary(serverReach) : '';return {...plan,+ ...(reachNote ? { suiteReasons: { ...plan.suiteReasons, server: `${plan.suiteReasons.server}; ${reachNote}` } } : {}),promptHistory: forceFull || touchesJobInputs(plan.changedFiles, promptHistoryInputs, PROMPT_HISTORY_PATH_RE),linuxContainment: forceFull|| touchesJobInputs(plan.changedFiles, linuxContainmentInputs, LINUX_CONTAINMENT_PATH_RE),@@ -1598,12 +1603,30 @@ export function collectPlanInputs({ baseSha, forceFull = false, changedFiles, cw.filter((path) => DEPENDENCY_MANIFEST_RE.test(path)).map((path) => [path, classifyDependencyChange(path, baseSha ? readAtBase(path) : null, readInTree(path))]));+ // `vitest related` follows dynamic imports too; report that fan-out per+ // changed server source so the selection size is attributable. A failed+ // walk only loses the note.+ let serverReach = null;+ if (hasRelatedServerSource) {+ try {+ serverReach = measureRepoImportReach({+ cwd,+ files: dependencyFiles,+ sources: collectedChangedFiles.filter((path) => isServerRunnerFile(path)+ && MODULE_SOURCE_RE.test(path) && !isTestFile(path)),+ });+ } catch (err) {+ console.warn(`⚠️ Dynamic-aware reach not measured: ${err.message}`);+ }+ }+// A forced-full run always runs both gated jobs, so it skips the walks.const promptHistoryInputs = forceFull ? null : collectPromptHistoryInputs(dependencyFiles, cwd);const linuxContainmentInputs = forceFull ? null : collectLinuxContainmentInputs(cwd);return { changedFiles: collectedChangedFiles, trackedFiles: presentFiles, appDiff, pathContractTests,- clientDependencies, dbDependencies, browserDependencies, dependencyChanges, promptHistoryInputs, linuxContainmentInputs };+ clientDependencies, dbDependencies, browserDependencies, dependencyChanges, promptHistoryInputs, linuxContainmentInputs,+ serverReach };}// ---------------------------------------------------------------------------@@ -1871,7 +1894,7 @@ function main() {}const {changedFiles, trackedFiles, appDiff, pathContractTests, clientDependencies, dbDependencies, browserDependencies,- dependencyChanges, promptHistoryInputs, linuxContainmentInputs,+ dependencyChanges, promptHistoryInputs, linuxContainmentInputs, serverReach,} = collectPlanInputs({baseSha: base,forceFull,@@ -1889,6 +1912,7 @@ function main() {dependencyChanges,promptHistoryInputs,linuxContainmentInputs,+ serverReach,});// Last, over the finished plan: the attestation only ever removes work.emitGitHubPlan(applyAttestation(plan, resolveCiAttestation()));diff --git a/scripts/lib/importGrowth.js b/scripts/lib/importGrowth.jsindex fd81db2d2f..3ce7983331 100644--- a/scripts/lib/importGrowth.js+++ b/scripts/lib/importGrowth.js@@ -7,6 +7,7 @@* counted once per test that reaches them, because that is how the suite pays* import cost: every worker re-evaluates the closure. Dynamic `import()` is* invisible here, on purpose — the same rule `staticImportGraph.js` uses.+ * CI selection does follow it; `importReach.js` reports that view (#12285).** An absolute ceiling on the sum made ordinary growth fail. A new leaf on a* widely reached module, or a new suite's own closure, inched the repositorydiff --git a/scripts/lib/importReach.js b/scripts/lib/importReach.jsnew file mode 100644index 0000000000..c8436d68ab--- /dev/null+++ b/scripts/lib/importReach.js@@ -0,0 +1,180 @@+/**+ * Dynamic-aware import reach: how many test files `vitest related` selects for+ * a changed source (#12285).+ *+ * `importGrowth.js` measures static closures because that is what a worker+ * pays to instantiate. Selection is a different graph: Vitest's related walk+ * follows `transformed.dynamicDeps` as well, so a lazy `import()` that keeps a+ * subtree out of every static closure still links the two modules for CI+ * selection. A change anywhere inside a dynamically connected cluster selects+ * every suite that reaches the cluster. This module reports both numbers per+ * source so a plan's selection size is attributable.+ *+ * The scan is textual, like `staticImportGraph.js`: a literal `import('…')`+ * outside a comment-only line is an edge. A specifier inside a string or a+ * trailing comment can add an extra edge (reach over-reports, never under).+ * Computed specifiers are not edges — Vite cannot follow them either.+ *+ * Builtins only: `scripts/ci-test-plan.js` imports this before `npm ci`.+ */++import { execFileSync } from 'child_process';+import { existsSync, readFileSync } from 'fs';+import { join, posix } from 'path';+import { staticImportSpecifiersFromSource } from '../../server/lib/staticImportGraph.js';+import { isDirectlyInvoked } from './directInvocation.js';+import { globToRegExp, toWorkspacePath, workspaceTestGlobs } from './workspaceTests.js';++const MODULE_SOURCE_RE = /\.[cm]?[jt]sx?$/i;+const RESOLVE_EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts'];+const DYNAMIC_IMPORT_RE = /\bimport\s*\(\s*(['"`])([^'"`]+?)\1\s*[,)]/g;+const COMMENT_LINE_RE = /^\s*(?:\/\/|\/?\*)/;++/** `{ static, dynamic }` relative and bare specifiers, in source order. */+export function importSpecifiersFromSource(src) {+ const code = src.split('\n').filter((line) => !COMMENT_LINE_RE.test(line)).join('\n');+ const dynamic = [];+ for (const match of code.matchAll(DYNAMIC_IMPORT_RE)) {+ if (!match[2].includes('${')) dynamic.push(match[2]);+ }+ return { static: staticImportSpecifiersFromSource(code), dynamic };+}++function resolveSpecifier(fromRel, spec, known) {+ if (!spec.startsWith('.')) return null;+ const base = posix.normalize(posix.join(posix.dirname(fromRel), spec.split(/[?#]/)[0]));+ if (base === '..' || base.startsWith('../')) return null;+ const candidates = [base, ...RESOLVE_EXTENSIONS.map((ext) => `${base}${ext}`),+ ...RESOLVE_EXTENSIONS.map((ext) => `${base}/index${ext}`)];+ return candidates.find((candidate) => known.has(candidate)) ?? null;+}++/**+ * Resolved repo-relative edges for every module in `files`.+ * `readText(rel)` returns the source or null. An edge that is both static and+ * dynamic is recorded as static.+ *+ * @returns {Map<string, { static: Set<string>, dynamic: Set<string> }>}+ */+export function buildImportGraph(files, readText) {+ const known = new Set(files);+ const graph = new Map();+ for (const file of files) {+ if (!MODULE_SOURCE_RE.test(file)) continue;+ const text = readText(file);+ if (text == null) continue;+ const specs = importSpecifiersFromSource(text);+ const edges = { static: new Set(), dynamic: new Set() };+ for (const spec of specs.static) {+ const target = resolveSpecifier(file, spec, known);+ if (target && target !== file) edges.static.add(target);+ }+ for (const spec of specs.dynamic) {+ const target = resolveSpecifier(file, spec, known);+ if (target && target !== file && !edges.static.has(target)) edges.dynamic.add(target);+ }+ graph.set(file, edges);+ }+ return graph;+}++function reverseEdges(graph, dynamic) {+ const reverse = new Map();+ for (const [from, edges] of graph) {+ const targets = dynamic ? [...edges.static, ...edges.dynamic] : edges.static;+ for (const to of targets) {+ if (!reverse.has(to)) reverse.set(to, []);+ reverse.get(to).push(from);+ }+ }+ return reverse;+}++function reachFrom(reverse, source, isTest) {+ const seen = new Set([source]);+ const stack = [source];+ const tests = new Set();+ while (stack.length) {+ const file = stack.pop();+ if (isTest(file)) tests.add(file);+ for (const importer of reverse.get(file) ?? []) {+ if (seen.has(importer)) continue;+ seen.add(importer);+ stack.push(importer);+ }+ }+ return tests;+}++/**+ * Per-source reach: test files whose static closure contains the source, and+ * test files `vitest related` selects for it (static plus dynamic edges).+ * `union` is the dynamic-aware selection for all sources together.+ */+export function importReachReport(graph, sources, isTest) {+ const staticReverse = reverseEdges(graph, false);+ const dynamicReverse = reverseEdges(graph, true);+ const union = new Set();+ const rows = sources.map((source) => {+ const staticTests = reachFrom(staticReverse, source, isTest);+ const dynamicTests = reachFrom(dynamicReverse, source, isTest);+ for (const test of dynamicTests) union.add(test);+ return { source, staticReach: staticTests.size, dynamicReach: dynamicTests.size };+ });+ return { rows, selected: union.size };+}++/** Test files the server Vitest runner collects, as a repo-relative predicate. */+export function serverRunnerTestPredicate() {+ const { include, exclude } = workspaceTestGlobs('server', { env: {} });+ const includes = include.map(globToRegExp);+ const excludes = exclude.map(globToRegExp);+ return (repoRel) => {+ if (repoRel.startsWith('client/')) return false;+ const rel = toWorkspacePath('server', repoRel);+ return includes.some((re) => re.test(rel)) && !excludes.some((re) => re.test(rel));+ };+}++/**+ * One-line summary for the CI plan: the widest changed source first, so a+ * selection of hundreds of suites names the module that pulled them in.+ */+export function formatReachSummary(report, { limit = 3 } = {}) {+ if (!report || report.rows.length === 0) return '';+ const rows = [...report.rows].sort((a, b) => b.dynamicReach - a.dynamicReach || a.source.localeCompare(b.source));+ const shown = rows.slice(0, limit)+ .map((row) => `${row.source} ${row.dynamicReach} (static ${row.staticReach})`);+ const more = rows.length > limit ? `, +${rows.length - limit} more` : '';+ return `dynamic-aware fan-out ${report.selected} suite(s): ${shown.join(', ')}${more}`;+}++/** Tracked (plus given untracked) module files and a disk reader rooted at `cwd`. */+export function measureRepoImportReach({ cwd, files, sources, isTest = serverRunnerTestPredicate() }) {+ const paths = files ?? execFileSync('git', ['ls-files', '-z'], { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })+ .split('\0').filter(Boolean);+ const modules = paths.filter((rel) => MODULE_SOURCE_RE.test(rel) && !rel.includes('node_modules/'));+ const graph = buildImportGraph(modules, (rel) => {+ const abs = join(cwd, ...rel.split('/'));+ return existsSync(abs) ? readFileSync(abs, 'utf8') : null;+ });+ return importReachReport(graph, sources.filter((source) => graph.has(source)), isTest);+}++function main() {+ const sources = process.argv.slice(2);+ if (sources.length === 0) {+ console.error('Usage: node scripts/lib/importReach.js <repo-relative source> [...]');+ process.exit(2);+ }+ const report = measureRepoImportReach({ cwd: process.cwd(), sources });+ for (const source of sources) {+ const row = report.rows.find((candidate) => candidate.source === source);+ console.log(row+ ? `${source} static reach ${row.staticReach}, dynamic-aware reach ${row.dynamicReach}`+ : `${source} not a tracked module`);+ }+ console.log(`Selected together: ${report.selected} suite(s)`);+}++if (isDirectlyInvoked(import.meta.url)) main();diff --git a/scripts/lib/importReach.test.js b/scripts/lib/importReach.test.jsnew file mode 100644index 0000000000..79a22c19af--- /dev/null+++ b/scripts/lib/importReach.test.js@@ -0,0 +1,61 @@+/**+ * Dynamic-aware reach contract (#12285): a lazy `import()` is invisible to the+ * static proxy but links modules for `vitest related` selection, so the report+ * must count it — and the CI plan summary must show it without changing what+ * runs.+ */++import { describe, expect, it } from 'vitest';+import { buildImportGraph, formatReachSummary, importReachReport } from './importReach.js';+import { buildCiTestPlan } from '../ci-test-plan.js';++const SOURCES = new Map([+ ['svc/caller.js', [+ "import { leaf } from './leaf.js';",+ '// await import(\'./ghost.js\') in a comment is not an edge',+ "export const load = (name) => import(`./${name}.js`);",+ "export async function rare() { return (await import('./heavy.js')).run(); }",+ ].join('\n')],+ ['svc/heavy.js', "import { core } from './core.js';\nexport const run = () => core;\n"],+ ['svc/core.js', 'export const core = 1;\n'],+ ['svc/leaf.js', 'export const leaf = 1;\n'],+ ['svc/ghost.js', 'export const ghost = 1;\n'],+ ['svc/caller.test.js', "import { load } from './caller.js';\n"],+ ['svc/core.test.js', "import { core } from './core';\n"],+]);+const isTest = (rel) => rel.endsWith('.test.js');+const graph = buildImportGraph([...SOURCES.keys()], (rel) => SOURCES.get(rel) ?? null);++describe('importReachReport', () => {+ it('counts a dynamic edge for selection while the static view stops at it', () => {+ expect([...graph.get('svc/caller.js').dynamic]).toEqual(['svc/heavy.js']);+ const { rows, selected } = importReachReport(graph, ['svc/core.js', 'svc/heavy.js', 'svc/ghost.js'], isTest);+ expect(rows).toEqual([+ // core.test.js resolves `./core` without an extension, like Vite.+ { source: 'svc/core.js', staticReach: 1, dynamicReach: 2 },+ { source: 'svc/heavy.js', staticReach: 0, dynamicReach: 1 },+ // A comment-only line and a computed specifier add no edge.+ { source: 'svc/ghost.js', staticReach: 0, dynamicReach: 0 },+ ]);+ expect(selected).toBe(2);+ });+});++describe('CI plan summary', () => {+ const serverReach = importReachReport(graph, ['svc/core.js'], isTest);+ const trackedFiles = ['server/services/core.js', 'server/services/core.test.js', 'server/index.js'];++ it('appends the widest changed source to a related server plan', () => {+ expect(formatReachSummary(serverReach)).toBe('dynamic-aware fan-out 2 suite(s): svc/core.js 2 (static 1)');+ const plan = buildCiTestPlan(['server/services/core.js'], { trackedFiles, serverReach });+ expect(plan.server.mode).toBe('related');+ expect(plan.suiteReasons.server).toMatch(/; dynamic-aware fan-out 2 suite\(s\): svc\/core\.js 2 \(static 1\)$/);+ const without = buildCiTestPlan(['server/services/core.js'], { trackedFiles });+ expect(plan.server).toEqual(without.server);+ });++ it('leaves a full plan reason alone', () => {+ const plan = buildCiTestPlan(['server/services/core.js'], { trackedFiles, serverReach, forceFull: true });+ expect(plan.suiteReasons.server).not.toContain('dynamic-aware');+ });+});diff --git a/server/AGENTS.md b/server/AGENTS.mdindex f147921f95..e45e23ccc0 100644--- a/server/AGENTS.md+++ b/server/AGENTS.md@@ -38,6 +38,7 @@ These apply to Express/service/model code. Universal constraints (no try/catch o## Import scoping- **A widely-reached module must not drag a subtree it only needs a constant from.** The Linux CI server suite spends more wall time importing modules than running assertions, because import cost multiplies: every test file that transitively reaches module `X` re-instantiates all of `X`'s closure in its own worker. So one convenient barrel import inside a module 400 suites reach is worth thousands of module instantiations. `server/lib/importScoping.test.js` pins the narrowings made in #6009 (which cut the suite from ~115.5k static module instantiations to ~94.5k) as negative reachability assertions over `staticImportClosure`; add a row there when you narrow another one. The two shapes worth looking for: a constant re-exported through a heavy barrel (import it from the module that *declares* it — `CHECK_SCOPES` from `server/lib/editorial/checkInfra/taxonomy.js`, not from `editorial/checkRegistry.js` and its 13 check-definition modules), and a dependency only a boot/rare path uses (`await import()` it inside that path — `ensureSchemaImpl()` composes the DDL that way, keeping `lib/db/schema/**` out of the ~400 suites that reach `lib/db.js` through `pgFileFacade.js`).+- **A lazy `import()` cuts instantiation cost, not CI selection.** `vitest related` follows dynamic imports as well as static ones, so a module that lazily imports a heavy service still links every suite reaching it to every suite reaching that service. In 2026-10 one dynamically connected component around `services/cos.js` meant a change to any module in it selected ~1,130 server suites, although the static reach of `cos.js` was 90 (#12285). To cut selection, move the piece the caller actually needs into a leaf module both sides import (`services/readinessSignal.js`), or have the heavy service register a callback at boot that the caller looks up (`initAutoFixer()` → `services/providerRecoveryHooks.js`, read by `promptRunner.js`). Measure with `node scripts/lib/importReach.js <repo-relative source>`; the CI plan's server reason prints the same dynamic-aware fan-out for the changed sources. Cut edges are pinned in `SELECTION_CUTS` in `server/lib/importScoping.test.js`. A registry has the mock caveat in reverse: a suite that `vi.mock`ed the heavy module must register test doubles instead.- **In a non-async scope, defer with `.then()` rather than giving up.** The lazy shape is not limited to `async` functions: a `new Promise` executor or a fire-and-forget branch takes `import('./x.js').then(({ fn }) => fn(…)).catch(handler)`, which also routes an import failure into the error path the call already had. That is how the TUI branch in `services/promptRunner.js` defers `tuiPromptRunner.js` (and with it `node-pty`). Prefer putting the `import()` INSIDE an existing predicate gate when there is one — `runner.js` defers `ollamaAgentContext.js` inside the `isOllamaBackedProvider` ternary, so a cloud-provider run pays neither the hop nor the subtree.- **The tree-wide backstop is a base-relative growth guard, not an absolute ceiling.** `importScoping.test.js` compares every server `*.test.js` file's static import closure against the merge base, using one analyzer for both sides (`scripts/lib/importGrowth.js`). The count is a **proxy**: closure size summed across suites, shared modules counted once per suite that reaches them, not a count of runtime instantiations or a wall-clock. New test files are reported as new-suite contribution and do not spend the allowance, so ordinary additions cannot fail the guard just because the repository total grew. The gated number is the sum, over suites that exist on both sides, of how much each closure grew (shrinkage elsewhere does not cancel it). The limit is 2,500, above the old ceiling's ~1,500 ordinary-growth note and below half of the smaller heavy regression this file already records: #6009 removed 19,286 (115,519 → 96,233) and #6156 removed 12,794 (96,233 → 83,439). A failure names the largest suite increases and the largest newly reached dependency closures. Narrow the import, or defer it with `await import()` on the path that uses it. Raising `EXISTING_SUITE_GROWTH_LIMIT` is the intentional-regression path: do it in the same change, name the module that cannot be deferred, and keep the limit under that #6156 band — the pin test rejects a limit high enough to admit those edges. A missing or unreadable base fails the guard; it is not treated as zero growth. `benchmarkProxySplit` reports synthetic collection time and execution time separately and neither is a per-PR threshold. CI and `npm run pregate` run this comparison: the test is on `ALWAYS_RUN_TESTS`, and pregate forwards the same `CI_BASE_SHA` the planner used.- **Narrowing an import must not step around a barrel a suite mocks.** `vi.mock('./cos.js')` intercepts the specifier, not the function — so re-pointing a caller from `./cos.js` to the `./cosTaskStore.js` that declares `addTask` makes production code reach the REAL implementation while the suite still believes it is doubled. The failure lands in an unrelated test file with no obvious link to the import you changed. Before narrowing, grep the tree for `vi.mock` against the module you are leaving; if any suite that reaches the caller mocks it, leave the import on the barrel. A lazy `await import()` has no such hazard — vitest's mock registry covers dynamic imports too.diff --git a/server/lib/importScoping.test.js b/server/lib/importScoping.test.jsindex f225440c35..9f00907491 100644--- a/server/lib/importScoping.test.js+++ b/server/lib/importScoping.test.js@@ -26,7 +26,7 @@import { describe, it, expect } from 'vitest';import { readFileSync } from 'fs';-import { dirname, join, relative, sep } from 'path';+import { dirname, join, posix, relative, sep } from 'path';import { fileURLToPath } from 'url';import { staticImportClosure } from './staticImportGraph.js';import {@@ -35,6 +35,7 @@ import {measureClosures,measureWorkingTree,} from '../../scripts/lib/importGrowth.js';+import { importSpecifiersFromSource } from '../../scripts/lib/importReach.js';const SERVER_DIR = dirname(dirname(fileURLToPath(import.meta.url)));@@ -332,6 +333,39 @@ describe('deferred imports stay deferred (#6156)', () => {});});+// `vitest related` follows dynamic imports, so a lazy import() narrows the+// static closure above but not CI selection. Each row is an edge cut from the+// 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/pm2.js', 'services/readinessNotify.js',+ 'notifies readiness subscribers through the readinessSignal.js leaf'],+ ['services/promptRunner.js', 'services/autoFixer.js',+ 'reports fallback lifecycle through handlers initAutoFixer() registers in providerRecoveryHooks.js'],+];++const importTargets = (entry) => {+ const { static: eager, dynamic: lazy } = importSpecifiersFromSource(readFileSync(abs(entry), 'utf-8'));+ return [...eager, ...lazy].filter((spec) => spec.startsWith('.'))+ .map((spec) => posix.normalize(posix.join(posix.dirname(entry), spec)));+};++describe('CI-selection cuts stay cut (#12285)', () => {+ it.each(SELECTION_CUTS)('%s imports %s neither statically nor dynamically — it %s', (entry, target) => {+ expect(importTargets(entry)).not.toContain(target);+ });++ it('the leaves the cut entries import stay import-free', () => {+ expect(importTargets('services/readinessSignal.js')).toEqual([]);+ expect(importTargets('services/providerRecoveryHooks.js')).toEqual([]);+ });++ // Positive control: the scanner sees the observer's own lazy imports.+ it('still sees the dynamic edges readinessNotify.js keeps', () => {+ expect(importTargets('services/readinessNotify.js')).toContain('services/systemHealthSnapshot.js');+ });+});+/*** Hoisted test imports (#7951).*diff --git a/server/routes/messages.cleanup.test.js b/server/routes/messages.cleanup.test.jsindex 06076550ce..eae8128a1d 100644--- a/server/routes/messages.cleanup.test.js+++ b/server/routes/messages.cleanup.test.js@@ -19,7 +19,7 @@ vi.mock('fs/promises', async (importOriginal) => {const actual = await importOriginal();return { ...actual, unlink: vi.fn(actual.unlink) };});-vi.mock('../services/readinessNotify.js', () => ({ noteReadinessChanged: vi.fn() }));+vi.mock('../services/readinessSignal.js', () => ({ noteReadinessChanged: vi.fn() }));vi.mock('../services/messageEvaluator.js', () => ({ evaluateMessages: vi.fn(), generateReplyBody: vi.fn() }));vi.mock('../services/messageSender.js', () => ({ sendDraft: vi.fn() }));vi.mock('../services/messagePlaywrightSync.js', () => ({diff --git a/server/services/autoFixer.js b/server/services/autoFixer.jsindex 0cfbc5cc54..83927f8ff6 100644--- a/server/services/autoFixer.js+++ b/server/services/autoFixer.js@@ -11,6 +11,7 @@ import { fileInvestigationTask, __resetInvestigationCircuit } from './investigatimport { investigationFingerprint } from '../lib/investigationTasks.js';import { errorEvents } from '../lib/errorHandler.js';import { ERROR_CATEGORIES } from '../lib/aiToolkit/errorDetection.js';+import { registerProviderRecoveryHandlers } from './providerRecoveryHooks.js';// Track recent errors to prevent duplicate auto-fix tasksconst recentErrors = new Map();@@ -404,6 +405,9 @@ export function initAutoFixer() {if (autoFixerInitialized) return;autoFixerInitialized = true;+ // promptRunner.js reaches these through the registry, not an import (#12285).+ registerProviderRecoveryHandlers({ noteFallbackStarted, noteFallbackFailed, noteFallbackHandled, escalateProviderFailure });+errorEvents.on('error', (error) => {(async () => {// Always handle AI provider errors (even if CoS not running)diff --git a/server/services/bootstrap.js b/server/services/bootstrap.jsindex f4ad28a7df..57c2f30a69 100644--- a/server/services/bootstrap.js+++ b/server/services/bootstrap.js@@ -1,7 +1,7 @@import { withBackupAssetPublication } from '../lib/backupSnapshotBoundary.js';import { bindMaintenanceIo } from './maintenanceControl.js';import { maintenance } from '../lib/maintenanceAdmission.js';-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';/*** Server boot orchestration.*diff --git a/server/services/calendarAccounts.js b/server/services/calendarAccounts.jsindex e9c728534b..45d6549e92 100644--- a/server/services/calendarAccounts.js+++ b/server/services/calendarAccounts.js@@ -1,4 +1,4 @@-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';import { join } from 'path';import { v4 as uuidv4 } from '../lib/uuid.js';import { ensureDir, PATHS, readJSONFile, atomicWrite } from '../lib/fileUtils.js';diff --git a/server/services/genome.js b/server/services/genome.jsindex 83cbb9008e..9fb0e52523 100644--- a/server/services/genome.js+++ b/server/services/genome.js@@ -1,4 +1,4 @@-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';import { join } from 'path';import { randomUUID } from 'crypto';import { atomicWrite, PATHS, ensureDir, safeJSONParse, tryReadFile, unlinkGuarded } from '../lib/fileUtils.js';diff --git a/server/services/messageAccounts.js b/server/services/messageAccounts.jsindex e3cd54015f..e87dcc7c9d 100644--- a/server/services/messageAccounts.js+++ b/server/services/messageAccounts.js@@ -1,4 +1,4 @@-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';import { join } from 'path';import { v4 as uuidv4 } from '../lib/uuid.js';import { ensureDir, PATHS, readJSONFile, atomicWrite } from '../lib/fileUtils.js';diff --git a/server/services/messageLogging.test.js b/server/services/messageLogging.test.jsindex 9790ed67b5..d3113971cb 100644--- a/server/services/messageLogging.test.js+++ b/server/services/messageLogging.test.js@@ -28,7 +28,7 @@ vi.mock('../lib/fileUtils.js', async (original) => ({tryReadFile: async path => doubles.files.get(path) ?? null,atomicWrite: async (path, value) => { doubles.files.set(path, JSON.stringify(value)); }}));-vi.mock('./readinessNotify.js', () => ({ noteReadinessChanged: () => {} }));+vi.mock('./readinessSignal.js', () => ({ noteReadinessChanged: () => {} }));vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({ getAccessToken: async () => ({ token: 'synthetic-token' }) }), GOOGLE_API_TIMEOUT_MS: 30_000 }));vi.mock('./gmailApi.js', () => ({createGmailApi: () => ({diff --git a/server/services/pm2.js b/server/services/pm2.jsindex c268540020..265cfec065 100644--- a/server/services/pm2.js+++ b/server/services/pm2.js@@ -9,6 +9,9 @@ import { atomicWrite, safeJSONParse, sleep, tryReadFile } from '../lib/fileUtilsimport { parseCommandArgs } from '../lib/commandSecurity.js';import { parsePm2JlistStdout } from '../lib/pm2Jlist.js';import { PATHS } from '../lib/paths.js';+// The leaf, not readinessNotify.js: its observer's lazy imports would join+// pm2.js's ~280 suites to the CoS cluster's CI selection (#12285).+import { noteReadinessChanged } from './readinessSignal.js';const IS_WIN = process.platform === 'win32';@@ -23,22 +26,14 @@ const cacheKey = (pm2Home) => pm2Home || '_default';// Track PM2 read failures per home so we log only on state change or reason changeconst jlistFailureState = new Map(); // home -> { lastError: string }-let readinessChangedLoader;--function notifyReadinessChanged(kind) {- readinessChangedLoader ??= import('./readinessNotify.js').then(({ noteReadinessChanged }) => noteReadinessChanged);- void readinessChangedLoader- .then(noteReadinessChanged => noteReadinessChanged(kind))- .catch(error => console.error(`❌ Readiness invalidation failed: ${error.message}`));-}/*** Invalidate the jlist TTL cache (e.g. after mutations like start/stop/delete).* @param {string|null} [pm2Home=null]*/export function clearJlistCache(pm2Home = null) {- notifyReadinessChanged('health');- notifyReadinessChanged('capabilities');+ noteReadinessChanged('health');+ noteReadinessChanged('capabilities');if (pm2Home !== undefined && pm2Home !== null) {jlistCache.delete(cacheKey(pm2Home));} else {diff --git a/server/services/promptRunner.js b/server/services/promptRunner.jsindex eff890daff..1bb7027fd7 100644--- a/server/services/promptRunner.js+++ b/server/services/promptRunner.js@@ -55,24 +55,12 @@ import { allowedModesFor, callerModeRejection } from '../lib/callerModePolicy.jsimport { attachGatewaySiblingKey } from '../lib/providerGateways.js';// The fallback-lifecycle notifiers live in services/autoFixer.js, which-// transitively pulls in services/cos.js (PM2 + fs + sockets). Importing it-// lazily on the failure path keeps anything that imports promptRunner.js from-// dragging the CoS stack along on the happy path. Node caches the module after-// the first dynamic import, so the cost is one-time.-let autoFixerLoadPromise;-function loadAutoFixer() {- if (!autoFixerLoadPromise) {- autoFixerLoadPromise = import('./autoFixer.js').catch((err) => {- // Keep a failed optional load retryable for a later failure after a- // transient module-resolution problem.- autoFixerLoadPromise = null;- throw err;- });- }- return autoFixerLoadPromise;-}+// pulls in services/cos.js. initAutoFixer() registers them through this leaf;+// neither a static nor a lazy import of autoFixer.js belongs here, because+// `vitest related` follows both into the CoS cluster (#12285).+import { getProviderRecoveryHandlers } from './providerRecoveryHooks.js';-// Deferred for the same reason as loadAutoFixer above: `observedContextWindows.js`+// Deferred so the static closure stays small: `observedContextWindows.js`// reaches `lib/localProviderRuntime.js` and the endpoint probe, which// promptRunner.js itself never needs, and ~160 suites only build or classify a// run. A static edge instantiated that subtree in every one of them and pushed@@ -553,22 +541,9 @@ export function assertVisionRunUsedImages(result, requestedProvider) {/** Own deferred investigations for one retry cascade, independently of retry policy. */function createRecoveryInvestigationScope({ failed, failedModel, firstError }) {- // Load the fallback-lifecycle notifiers lazily and once for the whole- // cascade. Lazy (see loadAutoFixer) so the happy path never drags in the- // CoS stack — and so a failure with NO recovery path (no tier applies)- // never loads it either, since none of the note* helpers below fire.- let autoFixerModule;- let autoFixerLoaded = false;- const getAutoFixer = async () => {- if (!autoFixerLoaded) {- autoFixerLoaded = true;- autoFixerModule = await loadAutoFixer().catch((err) => {- console.error(`❌ autoFixer load failed (investigation task not suppressed): ${err.message}`);- return null;- });- }- return autoFixerModule;- };+ // Handlers registered by initAutoFixer(); null when the auto-fixer is not+ // running in this process, in which case it has no deferred tasks to settle.+ const getAutoFixer = async () => getProviderRecoveryHandlers();// Keys must match what bootstrap.js's onRunFailed hook published// (metadata.providerName + metadata.model). Best-effort throughout: a// notifier failure must never turn a working retry into a user-visible error.diff --git a/server/services/promptRunner.test.js b/server/services/promptRunner.test.jsindex 19596d85e4..95e30ee81e 100644--- a/server/services/promptRunner.test.js+++ b/server/services/promptRunner.test.js@@ -49,16 +49,15 @@ vi.mock('./providers.js', () => ({getAllProviders: vi.fn().mockResolvedValue({ activeProvider: null, providers: [] }),}));-// autoFixer.js is called from runPromptThroughProvider on a successful-// fallback retry to cancel the deferred investigation task. Mock the export-// to a spy so tests can assert it was/wasn't invoked without wiring up the-// full task system.-vi.mock('./autoFixer.js', () => ({+// initAutoFixer() registers these at boot; runPromptThroughProvider reports+// fallback lifecycle through the registry. Spies stand in for the auto-fixer+// so tests can assert it was/wasn't invoked without wiring up the task system.+const autoFixer = {noteFallbackStarted: vi.fn(),noteFallbackHandled: vi.fn(),noteFallbackFailed: vi.fn(),escalateProviderFailure: vi.fn().mockResolvedValue(undefined),-}));+};// aiToolkitState lookups gate the retry path on a real providerStatus// service being present. By default the mock returns null so the retry@@ -81,7 +80,7 @@ const runner = await import('./runner.js');const tuiRunner = await import('./tuiPromptRunner.js');const executionReadiness = await import('./providerExecutionReadiness.js');const providers = await import('./providers.js');-const autoFixer = await import('./autoFixer.js');+(await import('./providerRecoveryHooks.js')).registerProviderRecoveryHandlers(autoFixer);const toolkitState = await import('../lib/aiToolkitState.js');const observedWindows = await import('./observedContextWindows.js');const { ERROR_CATEGORIES } = await import('../lib/aiToolkit/errorDetection.js');diff --git a/server/services/providerRecoveryHooks.js b/server/services/providerRecoveryHooks.jsnew file mode 100644index 0000000000..af05654105--- /dev/null+++ b/server/services/providerRecoveryHooks.js@@ -0,0 +1,29 @@+/**+ * Provider-fallback recovery handlers, registered at boot by `initAutoFixer()`.+ *+ * `promptRunner.js` tells the auto-fixer when a fallback cascade starts,+ * recovers, gives up or must escalate. It used to `import('./autoFixer.js')`+ * for that, which kept the CoS stack out of its static closure but not out of+ * CI selection: `vitest related` follows lazy imports, so every suite reaching+ * promptRunner.js joined the CoS cluster's selection (#12285). This leaf+ * breaks that edge. With nothing registered (a process or suite that never+ * initialized the auto-fixer) the notifications are skipped, which is also+ * what the auto-fixer's own deferred tasks need: none exist without it.+ */+let handlers = null;++/**+ * @param {{ noteFallbackStarted: Function, noteFallbackFailed: Function,+ * noteFallbackHandled: Function, escalateProviderFailure: Function }} next+ * @returns {() => void} unregister (only if still the registered set)+ */+export function registerProviderRecoveryHandlers(next) {+ handlers = next;+ return () => {+ if (handlers === next) handlers = null;+ };+}++export function getProviderRecoveryHandlers() {+ return handlers;+}diff --git a/server/services/readinessNotify.js b/server/services/readinessNotify.jsindex a453e745bc..86de6292b3 100644--- a/server/services/readinessNotify.js+++ b/server/services/readinessNotify.js@@ -12,10 +12,12 @@* Tailscale/cert state. Shared snapshot readers keep observer and HTTP failure* semantics identical; timestamps alone never trigger an invalidation.*/-const subscribers = new Set();+import { cancelPendingReadiness, noteReadinessChanged, readinessSubscribers as subscribers } from './readinessSignal.js';++// Writers import the leaf directly; these re-exports keep the observer's API whole.+export { noteReadinessChanged, READINESS_COALESCE_MS } from './readinessSignal.js';+const previous = new Map();-const pendingDomains = new Set();-let flushTimer = null;let observerTimer = null;let observing = false;let generation = 0;@@ -25,9 +27,7 @@ const READERS = {health: () => import('./systemHealthSnapshot.js').then(m => m.getSystemHealthSnapshot()),capabilities: () => import('./capabilitiesSnapshot.js').then(m => m.getCapabilitiesSnapshot()),};-const EVENTS = { health: 'system:health:changed', capabilities: 'capabilities:changed' };export const READINESS_OBSERVE_MS = 20_000;-export const READINESS_COALESCE_MS = 100;// Clock fields must not turn an unchanged observer sample into an invalidation.// Resource usage is real telemetry and remains part of health change detection.@@ -36,26 +36,6 @@ function signature(snapshot) {['timestamp', 'checkedAt', 'responseTime', 'uptime', 'uptimeFormatted'].includes(key) ? undefined : value);}-export function noteReadinessChanged(domain = 'capabilities') {- if (!EVENTS[domain] || !subscribers.size) return;- pendingDomains.add(domain);- if (flushTimer) return;- flushTimer = setTimeout(() => {- flushTimer = null;- const domains = [...pendingDomains];- pendingDomains.clear();- for (const socket of subscribers) {- if (!socket.connected) continue;- try {- for (const name of domains) socket.emit(EVENTS[name], {});- } catch (err) {- console.error(`❌ Readiness notification failed: ${err.message}`);- }- }- }, READINESS_COALESCE_MS);- flushTimer.unref?.();-}-async function observe() {if (observing || !subscribers.size) return;observing = true;@@ -86,10 +66,8 @@ function scheduleObservation() {function stopObservation() {generation += 1;clearTimeout(observerTimer);- clearTimeout(flushTimer);observerTimer = null;- flushTimer = null;- pendingDomains.clear();+ cancelPendingReadiness();previous.clear();}diff --git a/server/services/readinessSignal.js b/server/services/readinessSignal.jsnew file mode 100644index 0000000000..c5c372e974--- /dev/null+++ b/server/services/readinessSignal.js@@ -0,0 +1,43 @@+/**+ * Readiness invalidation fan-out: the subscriber set and the coalesced emit.+ *+ * A leaf on purpose (#12285). Writers (PM2 mutations, account services,+ * bootstrap) import this instead of `readinessNotify.js`, whose observer+ * lazily imports the health/capabilities snapshot readers and the service+ * buses. `vitest related` follows those lazy imports, so a writer importing+ * the observer joined every suite that reaches it to the CoS cluster's CI+ * selection. Only `readinessNotify.js` adds subscribers.+ */+export const READINESS_COALESCE_MS = 100;+const EVENTS = { health: 'system:health:changed', capabilities: 'capabilities:changed' };++export const readinessSubscribers = new Set();+const pendingDomains = new Set();+let flushTimer = null;++export function noteReadinessChanged(domain = 'capabilities') {+ if (!EVENTS[domain] || !readinessSubscribers.size) return;+ pendingDomains.add(domain);+ if (flushTimer) return;+ flushTimer = setTimeout(() => {+ flushTimer = null;+ const domains = [...pendingDomains];+ pendingDomains.clear();+ for (const socket of readinessSubscribers) {+ if (!socket.connected) continue;+ try {+ for (const name of domains) socket.emit(EVENTS[name], {});+ } catch (err) {+ console.error(`❌ Readiness notification failed: ${err.message}`);+ }+ }+ }, READINESS_COALESCE_MS);+ flushTimer.unref?.();+}++/** Drop a pending coalesced emit (the last subscriber left). */+export function cancelPendingReadiness() {+ clearTimeout(flushTimer);+ flushTimer = null;+ pendingDomains.clear();+}diff --git a/server/services/telegram.js b/server/services/telegram.jsindex 9d3645a9da..71fdc04ae1 100644--- a/server/services/telegram.js+++ b/server/services/telegram.js@@ -1,4 +1,4 @@-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';/*** Telegram Bot Service*diff --git a/server/services/telegramBridge.js b/server/services/telegramBridge.jsindex 3784c3d69a..3c23a354a0 100644--- a/server/services/telegramBridge.js+++ b/server/services/telegramBridge.js@@ -1,4 +1,4 @@-import { noteReadinessChanged } from './readinessNotify.js';+import { noteReadinessChanged } from './readinessSignal.js';/*** Telegram MCP Bridge Service*