Skip to main content

Module edit_metering

Module edit_metering 

Source
Expand description

Edit-efficiency metering (#1008, honest-metering philosophy #361).

Anchored editing (ctx_patch) saves output tokens: the model references a (line, hash) anchor instead of reproducing the replaced span byte-for-byte the way a str_replace old_string requires. This module measures that claim with real per-edit numbers instead of a marketing multiplier:

  • avoided output tokens — per successful anchored op: tokens(replaced span) − tokens(anchor args), floored at 0. The replaced span is exactly what a str_replace edit would have re-emitted as old_string; the anchor args are what the model actually sent instead.
  • conflict round-trips — stale-anchor CONFLICT responses (each one is an extra turn the anchored loop needed).
  • str_replace baseline — successful ctx_edit calls with the old_string tokens they really paid, plus old_string-miss round-trips.

This is a separate metric channel: values are never folded into the read-gain ledger and never appear in tool output bodies (#498 determinism). Consumers are ctx_metrics, the dashboard (/api/statsedit_efficiency) and the A/B eval harness.

Storage: ~/.lean-ctx/edit_metering.json, atomic write (tmp+rename), loaded once per process, flushed every few records like edit_quality.

Structs§

EditMeteringStore
All-time counters for both edit paths. Small, append-only aggregates — per-file/per-op detail intentionally lives in edit_quality, not here. serde(default) keeps older/partial store files loadable field-by-field instead of silently resetting all counters via unwrap_or_default.

Functions§

flush
metrics_snapshot
Snapshot for ctx_metrics and the dashboard /api/stats payload.
record_anchored_conflict
A stale-anchor CONFLICT response (one extra self-heal round-trip).
record_anchored_success
A successful anchored patch: ops applied, avoided_tokens output tokens the model did not have to reproduce (already anchor-overhead-adjusted).
record_str_replace_miss
An old_string-not-found miss (one blind retry round-trip).
record_str_replace_success
A successful str_replace edit and the old_string tokens it paid.