refactor: move dynamic editor context from system prompt to user message - #6225
Merged
Conversation
markijbema
force-pushed
the
mark/move-dynamic-context-to-user-message
branch
from
February 26, 2026 13:19
d88a6c8 to
2d8d49b
Compare
added 2 commits
March 17, 2026 15:00
Split editor context into static and dynamic parts: - Static (system prompt, cached): date, timezone, shell These rarely change and benefit from prompt caching. - Dynamic (user message, per-turn): active file, visible files, open tabs These change frequently as the user switches files/tabs. Injected as a synthetic <environment_details> block on each user message so the model always has fresh context. This matches the pattern used by the VS Code extension, where getEnvironmentDetails() is called before every agent turn.
The model didn't know to look for or use the <environment_details> block injected into user messages. Add a static instruction in the system prompt explaining it, similar to how the legacy VS Code extension does it. This instruction is static text that never changes, so it caches well alongside the rest of the system prompt.
markijbema
force-pushed
the
mark/move-dynamic-context-to-user-message
branch
from
March 17, 2026 14:01
2d8d49b to
f46b733
Compare
markijbema
marked this pull request as ready for review
March 17, 2026 14:02
Contributor
Code Review SummaryStatus: 1 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)No new issues found in the incremental diff. Other Observations (not in diff)Issues found in unchanged code that cannot receive inline comments:
Files Reviewed (1 files)
Reviewed by gpt-5.4-20260305 · 167,687 tokens |
Compute environmentDetails() once per turn and reuse across tool-loop steps so the last user message stays byte-identical, preserving prompt cache hits on subsequent steps.
…-loop Key the cached envBlock by lastUser.id so that when a new user message arrives while the tool loop is still running, environment details are recomputed with fresh editor context instead of reusing stale data.
added 2 commits
March 17, 2026 18:39
Copy editorContext from the original user message onto the synthetic summary message created after task.command, so environment_details are not lost when lastUser switches to the synthetic message.
…tails Replace parts.push() with a shallow copy of the message object so the original message from storage is never mutated. This prevents duplicate environment_details blocks from accumulating across tool-loop iterations.
chrarnoldus
reviewed
Mar 18, 2026
chrarnoldus
reviewed
Mar 18, 2026
| // kilocode_change start | ||
| ``, | ||
| `At the end of each user message, you may receive <environment_details> with information about the user's currently active file, visible editors, and open tabs. This is auto-generated context — use it to understand which files the user is working with.`, | ||
| // kilocode_change end |
Collaborator
There was a problem hiding this comment.
How did you verify including this in the system prompt is beneficial?
Co-authored-by: Christiaan Arnoldus <christiaan.arnoldus@outlook.com>
chrarnoldus
approved these changes
Mar 19, 2026
The injected TextPart for environment details was missing id, sessionID, and messageID fields required by the MessageV2.TextPart type.
markijbema
enabled auto-merge
March 19, 2026 11:03
kilo-code-bot Bot
added a commit
that referenced
this pull request
Mar 20, 2026
Analyzes recent PRs merged March 13-20 that could cause the CLI to lose conversation history mid-session. Identifies PR #6225 (move dynamic editor context to user message) as the most likely culprit due to token inflation from environment_details injection triggering premature context compaction.
jliounis
pushed a commit
to jliounis/kilocode
that referenced
this pull request
May 18, 2026
…ext-to-user-message refactor: move dynamic editor context from system prompt to user message
t7tran
pushed a commit
to t7tran/kilocode
that referenced
this pull request
Aug 14, 2026
…ext-to-user-message refactor: move dynamic editor context from system prompt to user message
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.
Motivation
PR #6151 added editor context (active file, visible files, open tabs, shell, timezone) to the system prompt
<env>block. However, the system prompt is built once per session and cached — putting dynamic information like active file and open tabs there means the model sees stale data as the session progresses.This matches the pattern used by the legacy VS Code extension, where
getEnvironmentDetails()is called before every agent turn.Changes
Split editor context into static and dynamic parts:
Static → System prompt (cached)
toDateString())Dynamic → User message (per-turn)
Dynamic context is injected as a synthetic
<environment_details>text part on each user message, so the model always has fresh context.Files changed
packages/opencode/src/kilocode/editor-context.ts— SpliteditorContextEnvLines()intostaticEnvLines()+environmentDetails()packages/opencode/src/session/system.ts— UsestaticEnvLinesfor system promptpackages/opencode/src/session/prompt.ts— InjectenvironmentDetails()as synthetic part on user message