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

perf: keep the jev federation guard off the media-library import edge (#7689)

[._.]/ Adam Eivy6618366dbd10
diff --git a/server/services/jevTraining.js b/server/services/jevTraining.js
index 63354c0b83..44d827d5b4 100644
--- a/server/services/jevTraining.js
+++ b/server/services/jevTraining.js
@@ -31,15 +31,23 @@ import { findCachedRepoFiles } from '../lib/hfCache.js';
import { JEV_MODEL, JEV_REQUIRED_FILES } from '../lib/jev.js';
import { jevDecisionEmbeddingsDir } from '../lib/jevPaths.js';
import { SCOPE_ADHERENCE_DECISION_ID } from '../lib/scopeAdherence.js';
-// The trainer runs under the SAME interpreter AND the SAME hardened spawn
-// options the sidecar runs under, resolved as ONE value from the module that
-// owns both. Recomposing the interpreter and the environment here is how the
-// "offline, no provider call" guarantee drifts apart from the process it is
-// supposed to describe.
-import { getJevStatus, jevVenvSpawnTarget } from './jev.js';
-import { buildScopeAdherenceCorpus } from './jevCorpusBuilder.js';
import { saveCandidateJevHead } from './jevHeads.js';
+// Deferred into `runTraining`, not imported at module scope: `./jev.js` carries
+// the whole sidecar lifecycle (the pinned model contract, the venv installer,
+// the HF cache and download stack) and `./jevCorpusBuilder.js` carries the forge
+// reads. An install that never trains a head must not pay for either in its
+// static import closure (`server/lib/importScoping.test.js`), and a training run
+// pays a single dynamic hop against a job measured in minutes. Vitest's mock
+// registry covers dynamic imports, so the suites that double these still do.
+//
+// What the deferral must NOT lose: the trainer runs under the SAME interpreter
+// AND the SAME hardened spawn options the sidecar runs under, resolved as ONE
+// value from the module that owns both. Recomposing either here is how the
+// "offline, no provider call" guarantee drifts from the process it describes.
+const jevLifecycle = () => import('./jev.js');
+const corpusBuilder = () => import('./jevCorpusBuilder.js');
+
const execFileAsync = promisify(execFile);
const TRAINER_SCRIPT = join(PATHS.root, 'scripts', 'train_jev_head.py');
// Generous: the dominant cost is one forward pass per (clause, change) pair on
@@ -78,6 +86,7 @@ export function trainScopeAdherenceHead({ repoPath = PATHS.root, architecture =
}
async function runTraining({ repoPath, architecture }) {
+ const { getJevStatus, jevVenvSpawnTarget } = await jevLifecycle();
const status = await getJevStatus();
// The trainer loads the same snapshot through the same venv the sidecar
// uses. An install that has not finished the scorer install cannot train,
@@ -96,6 +105,7 @@ async function runTraining({ repoPath, architecture }) {
// no other use for.
const { resolveForgeForRepo } = await import('./forgeAuth.js');
const forge = await resolveForgeForRepo(repoPath).catch(() => null);
+ const { buildScopeAdherenceCorpus } = await corpusBuilder();
const corpus = await buildScopeAdherenceCorpus({ repoPath, env: forge?.env || null });
if (!corpus.ok) return corpus;
diff --git a/server/services/sharing/jevNeverFederates.test.js b/server/services/sharing/jevNeverFederates.test.js
index d427977f03..6db87f0cd4 100644
--- a/server/services/sharing/jevNeverFederates.test.js
+++ b/server/services/sharing/jevNeverFederates.test.js
@@ -24,7 +24,6 @@ import { describe, it, expect } from 'vitest';
import { basename } from 'path';
import { PEER_SUBSCRIBABLE_KINDS } from './peerSyncShared.js';
import { NON_RECORD_SCHEMA_CATEGORIES, PORTOS_SCHEMA_VERSIONS } from '../../lib/schemaVersions.js';
-import { mediaLibraryDirs } from './peerMediaLibrarySync.js';
// `jev` alone would miss a kind called `trainedHead`; `head` alone would fire
// on an unrelated `headshot`. Both, anchored on a word-ish boundary.
@@ -65,12 +64,20 @@ describe('trained jev heads and their corpora never federate (#7689)', () => {
// The media-library manifest is the OTHER way `data/` bytes reach a peer: it
// walks whole directories rather than record kinds, so no schema-version or
// subscription guard above covers it.
- it('carries no jev directory into the media-library federation walk', () => {
+ //
+ // Imported INSIDE the body, like the dataSync check above: `peerMediaLibrarySync.js`
+ // drags ~400 modules, and this file would otherwise cost the whole server
+ // suite's import budget that much for one assertion (`lib/importScoping.test.js`).
+ // `beeperNeverFederates.test.js` pays it statically because Beeper attachments
+ // really are a media-shaped directory; `data/jev/` is a weights cache that
+ // could never be one, so the edge is not worth the budget.
+ it('carries no jev directory into the media-library federation walk', async () => {
+ const { mediaLibraryDirs } = await import('./peerMediaLibrarySync.js');
const dirs = mediaLibraryDirs();
// Non-vacuity pin: image, video, audio, music.
expect(dirs).toHaveLength(4);
expect(dirs.filter(namesJevMedia)).toEqual([]);
- });
+ }, 30000);
// The BACKUP tier, which is the other decision `data/jev/` needed making
// explicitly. Corpora and cached embeddings are regenerable bulk and are