Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(cost-tracker/compact): resolve from anywhere + free HEALTH_PORT (#1930) (#1971)

rUvc8258cc07c9f
diff --git a/plugins/ruflo-cost-tracker/scripts/compact.mjs b/plugins/ruflo-cost-tracker/scripts/compact.mjs
index 60465884ad..ea9da3cef6 100755
--- a/plugins/ruflo-cost-tracker/scripts/compact.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/compact.mjs
@@ -1,24 +1,65 @@
#!/usr/bin/env node
// cost-compact β€” wraps getTokenOptimizer().getCompactContext() so the
// cost-compact-context skill can invoke a single command instead of an
-// inlined Node one-liner. A proper MCP tool wrapping getTokenOptimizer is
-// still deferred (would require modifying @claude-flow/cli source); this
-// is the plugin-local equivalent.
+// inlined Node one-liner.
//
-// Resolution: must run from a directory where `@claude-flow/integration`
-// resolves (typically anywhere under `v3/`). The script resolves from
-// process.cwd() rather than from its own location so the user's `cd v3`
-// works without npm-installing the bridge into the plugin tree.
+// Resolution strategy (#1930): pnpm-workspace marketplace installs don't
+// hoist `@claude-flow/integration` into `v3/node_modules/`, so resolving
+// from cwd alone breaks. Try, in order:
+// 1. createRequire(cwd) β€” works for npm-style hoisted installs
+// 2. createRequire(script-dir) β€” works when run from inside the plugin
+// 3. absolute path from this file β€” works on the marketplace layout
+// (script lives at <ruflo>/plugins/ruflo-cost-tracker/scripts/, the
+// integration package at <ruflo>/v3/@claude-flow/integration/)
+//
+// Also: agentic-flow's index.js binds `process.env.HEALTH_PORT || 8080`
+// unconditionally on import. On any host with something already on 8080
+// (k8s NodePort, forwarded dev server) the import EADDRINUSEs. Set
+// HEALTH_PORT to 0 by default so the OS picks a free port β€” users can
+// still override with an explicit value.
//
// Usage:
// node scripts/compact.mjs "<query>"
//
// Optional env:
// COMPACT_QUIET=1 emit JSON only (no markdown banner)
+// HEALTH_PORT=<n> override agentic-flow's health-server port (default 0)
import { createRequire } from 'node:module';
-import { pathToFileURL } from 'node:url';
-import { join } from 'node:path';
+import { pathToFileURL, fileURLToPath } from 'node:url';
+import { join, resolve, dirname, isAbsolute } from 'node:path';
+import { existsSync } from 'node:fs';
+
+// #1930 step 4: pick a free port unless caller pinned one. Set BEFORE the
+// dynamic import below β€” agentic-flow reads HEALTH_PORT at module-load time.
+if (!process.env.HEALTH_PORT) process.env.HEALTH_PORT = '0';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = dirname(__filename);
+
+function tryResolveFrom(anchorPath) {
+ try {
+ const req = createRequire(anchorPath);
+ return req.resolve('@claude-flow/integration/token-optimizer');
+ } catch {
+ return null;
+ }
+}
+
+function tryAbsolutePath() {
+ // Walk up from this script looking for `v3/@claude-flow/integration/dist/token-optimizer.js`.
+ // Handles the marketplace layout (script at plugins/ruflo-cost-tracker/scripts/) and the
+ // dev-repo layout. Caps depth at 6 to avoid runaway traversal.
+ let dir = __dirname;
+ for (let i = 0; i < 6; i++) {
+ const candidate = join(dir, 'v3', '@claude-flow', 'integration', 'dist', 'token-optimizer.js');
+ if (existsSync(candidate)) return candidate;
+ const parent = dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ return null;
+}
async function main() {
const query = process.argv[2] || '';
@@ -27,22 +68,54 @@ async function main() {
process.exit(2);
}
+ // Resolution chain β€” cwd β†’ script dir β†’ absolute fallback (#1930 step 5)
+ const candidates = [
+ tryResolveFrom(join(process.cwd(), 'package.json')),
+ tryResolveFrom(join(__dirname, 'package.json')),
+ tryAbsolutePath(),
+ ].filter(Boolean);
+
let mod;
- try {
- const requireFromCwd = createRequire(join(process.cwd(), 'package.json'));
- const resolved = requireFromCwd.resolve('@claude-flow/integration/token-optimizer');
- mod = await import(pathToFileURL(resolved).href);
- } catch (err) {
+ let lastErr = null;
+ for (const resolved of candidates) {
+ try {
+ // Skip dangling resolutions that point at a non-existent file (a
+ // common pnpm-workspace symptom when dist/ wasn't built β€” see
+ // #1930 step 1).
+ const abs = isAbsolute(resolved) ? resolved : resolve(resolved);
+ if (!existsSync(abs)) {
+ lastErr = new Error(`token-optimizer.js missing at ${abs} β€” run \`cd v3/@claude-flow/integration && pnpm run build\` (#1930 step 1)`);
+ continue;
+ }
+ mod = await import(pathToFileURL(abs).href);
+ break;
+ } catch (err) {
+ lastErr = err;
+ }
+ }
+
+ if (!mod) {
+ const reason = lastErr
+ ? String(lastErr.message || lastErr).slice(0, 300)
+ : 'Cannot find module @claude-flow/integration/token-optimizer';
const out = {
bridgeUnavailable: true,
- reason: String(err.message || err).slice(0, 300),
+ reason,
memoriesRetrieved: 0,
tokensSaved: 0,
agenticFlowAvailable: false,
+ hint: 'Tried cwd, script dir, and absolute path from this script β€” see #1930 for full troubleshooting.',
};
if (process.env.COMPACT_QUIET === '1') return console.log(JSON.stringify(out));
- console.log(`# cost-compact-context\n\nbridge unavailable: ${out.reason}`);
- console.log('Run from a directory where `@claude-flow/integration` resolves, e.g. `cd v3 && ...`.');
+ console.log(`# cost-compact-context\n\nbridge unavailable: ${reason}`);
+ console.log('');
+ console.log('Tried:');
+ console.log(' - resolve from cwd');
+ console.log(' - resolve from script dir');
+ console.log(' - absolute path walk from this script (looking for v3/@claude-flow/integration/dist/token-optimizer.js)');
+ console.log('');
+ console.log('If `@claude-flow/integration` is installed but `dist/` is missing, run:');
+ console.log(' cd v3/@claude-flow/integration && pnpm run build');
return;
}