mirror of
https://github.com/JuliusBrussee/caveman.git
synced 2026-09-14 18:02:35 +08:00
82864c8f4c
Single-commit port of the Caveman 2 tree onto the original repo history (candidate caveman-2@88878a4 applied over main@3098342). No unrelated-history merge; original history untouched. Preserved v1 surfaces: - skills/caveman/SKILL.md byte-for-byte, plus plugin mirror and caveman.skill zip - legacy CI (Node 18/20/22 installer + Python 3.11 suites) alongside engine-ci - cli/install.js compatibility shim forwarding to bin/install.js - .codex/config.toml ships both hooks and codex_hooks for older Codex (#617) - README Trendshift + Skills.sh badges and local star-history asset - v1.10.0-pinned curl installs; benchmarks/tmp-starcharts ignore rules Fixed for public same-repo release: - SECURITY.md vulnerability reporting points at this repository - release-binaries.yml publishes to this repo via job-scoped GITHUB_TOKEN (cross-repo PAT removed); binary-release environment still gates signing - packages/cli/PUBLISHING.md rewritten for packages/ layout and same-repo flow - profile-contract and provider-catalog lanes skip lane-scope gates on initial import so the launch PR can gate on their real validation steps - ANNOUNCEMENT.md network claim corrected: compression is local; requests still go to the provider you configured Caveman 2 stays opt-in: bin-v1.0.0 binaries and @caveman-ai/cli publish separately after this lands and soaks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BLvb5QniwYjmcawyssQNw9
56 lines
2.0 KiB
Go
56 lines
2.0 KiB
Go
// Package sampling holds the one deterministic sampler every service shares.
|
|
//
|
|
// The construction is tenant-first, length-prefixed SHA-256 mapped onto the top
|
|
// 53 bits — the same mapping experiments.AssignBucket and the gateway's
|
|
// count-token baseline already used. Length prefixing matters: without it the
|
|
// key lists ("ab","c") and ("a","bc") would hash identically, and one tenant
|
|
// could steer another tenant's selection by choosing a request id. Nothing in
|
|
// the request body is hashed, so a caller can never influence its own selection.
|
|
//
|
|
// The point of sharing it is that the gateway and the worker must agree: the
|
|
// same (org, project, monitor, request) is sampled the same way in both, on
|
|
// every run, forever. Changing this hash silently reshuffles every population.
|
|
package sampling
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"hash"
|
|
)
|
|
|
|
// Fraction maps the ordered key list onto a stable value in [0, 1). The same
|
|
// keys always yield the same fraction; keys are length-prefixed so no two
|
|
// distinct key lists can collide by concatenation.
|
|
func Fraction(keys ...string) float64 {
|
|
h := sha256.New()
|
|
for _, key := range keys {
|
|
writePart(h, key)
|
|
}
|
|
sum := h.Sum(nil)
|
|
u := binary.BigEndian.Uint64(sum[:8])
|
|
return float64(u>>11) / float64(1<<53)
|
|
}
|
|
|
|
// Sampled reports whether the key list falls inside the given rate. rate <= 0
|
|
// selects nothing and rate >= 1 selects everything; a NaN rate fails closed
|
|
// (every comparison against NaN is false, so nothing is selected). Callers are
|
|
// responsible for rejecting an incomplete key scope before calling — an empty
|
|
// key still hashes, so a missing tenant id would otherwise sample as if it were
|
|
// a real, shared scope.
|
|
func Sampled(rate float64, keys ...string) bool {
|
|
if rate <= 0 {
|
|
return false
|
|
}
|
|
if rate >= 1 {
|
|
return true
|
|
}
|
|
return Fraction(keys...) < rate
|
|
}
|
|
|
|
func writePart(h hash.Hash, value string) {
|
|
var n [8]byte
|
|
binary.BigEndian.PutUint64(n[:], uint64(len(value)))
|
|
_, _ = h.Write(n[:])
|
|
_, _ = h.Write([]byte(value))
|
|
}
|