predictive

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 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.

README

Predictive keep-alive campaigns

A learned policy for the keep-alive: when a response ends, estimate how likely the conversation is to come back inside each ping window, and buy exactly as many pings as pay for themselves. Built per tenant from that tenant's own history, tried in shadow (decides on live traffic, changes nothing), then switched to active.

Off by default. Nothing runs until a manager builds a campaign for a tenant and switches it to shadow or active.

Turning it on

  1. Build a campaign for one tenant (manager session, audited): POST /api/cache/campaigns/build with {"scope":"tenant","id":"<tenant id>","window":"30d","builder":"auto"}. builder:"auto" needs nothing beyond the proxy; builder:"model" (the default) runs the Python training pipeline and needs CG_CAMPAIGN_PYTHON pointing at a Python with numpy, pandas, lightgbm and scikit-learn.
  2. Read the result: GET /api/cache/campaigns/{id}. recommendation is ship, baseline or insufficient_data.
  3. Switch it on: POST /api/cache/campaigns/{id}/mode with {"mode":"shadow"} to score live decisions without changing anything, then {"mode":"active"} to let it set the ping count. active is refused unless the build recommended ship (or "force":true).
Setting Default Meaning
CG_CAMPAIGN_DIR campaigns/ beside the dashboard database where built campaigns are stored; no directory means no campaigns
CG_CAMPAIGN_PYTHON python3 interpreter for builder:"model" builds
CG_CAMPAIGN_SCRIPT scripts/campaign/build_campaign.py beside the binary the model build pipeline
CG_CAMPAIGN_AUTO_REFRESH 24h rebuild live auto campaigns in place; off disables

How it fails safe. Shadow mode changes nothing. Active mode only sets how many pings a tenant's existing keep-alive sends: it never turns the keep-alive on for a tenant that has it off, never overrides a manager strategy or a per-session override, and never widens the per-ping dollar ceiling. The kill switch (CONTEXT_GURU_KEEPALIVE=off) stops campaigns with everything else. A panic inside a campaign is recovered and means "no campaign" for that response. A model file whose SHA-256 does not match its spec is refused. Purging or deleting a tenant erases its campaigns.

The policy

Two layers, deliberately separate:

  1. A model turns what the proxy knows at the end of a response into a survival curve S(t) of the time to the next request. The model is pluggable (predictive.Model, one method); two are shipped.

    • lgb-discrete-hazard (default): the research LightGBM discrete-time hazard. Time is cut into 20 bins; a gradient-boosted classifier gives the hazard of each bin from 72 request inputs plus the bin and its edge; S(edge) = prod (1 - h). 300 trees x 127 leaves by default. Its inputs are the research "meta" family (request columns plus thread and session features): no tenant-wide history, no content, nothing that needs the future of the thread. Trees are exported to JSON and evaluated in Go (lgb.go).
    • weibull3-mixture-mlp: a ~5.6K-parameter network with a censored Weibull-mixture output (37 inputs). Kept as the second implementation of the interface and as a parity target.
  2. The rule, model independent and with no tuned constant (Decide):

    k* = argmax_{k <= kmax}  sum_{j<=k}  stake * (S(x(j-1)+Tc) - S(xj+Tc))  -  pingCost * S(xj)       stake = prefix * (write5m - read),  x = 280 s,  Tc = 295 s,  kmax = 4
    

    The j-th ping is paid only if the user has not returned by xj (the S(xj) factor) and earns stake if the return falls in the window it opens. k* = 0 lets the cache lapse.

piece where
inference, the rule, the live feature tracker, spec v2 types predictive/ (Go, no dependencies; the default 300-tree LightGBM model about 0.5-2 ms per decision (1.7 ms measured on a host at load 77), the MLP ~30 us; both run after the response is complete)
training, replay, held-out comparison, JSON export scripts/campaign/ (Python: numpy, pandas, lightgbm, scikit-learn; torch only for the MLP; build time only)
runtime (shadow and active) proxy/predictive.go, hooked into keeper.arrive / keeper.record
build and lifecycle endpoints proxy/campaignbuild.go

Parity with the Python reference

  • Model (lgb_parity_test.go, 1,200 decision points, fixture testdata/lgb.*): the Go tree evaluator reproduces LightGBM's survival curve to about 1e-16 (requirement 1e-5) and every ping count, in two cost regimes so more than one k is exercised. The MLP (parity_test.go) agrees to about 1e-15. The Go evaluator rounds its inputs to float32 before comparing them with a threshold, because training and the Python reference see float32 inputs.
  • Features: the Go Tracker, fed only start / end / response events, reproduces the Python feature vectors (and so the decisions) on all rows except those where a thread had parallel requests (below) and at most 0.25% of the rest (two requests of one session finishing in the same millisecond).

