Expand description
Context Field Theory (CFT) – unified potential function for context items.
Combines information-theoretic, graph-based, and history signals into a single scalar potential Phi(i,t) per context item, enabling principled budget allocation and view selection.
Scientific basis: Phi(i,t) = w_RR + w_SS + w_GG + w_HH - w_CC - w_DD where R = task relevance (heat diffusion + PageRank), S = surprise (cross-entropy with Zipfian prior), G = graph proximity (weighted BFS distance), H = history signal (bandit feedback), C = token cost for the active view, D = redundancy with already-selected items (Jaccard).
Structs§
- Context
Field - The Context Field: computes Phi for a set of items given a task context.
- Context
Item Id - Stable, content-addressed identifier for a context item.
Derived from
kind + source_pathso the same file always maps to the same ID within a session, regardless of content changes. - Field
Potential - Combined potential for a context item.
- Field
Signals - Raw signal components for a single context item before combination.
- Field
Weights - Weights for the potential function components. Adapted via Thompson Sampling (bandit.rs) over time.
- Provenance
- Token
Budget - Token budget parameters for compilation.
- View
Costs - Token-cost estimates for each available view of a context item.
Enums§
Constants§
- MMR_
LAMBDA - Default MMR trade-off: how much relevance (Phi) is weighted against non-redundancy during integration-aware selection (#5). 0.7 keeps relevance dominant while still penalizing near-duplicates.
Functions§
- active_
weights - The current active (learned) FieldWeights, or the default when none have been
installed yet. Cheap — a single
RwLockread — so safe on the hot path. - compute_
signals_ for_ path - Compute real signals for a file path using existing scoring modules. Bridges CFT with the information-theoretic, graph-based, and history subsystems already in lean-ctx.
- efficiency
- Compute efficiency ratio: Phi per token. Used by the greedy knapsack in the compiler.
- mmr_
score - Maximal Marginal Relevance score (#5): reward relevance (
phi) but penalize redundancy with the already-selected set (max_similarity). This makes selection integration-aware in the IIT sense — a context package gains more from a complementary item than from a near-duplicate of one it already holds. Deterministic: a pure function of its inputs, no sampling. - normalize_
graph_ proximity - Normalize graph proximity (inverse of distance) to [0, 1]. Distance 0 = same file = 1.0, distance N = 1/(1+N).
- normalize_
relevance - Normalize a relevance score from task_relevance.rs to [0, 1].
- normalize_
surprise - Normalize a surprise score from surprise.rs to [0, 1]. Surprise range is typically 5.0 (common) to 17.0+ (rare).
- normalize_
token_ cost - Normalize token cost relative to budget.
- set_
active_ weights - Install the bandit-selected FieldWeights as the process-wide active weights (#4). Deterministic given the bandit posterior; called when an arm is chosen.