fix(ploeg): settle late gateway charges after the first settlement #175

Merged
ryangr0 merged 1 commit from ryangr0/ploeg-late-gateway-charges into development 2026-10-03 12:12:29 +00:00 AGit
Owner

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 verify on the pinned toolchain (all gates passed).

Ticket: https://vikunja.webgrip.dev/tasks/1755

🤖 Generated with Claude Code

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 verify` on the pinned toolchain (all gates passed). Ticket: https://vikunja.webgrip.dev/tasks/1755 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fix(ploeg): settle late gateway charges after the first settlement
All checks were successful
[Workflow] On Pull Request / checks (pull_request) Successful in 8m16s
[Workflow] On Pull Request / release-policy (pull_request) Successful in 2m42s
[Workflow] On Pull Request / warnings (pull_request) Successful in 0s
746ad48209
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.

VIK-1755

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
ryangr0 merged commit 0686130824 into development 2026-10-03 12:12:29 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
webgrip/unfold!175
No description provided.