budget_config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Configurable budget constants for tool result persistence.
  2. Per-tool resolution: pinned > config overrides > registry > default."""
  3. from dataclasses import dataclass, field
  4. from typing import Dict
  5. from agent.model_metadata import CHARS_PER_TOKEN
  6. # Never overridden; read_file=inf prevents infinite persist->read->persist loops.
  7. PINNED_THRESHOLDS: dict[str, float] = {"read_file": float("inf")}
  8. # Single source of truth for the defaults; tool_result_storage.py imports these.
  9. DEFAULT_RESULT_SIZE_CHARS: int = 100_000
  10. DEFAULT_TURN_BUDGET_CHARS: int = 200_000
  11. DEFAULT_PREVIEW_SIZE_CHARS: int = 1_500
  12. # Tighter per-result default for ``mcp_`` tools: MCP servers routinely return
  13. # un-paginated 20-50K payloads that sail under the generic 100K threshold; spillover
  14. # keeps the full payload on disk. Config: ``tool_budget.mcp_result_size_chars``.
  15. DEFAULT_MCP_RESULT_SIZE_CHARS: int = 50_000
  16. # Same prefix the untrusted-content wrapper keys on (agent/tool_dispatch_helpers.py).
  17. MCP_TOOL_PREFIX: str = "mcp_"
  18. def _configured_mcp_result_size() -> int:
  19. """Read ``tool_budget.mcp_result_size_chars`` via ``load_config_readonly`` (the
  20. sanctioned path; raw config.yaml parsing outside owner modules is test-guarded).
  21. Any error, missing key or non-positive value returns the built-in default.
  22. The ``tool_budget:`` block name is shared with the wider configurable-caps proposal (#80508) so the two
  23. can merge without a key rename.
  24. """
  25. try:
  26. from hermes_cli.config import load_config_readonly
  27. data = load_config_readonly()
  28. block = data.get("tool_budget") if isinstance(data, dict) else None
  29. raw = block.get("mcp_result_size_chars") if isinstance(block, dict) else None
  30. if raw is not None and int(raw) > 0:
  31. return int(raw)
  32. except Exception:
  33. pass
  34. return DEFAULT_MCP_RESULT_SIZE_CHARS
  35. @dataclass(frozen=True)
  36. class BudgetConfig:
  37. """Immutable budget constants: per-result threshold (``resolve_threshold``),
  38. per-turn aggregate (``turn_budget``) and inline snippet size (``preview_size``)."""
  39. default_result_size: int = DEFAULT_RESULT_SIZE_CHARS
  40. turn_budget: int = DEFAULT_TURN_BUDGET_CHARS
  41. preview_size: int = DEFAULT_PREVIEW_SIZE_CHARS
  42. mcp_result_size: int = DEFAULT_MCP_RESULT_SIZE_CHARS
  43. tool_overrides: dict[str, int] = field(default_factory=dict)
  44. def resolve_threshold(self, tool_name: str) -> int | float:
  45. """Priority: pinned -> tool_overrides -> mcp_ prefix -> registry per-tool -> default.
  46. MCP tools get ``mcp_result_size`` (no registry entry). MCP and registry values
  47. are capped at ``default_result_size`` so a context-scaled budget for a small
  48. model still constrains tools registering a fixed 100K ``max_result_size_chars``.
  49. For the default budget this is a no-op because both equal 100K; for a scaled-down budget it prevents
  50. a per-tool registry value from re-inflating the cap past the model's window (#23767).
  51. """
  52. if tool_name in PINNED_THRESHOLDS:
  53. return PINNED_THRESHOLDS[tool_name]
  54. if tool_name in self.tool_overrides:
  55. return self.tool_overrides[tool_name]
  56. if tool_name.startswith(MCP_TOOL_PREFIX):
  57. return min(self.mcp_result_size, self.default_result_size)
  58. from tools.registry import registry
  59. registry_value = registry.get_max_result_size(tool_name, default=self.default_result_size)
  60. if registry_value == float("inf"):
  61. return registry_value
  62. return min(registry_value, self.default_result_size)
  63. # Default config -- matches the historical hardcoded behavior exactly.
  64. DEFAULT_BUDGET = BudgetConfig()
  65. # Same rough chars-per-token the estimator uses; a smaller divisor would UNDER-protect small models.
  66. _CHARS_PER_TOKEN: int = CHARS_PER_TOKEN
  67. # Window fraction ONE result / the WHOLE turn's tool output may occupy — well
  68. # under 1.0 since system prompt, schemas, history and the reply all compete.
  69. _PER_RESULT_WINDOW_FRACTION: float = 0.15
  70. _PER_TURN_WINDOW_FRACTION: float = 0.30
  71. # Floors so a tiny model still gets a usable result, never a 0-char budget.
  72. _MIN_RESULT_SIZE_CHARS: int = 8_000
  73. _MIN_TURN_BUDGET_CHARS: int = 16_000
  74. def budget_for_context_window(context_length: int | None) -> BudgetConfig:
  75. """Return a BudgetConfig scaled to the model's context window: the fixed
  76. defaults suit 200K+ models but on 65K one result/turn can fill the window.
  77. The proportional value is clamped to the defaults as a CAP (large models
  78. stay byte-identical) and floored so a usable preview always survives.
  79. The fixed defaults (100K result / 200K turn chars) are correct for large (200K+ token) models but blind
  80. to small ones: on a 65K-token model a single tool result persisted at the 100K-char threshold, or a
  81. 200K-char turn budget (~50K tokens), can by itself approach or exceed the whole window and force an
  82. oversized request (#23767).
  83. """
  84. mcp_result_size = _configured_mcp_result_size()
  85. if not context_length or context_length <= 0:
  86. if mcp_result_size == DEFAULT_MCP_RESULT_SIZE_CHARS:
  87. return DEFAULT_BUDGET
  88. return BudgetConfig(mcp_result_size=mcp_result_size)
  89. window_chars = context_length * _CHARS_PER_TOKEN
  90. return BudgetConfig(
  91. default_result_size=max(_MIN_RESULT_SIZE_CHARS, min(int(window_chars * _PER_RESULT_WINDOW_FRACTION), DEFAULT_RESULT_SIZE_CHARS)),
  92. turn_budget=max(_MIN_TURN_BUDGET_CHARS, min(int(window_chars * _PER_TURN_WINDOW_FRACTION), DEFAULT_TURN_BUDGET_CHARS)),
  93. preview_size=DEFAULT_PREVIEW_SIZE_CHARS,
  94. mcp_result_size=mcp_result_size,
  95. )