fix(ploeg): settle late gateway charges after the first settlement #175
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "ryangr0/ploeg-late-gateway-charges"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The settlement sweep read an account's LiteLLM spend logs once, after the
quiet period, and never looked at a reconciled account again. A spend log
written after that read was missing from the Run's and the Shift's cost,
and a minted key with no spend-log entries settled at zero and was shown
as a zero cost.
A spend-log settlement now stays provisional for
PLOEG_LLM_CORRECTION_WINDOW (default 24h, 0 makes it final). Every 15
minutes the correction sweep reads the spend logs of reconciled accounts
in their window again. A higher total charges only the difference to the
Shift, raises reconciled_spend and is recorded as a run_llm_adjustments
row and an llm.reconciled audit event marked adjustment. The first
settlement stays in settled_at, settled_spend and its original evidence.
An unchanged re-read writes nothing.
A minted account settled from no spend-log entries records cost_known
false: the operator API shows its settledUsd as null and its Run
costStatus as unknown until an entry arrives; an entry costing zero
confirms zero. The Run list and Run detail gain settledAt, costFinalAt
and costFinal, so a provisional cost is distinguishable from a final one.
Migration 0036 adds the columns and table and backfills existing
reconciled accounts as final, marking spend-log settlements with
entries=0 as unknown.
Migration 0035 (run_llm_accounts settlement columns + run_llm_adjustments). New PLOEG_LLM_CORRECTION_WINDOW (default 24h; 0 = first settlement final). Corrections only ever raise a settlement and are recorded as adjustment rows plus llm.reconciled audit events with delta; the first settlement is never rewritten. A minted key with no spend-log entry settles its hold at zero but is cost_known=false (settledUsd null, costStatus unknown). Operator API gains optional settledAt, costFinalAt, costFinal. Touches cmd/ploegd/sweep.go like the VIK-1739 PR; whichever merges second needs a rebase. Vloer does not read costFinal yet.
Verified with
mise run verifyon the pinned toolchain (all gates passed).Ticket: https://vikunja.webgrip.dev/tasks/1755
🤖 Generated with Claude Code