The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
perf(gateway): keep answer-first analysis behind the calls it follows, and hash a Portkey call's tail once more (#1273)
kyle-elliott-asymptotec8f07d45002c
diff --git a/gateway/pipeline/pipeline.go b/gateway/pipeline/pipeline.goindex cc3ddef8..d1118671 100644--- a/gateway/pipeline/pipeline.go+++ b/gateway/pipeline/pipeline.go@@ -217,6 +217,13 @@ func (p *Pipeline) Handle(ctx context.Context, c *gateway.Call) gateway.Verdictdefer p.release()defer p.bodies.release(held)defer released(c)+ // A goroutine starts ahead of every goroutine already waiting to run on its processor,+ // and the analysis would take it from goroutines answering other calls. gRPC's writer,+ // which sends each Envoy reply, yields after a small write and waits for the global run+ // queue, which a processor reads only when it has nothing else or seldom: under load,+ // analyses kept every processor busy, Envoy exchanges took eight times as long and calls+ // stalled behind them. Yielding first puts the analysis behind them instead.+ runtime.Gosched()p.run(context.WithoutCancel(ctx), c)}()return allowdiff --git a/gateway/portkey/portkey.go b/gateway/portkey/portkey.goindex ed434975..a76a4b81 100644--- a/gateway/portkey/portkey.go+++ b/gateway/portkey/portkey.go@@ -319,16 +319,23 @@ func (a *adapter) call(env *envelope) (*gateway.Call, bool) {// in both phases. Two calls with byte-identical bodies share an id, and a mutator guardrail that// rewrites request.json before the response phase breaks the match. The digest needs to be the// same on every replica, not unforgeable: a client that makes two of its own calls collide only-// confuses which of its reads were already reported. Two hardware CRCs and the length cost a-// fraction of a SHA-256 over a body of a megabyte.+// confuses which of its reads were already reported.+//+// It is the length, a hardware CRC of the whole body, and a second, of its last callTail bytes:+// two bodies of one length that differ there (as a session's calls do, each ending in the turn it+// adds) must match both, and two that differ only earlier must match the first. A second CRC of+// the whole body cost as much as the first, a fifth of a Portkey call.func callID(requestJSON []byte) string {var sum [16]bytebinary.BigEndian.PutUint32(sum[0:], crc32.Checksum(requestJSON, castagnoli))- binary.BigEndian.PutUint32(sum[4:], crc32.ChecksumIEEE(requestJSON))+ binary.BigEndian.PutUint32(sum[4:], crc32.ChecksumIEEE(requestJSON[max(0, len(requestJSON)-callTail):]))binary.BigEndian.PutUint64(sum[8:], uint64(len(requestJSON)))return "portkey-" + hex.EncodeToString(sum[:])}+// callTail is how much of the end of request.json callID's second CRC covers.+const callTail = 64 << 10+var castagnoli = crc32.MakeTable(crc32.Castagnoli)// metadata reads key from the client's metadata. Portkey types metadata as string values butdiff --git a/gateway/portkey/portkey_test.go b/gateway/portkey/portkey_test.goindex b84cedb3..89da1958 100644--- a/gateway/portkey/portkey_test.go+++ b/gateway/portkey/portkey_test.go@@ -1,6 +1,7 @@package portkeyimport (+ "bytes""context""io""log/slog"@@ -126,6 +127,24 @@ func TestWebhookCalls(t *testing.T) {}}+// The call id covers the whole body: a change anywhere in a long request.json, before the stretch+// at its end the second CRC covers as well as in it, gives another id, and the same bytes the+// same one.+func TestCallIDCoversWholeBody(t *testing.T) {+ body := []byte(strings.Repeat("x", 3*callTail))+ id := callID(body)+ if callID(bytes.Clone(body)) != id {+ t.Fatal("equal bodies, different ids")+ }+ for _, at := range []int{0, callTail, len(body) - callTail, len(body) - 1} {+ changed := bytes.Clone(body)+ changed[at] = 'y'+ if callID(changed) == id {+ t.Errorf("a change at byte %d of %d kept the id", at, len(body))+ }+ }+}+// A request-phase call hands on the messages of request.json as the walk of the payload split// them, for the codec to take rather than walk again: decoding with them is decoding without.func TestRequestCarriesSplitMessages(t *testing.T) {