π 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
feat(cost-tracker): P11 β cost-per-conversation view (v0.12.0)
Reuven088e18f174b9
diff --git a/plugins/ruflo-cost-tracker/.claude-plugin/plugin.json b/plugins/ruflo-cost-tracker/.claude-plugin/plugin.jsonindex b2f56e1e76..d1dc172dbc 100644--- a/plugins/ruflo-cost-tracker/.claude-plugin/plugin.json+++ b/plugins/ruflo-cost-tracker/.claude-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "ruflo-cost-tracker","description": "Token usage tracking, model cost attribution per agent, budget alerts, and optimization recommendations β uses memory_* (namespace-routed) for cost-tracking and cost-patterns; pairs with federation budget circuit breaker (ADR-097)",- "version": "0.11.0",+ "version": "0.12.0","author": {"name": "ruvnet","url": "https://github.com/ruvnet"diff --git a/plugins/ruflo-cost-tracker/commands/ruflo-cost.md b/plugins/ruflo-cost-tracker/commands/ruflo-cost.mdindex 1043cd91cc..79c85934a8 100644--- a/plugins/ruflo-cost-tracker/commands/ruflo-cost.md+++ b/plugins/ruflo-cost-tracker/commands/ruflo-cost.md@@ -55,6 +55,11 @@ Cost tracking commands:3. The script wraps `npx @claude-flow/cli hooks model-outcome -t ... -m ... -o ...` with explicit-argv spawnSync so quoting is safe4. Without this, the router doesn't learn from cost-optimize recommendations and the Tier 1 bypass rate doesn't tighten over time+**`cost conversation`** -- Per-conversation cost view: list every session in `cost-tracking` with started-at, message count, top model, total cost. Different lens from `cost report` (which is per-agent/per-model).+1. Run `node plugins/ruflo-cost-tracker/scripts/conversation.mjs`+2. Optional `CONV_FORMAT=json`, `CONV_LIMIT=N`, `CONV_NAMESPACE=...`+3. Reports: total across conversations, per-tier rollup, per-session table+**`cost trend`** -- Read all docs/benchmarks/runs/*.json and surface drift in the gate metrics β win rate, avg latency, p99, escalation rate, speedup vs LLM. Flags regressions the binary smoke gate misses.1. Run `node plugins/ruflo-cost-tracker/scripts/trend.mjs`2. Optional `TREND_FORMAT=json` for machine-readable output, `TREND_LIMIT=N` to truncatediff --git a/plugins/ruflo-cost-tracker/scripts/conversation.mjs b/plugins/ruflo-cost-tracker/scripts/conversation.mjsnew file mode 100755index 0000000000..576143090f--- /dev/null+++ b/plugins/ruflo-cost-tracker/scripts/conversation.mjs@@ -0,0 +1,100 @@+#!/usr/bin/env node+// cost-conversation β list all session records in the cost-tracking namespace+// and show cost-per-conversation. Until now reports were per-agent / per-model;+// this gives a per-conversation timeline.+//+// Usage:+// node scripts/conversation.mjs # markdown table+// CONV_FORMAT=json node scripts/conversation.mjs # JSON+// CONV_LIMIT=20 node scripts/conversation.mjs # most recent N+// CONV_NAMESPACE=cost-tracking (default)++import { spawnSync } from 'node:child_process';++const NS = process.env.CONV_NAMESPACE || 'cost-tracking';++function memoryListSessionKeys() {+ const r = spawnSync('npx', [+ '@claude-flow/cli@latest', 'memory', 'list',+ '--namespace', NS, '--format', 'json',+ ], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8' });+ if (r.status !== 0) return [];+ const m = /\[[\s\S]*\]/.exec(r.stdout || '');+ if (!m) return [];+ let entries;+ try { entries = JSON.parse(m[0]); } catch { return []; }+ return entries+ .map((e) => e.key)+ .filter((k) => typeof k === 'string' && k.startsWith('session-'));+}++function memoryRetrieve(key) {+ const r = spawnSync('npx', [+ '@claude-flow/cli@latest', 'memory', 'retrieve',+ '--namespace', NS, '--key', key,+ ], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8' });+ if (r.status !== 0) return null;+ const m = /\{[\s\S]*\}/.exec(r.stdout || '');+ if (!m) return null;+ try { return JSON.parse(m[0]); } catch { return null; }+}++function fmtUsd(n) { return `$${(n || 0).toFixed(4)}`; }+function fmtTs(s) { return s ? new Date(s).toISOString().replace('T', ' ').replace(/\.\d+Z/, '') : ''; }+function topModel(byModel) {+ const entries = Object.entries(byModel || {}).sort((a, b) => (b[1].cost_usd || 0) - (a[1].cost_usd || 0));+ return entries[0]?.[0] || 'β';+}++function main() {+ const keys = memoryListSessionKeys();+ if (!keys.length) {+ console.log(`No sessions in '${NS}'. Run \`cost track\` first.`);+ return;+ }+ const records = keys.map((k) => memoryRetrieve(k)).filter(Boolean);+ records.sort((a, b) => (a.startedAt || '').localeCompare(b.startedAt || ''));++ const limit = parseInt(process.env.CONV_LIMIT || '50', 10);+ const recent = records.slice(-limit);++ const total = records.reduce((s, r) => s + (r.total_cost_usd || 0), 0);+ const byTier = { haiku: 0, sonnet: 0, opus: 0, unknown: 0 };+ for (const r of records) {+ if (r.byTier) {+ for (const [t, v] of Object.entries(r.byTier)) byTier[t] = (byTier[t] || 0) + v;+ }+ }++ if (process.env.CONV_FORMAT === 'json') {+ console.log(JSON.stringify({+ conversationCount: records.length,+ shownCount: recent.length,+ total_cost_usd: total,+ byTier,+ conversations: recent,+ }, null, 2));+ return;+ }++ console.log(`# cost-per-conversation β ${records.length} conversations in '${NS}'`);+ console.log('');+ console.log(`| Metric | Value |`);+ console.log(`|---|---:|`);+ console.log(`| Conversations tracked | ${records.length} |`);+ console.log(`| **Total cost (all)** | **${fmtUsd(total)}** |`);+ for (const t of ['haiku', 'sonnet', 'opus', 'unknown']) {+ if (byTier[t] > 0) console.log(`| Tier ${t} | ${fmtUsd(byTier[t])} |`);+ }+ console.log('');+ console.log(`## ${recent.length === records.length ? 'All' : 'Most recent ' + recent.length} conversations`);+ console.log('');+ console.log('| Started | Session | Messages | Top model | Cost |');+ console.log('|---|---|---:|---|---:|');+ for (const r of recent) {+ const sid = (r.sessionId || '').slice(0, 8);+ console.log(`| ${fmtTs(r.startedAt)} | \`${sid}\` | ${r.messageCount || 0} | \`${topModel(r.byModel)}\` | ${fmtUsd(r.total_cost_usd)} |`);+ }+}++main();diff --git a/plugins/ruflo-cost-tracker/scripts/smoke.sh b/plugins/ruflo-cost-tracker/scripts/smoke.shindex 2e7711e50c..0054824082 100755--- a/plugins/ruflo-cost-tracker/scripts/smoke.sh+++ b/plugins/ruflo-cost-tracker/scripts/smoke.sh@@ -8,10 +8,10 @@ step() { printf "β %s ... " "$1"; }ok() { printf "PASS\n"; PASS=$((PASS+1)); }bad() { printf "FAIL: %s\n" "$1"; FAIL=$((FAIL+1)); }-step "1. plugin.json declares 0.11.0 with new keywords"+step "1. plugin.json declares 0.12.0 with new keywords"v=$(grep -E '"version"' "$ROOT/.claude-plugin/plugin.json" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)-if [[ "$v" != "0.11.0" ]]; then- bad "expected 0.11.0, got '$v'"+if [[ "$v" != "0.12.0" ]]; then+ bad "expected 0.12.0, got '$v'"elsemiss=""for k in namespace-routing mcp agentic-flow agent-booster tier1-routing model-routing benchmarking verified telemetry budget; do@@ -20,9 +20,9 @@ else[[ -z "$miss" ]] && ok || bad "missing keywords:$miss"fi-step "2. all nine skills present with valid frontmatter"+step "2. all ten skills present with valid frontmatter"miss=""-for s in cost-report cost-optimize cost-booster-route cost-booster-edit cost-compact-context cost-benchmark cost-track cost-budget-check cost-trend; do+for s in cost-report cost-optimize cost-booster-route cost-booster-edit cost-compact-context cost-benchmark cost-track cost-budget-check cost-trend cost-conversation; dof="$ROOT/skills/$s/SKILL.md"[[ -f "$f" ]] || { miss="$miss missing-$s"; continue; }for k in 'name:' 'description:' 'allowed-tools:'; do@@ -321,6 +321,19 @@ step "39. ruflo-cost.md documents 'cost trend' subcommand"grep -q "cost trend" "$ROOT/commands/ruflo-cost.md" \&& ok || bad "missing"+step "40a. cost-conversation skill + conversation.mjs"+F1="$ROOT/scripts/conversation.mjs"+F2="$ROOT/skills/cost-conversation/SKILL.md"+miss=""+[[ -x "$F1" ]] || miss="$miss conv-not-executable"+node --check "$F1" 2>/dev/null || miss="$miss syntax-error"+grep -q "memoryListSessionKeys" "$F1" || miss="$miss no-list-fn"+grep -q "byTier" "$F1" || miss="$miss no-tier-aggr"+[[ -f "$F2" ]] || miss="$miss skill-missing"+grep -q "conversation\.mjs" "$F2" || miss="$miss skill-no-script-ref"+grep -q '^allowed-tools:[[:space:]]*\*' "$F2" && miss="$miss wildcard"+[[ -z "$miss" ]] && ok || bad "$miss"+step "40. CI workflow (smoke + booster bench on PR) present"WF="$ROOT/../../.github/workflows/cost-tracker-smoke.yml"miss=""diff --git a/plugins/ruflo-cost-tracker/skills/cost-conversation/SKILL.md b/plugins/ruflo-cost-tracker/skills/cost-conversation/SKILL.mdnew file mode 100644index 0000000000..d62fb8f7d2--- /dev/null+++ b/plugins/ruflo-cost-tracker/skills/cost-conversation/SKILL.md@@ -0,0 +1,38 @@+---+name: cost-conversation+description: Per-conversation cost view β list every session in cost-tracking with started-at, message count, top model, and total cost+argument-hint: ""+allowed-tools: Bash+---++# Cost per Conversation++`cost-report` and `cost-optimize` aggregate by **agent** and **model**. This skill aggregates by **conversation (session)** β a different lens that surfaces *which conversations cost the most*. Useful for retrospectives ("which sessions ran long on Opus?") and for evaluating whether a given project's session pattern is sustainable.++## When to use++- After multiple sessions, to see total spend per conversation.+- Before scoping a long session, to understand typical cost-per-conversation.+- For per-project rollups via `CONV_NAMESPACE=cost-tracking-<project>`.++## Steps++1. **Run the script** from anywhere:++ ```bash+ node plugins/ruflo-cost-tracker/scripts/conversation.mjs+ ```++ Optional env:+ - `CONV_FORMAT=json` β emit JSON instead of markdown+ - `CONV_LIMIT=20` β show only the most recent N conversations+ - `CONV_NAMESPACE=cost-tracking` β override target namespace++2. **Inspect the markdown table** β total cost across all conversations, per-tier rollup, then a per-session table (started-at, sessionId prefix, message count, top model, cost).++## Cross-references++- `cost-track` β the producer that populates `cost-tracking:session-*`+- `cost-report` β same data, per-agent / per-model lens+- `cost-trend` β drift across bench runs (different axis: corpus runs vs conversations)+- `cost-budget-check` β sums across conversations to evaluate the budget threshold