refactor: Tiered Context Management for Long Running Coding Sessions#21124
refactor: Tiered Context Management for Long Running Coding Sessions#21124czha168 wants to merge 2 commits into
Conversation
Add structured memory extraction, storage, and retrieval pipeline to prevent context loss during compaction and across sessions. Includes database tables (fact, window, artifact, project) with FTS5 search, a memory agent for structured JSON extraction, and Effect-based services for extraction, retrieval, injection, and project tracking.
…emory - Fix extractor.ts: LLM.stream returns Promise, access .text after resolution - Fix prompt.ts: circular dependency via lazy Layer.unwrap + split Layer.provide chains - Fix compaction.ts: create synthetic user continuation message when extraction falls back to agent - Add MemoryExtractor stubs to test memory compositions - Provide memory stubs to SessionPrompt.layer in tests - Migrate store.ts raw SQL to Drizzle sql tagged templates
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
This is my first ever contribution on github. Let's hope it passes your review. |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
I personally suffered from context rot. So here is the fix!
With this change, opencode can largely run autonomously for long period without suffering from context rot. No more spinning wheel for nothing and no more repetitive loop that leads to nowhere.
Oh, btw, opencode also run faster because of lightweight context. And it saves 30% on tokens too.
Type of change
What does this PR do?
After a few hours of running opencode, it suffers from context rot. What I observed is opencode just spinning to nowhere, either with any response message or repetitive babbles that makes no sense, apparently brain dead.
The alternative to my change is to manually suspend opencode, /clear the context, and restart. And repeat a couple of hours later. Good bio breaks. But annoying still.
So, I implemented tiered memory for context management. Now, context rarely get filled over 80%.
With condensed context, opencode runs faster! A surprise for me. And of course it saves on tokens.
This clearly is not AI talking. This is a battle scared engineer and tenured architect typing with flesh hand.
If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!
How did you verify your code works?
I have added test cases. It clearly shows context memory never exceeds 80%. From what I can gather, it generally condense memory by 30%.
Screenshots / recordings
Find a big project that will run overnight. Or better yet, runs for days. See how much this fix helps, yourself.
Checklist