loopx icon indicating copy to clipboard operation
loopx copied to clipboard

quota: an unreadable spend artifact silently removes the spend (fail-open)

Open BiBoyang opened this issue 1 month ago • 1 comments

What happens

One run's quota_slot_spent artifact is unreadable (truncated to 40 bytes; the index row is left intact). quota plan then reports spent_slots: 1 → 0 and spend_event_count: 1 → 0, the goal flips throttled → eligible, and both quota plan and history exit 0 with empty stderr. The index row still proves the run happened (index_rows stays 2), but it contributes nothing.

Why this path is taken

The spend index row carries no inline quota_event (spend_commit.ts, indexRecordFor), so load_quota_event_from_run reads the artifact via json_path. If that read fails (json.JSONDecodeError / OSError / missing file / non-dict), it returns None (loopx/control_plane/quota/ledger_readback.py:37-58). The caller skips that contribution (loopx/quota.py:419-429) and recomputes spent_slots from the events that were intact; :494-498 then decides throttled from that number, so with allowed_slots=1 one lost event removes the cap. The usage summary reads the same function, so every surface agrees on the wrong number. Nothing counts the unreadable event.

Trigger honesty

The artifact write is atomic, so this is not a same-version torn write. It models disk corruption, a partial restore/backup, or a writer from another version — exactly the cases where a quota ledger should fail closed or say "unknown" rather than "zero".

Expected

A spend that cannot be read should not silently become no spend. At minimum an unreadable_event_count surfaced in quota plan / history; arguably the affected goal should read as unknown/blocked-health rather than eligible.

Reproduced

Snapshot 105c4bc1; code unchanged on main at 1c7b258cb. Fixture: one goal (compute 1.0, window_hours 24, allowed_slots 1), one state_refreshed run (keeps waiting_on=codex), one quota_slot_spent run. 5/5 runs:

healthy: {"state":"throttled","spent_slots":1,"spend_event_count":1,"index_rows":2,"stderr_bytes":0}
wounded: {"state":"eligible","spent_slots":0,"spend_event_count":0,"index_rows":2,"stderr_bytes":0,"exit":0}

Control: with the truncation replaced by a plain read, the wounded half reports throttled/1/1 and the check fails, so the observation is not an artefact of the fixture.

BiBoyang avatar Oct 19 '26 08:10 BiBoyang

I checked the artifact-backed read model at main snapshot 0549083c62ff. The reported loss is still observable at that boundary.

Using that exact source helper and disposable synthetic files:

Input quota_slot_contribution result
Valid spend artifact One spent slot
Truncated JSON, missing file, non-object JSON, or a directory instead of a file None, without an exception
Original valid bytes restored One spent slot again
Valid inline quota_event, artifact missing One spent slot
Invalid UTF-8 bytes Raises UnicodeDecodeError at the helper boundary

The rolling-ledger caller skips a None contribution and counts only the remaining events. That supports the artifact-backed failure mechanism in this report. The inline-event and invalid-encoding controls also show why this should not be described as every damaged artifact taking the same path.

This was a source-helper diagnostic, not a rerun of your complete quota plan fixture or qualification of every admission/storage route. No active Goal, ledger or receipt was altered. The corrupt-payload results remain failures of the requested integrity property; the diagnostic completing does not make them passing product behavior.

The existing fix proposal is #6875; I have not qualified or approved that head. Keeping this issue open is appropriate. Useful follow-up controls include valid spend/void cancellation, window/Goal filtering, and restoring the same artifact bytes, so an integrity fix preserves legitimate accounting while making unreadable indexed events visible.

loopx-agent avatar Oct 23 '26 12:10 loopx-agent