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): a verdict's failures were held back for want of a turn to sit on

yhh15793a7d3c11
diff --git a/desktop/electron/agent/anthropic.ts b/desktop/electron/agent/anthropic.ts
index 6bf5cb3c5..6634e3fff 100644
--- a/desktop/electron/agent/anthropic.ts
+++ b/desktop/electron/agent/anthropic.ts
@@ -83,6 +83,28 @@ interface StepRequest {
* Anthropic-only field to a gateway fails in the two ways that are hardest to
* notice: silently ignored, or billed differently than the code says.
*/
+/**
+ * Put editor context — a diagnostics feed, a nudge, a verdict's failures — where
+ * the model will read it. After a user turn it rides that turn (or, on
+ * Anthropic's own dialect, the operator channel beside it); after the model's
+ * own answer, which is where a VERDICT speaks, it takes a user turn of its own.
+ */
+export function placeContext(messages: Message[], text: string, dialect: Dialect): void {
+ const last = messages[messages.length - 1];
+ if (last?.role !== 'user') {
+ messages.push({ role: 'user', content: [{ type: 'text', text }] });
+ return;
+ }
+ if (dialect === 'anthropic') {
+ messages.push({ role: 'system', content: text } as unknown as Message);
+ return;
+ }
+ const blocks = typeof last.content === 'string'
+ ? [{ type: 'text' as const, text: last.content }]
+ : [...last.content];
+ last.content = [...blocks, { type: 'text' as const, text }];
+}
+
export function buildStepRequest(opts: {
dialect: Dialect;
model: string;
@@ -297,25 +319,14 @@ class AnthropicSession implements AgentSession {
* core format, still after the cached prefix, but it IS user-role text and
* carries no more authority than the rest of that turn.
*
- * Placement is constrained either way: it must follow a user turn. That holds
- * here — the kernel always appends the user turn or a tool-result turn before
- * stepping — but hold the text rather than emitting an invalid message if
- * that ever stops being true.
+ * Placement is constrained either way: it must follow a user turn. The turn a
+ * verdict speaks in does not have one — the model has just stopped, so the
+ * history ends on its answer — and a user turn of our own is what carries the
+ * text there rather than losing it.
*/
private flushContext(): void {
if (this.pending.length === 0) return;
- const last = this.messages[this.messages.length - 1];
- if (!last || last.role !== 'user') return;
- const text = this.pending.join('\n\n');
-
- if (this.opts.dialect === 'anthropic') {
- this.messages.push({ role: 'system', content: text });
- } else {
- const blocks = typeof last.content === 'string'
- ? [{ type: 'text' as const, text: last.content }]
- : [...last.content];
- last.content = [...blocks, { type: 'text' as const, text }];
- }
+ placeContext(this.messages, this.pending.join('\n\n'), this.opts.dialect);
this.pending.length = 0;
}
diff --git a/desktop/tests/agent-dialect.test.ts b/desktop/tests/agent-dialect.test.ts
index 0c6f9a8fa..a336be860 100644
--- a/desktop/tests/agent-dialect.test.ts
+++ b/desktop/tests/agent-dialect.test.ts
@@ -14,8 +14,10 @@
*/
import { describe, it, expect } from 'vitest';
import {
- buildStepRequest, createAnthropicProvider, toolResultContent,
+ buildStepRequest, createAnthropicProvider, toolResultContent, placeContext,
} from '../electron/agent/anthropic';
+
+type Msg = Parameters<typeof placeContext>[0][number];
import { describeApiError } from '../electron/agent/apiError';
import { DEFAULT_MODEL } from '../src/settings/agentIds';
import type { CatalogTool } from '../electron/agent/types';
@@ -124,11 +126,16 @@ describe('where per-turn editor context lands', () => {
]);
});
- it('holds the text rather than emitting a message with nothing to attach to', () => {
+ // A VERDICT speaks where there is no user turn to attach to: the model has
+ // stopped and the history ends on its answer, so the failures it is being
+ // asked to fix need a turn of their own.
+ it('speaks on its own turn when there is nothing to attach to', () => {
const session = createAnthropicProvider({ apiKey: 'k' }).createSession({ system: 's', tools: TOOLS });
session.pushContext('orphan');
(session as unknown as { flushContext(): void }).flushContext();
- expect((session as unknown as { messages: unknown[] }).messages).toHaveLength(0);
+ const messages = (session as unknown as { messages: { role: string }[] }).messages;
+ expect(messages).toHaveLength(1);
+ expect(messages[0]).toMatchObject({ role: 'user' });
});
});
@@ -302,3 +309,36 @@ describe('an image on the person\'s turn', () => {
expect(msg.content).toBe('like this');
});
});
+
+/**
+ * Where the editor's own words go. They must follow a user turn, and the turn a
+ * VERDICT speaks in has none — the model has just answered — so context with
+ * nothing to attach to takes a user turn of its own rather than going nowhere.
+ */
+describe('editor context, placed', () => {
+ const user = (text: string): Msg => ({ role: 'user', content: text });
+ const assistant = (text: string): Msg => ({ role: 'assistant', content: text });
+ const roles = (m: Msg[]) => m.map((x) => x.role);
+
+ it('takes a user turn of its own after the model has answered', () => {
+ for (const dialect of ['anthropic', 'compatible'] as const) {
+ const messages: Msg[] = [user('go'), assistant('done!')];
+ placeContext(messages, 'two of the things this work has to do are not holding', dialect);
+ expect(roles(messages)).toEqual(['user', 'assistant', 'user']);
+ expect(JSON.stringify(messages[2])).toContain('not holding');
+ }
+ });
+
+ it('rides the user turn a gateway can carry, rather than a role it cannot', () => {
+ const messages: Msg[] = [user('go')];
+ placeContext(messages, 'the editor flags 2 problems', 'compatible');
+ expect(roles(messages)).toEqual(['user']);
+ expect(JSON.stringify(messages[0])).toContain('flags 2 problems');
+ });
+
+ it('uses the operator channel where there is one', () => {
+ const messages: Msg[] = [user('go')];
+ placeContext(messages, 'the editor flags 2 problems', 'anthropic');
+ expect(roles(messages)).toEqual(['user', 'system']);
+ });
+});