MP-21 · Context Budgeter
MP-21 turns Context manifests and budget allocation into a production-shaped Forge milestone. You will build a selection stage that can answer not only “what did the model see?” but also “what could it have seen, what was dropped, why, and under which policy?”
This is not a prompt-concatenation exercise. The artifact is a strict manifest with policy, candidate-set, stable-prefix, and selection identities. Required overflow stops before inference. Optional pressure produces typed decisions.
Acceptance contract
Section titled “Acceptance contract”Given the pinned policy and seven candidates, your implementation must:
- derive 640 input tokens from a 1,024 window and three independent reserves;
- bind every count to exact content and a counter release;
- preserve the required instruction and latest user turn or fail;
- enforce all six lane maxima;
- select optional items using deterministic priority/density ties;
- preserve stable-prefix/variable-body order;
- select exactly
instruction,demo,latest-user,crash-log, andmemory; - use 630 tokens, leave 10, and record a 260-token stable prefix;
- drop
screenshotfor evidence-lane pressure; - drop
old-turnfor total pressure; - reproduce the manifest from exact inputs;
- detect all nine supplied mutations.
Run it
Section titled “Run it”cd rust/rust-ai-engineeringcargo test -p mosaic-harness context_budgetcargo run -q -p mosaic-harness --example context_budgeter_labThe example writes:
target/labs/mp-21/95079edc3cc835f5496e4b386ce02f3f7a319cae2914004931fa70752a0a88c5.jsonPinned evidence:
policy SHA-256 95079edc3cc835f5496e4b386ce02f3f7a319cae2914004931fa70752a0a88c5candidate-set SHA-256 f8a9903857d9402e9cb756c30e7eae247319c904414fd5c648f1eec00d7184f1manifest SHA-256 0cb9127ebd297b75d93ee2da217cf6f9b014d4708389d4919fb3e3bff6c300b8artifact bytes 4,821artifact SHA-256 7a00e2210a1803c753ea7e552b51d4b6acf6aa0f85152ed83d368a7f57c4a4d2Predict
Section titled “Predict”Do not run the allocator yet. Read:
labs/harness/mp-21-context-budgeter/fixtures/context-policy.jsonlabs/harness/mp-21-context-budgeter/fixtures/candidates.jsonOn paper:
- calculate input capacity;
- admit required candidates;
- order optional candidates by policy;
- track total and lane usage after each decision;
- predict render order and stable prefix;
- classify every dropped item.
Your written trace is part of the learner evidence. A correct final list without intermediate budget state is insufficient.
Run the example, then follow crash-log through:
candidate content SHA→ counted-content SHA→ token counter release→ optional priority ordering→ evidence lane usage→ selected decision→ render position→ candidate-set and manifest identityRepeat for screenshot. Identify the exact inequality that fails. Repeat for old-turn and explain
why it reaches a different branch.
Implement
Section titled “Implement”Start from the strict types. Implement in this order:
- policy validation and checked reserve arithmetic;
- exact six-lane validation;
- candidate identity and token-receipt validation;
- duplicate ID/position and placement checks;
- required-first admission;
- deterministic optional comparator;
- total/lane/item drop reasons;
- stable render order;
- usage, prefix, and manifest identities;
- reproduction verifier.
Run the focused suite after each stage. Do not add provider calls to make the lab “more real”; the selection boundary should remain deterministic.
The mutation fixture covers:
- required total overflow;
- required lane overflow;
- weak count for a required candidate;
- token-count/content drift;
- duplicate candidate identity;
- illegal stable-prefix hoisting;
- ordinary lane pressure;
- stored manifest mutation.
For three cases, temporarily remove the responsible check. Demonstrate that the mutation test fails for the expected reason, then restore the implementation.
Add one mutation of your own. Strong choices include:
- duplicate lane limit;
- arithmetic boundary;
- optional item limit;
- output reserve reduced to zero;
- stable-prefix content changed without re-counting.
Measure
Section titled “Measure”Create deterministic candidate sets of 8, 32, 64, and 128 items. Record:
- allocation median and p95;
- allocations and peak memory;
- selected/drop counts by reason;
- deterministic manifest equality across 100 runs.
Then implement an exact knapsack oracle for at most 20 optional candidates. Compare:
regret = exact selected utility - greedy selected utilityDo not replace the production allocator yet. First measure whether regret changes downstream held-out behavior enough to justify a more complex bounded solver.
Diagnose the deliberate failure
Section titled “Diagnose the deliberate failure”Scenario: the manifest reports 630/640 input tokens, but the provider rejects the compiled request as too large.
Investigate:
- Was each count produced for exact candidate content?
- Did the tokenizer release match the selected model route?
- Were special tokens and message framing counted?
- Were tool schemas and output schema included?
- Did multimodal blocks use provider-native accounting?
- Was the fully compiled provider request counted?
- What was predicted-versus-observed delta for this route?
The repair is a new counter/adapter/reserve policy and a new manifest. Provider auto-truncation is not an acceptable hidden repair.
Improve
Section titled “Improve”Choose one extension:
- final-wire count with a monotonic removal-only repair pass;
- per-item maximum plus total/lane maxima;
- soft lane targets and bounded shared pool;
- duplicate/overlap-aware evidence selection;
- exact small-state solver with time/state ceiling;
- output reserve chosen by task/output contract;
- provider cache receipt correlated with stable-prefix identity.
Version the allocator or policy when behavior changes. Add before/after fixtures and explain which manifest fields change.
Defend
Section titled “Defend”Your design note must answer:
- Why can required context never become an ordinary drop?
- Who is authorized to assign lane, trust, priority, and required status?
- Why is provider file size not a universal token cost?
- Why does stable-prefix intent not prove a provider cache hit?
- When is a greedy allocator preferable to an exact optimizer?
- How will you detect lane starvation in production without logging content?
- What is strict replay if token-count service results change later?
Submission evidence
Section titled “Submission evidence”Submit:
- focused and full gate receipts;
- the exact lab artifact and digest;
- your prediction trace;
- one new mutation;
- the 8/32/64/128 benchmark table;
- greedy-versus-exact regret cases;
- the deliberate-failure diagnosis;
- the design defense.
Completion rubric
Section titled “Completion rubric”A strong solution:
- preserves all required candidates or returns a typed error;
- never silently truncates;
- binds count to content and counter release;
- enforces total and lane constraints independently;
- keeps selection and render order deterministic;
- records every candidate decision;
- reproduces stored manifests;
- measures both runtime cost and selection quality;
- treats trust and authorization separately from retrieval score.
A weak solution returns only selected strings, uses character count as tokens without versioning, drops required items, hides rejected candidates, or claims greedy selection is optimal.
Next dependency
Section titled “Next dependency”MP-21 feeds retrieval, memory, tools, and replay. Next, the retrieval chapters will produce better evidence candidates; MP-22 will verify memory writes before those records can compete for this manifest’s memory lane.