cupel — migrated from GitHub
  • HTML 47.8%
  • C# 29.6%
  • Rust 18%
  • JavaScript 2.8%
  • CSS 1.7%
Find a file
Alexander Wollan 0b82dad6ac
fix: replace removed doc_auto_cfg feature with doc_cfg
doc_auto_cfg was merged into doc_cfg and removed in Rust 1.92.0.
See https://github.com/rust-lang/rust/pull/138907
2026-03-30 09:18:59 -05:00
.bg-shell [kata/root/M003/S05] OTel bridge companion package (#82) 2026-03-23 15:20:01 -05:00
.cargo chore: migrate planning artifacts to Linear, clean up local state 2026-03-28 10:28:05 -05:00
.claude [kata/root/M003/S05] OTel bridge companion package (#82) 2026-03-23 15:20:01 -05:00
.github/workflows chore(update release-rust.yml actions) 2026-03-29 19:46:31 -05:00
.husky chore: add husky pre-commit and pre-push hooks 2026-03-24 15:15:29 -05:00
.kata chore: migrate planning artifacts to Linear, clean up local state 2026-03-28 10:28:05 -05:00
.rtk [kata/root/M003/S05] OTel bridge companion package (#82) 2026-03-23 15:20:01 -05:00
benchmarks/Wollax.Cupel.Benchmarks feat(06-05): add slicer benchmarks for all implementations 2026-03-13 16:39:09 -05:00
conformance feat(S02): Adopt TOML 1.1 syntax in conformance vectors & document requirement (#85) 2026-03-29 17:05:28 -05:00
crates fix: replace removed doc_auto_cfg feature with doc_cfg 2026-03-30 09:18:59 -05:00
packages [kata/root/M003/S05] OTel bridge companion package (#82) 2026-03-23 15:20:01 -05:00
spec feat(S02): Adopt TOML 1.1 syntax in conformance vectors & document requirement (#85) 2026-03-29 17:05:28 -05:00
src feat(M009/S04): MetadataKeyScorer — Rust + .NET implementation 2026-03-25 08:01:36 -05:00
tests feat(M009/S04): MetadataKeyScorer — Rust + .NET implementation 2026-03-25 08:01:36 -05:00
.editorconfig chore(16-01): create rust-toolchain.toml and extend .editorconfig 2026-03-14 20:17:21 -05:00
.gitignore [kata/root/M001/S01] feat(S01): Bump toml 0.8 → 1.1 and fix load_vector() parsing (#84) 2026-03-29 15:24:47 -05:00
Cargo.toml chore: migrate planning artifacts to Linear, clean up local state 2026-03-28 10:28:05 -05:00
CHANGELOG.md chore: bump version to 1.2.0 2026-03-29 11:49:38 -05:00
CLAUDE.md [kata/root/M003/S05] OTel bridge companion package (#82) 2026-03-23 15:20:01 -05:00
Cupel.slnx "feat(M003/S05): OTel bridge companion package" 2026-03-23 18:22:33 -05:00
Directory.Build.props chore: disable SourceLink for non-CI builds 2026-03-29 19:51:26 -05:00
Directory.Packages.props "feat(M003/S05): OTel bridge companion package" 2026-03-23 18:22:33 -05:00
global.json feat(01-01): create test and benchmark projects, wire solution 2026-03-11 04:24:11 -05:00
justfile chore: CI audit — workspace-level Rust commands, remove stale .githooks 2026-03-28 10:30:46 -05:00
LICENSE chore(16): add LICENSE and crate-level README 2026-03-14 20:40:40 -05:00
package.json chore: add husky pre-commit and pre-push hooks 2026-03-24 15:15:29 -05:00
README.md chore: bump version to 1.2.0 2026-03-29 11:49:38 -05:00
rust-toolchain.toml chore(16-01): create rust-toolchain.toml and extend .editorconfig 2026-03-14 20:17:21 -05:00

Cupel

Context management library for coding agents. Given a set of context items and a token budget, Cupel determines the optimal context window — maximizing information density while respecting LLM attention mechanics.

Available in .NET and Rust.

Install

.NET:

dotnet add package Wollax.Cupel

Optional companions:

dotnet add package Wollax.Cupel.Extensions.DependencyInjection
dotnet add package Wollax.Cupel.Tiktoken
dotnet add package Wollax.Cupel.Json

Rust:

[dependencies]
cupel = "1.2"

# Optional: serialization support
cupel = { version = "1.2", features = ["serde"] }

Quick Start

using Wollax.Cupel;
using Wollax.Cupel.Scoring;

var budget = new ContextBudget(maxTokens: 4000, targetTokens: 3000);

var pipeline = CupelPipeline.CreateBuilder()
    .WithBudget(budget)
    .AddScorer(new RecencyScorer(), weight: 0.6)
    .AddScorer(new PriorityScorer(), weight: 0.4)
    .UseGreedySlice()
    .Build();

var items = new[]
{
    new ContextItem { Content = "System prompt", Tokens = 100, Kind = ContextKind.SystemPrompt },
    new ContextItem { Content = "User message", Tokens = 50, Kind = ContextKind.Message },
    new ContextItem { Content = "Tool output", Tokens = 2000, Kind = ContextKind.ToolOutput },
};

ContextResult result = pipeline.Execute(items);
// result.Items — optimally selected and placed context
// result.TotalTokens — total tokens used
// result.Report — why each item was included/excluded

Named Policies

Skip manual configuration with opinionated presets:

var pipeline = CupelPipeline.CreateBuilder()
    .WithBudget(budget)
    .WithPolicy(CupelPresets.Chat())
    .Build();

Available presets: Chat, CodeReview, Rag, DocumentQa, ToolUse, LongRunning, Debugging

Pipeline

Cupel executes a fixed 5-stage pipeline:

Classify → Score → Deduplicate → Slice → Place
  • Classify — categorize items by kind
  • Score — rank items using composable scorers (0.01.0)
  • Deduplicate — remove byte-exact duplicates
  • Slice — select items within budget (greedy, knapsack, quota, or stream)
  • Place — order items for optimal attention (U-shaped or chronological)

Pinned items bypass scoring and are always included.

Scorers

Scorer Signal
RecencyScorer Temporal proximity (relative rank)
PriorityScorer Explicit priority value
KindScorer Content kind weights
TagScorer Tag-based categorical boosting
FrequencyScorer Reference frequency
ReflexiveScorer Caller-supplied FutureRelevanceHint
CompositeScorer Weighted combination of any scorers
ScaledScorer Normalizes any scorer to 01 range

Slicers

Slicer Strategy
GreedySlice O(N log N) fill by score/token ratio
KnapsackSlice 0-1 DP for optimal budget utilization
QuotaSlice Semantic quotas: Require(Kind, min%) / Cap(Kind, max%)
StreamSlice Online selection for IAsyncEnumerable sources

Explainability

Every inclusion and exclusion has a traceable reason:

ContextResult result = pipeline.DryRun(items);
SelectionReport report = result.Report!;

foreach (var included in report.Included)
    Console.WriteLine($"+ {included.Item.Content} (score: {included.Score:F2}, reason: {included.Reason})");

foreach (var excluded in report.Excluded)
    Console.WriteLine($"- {excluded.Item.Content} (score: {excluded.Score:F2}, reason: {excluded.Reason})");

Packages

Package Purpose
Wollax.Cupel Core library (zero dependencies)
Wollax.Cupel.Extensions.DependencyInjection Microsoft.Extensions.DI integration
Wollax.Cupel.Tiktoken Token counting via Microsoft.ML.Tokenizers
Wollax.Cupel.Json JSON policy serialization

Rust Crate

The cupel crate implements the full conformance specification in Rust. See crates/cupel/ for source and docs.rs for API documentation.

Features:

  • Full conformance with all 28 required test vectors
  • Optional serde feature for Serialize/Deserialize on all public types
  • Validation-on-deserialize for ContextBudget (constructor invariants enforced)
  • 3 runnable examples: basic_pipeline, serde_roundtrip, quota_slicing

Specification

Cupel's algorithm is documented as a language-agnostic specification with 28 conformance test vectors. Both the .NET and Rust implementations pass all conformance vectors.

License

MIT