Skip to content

fix(gradient): persist forceMinLayer and auto-recover from context overflow#21

Merged
BYK merged 2 commits intomainfrom
fix/overflow-auto-recovery
Feb 28, 2026
Merged

fix(gradient): persist forceMinLayer and auto-recover from context overflow#21
BYK merged 2 commits intomainfrom
fix/overflow-auto-recovery

Conversation

@BYK
Copy link
Owner

@BYK BYK commented Feb 28, 2026

Problem

Two failure modes made context overflow errors unrecoverable without user action:

  1. forceMinLayer lost on restart — when session.error fires with "prompt too long", the handler sets forceMinLayer=2 in-memory. If OpenCode restarts before the next turn, the escalation is lost and the overflow repeats.
  2. User must intervene — after overflow, the user either tries /compact (which bypasses the gradient transform and overflows again) or assumes the session is dead.

Changes

Persist forceMinLayer across restarts (db.ts, gradient.ts)

New DB migration (v4) adds a session_state table. 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.error fires with a context overflow, lore now:

  1. Sets forceMinLayer=2 (persisted)
  2. Force-distills all undistilled messages
  3. Injects a synthetic recovery message via session.prompt() — this goes through the normal chat path, the gradient transform fires with forceMinLayer=2, messages are compressed, and the model continues where it left off

If the session.prompt() call fails, the persisted forceMinLayer still ensures the user's next message recovers correctly.

Testability

isContextOverflow() and buildRecoveryMessage() extracted as exported functions and covered by unit tests.

Tests

16 new tests across test/db.test.ts, test/gradient.test.ts, and test/markdown.test.ts covering DB persistence roundtrip, cold-start load, one-shot consumption, all overflow error variants, and recovery message construction.

BYK added 2 commits February 28, 2026 21:06
…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.
@BYK BYK marked this pull request as ready for review February 28, 2026 21:09
@BYK BYK merged commit e023b45 into main Feb 28, 2026
1 check passed
@BYK BYK deleted the fix/overflow-auto-recovery branch February 28, 2026 21:09
@craft-deployer craft-deployer bot mentioned this pull request Feb 28, 2026
2 tasks
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