Every fixture under testdata/ is synthetic: scripts/campaign/synth_fixtures.py generates the traffic (fixed seeds) and runs export_fixture_lgb.py / export_fixture_mlp.py and the reference auto tuner on it (needs numpy, pandas, lightgbm, scikit-learn, torch; not run in CI).

Leak audit of the inputs

Neither model reads thread_len (a thread's total length, which with the turn index reveals the last request of the thread) or anything derived from it. Counts of a thread or session are running counts of the requests seen so far (turn_in_thread, m_n_req_thread, session_turn), never totals. The inputs, in model order:

  • LightGBM (72): messages, tools, system_blocks, stream, session_turn, turn_in_thread, hour_utc, dow, prefix_tok, output_tok, read_cost, rebuild_cost, ping_cost, cost_usd, fresh_input, stake, ping_over_stake, family, agent_type, provider, stop_reason, thinking and m_n_req_thread, m_n_req_session, m_session_age_s, m_thread_age_s, m_ctx_tokens, m_ctx_window, m_ctx_fill, m_cache_read_ratio, m_cache_write_ratio, m_fresh_ratio, m_rewrite, m_rewrites_last5, m_reqs_since_rewrite, m_ctx_growth, m_ctx_growth_ratio, m_gap1, m_gap2, m_gap3, m_gap_mean5, m_gap_median5, m_gap_std5, m_gap_max10, m_gap_trend5, m_gap_ewma, m_is_user_turn, m_since_user_turn_s, m_tool_streak, m_reqs_since_user_turn, m_messages, m_messages_delta, m_reqs_since_compaction, m_compactions, m_stop_reason, m_prev_stop_reason, m_model, m_agent, m_cache_ttl, m_tools, m_system_blocks, m_out_tokens, m_out_ratio, m_resp_s, m_ttfb_s, m_resp_mean5, m_thinking, m_effort, m_max_tokens, m_status_ok, m_cost_req, m_cost_session_cum (plus the time bin and its edge inside the hazard model). m_ctx_window is a per-model constant table read from the training rows only (a model unseen there gets a missing window and a missing category code, which the trees route by their stored default direction, the same in Python and Go; TestCacheCampaignWithTheLightGBMModel decides on an unseen model), the only input that uses more than the request's own past.
  • Weibull-mixture network (37): l_prefix, l_out, l_fresh, cw_frac, l_msgs, l_tools, l_turn, l_sturn, l_dur, l_sage, pg_first, pg_pos, pg_neg, pg2, hs, hc, ds, dc, kv_p5, kv_p1, kv_ln, gq_nan, l_gq, think, stream, l_ping, sr_tool_use, sr_stop_sequence, sr_end_turn, sr_stop, sr_tool_calls, fam_opus, fam_sonnet, fam_haiku, ag_claude-cli, ag_openai, ag_asyncopenai. l_turn is log1p of the running thread index; kv_* and l_gq are statistics of return gaps CLOSED before the decision time (a gap counts only once the successor request has started); pg_* are idle gaps before this request.

Every input, and how the proxy computes it

All 72 inputs of the default model are computed live at response end; none is dropped. Live below is LiveFacts, filled in proxy.go's terminal defer from the capture that also fills the dashboard row, so the campaign sees the values it was trained on. "Tracker" is predictive.Tracker (per campaign, in memory).

inputs how
messages / m_messages Live.Messages = the pipeline trace's message count (the dashboard messages column)
tools, system_blocks, stream, m_tools, m_system_blocks, m_max_tokens, thinking, m_thinking, m_effort Live, from metaFromBody on the pristine inbound body (the dashboard's own parse)
m_cache_ttl Live.CacheTTL = the pipeline trace's cache tier asked for
family, m_model, m_ctx_window, m_ctx_fill the model name; the window is the model file's own table (smallest of 128k / 200k / 400k / 1M above the largest context seen in training)
agent_type, m_agent dash.AgentFor(User-Agent), as stored on the row
provider the routed provider name, as stored on the row
prefix_tok, fresh_input, output_tok, m_ctx_tokens, m_cache_read_ratio, m_cache_write_ratio, m_fresh_ratio, m_out_tokens, m_out_ratio, m_rewrite, stop_reason, m_stop_reason the response's usage block and stop reason
read_cost, rebuild_cost, ping_cost, stake, ping_over_stake, cost_usd, m_cost_req usage x the model's rates (modelinfo.Price); a ping = prefix read + 7 fresh tokens + 1 output token
hour_utc, dow UTC calendar of the response end
m_resp_s, m_resp_mean5 request start to response end
m_ttfb_s Live.TTFBMs: time to first byte on streamed responses, 0 on buffered ones, exactly as the dashboard records it
m_status_ok constant 1: only successful responses are decision points
session_turn, m_n_req_session, m_session_age_s Tracker, per (tenant, session)
turn_in_thread, m_n_req_thread, m_thread_age_s, m_gap1..3, m_gap_mean5, m_gap_median5, m_gap_std5, m_gap_max10, m_gap_trend5, m_gap_ewma Tracker, per thread, from request start times and the previous response's end (known at the request's start; a previous request still running contributes gap 0)
m_ctx_growth, m_ctx_growth_ratio, m_messages_delta, m_prev_stop_reason, m_rewrites_last5, m_reqs_since_rewrite, m_is_user_turn, m_since_user_turn_s, m_tool_streak, m_reqs_since_user_turn, m_reqs_since_compaction, m_compactions Tracker, per thread, from the previous finished request's response (size, stop reason, message count)
m_cost_session_cum Tracker: billed cost of the session's requests the proxy had finished with by this one, including it

Where the live value can differ from the offline research definition (each is measured in the parity test or listed here):

  • Parallel requests on one thread (subagents, forks that the thread rule keeps together): the offline definition reads the previous request's tokens and stop reason even when it is still running at this one's end, which a live system cannot know. The tracker then uses the last finished request. This is a small share of decision points; Row.Skipped marks them and the 10 rows after. The LightGBM parity fixture contains such rows; the test bounds how many of their ping counts may differ from the offline definition (at most half, in both cost regimes) and requires the rest to match.
  • Thread identity: the proxy's own thread id (threadFor) stands in for the research thread rule (tools / system blocks / message-count continuity). They agree on the common case, not always.
  • Counters (turn_in_thread, session_turn, session age) restart at zero after a process restart or six idle hours.
  • m_cost_session_cum is redefined causally (the research feature sums every earlier-started request, which with parallel threads includes costs still unknown).
  • Response end is taken when the proxy finishes returning the response (about 40 ms after the provider's end).
  • The keeper never pings a conversation's first request (an existing gate), while training rows include them.

Not an input and not available live: the thread's total length (thread_len), which would reveal the last request of a thread. It is excluded everywhere.

Safety properties (tested)

  • Shadow is inert. The campaign runs in keeper.record only after the kill switch and the no-audit-sink refusal, so a switched-off or unaudited keep-alive tracks and retains nothing for it; any panic inside it is recovered and means "no campaign applies" for that response (TestShadowCampaignIsInert: a panicking model leaves the keeper's registrations identical to no campaign).
  • Active never overrides a tenant's opt-out. It acts only when the tenant's own keep-alive policy is on (TestActiveCampaignNeverOverridesTenantOptOut); the skipped activations are counted (active_skipped_tenant_opt_out). A manager strategy still forces it on, as before; a campaign does not. Precedence is strategy over campaign, per-session override over both, and the per-ping dollar ceiling is never widened.
  • Requests keep their identity. The tracker's Arrival travels with the request (through the capture), not with its thread, and shadow decisions are kept per request: overlapping requests on a thread are counted once each and scored against their own response (TestOverlappingRequestsOnAThreadKeepTheirOwnState, TestOverlappingRequestsAreCountedOnce).
  • No unbounded history. A request dropped before its response, or a plan-only campaign that never takes features, cannot grow a thread's predecessor chain (TestPredecessorChainsStayBounded, 100,000 requests).
  • The build subprocess gets a minimal environment (PATH, locale, temp dir, CG_CAMPAIGN_*, thread limits): none of the proxy's upstream or gateway credentials; arguments are passed as --name=value.

Campaign spec v2 (cg.cache.campaign/2)

A campaign is an ordered list of segments; each says which rule decides for the requests it matches. Predicates are [fact, op, value] triples over family, stop_reason, model, agent, prefix_tok, ping_cost; the first matching segment wins, else policy.fallback.

{ "schema": "cg.cache.campaign/2", "version": "2.0.0", "built_at": "...", "scope": {"type": "tenant", "id": "..."}, "window": {...},
  "menu": { "none": {...}, "model": {"gaps_s": [280,...], "max_pings": 4, "note": "K chosen per decision point by the model"}, "u280k2": {"gaps_s": [280,280], ...} },
  "policy": { "kind": "model+segments",                       // or "rules" (no model: plan / none segments only)
              "model": {"ref": "model.json", "sha256": "...", "schema": "cg.cache.predictor/1", "kind": "lgb-discrete-hazard", "n_features": 72, "rule": {"lam":1,"x_s":280,"kmax":4,"tc_s":295}},
              "segments": [ {"id": "family-haiku", "when": [["family","==","haiku"]], "rule": {"kind": "none"}},
                            {"id": "all", "when": [], "rule": {"kind": "model"}} ],     // rule kinds: model | plan (menu name) | none
              "fallback": {"kind": "none"} },
  "segments": [ ...per-segment held-out table... ], "evidence": {...}, "caveats": [...] }

The runtime refuses a model file whose SHA-256 differs from the one in the spec (it is not the model that was evaluated).

Endpoints (manager only, audited)

POST /api/cache/campaigns/build body {"scope":"user"|"tenant","id":"<tenant id>","window":"30d" | {"days":N} | {"since":ms,"until":ms},"objective":"net_usd","builder":"model"|"auto","session_end":false} -> 202 {"campaign_id","state":"running","poll","scope","window","objective"}. One build at a time (429 otherwise), 404 if the scope has no traffic in the window, 422 if none of its models is priced.
GET /api/cache/campaigns {"campaigns":[view...]}, newest first, without the spec
GET /api/cache/campaigns/{id} view below
POST /api/cache/campaigns/{id}/mode body {"mode":"off"|"shadow"|"active","force":false}. active needs the build's recommendation to be ship unless force. 409 if the build is unfinished or the scope had too little history.

View: {"id","state":"running|done|failed","stage","error","created_at","scope":{"type","id"},"window":{"since","until"},"objective","mode","live","recommendation":"ship|baseline|insufficient_data","seconds", "summary":{"model":{row},"best_baseline":{row},"vs_best_baseline":{"diff_usd","ci95"},"oracle_net_usd","split","class"}, "shadow":{...}, "spec":{...}}. A table row is {"strategy","net_usd","ci95","gap_captured","pings","credited_rate","ping_usd","wasted_ping_usd"}. shadow (only while the campaign is live) is {"decisions","k_hist":[13],"resolved","returned","would_pings","would_rescues","est_ping_usd","est_rescue_usd","est_net_usd","skipped_no_price","last_decision_ms"}: a running closed-form score of the live decisions (a decision resolves when the next request arrives on that thread, or counts as no-return after two hours).

recommendation defaults to baseline (do not run the model; keep the best fixed rule). It is ship only if the model's held-out net is positive and its paired difference to the best simple strategy (the best of the six on the same held-out rows, picked in hindsight, so the test is conservative) has a 95% interval entirely above zero. insufficient_data below 500 held-out rows, 20 sessions or 50 observed returns. shadow mode stays available whatever the recommendation (it changes nothing), to collect live evidence; active needs ship or force.

How a build works

  1. the server writes the tenant's model prices and runs scripts/campaign/build_campaign.py (niced, 2 threads, 45 min limit) against a read-only handle on the dashboard database;
  2. decision points are built (threads, next compatible request, outcome labels), causal features computed, the first 70% of the window (by response end) trains the model (LightGBM: 300 rounds on up to 200,000 requests expanded to person-period rows; --model mlp trains 3 seeds of the network instead);
  3. the last 30% is replayed in closed form for the model and every baseline (never, blanket K1 / K2 / K3, the tuned best single plan, the merged rule K2 at >= 80k prefix and <= $0.50 per ping, stop-reason-gated K2, and a hindsight oracle), with 95% session-bootstrap intervals and paired differences;
  4. the shipped model is the evaluated one (no refit on the held-out slice), so the table describes exactly what runs.

Trade-off: the build is Python because training and the bootstrap are build-time work that does not belong on the request path, and a Go port would duplicate the feature code a third time. The cost is a python3 + numpy + pandas + lightgbm + scikit-learn dependency (torch only for --model mlp) on the build host (CG_CAMPAIGN_PYTHON). Inference, the part that runs per request, is Go.

Dollars are closed-form units calibrated to an offline replay engine (scripts/campaign/engine_calibration.json). Compare strategies; do not read the level.

The auto builder ("builder":"auto")

A plan chooser, not a predictor, and no Python: predictive/autotune.go picks the tenant's plan from the menu "ping k times, 240 s apart", k = 0..6, and proxy/campaignauto.go wraps it in a campaign (spec kind rules, one segment, the chosen plan).

  • Data: the fleet's rows (for the prior) come from one read capped at the dashboard's newest-rows limit; the tenant's own rows are read on their own, so a busy fleet never pushes them out. The spec's evidence.data and caveats say when either read was truncated.
  • Every row of every tenant in the window is scored in closed form for each plan (the same arithmetic as the shadow score). With "session_end":true a session's last response sends no ping (the keeper does this when the client's SessionEnd hook is installed).
  • The tenant's score for plan k is (S_u[k] + kappa * g[k]) / (N_u + kappa): its own recency-weighted rows plus kappa pseudo-rows of the fleet mean g. kappa (0 ... +Inf, +Inf = the fleet plan) and the recency half-life (1 ... 30 days, or none) are chosen on the training rows only: fit on their first 70%, score on the rest, safest setting first, so a less shrunk one wins only if it is strictly better on rows it was not fitted on.
  • Held-out evidence: tuned on rows resolved before the tenant's 70% point, replayed on the tenant's last 30% against never, blanket K1/K2/K3 and the fleet plan, with session-bootstrap intervals. ship needs a positive net and a paired 95% interval above zero against BOTH blanket K3 and the fleet plan; a tenant whose plan is the fleet plan is therefore always baseline (nothing to gain over what it gets anyway). A non-ship build runs the fleet plan.
  • The shipped plan is the tuner refitted on the whole window (the evidence is for the procedure, not for one fit: recency is most of the value, below).
  • Refresh: a live (shadow or active) auto campaign is rebuilt in place every CG_CAMPAIGN_AUTO_REFRESH (default 24h, off disables) on the same window length ending now, from one shared read of the fleet's history per pass; the mode is kept, an ACTIVE campaign changes its plan only on a ship rebuild, a rebuild that finds too little history keeps the previous plan, and a new spec replaces the old one only after it parses.
  • An active campaign (either builder) sets the ping count; the spacing stays the tenant's configured keep-alive interval (240 s if none).

The tuner makes the same choices as a reference Python implementation (scripts/campaign/synth_fixtures.py) on the same rows (TestAutoTuneMatchesTheResearchImplementation, 20,000 synthetic rows of 19 scopes whose return habits drift over four weeks).

Run it

CG_CAMPAIGN_PYTHON=/path/to/venv/bin/python   # numpy, pandas, lightgbm, scikit-learn
CG_CAMPAIGN_DIR=/var/lib/.../campaigns          # default: "campaigns" beside the dashboard database
curl -X POST .../api/cache/campaigns/build -d '{"scope":"user","id":"<tenant>","window":"30d"}'
curl .../api/cache/campaigns/<id>
curl -X POST .../api/cache/campaigns/<id>/mode -d '{"mode":"shadow"}'

Auto: curl -X POST .../api/cache/campaigns/build -d '{"scope":"tenant","id":"<tenant>","window":"30d","builder":"auto","session_end":true}', then the same view and mode routes.

Tests: go test ./predictive ./proxy -run 'Parity|CacheCampaign|Auto' and python scripts/campaign/test_pipeline.py.

Documentation

Overview

Package predictive is the learned keep-alive policy: from the facts known when a response ends it estimates P(this conversation returns inside each ping window) and picks how many keep-alive pings are worth buying. It is a small MLP (~6K parameters, a few seeds averaged) that outputs a censored mixture-of-Weibulls survival curve of the time-to-return, plus the economic rule on top of it:

k* = argmax_k  lam*stake*(S(Tc) - S(k*x + Tc)) - pingCost * sum_{j<=k} S(j*x)

stake = prefix * (write5m - read) is what a rescued cache entry is worth. Training and the held-out comparison live in scripts/campaign; this package is inference only and costs microseconds.

Index

Constants

View Source
const (
	NGBS    = 72
	GBSBase = NIn
	NAll    = NIn + NGBS
)

NGBS is len(GBSNames); GBSBase is where they start in Row.F.

View Source
const (
	RuleModel = "model" // the predictor picks the number of pings per request
	RulePlan  = "plan"  // a fixed plan from the menu (uniform: Plan.GapsS[0] seconds apart, len(GapsS) pings)
	RuleNone  = "none"  // never ping
)

Rule kinds a segment may name.

View Source
const NCat = 12

NCat is len(CatNames).

View Source
const (
	NIn, H1, H2, NOut = 37, 48, 64, 10
)

Layer sizes of the exported network.

View Source
const SpecSchema = "cg.cache.campaign/2"

SpecSchema is the campaign document this package reads. v2 extends the v1 decision-tree campaign with a model reference: a campaign is an ordered list of segments, each naming the rule that decides for the requests it matches.

Variables

View Source
var CatNames = [NCat]string{"family", "agent_type", "provider", "stop_reason", "thinking", "m_stop_reason", "m_prev_stop_reason", "m_model", "m_agent", "m_cache_ttl", "m_thinking", "m_effort"}

CatNames are the categorical GBS inputs; Row.Cat is indexed by position in this list.

View Source
var FeatureNames = [NIn]string{
	"l_prefix", "l_out", "l_fresh", "cw_frac", "l_msgs", "l_tools", "l_turn", "l_sturn", "l_dur", "l_sage", "pg_first", "pg_pos", "pg_neg", "pg2",
	"hs", "hc", "ds", "dc", "kv_p5", "kv_p1", "kv_ln", "gq_nan", "l_gq", "think", "stream", "l_ping",
	"sr_tool_use", "sr_stop_sequence", "sr_end_turn", "sr_stop", "sr_tool_calls", "fam_opus", "fam_sonnet", "fam_haiku",
	"ag_claude-cli", "ag_openai", "ag_asyncopenai",
}

FeatureNames is the model's input vector, in order. Every one is a function of what the proxy has seen by the time a response ends: that response's own sizes and stop reason, the idle gaps this thread and tenant have ALREADY closed, and counters of its own traffic.

View Source
var GBSNames = [NGBS]string{
	"messages", "tools", "system_blocks", "stream", "session_turn", "turn_in_thread", "hour_utc", "dow", "prefix_tok", "output_tok", "read_cost", "rebuild_cost", "ping_cost", "cost_usd", "fresh_input",
	"stake", "ping_over_stake", "family", "agent_type", "provider", "stop_reason", "thinking",
	"m_n_req_thread", "m_n_req_session", "m_session_age_s", "m_thread_age_s", "m_ctx_tokens", "m_ctx_window", "m_ctx_fill", "m_cache_read_ratio", "m_cache_write_ratio", "m_fresh_ratio",
	"m_rewrite", "m_rewrites_last5", "m_reqs_since_rewrite", "m_ctx_growth", "m_ctx_growth_ratio", "m_gap1", "m_gap2", "m_gap3", "m_gap_mean5", "m_gap_median5", "m_gap_std5", "m_gap_max10",
	"m_gap_trend5", "m_gap_ewma", "m_is_user_turn", "m_since_user_turn_s", "m_tool_streak", "m_reqs_since_user_turn", "m_messages", "m_messages_delta", "m_reqs_since_compaction", "m_compactions",
	"m_stop_reason", "m_prev_stop_reason", "m_model", "m_agent", "m_cache_ttl", "m_tools", "m_system_blocks", "m_out_tokens", "m_out_ratio", "m_resp_s", "m_ttfb_s", "m_resp_mean5", "m_thinking",
	"m_effort", "m_max_tokens", "m_status_ok", "m_cost_req", "m_cost_session_cum",
}

GBSNames are the 72 inputs of the LightGBM discrete-time hazard, in training order (scripts/campaign/gbsfeat.py NAMES): 22 request columns and 50 thread / session features. Categorical ones are strings in Row.Cat and are coded by the model with its own table.

Functions

This section is empty.

Types

type Arrival

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

Arrival is the state a request carries from its start to its end: everything that depends on what came BEFORE it.

type AutoFit

type AutoFit struct {
	HalfLifeDays float64 // +Inf = no decay
	Kappa        float64 // +Inf = the fleet plan for everyone
	FleetPlan    int
	Plan         map[string]int     // per scope; a scope with no rows is not listed and gets FleetPlan
	Weight       map[string]float64 // recency-weighted rows behind each scope's choice
}

AutoFit is the tuner's answer.

func AutoTune

func AutoTune(rows []AutoRow, nowMs int64, grid AutoGrid) *AutoFit

AutoTune chooses (half-life, kappa) on an inner split of rows and refits on all of them. rows must be resolved by nowMs (the build's cut).

func (*AutoFit) PlanFor

func (f *AutoFit) PlanFor(scope string) int

PlanFor is the plan for one scope.

type AutoGrid

type AutoGrid struct {
	Kappas        []float64 // pseudo-rows of the fleet prior; +Inf = the fleet plan for everyone
	HalfLivesDays []float64 // recency half-life of a row's weight; +Inf = no decay
	InnerFrac     float64   // the inner split point of the training rows
}

AutoGrid is what the tuner may choose from. Zero values take the defaults below.

type AutoRow

type AutoRow struct {
	Scope   string
	Session string
	EndMs   int64
	Gain    []float64
}

AutoRow is one decision point: whose it is, when its response ended, and the net $ of each menu plan on it (Gain[0] = never ping = 0).

type Decision

type Decision struct {
	K       int     // pings to buy, 0 = let it lapse
	PWindow float64 // P(return inside the window the K pings cover), 0 when K == 0
	S295    float64 // P(still no return when the cache lifetime ends)
}

Decision is the model's answer for one decision point.

func Decide

func Decide(m Model, row *Row, e Econ) Decision

Decide applies the economic rule to one decision point: the number of pings k in 0..kmax that maximises

sum_{j<=k}  stake*(S(x(j-1)+Tc) - S(xj+Tc))  -  lam*pingCost*S(xj)

(the j-th ping is paid only if the conversation has not returned by xj and rescues a return in the window it opens). Ties keep fewer pings.

type Econ

type Econ struct {
	PrefixTok       float64
	PWrite5m, PRead float64 // $ per token
	PingCost        float64 // $ per ping
}

Econ is the money side of one decision: what the entry is worth and what a ping costs.

type Facts

type Facts struct {
	Family, StopReason, Model, Agent string
	PrefixTok, PingCost              float64
	SessionTurn                      float64
	// HourUTC (0-23) and DowUTC (0 = Sunday) are the request's start, UTC: per-time segments test them.
	HourUTC, DowUTC float64
}

Facts are the per-request values a segment predicate may test.

type LGB

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

LGB is a LightGBM discrete-time hazard model: the time to return is cut into bins at Edges; for every bin a gradient-boosted classifier gives the hazard h_b = P(return in bin b | not returned before it) from the 72 request inputs plus the bin and its right edge. S(edge_b) = prod_{i<=b} (1 - h_i). Trees are exported from LightGBM as flat node arrays; evaluation is the same arithmetic LightGBM's predict does (split on <=, NaN / zero routed by the stored default direction).

func (*LGB) Hazards

func (m *LGB) Hazards(row *Row, upTo float64, out []float64) []float64

Hazards returns the hazard of every bin whose right edge is <= upTo.

func (*LGB) Kind

func (m *LGB) Kind() string

Kind implements Model.

func (*LGB) Rule

func (m *LGB) Rule() Rule

Rule implements Model.

func (*LGB) Survival

func (m *LGB) Survival(row *Row, ts []float64, out []float64)

Survival implements Model. S at a time that is not a bin edge is interpolated linearly between the two edges around it.

type MLP

type MLP struct {
	Features []string
	// contains filtered or unexported fields
}

MLP is the Weibull-mixture network. Safe for concurrent use.

func (*MLP) Kind

func (m *MLP) Kind() string

Kind implements Model.

func (*MLP) NumNets

func (m *MLP) NumNets() int

NumNets is how many seeds are averaged.

func (*MLP) Rule

func (m *MLP) Rule() Rule

Rule implements Model.

func (*MLP) Standardize

func (m *MLP) Standardize(raw []float64, x *[NIn]float64)

Standardize writes (raw - mu) / sd into x.

func (*MLP) Survival

func (m *MLP) Survival(row *Row, ts []float64, out []float64)

Survival implements Model: the seed-averaged S(t) at each of ts. The input is the first NIn values of row.F (FeatureNames order).

type Model

type Model interface {
	Kind() string
	Rule() Rule
	// Survival writes S(t) for every t in ts into out (len(out) >= len(ts)).
	Survival(row *Row, ts []float64, out []float64)
}

Model is a pluggable time-to-return predictor: it turns one Row of live features into the survival curve S(t) = P(no return by t seconds). The economic rule (Decide) is model independent. To add a model: implement this, register its "kind" in ParseModel, and have it read the inputs it needs from Row (the research feature blocks are documented in features.go and gbsfeatures.go).

func LoadModel

func LoadModel(path string) (Model, error)

LoadModel reads a model file.

func ParseModel

func ParseModel(b []byte) (Model, error)

ParseModel loads a model document, dispatching on its "kind".

type ModelRef

type ModelRef struct {
	Ref      string `json:"ref"`
	SHA256   string `json:"sha256"`
	Schema   string `json:"schema"`
	Kind     string `json:"kind"`
	Features int    `json:"n_features"`
	Rule     Rule   `json:"rule"`
}

ModelRef points at the predictor file next to the spec.

type Plan

type Plan struct {
	GapsS    []float64 `json:"gaps_s"`
	MaxPings int       `json:"max_pings"`
	Tier     string    `json:"tier"`
}

Plan is a menu entry.

type Policy

type Policy struct {
	Kind     string    `json:"kind"` // "rules" (no model) or "model+segments"
	Model    *ModelRef `json:"model,omitempty"`
	Segments []Segment `json:"segments"`
	Fallback SegRule   `json:"fallback"`
}

Policy is the executable part of a campaign.

type Request

type Request struct {
	Tenant, Session, Thread, Model string
	TsMs                           int64
}

Request is what the proxy knows when a request STARTS.

type Response

type Response struct {
	Request
	EndMs, DecMs                      int64  // provider-side response end, and when the proxy finished returning it (>= EndMs)
	Provider, CacheTTL, Effort        string // request facts: serving provider, cache tier asked for ("ephemeral_5m", ...), reasoning effort
	MaxTokens, SystemBlocks           int
	TTFBMs, UpstreamMs, CostUSD       float64 // time to first byte, upstream duration, billed cost of this request
	Prefix, CacheWrite, Fresh, Output int64
	Messages, Tools                   int
	StopReason, Agent, Thinking       string // Agent: the dash agent family ("claude-cli", "openai", ...)
	Stream                            bool
	PIn, POut, PRead, PWrite5m        float64
}

Response is what it knows when that request's response has ENDED. Prices are $ per token. The request's own start time is the one the Arrival carries (Request.TsMs here is ignored).

func (*Response) PingCost

func (r *Response) PingCost() float64

PingCost is what one keep-alive ping on this prefix costs.

type Row

type Row struct {
	F         []float64
	Cat       [NCat]string
	Ctx       float64
	ModelName string
	// Skipped is true when, within the last 10 requests of the thread, an earlier request had not finished when this one's features were taken (parallel
	// requests on one thread): the response-dependent history inputs then come from the last finished request and can differ from the offline definition.
	Skipped bool
}

Row is every live input for one decision point. F[:NIn] is the MLP block; F[GBSBase:] the GBS block (categorical slots NaN, see Cat). Ctx and ModelName let a model derive the two inputs that depend on its own training table (served context window).

func NewRow

func NewRow() *Row

NewRow allocates a Row.

type Rule

type Rule struct {
	Lam  float64 `json:"lam"`
	XS   float64 `json:"x_s"`  // seconds between pings
	KMax int     `json:"kmax"` // most pings ever bought
	TCS  float64 `json:"tc_s"` // the provider's cache lifetime, seconds
}

Rule is the economic decision rule's constants, exported with the model.

type SegRule

type SegRule struct {
	Kind string `json:"kind"`
	Plan string `json:"plan,omitempty"`
	// Lam optionally rescales the model's value of a rescue for this segment (default 1).
	Lam float64 `json:"lam,omitempty"`
}

SegRule is the rule one segment applies.

type Segment

type Segment struct {
	ID   string         `json:"id"`
	When [][3]any       `json:"when"`
	Rule SegRule        `json:"rule"`
	Raw  map[string]any `json:"-"`
}

Segment applies Rule to the requests where every predicate in When holds. When is a list of [fact, op, value] triples.

type Spec

type Spec struct {
	Schema  string          `json:"schema"`
	Version string          `json:"version"`
	Policy  Policy          `json:"policy"`
	Menu    map[string]Plan `json:"menu"`
}

Spec is the campaign document. Only the fields a runtime needs are typed; the rest (evidence, caveats, ...) is kept verbatim in Extra for the API to return.

func ParseSpec

func ParseSpec(b []byte) (*Spec, error)

ParseSpec validates a campaign document.

func (*Spec) PlanOf

func (s *Spec) PlanOf(r SegRule) (k int, idleS float64)

PlanOf turns a fixed-plan rule into (count, seconds between pings).

func (*Spec) RuleFor

func (s *Spec) RuleFor(f *Facts) SegRule

RuleFor returns the first matching segment's rule, or the fallback.

type Tracker

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

Tracker accumulates, per campaign scope, the causal history the features need. All state is bounded: idle threads and sessions are dropped after six hours.

func NewTracker

func NewTracker() *Tracker

NewTracker returns an empty tracker.

func (*Tracker) Arrive

func (t *Tracker) Arrive(r Request) *Arrival

Arrive registers a request that is starting. Call it once per request, in start order; keep the result for Features.

func (*Tracker) Ended

func (t *Tracker) Ended(a *Arrival, r *Response)

Ended records that a response has finished, at its provider-side end: from now on a request that starts on the same thread closes this request's gap. A successor that already started (we were told late) closes it at once.

func (*Tracker) Features

func (t *Tracker) Features(a *Arrival, r *Response, row *Row)

Features computes every model input for a response that has ended into row (from NewRow). Call Ended first (or at the same moment).

func (*Tracker) Release

func (t *Tracker) Release(a *Arrival, r *Response)

Release is Ended for a request whose features are never taken (a campaign that only applies fixed plans): it drops the predecessor chain so the thread's history does not accumulate.

Jump to

Keyboard shortcuts

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