{"record":{"id":"72b99c82074ebff3","repo":"multica-ai/multica","slug":"invalid-runtime-counts","errorCode":null,"errorMessage":"invalid runtime counts","messagePattern":"invalid runtime counts","errorType":"validation","errorClass":null,"httpStatus":400,"severity":"warning","filePath":"server/internal/handler/client_usage.go","lineNumber":203,"sourceCode":"\nfunc validateClientUsageRuntime(probe clientUsageRuntimeProbe) (validatedRuntimeProbe, error) {\n\tresult := strings.ToLower(strings.TrimSpace(probe.ProbeResult))\n\tif result != \"success\" && result != \"error\" {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"runtime probe_result must be success or error\")\n\t}\n\tvalidated := validatedRuntimeProbe{Result: pgtype.Text{String: result, Valid: true}}\n\tif result == \"error\" {\n\t\tif probe.RuntimeCount != nil || probe.ProviderSummary != nil || probe.OnlineCount != nil || probe.OfflineCount != nil {\n\t\t\treturn validatedRuntimeProbe{}, errors.New(\"failed runtime probes must not include counts\")\n\t\t}\n\t\treturn validated, nil\n\t}\n\n\tif probe.RuntimeCount == nil || probe.ProviderSummary == nil || probe.OnlineCount == nil || probe.OfflineCount == nil {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"successful runtime probes require all counts\")\n\t}\n\tif *probe.RuntimeCount < 0 || *probe.RuntimeCount > 1000 || *probe.OnlineCount < 0 || *probe.OfflineCount < 0 || *probe.OnlineCount+*probe.OfflineCount != *probe.RuntimeCount {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"invalid runtime counts\")\n\t}\n\tif len(probe.ProviderSummary) > 32 {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"too many runtime providers\")\n\t}\n\tvar providerTotal int64\n\tfor provider, count := range probe.ProviderSummary {\n\t\tif !providerNamePattern.MatchString(provider) || count < 0 || count > 1000 {\n\t\t\treturn validatedRuntimeProbe{}, errors.New(\"invalid runtime provider summary\")\n\t\t}\n\t\tproviderTotal += int64(count)\n\t}\n\tif providerTotal != int64(*probe.RuntimeCount) {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"runtime provider counts do not match runtime_count\")\n\t}\n\tsummary, err := json.Marshal(probe.ProviderSummary)\n\tif err != nil {\n\t\treturn validatedRuntimeProbe{}, errors.New(\"invalid runtime provider summary\")\n\t}","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/multica-ai/multica/blob/2c0912b6ec764b373d44eeea1e80f0d9f11ab417/server/internal/handler/client_usage.go#L185-L221","documentation":"Once a success probe carries all four count fields, validateClientUsageRuntime checks their arithmetic and range: runtime_count must be within 0..1000, online_count and offline_count must each be >= 0, and online_count + offline_count must equal runtime_count. This rejects internally inconsistent telemetry (counts that cannot describe the same set of runtimes) and caps the magnitude to keep the stored rows bounded.","triggerScenarios":"POST success probe with runtime_count=1500 (over cap); online_count=3, offline_count=4 but runtime_count=6 (3+4≠6); negative offline_count=-1. Any one condition triggers the shared 'invalid runtime counts' error.","commonSituations":"Race between probing and counting on the client (set changes between online/offline tally and total tally); client counting 'pending' runtimes in runtime_count but in neither online nor offline; integer overflow or unit confusion after a refactor; genuinely >1000 runtimes needing a batched report.","solutions":["Compute all three counts from one consistent snapshot so online + offline == total by construction.","Cap the report at 1000 runtimes (sample or batch) before sending.","Assert the invariant client-side before the POST: online >= 0 && offline >= 0 && online + offline === runtime_count && runtime_count <= 1000.","If totals drift because of concurrency, take a lock or copy the runtime list once and derive all counts from it."],"exampleFix":"// before\nconst online = runtimes.filter(r => r.online).length;\nconst total = await recountRuntimes(); // second pass, may drift\n\n// after\nconst snapshot = [...runtimes]; // one consistent snapshot\nconst online = snapshot.filter(r => r.online).length;\nconst offline = snapshot.length - online;\nconst total = snapshot.length; // online + offline === total by construction","handlingStrategy":"validation","validationCode":"function assertCountsConsistent(online, offline, total) {\n  if (!Number.isInteger(total) || total < 0 || total > 1000) throw new RangeError(`runtime_count ${total} out of [0,1000]`);\n  if (!Number.isInteger(online) || online < 0) throw new RangeError(`online_count ${online} invalid`);\n  if (!Number.isInteger(offline) || offline < 0) throw new RangeError(`offline_count ${offline} invalid`);\n  if (online + offline !== total) throw new RangeError(`online+offline (${online + offline}) != runtime_count (${total})`);\n}","typeGuard":"const countsAreConsistent = (o, f, t) => Number.isInteger(t) && t >= 0 && t <= 1000 && Number.isInteger(o) && o >= 0 && Number.isInteger(f) && f >= 0 && o + f === t;","tryCatchPattern":null,"preventionTips":["Derive online/offline/total from a single copied snapshot of the runtime list.","Run the invariant check right before the POST and drop the report (with a log) rather than sending a known-inconsistent one.","If >1000 runtimes exist, sample or batch so total stays within the cap."],"tags":["validation","telemetry","invariant","http-400"],"backgroundTag":null,"analyzedSha":"2c0912b6ec764b373d44eeea1e80f0d9f11ab417","analyzedAt":"2026-08-15T13:25:18.241Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}