Skip to content

feat(core): add a budget-aware context compiler #383

Description

@vamgan

Summary

Add a deterministic, budget-aware context compiler that selects the highest-value context before serialization instead of resolving everything and truncating the final string.

Problem

Current token limiting uses a lightweight character estimate and slices final output. It can cut through sections, omit high-value context after lower-value content, duplicate the same element across focus/viewport/history, and spend time resolving sources that cannot fit.

Proposed API

const compiled = await ctx.compileContext({
  budget: { maxTokens: 800, count: modelTokenCounter },
  priority: ['explicit-selection', 'focus', 'selected-sources', 'viewport', 'history'],
  dedupe: 'askable-id',
  sources: 'all',
});

compiled.text;
compiled.packet;
compiled.report.included;
compiled.report.omitted;
compiled.report.estimatedTokens;

Acceptance criteria

  • Never exceeds the supplied token counter's budget
  • Trims whole items/sections rather than slicing arbitrary output
  • Explicit selection and current focus outrank ambient history
  • Sources that cannot receive budget are not unnecessarily resolved
  • Duplicate context across focus, viewport, history, and sources is removed
  • Inclusion/omission report is deterministic and inspector-friendly
  • Existing serializers remain compatibility wrappers
  • Performance benchmarks cover many sources and long histories

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreCore package and cross-package runtime behaviorenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions