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): run memory store in CLAUDE_FLOW_CWD (#4397 part 2) (#4407)

rUva2465d80ba51
diff --git a/plugins/ruflo-cost-tracker/scripts/smoke.sh b/plugins/ruflo-cost-tracker/scripts/smoke.sh
index f9da3dd859..6aec6146a1 100755
--- a/plugins/ruflo-cost-tracker/scripts/smoke.sh
+++ b/plugins/ruflo-cost-tracker/scripts/smoke.sh
@@ -682,5 +682,9 @@ step "46. #3227 diagnostic log: track.mjs / ruflo-hook.cjs failure logging"
node "$ROOT/scripts/test-cost-tracker-diagnostic-log-3227.mjs" >/dev/null 2>&1 \
&& ok || bad "test-cost-tracker-diagnostic-log-3227-failed"
+step "47. #4397 track.mjs runs memory store in CLAUDE_FLOW_CWD"
+node "$ROOT/scripts/test-cost-tracker-data-cwd-4397.mjs" >/dev/null 2>&1 \
+ && ok || bad "test-cost-tracker-data-cwd-4397-failed"
+
printf "\n%s passed, %s failed\n" "$PASS" "$FAIL"
[[ $FAIL -eq 0 ]] || exit 1
diff --git a/plugins/ruflo-cost-tracker/scripts/test-cost-tracker-data-cwd-4397.mjs b/plugins/ruflo-cost-tracker/scripts/test-cost-tracker-data-cwd-4397.mjs
new file mode 100644
index 0000000000..5614482ad3
--- /dev/null
+++ b/plugins/ruflo-cost-tracker/scripts/test-cost-tracker-data-cwd-4397.mjs
@@ -0,0 +1,71 @@
+#!/usr/bin/env node
+/**
+ * Regression guard for ruvnet/ruflo#4397 part 2 — track.mjs must run the
+ * `memory store` child in CLAUDE_FLOW_CWD (the store the MCP server uses),
+ * not in the project directory the Stop hook fired from. Without it the CLI
+ * creates a stray .swarm/memory.db in the session project.
+ *
+ * Hermetic: a fake POSIX `ruflo` on PATH records its working directory.
+ * Skipped on win32 (the fake CLI is a sh script).
+ *
+ * Usage: node plugins/ruflo-cost-tracker/scripts/test-cost-tracker-data-cwd-4397.mjs
+ */
+import { spawnSync } from 'node:child_process';
+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, writeFileSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { encodeProjectPath } from './_encode-project-path.mjs';
+
+const HERE = dirname(fileURLToPath(import.meta.url));
+const TRACK_SCRIPT = join(HERE, 'track.mjs');
+
+if (process.platform === 'win32') {
+ console.log('# SKIPPED on win32 (fake CLI is a POSIX sh script)');
+ process.exit(0);
+}
+
+let failed = 0;
+const assert = (cond, label, detail = '') => {
+ if (cond) console.log(`ok: ${label}`);
+ else { failed++; console.error(`FAIL: ${label}${detail ? `\n ${detail}` : ''}`); }
+};
+
+const TMP = realpathSync(mkdtempSync(join(tmpdir(), 'ruflo-cost-tracker-cwd-')));
+const BIN = join(TMP, 'bin');
+mkdirSync(BIN, { recursive: true });
+const MARK = join(TMP, 'cwd-mark');
+writeFileSync(join(BIN, 'ruflo'), `#!/bin/sh\npwd -P > "${MARK}"\nexit 0\n`);
+chmodSync(join(BIN, 'ruflo'), 0o755);
+
+function run(name, extraEnv) {
+ const home = join(TMP, `home-${name}`);
+ const project = join(TMP, `project-${name}`);
+ mkdirSync(project, { recursive: true });
+ const dir = join(home, '.claude', 'projects', encodeProjectPath(project));
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 's.jsonl'), [
+ { sessionId: 's', cwd: project, timestamp: '2026-10-11T00:00:00.000Z', type: 'user' },
+ { sessionId: 's', cwd: project, timestamp: '2026-10-11T00:00:05.000Z', type: 'assistant',
+ message: { model: 'claude-opus-5-5', usage: { input_tokens: 1, output_tokens: 1 } } },
+ ].map((l) => JSON.stringify(l)).join('\n') + '\n');
+ const env = { ...process.env, PATH: BIN, RUFLO_PLUGIN_SKIP_LOCAL_CLI: '1', HOME: home, USERPROFILE: home, TRACK_CWD: project, ...extraEnv };
+ for (const k of ['RUFLO_PLUGIN_SKIP_PATH_CLI', 'TRACK_DRY_RUN', 'TRACK_SESSION', 'CLI_CORE', 'NODE_OPTIONS']) delete env[k];
+ if (!extraEnv.CLAUDE_FLOW_CWD) delete env.CLAUDE_FLOW_CWD;
+ const r = spawnSync(process.execPath, [TRACK_SCRIPT], { cwd: project, encoding: 'utf-8', timeout: 15_000, env });
+ const seen = existsSync(MARK) ? readFileSync(MARK, 'utf-8').trim() : null;
+ return { r, project, seen };
+}
+
+const DATA = join(TMP, 'data-root');
+mkdirSync(DATA, { recursive: true });
+const a = run('a', { CLAUDE_FLOW_CWD: DATA });
+assert(a.r.status === 0, 'track.mjs exits 0', `status ${a.r.status}: ${a.r.stderr}`);
+assert(a.seen === DATA, 'memory store runs in CLAUDE_FLOW_CWD', `saw ${a.seen}, wanted ${DATA}`);
+
+writeFileSync(MARK, '');
+const b = run('b', {});
+assert(b.seen === b.project, 'without CLAUDE_FLOW_CWD it keeps the session cwd', `saw ${b.seen}, wanted ${b.project}`);
+
+console.log(failed ? `\n${failed} failed` : '\nall passed');
+process.exit(failed ? 1 : 0);
diff --git a/plugins/ruflo-cost-tracker/scripts/track.mjs b/plugins/ruflo-cost-tracker/scripts/track.mjs
index a965be7538..bea4a88e73 100755
--- a/plugins/ruflo-cost-tracker/scripts/track.mjs
+++ b/plugins/ruflo-cost-tracker/scripts/track.mjs
@@ -106,7 +106,12 @@ function persistToMemory(summary) {
'--namespace', ns,
'--key', key,
'--value', JSON.stringify(summary),
- ], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32' });
+ ], {
+ // #4397: land the row in the store the MCP server uses (CLAUDE_FLOW_CWD),
+ // not in whatever project dir the Stop hook happened to fire from.
+ cwd: process.env.CLAUDE_FLOW_CWD || process.cwd(),
+ stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', shell: process.platform === 'win32',
+ });
if (r.status !== 0) {
return { ok: false, reason: r.stderr?.slice(0, 300) || `exit ${r.status}` };
}