fix(opencode): inject agentmemory context on every turn, not just turn 1 - #1325
fix(opencode): inject agentmemory context on every turn, not just turn 1#1325Chewji9875 wants to merge 2 commits into
Conversation
OpenCode builds a fresh output.system array on each LLM step. The old one-time gate (contextInjectedSessions.add(sid) + startContextCache delete after first use) meant only step 0 of turn 1 received <agentmemory-instructions>/<agentmemory-context>; every subsequent step/turn in the session lost memory context entirely. Live call logs showed sysCtx=false on 100% of requests after the first. - skip internal requests via (input as any)?.agent === 'title' | 'compaction' or input?.small === true (robust; keeps brittle regex as fallback) - push AGENTMEMORY_INSTRUCTIONS + cached startContext on EVERY regular chat step; identical bytes per turn → prefix cache 100% preserved (rohitg00#720) - keep volatile file enrichment in messages.transform (message-tail)
|
@Chewji9875 is attempting to deploy a commit to the rohitg00's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe OpenCode system transform now injects memory instructions and start context on every regular chat step. It skips internal utility requests and title-generation prompts. Tests cover cache reuse and resumed-session refetch. ChangesOpenCode context injection
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to OpenCode chat steps now consistently receive cached memory context while internal title, compaction, and small-model requests remain unchanged. The covered behavior is ready to merge. Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant OpenCodeChat
participant TransformHook
participant StartContextSource
OpenCodeChat->>TransformHook: system transform request
alt Internal utility request or title-generation prompt
TransformHook-->>OpenCodeChat: return without injection
else Regular chat step
TransformHook->>StartContextSource: read cached or fresh start context
StartContextSource-->>TransformHook: start context
TransformHook-->>OpenCodeChat: memory instructions and start context
end
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…te, and add multi-turn tests - Remove unused contextInjectedSessions dead state from module and cleanup hooks - Typecast transform input via OpenCodeChatTransformInput interface with justification comment - Replace raw any in /context response parsing with OpenCodeContextResponse interface - Remove redundant consecutive Array.isArray(output.system) guards - Add comprehensive multi-turn behavioral test cases verifying context persistence across turns, prefix cache preservation (rohitg00#720), and internal agent skipping (rohitg00#1184) Signed-off-by: Choti Wongbussakorn <126886556+Chewji9875@users.noreply.github.com>
Problem
OpenCode's
<agentmemory-context>injection only lands on turn 1 step 0; every subsequent step/turn in the session silently loses memory context.Live call-log analysis across all sessions (OmniRoute gateway):
sysInst=true | sysCtx=truesysInst=false | sysCtx=falseRoot cause: OpenCode builds a fresh
output.systemarray on every LLM step. The plugin gated injection oncontextInjectedSessions.has(sid)+startContextCache.delete(sid)after first use — so step 0 consumes the injection and all later turns get nothing.Fix (
plugin/opencode/agentmemory-capture.ts)(input as any)?.agent === "title" | "compaction"andinput?.small === true, keeping the brittle title-regex as fallback. (Supersedes the fragile string-match-only guard.)AGENTMEMORY_INSTRUCTIONS+ cachedstartContext(fall back to/contextand re-cache, don't delete). Identical bytes per turn → prefix cache 100% preserved (Token input cache rate dropped ~10pp due to per-turn system prompt mutation #720).messages.transform(message tail) — never touchesoutput.system.Validation
Live E2E (fresh session,
opencode run):inst=true | ctx=trueinst=true | ctx=true(previously impossible)cache.read=60672— prefix cache hit confirmednpm test: 159 files, 1,718 tests pass.Summary by CodeRabbit