Skip to content

feat: core engine — TokenTracker, ModelPricing, BudgetCircuitBreaker#1

Merged
hermanngeorge15 merged 5 commits into
mainfrom
feat/core-engine
Apr 2, 2026
Merged

feat: core engine — TokenTracker, ModelPricing, BudgetCircuitBreaker#1
hermanngeorge15 merged 5 commits into
mainfrom
feat/core-engine

Conversation

@hermanngeorge15
Copy link
Copy Markdown
Contributor

Summary

PR 2 of budget-breaker v0.0.1 Phase 1.

  • TokenTracker: AtomicLong pair (prompt + completion), thread-safe, limit checks. Exposes model, hardLimitTokens, softLimitTokens from budget config.
  • ModelPricing: Hardcoded defaults for Claude/GPT/Gemini/Ollama + custom overrides via Map. Companion object convenience method.
  • BudgetReport: Data class for post-run summary (tokens, cost, duration, breach counts).
  • BudgetScope: trackCall() DSL with soft/hard limit checks. Uses AtomicInteger for breach count, AtomicBoolean for soft limit fired flag (no var).
  • BudgetCircuitBreaker: Coroutine supervisor with withBudget() API. Callback + SharedFlow for soft limit events. ConcurrentHashMap for reports.

Test plan

  • 7 TokenTracker tests: separate tracking, accumulation, soft/hard limit detection, percent used, thread safety, zero start
  • 6 ModelPricing tests: Claude Sonnet/Opus, combined cost, unknown model, custom overrides, override precedence
  • 2 BudgetReport tests: all fields, totalTokens consistency
  • 8 BudgetCircuitBreaker tests: normal completion, hard limit, soft limit callback, soft limit flow event, report generation, report after exception, cost estimation, concurrent agents
  • 23 total tests, 0 failures — clean build from scratch

Constraints verified

  • No var anywhere — uses AtomicInteger, AtomicBoolean, AtomicLong
  • No !! without justifying comment (one in test, explained)
  • No Thread.sleep — pure coroutines
  • All source in budget-breaker/src/main/kotlin/dev/unityinflow/budget/
  • All tests in budget-breaker/src/test/kotlin/dev/unityinflow/budget/

@hermanngeorge15 hermanngeorge15 merged commit c496d75 into main Apr 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant