Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(dashboard): rebind hero tile to file-backed savings, add cross-process lock

ashishpatel26a8b62a77d906
diff --git a/headroom/dashboard/templates/dashboard.html b/headroom/dashboard/templates/dashboard.html
index 67a696f15..a4c9988f5 100644
--- a/headroom/dashboard/templates/dashboard.html
+++ b/headroom/dashboard/templates/dashboard.html
@@ -105,12 +105,12 @@
<div class="bg-surface rounded-lg p-4 border border-border">
<div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Proxy $ Saved</div>
<div class="flex items-baseline gap-2">
- <span class="text-3xl font-light tabular-nums text-emerald-400" x-text="'$' + formatCurrency(stats.cost?.savings_usd || 0)"></span>
+ <span class="text-3xl font-light tabular-nums text-emerald-400" x-text="'$' + formatCurrency(stats.persistent_savings?.lifetime?.compression_savings_usd || 0)"></span>
</div>
<div class="mt-2 text-xs text-gray-500">
- <span x-show="stats.cost?.savings_usd > 0"
+ <span x-show="(stats.persistent_savings?.lifetime?.compression_savings_usd || 0) > 0"
x-text="formatNumber(stats.tokens?.proxy_compression_saved || 0) + ' proxy tokens only; ' + cliFilteringLabel + ' excluded from $'"></span>
- <span x-show="!(stats.cost?.savings_usd > 0)"
+ <span x-show="!((stats.persistent_savings?.lifetime?.compression_savings_usd || 0) > 0)"
x-text="formatNumber(stats.requests?.total || 0) + ' requests processed'"></span>
</div>
</div>
diff --git a/headroom/proxy/savings_tracker.py b/headroom/proxy/savings_tracker.py
index 600b86bbc..649e364c8 100644
--- a/headroom/proxy/savings_tracker.py
+++ b/headroom/proxy/savings_tracker.py
@@ -21,6 +21,19 @@ from typing import Any
from headroom import paths as _paths
+# fcntl is Unix-only; on Windows file locking is skipped (best-effort).
+# Typed Any so attribute access on flock/LOCK_EX/LOCK_UN doesn't fail on
+# platforms where the type stub is absent.
+_fcntl: Any = None
+_HAS_FCNTL = False
+try:
+ import fcntl as _fcntl_impl # noqa: E402
+
+ _fcntl = _fcntl_impl
+ _HAS_FCNTL = True
+except ImportError:
+ pass
+
logger = logging.getLogger(__name__)
HEADROOM_SAVINGS_PATH_ENV_VAR = _paths.HEADROOM_SAVINGS_PATH_ENV
@@ -812,7 +825,18 @@ class SavingsTracker:
f.write(json_data)
f.flush()
os.fsync(f.fileno())
- Path(tmp_path).replace(self._path)
+ # Acquire an exclusive cross-process lock on the target file
+ # before the atomic rename so concurrent workers don't clobber
+ # each other's increments. Unix only; Windows skips gracefully.
+ if _HAS_FCNTL and self._path.exists():
+ with open(self._path, "r+b") as lock_fh:
+ _fcntl.flock(lock_fh, _fcntl.LOCK_EX)
+ try:
+ Path(tmp_path).replace(self._path)
+ finally:
+ _fcntl.flock(lock_fh, _fcntl.LOCK_UN)
+ else:
+ Path(tmp_path).replace(self._path)
except Exception:
try:
Path(tmp_path).unlink()
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 48d00da35..92031fed9 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -3073,12 +3073,13 @@ def run_server(
# sticky-session workaround.
logger.warning(
"Headroom is running with workers=%d. The in-memory CCR store, "
- "compression cache, prefix tracker, and TOIN state are all "
+ "compression cache, prefix tracker, TOIN state, and CostTracker are all "
"per-process; multi-worker deployments produce silent retrieval "
- "failures and avoidable cache busts when sessions land on different "
- "workers. Run --workers 1 (or place a sticky-session load balancer "
- "in front of multiple --workers 1 processes). See RUST_DEV.md → "
- "'Multi-worker deployment — CCR fragmentation'.",
+ "failures, avoidable cache busts, and an unstable dashboard 'Proxy $ Saved' "
+ "hero tile (each /stats poll hits a different worker's partial total) when "
+ "sessions land on different workers. Run --workers 1 (or place a "
+ "sticky-session load balancer in front of multiple --workers 1 processes). "
+ "See RUST_DEV.md → 'Multi-worker deployment — CCR fragmentation'.",
workers,
)
os.environ[_MULTI_WORKER_CONFIG_ENV] = json.dumps(_proxy_config_payload(config))