Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(agent): the editor kept telling the model things it never heard

yhh7d4759dda664
diff --git a/desktop/electron/agent/openai.ts b/desktop/electron/agent/openai.ts
index 7f6628c14..31022c1b0 100644
--- a/desktop/electron/agent/openai.ts
+++ b/desktop/electron/agent/openai.ts
@@ -194,6 +194,16 @@ export function answerOrphanedCalls(messages: Message[]): number {
return added;
}
+/**
+ * Append editor context — a diagnostics feed, a nudge, a verdict's failures —
+ * as a USER turn. A mid-conversation `system` message is legal in this protocol
+ * and is not carried by every endpoint that speaks it: one delivered the turn
+ * with no content at all, and the model answered that the message was empty.
+ */
+export function pushContextMessage(messages: Message[], text: string): void {
+ messages.push({ role: 'user', content: text });
+}
+
const SHAPE: HistoryShape<Message> = {
askedIn: (message) => {
const content = message?.content;
@@ -303,18 +313,9 @@ class OpenAISession implements AgentSession {
});
}
- /**
- * Land buffered context after the history rather than by rewriting the system
- * prompt. The prompt is the first message of every request, so editing it per
- * turn would re-bill the whole conversation wherever caching is on.
- *
- * A `system` message mid-conversation is legal here — unlike the Anthropic
- * provider's compatible dialect, which has only two roles to work with and has
- * to smuggle the text onto the user turn.
- */
private flushContext(): void {
if (this.pending.length === 0) return;
- this.messages.push({ role: 'system', content: this.pending.join('\n\n') });
+ pushContextMessage(this.messages, this.pending.join('\n\n'));
this.pending.length = 0;
}
diff --git a/desktop/tests/agent-openai.test.ts b/desktop/tests/agent-openai.test.ts
index 83a949ace..57586a569 100644
--- a/desktop/tests/agent-openai.test.ts
+++ b/desktop/tests/agent-openai.test.ts
@@ -11,6 +11,7 @@
import { describe, it, expect } from 'vitest';
import {
buildChatRequest, answerOrphanedCalls, createOpenAIProvider, OPENAI_FORMAT,
+ pushContextMessage,
} from '../electron/agent/openai';
import { ANTHROPIC_FORMAT } from '../electron/agent/anthropic';
import type { CatalogTool } from '../electron/agent/types';
@@ -70,6 +71,23 @@ describe('the Chat Completions request', () => {
* refuses again on every message after it, with nothing on screen tying the
* failure to the Stop that caused it.
*/
+describe('editor context on its way to the model', () => {
+ it('lands as a user turn', () => {
+ const messages: Msg[] = [{ role: 'user', content: 'go' }];
+ pushContextMessage(messages, 'the editor flags 2 problems');
+ expect(messages[1]).toEqual({ role: 'user', content: 'the editor flags 2 problems' });
+ });
+
+ // A mid-conversation `system` message is legal here and is not carried by
+ // every endpoint speaking this protocol: one delivered the turn empty, taking
+ // the verdict's failures, the diagnostics feed and every nudge with it.
+ it('never rides a mid-conversation system message', () => {
+ const messages: Msg[] = [{ role: 'user', content: 'go' }];
+ pushContextMessage(messages, 'fix these before reporting the work as done');
+ expect(messages.filter((m) => m.role === 'system')).toEqual([]);
+ });
+});
+
describe('a history left half-answered by a Stop', () => {
const asked = (...ids: string[]): Msg => ({
role: 'assistant',