Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

perf(gateway): skip argument parsing, system-text joins and a reply copy nothing reads

Kyle Elliottfa0663eb94e4
diff --git a/gateway/events/events.go b/gateway/events/events.go
index 577f0f7d..6b31fcf3 100644
--- a/gateway/events/events.go
+++ b/gateway/events/events.go
@@ -136,9 +136,11 @@ func (b *builder) request() {
newTools[toolKey{tool.Name, tool.Description}] = true
}
resent := b.t.WantsResent()
+ // Joining a system message's text copies it (a system prompt is tens of KB), so it is done
+ // only when the session has new system text or an analyzer reads resent content.
for i := range r.Messages {
m := &r.Messages[i]
- if m.Role != llm.RoleSystem {
+ if m.Role != llm.RoleSystem || len(newSystem) == 0 && !resent {
continue
}
text := m.Text()
diff --git a/gateway/harness/harness.go b/gateway/harness/harness.go
index 3eb68831..65e57baa 100644
--- a/gateway/harness/harness.go
+++ b/gateway/harness/harness.go
@@ -79,8 +79,6 @@ type Call struct {
// tool names and argument keys of Claude Code, Codex, omp and OpenCode. Bindings key on the tool
// name, not the harness, because the names do not collide.
func Bind(name, arguments string) Call {
- var args map[string]any
- _ = json.Unmarshal([]byte(arguments), &args)
c := Call{Name: name}
switch {
case strings.HasPrefix(name, "mcp__"):
@@ -89,9 +87,9 @@ func Bind(name, arguments string) Call {
c.Server, c.Tool, _ = strings.Cut(strings.TrimPrefix(name, "mcp__"), "__")
case name == "Bash" || name == "bash" || name == "shell" || name == "local_shell" || name == "exec_command":
c.Kind = Shell
- c.Command = command(args)
+ c.Command = command(parseArgs(arguments))
case name == "Read" || name == "read" || name == "view_image":
- target := firstString(args, "file_path", "filePath", "path")
+ target := firstString(parseArgs(arguments), "file_path", "filePath", "path")
if isURL(target) {
// omp's read takes a URL as well as a path and returns the page.
c.Kind, c.URL = Fetch, target
@@ -100,11 +98,23 @@ func Bind(name, arguments string) Call {
}
case name == "WebFetch" || name == "webfetch" || name == "fetch" || name == "web_fetch":
c.Kind = Fetch
- c.URL = firstString(args, "url")
+ c.URL = firstString(parseArgs(arguments), "url")
}
return c
}
+// parseArgs decodes a call's JSON arguments, nil when they are not an object. Only the kinds that
+// read an argument decode them: a tool definition binds with none, and an MCP or unknown call's
+// arguments are never read.
+func parseArgs(arguments string) map[string]any {
+ if arguments == "" {
+ return nil
+ }
+ var args map[string]any
+ _ = json.Unmarshal([]byte(arguments), &args)
+ return args
+}
+
// command reads a shell call's command: a string, or Codex's argv list.
func command(args map[string]any) string {
if s := firstString(args, "command", "cmd"); s != "" {
diff --git a/gateway/litellm/litellm.go b/gateway/litellm/litellm.go
index bd0b5a0c..06084306 100644
--- a/gateway/litellm/litellm.go
+++ b/gateway/litellm/litellm.go
@@ -198,9 +198,11 @@ func (a *adapter) call(req request) (*gateway.Call, error) {
case "response":
c.Phase = gateway.PhaseResponse
c.Body, err = responseBody(req)
- // LiteLLM names the session by its trace id on both phases. A reply payload that also
- // carries the request's messages hands them on, for a reply without one.
- if err == nil && nullable(req.StructuredMessages) != nil {
+ // LiteLLM names the session by its trace id on both phases, and the pipeline reads a
+ // reply's request only when nothing names its session: a reply payload that also carries
+ // the request's messages hands them on only for a reply without a trace id, rather than
+ // copying the whole conversation into a body nothing reads.
+ if err == nil && req.TraceID == "" && nullable(req.StructuredMessages) != nil {
c.RequestBody, err = requestBody(req)
}
default: