feat(middleware): gojq improvements — caching, panic-on-broken-init, secureCompileOpts#8280
Merged
Merged
Conversation
- P1: Cache CompileToolResponseFilterWithVars with composite key
(filter + "\x00" + strings.Join(varNames, "\x00")) to eliminate
repeated gojq.Parse+gojq.Compile on the parameterized-filter hot path
- P2: Replace misleading log.Printf("FATAL: ...") + return in init()
with panic() — the binary is broken if the hardcoded filter fails,
and the old code silently continued running with a nil jqSchemaCode
- P3: Consolidate gojq.WithEnvironLoader into a package-level
secureCompileOpts slice in both middleware and config packages,
preventing future compile sites from accidentally omitting the
$ENV-disabled defense-in-depth option
- P4: Add TODO comment on filterCodeCache re: hot-reload bounded caching
Also removes the now-unused "log" import from jqschema.go and adds
tests for the new WithVars cache hit/miss behaviour.
…nt varNames The test previously used different filter strings to prove a cache miss. It now uses the same filter string with different varNames slices, which is the actual behavior being documented (composite cache key includes varNames).
Copilot
AI
changed the title
[WIP] Review go module itchyny/gojq for performance and usage
feat(middleware): gojq improvements — caching, panic-on-broken-init, secureCompileOpts
Jun 29, 2026
1 task
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves gojq usage in the middleware/config layers by making tool-response filter compilation faster (compile-once, reuse), more fail-fast for built-in filters (panic on broken init), and more consistently secure ($ENV disabled via shared compile options).
Changes:
- Add caching for
CompileToolResponseFilterWithVarsusing a composite key to avoid repeatedParse+Compileon hot paths. - Change
init()behavior for the built-in jq schema filter topanicon parse/compile failure (broken binary should not start). - Centralize
$ENV-disabled gojq compiler options (secureCompileOpts) and apply them consistently; add tests validating cache hit/miss behavior for var-based compilation.
Show a summary per file
| File | Description |
|---|---|
| internal/middleware/jqschema.go | Adds secure compile opts, fail-fast init compilation, and caching for var-based filter compilation. |
| internal/middleware/jqschema_coverage_test.go | Adds tests validating cache behavior for CompileToolResponseFilterWithVars. |
| internal/config/validation.go | Centralizes secure compile opts and reuses them when validating jq filters with variables. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 2
- Review effort level: Low
| // In practice this is bounded by the number of distinct filter strings in the | ||
| // gateway configuration, which is typically small (one per tool). | ||
| // | ||
| // TODO: if config hot-reload is added, replace sync.Map with a bounded LRU cache. |
Comment on lines
+285
to
+289
| cacheKey := filter + "\x00" + strings.Join(varNames, "\x00") | ||
| if cached, ok := filterCodeCache.Load(cacheKey); ok { | ||
| code, ok := cached.(*gojq.Code) | ||
| if !ok { | ||
| // Should never happen; the cache only stores *gojq.Code values. |
Collaborator
|
@copilot address review feedback |
Contributor
Author
Addressed in Changes made:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Four improvements to
gojqusage identified by the go-fan module review. The most impactful is eliminating repeatedParse+Compileon the parameterized-filter hot path; the most correctness-critical is replacing a silent-failureinit()with a properpanic.Changes
P1 – Cache
CompileToolResponseFilterWithVars(internal/middleware/jqschema.go)Adds composite-key caching:
filter + "\x00" + strings.Join(varNames, "\x00"). Previously every tool invocation re-parsed and re-compiled the same expression; now it's compile-once-run-many like the no-vars path.P2 –
init()panics instead of logging FATAL (internal/middleware/jqschema.go)Old code called
log.Printf("FATAL: …")thenreturned, letting the binary start withjqSchemaCode == niland silently returning errors on every schema call.jqSchemaFilteris a hardcoded constant — a compile failure means a broken binary, not a recoverable runtime condition.P3 –
secureCompileOptspackage-level variable (internal/middleware/jqschema.go,internal/config/validation.go)Extracts the repeated
gojq.WithEnvironLoader(func() []string { return nil })into a shared slice in each package. Allgojq.Compilecall sites now reference it (orappendto it), making the$ENV-disabled intent explicit and preventing future omissions.P4 –
TODOonfilterCodeCacheDocuments that the unbounded
sync.Mapshould become a bounded LRU if config hot-reload is ever added.Tests
Added
TestCompileToolResponseFilterWithVars_CacheHit(pointer equality on repeated identical calls) andTestCompileToolResponseFilterWithVars_DifferentVarsCacheMiss(same filter, differentvarNames→ distinct*gojq.Code).