"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): gate detail provider/model and label failed/cancelled tokens
shadowinlifed8750e4cb459
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7ecfd1d1f..6ce0190bd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).- Swarm run detail and the dashboard expose token totals and label eachfigure as provider-reported or `len//4`-estimated; the swarm worker nowcaptures prompt-cache tokens through backward-compatible `run.json` fields.+- The swarm provenance label survives the shared tool-result redaction choke+ point (a `token_source` was previously scrubbed to `[redacted]`); the swarm+ detail response gates `provider`/`model` through the same public-metadata+ filter the MCP payload applies; and failed or cancelled tasks report their+ tokens so the live card matches the run total.- This batch is token-count visibility only: it ships no currency cost orcross-run aggregation.diff --git a/agent/src/api/swarm_routes.py b/agent/src/api/swarm_routes.pyindex 446eccf4f..8cd4db22f 100644--- a/agent/src/api/swarm_routes.py+++ b/agent/src/api/swarm_routes.py@@ -143,6 +143,7 @@ def register_swarm_routes(run = runtime._store.reconcile_run(loaded, write=True)+ from src.swarm.models import public_model_metadata, public_provider_metadatafrom src.swarm.serialization import serialize_taskreturn {@@ -164,13 +165,16 @@ def register_swarm_routes("created_at": run.created_at,"completed_at": run.completed_at,"final_report": run.final_report,- # Key names mirror the MCP run payload (mcp_server._run_to_dict);- # values come from the reconciled run, so a never-run run reports- # int 0 totals and token_source None — never omitted or nulled.+ # Key names AND value policy mirror the MCP run payload+ # (mcp_server._run_to_dict): provider/model pass through the same+ # public-metadata gate on read, so a run.json written before that+ # gate existed cannot leak an endpoint- or credential-shaped string+ # to a browser client. Totals come from the reconciled run, so a+ # never-run run reports int 0 totals and token_source None."total_input_tokens": run.total_input_tokens,"total_output_tokens": run.total_output_tokens,- "provider": run.provider,- "model": run.model,+ "provider": public_provider_metadata(run.provider),+ "model": public_model_metadata(run.model),"token_source": run.token_source,}diff --git a/agent/src/swarm/runtime.py b/agent/src/swarm/runtime.pyindex 5908ed69f..9daad36de 100644--- a/agent/src/swarm/runtime.py+++ b/agent/src/swarm/runtime.py@@ -603,6 +603,9 @@ class SwarmRuntime:"iterations": result.iterations,"input_tokens": result.input_tokens,"output_tokens": result.output_tokens,+ "cache_read_tokens": result.cache_read_tokens,+ "cache_creation_tokens": result.cache_creation_tokens,+ "token_source": result.token_source,},),)@@ -625,6 +628,9 @@ class SwarmRuntime:"error": redact_internal_paths(result.error),"input_tokens": result.input_tokens,"output_tokens": result.output_tokens,+ "cache_read_tokens": result.cache_read_tokens,+ "cache_creation_tokens": result.cache_creation_tokens,+ "token_source": result.token_source,},),)diff --git a/agent/tests/test_swarm_routes_usage.py b/agent/tests/test_swarm_routes_usage.pyindex b0dab3bfd..11722fbf9 100644--- a/agent/tests/test_swarm_routes_usage.py+++ b/agent/tests/test_swarm_routes_usage.py@@ -2,9 +2,12 @@The detail response must surface the run's stored usage — totals, provider,model and the ``token_source`` provenance label — with the same key names the-MCP run payload uses. A run that never executed must report literal int ``0``-totals (not ``null``, not a missing key) so a cost UI can never mistake an-unmeasured run for a missing field.+MCP run payload uses. ``provider``/``model`` additionally pass through the same+public-metadata gate MCP applies on read, so a run.json written before that+gate existed cannot leak an endpoint- or credential-shaped string to a browser+client. A run that never executed must report literal int ``0`` totals (not+``null``, not a missing key) so a cost UI can never mistake an unmeasured run+for a missing field.The swarm runtime singleton is monkeypatched to a ``SimpleNamespace`` holding areal ``SwarmStore`` rooted at ``tmp_path``, so every response is read back from@@ -57,7 +60,8 @@ def test_detail_returns_stored_token_totals_and_provider_model(swarm_store: SwarmStore,) -> None:"""Given a persisted run with measured usage, When GET detail, Then the- response carries the exact stored values under the MCP-parity keys."""+ response carries the stored values under the MCP-parity keys (an allowlisted+ provider/model survives the public-metadata gate unchanged)."""_seed_run(swarm_store,"run-usage",@@ -130,3 +134,21 @@ def test_detail_reflects_updated_stored_run_not_stale_snapshot(assert payload["provider"] == "anthropic"assert payload["model"] == "claude-sonnet-5"assert payload["token_source"] == "estimated"+++def test_detail_gates_non_public_provider_and_endpoint_shaped_model(+ swarm_store: SwarmStore,+) -> None:+ """Given a stored run whose provider/model predate the write-time public+ gate, When GET detail, Then both are redacted rather than leaked raw."""+ _seed_run(+ swarm_store,+ "run-unsanitized",+ provider="internal-proxy",+ model="http://10.0.0.5:8080/v1",+ )++ payload = _client().get("/swarm/runs/run-unsanitized").json()++ assert payload["provider"] == "[redacted]"+ assert payload["model"] == "[redacted]"