contextguru

package
v0.0.0-...-53bd38a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 57 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package contextguru is an outbound AuthBridge plugin that compacts an agent's LLM request context before it is forwarded upstream, using the embedded github.com/rossoctl/context-guru engine. It runs in the pre-LLM hook (OnRequest): it hands the outbound request body to apply.BodyWithModel, and if the engine rewrote it (dropped/reduced tool outputs, injected cache_control, etc.) it replaces the body via pctx.SetBody. OnResponse is a pass-through in v1 — model-driven restoration/expand is a later integration.

It is a request-body writer, and request-body writers chain: it may share the outbound chain with tool-prune or any other, each seeing the body as the one before it left it. It declares RequiresAny: [inference-parser] so a parser establishes the request is an inference call before it runs.

LLM-backed engine components (summarize, extract:code) are optional: configure a `cheap_model:` block to enable a static cheap model, and the plugin also reconstructs the agent's own "incoming" model from the live request so a component with model.source: incoming can reuse it. With no model available those components degrade to deterministic/no-op.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextGuru

type ContextGuru struct {
	// contains filtered or unexported fields
}

ContextGuru is the plugin. pipe/store are built once at Configure and shared across requests (store is synchronized; the engine holds no mutable pipeline state). static is the configured cheap model's template (nil when none); it carries no pctx, since it is shared across requests — OnRequest copies it per request with staticModelFor so Complete can report its spend op to the right one.

func New

func New() *ContextGuru

New returns an unconfigured plugin instance.

func (*ContextGuru) Capabilities

func (p *ContextGuru) Capabilities() pipeline.PluginCapabilities

func (*ContextGuru) ConfigSchema

func (p *ContextGuru) ConfigSchema() []pipeline.FieldSchema

ConfigSchema surfaces the operator-facing fields in agentop / /v1/plugins.

func (*ContextGuru) Configure

func (p *ContextGuru) Configure(raw json.RawMessage) error

func (*ContextGuru) EraseAllSessions

func (p *ContextGuru) EraseAllSessions(context.Context) error

EraseAllSessions is EraseSession for every session: it empties the session state (memory and context-guru-state.db, the content-keyed results included), the forwarded bodies, the keep-alive entries and the restart-grace record, in every live instance.

func (*ContextGuru) EraseSession

func (p *ContextGuru) EraseSession(_ context.Context, sessionID string) error

