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: make seriesAutopilot's __testing bundle cycle-safe with lazy getters

[._.]/ Adam Eivyc0027c72fdae
diff --git a/server/services/pipeline/seriesAutopilot.js b/server/services/pipeline/seriesAutopilot.js
index 16bd4eb8f9..893e4c5c98 100644
--- a/server/services/pipeline/seriesAutopilot.js
+++ b/server/services/pipeline/seriesAutopilot.js
@@ -87,10 +87,26 @@ export * from './seriesAutopilot/orchestrator.js';
// Export internals for tests. Pulled back together from their new home modules
// so the existing `__testing` import contract survives the #2842 split.
-import { runs } from './seriesAutopilot/state.js';
-import { summarizePlanCost } from './seriesAutopilot/convergence.js';
-import { providerOverrideOpts, providerIdOpts } from './seriesAutopilot/session.js';
-import { meanQualityScore } from './seriesAutopilot/revisionSteps.js';
-import { buildDryRunPlan } from './seriesAutopilot/dryRun.js';
+//
+// The properties MUST be lazy getters, not eagerly-read values. `session.js`
+// reaches this barrel through a real import cycle
+// (session → autoRunner → episodeVideo → completionHook → planAdvance →
+// seriesAutopilot), so evaluating `providerOverrideOpts` at module-evaluation
+// time throws a TDZ ReferenceError on any cold import of a focused module. The
+// getters defer each read until a test actually touches the key, by which point
+// every binding is initialized. Behavior for callers is unchanged — `__testing`
+// is still a plain object whose keys resolve to the same functions.
+import * as state from './seriesAutopilot/state.js';
+import * as convergence from './seriesAutopilot/convergence.js';
+import * as session from './seriesAutopilot/session.js';
+import * as revisionSteps from './seriesAutopilot/revisionSteps.js';
+import * as dryRun from './seriesAutopilot/dryRun.js';
-export const __testing = { runs, buildDryRunPlan, summarizePlanCost, providerOverrideOpts, providerIdOpts, meanQualityScore };
+export const __testing = {
+ get runs() { return state.runs; },
+ get buildDryRunPlan() { return dryRun.buildDryRunPlan; },
+ get summarizePlanCost() { return convergence.summarizePlanCost; },
+ get providerOverrideOpts() { return session.providerOverrideOpts; },
+ get providerIdOpts() { return session.providerIdOpts; },
+ get meanQualityScore() { return revisionSteps.meanQualityScore; },
+};
diff --git a/server/services/pipeline/seriesAutopilotColdImport.test.js b/server/services/pipeline/seriesAutopilotColdImport.test.js
new file mode 100644
index 0000000000..506472117a
--- /dev/null
+++ b/server/services/pipeline/seriesAutopilotColdImport.test.js
@@ -0,0 +1,31 @@
+import { describe, it, expect, vi } from 'vitest';
+import { mockNoPeerSync, mockNoPeers } from '../../lib/mockPathsDataRoot.js';
+
+vi.mock('../instances.js', () => mockNoPeers());
+vi.mock('../sharing/peerSync.js', () => mockNoPeerSync());
+
+// COLD import: this file deliberately imports a FOCUSED module from the #2842
+// split WITHOUT importing ./seriesAutopilot.js first — the barrel test can't
+// catch this because importing the barrel initializes every binding up front.
+//
+// `session.js` reaches the barrel through a genuine import cycle
+// (session → autoRunner → episodeVideo → completionHook → planAdvance →
+// seriesAutopilot), so anything the barrel READS at module-evaluation time from
+// a not-yet-initialized module throws `ReferenceError: Cannot access 'x' before
+// initialization`. That is exactly what an eager `__testing = { …, providerOverrideOpts }`
+// did; the barrel now uses lazy getters. Keep this file's first autopilot import
+// pointed at a focused module or the guard silently stops guarding.
+const session = await import('./seriesAutopilot/session.js');
+
+describe('seriesAutopilot cold module import (issue #2842)', () => {
+ it('a focused module imports standalone without a TDZ error from the barrel cycle', () => {
+ expect(typeof session.providerOverrideOpts).toBe('function');
+ expect(typeof session.broadcast).toBe('function');
+ });
+
+ it('the barrel imported afterwards still exposes the __testing bundle', async () => {
+ const barrel = await import('./seriesAutopilot.js');
+ expect(barrel.__testing.providerOverrideOpts).toBe(session.providerOverrideOpts);
+ expect(barrel.__testing.runs).toBeInstanceOf(Map);
+ });
+});