fix(gradient): persist forceMinLayer and auto-recover from context overflow#21
Merged
fix(gradient): persist forceMinLayer and auto-recover from context overflow#21
Conversation
…erflow - Add session_state table (DB migration v4) to persist forceMinLayer across process restarts. Previously a 'prompt too long' error recovery was lost if OpenCode restarted before the next turn, causing the overflow to repeat. - setForceMinLayer() now writes to SQLite; transform() clears the row on consumption (one-shot); getSessionState() loads from DB on cold start. - session.error handler now auto-recovers without user intervention: sets forceMinLayer=2, force-distills, then injects a synthetic recovery message via session.prompt() so the gradient transform fires and compresses the context before the model replies. - Extract isContextOverflow() and buildRecoveryMessage() as testable functions. - Add 16 new tests covering DB persistence roundtrip, cold-start load, one-shot consumption, overflow detection, and recovery message construction.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Two failure modes made context overflow errors unrecoverable without user action:
session.errorfires with "prompt too long", the handler setsforceMinLayer=2in-memory. If OpenCode restarts before the next turn, the escalation is lost and the overflow repeats./compact(which bypasses the gradient transform and overflows again) or assumes the session is dead.Changes
Persist
forceMinLayeracross restarts (db.ts, gradient.ts)New DB migration (v4) adds a
session_statetable.setForceMinLayer()now writes to SQLite;transform()clears the row on consumption (one-shot);getSessionState()loads from DB on cold start so error recovery survives process restarts.Auto-recovery without user intervention (index.ts)
When
session.errorfires with a context overflow, lore now:forceMinLayer=2(persisted)session.prompt()— this goes through the normal chat path, the gradient transform fires withforceMinLayer=2, messages are compressed, and the model continues where it left offIf the
session.prompt()call fails, the persistedforceMinLayerstill ensures the user's next message recovers correctly.Testability
isContextOverflow()andbuildRecoveryMessage()extracted as exported functions and covered by unit tests.Tests
16 new tests across
test/db.test.ts,test/gradient.test.ts, andtest/markdown.test.tscovering DB persistence roundtrip, cold-start load, one-shot consumption, all overflow error variants, and recovery message construction.