Skip to content

fix: keep compacted request views stable across requests - #41

Merged
Brian Krabach (bkrabach) merged 2 commits into
mainfrom
lane/sb-context-delivery-a7ec
Sep 15, 2026
Merged

Brian Krabach (bkrabach) merged 2 commits into
mainfrom
lane/sb-context-delivery-a7ec

Conversation

@bkrabach

Copy link
Copy Markdown
Collaborator

Summary

Fix repeated context reduction after a provider-directed rebuild. Previously, an ordinary request below the normal compaction threshold could return the full canonical history instead of applying the existing sticky reductions. A provider guard could then treat that restored content as new growth and request another reduction.

  • Apply sticky compaction decisions before measuring and returning every request view, including ordinary and inactive-retention fetches. Preserve the full canonical history.
  • Add an optional, module-owned hard_fit=False argument to context.request_retention. When enabled, target the effective forced budget without applying the ordinary target_usage fraction again. Ordinary explicit token_budget behavior remains unchanged.
  • Define the hard-fit event delivery boundary after final notice-inclusive budget validation: cancellation before delivery rolls back decisions; once event delivery begins, validated decisions remain committed. Event delivery does not imply provider dispatch.

Preserved behavior

Required human messages, retained persisted reminders, the protected tool-result floor, atomic tool pairing, safe tail-notice placement, and irreducible-content errors remain protected. This does not change Core, dependencies, CI, user configuration, provider implementation, or advertised model limits.

Validation

Recorded against this exact candidate in an isolated DTU with Core 1.6.1:

  • Context full suite: 148 passed, 1 existing xfailed.
  • Focused sticky-compaction and retention tests: 41 passed, including cancellation and notice-inclusive fit checks.
  • Ruff: passed for the changed context implementation and both changed Python test files.
  • Joint validation with an unpublished streaming-loop companion and the real OpenAI adapter: one provider-forced rebuild followed by four ordinary requests, with five fake-SDK dispatches preserving required content and not resurrecting discarded history. Canonical history remained intact.
  • The same baseline/candidate fixture demonstrated restoration of discarded history on all four baseline ordinary fetches and none on the candidate. Forced target behavior changed from half of the supplied effective budget to the intended full effective budget.
  • A narrow real-Anthropic-adapter control produced identical assembled request parameters before and after the changes, using an in-memory SDK.

These are deterministic offline mechanism tests, not live-vendor requests, model-quality or cache-performance measurements, or proof of a native context-window ceiling. The downstream loop companion will be published after this producer change is merged.

Changed files

  • amplifier_module_context_simple/__init__.py
  • tests/test_request_retention.py
  • tests/test_sticky_compaction_and_tail_notice.py
  • README.md

Generated with Amplifier

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
Generated with Amplifier

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
@bkrabach
Brian Krabach (bkrabach) merged commit 15ba711 into main Sep 15, 2026
4 checks passed
Brian Krabach (bkrabach) added a commit to microsoft/amplifier-module-loop-streaming that referenced this pull request Sep 15, 2026
## Summary

Use the context manager's optional hard-fit capability for provider-directed rebuilds, and make provider-budget decisions discoverable by logging hooks.

- Request `hard_fit=True` only when a provider preflight asks for a rebuild, in both ordinary generation and finalization paths.
- Detect keyword-compatible retention callables without catching and retrying implementation-raised `TypeError`. Old retention signatures and generic contexts keep their existing call shape.
- Declare `orchestrator:provider_budget` in the module's `observability.events` contribution.
- Add regressions for compatibility, event discovery, and repeated real-module requests after a forced rebuild.

The at-most-one rebuild rule, second preflight, final provider payload guard, required-content retention, tool pairing, and safe notice placement remain intact. No Core, provider implementation, dependency, CI, user configuration, or model-window metadata changes.

## Dependency

The context producer change is merged: microsoft/amplifier-module-context-simple#41

Its exact merge commit passed main-branch CI before this downstream publication. Older contexts remain supported through the legacy fallback, with the second preflight still enforcing the provider budget.

## Validation

Recorded against this exact candidate in an isolated DTU:

- Loop full suite with Core 1.6.1: **306 passed**.
- Focused budget, real-module runtime, and retention tests: **40 passed**, including the non-skipped `test_hard_fit_stays_compacted_across_real_openai_dispatches`.
- Real context manager, streaming loop, and OpenAI adapter with an in-memory SDK: one provider-forced rebuild followed by four ordinary requests. All five dispatched payloads retained the required human messages, reminder, and current tool result without resurrecting removed history. Canonical history remained intact; impossible protected input was rejected locally.
- Ruff passed for the implementation and all three changed Python test files.
- Separate Python 3.12 control matching the existing Core 1.5.2 / Foundation lock setup: **305 passed, 1 skipped**. That skip is the optional cross-module test because sibling modules are not installed in the per-repo control; it executed successfully in the joint DTU run above.
- An unchanged real-Anthropic-adapter control produced identical assembled request parameters in the baseline and candidate, using an in-memory SDK.

The regression fixture uses generated supplementary Han characters so its character estimate remains below ordinary compaction's threshold while its serialized UTF-8 size triggers the real provider preflight. It does not replace the budget calculation or weaken the test assertions.

These are deterministic offline mechanism tests, not real-vendor requests, task-quality or cache-performance measurements, proof of a native context ceiling, or a claim that the whole Python CI matrix ran in the DTU.

## Changed files

- `amplifier_module_loop_streaming/__init__.py`
- `tests/test_provider_budget_guard.py`
- `tests/test_provider_budget_context_simple_runtime.py`
- `tests/test_request_retention.py`
- `README.md`

Generated with Amplifier

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants