ocm: emit status updates for plan-weight-only changes

This commit is contained in:
世界
2026-03-17 16:32:03 +08:00
parent 4a6a211775
commit cf2d677043
2 changed files with 36 additions and 1 deletions
+3 -1
View File
@@ -518,7 +518,9 @@ func (c *externalCredential) updateStateFromHeaders(headers http.Header) {
}
c.logger.Debug("usage update for ", c.tag, ": 5h=", c.state.fiveHourUtilization, "%, weekly=", c.state.weeklyUtilization, "%", resetSuffix)
}
shouldEmit := hadData && (c.state.fiveHourUtilization != oldFiveHour || c.state.weeklyUtilization != oldWeekly || c.state.remotePlanWeight != oldPlanWeight)
utilizationChanged := c.state.fiveHourUtilization != oldFiveHour || c.state.weeklyUtilization != oldWeekly
planWeightChanged := c.state.remotePlanWeight != oldPlanWeight
shouldEmit := (hadData && utilizationChanged) || planWeightChanged
shouldInterrupt := c.checkTransitionLocked()
c.stateAccess.Unlock()
if shouldInterrupt {
+33
View File
@@ -195,6 +195,39 @@ func TestExternalCredentialConnectStatusStreamMultiFrameKeepsLastUpdated(t *test
}
}
func TestExternalCredentialPlanWeightOnlyRateLimitsEventEmitsStatus(t *testing.T) {
subscriber := observable.NewSubscriber[struct{}](8)
subscription, _ := subscriber.Subscription()
credential := &externalCredential{
tag: "test",
logger: newTestLogger(),
statusSubscriber: subscriber,
}
credential.stateAccess.Lock()
credential.state.remotePlanWeight = 2
oldTime := time.Unix(123, 0)
credential.state.lastUpdated = oldTime
credential.stateAccess.Unlock()
(&Service{}).handleWebSocketRateLimitsEvent([]byte(`{"plan_weight":3}`), credential)
if weight := credential.planWeight(); weight != 3 {
t.Fatalf("expected plan weight 3, got %v", weight)
}
if count := drainStatusEvents(subscription); count != 1 {
t.Fatalf("expected 1 status event, got %d", count)
}
if !credential.lastUpdatedTime().Equal(oldTime) {
t.Fatalf("expected lastUpdated to stay %v, got %v", oldTime, credential.lastUpdatedTime())
}
(&Service{}).handleWebSocketRateLimitsEvent([]byte(`{"plan_weight":3}`), credential)
if count := drainStatusEvents(subscription); count != 0 {
t.Fatalf("expected no status event for unchanged plan weight, got %d", count)
}
}
func TestDefaultCredentialAvailabilityChangesEmitStatus(t *testing.T) {
credentialPath := filepath.Join(t.TempDir(), "auth.json")
err := os.WriteFile(credentialPath, []byte("{\"OPENAI_API_KEY\":\"sk-test\"}\n"), 0o600)