Merge branch 'worktree-agent-a11776f2a9241a0c9'

This commit is contained in:
2026-11-30 16:13:58 +00:00
22 changed files with 905 additions and 1254 deletions
+36
View File
@@ -59,6 +59,32 @@ type rateLimitInfo struct {
RateLimitType string `json:"rateLimitType"`
Utilization *float64 `json:"utilization"`
UnifiedWindows map[string]*rateLimitWindowInfo `json:"unifiedWindows"`
// Extra usage ("overage"): paid usage credits past the subscription
// limits. Claude Code reports whether they would be accepted, whether
// they are in use and why they are off; nothing here applies them.
OverageStatus string `json:"overageStatus"`
OverageResetsAt *float64 `json:"overageResetsAt"`
OverageDisabledReason string `json:"overageDisabledReason"`
IsUsingOverage *bool `json:"isUsingOverage"`
OverageInUse *bool `json:"overageInUse"`
}
// extraUsage is the event's overage state, or nil when it names none.
func (info rateLimitInfo) extraUsage() *protocol.ChatExtraUsage {
if info.OverageStatus == "" && info.OverageDisabledReason == "" {
return nil
}
inUse := (info.IsUsingOverage != nil && *info.IsUsingOverage) ||
(info.OverageInUse != nil && *info.OverageInUse)
extra := &protocol.ChatExtraUsage{
Status: info.OverageStatus,
InUse: inUse,
DisabledReason: info.OverageDisabledReason,
}
if at := info.OverageResetsAt; at != nil && *at > 0 && !math.IsInf(*at, 0) {
extra.ResetsAt = int64(*at * 1000)
}
return extra
}
// quotaWindowOrder lists the windows Claude Code names, in display order.
@@ -119,6 +145,11 @@ func mergeRateLimit(previous *protocol.ChatAccountUsage, raw json.RawMessage, pl
changed = true
}
}
// Overage news alone updates a report that already has windows.
extra := info.extraUsage()
if extra != nil && previous != nil {
changed = true
}
if !changed {
return nil
}
@@ -130,7 +161,12 @@ func mergeRateLimit(previous *protocol.ChatAccountUsage, raw json.RawMessage, pl
// A switch stays recorded: the refusal it cleared is still in the
// transcript.
next.SwitchedAt = previous.SwitchedAt
if extra == nil && previous.ExtraUsage != nil {
kept := *previous.ExtraUsage
extra = &kept
}
}
next.ExtraUsage = extra
for _, entry := range quotaWindowOrder {
if w, ok := windows[entry.id]; ok {
next.Windows = append(next.Windows, w)
+30
View File
@@ -55,6 +55,36 @@ func TestMergeRateLimitKeepsUnmentionedWindowsAndIgnoresStatusOnly(t *testing.T)
}
}
func TestMergeRateLimitCarriesExtraUsage(t *testing.T) {
now := time.UnixMilli(1_800_000_000_000)
first := mergeRateLimit(nil, json.RawMessage(unifiedRateLimit), "max", now)
if first.ExtraUsage != nil {
t.Fatalf("no overage fields, extra = %+v", first.ExtraUsage)
}
// Overage news alone updates a known report.
next := mergeRateLimit(first, json.RawMessage(`{"status":"rejected","overageStatus":"allowed","isUsingOverage":true,"overageResetsAt":1901000000}`), "", now)
want := &protocol.ChatExtraUsage{Status: "allowed", InUse: true, ResetsAt: 1_901_000_000_000}
if next == nil || !reflect.DeepEqual(next.ExtraUsage, want) || len(next.Windows) != 2 {
t.Fatalf("merge = %+v", next)
}
// A later window-only event keeps the last overage state.
kept := mergeRateLimit(next, json.RawMessage(`{"status":"allowed","rateLimitType":"five_hour","utilization":0.5,"resetsAt":1899990000}`), "", now)
if kept == nil || !reflect.DeepEqual(kept.ExtraUsage, want) {
t.Fatalf("kept = %+v", kept)
}
if clone := kept.Clone(); clone.ExtraUsage == kept.ExtraUsage {
t.Fatal("clone shares the extra usage")
}
off := mergeRateLimit(kept, json.RawMessage(`{"status":"rejected","overageStatus":"rejected","overageDisabledReason":"out_of_credits","isUsingOverage":false}`), "", now)
if off == nil || !reflect.DeepEqual(off.ExtraUsage, &protocol.ChatExtraUsage{Status: "rejected", DisabledReason: "out_of_credits"}) {
t.Fatalf("off = %+v", off)
}
// Without a report to attach to, overage alone shows nothing.
if got := mergeRateLimit(nil, json.RawMessage(`{"overageStatus":"allowed"}`), "", now); got != nil {
t.Fatalf("lone overage = %+v", got)
}
}
func TestDecodeAccountIdentity(t *testing.T) {
got := decodeAccountIdentity(json.RawMessage(`{"email":"a@example.invalid","organization":"Org","subscriptionType":"max","apiProvider":"firstParty"}`))
if got.key == "" || got.plan != "max" {
+21
View File
@@ -817,6 +817,23 @@ type ChatAccountUsage struct {
// account even with no windows yet, and a usage-limit refusal the chat
// received before it no longer applies.
SwitchedAt int64 `json:"switched_at,omitempty"`
// ExtraUsage is the account's extra usage (Claude Code's "overage": paid
// usage credits past the subscription limits) as the harness last
// reported it; nil when it never said.
ExtraUsage *ChatExtraUsage `json:"extra_usage,omitempty"`
}
// ChatExtraUsage is Claude Code's overage state from rate_limit_event:
// Status is its overageStatus ("allowed", "allowed_warning" or "rejected"),
// InUse whether requests are drawing on extra usage now, and DisabledReason
// its overageDisabledReason (e.g. "out_of_credits", "org_level_disabled",
// "overage_not_provisioned") when it is off. ResetsAt is when the extra usage
// period resets (epoch milliseconds), 0 if unknown.
type ChatExtraUsage struct {
Status string `json:"status,omitempty"`
InUse bool `json:"in_use,omitempty"`
DisabledReason string `json:"disabled_reason,omitempty"`
ResetsAt int64 `json:"resets_at,omitempty"`
}
// ChatQuotaWindow is one rolling quota window. UsedPercent is 0-100 (it can
@@ -836,6 +853,10 @@ func (a *ChatAccountUsage) Clone() *ChatAccountUsage {
}
copy := *a
copy.Windows = append([]ChatQuotaWindow(nil), a.Windows...)
if a.ExtraUsage != nil {
extra := *a.ExtraUsage
copy.ExtraUsage = &extra
}
return &copy
}