-
Notifications
You must be signed in to change notification settings - Fork 147
feat: implement RoundCostAwarenessHook for budget visibility #781 #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class RoundCostAwarenessHook: | ||
| """ | ||
| Hook for per-round cost awareness in MassGen. | ||
| Injects cumulative cost metadata with tool results. | ||
| Addresses issue #781. | ||
| """ | ||
| def __init__(self, budget=None): | ||
| self.cumulative_cost = 0.0 | ||
| self.budget = budget | ||
|
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Budget boundary handling is incorrect for Using Proposed fix class RoundCostAwarenessHook:
@@
def __init__(self, budget=None):
+ if budget is not None and budget < 0:
+ raise ValueError("budget must be >= 0")
self.cumulative_cost = 0.0
self.budget = budget
@@
- if self.budget and self.cumulative_cost > self.budget * 0.75:
+ if self.budget is not None and self.cumulative_cost > self.budget * 0.75:
print(f"Warning: Nearing budget limit [${self.cumulative_cost:.2f} / ${self.budget:.2f}]")
@@
- if self.budget:
+ if self.budget is not None:
return f"Cost: ${self.cumulative_cost:.2f} / ${self.budget:.2f} budget"Also applies to: 13-18 🤖 Prompt for AI Agents |
||
|
|
||
| def update_cost(self, cost): | ||
| self.cumulative_cost += cost | ||
| if self.budget and self.cumulative_cost > self.budget * 0.75: | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Without validation, Proposed fix def update_cost(self, cost):
+ if not isinstance(cost, (int, float)):
+ raise TypeError("cost must be numeric")
+ if cost < 0:
+ raise ValueError("cost must be >= 0")
self.cumulative_cost += cost🤖 Prompt for AI Agents |
||
| print(f"Warning: Nearing budget limit [${self.cumulative_cost:.2f} / ${self.budget:.2f}]") | ||
|
|
||
| def get_status_string(self): | ||
| if self.budget: | ||
| return f"Cost: ${self.cumulative_cost:.2f} / ${self.budget:.2f} budget" | ||
| return f"Cost: ${self.cumulative_cost:.2f}" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 30750
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 908
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 276
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 1090
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 2617
🏁 Script executed:
Repository: massgen/MassGen
Length of output: 3930
Inherit from
PatternHookand implement requiredexecute()method—hook will not execute in framework otherwise.RoundCostAwarenessHookcannot function as a hook in the MassGen framework. The framework expects all hooks to inherit fromPatternHookand implement anasync def execute(function_name, arguments, context, **kwargs) -> HookResultmethod. This class has neither. If someone tries to register it via configuration, it will fail at runtime.Additionally:
__init__,update_cost,get_status_string)if self.budget andfails for validbudget=0. Use explicitis not Nonecheck instead.logger.warning) instead ofprint()costis a valid numeric type before accumulating🤖 Prompt for AI Agents