-
Notifications
You must be signed in to change notification settings - Fork 710
fix(agent): isolate workspace baseline from trusted system prompt #2049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8ca5a39
6be16a8
f1c62e7
5c6b169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Isolate workspace-supplied baseline context (AGENTS.md, directory listings, skill listings, session time) in request-time user fragments with untrusted envelopes so it cannot live in the trusted system prompt or override host rules. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,8 @@ import { IWireService } from '#/wire/wire'; | |
| import type { PayloadOf } from '#/wire/types'; | ||
| import { IEventBus } from '#/app/event/eventBus'; | ||
| import { prepareSystemPromptContext } from './context'; | ||
| import { baselineMessagesForContext, skillActiveFor } from '#/app/agentProfileCatalog/profile-shared'; | ||
| import type { Message } from '#/kosong/contract/message'; | ||
| import type { | ||
| ApplyProfileOptions, | ||
| BindAgentInput, | ||
|
|
@@ -156,6 +158,7 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| } | ||
|
|
||
| private activeProfile: ResolvedAgentProfile | undefined; | ||
| private baselineContextMessages: readonly Message[] = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in f1c62e7: |
||
|
|
||
| constructor( | ||
| @IWireService private readonly wire: IWireService, | ||
|
|
@@ -221,6 +224,7 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| applyBindingSnapshot(snapshot: ProfileBindingSnapshot): void { | ||
| this.activeProfile = undefined; | ||
| this.activeToolNamesOverlay = undefined; | ||
| this.baselineContextMessages = snapshot.baselineContextMessages ?? []; | ||
| this.wire.dispatch( | ||
| profileBind({ | ||
| cwd: snapshot.cwd, | ||
|
|
@@ -277,6 +281,9 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| const currentProfileName = this.profileName; | ||
| const systemPrompt = profile.systemPrompt(context); | ||
| this.activeProfile = profile; | ||
| this.baselineContextMessages = baselineMessagesForContext(context, { | ||
| skillActive: skillActiveFor(profile.tools ?? []), | ||
| }); | ||
| this.cacheAgentsMdWarning(context); | ||
|
|
||
| const thinkingLevel = this.resolveThinkingEffort( | ||
|
|
@@ -359,6 +366,9 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
|
|
||
| useProfile(profile: ResolvedAgentProfile, context: SystemPromptContext): void { | ||
| this.activeProfile = profile; | ||
| this.baselineContextMessages = baselineMessagesForContext(context, { | ||
| skillActive: skillActiveFor(profile.tools ?? []), | ||
| }); | ||
| this.update({ | ||
| profileName: profile.name, | ||
| systemPrompt: profile.systemPrompt(context), | ||
|
|
@@ -391,6 +401,9 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| return; | ||
| } | ||
| this.activeProfile = profile; | ||
| this.baselineContextMessages = baselineMessagesForContext(context, { | ||
| skillActive: skillActiveFor(profile.tools ?? []), | ||
| }); | ||
| this.update({ | ||
| profileName: profile.name, | ||
| systemPrompt: profile.systemPrompt(context), | ||
|
|
@@ -416,6 +429,7 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| disallowedTools: [...(this.profileState.disallowedTools ?? [])], | ||
| subagents: | ||
| this.profileState.subagents === undefined ? undefined : [...this.profileState.subagents], | ||
| baselineContextMessages: this.baselineContextMessages, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -484,6 +498,10 @@ export class AgentProfileService extends Disposable implements IAgentProfileServ | |
| return this.systemPrompt; | ||
| } | ||
|
|
||
| getBaselineContextMessages(): readonly Message[] { | ||
| return this.baselineContextMessages; | ||
| } | ||
|
|
||
| getActiveToolNames(): readonly string[] | undefined { | ||
| return this.activeToolNames; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever a v2 turn has any baseline (the new builder normally emits the time fringe), this prepends extra user messages to
request.messages. Later the same service callsthis.contextSize.measured(request.messages, [message], usage), andAgentContextSizeService.measuredreturns early unless the input array exactly matchesIAgentContextMemoryService.get(). As a result normal v2 turns stop recording provider token usage, leaving status, budget sizing, and compaction decisions on stale estimates. Pass only the real history into context-size accounting, or teach it to ignore the request-only baseline while still sending the prefixed messages to the model.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f1c62e7:
contextSize.measurednow receiveshistoryMessages(conversation only). Request body still prepends baseline for the model.