reqfacts

package
v0.7.16 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: 4 Imported by: ? 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 reqfacts reads the facts a request body states: the metadata the client chose (Meta, MetaFromBody) and the tool results the request answers (Lifecycle, ScanLifecycle). One rule, one place, so every host reads a request the same way. It reads wire format only, keeps counts and short enums, and holds no transport and no stored customer data. It does not import dash: dash.Meta and dash.LifecycleReq are aliases of these types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lifecycle

type Lifecycle struct {
	ResultCount, ResultBytes int
	ResultError              bool
}

Lifecycle is what a request body says about the wait it ended: the tool results it answers.

func ScanLifecycle

func ScanLifecycle(norm func() []bschemas.ChatMessage, body []byte) (q Lifecycle)

ScanLifecycle reads the tool_results a request answers, from the same normalized conversation the pipeline and waitcause use: every role=tool message after the last assistant turn. Count and bytes come from there; is_error is the one thing the normalizer drops, so it is read straight off the last Anthropic user message. Runs on the dashboard writer goroutine, never the request path. norm yields the normalized conversation (shared with waitcause so the body is normalized once per request).

type Meta

type Meta struct {
	// ReasoningEffort is Anthropic's `output_config.effort` or OpenAI's
	// `reasoning_effort` — low|medium|high|xhigh|max, "" when unset. NOT a synonym for
	// thinking: on current Anthropic models effort is the depth control and
	// thinking.budget_tokens is the removed predecessor, so both are recorded.
	ReasoningEffort string `json:"reasoning_effort"`
	// ThinkingMode is Anthropic `thinking.type` — adaptive|enabled|disabled, "" absent.
	ThinkingMode string `json:"thinking_mode"`
	// ThinkingBudget is `thinking.budget_tokens` (pre-4.6 models only; 0 = unset).
	ThinkingBudget int `json:"thinking_budget"`
	// Temperature and TopP are POINTERS, and the column is NULLABLE, because "the client
	// did not set it" and "the client set it to 0" are different facts and 0 is a
	// legitimate value for both. A sentinel would make a deterministic request
	// indistinguishable from an unspecified one.
	Temperature *float64 `json:"temperature"`
	TopP        *float64 `json:"top_p"`
	// MaxTokens is the client's output cap (`max_tokens`, or OpenAI's
	// `max_completion_tokens`); 0 = unset.
	MaxTokens int  `json:"max_tokens"`
	Stream    bool `json:"stream"`
	// ToolChoice is the normalized forcing mode: auto|any|none|required|tool for the
	// object form, or the bare string OpenAI sends. The forced tool's NAME is
	// deliberately not stored — it is unbounded client text with no aggregate use.
	ToolChoice string `json:"tool_choice"`
	// Tools is how many tools the request declared; SystemBlocks how many blocks the
	// top-level `system` array carried (1 for a bare string).
	Tools        int `json:"tools"`
	SystemBlocks int `json:"system_blocks"`
	// The prompt-cache breakpoints ON ARRIVAL, by location: `tools` and `system` render
	// ahead of `messages`, so location decides how much prefix a breakpoint protects.
	// Zero on a request the pipeline never inspected (observe mode), like every other
	// pipeline-derived field on such a row.
	CacheBPSystem   int `json:"cache_bp_system"`
	CacheBPTools    int `json:"cache_bp_tools"`
	CacheBPMessages int `json:"cache_bp_messages"`
	CacheBPBlocks   int `json:"cache_bp_blocks"`
	CacheBPRoot     int `json:"cache_bp_root"` // the top-level cache_control (automatic caching)
	// StopReason is the provider's own terminal reason, normalized across dialects:
	// Anthropic `stop_reason` (end_turn|max_tokens|tool_use|stop_sequence|pause_turn|
	// refusal|model_context_window_exceeded) or OpenAI `choices.0.finish_reason`
	// (stop|length|tool_calls|content_filter). "" when the response carried none.
	// StopTimeoutEstimated is the one value the proxy writes itself (#754).
	StopReason string `json:"stop_reason"`
}

Meta is the request's own metadata — the knobs the CLIENT chose, plus the stop reason the provider answered with. Captured because they are the levers that explain a cost: reasoning effort and thinking budget buy output tokens, and where the cache_control breakpoints sit decides how much of the prefix was billed as a read rather than a write, which is the whole subject of this project.

Real columns rather than a JSON blob, deliberately. Every field here is either GROUPED BY in an aggregate (effort, thinking mode, stop reason, tool_choice, the breakpoint counts) or a scalar rendered on the request row; a JSON column would put json_extract() in the middle of every one of those queries and give the free-text tail somewhere to hide from the redactor. There is no long tail to justify one.

Anthropic and OpenAI spell the same knobs differently; capture normalizes them (see MetaFromBody) so one column means one thing across dialects.

func MetaFromBody

func MetaFromBody(body []byte) Meta

The dialect map MetaFromBody normalizes. Two dialects onto one set of columns, because a column that means `reasoning_effort` for one client and nothing for another cannot be aggregated:

Anthropic  max_tokens, temperature, top_p, stream, tools[], tool_choice{type},
           system[] (blocks, or a bare string), thinking{type,budget_tokens},
           output_config{effort}
OpenAI     max_tokens / max_completion_tokens, temperature, top_p, stream, tools[],
           tool_choice (a bare string, or {function:{name}}), reasoning_effort,
           and no top-level system at all — it is a role=system message

MetaFromBody reads the request's metadata off the raw body in ONE structural pass.

One pass, and it is measured rather than assumed. This runs on the request goroutine, so the shape of the cost matters more than its absolute size:

one ForEach over the top-level object   0.55 ms/MB, 1 allocation
one gjson path query per field (12)     2.7  ms/MB, ~5 allocations

The per-field version re-scans past the whole `messages` array once per field, which is 5x the work for a body of any interesting size — so the single pass wins, even though it pays for one body-length copy (`gjson.ParseBytes` is `Parse(string(json))`). For scale: the pipeline on this same path already runs seven whole-body path queries to count breakpoints, and re-serializes the body outright when a component rewrites a message. ponytail: costs one body-sized short-lived copy; swap for a zero-copy iterator if this ever appears in a profile.

The ForEach never parses `messages` — each value is skipped structurally by byte — so the cost is linear in the body and independent of the transcript's depth or message count.

Breakpoint counts are NOT read here: the pipeline already counts them to respect the provider's cap of four, and they reach the event on the trace for free (see FromTrace).

func (Meta) CacheBreakpoints

func (m Meta) CacheBreakpoints() int

CacheBreakpoints is the total the provider's cap of four applies to.

Jump to

Keyboard shortcuts

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