mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 08:01:07 +00:00
fix: withhold vis<8 and plaintext-ratio dumps; count streamed visible only
requestRetry still leaked grok-4.6 dumps after the short-usage patch: fake-enc/burst required visible >= minOutput, plaintext thinking vetoed the dump detectors, and chat usage.completion inflated visible tokens. Withhold when reasoning>=80 or ciphertext meets the floor and the visible flush is <2s. Plaintext thinking still withholds if flush<2s and reasoning/output >= 0.8. Cipher-only HasThinking waits until visible text has streamed for 2s, or the stream ends. Visible tokens come from streamed content only. Does not enable requestRetry and does not change lab Docker/VERSION defaults.
This commit is contained in:
1 parent
8913b53fe9
commit
7f3f3d3ce0
3 files changed
+261
-100
No files matched your search
@@ -29,9 +29,18 @@ const (
|
||||
defaultBurstFlushMS = int64(1000)
|
||||
defaultBurstMaxVisible = int64(32)
|
||||
defaultBurstMinReasoning = int64(80)
|
||||
defaultMissingThinkingCooldown = 12 * time.Hour
|
||||
lastErrorMissingThinking = accountdomain.LastErrorMissingThinking
|
||||
lastErrorMissingThinkingDisabled = accountdomain.LastErrorMissingThinkingDisabled
|
||||
// Fake encrypted thinking dumps the whole visible answer after a long
|
||||
// wait. Audit TPS is rewritten against full duration (looks like 60–120
|
||||
// tok/s) but first-token ≈ duration. Catch flush windows up to 2s so
|
||||
// 1.8s / 1962-token dumps are withheld too.
|
||||
defaultFakeEncFlushMS = int64(2000)
|
||||
// Cipher-only "thinking" that is already dumping this much visible text
|
||||
// with usage.reasoning_tokens=0 is the 128k status-loop drool, not a
|
||||
// real encrypted thinking stream.
|
||||
defaultCipherDroolVisible = int64(1024)
|
||||
defaultMissingThinkingCooldown = 12 * time.Hour
|
||||
lastErrorMissingThinking = accountdomain.LastErrorMissingThinking
|
||||
lastErrorMissingThinkingDisabled = accountdomain.LastErrorMissingThinkingDisabled
|
||||
// An empty stream that idles while held is treated as an account-quality
|
||||
// failure: the request can still rotate before any bytes reach the client.
|
||||
qualityIdleAccountCooldown = 15 * time.Minute
|
||||
@@ -161,18 +170,31 @@ func encryptedThinkingFloor(minBytes, bytesPerToken int, reasoningTokens int64)
|
||||
return floor
|
||||
}
|
||||
|
||||
func qualityIsBurstDump(sig QualityStreamSignals, minOutput int64) bool {
|
||||
visible := sig.VisibleTokens
|
||||
func qualityFastFlush(sig QualityStreamSignals, limitMS int64) bool {
|
||||
return sig.FirstVisible && sig.VisibleFlushMS >= 0 && sig.VisibleFlushMS < limitMS
|
||||
}
|
||||
|
||||
func qualityMeetsEncryptedFloor(sig QualityStreamSignals) bool {
|
||||
if sig.EncryptedBytes <= 0 {
|
||||
return false
|
||||
}
|
||||
floor := sig.EncryptedFloor
|
||||
if floor <= 0 {
|
||||
floor = encryptedThinkingFloor(0, 0, sig.ReasoningTokens)
|
||||
}
|
||||
barelyCeiling := int64(math.MaxInt64)
|
||||
if floor <= math.MaxInt64/2 {
|
||||
barelyCeiling = floor * 2
|
||||
return int64(sig.EncryptedBytes) >= floor
|
||||
}
|
||||
|
||||
func qualityHasDumpBill(sig QualityStreamSignals) bool {
|
||||
return sig.ReasoningTokens >= defaultBurstMinReasoning || qualityMeetsEncryptedFloor(sig)
|
||||
}
|
||||
|
||||
func qualityIsBurstDump(sig QualityStreamSignals, minOutput int64) bool {
|
||||
_ = minOutput
|
||||
if sig.HasReasoningDelta {
|
||||
return false
|
||||
}
|
||||
barelyCipher := sig.EncryptedBytes > 0 && int64(sig.EncryptedBytes) < barelyCeiling
|
||||
flushed := sig.FirstVisible && sig.VisibleFlushMS >= 0 && sig.VisibleFlushMS < defaultBurstFlushMS
|
||||
visible := sig.VisibleTokens
|
||||
heavyReasoning := sig.ReasoningTokens >= defaultBurstMinReasoning
|
||||
shortVisible := visible > 0 && visible < defaultBurstMaxVisible
|
||||
// Hold timed out, then a short greeting dumped with a large reasoning bill
|
||||
@@ -180,60 +202,95 @@ func qualityIsBurstDump(sig QualityStreamSignals, minOutput int64) bool {
|
||||
if sig.HoldExpired && shortVisible && heavyReasoning {
|
||||
return true
|
||||
}
|
||||
// Cipher met the floor so HasThinking is true, but visible tokens then
|
||||
// dump in <1s with almost no answer (148 out / 140 reasoning in 0.7s).
|
||||
if flushed && shortVisible && heavyReasoning {
|
||||
if qualityFastFlush(sig, defaultBurstFlushMS) && qualityHasDumpBill(sig) {
|
||||
return true
|
||||
}
|
||||
if barelyCipher && flushed && (visible >= minOutput || heavyReasoning) {
|
||||
return false
|
||||
}
|
||||
|
||||
// qualityIsFakeEncryptedDump is the 18190 / 18183 dump: ciphertext or a
|
||||
// large reasoning bill, then the visible answer arrives in <2s. Visible
|
||||
// token count is not a gate — vis<8 chat dumps were leaking on minOutput.
|
||||
func qualityIsFakeEncryptedDump(sig QualityStreamSignals, minOutput int64) bool {
|
||||
_ = minOutput
|
||||
if sig.HasReasoningDelta {
|
||||
return false
|
||||
}
|
||||
if !qualityFastFlush(sig, defaultFakeEncFlushMS) {
|
||||
return false
|
||||
}
|
||||
return qualityHasDumpBill(sig)
|
||||
}
|
||||
|
||||
// qualityIsFastReasoningRatioDump catches plaintext thinking that is still a
|
||||
// 1ms dump: billed reasoning is ≥80% of output and the visible flush is <2s.
|
||||
func qualityIsFastReasoningRatioDump(sig QualityStreamSignals) bool {
|
||||
if !sig.HasReasoningDelta {
|
||||
return false
|
||||
}
|
||||
if !qualityFastFlush(sig, defaultFakeEncFlushMS) {
|
||||
return false
|
||||
}
|
||||
output := sig.OutputTokens
|
||||
if output <= 0 {
|
||||
output = sig.VisibleTokens + sig.ReasoningTokens
|
||||
}
|
||||
if output <= 0 || sig.ReasoningTokens <= 0 {
|
||||
return false
|
||||
}
|
||||
return sig.ReasoningTokens*5 >= output*4
|
||||
}
|
||||
|
||||
// qualityIsCipherDrool is the 128k TUI status-loop: ciphertext met the
|
||||
// floor so HasThinking is true, but there is no plaintext reasoning and
|
||||
// usage.reasoning_tokens is still 0 while visible text is already dumping.
|
||||
func qualityIsCipherDrool(sig QualityStreamSignals, minOutput int64) bool {
|
||||
if minOutput <= 0 {
|
||||
minOutput = defaultQualityMinOutput
|
||||
}
|
||||
if sig.HasReasoningDelta || sig.ReasoningTokens > 0 {
|
||||
return false
|
||||
}
|
||||
if sig.EncryptedBytes <= 0 {
|
||||
return false
|
||||
}
|
||||
visible := sig.VisibleTokens
|
||||
if visible >= defaultCipherDroolVisible {
|
||||
return true
|
||||
}
|
||||
if sig.Terminal && visible >= minOutput {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ClassifyQualityHold decides whether a held stream may be forwarded.
|
||||
// Streamed thinking delivers: reasoning/summary deltas, or a reasoning item
|
||||
// whose encrypted_content meets the ciphertext floor. A non-empty stub such
|
||||
// as "gAAAA-cipher" is not thinking. Usage.reasoning_tokens alone does not —
|
||||
// degraded upstreams fill that field without ciphertext or deltas. A finished
|
||||
// sample with enough visible output and no streamed thinking is withheld.
|
||||
// Short replies below minOutput are delivered so "ok"/"yes" is not retried.
|
||||
// A hold timeout with no visible output is not fail-open: keep waiting for
|
||||
// more bytes or a stream abort so an empty hang is not flushed as HTTP 200.
|
||||
//
|
||||
// An empty reasoning stub is not thinking. Before the hold deadline, wait for
|
||||
// real evidence or a terminal event. A stub plus enough visible output at the
|
||||
// deadline is withheld — that is the TUI dump after 30s, not late ciphertext.
|
||||
// Stub-only empty streams keep waiting for idle/terminal handling.
|
||||
// HasThinking that is only a thin ciphertext dump after the hold (or a
|
||||
// barely-over-floor flush in <1s) is still withheld.
|
||||
// Dump detectors run first so plaintext thinking cannot veto a 1ms
|
||||
// reasoning-ratio dump, and vis<minOutput cannot skip fake-enc/burst.
|
||||
// Remaining plaintext deltas still deliver. Cipher-only HasThinking waits
|
||||
// until visible text has streamed for 2s or the stream ends.
|
||||
func ClassifyQualityHold(sig QualityStreamSignals, minOutput int64) QualityVerdict {
|
||||
if minOutput <= 0 {
|
||||
minOutput = defaultQualityMinOutput
|
||||
}
|
||||
// Readable reasoning deltas are direct proof and can preserve the original
|
||||
// low-latency release path. Ciphertext-only evidence remains provisional so
|
||||
// the classifier can observe visible output and terminal usage regardless of
|
||||
// how the SSE events were split across transport reads.
|
||||
if sig.HasReasoningDelta {
|
||||
return QualityDeliver
|
||||
if qualityIsBurstDump(sig, minOutput) || qualityIsCipherDrool(sig, minOutput) || qualityIsFakeEncryptedDump(sig, minOutput) || qualityIsFastReasoningRatioDump(sig) {
|
||||
return QualityWithhold
|
||||
}
|
||||
if sig.HasThinking {
|
||||
if !sig.FirstVisible && !sig.Terminal {
|
||||
return QualityWait
|
||||
if sig.HasReasoningDelta {
|
||||
return QualityDeliver
|
||||
}
|
||||
if qualityIsBurstDump(sig, minOutput) {
|
||||
if sig.Terminal {
|
||||
return QualityWithhold
|
||||
}
|
||||
return QualityWait
|
||||
// Cipher-only: do not release when encrypted_content first meets
|
||||
// the floor. Fake dumps send the blob, then the whole answer in
|
||||
// <2s; releasing early lets that dump bypass fake-enc. Wait until
|
||||
// visible text has streamed for 2s, or the stream ends.
|
||||
if sig.Terminal {
|
||||
return QualityDeliver
|
||||
}
|
||||
// The token-relative floor cannot be final until usage arrives. Preserve
|
||||
// HoldTimeout as the fail-open latency bound for still-open streams.
|
||||
if !sig.Terminal && !sig.HoldExpired && !sig.UsageReported {
|
||||
return QualityWait
|
||||
if sig.VisibleTokens >= minOutput && sig.FirstVisible && sig.VisibleFlushMS >= defaultFakeEncFlushMS {
|
||||
return QualityDeliver
|
||||
}
|
||||
return QualityDeliver
|
||||
return QualityWait
|
||||
}
|
||||
// Prefer observed/derived visible output. Total output includes reasoning
|
||||
// tokens, which are deliberately not trusted as quality evidence above. If
|
||||
|
||||
@@ -135,14 +135,12 @@ func qualityProtocolForOperation(operation audit.Operation) string {
|
||||
}
|
||||
|
||||
func (s *qualityScanState) signals() QualityStreamSignals {
|
||||
// Visible tokens come only from streamed content deltas (chat content /
|
||||
// responses output_text / message items). Do not lift from
|
||||
// usage.output − usage.reasoning: chat often reports completion_tokens
|
||||
// that still include reasoning, which made dumps look like long answers.
|
||||
visibleRunes := max(s.visibleRunes, s.aggregateRunes)
|
||||
visible := int64((visibleRunes + 3) / 4)
|
||||
if s.usage.Reported {
|
||||
fromUsage := s.usage.OutputTokens - s.usage.ReasoningTokens
|
||||
if fromUsage > visible {
|
||||
visible = fromUsage
|
||||
}
|
||||
}
|
||||
output := s.outputTokens
|
||||
if s.usage.Reported && s.usage.OutputTokens > output {
|
||||
output = s.usage.OutputTokens
|
||||
|
||||
@@ -68,50 +68,120 @@ func TestClassifyQualityHoldBurst(t *testing.T) {
|
||||
want QualityVerdict
|
||||
}{
|
||||
{
|
||||
name: "terminal hold-expired hello dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 2, ReasoningTokens: 954, EncryptedBytes: 4000, EncryptedFloor: 3816, UsageReported: true, FirstVisible: true, HoldExpired: true, Terminal: true},
|
||||
name: "hold-expired hello dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 2, ReasoningTokens: 954, EncryptedBytes: 4000, EncryptedFloor: 3816, HoldExpired: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "hold-expired long answer delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 200, ReasoningTokens: 954, EncryptedBytes: 8000, EncryptedFloor: 3816, UsageReported: true, FirstVisible: true, HoldExpired: true},
|
||||
want: QualityDeliver,
|
||||
},
|
||||
{
|
||||
name: "non-terminal barely-over-floor flush keeps observing",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 60, EncryptedBytes: 300, EncryptedFloor: 256, UsageReported: true, FirstVisible: true, VisibleFlushMS: 100},
|
||||
name: "hold-expired long answer still waiting without flush",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 200, ReasoningTokens: 954, EncryptedBytes: 8000, EncryptedFloor: 3816, HoldExpired: true},
|
||||
want: QualityWait,
|
||||
},
|
||||
{
|
||||
name: "terminal barely-over-floor flush withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 60, EncryptedBytes: 300, EncryptedFloor: 256, UsageReported: true, FirstVisible: true, VisibleFlushMS: 100, Terminal: true},
|
||||
name: "cipher-only visible streamed 2s delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 200, ReasoningTokens: 954, EncryptedBytes: 8000, EncryptedFloor: 3816, FirstVisible: true, VisibleFlushMS: 2500},
|
||||
want: QualityDeliver,
|
||||
},
|
||||
{
|
||||
name: "barely-over-floor flush withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 60, EncryptedBytes: 300, EncryptedFloor: 256, FirstVisible: true, VisibleFlushMS: 100},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "large cipher flush delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 60, EncryptedBytes: 2000, EncryptedFloor: 256, UsageReported: true, FirstVisible: true, VisibleFlushMS: 100},
|
||||
want: QualityDeliver,
|
||||
},
|
||||
{
|
||||
name: "non-terminal floor-met short burst keeps observing",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 3, ReasoningTokens: 1371, EncryptedBytes: 8000, EncryptedFloor: 5484, UsageReported: true, FirstVisible: true, VisibleFlushMS: 200},
|
||||
want: QualityWait,
|
||||
name: "large cipher flush without plaintext withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 60, EncryptedBytes: 2000, EncryptedFloor: 256, FirstVisible: true, VisibleFlushMS: 100},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "floor-met short visible fast dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 3, ReasoningTokens: 1371, EncryptedBytes: 8000, EncryptedFloor: 5484, UsageReported: true, FirstVisible: true, VisibleFlushMS: 200, Terminal: true},
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 3, ReasoningTokens: 1371, EncryptedBytes: 8000, EncryptedFloor: 5484, FirstVisible: true, VisibleFlushMS: 200},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "floor-met 8 visible 140 reasoning in under 1s withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 8, ReasoningTokens: 140, EncryptedBytes: 2000, EncryptedFloor: 560, UsageReported: true, FirstVisible: true, VisibleFlushMS: 660, Terminal: true},
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 8, ReasoningTokens: 140, EncryptedBytes: 2000, EncryptedFloor: 560, FirstVisible: true, VisibleFlushMS: 660},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "floor-met long visible fast flush delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 80, ReasoningTokens: 140, EncryptedBytes: 2000, EncryptedFloor: 560, UsageReported: true, FirstVisible: true, VisibleFlushMS: 200, Terminal: true},
|
||||
name: "floor-met long visible fast flush withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 80, ReasoningTokens: 140, EncryptedBytes: 2000, EncryptedFloor: 560, FirstVisible: true, VisibleFlushMS: 200},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "18190 fake enc 11s first-token dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 1425, ReasoningTokens: 1416, EncryptedBytes: 8000, EncryptedFloor: 5664, FirstVisible: true, VisibleFlushMS: 4, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "18190 fake enc 1.8s 1962-token dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 1962, ReasoningTokens: 1813, EncryptedBytes: 8000, EncryptedFloor: 7252, FirstVisible: true, VisibleFlushMS: 1776, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "plaintext thinking EOS dump withholds on reasoning ratio",
|
||||
sig: QualityStreamSignals{HasThinking: true, HasReasoningDelta: true, VisibleTokens: 9, OutputTokens: 1425, ReasoningTokens: 1416, EncryptedBytes: 8000, FirstVisible: true, VisibleFlushMS: 4, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "18183 vis7 chat dump withholds without minOutput",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 7, OutputTokens: 3812, ReasoningTokens: 3805, EncryptedBytes: 8000, EncryptedFloor: 15220, FirstVisible: true, VisibleFlushMS: 1, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "18183 vis1 chat dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 1, OutputTokens: 2313, ReasoningTokens: 2312, EncryptedBytes: 4000, EncryptedFloor: 9248, FirstVisible: true, VisibleFlushMS: 1, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "plaintext thinking slow stream still delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, HasReasoningDelta: true, VisibleTokens: 400, OutputTokens: 900, ReasoningTokens: 500, FirstVisible: true, VisibleFlushMS: 8000, Terminal: true},
|
||||
want: QualityDeliver,
|
||||
},
|
||||
{
|
||||
name: "cipher stub 128k drool withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 128000, ReasoningTokens: 0, EncryptedBytes: 800, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "cipher stub hold-expired short answer waits",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 64, ReasoningTokens: 0, EncryptedBytes: 400, HoldExpired: true},
|
||||
want: QualityWait,
|
||||
},
|
||||
{
|
||||
name: "cipher stub hold-expired 1024 visible withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 1024, ReasoningTokens: 0, EncryptedBytes: 400, HoldExpired: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "plaintext thinking with visible still delivers",
|
||||
sig: QualityStreamSignals{HasThinking: true, HasReasoningDelta: true, VisibleTokens: 64, ReasoningTokens: 0, EncryptedBytes: 400, HoldExpired: true},
|
||||
want: QualityDeliver,
|
||||
},
|
||||
{
|
||||
name: "cipher-only no visible waits",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 0, EncryptedBytes: 800, HoldExpired: true},
|
||||
want: QualityWait,
|
||||
},
|
||||
{
|
||||
name: "18190 smoke 107ms 1798-token dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 1798, ReasoningTokens: 1730, EncryptedBytes: 8000, EncryptedFloor: 6920, FirstVisible: true, VisibleFlushMS: 107, Terminal: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "cipher-only mid dump withholds",
|
||||
sig: QualityStreamSignals{HasThinking: true, VisibleTokens: 50, ReasoningTokens: 1730, EncryptedBytes: 8000, EncryptedFloor: 6920, FirstVisible: true, VisibleFlushMS: 50},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "usage-only vis7 heavy reasoning 1ms flush withholds",
|
||||
sig: QualityStreamSignals{VisibleTokens: 7, OutputTokens: 3812, ReasoningTokens: 3805, FirstVisible: true, VisibleFlushMS: 1, Terminal: true, UsageReported: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
{
|
||||
name: "usage-only vis1 heavy reasoning 1ms flush withholds",
|
||||
sig: QualityStreamSignals{VisibleTokens: 1, OutputTokens: 2313, ReasoningTokens: 2312, FirstVisible: true, VisibleFlushMS: 1, Terminal: true, UsageReported: true},
|
||||
want: QualityWithhold,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -133,13 +203,13 @@ func TestClassifyQualityHoldBurstUsesConfiguredFloor(t *testing.T) {
|
||||
if got := ClassifyQualityHold(configuredHigh, 8); got != QualityWithhold {
|
||||
t.Fatalf("configured high floor verdict = %s, want withhold", got)
|
||||
}
|
||||
configuredLow := QualityStreamSignals{
|
||||
configuredMiss := QualityStreamSignals{
|
||||
HasThinking: true, VisibleTokens: 40, ReasoningTokens: 60,
|
||||
EncryptedBytes: 300, EncryptedFloor: 64, UsageReported: true,
|
||||
EncryptedBytes: 300, EncryptedFloor: 4096, UsageReported: true,
|
||||
FirstVisible: true, VisibleFlushMS: 100, Terminal: true,
|
||||
}
|
||||
if got := ClassifyQualityHold(configuredLow, 8); got != QualityDeliver {
|
||||
t.Fatalf("configured low floor verdict = %s, want deliver", got)
|
||||
if got := ClassifyQualityHold(configuredMiss, 8); got != QualityDeliver {
|
||||
t.Fatalf("cipher below configured floor must not dump-bill: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,6 +448,55 @@ func TestObserveQualityChunkShortNoThinkIgnoresFakeReasoningUsage(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalsVisibleIgnoresUsageCompletion(t *testing.T) {
|
||||
t.Parallel()
|
||||
content := "Hi"
|
||||
chat := qualityScanState{protocol: qualityProtocolChat}
|
||||
ObserveQualityChunk(&chat, []byte(sse(
|
||||
`data: {"choices":[{"delta":{"content":"`+content+`"}}]}`,
|
||||
`data: {"usage":{"completion_tokens":2000,"completion_tokens_details":{"reasoning_tokens":0}}}`,
|
||||
"data: [DONE]",
|
||||
)))
|
||||
chatSig := chat.signals()
|
||||
if chatSig.VisibleTokens > 4 || chatSig.OutputTokens != 2000 {
|
||||
t.Fatalf("chat must not treat usage completion as visible: %#v", chatSig)
|
||||
}
|
||||
|
||||
resp := qualityScanState{protocol: qualityProtocolResponses}
|
||||
ObserveQualityChunk(&resp, []byte(sse(
|
||||
`data: {"type":"response.output_text.delta","delta":"`+content+`"}`,
|
||||
`data: {"type":"response.completed","response":{"id":"resp_1","usage":{"output_tokens":2000,"output_tokens_details":{"reasoning_tokens":0}}}}`,
|
||||
)))
|
||||
respSig := resp.signals()
|
||||
if respSig.VisibleTokens != chatSig.VisibleTokens {
|
||||
t.Fatalf("chat/responses visible mismatch: chat=%#v responses=%#v", chatSig, respSig)
|
||||
}
|
||||
if respSig.VisibleTokens > 4 || respSig.OutputTokens != 2000 {
|
||||
t.Fatalf("responses must not treat usage output as visible: %#v", respSig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObserveQualityChunkChatTinyVisibleHugeReasoningWithholds(t *testing.T) {
|
||||
t.Parallel()
|
||||
state := qualityScanState{protocol: qualityProtocolChat}
|
||||
ObserveQualityChunk(&state, []byte(sse(
|
||||
": grok2api-reasoning-start",
|
||||
`data: {"choices":[{"delta":{"content":"你好"}}]}`,
|
||||
`data: {"usage":{"completion_tokens":3812,"completion_tokens_details":{"reasoning_tokens":3805}}}`,
|
||||
"data: [DONE]",
|
||||
)))
|
||||
sig := state.signals()
|
||||
if sig.HasReasoningDelta {
|
||||
t.Fatalf("stub+content must not count as reasoning delta: %#v", sig)
|
||||
}
|
||||
if sig.VisibleTokens >= 8 {
|
||||
t.Fatalf("tiny chat dump visible inflated: %#v", sig)
|
||||
}
|
||||
if got := ClassifyQualityHold(sig, 8); got != QualityWithhold {
|
||||
t.Fatalf("vis<8 reasoning dump must withhold: %s (%#v)", got, sig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObserveQualityConvertedEncryptedThinking(t *testing.T) {
|
||||
t.Parallel()
|
||||
cipher := strings.Repeat("A", defaultMinEncryptedBytes*8)
|
||||
@@ -411,8 +530,8 @@ func TestObserveQualityConvertedEncryptedThinking(t *testing.T) {
|
||||
if !sig.HasThinking {
|
||||
t.Fatalf("converted encrypted thinking evidence was lost:\n%s", converted)
|
||||
}
|
||||
if got := ClassifyQualityHold(sig, 32); got != QualityDeliver {
|
||||
t.Fatalf("converted encrypted thinking verdict = %s (%#v)", got, sig)
|
||||
if got := ClassifyQualityHold(sig, 32); got != QualityWithhold {
|
||||
t.Fatalf("converted cipher dump without plaintext deltas must withhold: %s (%#v)", got, sig)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -562,8 +681,8 @@ func TestObserveQualityChunkResponsesReasoningItem(t *testing.T) {
|
||||
if !encSig.HasThinking || encSig.ReasoningTokens != 60 || encSig.EncryptedBytes < defaultMinEncryptedBytes {
|
||||
t.Fatalf("encrypted reasoning item must count as thinking: %#v", encSig)
|
||||
}
|
||||
if ClassifyQualityHold(encSig, 32) != QualityDeliver {
|
||||
t.Fatalf("encrypted thinking should deliver: %#v", encSig)
|
||||
if ClassifyQualityHold(encSig, 32) != QualityWithhold {
|
||||
t.Fatalf("encrypted thinking without plaintext deltas must withhold: %#v", encSig)
|
||||
}
|
||||
|
||||
floorCipher := strings.Repeat("A", defaultMinEncryptedBytes)
|
||||
@@ -700,30 +819,17 @@ func TestPeekQualityStreamCipherBurstWaitsAcrossEventSplits(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case result := <-done:
|
||||
if result.replay != nil {
|
||||
_ = result.replay.Close()
|
||||
}
|
||||
t.Fatalf("non-terminal burst was finalized early: verdict=%s err=%v", result.verdict, result.err)
|
||||
case <-time.After(40 * time.Millisecond):
|
||||
}
|
||||
if _, err := io.WriteString(writer, completed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case result := <-done:
|
||||
if result.replay != nil {
|
||||
_ = result.replay.Close()
|
||||
}
|
||||
if result.err != nil || result.verdict != QualityWithhold {
|
||||
t.Fatalf("split verdict=%s err=%v, want withhold", result.verdict, result.err)
|
||||
t.Fatalf("visible dump must withhold without waiting for completed: verdict=%s err=%v", result.verdict, result.err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("split terminal stream did not finish")
|
||||
t.Fatal("split visible dump did not withhold")
|
||||
}
|
||||
_ = writer.Close()
|
||||
}
|
||||
|
||||
func TestPeekQualityStreamWithholdsNoThinkEnough(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user