Skip to content

refactor: move dynamic editor context from system prompt to user message - #6225

Merged
markijbema merged 12 commits into
mainfrom
mark/move-dynamic-context-to-user-message
Mar 19, 2026
Merged

refactor: move dynamic editor context from system prompt to user message#6225
markijbema merged 12 commits into
mainfrom
mark/move-dynamic-context-to-user-message

Conversation

@markijbema

Copy link
Copy Markdown
Contributor

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)

Context Why static
Today's date (toDateString()) Only changes once per day
User timezone Rarely changes
Default shell Rarely changes

Dynamic → User message (per-turn)

Context Why dynamic
Active file Changes as user switches editors
Visible files Changes as user opens/closes files
Open tabs Changes as user opens/closes tabs

Dynamic context is injected as a synthetic <environment_details> text part on each user message, so the model always has fresh context.

Files changed

@markijbema
markijbema force-pushed the mark/move-dynamic-context-to-user-message branch from d88a6c8 to 2d8d49b Compare February 26, 2026 13:19
Mark IJbema 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
markijbema force-pushed the mark/move-dynamic-context-to-user-message branch from 2d8d49b to f46b733 Compare March 17, 2026 14:01
@markijbema
markijbema marked this pull request as ready for review March 17, 2026 14:02
Comment thread packages/opencode/src/session/prompt.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
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:

File Line Issue
packages/opencode/src/kilocode/editor-context.ts 13 Removing editorContext.timezone drops explicit user timezone context and changes the generated SDK/OpenAPI request schema, which can break typed callers that still send that field.

Fix these issues in Kilo Cloud

Files Reviewed (1 files)
  • packages/opencode/src/session/prompt.ts - 0 issues

Reviewed by gpt-5.4-20260305 · 167,687 tokens

Comment thread packages/opencode/src/kilocode/editor-context.ts
Comment thread packages/opencode/src/session/prompt.ts Outdated
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.
Comment thread packages/opencode/src/session/prompt.ts
…-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.
Comment thread packages/opencode/src/session/prompt.ts
Mark IJbema 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.
Comment thread packages/opencode/src/session/prompt.ts Outdated
Comment thread packages/opencode/src/session/system.ts Outdated
// 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you verify including this in the system prompt is beneficial?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not

Mark IJbema and others added 2 commits March 18, 2026 12:12
Co-authored-by: Christiaan Arnoldus <christiaan.arnoldus@outlook.com>
The injected TextPart for environment details was missing id, sessionID,
and messageID fields required by the MessageV2.TextPart type.
@markijbema
markijbema enabled auto-merge March 19, 2026 11:03
@markijbema
markijbema merged commit b1c91bc into main Mar 19, 2026
13 checks passed
@markijbema
markijbema deleted the mark/move-dynamic-context-to-user-message branch March 19, 2026 11:04
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
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