EraseSession removes what the plugin holds for one Cortex session (pipeline.SessionEraser), in every live instance (liveInstances). It removes:

  • the session state, every thread: in memory, in the writes not yet flushed, and in context-guru-state.db (store.Memory.EraseSession), with the session's rewind payloads that no other session owns;
  • the last forwarded body, main agent and Task subagents alike (stash, subagentStash);
  • the keep-alive entries, which hold the masked body a ping resends (Keeper.RetireSession);
  • the restart-grace record (threadSeenTable, rossoctl/cortex#2277 item 1): it lives in the same context-guru-state.db file, keyed by session, so an erase must remove it too, or a reused session id reads as recently seen and keeps its prefix frozen (review finding on #2295).

It leaves the thread tracker (hashes and counts, no text), the incoming-model credential holds (shared by every session on one credential) and the engine's content-keyed results, which name no session. DELETE /v1/sessions (EraseAllSessions) removes the results too.

func (*ContextGuru) Init

func (p *ContextGuru) Init(context.Context) error

Init attaches the store's disk copy and starts its flusher. It runs in Init and not in Configure so the flusher is under the pipeline's Start/unwind/Stop contract: a pipeline that a failed build or reload drops is never started, and so holds no flusher. Init also starts the keep-alive sweep, when the resolved cache policy has it on, and the incoming-model credential-hold sweep (held, not a nil/zero holds: Configure always builds one, see newCredHolds there). Each releases a hold idle past its own TTL on a timer, not only when a later request happens to ask for one. It FAILS OPEN and never returns an error.

func (*ContextGuru) Metrics

func (p *ContextGuru) Metrics() []pipeline.Metric

Metrics implements pipeline.MetricsProvider.

func (*ContextGuru) Name

func (p *ContextGuru) Name() string

func (*ContextGuru) OnFinish

func (p *ContextGuru) OnFinish(_ context.Context, pctx *pipeline.Context)

OnFinish hands the finished request to the keeper. Here and not in OnResponse: inference-parser settles token counts after this plugin's response hook, and by OnFinish they are final and pctx.Body still holds the compacted bytes that went upstream. A mapping and one Record call — no I/O — so it stays far inside the finish timeout and cannot change the outcome.

func (*ContextGuru) OnRequest

func (p *ContextGuru) OnRequest(ctx context.Context, pctx *pipeline.Context) pipeline.Action

func (*ContextGuru) OnResponse

func (p *ContextGuru) OnResponse(_ context.Context, _ *pipeline.Context) pipeline.Action

OnResponse is a pass-through in v1. The model-driven expand loop (resolve markers from the Store + re-invoke upstream via llmclient) would live here in a later integration; the Store + session key are already threaded for it.

func (*ContextGuru) Resend

func (p *ContextGuru) Resend(_ context.Context, pctx *pipeline.Context, status int, body []byte) bool

Resend implements pipeline.Resender. It is inband.RetryRefused, split over the listener's rounds. On a 400 or 422 to a request deferral changed, it sends the request without deferral, once. When that original is refused too because its transcript names a tool `tools` lacks, it sends it once more with a deferred stub for that tool (rossoctl/context-guru#871). The client never sees deferral's own 400: Claude Code would turn its tool search off for the session.

It returns false at once for every request this plugin did not defer, which is most of them: deferState is set only on a changed, deferred request (deferTools). But implementing Resender at all makes this plugin a Resender in EVERY chain it runs in, at every preset, not only ultrahigh: resenderOf is a type assertion on the method's presence, with no way to say "only when configured for deferral". pipeline.Pipeline.HasResenders() then reports true for that chain, and a forward-proxy request on it reads up to pipeline.ResendPeekLimit (64 KiB) of every non-2xx answer's body before relaying it — a quiet latency cost for a slow-streaming or open-ended error body, on every host that pipeline chain is used for, whether or not the request is Anthropic Messages or ultrahigh (clawgenti review finding). For an ordinary error body this costs nothing measurable; a follow-up could add a pctx-level opt-in that resendIfAsked checks before peeking, so an unrelated chain pays nothing at all.

func (*ContextGuru) SetAccounting

func (p *ContextGuru) SetAccounting(h *accounting.Host)

SetAccounting receives the process's accountant host (plugins.Deps, before Configure). Keep-alive pings are accounted through it; with none, a ping is only logged.

func (*ContextGuru) SetLocalInstall

func (p *ContextGuru) SetLocalInstall(local bool)

SetLocalInstall receives whether this is a laptop install (plugins.Deps, before Configure). On a local install the plugin keeps its session state at ~/.cortex/context-guru-state.db, and in the user cache directory otherwise (attachStateDB); its model calls and keep-alive pings bypass HTTPS_PROXY (see localModelTransport), which otherwise points at Cortex itself; and context_window.model_info defaults to the LiteLLM map rather than off (buildWindows).

func (*ContextGuru) SetPricingResolver

func (p *ContextGuru) SetPricingResolver(r pricing.Resolver)

SetPricingResolver receives Cortex's rate table (plugins.Deps, before Configure). The keep-alive dispatcher prices its pre-flight ceiling from it.

func (*ContextGuru) Shutdown

func (p *ContextGuru) Shutdown(context.Context) error

Shutdown stops both sweeps and releases every held credential before returning: on a hot-reload this instance is the old one, and its holds must not outlive it. The hold sweep is stopped before releaseAll, so the sweep goroutine's own release of an expired hold cannot race this one releasing the same hold a second time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL