-
Notifications
You must be signed in to change notification settings - Fork 656
feat: add bash context compaction #2448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cf0ad84
add bash context compaction
mikasenghaas b60b651
recover from context overflow
mikasenghaas c1a13ee
simplify compaction flow
mikasenghaas 4bb99de
propagate context limit errors
mikasenghaas 1472123
move compaction config into each harness
mikasenghaas 43a0b6d
relay overlong prompt errors untouched
mikasenghaas c5b1b2e
discover compaction thresholds harness-side
mikasenghaas ff011aa
remove unused overlong prompt error
mikasenghaas 70e8723
serve models through interception
mikasenghaas 5b48a26
drop ranged compaction thresholds
mikasenghaas f63d13c
relay the models listing statelessly
mikasenghaas dffe17d
resample checkpoint summaries
mikasenghaas db96ac1
forbid tool calls in the checkpoint prompt
mikasenghaas daec369
simplify checkpoint to one attempt
mikasenghaas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import httpx | ||
| from openai import BadRequestError | ||
|
|
||
| from verifiers.v1.clients.context import ( | ||
| compaction_threshold, | ||
| model_context_window, | ||
| ) | ||
| from verifiers.v1.configs.harness import CompactionConfig | ||
| from verifiers.v1.harnesses.bash.harness import BashHarnessConfig | ||
| from verifiers.v1.harnesses.bash.program import context_error | ||
| from verifiers.v1.harnesses.rlm.harness import RLMHarnessConfig | ||
|
|
||
|
|
||
| def test_model_context_window_reads_vllm_extension() -> None: | ||
| payload = { | ||
| "data": [ | ||
| {"id": "other", "max_model_len": 1}, | ||
| {"id": "target", "max_model_len": 32_768}, | ||
| ] | ||
| } | ||
|
|
||
| assert model_context_window(payload, "target") == 32_768 | ||
|
|
||
|
|
||
| def test_model_context_window_accepts_common_provider_extensions() -> None: | ||
| payload = {"data": [{"id": "target", "context_length": 128_000}]} | ||
|
|
||
| assert model_context_window(payload, "target") == 128_000 | ||
|
|
||
|
|
||
| def test_model_context_window_is_unknown_for_standard_model_card() -> None: | ||
| payload = { | ||
| "data": [ | ||
| { | ||
| "id": "target", | ||
| "object": "model", | ||
| "created": 1, | ||
| "owned_by": "provider", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| assert model_context_window(payload, "target") is None | ||
|
|
||
|
|
||
| def test_compaction_threshold_reserves_ten_percent() -> None: | ||
| assert compaction_threshold(32_768) == 29_491 | ||
|
|
||
|
|
||
| def test_compaction_is_disabled_by_default_for_both_harnesses() -> None: | ||
| assert BashHarnessConfig().compaction is None | ||
| assert RLMHarnessConfig().compaction is None | ||
|
|
||
|
|
||
| def test_compaction_config_has_shared_automatic_default() -> None: | ||
| assert CompactionConfig().summarize_at_tokens is None | ||
|
|
||
|
|
||
| def test_threshold_is_learned_from_provider_error() -> None: | ||
| response = httpx.Response( | ||
| 400, | ||
| request=httpx.Request("POST", "http://provider/v1/chat/completions"), | ||
| ) | ||
| error = BadRequestError( | ||
| "maximum context length is 32,768 tokens", | ||
| response=response, | ||
| body={"error": {"message": "maximum context length is 32,768 tokens"}}, | ||
| ) | ||
|
|
||
| assert context_error(error) == (True, 29_491) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Model context-window discovery for OpenAI-compatible endpoints.""" | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any, cast | ||
|
|
||
| from openai import APIError | ||
|
|
||
| from verifiers.v1.clients.base import build_async_openai | ||
| from verifiers.v1.clients.client import ModelContext | ||
|
|
||
| CONTEXT_WINDOW_FIELDS = ( | ||
| "max_model_len", | ||
| "context_length", | ||
| "context_window", | ||
| "max_context_length", | ||
| ) | ||
| _context_window_cache: dict[tuple[str, str], int | None] = {} | ||
|
|
||
|
|
||
| def model_context_window(payload: Mapping[str, Any], model: str) -> int | None: | ||
| """Read a provider context-window extension from one model card.""" | ||
| for card in payload.get("data") or []: | ||
| if not isinstance(card, Mapping) or card.get("id") != model: | ||
| continue | ||
| for field in CONTEXT_WINDOW_FIELDS: | ||
| value = card.get(field) | ||
| if isinstance(value, int) and not isinstance(value, bool) and value > 0: | ||
| return value | ||
| break | ||
| return None | ||
|
|
||
|
|
||
| def compaction_threshold(context_window: int) -> int: | ||
| """Reserve ten percent of the model context for checkpointing.""" | ||
| return max(1, context_window * 9 // 10) | ||
|
|
||
|
|
||
| async def resolve_compaction_threshold(ctx: ModelContext) -> int | None: | ||
| """Discover a model's proactive compaction threshold when advertised.""" | ||
| key = (ctx.client.model_dump_json(), ctx.model) | ||
| if key not in _context_window_cache: | ||
| try: | ||
| async with build_async_openai(ctx.client) as client: | ||
| payload = await client.get("/models", cast_to=cast(Any, dict[str, Any])) | ||
| _context_window_cache[key] = model_context_window(payload, ctx.model) | ||
| except APIError: | ||
| _context_window_cache[key] = None | ||
|
|
||
| window = _context_window_cache[key] | ||
| return compaction_threshold(window) if window is not None else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,39 @@ | |
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import random | ||
| from pathlib import Path | ||
|
|
||
| from pydantic import ConfigDict, Field, FiniteFloat | ||
| from pydantic import ConfigDict, Field, FiniteFloat, PositiveInt, model_validator | ||
| from pydantic_config import BaseConfig | ||
|
|
||
| from verifiers.v1.types import ID | ||
|
|
||
|
|
||
| class CompactionConfig(BaseConfig): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicate this in bash and rlm, not in top-level config |
||
| """Optional context compaction policy for in-house agent loops.""" | ||
|
|
||
| summarize_at_tokens: PositiveInt | tuple[PositiveInt, PositiveInt] | None = None | ||
| """Compact at this token count. A pair draws a task-seeded threshold. When unset, use | ||
| 90% of the model context window when the provider advertises it.""" | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_range(self) -> CompactionConfig: | ||
| value = self.summarize_at_tokens | ||
| if isinstance(value, tuple) and value[0] > value[1]: | ||
| raise ValueError( | ||
| "`summarize_at_tokens` range must be (lo, hi) with lo <= hi." | ||
| ) | ||
| return self | ||
|
|
||
| def summarize_threshold(self, task_idx: int | None) -> int | None: | ||
| value = self.summarize_at_tokens | ||
| if isinstance(value, tuple): | ||
| lo, hi = value | ||
| return random.Random(task_idx or 0).randint(lo, hi) | ||
| return value | ||
|
|
||
|
|
||
| class HarnessConfig(BaseConfig): | ||
| id: ID = "bash" | ||
| """Installed harness package, set through the seat's | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf is this?