AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): seed dynamic-cost block preflight from historical averages (#13031)
Zamil Majdy9b0edd84d3f1
diff --git a/autogpt_platform/backend/backend/api/features/admin/block_cost_admin_routes.py b/autogpt_platform/backend/backend/api/features/admin/block_cost_admin_routes.pynew file mode 100644index 0000000000..88548f0ecb--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/admin/block_cost_admin_routes.py@@ -0,0 +1,96 @@+import logging+import math+import typing+from datetime import datetime, timezone++from autogpt_libs.auth import get_user_id, requires_admin_user+from fastapi import APIRouter, HTTPException, Query, Security+from pydantic import BaseModel++from backend.data.block_cost_analytics import (+ ANALYTICS_MAX_DAYS,+ BlockCostEstimateRow,+ compute_block_cost_estimates,+)++logger = logging.getLogger(__name__)+++router = APIRouter(+ prefix="/admin",+ tags=["admin", "blocks"],+ dependencies=[Security(requires_admin_user)],+)+++class BlockCostEstimatesResponse(BaseModel):+ estimates: list[BlockCostEstimateRow]+ total_rows: int+ window_days: int+ max_window_days: int+ min_samples: int+ generated_at: datetime+++ "/blocks/cost-estimates",+ response_model=BlockCostEstimatesResponse,+ summary="Export Block Cost Estimates",+)+async def get_block_cost_estimates(+ start: typing.Optional[datetime] = Query(+ None, description="ISO timestamp (inclusive)"+ ),+ end: typing.Optional[datetime] = Query(+ None, description="ISO timestamp (inclusive)"+ ),+ min_samples: int = Query(+ 10, ge=1, description="Minimum executions per block to include"+ ),+ admin_user_id: str = Security(get_user_id),+) -> BlockCostEstimatesResponse:+ """Aggregate per-block average credits-per-execution over [start, end].++ Capped at ANALYTICS_MAX_DAYS days. Returns only blocks whose current cost+ type is dynamic (SECOND/ITEMS/COST_USD) — static-cost blocks already+ charge correctly pre-flight and don't need an estimate override. TOKENS+ is excluded because `compute_token_credits` already supplies a per-model+ floor at pre-flight; a per-block historical mean would lose that+ granularity.+ """+ if start is None or end is None:+ raise HTTPException(+ status_code=400, detail="start and end query params are required"+ )+ if start.tzinfo is None:+ start = start.replace(tzinfo=timezone.utc)+ if end.tzinfo is None:+ end = end.replace(tzinfo=timezone.utc)++ logger.info(+ "Admin %s aggregating block cost estimates [%s..%s] min_samples=%s",+ admin_user_id,+ start.isoformat(),+ end.isoformat(),+ min_samples,+ )++ try:+ rows = await compute_block_cost_estimates(+ start=start, end=end, min_samples=min_samples+ )+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ # Ceil so a [00:00:00Z, 23:59:59.999Z] window — what the frontend sends for+ # an inclusive 7-day pick — reports 7, not 6 (`.days` would truncate).+ window_days = math.ceil((end - start).total_seconds() / 86400)++ return BlockCostEstimatesResponse(+ estimates=rows,+ total_rows=len(rows),+ window_days=window_days,+ max_window_days=ANALYTICS_MAX_DAYS,+ min_samples=min_samples,+ generated_at=datetime.now(timezone.utc),+ )diff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.pyindex 0bb578e00d..13721c6203 100644--- a/autogpt_platform/backend/backend/api/rest_api.py+++ b/autogpt_platform/backend/backend/api/rest_api.py@@ -16,6 +16,7 @@ from fastapi.middleware.gzip import GZipMiddlewarefrom fastapi.routing import APIRoutefrom prisma.errors import PrismaError+import backend.api.features.admin.block_cost_admin_routesimport backend.api.features.admin.credit_admin_routesimport backend.api.features.admin.diagnostics_admin_routesimport backend.api.features.admin.execution_analytics_routes@@ -364,6 +365,11 @@ app.include_router(tags=["v2", "admin"],prefix="/api/admin",)+app.include_router(+ backend.api.features.admin.block_cost_admin_routes.router,+ tags=["v2", "admin"],+ prefix="/api",+)app.include_router(backend.api.features.executions.review.routes.router,tags=["v2", "executions", "review"],diff --git a/autogpt_platform/backend/backend/blocks/claude_code_cost_test.py b/autogpt_platform/backend/backend/blocks/claude_code_cost_test.pyindex 1e51d72f42..737c79483f 100644--- a/autogpt_platform/backend/backend/blocks/claude_code_cost_test.py+++ b/autogpt_platform/backend/backend/blocks/claude_code_cost_test.py@@ -16,9 +16,19 @@ from backend.blocks._base import BlockCostTypefrom backend.blocks.claude_code import ClaudeCodeBlockfrom backend.data.block_cost_config import BLOCK_COSTSfrom backend.data.model import NodeExecutionStats+from backend.executor import utils as executor_utilsfrom backend.executor.utils import block_usage_cost[email protected](autouse=True)+def _stub_preflight_estimate(monkeypatch):+ """Force `get_preflight_estimate` to return 0 in this module's tests so+ the COST_USD pre-flight assertion doesn't couple to a populated+ `block_preflight_estimates.json` once the seeding PR registers a non-zero+ estimate for ClaudeCodeBlock."""+ monkeypatch.setattr(executor_utils, "get_preflight_estimate", lambda _bid: 0)++def test_claude_code_registered_as_cost_usd_150():"""Sanity: BLOCK_COSTS holds the COST_USD, 150 cr/$ entry."""entries = BLOCK_COSTS[ClaudeCodeBlock]diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex dc418818a2..b79385c9fb 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -322,8 +322,15 @@ async def execute_block(# Coerce non-matching data types to the expected input schema.coerce_inputs_to_schema(input_data, block.input_schema)- # Pre-execution credit check (courtesy; spend_credits is atomic)- cost, cost_filter = block_usage_cost(block, input_data)+ # Pre-execution credit check (courtesy; spend_credits is atomic).+ # Pass `use_preflight_estimate=False` because this path bypasses+ # the executor manager and never runs reconciliation — locking in+ # the historical-average estimate as the final charge would be+ # incorrect for SECOND/ITEMS/COST_USD blocks. Returns 0 here for+ # those types, preserving the pre-#13031 contract.+ cost, cost_filter = block_usage_cost(+ block, input_data, use_preflight_estimate=False+ )has_cost = cost > 0_credit_db = credit_db()if has_cost:diff --git a/autogpt_platform/backend/backend/data/block_cost_analytics.py b/autogpt_platform/backend/backend/data/block_cost_analytics.pynew file mode 100644index 0000000000..5e4b738c4a--- /dev/null+++ b/autogpt_platform/backend/backend/data/block_cost_analytics.py@@ -0,0 +1,150 @@+"""Aggregate historical block-execution credit costs from CreditTransaction.++Used by the admin export endpoint to seed `block_preflight_estimates.json`,+giving dynamic-cost blocks a non-zero pre-flight charge so post-flight+reconciliation only settles a small delta.+"""++from __future__ import annotations++from datetime import datetime, timedelta+from typing import Any++from pydantic import BaseModel++from backend.blocks import get_block+from backend.data.block_cost_config import BLOCK_COSTS+from backend.data.db import query_raw_with_schema++# Match the cost types whose pre-flight is genuinely 0 today (see+# `block_usage_cost` in executor/utils.py). TOKENS is excluded because+# `compute_token_credits` already provides a per-model floor at pre-flight,+# which is more accurate than a per-block historical average that mixes+# Haiku-class and Opus-class executions.+DYNAMIC_COST_TYPES = {"second", "items", "cost_usd"}++ANALYTICS_MAX_DAYS = 90+ANALYTICS_MIN_SAMPLES_DEFAULT = 10+++class BlockCostEstimateRow(BaseModel):+ block_id: str+ block_name: str+ cost_type: str+ samples: int+ mean_credits: int+ p50_credits: int+ p95_credits: int+++def _resolve_cost_type(block_id: str) -> str | None:+ """Return the first dynamic cost type registered for this block, if any.++ A block can declare multiple BlockCost entries with different cost_filters.+ We collapse them to a single representative type for the export so the+ admin view stays compact; reconciliation continues to use the per-filter+ entry at runtime.++ Trade-off: the JSON estimate is **per-block-id, not per-filter**. For+ blocks with tier-style filters (e.g. Ayrshare `is_video=True → 5cr` vs+ `is_video=False → 2cr`), the seeded mean averages across all filters and+ the pre-flight charge will over-bill low-tier runs and under-bill high-tier+ runs; reconciliation refunds/debt settles the delta. The seeding-PR+ reviewer should spot-check the `p95/mean` ratio for any block whose ratio+ is wide enough to suggest filter-tier costs.+ """+ block = get_block(block_id)+ if not block:+ return None+ costs = BLOCK_COSTS.get(type(block), [])+ for c in costs:+ if c.cost_type.value in DYNAMIC_COST_TYPES:+ return c.cost_type.value+ return None+++async def compute_block_cost_estimates(+ *,+ start: datetime,+ end: datetime,+ min_samples: int = ANALYTICS_MIN_SAMPLES_DEFAULT,+) -> list[BlockCostEstimateRow]:+ """Aggregate per-(block_id, node_exec_id) credit cost over [start, end].++ Sums all USAGE rows per node-execution (capturing pre-flight + reconciliation+ deltas as one cost), then averages across executions per block. Filters to+ blocks whose current cost type is dynamic — static-cost blocks already+ charge correctly pre-flight and don't need an estimate.++ User-ID exemption: this query intentionally aggregates across users — the+ purpose is platform-wide cost calibration, not per-user data access. Per+ AGENTS.md the caller must enforce admin-only auth (the only caller is the+ admin route, which is gated by `requires_admin_user`).+ """+ if start >= end:+ raise ValueError("start must be before end")+ # Use total_seconds rather than `.days` so a 90d-and-1-hour window doesn't+ # slip past the cap because `.days` truncates fractional days.+ if (end - start) > timedelta(days=ANALYTICS_MAX_DAYS):+ approx_days = (end - start).total_seconds() / 86400+ raise ValueError(f"window {approx_days:.2f}d exceeds max {ANALYTICS_MAX_DAYS}d")++ query = """+ WITH per_exec AS (+ SELECT+ metadata->>'block_id' AS block_id,+ MAX(metadata->>'block') AS block_name,+ metadata->>'node_exec_id' AS node_exec_id,+ SUM(-amount) AS exec_cost+ FROM {schema_prefix}"CreditTransaction"+ WHERE type = 'USAGE'+ AND "createdAt" >= $1::timestamp+ AND "createdAt" <= $2::timestamp+ AND "isActive" = true+ AND metadata->>'block_id' IS NOT NULL+ AND metadata->>'node_exec_id' IS NOT NULL+ GROUP BY metadata->>'block_id', metadata->>'node_exec_id'+ )+ SELECT+ block_id,+ MAX(block_name) AS block_name,+ COUNT(*)::int AS samples,+ ROUND(AVG(exec_cost))::int AS mean_credits,+ ROUND(PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY exec_cost))::int AS p50_credits,+ ROUND(PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY exec_cost))::int AS p95_credits+ FROM per_exec+ -- Keep zero-cost executions (post-flight refund cancelled the pre-flight+ -- estimate) so the historical mean reflects the full distribution. Drop+ -- only over-refunded outliers, which are net-negative on the user side.+ WHERE exec_cost >= 0+ GROUP BY block_id+ HAVING COUNT(*) >= $3+ ORDER BY samples DESC+ """+ rows: list[dict[str, Any]] = await query_raw_with_schema(+ query, start, end, min_samples+ )++ out: list[BlockCostEstimateRow] = []+ for r in rows:+ block_id = r["block_id"]+ cost_type = _resolve_cost_type(block_id)+ if not cost_type:+ # Skip blocks that don't have a dynamic cost type — their pre-flight+ # is already correct and shouldn't be overridden by a historical mean.+ continue+ # The SQL guards against NULL aggregates (HAVING COUNT >= 1, exec_cost >= 0)+ # but a single corrupt row could still surface as NULL — coerce defensively+ # so a malformed row doesn't 500 the endpoint.+ out.append(+ BlockCostEstimateRow(+ block_id=block_id,+ block_name=r.get("block_name") or block_id,+ cost_type=cost_type,+ samples=int(r.get("samples") or 0),+ mean_credits=int(r.get("mean_credits") or 0),+ p50_credits=int(r.get("p50_credits") or 0),+ p95_credits=int(r.get("p95_credits") or 0),+ )+ )+ return outdiff --git a/autogpt_platform/backend/backend/data/block_cost_analytics_test.py b/autogpt_platform/backend/backend/data/block_cost_analytics_test.pynew file mode 100644index 0000000000..d16ffb8336--- /dev/null+++ b/autogpt_platform/backend/backend/data/block_cost_analytics_test.py@@ -0,0 +1,182 @@+from datetime import datetime, timedelta, timezone+from unittest.mock import AsyncMock, patch++import pytest++from backend.data.block_cost_analytics import (+ ANALYTICS_MAX_DAYS,+ compute_block_cost_estimates,+)+++async def test_window_cap_rejected():+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=ANALYTICS_MAX_DAYS + 1)+ with pytest.raises(ValueError, match="exceeds max"):+ await compute_block_cost_estimates(start=start, end=end)+++async def test_inverted_window_rejected():+ end = datetime.now(timezone.utc)+ start = end + timedelta(hours=1)+ with pytest.raises(ValueError, match="start must be before end"):+ await compute_block_cost_estimates(start=start, end=end)+++async def test_static_cost_blocks_filtered_out():+ """A row whose block resolves to a RUN/BYTE cost block must not appear in+ the response — those types already have a valid pre-flight."""+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=7)++ raw_rows = [+ {+ "block_id": "static-run-block",+ "block_name": "RunOnlyBlock",+ "samples": 50,+ "mean_credits": 3,+ "p50_credits": 3,+ "p95_credits": 3,+ },+ ]++ with (+ patch(+ "backend.data.block_cost_analytics.query_raw_with_schema",+ new_callable=AsyncMock,+ return_value=raw_rows,+ ),+ patch(+ "backend.data.block_cost_analytics._resolve_cost_type",+ return_value=None,+ ),+ ):+ out = await compute_block_cost_estimates(start=start, end=end)++ assert out == []+++async def test_dynamic_cost_blocks_returned_with_resolved_type():+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=7)++ raw_rows = [+ {+ "block_id": "dyn-1",+ "block_name": "DynamicSearchBlock",+ "samples": 200,+ "mean_credits": 7,+ "p50_credits": 6,+ "p95_credits": 12,+ },+ {+ "block_id": "dyn-2",+ "block_name": "DynamicCostBlock",+ "samples": 30,+ "mean_credits": 50,+ "p50_credits": 40,+ "p95_credits": 80,+ },+ ]++ type_lookup = {"dyn-1": "second", "dyn-2": "cost_usd"}++ with (+ patch(+ "backend.data.block_cost_analytics.query_raw_with_schema",+ new_callable=AsyncMock,+ return_value=raw_rows,+ ),+ patch(+ "backend.data.block_cost_analytics._resolve_cost_type",+ side_effect=lambda bid: type_lookup.get(bid),+ ),+ ):+ out = await compute_block_cost_estimates(start=start, end=end)++ assert len(out) == 2+ assert out[0].block_id == "dyn-1"+ assert out[0].cost_type == "second"+ assert out[0].mean_credits == 7+ assert out[1].block_id == "dyn-2"+ assert out[1].cost_type == "cost_usd"+ assert out[1].mean_credits == 50+++async def test_window_cap_catches_fractional_overflow():+ """A window of 90 days + 1 hour must trip the cap — the older `.days`+ check truncated and silently let the over-cap window through."""+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=ANALYTICS_MAX_DAYS, hours=1)+ with pytest.raises(ValueError, match="exceeds max"):+ await compute_block_cost_estimates(start=start, end=end)+++async def test_min_samples_threshold_passed_to_sql_query():+ """`min_samples` is enforced by `HAVING COUNT(*) >= $3` in the SQL — the+ aggregator must forward the parameter unchanged so rows below the threshold+ never surface from the database."""+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=7)++ with (+ patch(+ "backend.data.block_cost_analytics.query_raw_with_schema",+ new_callable=AsyncMock,+ return_value=[],+ ) as mock_query,+ patch(+ "backend.data.block_cost_analytics._resolve_cost_type",+ return_value="second",+ ),+ ):+ await compute_block_cost_estimates(start=start, end=end, min_samples=42)++ # query_raw_with_schema(query, start, end, min_samples)+ args = mock_query.await_args.args+ assert args[3] == 42+++async def test_block_name_falls_back_to_block_id_when_metadata_missing():+ """Old USAGE rows may not have populated metadata->>'block', leading the+ raw SQL to return a NULL block_name. Verify we fall back to block_id+ rather than emitting `null`."""+ end = datetime.now(timezone.utc)+ start = end - timedelta(days=7)++ raw_rows = [+ {+ "block_id": "dyn-1",+ "block_name": None,+ "samples": 12,+ "mean_credits": 4,+ "p50_credits": 4,+ "p95_credits": 5,+ }+ ]++ with (+ patch(+ "backend.data.block_cost_analytics.query_raw_with_schema",+ new_callable=AsyncMock,+ return_value=raw_rows,+ ),+ patch(+ "backend.data.block_cost_analytics._resolve_cost_type",+ return_value="items",+ ),+ ):+ out = await compute_block_cost_estimates(start=start, end=end)++ assert len(out) == 1+ # Falls back to block_id when block_name is null in the historical row+ # (not the block class name — the resolver isn't called here because the+ # row already supplied a block_id; we just want a non-null label).+ assert out[0].block_name == "dyn-1"diff --git a/autogpt_platform/backend/backend/data/block_cost_config_test.py b/autogpt_platform/backend/backend/data/block_cost_config_test.pyindex 9af880cf04..80f603224b 100644--- a/autogpt_platform/backend/backend/data/block_cost_config_test.py+++ b/autogpt_platform/backend/backend/data/block_cost_config_test.py@@ -23,6 +23,7 @@ from backend.blocks.fal.ai_video_generator import AIVideoGeneratorBlockfrom backend.blocks.jina.chunking import JinaChunkingBlockfrom backend.blocks.youtube import TranscribeYoutubeVideoBlockfrom backend.data.block_cost_config import BLOCK_COSTS+from backend.executor import utils as executor_utilsfrom backend.executor.utils import block_usage_costfrom backend.integrations.credentials_store import (e2b_credentials,@@ -31,6 +32,16 @@ from backend.integrations.credentials_store import (webshare_proxy_credentials,)+[email protected](autouse=True)+def _stub_preflight_estimate(monkeypatch):+ """Force `get_preflight_estimate` to return 0 in this module's tests so+ the dynamic-cost (E2B SECOND / FAL SECOND) pre-flight assertions don't+ couple to a populated `block_preflight_estimates.json` once the seeding+ PR registers non-zero estimates for these blocks."""+ monkeypatch.setattr(executor_utils, "get_preflight_estimate", lambda _bid: 0)++ALL_AYRSHARE_BLOCKS = [PostToBlueskyBlock,PostToFacebookBlock,diff --git a/autogpt_platform/backend/backend/data/block_preflight_estimates.json b/autogpt_platform/backend/backend/data/block_preflight_estimates.jsonnew file mode 100644index 0000000000..e9cfd525bd--- /dev/null+++ b/autogpt_platform/backend/backend/data/block_preflight_estimates.json@@ -0,0 +1,6 @@+{+ "version": 1,+ "generated_at": null,+ "source_window_days": null,+ "estimates": {}+}diff --git a/autogpt_platform/backend/backend/data/block_preflight_estimates.py b/autogpt_platform/backend/backend/data/block_preflight_estimates.pynew file mode 100644index 0000000000..7097771046--- /dev/null+++ b/autogpt_platform/backend/backend/data/block_preflight_estimates.py@@ -0,0 +1,114 @@+"""Loader for the historical-average preflight cost estimates.++The JSON file is a snapshot of average credits-per-execution per block_id,+populated by an admin pulling fresh aggregates from `/admin/blocks/cost-estimates`+and committing the result. Used by `block_usage_cost()` to give dynamic-cost+blocks (SECOND/ITEMS/COST_USD) a non-zero pre-flight charge so post-flight+reconciliation only settles a small delta — bounding the billing-leak surface.+"""++from __future__ import annotations++import json+import logging+import math+from pathlib import Path+from typing import TypedDict++logger = logging.getLogger(__name__)++_ESTIMATES_PATH = Path(__file__).parent / "block_preflight_estimates.json"+++class BlockPreflightEstimate(TypedDict):+ block_name: str+ cost_type: str+ samples: int+ mean_credits: int+++_cache: dict[str, BlockPreflightEstimate] | None = None+_cache_mtime_ns: int | None = None+++def _load() -> dict[str, BlockPreflightEstimate]:+ """Return the estimates dict, refreshing if the JSON file has changed.++ The cache is keyed on the file's `mtime_ns` so a hot-swapped JSON (mounted+ config, manual edit on a long-running pod) is picked up on the next call+ without requiring a process restart. The fast path — file unchanged — is a+ single `stat()`.+ """+ global _cache, _cache_mtime_ns+ try:+ current_mtime = _ESTIMATES_PATH.stat().st_mtime_ns+ except OSError:+ # File missing or inaccessible — keep whatever's cached; if nothing+ # has ever loaded, return empty.+ return _cache if _cache is not None else {}++ if _cache is not None and _cache_mtime_ns == current_mtime:+ return _cache++ try:+ raw = json.loads(_ESTIMATES_PATH.read_text(encoding="utf-8"))+ except (json.JSONDecodeError, OSError):+ logger.exception(+ "Failed to load %s; preflight estimates disabled",+ _ESTIMATES_PATH.name,+ )+ # Don't bump _cache_mtime_ns on a failed parse — otherwise a same-mtime+ # in-place fix (low-resolution FS, atomic-rename to a buffer with the+ # same timestamp) leaves the empty cache poisoned forever.+ _cache = {}+ return _cache++ if not isinstance(raw, dict):+ logger.error(+ "Invalid estimates JSON root in %s (expected object); "+ "preflight estimates disabled",+ _ESTIMATES_PATH.name,+ )+ _cache = {}+ return _cache++ estimates = raw.get("estimates", {}) or {}+ loaded: dict[str, BlockPreflightEstimate] = (+ estimates if isinstance(estimates, dict) else {}+ )++ _cache = loaded+ _cache_mtime_ns = current_mtime+ return loaded+++def get_preflight_estimate(block_id: str) -> int:+ """Return the historical-average preflight cost for a block in credits.++ Returns 0 when no estimate is available (unseen block, low sample count,+ file missing, or malformed entry) so the caller falls back to the+ existing 0-preflight behaviour. Negative or non-numeric `mean_credits`+ are clamped to 0 — billing must never go negative on a corrupt entry.+ """+ entry = _load().get(block_id)+ if not entry:+ return 0+ raw_mean = entry.get("mean_credits", 0)+ try:+ mean = float(raw_mean)+ except (TypeError, ValueError):+ return 0+ # Python's `json.loads` accepts non-spec `NaN`/`Infinity`, so a corrupt+ # or hot-swapped JSON could land us with a non-finite mean — `int(round)`+ # raises on those. Clamp to 0 to keep the docstring's promise that no+ # JSON content can crash a billing call site.+ if not math.isfinite(mean):+ return 0+ return max(0, int(round(mean)))+++def reset_cache() -> None:+ """Test-only: drop the in-memory cache so a freshly-written JSON re-loads."""+ global _cache, _cache_mtime_ns+ _cache = None+ _cache_mtime_ns = Nonediff --git a/autogpt_platform/backend/backend/data/block_preflight_estimates_test.py b/autogpt_platform/backend/backend/data/block_preflight_estimates_test.pynew file mode 100644index 0000000000..4fe588628e--- /dev/null+++ b/autogpt_platform/backend/backend/data/block_preflight_estimates_test.py@@ -0,0 +1,201 @@+import json+from pathlib import Path++import pytest++from backend.data import block_preflight_estimates as bpe++[email protected](autouse=True)+def reset_cache():+ bpe.reset_cache()+ yield+ bpe.reset_cache()+++def test_missing_file_returns_zero(monkeypatch, tmp_path: Path):+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", tmp_path / "missing.json")+ assert bpe.get_preflight_estimate("any-id") == 0+++def test_malformed_json_falls_back_to_zero(monkeypatch, tmp_path: Path):+ bad = tmp_path / "estimates.json"+ bad.write_text("{ not valid json")+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", bad)+ assert bpe.get_preflight_estimate("any-id") == 0+++def test_returns_estimate_for_known_block(monkeypatch, tmp_path: Path):+ f = tmp_path / "estimates.json"+ f.write_text(+ json.dumps(+ {+ "version": 1,+ "estimates": {+ "block-1": {+ "block_name": "FooBlock",+ "cost_type": "SECOND",+ "samples": 50,+ "mean_credits": 7,+ }+ },+ }+ )+ )+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("block-1") == 7+ assert bpe.get_preflight_estimate("block-2") == 0+++def test_cache_refreshes_on_file_mtime_change(monkeypatch, tmp_path: Path):+ """Hot-swapping the JSON on a running pod must be picked up on the next+ call, without a process restart — keyed on file mtime."""+ import os+ import time++ f = tmp_path / "estimates.json"+ f.write_text(+ json.dumps(+ {+ "version": 1,+ "estimates": {+ "block-1": {+ "block_name": "FooBlock",+ "cost_type": "SECOND",+ "samples": 50,+ "mean_credits": 7,+ }+ },+ }+ )+ )+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("block-1") == 7++ # Rewrite with a newer mtime — cache must refresh on the next call.+ new_payload = {+ "version": 1,+ "estimates": {+ "block-1": {+ "block_name": "FooBlock",+ "cost_type": "SECOND",+ "samples": 50,+ "mean_credits": 999,+ }+ },+ }+ f.write_text(json.dumps(new_payload))+ # Bump mtime to guarantee the stat-based cache key invalidates even on+ # filesystems with second-resolution mtime.+ later = time.time() + 1+ os.utime(f, (later, later))++ assert bpe.get_preflight_estimate("block-1") == 999+++def test_negative_or_non_numeric_mean_clamps_to_zero(monkeypatch, tmp_path: Path):+ """Corrupt entries must never produce negative billing pre-flight."""+ f = tmp_path / "estimates.json"+ f.write_text(+ json.dumps(+ {+ "version": 1,+ "estimates": {+ "negative": {+ "block_name": "Bad",+ "cost_type": "SECOND",+ "samples": 10,+ "mean_credits": -42,+ },+ "non_numeric": {+ "block_name": "Bad",+ "cost_type": "SECOND",+ "samples": 10,+ "mean_credits": "oops",+ },+ "fractional": {+ "block_name": "Ok",+ "cost_type": "SECOND",+ "samples": 10,+ "mean_credits": 2.6,+ },+ },+ }+ )+ )+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("negative") == 0+ assert bpe.get_preflight_estimate("non_numeric") == 0+ # Round, don't truncate — 2.6 → 3.+ assert bpe.get_preflight_estimate("fractional") == 3+++def test_non_object_root_disables_estimates(monkeypatch, tmp_path: Path):+ f = tmp_path / "estimates.json"+ f.write_text("[1, 2, 3]")+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("anything") == 0+++def test_corrupt_json_does_not_poison_cache_when_fixed_in_place(+ monkeypatch, tmp_path: Path+):+ """Atomic-rename / same-mtime fixes must recover. Bumping `_cache_mtime_ns`+ on a failed parse would leave an empty cache pinned until the file's+ mtime advances — on low-resolution filesystems or atomic-rename flows+ that can pin to the corrupt timestamp indefinitely.+ """+ import os++ f = tmp_path / "estimates.json"+ f.write_text("{ not valid json")+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("block-1") == 0+ poisoned_mtime = f.stat().st_mtime++ # Fix the file but pin the mtime to the corrupt-version timestamp —+ # simulates a low-resolution FS or atomic-rename flow that doesn't+ # bump the timestamp.+ f.write_text(+ json.dumps(+ {+ "version": 1,+ "estimates": {+ "block-1": {+ "block_name": "FooBlock",+ "cost_type": "SECOND",+ "samples": 50,+ "mean_credits": 11,+ }+ },+ }+ )+ )+ os.utime(f, (poisoned_mtime, poisoned_mtime))++ assert bpe.get_preflight_estimate("block-1") == 11+++def test_non_finite_mean_clamps_to_zero(monkeypatch, tmp_path: Path):+ """Python's json.loads accepts non-spec NaN/Infinity; a hot-swapped or+ corrupted JSON containing those would crash `int(round(...))`. Verify+ they clamp to 0 instead — the docstring promises no JSON content can+ crash a billing call site.+ """+ f = tmp_path / "estimates.json"+ # Write the file directly with NaN/Infinity literals — Python's json.loads+ # accepts these even though spec JSON forbids them.+ f.write_text(+ '{"version": 1, "estimates": {'+ '"nan_block": {"block_name": "Bad", "cost_type": "SECOND",'+ ' "samples": 10, "mean_credits": NaN},'+ '"inf_block": {"block_name": "Bad", "cost_type": "SECOND",'+ ' "samples": 10, "mean_credits": Infinity},'+ '"neg_inf_block": {"block_name": "Bad", "cost_type": "SECOND",'+ ' "samples": 10, "mean_credits": -Infinity}'+ "}}"+ )+ monkeypatch.setattr(bpe, "_ESTIMATES_PATH", f)+ assert bpe.get_preflight_estimate("nan_block") == 0+ assert bpe.get_preflight_estimate("inf_block") == 0+ assert bpe.get_preflight_estimate("neg_inf_block") == 0diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.pyindex 88c762cdad..01c33504d7 100644--- a/autogpt_platform/backend/backend/data/execution.py+++ b/autogpt_platform/backend/backend/data/execution.py@@ -1282,6 +1282,13 @@ class NodeExecutionEntry(BaseModel):block_id: strinputs: BlockInputexecution_context: ExecutionContext = Field(default_factory=ExecutionContext)+ # Block-only pre-flight credits actually billed by `charge_usage`. Set by+ # the dispatcher right after the charge so reconciliation can pin its+ # baseline to the value spent on the wallet, instead of re-reading the+ # estimates JSON (which could have been hot-swapped between charge and+ # reconcile, leaving the delta computed against a different number).+ # Excluded from dumps — in-memory only, scoped to a single live execution.+ pre_flight_charge: Optional[int] = Field(default=None, exclude=True)class ExecutionQueue(Generic[T]):diff --git a/autogpt_platform/backend/backend/executor/billing.py b/autogpt_platform/backend/backend/executor/billing.pyindex 58e89533c5..10d1567bd3 100644--- a/autogpt_platform/backend/backend/executor/billing.py+++ b/autogpt_platform/backend/backend/executor/billing.py@@ -118,13 +118,23 @@ def resolve_block_cost(def charge_usage(node_exec: NodeExecutionEntry,execution_count: int,-) -> tuple[int, int]:+) -> tuple[int, int, int]:+ """Pre-flight charge for a node execution.++ Returns ``(total_cost, remaining_balance, block_pre_flight_cost)``.+ ``block_pre_flight_cost`` is the block-only portion (excludes the+ every-N-blocks ``execution_usage_cost``) so reconciliation can settle+ its delta against the exact value charged here, not a re-fetched one+ that may have drifted if the estimates JSON was hot-swapped between+ the two calls.+ """total_cost = 0remaining_balance = 0+ block_pre_flight = 0db_client = get_db_client()block, cost, matching_filter = resolve_block_cost(node_exec)if not block:- return total_cost, 0+ return total_cost, 0, block_pre_flightif cost > 0:remaining_balance = db_client.spend_credits(@@ -142,6 +152,7 @@ def charge_usage(),)total_cost += cost+ block_pre_flight = costelif _block_has_paid_cost_entry(block, node_exec.inputs):# Dynamic-cost blocks (SECOND/ITEMS/COST_USD) compute 0 pre-flight# because the real charge is settled post-flight against stats. Guard@@ -183,12 +194,13 @@ def charge_usage()total_cost += cost- return total_cost, remaining_balance+ return total_cost, remaining_balance, block_pre_flightasync def charge_reconciled_usage(node_exec: NodeExecutionEntry,stats: NodeExecutionStats,+ pre_flight_charge: int | None = None,) -> tuple[int, int]:"""Charge the dynamic portion of a block's cost from its execution stats.@@ -209,6 +221,12 @@ async def charge_reconciled_usage(Called once per node execution AFTER the block has finished running andstats (walltime, tokens, provider_cost) are populated. Swallows anyunexpected exception so reconciliation never poisons the success path.++ ``pre_flight_charge`` lets the caller pin the baseline to the exact+ amount that was actually billed in `charge_usage`, instead of re-reading+ the historical-average JSON. Without this, a hot-swap of+ ``block_preflight_estimates.json`` between charge and reconciliation+ would shift the baseline and skew the delta."""try:db_client = get_database_manager_async_client()@@ -216,7 +234,10 @@ async def charge_reconciled_usage(if not block:return 0, 0- pre_flight, _ = block_usage_cost(block=block, input_data=node_exec.inputs)+ if pre_flight_charge is None:+ pre_flight, _ = block_usage_cost(block=block, input_data=node_exec.inputs)+ else:+ pre_flight = pre_flight_chargepost_flight, matching_filter = block_usage_cost(block=block, input_data=node_exec.inputs, stats=stats)@@ -291,7 +312,7 @@ async def charge_node_usage(node_exec: NodeExecutionEntry) -> tuple[int, int]:"""def _run():- total_cost, remaining = charge_usage(node_exec, 0)+ total_cost, remaining, _ = charge_usage(node_exec, 0)if total_cost > 0:handle_low_balance(get_db_client(), node_exec.user_id, remaining, total_costdiff --git a/autogpt_platform/backend/backend/executor/billing_reconciliation_test.py b/autogpt_platform/backend/backend/executor/billing_reconciliation_test.pyindex 4e0e27546b..deb440cee2 100644--- a/autogpt_platform/backend/backend/executor/billing_reconciliation_test.py+++ b/autogpt_platform/backend/backend/executor/billing_reconciliation_test.py@@ -99,10 +99,14 @@ def test_dynamic_cost_block_with_positive_balance_starts(tmp_block_costs_overridpatch("backend.executor.billing.get_db_client", return_value=db_client),patch("backend.executor.billing.get_block", return_value=SearchTheWebBlock()),):- total_cost, remaining = charge_usage(exec_entry, execution_count=0)+ total_cost, remaining, block_pre_flight = charge_usage(+ exec_entry, execution_count=0+ )assert total_cost == 0assert remaining == 50+ # Dynamic-cost block returns 0 pre-flight; reconciliation will settle.+ assert block_pre_flight == 0db_client.spend_credits.assert_not_called()@@ -330,3 +334,59 @@ async def test_tokens_cost_refunds_when_actual_below_estimate(tmp_block_costs_ov# Refunds can't push the user below the low-balance threshold, so# handle_low_balance must not fire here.handle_lb.assert_not_called()+++async def test_pre_flight_charge_override_pins_reconciliation_baseline(+ tmp_block_costs_override,+):+ """Reconciliation must use the caller-supplied ``pre_flight_charge``+ instead of recomputing from ``block_usage_cost`` — protects against a+ hot-swap of ``block_preflight_estimates.json`` between charge and+ reconcile that would otherwise shift the delta baseline.++ Setup mimics the race: the executor charged 3 credits up front (captured+ on ``NodeExecutionEntry.pre_flight_charge``); a (counterfactual) JSON+ edit during execution would now make ``block_usage_cost`` return 100 for+ the same inputs. Without the override, delta would be ``post - 100``;+ with the override, delta = ``post - 3``.+ """+ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ exec_entry.pre_flight_charge = 3 # actual amount billed+ # post-flight: $0.05 * 100 = 5 credits+ stats = NodeExecutionStats(provider_cost=0.05, provider_cost_type="cost_usd")++ db_client = _async_db_client(spend_credits_return=42)+ with (+ patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ),+ patch("backend.executor.billing.get_db_client", return_value=MagicMock()),+ patch("backend.executor.billing.handle_low_balance"),+ # If the override is honored, this MUST NOT be called for the+ # pre-flight branch (stats=None). It is still called once for the+ # post-flight branch (stats=...). Use a spy that delegates to the+ # real implementation.+ patch(+ "backend.data.block_preflight_estimates.get_preflight_estimate"+ ) as get_preflight,+ ):+ get_preflight.return_value = 100 # would-be hot-swapped value+ delta, _ = await charge_reconciled_usage(+ exec_entry,+ stats,+ pre_flight_charge=exec_entry.pre_flight_charge,+ )++ # Delta must be (post 5) - (pinned pre 3) = 2, NOT (post 5) - (drifted 100) = -95+ assert delta == 2+ # The pinned baseline must short-circuit the JSON re-read for pre-flight.+ # `get_preflight_estimate` is also reachable from the post-flight path+ # (where stats are populated, dynamic types skip it), so 0 calls is the+ # right assertion: with stats provided, post-flight goes through the+ # COST_USD post-flight branch which doesn't touch get_preflight_estimate.+ assert get_preflight.call_count == 0diff --git a/autogpt_platform/backend/backend/executor/block_usage_cost_test.py b/autogpt_platform/backend/backend/executor/block_usage_cost_test.pyindex c9d4dd8f3b..fe0cf14732 100644--- a/autogpt_platform/backend/backend/executor/block_usage_cost_test.py+++ b/autogpt_platform/backend/backend/executor/block_usage_cost_test.py@@ -21,6 +21,7 @@ from backend.data.block_cost_config import (TokenRate,)from backend.data.model import NodeExecutionStats+from backend.executor import utils as executor_utilsfrom backend.executor.utils import block_usage_costfrom backend.integrations.credentials_store import (anthropic_credentials,@@ -31,6 +32,18 @@ from backend.integrations.credentials_store import ()[email protected](autouse=True)+def _stub_preflight_estimate(monkeypatch):+ """Force `get_preflight_estimate` to return 0 in this module's tests so+ they don't accidentally couple to a populated+ `block_preflight_estimates.json` once the admin export tool seeds it.+ Tests that want a non-zero estimate (e.g.+ `test_preflight_uses_historical_estimate_for_dynamic_cost_types`) override+ this themselves on `executor_utils.get_preflight_estimate`.+ """+ monkeypatch.setattr(executor_utils, "get_preflight_estimate", lambda _bid: 0)++@pytest.fixturedef tmp_block_costs_override(monkeypatch):"""Swap out BLOCK_COSTS[SearchTheWebBlock] for the duration of a test."""@@ -123,6 +136,52 @@ def test_cost_usd_zero_when_no_stats(tmp_block_costs_override):assert cost == 0+def test_preflight_estimate_disabled_for_no_reconciliation_callers(+ tmp_block_costs_override, monkeypatch+):+ """The direct block-execute API endpoints bypass the executor manager+ and have no post-flight reconciliation. They MUST pass+ `use_preflight_estimate=False` so dynamic-cost blocks return 0 instead+ of locking in an unreconciled estimate as the final charge.+ """+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.SECOND, cost_divisor=10)]+ )+ block = SearchTheWebBlock()++ # Even with a registered estimate, the flag wins.+ monkeypatch.setattr(executor_utils, "get_preflight_estimate", lambda _bid: 999)++ cost, _ = block_usage_cost(block, {}, use_preflight_estimate=False)+ assert cost == 0++ # Sanity: with the flag default-True, the same call returns the estimate.+ cost_with_estimate, _ = block_usage_cost(block, {})+ assert cost_with_estimate == 999+++def test_preflight_uses_historical_estimate_for_dynamic_cost_types(+ tmp_block_costs_override, monkeypatch+):+ """When stats is None and run_time=0, dynamic-cost branches return the+ registered historical-average estimate instead of 0 — so the post-flight+ reconciliation only settles a small delta and a billing leak is bounded+ by that delta rather than the full execution cost."""+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.SECOND, cost_divisor=10)]+ )+ block = SearchTheWebBlock()++ monkeypatch.setattr(+ executor_utils,+ "get_preflight_estimate",+ lambda block_id: 5 if block_id == block.id else 0,+ )++ cost, _ = block_usage_cost(block, {})+ assert cost == 5++def test_cost_usd_ignores_non_usd_provider_cost(tmp_block_costs_override):"""provider_cost_type='items' should not be mistaken for dollars."""tmp_block_costs_override(diff --git a/autogpt_platform/backend/backend/executor/cost_tracking.py b/autogpt_platform/backend/backend/executor/cost_tracking.pyindex 7e6c9a5884..16e64f0215 100644--- a/autogpt_platform/backend/backend/executor/cost_tracking.py+++ b/autogpt_platform/backend/backend/executor/cost_tracking.py@@ -234,7 +234,13 @@ async def log_system_credential_cost(model_name = _extract_model_name(input_data.get("model"))- credit_cost, _ = block_usage_cost(block=block, input_data=input_data)+ # Pass stats so post-flight branches (SECOND/ITEMS/COST_USD/TOKENS)+ # compute the *actual* charge from the captured stats. Without this+ # the call hits the pre-flight path and records the historical+ # estimate, skewing platform-cost analytics.+ credit_cost, _ = block_usage_cost(+ block=block, input_data=input_data, stats=stats+ )provider_name = cred_data.get("provider", "unknown")tracking_type, tracking_amount = resolve_tracking(diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 3ed5dcf1db..e9e8ad114d 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -626,6 +626,7 @@ class ExecutionProcessor:reconciled_delta, _ = await billing.charge_reconciled_usage(node_exec=node_exec,stats=execution_stats,+ pre_flight_charge=node_exec.pre_flight_charge,)if reconciled_delta != 0:execution_stats.reconciled_cost_delta += reconciled_delta@@ -1014,12 +1015,20 @@ class ExecutionProcessor:# Charge usage (may raise) — skipped for dry runstry:if not graph_exec.execution_context.dry_run:- cost, remaining_balance = billing.charge_usage(+ (+ cost,+ remaining_balance,+ block_pre_flight,+ ) = billing.charge_usage(node_exec=queued_node_exec,execution_count=increment_execution_count(graph_exec.user_id),)+ # Pin the reconciliation baseline to what was just+ # billed — protects against a hot-swap of the+ # estimates JSON between charge and reconcile.+ queued_node_exec.pre_flight_charge = block_pre_flightwith execution_stats_lock:execution_stats.cost += cost# Check if we crossed the low balance thresholddiff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.pyindex ff676842b1..563053f783 100644--- a/autogpt_platform/backend/backend/executor/utils.py+++ b/autogpt_platform/backend/backend/executor/utils.py@@ -22,6 +22,7 @@ from backend.data import workspace as workspace_db# Import dynamic field utilities from centralized locationfrom backend.data.block import BlockInput, BlockOutputEntryfrom backend.data.block_cost_config import BLOCK_COSTS, compute_token_credits+from backend.data.block_preflight_estimates import get_preflight_estimatefrom backend.data.credit import UsageTransactionMetadata, get_user_credit_modelfrom backend.data.db import prismafrom backend.data.dynamic_fields import merge_execution_input@@ -125,24 +126,35 @@ def block_usage_cost(data_size: float = 0,run_time: float = 0,stats: NodeExecutionStats | None = None,+ use_preflight_estimate: bool = True,) -> tuple[int, BlockInput]:"""Calculate the credit charge for a block invocation.Two calling contexts:- - Pre-flight (no stats): charge the fixed floor / estimate. Dynamic cost- types (ITEMS/COST_USD/TOKENS) return 0 here so they don't block- execution on a balance check when we don't yet know the true cost.+ - Pre-flight (no stats): charge the fixed floor / historical-average+ estimate. SECOND/ITEMS/COST_USD types fall back to 0 only when no+ estimate is registered for the block in `block_preflight_estimates.json`.- Post-flight (stats populated): dynamic types consume the capturedstats to compute the actual charge.For SECOND/ITEMS/TOKENS cost entries, ``cost_amount`` is interpreted as"credits per ``cost_divisor`` units" — e.g. ``cost_amount=1,cost_divisor=10`` under SECOND means "1 credit per 10 seconds".++ ``use_preflight_estimate`` (default True) enables the historical-average+ pre-flight charge for SECOND/ITEMS/COST_USD types. Callers that have NO+ post-flight reconciliation step (e.g. the direct block-execute API+ endpoints, which bypass the executor manager) MUST pass False — otherwise+ the estimate becomes the final charge and users are over- or undercharged+ relative to their actual run cost. The executor pre-flight path keeps the+ default since reconciliation always follows there."""block_costs = BLOCK_COSTS.get(type(block))if not block_costs:return 0, {}+ is_preflight = stats is None and run_time == 0 and use_preflight_estimate+for block_cost in block_costs:if not _is_cost_filter_match(block_cost.cost_filter, input_data):continue@@ -157,6 +169,8 @@ def block_usage_cost()if block_cost.cost_type == BlockCostType.SECOND:+ if is_preflight:+ return get_preflight_estimate(block.id), block_cost.cost_filter# Ceil so partial divisor-units still bill — avoids 0-credit leaks# on sub-divisor runs (e.g. 1s on a `1cr / 3s` block).seconds = _coerce_seconds(run_time, stats)@@ -168,6 +182,8 @@ def block_usage_cost(return credits, block_cost.cost_filterif block_cost.cost_type == BlockCostType.ITEMS:+ if is_preflight:+ return get_preflight_estimate(block.id), block_cost.cost_filter# Ceil so partial buckets still bill — avoids 0-credit leaks on# single-item returns under a >1 divisor (e.g. Apollo 1cr/2-items).items = _coerce_items(stats)@@ -179,6 +195,8 @@ def block_usage_cost(return credits, block_cost.cost_filterif block_cost.cost_type == BlockCostType.COST_USD:+ if is_preflight:+ return get_preflight_estimate(block.id), block_cost.cost_filterusd = _coerce_usd(stats)return (max(0, math.ceil(usd * block_cost.cost_amount)),@@ -210,13 +228,17 @@ async def charge_for_direct_block_execution(``reason`` so transactions remain attributable to the originatingsurface.- Dynamic-cost blocks (TOKENS / COST_USD / SECOND / ITEMS) return 0- from ``block_usage_cost`` pre-flight and are NOT charged on this- code path — the post-flight reconciliation only runs inside- ``executor/manager.py``, which the direct block-execute API- endpoints bypass entirely.+ Dynamic-cost blocks (TOKENS / COST_USD / SECOND / ITEMS) are NOT charged+ on this code path — they return 0 from ``block_usage_cost`` because we+ pass ``use_preflight_estimate=False``. The estimate path is only safe+ when post-flight reconciliation follows (executor/manager.py); the+ direct block-execute API endpoints bypass the manager and have no+ reconciliation step, so charging the estimate would lock in an+ incorrect amount with no chance to settle the delta."""- cost, cost_filter = block_usage_cost(block, input_data)+ cost, cost_filter = block_usage_cost(+ block, input_data, use_preflight_estimate=False+ )if cost <= 0:returncredit_model = await get_user_credit_model(user_id)diff --git a/autogpt_platform/backend/scripts/seed_block_preflight_estimates.py b/autogpt_platform/backend/scripts/seed_block_preflight_estimates.pynew file mode 100644index 0000000000..4ede5449f2--- /dev/null+++ b/autogpt_platform/backend/scripts/seed_block_preflight_estimates.py@@ -0,0 +1,95 @@+"""Standalone seeder for block_preflight_estimates.json.++Runs the same aggregation as ``GET /api/admin/blocks/cost-estimates`` against+``DATABASE_URL`` via Prisma, then writes the result to+``backend/data/block_preflight_estimates.json``. Use this to seed the file+before the admin endpoint is deployed (chicken-and-egg) or to refresh from a+machine with read-only prod credentials.++Usage::++ DATABASE_URL=postgresql://... \\+ poetry run python scripts/seed_block_preflight_estimates.py --days 7++If ``--dry-run`` is set, prints the JSON to stdout without touching the file.+"""++from __future__ import annotations++import argparse+import asyncio+import json+import os+from datetime import datetime, timedelta, timezone+from pathlib import Path++REPO_BACKEND = Path(__file__).resolve().parent.parent+ESTIMATES_PATH = REPO_BACKEND / "backend" / "data" / "block_preflight_estimates.json"+++async def main(days: int, min_samples: int, write: bool) -> None:+ if days <= 0:+ raise SystemExit("--days must be a positive integer")+ if not os.environ.get("DATABASE_URL"):+ raise SystemExit("DATABASE_URL must be set")++ # Imported lazily so --help works without the backend env being loaded.+ from backend.data.block_cost_analytics import compute_block_cost_estimates+ from backend.data.db import connect, disconnect++ end = datetime.now(timezone.utc)+ start = end - timedelta(days=days)++ await connect()+ try:+ rows = await compute_block_cost_estimates(+ start=start, end=end, min_samples=min_samples+ )+ finally:+ await disconnect()++ estimates: dict[str, dict[str, object]] = {+ r.block_id: {+ "block_name": r.block_name,+ "cost_type": r.cost_type,+ "samples": r.samples,+ "mean_credits": r.mean_credits,+ }+ for r in rows+ }++ payload = {+ "version": 1,+ "generated_at": end.isoformat(),+ "source_window_days": days,+ "estimates": estimates,+ }+ serialised = json.dumps(payload, indent=2) + "\n"++ print(+ f"Aggregated {len(estimates)} dynamic-cost blocks over {days}d "+ f"with min_samples={min_samples}."+ )++ if write:+ ESTIMATES_PATH.write_text(serialised, encoding="utf-8")+ print(f"Wrote {ESTIMATES_PATH}")+ else:+ print(serialised)+++if __name__ == "__main__":+ parser = argparse.ArgumentParser()+ parser.add_argument("--days", type=int, default=7)+ parser.add_argument(+ "--min-samples",+ type=int,+ default=int(os.environ.get("BLOCK_PREFLIGHT_MIN_SAMPLES", "10")),+ )+ parser.add_argument(+ "--dry-run",+ action="store_true",+ help="Print the JSON to stdout instead of writing the file.",+ )+ args = parser.parse_args()+ asyncio.run(main(args.days, args.min_samples, write=not args.dry_run))diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/BlockCostEstimatesContent.test.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/BlockCostEstimatesContent.test.tsxnew file mode 100644index 0000000000..447f1d4a09--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/BlockCostEstimatesContent.test.tsx@@ -0,0 +1,144 @@+import {+ render,+ screen,+ fireEvent,+ waitFor,+ cleanup,+} from "@/tests/integrations/test-utils";+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";+import { ApiError } from "@/lib/autogpt-server-api/helpers";+import { BlockCostEstimatesContent } from "../components/BlockCostEstimatesContent";++const toastSpy = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", () => ({+ useToast: () => ({ toast: toastSpy }),+}));++const exportSpy = vi.fn();+vi.mock("@/app/api/__generated__/endpoints/admin/admin", () => ({+ getV2ExportBlockCostEstimates: (+ ...args: Parameters<typeof exportSpy>+ ): ReturnType<typeof exportSpy> => exportSpy(...args),+}));++beforeEach(() => {+ toastSpy.mockReset();+ exportSpy.mockReset();+});++afterEach(() => {+ cleanup();+});++const sampleResponse = {+ status: 200,+ data: {+ estimates: [+ {+ block_id: "blk-1",+ block_name: "SearchTheWebBlock",+ cost_type: "second",+ samples: 200,+ mean_credits: 7,+ p50_credits: 6,+ p95_credits: 12,+ },+ ],+ total_rows: 1,+ window_days: 7,+ max_window_days: 90,+ min_samples: 10,+ generated_at: "2026-05-07T13:00:00.000Z",+ },+};++describe("BlockCostEstimatesContent", () => {+ test("renders date inputs, min-samples and Aggregate button", () => {+ render(<BlockCostEstimatesContent />);+ expect(screen.getByLabelText(/Start date/i)).toBeDefined();+ expect(screen.getByLabelText(/End date/i)).toBeDefined();+ expect(screen.getByLabelText(/Min samples/i)).toBeDefined();+ expect(screen.getByRole("button", { name: /Aggregate/i })).toBeDefined();+ });++ test("Download JSON is disabled until aggregation succeeds", () => {+ render(<BlockCostEstimatesContent />);+ const download = screen.getByRole("button", {+ name: /Download JSON/i,+ }) as HTMLButtonElement;+ expect(download.disabled).toBe(true);+ });++ test("aggregates and renders the table on success", async () => {+ exportSpy.mockResolvedValue(sampleResponse);+ render(<BlockCostEstimatesContent />);+ fireEvent.click(screen.getByRole("button", { name: /Aggregate/i }));+ await waitFor(() => expect(exportSpy).toHaveBeenCalledTimes(1));+ await waitFor(() =>+ expect(screen.getByText(/SearchTheWebBlock/)).toBeDefined(),+ );+ expect(screen.getByText(/blk-1/)).toBeDefined();+ expect(toastSpy).toHaveBeenCalledWith(+ expect.objectContaining({ title: "Aggregation complete" }),+ );+ });++ test("downloads JSON after a successful aggregation", async () => {+ exportSpy.mockResolvedValue(sampleResponse);+ const createUrl = vi+ .spyOn(URL, "createObjectURL")+ .mockReturnValue("blob:mock");+ const revokeUrl = vi+ .spyOn(URL, "revokeObjectURL")+ .mockImplementation(() => {});+ const click = vi+ .spyOn(HTMLAnchorElement.prototype, "click")+ .mockImplementation(() => {});++ render(<BlockCostEstimatesContent />);+ fireEvent.click(screen.getByRole("button", { name: /Aggregate/i }));+ await waitFor(() => expect(exportSpy).toHaveBeenCalledTimes(1));+ await waitFor(() => screen.getByText(/SearchTheWebBlock/));++ fireEvent.click(screen.getByRole("button", { name: /Download JSON/i }));+ await waitFor(() => expect(click).toHaveBeenCalledTimes(1));+ expect(createUrl).toHaveBeenCalledTimes(1);++ createUrl.mockRestore();+ revokeUrl.mockRestore();+ click.mockRestore();+ });++ test("surfaces a 400 detail in the toast when the API rejects the window", async () => {+ exportSpy.mockRejectedValue(+ new ApiError("window 200d exceeds max 90d", 400, {+ detail: "window 200d exceeds max 90d",+ }),+ );+ render(<BlockCostEstimatesContent />);+ fireEvent.click(screen.getByRole("button", { name: /Aggregate/i }));+ await waitFor(() => expect(exportSpy).toHaveBeenCalledTimes(1));+ await waitFor(() =>+ expect(toastSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ title: "Window too large",+ variant: "destructive",+ }),+ ),+ );+ });++ test("falls back to a generic toast on non-ApiError failures", async () => {+ exportSpy.mockRejectedValue(new Error("network blip"));+ render(<BlockCostEstimatesContent />);+ fireEvent.click(screen.getByRole("button", { name: /Aggregate/i }));+ await waitFor(() =>+ expect(toastSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ title: "Aggregation failed",+ description: "network blip",+ }),+ ),+ );+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/helpers.test.tsnew file mode 100644index 0000000000..cd96685b7a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/__tests__/helpers.test.ts@@ -0,0 +1,111 @@+import { describe, expect, test, vi } from "vitest";+import {+ buildEstimatesJson,+ dateInputToUtcIso,+ dateInputToUtcIsoEnd,+ defaultEndDate,+ defaultStartDate,+ downloadJson,+} from "../helpers";++describe("dateInputToUtcIso", () => {+ test("returns null on empty input", () => {+ expect(dateInputToUtcIso("")).toBeNull();+ });++ test("converts YYYY-MM-DD to ISO 8601 UTC midnight", () => {+ expect(dateInputToUtcIso("2026-05-07")).toBe("2026-05-07T00:00:00.000Z");+ });+});++describe("dateInputToUtcIsoEnd", () => {+ test("returns null on empty input", () => {+ expect(dateInputToUtcIsoEnd("")).toBeNull();+ });++ test("pins to end-of-day so the inclusive end filter covers the whole day", () => {+ expect(dateInputToUtcIsoEnd("2026-05-07")).toBe("2026-05-07T23:59:59.999Z");+ });+});++describe("defaultStartDate", () => {+ test("subtracts 6 days so the inclusive [start, end] window is exactly 7 days", () => {+ // Mock Date so the assertion is stable regardless of when CI runs.+ vi.useFakeTimers();+ vi.setSystemTime(new Date("2026-05-07T12:00:00Z"));+ try {+ expect(defaultStartDate()).toBe("2026-05-01");+ expect(defaultEndDate()).toBe("2026-05-07");+ } finally {+ vi.useRealTimers();+ }+ });+});++describe("buildEstimatesJson", () => {+ test("serialises rows into the JSON file shape with stable key order", () => {+ const json = buildEstimatesJson(+ [+ {+ block_id: "blk-1",+ block_name: "Foo",+ cost_type: "second",+ samples: 10,+ mean_credits: 7,+ p50_credits: 6,+ p95_credits: 12,+ },+ ],+ "2026-05-07T13:00:00.000Z",+ 7,+ );+ const parsed = JSON.parse(json);+ expect(parsed.version).toBe(1);+ expect(parsed.generated_at).toBe("2026-05-07T13:00:00.000Z");+ expect(parsed.source_window_days).toBe(7);+ expect(parsed.estimates["blk-1"]).toEqual({+ block_name: "Foo",+ cost_type: "second",+ samples: 10,+ mean_credits: 7,+ });+ // p50/p95 don't enter the runtime JSON — they're for the admin UI only.+ expect(parsed.estimates["blk-1"].p50_credits).toBeUndefined();+ expect(json.endsWith("\n")).toBe(true);+ });++ test("emits an empty estimates object for zero rows", () => {+ const json = buildEstimatesJson([], "2026-05-07T13:00:00.000Z", 7);+ expect(JSON.parse(json).estimates).toEqual({});+ });+});++describe("downloadJson", () => {+ test("creates a Blob URL, triggers a click, and revokes the URL", async () => {+ const createUrl = vi+ .spyOn(URL, "createObjectURL")+ .mockReturnValue("blob:mock");+ const revokeUrl = vi+ .spyOn(URL, "revokeObjectURL")+ .mockImplementation(() => {});+ const click = vi+ .spyOn(HTMLAnchorElement.prototype, "click")+ .mockImplementation(() => {});++ try {+ downloadJson('{"hello":"world"}\n', "test.json");++ expect(createUrl).toHaveBeenCalledTimes(1);+ expect(click).toHaveBeenCalledTimes(1);+ // The setTimeout(0) revoke fires on the next macrotask — flush it so the+ // assertion is deterministic and the test name's promise is honored.+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(revokeUrl).toHaveBeenCalledWith("blob:mock");+ } finally {+ // Failure-safe restore: spies survive even if the assertions above throw.+ createUrl.mockRestore();+ revokeUrl.mockRestore();+ click.mockRestore();+ }+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/BlockCostEstimatesContent.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/BlockCostEstimatesContent.tsxnew file mode 100644index 0000000000..6acfd48914--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/BlockCostEstimatesContent.tsx@@ -0,0 +1,145 @@+"use client";++import { DownloadSimpleIcon } from "@phosphor-icons/react";+import { Button } from "@/components/atoms/Button/Button";+import { buildEstimatesJson, downloadJson } from "../helpers";+import { useBlockCostEstimates } from "./useBlockCostEstimates";++export function BlockCostEstimatesContent() {+ const {+ start,+ end,+ minSamples,+ data,+ loading,+ setStart,+ setEnd,+ setMinSamples,+ fetchEstimates,+ } = useBlockCostEstimates();++ function handleDownload() {+ if (!data) return;+ const generatedAtIso =+ data.generated_at instanceof Date+ ? data.generated_at.toISOString()+ : String(data.generated_at);+ const json = buildEstimatesJson(+ data.estimates,+ generatedAtIso,+ data.window_days,+ );+ downloadJson(json, `block_preflight_estimates_${start}_${end}.json`);+ }++ return (+ <div className="flex flex-col gap-4">+ <div className="flex flex-wrap items-end gap-3 rounded border p-4">+ <div className="flex flex-col gap-1">+ <label htmlFor="bce-start" className="text-sm">+ Start date (UTC)+ </label>+ <input+ id="bce-start"+ type="date"+ className="rounded border px-3 py-1.5 text-sm"+ value={start}+ onChange={(e) => setStart(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1">+ <label htmlFor="bce-end" className="text-sm">+ End date (UTC)+ </label>+ <input+ id="bce-end"+ type="date"+ className="rounded border px-3 py-1.5 text-sm"+ value={end}+ onChange={(e) => setEnd(e.target.value)}+ />+ </div>+ <div className="flex flex-col gap-1">+ <label htmlFor="bce-min-samples" className="text-sm">+ Min samples+ </label>+ <input+ id="bce-min-samples"+ type="number"+ min={1}+ className="w-28 rounded border px-3 py-1.5 text-sm"+ value={minSamples}+ onChange={(e) =>+ setMinSamples(Math.max(1, Number(e.target.value) || 1))+ }+ />+ </div>+ <Button+ variant="primary"+ size="small"+ onClick={fetchEstimates}+ loading={loading}+ >+ Aggregate+ </Button>+ <Button+ variant="secondary"+ size="small"+ onClick={handleDownload}+ disabled={!data || data.total_rows === 0}+ leftIcon={<DownloadSimpleIcon weight="bold" />}+ >+ Download JSON+ </Button>+ </div>++ {data ? (+ <div className="flex flex-col gap-2">+ <p className="text-sm text-muted-foreground">+ {data.total_rows} blocks · window {data.window_days}d (cap{" "}+ {data.max_window_days}d) · min samples {data.min_samples} ·+ generated{" "}+ {data.generated_at instanceof Date+ ? data.generated_at.toISOString()+ : String(data.generated_at)}+ </p>+ <div className="overflow-x-auto rounded border">+ <table className="w-full text-sm">+ <thead className="bg-muted/50">+ <tr>+ <th className="p-2 text-left">Block ID</th>+ <th className="p-2 text-left">Block name</th>+ <th className="p-2 text-left">Cost type</th>+ <th className="p-2 text-right">Samples</th>+ <th className="p-2 text-right">Mean (credits)</th>+ <th className="p-2 text-right">P50</th>+ <th className="p-2 text-right">P95</th>+ </tr>+ </thead>+ <tbody>+ {data.estimates.map((r) => (+ <tr key={r.block_id} className="border-t">+ <td className="p-2 font-mono text-xs">{r.block_id}</td>+ <td className="p-2">{r.block_name}</td>+ <td className="p-2">{r.cost_type}</td>+ <td className="p-2 text-right">{r.samples}</td>+ <td className="p-2 text-right font-semibold">+ {r.mean_credits}+ </td>+ <td className="p-2 text-right">{r.p50_credits}</td>+ <td className="p-2 text-right">{r.p95_credits}</td>+ </tr>+ ))}+ </tbody>+ </table>+ </div>+ </div>+ ) : (+ <p className="text-sm text-muted-foreground">+ Pick a window and click Aggregate to compute per-block average+ credits-per-execution.+ </p>+ )}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/useBlockCostEstimates.ts b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/useBlockCostEstimates.tsnew file mode 100644index 0000000000..b196f34411--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/components/useBlockCostEstimates.ts@@ -0,0 +1,105 @@+"use client";++import { useEffect, useState } from "react";+import { getV2ExportBlockCostEstimates } from "@/app/api/__generated__/endpoints/admin/admin";+import { okData } from "@/app/api/helpers";+import { ApiError } from "@/lib/autogpt-server-api/helpers";+import { useToast } from "@/components/molecules/Toast/use-toast";+import type { BlockCostEstimatesResponse } from "@/app/api/__generated__/models/blockCostEstimatesResponse";+import {+ dateInputToUtcIso,+ dateInputToUtcIsoEnd,+ defaultEndDate,+ defaultStartDate,+} from "../helpers";++export function useBlockCostEstimates() {+ const { toast } = useToast();+ const [start, setStart] = useState(defaultStartDate);+ const [end, setEnd] = useState(defaultEndDate);+ const [minSamples, setMinSamples] = useState(10);+ const [data, setData] = useState<BlockCostEstimatesResponse | null>(null);+ const [loading, setLoading] = useState(false);++ // Re-baseline the default window every mount so a long-lived tab doesn't+ // freeze the dates at the original mount time.+ useEffect(() => {+ setStart(defaultStartDate());+ setEnd(defaultEndDate());+ }, []);++ async function fetchEstimates() {+ if (!start || !end) {+ toast({+ title: "Pick a date range",+ description: "Both start and end dates are required.",+ variant: "destructive",+ });+ return;+ }+ setLoading(true);+ try {+ const startIso = dateInputToUtcIso(start);+ const endIso = dateInputToUtcIsoEnd(end);+ if (!startIso || !endIso) {+ toast({+ title: "Invalid date range",+ description: "Could not parse the selected start/end as UTC.",+ variant: "destructive",+ });+ return;+ }+ const safeMinSamples = Math.max(1, Math.floor(Number(minSamples) || 1));+ const response = await getV2ExportBlockCostEstimates({+ start: startIso as unknown as Date,+ end: endIso as unknown as Date,+ min_samples: safeMinSamples,+ });+ const payload = okData(response);+ if (!payload) {+ toast({+ title: "Aggregation failed",+ description: "Unexpected response shape from the export endpoint.",+ variant: "destructive",+ });+ return;+ }+ setData(payload);+ toast({+ title: "Aggregation complete",+ description: `${payload.total_rows} dynamic-cost blocks aggregated.`,+ });+ } catch (err) {+ if (err instanceof ApiError) {+ const detail =+ (err.response as { detail?: string } | undefined)?.detail ??+ err.message;+ toast({+ title: err.status === 400 ? "Window too large" : "Aggregation failed",+ description: detail,+ variant: "destructive",+ });+ } else {+ toast({+ title: "Aggregation failed",+ description: err instanceof Error ? err.message : "Unknown error",+ variant: "destructive",+ });+ }+ } finally {+ setLoading(false);+ }+ }++ return {+ start,+ end,+ minSamples,+ data,+ loading,+ setStart,+ setEnd,+ setMinSamples,+ fetchEstimates,+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/helpers.tsnew file mode 100644index 0000000000..46b94759b9--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/helpers.ts@@ -0,0 +1,59 @@+import type { BlockCostEstimateRow } from "@/app/api/__generated__/models/blockCostEstimateRow";++export function dateInputToUtcIso(input: string): string | null {+ if (!input) return null;+ return new Date(`${input}T00:00:00Z`).toISOString();+}++export function dateInputToUtcIsoEnd(input: string): string | null {+ if (!input) return null;+ return new Date(`${input}T23:59:59.999Z`).toISOString();+}++export function defaultStartDate(): string {+ // 6 days back so the inclusive [start, end] window covers exactly 7+ // calendar days when end is today.+ const d = new Date();+ d.setUTCDate(d.getUTCDate() - 6);+ return d.toISOString().slice(0, 10);+}++export function defaultEndDate(): string {+ return new Date().toISOString().slice(0, 10);+}++export function buildEstimatesJson(+ rows: BlockCostEstimateRow[],+ generatedAt: string,+ windowDays: number,+): string {+ const payload = {+ version: 1,+ generated_at: generatedAt,+ source_window_days: windowDays,+ estimates: Object.fromEntries(+ rows.map((r) => [+ r.block_id,+ {+ block_name: r.block_name,+ cost_type: r.cost_type,+ samples: r.samples,+ mean_credits: r.mean_credits,+ },+ ]),+ ),+ };+ return JSON.stringify(payload, null, 2) + "\n";+}++export function downloadJson(json: string, filename: string): void {+ const blob = new Blob([json], { type: "application/json;charset=utf-8;" });+ const url = URL.createObjectURL(blob);+ const link = document.createElement("a");+ link.href = url;+ link.download = filename;+ document.body.appendChild(link);+ link.click();+ document.body.removeChild(link);+ setTimeout(() => URL.revokeObjectURL(url), 0);+}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/page.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/page.tsxnew file mode 100644index 0000000000..d0ce966d5d--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/admin/block-cost-estimates/page.tsx@@ -0,0 +1,28 @@+import { withRoleAccess } from "@/lib/withRoleAccess";+import { BlockCostEstimatesContent } from "./components/BlockCostEstimatesContent";++function BlockCostEstimatesDashboard() {+ return (+ <div className="mx-auto p-6">+ <div className="flex flex-col gap-4">+ <div>+ <h1 className="text-3xl font-bold">Block Cost Estimates</h1>+ <p className="text-muted-foreground">+ Aggregate per-block average credits-per-execution from historical+ CreditTransaction USAGE rows. Use this to seed+ <code className="mx-1 text-xs">block_preflight_estimates.json</code>+ so dynamic-cost blocks charge a sensible amount up front and+ post-flight reconciliation only settles a small delta.+ </p>+ </div>+ <BlockCostEstimatesContent />+ </div>+ </div>+ );+}++export default async function BlockCostEstimatesDashboardPage() {+ const withAdminAccess = await withRoleAccess(["admin"]);+ const ProtectedDashboard = await withAdminAccess(BlockCostEstimatesDashboard);+ return <ProtectedDashboard />;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/layout.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/layout.tsxindex 13dd942b52..a3afdd7568 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/layout.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/layout.tsx@@ -7,6 +7,7 @@ import {Receipt,FileText,Heartbeat,+ CalculatorIcon,} from "@phosphor-icons/react/dist/ssr";import { IconSliders } from "@/components/__legacy__/ui/icons";@@ -49,6 +50,11 @@ const sidebarLinkGroups = [href: "/admin/execution-analytics",icon: <FileText className="h-6 w-6" />,},+ {+ text: "Block Cost Estimates",+ href: "/admin/block-cost-estimates",+ icon: <CalculatorIcon className="h-6 w-6" />,+ },{text: "Admin User Management",href: "/admin/settings",diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 0bd220e6ba..480f240a32 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -7,6 +7,81 @@"version": "0.1"},"paths": {+ "/api/admin/blocks/cost-estimates": {+ "get": {+ "tags": ["v2", "admin", "admin", "blocks"],+ "summary": "Export Block Cost Estimates",+ "description": "Aggregate per-block average credits-per-execution over [start, end].\n\nCapped at ANALYTICS_MAX_DAYS days. Returns only blocks whose current cost\ntype is dynamic (SECOND/ITEMS/COST_USD) — static-cost blocks already\ncharge correctly pre-flight and don't need an estimate override. TOKENS\nis excluded because `compute_token_credits` already supplies a per-model\nfloor at pre-flight; a per-block historical mean would lose that\ngranularity.",+ "operationId": "getV2Export block cost estimates",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "start",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "description": "ISO timestamp (inclusive)",+ "title": "Start"+ },+ "description": "ISO timestamp (inclusive)"+ },+ {+ "name": "end",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "description": "ISO timestamp (inclusive)",+ "title": "End"+ },+ "description": "ISO timestamp (inclusive)"+ },+ {+ "name": "min_samples",+ "in": "query",+ "required": false,+ "schema": {+ "type": "integer",+ "minimum": 1,+ "description": "Minimum executions per block to include",+ "default": 10,+ "title": "Min Samples"+ },+ "description": "Minimum executions per block to include"+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/BlockCostEstimatesResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },"/api/admin/diagnostics/agents": {"get": {"tags": ["v2", "admin", "diagnostics", "admin"],@@ -10204,6 +10279,56 @@"required": ["cost_amount", "cost_filter", "cost_type"],"title": "BlockCost"},+ "BlockCostEstimateRow": {+ "properties": {+ "block_id": { "type": "string", "title": "Block Id" },+ "block_name": { "type": "string", "title": "Block Name" },+ "cost_type": { "type": "string", "title": "Cost Type" },+ "samples": { "type": "integer", "title": "Samples" },+ "mean_credits": { "type": "integer", "title": "Mean Credits" },+ "p50_credits": { "type": "integer", "title": "P50 Credits" },+ "p95_credits": { "type": "integer", "title": "P95 Credits" }+ },+ "type": "object",+ "required": [+ "block_id",+ "block_name",+ "cost_type",+ "samples",+ "mean_credits",+ "p50_credits",+ "p95_credits"+ ],+ "title": "BlockCostEstimateRow"+ },+ "BlockCostEstimatesResponse": {+ "properties": {+ "estimates": {+ "items": { "$ref": "#/components/schemas/BlockCostEstimateRow" },+ "type": "array",+ "title": "Estimates"+ },+ "total_rows": { "type": "integer", "title": "Total Rows" },+ "window_days": { "type": "integer", "title": "Window Days" },+ "max_window_days": { "type": "integer", "title": "Max Window Days" },+ "min_samples": { "type": "integer", "title": "Min Samples" },+ "generated_at": {+ "type": "string",+ "format": "date-time",+ "title": "Generated At"+ }+ },+ "type": "object",+ "required": [+ "estimates",+ "total_rows",+ "window_days",+ "max_window_days",+ "min_samples",+ "generated_at"+ ],+ "title": "BlockCostEstimatesResponse"+ },"BlockCostType": {"type": "string","enum": ["run", "byte", "second", "items", "cost_usd", "tokens"],