Skip to content

shouldEmitBudgetRemaining spams events when the initial budget is zero #80

@nficano

Description

@nficano

packages/runtime/src/job.ts:180-194 debounces cost.budget.remaining emission by computing threshold = initial * 0.05 and emitting when Math.abs(last - remaining) >= threshold || remaining <= 0. When the initial budget for a currency is zero — either because the runtime created the job with a budget: { tokens: 0 } for a hard cap, or because the budget tracks something whose initial value is genuinely zero — the threshold is also zero, so the first if (Math.abs(last - remaining) >= threshold) check is satisfied by every subsequent metric (the absolute difference is always ≥ 0). The runtime then emits a cost.budget.remaining event on every single cost.* metric tick for the rest of the job, defeating the debounce and flooding the event stream and the event log.

Fix prompt: in shouldEmitBudgetRemaining (packages/runtime/src/job.ts:178-194), special-case initial === 0 to a fixed minimum threshold (e.g. emit only when remaining changes by an absolute delta of at least one unit, or only on remaining <= 0), or compute threshold = Math.max(initial * 0.05, MIN_BUDGET_DEBOUNCE_DELTA) with a documented constant. Also guard against negative-going initial from a malformed budget by clamping to zero. Add a unit test that constructs a Job with initial = 0 for a currency, fires ten cost.tokens metrics, and asserts only the expected single emission rather than ten.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingseverity:mediumMedium severity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions