Skip to content

fix: cap OpenRouter context history#3339

Open
desmond-rai wants to merge 1 commit into
thedotmack:mainfrom
desmond-rai:agent/cap-openrouter-context
Open

fix: cap OpenRouter context history#3339
desmond-rai wants to merge 1 commit into
thedotmack:mainfrom
desmond-rai:agent/cap-openrouter-context

Conversation

@desmond-rai

Copy link
Copy Markdown

What changed

  • Add CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES with a default of 20 and a safe 2-200 range.
  • Keep the initial observer prompt plus the newest messages when an OpenRouter session exceeds the cap.
  • Preserve the full in-memory session history while bounding only the outbound API request.
  • Ship the corresponding worker bundle and regression coverage.

Why

OpenRouter observation requests reused the complete multi-turn history. Long Claude Mem sessions therefore resent an ever-growing context for every tool event, causing extreme token usage even when the configured context-message cap was present in settings.json.

Impact

OpenRouter requests remain bounded by default while retaining the observer instructions and latest context. Existing users can tune the cap through settings without changing providers or models.

Validation

  • bun test tests/openrouter_provider.test.ts tests/shared/settings-defaults-manager.test.ts - 37 passed, 0 failed
  • bun run typecheck:root
  • node --check plugin/scripts/worker-service.cjs
  • Local worker probe: 43 post-fix calls, maximum 20 messages per request, no runtime errors

@greptile-apps

greptile-apps Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a configurable cap for OpenRouter request history. The main changes are:

  • Adds CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES with a default of 20.
  • Clamps the OpenRouter context cap to the 2-200 range.
  • Sends only the first observer prompt and newest messages to OpenRouter when history exceeds the cap.
  • Keeps the full in-memory session history unchanged.
  • Updates the bundled worker and adds tests for settings and capped request behavior.

Confidence Score: 5/5

Safe to merge with minimal risk.

The change is localized to OpenRouter request construction and settings defaults. Invalid cap values are clamped, the existing in-memory session history behavior is preserved, and tests cover the new settings and capped request behavior.

No files require special attention.

T-Rex T-Rex Logs

What T-Rex did

  • Ran the OpenRouter regression suite and confirmed 37 tests passed with 0 failures.
  • Verified type checking completed successfully, with the typecheck log exiting code 0.
  • Executed the worker syntax checks and confirmed they finished with exit code 0.
  • Examined the CAP probe output and noted metrics including maxObservedOutboundMessages of 20, totalInMemoryHistoryLength of 44, firstMessagePreservedEveryRequest true, newest final message observation-43, and an empty runtimeErrors field.
  • Collected and inspected the artifact set for contract validation, including the regression tests log, typecheck log, worker syntax log, and CAP probe output.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
plugin/scripts/worker-service.cjs Bundled worker output updated to include the new OpenRouter max-context default.
src/services/worker/OpenRouterProvider.ts Adds bounded outbound OpenRouter history with default parsing and 2-200 clamping while preserving full in-memory session history.
src/shared/SettingsDefaultsManager.ts Adds CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES to typed settings defaults.
tests/openrouter_provider.test.ts Adds tests for preserving the first prompt and newest messages in capped OpenRouter requests.
tests/shared/settings-defaults-manager.test.ts Adds settings coverage confirming the OpenRouter context cap is loaded from file settings.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant Worker as Worker session
participant Provider as OpenRouterProvider
participant Settings as SettingsDefaultsManager
participant API as OpenRouter API

Worker->>Provider: query(conversationHistory)
Provider->>Settings: loadFromFile(USER_SETTINGS_PATH)
Settings-->>Provider: max context setting/default
Provider->>Provider: clamp setting to 2..200
Provider->>Provider: keep first message + newest messages
Provider->>API: POST /chat/completions with bounded messages
API-->>Provider: completion + usage
Provider-->>Worker: ProviderQueryResult
Note over Worker,Provider: Full in-memory conversationHistory remains unchanged
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant Worker as Worker session
participant Provider as OpenRouterProvider
participant Settings as SettingsDefaultsManager
participant API as OpenRouter API

Worker->>Provider: query(conversationHistory)
Provider->>Settings: loadFromFile(USER_SETTINGS_PATH)
Settings-->>Provider: max context setting/default
Provider->>Provider: clamp setting to 2..200
Provider->>Provider: keep first message + newest messages
Provider->>API: POST /chat/completions with bounded messages
API-->>Provider: completion + usage
Provider-->>Worker: ProviderQueryResult
Note over Worker,Provider: Full in-memory conversationHistory remains unchanged
Loading

Reviews (1): Last reviewed commit: "fix: cap OpenRouter context history" | Re-trigger Greptile

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.

1 participant