π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(autopilot): load agentic-flow autopilot-learning past its exports map (#4146)
rc49665ce2bb1
diff --git a/v3/@claude-flow/cli/__tests__/autopilot-learning-load-4146.test.ts b/v3/@claude-flow/cli/__tests__/autopilot-learning-load-4146.test.tsnew file mode 100644index 0000000000..65f6b1f2c5--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/autopilot-learning-load-4146.test.ts@@ -0,0 +1,24 @@+import { describe, expect, it } from 'vitest';+import { createRequire } from 'node:module';+import { spawnSync } from 'node:child_process';+import { getLearningLoadError, tryLoadLearning } from '../src/autopilot-state.js';++describe('autopilot learning loads past the agentic-flow exports map (#4146)', () => {+ it('the bare subpath really is blocked (the bug precondition)', () => {+ const r = spawnSync(process.execPath, ['--input-type=module', '-e', "import('agentic-flow/dist/coordination/autopilot-learning.js').catch(e => console.log(e.code))"], { cwd: process.cwd(), encoding: 'utf8' });+ expect(r.stdout.trim()).toBe('ERR_PACKAGE_PATH_NOT_EXPORTED');+ });++ it('tryLoadLearning no longer fails with ERR_PACKAGE_PATH_NOT_EXPORTED', async () => {+ const learning = await tryLoadLearning();+ const err = getLearningLoadError();+ expect(err ?? '').not.toMatch(/ERR_PACKAGE_PATH_NOT_EXPORTED|not defined by "exports"/);+ // Either it loaded, or it failed for the real reason (AgentDB init) and says so.+ if (!learning) expect(err).toMatch(/AgentDB|initialize|autopilot/i);+ else expect(getLearningLoadError()).toBeNull();+ });++ it('agentic-flow resolves through its exported entry (what the loader relies on)', () => {+ expect(() => createRequire(import.meta.url).resolve('agentic-flow')).not.toThrow();+ });+});diff --git a/v3/@claude-flow/cli/src/autopilot-state.ts b/v3/@claude-flow/cli/src/autopilot-state.tsindex d9ec7e6924..f5ce12f868 100644--- a/v3/@claude-flow/cli/src/autopilot-state.ts+++ b/v3/@claude-flow/cli/src/autopilot-state.ts@@ -10,7 +10,9 @@import { randomUUID } from 'node:crypto';import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, renameSync } from 'node:fs';-import { resolve, join } from 'node:path';+import { resolve, join, dirname } from 'node:path';+import { createRequire } from 'node:module';+import { pathToFileURL } from 'node:url';import { homedir } from 'node:os';import { writeFileAtomic } from './fs-secure.js';@@ -342,14 +344,43 @@ export function calculateReward(iterations: number, durationMs: number): number// ββ Learning Integration ββββββββββββββββββββββββββββββββββββββ+let lastLearningLoadError: string | null = null;++/** Why the last tryLoadLearning() returned null (null when it succeeded or was never called). */+export function getLearningLoadError(): string | null {+ return lastLearningLoadError;+}++/**+ * agentic-flow's `exports` map does not expose dist/coordination/*, so a bare+ * specifier import throws ERR_PACKAGE_PATH_NOT_EXPORTED (#4146). Resolve the+ * package root through its exported entry point and import the file by URL.+ */+function resolveAutopilotLearningUrl(): string {+ const req = createRequire(import.meta.url);+ let dir = dirname(req.resolve('agentic-flow'));+ for (let i = 0; i < 6; i++) {+ if (existsSync(join(dir, 'package.json')) && existsSync(join(dir, 'dist', 'coordination', 'autopilot-learning.js'))) {+ return pathToFileURL(join(dir, 'dist', 'coordination', 'autopilot-learning.js')).href;+ }+ dir = dirname(dir);+ }+ throw new Error('agentic-flow does not ship dist/coordination/autopilot-learning.js');+}+export async function tryLoadLearning(): Promise<{ initialize: () => Promise<boolean>; [key: string]: unknown } | null> {+ lastLearningLoadError = null;try {- const modPath = 'agentic-flow/dist/coordination/autopilot-learning.js';- const mod = await import(/* webpackIgnore: true */ modPath).catch(() => null);- if (mod?.AutopilotLearning) {- const instance = new mod.AutopilotLearning();- if (await instance.initialize()) return instance;+ const mod = await import(/* webpackIgnore: true */ resolveAutopilotLearningUrl());+ if (!mod?.AutopilotLearning) {+ lastLearningLoadError = 'agentic-flow autopilot-learning module has no AutopilotLearning export';+ return null;}- } catch { /* not available */ }+ const instance = new mod.AutopilotLearning();+ if (await instance.initialize()) return instance;+ lastLearningLoadError = 'AutopilotLearning.initialize() returned false (AgentDB not initialized)';+ } catch (e) {+ lastLearningLoadError = `Could not load agentic-flow autopilot learning: ${e instanceof Error ? e.message : String(e)}`;+ }return null;}diff --git a/v3/@claude-flow/cli/src/mcp-tools/autopilot-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/autopilot-tools.tsindex b7ff5e54ca..21315a04ba 100644--- a/v3/@claude-flow/cli/src/mcp-tools/autopilot-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/autopilot-tools.ts@@ -12,7 +12,7 @@ import type { MCPTool } from './types.js';import { validateText } from './validate-input.js';import {loadState, saveState, appendLog, loadLog, discoverTasks,- isTerminal, tryLoadLearning,+ isTerminal, tryLoadLearning, getLearningLoadError,validateNumber, validateConfiguredTaskSources,VALID_TASK_SOURCES,} from '../autopilot-state.js';@@ -193,7 +193,7 @@ const autopilotLearn: MCPTool = {]);return ok({ metrics, patterns });}- return ok({ available: false, reason: 'AgentDB/AutopilotLearning not initialized', patterns: [] });+ return ok({ available: false, reason: getLearningLoadError() ?? 'AgentDB/AutopilotLearning not initialized', patterns: [] });},};