Skip to content

refactor: Tiered Context Management for Long Running Coding Sessions#21124

Closed
czha168 wants to merge 2 commits into
anomalyco:devfrom
czha168:dev
Closed

refactor: Tiered Context Management for Long Running Coding Sessions#21124
czha168 wants to merge 2 commits into
anomalyco:devfrom
czha168:dev

Conversation

@czha168
Copy link
Copy Markdown

@czha168 czha168 commented Apr 5, 2026

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

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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

  • I have tested my changes locally. Yes, I used it personally. And I was very pleased. That's why I decided to contribute.
  • I have not included unrelated changes in this PR

czha168 added 2 commits April 3, 2026 22:06
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
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

Hey! Your PR title Tiered Context Management for Long Running Coding Sessions doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 5, 2026
@czha168 czha168 changed the title Tiered Context Management for Long Running Coding Sessions refactor: Tiered Context Management for Long Running Coding Sessions Apr 5, 2026
@github-actions github-actions Bot removed needs:title needs:compliance This means the issue will auto-close after 2 hours. labels Apr 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@czha168
Copy link
Copy Markdown
Author

czha168 commented Apr 5, 2026

This is my first ever contribution on github. Let's hope it passes your review.

@rekram1-node
Copy link
Copy Markdown
Collaborator

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:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants