Skip to content

fix: resolve the model context limit before injecting nudges on a fresh process - #670

Open
plfavreau wants to merge 1 commit into
Tarquinen:masterfrom
plfavreau:fix/nudge-before-model-limit
Open

plfavreau wants to merge 1 commit into
Tarquinen:masterfrom
plfavreau:fix/nudge-before-model-limit

Conversation

@plfavreau

Copy link
Copy Markdown

Problem

On OpenCode V1, experimental.chat.messages.transform runs before experimental.chat.system.transform. The system hook is where DCP caches modelContextLimit, so on the first request of a fresh OpenCode process the limit is unknown. With percentage thresholds (minContextLimit: "55%"), isContextOverLimits then treats the context as over the limit and injects turn nudges, far below the real threshold. The next request knows the limit, clears the nudges, and the edited history misses the prompt cache.

Every OpenCode restart therefore costs one full rewrite of each resumed session's history.

Fix

If the limit is still unknown when messages are transformed, read it once from client.config.providers() for the model of the last user message. No change when the limit is already known.

Proof

OpenCode 1.19.5, Anthropic Sonnet, real API. Three turns of ~13k tokens, restart the server, three more turns. Usage from the API response:

Request DCP 3.2.0 This PR
1st after restart 3 nudges injected, read 0, write 89.6k 0 nudges, read 0, write 89.0k
2nd after restart read 50.1k, write 39.0k read 89.0k, write 66

(The first request after a restart misses in both cases: OpenCode's own system prompt changes on restart.)

Regression test added in tests/hooks-permission.test.ts; it fails on master. npm test, typecheck, check:package and format:check pass.

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